Whamcloud - gitweb
LU-13099 lmv: disable statahead for remote objects
[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_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
35 LUSTRE=${LUSTRE:-$(dirname $0)/..}
36 . $LUSTRE/tests/test-framework.sh
37 init_test_env $@
38
39 init_logging
40
41 ALWAYS_EXCEPT="$SANITY_EXCEPT "
42 # bug number for skipped test: LU-9693 LU-6493 LU-9693
43 ALWAYS_EXCEPT+="               42a     42b     42c "
44 # bug number:    LU-8411 LU-9054
45 ALWAYS_EXCEPT+=" 407     312 "
46
47 if $SHARED_KEY; then
48         # bug number:    LU-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
72 #                                  5          12          (min)"
73 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 135 136 300o"
74
75 if [ "$mds1_FSTYPE" = "zfs" ]; then
76         # bug number for skipped test:
77         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  "
78         #                                               13    (min)"
79         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
80 fi
81
82 # Get the SLES distro version
83 #
84 # Returns a version string that should only be used in comparing
85 # strings returned by version_code()
86 sles_version_code()
87 {
88         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
89
90         # All SuSE Linux versions have one decimal. version_code expects two
91         local sles_version=$version.0
92         version_code $sles_version
93 }
94
95 # Check if we are running on Ubuntu or SLES so we can make decisions on
96 # what tests to run
97 if [ -r /etc/SuSE-release ]; then
98         sles_version=$(sles_version_code)
99         [ $sles_version -lt $(version_code 11.4.0) ] &&
100                 # bug number for skipped test: LU-4341
101                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
102         [ $sles_version -lt $(version_code 12.0.0) ] &&
103                 # bug number for skipped test: LU-3703
104                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
105 elif [ -r /etc/os-release ]; then
106         if grep -qi ubuntu /etc/os-release; then
107                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
108                                                 -e 's/^VERSION=//p' \
109                                                 /etc/os-release |
110                                                 awk '{ print $1 }'))
111
112                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
113                         # bug number for skipped test:
114                         #                LU-10334 LU-10366
115                         ALWAYS_EXCEPT+=" 103a     410"
116                 fi
117         fi
118 fi
119
120 build_test_filter
121 FAIL_ON_ERROR=false
122
123 cleanup() {
124         echo -n "cln.."
125         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
126         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
127 }
128 setup() {
129         echo -n "mnt.."
130         load_modules
131         setupall || exit 10
132         echo "done"
133 }
134
135 check_swap_layouts_support()
136 {
137         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
138                 skip "Does not support layout lock."
139 }
140
141 check_and_setup_lustre
142 DIR=${DIR:-$MOUNT}
143 assert_DIR
144
145 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
146
147 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
148 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
149 rm -rf $DIR/[Rdfs][0-9]*
150
151 # $RUNAS_ID may get set incorrectly somewhere else
152 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
153         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
154
155 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
156
157 if [ "${ONLY}" = "MOUNT" ] ; then
158         echo "Lustre is up, please go on"
159         exit
160 fi
161
162 echo "preparing for tests involving mounts"
163 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
164 touch $EXT2_DEV
165 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
166 echo # add a newline after mke2fs.
167
168 umask 077
169
170 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
171 lctl set_param debug=-1 2> /dev/null || true
172 test_0a() {
173         touch $DIR/$tfile
174         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
175         rm $DIR/$tfile
176         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
177 }
178 run_test 0a "touch; rm ====================="
179
180 test_0b() {
181         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
182         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
183 }
184 run_test 0b "chmod 0755 $DIR ============================="
185
186 test_0c() {
187         $LCTL get_param mdc.*.import | grep "state: FULL" ||
188                 error "import not FULL"
189         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
190                 error "bad target"
191 }
192 run_test 0c "check import proc"
193
194 test_0d() { # LU-3397
195         [ $MGS_VERSION -lt $(version_code 2.10.57) ] &&
196                 skip "proc exports not supported before 2.10.57"
197
198         local mgs_exp="mgs.MGS.exports"
199         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
200         local exp_client_nid
201         local exp_client_version
202         local exp_val
203         local imp_val
204         local temp_imp=$DIR/$tfile.import
205         local temp_exp=$DIR/$tfile.export
206
207         # save mgc import file to $temp_imp
208         $LCTL get_param mgc.*.import | tee $temp_imp
209         # Check if client uuid is found in MGS export
210         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
211                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
212                         $client_uuid ] &&
213                         break;
214         done
215         # save mgs export file to $temp_exp
216         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
217
218         # Compare the value of field "connect_flags"
219         imp_val=$(grep "connect_flags" $temp_imp)
220         exp_val=$(grep "connect_flags" $temp_exp)
221         [ "$exp_val" == "$imp_val" ] ||
222                 error "export flags '$exp_val' != import flags '$imp_val'"
223
224         # Compare the value of client version
225         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
226         exp_val=$(version_code $exp_client_version)
227         imp_val=$CLIENT_VERSION
228         [ "$exp_val" == "$imp_val" ] ||
229                 error "export client version '$exp_val' != '$imp_val'"
230 }
231 run_test 0d "check export proc ============================="
232
233 test_1() {
234         test_mkdir $DIR/$tdir
235         test_mkdir $DIR/$tdir/d2
236         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
237         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
238         rmdir $DIR/$tdir/d2
239         rmdir $DIR/$tdir
240         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
241 }
242 run_test 1 "mkdir; remkdir; rmdir"
243
244 test_2() {
245         test_mkdir $DIR/$tdir
246         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
247         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
248         rm -r $DIR/$tdir
249         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
250 }
251 run_test 2 "mkdir; touch; rmdir; check file"
252
253 test_3() {
254         test_mkdir $DIR/$tdir
255         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
256         touch $DIR/$tdir/$tfile
257         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
258         rm -r $DIR/$tdir
259         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
260 }
261 run_test 3 "mkdir; touch; rmdir; check dir"
262
263 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
264 test_4() {
265         test_mkdir -i 1 $DIR/$tdir
266
267         touch $DIR/$tdir/$tfile ||
268                 error "Create file under remote directory failed"
269
270         rmdir $DIR/$tdir &&
271                 error "Expect error removing in-use dir $DIR/$tdir"
272
273         test -d $DIR/$tdir || error "Remote directory disappeared"
274
275         rm -rf $DIR/$tdir || error "remove remote dir error"
276 }
277 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
278
279 test_5() {
280         test_mkdir $DIR/$tdir
281         test_mkdir $DIR/$tdir/d2
282         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
283         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
284         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
285 }
286 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
287
288 test_6a() {
289         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
290         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
291         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
292                 error "$tfile does not have perm 0666 or UID $UID"
293         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
294         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
295                 error "$tfile should be 0666 and owned by UID $UID"
296 }
297 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
298
299 test_6c() {
300         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
301
302         touch $DIR/$tfile
303         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
304         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
305                 error "$tfile should be owned by UID $RUNAS_ID"
306         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
307         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
308                 error "$tfile should be owned by UID $RUNAS_ID"
309 }
310 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
311
312 test_6e() {
313         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
314
315         touch $DIR/$tfile
316         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
317         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
318                 error "$tfile should be owned by GID $UID"
319         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
320         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
321                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
322 }
323 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
324
325 test_6g() {
326         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
327
328         test_mkdir $DIR/$tdir
329         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
330         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
331         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
332         test_mkdir $DIR/$tdir/d/subdir
333         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
334                 error "$tdir/d/subdir should be GID $RUNAS_GID"
335         if [[ $MDSCOUNT -gt 1 ]]; then
336                 # check remote dir sgid inherite
337                 $LFS mkdir -i 0 $DIR/$tdir.local ||
338                         error "mkdir $tdir.local failed"
339                 chmod g+s $DIR/$tdir.local ||
340                         error "chmod $tdir.local failed"
341                 chgrp $RUNAS_GID $DIR/$tdir.local ||
342                         error "chgrp $tdir.local failed"
343                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
344                         error "mkdir $tdir.remote failed"
345                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
346                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
347                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
348                         error "$tdir.remote should be mode 02755"
349         fi
350 }
351 run_test 6g "verify new dir in sgid dir inherits group"
352
353 test_6h() { # bug 7331
354         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
355
356         touch $DIR/$tfile || error "touch failed"
357         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
358         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
359                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
360         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
361                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
362 }
363 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
364
365 test_7a() {
366         test_mkdir $DIR/$tdir
367         $MCREATE $DIR/$tdir/$tfile
368         chmod 0666 $DIR/$tdir/$tfile
369         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
370                 error "$tdir/$tfile should be mode 0666"
371 }
372 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
373
374 test_7b() {
375         if [ ! -d $DIR/$tdir ]; then
376                 test_mkdir $DIR/$tdir
377         fi
378         $MCREATE $DIR/$tdir/$tfile
379         echo -n foo > $DIR/$tdir/$tfile
380         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
381         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
382 }
383 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
384
385 test_8() {
386         test_mkdir $DIR/$tdir
387         touch $DIR/$tdir/$tfile
388         chmod 0666 $DIR/$tdir/$tfile
389         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
390                 error "$tfile mode not 0666"
391 }
392 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
393
394 test_9() {
395         test_mkdir $DIR/$tdir
396         test_mkdir $DIR/$tdir/d2
397         test_mkdir $DIR/$tdir/d2/d3
398         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
399 }
400 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
401
402 test_10() {
403         test_mkdir $DIR/$tdir
404         test_mkdir $DIR/$tdir/d2
405         touch $DIR/$tdir/d2/$tfile
406         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
407                 error "$tdir/d2/$tfile not a file"
408 }
409 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
410
411 test_11() {
412         test_mkdir $DIR/$tdir
413         test_mkdir $DIR/$tdir/d2
414         chmod 0666 $DIR/$tdir/d2
415         chmod 0705 $DIR/$tdir/d2
416         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
417                 error "$tdir/d2 mode not 0705"
418 }
419 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
420
421 test_12() {
422         test_mkdir $DIR/$tdir
423         touch $DIR/$tdir/$tfile
424         chmod 0666 $DIR/$tdir/$tfile
425         chmod 0654 $DIR/$tdir/$tfile
426         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
427                 error "$tdir/d2 mode not 0654"
428 }
429 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
430
431 test_13() {
432         test_mkdir $DIR/$tdir
433         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
434         >  $DIR/$tdir/$tfile
435         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
436                 error "$tdir/$tfile size not 0 after truncate"
437 }
438 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
439
440 test_14() {
441         test_mkdir $DIR/$tdir
442         touch $DIR/$tdir/$tfile
443         rm $DIR/$tdir/$tfile
444         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
445 }
446 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
447
448 test_15() {
449         test_mkdir $DIR/$tdir
450         touch $DIR/$tdir/$tfile
451         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
452         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
453                 error "$tdir/${tfile_2} not a file after rename"
454         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
455 }
456 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
457
458 test_16() {
459         test_mkdir $DIR/$tdir
460         touch $DIR/$tdir/$tfile
461         rm -rf $DIR/$tdir/$tfile
462         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
463 }
464 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
465
466 test_17a() {
467         test_mkdir $DIR/$tdir
468         touch $DIR/$tdir/$tfile
469         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
470         ls -l $DIR/$tdir
471         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
472                 error "$tdir/l-exist not a symlink"
473         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
474                 error "$tdir/l-exist not referencing a file"
475         rm -f $DIR/$tdir/l-exist
476         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
477 }
478 run_test 17a "symlinks: create, remove (real)"
479
480 test_17b() {
481         test_mkdir $DIR/$tdir
482         ln -s no-such-file $DIR/$tdir/l-dangle
483         ls -l $DIR/$tdir
484         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
485                 error "$tdir/l-dangle not referencing no-such-file"
486         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
487                 error "$tdir/l-dangle not referencing non-existent file"
488         rm -f $DIR/$tdir/l-dangle
489         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
490 }
491 run_test 17b "symlinks: create, remove (dangling)"
492
493 test_17c() { # bug 3440 - don't save failed open RPC for replay
494         test_mkdir $DIR/$tdir
495         ln -s foo $DIR/$tdir/$tfile
496         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
497 }
498 run_test 17c "symlinks: open dangling (should return error)"
499
500 test_17d() {
501         test_mkdir $DIR/$tdir
502         ln -s foo $DIR/$tdir/$tfile
503         touch $DIR/$tdir/$tfile || error "creating to new symlink"
504 }
505 run_test 17d "symlinks: create dangling"
506
507 test_17e() {
508         test_mkdir $DIR/$tdir
509         local foo=$DIR/$tdir/$tfile
510         ln -s $foo $foo || error "create symlink failed"
511         ls -l $foo || error "ls -l failed"
512         ls $foo && error "ls not failed" || true
513 }
514 run_test 17e "symlinks: create recursive symlink (should return error)"
515
516 test_17f() {
517         test_mkdir $DIR/$tdir
518         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
519         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
520         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
521         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
522         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
523         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
524         ls -l  $DIR/$tdir
525 }
526 run_test 17f "symlinks: long and very long symlink name"
527
528 # str_repeat(S, N) generate a string that is string S repeated N times
529 str_repeat() {
530         local s=$1
531         local n=$2
532         local ret=''
533         while [ $((n -= 1)) -ge 0 ]; do
534                 ret=$ret$s
535         done
536         echo $ret
537 }
538
539 # Long symlinks and LU-2241
540 test_17g() {
541         test_mkdir $DIR/$tdir
542         local TESTS="59 60 61 4094 4095"
543
544         # Fix for inode size boundary in 2.1.4
545         [ $MDS1_VERSION -lt $(version_code 2.1.4) ] &&
546                 TESTS="4094 4095"
547
548         # Patch not applied to 2.2 or 2.3 branches
549         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
550         [ $MDS1_VERSION -le $(version_code 2.3.55) ] &&
551                 TESTS="4094 4095"
552
553         # skip long symlink name for rhel6.5.
554         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
555         grep -q '6.5' /etc/redhat-release &>/dev/null &&
556                 TESTS="59 60 61 4062 4063"
557
558         for i in $TESTS; do
559                 local SYMNAME=$(str_repeat 'x' $i)
560                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
561                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
562         done
563 }
564 run_test 17g "symlinks: really long symlink name and inode boundaries"
565
566 test_17h() { #bug 17378
567         [ $PARALLEL == "yes" ] && skip "skip parallel run"
568         remote_mds_nodsh && skip "remote MDS with nodsh"
569
570         local mdt_idx
571
572         test_mkdir $DIR/$tdir
573         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
574         $LFS setstripe -c -1 $DIR/$tdir
575         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
576         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
577         touch $DIR/$tdir/$tfile || true
578 }
579 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
580
581 test_17i() { #bug 20018
582         [ $PARALLEL == "yes" ] && skip "skip parallel run"
583         remote_mds_nodsh && skip "remote MDS with nodsh"
584
585         local foo=$DIR/$tdir/$tfile
586         local mdt_idx
587
588         test_mkdir -c1 $DIR/$tdir
589         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
590         ln -s $foo $foo || error "create symlink failed"
591 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
592         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
593         ls -l $foo && error "error not detected"
594         return 0
595 }
596 run_test 17i "don't panic on short symlink (should return error)"
597
598 test_17k() { #bug 22301
599         [ $PARALLEL == "yes" ] && skip "skip parallel run"
600         [[ -z "$(which rsync 2>/dev/null)" ]] &&
601                 skip "no rsync command"
602         rsync --help | grep -q xattr ||
603                 skip_env "$(rsync --version | head -n1) does not support xattrs"
604         test_mkdir $DIR/$tdir
605         test_mkdir $DIR/$tdir.new
606         touch $DIR/$tdir/$tfile
607         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
608         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
609                 error "rsync failed with xattrs enabled"
610 }
611 run_test 17k "symlinks: rsync with xattrs enabled"
612
613 test_17l() { # LU-279
614         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
615                 skip "no getfattr command"
616
617         test_mkdir $DIR/$tdir
618         touch $DIR/$tdir/$tfile
619         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
620         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
621                 # -h to not follow symlinks. -m '' to list all the xattrs.
622                 # grep to remove first line: '# file: $path'.
623                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
624                 do
625                         lgetxattr_size_check $path $xattr ||
626                                 error "lgetxattr_size_check $path $xattr failed"
627                 done
628         done
629 }
630 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
631
632 # LU-1540
633 test_17m() {
634         [ $PARALLEL == "yes" ] && skip "skip parallel run"
635         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
636         remote_mds_nodsh && skip "remote MDS with nodsh"
637         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
638         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
639                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
640
641         local short_sym="0123456789"
642         local wdir=$DIR/$tdir
643         local i
644
645         test_mkdir $wdir
646         long_sym=$short_sym
647         # create a long symlink file
648         for ((i = 0; i < 4; ++i)); do
649                 long_sym=${long_sym}${long_sym}
650         done
651
652         echo "create 512 short and long symlink files under $wdir"
653         for ((i = 0; i < 256; ++i)); do
654                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
655                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
656         done
657
658         echo "erase them"
659         rm -f $wdir/*
660         sync
661         wait_delete_completed
662
663         echo "recreate the 512 symlink files with a shorter string"
664         for ((i = 0; i < 512; ++i)); do
665                 # rewrite the symlink file with a shorter string
666                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
667                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
668         done
669
670         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
671         local devname=$(mdsdevname $mds_index)
672
673         echo "stop and checking mds${mds_index}:"
674         # e2fsck should not return error
675         stop mds${mds_index}
676         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
677         rc=$?
678
679         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
680                 error "start mds${mds_index} failed"
681         df $MOUNT > /dev/null 2>&1
682         [ $rc -eq 0 ] ||
683                 error "e2fsck detected error for short/long symlink: rc=$rc"
684         rm -f $wdir/*
685 }
686 run_test 17m "run e2fsck against MDT which contains short/long symlink"
687
688 check_fs_consistency_17n() {
689         local mdt_index
690         local rc=0
691
692         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
693         # so it only check MDT1/MDT2 instead of all of MDTs.
694         for mdt_index in 1 2; do
695                 local devname=$(mdsdevname $mdt_index)
696                 # e2fsck should not return error
697                 stop mds${mdt_index}
698                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
699                         rc=$((rc + $?))
700
701                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
702                         error "mount mds$mdt_index failed"
703                 df $MOUNT > /dev/null 2>&1
704         done
705         return $rc
706 }
707
708 test_17n() {
709         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
710         [ $PARALLEL == "yes" ] && skip "skip parallel run"
711         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
712         remote_mds_nodsh && skip "remote MDS with nodsh"
713         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
714         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
715                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
716
717         local i
718
719         test_mkdir $DIR/$tdir
720         for ((i=0; i<10; i++)); do
721                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
722                         error "create remote dir error $i"
723                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
724                         error "create files under remote dir failed $i"
725         done
726
727         check_fs_consistency_17n ||
728                 error "e2fsck report error after create files under remote dir"
729
730         for ((i = 0; i < 10; i++)); do
731                 rm -rf $DIR/$tdir/remote_dir_${i} ||
732                         error "destroy remote dir error $i"
733         done
734
735         check_fs_consistency_17n ||
736                 error "e2fsck report error after unlink files under remote dir"
737
738         [ $MDS1_VERSION -lt $(version_code 2.4.50) ] &&
739                 skip "lustre < 2.4.50 does not support migrate mv"
740
741         for ((i = 0; i < 10; i++)); do
742                 mkdir -p $DIR/$tdir/remote_dir_${i}
743                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
744                         error "create files under remote dir failed $i"
745                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
746                         error "migrate remote dir error $i"
747         done
748         check_fs_consistency_17n || error "e2fsck report error after migration"
749
750         for ((i = 0; i < 10; i++)); do
751                 rm -rf $DIR/$tdir/remote_dir_${i} ||
752                         error "destroy remote dir error $i"
753         done
754
755         check_fs_consistency_17n || error "e2fsck report error after unlink"
756 }
757 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
758
759 test_17o() {
760         remote_mds_nodsh && skip "remote MDS with nodsh"
761         [ $MDS1_VERSION -lt $(version_code 2.3.64) ] &&
762                 skip "Need MDS version at least 2.3.64"
763
764         local wdir=$DIR/${tdir}o
765         local mdt_index
766         local rc=0
767
768         test_mkdir $wdir
769         touch $wdir/$tfile
770         mdt_index=$($LFS getstripe -m $wdir/$tfile)
771         mdt_index=$((mdt_index + 1))
772
773         cancel_lru_locks mdc
774         #fail mds will wait the failover finish then set
775         #following fail_loc to avoid interfer the recovery process.
776         fail mds${mdt_index}
777
778         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
779         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
780         ls -l $wdir/$tfile && rc=1
781         do_facet mds${mdt_index} lctl set_param fail_loc=0
782         [[ $rc -eq 0 ]] || error "stat file should fail"
783 }
784 run_test 17o "stat file with incompat LMA feature"
785
786 test_18() {
787         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
788         ls $DIR || error "Failed to ls $DIR: $?"
789 }
790 run_test 18 "touch .../f ; ls ... =============================="
791
792 test_19a() {
793         touch $DIR/$tfile
794         ls -l $DIR
795         rm $DIR/$tfile
796         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
797 }
798 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
799
800 test_19b() {
801         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
802 }
803 run_test 19b "ls -l .../f19 (should return error) =============="
804
805 test_19c() {
806         [ $RUNAS_ID -eq $UID ] &&
807                 skip_env "RUNAS_ID = UID = $UID -- skipping"
808
809         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
810 }
811 run_test 19c "$RUNAS touch .../f19 (should return error) =="
812
813 test_19d() {
814         cat $DIR/f19 && error || true
815 }
816 run_test 19d "cat .../f19 (should return error) =============="
817
818 test_20() {
819         touch $DIR/$tfile
820         rm $DIR/$tfile
821         touch $DIR/$tfile
822         rm $DIR/$tfile
823         touch $DIR/$tfile
824         rm $DIR/$tfile
825         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
826 }
827 run_test 20 "touch .../f ; ls -l ..."
828
829 test_21() {
830         test_mkdir $DIR/$tdir
831         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
832         ln -s dangle $DIR/$tdir/link
833         echo foo >> $DIR/$tdir/link
834         cat $DIR/$tdir/dangle
835         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
836         $CHECKSTAT -f -t file $DIR/$tdir/link ||
837                 error "$tdir/link not linked to a file"
838 }
839 run_test 21 "write to dangling link"
840
841 test_22() {
842         local wdir=$DIR/$tdir
843         test_mkdir $wdir
844         chown $RUNAS_ID:$RUNAS_GID $wdir
845         (cd $wdir || error "cd $wdir failed";
846                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
847                 $RUNAS tar xf -)
848         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
849         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
850         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
851                 error "checkstat -u failed"
852 }
853 run_test 22 "unpack tar archive as non-root user"
854
855 # was test_23
856 test_23a() {
857         test_mkdir $DIR/$tdir
858         local file=$DIR/$tdir/$tfile
859
860         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
861         openfile -f O_CREAT:O_EXCL $file &&
862                 error "$file recreate succeeded" || true
863 }
864 run_test 23a "O_CREAT|O_EXCL in subdir"
865
866 test_23b() { # bug 18988
867         test_mkdir $DIR/$tdir
868         local file=$DIR/$tdir/$tfile
869
870         rm -f $file
871         echo foo > $file || error "write filed"
872         echo bar >> $file || error "append filed"
873         $CHECKSTAT -s 8 $file || error "wrong size"
874         rm $file
875 }
876 run_test 23b "O_APPEND check"
877
878 # LU-9409, size with O_APPEND and tiny writes
879 test_23c() {
880         local file=$DIR/$tfile
881
882         # single dd
883         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
884         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
885         rm -f $file
886
887         # racing tiny writes
888         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
889         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
890         wait
891         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
892         rm -f $file
893
894         #racing tiny & normal writes
895         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
896         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
897         wait
898         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
899         rm -f $file
900
901         #racing tiny & normal writes 2, ugly numbers
902         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
903         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
904         wait
905         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
906         rm -f $file
907 }
908 run_test 23c "O_APPEND size checks for tiny writes"
909
910 # LU-11069 file offset is correct after appending writes
911 test_23d() {
912         local file=$DIR/$tfile
913         local offset
914
915         echo CentaurHauls > $file
916         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
917         if ((offset != 26)); then
918                 error "wrong offset, expected 26, got '$offset'"
919         fi
920 }
921 run_test 23d "file offset is correct after appending writes"
922
923 # rename sanity
924 test_24a() {
925         echo '-- same directory rename'
926         test_mkdir $DIR/$tdir
927         touch $DIR/$tdir/$tfile.1
928         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
929         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
930 }
931 run_test 24a "rename file to non-existent target"
932
933 test_24b() {
934         test_mkdir $DIR/$tdir
935         touch $DIR/$tdir/$tfile.{1,2}
936         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
937         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
938         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
939 }
940 run_test 24b "rename file to existing target"
941
942 test_24c() {
943         test_mkdir $DIR/$tdir
944         test_mkdir $DIR/$tdir/d$testnum.1
945         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
946         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
947         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
948 }
949 run_test 24c "rename directory to non-existent target"
950
951 test_24d() {
952         test_mkdir -c1 $DIR/$tdir
953         test_mkdir -c1 $DIR/$tdir/d$testnum.1
954         test_mkdir -c1 $DIR/$tdir/d$testnum.2
955         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
956         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
957         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
958 }
959 run_test 24d "rename directory to existing target"
960
961 test_24e() {
962         echo '-- cross directory renames --'
963         test_mkdir $DIR/R5a
964         test_mkdir $DIR/R5b
965         touch $DIR/R5a/f
966         mv $DIR/R5a/f $DIR/R5b/g
967         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
968         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
969 }
970 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
971
972 test_24f() {
973         test_mkdir $DIR/R6a
974         test_mkdir $DIR/R6b
975         touch $DIR/R6a/f $DIR/R6b/g
976         mv $DIR/R6a/f $DIR/R6b/g
977         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
978         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
979 }
980 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
981
982 test_24g() {
983         test_mkdir $DIR/R7a
984         test_mkdir $DIR/R7b
985         test_mkdir $DIR/R7a/d
986         mv $DIR/R7a/d $DIR/R7b/e
987         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
988         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
989 }
990 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
991
992 test_24h() {
993         test_mkdir -c1 $DIR/R8a
994         test_mkdir -c1 $DIR/R8b
995         test_mkdir -c1 $DIR/R8a/d
996         test_mkdir -c1 $DIR/R8b/e
997         mrename $DIR/R8a/d $DIR/R8b/e
998         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
999         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
1000 }
1001 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
1002
1003 test_24i() {
1004         echo "-- rename error cases"
1005         test_mkdir $DIR/R9
1006         test_mkdir $DIR/R9/a
1007         touch $DIR/R9/f
1008         mrename $DIR/R9/f $DIR/R9/a
1009         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1010         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1011         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1012 }
1013 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1014
1015 test_24j() {
1016         test_mkdir $DIR/R10
1017         mrename $DIR/R10/f $DIR/R10/g
1018         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1019         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1020         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1021 }
1022 run_test 24j "source does not exist ============================"
1023
1024 test_24k() {
1025         test_mkdir $DIR/R11a
1026         test_mkdir $DIR/R11a/d
1027         touch $DIR/R11a/f
1028         mv $DIR/R11a/f $DIR/R11a/d
1029         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1030         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1031 }
1032 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1033
1034 # bug 2429 - rename foo foo foo creates invalid file
1035 test_24l() {
1036         f="$DIR/f24l"
1037         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1038 }
1039 run_test 24l "Renaming a file to itself ========================"
1040
1041 test_24m() {
1042         f="$DIR/f24m"
1043         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1044         # on ext3 this does not remove either the source or target files
1045         # though the "expected" operation would be to remove the source
1046         $CHECKSTAT -t file ${f} || error "${f} missing"
1047         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1048 }
1049 run_test 24m "Renaming a file to a hard link to itself ========="
1050
1051 test_24n() {
1052     f="$DIR/f24n"
1053     # this stats the old file after it was renamed, so it should fail
1054     touch ${f}
1055     $CHECKSTAT ${f} || error "${f} missing"
1056     mv ${f} ${f}.rename
1057     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1058     $CHECKSTAT -a ${f} || error "${f} exists"
1059 }
1060 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1061
1062 test_24o() {
1063         test_mkdir $DIR/$tdir
1064         rename_many -s random -v -n 10 $DIR/$tdir
1065 }
1066 run_test 24o "rename of files during htree split"
1067
1068 test_24p() {
1069         test_mkdir $DIR/R12a
1070         test_mkdir $DIR/R12b
1071         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1072         mrename $DIR/R12a $DIR/R12b
1073         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1074         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1075         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1076         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1077 }
1078 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1079
1080 cleanup_multiop_pause() {
1081         trap 0
1082         kill -USR1 $MULTIPID
1083 }
1084
1085 test_24q() {
1086         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1087
1088         test_mkdir $DIR/R13a
1089         test_mkdir $DIR/R13b
1090         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1091         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1092         MULTIPID=$!
1093
1094         trap cleanup_multiop_pause EXIT
1095         mrename $DIR/R13a $DIR/R13b
1096         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1097         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1098         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1099         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1100         cleanup_multiop_pause
1101         wait $MULTIPID || error "multiop close failed"
1102 }
1103 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1104
1105 test_24r() { #bug 3789
1106         test_mkdir $DIR/R14a
1107         test_mkdir $DIR/R14a/b
1108         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1109         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1110         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1111 }
1112 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1113
1114 test_24s() {
1115         test_mkdir $DIR/R15a
1116         test_mkdir $DIR/R15a/b
1117         test_mkdir $DIR/R15a/b/c
1118         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1119         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1120         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1121 }
1122 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1123 test_24t() {
1124         test_mkdir $DIR/R16a
1125         test_mkdir $DIR/R16a/b
1126         test_mkdir $DIR/R16a/b/c
1127         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1128         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1129         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1130 }
1131 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1132
1133 test_24u() { # bug12192
1134         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1135         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1136 }
1137 run_test 24u "create stripe file"
1138
1139 simple_cleanup_common() {
1140         local rc=0
1141         trap 0
1142         [ -z "$DIR" ] || [ -z "$tdir" ] && return 0
1143
1144         local start=$SECONDS
1145         rm -rf $DIR/$tdir
1146         rc=$?
1147         wait_delete_completed
1148         echo "cleanup time $((SECONDS - start))"
1149         return $rc
1150 }
1151
1152 max_pages_per_rpc() {
1153         local mdtname="$(printf "MDT%04x" ${1:-0})"
1154         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1155 }
1156
1157 test_24v() {
1158         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1159
1160         local nrfiles=${COUNT:-100000}
1161         local fname="$DIR/$tdir/$tfile"
1162
1163         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1164         [ "$mds1_FSTYPE" = "zfs" ] && nrfiles=${COUNT:-10000}
1165
1166         test_mkdir "$(dirname $fname)"
1167         # assume MDT0000 has the fewest inodes
1168         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1169         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1170         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1171
1172         trap simple_cleanup_common EXIT
1173
1174         createmany -m "$fname" $nrfiles
1175
1176         cancel_lru_locks mdc
1177         lctl set_param mdc.*.stats clear
1178
1179         # was previously test_24D: LU-6101
1180         # readdir() returns correct number of entries after cursor reload
1181         local num_ls=$(ls $DIR/$tdir | wc -l)
1182         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1183         local num_all=$(ls -a $DIR/$tdir | wc -l)
1184         if [ $num_ls -ne $nrfiles ] || [ $num_uniq -ne $nrfiles ] ||
1185                 [ $num_all -ne $((nrfiles + 2)) ]; then
1186                         error "Expected $nrfiles files, got $num_ls " \
1187                                 "($num_uniq unique $num_all .&..)"
1188         fi
1189         # LU-5 large readdir
1190         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1191         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1192         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1193         # take into account of overhead in lu_dirpage header and end mark in
1194         # each page, plus one in rpc_num calculation.
1195         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1196         local page_entries=$(((PAGE_SIZE - 24) / dirent_size))
1197         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1198         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1199         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1200         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1201         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1202         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1203                 error "large readdir doesn't take effect: " \
1204                       "$mds_readpage should be about $rpc_max"
1205
1206         simple_cleanup_common
1207 }
1208 run_test 24v "list large directory (test hash collision, b=17560)"
1209
1210 test_24w() { # bug21506
1211         SZ1=234852
1212         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1213         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1214         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1215         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1216         [[ "$SZ1" -eq "$SZ2" ]] ||
1217                 error "Error reading at the end of the file $tfile"
1218 }
1219 run_test 24w "Reading a file larger than 4Gb"
1220
1221 test_24x() {
1222         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1223         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1224         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1225                 skip "Need MDS version at least 2.7.56"
1226
1227         local MDTIDX=1
1228         local remote_dir=$DIR/$tdir/remote_dir
1229
1230         test_mkdir $DIR/$tdir
1231         $LFS mkdir -i $MDTIDX $remote_dir ||
1232                 error "create remote directory failed"
1233
1234         test_mkdir $DIR/$tdir/src_dir
1235         touch $DIR/$tdir/src_file
1236         test_mkdir $remote_dir/tgt_dir
1237         touch $remote_dir/tgt_file
1238
1239         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1240                 error "rename dir cross MDT failed!"
1241
1242         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1243                 error "rename file cross MDT failed!"
1244
1245         touch $DIR/$tdir/ln_file
1246         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1247                 error "ln file cross MDT failed"
1248
1249         rm -rf $DIR/$tdir || error "Can not delete directories"
1250 }
1251 run_test 24x "cross MDT rename/link"
1252
1253 test_24y() {
1254         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1255         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1256
1257         local remote_dir=$DIR/$tdir/remote_dir
1258         local mdtidx=1
1259
1260         test_mkdir $DIR/$tdir
1261         $LFS mkdir -i $mdtidx $remote_dir ||
1262                 error "create remote directory failed"
1263
1264         test_mkdir $remote_dir/src_dir
1265         touch $remote_dir/src_file
1266         test_mkdir $remote_dir/tgt_dir
1267         touch $remote_dir/tgt_file
1268
1269         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1270                 error "rename subdir in the same remote dir failed!"
1271
1272         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1273                 error "rename files in the same remote dir failed!"
1274
1275         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1276                 error "link files in the same remote dir failed!"
1277
1278         rm -rf $DIR/$tdir || error "Can not delete directories"
1279 }
1280 run_test 24y "rename/link on the same dir should succeed"
1281
1282 test_24z() {
1283         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1284         [[ $MDS1_VERSION -lt $(version_code 2.12.51) ]] &&
1285                 skip "Need MDS version at least 2.12.51"
1286
1287         local index
1288
1289         for index in 0 1; do
1290                 $LFS mkdir -i $index $DIR/$tdir.$index || error "mkdir failed"
1291                 touch $DIR/$tdir.0/$tfile.$index || error "touch failed"
1292         done
1293
1294         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1 || error "mv $tfile.0 failed"
1295
1296         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.0)
1297         [ $index -eq 0 ] || error "$tfile.0 is on MDT$index"
1298
1299         local mdts=$(comma_list $(mdts_nodes))
1300
1301         do_nodes $mdts $LCTL set_param mdt.*.enable_remote_rename=0
1302         stack_trap "do_nodes $mdts $LCTL \
1303                 set_param mdt.*.enable_remote_rename=1" EXIT
1304
1305         mv $DIR/$tdir.0/$tfile.1 $DIR/$tdir.1 || error "mv $tfile.1 failed"
1306
1307         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.1)
1308         [ $index -eq 1 ] || error "$tfile.1 is on MDT$index"
1309 }
1310 run_test 24z "cross-MDT rename is done as cp"
1311
1312 test_24A() { # LU-3182
1313         local NFILES=5000
1314
1315         rm -rf $DIR/$tdir
1316         test_mkdir $DIR/$tdir
1317         trap simple_cleanup_common EXIT
1318         createmany -m $DIR/$tdir/$tfile $NFILES
1319         local t=$(ls $DIR/$tdir | wc -l)
1320         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1321         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1322         if [ $t -ne $NFILES ] || [ $u -ne $NFILES ] ||
1323            [ $v -ne $((NFILES + 2)) ] ; then
1324                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1325         fi
1326
1327         simple_cleanup_common || error "Can not delete directories"
1328 }
1329 run_test 24A "readdir() returns correct number of entries."
1330
1331 test_24B() { # LU-4805
1332         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1333
1334         local count
1335
1336         test_mkdir $DIR/$tdir
1337         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1338                 error "create striped dir failed"
1339
1340         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1341         [ $count -eq 2 ] || error "Expected 2, got $count"
1342
1343         touch $DIR/$tdir/striped_dir/a
1344
1345         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1346         [ $count -eq 3 ] || error "Expected 3, got $count"
1347
1348         touch $DIR/$tdir/striped_dir/.f
1349
1350         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1351         [ $count -eq 4 ] || error "Expected 4, got $count"
1352
1353         rm -rf $DIR/$tdir || error "Can not delete directories"
1354 }
1355 run_test 24B "readdir for striped dir return correct number of entries"
1356
1357 test_24C() {
1358         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1359
1360         mkdir $DIR/$tdir
1361         mkdir $DIR/$tdir/d0
1362         mkdir $DIR/$tdir/d1
1363
1364         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1365                 error "create striped dir failed"
1366
1367         cd $DIR/$tdir/d0/striped_dir
1368
1369         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1370         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1371         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1372
1373         [ "$d0_ino" = "$parent_ino" ] ||
1374                 error ".. wrong, expect $d0_ino, get $parent_ino"
1375
1376         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1377                 error "mv striped dir failed"
1378
1379         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1380
1381         [ "$d1_ino" = "$parent_ino" ] ||
1382                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1383 }
1384 run_test 24C "check .. in striped dir"
1385
1386 test_24E() {
1387         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1388         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1389
1390         mkdir -p $DIR/$tdir
1391         mkdir $DIR/$tdir/src_dir
1392         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1393                 error "create remote source failed"
1394
1395         touch $DIR/$tdir/src_dir/src_child/a
1396
1397         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1398                 error "create remote target dir failed"
1399
1400         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1401                 error "create remote target child failed"
1402
1403         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1404                 error "rename dir cross MDT failed!"
1405
1406         find $DIR/$tdir
1407
1408         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1409                 error "src_child still exists after rename"
1410
1411         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1412                 error "missing file(a) after rename"
1413
1414         rm -rf $DIR/$tdir || error "Can not delete directories"
1415 }
1416 run_test 24E "cross MDT rename/link"
1417
1418 test_24F () {
1419         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
1420
1421         local repeats=1000
1422         [ "$SLOW" = "no" ] && repeats=100
1423
1424         mkdir -p $DIR/$tdir
1425
1426         echo "$repeats repeats"
1427         for ((i = 0; i < repeats; i++)); do
1428                 $LFS mkdir -i0 -c2 $DIR/$tdir/test || error "mkdir fails"
1429                 touch $DIR/$tdir/test/a || error "touch fails"
1430                 mkdir $DIR/$tdir/test/b || error "mkdir fails"
1431                 rm -rf $DIR/$tdir/test || error "rmdir fails"
1432         done
1433
1434         true
1435 }
1436 run_test 24F "hash order vs readdir (LU-11330)"
1437
1438 test_25a() {
1439         echo '== symlink sanity ============================================='
1440
1441         test_mkdir $DIR/d25
1442         ln -s d25 $DIR/s25
1443         touch $DIR/s25/foo ||
1444                 error "File creation in symlinked directory failed"
1445 }
1446 run_test 25a "create file in symlinked directory ==============="
1447
1448 test_25b() {
1449         [ ! -d $DIR/d25 ] && test_25a
1450         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1451 }
1452 run_test 25b "lookup file in symlinked directory ==============="
1453
1454 test_26a() {
1455         test_mkdir $DIR/d26
1456         test_mkdir $DIR/d26/d26-2
1457         ln -s d26/d26-2 $DIR/s26
1458         touch $DIR/s26/foo || error "File creation failed"
1459 }
1460 run_test 26a "multiple component symlink ======================="
1461
1462 test_26b() {
1463         test_mkdir -p $DIR/$tdir/d26-2
1464         ln -s $tdir/d26-2/foo $DIR/s26-2
1465         touch $DIR/s26-2 || error "File creation failed"
1466 }
1467 run_test 26b "multiple component symlink at end of lookup ======"
1468
1469 test_26c() {
1470         test_mkdir $DIR/d26.2
1471         touch $DIR/d26.2/foo
1472         ln -s d26.2 $DIR/s26.2-1
1473         ln -s s26.2-1 $DIR/s26.2-2
1474         ln -s s26.2-2 $DIR/s26.2-3
1475         chmod 0666 $DIR/s26.2-3/foo
1476 }
1477 run_test 26c "chain of symlinks"
1478
1479 # recursive symlinks (bug 439)
1480 test_26d() {
1481         ln -s d26-3/foo $DIR/d26-3
1482 }
1483 run_test 26d "create multiple component recursive symlink"
1484
1485 test_26e() {
1486         [ ! -h $DIR/d26-3 ] && test_26d
1487         rm $DIR/d26-3
1488 }
1489 run_test 26e "unlink multiple component recursive symlink"
1490
1491 # recursive symlinks (bug 7022)
1492 test_26f() {
1493         test_mkdir $DIR/$tdir
1494         test_mkdir $DIR/$tdir/$tfile
1495         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1496         test_mkdir -p lndir/bar1
1497         test_mkdir $DIR/$tdir/$tfile/$tfile
1498         cd $tfile                || error "cd $tfile failed"
1499         ln -s .. dotdot          || error "ln dotdot failed"
1500         ln -s dotdot/lndir lndir || error "ln lndir failed"
1501         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1502         output=`ls $tfile/$tfile/lndir/bar1`
1503         [ "$output" = bar1 ] && error "unexpected output"
1504         rm -r $tfile             || error "rm $tfile failed"
1505         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1506 }
1507 run_test 26f "rm -r of a directory which has recursive symlink"
1508
1509 test_27a() {
1510         test_mkdir $DIR/$tdir
1511         $LFS getstripe $DIR/$tdir
1512         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1513         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1514         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1515 }
1516 run_test 27a "one stripe file"
1517
1518 test_27b() {
1519         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1520
1521         test_mkdir $DIR/$tdir
1522         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1523         $LFS getstripe -c $DIR/$tdir/$tfile
1524         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1525                 error "two-stripe file doesn't have two stripes"
1526
1527         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1528 }
1529 run_test 27b "create and write to two stripe file"
1530
1531 # 27c family tests specific striping, setstripe -o
1532 test_27ca() {
1533         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1534         test_mkdir -p $DIR/$tdir
1535         local osts="1"
1536
1537         $LFS setstripe -o $osts $DIR/$tdir/$tfile  || error "setstripe failed"
1538         $LFS getstripe -i $DIR/$tdir/$tfile
1539         [ $($LFS getstripe -i $DIR/$tdir/$tfile ) -eq $osts ] ||
1540                 error "stripe not on specified OST"
1541
1542         dd if=/dev/zero of=$DIR/$tdir/$tfile  bs=1M count=4 || error "dd failed"
1543 }
1544 run_test 27ca "one stripe on specified OST"
1545
1546 test_27cb() {
1547         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1548         test_mkdir -p $DIR/$tdir
1549         local osts="1,0"
1550         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1551         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1552         echo "$getstripe"
1553
1554         # Strip getstripe output to a space separated list of OSTs
1555         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1556                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1557         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1558                 error "stripes not on specified OSTs"
1559
1560         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1561 }
1562 run_test 27cb "two stripes on specified OSTs"
1563
1564 test_27cc() {
1565         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1566         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1567                 skip "server does not support overstriping"
1568
1569         test_mkdir -p $DIR/$tdir
1570         local osts="0,0"
1571         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1572         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1573         echo "$getstripe"
1574
1575         # Strip getstripe output to a space separated list of OSTs
1576         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1577                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1578         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1579                 error "stripes not on specified OSTs"
1580
1581         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1582 }
1583 run_test 27cc "two stripes on the same OST"
1584
1585 test_27cd() {
1586         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1587         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1588                 skip "server does not support overstriping"
1589         test_mkdir -p $DIR/$tdir
1590         local osts="0,1,1,0"
1591         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1592         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1593         echo "$getstripe"
1594
1595         # Strip getstripe output to a space separated list of OSTs
1596         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1597                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1598         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1599                 error "stripes not on specified OSTs"
1600
1601         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1602 }
1603 run_test 27cd "four stripes on two OSTs"
1604
1605 test_27ce() {
1606         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
1607                 skip_env "too many osts, skipping"
1608         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1609                 skip "server does not support overstriping"
1610         # We do one more stripe than we have OSTs
1611         [ $OSTCOUNT -ge 159 ] || large_xattr_enabled ||
1612                 skip_env "ea_inode feature disabled"
1613
1614         test_mkdir -p $DIR/$tdir
1615         local osts=""
1616         for i in $(seq 0 $OSTCOUNT);
1617         do
1618                 osts=$osts"0"
1619                 if [ $i -ne $OSTCOUNT ]; then
1620                         osts=$osts","
1621                 fi
1622         done
1623         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1624         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1625         echo "$getstripe"
1626
1627         # Strip getstripe output to a space separated list of OSTs
1628         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1629                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1630         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1631                 error "stripes not on specified OSTs"
1632
1633         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1634 }
1635 run_test 27ce "more stripes than OSTs with -o"
1636
1637 test_27d() {
1638         test_mkdir $DIR/$tdir
1639         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1640                 error "setstripe failed"
1641         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1642         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1643 }
1644 run_test 27d "create file with default settings"
1645
1646 test_27e() {
1647         # LU-5839 adds check for existed layout before setting it
1648         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1649                 skip "Need MDS version at least 2.7.56"
1650
1651         test_mkdir $DIR/$tdir
1652         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1653         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1654         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1655 }
1656 run_test 27e "setstripe existing file (should return error)"
1657
1658 test_27f() {
1659         test_mkdir $DIR/$tdir
1660         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1661                 error "$LFS setstripe $DIR/$tdir/$tfile failed"
1662         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1663                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1664         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1665         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1666 }
1667 run_test 27f "setstripe with bad stripe size (should return error)"
1668
1669 test_27g() {
1670         test_mkdir $DIR/$tdir
1671         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1672         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1673                 error "$DIR/$tdir/$tfile has object"
1674 }
1675 run_test 27g "$LFS getstripe with no objects"
1676
1677 test_27ga() {
1678         test_mkdir $DIR/$tdir
1679         touch $DIR/$tdir/$tfile || error "touch failed"
1680         ln -s bogus $DIR/$tdir/$tfile.2 || error "ln failed"
1681         $LFS getstripe -m $DIR/$tdir/$tfile $DIR/$tdir/$tfile.2
1682         local rc=$?
1683         (( rc == 2 )) || error "getstripe did not return ENOENT"
1684 }
1685 run_test 27ga "$LFS getstripe with missing file (should return error)"
1686
1687 test_27i() {
1688         test_mkdir $DIR/$tdir
1689         touch $DIR/$tdir/$tfile || error "touch failed"
1690         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1691                 error "missing objects"
1692 }
1693 run_test 27i "$LFS getstripe with some objects"
1694
1695 test_27j() {
1696         test_mkdir $DIR/$tdir
1697         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1698                 error "setstripe failed" || true
1699 }
1700 run_test 27j "setstripe with bad stripe offset (should return error)"
1701
1702 test_27k() { # bug 2844
1703         test_mkdir $DIR/$tdir
1704         local file=$DIR/$tdir/$tfile
1705         local ll_max_blksize=$((4 * 1024 * 1024))
1706         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1707         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1708         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1709         dd if=/dev/zero of=$file bs=4k count=1
1710         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1711         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1712 }
1713 run_test 27k "limit i_blksize for broken user apps"
1714
1715 test_27l() {
1716         mcreate $DIR/$tfile || error "creating file"
1717         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1718                 error "setstripe should have failed" || true
1719 }
1720 run_test 27l "check setstripe permissions (should return error)"
1721
1722 test_27m() {
1723         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1724
1725         [ -n "$RCLIENTS" -o -n "$MOUNT_2" ] &&
1726                 skip_env "multiple clients -- skipping"
1727
1728         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1729                    head -n1)
1730         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1731                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1732         fi
1733         trap simple_cleanup_common EXIT
1734         test_mkdir $DIR/$tdir
1735         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1736         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1737                 error "dd should fill OST0"
1738         i=2
1739         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1740                 i=$((i + 1))
1741                 [ $i -gt 256 ] && break
1742         done
1743         i=$((i + 1))
1744         touch $DIR/$tdir/$tfile.$i
1745         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1746             awk '{print $1}'| grep -w "0") ] &&
1747                 error "OST0 was full but new created file still use it"
1748         i=$((i + 1))
1749         touch $DIR/$tdir/$tfile.$i
1750         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1751             awk '{print $1}'| grep -w "0") ] &&
1752                 error "OST0 was full but new created file still use it"
1753         simple_cleanup_common
1754 }
1755 run_test 27m "create file while OST0 was full"
1756
1757 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1758 # if the OST isn't full anymore.
1759 reset_enospc() {
1760         local OSTIDX=${1:-""}
1761
1762         local list=$(comma_list $(osts_nodes))
1763         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1764
1765         do_nodes $list lctl set_param fail_loc=0
1766         sync    # initiate all OST_DESTROYs from MDS to OST
1767         sleep_maxage
1768 }
1769
1770 exhaust_precreations() {
1771         local OSTIDX=$1
1772         local FAILLOC=$2
1773         local FAILIDX=${3:-$OSTIDX}
1774         local ofacet=ost$((OSTIDX + 1))
1775
1776         test_mkdir -p -c1 $DIR/$tdir
1777         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1778         local mfacet=mds$((mdtidx + 1))
1779         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1780
1781         local OST=$(ostname_from_index $OSTIDX)
1782
1783         # on the mdt's osc
1784         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1785         local last_id=$(do_facet $mfacet lctl get_param -n \
1786                         osp.$mdtosc_proc1.prealloc_last_id)
1787         local next_id=$(do_facet $mfacet lctl get_param -n \
1788                         osp.$mdtosc_proc1.prealloc_next_id)
1789
1790         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1791         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1792
1793         test_mkdir -p $DIR/$tdir/${OST}
1794         $LFS setstripe -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1795 #define OBD_FAIL_OST_ENOSPC              0x215
1796         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1797         echo "Creating to objid $last_id on ost $OST..."
1798         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1799         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1800         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1801         sleep_maxage
1802 }
1803
1804 exhaust_all_precreations() {
1805         local i
1806         for (( i=0; i < OSTCOUNT; i++ )) ; do
1807                 exhaust_precreations $i $1 -1
1808         done
1809 }
1810
1811 test_27n() {
1812         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1813         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1814         remote_mds_nodsh && skip "remote MDS with nodsh"
1815         remote_ost_nodsh && skip "remote OST with nodsh"
1816
1817         reset_enospc
1818         rm -f $DIR/$tdir/$tfile
1819         exhaust_precreations 0 0x80000215
1820         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1821         touch $DIR/$tdir/$tfile || error "touch failed"
1822         $LFS getstripe $DIR/$tdir/$tfile
1823         reset_enospc
1824 }
1825 run_test 27n "create file with some full OSTs"
1826
1827 test_27o() {
1828         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1829         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1830         remote_mds_nodsh && skip "remote MDS with nodsh"
1831         remote_ost_nodsh && skip "remote OST with nodsh"
1832
1833         reset_enospc
1834         rm -f $DIR/$tdir/$tfile
1835         exhaust_all_precreations 0x215
1836
1837         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1838
1839         reset_enospc
1840         rm -rf $DIR/$tdir/*
1841 }
1842 run_test 27o "create file with all full OSTs (should error)"
1843
1844 test_27p() {
1845         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1846         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1847         remote_mds_nodsh && skip "remote MDS with nodsh"
1848         remote_ost_nodsh && skip "remote OST with nodsh"
1849
1850         reset_enospc
1851         rm -f $DIR/$tdir/$tfile
1852         test_mkdir $DIR/$tdir
1853
1854         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1855         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1856         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1857
1858         exhaust_precreations 0 0x80000215
1859         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1860         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1861         $LFS getstripe $DIR/$tdir/$tfile
1862
1863         reset_enospc
1864 }
1865 run_test 27p "append to a truncated file with some full OSTs"
1866
1867 test_27q() {
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
1876         test_mkdir $DIR/$tdir
1877         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1878         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1879                 error "truncate $DIR/$tdir/$tfile failed"
1880         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1881
1882         exhaust_all_precreations 0x215
1883
1884         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1885         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1886
1887         reset_enospc
1888 }
1889 run_test 27q "append to truncated file with all OSTs full (should error)"
1890
1891 test_27r() {
1892         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1893         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1894         remote_mds_nodsh && skip "remote MDS with nodsh"
1895         remote_ost_nodsh && skip "remote OST with nodsh"
1896
1897         reset_enospc
1898         rm -f $DIR/$tdir/$tfile
1899         exhaust_precreations 0 0x80000215
1900
1901         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1902
1903         reset_enospc
1904 }
1905 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1906
1907 test_27s() { # bug 10725
1908         test_mkdir $DIR/$tdir
1909         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1910         local stripe_count=0
1911         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1912         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1913                 error "stripe width >= 2^32 succeeded" || true
1914
1915 }
1916 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1917
1918 test_27t() { # bug 10864
1919         WDIR=$(pwd)
1920         WLFS=$(which lfs)
1921         cd $DIR
1922         touch $tfile
1923         $WLFS getstripe $tfile
1924         cd $WDIR
1925 }
1926 run_test 27t "check that utils parse path correctly"
1927
1928 test_27u() { # bug 4900
1929         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1930         remote_mds_nodsh && skip "remote MDS with nodsh"
1931
1932         local index
1933         local list=$(comma_list $(mdts_nodes))
1934
1935 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1936         do_nodes $list $LCTL set_param fail_loc=0x139
1937         test_mkdir -p $DIR/$tdir
1938         trap simple_cleanup_common EXIT
1939         createmany -o $DIR/$tdir/t- 1000
1940         do_nodes $list $LCTL set_param fail_loc=0
1941
1942         TLOG=$TMP/$tfile.getstripe
1943         $LFS getstripe $DIR/$tdir > $TLOG
1944         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1945         unlinkmany $DIR/$tdir/t- 1000
1946         trap 0
1947         [[ $OBJS -gt 0 ]] &&
1948                 error "$OBJS objects created on OST-0. See $TLOG" ||
1949                 rm -f $TLOG
1950 }
1951 run_test 27u "skip object creation on OSC w/o objects"
1952
1953 test_27v() { # bug 4900
1954         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1955         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1956         remote_mds_nodsh && skip "remote MDS with nodsh"
1957         remote_ost_nodsh && skip "remote OST with nodsh"
1958
1959         exhaust_all_precreations 0x215
1960         reset_enospc
1961
1962         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1963
1964         touch $DIR/$tdir/$tfile
1965         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1966         # all except ost1
1967         for (( i=1; i < OSTCOUNT; i++ )); do
1968                 do_facet ost$i lctl set_param fail_loc=0x705
1969         done
1970         local START=`date +%s`
1971         createmany -o $DIR/$tdir/$tfile 32
1972
1973         local FINISH=`date +%s`
1974         local TIMEOUT=`lctl get_param -n timeout`
1975         local PROCESS=$((FINISH - START))
1976         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1977                error "$FINISH - $START >= $TIMEOUT / 2"
1978         sleep $((TIMEOUT / 2 - PROCESS))
1979         reset_enospc
1980 }
1981 run_test 27v "skip object creation on slow OST"
1982
1983 test_27w() { # bug 10997
1984         test_mkdir $DIR/$tdir
1985         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1986         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1987                 error "stripe size $size != 65536" || true
1988         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1989                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1990 }
1991 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1992
1993 test_27wa() {
1994         [[ $OSTCOUNT -lt 2 ]] &&
1995                 skip_env "skipping multiple stripe count/offset test"
1996
1997         test_mkdir $DIR/$tdir
1998         for i in $(seq 1 $OSTCOUNT); do
1999                 offset=$((i - 1))
2000                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
2001                         error "setstripe -c $i -i $offset failed"
2002                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
2003                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
2004                 [ $count -ne $i ] && error "stripe count $count != $i" || true
2005                 [ $index -ne $offset ] &&
2006                         error "stripe offset $index != $offset" || true
2007         done
2008 }
2009 run_test 27wa "check $LFS setstripe -c -i options"
2010
2011 test_27x() {
2012         remote_ost_nodsh && skip "remote OST with nodsh"
2013         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2014         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2015
2016         OFFSET=$(($OSTCOUNT - 1))
2017         OSTIDX=0
2018         local OST=$(ostname_from_index $OSTIDX)
2019
2020         test_mkdir $DIR/$tdir
2021         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
2022         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
2023         sleep_maxage
2024         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
2025         for i in $(seq 0 $OFFSET); do
2026                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
2027                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
2028                 error "OST0 was degraded but new created file still use it"
2029         done
2030         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
2031 }
2032 run_test 27x "create files while OST0 is degraded"
2033
2034 test_27y() {
2035         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2036         remote_mds_nodsh && skip "remote MDS with nodsh"
2037         remote_ost_nodsh && skip "remote OST with nodsh"
2038         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2039
2040         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
2041         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
2042                 osp.$mdtosc.prealloc_last_id)
2043         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
2044                 osp.$mdtosc.prealloc_next_id)
2045         local fcount=$((last_id - next_id))
2046         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
2047         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
2048
2049         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
2050                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
2051         local OST_DEACTIVE_IDX=-1
2052         local OSC
2053         local OSTIDX
2054         local OST
2055
2056         for OSC in $MDS_OSCS; do
2057                 OST=$(osc_to_ost $OSC)
2058                 OSTIDX=$(index_from_ostuuid $OST)
2059                 if [ $OST_DEACTIVE_IDX == -1 ]; then
2060                         OST_DEACTIVE_IDX=$OSTIDX
2061                 fi
2062                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
2063                         echo $OSC "is Deactivated:"
2064                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
2065                 fi
2066         done
2067
2068         OSTIDX=$(index_from_ostuuid $OST)
2069         test_mkdir $DIR/$tdir
2070         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
2071
2072         for OSC in $MDS_OSCS; do
2073                 OST=$(osc_to_ost $OSC)
2074                 OSTIDX=$(index_from_ostuuid $OST)
2075                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2076                         echo $OST "is degraded:"
2077                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2078                                                 obdfilter.$OST.degraded=1
2079                 fi
2080         done
2081
2082         sleep_maxage
2083         createmany -o $DIR/$tdir/$tfile $fcount
2084
2085         for OSC in $MDS_OSCS; do
2086                 OST=$(osc_to_ost $OSC)
2087                 OSTIDX=$(index_from_ostuuid $OST)
2088                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2089                         echo $OST "is recovered from degraded:"
2090                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2091                                                 obdfilter.$OST.degraded=0
2092                 else
2093                         do_facet $SINGLEMDS lctl --device %$OSC activate
2094                 fi
2095         done
2096
2097         # all osp devices get activated, hence -1 stripe count restored
2098         local stripe_count=0
2099
2100         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
2101         # devices get activated.
2102         sleep_maxage
2103         $LFS setstripe -c -1 $DIR/$tfile
2104         stripe_count=$($LFS getstripe -c $DIR/$tfile)
2105         rm -f $DIR/$tfile
2106         [ $stripe_count -ne $OSTCOUNT ] &&
2107                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
2108         return 0
2109 }
2110 run_test 27y "create files while OST0 is degraded and the rest inactive"
2111
2112 check_seq_oid()
2113 {
2114         log "check file $1"
2115
2116         lmm_count=$($LFS getstripe -c $1)
2117         lmm_seq=$($LFS getstripe -v $1 | awk '/lmm_seq/ { print $2 }')
2118         lmm_oid=$($LFS getstripe -v $1 | awk '/lmm_object_id/ { print $2 }')
2119
2120         local old_ifs="$IFS"
2121         IFS=$'[:]'
2122         fid=($($LFS path2fid $1))
2123         IFS="$old_ifs"
2124
2125         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
2126         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
2127
2128         # compare lmm_seq and lu_fid->f_seq
2129         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
2130         # compare lmm_object_id and lu_fid->oid
2131         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
2132
2133         # check the trusted.fid attribute of the OST objects of the file
2134         local have_obdidx=false
2135         local stripe_nr=0
2136         $LFS getstripe $1 | while read obdidx oid hex seq; do
2137                 # skip lines up to and including "obdidx"
2138                 [ -z "$obdidx" ] && break
2139                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
2140                 $have_obdidx || continue
2141
2142                 local ost=$((obdidx + 1))
2143                 local dev=$(ostdevname $ost)
2144                 local oid_hex
2145
2146                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
2147
2148                 seq=$(echo $seq | sed -e "s/^0x//g")
2149                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
2150                         oid_hex=$(echo $oid)
2151                 else
2152                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
2153                 fi
2154                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
2155
2156                 local ff=""
2157                 #
2158                 # Don't unmount/remount the OSTs if we don't need to do that.
2159                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2160                 # update too, until that use mount/ll_decode_filter_fid/mount.
2161                 # Re-enable when debugfs will understand new filter_fid.
2162                 #
2163                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2164                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2165                                 $dev 2>/dev/null" | grep "parent=")
2166                 fi
2167                 if [ -z "$ff" ]; then
2168                         stop ost$ost
2169                         mount_fstype ost$ost
2170                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2171                                 $(facet_mntpt ost$ost)/$obj_file)
2172                         unmount_fstype ost$ost
2173                         start ost$ost $dev $OST_MOUNT_OPTS
2174                         clients_up
2175                 fi
2176
2177                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2178
2179                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2180
2181                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2182                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2183                 #
2184                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2185                 #       stripe_size=1048576 component_id=1 component_start=0 \
2186                 #       component_end=33554432
2187                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2188                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2189                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2190                 local ff_pstripe
2191                 if grep -q 'stripe=' <<<$ff; then
2192                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2193                 else
2194                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2195                         # into f_ver in this case.  See comment on ff_parent.
2196                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2197                 fi
2198
2199                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2200                 [ $ff_pseq = $lmm_seq ] ||
2201                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2202                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2203                 [ $ff_poid = $lmm_oid ] ||
2204                         error "FF parent OID $ff_poid != $lmm_oid"
2205                 (($ff_pstripe == $stripe_nr)) ||
2206                         error "FF stripe $ff_pstripe != $stripe_nr"
2207
2208                 stripe_nr=$((stripe_nr + 1))
2209                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2210                         continue
2211                 if grep -q 'stripe_count=' <<<$ff; then
2212                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2213                                             -e 's/ .*//' <<<$ff)
2214                         [ $lmm_count = $ff_scnt ] ||
2215                                 error "FF stripe count $lmm_count != $ff_scnt"
2216                 fi
2217         done
2218 }
2219
2220 test_27z() {
2221         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2222         remote_ost_nodsh && skip "remote OST with nodsh"
2223
2224         test_mkdir $DIR/$tdir
2225         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2226                 { error "setstripe -c -1 failed"; return 1; }
2227         # We need to send a write to every object to get parent FID info set.
2228         # This _should_ also work for setattr, but does not currently.
2229         # touch $DIR/$tdir/$tfile-1 ||
2230         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2231                 { error "dd $tfile-1 failed"; return 2; }
2232         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2233                 { error "setstripe -c -1 failed"; return 3; }
2234         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2235                 { error "dd $tfile-2 failed"; return 4; }
2236
2237         # make sure write RPCs have been sent to OSTs
2238         sync; sleep 5; sync
2239
2240         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2241         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2242 }
2243 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2244
2245 test_27A() { # b=19102
2246         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2247
2248         save_layout_restore_at_exit $MOUNT
2249         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2250         wait_update $HOSTNAME "$LFS getstripe -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2251                 error "stripe count $($LFS getstripe -c $MOUNT) != 1"
2252         local default_size=$($LFS getstripe -S $MOUNT)
2253         local default_offset=$($LFS getstripe -i $MOUNT)
2254         local dsize=$(do_facet $SINGLEMDS \
2255                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2256         [ $default_size -eq $dsize ] ||
2257                 error "stripe size $default_size != $dsize"
2258         [ $default_offset -eq -1 ] ||
2259                 error "stripe offset $default_offset != -1"
2260 }
2261 run_test 27A "check filesystem-wide default LOV EA values"
2262
2263 test_27B() { # LU-2523
2264         test_mkdir $DIR/$tdir
2265         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2266         touch $DIR/$tdir/f0
2267         # open f1 with O_LOV_DELAY_CREATE
2268         # rename f0 onto f1
2269         # call setstripe ioctl on open file descriptor for f1
2270         # close
2271         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2272                 $DIR/$tdir/f0
2273
2274         rm -f $DIR/$tdir/f1
2275         # open f1 with O_LOV_DELAY_CREATE
2276         # unlink f1
2277         # call setstripe ioctl on open file descriptor for f1
2278         # close
2279         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2280
2281         # Allow multiop to fail in imitation of NFS's busted semantics.
2282         true
2283 }
2284 run_test 27B "call setstripe on open unlinked file/rename victim"
2285
2286 # 27C family tests full striping and overstriping
2287 test_27Ca() { #LU-2871
2288         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2289
2290         declare -a ost_idx
2291         local index
2292         local found
2293         local i
2294         local j
2295
2296         test_mkdir $DIR/$tdir
2297         cd $DIR/$tdir
2298         for i in $(seq 0 $((OSTCOUNT - 1))); do
2299                 # set stripe across all OSTs starting from OST$i
2300                 $LFS setstripe -i $i -c -1 $tfile$i
2301                 # get striping information
2302                 ost_idx=($($LFS getstripe $tfile$i |
2303                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2304                 echo ${ost_idx[@]}
2305
2306                 # check the layout
2307                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2308                         error "${#ost_idx[@]} != $OSTCOUNT"
2309
2310                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2311                         found=0
2312                         for j in $(echo ${ost_idx[@]}); do
2313                                 if [ $index -eq $j ]; then
2314                                         found=1
2315                                         break
2316                                 fi
2317                         done
2318                         [ $found = 1 ] ||
2319                                 error "Can not find $index in ${ost_idx[@]}"
2320                 done
2321         done
2322 }
2323 run_test 27Ca "check full striping across all OSTs"
2324
2325 test_27Cb() {
2326         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2327                 skip "server does not support overstriping"
2328         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2329                 skip_env "too many osts, skipping"
2330
2331         test_mkdir -p $DIR/$tdir
2332         local setcount=$(($OSTCOUNT * 2))
2333         [ $setcount -ge 160 ] || large_xattr_enabled ||
2334                 skip_env "ea_inode feature disabled"
2335
2336         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2337                 error "setstripe failed"
2338
2339         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2340         [ $count -eq $setcount ] ||
2341                 error "stripe count $count, should be $setcount"
2342
2343         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2344                 error "overstriped should be set in pattern"
2345
2346         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2347                 error "dd failed"
2348 }
2349 run_test 27Cb "more stripes than OSTs with -C"
2350
2351 test_27Cc() {
2352         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2353                 skip "server does not support overstriping"
2354         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2355
2356         test_mkdir -p $DIR/$tdir
2357         local setcount=$(($OSTCOUNT - 1))
2358
2359         [ $setcount -ge 160 ] || large_xattr_enabled ||
2360                 skip_env "ea_inode feature disabled"
2361
2362         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2363                 error "setstripe failed"
2364
2365         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2366         [ $count -eq $setcount ] ||
2367                 error "stripe count $count, should be $setcount"
2368
2369         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" &&
2370                 error "overstriped should not be set in pattern"
2371
2372         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2373                 error "dd failed"
2374 }
2375 run_test 27Cc "fewer stripes than OSTs does not set overstriping"
2376
2377 test_27Cd() {
2378         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2379                 skip "server does not support overstriping"
2380         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2381         large_xattr_enabled || skip_env "ea_inode feature disabled"
2382
2383         test_mkdir -p $DIR/$tdir
2384         local setcount=$LOV_MAX_STRIPE_COUNT
2385
2386         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2387                 error "setstripe failed"
2388
2389         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2390         [ $count -eq $setcount ] ||
2391                 error "stripe count $count, should be $setcount"
2392
2393         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2394                 error "overstriped should be set in pattern"
2395
2396         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2397                 error "dd failed"
2398
2399         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2400 }
2401 run_test 27Cd "test maximum stripe count"
2402
2403 test_27Ce() {
2404         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2405                 skip "server does not support overstriping"
2406         test_mkdir -p $DIR/$tdir
2407
2408         pool_add $TESTNAME || error "Pool creation failed"
2409         pool_add_targets $TESTNAME 0 || error "pool_add_targets failed"
2410
2411         local setcount=8
2412
2413         $LFS setstripe  -C $setcount -p "$TESTNAME" $DIR/$tdir/$tfile ||
2414                 error "setstripe failed"
2415
2416         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2417         [ $count -eq $setcount ] ||
2418                 error "stripe count $count, should be $setcount"
2419
2420         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2421                 error "overstriped should be set in pattern"
2422
2423         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2424                 error "dd failed"
2425
2426         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2427 }
2428 run_test 27Ce "test pool with overstriping"
2429
2430 test_27Cf() {
2431         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2432                 skip "server does not support overstriping"
2433         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2434                 skip_env "too many osts, skipping"
2435
2436         test_mkdir -p $DIR/$tdir
2437
2438         local setcount=$(($OSTCOUNT * 2))
2439         [ $setcount -ge 160 ] || large_xattr_enabled ||
2440                 skip_env "ea_inode feature disabled"
2441
2442         $LFS setstripe  -C $setcount $DIR/$tdir/ ||
2443                 error "setstripe failed"
2444
2445         echo 1 > $DIR/$tdir/$tfile
2446
2447         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2448         [ $count -eq $setcount ] ||
2449                 error "stripe count $count, should be $setcount"
2450
2451         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2452                 error "overstriped should be set in pattern"
2453
2454         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2455                 error "dd failed"
2456
2457         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2458 }
2459 run_test 27Cf "test default inheritance with overstriping"
2460
2461 test_27D() {
2462         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2463         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2464         remote_mds_nodsh && skip "remote MDS with nodsh"
2465
2466         local POOL=${POOL:-testpool}
2467         local first_ost=0
2468         local last_ost=$(($OSTCOUNT - 1))
2469         local ost_step=1
2470         local ost_list=$(seq $first_ost $ost_step $last_ost)
2471         local ost_range="$first_ost $last_ost $ost_step"
2472
2473         test_mkdir $DIR/$tdir
2474         pool_add $POOL || error "pool_add failed"
2475         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2476
2477         local skip27D
2478         [ $MDS1_VERSION -lt $(version_code 2.8.55) ] &&
2479                 skip27D+="-s 29"
2480         [ $MDS1_VERSION -lt $(version_code 2.9.55) ] ||
2481                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2482                         skip27D+=" -s 30,31"
2483         [[ ! $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ||
2484           $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2485                 skip27D+=" -s 32,33"
2486         [[ $MDS_VERSION -lt $(version_code $SEL_VER) ]] &&
2487                 skip27D+=" -s 34"
2488         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2489                 error "llapi_layout_test failed"
2490
2491         destroy_test_pools || error "destroy test pools failed"
2492 }
2493 run_test 27D "validate llapi_layout API"
2494
2495 # Verify that default_easize is increased from its initial value after
2496 # accessing a widely striped file.
2497 test_27E() {
2498         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2499         [ $CLIENT_VERSION -lt $(version_code 2.5.57) ] &&
2500                 skip "client does not have LU-3338 fix"
2501
2502         # 72 bytes is the minimum space required to store striping
2503         # information for a file striped across one OST:
2504         # (sizeof(struct lov_user_md_v3) +
2505         #  sizeof(struct lov_user_ost_data_v1))
2506         local min_easize=72
2507         $LCTL set_param -n llite.*.default_easize $min_easize ||
2508                 error "lctl set_param failed"
2509         local easize=$($LCTL get_param -n llite.*.default_easize)
2510
2511         [ $easize -eq $min_easize ] ||
2512                 error "failed to set default_easize"
2513
2514         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2515                 error "setstripe failed"
2516         # In order to ensure stat() call actually talks to MDS we need to
2517         # do something drastic to this file to shake off all lock, e.g.
2518         # rename it (kills lookup lock forcing cache cleaning)
2519         mv $DIR/$tfile $DIR/${tfile}-1
2520         ls -l $DIR/${tfile}-1
2521         rm $DIR/${tfile}-1
2522
2523         easize=$($LCTL get_param -n llite.*.default_easize)
2524
2525         [ $easize -gt $min_easize ] ||
2526                 error "default_easize not updated"
2527 }
2528 run_test 27E "check that default extended attribute size properly increases"
2529
2530 test_27F() { # LU-5346/LU-7975
2531         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2532         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2533         [[ $MDS1_VERSION -lt $(version_code 2.8.51) ]] &&
2534                 skip "Need MDS version at least 2.8.51"
2535         remote_ost_nodsh && skip "remote OST with nodsh"
2536
2537         test_mkdir $DIR/$tdir
2538         rm -f $DIR/$tdir/f0
2539         $LFS setstripe -c 2 $DIR/$tdir
2540
2541         # stop all OSTs to reproduce situation for LU-7975 ticket
2542         for num in $(seq $OSTCOUNT); do
2543                 stop ost$num
2544         done
2545
2546         # open/create f0 with O_LOV_DELAY_CREATE
2547         # truncate f0 to a non-0 size
2548         # close
2549         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2550
2551         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2552         # open/write it again to force delayed layout creation
2553         cat /etc/hosts > $DIR/$tdir/f0 &
2554         catpid=$!
2555
2556         # restart OSTs
2557         for num in $(seq $OSTCOUNT); do
2558                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2559                         error "ost$num failed to start"
2560         done
2561
2562         wait $catpid || error "cat failed"
2563
2564         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2565         [[ $($LFS getstripe -c $DIR/$tdir/f0) == 2 ]] ||
2566                 error "wrong stripecount"
2567
2568 }
2569 run_test 27F "Client resend delayed layout creation with non-zero size"
2570
2571 test_27G() { #LU-10629
2572         [ $MDS1_VERSION -lt $(version_code 2.11.51) ] &&
2573                 skip "Need MDS version at least 2.11.51"
2574         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2575         remote_mds_nodsh && skip "remote MDS with nodsh"
2576         local POOL=${POOL:-testpool}
2577         local ostrange="0 0 1"
2578
2579         test_mkdir $DIR/$tdir
2580         pool_add $POOL || error "pool_add failed"
2581         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2582         $LFS setstripe -p $POOL $DIR/$tdir
2583
2584         local pool=$($LFS getstripe -p $DIR/$tdir)
2585
2586         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2587
2588         $LFS setstripe -d $DIR/$tdir
2589
2590         pool=$($LFS getstripe -p $DIR/$tdir)
2591
2592         rmdir $DIR/$tdir
2593
2594         [ -z "$pool" ] || error "'$pool' is not empty"
2595 }
2596 run_test 27G "Clear OST pool from stripe"
2597
2598 test_27H() {
2599         [[ $MDS1_VERSION -le $(version_code 2.11.54) ]] &&
2600                 skip "Need MDS version newer than 2.11.54"
2601         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2602         test_mkdir $DIR/$tdir
2603         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2604         touch $DIR/$tdir/$tfile
2605         $LFS getstripe -c $DIR/$tdir/$tfile
2606         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2607                 error "two-stripe file doesn't have two stripes"
2608
2609         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2610         $LFS getstripe -y $DIR/$tdir/$tfile
2611         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2612              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2613                 error "expected l_ost_idx: [02]$ not matched"
2614
2615         # make sure ost list has been cleared
2616         local stripesize=$($LFS getstripe -S $DIR/$tdir)
2617         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2618                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2619         touch $DIR/$tdir/f3
2620         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2621 }
2622 run_test 27H "Set specific OSTs stripe"
2623
2624 test_27I() {
2625         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2626         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2627         [[ $MDS1_VERSION -gt $(version_code 2.12.52) ]] ||
2628                 skip "Need MDS version newer than 2.12.52"
2629         local pool=$TESTNAME
2630         local ostrange="1 1 1"
2631
2632         save_layout_restore_at_exit $MOUNT
2633         $LFS setstripe -c 2 -i 0 $MOUNT
2634         pool_add $pool || error "pool_add failed"
2635         pool_add_targets $pool $ostrange || "pool_add_targets failed"
2636         test_mkdir $DIR/$tdir
2637         $LFS setstripe -p $pool $DIR/$tdir
2638         $MULTIOP $DIR/$tdir/$tfile Oc || error "multiop failed"
2639         $LFS getstripe $DIR/$tdir/$tfile
2640 }
2641 run_test 27I "check that root dir striping does not break parent dir one"
2642
2643 test_27J() {
2644         [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.12.51) ]] &&
2645                 skip "Need MDS version newer than 2.12.51"
2646
2647         test_mkdir $DIR/$tdir
2648         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2649         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2650
2651         # create foreign file (raw way)
2652         create_foreign_file -f $DIR/$tdir/$tfile -x "${uuid1}@${uuid2}" \
2653                 -t 1 -F 0xda08 || error "create_foreign_file failed"
2654
2655         # verify foreign file (raw way)
2656         parse_foreign_file -f $DIR/$tdir/$tfile |
2657                 grep "lov_foreign_magic: 0x0BD70BD0" ||
2658                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign magic"
2659         parse_foreign_file -f $DIR/$tdir/$tfile | grep "lov_xattr_size: 89" ||
2660                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2661         parse_foreign_file -f $DIR/$tdir/$tfile |
2662                 grep "lov_foreign_size: 73" ||
2663                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2664         parse_foreign_file -f $DIR/$tdir/$tfile |
2665                 grep "lov_foreign_type: 1" ||
2666                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign type"
2667         parse_foreign_file -f $DIR/$tdir/$tfile |
2668                 grep "lov_foreign_flags: 0x0000DA08" ||
2669                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign flags"
2670         local lov=$(parse_foreign_file -f $DIR/$tdir/$tfile |
2671                 grep "lov_foreign_value: 0x" |
2672                 sed -e 's/lov_foreign_value: 0x//')
2673         local lov2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160)
2674         [[ $lov = ${lov2// /} ]] ||
2675                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign value"
2676
2677         # create foreign file (lfs + API)
2678         $LFS setstripe --foreign=daos --flags 0xda08 \
2679                 -x "${uuid1}@${uuid2}" $DIR/$tdir/${tfile}2 ||
2680                 error "$DIR/$tdir/${tfile}2: create failed"
2681
2682         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2683                 grep "lfm_magic:.*0x0BD70BD0" ||
2684                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign magic"
2685         # lfm_length is LOV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2686         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_length:.*73" ||
2687                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign size"
2688         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_type:.*daos" ||
2689                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign type"
2690         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2691                 grep "lfm_flags:.*0x0000DA08" ||
2692                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign flags"
2693         $LFS getstripe $DIR/$tdir/${tfile}2 |
2694                 grep "lfm_value:.*${uuid1}@${uuid2}" ||
2695                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign value"
2696
2697         # modify striping should fail
2698         $LFS setstripe -c 2 $DIR/$tdir/$tfile &&
2699                 error "$DIR/$tdir/$tfile: setstripe should fail"
2700         $LFS setstripe -c 2 $DIR/$tdir/${tfile}2 &&
2701                 error "$DIR/$tdir/${tfile}2: setstripe should fail"
2702
2703         # R/W should fail
2704         cat $DIR/$tdir/$tfile && error "$DIR/$tdir/$tfile: read should fail"
2705         cat $DIR/$tdir/${tfile}2 &&
2706                 error "$DIR/$tdir/${tfile}2: read should fail"
2707         cat /etc/passwd > $DIR/$tdir/$tfile &&
2708                 error "$DIR/$tdir/$tfile: write should fail"
2709         cat /etc/passwd > $DIR/$tdir/${tfile}2 &&
2710                 error "$DIR/$tdir/${tfile}2: write should fail"
2711
2712         # chmod should work
2713         chmod 222 $DIR/$tdir/$tfile ||
2714                 error "$DIR/$tdir/$tfile: chmod failed"
2715         chmod 222 $DIR/$tdir/${tfile}2 ||
2716                 error "$DIR/$tdir/${tfile}2: chmod failed"
2717
2718         # chown should work
2719         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tfile ||
2720                 error "$DIR/$tdir/$tfile: chown failed"
2721         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tfile}2 ||
2722                 error "$DIR/$tdir/${tfile}2: chown failed"
2723
2724         # rename should work
2725         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}.new ||
2726                 error "$DIR/$tdir/$tfile: rename of foreign file has failed"
2727         mv $DIR/$tdir/${tfile}2 $DIR/$tdir/${tfile}2.new ||
2728                 error "$DIR/$tdir/${tfile}2: rename of foreign file has failed"
2729
2730         #remove foreign file
2731         rm $DIR/$tdir/${tfile}.new ||
2732                 error "$DIR/$tdir/${tfile}.new: remove of foreign file has failed"
2733         rm $DIR/$tdir/${tfile}2.new ||
2734                 error "$DIR/$tdir/${tfile}2.new: remove of foreign file has failed"
2735 }
2736 run_test 27J "basic ops on file with foreign LOV"
2737
2738 test_27K() {
2739         [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.12.49) ]] &&
2740                 skip "Need MDS version newer than 2.12.49"
2741
2742         test_mkdir $DIR/$tdir
2743         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2744         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2745
2746         # create foreign dir (raw way)
2747         create_foreign_dir -d $DIR/$tdir/$tdir -x "${uuid1}@${uuid2}" -t 1 ||
2748                 error "create_foreign_dir FAILED"
2749
2750         # verify foreign dir (raw way)
2751         parse_foreign_dir -d $DIR/$tdir/$tdir |
2752                 grep "lmv_foreign_magic:.*0xcd50cd0" ||
2753                 error "$DIR/$tdir/$tfile: invalid LMV EA magic"
2754         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_xattr_size:.*89$" ||
2755                 error "$DIR/$tdir/$tdir: invalid LMV EA size"
2756         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_type: 1$" ||
2757                 error "$DIR/$tdir/$tdir: invalid LMV EA type"
2758         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_flags: 0$" ||
2759                 error "$DIR/$tdir/$tdir: invalid LMV EA flags"
2760         local lmv=$(parse_foreign_dir -d $DIR/$tdir/$tdir |
2761                 grep "lmv_foreign_value: 0x" |
2762                 sed 's/lmv_foreign_value: 0x//')
2763         local lmv2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160 |
2764                 sed 's/ //g')
2765         [[ $lmv == $lmv2 ]] || error "$DIR/$tdir/$tdir: invalid LMV EA value"
2766
2767         # create foreign dir (lfs + API)
2768         $LFS mkdir --foreign=daos --xattr="${uuid1}@${uuid2}" --flags=0xda05 \
2769                 $DIR/$tdir/${tdir}2 ||
2770                 error "$DIR/$tdir/${tdir}2: create failed"
2771
2772         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2773                 grep "lfm_magic:.*0x0CD50CD0" ||
2774                 error "$DIR/$tdir/${tdir}2: invalid LMV EA magic"
2775         # lfm_length is LMV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2776         # - sizeof(lfm_type) - sizeof(lfm_flags)
2777         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_length:.*73" ||
2778                 error "$DIR/$tdir/${tdir}2: invalid LMV EA size"
2779         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_type:.*daos" ||
2780                 error "$DIR/$tdir/${tdir}2: invalid LMV EA type"
2781         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2782                 grep "lfm_flags:.*0x0000DA05" ||
2783                 error "$DIR/$tdir/${tdir}2: invalid LMV EA flags"
2784         $LFS getdirstripe $DIR/$tdir/${tdir}2 |
2785                 grep "lfm_value.*${uuid1}@${uuid2}" ||
2786                 error "$DIR/$tdir/${tdir}2: invalid LMV EA value"
2787
2788         # file create in dir should fail
2789         touch $DIR/$tdir/$tdir/$tfile && "$DIR/$tdir: file create should fail"
2790         touch $DIR/$tdir/${tdir}2/$tfile &&
2791                 "$DIR/${tdir}2: file create should fail"
2792
2793         # chmod should work
2794         chmod 777 $DIR/$tdir/$tdir ||
2795                 error "$DIR/$tdir: chmod failed"
2796         chmod 777 $DIR/$tdir/${tdir}2 ||
2797                 error "$DIR/${tdir}2: chmod failed"
2798
2799         # chown should work
2800         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tdir ||
2801                 error "$DIR/$tdir: chown failed"
2802         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tdir}2 ||
2803                 error "$DIR/${tdir}2: chown failed"
2804
2805         # rename should work
2806         mv $DIR/$tdir/$tdir $DIR/$tdir/${tdir}.new ||
2807                 error "$DIR/$tdir/$tdir: rename of foreign dir has failed"
2808         mv $DIR/$tdir/${tdir}2 $DIR/$tdir/${tdir}2.new ||
2809                 error "$DIR/$tdir/${tdir}2: rename of foreign dir has failed"
2810
2811         #remove foreign dir
2812         rmdir $DIR/$tdir/${tdir}.new ||
2813                 error "$DIR/$tdir/${tdir}.new: remove of foreign dir has failed"
2814         rmdir $DIR/$tdir/${tdir}2.new ||
2815                 error "$DIR/$tdir/${tdir}2.new: remove of foreign dir has failed"
2816 }
2817 run_test 27K "basic ops on dir with foreign LMV"
2818
2819 test_27L() {
2820         remote_mds_nodsh && skip "remote MDS with nodsh"
2821
2822         local POOL=${POOL:-$TESTNAME}
2823
2824         pool_add $POOL || error "pool_add failed"
2825
2826         lfs pool_list $MOUNT | grep -Fx "${FSNAME}.${POOL}" ||
2827                  error "pool_list does not contain ${FSNAME}.${POOL}:" \
2828                        "$(lfs pool_list $MOUNT | grep -F "${POOL}")"
2829 }
2830 run_test 27L "lfs pool_list gives correct pool name"
2831
2832 test_27M() {
2833         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.12.57) ]] &&
2834                 skip "Need MDS version >= than 2.12.57"
2835         remote_mds_nodsh && skip "remote MDS with nodsh"
2836         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2837
2838         test_mkdir $DIR/$tdir
2839
2840         # Set default striping on directory
2841         $LFS setstripe -C 4 $DIR/$tdir
2842
2843         echo 1 > $DIR/$tdir/${tfile}.1
2844         local count=$($LFS getstripe -c $DIR/$tdir/${tfile}.1)
2845         local setcount=4
2846         [ $count -eq $setcount ] ||
2847                 error "(1) stripe count $count, should be $setcount"
2848
2849         # Capture existing append_stripe_count setting for restore
2850         local orig_count=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_stripe_count)
2851         local mdts=$(comma_list $(mdts_nodes))
2852         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=$orig_count" EXIT
2853
2854         local appendcount=$orig_count
2855         echo 1 >> $DIR/$tdir/${tfile}.2_append
2856         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.2_append)
2857         [ $count -eq $appendcount ] ||
2858                 error "(2)stripe count $count, should be $appendcount for append"
2859
2860         # Disable O_APPEND striping, verify it works
2861         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2862
2863         # Should now get the default striping, which is 4
2864         setcount=4
2865         echo 1 >> $DIR/$tdir/${tfile}.3_append
2866         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.3_append)
2867         [ $count -eq $setcount ] ||
2868                 error "(3) stripe count $count, should be $setcount"
2869
2870         # Try changing the stripe count for append files
2871         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=2
2872
2873         # Append striping is now 2 (directory default is still 4)
2874         appendcount=2
2875         echo 1 >> $DIR/$tdir/${tfile}.4_append
2876         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.4_append)
2877         [ $count -eq $appendcount ] ||
2878                 error "(4) stripe count $count, should be $appendcount for append"
2879
2880         # Test append stripe count of -1
2881         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=-1
2882         appendcount=$OSTCOUNT
2883         echo 1 >> $DIR/$tdir/${tfile}.5
2884         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.5)
2885         [ $count -eq $appendcount ] ||
2886                 error "(5) stripe count $count, should be $appendcount for append"
2887
2888         # Set append striping back to default of 1
2889         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=1
2890
2891         # Try a new default striping, PFL + DOM
2892         $LFS setstripe -L mdt -E 1M -E -1 -c 2 $DIR/$tdir
2893
2894         # Create normal DOM file, DOM returns stripe count == 0
2895         setcount=0
2896         touch $DIR/$tdir/${tfile}.6
2897         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.6)
2898         [ $count -eq $setcount ] ||
2899                 error "(6) stripe count $count, should be $setcount"
2900
2901         # Show
2902         appendcount=1
2903         echo 1 >> $DIR/$tdir/${tfile}.7_append
2904         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.7_append)
2905         [ $count -eq $appendcount ] ||
2906                 error "(7) stripe count $count, should be $appendcount for append"
2907
2908         # Clean up DOM layout
2909         $LFS setstripe -d $DIR/$tdir
2910
2911         # Now test that append striping works when layout is from root
2912         $LFS setstripe -c 2 $MOUNT
2913         # Make a special directory for this
2914         mkdir $DIR/${tdir}/${tdir}.2
2915         stack_trap "$LFS setstripe -d $MOUNT" EXIT
2916
2917         # Verify for normal file
2918         setcount=2
2919         echo 1 > $DIR/${tdir}/${tdir}.2/${tfile}.8
2920         count=$($LFS getstripe -c $DIR/$tdir/${tdir}.2/${tfile}.8)
2921         [ $count -eq $setcount ] ||
2922                 error "(8) stripe count $count, should be $setcount"
2923
2924         appendcount=1
2925         echo 1 >> $DIR/${tdir}/${tdir}.2/${tfile}.9_append
2926         count=$($LFS getstripe -c $DIR/${tdir}/${tdir}.2/${tfile}.9_append)
2927         [ $count -eq $appendcount ] ||
2928                 error "(9) stripe count $count, should be $appendcount for append"
2929
2930         # Now test O_APPEND striping with pools
2931         do_nodes $mdts $LCTL set_param mdd.*.append_pool="$TESTNAME"
2932         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'" EXIT
2933
2934         # Create the pool
2935         pool_add $TESTNAME || error "pool creation failed"
2936         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
2937
2938         echo 1 >> $DIR/$tdir/${tfile}.10_append
2939
2940         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.10_append)
2941         [ "$pool" = "$TESTNAME" ] || error "(10) incorrect pool: $pool"
2942
2943         # Check that count is still correct
2944         appendcount=1
2945         echo 1 >> $DIR/$tdir/${tfile}.11_append
2946         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.11_append)
2947         [ $count -eq $appendcount ] ||
2948                 error "(11) stripe count $count, should be $appendcount for append"
2949
2950         # Disable O_APPEND stripe count, verify pool works separately
2951         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2952
2953         echo 1 >> $DIR/$tdir/${tfile}.12_append
2954
2955         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.12_append)
2956         [ "$pool" = "$TESTNAME" ] || error "(12) incorrect pool: $pool"
2957
2958         # Remove pool setting, verify it's not applied
2959         do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'
2960
2961         echo 1 >> $DIR/$tdir/${tfile}.13_append
2962
2963         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.13_append)
2964         [ "$pool" = "" ] || error "(13) pool found: $pool"
2965 }
2966 run_test 27M "test O_APPEND striping"
2967
2968 test_27N() {
2969         combined_mgs_mds && skip "needs separate MGS/MDT"
2970
2971         pool_add $TESTNAME || error "pool_add failed"
2972         do_facet mgs "$LCTL pool_list $FSNAME" |
2973                 grep -Fx "${FSNAME}.${TESTNAME}" ||
2974                 error "lctl pool_list on MGS failed"
2975 }
2976 run_test 27N "lctl pool_list on separate MGS gives correct pool name"
2977
2978 # createtest also checks that device nodes are created and
2979 # then visible correctly (#2091)
2980 test_28() { # bug 2091
2981         test_mkdir $DIR/d28
2982         $CREATETEST $DIR/d28/ct || error "createtest failed"
2983 }
2984 run_test 28 "create/mknod/mkdir with bad file types ============"
2985
2986 test_29() {
2987         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2988
2989         sync; sleep 1; sync # flush out any dirty pages from previous tests
2990         cancel_lru_locks
2991         test_mkdir $DIR/d29
2992         touch $DIR/d29/foo
2993         log 'first d29'
2994         ls -l $DIR/d29
2995
2996         declare -i LOCKCOUNTORIG=0
2997         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2998                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2999         done
3000         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
3001
3002         declare -i LOCKUNUSEDCOUNTORIG=0
3003         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3004                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
3005         done
3006
3007         log 'second d29'
3008         ls -l $DIR/d29
3009         log 'done'
3010
3011         declare -i LOCKCOUNTCURRENT=0
3012         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3013                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
3014         done
3015
3016         declare -i LOCKUNUSEDCOUNTCURRENT=0
3017         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3018                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
3019         done
3020
3021         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
3022                 $LCTL set_param -n ldlm.dump_namespaces ""
3023                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
3024                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3025                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3026                 return 2
3027         fi
3028         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
3029                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
3030                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3031                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3032                 return 3
3033         fi
3034 }
3035 run_test 29 "IT_GETATTR regression  ============================"
3036
3037 test_30a() { # was test_30
3038         cp $(which ls) $DIR || cp /bin/ls $DIR
3039         $DIR/ls / || error "Can't execute binary from lustre"
3040         rm $DIR/ls
3041 }
3042 run_test 30a "execute binary from Lustre (execve) =============="
3043
3044 test_30b() {
3045         cp `which ls` $DIR || cp /bin/ls $DIR
3046         chmod go+rx $DIR/ls
3047         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
3048         rm $DIR/ls
3049 }
3050 run_test 30b "execute binary from Lustre as non-root ==========="
3051
3052 test_30c() { # b=22376
3053         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3054
3055         cp `which ls` $DIR || cp /bin/ls $DIR
3056         chmod a-rw $DIR/ls
3057         cancel_lru_locks mdc
3058         cancel_lru_locks osc
3059         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
3060         rm -f $DIR/ls
3061 }
3062 run_test 30c "execute binary from Lustre without read perms ===="
3063
3064 test_31a() {
3065         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
3066         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
3067 }
3068 run_test 31a "open-unlink file =================================="
3069
3070 test_31b() {
3071         touch $DIR/f31 || error "touch $DIR/f31 failed"
3072         ln $DIR/f31 $DIR/f31b || error "ln failed"
3073         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
3074         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
3075 }
3076 run_test 31b "unlink file with multiple links while open ======="
3077
3078 test_31c() {
3079         touch $DIR/f31 || error "touch $DIR/f31 failed"
3080         ln $DIR/f31 $DIR/f31c || error "ln failed"
3081         multiop_bg_pause $DIR/f31 O_uc ||
3082                 error "multiop_bg_pause for $DIR/f31 failed"
3083         MULTIPID=$!
3084         $MULTIOP $DIR/f31c Ouc
3085         kill -USR1 $MULTIPID
3086         wait $MULTIPID
3087 }
3088 run_test 31c "open-unlink file with multiple links ============="
3089
3090 test_31d() {
3091         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
3092         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
3093 }
3094 run_test 31d "remove of open directory ========================="
3095
3096 test_31e() { # bug 2904
3097         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
3098 }
3099 run_test 31e "remove of open non-empty directory ==============="
3100
3101 test_31f() { # bug 4554
3102         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3103
3104         set -vx
3105         test_mkdir $DIR/d31f
3106         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3107         cp /etc/hosts $DIR/d31f
3108         ls -l $DIR/d31f
3109         $LFS getstripe $DIR/d31f/hosts
3110         multiop_bg_pause $DIR/d31f D_c || return 1
3111         MULTIPID=$!
3112
3113         rm -rv $DIR/d31f || error "first of $DIR/d31f"
3114         test_mkdir $DIR/d31f
3115         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3116         cp /etc/hosts $DIR/d31f
3117         ls -l $DIR/d31f
3118         $LFS getstripe $DIR/d31f/hosts
3119         multiop_bg_pause $DIR/d31f D_c || return 1
3120         MULTIPID2=$!
3121
3122         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
3123         wait $MULTIPID || error "first opendir $MULTIPID failed"
3124
3125         sleep 6
3126
3127         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
3128         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
3129         set +vx
3130 }
3131 run_test 31f "remove of open directory with open-unlink file ==="
3132
3133 test_31g() {
3134         echo "-- cross directory link --"
3135         test_mkdir -c1 $DIR/${tdir}ga
3136         test_mkdir -c1 $DIR/${tdir}gb
3137         touch $DIR/${tdir}ga/f
3138         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
3139         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
3140         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
3141         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
3142         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
3143 }
3144 run_test 31g "cross directory link==============="
3145
3146 test_31h() {
3147         echo "-- cross directory link --"
3148         test_mkdir -c1 $DIR/${tdir}
3149         test_mkdir -c1 $DIR/${tdir}/dir
3150         touch $DIR/${tdir}/f
3151         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
3152         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
3153         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
3154         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
3155         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
3156 }
3157 run_test 31h "cross directory link under child==============="
3158
3159 test_31i() {
3160         echo "-- cross directory link --"
3161         test_mkdir -c1 $DIR/$tdir
3162         test_mkdir -c1 $DIR/$tdir/dir
3163         touch $DIR/$tdir/dir/f
3164         ln $DIR/$tdir/dir/f $DIR/$tdir/g
3165         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
3166         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
3167         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
3168         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
3169 }
3170 run_test 31i "cross directory link under parent==============="
3171
3172 test_31j() {
3173         test_mkdir -c1 -p $DIR/$tdir
3174         test_mkdir -c1 -p $DIR/$tdir/dir1
3175         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
3176         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
3177         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
3178         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
3179         return 0
3180 }
3181 run_test 31j "link for directory==============="
3182
3183 test_31k() {
3184         test_mkdir -c1 -p $DIR/$tdir
3185         touch $DIR/$tdir/s
3186         touch $DIR/$tdir/exist
3187         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
3188         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
3189         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
3190         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
3191         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
3192         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
3193         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
3194         return 0
3195 }
3196 run_test 31k "link to file: the same, non-existing, dir==============="
3197
3198 test_31m() {
3199         mkdir $DIR/d31m
3200         touch $DIR/d31m/s
3201         mkdir $DIR/d31m2
3202         touch $DIR/d31m2/exist
3203         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
3204         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
3205         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
3206         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
3207         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
3208         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
3209         return 0
3210 }
3211 run_test 31m "link to file: the same, non-existing, dir==============="
3212
3213 test_31n() {
3214         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
3215         nlink=$(stat --format=%h $DIR/$tfile)
3216         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3217         local fd=$(free_fd)
3218         local cmd="exec $fd<$DIR/$tfile"
3219         eval $cmd
3220         cmd="exec $fd<&-"
3221         trap "eval $cmd" EXIT
3222         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3223         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3224         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
3225         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3226         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
3227         eval $cmd
3228 }
3229 run_test 31n "check link count of unlinked file"
3230
3231 link_one() {
3232         local tempfile=$(mktemp $1_XXXXXX)
3233         mlink $tempfile $1 2> /dev/null &&
3234                 echo "$BASHPID: link $tempfile to $1 succeeded"
3235         munlink $tempfile
3236 }
3237
3238 test_31o() { # LU-2901
3239         test_mkdir $DIR/$tdir
3240         for LOOP in $(seq 100); do
3241                 rm -f $DIR/$tdir/$tfile*
3242                 for THREAD in $(seq 8); do
3243                         link_one $DIR/$tdir/$tfile.$LOOP &
3244                 done
3245                 wait
3246                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
3247                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
3248                         error "$LINKS duplicate links to $tfile.$LOOP" &&
3249                         break || true
3250         done
3251 }
3252 run_test 31o "duplicate hard links with same filename"
3253
3254 test_31p() {
3255         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3256
3257         test_mkdir $DIR/$tdir
3258         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3259         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
3260
3261         opendirunlink $DIR/$tdir/striped_dir/test1 ||
3262                 error "open unlink test1 failed"
3263         opendirunlink $DIR/$tdir/striped_dir/test2 ||
3264                 error "open unlink test2 failed"
3265
3266         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
3267                 error "test1 still exists"
3268         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
3269                 error "test2 still exists"
3270 }
3271 run_test 31p "remove of open striped directory"
3272
3273 cleanup_test32_mount() {
3274         local rc=0
3275         trap 0
3276         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
3277         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
3278         losetup -d $loopdev || true
3279         rm -rf $DIR/$tdir
3280         return $rc
3281 }
3282
3283 test_32a() {
3284         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3285
3286         echo "== more mountpoints and symlinks ================="
3287         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3288         trap cleanup_test32_mount EXIT
3289         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3290         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3291                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3292         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
3293                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
3294         cleanup_test32_mount
3295 }
3296 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
3297
3298 test_32b() {
3299         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3300
3301         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3302         trap cleanup_test32_mount EXIT
3303         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3304         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3305                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3306         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
3307                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
3308         cleanup_test32_mount
3309 }
3310 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
3311
3312 test_32c() {
3313         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3314
3315         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3316         trap cleanup_test32_mount EXIT
3317         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3318         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3319                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3320         test_mkdir -p $DIR/$tdir/d2/test_dir
3321         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3322                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
3323         cleanup_test32_mount
3324 }
3325 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
3326
3327 test_32d() {
3328         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3329
3330         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3331         trap cleanup_test32_mount EXIT
3332         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3333         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3334                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3335         test_mkdir -p $DIR/$tdir/d2/test_dir
3336         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3337                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
3338         cleanup_test32_mount
3339 }
3340 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
3341
3342 test_32e() {
3343         rm -fr $DIR/$tdir
3344         test_mkdir -p $DIR/$tdir/tmp
3345         local tmp_dir=$DIR/$tdir/tmp
3346         ln -s $DIR/$tdir $tmp_dir/symlink11
3347         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3348         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
3349         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
3350 }
3351 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
3352
3353 test_32f() {
3354         rm -fr $DIR/$tdir
3355         test_mkdir -p $DIR/$tdir/tmp
3356         local tmp_dir=$DIR/$tdir/tmp
3357         ln -s $DIR/$tdir $tmp_dir/symlink11
3358         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3359         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
3360         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
3361 }
3362 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
3363
3364 test_32g() {
3365         local tmp_dir=$DIR/$tdir/tmp
3366         test_mkdir -p $tmp_dir
3367         test_mkdir $DIR/${tdir}2
3368         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3369         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3370         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
3371         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
3372         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
3373         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
3374 }
3375 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3376
3377 test_32h() {
3378         rm -fr $DIR/$tdir $DIR/${tdir}2
3379         tmp_dir=$DIR/$tdir/tmp
3380         test_mkdir -p $tmp_dir
3381         test_mkdir $DIR/${tdir}2
3382         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3383         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3384         ls $tmp_dir/symlink12 || error "listing symlink12"
3385         ls $DIR/$tdir/symlink02  || error "listing symlink02"
3386 }
3387 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3388
3389 test_32i() {
3390         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3391
3392         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3393         trap cleanup_test32_mount EXIT
3394         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3395         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3396                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3397         touch $DIR/$tdir/test_file
3398         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
3399                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
3400         cleanup_test32_mount
3401 }
3402 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
3403
3404 test_32j() {
3405         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3406
3407         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3408         trap cleanup_test32_mount EXIT
3409         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3410         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3411                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3412         touch $DIR/$tdir/test_file
3413         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
3414                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
3415         cleanup_test32_mount
3416 }
3417 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
3418
3419 test_32k() {
3420         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3421
3422         rm -fr $DIR/$tdir
3423         trap cleanup_test32_mount EXIT
3424         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3425         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3426                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3427         test_mkdir -p $DIR/$tdir/d2
3428         touch $DIR/$tdir/d2/test_file || error "touch failed"
3429         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3430                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
3431         cleanup_test32_mount
3432 }
3433 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
3434
3435 test_32l() {
3436         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3437
3438         rm -fr $DIR/$tdir
3439         trap cleanup_test32_mount EXIT
3440         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3441         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3442                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3443         test_mkdir -p $DIR/$tdir/d2
3444         touch $DIR/$tdir/d2/test_file || error "touch failed"
3445         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3446                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
3447         cleanup_test32_mount
3448 }
3449 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
3450
3451 test_32m() {
3452         rm -fr $DIR/d32m
3453         test_mkdir -p $DIR/d32m/tmp
3454         TMP_DIR=$DIR/d32m/tmp
3455         ln -s $DIR $TMP_DIR/symlink11
3456         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3457         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
3458                 error "symlink11 not a link"
3459         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
3460                 error "symlink01 not a link"
3461 }
3462 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
3463
3464 test_32n() {
3465         rm -fr $DIR/d32n
3466         test_mkdir -p $DIR/d32n/tmp
3467         TMP_DIR=$DIR/d32n/tmp
3468         ln -s $DIR $TMP_DIR/symlink11
3469         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3470         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
3471         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
3472 }
3473 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
3474
3475 test_32o() {
3476         touch $DIR/$tfile
3477         test_mkdir -p $DIR/d32o/tmp
3478         TMP_DIR=$DIR/d32o/tmp
3479         ln -s $DIR/$tfile $TMP_DIR/symlink12
3480         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3481         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
3482                 error "symlink12 not a link"
3483         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
3484         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
3485                 error "$DIR/d32o/tmp/symlink12 not file type"
3486         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
3487                 error "$DIR/d32o/symlink02 not file type"
3488 }
3489 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
3490
3491 test_32p() {
3492         log 32p_1
3493         rm -fr $DIR/d32p
3494         log 32p_2
3495         rm -f $DIR/$tfile
3496         log 32p_3
3497         touch $DIR/$tfile
3498         log 32p_4
3499         test_mkdir -p $DIR/d32p/tmp
3500         log 32p_5
3501         TMP_DIR=$DIR/d32p/tmp
3502         log 32p_6
3503         ln -s $DIR/$tfile $TMP_DIR/symlink12
3504         log 32p_7
3505         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3506         log 32p_8
3507         cat $DIR/d32p/tmp/symlink12 ||
3508                 error "Can't open $DIR/d32p/tmp/symlink12"
3509         log 32p_9
3510         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
3511         log 32p_10
3512 }
3513 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
3514
3515 test_32q() {
3516         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3517
3518         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3519         trap cleanup_test32_mount EXIT
3520         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3521         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3522         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3523                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3524         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
3525         cleanup_test32_mount
3526 }
3527 run_test 32q "stat follows mountpoints in Lustre (should return error)"
3528
3529 test_32r() {
3530         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3531
3532         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3533         trap cleanup_test32_mount EXIT
3534         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3535         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3536         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3537                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3538         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
3539         cleanup_test32_mount
3540 }
3541 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
3542
3543 test_33aa() {
3544         rm -f $DIR/$tfile
3545         touch $DIR/$tfile
3546         chmod 444 $DIR/$tfile
3547         chown $RUNAS_ID $DIR/$tfile
3548         log 33_1
3549         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3550         log 33_2
3551 }
3552 run_test 33aa "write file with mode 444 (should return error)"
3553
3554 test_33a() {
3555         rm -fr $DIR/$tdir
3556         test_mkdir $DIR/$tdir
3557         chown $RUNAS_ID $DIR/$tdir
3558         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
3559                 error "$RUNAS create $tdir/$tfile failed"
3560         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
3561                 error "open RDWR" || true
3562 }
3563 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
3564
3565 test_33b() {
3566         rm -fr $DIR/$tdir
3567         test_mkdir $DIR/$tdir
3568         chown $RUNAS_ID $DIR/$tdir
3569         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
3570 }
3571 run_test 33b "test open file with malformed flags (No panic)"
3572
3573 test_33c() {
3574         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3575         remote_ost_nodsh && skip "remote OST with nodsh"
3576
3577         local ostnum
3578         local ostname
3579         local write_bytes
3580         local all_zeros
3581
3582         all_zeros=:
3583         rm -fr $DIR/$tdir
3584         test_mkdir $DIR/$tdir
3585         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
3586
3587         sync
3588         for ostnum in $(seq $OSTCOUNT); do
3589                 # test-framework's OST numbering is one-based, while Lustre's
3590                 # is zero-based
3591                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3592                 # Parsing llobdstat's output sucks; we could grep the /proc
3593                 # path, but that's likely to not be as portable as using the
3594                 # llobdstat utility.  So we parse lctl output instead.
3595                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3596                         obdfilter/$ostname/stats |
3597                         awk '/^write_bytes/ {print $7}' )
3598                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
3599                 if (( ${write_bytes:-0} > 0 ))
3600                 then
3601                         all_zeros=false
3602                         break;
3603                 fi
3604         done
3605
3606         $all_zeros || return 0
3607
3608         # Write four bytes
3609         echo foo > $DIR/$tdir/bar
3610         # Really write them
3611         sync
3612
3613         # Total up write_bytes after writing.  We'd better find non-zeros.
3614         for ostnum in $(seq $OSTCOUNT); do
3615                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3616                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3617                         obdfilter/$ostname/stats |
3618                         awk '/^write_bytes/ {print $7}' )
3619                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
3620                 if (( ${write_bytes:-0} > 0 ))
3621                 then
3622                         all_zeros=false
3623                         break;
3624                 fi
3625         done
3626
3627         if $all_zeros
3628         then
3629                 for ostnum in $(seq $OSTCOUNT); do
3630                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3631                         echo "Check that write_bytes is present in obdfilter/*/stats:"
3632                         do_facet ost$ostnum lctl get_param -n \
3633                                 obdfilter/$ostname/stats
3634                 done
3635                 error "OST not keeping write_bytes stats (b22312)"
3636         fi
3637 }
3638 run_test 33c "test llobdstat and write_bytes"
3639
3640 test_33d() {
3641         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3642         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3643
3644         local MDTIDX=1
3645         local remote_dir=$DIR/$tdir/remote_dir
3646
3647         test_mkdir $DIR/$tdir
3648         $LFS mkdir -i $MDTIDX $remote_dir ||
3649                 error "create remote directory failed"
3650
3651         touch $remote_dir/$tfile
3652         chmod 444 $remote_dir/$tfile
3653         chown $RUNAS_ID $remote_dir/$tfile
3654
3655         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3656
3657         chown $RUNAS_ID $remote_dir
3658         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
3659                                         error "create" || true
3660         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
3661                                     error "open RDWR" || true
3662         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
3663 }
3664 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
3665
3666 test_33e() {
3667         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3668
3669         mkdir $DIR/$tdir
3670
3671         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3672         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3673         mkdir $DIR/$tdir/local_dir
3674
3675         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3676         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3677         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3678
3679         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3680                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
3681
3682         rmdir $DIR/$tdir/* || error "rmdir failed"
3683
3684         umask 777
3685         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3686         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3687         mkdir $DIR/$tdir/local_dir
3688
3689         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3690         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3691         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3692
3693         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3694                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3695
3696         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3697
3698         umask 000
3699         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3700         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3701         mkdir $DIR/$tdir/local_dir
3702
3703         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3704         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3705         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3706
3707         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3708                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3709 }
3710 run_test 33e "mkdir and striped directory should have same mode"
3711
3712 cleanup_33f() {
3713         trap 0
3714         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3715 }
3716
3717 test_33f() {
3718         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3719         remote_mds_nodsh && skip "remote MDS with nodsh"
3720
3721         mkdir $DIR/$tdir
3722         chmod go+rwx $DIR/$tdir
3723         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3724         trap cleanup_33f EXIT
3725
3726         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3727                 error "cannot create striped directory"
3728
3729         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3730                 error "cannot create files in striped directory"
3731
3732         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3733                 error "cannot remove files in striped directory"
3734
3735         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3736                 error "cannot remove striped directory"
3737
3738         cleanup_33f
3739 }
3740 run_test 33f "nonroot user can create, access, and remove a striped directory"
3741
3742 test_33g() {
3743         mkdir -p $DIR/$tdir/dir2
3744
3745         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3746         echo $err
3747         [[ $err =~ "exists" ]] || error "Not exists error"
3748 }
3749 run_test 33g "nonroot user create already existing root created file"
3750
3751 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3752 test_34a() {
3753         rm -f $DIR/f34
3754         $MCREATE $DIR/f34 || error "mcreate failed"
3755         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3756                 error "getstripe failed"
3757         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3758         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3759                 error "getstripe failed"
3760         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3761                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3762 }
3763 run_test 34a "truncate file that has not been opened ==========="
3764
3765 test_34b() {
3766         [ ! -f $DIR/f34 ] && test_34a
3767         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3768                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3769         $OPENFILE -f O_RDONLY $DIR/f34
3770         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3771                 error "getstripe failed"
3772         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3773                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3774 }
3775 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3776
3777 test_34c() {
3778         [ ! -f $DIR/f34 ] && test_34a
3779         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3780                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3781         $OPENFILE -f O_RDWR $DIR/f34
3782         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" &&
3783                 error "$LFS getstripe failed"
3784         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3785                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3786 }
3787 run_test 34c "O_RDWR opening file-with-size works =============="
3788
3789 test_34d() {
3790         [ ! -f $DIR/f34 ] && test_34a
3791         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3792                 error "dd failed"
3793         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3794                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3795         rm $DIR/f34
3796 }
3797 run_test 34d "write to sparse file ============================="
3798
3799 test_34e() {
3800         rm -f $DIR/f34e
3801         $MCREATE $DIR/f34e || error "mcreate failed"
3802         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3803         $CHECKSTAT -s 1000 $DIR/f34e ||
3804                 error "Size of $DIR/f34e not equal to 1000 bytes"
3805         $OPENFILE -f O_RDWR $DIR/f34e
3806         $CHECKSTAT -s 1000 $DIR/f34e ||
3807                 error "Size of $DIR/f34e not equal to 1000 bytes"
3808 }
3809 run_test 34e "create objects, some with size and some without =="
3810
3811 test_34f() { # bug 6242, 6243
3812         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3813
3814         SIZE34F=48000
3815         rm -f $DIR/f34f
3816         $MCREATE $DIR/f34f || error "mcreate failed"
3817         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3818         dd if=$DIR/f34f of=$TMP/f34f
3819         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3820         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3821         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3822         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3823         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3824 }
3825 run_test 34f "read from a file with no objects until EOF ======="
3826
3827 test_34g() {
3828         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3829
3830         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3831                 error "dd failed"
3832         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3833         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3834                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3835         cancel_lru_locks osc
3836         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3837                 error "wrong size after lock cancel"
3838
3839         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3840         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3841                 error "expanding truncate failed"
3842         cancel_lru_locks osc
3843         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3844                 error "wrong expanded size after lock cancel"
3845 }
3846 run_test 34g "truncate long file ==============================="
3847
3848 test_34h() {
3849         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3850
3851         local gid=10
3852         local sz=1000
3853
3854         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3855         sync # Flush the cache so that multiop below does not block on cache
3856              # flush when getting the group lock
3857         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3858         MULTIPID=$!
3859
3860         # Since just timed wait is not good enough, let's do a sync write
3861         # that way we are sure enough time for a roundtrip + processing
3862         # passed + 2 seconds of extra margin.
3863         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
3864         rm $DIR/${tfile}-1
3865         sleep 2
3866
3867         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3868                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3869                 kill -9 $MULTIPID
3870         fi
3871         wait $MULTIPID
3872         local nsz=`stat -c %s $DIR/$tfile`
3873         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3874 }
3875 run_test 34h "ftruncate file under grouplock should not block"
3876
3877 test_35a() {
3878         cp /bin/sh $DIR/f35a
3879         chmod 444 $DIR/f35a
3880         chown $RUNAS_ID $DIR/f35a
3881         $RUNAS $DIR/f35a && error || true
3882         rm $DIR/f35a
3883 }
3884 run_test 35a "exec file with mode 444 (should return and not leak)"
3885
3886 test_36a() {
3887         rm -f $DIR/f36
3888         utime $DIR/f36 || error "utime failed for MDS"
3889 }
3890 run_test 36a "MDS utime check (mknod, utime)"
3891
3892 test_36b() {
3893         echo "" > $DIR/f36
3894         utime $DIR/f36 || error "utime failed for OST"
3895 }
3896 run_test 36b "OST utime check (open, utime)"
3897
3898 test_36c() {
3899         rm -f $DIR/d36/f36
3900         test_mkdir $DIR/d36
3901         chown $RUNAS_ID $DIR/d36
3902         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3903 }
3904 run_test 36c "non-root MDS utime check (mknod, utime)"
3905
3906 test_36d() {
3907         [ ! -d $DIR/d36 ] && test_36c
3908         echo "" > $DIR/d36/f36
3909         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3910 }
3911 run_test 36d "non-root OST utime check (open, utime)"
3912
3913 test_36e() {
3914         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3915
3916         test_mkdir $DIR/$tdir
3917         touch $DIR/$tdir/$tfile
3918         $RUNAS utime $DIR/$tdir/$tfile &&
3919                 error "utime worked, expected failure" || true
3920 }
3921 run_test 36e "utime on non-owned file (should return error)"
3922
3923 subr_36fh() {
3924         local fl="$1"
3925         local LANG_SAVE=$LANG
3926         local LC_LANG_SAVE=$LC_LANG
3927         export LANG=C LC_LANG=C # for date language
3928
3929         DATESTR="Dec 20  2000"
3930         test_mkdir $DIR/$tdir
3931         lctl set_param fail_loc=$fl
3932         date; date +%s
3933         cp /etc/hosts $DIR/$tdir/$tfile
3934         sync & # write RPC generated with "current" inode timestamp, but delayed
3935         sleep 1
3936         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3937         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3938         cancel_lru_locks $OSC
3939         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3940         date; date +%s
3941         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3942                 echo "BEFORE: $LS_BEFORE" && \
3943                 echo "AFTER : $LS_AFTER" && \
3944                 echo "WANT  : $DATESTR" && \
3945                 error "$DIR/$tdir/$tfile timestamps changed" || true
3946
3947         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3948 }
3949
3950 test_36f() {
3951         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3952
3953         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3954         subr_36fh "0x80000214"
3955 }
3956 run_test 36f "utime on file racing with OST BRW write =========="
3957
3958 test_36g() {
3959         remote_ost_nodsh && skip "remote OST with nodsh"
3960         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3961         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
3962                 skip "Need MDS version at least 2.12.51"
3963
3964         local fmd_max_age
3965         local fmd
3966         local facet="ost1"
3967         local tgt="obdfilter"
3968
3969         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
3970
3971         test_mkdir $DIR/$tdir
3972         fmd_max_age=$(do_facet $facet \
3973                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
3974                 head -n 1")
3975
3976         echo "FMD max age: ${fmd_max_age}s"
3977         touch $DIR/$tdir/$tfile
3978         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3979                 gawk '{cnt=cnt+$1}  END{print cnt}')
3980         echo "FMD before: $fmd"
3981         [[ $fmd == 0 ]] &&
3982                 error "FMD wasn't create by touch"
3983         sleep $((fmd_max_age + 12))
3984         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3985                 gawk '{cnt=cnt+$1}  END{print cnt}')
3986         echo "FMD after: $fmd"
3987         [[ $fmd == 0 ]] ||
3988                 error "FMD wasn't expired by ping"
3989 }
3990 run_test 36g "FMD cache expiry ====================="
3991
3992 test_36h() {
3993         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3994
3995         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3996         subr_36fh "0x80000227"
3997 }
3998 run_test 36h "utime on file racing with OST BRW write =========="
3999
4000 test_36i() {
4001         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4002
4003         test_mkdir $DIR/$tdir
4004         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
4005
4006         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
4007         local new_mtime=$((mtime + 200))
4008
4009         #change Modify time of striped dir
4010         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
4011                         error "change mtime failed"
4012
4013         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
4014
4015         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
4016 }
4017 run_test 36i "change mtime on striped directory"
4018
4019 # test_37 - duplicate with tests 32q 32r
4020
4021 test_38() {
4022         local file=$DIR/$tfile
4023         touch $file
4024         openfile -f O_DIRECTORY $file
4025         local RC=$?
4026         local ENOTDIR=20
4027         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
4028         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
4029 }
4030 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
4031
4032 test_39a() { # was test_39
4033         touch $DIR/$tfile
4034         touch $DIR/${tfile}2
4035 #       ls -l  $DIR/$tfile $DIR/${tfile}2
4036 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
4037 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
4038         sleep 2
4039         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
4040         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
4041                 echo "mtime"
4042                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
4043                 echo "atime"
4044                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
4045                 echo "ctime"
4046                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
4047                 error "O_TRUNC didn't change timestamps"
4048         fi
4049 }
4050 run_test 39a "mtime changed on create"
4051
4052 test_39b() {
4053         test_mkdir -c1 $DIR/$tdir
4054         cp -p /etc/passwd $DIR/$tdir/fopen
4055         cp -p /etc/passwd $DIR/$tdir/flink
4056         cp -p /etc/passwd $DIR/$tdir/funlink
4057         cp -p /etc/passwd $DIR/$tdir/frename
4058         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
4059
4060         sleep 1
4061         echo "aaaaaa" >> $DIR/$tdir/fopen
4062         echo "aaaaaa" >> $DIR/$tdir/flink
4063         echo "aaaaaa" >> $DIR/$tdir/funlink
4064         echo "aaaaaa" >> $DIR/$tdir/frename
4065
4066         local open_new=`stat -c %Y $DIR/$tdir/fopen`
4067         local link_new=`stat -c %Y $DIR/$tdir/flink`
4068         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
4069         local rename_new=`stat -c %Y $DIR/$tdir/frename`
4070
4071         cat $DIR/$tdir/fopen > /dev/null
4072         ln $DIR/$tdir/flink $DIR/$tdir/flink2
4073         rm -f $DIR/$tdir/funlink2
4074         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
4075
4076         for (( i=0; i < 2; i++ )) ; do
4077                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
4078                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
4079                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
4080                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
4081
4082                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
4083                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
4084                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
4085                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
4086
4087                 cancel_lru_locks $OSC
4088                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4089         done
4090 }
4091 run_test 39b "mtime change on open, link, unlink, rename  ======"
4092
4093 # this should be set to past
4094 TEST_39_MTIME=`date -d "1 year ago" +%s`
4095
4096 # bug 11063
4097 test_39c() {
4098         touch $DIR1/$tfile
4099         sleep 2
4100         local mtime0=`stat -c %Y $DIR1/$tfile`
4101
4102         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4103         local mtime1=`stat -c %Y $DIR1/$tfile`
4104         [ "$mtime1" = $TEST_39_MTIME ] || \
4105                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
4106
4107         local d1=`date +%s`
4108         echo hello >> $DIR1/$tfile
4109         local d2=`date +%s`
4110         local mtime2=`stat -c %Y $DIR1/$tfile`
4111         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
4112                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
4113
4114         mv $DIR1/$tfile $DIR1/$tfile-1
4115
4116         for (( i=0; i < 2; i++ )) ; do
4117                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
4118                 [ "$mtime2" = "$mtime3" ] || \
4119                         error "mtime ($mtime2) changed (to $mtime3) on rename"
4120
4121                 cancel_lru_locks $OSC
4122                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4123         done
4124 }
4125 run_test 39c "mtime change on rename ==========================="
4126
4127 # bug 21114
4128 test_39d() {
4129         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4130
4131         touch $DIR1/$tfile
4132         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4133
4134         for (( i=0; i < 2; i++ )) ; do
4135                 local mtime=`stat -c %Y $DIR1/$tfile`
4136                 [ $mtime = $TEST_39_MTIME ] || \
4137                         error "mtime($mtime) is not set to $TEST_39_MTIME"
4138
4139                 cancel_lru_locks $OSC
4140                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4141         done
4142 }
4143 run_test 39d "create, utime, stat =============================="
4144
4145 # bug 21114
4146 test_39e() {
4147         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4148
4149         touch $DIR1/$tfile
4150         local mtime1=`stat -c %Y $DIR1/$tfile`
4151
4152         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4153
4154         for (( i=0; i < 2; i++ )) ; do
4155                 local mtime2=`stat -c %Y $DIR1/$tfile`
4156                 [ $mtime2 = $TEST_39_MTIME ] || \
4157                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4158
4159                 cancel_lru_locks $OSC
4160                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4161         done
4162 }
4163 run_test 39e "create, stat, utime, stat ========================"
4164
4165 # bug 21114
4166 test_39f() {
4167         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4168
4169         touch $DIR1/$tfile
4170         mtime1=`stat -c %Y $DIR1/$tfile`
4171
4172         sleep 2
4173         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4174
4175         for (( i=0; i < 2; i++ )) ; do
4176                 local mtime2=`stat -c %Y $DIR1/$tfile`
4177                 [ $mtime2 = $TEST_39_MTIME ] || \
4178                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4179
4180                 cancel_lru_locks $OSC
4181                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4182         done
4183 }
4184 run_test 39f "create, stat, sleep, utime, stat ================="
4185
4186 # bug 11063
4187 test_39g() {
4188         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4189
4190         echo hello >> $DIR1/$tfile
4191         local mtime1=`stat -c %Y $DIR1/$tfile`
4192
4193         sleep 2
4194         chmod o+r $DIR1/$tfile
4195
4196         for (( i=0; i < 2; i++ )) ; do
4197                 local mtime2=`stat -c %Y $DIR1/$tfile`
4198                 [ "$mtime1" = "$mtime2" ] || \
4199                         error "lost mtime: $mtime2, should be $mtime1"
4200
4201                 cancel_lru_locks $OSC
4202                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4203         done
4204 }
4205 run_test 39g "write, chmod, stat ==============================="
4206
4207 # bug 11063
4208 test_39h() {
4209         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4210
4211         touch $DIR1/$tfile
4212         sleep 1
4213
4214         local d1=`date`
4215         echo hello >> $DIR1/$tfile
4216         local mtime1=`stat -c %Y $DIR1/$tfile`
4217
4218         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4219         local d2=`date`
4220         if [ "$d1" != "$d2" ]; then
4221                 echo "write and touch not within one second"
4222         else
4223                 for (( i=0; i < 2; i++ )) ; do
4224                         local mtime2=`stat -c %Y $DIR1/$tfile`
4225                         [ "$mtime2" = $TEST_39_MTIME ] || \
4226                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
4227
4228                         cancel_lru_locks $OSC
4229                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4230                 done
4231         fi
4232 }
4233 run_test 39h "write, utime within one second, stat ============="
4234
4235 test_39i() {
4236         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4237
4238         touch $DIR1/$tfile
4239         sleep 1
4240
4241         echo hello >> $DIR1/$tfile
4242         local mtime1=`stat -c %Y $DIR1/$tfile`
4243
4244         mv $DIR1/$tfile $DIR1/$tfile-1
4245
4246         for (( i=0; i < 2; i++ )) ; do
4247                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4248
4249                 [ "$mtime1" = "$mtime2" ] || \
4250                         error "lost mtime: $mtime2, should be $mtime1"
4251
4252                 cancel_lru_locks $OSC
4253                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4254         done
4255 }
4256 run_test 39i "write, rename, stat =============================="
4257
4258 test_39j() {
4259         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4260
4261         start_full_debug_logging
4262         touch $DIR1/$tfile
4263         sleep 1
4264
4265         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
4266         lctl set_param fail_loc=0x80000412
4267         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
4268                 error "multiop failed"
4269         local multipid=$!
4270         local mtime1=`stat -c %Y $DIR1/$tfile`
4271
4272         mv $DIR1/$tfile $DIR1/$tfile-1
4273
4274         kill -USR1 $multipid
4275         wait $multipid || error "multiop close failed"
4276
4277         for (( i=0; i < 2; i++ )) ; do
4278                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4279                 [ "$mtime1" = "$mtime2" ] ||
4280                         error "mtime is lost on close: $mtime2, " \
4281                               "should be $mtime1"
4282
4283                 cancel_lru_locks
4284                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4285         done
4286         lctl set_param fail_loc=0
4287         stop_full_debug_logging
4288 }
4289 run_test 39j "write, rename, close, stat ======================="
4290
4291 test_39k() {
4292         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4293
4294         touch $DIR1/$tfile
4295         sleep 1
4296
4297         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
4298         local multipid=$!
4299         local mtime1=`stat -c %Y $DIR1/$tfile`
4300
4301         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4302
4303         kill -USR1 $multipid
4304         wait $multipid || error "multiop close failed"
4305
4306         for (( i=0; i < 2; i++ )) ; do
4307                 local mtime2=`stat -c %Y $DIR1/$tfile`
4308
4309                 [ "$mtime2" = $TEST_39_MTIME ] || \
4310                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
4311
4312                 cancel_lru_locks
4313                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4314         done
4315 }
4316 run_test 39k "write, utime, close, stat ========================"
4317
4318 # this should be set to future
4319 TEST_39_ATIME=`date -d "1 year" +%s`
4320
4321 test_39l() {
4322         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4323         remote_mds_nodsh && skip "remote MDS with nodsh"
4324
4325         local atime_diff=$(do_facet $SINGLEMDS \
4326                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4327         rm -rf $DIR/$tdir
4328         mkdir -p $DIR/$tdir
4329
4330         # test setting directory atime to future
4331         touch -a -d @$TEST_39_ATIME $DIR/$tdir
4332         local atime=$(stat -c %X $DIR/$tdir)
4333         [ "$atime" = $TEST_39_ATIME ] ||
4334                 error "atime is not set to future: $atime, $TEST_39_ATIME"
4335
4336         # test setting directory atime from future to now
4337         local now=$(date +%s)
4338         touch -a -d @$now $DIR/$tdir
4339
4340         atime=$(stat -c %X $DIR/$tdir)
4341         [ "$atime" -eq "$now"  ] ||
4342                 error "atime is not updated from future: $atime, $now"
4343
4344         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
4345         sleep 3
4346
4347         # test setting directory atime when now > dir atime + atime_diff
4348         local d1=$(date +%s)
4349         ls $DIR/$tdir
4350         local d2=$(date +%s)
4351         cancel_lru_locks mdc
4352         atime=$(stat -c %X $DIR/$tdir)
4353         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4354                 error "atime is not updated  : $atime, should be $d2"
4355
4356         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
4357         sleep 3
4358
4359         # test not setting directory atime when now < dir atime + atime_diff
4360         ls $DIR/$tdir
4361         cancel_lru_locks mdc
4362         atime=$(stat -c %X $DIR/$tdir)
4363         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4364                 error "atime is updated to $atime, should remain $d1<atime<$d2"
4365
4366         do_facet $SINGLEMDS \
4367                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4368 }
4369 run_test 39l "directory atime update ==========================="
4370
4371 test_39m() {
4372         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4373
4374         touch $DIR1/$tfile
4375         sleep 2
4376         local far_past_mtime=$(date -d "May 29 1953" +%s)
4377         local far_past_atime=$(date -d "Dec 17 1903" +%s)
4378
4379         touch -m -d @$far_past_mtime $DIR1/$tfile
4380         touch -a -d @$far_past_atime $DIR1/$tfile
4381
4382         for (( i=0; i < 2; i++ )) ; do
4383                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
4384                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
4385                         error "atime or mtime set incorrectly"
4386
4387                 cancel_lru_locks $OSC
4388                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4389         done
4390 }
4391 run_test 39m "test atime and mtime before 1970"
4392
4393 test_39n() { # LU-3832
4394         remote_mds_nodsh && skip "remote MDS with nodsh"
4395
4396         local atime_diff=$(do_facet $SINGLEMDS \
4397                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4398         local atime0
4399         local atime1
4400         local atime2
4401
4402         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
4403
4404         rm -rf $DIR/$tfile
4405         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
4406         atime0=$(stat -c %X $DIR/$tfile)
4407
4408         sleep 5
4409         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4410         atime1=$(stat -c %X $DIR/$tfile)
4411
4412         sleep 5
4413         cancel_lru_locks mdc
4414         cancel_lru_locks osc
4415         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4416         atime2=$(stat -c %X $DIR/$tfile)
4417
4418         do_facet $SINGLEMDS \
4419                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4420
4421         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
4422         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
4423 }
4424 run_test 39n "check that O_NOATIME is honored"
4425
4426 test_39o() {
4427         TESTDIR=$DIR/$tdir/$tfile
4428         [ -e $TESTDIR ] && rm -rf $TESTDIR
4429         mkdir -p $TESTDIR
4430         cd $TESTDIR
4431         links1=2
4432         ls
4433         mkdir a b
4434         ls
4435         links2=$(stat -c %h .)
4436         [ $(($links1 + 2)) != $links2 ] &&
4437                 error "wrong links count $(($links1 + 2)) != $links2"
4438         rmdir b
4439         links3=$(stat -c %h .)
4440         [ $(($links1 + 1)) != $links3 ] &&
4441                 error "wrong links count $links1 != $links3"
4442         return 0
4443 }
4444 run_test 39o "directory cached attributes updated after create"
4445
4446 test_39p() {
4447         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4448
4449         local MDTIDX=1
4450         TESTDIR=$DIR/$tdir/$tdir
4451         [ -e $TESTDIR ] && rm -rf $TESTDIR
4452         test_mkdir -p $TESTDIR
4453         cd $TESTDIR
4454         links1=2
4455         ls
4456         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
4457         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
4458         ls
4459         links2=$(stat -c %h .)
4460         [ $(($links1 + 2)) != $links2 ] &&
4461                 error "wrong links count $(($links1 + 2)) != $links2"
4462         rmdir remote_dir2
4463         links3=$(stat -c %h .)
4464         [ $(($links1 + 1)) != $links3 ] &&
4465                 error "wrong links count $links1 != $links3"
4466         return 0
4467 }
4468 run_test 39p "remote directory cached attributes updated after create ========"
4469
4470
4471 test_39q() { # LU-8041
4472         local testdir=$DIR/$tdir
4473         mkdir -p $testdir
4474         multiop_bg_pause $testdir D_c || error "multiop failed"
4475         local multipid=$!
4476         cancel_lru_locks mdc
4477         kill -USR1 $multipid
4478         local atime=$(stat -c %X $testdir)
4479         [ "$atime" -ne 0 ] || error "atime is zero"
4480 }
4481 run_test 39q "close won't zero out atime"
4482
4483 test_40() {
4484         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
4485         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
4486                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
4487         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
4488                 error "$tfile is not 4096 bytes in size"
4489 }
4490 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
4491
4492 test_41() {
4493         # bug 1553
4494         small_write $DIR/f41 18
4495 }
4496 run_test 41 "test small file write + fstat ====================="
4497
4498 count_ost_writes() {
4499         lctl get_param -n ${OSC}.*.stats |
4500                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
4501                         END { printf("%0.0f", writes) }'
4502 }
4503
4504 # decent default
4505 WRITEBACK_SAVE=500
4506 DIRTY_RATIO_SAVE=40
4507 MAX_DIRTY_RATIO=50
4508 BG_DIRTY_RATIO_SAVE=10
4509 MAX_BG_DIRTY_RATIO=25
4510
4511 start_writeback() {
4512         trap 0
4513         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
4514         # dirty_ratio, dirty_background_ratio
4515         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4516                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
4517                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
4518                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
4519         else
4520                 # if file not here, we are a 2.4 kernel
4521                 kill -CONT `pidof kupdated`
4522         fi
4523 }
4524
4525 stop_writeback() {
4526         # setup the trap first, so someone cannot exit the test at the
4527         # exact wrong time and mess up a machine
4528         trap start_writeback EXIT
4529         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
4530         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4531                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
4532                 sysctl -w vm.dirty_writeback_centisecs=0
4533                 sysctl -w vm.dirty_writeback_centisecs=0
4534                 # save and increase /proc/sys/vm/dirty_ratio
4535                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
4536                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
4537                 # save and increase /proc/sys/vm/dirty_background_ratio
4538                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
4539                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
4540         else
4541                 # if file not here, we are a 2.4 kernel
4542                 kill -STOP `pidof kupdated`
4543         fi
4544 }
4545
4546 # ensure that all stripes have some grant before we test client-side cache
4547 setup_test42() {
4548         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
4549                 dd if=/dev/zero of=$i bs=4k count=1
4550                 rm $i
4551         done
4552 }
4553
4554 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
4555 # file truncation, and file removal.
4556 test_42a() {
4557         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4558
4559         setup_test42
4560         cancel_lru_locks $OSC
4561         stop_writeback
4562         sync; sleep 1; sync # just to be safe
4563         BEFOREWRITES=`count_ost_writes`
4564         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
4565         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
4566         AFTERWRITES=`count_ost_writes`
4567         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
4568                 error "$BEFOREWRITES < $AFTERWRITES"
4569         start_writeback
4570 }
4571 run_test 42a "ensure that we don't flush on close"
4572
4573 test_42b() {
4574         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4575
4576         setup_test42
4577         cancel_lru_locks $OSC
4578         stop_writeback
4579         sync
4580         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
4581         BEFOREWRITES=$(count_ost_writes)
4582         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
4583         AFTERWRITES=$(count_ost_writes)
4584         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4585                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
4586         fi
4587         BEFOREWRITES=$(count_ost_writes)
4588         sync || error "sync: $?"
4589         AFTERWRITES=$(count_ost_writes)
4590         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4591                 error "$BEFOREWRITES < $AFTERWRITES on sync"
4592         fi
4593         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
4594         start_writeback
4595         return 0
4596 }
4597 run_test 42b "test destroy of file with cached dirty data ======"
4598
4599 # if these tests just want to test the effect of truncation,
4600 # they have to be very careful.  consider:
4601 # - the first open gets a {0,EOF}PR lock
4602 # - the first write conflicts and gets a {0, count-1}PW
4603 # - the rest of the writes are under {count,EOF}PW
4604 # - the open for truncate tries to match a {0,EOF}PR
4605 #   for the filesize and cancels the PWs.
4606 # any number of fixes (don't get {0,EOF} on open, match
4607 # composite locks, do smarter file size management) fix
4608 # this, but for now we want these tests to verify that
4609 # the cancellation with truncate intent works, so we
4610 # start the file with a full-file pw lock to match against
4611 # until the truncate.
4612 trunc_test() {
4613         test=$1
4614         file=$DIR/$test
4615         offset=$2
4616         cancel_lru_locks $OSC
4617         stop_writeback
4618         # prime the file with 0,EOF PW to match
4619         touch $file
4620         $TRUNCATE $file 0
4621         sync; sync
4622         # now the real test..
4623         dd if=/dev/zero of=$file bs=1024 count=100
4624         BEFOREWRITES=`count_ost_writes`
4625         $TRUNCATE $file $offset
4626         cancel_lru_locks $OSC
4627         AFTERWRITES=`count_ost_writes`
4628         start_writeback
4629 }
4630
4631 test_42c() {
4632         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4633
4634         trunc_test 42c 1024
4635         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
4636                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
4637         rm $file
4638 }
4639 run_test 42c "test partial truncate of file with cached dirty data"
4640
4641 test_42d() {
4642         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4643
4644         trunc_test 42d 0
4645         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
4646                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
4647         rm $file
4648 }
4649 run_test 42d "test complete truncate of file with cached dirty data"
4650
4651 test_42e() { # bug22074
4652         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4653
4654         local TDIR=$DIR/${tdir}e
4655         local pages=16 # hardcoded 16 pages, don't change it.
4656         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
4657         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
4658         local max_dirty_mb
4659         local warmup_files
4660
4661         test_mkdir $DIR/${tdir}e
4662         $LFS setstripe -c 1 $TDIR
4663         createmany -o $TDIR/f $files
4664
4665         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
4666
4667         # we assume that with $OSTCOUNT files, at least one of them will
4668         # be allocated on OST0.
4669         warmup_files=$((OSTCOUNT * max_dirty_mb))
4670         createmany -o $TDIR/w $warmup_files
4671
4672         # write a large amount of data into one file and sync, to get good
4673         # avail_grant number from OST.
4674         for ((i=0; i<$warmup_files; i++)); do
4675                 idx=$($LFS getstripe -i $TDIR/w$i)
4676                 [ $idx -ne 0 ] && continue
4677                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
4678                 break
4679         done
4680         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
4681         sync
4682         $LCTL get_param $proc_osc0/cur_dirty_bytes
4683         $LCTL get_param $proc_osc0/cur_grant_bytes
4684
4685         # create as much dirty pages as we can while not to trigger the actual
4686         # RPCs directly. but depends on the env, VFS may trigger flush during this
4687         # period, hopefully we are good.
4688         for ((i=0; i<$warmup_files; i++)); do
4689                 idx=$($LFS getstripe -i $TDIR/w$i)
4690                 [ $idx -ne 0 ] && continue
4691                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4692         done
4693         $LCTL get_param $proc_osc0/cur_dirty_bytes
4694         $LCTL get_param $proc_osc0/cur_grant_bytes
4695
4696         # perform the real test
4697         $LCTL set_param $proc_osc0/rpc_stats 0
4698         for ((;i<$files; i++)); do
4699                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
4700                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
4701         done
4702         sync
4703         $LCTL get_param $proc_osc0/rpc_stats
4704
4705         local percent=0
4706         local have_ppr=false
4707         $LCTL get_param $proc_osc0/rpc_stats |
4708                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4709                         # skip lines until we are at the RPC histogram data
4710                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4711                         $have_ppr || continue
4712
4713                         # we only want the percent stat for < 16 pages
4714                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4715
4716                         percent=$((percent + WPCT))
4717                         if [[ $percent -gt 15 ]]; then
4718                                 error "less than 16-pages write RPCs" \
4719                                       "$percent% > 15%"
4720                                 break
4721                         fi
4722                 done
4723         rm -rf $TDIR
4724 }
4725 run_test 42e "verify sub-RPC writes are not done synchronously"
4726
4727 test_43A() { # was test_43
4728         test_mkdir $DIR/$tdir
4729         cp -p /bin/ls $DIR/$tdir/$tfile
4730         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4731         pid=$!
4732         # give multiop a chance to open
4733         sleep 1
4734
4735         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4736         kill -USR1 $pid
4737 }
4738 run_test 43A "execution of file opened for write should return -ETXTBSY"
4739
4740 test_43a() {
4741         test_mkdir $DIR/$tdir
4742         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4743         $DIR/$tdir/sleep 60 &
4744         SLEEP_PID=$!
4745         # Make sure exec of $tdir/sleep wins race with truncate
4746         sleep 1
4747         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
4748         kill $SLEEP_PID
4749 }
4750 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4751
4752 test_43b() {
4753         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4754
4755         test_mkdir $DIR/$tdir
4756         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4757         $DIR/$tdir/sleep 60 &
4758         SLEEP_PID=$!
4759         # Make sure exec of $tdir/sleep wins race with truncate
4760         sleep 1
4761         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
4762         kill $SLEEP_PID
4763 }
4764 run_test 43b "truncate of file being executed should return -ETXTBSY"
4765
4766 test_43c() {
4767         local testdir="$DIR/$tdir"
4768         test_mkdir $testdir
4769         cp $SHELL $testdir/
4770         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4771                 ( cd $testdir && md5sum -c )
4772 }
4773 run_test 43c "md5sum of copy into lustre"
4774
4775 test_44A() { # was test_44
4776         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4777
4778         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4779         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4780 }
4781 run_test 44A "zero length read from a sparse stripe"
4782
4783 test_44a() {
4784         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4785                 awk '{ print $2 }')
4786         [ -z "$nstripe" ] && skip "can't get stripe info"
4787         [[ $nstripe -gt $OSTCOUNT ]] &&
4788                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4789
4790         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4791                 awk '{ print $2 }')
4792         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4793                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4794                         awk '{ print $2 }')
4795         fi
4796
4797         OFFSETS="0 $((stride/2)) $((stride-1))"
4798         for offset in $OFFSETS; do
4799                 for i in $(seq 0 $((nstripe-1))); do
4800                         local GLOBALOFFSETS=""
4801                         # size in Bytes
4802                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4803                         local myfn=$DIR/d44a-$size
4804                         echo "--------writing $myfn at $size"
4805                         ll_sparseness_write $myfn $size ||
4806                                 error "ll_sparseness_write"
4807                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4808                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4809                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4810
4811                         for j in $(seq 0 $((nstripe-1))); do
4812                                 # size in Bytes
4813                                 size=$((((j + $nstripe )*$stride + $offset)))
4814                                 ll_sparseness_write $myfn $size ||
4815                                         error "ll_sparseness_write"
4816                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4817                         done
4818                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4819                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4820                         rm -f $myfn
4821                 done
4822         done
4823 }
4824 run_test 44a "test sparse pwrite ==============================="
4825
4826 dirty_osc_total() {
4827         tot=0
4828         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4829                 tot=$(($tot + $d))
4830         done
4831         echo $tot
4832 }
4833 do_dirty_record() {
4834         before=`dirty_osc_total`
4835         echo executing "\"$*\""
4836         eval $*
4837         after=`dirty_osc_total`
4838         echo before $before, after $after
4839 }
4840 test_45() {
4841         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4842
4843         f="$DIR/f45"
4844         # Obtain grants from OST if it supports it
4845         echo blah > ${f}_grant
4846         stop_writeback
4847         sync
4848         do_dirty_record "echo blah > $f"
4849         [[ $before -eq $after ]] && error "write wasn't cached"
4850         do_dirty_record "> $f"
4851         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4852         do_dirty_record "echo blah > $f"
4853         [[ $before -eq $after ]] && error "write wasn't cached"
4854         do_dirty_record "sync"
4855         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4856         do_dirty_record "echo blah > $f"
4857         [[ $before -eq $after ]] && error "write wasn't cached"
4858         do_dirty_record "cancel_lru_locks osc"
4859         [[ $before -gt $after ]] ||
4860                 error "lock cancellation didn't lower dirty count"
4861         start_writeback
4862 }
4863 run_test 45 "osc io page accounting ============================"
4864
4865 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4866 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4867 # objects offset and an assert hit when an rpc was built with 1023's mapped
4868 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4869 test_46() {
4870         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4871
4872         f="$DIR/f46"
4873         stop_writeback
4874         sync
4875         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4876         sync
4877         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
4878         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4879         sync
4880         start_writeback
4881 }
4882 run_test 46 "dirtying a previously written page ================"
4883
4884 # test_47 is removed "Device nodes check" is moved to test_28
4885
4886 test_48a() { # bug 2399
4887         [ "$mds1_FSTYPE" = "zfs" ] &&
4888         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
4889                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
4890
4891         test_mkdir $DIR/$tdir
4892         cd $DIR/$tdir
4893         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
4894         test_mkdir $DIR/$tdir
4895         touch foo || error "'touch foo' failed after recreating cwd"
4896         test_mkdir bar
4897         touch .foo || error "'touch .foo' failed after recreating cwd"
4898         test_mkdir .bar
4899         ls . > /dev/null || error "'ls .' failed after recreating cwd"
4900         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4901         cd . || error "'cd .' failed after recreating cwd"
4902         mkdir . && error "'mkdir .' worked after recreating cwd"
4903         rmdir . && error "'rmdir .' worked after recreating cwd"
4904         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4905         cd .. || error "'cd ..' failed after recreating cwd"
4906 }
4907 run_test 48a "Access renamed working dir (should return errors)="
4908
4909 test_48b() { # bug 2399
4910         rm -rf $DIR/$tdir
4911         test_mkdir $DIR/$tdir
4912         cd $DIR/$tdir
4913         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4914         touch foo && error "'touch foo' worked after removing cwd"
4915         mkdir foo && error "'mkdir foo' worked after removing cwd"
4916         touch .foo && error "'touch .foo' worked after removing cwd"
4917         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4918         ls . > /dev/null && error "'ls .' worked after removing cwd"
4919         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4920         mkdir . && error "'mkdir .' worked after removing cwd"
4921         rmdir . && error "'rmdir .' worked after removing cwd"
4922         ln -s . foo && error "'ln -s .' worked after removing cwd"
4923         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4924 }
4925 run_test 48b "Access removed working dir (should return errors)="
4926
4927 test_48c() { # bug 2350
4928         #lctl set_param debug=-1
4929         #set -vx
4930         rm -rf $DIR/$tdir
4931         test_mkdir -p $DIR/$tdir/dir
4932         cd $DIR/$tdir/dir
4933         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4934         $TRACE touch foo && error "touch foo worked after removing cwd"
4935         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4936         touch .foo && error "touch .foo worked after removing cwd"
4937         mkdir .foo && error "mkdir .foo worked after removing cwd"
4938         $TRACE ls . && error "'ls .' worked after removing cwd"
4939         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4940         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4941         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4942         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4943         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4944 }
4945 run_test 48c "Access removed working subdir (should return errors)"
4946
4947 test_48d() { # bug 2350
4948         #lctl set_param debug=-1
4949         #set -vx
4950         rm -rf $DIR/$tdir
4951         test_mkdir -p $DIR/$tdir/dir
4952         cd $DIR/$tdir/dir
4953         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4954         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4955         $TRACE touch foo && error "'touch foo' worked after removing parent"
4956         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4957         touch .foo && error "'touch .foo' worked after removing parent"
4958         mkdir .foo && error "mkdir .foo worked after removing parent"
4959         $TRACE ls . && error "'ls .' worked after removing parent"
4960         $TRACE ls .. && error "'ls ..' worked after removing parent"
4961         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
4962         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4963         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4964         true
4965 }
4966 run_test 48d "Access removed parent subdir (should return errors)"
4967
4968 test_48e() { # bug 4134
4969         #lctl set_param debug=-1
4970         #set -vx
4971         rm -rf $DIR/$tdir
4972         test_mkdir -p $DIR/$tdir/dir
4973         cd $DIR/$tdir/dir
4974         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4975         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4976         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4977         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4978         # On a buggy kernel addition of "touch foo" after cd .. will
4979         # produce kernel oops in lookup_hash_it
4980         touch ../foo && error "'cd ..' worked after recreate parent"
4981         cd $DIR
4982         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4983 }
4984 run_test 48e "Access to recreated parent subdir (should return errors)"
4985
4986 test_49() { # LU-1030
4987         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4988         remote_ost_nodsh && skip "remote OST with nodsh"
4989
4990         # get ost1 size - $FSNAME-OST0000
4991         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4992                 awk '{ print $4 }')
4993         # write 800M at maximum
4994         [[ $ost1_size -lt 2 ]] && ost1_size=2
4995         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4996
4997         $LFS setstripe -c 1 -i 0 $DIR/$tfile
4998         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4999         local dd_pid=$!
5000
5001         # change max_pages_per_rpc while writing the file
5002         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
5003         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
5004         # loop until dd process exits
5005         while ps ax -opid | grep -wq $dd_pid; do
5006                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
5007                 sleep $((RANDOM % 5 + 1))
5008         done
5009         # restore original max_pages_per_rpc
5010         $LCTL set_param $osc1_mppc=$orig_mppc
5011         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
5012 }
5013 run_test 49 "Change max_pages_per_rpc won't break osc extent"
5014
5015 test_50() {
5016         # bug 1485
5017         test_mkdir $DIR/$tdir
5018         cd $DIR/$tdir
5019         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
5020 }
5021 run_test 50 "special situations: /proc symlinks  ==============="
5022
5023 test_51a() {    # was test_51
5024         # bug 1516 - create an empty entry right after ".." then split dir
5025         test_mkdir -c1 $DIR/$tdir
5026         touch $DIR/$tdir/foo
5027         $MCREATE $DIR/$tdir/bar
5028         rm $DIR/$tdir/foo
5029         createmany -m $DIR/$tdir/longfile 201
5030         FNUM=202
5031         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
5032                 $MCREATE $DIR/$tdir/longfile$FNUM
5033                 FNUM=$(($FNUM + 1))
5034                 echo -n "+"
5035         done
5036         echo
5037         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
5038 }
5039 run_test 51a "special situations: split htree with empty entry =="
5040
5041 cleanup_print_lfs_df () {
5042         trap 0
5043         $LFS df
5044         $LFS df -i
5045 }
5046
5047 test_51b() {
5048         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5049
5050         local dir=$DIR/$tdir
5051         local nrdirs=$((65536 + 100))
5052
5053         # cleanup the directory
5054         rm -fr $dir
5055
5056         test_mkdir -c1 $dir
5057
5058         $LFS df
5059         $LFS df -i
5060         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
5061         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
5062         [[ $numfree -lt $nrdirs ]] &&
5063                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
5064
5065         # need to check free space for the directories as well
5066         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
5067         numfree=$(( blkfree / $(fs_inode_ksize) ))
5068         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
5069
5070         trap cleanup_print_lfs_df EXIT
5071
5072         # create files
5073         createmany -d $dir/d $nrdirs || {
5074                 unlinkmany $dir/d $nrdirs
5075                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
5076         }
5077
5078         # really created :
5079         nrdirs=$(ls -U $dir | wc -l)
5080
5081         # unlink all but 100 subdirectories, then check it still works
5082         local left=100
5083         local delete=$((nrdirs - left))
5084
5085         $LFS df
5086         $LFS df -i
5087
5088         # for ldiskfs the nlink count should be 1, but this is OSD specific
5089         # and so this is listed for informational purposes only
5090         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
5091         unlinkmany -d $dir/d $delete ||
5092                 error "unlink of first $delete subdirs failed"
5093
5094         echo "nlink between: $(stat -c %h $dir)"
5095         local found=$(ls -U $dir | wc -l)
5096         [ $found -ne $left ] &&
5097                 error "can't find subdirs: found only $found, expected $left"
5098
5099         unlinkmany -d $dir/d $delete $left ||
5100                 error "unlink of second $left subdirs failed"
5101         # regardless of whether the backing filesystem tracks nlink accurately
5102         # or not, the nlink count shouldn't be more than "." and ".." here
5103         local after=$(stat -c %h $dir)
5104         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
5105                 echo "nlink after: $after"
5106
5107         cleanup_print_lfs_df
5108 }
5109 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
5110
5111 test_51d() {
5112         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5113         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
5114
5115         test_mkdir $DIR/$tdir
5116         createmany -o $DIR/$tdir/t- 1000
5117         $LFS getstripe $DIR/$tdir > $TMP/$tfile
5118         for N in $(seq 0 $((OSTCOUNT - 1))); do
5119                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
5120                         END { printf("%0.0f", objs) }' $TMP/$tfile)
5121                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
5122                         '($1 == '$N') { objs += 1 } \
5123                         END { printf("%0.0f", objs) }')
5124                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
5125         done
5126         unlinkmany $DIR/$tdir/t- 1000
5127
5128         NLAST=0
5129         for N in $(seq 1 $((OSTCOUNT - 1))); do
5130                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
5131                         error "OST $N has less objects vs OST $NLAST" \
5132                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5133                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
5134                         error "OST $N has less objects vs OST $NLAST" \
5135                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5136
5137                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
5138                         error "OST $N has less #0 objects vs OST $NLAST" \
5139                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5140                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
5141                         error "OST $N has less #0 objects vs OST $NLAST" \
5142                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5143                 NLAST=$N
5144         done
5145         rm -f $TMP/$tfile
5146 }
5147 run_test 51d "check object distribution"
5148
5149 test_51e() {
5150         if [ "$mds1_FSTYPE" != ldiskfs ]; then
5151                 skip_env "ldiskfs only test"
5152         fi
5153
5154         test_mkdir -c1 $DIR/$tdir
5155         test_mkdir -c1 $DIR/$tdir/d0
5156
5157         touch $DIR/$tdir/d0/foo
5158         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
5159                 error "file exceed 65000 nlink limit!"
5160         unlinkmany $DIR/$tdir/d0/f- 65001
5161         return 0
5162 }
5163 run_test 51e "check file nlink limit"
5164
5165 test_51f() {
5166         test_mkdir $DIR/$tdir
5167
5168         local max=100000
5169         local ulimit_old=$(ulimit -n)
5170         local spare=20 # number of spare fd's for scripts/libraries, etc.
5171         local mdt=$($LFS getstripe -m $DIR/$tdir)
5172         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
5173
5174         echo "MDT$mdt numfree=$numfree, max=$max"
5175         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
5176         if [ $((numfree + spare)) -gt $ulimit_old ]; then
5177                 while ! ulimit -n $((numfree + spare)); do
5178                         numfree=$((numfree * 3 / 4))
5179                 done
5180                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
5181         else
5182                 echo "left ulimit at $ulimit_old"
5183         fi
5184
5185         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
5186                 unlinkmany $DIR/$tdir/f $numfree
5187                 error "create+open $numfree files in $DIR/$tdir failed"
5188         }
5189         ulimit -n $ulimit_old
5190
5191         # if createmany exits at 120s there will be fewer than $numfree files
5192         unlinkmany $DIR/$tdir/f $numfree || true
5193 }
5194 run_test 51f "check many open files limit"
5195
5196 test_52a() {
5197         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
5198         test_mkdir $DIR/$tdir
5199         touch $DIR/$tdir/foo
5200         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
5201         echo bar >> $DIR/$tdir/foo || error "append bar failed"
5202         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5203         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5204         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5205                                         error "link worked"
5206         echo foo >> $DIR/$tdir/foo || error "append foo failed"
5207         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5208         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
5209                                                      error "lsattr"
5210         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
5211         cp -r $DIR/$tdir $TMP/
5212         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
5213 }
5214 run_test 52a "append-only flag test (should return errors)"
5215
5216 test_52b() {
5217         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
5218         test_mkdir $DIR/$tdir
5219         touch $DIR/$tdir/foo
5220         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
5221         cat test > $DIR/$tdir/foo && error "cat test worked"
5222         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5223         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5224         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5225                                         error "link worked"
5226         echo foo >> $DIR/$tdir/foo && error "echo worked"
5227         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5228         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
5229         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
5230         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
5231                                                         error "lsattr"
5232         chattr -i $DIR/$tdir/foo || error "chattr failed"
5233
5234         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
5235 }
5236 run_test 52b "immutable flag test (should return errors) ======="
5237
5238 test_53() {
5239         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5240         remote_mds_nodsh && skip "remote MDS with nodsh"
5241         remote_ost_nodsh && skip "remote OST with nodsh"
5242
5243         local param
5244         local param_seq
5245         local ostname
5246         local mds_last
5247         local mds_last_seq
5248         local ost_last
5249         local ost_last_seq
5250         local ost_last_id
5251         local ostnum
5252         local node
5253         local found=false
5254         local support_last_seq=true
5255
5256         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
5257                 support_last_seq=false
5258
5259         # only test MDT0000
5260         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
5261         local value
5262         for value in $(do_facet $SINGLEMDS \
5263                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
5264                 param=$(echo ${value[0]} | cut -d "=" -f1)
5265                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
5266
5267                 if $support_last_seq; then
5268                         param_seq=$(echo $param |
5269                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
5270                         mds_last_seq=$(do_facet $SINGLEMDS \
5271                                        $LCTL get_param -n $param_seq)
5272                 fi
5273                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
5274
5275                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
5276                 node=$(facet_active_host ost$((ostnum+1)))
5277                 param="obdfilter.$ostname.last_id"
5278                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
5279                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
5280                         ost_last_id=$ost_last
5281
5282                         if $support_last_seq; then
5283                                 ost_last_id=$(echo $ost_last |
5284                                               awk -F':' '{print $2}' |
5285                                               sed -e "s/^0x//g")
5286                                 ost_last_seq=$(echo $ost_last |
5287                                                awk -F':' '{print $1}')
5288                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
5289                         fi
5290
5291                         if [[ $ost_last_id != $mds_last ]]; then
5292                                 error "$ost_last_id != $mds_last"
5293                         else
5294                                 found=true
5295                                 break
5296                         fi
5297                 done
5298         done
5299         $found || error "can not match last_seq/last_id for $mdtosc"
5300         return 0
5301 }
5302 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
5303
5304 test_54a() {
5305         perl -MSocket -e ';' || skip "no Socket perl module installed"
5306
5307         $SOCKETSERVER $DIR/socket ||
5308                 error "$SOCKETSERVER $DIR/socket failed: $?"
5309         $SOCKETCLIENT $DIR/socket ||
5310                 error "$SOCKETCLIENT $DIR/socket failed: $?"
5311         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
5312 }
5313 run_test 54a "unix domain socket test =========================="
5314
5315 test_54b() {
5316         f="$DIR/f54b"
5317         mknod $f c 1 3
5318         chmod 0666 $f
5319         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
5320 }
5321 run_test 54b "char device works in lustre ======================"
5322
5323 find_loop_dev() {
5324         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
5325         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
5326         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
5327
5328         for i in $(seq 3 7); do
5329                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
5330                 LOOPDEV=$LOOPBASE$i
5331                 LOOPNUM=$i
5332                 break
5333         done
5334 }
5335
5336 cleanup_54c() {
5337         local rc=0
5338         loopdev="$DIR/loop54c"
5339
5340         trap 0
5341         $UMOUNT $DIR/$tdir || rc=$?
5342         losetup -d $loopdev || true
5343         losetup -d $LOOPDEV || true
5344         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
5345         return $rc
5346 }
5347
5348 test_54c() {
5349         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5350
5351         loopdev="$DIR/loop54c"
5352
5353         find_loop_dev
5354         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
5355         trap cleanup_54c EXIT
5356         mknod $loopdev b 7 $LOOPNUM
5357         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
5358         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
5359         losetup $loopdev $DIR/$tfile ||
5360                 error "can't set up $loopdev for $DIR/$tfile"
5361         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
5362         test_mkdir $DIR/$tdir
5363         mount -t ext2 $loopdev $DIR/$tdir ||
5364                 error "error mounting $loopdev on $DIR/$tdir"
5365         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
5366                 error "dd write"
5367         df $DIR/$tdir
5368         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
5369                 error "dd read"
5370         cleanup_54c
5371 }
5372 run_test 54c "block device works in lustre ====================="
5373
5374 test_54d() {
5375         f="$DIR/f54d"
5376         string="aaaaaa"
5377         mknod $f p
5378         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
5379 }
5380 run_test 54d "fifo device works in lustre ======================"
5381
5382 test_54e() {
5383         f="$DIR/f54e"
5384         string="aaaaaa"
5385         cp -aL /dev/console $f
5386         echo $string > $f || error "echo $string to $f failed"
5387 }
5388 run_test 54e "console/tty device works in lustre ======================"
5389
5390 test_56a() {
5391         local numfiles=3
5392         local dir=$DIR/$tdir
5393
5394         rm -rf $dir
5395         test_mkdir -p $dir/dir
5396         for i in $(seq $numfiles); do
5397                 touch $dir/file$i
5398                 touch $dir/dir/file$i
5399         done
5400
5401         local numcomp=$($LFS getstripe --component-count $dir)
5402
5403         [[ $numcomp == 0 ]] && numcomp=1
5404
5405         # test lfs getstripe with --recursive
5406         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
5407
5408         [[ $filenum -eq $((numfiles * 2)) ]] ||
5409                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
5410         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
5411         [[ $filenum -eq $numfiles ]] ||
5412                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
5413         echo "$LFS getstripe showed obdidx or l_ost_idx"
5414
5415         # test lfs getstripe with file instead of dir
5416         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
5417         [[ $filenum -eq 1 ]] ||
5418                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
5419         echo "$LFS getstripe file1 passed"
5420
5421         #test lfs getstripe with --verbose
5422         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
5423         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5424                 error "$LFS getstripe --verbose $dir: "\
5425                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
5426         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
5427                 error "$LFS getstripe $dir: showed lmm_magic"
5428
5429         #test lfs getstripe with -v prints lmm_fid
5430         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
5431         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5432                 error "$LFS getstripe -v $dir: "\
5433                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
5434         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
5435                 error "$LFS getstripe $dir: showed lmm_fid by default"
5436         echo "$LFS getstripe --verbose passed"
5437
5438         #check for FID information
5439         local fid1=$($LFS getstripe --fid $dir/file1)
5440         local fid2=$($LFS getstripe --verbose $dir/file1 |
5441                      awk '/lmm_fid: / { print $2; exit; }')
5442         local fid3=$($LFS path2fid $dir/file1)
5443
5444         [ "$fid1" != "$fid2" ] &&
5445                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
5446         [ "$fid1" != "$fid3" ] &&
5447                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
5448         echo "$LFS getstripe --fid passed"
5449
5450         #test lfs getstripe with --obd
5451         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
5452                 error "$LFS getstripe --obd wrong_uuid: should return error"
5453
5454         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5455
5456         local ostidx=1
5457         local obduuid=$(ostuuid_from_index $ostidx)
5458         local found=$($LFS getstripe -r --obd $obduuid $dir |
5459                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
5460
5461         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
5462         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
5463                 ((filenum--))
5464         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
5465                 ((filenum--))
5466
5467         [[ $found -eq $filenum ]] ||
5468                 error "$LFS getstripe --obd: found $found expect $filenum"
5469         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
5470                 sed '/^[         ]*'${ostidx}'[  ]/d' |
5471                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
5472                 error "$LFS getstripe --obd: should not show file on other obd"
5473         echo "$LFS getstripe --obd passed"
5474 }
5475 run_test 56a "check $LFS getstripe"
5476
5477 test_56b() {
5478         local dir=$DIR/$tdir
5479         local numdirs=3
5480
5481         test_mkdir $dir
5482         for i in $(seq $numdirs); do
5483                 test_mkdir $dir/dir$i
5484         done
5485
5486         # test lfs getdirstripe default mode is non-recursion, which is
5487         # different from lfs getstripe
5488         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
5489
5490         [[ $dircnt -eq 1 ]] ||
5491                 error "$LFS getdirstripe: found $dircnt, not 1"
5492         dircnt=$($LFS getdirstripe --recursive $dir |
5493                 grep -c lmv_stripe_count)
5494         [[ $dircnt -eq $((numdirs + 1)) ]] ||
5495                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
5496 }
5497 run_test 56b "check $LFS getdirstripe"
5498
5499 test_56c() {
5500         remote_ost_nodsh && skip "remote OST with nodsh"
5501
5502         local ost_idx=0
5503         local ost_name=$(ostname_from_index $ost_idx)
5504         local old_status=$(ost_dev_status $ost_idx)
5505
5506         [[ -z "$old_status" ]] ||
5507                 skip_env "OST $ost_name is in $old_status status"
5508
5509         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
5510         [[ $OST1_VERSION -ge $(version_code 2.12.55) ]] && do_facet ost1 \
5511                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
5512         sleep_maxage
5513
5514         local new_status=$(ost_dev_status $ost_idx)
5515
5516         [[ "$new_status" =~ "D" ]] ||
5517                 error "$ost_name status is '$new_status', missing 'D'"
5518         if [[ $OST1_VERSION -ge $(version_code 2.12.55) ]]; then
5519                 [[ "$new_status" =~ "N" ]] ||
5520                         error "$ost_name status is '$new_status', missing 'N'"
5521         fi
5522
5523         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
5524         [[ $OST1_VERSION -ge $(version_code 2.12.55) ]] && do_facet ost1 \
5525                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
5526         sleep_maxage
5527
5528         new_status=$(ost_dev_status $ost_idx)
5529         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
5530                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
5531 }
5532 run_test 56c "check 'lfs df' showing device status"
5533
5534 NUMFILES=3
5535 NUMDIRS=3
5536 setup_56() {
5537         local local_tdir="$1"
5538         local local_numfiles="$2"
5539         local local_numdirs="$3"
5540         local dir_params="$4"
5541         local dir_stripe_params="$5"
5542
5543         if [ ! -d "$local_tdir" ] ; then
5544                 test_mkdir -p $dir_stripe_params $local_tdir
5545                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
5546                 for i in $(seq $local_numfiles) ; do
5547                         touch $local_tdir/file$i
5548                 done
5549                 for i in $(seq $local_numdirs) ; do
5550                         test_mkdir $dir_stripe_params $local_tdir/dir$i
5551                         for j in $(seq $local_numfiles) ; do
5552                                 touch $local_tdir/dir$i/file$j
5553                         done
5554                 done
5555         fi
5556 }
5557
5558 setup_56_special() {
5559         local local_tdir=$1
5560         local local_numfiles=$2
5561         local local_numdirs=$3
5562
5563         setup_56 $local_tdir $local_numfiles $local_numdirs
5564
5565         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
5566                 for i in $(seq $local_numfiles) ; do
5567                         mknod $local_tdir/loop${i}b b 7 $i
5568                         mknod $local_tdir/null${i}c c 1 3
5569                         ln -s $local_tdir/file1 $local_tdir/link${i}
5570                 done
5571                 for i in $(seq $local_numdirs) ; do
5572                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
5573                         mknod $local_tdir/dir$i/null${i}c c 1 3
5574                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
5575                 done
5576         fi
5577 }
5578
5579 test_56g() {
5580         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5581         local expected=$(($NUMDIRS + 2))
5582
5583         setup_56 $dir $NUMFILES $NUMDIRS
5584
5585         # test lfs find with -name
5586         for i in $(seq $NUMFILES) ; do
5587                 local nums=$($LFS find -name "*$i" $dir | wc -l)
5588
5589                 [ $nums -eq $expected ] ||
5590                         error "lfs find -name '*$i' $dir wrong: "\
5591                               "found $nums, expected $expected"
5592         done
5593 }
5594 run_test 56g "check lfs find -name"
5595
5596 test_56h() {
5597         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5598         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
5599
5600         setup_56 $dir $NUMFILES $NUMDIRS
5601
5602         # test lfs find with ! -name
5603         for i in $(seq $NUMFILES) ; do
5604                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
5605
5606                 [ $nums -eq $expected ] ||
5607                         error "lfs find ! -name '*$i' $dir wrong: "\
5608                               "found $nums, expected $expected"
5609         done
5610 }
5611 run_test 56h "check lfs find ! -name"
5612
5613 test_56i() {
5614         local dir=$DIR/$tdir
5615
5616         test_mkdir $dir
5617
5618         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
5619         local out=$($cmd)
5620
5621         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
5622 }
5623 run_test 56i "check 'lfs find -ost UUID' skips directories"
5624
5625 test_56j() {
5626         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5627
5628         setup_56_special $dir $NUMFILES $NUMDIRS
5629
5630         local expected=$((NUMDIRS + 1))
5631         local cmd="$LFS find -type d $dir"
5632         local nums=$($cmd | wc -l)
5633
5634         [ $nums -eq $expected ] ||
5635                 error "'$cmd' wrong: found $nums, expected $expected"
5636 }
5637 run_test 56j "check lfs find -type d"
5638
5639 test_56k() {
5640         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5641
5642         setup_56_special $dir $NUMFILES $NUMDIRS
5643
5644         local expected=$(((NUMDIRS + 1) * NUMFILES))
5645         local cmd="$LFS find -type f $dir"
5646         local nums=$($cmd | wc -l)
5647
5648         [ $nums -eq $expected ] ||
5649                 error "'$cmd' wrong: found $nums, expected $expected"
5650 }
5651 run_test 56k "check lfs find -type f"
5652
5653 test_56l() {
5654         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5655
5656         setup_56_special $dir $NUMFILES $NUMDIRS
5657
5658         local expected=$((NUMDIRS + NUMFILES))
5659         local cmd="$LFS find -type b $dir"
5660         local nums=$($cmd | wc -l)
5661
5662         [ $nums -eq $expected ] ||
5663                 error "'$cmd' wrong: found $nums, expected $expected"
5664 }
5665 run_test 56l "check lfs find -type b"
5666
5667 test_56m() {
5668         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5669
5670         setup_56_special $dir $NUMFILES $NUMDIRS
5671
5672         local expected=$((NUMDIRS + NUMFILES))
5673         local cmd="$LFS find -type c $dir"
5674         local nums=$($cmd | wc -l)
5675         [ $nums -eq $expected ] ||
5676                 error "'$cmd' wrong: found $nums, expected $expected"
5677 }
5678 run_test 56m "check lfs find -type c"
5679
5680 test_56n() {
5681         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5682         setup_56_special $dir $NUMFILES $NUMDIRS
5683
5684         local expected=$((NUMDIRS + NUMFILES))
5685         local cmd="$LFS find -type l $dir"
5686         local nums=$($cmd | wc -l)
5687
5688         [ $nums -eq $expected ] ||
5689                 error "'$cmd' wrong: found $nums, expected $expected"
5690 }
5691 run_test 56n "check lfs find -type l"
5692
5693 test_56o() {
5694         local dir=$DIR/$tdir
5695
5696         setup_56 $dir $NUMFILES $NUMDIRS
5697         utime $dir/file1 > /dev/null || error "utime (1)"
5698         utime $dir/file2 > /dev/null || error "utime (2)"
5699         utime $dir/dir1 > /dev/null || error "utime (3)"
5700         utime $dir/dir2 > /dev/null || error "utime (4)"
5701         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5702         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5703
5704         local expected=4
5705         local nums=$($LFS find -mtime +0 $dir | wc -l)
5706
5707         [ $nums -eq $expected ] ||
5708                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5709
5710         expected=12
5711         cmd="$LFS find -mtime 0 $dir"
5712         nums=$($cmd | wc -l)
5713         [ $nums -eq $expected ] ||
5714                 error "'$cmd' wrong: found $nums, expected $expected"
5715 }
5716 run_test 56o "check lfs find -mtime for old files"
5717
5718 test_56ob() {
5719         local dir=$DIR/$tdir
5720         local expected=1
5721         local count=0
5722
5723         # just to make sure there is something that won't be found
5724         test_mkdir $dir
5725         touch $dir/$tfile.now
5726
5727         for age in year week day hour min; do
5728                 count=$((count + 1))
5729
5730                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
5731                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
5732                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
5733
5734                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
5735                 local nums=$($cmd | wc -l)
5736                 [ $nums -eq $expected ] ||
5737                         error "'$cmd' wrong: found $nums, expected $expected"
5738
5739                 cmd="$LFS find $dir -atime $count${age:0:1}"
5740                 nums=$($cmd | wc -l)
5741                 [ $nums -eq $expected ] ||
5742                         error "'$cmd' wrong: found $nums, expected $expected"
5743         done
5744
5745         sleep 2
5746         cmd="$LFS find $dir -ctime +1s -type f"
5747         nums=$($cmd | wc -l)
5748         (( $nums == $count * 2 + 1)) ||
5749                 error "'$cmd' wrong: found $nums, expected $((expected*2+1))"
5750 }
5751 run_test 56ob "check lfs find -atime -mtime -ctime with units"
5752
5753 test_newerXY_base() {
5754         local x=$1
5755         local y=$2
5756         local dir=$DIR/$tdir
5757         local ref
5758         local negref
5759
5760         if [ $y == "t" ]; then
5761                 ref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5762         else
5763                 ref=$DIR/$tfile.newer
5764                 touch $ref || error "touch $ref failed"
5765         fi
5766         sleep 2
5767         setup_56 $dir $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
5768         sleep 2
5769         if [ $y == "t" ]; then
5770                 negref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5771         else
5772                 negref=$DIR/$tfile.newerneg
5773                 touch $negref || error "touch $negref failed"
5774         fi
5775
5776         local cmd="$LFS find $dir -newer$x$y $ref"
5777         local nums=$(eval $cmd | wc -l)
5778         local expected=$(((NUMFILES + 2) * NUMDIRS + 1))
5779
5780         [ $nums -eq $expected ] ||
5781                 error "'$cmd' wrong: found $nums, expected $expected"
5782
5783         cmd="$LFS find $dir ! -newer$x$y $negref"
5784         nums=$(eval $cmd | wc -l)
5785         [ $nums -eq $expected ] ||
5786                 error "'$cmd' wrong: found $nums, expected $expected"
5787
5788         cmd="$LFS find $dir -newer$x$y $ref ! -newer$x$y $negref"
5789         nums=$(eval $cmd | wc -l)
5790         [ $nums -eq $expected ] ||
5791                 error "'$cmd' wrong: found $nums, expected $expected"
5792
5793         rm -rf $DIR/*
5794 }
5795
5796 test_56oc() {
5797         test_newerXY_base "a" "a"
5798         test_newerXY_base "a" "m"
5799         test_newerXY_base "a" "c"
5800         test_newerXY_base "m" "a"
5801         test_newerXY_base "m" "m"
5802         test_newerXY_base "m" "c"
5803         test_newerXY_base "c" "a"
5804         test_newerXY_base "c" "m"
5805         test_newerXY_base "c" "c"
5806         test_newerXY_base "a" "t"
5807         test_newerXY_base "m" "t"
5808         test_newerXY_base "c" "t"
5809 }
5810 run_test 56oc "check lfs find -newerXY work"
5811
5812 test_56p() {
5813         [ $RUNAS_ID -eq $UID ] &&
5814                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5815
5816         local dir=$DIR/$tdir
5817
5818         setup_56 $dir $NUMFILES $NUMDIRS
5819         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
5820
5821         local expected=$NUMFILES
5822         local cmd="$LFS find -uid $RUNAS_ID $dir"
5823         local nums=$($cmd | wc -l)
5824
5825         [ $nums -eq $expected ] ||
5826                 error "'$cmd' wrong: found $nums, expected $expected"
5827
5828         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
5829         cmd="$LFS find ! -uid $RUNAS_ID $dir"
5830         nums=$($cmd | wc -l)
5831         [ $nums -eq $expected ] ||
5832                 error "'$cmd' wrong: found $nums, expected $expected"
5833 }
5834 run_test 56p "check lfs find -uid and ! -uid"
5835
5836 test_56q() {
5837         [ $RUNAS_ID -eq $UID ] &&
5838                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5839
5840         local dir=$DIR/$tdir
5841
5842         setup_56 $dir $NUMFILES $NUMDIRS
5843         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
5844
5845         local expected=$NUMFILES
5846         local cmd="$LFS find -gid $RUNAS_GID $dir"
5847         local nums=$($cmd | wc -l)
5848
5849         [ $nums -eq $expected ] ||
5850                 error "'$cmd' wrong: found $nums, expected $expected"
5851
5852         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
5853         cmd="$LFS find ! -gid $RUNAS_GID $dir"
5854         nums=$($cmd | wc -l)
5855         [ $nums -eq $expected ] ||
5856                 error "'$cmd' wrong: found $nums, expected $expected"
5857 }
5858 run_test 56q "check lfs find -gid and ! -gid"
5859
5860 test_56r() {
5861         local dir=$DIR/$tdir
5862
5863         setup_56 $dir $NUMFILES $NUMDIRS
5864
5865         local expected=12
5866         local cmd="$LFS find -size 0 -type f -lazy $dir"
5867         local nums=$($cmd | wc -l)
5868
5869         [ $nums -eq $expected ] ||
5870                 error "'$cmd' wrong: found $nums, expected $expected"
5871         cmd="$LFS find -size 0 -type f $dir"
5872         nums=$($cmd | wc -l)
5873         [ $nums -eq $expected ] ||
5874                 error "'$cmd' wrong: found $nums, expected $expected"
5875
5876         expected=0
5877         cmd="$LFS find ! -size 0 -type f -lazy $dir"
5878         nums=$($cmd | wc -l)
5879         [ $nums -eq $expected ] ||
5880                 error "'$cmd' wrong: found $nums, expected $expected"
5881         cmd="$LFS find ! -size 0 -type f $dir"
5882         nums=$($cmd | wc -l)
5883         [ $nums -eq $expected ] ||
5884                 error "'$cmd' wrong: found $nums, expected $expected"
5885
5886         echo "test" > $dir/$tfile
5887         echo "test2" > $dir/$tfile.2 && sync
5888         expected=1
5889         cmd="$LFS find -size 5 -type f -lazy $dir"
5890         nums=$($cmd | wc -l)
5891         [ $nums -eq $expected ] ||
5892                 error "'$cmd' wrong: found $nums, expected $expected"
5893         cmd="$LFS find -size 5 -type f $dir"
5894         nums=$($cmd | wc -l)
5895         [ $nums -eq $expected ] ||
5896                 error "'$cmd' wrong: found $nums, expected $expected"
5897
5898         expected=1
5899         cmd="$LFS find -size +5 -type f -lazy $dir"
5900         nums=$($cmd | wc -l)
5901         [ $nums -eq $expected ] ||
5902                 error "'$cmd' wrong: found $nums, expected $expected"
5903         cmd="$LFS find -size +5 -type f $dir"
5904         nums=$($cmd | wc -l)
5905         [ $nums -eq $expected ] ||
5906                 error "'$cmd' wrong: found $nums, expected $expected"
5907
5908         expected=2
5909         cmd="$LFS find -size +0 -type f -lazy $dir"
5910         nums=$($cmd | wc -l)
5911         [ $nums -eq $expected ] ||
5912                 error "'$cmd' wrong: found $nums, expected $expected"
5913         cmd="$LFS find -size +0 -type f $dir"
5914         nums=$($cmd | wc -l)
5915         [ $nums -eq $expected ] ||
5916                 error "'$cmd' wrong: found $nums, expected $expected"
5917
5918         expected=2
5919         cmd="$LFS find ! -size -5 -type f -lazy $dir"
5920         nums=$($cmd | wc -l)
5921         [ $nums -eq $expected ] ||
5922                 error "'$cmd' wrong: found $nums, expected $expected"
5923         cmd="$LFS find ! -size -5 -type f $dir"
5924         nums=$($cmd | wc -l)
5925         [ $nums -eq $expected ] ||
5926                 error "'$cmd' wrong: found $nums, expected $expected"
5927
5928         expected=12
5929         cmd="$LFS find -size -5 -type f -lazy $dir"
5930         nums=$($cmd | wc -l)
5931         [ $nums -eq $expected ] ||
5932                 error "'$cmd' wrong: found $nums, expected $expected"
5933         cmd="$LFS find -size -5 -type f $dir"
5934         nums=$($cmd | wc -l)
5935         [ $nums -eq $expected ] ||
5936                 error "'$cmd' wrong: found $nums, expected $expected"
5937 }
5938 run_test 56r "check lfs find -size works"
5939
5940 test_56ra() {
5941         [[ $MDS1_VERSION -gt $(version_code 2.12.58) ]] ||
5942                 skip "MDS < 2.12.58 doesn't return LSOM data"
5943         local dir=$DIR/$tdir
5944
5945         [[ $OSC == "mdc" ]] && skip "DoM files" && return
5946
5947         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
5948
5949         cancel_lru_locks $OSC
5950
5951         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5952         local expected=12
5953         local cmd="$LFS find -size 0 -type f -lazy $dir"
5954         local nums=$($cmd | wc -l)
5955
5956         [ $nums -eq $expected ] ||
5957                 error "'$cmd' wrong: found $nums, expected $expected"
5958
5959         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5960         [ $rpcs_before -eq $rpcs_after ] ||
5961                 error "'$cmd' should not send glimpse RPCs to OST"
5962         cmd="$LFS find -size 0 -type f $dir"
5963         nums=$($cmd | wc -l)
5964         [ $nums -eq $expected ] ||
5965                 error "'$cmd' wrong: found $nums, expected $expected"
5966         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5967         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
5968         $LCTL get_param osc.*.stats
5969         [ $rpcs_after -eq $((rpcs_before + 12)) ] ||
5970                 error "'$cmd' should send 12 glimpse RPCs to OST"
5971
5972         cancel_lru_locks $OSC
5973         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5974         expected=0
5975         cmd="$LFS find ! -size 0 -type f -lazy $dir"
5976         nums=$($cmd | wc -l)
5977         [ $nums -eq $expected ] ||
5978                 error "'$cmd' wrong: found $nums, expected $expected"
5979         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5980         $LCTL get_param mdc.*.stats
5981         [ $rpcs_before -eq $rpcs_after ] ||
5982                 error "'$cmd' should not send glimpse RPCs to OST"
5983         cmd="$LFS find ! -size 0 -type f $dir"
5984         nums=$($cmd | wc -l)
5985         [ $nums -eq $expected ] ||
5986                 error "'$cmd' wrong: found $nums, expected $expected"
5987         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5988         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
5989         [ $rpcs_after -eq $((rpcs_before + 12)) ] ||
5990                 error "'$cmd' should send 12 glimpse RPCs to OST"
5991
5992         echo "test" > $dir/$tfile
5993         echo "test2" > $dir/$tfile.2 && sync
5994         cancel_lru_locks $OSC
5995         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5996         expected=1
5997         cmd="$LFS find -size 5 -type f -lazy $dir"
5998         nums=$($cmd | wc -l)
5999         [ $nums -eq $expected ] ||
6000                 error "'$cmd' wrong: found $nums, expected $expected"
6001         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6002         [ $rpcs_before -eq $rpcs_after ] ||
6003                 error "'$cmd' should not send glimpse RPCs to OST"
6004         cmd="$LFS find -size 5 -type f $dir"
6005         nums=$($cmd | wc -l)
6006         [ $nums -eq $expected ] ||
6007                 error "'$cmd' wrong: found $nums, expected $expected"
6008         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6009         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6010         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6011                 error "'$cmd' should send 14 glimpse RPCs to OST"
6012
6013         cancel_lru_locks $OSC
6014         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6015         expected=1
6016         cmd="$LFS find -size +5 -type f -lazy $dir"
6017         nums=$($cmd | wc -l)
6018         [ $nums -eq $expected ] ||
6019                 error "'$cmd' wrong: found $nums, expected $expected"
6020         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6021         [ $rpcs_before -eq $rpcs_after ] ||
6022                 error "'$cmd' should not send glimpse RPCs to OST"
6023         cmd="$LFS find -size +5 -type f $dir"
6024         nums=$($cmd | wc -l)
6025         [ $nums -eq $expected ] ||
6026                 error "'$cmd' wrong: found $nums, expected $expected"
6027         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6028         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6029         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6030                 error "'$cmd' should send 14 glimpse RPCs to OST"
6031
6032         cancel_lru_locks $OSC
6033         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6034         expected=2
6035         cmd="$LFS find -size +0 -type f -lazy $dir"
6036         nums=$($cmd | wc -l)
6037         [ $nums -eq $expected ] ||
6038                 error "'$cmd' wrong: found $nums, expected $expected"
6039         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6040         [ $rpcs_before -eq $rpcs_after ] ||
6041                 error "'$cmd' should not send glimpse RPCs to OST"
6042         cmd="$LFS find -size +0 -type f $dir"
6043         nums=$($cmd | wc -l)
6044         [ $nums -eq $expected ] ||
6045                 error "'$cmd' wrong: found $nums, expected $expected"
6046         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6047         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6048         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6049                 error "'$cmd' should send 14 glimpse RPCs to OST"
6050
6051         cancel_lru_locks $OSC
6052         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6053         expected=2
6054         cmd="$LFS find ! -size -5 -type f -lazy $dir"
6055         nums=$($cmd | wc -l)
6056         [ $nums -eq $expected ] ||
6057                 error "'$cmd' wrong: found $nums, expected $expected"
6058         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6059         [ $rpcs_before -eq $rpcs_after ] ||
6060                 error "'$cmd' should not send glimpse RPCs to OST"
6061         cmd="$LFS find ! -size -5 -type f $dir"
6062         nums=$($cmd | wc -l)
6063         [ $nums -eq $expected ] ||
6064                 error "'$cmd' wrong: found $nums, expected $expected"
6065         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6066         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6067         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6068                 error "'$cmd' should send 14 glimpse RPCs to OST"
6069
6070         cancel_lru_locks $OSC
6071         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6072         expected=12
6073         cmd="$LFS find -size -5 -type f -lazy $dir"
6074         nums=$($cmd | wc -l)
6075         [ $nums -eq $expected ] ||
6076                 error "'$cmd' wrong: found $nums, expected $expected"
6077         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6078         [ $rpcs_before -eq $rpcs_after ] ||
6079                 error "'$cmd' should not send glimpse RPCs to OST"
6080         cmd="$LFS find -size -5 -type f $dir"
6081         nums=$($cmd | wc -l)
6082         [ $nums -eq $expected ] ||
6083                 error "'$cmd' wrong: found $nums, expected $expected"
6084         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6085         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6086         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6087                 error "'$cmd' should send 14 glimpse RPCs to OST"
6088 }
6089 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
6090
6091 test_56s() { # LU-611 #LU-9369
6092         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
6093
6094         local dir=$DIR/$tdir
6095         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
6096
6097         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6098         for i in $(seq $NUMDIRS); do
6099                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
6100         done
6101
6102         local expected=$NUMDIRS
6103         local cmd="$LFS find -c $OSTCOUNT $dir"
6104         local nums=$($cmd | wc -l)
6105
6106         [ $nums -eq $expected ] || {
6107                 $LFS getstripe -R $dir
6108                 error "'$cmd' wrong: found $nums, expected $expected"
6109         }
6110
6111         expected=$((NUMDIRS + onestripe))
6112         cmd="$LFS find -stripe-count +0 -type f $dir"
6113         nums=$($cmd | wc -l)
6114         [ $nums -eq $expected ] || {
6115                 $LFS getstripe -R $dir
6116                 error "'$cmd' wrong: found $nums, expected $expected"
6117         }
6118
6119         expected=$onestripe
6120         cmd="$LFS find -stripe-count 1 -type f $dir"
6121         nums=$($cmd | wc -l)
6122         [ $nums -eq $expected ] || {
6123                 $LFS getstripe -R $dir
6124                 error "'$cmd' wrong: found $nums, expected $expected"
6125         }
6126
6127         cmd="$LFS find -stripe-count -2 -type f $dir"
6128         nums=$($cmd | wc -l)
6129         [ $nums -eq $expected ] || {
6130                 $LFS getstripe -R $dir
6131                 error "'$cmd' wrong: found $nums, expected $expected"
6132         }
6133
6134         expected=0
6135         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
6136         nums=$($cmd | wc -l)
6137         [ $nums -eq $expected ] || {
6138                 $LFS getstripe -R $dir
6139                 error "'$cmd' wrong: found $nums, expected $expected"
6140         }
6141 }
6142 run_test 56s "check lfs find -stripe-count works"
6143
6144 test_56t() { # LU-611 #LU-9369
6145         local dir=$DIR/$tdir
6146
6147         setup_56 $dir 0 $NUMDIRS
6148         for i in $(seq $NUMDIRS); do
6149                 $LFS setstripe -S 8M $dir/dir$i/$tfile
6150         done
6151
6152         local expected=$NUMDIRS
6153         local cmd="$LFS find -S 8M $dir"
6154         local nums=$($cmd | wc -l)
6155
6156         [ $nums -eq $expected ] || {
6157                 $LFS getstripe -R $dir
6158                 error "'$cmd' wrong: found $nums, expected $expected"
6159         }
6160         rm -rf $dir
6161
6162         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
6163
6164         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
6165
6166         expected=$(((NUMDIRS + 1) * NUMFILES))
6167         cmd="$LFS find -stripe-size 512k -type f $dir"
6168         nums=$($cmd | wc -l)
6169         [ $nums -eq $expected ] ||
6170                 error "'$cmd' wrong: found $nums, expected $expected"
6171
6172         cmd="$LFS find -stripe-size +320k -type f $dir"
6173         nums=$($cmd | wc -l)
6174         [ $nums -eq $expected ] ||
6175                 error "'$cmd' wrong: found $nums, expected $expected"
6176
6177         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
6178         cmd="$LFS find -stripe-size +200k -type f $dir"
6179         nums=$($cmd | wc -l)
6180         [ $nums -eq $expected ] ||
6181                 error "'$cmd' wrong: found $nums, expected $expected"
6182
6183         cmd="$LFS find -stripe-size -640k -type f $dir"
6184         nums=$($cmd | wc -l)
6185         [ $nums -eq $expected ] ||
6186                 error "'$cmd' wrong: found $nums, expected $expected"
6187
6188         expected=4
6189         cmd="$LFS find -stripe-size 256k -type f $dir"
6190         nums=$($cmd | wc -l)
6191         [ $nums -eq $expected ] ||
6192                 error "'$cmd' wrong: found $nums, expected $expected"
6193
6194         cmd="$LFS find -stripe-size -320k -type f $dir"
6195         nums=$($cmd | wc -l)
6196         [ $nums -eq $expected ] ||
6197                 error "'$cmd' wrong: found $nums, expected $expected"
6198
6199         expected=0
6200         cmd="$LFS find -stripe-size 1024k -type f $dir"
6201         nums=$($cmd | wc -l)
6202         [ $nums -eq $expected ] ||
6203                 error "'$cmd' wrong: found $nums, expected $expected"
6204 }
6205 run_test 56t "check lfs find -stripe-size works"
6206
6207 test_56u() { # LU-611
6208         local dir=$DIR/$tdir
6209
6210         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
6211
6212         if [[ $OSTCOUNT -gt 1 ]]; then
6213                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
6214                 onestripe=4
6215         else
6216                 onestripe=0
6217         fi
6218
6219         local expected=$(((NUMDIRS + 1) * NUMFILES))
6220         local cmd="$LFS find -stripe-index 0 -type f $dir"
6221         local nums=$($cmd | wc -l)
6222
6223         [ $nums -eq $expected ] ||
6224                 error "'$cmd' wrong: found $nums, expected $expected"
6225
6226         expected=$onestripe
6227         cmd="$LFS find -stripe-index 1 -type f $dir"
6228         nums=$($cmd | wc -l)
6229         [ $nums -eq $expected ] ||
6230                 error "'$cmd' wrong: found $nums, expected $expected"
6231
6232         cmd="$LFS find ! -stripe-index 0 -type f $dir"
6233         nums=$($cmd | wc -l)
6234         [ $nums -eq $expected ] ||
6235                 error "'$cmd' wrong: found $nums, expected $expected"
6236
6237         expected=0
6238         # This should produce an error and not return any files
6239         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
6240         nums=$($cmd 2>/dev/null | wc -l)
6241         [ $nums -eq $expected ] ||
6242                 error "'$cmd' wrong: found $nums, expected $expected"
6243
6244         if [[ $OSTCOUNT -gt 1 ]]; then
6245                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
6246                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
6247                 nums=$($cmd | wc -l)
6248                 [ $nums -eq $expected ] ||
6249                         error "'$cmd' wrong: found $nums, expected $expected"
6250         fi
6251 }
6252 run_test 56u "check lfs find -stripe-index works"
6253
6254 test_56v() {
6255         local mdt_idx=0
6256         local dir=$DIR/$tdir
6257
6258         setup_56 $dir $NUMFILES $NUMDIRS
6259
6260         UUID=$(mdtuuid_from_index $mdt_idx $dir)
6261         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
6262
6263         for file in $($LFS find -m $UUID $dir); do
6264                 file_midx=$($LFS getstripe -m $file)
6265                 [ $file_midx -eq $mdt_idx ] ||
6266                         error "lfs find -m $UUID != getstripe -m $file_midx"
6267         done
6268 }
6269 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
6270
6271 test_56w() {
6272         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6273         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6274
6275         local dir=$DIR/$tdir
6276
6277         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
6278
6279         local stripe_size=$($LFS getstripe -S -d $dir) ||
6280                 error "$LFS getstripe -S -d $dir failed"
6281         stripe_size=${stripe_size%% *}
6282
6283         local file_size=$((stripe_size * OSTCOUNT))
6284         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
6285         local required_space=$((file_num * file_size))
6286         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
6287                            head -n1)
6288         [[ $free_space -le $((required_space / 1024)) ]] &&
6289                 skip_env "need $required_space, have $free_space kbytes"
6290
6291         local dd_bs=65536
6292         local dd_count=$((file_size / dd_bs))
6293
6294         # write data into the files
6295         local i
6296         local j
6297         local file
6298
6299         for i in $(seq $NUMFILES); do
6300                 file=$dir/file$i
6301                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6302                         error "write data into $file failed"
6303         done
6304         for i in $(seq $NUMDIRS); do
6305                 for j in $(seq $NUMFILES); do
6306                         file=$dir/dir$i/file$j
6307                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6308                                 error "write data into $file failed"
6309                 done
6310         done
6311
6312         # $LFS_MIGRATE will fail if hard link migration is unsupported
6313         if [[ $MDS1_VERSION -gt $(version_code 2.5.55) ]]; then
6314                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
6315                         error "creating links to $dir/dir1/file1 failed"
6316         fi
6317
6318         local expected=-1
6319
6320         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
6321
6322         # lfs_migrate file
6323         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
6324
6325         echo "$cmd"
6326         eval $cmd || error "$cmd failed"
6327
6328         check_stripe_count $dir/file1 $expected
6329
6330         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
6331         then
6332                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
6333                 # OST 1 if it is on OST 0. This file is small enough to
6334                 # be on only one stripe.
6335                 file=$dir/migr_1_ost
6336                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
6337                         error "write data into $file failed"
6338                 local obdidx=$($LFS getstripe -i $file)
6339                 local oldmd5=$(md5sum $file)
6340                 local newobdidx=0
6341
6342                 [[ $obdidx -eq 0 ]] && newobdidx=1
6343                 cmd="$LFS migrate -i $newobdidx $file"
6344                 echo $cmd
6345                 eval $cmd || error "$cmd failed"
6346
6347                 local realobdix=$($LFS getstripe -i $file)
6348                 local newmd5=$(md5sum $file)
6349
6350                 [[ $newobdidx -ne $realobdix ]] &&
6351                         error "new OST is different (was=$obdidx, "\
6352                               "wanted=$newobdidx, got=$realobdix)"
6353                 [[ "$oldmd5" != "$newmd5" ]] &&
6354                         error "md5sum differ: $oldmd5, $newmd5"
6355         fi
6356
6357         # lfs_migrate dir
6358         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
6359         echo "$cmd"
6360         eval $cmd || error "$cmd failed"
6361
6362         for j in $(seq $NUMFILES); do
6363                 check_stripe_count $dir/dir1/file$j $expected
6364         done
6365
6366         # lfs_migrate works with lfs find
6367         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
6368              $LFS_MIGRATE -y -c $expected"
6369         echo "$cmd"
6370         eval $cmd || error "$cmd failed"
6371
6372         for i in $(seq 2 $NUMFILES); do
6373                 check_stripe_count $dir/file$i $expected
6374         done
6375         for i in $(seq 2 $NUMDIRS); do
6376                 for j in $(seq $NUMFILES); do
6377                 check_stripe_count $dir/dir$i/file$j $expected
6378                 done
6379         done
6380 }
6381 run_test 56w "check lfs_migrate -c stripe_count works"
6382
6383 test_56wb() {
6384         local file1=$DIR/$tdir/file1
6385         local create_pool=false
6386         local initial_pool=$($LFS getstripe -p $DIR)
6387         local pool_list=()
6388         local pool=""
6389
6390         echo -n "Creating test dir..."
6391         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6392         echo "done."
6393
6394         echo -n "Creating test file..."
6395         touch $file1 || error "cannot create file"
6396         echo "done."
6397
6398         echo -n "Detecting existing pools..."
6399         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
6400
6401         if [ ${#pool_list[@]} -gt 0 ]; then
6402                 echo "${pool_list[@]}"
6403                 for thispool in "${pool_list[@]}"; do
6404                         if [[ -z "$initial_pool" ||
6405                               "$initial_pool" != "$thispool" ]]; then
6406                                 pool="$thispool"
6407                                 echo "Using existing pool '$pool'"
6408                                 break
6409                         fi
6410                 done
6411         else
6412                 echo "none detected."
6413         fi
6414         if [ -z "$pool" ]; then
6415                 pool=${POOL:-testpool}
6416                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
6417                 echo -n "Creating pool '$pool'..."
6418                 create_pool=true
6419                 pool_add $pool &> /dev/null ||
6420                         error "pool_add failed"
6421                 echo "done."
6422
6423                 echo -n "Adding target to pool..."
6424                 pool_add_targets $pool 0 0 1 &> /dev/null ||
6425                         error "pool_add_targets failed"
6426                 echo "done."
6427         fi
6428
6429         echo -n "Setting pool using -p option..."
6430         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
6431                 error "migrate failed rc = $?"
6432         echo "done."
6433
6434         echo -n "Verifying test file is in pool after migrating..."
6435         [ "$($LFS getstripe -p $file1)" = $pool ] ||
6436                 error "file was not migrated to pool $pool"
6437         echo "done."
6438
6439         echo -n "Removing test file from pool '$pool'..."
6440         $LFS migrate $file1 &> /dev/null ||
6441                 error "cannot remove from pool"
6442         [ "$($LFS getstripe -p $file1)" ] &&
6443                 error "pool still set"
6444         echo "done."
6445
6446         echo -n "Setting pool using --pool option..."
6447         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
6448                 error "migrate failed rc = $?"
6449         echo "done."
6450
6451         # Clean up
6452         rm -f $file1
6453         if $create_pool; then
6454                 destroy_test_pools 2> /dev/null ||
6455                         error "destroy test pools failed"
6456         fi
6457 }
6458 run_test 56wb "check lfs_migrate pool support"
6459
6460 test_56wc() {
6461         local file1="$DIR/$tdir/file1"
6462
6463         echo -n "Creating test dir..."
6464         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6465         local def_stripe_size=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
6466         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
6467                 error "cannot set stripe"
6468         echo "done"
6469
6470         echo -n "Setting initial stripe for test file..."
6471         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
6472                 error "cannot set stripe"
6473         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
6474                 error "stripe size not set"
6475         echo "done."
6476
6477         # File currently set to -S 512K -c 1
6478
6479         # Ensure -c and -S options are rejected when -R is set
6480         echo -n "Verifying incompatible options are detected..."
6481         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
6482                 error "incompatible -c and -R options not detected"
6483         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
6484                 error "incompatible -S and -R options not detected"
6485         echo "done."
6486
6487         # Ensure unrecognized options are passed through to 'lfs migrate'
6488         echo -n "Verifying -S option is passed through to lfs migrate..."
6489         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
6490                 error "migration failed"
6491         [ $($LFS getstripe -S "$file1") -eq 1048576 ] ||
6492                 error "file was not restriped"
6493         echo "done."
6494
6495         # File currently set to -S 1M -c 1
6496
6497         # Ensure long options are supported
6498         echo -n "Verifying long options supported..."
6499         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
6500                 error "long option without argument not supported"
6501         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
6502                 error "long option with argument not supported"
6503         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
6504                 error "file not restriped with --stripe-size option"
6505         echo "done."
6506
6507         # File currently set to -S 512K -c 1
6508
6509         if [ "$OSTCOUNT" -gt 1 ]; then
6510                 echo -n "Verifying explicit stripe count can be set..."
6511                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
6512                         error "migrate failed"
6513                 [ $($LFS getstripe -c "$file1") -eq 2 ] ||
6514                         error "file not restriped to explicit count"
6515                 echo "done."
6516         fi
6517
6518         # File currently set to -S 512K -c 1 or -S 512K -c 2
6519
6520         # Ensure parent striping is used if -R is set, and no stripe
6521         # count or size is specified
6522         echo -n "Setting stripe for parent directory..."
6523         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
6524                 error "cannot set stripe"
6525         echo "done."
6526
6527         echo -n "Verifying restripe option uses parent stripe settings..."
6528         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
6529                 error "migrate failed"
6530         [ $($LFS getstripe -S "$file1") -eq $def_stripe_size ] ||
6531                 error "file not restriped to parent settings"
6532         [ $($LFS getstripe -c "$file1") -eq 1 ] ||
6533                 error "file not restriped to parent settings"
6534         echo "done."
6535
6536         # File currently set to -S 1M -c 1
6537
6538         # Ensure striping is preserved if -R is not set, and no stripe
6539         # count or size is specified
6540         echo -n "Verifying striping size preserved when not specified..."
6541         local orig_stripe_size=$($LFS getstripe -S "$file1" 2>/dev/null)
6542         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
6543                 error "cannot set stripe on parent directory"
6544         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6545                 error "migrate failed"
6546         [ $($LFS getstripe -S "$file1") -eq $orig_stripe_size ] ||
6547                 error "file was restriped"
6548         echo "done."
6549
6550         # Ensure file name properly detected when final option has no argument
6551         echo -n "Verifying file name properly detected..."
6552         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6553                 error "file name interpreted as option argument"
6554         echo "done."
6555
6556         # Clean up
6557         rm -f "$file1"
6558 }
6559 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
6560
6561 test_56wd() {
6562         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6563
6564         local file1=$DIR/$tdir/file1
6565
6566         echo -n "Creating test dir..."
6567         test_mkdir $DIR/$tdir || error "cannot create dir"
6568         echo "done."
6569
6570         echo -n "Creating test file..."
6571         touch $file1
6572         echo "done."
6573
6574         # Ensure 'lfs migrate' will fail by using a non-existent option,
6575         # and make sure rsync is not called to recover
6576         echo -n "Make sure --no-rsync option works..."
6577         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
6578                 grep -q 'refusing to fall back to rsync' ||
6579                 error "rsync was called with --no-rsync set"
6580         echo "done."
6581
6582         # Ensure rsync is called without trying 'lfs migrate' first
6583         echo -n "Make sure --rsync option works..."
6584         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
6585                 grep -q 'falling back to rsync' &&
6586                 error "lfs migrate was called with --rsync set"
6587         echo "done."
6588
6589         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
6590         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
6591                 grep -q 'at the same time' ||
6592                 error "--rsync and --no-rsync accepted concurrently"
6593         echo "done."
6594
6595         # Clean up
6596         rm -f $file1
6597 }
6598 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
6599
6600 test_56x() {
6601         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6602         check_swap_layouts_support
6603
6604         local dir=$DIR/$tdir
6605         local ref1=/etc/passwd
6606         local file1=$dir/file1
6607
6608         test_mkdir $dir || error "creating dir $dir"
6609         $LFS setstripe -c 2 $file1
6610         cp $ref1 $file1
6611         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
6612         stripe=$($LFS getstripe -c $file1)
6613         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6614         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6615
6616         # clean up
6617         rm -f $file1
6618 }
6619 run_test 56x "lfs migration support"
6620
6621 test_56xa() {
6622         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6623         check_swap_layouts_support
6624
6625         local dir=$DIR/$tdir/$testnum
6626
6627         test_mkdir -p $dir
6628
6629         local ref1=/etc/passwd
6630         local file1=$dir/file1
6631
6632         $LFS setstripe -c 2 $file1
6633         cp $ref1 $file1
6634         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
6635
6636         local stripe=$($LFS getstripe -c $file1)
6637
6638         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6639         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6640
6641         # clean up
6642         rm -f $file1
6643 }
6644 run_test 56xa "lfs migration --block support"
6645
6646 check_migrate_links() {
6647         local dir="$1"
6648         local file1="$dir/file1"
6649         local begin="$2"
6650         local count="$3"
6651         local runas="$4"
6652         local total_count=$(($begin + $count - 1))
6653         local symlink_count=10
6654         local uniq_count=10
6655
6656         if [ ! -f "$file1" ]; then
6657                 echo -n "creating initial file..."
6658                 $LFS setstripe -c 1 -S "512k" "$file1" ||
6659                         error "cannot setstripe initial file"
6660                 echo "done"
6661
6662                 echo -n "creating symlinks..."
6663                 for s in $(seq 1 $symlink_count); do
6664                         ln -s "$file1" "$dir/slink$s" ||
6665                                 error "cannot create symlinks"
6666                 done
6667                 echo "done"
6668
6669                 echo -n "creating nonlinked files..."
6670                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
6671                         error "cannot create nonlinked files"
6672                 echo "done"
6673         fi
6674
6675         # create hard links
6676         if [ ! -f "$dir/file$total_count" ]; then
6677                 echo -n "creating hard links $begin:$total_count..."
6678                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
6679                         /dev/null || error "cannot create hard links"
6680                 echo "done"
6681         fi
6682
6683         echo -n "checking number of hard links listed in xattrs..."
6684         local fid=$($LFS getstripe -F "$file1")
6685         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
6686
6687         echo "${#paths[*]}"
6688         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
6689                         skip "hard link list has unexpected size, skipping test"
6690         fi
6691         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
6692                         error "link names should exceed xattrs size"
6693         fi
6694
6695         echo -n "migrating files..."
6696         local migrate_out=$($runas $LFS_MIGRATE -y -S '1m' $dir)
6697         local rc=$?
6698         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
6699         echo "done"
6700
6701         # make sure all links have been properly migrated
6702         echo -n "verifying files..."
6703         fid=$($LFS getstripe -F "$file1") ||
6704                 error "cannot get fid for file $file1"
6705         for i in $(seq 2 $total_count); do
6706                 local fid2=$($LFS getstripe -F $dir/file$i)
6707
6708                 [ "$fid2" == "$fid" ] ||
6709                         error "migrated hard link has mismatched FID"
6710         done
6711
6712         # make sure hard links were properly detected, and migration was
6713         # performed only once for the entire link set; nonlinked files should
6714         # also be migrated
6715         local actual=$(grep -c 'done' <<< "$migrate_out")
6716         local expected=$(($uniq_count + 1))
6717
6718         [ "$actual" -eq  "$expected" ] ||
6719                 error "hard links individually migrated ($actual != $expected)"
6720
6721         # make sure the correct number of hard links are present
6722         local hardlinks=$(stat -c '%h' "$file1")
6723
6724         [ $hardlinks -eq $total_count ] ||
6725                 error "num hard links $hardlinks != $total_count"
6726         echo "done"
6727
6728         return 0
6729 }
6730
6731 test_56xb() {
6732         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
6733                 skip "Need MDS version at least 2.10.55"
6734
6735         local dir="$DIR/$tdir"
6736
6737         test_mkdir "$dir" || error "cannot create dir $dir"
6738
6739         echo "testing lfs migrate mode when all links fit within xattrs"
6740         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
6741
6742         echo "testing rsync mode when all links fit within xattrs"
6743         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
6744
6745         echo "testing lfs migrate mode when all links do not fit within xattrs"
6746         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
6747
6748         echo "testing rsync mode when all links do not fit within xattrs"
6749         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
6750
6751         chown -R $RUNAS_ID $dir
6752         echo "testing non-root lfs migrate mode when not all links are in xattr"
6753         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100 "$RUNAS"
6754
6755         # clean up
6756         rm -rf $dir
6757 }
6758 run_test 56xb "lfs migration hard link support"
6759
6760 test_56xc() {
6761         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6762
6763         local dir="$DIR/$tdir"
6764
6765         test_mkdir "$dir" || error "cannot create dir $dir"
6766
6767         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
6768         echo -n "Setting initial stripe for 20MB test file..."
6769         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
6770                 error "cannot setstripe 20MB file"
6771         echo "done"
6772         echo -n "Sizing 20MB test file..."
6773         truncate "$dir/20mb" 20971520 || error "cannot create 20MB test file"
6774         echo "done"
6775         echo -n "Verifying small file autostripe count is 1..."
6776         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
6777                 error "cannot migrate 20MB file"
6778         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
6779                 error "cannot get stripe for $dir/20mb"
6780         [ $stripe_count -eq 1 ] ||
6781                 error "unexpected stripe count $stripe_count for 20MB file"
6782         rm -f "$dir/20mb"
6783         echo "done"
6784
6785         # Test 2: File is small enough to fit within the available space on
6786         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
6787         # have at least an additional 1KB for each desired stripe for test 3
6788         echo -n "Setting stripe for 1GB test file..."
6789         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
6790         echo "done"
6791         echo -n "Sizing 1GB test file..."
6792         # File size is 1GB + 3KB
6793         truncate "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
6794         echo "done"
6795
6796         # need at least 512MB per OST for 1GB file to fit in 2 stripes
6797         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
6798         if (( avail > 524288 * OSTCOUNT )); then
6799                 echo -n "Migrating 1GB file..."
6800                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
6801                         error "cannot migrate 1GB file"
6802                 echo "done"
6803                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
6804                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
6805                         error "cannot getstripe for 1GB file"
6806                 [ $stripe_count -eq 2 ] ||
6807                         error "unexpected stripe count $stripe_count != 2"
6808                 echo "done"
6809         fi
6810
6811         # Test 3: File is too large to fit within the available space on
6812         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
6813         if [ $OSTCOUNT -ge 3 ]; then
6814                 # The required available space is calculated as
6815                 # file size (1GB + 3KB) / OST count (3).
6816                 local kb_per_ost=349526
6817
6818                 echo -n "Migrating 1GB file with limit..."
6819                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
6820                         error "cannot migrate 1GB file with limit"
6821                 echo "done"
6822
6823                 stripe_count=$($LFS getstripe -c "$dir/1gb")
6824                 echo -n "Verifying 1GB autostripe count with limited space..."
6825                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
6826                         error "unexpected stripe count $stripe_count (min 3)"
6827                 echo "done"
6828         fi
6829
6830         # clean up
6831         rm -rf $dir
6832 }
6833 run_test 56xc "lfs migration autostripe"
6834
6835 test_56y() {
6836         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
6837                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
6838
6839         local res=""
6840         local dir=$DIR/$tdir
6841         local f1=$dir/file1
6842         local f2=$dir/file2
6843
6844         test_mkdir -p $dir || error "creating dir $dir"
6845         touch $f1 || error "creating std file $f1"
6846         $MULTIOP $f2 H2c || error "creating released file $f2"
6847
6848         # a directory can be raid0, so ask only for files
6849         res=$($LFS find $dir -L raid0 -type f | wc -l)
6850         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
6851
6852         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
6853         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
6854
6855         # only files can be released, so no need to force file search
6856         res=$($LFS find $dir -L released)
6857         [[ $res == $f2 ]] || error "search released: found $res != $f2"
6858
6859         res=$($LFS find $dir -type f \! -L released)
6860         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
6861 }
6862 run_test 56y "lfs find -L raid0|released"
6863
6864 test_56z() { # LU-4824
6865         # This checks to make sure 'lfs find' continues after errors
6866         # There are two classes of errors that should be caught:
6867         # - If multiple paths are provided, all should be searched even if one
6868         #   errors out
6869         # - If errors are encountered during the search, it should not terminate
6870         #   early
6871         local dir=$DIR/$tdir
6872         local i
6873
6874         test_mkdir $dir
6875         for i in d{0..9}; do
6876                 test_mkdir $dir/$i
6877                 touch $dir/$i/$tfile
6878         done
6879         $LFS find $DIR/non_existent_dir $dir &&
6880                 error "$LFS find did not return an error"
6881         # Make a directory unsearchable. This should NOT be the last entry in
6882         # directory order.  Arbitrarily pick the 6th entry
6883         chmod 700 $($LFS find $dir -type d | sed '6!d')
6884
6885         $RUNAS $LFS find $DIR/non_existent $dir
6886         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
6887
6888         # The user should be able to see 10 directories and 9 files
6889         (( count == 19 )) ||
6890                 error "$LFS find found $count != 19 entries after error"
6891 }
6892 run_test 56z "lfs find should continue after an error"
6893
6894 test_56aa() { # LU-5937
6895         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
6896
6897         local dir=$DIR/$tdir
6898
6899         mkdir $dir
6900         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
6901
6902         createmany -o $dir/striped_dir/${tfile}- 1024
6903         local dirs=$($LFS find --size +8k $dir/)
6904
6905         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
6906 }
6907 run_test 56aa "lfs find --size under striped dir"
6908
6909 test_56ab() { # LU-10705
6910         test_mkdir $DIR/$tdir
6911         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
6912         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
6913         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
6914         # Flush writes to ensure valid blocks.  Need to be more thorough for
6915         # ZFS, since blocks are not allocated/returned to client immediately.
6916         sync_all_data
6917         wait_zfs_commit ost1 2
6918         cancel_lru_locks osc
6919         ls -ls $DIR/$tdir
6920
6921         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
6922
6923         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
6924
6925         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
6926         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
6927
6928         rm -f $DIR/$tdir/$tfile.[123]
6929 }
6930 run_test 56ab "lfs find --blocks"
6931
6932 test_56ba() {
6933         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
6934                 skip "Need MDS version at least 2.10.50"
6935
6936         # Create composite files with one component
6937         local dir=$DIR/$tdir
6938
6939         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
6940         # Create composite files with three components
6941         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
6942         # Create non-composite files
6943         createmany -o $dir/${tfile}- 10
6944
6945         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
6946
6947         [[ $nfiles == 10 ]] ||
6948                 error "lfs find -E 1M found $nfiles != 10 files"
6949
6950         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
6951         [[ $nfiles == 25 ]] ||
6952                 error "lfs find ! -E 1M found $nfiles != 25 files"
6953
6954         # All files have a component that starts at 0
6955         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
6956         [[ $nfiles == 35 ]] ||
6957                 error "lfs find --component-start 0 - $nfiles != 35 files"
6958
6959         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
6960         [[ $nfiles == 15 ]] ||
6961                 error "lfs find --component-start 2M - $nfiles != 15 files"
6962
6963         # All files created here have a componenet that does not starts at 2M
6964         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
6965         [[ $nfiles == 35 ]] ||
6966                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
6967
6968         # Find files with a specified number of components
6969         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
6970         [[ $nfiles == 15 ]] ||
6971                 error "lfs find --component-count 3 - $nfiles != 15 files"
6972
6973         # Remember non-composite files have a component count of zero
6974         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
6975         [[ $nfiles == 10 ]] ||
6976                 error "lfs find --component-count 0 - $nfiles != 10 files"
6977
6978         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
6979         [[ $nfiles == 20 ]] ||
6980                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
6981
6982         # All files have a flag called "init"
6983         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
6984         [[ $nfiles == 35 ]] ||
6985                 error "lfs find --component-flags init - $nfiles != 35 files"
6986
6987         # Multi-component files will have a component not initialized
6988         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
6989         [[ $nfiles == 15 ]] ||
6990                 error "lfs find !--component-flags init - $nfiles != 15 files"
6991
6992         rm -rf $dir
6993
6994 }
6995 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
6996
6997 test_56ca() {
6998         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
6999                 skip "Need MDS version at least 2.10.57"
7000
7001         local td=$DIR/$tdir
7002         local tf=$td/$tfile
7003         local dir
7004         local nfiles
7005         local cmd
7006         local i
7007         local j
7008
7009         # create mirrored directories and mirrored files
7010         mkdir $td || error "mkdir $td failed"
7011         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
7012         createmany -o $tf- 10 || error "create $tf- failed"
7013
7014         for i in $(seq 2); do
7015                 dir=$td/dir$i
7016                 mkdir $dir || error "mkdir $dir failed"
7017                 $LFS mirror create -N$((3 + i)) $dir ||
7018                         error "create mirrored dir $dir failed"
7019                 createmany -o $dir/$tfile- 10 ||
7020                         error "create $dir/$tfile- failed"
7021         done
7022
7023         # change the states of some mirrored files
7024         echo foo > $tf-6
7025         for i in $(seq 2); do
7026                 dir=$td/dir$i
7027                 for j in $(seq 4 9); do
7028                         echo foo > $dir/$tfile-$j
7029                 done
7030         done
7031
7032         # find mirrored files with specific mirror count
7033         cmd="$LFS find --mirror-count 3 --type f $td"
7034         nfiles=$($cmd | wc -l)
7035         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
7036
7037         cmd="$LFS find ! --mirror-count 3 --type f $td"
7038         nfiles=$($cmd | wc -l)
7039         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
7040
7041         cmd="$LFS find --mirror-count +2 --type f $td"
7042         nfiles=$($cmd | wc -l)
7043         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7044
7045         cmd="$LFS find --mirror-count -6 --type f $td"
7046         nfiles=$($cmd | wc -l)
7047         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7048
7049         # find mirrored files with specific file state
7050         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
7051         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
7052
7053         cmd="$LFS find --mirror-state=ro --type f $td"
7054         nfiles=$($cmd | wc -l)
7055         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
7056
7057         cmd="$LFS find ! --mirror-state=ro --type f $td"
7058         nfiles=$($cmd | wc -l)
7059         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7060
7061         cmd="$LFS find --mirror-state=wp --type f $td"
7062         nfiles=$($cmd | wc -l)
7063         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7064
7065         cmd="$LFS find ! --mirror-state=sp --type f $td"
7066         nfiles=$($cmd | wc -l)
7067         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7068 }
7069 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
7070
7071 test_57a() {
7072         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7073         # note test will not do anything if MDS is not local
7074         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7075                 skip_env "ldiskfs only test"
7076         fi
7077         remote_mds_nodsh && skip "remote MDS with nodsh"
7078
7079         local MNTDEV="osd*.*MDT*.mntdev"
7080         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
7081         [ -z "$DEV" ] && error "can't access $MNTDEV"
7082         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
7083                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
7084                         error "can't access $DEV"
7085                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
7086                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
7087                 rm $TMP/t57a.dump
7088         done
7089 }
7090 run_test 57a "verify MDS filesystem created with large inodes =="
7091
7092 test_57b() {
7093         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7094         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7095                 skip_env "ldiskfs only test"
7096         fi
7097         remote_mds_nodsh && skip "remote MDS with nodsh"
7098
7099         local dir=$DIR/$tdir
7100         local filecount=100
7101         local file1=$dir/f1
7102         local fileN=$dir/f$filecount
7103
7104         rm -rf $dir || error "removing $dir"
7105         test_mkdir -c1 $dir
7106         local mdtidx=$($LFS getstripe -m $dir)
7107         local mdtname=MDT$(printf %04x $mdtidx)
7108         local facet=mds$((mdtidx + 1))
7109
7110         echo "mcreating $filecount files"
7111         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
7112
7113         # verify that files do not have EAs yet
7114         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
7115                 error "$file1 has an EA"
7116         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
7117                 error "$fileN has an EA"
7118
7119         sync
7120         sleep 1
7121         df $dir  #make sure we get new statfs data
7122         local mdsfree=$(do_facet $facet \
7123                         lctl get_param -n osd*.*$mdtname.kbytesfree)
7124         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7125         local file
7126
7127         echo "opening files to create objects/EAs"
7128         for file in $(seq -f $dir/f%g 1 $filecount); do
7129                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
7130                         error "opening $file"
7131         done
7132
7133         # verify that files have EAs now
7134         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
7135         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
7136
7137         sleep 1  #make sure we get new statfs data
7138         df $dir
7139         local mdsfree2=$(do_facet $facet \
7140                          lctl get_param -n osd*.*$mdtname.kbytesfree)
7141         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7142
7143         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
7144                 if [ "$mdsfree" != "$mdsfree2" ]; then
7145                         error "MDC before $mdcfree != after $mdcfree2"
7146                 else
7147                         echo "MDC before $mdcfree != after $mdcfree2"
7148                         echo "unable to confirm if MDS has large inodes"
7149                 fi
7150         fi
7151         rm -rf $dir
7152 }
7153 run_test 57b "default LOV EAs are stored inside large inodes ==="
7154
7155 test_58() {
7156         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7157         [ -z "$(which wiretest 2>/dev/null)" ] &&
7158                         skip_env "could not find wiretest"
7159
7160         wiretest
7161 }
7162 run_test 58 "verify cross-platform wire constants =============="
7163
7164 test_59() {
7165         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7166
7167         echo "touch 130 files"
7168         createmany -o $DIR/f59- 130
7169         echo "rm 130 files"
7170         unlinkmany $DIR/f59- 130
7171         sync
7172         # wait for commitment of removal
7173         wait_delete_completed
7174 }
7175 run_test 59 "verify cancellation of llog records async ========="
7176
7177 TEST60_HEAD="test_60 run $RANDOM"
7178 test_60a() {
7179         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7180         remote_mgs_nodsh && skip "remote MGS with nodsh"
7181         do_facet mgs "! which run-llog.sh &> /dev/null" &&
7182                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
7183                         skip_env "missing subtest run-llog.sh"
7184
7185         log "$TEST60_HEAD - from kernel mode"
7186         do_facet mgs "$LCTL dk > /dev/null"
7187         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
7188         do_facet mgs $LCTL dk > $TMP/$tfile
7189
7190         # LU-6388: test llog_reader
7191         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
7192         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
7193         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
7194                         skip_env "missing llog_reader"
7195         local fstype=$(facet_fstype mgs)
7196         [ $fstype != ldiskfs -a $fstype != zfs ] &&
7197                 skip_env "Only for ldiskfs or zfs type mgs"
7198
7199         local mntpt=$(facet_mntpt mgs)
7200         local mgsdev=$(mgsdevname 1)
7201         local fid_list
7202         local fid
7203         local rec_list
7204         local rec
7205         local rec_type
7206         local obj_file
7207         local path
7208         local seq
7209         local oid
7210         local pass=true
7211
7212         #get fid and record list
7213         fid_list=($(awk '/9_sub.*record/ { print $NF }' $TMP/$tfile |
7214                 tail -n 4))
7215         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' $TMP/$tfile |
7216                 tail -n 4))
7217         #remount mgs as ldiskfs or zfs type
7218         stop mgs || error "stop mgs failed"
7219         mount_fstype mgs || error "remount mgs failed"
7220         for ((i = 0; i < ${#fid_list[@]}; i++)); do
7221                 fid=${fid_list[i]}
7222                 rec=${rec_list[i]}
7223                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
7224                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
7225                 oid=$((16#$oid))
7226
7227                 case $fstype in
7228                         ldiskfs )
7229                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
7230                         zfs )
7231                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
7232                 esac
7233                 echo "obj_file is $obj_file"
7234                 do_facet mgs $llog_reader $obj_file
7235
7236                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
7237                         awk '{ print $3 }' | sed -e "s/^type=//g")
7238                 if [ $rec_type != $rec ]; then
7239                         echo "FAILED test_60a wrong record type $rec_type," \
7240                               "should be $rec"
7241                         pass=false
7242                         break
7243                 fi
7244
7245                 #check obj path if record type is LLOG_LOGID_MAGIC
7246                 if [ "$rec" == "1064553b" ]; then
7247                         path=$(do_facet mgs $llog_reader $obj_file |
7248                                 grep "path=" | awk '{ print $NF }' |
7249                                 sed -e "s/^path=//g")
7250                         if [ $obj_file != $mntpt/$path ]; then
7251                                 echo "FAILED test_60a wrong obj path" \
7252                                       "$montpt/$path, should be $obj_file"
7253                                 pass=false
7254                                 break
7255                         fi
7256                 fi
7257         done
7258         rm -f $TMP/$tfile
7259         #restart mgs before "error", otherwise it will block the next test
7260         stop mgs || error "stop mgs failed"
7261         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
7262         $pass || error "test failed, see FAILED test_60a messages for specifics"
7263 }
7264 run_test 60a "llog_test run from kernel module and test llog_reader"
7265
7266 test_60b() { # bug 6411
7267         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7268
7269         dmesg > $DIR/$tfile
7270         LLOG_COUNT=$(do_facet mgs dmesg |
7271                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
7272                           /llog_[a-z]*.c:[0-9]/ {
7273                                 if (marker)
7274                                         from_marker++
7275                                 from_begin++
7276                           }
7277                           END {
7278                                 if (marker)
7279                                         print from_marker
7280                                 else
7281                                         print from_begin
7282                           }")
7283
7284         [[ $LLOG_COUNT -gt 120 ]] &&
7285                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
7286 }
7287 run_test 60b "limit repeated messages from CERROR/CWARN"
7288
7289 test_60c() {
7290         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7291
7292         echo "create 5000 files"
7293         createmany -o $DIR/f60c- 5000
7294 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
7295         lctl set_param fail_loc=0x80000137
7296         unlinkmany $DIR/f60c- 5000
7297         lctl set_param fail_loc=0
7298 }
7299 run_test 60c "unlink file when mds full"
7300
7301 test_60d() {
7302         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7303
7304         SAVEPRINTK=$(lctl get_param -n printk)
7305         # verify "lctl mark" is even working"
7306         MESSAGE="test message ID $RANDOM $$"
7307         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7308         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
7309
7310         lctl set_param printk=0 || error "set lnet.printk failed"
7311         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
7312         MESSAGE="new test message ID $RANDOM $$"
7313         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
7314         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7315         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
7316
7317         lctl set_param -n printk="$SAVEPRINTK"
7318 }
7319 run_test 60d "test printk console message masking"
7320
7321 test_60e() {
7322         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7323         remote_mds_nodsh && skip "remote MDS with nodsh"
7324
7325         touch $DIR/$tfile
7326 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
7327         do_facet mds1 lctl set_param fail_loc=0x15b
7328         rm $DIR/$tfile
7329 }
7330 run_test 60e "no space while new llog is being created"
7331
7332 test_60g() {
7333         local pid
7334         local i
7335
7336         test_mkdir -c $MDSCOUNT $DIR/$tdir
7337
7338         (
7339                 local index=0
7340                 while true; do
7341                         $LFS setdirstripe -i $(($index % $MDSCOUNT)) \
7342                                 -c $MDSCOUNT $DIR/$tdir/subdir$index \
7343                                 2>/dev/null
7344                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
7345                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
7346                         index=$((index + 1))
7347                 done
7348         ) &
7349
7350         pid=$!
7351
7352         for i in {0..100}; do
7353                 # define OBD_FAIL_OSD_TXN_START    0x19a
7354                 local index=$((i % MDSCOUNT + 1))
7355
7356                 do_facet mds$index $LCTL set_param fail_loc=0x8000019a \
7357                         > /dev/null
7358                 usleep 100
7359         done
7360
7361         kill -9 $pid
7362
7363         for i in $(seq $MDSCOUNT); do
7364                 do_facet mds$i $LCTL set_param fail_loc=0 > /dev/null
7365         done
7366
7367         mkdir $DIR/$tdir/new || error "mkdir failed"
7368         rmdir $DIR/$tdir/new || error "rmdir failed"
7369
7370         do_facet mds1 $LCTL lfsck_start -M $(facet_svc mds1) -A -C \
7371                 -t namespace
7372         for i in $(seq $MDSCOUNT); do
7373                 wait_update_facet mds$i "$LCTL get_param -n \
7374                         mdd.$(facet_svc mds$i).lfsck_namespace |
7375                         awk '/^status/ { print \\\$2 }'" "completed"
7376         done
7377
7378         ls -R $DIR/$tdir || error "ls failed"
7379         rm -rf $DIR/$tdir || error "rmdir failed"
7380 }
7381 run_test 60g "transaction abort won't cause MDT hung"
7382
7383 test_60h() {
7384         [ $MDS1_VERSION -le $(version_code 2.12.52) ] &&
7385                 skip "Need MDS version at least 2.12.52"
7386         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
7387
7388         local f
7389
7390         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
7391         #define OBD_FAIL_MDS_STRIPE_FID          0x189
7392         for fail_loc in 0x80000188 0x80000189; do
7393                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
7394                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
7395                         error "mkdir $dir-$fail_loc failed"
7396                 for i in {0..10}; do
7397                         # create may fail on missing stripe
7398                         echo $i > $DIR/$tdir-$fail_loc/$i
7399                 done
7400                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7401                         error "getdirstripe $tdir-$fail_loc failed"
7402                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
7403                         error "migrate $tdir-$fail_loc failed"
7404                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7405                         error "getdirstripe $tdir-$fail_loc failed"
7406                 pushd $DIR/$tdir-$fail_loc
7407                 for f in *; do
7408                         echo $f | cmp $f - || error "$f data mismatch"
7409                 done
7410                 popd
7411                 rm -rf $DIR/$tdir-$fail_loc
7412         done
7413 }
7414 run_test 60h "striped directory with missing stripes can be accessed"
7415
7416 test_61a() {
7417         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7418
7419         f="$DIR/f61"
7420         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
7421         cancel_lru_locks osc
7422         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
7423         sync
7424 }
7425 run_test 61a "mmap() writes don't make sync hang ================"
7426
7427 test_61b() {
7428         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
7429 }
7430 run_test 61b "mmap() of unstriped file is successful"
7431
7432 # bug 2330 - insufficient obd_match error checking causes LBUG
7433 test_62() {
7434         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7435
7436         f="$DIR/f62"
7437         echo foo > $f
7438         cancel_lru_locks osc
7439         lctl set_param fail_loc=0x405
7440         cat $f && error "cat succeeded, expect -EIO"
7441         lctl set_param fail_loc=0
7442 }
7443 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
7444 # match every page all of the time.
7445 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
7446
7447 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
7448 # Though this test is irrelevant anymore, it helped to reveal some
7449 # other grant bugs (LU-4482), let's keep it.
7450 test_63a() {   # was test_63
7451         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7452
7453         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
7454
7455         for i in `seq 10` ; do
7456                 dd if=/dev/zero of=$DIR/f63 bs=8k &
7457                 sleep 5
7458                 kill $!
7459                 sleep 1
7460         done
7461
7462         rm -f $DIR/f63 || true
7463 }
7464 run_test 63a "Verify oig_wait interruption does not crash ======="
7465
7466 # bug 2248 - async write errors didn't return to application on sync
7467 # bug 3677 - async write errors left page locked
7468 test_63b() {
7469         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7470
7471         debugsave
7472         lctl set_param debug=-1
7473
7474         # ensure we have a grant to do async writes
7475         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
7476         rm $DIR/$tfile
7477
7478         sync    # sync lest earlier test intercept the fail_loc
7479
7480         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7481         lctl set_param fail_loc=0x80000406
7482         $MULTIOP $DIR/$tfile Owy && \
7483                 error "sync didn't return ENOMEM"
7484         sync; sleep 2; sync     # do a real sync this time to flush page
7485         lctl get_param -n llite.*.dump_page_cache | grep locked && \
7486                 error "locked page left in cache after async error" || true
7487         debugrestore
7488 }
7489 run_test 63b "async write errors should be returned to fsync ==="
7490
7491 test_64a () {
7492         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7493
7494         df $DIR
7495         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
7496 }
7497 run_test 64a "verify filter grant calculations (in kernel) ====="
7498
7499 test_64b () {
7500         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7501
7502         sh oos.sh $MOUNT || error "oos.sh failed: $?"
7503 }
7504 run_test 64b "check out-of-space detection on client"
7505
7506 test_64c() {
7507         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
7508 }
7509 run_test 64c "verify grant shrink"
7510
7511 # this does exactly what osc_request.c:osc_announce_cached() does in
7512 # order to calculate max amount of grants to ask from server
7513 want_grant() {
7514         local tgt=$1
7515
7516         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
7517         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
7518
7519         ((rpc_in_flight ++));
7520         nrpages=$((nrpages * rpc_in_flight))
7521
7522         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
7523
7524         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
7525
7526         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
7527         local undirty=$((nrpages * PAGE_SIZE))
7528
7529         local max_extent_pages
7530         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
7531             grep grant_max_extent_size | awk '{print $2}')
7532         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
7533         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
7534         local grant_extent_tax
7535         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7536             grep grant_extent_tax | awk '{print $2}')
7537
7538         undirty=$((undirty + nrextents * grant_extent_tax))
7539
7540         echo $undirty
7541 }
7542
7543 # this is size of unit for grant allocation. It should be equal to
7544 # what tgt_grant.c:tgt_grant_chunk() calculates
7545 grant_chunk() {
7546         local tgt=$1
7547         local max_brw_size
7548         local grant_extent_tax
7549
7550         max_brw_size=$($LCTL get_param osc.${tgt}.import |
7551             grep max_brw_size | awk '{print $2}')
7552
7553         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7554             grep grant_extent_tax | awk '{print $2}')
7555
7556         echo $(((max_brw_size + grant_extent_tax) * 2))
7557 }
7558
7559 test_64d() {
7560         [ $OST1_VERSION -lt $(version_code 2.10.56) ] &&
7561                 skip "OST < 2.10.55 doesn't limit grants enough"
7562
7563         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
7564         local file=$DIR/$tfile
7565
7566         [[ $($LCTL get_param osc.${tgt}.import |
7567              grep "connect_flags:.*grant_param") ]] ||
7568                 skip "no grant_param connect flag"
7569
7570         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
7571
7572         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
7573
7574         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
7575         stack_trap "rm -f $file" EXIT
7576
7577         $LFS setstripe $file -i 0 -c 1
7578         dd if=/dev/zero of=$file bs=1M count=1000 &
7579         ddpid=$!
7580
7581         while true
7582         do
7583                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
7584                 if [[ $cur_grant -gt $max_cur_granted ]]
7585                 then
7586                         kill $ddpid
7587                         error "cur_grant $cur_grant > $max_cur_granted"
7588                 fi
7589                 kill -0 $ddpid
7590                 [[ $? -ne 0 ]] && break;
7591                 sleep 2
7592         done
7593
7594         rm -f $DIR/$tfile
7595         wait_delete_completed
7596         $LCTL set_param debug="$olddebug" 2> /dev/null || true
7597 }
7598 run_test 64d "check grant limit exceed"
7599
7600 # bug 1414 - set/get directories' stripe info
7601 test_65a() {
7602         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7603
7604         test_mkdir $DIR/$tdir
7605         touch $DIR/$tdir/f1
7606         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
7607 }
7608 run_test 65a "directory with no stripe info"
7609
7610 test_65b() {
7611         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7612
7613         test_mkdir $DIR/$tdir
7614         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7615
7616         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7617                                                 error "setstripe"
7618         touch $DIR/$tdir/f2
7619         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
7620 }
7621 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
7622
7623 test_65c() {
7624         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7625         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
7626
7627         test_mkdir $DIR/$tdir
7628         local stripesize=$($LFS getstripe -S $DIR/$tdir)
7629
7630         $LFS setstripe -S $((stripesize * 4)) -i 1 \
7631                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
7632         touch $DIR/$tdir/f3
7633         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
7634 }
7635 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
7636
7637 test_65d() {
7638         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7639
7640         test_mkdir $DIR/$tdir
7641         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
7642         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7643
7644         if [[ $STRIPECOUNT -le 0 ]]; then
7645                 sc=1
7646         elif [[ $STRIPECOUNT -gt $LOV_MAX_STRIPE_COUNT ]]; then
7647                 [[ $OSTCOUNT -gt $LOV_MAX_STRIPE_COUNT ]] &&
7648                         sc=$LOV_MAX_STRIPE_COUNT || sc=$(($OSTCOUNT - 1))
7649         else
7650                 sc=$(($STRIPECOUNT - 1))
7651         fi
7652         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
7653         touch $DIR/$tdir/f4 $DIR/$tdir/f5
7654         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
7655                 error "lverify failed"
7656 }
7657 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
7658
7659 test_65e() {
7660         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7661
7662         test_mkdir $DIR/$tdir
7663
7664         $LFS setstripe $DIR/$tdir || error "setstripe"
7665         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7666                                         error "no stripe info failed"
7667         touch $DIR/$tdir/f6
7668         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
7669 }
7670 run_test 65e "directory setstripe defaults"
7671
7672 test_65f() {
7673         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7674
7675         test_mkdir $DIR/${tdir}f
7676         $RUNAS $LFS setstripe $DIR/${tdir}f &&
7677                 error "setstripe succeeded" || true
7678 }
7679 run_test 65f "dir setstripe permission (should return error) ==="
7680
7681 test_65g() {
7682         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7683
7684         test_mkdir $DIR/$tdir
7685         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7686
7687         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7688                 error "setstripe -S failed"
7689         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
7690         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7691                 error "delete default stripe failed"
7692 }
7693 run_test 65g "directory setstripe -d"
7694
7695 test_65h() {
7696         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7697
7698         test_mkdir $DIR/$tdir
7699         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7700
7701         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7702                 error "setstripe -S failed"
7703         test_mkdir $DIR/$tdir/dd1
7704         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
7705                 error "stripe info inherit failed"
7706 }
7707 run_test 65h "directory stripe info inherit ===================="
7708
7709 test_65i() {
7710         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7711
7712         save_layout_restore_at_exit $MOUNT
7713
7714         # bug6367: set non-default striping on root directory
7715         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
7716
7717         # bug12836: getstripe on -1 default directory striping
7718         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
7719
7720         # bug12836: getstripe -v on -1 default directory striping
7721         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
7722
7723         # bug12836: new find on -1 default directory striping
7724         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
7725 }
7726 run_test 65i "various tests to set root directory striping"
7727
7728 test_65j() { # bug6367
7729         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7730
7731         sync; sleep 1
7732
7733         # if we aren't already remounting for each test, do so for this test
7734         if [ "$I_MOUNTED" = "yes" ]; then
7735                 cleanup || error "failed to unmount"
7736                 setup
7737         fi
7738
7739         save_layout_restore_at_exit $MOUNT
7740
7741         $LFS setstripe -d $MOUNT || error "setstripe failed"
7742 }
7743 run_test 65j "set default striping on root directory (bug 6367)="
7744
7745 cleanup_65k() {
7746         rm -rf $DIR/$tdir
7747         wait_delete_completed
7748         do_facet $SINGLEMDS "lctl set_param -n \
7749                 osp.$ost*MDT0000.max_create_count=$max_count"
7750         do_facet $SINGLEMDS "lctl set_param -n \
7751                 osp.$ost*MDT0000.create_count=$count"
7752         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7753         echo $INACTIVE_OSC "is Activate"
7754
7755         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7756 }
7757
7758 test_65k() { # bug11679
7759         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7760         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7761         remote_mds_nodsh && skip "remote MDS with nodsh"
7762
7763         local disable_precreate=true
7764         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
7765                 disable_precreate=false
7766
7767         echo "Check OST status: "
7768         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
7769                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
7770
7771         for OSC in $MDS_OSCS; do
7772                 echo $OSC "is active"
7773                 do_facet $SINGLEMDS lctl --device %$OSC activate
7774         done
7775
7776         for INACTIVE_OSC in $MDS_OSCS; do
7777                 local ost=$(osc_to_ost $INACTIVE_OSC)
7778                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
7779                                lov.*md*.target_obd |
7780                                awk -F: /$ost/'{ print $1 }' | head -n 1)
7781
7782                 mkdir -p $DIR/$tdir
7783                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
7784                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
7785
7786                 echo "Deactivate: " $INACTIVE_OSC
7787                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
7788
7789                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
7790                               osp.$ost*MDT0000.create_count")
7791                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
7792                                   osp.$ost*MDT0000.max_create_count")
7793                 $disable_precreate &&
7794                         do_facet $SINGLEMDS "lctl set_param -n \
7795                                 osp.$ost*MDT0000.max_create_count=0"
7796
7797                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
7798                         [ -f $DIR/$tdir/$idx ] && continue
7799                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
7800                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
7801                                 { cleanup_65k;
7802                                   error "setstripe $idx should succeed"; }
7803                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
7804                 done
7805                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
7806                 rmdir $DIR/$tdir
7807
7808                 do_facet $SINGLEMDS "lctl set_param -n \
7809                         osp.$ost*MDT0000.max_create_count=$max_count"
7810                 do_facet $SINGLEMDS "lctl set_param -n \
7811                         osp.$ost*MDT0000.create_count=$count"
7812                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7813                 echo $INACTIVE_OSC "is Activate"
7814
7815                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7816         done
7817 }
7818 run_test 65k "validate manual striping works properly with deactivated OSCs"
7819
7820 test_65l() { # bug 12836
7821         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7822
7823         test_mkdir -p $DIR/$tdir/test_dir
7824         $LFS setstripe -c -1 $DIR/$tdir/test_dir
7825         $LFS find -mtime -1 $DIR/$tdir >/dev/null
7826 }
7827 run_test 65l "lfs find on -1 stripe dir ========================"
7828
7829 test_65m() {
7830         local layout=$(save_layout $MOUNT)
7831         $RUNAS $LFS setstripe -c 2 $MOUNT && {
7832                 restore_layout $MOUNT $layout
7833                 error "setstripe should fail by non-root users"
7834         }
7835         true
7836 }
7837 run_test 65m "normal user can't set filesystem default stripe"
7838
7839 test_65n() {
7840         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
7841         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.12.50) ]] ||
7842                 skip "Need MDS version at least 2.12.50"
7843         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
7844
7845         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7846         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
7847         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
7848
7849         local root_layout=$(save_layout $MOUNT)
7850         stack_trap "restore_layout $MOUNT $root_layout" EXIT
7851
7852         # new subdirectory under root directory should not inherit
7853         # the default layout from root
7854         local dir1=$MOUNT/$tdir-1
7855         mkdir $dir1 || error "mkdir $dir1 failed"
7856         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
7857                 error "$dir1 shouldn't have LOV EA"
7858
7859         # delete the default layout on root directory
7860         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
7861
7862         local dir2=$MOUNT/$tdir-2
7863         mkdir $dir2 || error "mkdir $dir2 failed"
7864         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
7865                 error "$dir2 shouldn't have LOV EA"
7866
7867         # set a new striping pattern on root directory
7868         local def_stripe_size=$($LFS getstripe -S $MOUNT)
7869         local new_def_stripe_size=$((def_stripe_size * 2))
7870         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
7871                 error "set stripe size on $MOUNT failed"
7872
7873         # new file created in $dir2 should inherit the new stripe size from
7874         # the filesystem default
7875         local file2=$dir2/$tfile-2
7876         touch $file2 || error "touch $file2 failed"
7877
7878         local file2_stripe_size=$($LFS getstripe -S $file2)
7879         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
7880                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
7881
7882         local dir3=$MOUNT/$tdir-3
7883         mkdir $dir3 || error "mkdir $dir3 failed"
7884         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
7885         # the root layout, which is the actual default layout that will be used
7886         # when new files are created in $dir3.
7887         local dir3_layout=$(get_layout_param $dir3)
7888         local root_dir_layout=$(get_layout_param $MOUNT)
7889         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
7890                 error "$dir3 should show the default layout from $MOUNT"
7891
7892         # set OST pool on root directory
7893         local pool=$TESTNAME
7894         pool_add $pool || error "add $pool failed"
7895         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
7896                 error "add targets to $pool failed"
7897
7898         $LFS setstripe -p $pool $MOUNT ||
7899                 error "set OST pool on $MOUNT failed"
7900
7901         # new file created in $dir3 should inherit the pool from
7902         # the filesystem default
7903         local file3=$dir3/$tfile-3
7904         touch $file3 || error "touch $file3 failed"
7905
7906         local file3_pool=$($LFS getstripe -p $file3)
7907         [[ "$file3_pool" = "$pool" ]] ||
7908                 error "$file3 didn't inherit OST pool $pool"
7909
7910         local dir4=$MOUNT/$tdir-4
7911         mkdir $dir4 || error "mkdir $dir4 failed"
7912         local dir4_layout=$(get_layout_param $dir4)
7913         root_dir_layout=$(get_layout_param $MOUNT)
7914         echo "$LFS getstripe -d $dir4"
7915         $LFS getstripe -d $dir4
7916         echo "$LFS getstripe -d $MOUNT"
7917         $LFS getstripe -d $MOUNT
7918         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
7919                 error "$dir4 should show the default layout from $MOUNT"
7920
7921         # new file created in $dir4 should inherit the pool from
7922         # the filesystem default
7923         local file4=$dir4/$tfile-4
7924         touch $file4 || error "touch $file4 failed"
7925
7926         local file4_pool=$($LFS getstripe -p $file4)
7927         [[ "$file4_pool" = "$pool" ]] ||
7928                 error "$file4 didn't inherit OST pool $pool"
7929
7930         # new subdirectory under non-root directory should inherit
7931         # the default layout from its parent directory
7932         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
7933                 error "set directory layout on $dir4 failed"
7934
7935         local dir5=$dir4/$tdir-5
7936         mkdir $dir5 || error "mkdir $dir5 failed"
7937
7938         dir4_layout=$(get_layout_param $dir4)
7939         local dir5_layout=$(get_layout_param $dir5)
7940         [[ "$dir4_layout" = "$dir5_layout" ]] ||
7941                 error "$dir5 should inherit the default layout from $dir4"
7942
7943         # though subdir under ROOT doesn't inherit default layout, but
7944         # its sub dir/file should be created with default layout.
7945         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
7946         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
7947                 skip "Need MDS version at least 2.12.59"
7948
7949         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
7950         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
7951         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
7952
7953         if [ $default_lmv_hash == "none" ]; then
7954                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
7955         else
7956                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
7957                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
7958         fi
7959
7960         $LFS setdirstripe -D -c 2 $MOUNT ||
7961                 error "setdirstripe -D -c 2 failed"
7962         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
7963         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
7964         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
7965 }
7966 run_test 65n "don't inherit default layout from root for new subdirectories"
7967
7968 # bug 2543 - update blocks count on client
7969 test_66() {
7970         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7971
7972         COUNT=${COUNT:-8}
7973         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
7974         sync; sync_all_data; sync; sync_all_data
7975         cancel_lru_locks osc
7976         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
7977         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
7978 }
7979 run_test 66 "update inode blocks count on client ==============="
7980
7981 meminfo() {
7982         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
7983 }
7984
7985 swap_used() {
7986         swapon -s | awk '($1 == "'$1'") { print $4 }'
7987 }
7988
7989 # bug5265, obdfilter oa2dentry return -ENOENT
7990 # #define OBD_FAIL_SRV_ENOENT 0x217
7991 test_69() {
7992         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7993         remote_ost_nodsh && skip "remote OST with nodsh"
7994
7995         f="$DIR/$tfile"
7996         $LFS setstripe -c 1 -i 0 $f
7997
7998         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
7999
8000         do_facet ost1 lctl set_param fail_loc=0x217
8001         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
8002         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
8003
8004         do_facet ost1 lctl set_param fail_loc=0
8005         $DIRECTIO write $f 0 2 || error "write error"
8006
8007         cancel_lru_locks osc
8008         $DIRECTIO read $f 0 1 || error "read error"
8009
8010         do_facet ost1 lctl set_param fail_loc=0x217
8011         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
8012
8013         do_facet ost1 lctl set_param fail_loc=0
8014         rm -f $f
8015 }
8016 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
8017
8018 test_71() {
8019         test_mkdir $DIR/$tdir
8020         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
8021         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
8022 }
8023 run_test 71 "Running dbench on lustre (don't segment fault) ===="
8024
8025 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
8026         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8027         [ "$RUNAS_ID" = "$UID" ] &&
8028                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8029         # Check that testing environment is properly set up. Skip if not
8030         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
8031                 skip_env "User $RUNAS_ID does not exist - skipping"
8032
8033         touch $DIR/$tfile
8034         chmod 777 $DIR/$tfile
8035         chmod ug+s $DIR/$tfile
8036         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
8037                 error "$RUNAS dd $DIR/$tfile failed"
8038         # See if we are still setuid/sgid
8039         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8040                 error "S/gid is not dropped on write"
8041         # Now test that MDS is updated too
8042         cancel_lru_locks mdc
8043         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8044                 error "S/gid is not dropped on MDS"
8045         rm -f $DIR/$tfile
8046 }
8047 run_test 72a "Test that remove suid works properly (bug5695) ===="
8048
8049 test_72b() { # bug 24226 -- keep mode setting when size is not changing
8050         local perm
8051
8052         [ "$RUNAS_ID" = "$UID" ] &&
8053                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8054         [ "$RUNAS_ID" -eq 0 ] &&
8055                 skip_env "RUNAS_ID = 0 -- skipping"
8056         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8057         # Check that testing environment is properly set up. Skip if not
8058         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
8059                 skip_env "User $RUNAS_ID does not exist - skipping"
8060
8061         touch $DIR/${tfile}-f{g,u}
8062         test_mkdir $DIR/${tfile}-dg
8063         test_mkdir $DIR/${tfile}-du
8064         chmod 770 $DIR/${tfile}-{f,d}{g,u}
8065         chmod g+s $DIR/${tfile}-{f,d}g
8066         chmod u+s $DIR/${tfile}-{f,d}u
8067         for perm in 777 2777 4777; do
8068                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
8069                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
8070                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
8071                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
8072         done
8073         true
8074 }
8075 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
8076
8077 # bug 3462 - multiple simultaneous MDC requests
8078 test_73() {
8079         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8080
8081         test_mkdir $DIR/d73-1
8082         test_mkdir $DIR/d73-2
8083         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
8084         pid1=$!
8085
8086         lctl set_param fail_loc=0x80000129
8087         $MULTIOP $DIR/d73-1/f73-2 Oc &
8088         sleep 1
8089         lctl set_param fail_loc=0
8090
8091         $MULTIOP $DIR/d73-2/f73-3 Oc &
8092         pid3=$!
8093
8094         kill -USR1 $pid1
8095         wait $pid1 || return 1
8096
8097         sleep 25
8098
8099         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
8100         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
8101         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
8102
8103         rm -rf $DIR/d73-*
8104 }
8105 run_test 73 "multiple MDC requests (should not deadlock)"
8106
8107 test_74a() { # bug 6149, 6184
8108         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8109
8110         touch $DIR/f74a
8111         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8112         #
8113         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8114         # will spin in a tight reconnection loop
8115         $LCTL set_param fail_loc=0x8000030e
8116         # get any lock that won't be difficult - lookup works.
8117         ls $DIR/f74a
8118         $LCTL set_param fail_loc=0
8119         rm -f $DIR/f74a
8120         true
8121 }
8122 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
8123
8124 test_74b() { # bug 13310
8125         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8126
8127         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8128         #
8129         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8130         # will spin in a tight reconnection loop
8131         $LCTL set_param fail_loc=0x8000030e
8132         # get a "difficult" lock
8133         touch $DIR/f74b
8134         $LCTL set_param fail_loc=0
8135         rm -f $DIR/f74b
8136         true
8137 }
8138 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
8139
8140 test_74c() {
8141         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8142
8143         #define OBD_FAIL_LDLM_NEW_LOCK
8144         $LCTL set_param fail_loc=0x319
8145         touch $DIR/$tfile && error "touch successful"
8146         $LCTL set_param fail_loc=0
8147         true
8148 }
8149 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
8150
8151 num_inodes() {
8152         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
8153 }
8154
8155 test_76() { # Now for bug 20433, added originally in bug 1443
8156         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8157
8158         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
8159
8160         cancel_lru_locks osc
8161         BEFORE_INODES=$(num_inodes)
8162         echo "before inodes: $BEFORE_INODES"
8163         local COUNT=1000
8164         [ "$SLOW" = "no" ] && COUNT=100
8165         for i in $(seq $COUNT); do
8166                 touch $DIR/$tfile
8167                 rm -f $DIR/$tfile
8168         done
8169         cancel_lru_locks osc
8170         AFTER_INODES=$(num_inodes)
8171         echo "after inodes: $AFTER_INODES"
8172         local wait=0
8173         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
8174                 sleep 2
8175                 AFTER_INODES=$(num_inodes)
8176                 wait=$((wait+2))
8177                 echo "wait $wait seconds inodes: $AFTER_INODES"
8178                 if [ $wait -gt 30 ]; then
8179                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
8180                 fi
8181         done
8182 }
8183 run_test 76 "confirm clients recycle inodes properly ===="
8184
8185
8186 export ORIG_CSUM=""
8187 set_checksums()
8188 {
8189         # Note: in sptlrpc modes which enable its own bulk checksum, the
8190         # original crc32_le bulk checksum will be automatically disabled,
8191         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
8192         # will be checked by sptlrpc code against sptlrpc bulk checksum.
8193         # In this case set_checksums() will not be no-op, because sptlrpc
8194         # bulk checksum will be enabled all through the test.
8195
8196         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
8197         lctl set_param -n osc.*.checksums $1
8198         return 0
8199 }
8200
8201 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8202                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
8203 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8204                              tr -d [] | head -n1)}
8205 set_checksum_type()
8206 {
8207         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
8208         rc=$?
8209         log "set checksum type to $1, rc = $rc"
8210         return $rc
8211 }
8212
8213 get_osc_checksum_type()
8214 {
8215         # arugment 1: OST name, like OST0000
8216         ost=$1
8217         checksum_type=$(lctl get_param -n osc.*${ost}-osc-[^mM]*.checksum_type |
8218                         sed 's/.*\[\(.*\)\].*/\1/g')
8219         rc=$?
8220         [ $rc -ne 0 ] && error "failed to get checksum type of $ost, rc = $rc, output = $checksum_type"
8221         echo $checksum_type
8222 }
8223
8224 F77_TMP=$TMP/f77-temp
8225 F77SZ=8
8226 setup_f77() {
8227         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
8228                 error "error writing to $F77_TMP"
8229 }
8230
8231 test_77a() { # bug 10889
8232         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8233         $GSS && skip_env "could not run with gss"
8234
8235         [ ! -f $F77_TMP ] && setup_f77
8236         set_checksums 1
8237         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
8238         set_checksums 0
8239         rm -f $DIR/$tfile
8240 }
8241 run_test 77a "normal checksum read/write operation"
8242
8243 test_77b() { # bug 10889
8244         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8245         $GSS && skip_env "could not run with gss"
8246
8247         [ ! -f $F77_TMP ] && setup_f77
8248         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8249         $LCTL set_param fail_loc=0x80000409
8250         set_checksums 1
8251
8252         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8253                 error "dd error: $?"
8254         $LCTL set_param fail_loc=0
8255
8256         for algo in $CKSUM_TYPES; do
8257                 cancel_lru_locks osc
8258                 set_checksum_type $algo
8259                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8260                 $LCTL set_param fail_loc=0x80000408
8261                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
8262                 $LCTL set_param fail_loc=0
8263         done
8264         set_checksums 0
8265         set_checksum_type $ORIG_CSUM_TYPE
8266         rm -f $DIR/$tfile
8267 }
8268 run_test 77b "checksum error on client write, read"
8269
8270 cleanup_77c() {
8271         trap 0
8272         set_checksums 0
8273         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
8274         $check_ost &&
8275                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
8276         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
8277         $check_ost && [ -n "$ost_file_prefix" ] &&
8278                 do_facet ost1 rm -f ${ost_file_prefix}\*
8279 }
8280
8281 test_77c() {
8282         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8283         $GSS && skip_env "could not run with gss"
8284         remote_ost_nodsh && skip "remote OST with nodsh"
8285
8286         local bad1
8287         local osc_file_prefix
8288         local osc_file
8289         local check_ost=false
8290         local ost_file_prefix
8291         local ost_file
8292         local orig_cksum
8293         local dump_cksum
8294         local fid
8295
8296         # ensure corruption will occur on first OSS/OST
8297         $LFS setstripe -i 0 $DIR/$tfile
8298
8299         [ ! -f $F77_TMP ] && setup_f77
8300         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8301                 error "dd write error: $?"
8302         fid=$($LFS path2fid $DIR/$tfile)
8303
8304         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
8305         then
8306                 check_ost=true
8307                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
8308                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
8309         else
8310                 echo "OSS do not support bulk pages dump upon error"
8311         fi
8312
8313         osc_file_prefix=$($LCTL get_param -n debug_path)
8314         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
8315
8316         trap cleanup_77c EXIT
8317
8318         set_checksums 1
8319         # enable bulk pages dump upon error on Client
8320         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
8321         # enable bulk pages dump upon error on OSS
8322         $check_ost &&
8323                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
8324
8325         # flush Client cache to allow next read to reach OSS
8326         cancel_lru_locks osc
8327
8328         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
8329         $LCTL set_param fail_loc=0x80000408
8330         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
8331         $LCTL set_param fail_loc=0
8332
8333         rm -f $DIR/$tfile
8334
8335         # check cksum dump on Client
8336         osc_file=$(ls ${osc_file_prefix}*)
8337         [ -n "$osc_file" ] || error "no checksum dump file on Client"
8338         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
8339         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
8340         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
8341         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
8342                      cksum)
8343         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
8344         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8345                 error "dump content does not match on Client"
8346
8347         $check_ost || skip "No need to check cksum dump on OSS"
8348
8349         # check cksum dump on OSS
8350         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
8351         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
8352         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
8353         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
8354         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8355                 error "dump content does not match on OSS"
8356
8357         cleanup_77c
8358 }
8359 run_test 77c "checksum error on client read with debug"
8360
8361 test_77d() { # bug 10889
8362         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8363         $GSS && skip_env "could not run with gss"
8364
8365         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8366         $LCTL set_param fail_loc=0x80000409
8367         set_checksums 1
8368         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8369                 error "direct write: rc=$?"
8370         $LCTL set_param fail_loc=0
8371         set_checksums 0
8372
8373         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8374         $LCTL set_param fail_loc=0x80000408
8375         set_checksums 1
8376         cancel_lru_locks osc
8377         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8378                 error "direct read: rc=$?"
8379         $LCTL set_param fail_loc=0
8380         set_checksums 0
8381 }
8382 run_test 77d "checksum error on OST direct write, read"
8383
8384 test_77f() { # bug 10889
8385         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8386         $GSS && skip_env "could not run with gss"
8387
8388         set_checksums 1
8389         for algo in $CKSUM_TYPES; do
8390                 cancel_lru_locks osc
8391                 set_checksum_type $algo
8392                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8393                 $LCTL set_param fail_loc=0x409
8394                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
8395                         error "direct write succeeded"
8396                 $LCTL set_param fail_loc=0
8397         done
8398         set_checksum_type $ORIG_CSUM_TYPE
8399         set_checksums 0
8400 }
8401 run_test 77f "repeat checksum error on write (expect error)"
8402
8403 test_77g() { # bug 10889
8404         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8405         $GSS && skip_env "could not run with gss"
8406         remote_ost_nodsh && skip "remote OST with nodsh"
8407
8408         [ ! -f $F77_TMP ] && setup_f77
8409
8410         local file=$DIR/$tfile
8411         stack_trap "rm -f $file" EXIT
8412
8413         $LFS setstripe -c 1 -i 0 $file
8414         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
8415         do_facet ost1 lctl set_param fail_loc=0x8000021a
8416         set_checksums 1
8417         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
8418                 error "write error: rc=$?"
8419         do_facet ost1 lctl set_param fail_loc=0
8420         set_checksums 0
8421
8422         cancel_lru_locks osc
8423         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
8424         do_facet ost1 lctl set_param fail_loc=0x8000021b
8425         set_checksums 1
8426         cmp $F77_TMP $file || error "file compare failed"
8427         do_facet ost1 lctl set_param fail_loc=0
8428         set_checksums 0
8429 }
8430 run_test 77g "checksum error on OST write, read"
8431
8432 test_77k() { # LU-10906
8433         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8434         $GSS && skip_env "could not run with gss"
8435
8436         local cksum_param="osc.$FSNAME*.checksums"
8437         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
8438         local checksum
8439         local i
8440
8441         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
8442         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
8443         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
8444                 EXIT
8445
8446         for i in 0 1; do
8447                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
8448                         error "failed to set checksum=$i on MGS"
8449                 wait_update $HOSTNAME "$get_checksum" $i
8450                 #remount
8451                 echo "remount client, checksum should be $i"
8452                 remount_client $MOUNT || error "failed to remount client"
8453                 checksum=$(eval $get_checksum)
8454                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8455         done
8456         # remove persistent param to avoid races with checksum mountopt below
8457         do_facet mgs $LCTL set_param -P -d $cksum_param ||
8458                 error "failed to delete checksum on MGS"
8459
8460         for opt in "checksum" "nochecksum"; do
8461                 #remount with mount option
8462                 echo "remount client with option $opt, checksum should be $i"
8463                 umount_client $MOUNT || error "failed to umount client"
8464                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
8465                         error "failed to mount client with option '$opt'"
8466                 checksum=$(eval $get_checksum)
8467                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8468                 i=$((i - 1))
8469         done
8470
8471         remount_client $MOUNT || error "failed to remount client"
8472 }
8473 run_test 77k "enable/disable checksum correctly"
8474
8475 test_77l() {
8476         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8477         $GSS && skip_env "could not run with gss"
8478
8479         set_checksums 1
8480         stack_trap "set_checksums $ORIG_CSUM" EXIT
8481         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
8482
8483         set_checksum_type invalid && error "unexpected success of invalid checksum type"
8484
8485         $LFS setstripe -c 1 -i 0 $DIR/$tfile
8486         for algo in $CKSUM_TYPES; do
8487                 set_checksum_type $algo || error "fail to set checksum type $algo"
8488                 osc_algo=$(get_osc_checksum_type OST0000)
8489                 [ "$osc_algo" != "$algo" ] && error "checksum type is $osc_algo after setting it to $algo"
8490
8491                 # no locks, no reqs to let the connection idle
8492                 cancel_lru_locks osc
8493                 lru_resize_disable osc
8494                 wait_osc_import_state client ost1 IDLE
8495
8496                 # ensure ost1 is connected
8497                 stat $DIR/$tfile >/dev/null || error "can't stat"
8498                 wait_osc_import_state client ost1 FULL
8499
8500                 osc_algo=$(get_osc_checksum_type OST0000)
8501                 [ "$osc_algo" != "$algo" ] && error "checksum type changed from $algo to $osc_algo after reconnection"
8502         done
8503         return 0
8504 }
8505 run_test 77l "preferred checksum type is remembered after reconnected"
8506
8507 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
8508 rm -f $F77_TMP
8509 unset F77_TMP
8510
8511 cleanup_test_78() {
8512         trap 0
8513         rm -f $DIR/$tfile
8514 }
8515
8516 test_78() { # bug 10901
8517         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8518         remote_ost || skip_env "local OST"
8519
8520         NSEQ=5
8521         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
8522         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
8523         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
8524         echo "MemTotal: $MEMTOTAL"
8525
8526         # reserve 256MB of memory for the kernel and other running processes,
8527         # and then take 1/2 of the remaining memory for the read/write buffers.
8528         if [ $MEMTOTAL -gt 512 ] ;then
8529                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
8530         else
8531                 # for those poor memory-starved high-end clusters...
8532                 MEMTOTAL=$((MEMTOTAL / 2))
8533         fi
8534         echo "Mem to use for directio: $MEMTOTAL"
8535
8536         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
8537         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
8538         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
8539         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
8540                 head -n1)
8541         echo "Smallest OST: $SMALLESTOST"
8542         [[ $SMALLESTOST -lt 10240 ]] &&
8543                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
8544
8545         trap cleanup_test_78 EXIT
8546
8547         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
8548                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
8549
8550         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
8551         echo "File size: $F78SIZE"
8552         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
8553         for i in $(seq 1 $NSEQ); do
8554                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
8555                 echo directIO rdwr round $i of $NSEQ
8556                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
8557         done
8558
8559         cleanup_test_78
8560 }
8561 run_test 78 "handle large O_DIRECT writes correctly ============"
8562
8563 test_79() { # bug 12743
8564         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8565
8566         wait_delete_completed
8567
8568         BKTOTAL=$(calc_osc_kbytes kbytestotal)
8569         BKFREE=$(calc_osc_kbytes kbytesfree)
8570         BKAVAIL=$(calc_osc_kbytes kbytesavail)
8571
8572         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
8573         DFTOTAL=`echo $STRING | cut -d, -f1`
8574         DFUSED=`echo $STRING  | cut -d, -f2`
8575         DFAVAIL=`echo $STRING | cut -d, -f3`
8576         DFFREE=$(($DFTOTAL - $DFUSED))
8577
8578         ALLOWANCE=$((64 * $OSTCOUNT))
8579
8580         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
8581            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
8582                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
8583         fi
8584         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
8585            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
8586                 error "df free($DFFREE) mismatch OST free($BKFREE)"
8587         fi
8588         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
8589            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
8590                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
8591         fi
8592 }
8593 run_test 79 "df report consistency check ======================="
8594
8595 test_80() { # bug 10718
8596         remote_ost_nodsh && skip "remote OST with nodsh"
8597         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8598
8599         # relax strong synchronous semantics for slow backends like ZFS
8600         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
8601                 local soc="obdfilter.*.sync_lock_cancel"
8602                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8603
8604                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
8605                 if [ -z "$save" ]; then
8606                         soc="obdfilter.*.sync_on_lock_cancel"
8607                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8608                 fi
8609
8610                 if [ "$save" != "never" ]; then
8611                         local hosts=$(comma_list $(osts_nodes))
8612
8613                         do_nodes $hosts $LCTL set_param $soc=never
8614                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
8615                 fi
8616         fi
8617
8618         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
8619         sync; sleep 1; sync
8620         local before=$(date +%s)
8621         cancel_lru_locks osc
8622         local after=$(date +%s)
8623         local diff=$((after - before))
8624         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
8625
8626         rm -f $DIR/$tfile
8627 }
8628 run_test 80 "Page eviction is equally fast at high offsets too"
8629
8630 test_81a() { # LU-456
8631         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8632         remote_ost_nodsh && skip "remote OST with nodsh"
8633
8634         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8635         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
8636         do_facet ost1 lctl set_param fail_loc=0x80000228
8637
8638         # write should trigger a retry and success
8639         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8640         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8641         RC=$?
8642         if [ $RC -ne 0 ] ; then
8643                 error "write should success, but failed for $RC"
8644         fi
8645 }
8646 run_test 81a "OST should retry write when get -ENOSPC ==============="
8647
8648 test_81b() { # LU-456
8649         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8650         remote_ost_nodsh && skip "remote OST with nodsh"
8651
8652         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8653         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
8654         do_facet ost1 lctl set_param fail_loc=0x228
8655
8656         # write should retry several times and return -ENOSPC finally
8657         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8658         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8659         RC=$?
8660         ENOSPC=28
8661         if [ $RC -ne $ENOSPC ] ; then
8662                 error "dd should fail for -ENOSPC, but succeed."
8663         fi
8664 }
8665 run_test 81b "OST should return -ENOSPC when retry still fails ======="
8666
8667 test_82() { # LU-1031
8668         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
8669         local gid1=14091995
8670         local gid2=16022000
8671
8672         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
8673         local MULTIPID1=$!
8674         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
8675         local MULTIPID2=$!
8676         kill -USR1 $MULTIPID2
8677         sleep 2
8678         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
8679                 error "First grouplock does not block second one"
8680         else
8681                 echo "Second grouplock blocks first one"
8682         fi
8683         kill -USR1 $MULTIPID1
8684         wait $MULTIPID1
8685         wait $MULTIPID2
8686 }
8687 run_test 82 "Basic grouplock test"
8688
8689 test_99() {
8690         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
8691
8692         test_mkdir $DIR/$tdir.cvsroot
8693         chown $RUNAS_ID $DIR/$tdir.cvsroot
8694
8695         cd $TMP
8696         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
8697
8698         cd /etc/init.d
8699         # some versions of cvs import exit(1) when asked to import links or
8700         # files they can't read.  ignore those files.
8701         local toignore=$(find . -type l -printf '-I %f\n' -o \
8702                          ! -perm /4 -printf '-I %f\n')
8703         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
8704                 $tdir.reposname vtag rtag
8705
8706         cd $DIR
8707         test_mkdir $DIR/$tdir.reposname
8708         chown $RUNAS_ID $DIR/$tdir.reposname
8709         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
8710
8711         cd $DIR/$tdir.reposname
8712         $RUNAS touch foo99
8713         $RUNAS cvs add -m 'addmsg' foo99
8714         $RUNAS cvs update
8715         $RUNAS cvs commit -m 'nomsg' foo99
8716         rm -fr $DIR/$tdir.cvsroot
8717 }
8718 run_test 99 "cvs strange file/directory operations"
8719
8720 test_100() {
8721         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8722         [[ "$NETTYPE" =~ tcp ]] ||
8723                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
8724         remote_ost_nodsh && skip "remote OST with nodsh"
8725         remote_mds_nodsh && skip "remote MDS with nodsh"
8726         remote_servers ||
8727                 skip "useless for local single node setup"
8728
8729         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
8730                 [ "$PROT" != "tcp" ] && continue
8731                 RPORT=$(echo $REMOTE | cut -d: -f2)
8732                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
8733
8734                 rc=0
8735                 LPORT=`echo $LOCAL | cut -d: -f2`
8736                 if [ $LPORT -ge 1024 ]; then
8737                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
8738                         netstat -tna
8739                         error_exit "local: $LPORT > 1024, remote: $RPORT"
8740                 fi
8741         done
8742         [ "$rc" = 0 ] || error_exit "privileged port not found" )
8743 }
8744 run_test 100 "check local port using privileged port ==========="
8745
8746 function get_named_value()
8747 {
8748     local tag
8749
8750     tag=$1
8751     while read ;do
8752         line=$REPLY
8753         case $line in
8754         $tag*)
8755             echo $line | sed "s/^$tag[ ]*//"
8756             break
8757             ;;
8758         esac
8759     done
8760 }
8761
8762 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
8763                    awk '/^max_cached_mb/ { print $2 }')
8764
8765 cleanup_101a() {
8766         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
8767         trap 0
8768 }
8769
8770 test_101a() {
8771         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8772
8773         local s
8774         local discard
8775         local nreads=10000
8776         local cache_limit=32
8777
8778         $LCTL set_param -n osc.*-osc*.rpc_stats 0
8779         trap cleanup_101a EXIT
8780         $LCTL set_param -n llite.*.read_ahead_stats 0
8781         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
8782
8783         #
8784         # randomly read 10000 of 64K chunks from file 3x 32MB in size
8785         #
8786         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
8787         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
8788
8789         discard=0
8790         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
8791                 get_named_value 'read but discarded' | cut -d" " -f1); do
8792                         discard=$(($discard + $s))
8793         done
8794         cleanup_101a
8795
8796         $LCTL get_param osc.*-osc*.rpc_stats
8797         $LCTL get_param llite.*.read_ahead_stats
8798
8799         # Discard is generally zero, but sometimes a few random reads line up
8800         # and trigger larger readahead, which is wasted & leads to discards.
8801         if [[ $(($discard)) -gt $nreads ]]; then
8802                 error "too many ($discard) discarded pages"
8803         fi
8804         rm -f $DIR/$tfile || true
8805 }
8806 run_test 101a "check read-ahead for random reads"
8807
8808 setup_test101bc() {
8809         test_mkdir $DIR/$tdir
8810         local ssize=$1
8811         local FILE_LENGTH=$2
8812         STRIPE_OFFSET=0
8813
8814         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
8815
8816         local list=$(comma_list $(osts_nodes))
8817         set_osd_param $list '' read_cache_enable 0
8818         set_osd_param $list '' writethrough_cache_enable 0
8819
8820         trap cleanup_test101bc EXIT
8821         # prepare the read-ahead file
8822         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
8823
8824         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
8825                                 count=$FILE_SIZE_MB 2> /dev/null
8826
8827 }
8828
8829 cleanup_test101bc() {
8830         trap 0
8831         rm -rf $DIR/$tdir
8832         rm -f $DIR/$tfile
8833
8834         local list=$(comma_list $(osts_nodes))
8835         set_osd_param $list '' read_cache_enable 1
8836         set_osd_param $list '' writethrough_cache_enable 1
8837 }
8838
8839 calc_total() {
8840         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
8841 }
8842
8843 ra_check_101() {
8844         local READ_SIZE=$1
8845         local STRIPE_SIZE=$2
8846         local FILE_LENGTH=$3
8847         local RA_INC=1048576
8848         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
8849         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
8850                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
8851         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
8852                         get_named_value 'read but discarded' |
8853                         cut -d" " -f1 | calc_total)
8854         if [[ $DISCARD -gt $discard_limit ]]; then
8855                 $LCTL get_param llite.*.read_ahead_stats
8856                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
8857         else
8858                 echo "Read-ahead success for size ${READ_SIZE}"
8859         fi
8860 }
8861
8862 test_101b() {
8863         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8864         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8865
8866         local STRIPE_SIZE=1048576
8867         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
8868
8869         if [ $SLOW == "yes" ]; then
8870                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
8871         else
8872                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
8873         fi
8874
8875         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
8876
8877         # prepare the read-ahead file
8878         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8879         cancel_lru_locks osc
8880         for BIDX in 2 4 8 16 32 64 128 256
8881         do
8882                 local BSIZE=$((BIDX*4096))
8883                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
8884                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
8885                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
8886                 $LCTL set_param -n llite.*.read_ahead_stats 0
8887                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
8888                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
8889                 cancel_lru_locks osc
8890                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
8891         done
8892         cleanup_test101bc
8893         true
8894 }
8895 run_test 101b "check stride-io mode read-ahead ================="
8896
8897 test_101c() {
8898         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8899
8900         local STRIPE_SIZE=1048576
8901         local FILE_LENGTH=$((STRIPE_SIZE*100))
8902         local nreads=10000
8903         local rsize=65536
8904         local osc_rpc_stats
8905
8906         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8907
8908         cancel_lru_locks osc
8909         $LCTL set_param osc.*.rpc_stats 0
8910         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
8911         $LCTL get_param osc.*.rpc_stats
8912         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
8913                 local stats=$($LCTL get_param -n $osc_rpc_stats)
8914                 local lines=$(echo "$stats" | awk 'END {print NR;}')
8915                 local size
8916
8917                 if [ $lines -le 20 ]; then
8918                         echo "continue debug"
8919                         continue
8920                 fi
8921                 for size in 1 2 4 8; do
8922                         local rpc=$(echo "$stats" |
8923                                     awk '($1 == "'$size':") {print $2; exit; }')
8924                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
8925                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
8926                 done
8927                 echo "$osc_rpc_stats check passed!"
8928         done
8929         cleanup_test101bc
8930         true
8931 }
8932 run_test 101c "check stripe_size aligned read-ahead ================="
8933
8934 set_read_ahead() {
8935         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
8936         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
8937 }
8938
8939 test_101d() {
8940         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8941
8942         local file=$DIR/$tfile
8943         local sz_MB=${FILESIZE_101d:-500}
8944         local ra_MB=${READAHEAD_MB:-40}
8945
8946         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
8947         [ $free_MB -lt $sz_MB ] &&
8948                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
8949
8950         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
8951         $LFS setstripe -c -1 $file || error "setstripe failed"
8952
8953         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
8954         echo Cancel LRU locks on lustre client to flush the client cache
8955         cancel_lru_locks osc
8956
8957         echo Disable read-ahead
8958         local old_READAHEAD=$(set_read_ahead 0)
8959
8960         echo Reading the test file $file with read-ahead disabled
8961         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8962
8963         echo Cancel LRU locks on lustre client to flush the client cache
8964         cancel_lru_locks osc
8965         echo Enable read-ahead with ${ra_MB}MB
8966         set_read_ahead $ra_MB
8967
8968         echo Reading the test file $file with read-ahead enabled
8969         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8970
8971         echo "read-ahead disabled time read $raOFF"
8972         echo "read-ahead enabled  time read $raON"
8973
8974         set_read_ahead $old_READAHEAD
8975         rm -f $file
8976         wait_delete_completed
8977
8978         [ $raOFF -le 1 ] || [ $raON -lt $raOFF ] ||
8979                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
8980 }
8981 run_test 101d "file read with and without read-ahead enabled"
8982
8983 test_101e() {
8984         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8985
8986         local file=$DIR/$tfile
8987         local size_KB=500  #KB
8988         local count=100
8989         local bsize=1024
8990
8991         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
8992         local need_KB=$((count * size_KB))
8993         [[ $free_KB -le $need_KB ]] &&
8994                 skip_env "Need free space $need_KB, have $free_KB"
8995
8996         echo "Creating $count ${size_KB}K test files"
8997         for ((i = 0; i < $count; i++)); do
8998                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
8999         done
9000
9001         echo "Cancel LRU locks on lustre client to flush the client cache"
9002         cancel_lru_locks $OSC
9003
9004         echo "Reset readahead stats"
9005         $LCTL set_param -n llite.*.read_ahead_stats 0
9006
9007         for ((i = 0; i < $count; i++)); do
9008                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
9009         done
9010
9011         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9012                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
9013
9014         for ((i = 0; i < $count; i++)); do
9015                 rm -rf $file.$i 2>/dev/null
9016         done
9017
9018         #10000 means 20% reads are missing in readahead
9019         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
9020 }
9021 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
9022
9023 test_101f() {
9024         which iozone || skip_env "no iozone installed"
9025
9026         local old_debug=$($LCTL get_param debug)
9027         old_debug=${old_debug#*=}
9028         $LCTL set_param debug="reada mmap"
9029
9030         # create a test file
9031         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
9032
9033         echo Cancel LRU locks on lustre client to flush the client cache
9034         cancel_lru_locks osc
9035
9036         echo Reset readahead stats
9037         $LCTL set_param -n llite.*.read_ahead_stats 0
9038
9039         echo mmap read the file with small block size
9040         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
9041                 > /dev/null 2>&1
9042
9043         echo checking missing pages
9044         $LCTL get_param llite.*.read_ahead_stats
9045         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9046                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9047
9048         $LCTL set_param debug="$old_debug"
9049         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
9050         rm -f $DIR/$tfile
9051 }
9052 run_test 101f "check mmap read performance"
9053
9054 test_101g_brw_size_test() {
9055         local mb=$1
9056         local pages=$((mb * 1048576 / PAGE_SIZE))
9057         local file=$DIR/$tfile
9058
9059         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
9060                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
9061         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
9062                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
9063                         return 2
9064         done
9065
9066         stack_trap "rm -f $file" EXIT
9067         $LCTL set_param -n osc.*.rpc_stats=0
9068
9069         # 10 RPCs should be enough for the test
9070         local count=10
9071         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
9072                 { error "dd write ${mb} MB blocks failed"; return 3; }
9073         cancel_lru_locks osc
9074         dd of=/dev/null if=$file bs=${mb}M count=$count ||
9075                 { error "dd write ${mb} MB blocks failed"; return 4; }
9076
9077         # calculate number of full-sized read and write RPCs
9078         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
9079                 sed -n '/pages per rpc/,/^$/p' |
9080                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
9081                 END { print reads,writes }'))
9082         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
9083                 return 5
9084         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
9085                 return 6
9086
9087         return 0
9088 }
9089
9090 test_101g() {
9091         remote_ost_nodsh && skip "remote OST with nodsh"
9092
9093         local rpcs
9094         local osts=$(get_facets OST)
9095         local list=$(comma_list $(osts_nodes))
9096         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
9097         local brw_size="obdfilter.*.brw_size"
9098
9099         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9100
9101         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
9102
9103         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
9104                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
9105                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
9106            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
9107                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
9108                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
9109
9110                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
9111                         suffix="M"
9112
9113                 if [[ $orig_mb -lt 16 ]]; then
9114                         save_lustre_params $osts "$brw_size" > $p
9115                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
9116                                 error "set 16MB RPC size failed"
9117
9118                         echo "remount client to enable new RPC size"
9119                         remount_client $MOUNT || error "remount_client failed"
9120                 fi
9121
9122                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
9123                 # should be able to set brw_size=12, but no rpc_stats for that
9124                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
9125         fi
9126
9127         test_101g_brw_size_test 4 || error "4MB RPC test failed"
9128
9129         if [[ $orig_mb -lt 16 ]]; then
9130                 restore_lustre_params < $p
9131                 remount_client $MOUNT || error "remount_client restore failed"
9132         fi
9133
9134         rm -f $p $DIR/$tfile
9135 }
9136 run_test 101g "Big bulk(4/16 MiB) readahead"
9137
9138 test_101h() {
9139         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9140
9141         dd if=/dev/zero of=$DIR/$tfile bs=1M count=70 ||
9142                 error "dd 70M file failed"
9143         echo Cancel LRU locks on lustre client to flush the client cache
9144         cancel_lru_locks osc
9145
9146         echo "Reset readahead stats"
9147         $LCTL set_param -n llite.*.read_ahead_stats 0
9148
9149         echo "Read 10M of data but cross 64M bundary"
9150         dd if=$DIR/$tfile of=/dev/null bs=10M skip=6 count=1
9151         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9152                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9153         [ $miss -eq 1 ] || error "expected miss 1 but got $miss"
9154         rm -f $p $DIR/$tfile
9155 }
9156 run_test 101h "Readahead should cover current read window"
9157
9158 setup_test102() {
9159         test_mkdir $DIR/$tdir
9160         chown $RUNAS_ID $DIR/$tdir
9161         STRIPE_SIZE=65536
9162         STRIPE_OFFSET=1
9163         STRIPE_COUNT=$OSTCOUNT
9164         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
9165
9166         trap cleanup_test102 EXIT
9167         cd $DIR
9168         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
9169         cd $DIR/$tdir
9170         for num in 1 2 3 4; do
9171                 for count in $(seq 1 $STRIPE_COUNT); do
9172                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
9173                                 local size=`expr $STRIPE_SIZE \* $num`
9174                                 local file=file"$num-$idx-$count"
9175                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
9176                         done
9177                 done
9178         done
9179
9180         cd $DIR
9181         $1 tar cf $TMP/f102.tar $tdir --xattrs
9182 }
9183
9184 cleanup_test102() {
9185         trap 0
9186         rm -f $TMP/f102.tar
9187         rm -rf $DIR/d0.sanity/d102
9188 }
9189
9190 test_102a() {
9191         [ "$UID" != 0 ] && skip "must run as root"
9192         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
9193                 skip_env "must have user_xattr"
9194
9195         [ -z "$(which setfattr 2>/dev/null)" ] &&
9196                 skip_env "could not find setfattr"
9197
9198         local testfile=$DIR/$tfile
9199
9200         touch $testfile
9201         echo "set/get xattr..."
9202         setfattr -n trusted.name1 -v value1 $testfile ||
9203                 error "setfattr -n trusted.name1=value1 $testfile failed"
9204         getfattr -n trusted.name1 $testfile 2> /dev/null |
9205           grep "trusted.name1=.value1" ||
9206                 error "$testfile missing trusted.name1=value1"
9207
9208         setfattr -n user.author1 -v author1 $testfile ||
9209                 error "setfattr -n user.author1=author1 $testfile failed"
9210         getfattr -n user.author1 $testfile 2> /dev/null |
9211           grep "user.author1=.author1" ||
9212                 error "$testfile missing trusted.author1=author1"
9213
9214         echo "listxattr..."
9215         setfattr -n trusted.name2 -v value2 $testfile ||
9216                 error "$testfile unable to set trusted.name2"
9217         setfattr -n trusted.name3 -v value3 $testfile ||
9218                 error "$testfile unable to set trusted.name3"
9219         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
9220             grep "trusted.name" | wc -l) -eq 3 ] ||
9221                 error "$testfile missing 3 trusted.name xattrs"
9222
9223         setfattr -n user.author2 -v author2 $testfile ||
9224                 error "$testfile unable to set user.author2"
9225         setfattr -n user.author3 -v author3 $testfile ||
9226                 error "$testfile unable to set user.author3"
9227         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
9228             grep "user.author" | wc -l) -eq 3 ] ||
9229                 error "$testfile missing 3 user.author xattrs"
9230
9231         echo "remove xattr..."
9232         setfattr -x trusted.name1 $testfile ||
9233                 error "$testfile error deleting trusted.name1"
9234         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
9235                 error "$testfile did not delete trusted.name1 xattr"
9236
9237         setfattr -x user.author1 $testfile ||
9238                 error "$testfile error deleting user.author1"
9239         echo "set lustre special xattr ..."
9240         $LFS setstripe -c1 $testfile
9241         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
9242                 awk -F "=" '/trusted.lov/ { print $2 }' )
9243         setfattr -n "trusted.lov" -v $lovea $testfile ||
9244                 error "$testfile doesn't ignore setting trusted.lov again"
9245         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
9246                 error "$testfile allow setting invalid trusted.lov"
9247         rm -f $testfile
9248 }
9249 run_test 102a "user xattr test =================================="
9250
9251 test_102b() {
9252         [ -z "$(which setfattr 2>/dev/null)" ] &&
9253                 skip_env "could not find setfattr"
9254         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9255
9256         # b10930: get/set/list trusted.lov xattr
9257         echo "get/set/list trusted.lov xattr ..."
9258         local testfile=$DIR/$tfile
9259         $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
9260                 error "setstripe failed"
9261         local STRIPECOUNT=$($LFS getstripe -c $testfile) ||
9262                 error "getstripe failed"
9263         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
9264                 error "can't get trusted.lov from $testfile"
9265
9266         local testfile2=${testfile}2
9267         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
9268                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
9269
9270         $MCREATE $testfile2
9271         setfattr -n trusted.lov -v $value $testfile2
9272         local stripe_size=$($LFS getstripe -S $testfile2)
9273         local stripe_count=$($LFS getstripe -c $testfile2)
9274         [[ $stripe_size -eq 65536 ]] ||
9275                 error "stripe size $stripe_size != 65536"
9276         [[ $stripe_count -eq $STRIPECOUNT ]] ||
9277                 error "stripe count $stripe_count != $STRIPECOUNT"
9278         rm -f $DIR/$tfile
9279 }
9280 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
9281
9282 test_102c() {
9283         [ -z "$(which setfattr 2>/dev/null)" ] &&
9284                 skip_env "could not find setfattr"
9285         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9286
9287         # b10930: get/set/list lustre.lov xattr
9288         echo "get/set/list lustre.lov xattr ..."
9289         test_mkdir $DIR/$tdir
9290         chown $RUNAS_ID $DIR/$tdir
9291         local testfile=$DIR/$tdir/$tfile
9292         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
9293                 error "setstripe failed"
9294         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
9295                 error "getstripe failed"
9296         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
9297         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
9298
9299         local testfile2=${testfile}2
9300         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
9301                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
9302
9303         $RUNAS $MCREATE $testfile2
9304         $RUNAS setfattr -n lustre.lov -v $value $testfile2
9305         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
9306         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
9307         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
9308         [ $stripe_count -eq $STRIPECOUNT ] ||
9309                 error "stripe count $stripe_count != $STRIPECOUNT"
9310 }
9311 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
9312
9313 compare_stripe_info1() {
9314         local stripe_index_all_zero=true
9315
9316         for num in 1 2 3 4; do
9317                 for count in $(seq 1 $STRIPE_COUNT); do
9318                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
9319                                 local size=$((STRIPE_SIZE * num))
9320                                 local file=file"$num-$offset-$count"
9321                                 stripe_size=$($LFS getstripe -S $PWD/$file)
9322                                 [[ $stripe_size -ne $size ]] &&
9323                                     error "$file: size $stripe_size != $size"
9324                                 stripe_count=$($LFS getstripe -c $PWD/$file)
9325                                 # allow fewer stripes to be created, ORI-601
9326                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
9327                                     error "$file: count $stripe_count != $count"
9328                                 stripe_index=$($LFS getstripe -i $PWD/$file)
9329                                 [[ $stripe_index -ne 0 ]] &&
9330                                         stripe_index_all_zero=false
9331                         done
9332                 done
9333         done
9334         $stripe_index_all_zero &&
9335                 error "all files are being extracted starting from OST index 0"
9336         return 0
9337 }
9338
9339 have_xattrs_include() {
9340         tar --help | grep -q xattrs-include &&
9341                 echo --xattrs-include="lustre.*"
9342 }
9343
9344 test_102d() {
9345         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9346         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9347
9348         XINC=$(have_xattrs_include)
9349         setup_test102
9350         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9351         cd $DIR/$tdir/$tdir
9352         compare_stripe_info1
9353 }
9354 run_test 102d "tar restore stripe info from tarfile,not keep osts"
9355
9356 test_102f() {
9357         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9358         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9359
9360         XINC=$(have_xattrs_include)
9361         setup_test102
9362         test_mkdir $DIR/$tdir.restore
9363         cd $DIR
9364         tar cf - --xattrs $tdir | tar xf - \
9365                 -C $DIR/$tdir.restore --xattrs $XINC
9366         cd $DIR/$tdir.restore/$tdir
9367         compare_stripe_info1
9368 }
9369 run_test 102f "tar copy files, not keep osts"
9370
9371 grow_xattr() {
9372         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
9373                 skip "must have user_xattr"
9374         [ -z "$(which setfattr 2>/dev/null)" ] &&
9375                 skip_env "could not find setfattr"
9376         [ -z "$(which getfattr 2>/dev/null)" ] &&
9377                 skip_env "could not find getfattr"
9378
9379         local xsize=${1:-1024}  # in bytes
9380         local file=$DIR/$tfile
9381         local value="$(generate_string $xsize)"
9382         local xbig=trusted.big
9383         local toobig=$2
9384
9385         touch $file
9386         log "save $xbig on $file"
9387         if [ -z "$toobig" ]
9388         then
9389                 setfattr -n $xbig -v $value $file ||
9390                         error "saving $xbig on $file failed"
9391         else
9392                 setfattr -n $xbig -v $value $file &&
9393                         error "saving $xbig on $file succeeded"
9394                 return 0
9395         fi
9396
9397         local orig=$(get_xattr_value $xbig $file)
9398         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
9399
9400         local xsml=trusted.sml
9401         log "save $xsml on $file"
9402         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
9403
9404         local new=$(get_xattr_value $xbig $file)
9405         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
9406
9407         log "grow $xsml on $file"
9408         setfattr -n $xsml -v "$value" $file ||
9409                 error "growing $xsml on $file failed"
9410
9411         new=$(get_xattr_value $xbig $file)
9412         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
9413         log "$xbig still valid after growing $xsml"
9414
9415         rm -f $file
9416 }
9417
9418 test_102h() { # bug 15777
9419         grow_xattr 1024
9420 }
9421 run_test 102h "grow xattr from inside inode to external block"
9422
9423 test_102ha() {
9424         large_xattr_enabled || skip_env "ea_inode feature disabled"
9425
9426         echo "setting xattr of max xattr size: $(max_xattr_size)"
9427         grow_xattr $(max_xattr_size)
9428
9429         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
9430         echo "This should fail:"
9431         grow_xattr $(($(max_xattr_size) + 10)) 1
9432 }
9433 run_test 102ha "grow xattr from inside inode to external inode"
9434
9435 test_102i() { # bug 17038
9436         [ -z "$(which getfattr 2>/dev/null)" ] &&
9437                 skip "could not find getfattr"
9438
9439         touch $DIR/$tfile
9440         ln -s $DIR/$tfile $DIR/${tfile}link
9441         getfattr -n trusted.lov $DIR/$tfile ||
9442                 error "lgetxattr on $DIR/$tfile failed"
9443         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
9444                 grep -i "no such attr" ||
9445                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
9446         rm -f $DIR/$tfile $DIR/${tfile}link
9447 }
9448 run_test 102i "lgetxattr test on symbolic link ============"
9449
9450 test_102j() {
9451         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9452         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9453
9454         XINC=$(have_xattrs_include)
9455         setup_test102 "$RUNAS"
9456         chown $RUNAS_ID $DIR/$tdir
9457         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9458         cd $DIR/$tdir/$tdir
9459         compare_stripe_info1 "$RUNAS"
9460 }
9461 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
9462
9463 test_102k() {
9464         [ -z "$(which setfattr 2>/dev/null)" ] &&
9465                 skip "could not find setfattr"
9466
9467         touch $DIR/$tfile
9468         # b22187 just check that does not crash for regular file.
9469         setfattr -n trusted.lov $DIR/$tfile
9470         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
9471         local test_kdir=$DIR/$tdir
9472         test_mkdir $test_kdir
9473         local default_size=$($LFS getstripe -S $test_kdir)
9474         local default_count=$($LFS getstripe -c $test_kdir)
9475         local default_offset=$($LFS getstripe -i $test_kdir)
9476         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
9477                 error 'dir setstripe failed'
9478         setfattr -n trusted.lov $test_kdir
9479         local stripe_size=$($LFS getstripe -S $test_kdir)
9480         local stripe_count=$($LFS getstripe -c $test_kdir)
9481         local stripe_offset=$($LFS getstripe -i $test_kdir)
9482         [ $stripe_size -eq $default_size ] ||
9483                 error "stripe size $stripe_size != $default_size"
9484         [ $stripe_count -eq $default_count ] ||
9485                 error "stripe count $stripe_count != $default_count"
9486         [ $stripe_offset -eq $default_offset ] ||
9487                 error "stripe offset $stripe_offset != $default_offset"
9488         rm -rf $DIR/$tfile $test_kdir
9489 }
9490 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
9491
9492 test_102l() {
9493         [ -z "$(which getfattr 2>/dev/null)" ] &&
9494                 skip "could not find getfattr"
9495
9496         # LU-532 trusted. xattr is invisible to non-root
9497         local testfile=$DIR/$tfile
9498
9499         touch $testfile
9500
9501         echo "listxattr as user..."
9502         chown $RUNAS_ID $testfile
9503         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
9504             grep -q "trusted" &&
9505                 error "$testfile trusted xattrs are user visible"
9506
9507         return 0;
9508 }
9509 run_test 102l "listxattr size test =================================="
9510
9511 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
9512         local path=$DIR/$tfile
9513         touch $path
9514
9515         listxattr_size_check $path || error "listattr_size_check $path failed"
9516 }
9517 run_test 102m "Ensure listxattr fails on small bufffer ========"
9518
9519 cleanup_test102
9520
9521 getxattr() { # getxattr path name
9522         # Return the base64 encoding of the value of xattr name on path.
9523         local path=$1
9524         local name=$2
9525
9526         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
9527         # file: $path
9528         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9529         #
9530         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9531
9532         getfattr --absolute-names --encoding=base64 --name=$name $path |
9533                 awk -F= -v name=$name '$1 == name {
9534                         print substr($0, index($0, "=") + 1);
9535         }'
9536 }
9537
9538 test_102n() { # LU-4101 mdt: protect internal xattrs
9539         [ -z "$(which setfattr 2>/dev/null)" ] &&
9540                 skip "could not find setfattr"
9541         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
9542         then
9543                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
9544         fi
9545
9546         local file0=$DIR/$tfile.0
9547         local file1=$DIR/$tfile.1
9548         local xattr0=$TMP/$tfile.0
9549         local xattr1=$TMP/$tfile.1
9550         local namelist="lov lma lmv link fid version som hsm"
9551         local name
9552         local value
9553
9554         rm -rf $file0 $file1 $xattr0 $xattr1
9555         touch $file0 $file1
9556
9557         # Get 'before' xattrs of $file1.
9558         getfattr --absolute-names --dump --match=- $file1 > $xattr0
9559
9560         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
9561                 namelist+=" lfsck_namespace"
9562         for name in $namelist; do
9563                 # Try to copy xattr from $file0 to $file1.
9564                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9565
9566                 setfattr --name=trusted.$name --value="$value" $file1 ||
9567                         error "setxattr 'trusted.$name' failed"
9568
9569                 # Try to set a garbage xattr.
9570                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9571
9572                 if [[ x$name == "xlov" ]]; then
9573                         setfattr --name=trusted.lov --value="$value" $file1 &&
9574                         error "setxattr invalid 'trusted.lov' success"
9575                 else
9576                         setfattr --name=trusted.$name --value="$value" $file1 ||
9577                                 error "setxattr invalid 'trusted.$name' failed"
9578                 fi
9579
9580                 # Try to remove the xattr from $file1. We don't care if this
9581                 # appears to succeed or fail, we just don't want there to be
9582                 # any changes or crashes.
9583                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9584         done
9585
9586         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
9587         then
9588                 name="lfsck_ns"
9589                 # Try to copy xattr from $file0 to $file1.
9590                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9591
9592                 setfattr --name=trusted.$name --value="$value" $file1 ||
9593                         error "setxattr 'trusted.$name' failed"
9594
9595                 # Try to set a garbage xattr.
9596                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9597
9598                 setfattr --name=trusted.$name --value="$value" $file1 ||
9599                         error "setxattr 'trusted.$name' failed"
9600
9601                 # Try to remove the xattr from $file1. We don't care if this
9602                 # appears to succeed or fail, we just don't want there to be
9603                 # any changes or crashes.
9604                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9605         fi
9606
9607         # Get 'after' xattrs of file1.
9608         getfattr --absolute-names --dump --match=- $file1 > $xattr1
9609
9610         if ! diff $xattr0 $xattr1; then
9611                 error "before and after xattrs of '$file1' differ"
9612         fi
9613
9614         rm -rf $file0 $file1 $xattr0 $xattr1
9615
9616         return 0
9617 }
9618 run_test 102n "silently ignore setxattr on internal trusted xattrs"
9619
9620 test_102p() { # LU-4703 setxattr did not check ownership
9621         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
9622                 skip "MDS needs to be at least 2.5.56"
9623
9624         local testfile=$DIR/$tfile
9625
9626         touch $testfile
9627
9628         echo "setfacl as user..."
9629         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
9630         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
9631
9632         echo "setfattr as user..."
9633         setfacl -m "u:$RUNAS_ID:---" $testfile
9634         $RUNAS setfattr -x system.posix_acl_access $testfile
9635         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
9636 }
9637 run_test 102p "check setxattr(2) correctly fails without permission"
9638
9639 test_102q() {
9640         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
9641                 skip "MDS needs to be at least 2.6.92"
9642
9643         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
9644 }
9645 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
9646
9647 test_102r() {
9648         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
9649                 skip "MDS needs to be at least 2.6.93"
9650
9651         touch $DIR/$tfile || error "touch"
9652         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
9653         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
9654         rm $DIR/$tfile || error "rm"
9655
9656         #normal directory
9657         mkdir -p $DIR/$tdir || error "mkdir"
9658         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9659         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9660         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9661                 error "$testfile error deleting user.author1"
9662         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9663                 grep "user.$(basename $tdir)" &&
9664                 error "$tdir did not delete user.$(basename $tdir)"
9665         rmdir $DIR/$tdir || error "rmdir"
9666
9667         #striped directory
9668         test_mkdir $DIR/$tdir
9669         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9670         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9671         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9672                 error "$testfile error deleting user.author1"
9673         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9674                 grep "user.$(basename $tdir)" &&
9675                 error "$tdir did not delete user.$(basename $tdir)"
9676         rmdir $DIR/$tdir || error "rm striped dir"
9677 }
9678 run_test 102r "set EAs with empty values"
9679
9680 test_102s() {
9681         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9682                 skip "MDS needs to be at least 2.11.52"
9683
9684         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9685
9686         save_lustre_params client "llite.*.xattr_cache" > $save
9687
9688         for cache in 0 1; do
9689                 lctl set_param llite.*.xattr_cache=$cache
9690
9691                 rm -f $DIR/$tfile
9692                 touch $DIR/$tfile || error "touch"
9693                 for prefix in lustre security system trusted user; do
9694                         # Note getxattr() may fail with 'Operation not
9695                         # supported' or 'No such attribute' depending
9696                         # on prefix and cache.
9697                         getfattr -n $prefix.n102s $DIR/$tfile &&
9698                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
9699                 done
9700         done
9701
9702         restore_lustre_params < $save
9703 }
9704 run_test 102s "getting nonexistent xattrs should fail"
9705
9706 test_102t() {
9707         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9708                 skip "MDS needs to be at least 2.11.52"
9709
9710         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9711
9712         save_lustre_params client "llite.*.xattr_cache" > $save
9713
9714         for cache in 0 1; do
9715                 lctl set_param llite.*.xattr_cache=$cache
9716
9717                 for buf_size in 0 256; do
9718                         rm -f $DIR/$tfile
9719                         touch $DIR/$tfile || error "touch"
9720                         setfattr -n user.multiop $DIR/$tfile
9721                         $MULTIOP $DIR/$tfile oa$buf_size ||
9722                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
9723                 done
9724         done
9725
9726         restore_lustre_params < $save
9727 }
9728 run_test 102t "zero length xattr values handled correctly"
9729
9730 run_acl_subtest()
9731 {
9732     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
9733     return $?
9734 }
9735
9736 test_103a() {
9737         [ "$UID" != 0 ] && skip "must run as root"
9738         $GSS && skip_env "could not run under gss"
9739         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
9740                 skip_env "must have acl enabled"
9741         [ -z "$(which setfacl 2>/dev/null)" ] &&
9742                 skip_env "could not find setfacl"
9743         remote_mds_nodsh && skip "remote MDS with nodsh"
9744
9745         gpasswd -a daemon bin                           # LU-5641
9746         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
9747
9748         declare -a identity_old
9749
9750         for num in $(seq $MDSCOUNT); do
9751                 switch_identity $num true || identity_old[$num]=$?
9752         done
9753
9754         SAVE_UMASK=$(umask)
9755         umask 0022
9756         mkdir -p $DIR/$tdir
9757         cd $DIR/$tdir
9758
9759         echo "performing cp ..."
9760         run_acl_subtest cp || error "run_acl_subtest cp failed"
9761         echo "performing getfacl-noacl..."
9762         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
9763         echo "performing misc..."
9764         run_acl_subtest misc || error  "misc test failed"
9765         echo "performing permissions..."
9766         run_acl_subtest permissions || error "permissions failed"
9767         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
9768         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
9769                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
9770                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
9771         then
9772                 echo "performing permissions xattr..."
9773                 run_acl_subtest permissions_xattr ||
9774                         error "permissions_xattr failed"
9775         fi
9776         echo "performing setfacl..."
9777         run_acl_subtest setfacl || error  "setfacl test failed"
9778
9779         # inheritance test got from HP
9780         echo "performing inheritance..."
9781         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
9782         chmod +x make-tree || error "chmod +x failed"
9783         run_acl_subtest inheritance || error "inheritance test failed"
9784         rm -f make-tree
9785
9786         echo "LU-974 ignore umask when acl is enabled..."
9787         run_acl_subtest 974 || error "LU-974 umask test failed"
9788         if [ $MDSCOUNT -ge 2 ]; then
9789                 run_acl_subtest 974_remote ||
9790                         error "LU-974 umask test failed under remote dir"
9791         fi
9792
9793         echo "LU-2561 newly created file is same size as directory..."
9794         if [ "$mds1_FSTYPE" != "zfs" ]; then
9795                 run_acl_subtest 2561 || error "LU-2561 test failed"
9796         else
9797                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
9798         fi
9799
9800         run_acl_subtest 4924 || error "LU-4924 test failed"
9801
9802         cd $SAVE_PWD
9803         umask $SAVE_UMASK
9804
9805         for num in $(seq $MDSCOUNT); do
9806                 if [ "${identity_old[$num]}" = 1 ]; then
9807                         switch_identity $num false || identity_old[$num]=$?
9808                 fi
9809         done
9810 }
9811 run_test 103a "acl test"
9812
9813 test_103b() {
9814         declare -a pids
9815         local U
9816
9817         for U in {0..511}; do
9818                 {
9819                 local O=$(printf "%04o" $U)
9820
9821                 umask $(printf "%04o" $((511 ^ $O)))
9822                 $LFS setstripe -c 1 $DIR/$tfile.s$O
9823                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
9824
9825                 (( $S == ($O & 0666) )) ||
9826                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
9827
9828                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
9829                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
9830                 (( $S == ($O & 0666) )) ||
9831                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
9832
9833                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
9834                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
9835                 (( $S == ($O & 0666) )) ||
9836                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
9837                 rm -f $DIR/$tfile.[smp]$0
9838                 } &
9839                 local pid=$!
9840
9841                 # limit the concurrently running threads to 64. LU-11878
9842                 local idx=$((U % 64))
9843                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
9844                 pids[idx]=$pid
9845         done
9846         wait
9847 }
9848 run_test 103b "umask lfs setstripe"
9849
9850 test_103c() {
9851         mkdir -p $DIR/$tdir
9852         cp -rp $DIR/$tdir $DIR/$tdir.bak
9853
9854         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
9855                 error "$DIR/$tdir shouldn't contain default ACL"
9856         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
9857                 error "$DIR/$tdir.bak shouldn't contain default ACL"
9858         true
9859 }
9860 run_test 103c "'cp -rp' won't set empty acl"
9861
9862 test_104a() {
9863         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9864
9865         touch $DIR/$tfile
9866         lfs df || error "lfs df failed"
9867         lfs df -ih || error "lfs df -ih failed"
9868         lfs df -h $DIR || error "lfs df -h $DIR failed"
9869         lfs df -i $DIR || error "lfs df -i $DIR failed"
9870         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
9871         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
9872
9873         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
9874         lctl --device %$OSC deactivate
9875         lfs df || error "lfs df with deactivated OSC failed"
9876         lctl --device %$OSC activate
9877         # wait the osc back to normal
9878         wait_osc_import_ready client ost
9879
9880         lfs df || error "lfs df with reactivated OSC failed"
9881         rm -f $DIR/$tfile
9882 }
9883 run_test 104a "lfs df [-ih] [path] test ========================="
9884
9885 test_104b() {
9886         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9887         [ $RUNAS_ID -eq $UID ] &&
9888                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9889
9890         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
9891                         grep "Permission denied" | wc -l)))
9892         if [ $denied_cnt -ne 0 ]; then
9893                 error "lfs check servers test failed"
9894         fi
9895 }
9896 run_test 104b "$RUNAS lfs check servers test ===================="
9897
9898 test_105a() {
9899         # doesn't work on 2.4 kernels
9900         touch $DIR/$tfile
9901         if $(flock_is_enabled); then
9902                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
9903         else
9904                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
9905         fi
9906         rm -f $DIR/$tfile
9907 }
9908 run_test 105a "flock when mounted without -o flock test ========"
9909
9910 test_105b() {
9911         touch $DIR/$tfile
9912         if $(flock_is_enabled); then
9913                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
9914         else
9915                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
9916         fi
9917         rm -f $DIR/$tfile
9918 }
9919 run_test 105b "fcntl when mounted without -o flock test ========"
9920
9921 test_105c() {
9922         touch $DIR/$tfile
9923         if $(flock_is_enabled); then
9924                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
9925         else
9926                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
9927         fi
9928         rm -f $DIR/$tfile
9929 }
9930 run_test 105c "lockf when mounted without -o flock test"
9931
9932 test_105d() { # bug 15924
9933         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9934
9935         test_mkdir $DIR/$tdir
9936         flock_is_enabled || skip_env "mount w/o flock enabled"
9937         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
9938         $LCTL set_param fail_loc=0x80000315
9939         flocks_test 2 $DIR/$tdir
9940 }
9941 run_test 105d "flock race (should not freeze) ========"
9942
9943 test_105e() { # bug 22660 && 22040
9944         flock_is_enabled || skip_env "mount w/o flock enabled"
9945
9946         touch $DIR/$tfile
9947         flocks_test 3 $DIR/$tfile
9948 }
9949 run_test 105e "Two conflicting flocks from same process"
9950
9951 test_106() { #bug 10921
9952         test_mkdir $DIR/$tdir
9953         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
9954         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
9955 }
9956 run_test 106 "attempt exec of dir followed by chown of that dir"
9957
9958 test_107() {
9959         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9960
9961         CDIR=`pwd`
9962         local file=core
9963
9964         cd $DIR
9965         rm -f $file
9966
9967         local save_pattern=$(sysctl -n kernel.core_pattern)
9968         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
9969         sysctl -w kernel.core_pattern=$file
9970         sysctl -w kernel.core_uses_pid=0
9971
9972         ulimit -c unlimited
9973         sleep 60 &
9974         SLEEPPID=$!
9975
9976         sleep 1
9977
9978         kill -s 11 $SLEEPPID
9979         wait $SLEEPPID
9980         if [ -e $file ]; then
9981                 size=`stat -c%s $file`
9982                 [ $size -eq 0 ] && error "Fail to create core file $file"
9983         else
9984                 error "Fail to create core file $file"
9985         fi
9986         rm -f $file
9987         sysctl -w kernel.core_pattern=$save_pattern
9988         sysctl -w kernel.core_uses_pid=$save_uses_pid
9989         cd $CDIR
9990 }
9991 run_test 107 "Coredump on SIG"
9992
9993 test_110() {
9994         test_mkdir $DIR/$tdir
9995         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
9996         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
9997                 error "mkdir with 256 char should fail, but did not"
9998         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
9999                 error "create with 255 char failed"
10000         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
10001                 error "create with 256 char should fail, but did not"
10002
10003         ls -l $DIR/$tdir
10004         rm -rf $DIR/$tdir
10005 }
10006 run_test 110 "filename length checking"
10007
10008 #
10009 # Purpose: To verify dynamic thread (OSS) creation.
10010 #
10011 test_115() {
10012         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10013         remote_ost_nodsh && skip "remote OST with nodsh"
10014
10015         # Lustre does not stop service threads once they are started.
10016         # Reset number of running threads to default.
10017         stopall
10018         setupall
10019
10020         local OSTIO_pre
10021         local save_params="$TMP/sanity-$TESTNAME.parameters"
10022
10023         # Get ll_ost_io count before I/O
10024         OSTIO_pre=$(do_facet ost1 \
10025                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
10026         # Exit if lustre is not running (ll_ost_io not running).
10027         [ -z "$OSTIO_pre" ] && error "no OSS threads"
10028
10029         echo "Starting with $OSTIO_pre threads"
10030         local thread_max=$((OSTIO_pre * 2))
10031         local rpc_in_flight=$((thread_max * 2))
10032         # Number of I/O Process proposed to be started.
10033         local nfiles
10034         local facets=$(get_facets OST)
10035
10036         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
10037         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
10038
10039         # Set in_flight to $rpc_in_flight
10040         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
10041                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
10042         nfiles=${rpc_in_flight}
10043         # Set ost thread_max to $thread_max
10044         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
10045
10046         # 5 Minutes should be sufficient for max number of OSS
10047         # threads(thread_max) to be created.
10048         local timeout=300
10049
10050         # Start I/O.
10051         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
10052         test_mkdir $DIR/$tdir
10053         for i in $(seq $nfiles); do
10054                 local file=$DIR/$tdir/${tfile}-$i
10055                 $LFS setstripe -c -1 -i 0 $file
10056                 ($WTL $file $timeout)&
10057         done
10058
10059         # I/O Started - Wait for thread_started to reach thread_max or report
10060         # error if thread_started is more than thread_max.
10061         echo "Waiting for thread_started to reach thread_max"
10062         local thread_started=0
10063         local end_time=$((SECONDS + timeout))
10064
10065         while [ $SECONDS -le $end_time ] ; do
10066                 echo -n "."
10067                 # Get ost i/o thread_started count.
10068                 thread_started=$(do_facet ost1 \
10069                         "$LCTL get_param \
10070                         ost.OSS.ost_io.threads_started | cut -d= -f2")
10071                 # Break out if thread_started is equal/greater than thread_max
10072                 if [[ $thread_started -ge $thread_max ]]; then
10073                         echo ll_ost_io thread_started $thread_started, \
10074                                 equal/greater than thread_max $thread_max
10075                         break
10076                 fi
10077                 sleep 1
10078         done
10079
10080         # Cleanup - We have the numbers, Kill i/o jobs if running.
10081         jobcount=($(jobs -p))
10082         for i in $(seq 0 $((${#jobcount[@]}-1)))
10083         do
10084                 kill -9 ${jobcount[$i]}
10085                 if [ $? -ne 0 ] ; then
10086                         echo Warning: \
10087                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
10088                 fi
10089         done
10090
10091         # Cleanup files left by WTL binary.
10092         for i in $(seq $nfiles); do
10093                 local file=$DIR/$tdir/${tfile}-$i
10094                 rm -rf $file
10095                 if [ $? -ne 0 ] ; then
10096                         echo "Warning: Failed to delete file $file"
10097                 fi
10098         done
10099
10100         restore_lustre_params <$save_params
10101         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
10102
10103         # Error out if no new thread has started or Thread started is greater
10104         # than thread max.
10105         if [[ $thread_started -le $OSTIO_pre ||
10106                         $thread_started -gt $thread_max ]]; then
10107                 error "ll_ost_io: thread_started $thread_started" \
10108                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
10109                       "No new thread started or thread started greater " \
10110                       "than thread_max."
10111         fi
10112 }
10113 run_test 115 "verify dynamic thread creation===================="
10114
10115 free_min_max () {
10116         wait_delete_completed
10117         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
10118         echo "OST kbytes available: ${AVAIL[@]}"
10119         MAXV=${AVAIL[0]}
10120         MAXI=0
10121         MINV=${AVAIL[0]}
10122         MINI=0
10123         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
10124                 #echo OST $i: ${AVAIL[i]}kb
10125                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
10126                         MAXV=${AVAIL[i]}
10127                         MAXI=$i
10128                 fi
10129                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
10130                         MINV=${AVAIL[i]}
10131                         MINI=$i
10132                 fi
10133         done
10134         echo "Min free space: OST $MINI: $MINV"
10135         echo "Max free space: OST $MAXI: $MAXV"
10136 }
10137
10138 test_116a() { # was previously test_116()
10139         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10140         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10141         remote_mds_nodsh && skip "remote MDS with nodsh"
10142
10143         echo -n "Free space priority "
10144         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
10145                 head -n1
10146         declare -a AVAIL
10147         free_min_max
10148
10149         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
10150         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
10151         trap simple_cleanup_common EXIT
10152
10153         # Check if we need to generate uneven OSTs
10154         test_mkdir -p $DIR/$tdir/OST${MINI}
10155         local FILL=$((MINV / 4))
10156         local DIFF=$((MAXV - MINV))
10157         local DIFF2=$((DIFF * 100 / MINV))
10158
10159         local threshold=$(do_facet $SINGLEMDS \
10160                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
10161         threshold=${threshold%%%}
10162         echo -n "Check for uneven OSTs: "
10163         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
10164
10165         if [[ $DIFF2 -gt $threshold ]]; then
10166                 echo "ok"
10167                 echo "Don't need to fill OST$MINI"
10168         else
10169                 # generate uneven OSTs. Write 2% over the QOS threshold value
10170                 echo "no"
10171                 DIFF=$((threshold - DIFF2 + 2))
10172                 DIFF2=$((MINV * DIFF / 100))
10173                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
10174                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
10175                         error "setstripe failed"
10176                 DIFF=$((DIFF2 / 2048))
10177                 i=0
10178                 while [ $i -lt $DIFF ]; do
10179                         i=$((i + 1))
10180                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
10181                                 bs=2M count=1 2>/dev/null
10182                         echo -n .
10183                 done
10184                 echo .
10185                 sync
10186                 sleep_maxage
10187                 free_min_max
10188         fi
10189
10190         DIFF=$((MAXV - MINV))
10191         DIFF2=$((DIFF * 100 / MINV))
10192         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
10193         if [ $DIFF2 -gt $threshold ]; then
10194                 echo "ok"
10195         else
10196                 echo "failed - QOS mode won't be used"
10197                 simple_cleanup_common
10198                 skip "QOS imbalance criteria not met"
10199         fi
10200
10201         MINI1=$MINI
10202         MINV1=$MINV
10203         MAXI1=$MAXI
10204         MAXV1=$MAXV
10205
10206         # now fill using QOS
10207         $LFS setstripe -c 1 $DIR/$tdir
10208         FILL=$((FILL / 200))
10209         if [ $FILL -gt 600 ]; then
10210                 FILL=600
10211         fi
10212         echo "writing $FILL files to QOS-assigned OSTs"
10213         i=0
10214         while [ $i -lt $FILL ]; do
10215                 i=$((i + 1))
10216                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
10217                         count=1 2>/dev/null
10218                 echo -n .
10219         done
10220         echo "wrote $i 200k files"
10221         sync
10222         sleep_maxage
10223
10224         echo "Note: free space may not be updated, so measurements might be off"
10225         free_min_max
10226         DIFF2=$((MAXV - MINV))
10227         echo "free space delta: orig $DIFF final $DIFF2"
10228         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
10229         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
10230         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
10231         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
10232         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
10233         if [[ $DIFF -gt 0 ]]; then
10234                 FILL=$((DIFF2 * 100 / DIFF - 100))
10235                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
10236         fi
10237
10238         # Figure out which files were written where
10239         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10240                awk '/'$MINI1': / {print $2; exit}')
10241         echo $UUID
10242         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10243         echo "$MINC files created on smaller OST $MINI1"
10244         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10245                awk '/'$MAXI1': / {print $2; exit}')
10246         echo $UUID
10247         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10248         echo "$MAXC files created on larger OST $MAXI1"
10249         if [[ $MINC -gt 0 ]]; then
10250                 FILL=$((MAXC * 100 / MINC - 100))
10251                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
10252         fi
10253         [[ $MAXC -gt $MINC ]] ||
10254                 error_ignore LU-9 "stripe QOS didn't balance free space"
10255         simple_cleanup_common
10256 }
10257 run_test 116a "stripe QOS: free space balance ==================="
10258
10259 test_116b() { # LU-2093
10260         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10261         remote_mds_nodsh && skip "remote MDS with nodsh"
10262
10263 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
10264         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
10265                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
10266         [ -z "$old_rr" ] && skip "no QOS"
10267         do_facet $SINGLEMDS lctl set_param \
10268                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
10269         mkdir -p $DIR/$tdir
10270         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
10271         createmany -o $DIR/$tdir/f- 20 || error "can't create"
10272         do_facet $SINGLEMDS lctl set_param fail_loc=0
10273         rm -rf $DIR/$tdir
10274         do_facet $SINGLEMDS lctl set_param \
10275                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
10276 }
10277 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
10278
10279 test_117() # bug 10891
10280 {
10281         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10282
10283         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
10284         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
10285         lctl set_param fail_loc=0x21e
10286         > $DIR/$tfile || error "truncate failed"
10287         lctl set_param fail_loc=0
10288         echo "Truncate succeeded."
10289         rm -f $DIR/$tfile
10290 }
10291 run_test 117 "verify osd extend =========="
10292
10293 NO_SLOW_RESENDCOUNT=4
10294 export OLD_RESENDCOUNT=""
10295 set_resend_count () {
10296         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
10297         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
10298         lctl set_param -n $PROC_RESENDCOUNT $1
10299         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
10300 }
10301
10302 # for reduce test_118* time (b=14842)
10303 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10304
10305 # Reset async IO behavior after error case
10306 reset_async() {
10307         FILE=$DIR/reset_async
10308
10309         # Ensure all OSCs are cleared
10310         $LFS setstripe -c -1 $FILE
10311         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
10312         sync
10313         rm $FILE
10314 }
10315
10316 test_118a() #bug 11710
10317 {
10318         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10319
10320         reset_async
10321
10322         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10323         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10324         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10325
10326         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10327                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10328                 return 1;
10329         fi
10330         rm -f $DIR/$tfile
10331 }
10332 run_test 118a "verify O_SYNC works =========="
10333
10334 test_118b()
10335 {
10336         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10337         remote_ost_nodsh && skip "remote OST with nodsh"
10338
10339         reset_async
10340
10341         #define OBD_FAIL_SRV_ENOENT 0x217
10342         set_nodes_failloc "$(osts_nodes)" 0x217
10343         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10344         RC=$?
10345         set_nodes_failloc "$(osts_nodes)" 0
10346         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10347         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10348                     grep -c writeback)
10349
10350         if [[ $RC -eq 0 ]]; then
10351                 error "Must return error due to dropped pages, rc=$RC"
10352                 return 1;
10353         fi
10354
10355         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10356                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10357                 return 1;
10358         fi
10359
10360         echo "Dirty pages not leaked on ENOENT"
10361
10362         # Due to the above error the OSC will issue all RPCs syncronously
10363         # until a subsequent RPC completes successfully without error.
10364         $MULTIOP $DIR/$tfile Ow4096yc
10365         rm -f $DIR/$tfile
10366
10367         return 0
10368 }
10369 run_test 118b "Reclaim dirty pages on fatal error =========="
10370
10371 test_118c()
10372 {
10373         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10374
10375         # for 118c, restore the original resend count, LU-1940
10376         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
10377                                 set_resend_count $OLD_RESENDCOUNT
10378         remote_ost_nodsh && skip "remote OST with nodsh"
10379
10380         reset_async
10381
10382         #define OBD_FAIL_OST_EROFS               0x216
10383         set_nodes_failloc "$(osts_nodes)" 0x216
10384
10385         # multiop should block due to fsync until pages are written
10386         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10387         MULTIPID=$!
10388         sleep 1
10389
10390         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10391                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10392         fi
10393
10394         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10395                     grep -c writeback)
10396         if [[ $WRITEBACK -eq 0 ]]; then
10397                 error "No page in writeback, writeback=$WRITEBACK"
10398         fi
10399
10400         set_nodes_failloc "$(osts_nodes)" 0
10401         wait $MULTIPID
10402         RC=$?
10403         if [[ $RC -ne 0 ]]; then
10404                 error "Multiop fsync failed, rc=$RC"
10405         fi
10406
10407         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10408         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10409                     grep -c writeback)
10410         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10411                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10412         fi
10413
10414         rm -f $DIR/$tfile
10415         echo "Dirty pages flushed via fsync on EROFS"
10416         return 0
10417 }
10418 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
10419
10420 # continue to use small resend count to reduce test_118* time (b=14842)
10421 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10422
10423 test_118d()
10424 {
10425         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10426         remote_ost_nodsh && skip "remote OST with nodsh"
10427
10428         reset_async
10429
10430         #define OBD_FAIL_OST_BRW_PAUSE_BULK
10431         set_nodes_failloc "$(osts_nodes)" 0x214
10432         # multiop should block due to fsync until pages are written
10433         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10434         MULTIPID=$!
10435         sleep 1
10436
10437         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10438                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10439         fi
10440
10441         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10442                     grep -c writeback)
10443         if [[ $WRITEBACK -eq 0 ]]; then
10444                 error "No page in writeback, writeback=$WRITEBACK"
10445         fi
10446
10447         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
10448         set_nodes_failloc "$(osts_nodes)" 0
10449
10450         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10451         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10452                     grep -c writeback)
10453         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10454                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10455         fi
10456
10457         rm -f $DIR/$tfile
10458         echo "Dirty pages gaurenteed flushed via fsync"
10459         return 0
10460 }
10461 run_test 118d "Fsync validation inject a delay of the bulk =========="
10462
10463 test_118f() {
10464         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10465
10466         reset_async
10467
10468         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
10469         lctl set_param fail_loc=0x8000040a
10470
10471         # Should simulate EINVAL error which is fatal
10472         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10473         RC=$?
10474         if [[ $RC -eq 0 ]]; then
10475                 error "Must return error due to dropped pages, rc=$RC"
10476         fi
10477
10478         lctl set_param fail_loc=0x0
10479
10480         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10481         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10482         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10483                     grep -c writeback)
10484         if [[ $LOCKED -ne 0 ]]; then
10485                 error "Locked pages remain in cache, locked=$LOCKED"
10486         fi
10487
10488         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10489                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10490         fi
10491
10492         rm -f $DIR/$tfile
10493         echo "No pages locked after fsync"
10494
10495         reset_async
10496         return 0
10497 }
10498 run_test 118f "Simulate unrecoverable OSC side error =========="
10499
10500 test_118g() {
10501         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10502
10503         reset_async
10504
10505         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
10506         lctl set_param fail_loc=0x406
10507
10508         # simulate local -ENOMEM
10509         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10510         RC=$?
10511
10512         lctl set_param fail_loc=0
10513         if [[ $RC -eq 0 ]]; then
10514                 error "Must return error due to dropped pages, rc=$RC"
10515         fi
10516
10517         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10518         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10519         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10520                         grep -c writeback)
10521         if [[ $LOCKED -ne 0 ]]; then
10522                 error "Locked pages remain in cache, locked=$LOCKED"
10523         fi
10524
10525         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10526                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10527         fi
10528
10529         rm -f $DIR/$tfile
10530         echo "No pages locked after fsync"
10531
10532         reset_async
10533         return 0
10534 }
10535 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
10536
10537 test_118h() {
10538         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10539         remote_ost_nodsh && skip "remote OST with nodsh"
10540
10541         reset_async
10542
10543         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10544         set_nodes_failloc "$(osts_nodes)" 0x20e
10545         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10546         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10547         RC=$?
10548
10549         set_nodes_failloc "$(osts_nodes)" 0
10550         if [[ $RC -eq 0 ]]; then
10551                 error "Must return error due to dropped pages, rc=$RC"
10552         fi
10553
10554         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10555         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10556         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10557                     grep -c writeback)
10558         if [[ $LOCKED -ne 0 ]]; then
10559                 error "Locked pages remain in cache, locked=$LOCKED"
10560         fi
10561
10562         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10563                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10564         fi
10565
10566         rm -f $DIR/$tfile
10567         echo "No pages locked after fsync"
10568
10569         return 0
10570 }
10571 run_test 118h "Verify timeout in handling recoverables errors  =========="
10572
10573 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10574
10575 test_118i() {
10576         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10577         remote_ost_nodsh && skip "remote OST with nodsh"
10578
10579         reset_async
10580
10581         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10582         set_nodes_failloc "$(osts_nodes)" 0x20e
10583
10584         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10585         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10586         PID=$!
10587         sleep 5
10588         set_nodes_failloc "$(osts_nodes)" 0
10589
10590         wait $PID
10591         RC=$?
10592         if [[ $RC -ne 0 ]]; then
10593                 error "got error, but should be not, rc=$RC"
10594         fi
10595
10596         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10597         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10598         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10599         if [[ $LOCKED -ne 0 ]]; then
10600                 error "Locked pages remain in cache, locked=$LOCKED"
10601         fi
10602
10603         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10604                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10605         fi
10606
10607         rm -f $DIR/$tfile
10608         echo "No pages locked after fsync"
10609
10610         return 0
10611 }
10612 run_test 118i "Fix error before timeout in recoverable error  =========="
10613
10614 [ "$SLOW" = "no" ] && set_resend_count 4
10615
10616 test_118j() {
10617         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10618         remote_ost_nodsh && skip "remote OST with nodsh"
10619
10620         reset_async
10621
10622         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
10623         set_nodes_failloc "$(osts_nodes)" 0x220
10624
10625         # return -EIO from OST
10626         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10627         RC=$?
10628         set_nodes_failloc "$(osts_nodes)" 0x0
10629         if [[ $RC -eq 0 ]]; then
10630                 error "Must return error due to dropped pages, rc=$RC"
10631         fi
10632
10633         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10634         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10635         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10636         if [[ $LOCKED -ne 0 ]]; then
10637                 error "Locked pages remain in cache, locked=$LOCKED"
10638         fi
10639
10640         # in recoverable error on OST we want resend and stay until it finished
10641         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10642                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10643         fi
10644
10645         rm -f $DIR/$tfile
10646         echo "No pages locked after fsync"
10647
10648         return 0
10649 }
10650 run_test 118j "Simulate unrecoverable OST side error =========="
10651
10652 test_118k()
10653 {
10654         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10655         remote_ost_nodsh && skip "remote OSTs with nodsh"
10656
10657         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10658         set_nodes_failloc "$(osts_nodes)" 0x20e
10659         test_mkdir $DIR/$tdir
10660
10661         for ((i=0;i<10;i++)); do
10662                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
10663                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
10664                 SLEEPPID=$!
10665                 sleep 0.500s
10666                 kill $SLEEPPID
10667                 wait $SLEEPPID
10668         done
10669
10670         set_nodes_failloc "$(osts_nodes)" 0
10671         rm -rf $DIR/$tdir
10672 }
10673 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
10674
10675 test_118l() # LU-646
10676 {
10677         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10678
10679         test_mkdir $DIR/$tdir
10680         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
10681         rm -rf $DIR/$tdir
10682 }
10683 run_test 118l "fsync dir"
10684
10685 test_118m() # LU-3066
10686 {
10687         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10688
10689         test_mkdir $DIR/$tdir
10690         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
10691         rm -rf $DIR/$tdir
10692 }
10693 run_test 118m "fdatasync dir ========="
10694
10695 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10696
10697 test_118n()
10698 {
10699         local begin
10700         local end
10701
10702         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10703         remote_ost_nodsh && skip "remote OSTs with nodsh"
10704
10705         # Sleep to avoid a cached response.
10706         #define OBD_STATFS_CACHE_SECONDS 1
10707         sleep 2
10708
10709         # Inject a 10 second delay in the OST_STATFS handler.
10710         #define OBD_FAIL_OST_STATFS_DELAY 0x242
10711         set_nodes_failloc "$(osts_nodes)" 0x242
10712
10713         begin=$SECONDS
10714         stat --file-system $MOUNT > /dev/null
10715         end=$SECONDS
10716
10717         set_nodes_failloc "$(osts_nodes)" 0
10718
10719         if ((end - begin > 20)); then
10720             error "statfs took $((end - begin)) seconds, expected 10"
10721         fi
10722 }
10723 run_test 118n "statfs() sends OST_STATFS requests in parallel"
10724
10725 test_119a() # bug 11737
10726 {
10727         BSIZE=$((512 * 1024))
10728         directio write $DIR/$tfile 0 1 $BSIZE
10729         # We ask to read two blocks, which is more than a file size.
10730         # directio will indicate an error when requested and actual
10731         # sizes aren't equeal (a normal situation in this case) and
10732         # print actual read amount.
10733         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
10734         if [ "$NOB" != "$BSIZE" ]; then
10735                 error "read $NOB bytes instead of $BSIZE"
10736         fi
10737         rm -f $DIR/$tfile
10738 }
10739 run_test 119a "Short directIO read must return actual read amount"
10740
10741 test_119b() # bug 11737
10742 {
10743         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10744
10745         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
10746         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
10747         sync
10748         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
10749                 error "direct read failed"
10750         rm -f $DIR/$tfile
10751 }
10752 run_test 119b "Sparse directIO read must return actual read amount"
10753
10754 test_119c() # bug 13099
10755 {
10756         BSIZE=1048576
10757         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
10758         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
10759         rm -f $DIR/$tfile
10760 }
10761 run_test 119c "Testing for direct read hitting hole"
10762
10763 test_119d() # bug 15950
10764 {
10765         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10766
10767         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
10768         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
10769         BSIZE=1048576
10770         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
10771         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
10772         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
10773         lctl set_param fail_loc=0x40d
10774         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
10775         pid_dio=$!
10776         sleep 1
10777         cat $DIR/$tfile > /dev/null &
10778         lctl set_param fail_loc=0
10779         pid_reads=$!
10780         wait $pid_dio
10781         log "the DIO writes have completed, now wait for the reads (should not block very long)"
10782         sleep 2
10783         [ -n "`ps h -p $pid_reads -o comm`" ] && \
10784         error "the read rpcs have not completed in 2s"
10785         rm -f $DIR/$tfile
10786         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
10787 }
10788 run_test 119d "The DIO path should try to send a new rpc once one is completed"
10789
10790 test_120a() {
10791         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10792         remote_mds_nodsh && skip "remote MDS with nodsh"
10793         test_mkdir -i0 -c1 $DIR/$tdir
10794         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10795                 skip_env "no early lock cancel on server"
10796
10797         lru_resize_disable mdc
10798         lru_resize_disable osc
10799         cancel_lru_locks mdc
10800         # asynchronous object destroy at MDT could cause bl ast to client
10801         cancel_lru_locks osc
10802
10803         stat $DIR/$tdir > /dev/null
10804         can1=$(do_facet mds1 \
10805                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10806                awk '/ldlm_cancel/ {print $2}')
10807         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10808                awk '/ldlm_bl_callback/ {print $2}')
10809         test_mkdir -i0 -c1 $DIR/$tdir/d1
10810         can2=$(do_facet mds1 \
10811                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10812                awk '/ldlm_cancel/ {print $2}')
10813         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10814                awk '/ldlm_bl_callback/ {print $2}')
10815         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10816         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10817         lru_resize_enable mdc
10818         lru_resize_enable osc
10819 }
10820 run_test 120a "Early Lock Cancel: mkdir test"
10821
10822 test_120b() {
10823         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10824         remote_mds_nodsh && skip "remote MDS with nodsh"
10825         test_mkdir $DIR/$tdir
10826         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10827                 skip_env "no early lock cancel on server"
10828
10829         lru_resize_disable mdc
10830         lru_resize_disable osc
10831         cancel_lru_locks mdc
10832         stat $DIR/$tdir > /dev/null
10833         can1=$(do_facet $SINGLEMDS \
10834                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10835                awk '/ldlm_cancel/ {print $2}')
10836         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10837                awk '/ldlm_bl_callback/ {print $2}')
10838         touch $DIR/$tdir/f1
10839         can2=$(do_facet $SINGLEMDS \
10840                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10841                awk '/ldlm_cancel/ {print $2}')
10842         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10843                awk '/ldlm_bl_callback/ {print $2}')
10844         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10845         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10846         lru_resize_enable mdc
10847         lru_resize_enable osc
10848 }
10849 run_test 120b "Early Lock Cancel: create test"
10850
10851 test_120c() {
10852         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10853         remote_mds_nodsh && skip "remote MDS with nodsh"
10854         test_mkdir -i0 -c1 $DIR/$tdir
10855         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10856                 skip "no early lock cancel on server"
10857
10858         lru_resize_disable mdc
10859         lru_resize_disable osc
10860         test_mkdir -i0 -c1 $DIR/$tdir/d1
10861         test_mkdir -i0 -c1 $DIR/$tdir/d2
10862         touch $DIR/$tdir/d1/f1
10863         cancel_lru_locks mdc
10864         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
10865         can1=$(do_facet mds1 \
10866                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10867                awk '/ldlm_cancel/ {print $2}')
10868         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10869                awk '/ldlm_bl_callback/ {print $2}')
10870         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10871         can2=$(do_facet mds1 \
10872                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10873                awk '/ldlm_cancel/ {print $2}')
10874         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10875                awk '/ldlm_bl_callback/ {print $2}')
10876         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10877         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10878         lru_resize_enable mdc
10879         lru_resize_enable osc
10880 }
10881 run_test 120c "Early Lock Cancel: link test"
10882
10883 test_120d() {
10884         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10885         remote_mds_nodsh && skip "remote MDS with nodsh"
10886         test_mkdir -i0 -c1 $DIR/$tdir
10887         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10888                 skip_env "no early lock cancel on server"
10889
10890         lru_resize_disable mdc
10891         lru_resize_disable osc
10892         touch $DIR/$tdir
10893         cancel_lru_locks mdc
10894         stat $DIR/$tdir > /dev/null
10895         can1=$(do_facet mds1 \
10896                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10897                awk '/ldlm_cancel/ {print $2}')
10898         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10899                awk '/ldlm_bl_callback/ {print $2}')
10900         chmod a+x $DIR/$tdir
10901         can2=$(do_facet mds1 \
10902                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10903                awk '/ldlm_cancel/ {print $2}')
10904         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10905                awk '/ldlm_bl_callback/ {print $2}')
10906         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10907         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10908         lru_resize_enable mdc
10909         lru_resize_enable osc
10910 }
10911 run_test 120d "Early Lock Cancel: setattr test"
10912
10913 test_120e() {
10914         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10915         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10916                 skip_env "no early lock cancel on server"
10917         remote_mds_nodsh && skip "remote MDS with nodsh"
10918
10919         local dlmtrace_set=false
10920
10921         test_mkdir -i0 -c1 $DIR/$tdir
10922         lru_resize_disable mdc
10923         lru_resize_disable osc
10924         ! $LCTL get_param debug | grep -q dlmtrace &&
10925                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
10926         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
10927         cancel_lru_locks mdc
10928         cancel_lru_locks osc
10929         dd if=$DIR/$tdir/f1 of=/dev/null
10930         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
10931         # XXX client can not do early lock cancel of OST lock
10932         # during unlink (LU-4206), so cancel osc lock now.
10933         sleep 2
10934         cancel_lru_locks osc
10935         can1=$(do_facet mds1 \
10936                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10937                awk '/ldlm_cancel/ {print $2}')
10938         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10939                awk '/ldlm_bl_callback/ {print $2}')
10940         unlink $DIR/$tdir/f1
10941         sleep 5
10942         can2=$(do_facet mds1 \
10943                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10944                awk '/ldlm_cancel/ {print $2}')
10945         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10946                awk '/ldlm_bl_callback/ {print $2}')
10947         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
10948                 $LCTL dk $TMP/cancel.debug.txt
10949         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
10950                 $LCTL dk $TMP/blocking.debug.txt
10951         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
10952         lru_resize_enable mdc
10953         lru_resize_enable osc
10954 }
10955 run_test 120e "Early Lock Cancel: unlink test"
10956
10957 test_120f() {
10958         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10959         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10960                 skip_env "no early lock cancel on server"
10961         remote_mds_nodsh && skip "remote MDS with nodsh"
10962
10963         test_mkdir -i0 -c1 $DIR/$tdir
10964         lru_resize_disable mdc
10965         lru_resize_disable osc
10966         test_mkdir -i0 -c1 $DIR/$tdir/d1
10967         test_mkdir -i0 -c1 $DIR/$tdir/d2
10968         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
10969         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
10970         cancel_lru_locks mdc
10971         cancel_lru_locks osc
10972         dd if=$DIR/$tdir/d1/f1 of=/dev/null
10973         dd if=$DIR/$tdir/d2/f2 of=/dev/null
10974         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
10975         # XXX client can not do early lock cancel of OST lock
10976         # during rename (LU-4206), so cancel osc lock now.
10977         sleep 2
10978         cancel_lru_locks osc
10979         can1=$(do_facet mds1 \
10980                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10981                awk '/ldlm_cancel/ {print $2}')
10982         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10983                awk '/ldlm_bl_callback/ {print $2}')
10984         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10985         sleep 5
10986         can2=$(do_facet mds1 \
10987                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10988                awk '/ldlm_cancel/ {print $2}')
10989         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10990                awk '/ldlm_bl_callback/ {print $2}')
10991         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10992         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10993         lru_resize_enable mdc
10994         lru_resize_enable osc
10995 }
10996 run_test 120f "Early Lock Cancel: rename test"
10997
10998 test_120g() {
10999         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11000         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11001                 skip_env "no early lock cancel on server"
11002         remote_mds_nodsh && skip "remote MDS with nodsh"
11003
11004         lru_resize_disable mdc
11005         lru_resize_disable osc
11006         count=10000
11007         echo create $count files
11008         test_mkdir $DIR/$tdir
11009         cancel_lru_locks mdc
11010         cancel_lru_locks osc
11011         t0=$(date +%s)
11012
11013         can0=$(do_facet $SINGLEMDS \
11014                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11015                awk '/ldlm_cancel/ {print $2}')
11016         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11017                awk '/ldlm_bl_callback/ {print $2}')
11018         createmany -o $DIR/$tdir/f $count
11019         sync
11020         can1=$(do_facet $SINGLEMDS \
11021                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11022                awk '/ldlm_cancel/ {print $2}')
11023         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11024                awk '/ldlm_bl_callback/ {print $2}')
11025         t1=$(date +%s)
11026         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
11027         echo rm $count files
11028         rm -r $DIR/$tdir
11029         sync
11030         can2=$(do_facet $SINGLEMDS \
11031                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11032                awk '/ldlm_cancel/ {print $2}')
11033         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11034                awk '/ldlm_bl_callback/ {print $2}')
11035         t2=$(date +%s)
11036         echo total: $count removes in $((t2-t1))
11037         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
11038         sleep 2
11039         # wait for commitment of removal
11040         lru_resize_enable mdc
11041         lru_resize_enable osc
11042 }
11043 run_test 120g "Early Lock Cancel: performance test"
11044
11045 test_121() { #bug #10589
11046         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11047
11048         rm -rf $DIR/$tfile
11049         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
11050 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
11051         lctl set_param fail_loc=0x310
11052         cancel_lru_locks osc > /dev/null
11053         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
11054         lctl set_param fail_loc=0
11055         [[ $reads -eq $writes ]] ||
11056                 error "read $reads blocks, must be $writes blocks"
11057 }
11058 run_test 121 "read cancel race ========="
11059
11060 test_123a() { # was test 123, statahead(bug 11401)
11061         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11062
11063         SLOWOK=0
11064         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
11065                 log "testing UP system. Performance may be lower than expected."
11066                 SLOWOK=1
11067         fi
11068
11069         rm -rf $DIR/$tdir
11070         test_mkdir $DIR/$tdir
11071         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
11072         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
11073         MULT=10
11074         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
11075                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
11076
11077                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
11078                 lctl set_param -n llite.*.statahead_max 0
11079                 lctl get_param llite.*.statahead_max
11080                 cancel_lru_locks mdc
11081                 cancel_lru_locks osc
11082                 stime=`date +%s`
11083                 time ls -l $DIR/$tdir | wc -l
11084                 etime=`date +%s`
11085                 delta=$((etime - stime))
11086                 log "ls $i files without statahead: $delta sec"
11087                 lctl set_param llite.*.statahead_max=$max
11088
11089                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
11090                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
11091                 cancel_lru_locks mdc
11092                 cancel_lru_locks osc
11093                 stime=`date +%s`
11094                 time ls -l $DIR/$tdir | wc -l
11095                 etime=`date +%s`
11096                 delta_sa=$((etime - stime))
11097                 log "ls $i files with statahead: $delta_sa sec"
11098                 lctl get_param -n llite.*.statahead_stats
11099                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
11100
11101                 [[ $swrong -lt $ewrong ]] &&
11102                         log "statahead was stopped, maybe too many locks held!"
11103                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
11104
11105                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
11106                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
11107                     lctl set_param -n llite.*.statahead_max 0
11108                     lctl get_param llite.*.statahead_max
11109                     cancel_lru_locks mdc
11110                     cancel_lru_locks osc
11111                     stime=`date +%s`
11112                     time ls -l $DIR/$tdir | wc -l
11113                     etime=`date +%s`
11114                     delta=$((etime - stime))
11115                     log "ls $i files again without statahead: $delta sec"
11116                     lctl set_param llite.*.statahead_max=$max
11117                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
11118                         if [  $SLOWOK -eq 0 ]; then
11119                                 error "ls $i files is slower with statahead!"
11120                         else
11121                                 log "ls $i files is slower with statahead!"
11122                         fi
11123                         break
11124                     fi
11125                 fi
11126
11127                 [ $delta -gt 20 ] && break
11128                 [ $delta -gt 8 ] && MULT=$((50 / delta))
11129                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
11130         done
11131         log "ls done"
11132
11133         stime=`date +%s`
11134         rm -r $DIR/$tdir
11135         sync
11136         etime=`date +%s`
11137         delta=$((etime - stime))
11138         log "rm -r $DIR/$tdir/: $delta seconds"
11139         log "rm done"
11140         lctl get_param -n llite.*.statahead_stats
11141 }
11142 run_test 123a "verify statahead work"
11143
11144 test_123b () { # statahead(bug 15027)
11145         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11146
11147         test_mkdir $DIR/$tdir
11148         createmany -o $DIR/$tdir/$tfile-%d 1000
11149
11150         cancel_lru_locks mdc
11151         cancel_lru_locks osc
11152
11153 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
11154         lctl set_param fail_loc=0x80000803
11155         ls -lR $DIR/$tdir > /dev/null
11156         log "ls done"
11157         lctl set_param fail_loc=0x0
11158         lctl get_param -n llite.*.statahead_stats
11159         rm -r $DIR/$tdir
11160         sync
11161
11162 }
11163 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
11164
11165 test_123c() {
11166         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
11167
11168         test_mkdir -i 0 -c 1 $DIR/$tdir.0
11169         test_mkdir -i 1 -c 1 $DIR/$tdir.1
11170         touch $DIR/$tdir.1/{1..3}
11171         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
11172
11173         remount_client $MOUNT
11174
11175         $MULTIOP $DIR/$tdir.0 Q
11176
11177         # let statahead to complete
11178         ls -l $DIR/$tdir.0 > /dev/null
11179
11180         testid=$(echo $TESTNAME | tr '_' ' ')
11181         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
11182                 error "statahead warning" || true
11183 }
11184 run_test 123c "Can not initialize inode warning on DNE statahead"
11185
11186 test_124a() {
11187         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11188         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11189                 skip_env "no lru resize on server"
11190
11191         local NR=2000
11192
11193         test_mkdir $DIR/$tdir
11194
11195         log "create $NR files at $DIR/$tdir"
11196         createmany -o $DIR/$tdir/f $NR ||
11197                 error "failed to create $NR files in $DIR/$tdir"
11198
11199         cancel_lru_locks mdc
11200         ls -l $DIR/$tdir > /dev/null
11201
11202         local NSDIR=""
11203         local LRU_SIZE=0
11204         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
11205                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
11206                 LRU_SIZE=$($LCTL get_param -n $PARAM)
11207                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
11208                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
11209                         log "NSDIR=$NSDIR"
11210                         log "NS=$(basename $NSDIR)"
11211                         break
11212                 fi
11213         done
11214
11215         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
11216                 skip "Not enough cached locks created!"
11217         fi
11218         log "LRU=$LRU_SIZE"
11219
11220         local SLEEP=30
11221
11222         # We know that lru resize allows one client to hold $LIMIT locks
11223         # for 10h. After that locks begin to be killed by client.
11224         local MAX_HRS=10
11225         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
11226         log "LIMIT=$LIMIT"
11227         if [ $LIMIT -lt $LRU_SIZE ]; then
11228                 skip "Limit is too small $LIMIT"
11229         fi
11230
11231         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
11232         # killing locks. Some time was spent for creating locks. This means
11233         # that up to the moment of sleep finish we must have killed some of
11234         # them (10-100 locks). This depends on how fast ther were created.
11235         # Many of them were touched in almost the same moment and thus will
11236         # be killed in groups.
11237         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
11238
11239         # Use $LRU_SIZE_B here to take into account real number of locks
11240         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
11241         local LRU_SIZE_B=$LRU_SIZE
11242         log "LVF=$LVF"
11243         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
11244         log "OLD_LVF=$OLD_LVF"
11245         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
11246
11247         # Let's make sure that we really have some margin. Client checks
11248         # cached locks every 10 sec.
11249         SLEEP=$((SLEEP+20))
11250         log "Sleep ${SLEEP} sec"
11251         local SEC=0
11252         while ((SEC<$SLEEP)); do
11253                 echo -n "..."
11254                 sleep 5
11255                 SEC=$((SEC+5))
11256                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
11257                 echo -n "$LRU_SIZE"
11258         done
11259         echo ""
11260         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
11261         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
11262
11263         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
11264                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
11265                 unlinkmany $DIR/$tdir/f $NR
11266                 return
11267         }
11268
11269         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
11270         log "unlink $NR files at $DIR/$tdir"
11271         unlinkmany $DIR/$tdir/f $NR
11272 }
11273 run_test 124a "lru resize ======================================="
11274
11275 get_max_pool_limit()
11276 {
11277         local limit=$($LCTL get_param \
11278                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
11279         local max=0
11280         for l in $limit; do
11281                 if [[ $l -gt $max ]]; then
11282                         max=$l
11283                 fi
11284         done
11285         echo $max
11286 }
11287
11288 test_124b() {
11289         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11290         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11291                 skip_env "no lru resize on server"
11292
11293         LIMIT=$(get_max_pool_limit)
11294
11295         NR=$(($(default_lru_size)*20))
11296         if [[ $NR -gt $LIMIT ]]; then
11297                 log "Limit lock number by $LIMIT locks"
11298                 NR=$LIMIT
11299         fi
11300
11301         IFree=$(mdsrate_inodes_available)
11302         if [ $IFree -lt $NR ]; then
11303                 log "Limit lock number by $IFree inodes"
11304                 NR=$IFree
11305         fi
11306
11307         lru_resize_disable mdc
11308         test_mkdir -p $DIR/$tdir/disable_lru_resize
11309
11310         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
11311         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
11312         cancel_lru_locks mdc
11313         stime=`date +%s`
11314         PID=""
11315         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11316         PID="$PID $!"
11317         sleep 2
11318         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11319         PID="$PID $!"
11320         sleep 2
11321         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11322         PID="$PID $!"
11323         wait $PID
11324         etime=`date +%s`
11325         nolruresize_delta=$((etime-stime))
11326         log "ls -la time: $nolruresize_delta seconds"
11327         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11328         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
11329
11330         lru_resize_enable mdc
11331         test_mkdir -p $DIR/$tdir/enable_lru_resize
11332
11333         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
11334         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
11335         cancel_lru_locks mdc
11336         stime=`date +%s`
11337         PID=""
11338         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11339         PID="$PID $!"
11340         sleep 2
11341         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11342         PID="$PID $!"
11343         sleep 2
11344         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11345         PID="$PID $!"
11346         wait $PID
11347         etime=`date +%s`
11348         lruresize_delta=$((etime-stime))
11349         log "ls -la time: $lruresize_delta seconds"
11350         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11351
11352         if [ $lruresize_delta -gt $nolruresize_delta ]; then
11353                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
11354         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
11355                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
11356         else
11357                 log "lru resize performs the same with no lru resize"
11358         fi
11359         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
11360 }
11361 run_test 124b "lru resize (performance test) ======================="
11362
11363 test_124c() {
11364         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11365         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11366                 skip_env "no lru resize on server"
11367
11368         # cache ununsed locks on client
11369         local nr=100
11370         cancel_lru_locks mdc
11371         test_mkdir $DIR/$tdir
11372         createmany -o $DIR/$tdir/f $nr ||
11373                 error "failed to create $nr files in $DIR/$tdir"
11374         ls -l $DIR/$tdir > /dev/null
11375
11376         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11377         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11378         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11379         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11380         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11381
11382         # set lru_max_age to 1 sec
11383         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11384         echo "sleep $((recalc_p * 2)) seconds..."
11385         sleep $((recalc_p * 2))
11386
11387         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11388         # restore lru_max_age
11389         $LCTL set_param -n $nsdir.lru_max_age $max_age
11390         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11391         unlinkmany $DIR/$tdir/f $nr
11392 }
11393 run_test 124c "LRUR cancel very aged locks"
11394
11395 test_124d() {
11396         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11397         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11398                 skip_env "no lru resize on server"
11399
11400         # cache ununsed locks on client
11401         local nr=100
11402
11403         lru_resize_disable mdc
11404         stack_trap "lru_resize_enable mdc" EXIT
11405
11406         cancel_lru_locks mdc
11407
11408         # asynchronous object destroy at MDT could cause bl ast to client
11409         test_mkdir $DIR/$tdir
11410         createmany -o $DIR/$tdir/f $nr ||
11411                 error "failed to create $nr files in $DIR/$tdir"
11412         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
11413
11414         ls -l $DIR/$tdir > /dev/null
11415
11416         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11417         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11418         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11419         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11420
11421         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11422
11423         # set lru_max_age to 1 sec
11424         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11425         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
11426
11427         echo "sleep $((recalc_p * 2)) seconds..."
11428         sleep $((recalc_p * 2))
11429
11430         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11431
11432         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11433 }
11434 run_test 124d "cancel very aged locks if lru-resize diasbaled"
11435
11436 test_125() { # 13358
11437         $LCTL get_param -n llite.*.client_type | grep -q local ||
11438                 skip "must run as local client"
11439         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
11440                 skip_env "must have acl enabled"
11441         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
11442
11443         test_mkdir $DIR/$tdir
11444         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
11445         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
11446         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
11447 }
11448 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
11449
11450 test_126() { # bug 12829/13455
11451         $GSS && skip_env "must run as gss disabled"
11452         $LCTL get_param -n llite.*.client_type | grep -q local ||
11453                 skip "must run as local client"
11454         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
11455
11456         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
11457         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
11458         rm -f $DIR/$tfile
11459         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
11460 }
11461 run_test 126 "check that the fsgid provided by the client is taken into account"
11462
11463 test_127a() { # bug 15521
11464         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11465         local name count samp unit min max sum sumsq
11466
11467         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
11468         echo "stats before reset"
11469         $LCTL get_param osc.*.stats
11470         $LCTL set_param osc.*.stats=0
11471         local fsize=$((2048 * 1024))
11472
11473         dd if=/dev/zero of=$DIR/$tfile bs=$fsize count=1
11474         cancel_lru_locks osc
11475         dd if=$DIR/$tfile of=/dev/null bs=$fsize
11476
11477         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/$tfile.tmp
11478         stack_trap "rm -f $TMP/$tfile.tmp"
11479         while read name count samp unit min max sum sumsq; do
11480                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11481                 [ ! $min ] && error "Missing min value for $name proc entry"
11482                 eval $name=$count || error "Wrong proc format"
11483
11484                 case $name in
11485                 read_bytes|write_bytes)
11486                         [[ "$unit" =~ "bytes" ]] ||
11487                                 error "unit is not 'bytes': $unit"
11488                         (( $min >= 4096 )) || error "min is too small: $min"
11489                         (( $min <= $fsize )) || error "min is too big: $min"
11490                         (( $max >= 4096 )) || error "max is too small: $max"
11491                         (( $max <= $fsize )) || error "max is too big: $max"
11492                         (( $sum == $fsize )) || error "sum is wrong: $sum"
11493                         (( $sumsq >= ($fsize / 4096) * (4096 * 4096) )) ||
11494                                 error "sumsquare is too small: $sumsq"
11495                         (( $sumsq <= $fsize * $fsize )) ||
11496                                 error "sumsquare is too big: $sumsq"
11497                         ;;
11498                 ost_read|ost_write)
11499                         [[ "$unit" =~ "usec" ]] ||
11500                                 error "unit is not 'usec': $unit"
11501                         ;;
11502                 *)      ;;
11503                 esac
11504         done < $DIR/$tfile.tmp
11505
11506         #check that we actually got some stats
11507         [ "$read_bytes" ] || error "Missing read_bytes stats"
11508         [ "$write_bytes" ] || error "Missing write_bytes stats"
11509         [ "$read_bytes" != 0 ] || error "no read done"
11510         [ "$write_bytes" != 0 ] || error "no write done"
11511 }
11512 run_test 127a "verify the client stats are sane"
11513
11514 test_127b() { # bug LU-333
11515         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11516         local name count samp unit min max sum sumsq
11517
11518         echo "stats before reset"
11519         $LCTL get_param llite.*.stats
11520         $LCTL set_param llite.*.stats=0
11521
11522         # perform 2 reads and writes so MAX is different from SUM.
11523         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11524         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11525         cancel_lru_locks osc
11526         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11527         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11528
11529         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
11530         stack_trap "rm -f $TMP/$tfile.tmp"
11531         while read name count samp unit min max sum sumsq; do
11532                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11533                 eval $name=$count || error "Wrong proc format"
11534
11535                 case $name in
11536                 read_bytes|write_bytes)
11537                         [[ "$unit" =~ "bytes" ]] ||
11538                                 error "unit is not 'bytes': $unit"
11539                         (( $count == 2 )) || error "count is not 2: $count"
11540                         (( $min == $PAGE_SIZE )) ||
11541                                 error "min is not $PAGE_SIZE: $min"
11542                         (( $max == $PAGE_SIZE )) ||
11543                                 error "max is not $PAGE_SIZE: $max"
11544                         (( $sum == $PAGE_SIZE * 2 )) ||
11545                                 error "sum is not $((PAGE_SIZE * 2)): $sum"
11546                         ;;
11547                 read|write)
11548                         [[ "$unit" =~ "usec" ]] ||
11549                                 error "unit is not 'usec': $unit"
11550                         ;;
11551                 *)      ;;
11552                 esac
11553         done < $TMP/$tfile.tmp
11554
11555         #check that we actually got some stats
11556         [ "$read_bytes" ] || error "Missing read_bytes stats"
11557         [ "$write_bytes" ] || error "Missing write_bytes stats"
11558         [ "$read_bytes" != 0 ] || error "no read done"
11559         [ "$write_bytes" != 0 ] || error "no write done"
11560 }
11561 run_test 127b "verify the llite client stats are sane"
11562
11563 test_127c() { # LU-12394
11564         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11565         local size
11566         local bsize
11567         local reads
11568         local writes
11569         local count
11570
11571         $LCTL set_param llite.*.extents_stats=1
11572         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
11573
11574         # Use two stripes so there is enough space in default config
11575         $LFS setstripe -c 2 $DIR/$tfile
11576
11577         # Extent stats start at 0-4K and go in power of two buckets
11578         # LL_HIST_START = 12 --> 2^12 = 4K
11579         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
11580         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
11581         # small configs
11582         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
11583                 do
11584                 # Write and read, 2x each, second time at a non-zero offset
11585                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
11586                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
11587                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
11588                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
11589                 rm -f $DIR/$tfile
11590         done
11591
11592         $LCTL get_param llite.*.extents_stats
11593
11594         count=2
11595         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
11596                 do
11597                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
11598                                 grep -m 1 $bsize)
11599                 reads=$(echo $bucket | awk '{print $5}')
11600                 writes=$(echo $bucket | awk '{print $9}')
11601                 [ "$reads" -eq $count ] ||
11602                         error "$reads reads in < $bsize bucket, expect $count"
11603                 [ "$writes" -eq $count ] ||
11604                         error "$writes writes in < $bsize bucket, expect $count"
11605         done
11606
11607         # Test mmap write and read
11608         $LCTL set_param llite.*.extents_stats=c
11609         size=512
11610         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
11611         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
11612         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
11613
11614         $LCTL get_param llite.*.extents_stats
11615
11616         count=$(((size*1024) / PAGE_SIZE))
11617
11618         bsize=$((2 * PAGE_SIZE / 1024))K
11619
11620         bucket=$($LCTL get_param -n llite.*.extents_stats |
11621                         grep -m 1 $bsize)
11622         reads=$(echo $bucket | awk '{print $5}')
11623         writes=$(echo $bucket | awk '{print $9}')
11624         # mmap writes fault in the page first, creating an additonal read
11625         [ "$reads" -eq $((2 * count)) ] ||
11626                 error "$reads reads in < $bsize bucket, expect $count"
11627         [ "$writes" -eq $count ] ||
11628                 error "$writes writes in < $bsize bucket, expect $count"
11629 }
11630 run_test 127c "test llite extent stats with regular & mmap i/o"
11631
11632 test_128() { # bug 15212
11633         touch $DIR/$tfile
11634         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
11635                 find $DIR/$tfile
11636                 find $DIR/$tfile
11637         EOF
11638
11639         result=$(grep error $TMP/$tfile.log)
11640         rm -f $DIR/$tfile $TMP/$tfile.log
11641         [ -z "$result" ] ||
11642                 error "consecutive find's under interactive lfs failed"
11643 }
11644 run_test 128 "interactive lfs for 2 consecutive find's"
11645
11646 set_dir_limits () {
11647         local mntdev
11648         local canondev
11649         local node
11650
11651         local ldproc=/proc/fs/ldiskfs
11652         local facets=$(get_facets MDS)
11653
11654         for facet in ${facets//,/ }; do
11655                 canondev=$(ldiskfs_canon \
11656                            *.$(convert_facet2label $facet).mntdev $facet)
11657                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
11658                         ldproc=/sys/fs/ldiskfs
11659                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
11660                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
11661         done
11662 }
11663
11664 check_mds_dmesg() {
11665         local facets=$(get_facets MDS)
11666         for facet in ${facets//,/ }; do
11667                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
11668         done
11669         return 1
11670 }
11671
11672 test_129() {
11673         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11674         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
11675                 skip "Need MDS version with at least 2.5.56"
11676         if [ "$mds1_FSTYPE" != ldiskfs ]; then
11677                 skip_env "ldiskfs only test"
11678         fi
11679         remote_mds_nodsh && skip "remote MDS with nodsh"
11680
11681         local ENOSPC=28
11682         local EFBIG=27
11683         local has_warning=false
11684
11685         rm -rf $DIR/$tdir
11686         mkdir -p $DIR/$tdir
11687
11688         # block size of mds1
11689         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
11690         set_dir_limits $maxsize $maxsize
11691         local dirsize=$(stat -c%s "$DIR/$tdir")
11692         local nfiles=0
11693         while [[ $dirsize -le $maxsize ]]; do
11694                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
11695                 rc=$?
11696                 if ! $has_warning; then
11697                         check_mds_dmesg '"is approaching"' && has_warning=true
11698                 fi
11699                 # check two errors:
11700                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
11701                 # EFBIG for previous versions included in ldiskfs series
11702                 if [ $rc -eq $EFBIG ] || [ $rc -eq $ENOSPC ]; then
11703                         set_dir_limits 0 0
11704                         echo "return code $rc received as expected"
11705
11706                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
11707                                 error_exit "create failed w/o dir size limit"
11708
11709                         check_mds_dmesg '"has reached"' ||
11710                                 error_exit "reached message should be output"
11711
11712                         [ $has_warning = "false" ] &&
11713                                 error_exit "warning message should be output"
11714
11715                         dirsize=$(stat -c%s "$DIR/$tdir")
11716
11717                         [[ $dirsize -ge $maxsize ]] && return 0
11718                         error_exit "current dir size $dirsize, " \
11719                                    "previous limit $maxsize"
11720                 elif [ $rc -ne 0 ]; then
11721                         set_dir_limits 0 0
11722                         error_exit "return $rc received instead of expected " \
11723                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
11724                 fi
11725                 nfiles=$((nfiles + 1))
11726                 dirsize=$(stat -c%s "$DIR/$tdir")
11727         done
11728
11729         set_dir_limits 0 0
11730         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
11731 }
11732 run_test 129 "test directory size limit ========================"
11733
11734 OLDIFS="$IFS"
11735 cleanup_130() {
11736         trap 0
11737         IFS="$OLDIFS"
11738 }
11739
11740 test_130a() {
11741         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11742         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11743
11744         trap cleanup_130 EXIT RETURN
11745
11746         local fm_file=$DIR/$tfile
11747         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
11748         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
11749                 error "dd failed for $fm_file"
11750
11751         # LU-1795: test filefrag/FIEMAP once, even if unsupported
11752         filefrag -ves $fm_file
11753         RC=$?
11754         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11755                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11756         [ $RC != 0 ] && error "filefrag $fm_file failed"
11757
11758         filefrag_op=$(filefrag -ve -k $fm_file |
11759                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11760         lun=$($LFS getstripe -i $fm_file)
11761
11762         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
11763         IFS=$'\n'
11764         tot_len=0
11765         for line in $filefrag_op
11766         do
11767                 frag_lun=`echo $line | cut -d: -f5`
11768                 ext_len=`echo $line | cut -d: -f4`
11769                 if (( $frag_lun != $lun )); then
11770                         cleanup_130
11771                         error "FIEMAP on 1-stripe file($fm_file) failed"
11772                         return
11773                 fi
11774                 (( tot_len += ext_len ))
11775         done
11776
11777         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
11778                 cleanup_130
11779                 error "FIEMAP on 1-stripe file($fm_file) failed;"
11780                 return
11781         fi
11782
11783         cleanup_130
11784
11785         echo "FIEMAP on single striped file succeeded"
11786 }
11787 run_test 130a "FIEMAP (1-stripe file)"
11788
11789 test_130b() {
11790         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11791
11792         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11793         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11794
11795         trap cleanup_130 EXIT RETURN
11796
11797         local fm_file=$DIR/$tfile
11798         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
11799                         error "setstripe on $fm_file"
11800         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11801                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11802
11803         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
11804                 error "dd failed on $fm_file"
11805
11806         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11807         filefrag_op=$(filefrag -ve -k $fm_file |
11808                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11809
11810         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11811                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11812
11813         IFS=$'\n'
11814         tot_len=0
11815         num_luns=1
11816         for line in $filefrag_op
11817         do
11818                 frag_lun=$(echo $line | cut -d: -f5 |
11819                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11820                 ext_len=$(echo $line | cut -d: -f4)
11821                 if (( $frag_lun != $last_lun )); then
11822                         if (( tot_len != 1024 )); then
11823                                 cleanup_130
11824                                 error "FIEMAP on $fm_file failed; returned " \
11825                                 "len $tot_len for OST $last_lun instead of 1024"
11826                                 return
11827                         else
11828                                 (( num_luns += 1 ))
11829                                 tot_len=0
11830                         fi
11831                 fi
11832                 (( tot_len += ext_len ))
11833                 last_lun=$frag_lun
11834         done
11835         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
11836                 cleanup_130
11837                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11838                         "luns or wrong len for OST $last_lun"
11839                 return
11840         fi
11841
11842         cleanup_130
11843
11844         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
11845 }
11846 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
11847
11848 test_130c() {
11849         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11850
11851         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11852         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11853
11854         trap cleanup_130 EXIT RETURN
11855
11856         local fm_file=$DIR/$tfile
11857         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
11858         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11859                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11860
11861         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
11862                         error "dd failed on $fm_file"
11863
11864         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11865         filefrag_op=$(filefrag -ve -k $fm_file |
11866                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11867
11868         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11869                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11870
11871         IFS=$'\n'
11872         tot_len=0
11873         num_luns=1
11874         for line in $filefrag_op
11875         do
11876                 frag_lun=$(echo $line | cut -d: -f5 |
11877                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11878                 ext_len=$(echo $line | cut -d: -f4)
11879                 if (( $frag_lun != $last_lun )); then
11880                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
11881                         if (( logical != 512 )); then
11882                                 cleanup_130
11883                                 error "FIEMAP on $fm_file failed; returned " \
11884                                 "logical start for lun $logical instead of 512"
11885                                 return
11886                         fi
11887                         if (( tot_len != 512 )); then
11888                                 cleanup_130
11889                                 error "FIEMAP on $fm_file failed; returned " \
11890                                 "len $tot_len for OST $last_lun instead of 1024"
11891                                 return
11892                         else
11893                                 (( num_luns += 1 ))
11894                                 tot_len=0
11895                         fi
11896                 fi
11897                 (( tot_len += ext_len ))
11898                 last_lun=$frag_lun
11899         done
11900         if (( num_luns != 2 || tot_len != 512 )); then
11901                 cleanup_130
11902                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11903                         "luns or wrong len for OST $last_lun"
11904                 return
11905         fi
11906
11907         cleanup_130
11908
11909         echo "FIEMAP on 2-stripe file with hole succeeded"
11910 }
11911 run_test 130c "FIEMAP (2-stripe file with hole)"
11912
11913 test_130d() {
11914         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
11915
11916         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11917         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11918
11919         trap cleanup_130 EXIT RETURN
11920
11921         local fm_file=$DIR/$tfile
11922         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
11923                         error "setstripe on $fm_file"
11924         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11925                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11926
11927         local actual_stripe_count=$($LFS getstripe -c $fm_file)
11928         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
11929                 error "dd failed on $fm_file"
11930
11931         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11932         filefrag_op=$(filefrag -ve -k $fm_file |
11933                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11934
11935         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11936                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11937
11938         IFS=$'\n'
11939         tot_len=0
11940         num_luns=1
11941         for line in $filefrag_op
11942         do
11943                 frag_lun=$(echo $line | cut -d: -f5 |
11944                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11945                 ext_len=$(echo $line | cut -d: -f4)
11946                 if (( $frag_lun != $last_lun )); then
11947                         if (( tot_len != 1024 )); then
11948                                 cleanup_130
11949                                 error "FIEMAP on $fm_file failed; returned " \
11950                                 "len $tot_len for OST $last_lun instead of 1024"
11951                                 return
11952                         else
11953                                 (( num_luns += 1 ))
11954                                 tot_len=0
11955                         fi
11956                 fi
11957                 (( tot_len += ext_len ))
11958                 last_lun=$frag_lun
11959         done
11960         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
11961                 cleanup_130
11962                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11963                         "luns or wrong len for OST $last_lun"
11964                 return
11965         fi
11966
11967         cleanup_130
11968
11969         echo "FIEMAP on N-stripe file succeeded"
11970 }
11971 run_test 130d "FIEMAP (N-stripe file)"
11972
11973 test_130e() {
11974         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11975
11976         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11977         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11978
11979         trap cleanup_130 EXIT RETURN
11980
11981         local fm_file=$DIR/$tfile
11982         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
11983         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11984                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11985
11986         NUM_BLKS=512
11987         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
11988         for ((i = 0; i < $NUM_BLKS; i++))
11989         do
11990                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
11991         done
11992
11993         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11994         filefrag_op=$(filefrag -ve -k $fm_file |
11995                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11996
11997         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11998                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11999
12000         IFS=$'\n'
12001         tot_len=0
12002         num_luns=1
12003         for line in $filefrag_op
12004         do
12005                 frag_lun=$(echo $line | cut -d: -f5 |
12006                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12007                 ext_len=$(echo $line | cut -d: -f4)
12008                 if (( $frag_lun != $last_lun )); then
12009                         if (( tot_len != $EXPECTED_LEN )); then
12010                                 cleanup_130
12011                                 error "FIEMAP on $fm_file failed; returned " \
12012                                 "len $tot_len for OST $last_lun instead " \
12013                                 "of $EXPECTED_LEN"
12014                                 return
12015                         else
12016                                 (( num_luns += 1 ))
12017                                 tot_len=0
12018                         fi
12019                 fi
12020                 (( tot_len += ext_len ))
12021                 last_lun=$frag_lun
12022         done
12023         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
12024                 cleanup_130
12025                 error "FIEMAP on $fm_file failed; returned wrong number " \
12026                         "of luns or wrong len for OST $last_lun"
12027                 return
12028         fi
12029
12030         cleanup_130
12031
12032         echo "FIEMAP with continuation calls succeeded"
12033 }
12034 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
12035
12036 test_130f() {
12037         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12038         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12039
12040         local fm_file=$DIR/$tfile
12041         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
12042                 error "multiop create with lov_delay_create on $fm_file"
12043
12044         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12045         filefrag_extents=$(filefrag -vek $fm_file |
12046                            awk '/extents? found/ { print $2 }')
12047         if [[ "$filefrag_extents" != "0" ]]; then
12048                 error "FIEMAP on $fm_file failed; " \
12049                       "returned $filefrag_extents expected 0"
12050         fi
12051
12052         rm -f $fm_file
12053 }
12054 run_test 130f "FIEMAP (unstriped file)"
12055
12056 # Test for writev/readv
12057 test_131a() {
12058         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
12059                 error "writev test failed"
12060         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
12061                 error "readv failed"
12062         rm -f $DIR/$tfile
12063 }
12064 run_test 131a "test iov's crossing stripe boundary for writev/readv"
12065
12066 test_131b() {
12067         local fsize=$((524288 + 1048576 + 1572864))
12068         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
12069                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12070                         error "append writev test failed"
12071
12072         ((fsize += 1572864 + 1048576))
12073         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
12074                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12075                         error "append writev test failed"
12076         rm -f $DIR/$tfile
12077 }
12078 run_test 131b "test append writev"
12079
12080 test_131c() {
12081         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
12082         error "NOT PASS"
12083 }
12084 run_test 131c "test read/write on file w/o objects"
12085
12086 test_131d() {
12087         rwv -f $DIR/$tfile -w -n 1 1572864
12088         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
12089         if [ "$NOB" != 1572864 ]; then
12090                 error "Short read filed: read $NOB bytes instead of 1572864"
12091         fi
12092         rm -f $DIR/$tfile
12093 }
12094 run_test 131d "test short read"
12095
12096 test_131e() {
12097         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
12098         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
12099         error "read hitting hole failed"
12100         rm -f $DIR/$tfile
12101 }
12102 run_test 131e "test read hitting hole"
12103
12104 check_stats() {
12105         local facet=$1
12106         local op=$2
12107         local want=${3:-0}
12108         local res
12109
12110         case $facet in
12111         mds*) res=$(do_facet $facet \
12112                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
12113                  ;;
12114         ost*) res=$(do_facet $facet \
12115                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
12116                  ;;
12117         *) error "Wrong facet '$facet'" ;;
12118         esac
12119         [ "$res" ] || error "The counter for $op on $facet was not incremented"
12120         # if the argument $3 is zero, it means any stat increment is ok.
12121         if [[ $want -gt 0 ]]; then
12122                 local count=$(echo $res | awk '{ print $2 }')
12123                 [[ $count -ne $want ]] &&
12124                         error "The $op counter on $facet is $count, not $want"
12125         fi
12126 }
12127
12128 test_133a() {
12129         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12130         remote_ost_nodsh && skip "remote OST with nodsh"
12131         remote_mds_nodsh && skip "remote MDS with nodsh"
12132         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12133                 skip_env "MDS doesn't support rename stats"
12134
12135         local testdir=$DIR/${tdir}/stats_testdir
12136
12137         mkdir -p $DIR/${tdir}
12138
12139         # clear stats.
12140         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12141         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12142
12143         # verify mdt stats first.
12144         mkdir ${testdir} || error "mkdir failed"
12145         check_stats $SINGLEMDS "mkdir" 1
12146         touch ${testdir}/${tfile} || error "touch failed"
12147         check_stats $SINGLEMDS "open" 1
12148         check_stats $SINGLEMDS "close" 1
12149         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
12150                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
12151                 check_stats $SINGLEMDS "mknod" 2
12152         }
12153         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
12154         check_stats $SINGLEMDS "unlink" 1
12155         rm -f ${testdir}/${tfile} || error "file remove failed"
12156         check_stats $SINGLEMDS "unlink" 2
12157
12158         # remove working dir and check mdt stats again.
12159         rmdir ${testdir} || error "rmdir failed"
12160         check_stats $SINGLEMDS "rmdir" 1
12161
12162         local testdir1=$DIR/${tdir}/stats_testdir1
12163         mkdir -p ${testdir}
12164         mkdir -p ${testdir1}
12165         touch ${testdir1}/test1
12166         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
12167         check_stats $SINGLEMDS "crossdir_rename" 1
12168
12169         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
12170         check_stats $SINGLEMDS "samedir_rename" 1
12171
12172         rm -rf $DIR/${tdir}
12173 }
12174 run_test 133a "Verifying MDT stats ========================================"
12175
12176 test_133b() {
12177         local res
12178
12179         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12180         remote_ost_nodsh && skip "remote OST with nodsh"
12181         remote_mds_nodsh && skip "remote MDS with nodsh"
12182
12183         local testdir=$DIR/${tdir}/stats_testdir
12184
12185         mkdir -p ${testdir} || error "mkdir failed"
12186         touch ${testdir}/${tfile} || error "touch failed"
12187         cancel_lru_locks mdc
12188
12189         # clear stats.
12190         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12191         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12192
12193         # extra mdt stats verification.
12194         chmod 444 ${testdir}/${tfile} || error "chmod failed"
12195         check_stats $SINGLEMDS "setattr" 1
12196         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12197         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
12198         then            # LU-1740
12199                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
12200                 check_stats $SINGLEMDS "getattr" 1
12201         fi
12202         rm -rf $DIR/${tdir}
12203
12204         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
12205         # so the check below is not reliable
12206         [ $MDSCOUNT -eq 1 ] || return 0
12207
12208         # Sleep to avoid a cached response.
12209         #define OBD_STATFS_CACHE_SECONDS 1
12210         sleep 2
12211         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12212         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12213         $LFS df || error "lfs failed"
12214         check_stats $SINGLEMDS "statfs" 1
12215
12216         # check aggregated statfs (LU-10018)
12217         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
12218                 return 0
12219         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
12220                 return 0
12221         sleep 2
12222         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12223         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12224         df $DIR
12225         check_stats $SINGLEMDS "statfs" 1
12226
12227         # We want to check that the client didn't send OST_STATFS to
12228         # ost1 but the MDT also uses OST_STATFS for precreate. So some
12229         # extra care is needed here.
12230         if remote_mds; then
12231                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
12232                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
12233
12234                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
12235                 [ "$res" ] && error "OST got STATFS"
12236         fi
12237
12238         return 0
12239 }
12240 run_test 133b "Verifying extra MDT stats =================================="
12241
12242 test_133c() {
12243         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12244         remote_ost_nodsh && skip "remote OST with nodsh"
12245         remote_mds_nodsh && skip "remote MDS with nodsh"
12246
12247         local testdir=$DIR/$tdir/stats_testdir
12248
12249         test_mkdir -p $testdir
12250
12251         # verify obdfilter stats.
12252         $LFS setstripe -c 1 -i 0 $testdir/$tfile
12253         sync
12254         cancel_lru_locks osc
12255         wait_delete_completed
12256
12257         # clear stats.
12258         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12259         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12260
12261         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
12262                 error "dd failed"
12263         sync
12264         cancel_lru_locks osc
12265         check_stats ost1 "write" 1
12266
12267         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
12268         check_stats ost1 "read" 1
12269
12270         > $testdir/$tfile || error "truncate failed"
12271         check_stats ost1 "punch" 1
12272
12273         rm -f $testdir/$tfile || error "file remove failed"
12274         wait_delete_completed
12275         check_stats ost1 "destroy" 1
12276
12277         rm -rf $DIR/$tdir
12278 }
12279 run_test 133c "Verifying OST stats ========================================"
12280
12281 order_2() {
12282         local value=$1
12283         local orig=$value
12284         local order=1
12285
12286         while [ $value -ge 2 ]; do
12287                 order=$((order*2))
12288                 value=$((value/2))
12289         done
12290
12291         if [ $orig -gt $order ]; then
12292                 order=$((order*2))
12293         fi
12294         echo $order
12295 }
12296
12297 size_in_KMGT() {
12298     local value=$1
12299     local size=('K' 'M' 'G' 'T');
12300     local i=0
12301     local size_string=$value
12302
12303     while [ $value -ge 1024 ]; do
12304         if [ $i -gt 3 ]; then
12305             #T is the biggest unit we get here, if that is bigger,
12306             #just return XXXT
12307             size_string=${value}T
12308             break
12309         fi
12310         value=$((value >> 10))
12311         if [ $value -lt 1024 ]; then
12312             size_string=${value}${size[$i]}
12313             break
12314         fi
12315         i=$((i + 1))
12316     done
12317
12318     echo $size_string
12319 }
12320
12321 get_rename_size() {
12322         local size=$1
12323         local context=${2:-.}
12324         local sample=$(do_facet $SINGLEMDS $LCTL \
12325                 get_param mdt.$FSNAME-MDT0000.rename_stats |
12326                 grep -A1 $context |
12327                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
12328         echo $sample
12329 }
12330
12331 test_133d() {
12332         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12333         remote_ost_nodsh && skip "remote OST with nodsh"
12334         remote_mds_nodsh && skip "remote MDS with nodsh"
12335         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12336                 skip_env "MDS doesn't support rename stats"
12337
12338         local testdir1=$DIR/${tdir}/stats_testdir1
12339         local testdir2=$DIR/${tdir}/stats_testdir2
12340         mkdir -p $DIR/${tdir}
12341
12342         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12343
12344         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
12345         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
12346
12347         createmany -o $testdir1/test 512 || error "createmany failed"
12348
12349         # check samedir rename size
12350         mv ${testdir1}/test0 ${testdir1}/test_0
12351
12352         local testdir1_size=$(ls -l $DIR/${tdir} |
12353                 awk '/stats_testdir1/ {print $5}')
12354         local testdir2_size=$(ls -l $DIR/${tdir} |
12355                 awk '/stats_testdir2/ {print $5}')
12356
12357         testdir1_size=$(order_2 $testdir1_size)
12358         testdir2_size=$(order_2 $testdir2_size)
12359
12360         testdir1_size=$(size_in_KMGT $testdir1_size)
12361         testdir2_size=$(size_in_KMGT $testdir2_size)
12362
12363         echo "source rename dir size: ${testdir1_size}"
12364         echo "target rename dir size: ${testdir2_size}"
12365
12366         local cmd="do_facet $SINGLEMDS $LCTL "
12367         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
12368
12369         eval $cmd || error "$cmd failed"
12370         local samedir=$($cmd | grep 'same_dir')
12371         local same_sample=$(get_rename_size $testdir1_size)
12372         [ -z "$samedir" ] && error "samedir_rename_size count error"
12373         [[ $same_sample -eq 1 ]] ||
12374                 error "samedir_rename_size error $same_sample"
12375         echo "Check same dir rename stats success"
12376
12377         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12378
12379         # check crossdir rename size
12380         mv ${testdir1}/test_0 ${testdir2}/test_0
12381
12382         testdir1_size=$(ls -l $DIR/${tdir} |
12383                 awk '/stats_testdir1/ {print $5}')
12384         testdir2_size=$(ls -l $DIR/${tdir} |
12385                 awk '/stats_testdir2/ {print $5}')
12386
12387         testdir1_size=$(order_2 $testdir1_size)
12388         testdir2_size=$(order_2 $testdir2_size)
12389
12390         testdir1_size=$(size_in_KMGT $testdir1_size)
12391         testdir2_size=$(size_in_KMGT $testdir2_size)
12392
12393         echo "source rename dir size: ${testdir1_size}"
12394         echo "target rename dir size: ${testdir2_size}"
12395
12396         eval $cmd || error "$cmd failed"
12397         local crossdir=$($cmd | grep 'crossdir')
12398         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
12399         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
12400         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
12401         [[ $src_sample -eq 1 ]] ||
12402                 error "crossdir_rename_size error $src_sample"
12403         [[ $tgt_sample -eq 1 ]] ||
12404                 error "crossdir_rename_size error $tgt_sample"
12405         echo "Check cross dir rename stats success"
12406         rm -rf $DIR/${tdir}
12407 }
12408 run_test 133d "Verifying rename_stats ========================================"
12409
12410 test_133e() {
12411         remote_mds_nodsh && skip "remote MDS with nodsh"
12412         remote_ost_nodsh && skip "remote OST with nodsh"
12413         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12414
12415         local testdir=$DIR/${tdir}/stats_testdir
12416         local ctr f0 f1 bs=32768 count=42 sum
12417
12418         mkdir -p ${testdir} || error "mkdir failed"
12419
12420         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
12421
12422         for ctr in {write,read}_bytes; do
12423                 sync
12424                 cancel_lru_locks osc
12425
12426                 do_facet ost1 $LCTL set_param -n \
12427                         "obdfilter.*.exports.clear=clear"
12428
12429                 if [ $ctr = write_bytes ]; then
12430                         f0=/dev/zero
12431                         f1=${testdir}/${tfile}
12432                 else
12433                         f0=${testdir}/${tfile}
12434                         f1=/dev/null
12435                 fi
12436
12437                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
12438                         error "dd failed"
12439                 sync
12440                 cancel_lru_locks osc
12441
12442                 sum=$(do_facet ost1 $LCTL get_param \
12443                         "obdfilter.*.exports.*.stats" |
12444                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
12445                                 $1 == ctr { sum += $7 }
12446                                 END { printf("%0.0f", sum) }')
12447
12448                 if ((sum != bs * count)); then
12449                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
12450                 fi
12451         done
12452
12453         rm -rf $DIR/${tdir}
12454 }
12455 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
12456
12457 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
12458
12459 # Some versions of find (4.5.11, 4.5.14) included in CentOS 7.3-7.5 do
12460 # not honor the -ignore_readdir_race option correctly. So we call
12461 # error_ignore() rather than error() in these cases. See LU-11152.
12462 error_133() {
12463         if (find --version; do_facet mds1 find --version) |
12464                 grep -q '\b4\.5\.1[1-4]\b'; then
12465                 error_ignore LU-11152 "$@"
12466         else
12467                 error "$@"
12468         fi
12469 }
12470
12471 test_133f() {
12472         # First without trusting modes.
12473         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
12474         echo "proc_dirs='$proc_dirs'"
12475         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
12476         find $proc_dirs -exec cat '{}' \; &> /dev/null
12477
12478         # Second verifying readability.
12479         $LCTL get_param -R '*' &> /dev/null
12480
12481         # Verifing writability with badarea_io.
12482         find $proc_dirs \
12483                 -ignore_readdir_race \
12484                 -type f \
12485                 -not -name force_lbug \
12486                 -not -name changelog_mask \
12487                 -exec badarea_io '{}' \; ||
12488                         error_133 "find $proc_dirs failed"
12489 }
12490 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
12491
12492 test_133g() {
12493         remote_mds_nodsh && skip "remote MDS with nodsh"
12494         remote_ost_nodsh && skip "remote OST with nodsh"
12495
12496         # eventually, this can also be replaced with "lctl get_param -R",
12497         # but not until that option is always available on the server
12498         local facet
12499         for facet in mds1 ost1; do
12500                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
12501                         skip_noexit "Too old lustre on $facet"
12502                 local facet_proc_dirs=$(do_facet $facet \
12503                                         \\\ls -d $proc_regexp 2>/dev/null)
12504                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12505                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12506                 do_facet $facet find $facet_proc_dirs \
12507                         ! -name req_history \
12508                         -exec cat '{}' \\\; &> /dev/null
12509
12510                 do_facet $facet find $facet_proc_dirs \
12511                         ! -name req_history \
12512                         -type f \
12513                         -exec cat '{}' \\\; &> /dev/null ||
12514                                 error "proc file read failed"
12515
12516                 do_facet $facet find $facet_proc_dirs \
12517                         -ignore_readdir_race \
12518                         -type f \
12519                         -not -name force_lbug \
12520                         -not -name changelog_mask \
12521                         -exec badarea_io '{}' \\\; ||
12522                                 error_133 "$facet find $facet_proc_dirs failed"
12523         done
12524
12525         # remount the FS in case writes/reads /proc break the FS
12526         cleanup || error "failed to unmount"
12527         setup || error "failed to setup"
12528         true
12529 }
12530 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
12531
12532 test_133h() {
12533         remote_mds_nodsh && skip "remote MDS with nodsh"
12534         remote_ost_nodsh && skip "remote OST with nodsh"
12535         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
12536                 skip "Need MDS version at least 2.9.54"
12537
12538         local facet
12539
12540         for facet in client mds1 ost1; do
12541                 local facet_proc_dirs=$(do_facet $facet \
12542                                         \\\ls -d $proc_regexp 2> /dev/null)
12543                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12544                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12545                 # Get the list of files that are missing the terminating newline
12546                 local missing=($(do_facet $facet \
12547                         find ${facet_proc_dirs} -type f \|              \
12548                                 while read F\; do                       \
12549                                         awk -v FS='\v' -v RS='\v\v'     \
12550                                         "'END { if(NR>0 &&              \
12551                                         \\\$NF !~ /.*\\\n\$/)           \
12552                                                 print FILENAME}'"       \
12553                                         '\$F'\;                         \
12554                                 done 2>/dev/null))
12555                 [ ${#missing[*]} -eq 0 ] ||
12556                         error "files do not end with newline: ${missing[*]}"
12557         done
12558 }
12559 run_test 133h "Proc files should end with newlines"
12560
12561 test_134a() {
12562         remote_mds_nodsh && skip "remote MDS with nodsh"
12563         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12564                 skip "Need MDS version at least 2.7.54"
12565
12566         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12567         cancel_lru_locks mdc
12568
12569         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
12570         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12571         [ $unused -eq 0 ] || error "$unused locks are not cleared"
12572
12573         local nr=1000
12574         createmany -o $DIR/$tdir/f $nr ||
12575                 error "failed to create $nr files in $DIR/$tdir"
12576         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12577
12578         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
12579         do_facet mds1 $LCTL set_param fail_loc=0x327
12580         do_facet mds1 $LCTL set_param fail_val=500
12581         touch $DIR/$tdir/m
12582
12583         echo "sleep 10 seconds ..."
12584         sleep 10
12585         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
12586
12587         do_facet mds1 $LCTL set_param fail_loc=0
12588         do_facet mds1 $LCTL set_param fail_val=0
12589         [ $lck_cnt -lt $unused ] ||
12590                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
12591
12592         rm $DIR/$tdir/m
12593         unlinkmany $DIR/$tdir/f $nr
12594 }
12595 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
12596
12597 test_134b() {
12598         remote_mds_nodsh && skip "remote MDS with nodsh"
12599         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12600                 skip "Need MDS version at least 2.7.54"
12601
12602         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12603         cancel_lru_locks mdc
12604
12605         local low_wm=$(do_facet mds1 $LCTL get_param -n \
12606                         ldlm.lock_reclaim_threshold_mb)
12607         # disable reclaim temporarily
12608         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
12609
12610         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
12611         do_facet mds1 $LCTL set_param fail_loc=0x328
12612         do_facet mds1 $LCTL set_param fail_val=500
12613
12614         $LCTL set_param debug=+trace
12615
12616         local nr=600
12617         createmany -o $DIR/$tdir/f $nr &
12618         local create_pid=$!
12619
12620         echo "Sleep $TIMEOUT seconds ..."
12621         sleep $TIMEOUT
12622         if ! ps -p $create_pid  > /dev/null 2>&1; then
12623                 do_facet mds1 $LCTL set_param fail_loc=0
12624                 do_facet mds1 $LCTL set_param fail_val=0
12625                 do_facet mds1 $LCTL set_param \
12626                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
12627                 error "createmany finished incorrectly!"
12628         fi
12629         do_facet mds1 $LCTL set_param fail_loc=0
12630         do_facet mds1 $LCTL set_param fail_val=0
12631         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
12632         wait $create_pid || return 1
12633
12634         unlinkmany $DIR/$tdir/f $nr
12635 }
12636 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
12637
12638 test_135() {
12639         remote_mds_nodsh && skip "remote MDS with nodsh"
12640         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
12641                 skip "Need MDS version at least 2.13.50"
12642         local fname
12643
12644         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12645
12646 #define OBD_FAIL_PLAIN_RECORDS 0x1319
12647         #set only one record at plain llog
12648         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1319 fail_val=1
12649
12650         #fill already existed plain llog each 64767
12651         #wrapping whole catalog
12652         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
12653
12654         createmany -o $DIR/$tdir/$tfile_ 64700
12655         for (( i = 0; i < 64700; i = i + 2 ))
12656         do
12657                 rm $DIR/$tdir/$tfile_$i &
12658                 rm $DIR/$tdir/$tfile_$((i + 1)) &
12659                 local pid=$!
12660                 wait $pid
12661         done
12662
12663         #waiting osp synchronization
12664         wait_delete_completed
12665 }
12666 run_test 135 "Race catalog processing"
12667
12668 test_136() {
12669         remote_mds_nodsh && skip "remote MDS with nodsh"
12670         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
12671                 skip "Need MDS version at least 2.13.50"
12672         local fname
12673
12674         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12675         $SETSTRIPE -c 1 -i 0 $DIR/$tdir || error "failed to set striping"
12676         #set only one record at plain llog
12677 #define OBD_FAIL_CATALOG_FULL_CHECK                0x131a
12678         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x131a fail_val=1
12679
12680         #fill already existed 2 plain llogs each 64767
12681         #wrapping whole catalog
12682         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
12683         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 3 / 2))
12684         wait_delete_completed
12685
12686         createmany -o $DIR/$tdir/$tfile_ 10
12687         sleep 25
12688
12689         do_facet $SINGLEMDS $LCTL set_param fail_val=3
12690         for (( i = 0; i < 10; i = i + 3 ))
12691         do
12692                 rm $DIR/$tdir/$tfile_$i &
12693                 rm $DIR/$tdir/$tfile_$((i + 1)) &
12694                 local pid=$!
12695                 wait $pid
12696                 sleep 7
12697                 rm $DIR/$tdir/$tfile_$((i + 2)) &
12698         done
12699
12700         #waiting osp synchronization
12701         wait_delete_completed
12702 }
12703 run_test 136 "Race catalog processing 2"
12704
12705 test_140() { #bug-17379
12706         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12707
12708         test_mkdir $DIR/$tdir
12709         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
12710         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
12711
12712         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
12713         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
12714         local i=0
12715         while i=$((i + 1)); do
12716                 test_mkdir $i
12717                 cd $i || error "Changing to $i"
12718                 ln -s ../stat stat || error "Creating stat symlink"
12719                 # Read the symlink until ELOOP present,
12720                 # not LBUGing the system is considered success,
12721                 # we didn't overrun the stack.
12722                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
12723                 if [ $ret -ne 0 ]; then
12724                         if [ $ret -eq 40 ]; then
12725                                 break  # -ELOOP
12726                         else
12727                                 error "Open stat symlink"
12728                                         return
12729                         fi
12730                 fi
12731         done
12732         i=$((i - 1))
12733         echo "The symlink depth = $i"
12734         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
12735                 error "Invalid symlink depth"
12736
12737         # Test recursive symlink
12738         ln -s symlink_self symlink_self
12739         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
12740         echo "open symlink_self returns $ret"
12741         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
12742 }
12743 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
12744
12745 test_150() {
12746         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12747
12748         local TF="$TMP/$tfile"
12749
12750         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12751         cp $TF $DIR/$tfile
12752         cancel_lru_locks $OSC
12753         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
12754         remount_client $MOUNT
12755         df -P $MOUNT
12756         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
12757
12758         $TRUNCATE $TF 6000
12759         $TRUNCATE $DIR/$tfile 6000
12760         cancel_lru_locks $OSC
12761         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
12762
12763         echo "12345" >>$TF
12764         echo "12345" >>$DIR/$tfile
12765         cancel_lru_locks $OSC
12766         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
12767
12768         echo "12345" >>$TF
12769         echo "12345" >>$DIR/$tfile
12770         cancel_lru_locks $OSC
12771         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
12772
12773         rm -f $TF
12774         true
12775 }
12776 run_test 150 "truncate/append tests"
12777
12778 #LU-2902 roc_hit was not able to read all values from lproc
12779 function roc_hit_init() {
12780         local list=$(comma_list $(osts_nodes))
12781         local dir=$DIR/$tdir-check
12782         local file=$dir/$tfile
12783         local BEFORE
12784         local AFTER
12785         local idx
12786
12787         test_mkdir $dir
12788         #use setstripe to do a write to every ost
12789         for i in $(seq 0 $((OSTCOUNT-1))); do
12790                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
12791                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
12792                 idx=$(printf %04x $i)
12793                 BEFORE=$(get_osd_param $list *OST*$idx stats |
12794                         awk '$1 == "cache_access" {sum += $7}
12795                                 END { printf("%0.0f", sum) }')
12796
12797                 cancel_lru_locks osc
12798                 cat $file >/dev/null
12799
12800                 AFTER=$(get_osd_param $list *OST*$idx stats |
12801                         awk '$1 == "cache_access" {sum += $7}
12802                                 END { printf("%0.0f", sum) }')
12803
12804                 echo BEFORE:$BEFORE AFTER:$AFTER
12805                 if ! let "AFTER - BEFORE == 4"; then
12806                         rm -rf $dir
12807                         error "roc_hit is not safe to use"
12808                 fi
12809                 rm $file
12810         done
12811
12812         rm -rf $dir
12813 }
12814
12815 function roc_hit() {
12816         local list=$(comma_list $(osts_nodes))
12817         echo $(get_osd_param $list '' stats |
12818                 awk '$1 == "cache_hit" {sum += $7}
12819                         END { printf("%0.0f", sum) }')
12820 }
12821
12822 function set_cache() {
12823         local on=1
12824
12825         if [ "$2" == "off" ]; then
12826                 on=0;
12827         fi
12828         local list=$(comma_list $(osts_nodes))
12829         set_osd_param $list '' $1_cache_enable $on
12830
12831         cancel_lru_locks osc
12832 }
12833
12834 test_151() {
12835         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12836         remote_ost_nodsh && skip "remote OST with nodsh"
12837
12838         local CPAGES=3
12839         local list=$(comma_list $(osts_nodes))
12840
12841         # check whether obdfilter is cache capable at all
12842         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
12843                 skip "not cache-capable obdfilter"
12844         fi
12845
12846         # check cache is enabled on all obdfilters
12847         if get_osd_param $list '' read_cache_enable | grep 0; then
12848                 skip "oss cache is disabled"
12849         fi
12850
12851         set_osd_param $list '' writethrough_cache_enable 1
12852
12853         # check write cache is enabled on all obdfilters
12854         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
12855                 skip "oss write cache is NOT enabled"
12856         fi
12857
12858         roc_hit_init
12859
12860         #define OBD_FAIL_OBD_NO_LRU  0x609
12861         do_nodes $list $LCTL set_param fail_loc=0x609
12862
12863         # pages should be in the case right after write
12864         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
12865                 error "dd failed"
12866
12867         local BEFORE=$(roc_hit)
12868         cancel_lru_locks osc
12869         cat $DIR/$tfile >/dev/null
12870         local AFTER=$(roc_hit)
12871
12872         do_nodes $list $LCTL set_param fail_loc=0
12873
12874         if ! let "AFTER - BEFORE == CPAGES"; then
12875                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12876         fi
12877
12878         cancel_lru_locks osc
12879         # invalidates OST cache
12880         do_nodes $list "echo 1 > /proc/sys/vm/drop_caches"
12881         set_osd_param $list '' read_cache_enable 0
12882         cat $DIR/$tfile >/dev/null
12883
12884         # now data shouldn't be found in the cache
12885         BEFORE=$(roc_hit)
12886         cancel_lru_locks osc
12887         cat $DIR/$tfile >/dev/null
12888         AFTER=$(roc_hit)
12889         if let "AFTER - BEFORE != 0"; then
12890                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12891         fi
12892
12893         set_osd_param $list '' read_cache_enable 1
12894         rm -f $DIR/$tfile
12895 }
12896 run_test 151 "test cache on oss and controls ==============================="
12897
12898 test_152() {
12899         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12900
12901         local TF="$TMP/$tfile"
12902
12903         # simulate ENOMEM during write
12904 #define OBD_FAIL_OST_NOMEM      0x226
12905         lctl set_param fail_loc=0x80000226
12906         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12907         cp $TF $DIR/$tfile
12908         sync || error "sync failed"
12909         lctl set_param fail_loc=0
12910
12911         # discard client's cache
12912         cancel_lru_locks osc
12913
12914         # simulate ENOMEM during read
12915         lctl set_param fail_loc=0x80000226
12916         cmp $TF $DIR/$tfile || error "cmp failed"
12917         lctl set_param fail_loc=0
12918
12919         rm -f $TF
12920 }
12921 run_test 152 "test read/write with enomem ============================"
12922
12923 test_153() {
12924         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
12925 }
12926 run_test 153 "test if fdatasync does not crash ======================="
12927
12928 dot_lustre_fid_permission_check() {
12929         local fid=$1
12930         local ffid=$MOUNT/.lustre/fid/$fid
12931         local test_dir=$2
12932
12933         echo "stat fid $fid"
12934         stat $ffid > /dev/null || error "stat $ffid failed."
12935         echo "touch fid $fid"
12936         touch $ffid || error "touch $ffid failed."
12937         echo "write to fid $fid"
12938         cat /etc/hosts > $ffid || error "write $ffid failed."
12939         echo "read fid $fid"
12940         diff /etc/hosts $ffid || error "read $ffid failed."
12941         echo "append write to fid $fid"
12942         cat /etc/hosts >> $ffid || error "append write $ffid failed."
12943         echo "rename fid $fid"
12944         mv $ffid $test_dir/$tfile.1 &&
12945                 error "rename $ffid to $tfile.1 should fail."
12946         touch $test_dir/$tfile.1
12947         mv $test_dir/$tfile.1 $ffid &&
12948                 error "rename $tfile.1 to $ffid should fail."
12949         rm -f $test_dir/$tfile.1
12950         echo "truncate fid $fid"
12951         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
12952         echo "link fid $fid"
12953         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
12954         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
12955                 echo "setfacl fid $fid"
12956                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
12957                 echo "getfacl fid $fid"
12958                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
12959         fi
12960         echo "unlink fid $fid"
12961         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
12962         echo "mknod fid $fid"
12963         mknod $ffid c 1 3 && error "mknod $ffid should fail."
12964
12965         fid=[0xf00000400:0x1:0x0]
12966         ffid=$MOUNT/.lustre/fid/$fid
12967
12968         echo "stat non-exist fid $fid"
12969         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
12970         echo "write to non-exist fid $fid"
12971         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
12972         echo "link new fid $fid"
12973         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
12974
12975         mkdir -p $test_dir/$tdir
12976         touch $test_dir/$tdir/$tfile
12977         fid=$($LFS path2fid $test_dir/$tdir)
12978         rc=$?
12979         [ $rc -ne 0 ] &&
12980                 error "error: could not get fid for $test_dir/$dir/$tfile."
12981
12982         ffid=$MOUNT/.lustre/fid/$fid
12983
12984         echo "ls $fid"
12985         ls $ffid > /dev/null || error "ls $ffid failed."
12986         echo "touch $fid/$tfile.1"
12987         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
12988
12989         echo "touch $MOUNT/.lustre/fid/$tfile"
12990         touch $MOUNT/.lustre/fid/$tfile && \
12991                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
12992
12993         echo "setxattr to $MOUNT/.lustre/fid"
12994         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
12995
12996         echo "listxattr for $MOUNT/.lustre/fid"
12997         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
12998
12999         echo "delxattr from $MOUNT/.lustre/fid"
13000         setfattr -x trusted.name1 $MOUNT/.lustre/fid
13001
13002         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
13003         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
13004                 error "touch invalid fid should fail."
13005
13006         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
13007         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
13008                 error "touch non-normal fid should fail."
13009
13010         echo "rename $tdir to $MOUNT/.lustre/fid"
13011         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
13012                 error "rename to $MOUNT/.lustre/fid should fail."
13013
13014         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
13015         then            # LU-3547
13016                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
13017                 local new_obf_mode=777
13018
13019                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
13020                 chmod $new_obf_mode $DIR/.lustre/fid ||
13021                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
13022
13023                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
13024                 [ $obf_mode -eq $new_obf_mode ] ||
13025                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
13026
13027                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
13028                 chmod $old_obf_mode $DIR/.lustre/fid ||
13029                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
13030         fi
13031
13032         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
13033         fid=$($LFS path2fid $test_dir/$tfile-2)
13034
13035         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
13036         then # LU-5424
13037                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
13038                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
13039                         error "create lov data thru .lustre failed"
13040         fi
13041         echo "cp /etc/passwd $test_dir/$tfile-2"
13042         cp /etc/passwd $test_dir/$tfile-2 ||
13043                 error "copy to $test_dir/$tfile-2 failed."
13044         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
13045         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
13046                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
13047
13048         rm -rf $test_dir/tfile.lnk
13049         rm -rf $test_dir/$tfile-2
13050 }
13051
13052 test_154A() {
13053         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13054                 skip "Need MDS version at least 2.4.1"
13055
13056         local tf=$DIR/$tfile
13057         touch $tf
13058
13059         local fid=$($LFS path2fid $tf)
13060         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
13061
13062         # check that we get the same pathname back
13063         local found=$($LFS fid2path $MOUNT "$fid")
13064         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
13065         [ "$found" == "$tf" ] ||
13066                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
13067 }
13068 run_test 154A "lfs path2fid and fid2path basic checks"
13069
13070 test_154B() {
13071         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13072                 skip "Need MDS version at least 2.4.1"
13073
13074         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
13075         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
13076         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
13077         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
13078
13079         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
13080         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
13081
13082         # check that we get the same pathname
13083         echo "PFID: $PFID, name: $name"
13084         local FOUND=$($LFS fid2path $MOUNT "$PFID")
13085         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
13086         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
13087                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
13088
13089         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
13090 }
13091 run_test 154B "verify the ll_decode_linkea tool"
13092
13093 test_154a() {
13094         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13095         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13096         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
13097                 skip "Need MDS version at least 2.2.51"
13098         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
13099
13100         cp /etc/hosts $DIR/$tfile
13101
13102         fid=$($LFS path2fid $DIR/$tfile)
13103         rc=$?
13104         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
13105
13106         dot_lustre_fid_permission_check "$fid" $DIR ||
13107                 error "dot lustre permission check $fid failed"
13108
13109         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
13110
13111         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
13112
13113         touch $MOUNT/.lustre/file &&
13114                 error "creation is not allowed under .lustre"
13115
13116         mkdir $MOUNT/.lustre/dir &&
13117                 error "mkdir is not allowed under .lustre"
13118
13119         rm -rf $DIR/$tfile
13120 }
13121 run_test 154a "Open-by-FID"
13122
13123 test_154b() {
13124         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13125         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13126         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13127         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
13128                 skip "Need MDS version at least 2.2.51"
13129
13130         local remote_dir=$DIR/$tdir/remote_dir
13131         local MDTIDX=1
13132         local rc=0
13133
13134         mkdir -p $DIR/$tdir
13135         $LFS mkdir -i $MDTIDX $remote_dir ||
13136                 error "create remote directory failed"
13137
13138         cp /etc/hosts $remote_dir/$tfile
13139
13140         fid=$($LFS path2fid $remote_dir/$tfile)
13141         rc=$?
13142         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
13143
13144         dot_lustre_fid_permission_check "$fid" $remote_dir ||
13145                 error "dot lustre permission check $fid failed"
13146         rm -rf $DIR/$tdir
13147 }
13148 run_test 154b "Open-by-FID for remote directory"
13149
13150 test_154c() {
13151         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13152                 skip "Need MDS version at least 2.4.1"
13153
13154         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
13155         local FID1=$($LFS path2fid $DIR/$tfile.1)
13156         local FID2=$($LFS path2fid $DIR/$tfile.2)
13157         local FID3=$($LFS path2fid $DIR/$tfile.3)
13158
13159         local N=1
13160         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
13161                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
13162                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
13163                 local want=FID$N
13164                 [ "$FID" = "${!want}" ] ||
13165                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
13166                 N=$((N + 1))
13167         done
13168
13169         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
13170         do
13171                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
13172                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
13173                 N=$((N + 1))
13174         done
13175 }
13176 run_test 154c "lfs path2fid and fid2path multiple arguments"
13177
13178 test_154d() {
13179         remote_mds_nodsh && skip "remote MDS with nodsh"
13180         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
13181                 skip "Need MDS version at least 2.5.53"
13182
13183         if remote_mds; then
13184                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
13185         else
13186                 nid="0@lo"
13187         fi
13188         local proc_ofile="mdt.*.exports.'$nid'.open_files"
13189         local fd
13190         local cmd
13191
13192         rm -f $DIR/$tfile
13193         touch $DIR/$tfile
13194
13195         local fid=$($LFS path2fid $DIR/$tfile)
13196         # Open the file
13197         fd=$(free_fd)
13198         cmd="exec $fd<$DIR/$tfile"
13199         eval $cmd
13200         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
13201         echo "$fid_list" | grep "$fid"
13202         rc=$?
13203
13204         cmd="exec $fd>/dev/null"
13205         eval $cmd
13206         if [ $rc -ne 0 ]; then
13207                 error "FID $fid not found in open files list $fid_list"
13208         fi
13209 }
13210 run_test 154d "Verify open file fid"
13211
13212 test_154e()
13213 {
13214         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
13215                 skip "Need MDS version at least 2.6.50"
13216
13217         if ls -a $MOUNT | grep -q '^\.lustre$'; then
13218                 error ".lustre returned by readdir"
13219         fi
13220 }
13221 run_test 154e ".lustre is not returned by readdir"
13222
13223 test_154f() {
13224         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13225
13226         # create parent directory on a single MDT to avoid cross-MDT hardlinks
13227         test_mkdir -p -c1 $DIR/$tdir/d
13228         # test dirs inherit from its stripe
13229         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
13230         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
13231         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
13232         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
13233         touch $DIR/f
13234
13235         # get fid of parents
13236         local FID0=$($LFS path2fid $DIR/$tdir/d)
13237         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
13238         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
13239         local FID3=$($LFS path2fid $DIR)
13240
13241         # check that path2fid --parents returns expected <parent_fid>/name
13242         # 1) test for a directory (single parent)
13243         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
13244         [ "$parent" == "$FID0/foo1" ] ||
13245                 error "expected parent: $FID0/foo1, got: $parent"
13246
13247         # 2) test for a file with nlink > 1 (multiple parents)
13248         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
13249         echo "$parent" | grep -F "$FID1/$tfile" ||
13250                 error "$FID1/$tfile not returned in parent list"
13251         echo "$parent" | grep -F "$FID2/link" ||
13252                 error "$FID2/link not returned in parent list"
13253
13254         # 3) get parent by fid
13255         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
13256         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13257         echo "$parent" | grep -F "$FID1/$tfile" ||
13258                 error "$FID1/$tfile not returned in parent list (by fid)"
13259         echo "$parent" | grep -F "$FID2/link" ||
13260                 error "$FID2/link not returned in parent list (by fid)"
13261
13262         # 4) test for entry in root directory
13263         parent=$($LFS path2fid --parents $DIR/f)
13264         echo "$parent" | grep -F "$FID3/f" ||
13265                 error "$FID3/f not returned in parent list"
13266
13267         # 5) test it on root directory
13268         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
13269                 error "$MOUNT should not have parents"
13270
13271         # enable xattr caching and check that linkea is correctly updated
13272         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
13273         save_lustre_params client "llite.*.xattr_cache" > $save
13274         lctl set_param llite.*.xattr_cache 1
13275
13276         # 6.1) linkea update on rename
13277         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
13278
13279         # get parents by fid
13280         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13281         # foo1 should no longer be returned in parent list
13282         echo "$parent" | grep -F "$FID1" &&
13283                 error "$FID1 should no longer be in parent list"
13284         # the new path should appear
13285         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
13286                 error "$FID2/$tfile.moved is not in parent list"
13287
13288         # 6.2) linkea update on unlink
13289         rm -f $DIR/$tdir/d/foo2/link
13290         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13291         # foo2/link should no longer be returned in parent list
13292         echo "$parent" | grep -F "$FID2/link" &&
13293                 error "$FID2/link should no longer be in parent list"
13294         true
13295
13296         rm -f $DIR/f
13297         restore_lustre_params < $save
13298         rm -f $save
13299 }
13300 run_test 154f "get parent fids by reading link ea"
13301
13302 test_154g()
13303 {
13304         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13305         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
13306            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
13307                 skip "Need MDS version at least 2.6.92"
13308
13309         mkdir -p $DIR/$tdir
13310         llapi_fid_test -d $DIR/$tdir
13311 }
13312 run_test 154g "various llapi FID tests"
13313
13314 test_155_small_load() {
13315     local temp=$TMP/$tfile
13316     local file=$DIR/$tfile
13317
13318     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
13319         error "dd of=$temp bs=6096 count=1 failed"
13320     cp $temp $file
13321     cancel_lru_locks $OSC
13322     cmp $temp $file || error "$temp $file differ"
13323
13324     $TRUNCATE $temp 6000
13325     $TRUNCATE $file 6000
13326     cmp $temp $file || error "$temp $file differ (truncate1)"
13327
13328     echo "12345" >>$temp
13329     echo "12345" >>$file
13330     cmp $temp $file || error "$temp $file differ (append1)"
13331
13332     echo "12345" >>$temp
13333     echo "12345" >>$file
13334     cmp $temp $file || error "$temp $file differ (append2)"
13335
13336     rm -f $temp $file
13337     true
13338 }
13339
13340 test_155_big_load() {
13341         remote_ost_nodsh && skip "remote OST with nodsh"
13342
13343         local temp=$TMP/$tfile
13344         local file=$DIR/$tfile
13345
13346         free_min_max
13347         local cache_size=$(do_facet ost$((MAXI+1)) \
13348                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
13349         local large_file_size=$((cache_size * 2))
13350
13351         echo "OSS cache size: $cache_size KB"
13352         echo "Large file size: $large_file_size KB"
13353
13354         [ $MAXV -le $large_file_size ] &&
13355                 skip_env "max available OST size needs > $large_file_size KB"
13356
13357         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
13358
13359         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
13360                 error "dd of=$temp bs=$large_file_size count=1k failed"
13361         cp $temp $file
13362         ls -lh $temp $file
13363         cancel_lru_locks osc
13364         cmp $temp $file || error "$temp $file differ"
13365
13366         rm -f $temp $file
13367         true
13368 }
13369
13370 save_writethrough() {
13371         local facets=$(get_facets OST)
13372
13373         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
13374 }
13375
13376 test_155a() {
13377         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13378
13379         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13380
13381         save_writethrough $p
13382
13383         set_cache read on
13384         set_cache writethrough on
13385         test_155_small_load
13386         restore_lustre_params < $p
13387         rm -f $p
13388 }
13389 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
13390
13391 test_155b() {
13392         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13393
13394         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13395
13396         save_writethrough $p
13397
13398         set_cache read on
13399         set_cache writethrough off
13400         test_155_small_load
13401         restore_lustre_params < $p
13402         rm -f $p
13403 }
13404 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
13405
13406 test_155c() {
13407         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13408
13409         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13410
13411         save_writethrough $p
13412
13413         set_cache read off
13414         set_cache writethrough on
13415         test_155_small_load
13416         restore_lustre_params < $p
13417         rm -f $p
13418 }
13419 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
13420
13421 test_155d() {
13422         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13423
13424         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13425
13426         save_writethrough $p
13427
13428         set_cache read off
13429         set_cache writethrough off
13430         test_155_small_load
13431         restore_lustre_params < $p
13432         rm -f $p
13433 }
13434 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
13435
13436 test_155e() {
13437         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13438
13439         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13440
13441         save_writethrough $p
13442
13443         set_cache read on
13444         set_cache writethrough on
13445         test_155_big_load
13446         restore_lustre_params < $p
13447         rm -f $p
13448 }
13449 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
13450
13451 test_155f() {
13452         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13453
13454         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13455
13456         save_writethrough $p
13457
13458         set_cache read on
13459         set_cache writethrough off
13460         test_155_big_load
13461         restore_lustre_params < $p
13462         rm -f $p
13463 }
13464 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
13465
13466 test_155g() {
13467         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13468
13469         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13470
13471         save_writethrough $p
13472
13473         set_cache read off
13474         set_cache writethrough on
13475         test_155_big_load
13476         restore_lustre_params < $p
13477         rm -f $p
13478 }
13479 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
13480
13481 test_155h() {
13482         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13483
13484         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13485
13486         save_writethrough $p
13487
13488         set_cache read off
13489         set_cache writethrough off
13490         test_155_big_load
13491         restore_lustre_params < $p
13492         rm -f $p
13493 }
13494 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
13495
13496 test_156() {
13497         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13498         remote_ost_nodsh && skip "remote OST with nodsh"
13499         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
13500                 skip "stats not implemented on old servers"
13501         [ "$ost1_FSTYPE" = "zfs" ] &&
13502                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
13503
13504         local CPAGES=3
13505         local BEFORE
13506         local AFTER
13507         local file="$DIR/$tfile"
13508         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13509
13510         save_writethrough $p
13511         roc_hit_init
13512
13513         log "Turn on read and write cache"
13514         set_cache read on
13515         set_cache writethrough on
13516
13517         log "Write data and read it back."
13518         log "Read should be satisfied from the cache."
13519         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13520         BEFORE=$(roc_hit)
13521         cancel_lru_locks osc
13522         cat $file >/dev/null
13523         AFTER=$(roc_hit)
13524         if ! let "AFTER - BEFORE == CPAGES"; then
13525                 error "NOT IN CACHE (2): before: $BEFORE, after: $AFTER"
13526         else
13527                 log "cache hits: before: $BEFORE, after: $AFTER"
13528         fi
13529
13530         log "Read again; it should be satisfied from the cache."
13531         BEFORE=$AFTER
13532         cancel_lru_locks osc
13533         cat $file >/dev/null
13534         AFTER=$(roc_hit)
13535         if ! let "AFTER - BEFORE == CPAGES"; then
13536                 error "NOT IN CACHE (3): before: $BEFORE, after: $AFTER"
13537         else
13538                 log "cache hits:: before: $BEFORE, after: $AFTER"
13539         fi
13540
13541         log "Turn off the read cache and turn on the write cache"
13542         set_cache read off
13543         set_cache writethrough on
13544
13545         log "Read again; it should be satisfied from the cache."
13546         BEFORE=$(roc_hit)
13547         cancel_lru_locks osc
13548         cat $file >/dev/null
13549         AFTER=$(roc_hit)
13550         if ! let "AFTER - BEFORE == CPAGES"; then
13551                 error "NOT IN CACHE (4): before: $BEFORE, after: $AFTER"
13552         else
13553                 log "cache hits:: before: $BEFORE, after: $AFTER"
13554         fi
13555
13556         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
13557                 # > 2.12.56 uses pagecache if cached
13558                 log "Read again; it should not be satisfied from the cache."
13559                 BEFORE=$AFTER
13560                 cancel_lru_locks osc
13561                 cat $file >/dev/null
13562                 AFTER=$(roc_hit)
13563                 if ! let "AFTER - BEFORE == 0"; then
13564                         error "IN CACHE (5): before: $BEFORE, after: $AFTER"
13565                 else
13566                         log "cache hits:: before: $BEFORE, after: $AFTER"
13567                 fi
13568         fi
13569
13570         log "Write data and read it back."
13571         log "Read should be satisfied from the cache."
13572         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13573         BEFORE=$(roc_hit)
13574         cancel_lru_locks osc
13575         cat $file >/dev/null
13576         AFTER=$(roc_hit)
13577         if ! let "AFTER - BEFORE == CPAGES"; then
13578                 error "NOT IN CACHE (6): before: $BEFORE, after: $AFTER"
13579         else
13580                 log "cache hits:: before: $BEFORE, after: $AFTER"
13581         fi
13582
13583         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
13584                 # > 2.12.56 uses pagecache if cached
13585                 log "Read again; it should not be satisfied from the cache."
13586                 BEFORE=$AFTER
13587                 cancel_lru_locks osc
13588                 cat $file >/dev/null
13589                 AFTER=$(roc_hit)
13590                 if ! let "AFTER - BEFORE == 0"; then
13591                         error "IN CACHE (7): before: $BEFORE, after: $AFTER"
13592                 else
13593                         log "cache hits:: before: $BEFORE, after: $AFTER"
13594                 fi
13595         fi
13596
13597         log "Turn off read and write cache"
13598         set_cache read off
13599         set_cache writethrough off
13600
13601         log "Write data and read it back"
13602         log "It should not be satisfied from the cache."
13603         rm -f $file
13604         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13605         cancel_lru_locks osc
13606         BEFORE=$(roc_hit)
13607         cat $file >/dev/null
13608         AFTER=$(roc_hit)
13609         if ! let "AFTER - BEFORE == 0"; then
13610                 error_ignore bz20762 "IN CACHE (8):before:$BEFORE,after:$AFTER"
13611         else
13612                 log "cache hits:: before: $BEFORE, after: $AFTER"
13613         fi
13614
13615         log "Turn on the read cache and turn off the write cache"
13616         set_cache read on
13617         set_cache writethrough off
13618
13619         log "Write data and read it back"
13620         log "It should not be satisfied from the cache."
13621         rm -f $file
13622         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13623         BEFORE=$(roc_hit)
13624         cancel_lru_locks osc
13625         cat $file >/dev/null
13626         AFTER=$(roc_hit)
13627         if ! let "AFTER - BEFORE == 0"; then
13628                 error_ignore bz20762 "IN CACHE (9):before:$BEFORE,after:$AFTER"
13629         else
13630                 log "cache hits:: before: $BEFORE, after: $AFTER"
13631         fi
13632
13633         log "Read again; it should be satisfied from the cache."
13634         BEFORE=$(roc_hit)
13635         cancel_lru_locks osc
13636         cat $file >/dev/null
13637         AFTER=$(roc_hit)
13638         if ! let "AFTER - BEFORE == CPAGES"; then
13639                 error "NOT IN CACHE (1): before: $BEFORE, after: $AFTER"
13640         else
13641                 log "cache hits:: before: $BEFORE, after: $AFTER"
13642         fi
13643
13644         restore_lustre_params < $p
13645         rm -f $p $file
13646 }
13647 run_test 156 "Verification of tunables"
13648
13649 test_160a() {
13650         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13651         remote_mds_nodsh && skip "remote MDS with nodsh"
13652         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13653                 skip "Need MDS version at least 2.2.0"
13654
13655         changelog_register || error "changelog_register failed"
13656         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13657         changelog_users $SINGLEMDS | grep -q $cl_user ||
13658                 error "User $cl_user not found in changelog_users"
13659
13660         # change something
13661         test_mkdir -p $DIR/$tdir/pics/2008/zachy
13662         changelog_clear 0 || error "changelog_clear failed"
13663         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
13664         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
13665         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
13666         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
13667         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
13668         rm $DIR/$tdir/pics/desktop.jpg
13669
13670         changelog_dump | tail -10
13671
13672         echo "verifying changelog mask"
13673         changelog_chmask "-MKDIR"
13674         changelog_chmask "-CLOSE"
13675
13676         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
13677         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
13678
13679         changelog_chmask "+MKDIR"
13680         changelog_chmask "+CLOSE"
13681
13682         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
13683         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
13684
13685         changelog_dump | tail -10
13686         MKDIRS=$(changelog_dump | grep -c "MKDIR")
13687         CLOSES=$(changelog_dump | grep -c "CLOSE")
13688         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
13689         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
13690
13691         # verify contents
13692         echo "verifying target fid"
13693         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
13694         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
13695         [ "$fidc" == "$fidf" ] ||
13696                 error "changelog '$tfile' fid $fidc != file fid $fidf"
13697         echo "verifying parent fid"
13698         # The FID returned from the Changelog may be the directory shard on
13699         # a different MDT, and not the FID returned by path2fid on the parent.
13700         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
13701         # since this is what will matter when recreating this file in the tree.
13702         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
13703         local pathp=$($LFS fid2path $MOUNT "$fidp")
13704         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
13705                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
13706
13707         echo "getting records for $cl_user"
13708         changelog_users $SINGLEMDS
13709         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
13710         local nclr=3
13711         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
13712                 error "changelog_clear failed"
13713         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
13714         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
13715         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
13716                 error "user index expect $user_rec1 + $nclr != $user_rec2"
13717
13718         local min0_rec=$(changelog_users $SINGLEMDS |
13719                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
13720         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
13721                           awk '{ print $1; exit; }')
13722
13723         changelog_dump | tail -n 5
13724         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
13725         [ $first_rec == $((min0_rec + 1)) ] ||
13726                 error "first index should be $min0_rec + 1 not $first_rec"
13727
13728         # LU-3446 changelog index reset on MDT restart
13729         local cur_rec1=$(changelog_users $SINGLEMDS |
13730                          awk '/^current.index:/ { print $NF }')
13731         changelog_clear 0 ||
13732                 error "clear all changelog records for $cl_user failed"
13733         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
13734         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
13735                 error "Fail to start $SINGLEMDS"
13736         local cur_rec2=$(changelog_users $SINGLEMDS |
13737                          awk '/^current.index:/ { print $NF }')
13738         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
13739         [ $cur_rec1 == $cur_rec2 ] ||
13740                 error "current index should be $cur_rec1 not $cur_rec2"
13741
13742         echo "verifying users from this test are deregistered"
13743         changelog_deregister || error "changelog_deregister failed"
13744         changelog_users $SINGLEMDS | grep -q $cl_user &&
13745                 error "User '$cl_user' still in changelog_users"
13746
13747         # lctl get_param -n mdd.*.changelog_users
13748         # current index: 144
13749         # ID    index (idle seconds)
13750         # cl3   144 (2)
13751         if ! changelog_users $SINGLEMDS | grep "^cl"; then
13752                 # this is the normal case where all users were deregistered
13753                 # make sure no new records are added when no users are present
13754                 local last_rec1=$(changelog_users $SINGLEMDS |
13755                                   awk '/^current.index:/ { print $NF }')
13756                 touch $DIR/$tdir/chloe
13757                 local last_rec2=$(changelog_users $SINGLEMDS |
13758                                   awk '/^current.index:/ { print $NF }')
13759                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
13760                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
13761         else
13762                 # any changelog users must be leftovers from a previous test
13763                 changelog_users $SINGLEMDS
13764                 echo "other changelog users; can't verify off"
13765         fi
13766 }
13767 run_test 160a "changelog sanity"
13768
13769 test_160b() { # LU-3587
13770         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13771         remote_mds_nodsh && skip "remote MDS with nodsh"
13772         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13773                 skip "Need MDS version at least 2.2.0"
13774
13775         changelog_register || error "changelog_register failed"
13776         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13777         changelog_users $SINGLEMDS | grep -q $cl_user ||
13778                 error "User '$cl_user' not found in changelog_users"
13779
13780         local longname1=$(str_repeat a 255)
13781         local longname2=$(str_repeat b 255)
13782
13783         cd $DIR
13784         echo "creating very long named file"
13785         touch $longname1 || error "create of '$longname1' failed"
13786         echo "renaming very long named file"
13787         mv $longname1 $longname2
13788
13789         changelog_dump | grep RENME | tail -n 5
13790         rm -f $longname2
13791 }
13792 run_test 160b "Verify that very long rename doesn't crash in changelog"
13793
13794 test_160c() {
13795         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13796         remote_mds_nodsh && skip "remote MDS with nodsh"
13797
13798         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
13799                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
13800                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
13801                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
13802
13803         local rc=0
13804
13805         # Registration step
13806         changelog_register || error "changelog_register failed"
13807
13808         rm -rf $DIR/$tdir
13809         mkdir -p $DIR/$tdir
13810         $MCREATE $DIR/$tdir/foo_160c
13811         changelog_chmask "-TRUNC"
13812         $TRUNCATE $DIR/$tdir/foo_160c 200
13813         changelog_chmask "+TRUNC"
13814         $TRUNCATE $DIR/$tdir/foo_160c 199
13815         changelog_dump | tail -n 5
13816         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
13817         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
13818 }
13819 run_test 160c "verify that changelog log catch the truncate event"
13820
13821 test_160d() {
13822         remote_mds_nodsh && skip "remote MDS with nodsh"
13823         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13824         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13825         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
13826                 skip "Need MDS version at least 2.7.60"
13827
13828         # Registration step
13829         changelog_register || error "changelog_register failed"
13830
13831         mkdir -p $DIR/$tdir/migrate_dir
13832         changelog_clear 0 || error "changelog_clear failed"
13833
13834         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
13835         changelog_dump | tail -n 5
13836         local migrates=$(changelog_dump | grep -c "MIGRT")
13837         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
13838 }
13839 run_test 160d "verify that changelog log catch the migrate event"
13840
13841 test_160e() {
13842         remote_mds_nodsh && skip "remote MDS with nodsh"
13843
13844         # Create a user
13845         changelog_register || error "changelog_register failed"
13846
13847         # Delete a future user (expect fail)
13848         local MDT0=$(facet_svc $SINGLEMDS)
13849         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
13850         local rc=$?
13851
13852         if [ $rc -eq 0 ]; then
13853                 error "Deleted non-existant user cl77"
13854         elif [ $rc -ne 2 ]; then
13855                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
13856         fi
13857
13858         # Clear to a bad index (1 billion should be safe)
13859         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
13860         rc=$?
13861
13862         if [ $rc -eq 0 ]; then
13863                 error "Successfully cleared to invalid CL index"
13864         elif [ $rc -ne 22 ]; then
13865                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
13866         fi
13867 }
13868 run_test 160e "changelog negative testing (should return errors)"
13869
13870 test_160f() {
13871         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13872         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13873                 skip "Need MDS version at least 2.10.56"
13874
13875         local mdts=$(comma_list $(mdts_nodes))
13876
13877         # Create a user
13878         changelog_register || error "first changelog_register failed"
13879         changelog_register || error "second changelog_register failed"
13880         local cl_users
13881         declare -A cl_user1
13882         declare -A cl_user2
13883         local user_rec1
13884         local user_rec2
13885         local i
13886
13887         # generate some changelog records to accumulate on each MDT
13888         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
13889         log "$(date +%s): creating first files"
13890         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13891                 error "create $DIR/$tdir/$tfile failed"
13892
13893         # check changelogs have been generated
13894         local start=$SECONDS
13895         local idle_time=$((MDSCOUNT * 5 + 5))
13896         local nbcl=$(changelog_dump | wc -l)
13897         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13898
13899         for param in "changelog_max_idle_time=$idle_time" \
13900                      "changelog_gc=1" \
13901                      "changelog_min_gc_interval=2" \
13902                      "changelog_min_free_cat_entries=3"; do
13903                 local MDT0=$(facet_svc $SINGLEMDS)
13904                 local var="${param%=*}"
13905                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13906
13907                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13908                 do_nodes $mdts $LCTL set_param mdd.*.$param
13909         done
13910
13911         # force cl_user2 to be idle (1st part), but also cancel the
13912         # cl_user1 records so that it is not evicted later in the test.
13913         local sleep1=$((idle_time / 2))
13914         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
13915         sleep $sleep1
13916
13917         # simulate changelog catalog almost full
13918         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
13919         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13920
13921         for i in $(seq $MDSCOUNT); do
13922                 cl_users=(${CL_USERS[mds$i]})
13923                 cl_user1[mds$i]="${cl_users[0]}"
13924                 cl_user2[mds$i]="${cl_users[1]}"
13925
13926                 [ -n "${cl_user1[mds$i]}" ] ||
13927                         error "mds$i: no user registered"
13928                 [ -n "${cl_user2[mds$i]}" ] ||
13929                         error "mds$i: only ${cl_user2[mds$i]} is registered"
13930
13931                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13932                 [ -n "$user_rec1" ] ||
13933                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13934                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13935                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13936                 [ -n "$user_rec2" ] ||
13937                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13938                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13939                      "$user_rec1 + 2 == $user_rec2"
13940                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13941                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13942                               "$user_rec1 + 2, but is $user_rec2"
13943                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13944                 [ -n "$user_rec2" ] ||
13945                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13946                 [ $user_rec1 == $user_rec2 ] ||
13947                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13948                               "$user_rec1, but is $user_rec2"
13949         done
13950
13951         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
13952         local sleep2=$((idle_time - (SECONDS - start) + 1))
13953         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
13954         sleep $sleep2
13955
13956         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
13957         # cl_user1 should be OK because it recently processed records.
13958         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
13959         createmany -m $DIR/$tdir/${tfile}b $((MDSCOUNT * 2)) ||
13960                 error "create $DIR/$tdir/${tfile}b failed"
13961
13962         # ensure gc thread is done
13963         for i in $(mdts_nodes); do
13964                 wait_update $i \
13965                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13966                         error "$i: GC-thread not done"
13967         done
13968
13969         local first_rec
13970         for i in $(seq $MDSCOUNT); do
13971                 # check cl_user1 still registered
13972                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13973                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13974                 # check cl_user2 unregistered
13975                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13976                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13977
13978                 # check changelogs are present and starting at $user_rec1 + 1
13979                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13980                 [ -n "$user_rec1" ] ||
13981                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13982                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13983                             awk '{ print $1; exit; }')
13984
13985                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13986                 [ $((user_rec1 + 1)) == $first_rec ] ||
13987                         error "mds$i: first index should be $user_rec1 + 1, " \
13988                               "but is $first_rec"
13989         done
13990 }
13991 run_test 160f "changelog garbage collect (timestamped users)"
13992
13993 test_160g() {
13994         remote_mds_nodsh && skip "remote MDS with nodsh"
13995         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13996                 skip "Need MDS version at least 2.10.56"
13997
13998         local mdts=$(comma_list $(mdts_nodes))
13999
14000         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
14001         do_nodes $mdts $LCTL set_param fail_loc=0x1314
14002
14003         # Create a user
14004         changelog_register || error "first changelog_register failed"
14005         changelog_register || error "second changelog_register failed"
14006         local cl_users
14007         declare -A cl_user1
14008         declare -A cl_user2
14009         local user_rec1
14010         local user_rec2
14011         local i
14012
14013         # generate some changelog records to accumulate on each MDT
14014         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
14015         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14016                 error "create $DIR/$tdir/$tfile failed"
14017
14018         # check changelogs have been generated
14019         local nbcl=$(changelog_dump | wc -l)
14020         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14021
14022         # reduce the max_idle_indexes value to make sure we exceed it
14023         max_ndx=$((nbcl / 2 - 1))
14024
14025         for param in "changelog_max_idle_indexes=$max_ndx" \
14026                      "changelog_gc=1" \
14027                      "changelog_min_gc_interval=2" \
14028                      "changelog_min_free_cat_entries=3"; do
14029                 local MDT0=$(facet_svc $SINGLEMDS)
14030                 local var="${param%=*}"
14031                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14032
14033                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14034                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
14035                         error "unable to set mdd.*.$param"
14036         done
14037
14038         # simulate changelog catalog almost full
14039         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
14040         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
14041
14042         for i in $(seq $MDSCOUNT); do
14043                 cl_users=(${CL_USERS[mds$i]})
14044                 cl_user1[mds$i]="${cl_users[0]}"
14045                 cl_user2[mds$i]="${cl_users[1]}"
14046
14047                 [ -n "${cl_user1[mds$i]}" ] ||
14048                         error "mds$i: no user registered"
14049                 [ -n "${cl_user2[mds$i]}" ] ||
14050                         error "mds$i: only ${cl_user1[mds$i]} is registered"
14051
14052                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14053                 [ -n "$user_rec1" ] ||
14054                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14055                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14056                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14057                 [ -n "$user_rec2" ] ||
14058                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14059                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14060                      "$user_rec1 + 2 == $user_rec2"
14061                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14062                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14063                               "$user_rec1 + 2, but is $user_rec2"
14064                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14065                 [ -n "$user_rec2" ] ||
14066                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14067                 [ $user_rec1 == $user_rec2 ] ||
14068                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14069                               "$user_rec1, but is $user_rec2"
14070         done
14071
14072         # ensure we are past the previous changelog_min_gc_interval set above
14073         sleep 2
14074
14075         # generate one more changelog to trigger fail_loc
14076         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14077                 error "create $DIR/$tdir/${tfile}bis failed"
14078
14079         # ensure gc thread is done
14080         for i in $(mdts_nodes); do
14081                 wait_update $i \
14082                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
14083                         error "$i: GC-thread not done"
14084         done
14085
14086         local first_rec
14087         for i in $(seq $MDSCOUNT); do
14088                 # check cl_user1 still registered
14089                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14090                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14091                 # check cl_user2 unregistered
14092                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14093                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14094
14095                 # check changelogs are present and starting at $user_rec1 + 1
14096                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14097                 [ -n "$user_rec1" ] ||
14098                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14099                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14100                             awk '{ print $1; exit; }')
14101
14102                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14103                 [ $((user_rec1 + 1)) == $first_rec ] ||
14104                         error "mds$i: first index should be $user_rec1 + 1, " \
14105                               "but is $first_rec"
14106         done
14107 }
14108 run_test 160g "changelog garbage collect (old users)"
14109
14110 test_160h() {
14111         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14112         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14113                 skip "Need MDS version at least 2.10.56"
14114
14115         local mdts=$(comma_list $(mdts_nodes))
14116
14117         # Create a user
14118         changelog_register || error "first changelog_register failed"
14119         changelog_register || error "second changelog_register failed"
14120         local cl_users
14121         declare -A cl_user1
14122         declare -A cl_user2
14123         local user_rec1
14124         local user_rec2
14125         local i
14126
14127         # generate some changelog records to accumulate on each MDT
14128         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
14129         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14130                 error "create $DIR/$tdir/$tfile failed"
14131
14132         # check changelogs have been generated
14133         local nbcl=$(changelog_dump | wc -l)
14134         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14135
14136         for param in "changelog_max_idle_time=10" \
14137                      "changelog_gc=1" \
14138                      "changelog_min_gc_interval=2"; do
14139                 local MDT0=$(facet_svc $SINGLEMDS)
14140                 local var="${param%=*}"
14141                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14142
14143                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14144                 do_nodes $mdts $LCTL set_param mdd.*.$param
14145         done
14146
14147         # force cl_user2 to be idle (1st part)
14148         sleep 9
14149
14150         for i in $(seq $MDSCOUNT); do
14151                 cl_users=(${CL_USERS[mds$i]})
14152                 cl_user1[mds$i]="${cl_users[0]}"
14153                 cl_user2[mds$i]="${cl_users[1]}"
14154
14155                 [ -n "${cl_user1[mds$i]}" ] ||
14156                         error "mds$i: no user registered"
14157                 [ -n "${cl_user2[mds$i]}" ] ||
14158                         error "mds$i: only ${cl_user2[mds$i]} is registered"
14159
14160                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14161                 [ -n "$user_rec1" ] ||
14162                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14163                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14164                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14165                 [ -n "$user_rec2" ] ||
14166                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14167                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14168                      "$user_rec1 + 2 == $user_rec2"
14169                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14170                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14171                               "$user_rec1 + 2, but is $user_rec2"
14172                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14173                 [ -n "$user_rec2" ] ||
14174                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14175                 [ $user_rec1 == $user_rec2 ] ||
14176                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14177                               "$user_rec1, but is $user_rec2"
14178         done
14179
14180         # force cl_user2 to be idle (2nd part) and to reach
14181         # changelog_max_idle_time
14182         sleep 2
14183
14184         # force each GC-thread start and block then
14185         # one per MDT/MDD, set fail_val accordingly
14186         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
14187         do_nodes $mdts $LCTL set_param fail_loc=0x1316
14188
14189         # generate more changelogs to trigger fail_loc
14190         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14191                 error "create $DIR/$tdir/${tfile}bis failed"
14192
14193         # stop MDT to stop GC-thread, should be done in back-ground as it will
14194         # block waiting for the thread to be released and exit
14195         declare -A stop_pids
14196         for i in $(seq $MDSCOUNT); do
14197                 stop mds$i &
14198                 stop_pids[mds$i]=$!
14199         done
14200
14201         for i in $(mdts_nodes); do
14202                 local facet
14203                 local nb=0
14204                 local facets=$(facets_up_on_host $i)
14205
14206                 for facet in ${facets//,/ }; do
14207                         if [[ $facet == mds* ]]; then
14208                                 nb=$((nb + 1))
14209                         fi
14210                 done
14211                 # ensure each MDS's gc threads are still present and all in "R"
14212                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
14213                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
14214                         error "$i: expected $nb GC-thread"
14215                 wait_update $i \
14216                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
14217                         "R" 20 ||
14218                         error "$i: GC-thread not found in R-state"
14219                 # check umounts of each MDT on MDS have reached kthread_stop()
14220                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
14221                         error "$i: expected $nb umount"
14222                 wait_update $i \
14223                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
14224                         error "$i: umount not found in D-state"
14225         done
14226
14227         # release all GC-threads
14228         do_nodes $mdts $LCTL set_param fail_loc=0
14229
14230         # wait for MDT stop to complete
14231         for i in $(seq $MDSCOUNT); do
14232                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
14233         done
14234
14235         # XXX
14236         # may try to check if any orphan changelog records are present
14237         # via ldiskfs/zfs and llog_reader...
14238
14239         # re-start/mount MDTs
14240         for i in $(seq $MDSCOUNT); do
14241                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
14242                         error "Fail to start mds$i"
14243         done
14244
14245         local first_rec
14246         for i in $(seq $MDSCOUNT); do
14247                 # check cl_user1 still registered
14248                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14249                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14250                 # check cl_user2 unregistered
14251                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14252                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14253
14254                 # check changelogs are present and starting at $user_rec1 + 1
14255                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14256                 [ -n "$user_rec1" ] ||
14257                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14258                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14259                             awk '{ print $1; exit; }')
14260
14261                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14262                 [ $((user_rec1 + 1)) == $first_rec ] ||
14263                         error "mds$i: first index should be $user_rec1 + 1, " \
14264                               "but is $first_rec"
14265         done
14266 }
14267 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
14268               "during mount"
14269
14270 test_160i() {
14271
14272         local mdts=$(comma_list $(mdts_nodes))
14273
14274         changelog_register || error "first changelog_register failed"
14275
14276         # generate some changelog records to accumulate on each MDT
14277         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
14278         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14279                 error "create $DIR/$tdir/$tfile failed"
14280
14281         # check changelogs have been generated
14282         local nbcl=$(changelog_dump | wc -l)
14283         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14284
14285         # simulate race between register and unregister
14286         # XXX as fail_loc is set per-MDS, with DNE configs the race
14287         # simulation will only occur for one MDT per MDS and for the
14288         # others the normal race scenario will take place
14289         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
14290         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
14291         do_nodes $mdts $LCTL set_param fail_val=1
14292
14293         # unregister 1st user
14294         changelog_deregister &
14295         local pid1=$!
14296         # wait some time for deregister work to reach race rdv
14297         sleep 2
14298         # register 2nd user
14299         changelog_register || error "2nd user register failed"
14300
14301         wait $pid1 || error "1st user deregister failed"
14302
14303         local i
14304         local last_rec
14305         declare -A LAST_REC
14306         for i in $(seq $MDSCOUNT); do
14307                 if changelog_users mds$i | grep "^cl"; then
14308                         # make sure new records are added with one user present
14309                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
14310                                           awk '/^current.index:/ { print $NF }')
14311                 else
14312                         error "mds$i has no user registered"
14313                 fi
14314         done
14315
14316         # generate more changelog records to accumulate on each MDT
14317         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14318                 error "create $DIR/$tdir/${tfile}bis failed"
14319
14320         for i in $(seq $MDSCOUNT); do
14321                 last_rec=$(changelog_users $SINGLEMDS |
14322                            awk '/^current.index:/ { print $NF }')
14323                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
14324                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
14325                         error "changelogs are off on mds$i"
14326         done
14327 }
14328 run_test 160i "changelog user register/unregister race"
14329
14330 test_160j() {
14331         remote_mds_nodsh && skip "remote MDS with nodsh"
14332         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
14333                 skip "Need MDS version at least 2.12.56"
14334
14335         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
14336
14337         changelog_register || error "first changelog_register failed"
14338
14339         # generate some changelog
14340         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
14341         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14342                 error "create $DIR/$tdir/${tfile}bis failed"
14343
14344         # open the changelog device
14345         exec 3>/dev/changelog-$FSNAME-MDT0000
14346         exec 4</dev/changelog-$FSNAME-MDT0000
14347
14348         # umount the first lustre mount
14349         umount $MOUNT
14350
14351         # read changelog
14352         cat <&4 >/dev/null || error "read changelog failed"
14353
14354         # clear changelog
14355         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14356         changelog_users $SINGLEMDS | grep -q $cl_user ||
14357                 error "User $cl_user not found in changelog_users"
14358
14359         printf 'clear:'$cl_user':0' >&3
14360
14361         # close
14362         exec 3>&-
14363         exec 4<&-
14364
14365         # cleanup
14366         changelog_deregister || error "changelog_deregister failed"
14367
14368         umount $MOUNT2
14369         mount_client $MOUNT || error "mount_client on $MOUNT failed"
14370 }
14371 run_test 160j "client can be umounted  while its chanangelog is being used"
14372
14373 test_160k() {
14374         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14375         remote_mds_nodsh && skip "remote MDS with nodsh"
14376
14377         mkdir -p $DIR/$tdir/1/1
14378
14379         changelog_register || error "changelog_register failed"
14380         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14381
14382         changelog_users $SINGLEMDS | grep -q $cl_user ||
14383                 error "User '$cl_user' not found in changelog_users"
14384 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
14385         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
14386         rmdir $DIR/$tdir/1/1 & sleep 1
14387         mkdir $DIR/$tdir/2
14388         touch $DIR/$tdir/2/2
14389         rm -rf $DIR/$tdir/2
14390
14391         wait
14392         sleep 4
14393
14394         changelog_dump | grep rmdir || error "rmdir not recorded"
14395
14396         rm -rf $DIR/$tdir
14397         changelog_deregister
14398 }
14399 run_test 160k "Verify that changelog records are not lost"
14400
14401 test_161a() {
14402         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14403
14404         test_mkdir -c1 $DIR/$tdir
14405         cp /etc/hosts $DIR/$tdir/$tfile
14406         test_mkdir -c1 $DIR/$tdir/foo1
14407         test_mkdir -c1 $DIR/$tdir/foo2
14408         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
14409         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
14410         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
14411         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
14412         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
14413         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14414                 $LFS fid2path $DIR $FID
14415                 error "bad link ea"
14416         fi
14417         # middle
14418         rm $DIR/$tdir/foo2/zachary
14419         # last
14420         rm $DIR/$tdir/foo2/thor
14421         # first
14422         rm $DIR/$tdir/$tfile
14423         # rename
14424         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
14425         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
14426                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
14427         rm $DIR/$tdir/foo2/maggie
14428
14429         # overflow the EA
14430         local longname=$tfile.avg_len_is_thirty_two_
14431         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
14432                 error_noexit 'failed to unlink many hardlinks'" EXIT
14433         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
14434                 error "failed to hardlink many files"
14435         links=$($LFS fid2path $DIR $FID | wc -l)
14436         echo -n "${links}/1000 links in link EA"
14437         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
14438 }
14439 run_test 161a "link ea sanity"
14440
14441 test_161b() {
14442         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14443         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
14444
14445         local MDTIDX=1
14446         local remote_dir=$DIR/$tdir/remote_dir
14447
14448         mkdir -p $DIR/$tdir
14449         $LFS mkdir -i $MDTIDX $remote_dir ||
14450                 error "create remote directory failed"
14451
14452         cp /etc/hosts $remote_dir/$tfile
14453         mkdir -p $remote_dir/foo1
14454         mkdir -p $remote_dir/foo2
14455         ln $remote_dir/$tfile $remote_dir/foo1/sofia
14456         ln $remote_dir/$tfile $remote_dir/foo2/zachary
14457         ln $remote_dir/$tfile $remote_dir/foo1/luna
14458         ln $remote_dir/$tfile $remote_dir/foo2/thor
14459
14460         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
14461                      tr -d ']')
14462         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14463                 $LFS fid2path $DIR $FID
14464                 error "bad link ea"
14465         fi
14466         # middle
14467         rm $remote_dir/foo2/zachary
14468         # last
14469         rm $remote_dir/foo2/thor
14470         # first
14471         rm $remote_dir/$tfile
14472         # rename
14473         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
14474         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
14475         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
14476                 $LFS fid2path $DIR $FID
14477                 error "bad link rename"
14478         fi
14479         rm $remote_dir/foo2/maggie
14480
14481         # overflow the EA
14482         local longname=filename_avg_len_is_thirty_two_
14483         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
14484                 error "failed to hardlink many files"
14485         links=$($LFS fid2path $DIR $FID | wc -l)
14486         echo -n "${links}/1000 links in link EA"
14487         [[ ${links} -gt 60 ]] ||
14488                 error "expected at least 60 links in link EA"
14489         unlinkmany $remote_dir/foo2/$longname 1000 ||
14490         error "failed to unlink many hardlinks"
14491 }
14492 run_test 161b "link ea sanity under remote directory"
14493
14494 test_161c() {
14495         remote_mds_nodsh && skip "remote MDS with nodsh"
14496         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14497         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
14498                 skip "Need MDS version at least 2.1.5"
14499
14500         # define CLF_RENAME_LAST 0x0001
14501         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
14502         changelog_register || error "changelog_register failed"
14503
14504         rm -rf $DIR/$tdir
14505         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
14506         touch $DIR/$tdir/foo_161c
14507         touch $DIR/$tdir/bar_161c
14508         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14509         changelog_dump | grep RENME | tail -n 5
14510         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14511         changelog_clear 0 || error "changelog_clear failed"
14512         if [ x$flags != "x0x1" ]; then
14513                 error "flag $flags is not 0x1"
14514         fi
14515
14516         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
14517         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
14518         touch $DIR/$tdir/foo_161c
14519         touch $DIR/$tdir/bar_161c
14520         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14521         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14522         changelog_dump | grep RENME | tail -n 5
14523         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14524         changelog_clear 0 || error "changelog_clear failed"
14525         if [ x$flags != "x0x0" ]; then
14526                 error "flag $flags is not 0x0"
14527         fi
14528         echo "rename overwrite a target having nlink > 1," \
14529                 "changelog record has flags of $flags"
14530
14531         # rename doesn't overwrite a target (changelog flag 0x0)
14532         touch $DIR/$tdir/foo_161c
14533         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
14534         changelog_dump | grep RENME | tail -n 5
14535         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
14536         changelog_clear 0 || error "changelog_clear failed"
14537         if [ x$flags != "x0x0" ]; then
14538                 error "flag $flags is not 0x0"
14539         fi
14540         echo "rename doesn't overwrite a target," \
14541                 "changelog record has flags of $flags"
14542
14543         # define CLF_UNLINK_LAST 0x0001
14544         # unlink a file having nlink = 1 (changelog flag 0x1)
14545         rm -f $DIR/$tdir/foo2_161c
14546         changelog_dump | grep UNLNK | tail -n 5
14547         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14548         changelog_clear 0 || error "changelog_clear failed"
14549         if [ x$flags != "x0x1" ]; then
14550                 error "flag $flags is not 0x1"
14551         fi
14552         echo "unlink a file having nlink = 1," \
14553                 "changelog record has flags of $flags"
14554
14555         # unlink a file having nlink > 1 (changelog flag 0x0)
14556         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14557         rm -f $DIR/$tdir/foobar_161c
14558         changelog_dump | grep UNLNK | tail -n 5
14559         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14560         changelog_clear 0 || error "changelog_clear failed"
14561         if [ x$flags != "x0x0" ]; then
14562                 error "flag $flags is not 0x0"
14563         fi
14564         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
14565 }
14566 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
14567
14568 test_161d() {
14569         remote_mds_nodsh && skip "remote MDS with nodsh"
14570         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
14571
14572         local pid
14573         local fid
14574
14575         changelog_register || error "changelog_register failed"
14576
14577         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
14578         # interfer with $MOUNT/.lustre/fid/ access
14579         mkdir $DIR/$tdir
14580         [[ $? -eq 0 ]] || error "mkdir failed"
14581
14582         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
14583         $LCTL set_param fail_loc=0x8000140c
14584         # 5s pause
14585         $LCTL set_param fail_val=5
14586
14587         # create file
14588         echo foofoo > $DIR/$tdir/$tfile &
14589         pid=$!
14590
14591         # wait for create to be delayed
14592         sleep 2
14593
14594         ps -p $pid
14595         [[ $? -eq 0 ]] || error "create should be blocked"
14596
14597         local tempfile="$(mktemp --tmpdir $tfile.XXXXXX)"
14598         stack_trap "rm -f $tempfile"
14599         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
14600         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
14601         # some delay may occur during ChangeLog publishing and file read just
14602         # above, that could allow file write to happen finally
14603         [[ -s $tempfile ]] && echo "file should be empty"
14604
14605         $LCTL set_param fail_loc=0
14606
14607         wait $pid
14608         [[ $? -eq 0 ]] || error "create failed"
14609 }
14610 run_test 161d "create with concurrent .lustre/fid access"
14611
14612 check_path() {
14613         local expected="$1"
14614         shift
14615         local fid="$2"
14616
14617         local path
14618         path=$($LFS fid2path "$@")
14619         local rc=$?
14620
14621         if [ $rc -ne 0 ]; then
14622                 error "path looked up of '$expected' failed: rc=$rc"
14623         elif [ "$path" != "$expected" ]; then
14624                 error "path looked up '$path' instead of '$expected'"
14625         else
14626                 echo "FID '$fid' resolves to path '$path' as expected"
14627         fi
14628 }
14629
14630 test_162a() { # was test_162
14631         test_mkdir -p -c1 $DIR/$tdir/d2
14632         touch $DIR/$tdir/d2/$tfile
14633         touch $DIR/$tdir/d2/x1
14634         touch $DIR/$tdir/d2/x2
14635         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
14636         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
14637         # regular file
14638         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
14639         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
14640
14641         # softlink
14642         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
14643         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
14644         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
14645
14646         # softlink to wrong file
14647         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
14648         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
14649         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
14650
14651         # hardlink
14652         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
14653         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
14654         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
14655         # fid2path dir/fsname should both work
14656         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
14657         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
14658
14659         # hardlink count: check that there are 2 links
14660         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
14661         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
14662
14663         # hardlink indexing: remove the first link
14664         rm $DIR/$tdir/d2/p/q/r/hlink
14665         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
14666 }
14667 run_test 162a "path lookup sanity"
14668
14669 test_162b() {
14670         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14671         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14672
14673         mkdir $DIR/$tdir
14674         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
14675                                 error "create striped dir failed"
14676
14677         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
14678                                         tail -n 1 | awk '{print $2}')
14679         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
14680
14681         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
14682         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
14683
14684         # regular file
14685         for ((i=0;i<5;i++)); do
14686                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
14687                         error "get fid for f$i failed"
14688                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
14689
14690                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
14691                         error "get fid for d$i failed"
14692                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
14693         done
14694
14695         return 0
14696 }
14697 run_test 162b "striped directory path lookup sanity"
14698
14699 # LU-4239: Verify fid2path works with paths 100 or more directories deep
14700 test_162c() {
14701         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
14702                 skip "Need MDS version at least 2.7.51"
14703
14704         local lpath=$tdir.local
14705         local rpath=$tdir.remote
14706
14707         test_mkdir $DIR/$lpath
14708         test_mkdir $DIR/$rpath
14709
14710         for ((i = 0; i <= 101; i++)); do
14711                 lpath="$lpath/$i"
14712                 mkdir $DIR/$lpath
14713                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
14714                         error "get fid for local directory $DIR/$lpath failed"
14715                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
14716
14717                 rpath="$rpath/$i"
14718                 test_mkdir $DIR/$rpath
14719                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
14720                         error "get fid for remote directory $DIR/$rpath failed"
14721                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
14722         done
14723
14724         return 0
14725 }
14726 run_test 162c "fid2path works with paths 100 or more directories deep"
14727
14728 test_169() {
14729         # do directio so as not to populate the page cache
14730         log "creating a 10 Mb file"
14731         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
14732         log "starting reads"
14733         dd if=$DIR/$tfile of=/dev/null bs=4096 &
14734         log "truncating the file"
14735         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
14736         log "killing dd"
14737         kill %+ || true # reads might have finished
14738         echo "wait until dd is finished"
14739         wait
14740         log "removing the temporary file"
14741         rm -rf $DIR/$tfile || error "tmp file removal failed"
14742 }
14743 run_test 169 "parallel read and truncate should not deadlock"
14744
14745 test_170() {
14746         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14747
14748         $LCTL clear     # bug 18514
14749         $LCTL debug_daemon start $TMP/${tfile}_log_good
14750         touch $DIR/$tfile
14751         $LCTL debug_daemon stop
14752         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
14753                 error "sed failed to read log_good"
14754
14755         $LCTL debug_daemon start $TMP/${tfile}_log_good
14756         rm -rf $DIR/$tfile
14757         $LCTL debug_daemon stop
14758
14759         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
14760                error "lctl df log_bad failed"
14761
14762         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14763         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14764
14765         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
14766         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
14767
14768         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
14769                 error "bad_line good_line1 good_line2 are empty"
14770
14771         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14772         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
14773         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14774
14775         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
14776         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14777         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14778
14779         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
14780                 error "bad_line_new good_line_new are empty"
14781
14782         local expected_good=$((good_line1 + good_line2*2))
14783
14784         rm -f $TMP/${tfile}*
14785         # LU-231, short malformed line may not be counted into bad lines
14786         if [ $bad_line -ne $bad_line_new ] &&
14787                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
14788                 error "expected $bad_line bad lines, but got $bad_line_new"
14789                 return 1
14790         fi
14791
14792         if [ $expected_good -ne $good_line_new ]; then
14793                 error "expected $expected_good good lines, but got $good_line_new"
14794                 return 2
14795         fi
14796         true
14797 }
14798 run_test 170 "test lctl df to handle corrupted log ====================="
14799
14800 test_171() { # bug20592
14801         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14802
14803         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
14804         $LCTL set_param fail_loc=0x50e
14805         $LCTL set_param fail_val=3000
14806         multiop_bg_pause $DIR/$tfile O_s || true
14807         local MULTIPID=$!
14808         kill -USR1 $MULTIPID
14809         # cause log dump
14810         sleep 3
14811         wait $MULTIPID
14812         if dmesg | grep "recursive fault"; then
14813                 error "caught a recursive fault"
14814         fi
14815         $LCTL set_param fail_loc=0
14816         true
14817 }
14818 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
14819
14820 # it would be good to share it with obdfilter-survey/iokit-libecho code
14821 setup_obdecho_osc () {
14822         local rc=0
14823         local ost_nid=$1
14824         local obdfilter_name=$2
14825         echo "Creating new osc for $obdfilter_name on $ost_nid"
14826         # make sure we can find loopback nid
14827         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
14828
14829         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
14830                            ${obdfilter_name}_osc_UUID || rc=2; }
14831         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
14832                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
14833         return $rc
14834 }
14835
14836 cleanup_obdecho_osc () {
14837         local obdfilter_name=$1
14838         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
14839         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
14840         return 0
14841 }
14842
14843 obdecho_test() {
14844         local OBD=$1
14845         local node=$2
14846         local pages=${3:-64}
14847         local rc=0
14848         local id
14849
14850         local count=10
14851         local obd_size=$(get_obd_size $node $OBD)
14852         local page_size=$(get_page_size $node)
14853         if [[ -n "$obd_size" ]]; then
14854                 local new_count=$((obd_size / (pages * page_size / 1024)))
14855                 [[ $new_count -ge $count ]] || count=$new_count
14856         fi
14857
14858         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
14859         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
14860                            rc=2; }
14861         if [ $rc -eq 0 ]; then
14862             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
14863             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
14864         fi
14865         echo "New object id is $id"
14866         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
14867                            rc=4; }
14868         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
14869                            "test_brw $count w v $pages $id" || rc=4; }
14870         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
14871                            rc=4; }
14872         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
14873                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
14874         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
14875                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
14876         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
14877         return $rc
14878 }
14879
14880 test_180a() {
14881         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14882
14883         if ! module_loaded obdecho; then
14884                 load_module obdecho/obdecho &&
14885                         stack_trap "rmmod obdecho" EXIT ||
14886                         error "unable to load obdecho on client"
14887         fi
14888
14889         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
14890         local host=$($LCTL get_param -n osc.$osc.import |
14891                      awk '/current_connection:/ { print $2 }' )
14892         local target=$($LCTL get_param -n osc.$osc.import |
14893                        awk '/target:/ { print $2 }' )
14894         target=${target%_UUID}
14895
14896         if [ -n "$target" ]; then
14897                 setup_obdecho_osc $host $target &&
14898                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
14899                         { error "obdecho setup failed with $?"; return; }
14900
14901                 obdecho_test ${target}_osc client ||
14902                         error "obdecho_test failed on ${target}_osc"
14903         else
14904                 $LCTL get_param osc.$osc.import
14905                 error "there is no osc.$osc.import target"
14906         fi
14907 }
14908 run_test 180a "test obdecho on osc"
14909
14910 test_180b() {
14911         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14912         remote_ost_nodsh && skip "remote OST with nodsh"
14913
14914         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14915                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14916                 error "failed to load module obdecho"
14917
14918         local target=$(do_facet ost1 $LCTL dl |
14919                        awk '/obdfilter/ { print $4; exit; }')
14920
14921         if [ -n "$target" ]; then
14922                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
14923         else
14924                 do_facet ost1 $LCTL dl
14925                 error "there is no obdfilter target on ost1"
14926         fi
14927 }
14928 run_test 180b "test obdecho directly on obdfilter"
14929
14930 test_180c() { # LU-2598
14931         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14932         remote_ost_nodsh && skip "remote OST with nodsh"
14933         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
14934                 skip "Need MDS version at least 2.4.0"
14935
14936         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14937                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14938                 error "failed to load module obdecho"
14939
14940         local target=$(do_facet ost1 $LCTL dl |
14941                        awk '/obdfilter/ { print $4; exit; }')
14942
14943         if [ -n "$target" ]; then
14944                 local pages=16384 # 64MB bulk I/O RPC size
14945
14946                 obdecho_test "$target" ost1 "$pages" ||
14947                         error "obdecho_test with pages=$pages failed with $?"
14948         else
14949                 do_facet ost1 $LCTL dl
14950                 error "there is no obdfilter target on ost1"
14951         fi
14952 }
14953 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
14954
14955 test_181() { # bug 22177
14956         test_mkdir $DIR/$tdir
14957         # create enough files to index the directory
14958         createmany -o $DIR/$tdir/foobar 4000
14959         # print attributes for debug purpose
14960         lsattr -d .
14961         # open dir
14962         multiop_bg_pause $DIR/$tdir D_Sc || return 1
14963         MULTIPID=$!
14964         # remove the files & current working dir
14965         unlinkmany $DIR/$tdir/foobar 4000
14966         rmdir $DIR/$tdir
14967         kill -USR1 $MULTIPID
14968         wait $MULTIPID
14969         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
14970         return 0
14971 }
14972 run_test 181 "Test open-unlinked dir ========================"
14973
14974 test_182() {
14975         local fcount=1000
14976         local tcount=10
14977
14978         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14979
14980         $LCTL set_param mdc.*.rpc_stats=clear
14981
14982         for (( i = 0; i < $tcount; i++ )) ; do
14983                 mkdir $DIR/$tdir/$i
14984         done
14985
14986         for (( i = 0; i < $tcount; i++ )) ; do
14987                 createmany -o $DIR/$tdir/$i/f- $fcount &
14988         done
14989         wait
14990
14991         for (( i = 0; i < $tcount; i++ )) ; do
14992                 unlinkmany $DIR/$tdir/$i/f- $fcount &
14993         done
14994         wait
14995
14996         $LCTL get_param mdc.*.rpc_stats
14997
14998         rm -rf $DIR/$tdir
14999 }
15000 run_test 182 "Test parallel modify metadata operations ================"
15001
15002 test_183() { # LU-2275
15003         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15004         remote_mds_nodsh && skip "remote MDS with nodsh"
15005         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
15006                 skip "Need MDS version at least 2.3.56"
15007
15008         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15009         echo aaa > $DIR/$tdir/$tfile
15010
15011 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
15012         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
15013
15014         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
15015         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
15016
15017         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
15018
15019         # Flush negative dentry cache
15020         touch $DIR/$tdir/$tfile
15021
15022         # We are not checking for any leaked references here, they'll
15023         # become evident next time we do cleanup with module unload.
15024         rm -rf $DIR/$tdir
15025 }
15026 run_test 183 "No crash or request leak in case of strange dispositions ========"
15027
15028 # test suite 184 is for LU-2016, LU-2017
15029 test_184a() {
15030         check_swap_layouts_support
15031
15032         dir0=$DIR/$tdir/$testnum
15033         test_mkdir -p -c1 $dir0
15034         ref1=/etc/passwd
15035         ref2=/etc/group
15036         file1=$dir0/f1
15037         file2=$dir0/f2
15038         $LFS setstripe -c1 $file1
15039         cp $ref1 $file1
15040         $LFS setstripe -c2 $file2
15041         cp $ref2 $file2
15042         gen1=$($LFS getstripe -g $file1)
15043         gen2=$($LFS getstripe -g $file2)
15044
15045         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
15046         gen=$($LFS getstripe -g $file1)
15047         [[ $gen1 != $gen ]] ||
15048                 "Layout generation on $file1 does not change"
15049         gen=$($LFS getstripe -g $file2)
15050         [[ $gen2 != $gen ]] ||
15051                 "Layout generation on $file2 does not change"
15052
15053         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
15054         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
15055
15056         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
15057 }
15058 run_test 184a "Basic layout swap"
15059
15060 test_184b() {
15061         check_swap_layouts_support
15062
15063         dir0=$DIR/$tdir/$testnum
15064         mkdir -p $dir0 || error "creating dir $dir0"
15065         file1=$dir0/f1
15066         file2=$dir0/f2
15067         file3=$dir0/f3
15068         dir1=$dir0/d1
15069         dir2=$dir0/d2
15070         mkdir $dir1 $dir2
15071         $LFS setstripe -c1 $file1
15072         $LFS setstripe -c2 $file2
15073         $LFS setstripe -c1 $file3
15074         chown $RUNAS_ID $file3
15075         gen1=$($LFS getstripe -g $file1)
15076         gen2=$($LFS getstripe -g $file2)
15077
15078         $LFS swap_layouts $dir1 $dir2 &&
15079                 error "swap of directories layouts should fail"
15080         $LFS swap_layouts $dir1 $file1 &&
15081                 error "swap of directory and file layouts should fail"
15082         $RUNAS $LFS swap_layouts $file1 $file2 &&
15083                 error "swap of file we cannot write should fail"
15084         $LFS swap_layouts $file1 $file3 &&
15085                 error "swap of file with different owner should fail"
15086         /bin/true # to clear error code
15087 }
15088 run_test 184b "Forbidden layout swap (will generate errors)"
15089
15090 test_184c() {
15091         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
15092         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
15093         check_swap_layouts_support
15094
15095         local dir0=$DIR/$tdir/$testnum
15096         mkdir -p $dir0 || error "creating dir $dir0"
15097
15098         local ref1=$dir0/ref1
15099         local ref2=$dir0/ref2
15100         local file1=$dir0/file1
15101         local file2=$dir0/file2
15102         # create a file large enough for the concurrent test
15103         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
15104         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
15105         echo "ref file size: ref1($(stat -c %s $ref1))," \
15106              "ref2($(stat -c %s $ref2))"
15107
15108         cp $ref2 $file2
15109         dd if=$ref1 of=$file1 bs=16k &
15110         local DD_PID=$!
15111
15112         # Make sure dd starts to copy file
15113         while [ ! -f $file1 ]; do sleep 0.1; done
15114
15115         $LFS swap_layouts $file1 $file2
15116         local rc=$?
15117         wait $DD_PID
15118         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
15119         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
15120
15121         # how many bytes copied before swapping layout
15122         local copied=$(stat -c %s $file2)
15123         local remaining=$(stat -c %s $ref1)
15124         remaining=$((remaining - copied))
15125         echo "Copied $copied bytes before swapping layout..."
15126
15127         cmp -n $copied $file1 $ref2 | grep differ &&
15128                 error "Content mismatch [0, $copied) of ref2 and file1"
15129         cmp -n $copied $file2 $ref1 ||
15130                 error "Content mismatch [0, $copied) of ref1 and file2"
15131         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
15132                 error "Content mismatch [$copied, EOF) of ref1 and file1"
15133
15134         # clean up
15135         rm -f $ref1 $ref2 $file1 $file2
15136 }
15137 run_test 184c "Concurrent write and layout swap"
15138
15139 test_184d() {
15140         check_swap_layouts_support
15141         [ -z "$(which getfattr 2>/dev/null)" ] &&
15142                 skip_env "no getfattr command"
15143
15144         local file1=$DIR/$tdir/$tfile-1
15145         local file2=$DIR/$tdir/$tfile-2
15146         local file3=$DIR/$tdir/$tfile-3
15147         local lovea1
15148         local lovea2
15149
15150         mkdir -p $DIR/$tdir
15151         touch $file1 || error "create $file1 failed"
15152         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15153                 error "create $file2 failed"
15154         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15155                 error "create $file3 failed"
15156         lovea1=$(get_layout_param $file1)
15157
15158         $LFS swap_layouts $file2 $file3 ||
15159                 error "swap $file2 $file3 layouts failed"
15160         $LFS swap_layouts $file1 $file2 ||
15161                 error "swap $file1 $file2 layouts failed"
15162
15163         lovea2=$(get_layout_param $file2)
15164         echo "$lovea1"
15165         echo "$lovea2"
15166         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
15167
15168         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15169         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
15170 }
15171 run_test 184d "allow stripeless layouts swap"
15172
15173 test_184e() {
15174         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
15175                 skip "Need MDS version at least 2.6.94"
15176         check_swap_layouts_support
15177         [ -z "$(which getfattr 2>/dev/null)" ] &&
15178                 skip_env "no getfattr command"
15179
15180         local file1=$DIR/$tdir/$tfile-1
15181         local file2=$DIR/$tdir/$tfile-2
15182         local file3=$DIR/$tdir/$tfile-3
15183         local lovea
15184
15185         mkdir -p $DIR/$tdir
15186         touch $file1 || error "create $file1 failed"
15187         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15188                 error "create $file2 failed"
15189         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15190                 error "create $file3 failed"
15191
15192         $LFS swap_layouts $file1 $file2 ||
15193                 error "swap $file1 $file2 layouts failed"
15194
15195         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15196         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
15197
15198         echo 123 > $file1 || error "Should be able to write into $file1"
15199
15200         $LFS swap_layouts $file1 $file3 ||
15201                 error "swap $file1 $file3 layouts failed"
15202
15203         echo 123 > $file1 || error "Should be able to write into $file1"
15204
15205         rm -rf $file1 $file2 $file3
15206 }
15207 run_test 184e "Recreate layout after stripeless layout swaps"
15208
15209 test_184f() {
15210         # Create a file with name longer than sizeof(struct stat) ==
15211         # 144 to see if we can get chars from the file name to appear
15212         # in the returned striping. Note that 'f' == 0x66.
15213         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
15214
15215         mkdir -p $DIR/$tdir
15216         mcreate $DIR/$tdir/$file
15217         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
15218                 error "IOC_MDC_GETFILEINFO returned garbage striping"
15219         fi
15220 }
15221 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
15222
15223 test_185() { # LU-2441
15224         # LU-3553 - no volatile file support in old servers
15225         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
15226                 skip "Need MDS version at least 2.3.60"
15227
15228         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15229         touch $DIR/$tdir/spoo
15230         local mtime1=$(stat -c "%Y" $DIR/$tdir)
15231         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
15232                 error "cannot create/write a volatile file"
15233         [ "$FILESET" == "" ] &&
15234         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
15235                 error "FID is still valid after close"
15236
15237         multiop_bg_pause $DIR/$tdir vVw4096_c
15238         local multi_pid=$!
15239
15240         local OLD_IFS=$IFS
15241         IFS=":"
15242         local fidv=($fid)
15243         IFS=$OLD_IFS
15244         # assume that the next FID for this client is sequential, since stdout
15245         # is unfortunately eaten by multiop_bg_pause
15246         local n=$((${fidv[1]} + 1))
15247         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
15248         if [ "$FILESET" == "" ]; then
15249                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
15250                         error "FID is missing before close"
15251         fi
15252         kill -USR1 $multi_pid
15253         # 1 second delay, so if mtime change we will see it
15254         sleep 1
15255         local mtime2=$(stat -c "%Y" $DIR/$tdir)
15256         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
15257 }
15258 run_test 185 "Volatile file support"
15259
15260 function create_check_volatile() {
15261         local idx=$1
15262         local tgt
15263
15264         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
15265         local PID=$!
15266         sleep 1
15267         local FID=$(cat /tmp/${tfile}.fid)
15268         [ "$FID" == "" ] && error "can't get FID for volatile"
15269         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
15270         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
15271         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
15272         kill -USR1 $PID
15273         wait
15274         sleep 1
15275         cancel_lru_locks mdc # flush opencache
15276         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
15277         return 0
15278 }
15279
15280 test_185a(){
15281         # LU-12516 - volatile creation via .lustre
15282         [[ $MDS1_VERSION -ge $(version_code 2.12.55) ]] ||
15283                 skip "Need MDS version at least 2.3.55"
15284
15285         create_check_volatile 0
15286         [ $MDSCOUNT -lt 2 ] && return 0
15287
15288         # DNE case
15289         create_check_volatile 1
15290
15291         return 0
15292 }
15293 run_test 185a "Volatile file creation in .lustre/fid/"
15294
15295 test_187a() {
15296         remote_mds_nodsh && skip "remote MDS with nodsh"
15297         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15298                 skip "Need MDS version at least 2.3.0"
15299
15300         local dir0=$DIR/$tdir/$testnum
15301         mkdir -p $dir0 || error "creating dir $dir0"
15302
15303         local file=$dir0/file1
15304         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
15305         local dv1=$($LFS data_version $file)
15306         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
15307         local dv2=$($LFS data_version $file)
15308         [[ $dv1 != $dv2 ]] ||
15309                 error "data version did not change on write $dv1 == $dv2"
15310
15311         # clean up
15312         rm -f $file1
15313 }
15314 run_test 187a "Test data version change"
15315
15316 test_187b() {
15317         remote_mds_nodsh && skip "remote MDS with nodsh"
15318         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15319                 skip "Need MDS version at least 2.3.0"
15320
15321         local dir0=$DIR/$tdir/$testnum
15322         mkdir -p $dir0 || error "creating dir $dir0"
15323
15324         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
15325         [[ ${DV[0]} != ${DV[1]} ]] ||
15326                 error "data version did not change on write"\
15327                       " ${DV[0]} == ${DV[1]}"
15328
15329         # clean up
15330         rm -f $file1
15331 }
15332 run_test 187b "Test data version change on volatile file"
15333
15334 test_200() {
15335         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15336         remote_mgs_nodsh && skip "remote MGS with nodsh"
15337         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15338
15339         local POOL=${POOL:-cea1}
15340         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
15341         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
15342         # Pool OST targets
15343         local first_ost=0
15344         local last_ost=$(($OSTCOUNT - 1))
15345         local ost_step=2
15346         local ost_list=$(seq $first_ost $ost_step $last_ost)
15347         local ost_range="$first_ost $last_ost $ost_step"
15348         local test_path=$POOL_ROOT/$POOL_DIR_NAME
15349         local file_dir=$POOL_ROOT/file_tst
15350         local subdir=$test_path/subdir
15351         local rc=0
15352
15353         while : ; do
15354                 # former test_200a test_200b
15355                 pool_add $POOL                          || { rc=$? ; break; }
15356                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
15357                 # former test_200c test_200d
15358                 mkdir -p $test_path
15359                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
15360                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
15361                 mkdir -p $subdir
15362                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
15363                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
15364                                                         || { rc=$? ; break; }
15365                 # former test_200e test_200f
15366                 local files=$((OSTCOUNT*3))
15367                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
15368                                                         || { rc=$? ; break; }
15369                 pool_create_files $POOL $file_dir $files "$ost_list" \
15370                                                         || { rc=$? ; break; }
15371                 # former test_200g test_200h
15372                 pool_lfs_df $POOL                       || { rc=$? ; break; }
15373                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
15374
15375                 # former test_201a test_201b test_201c
15376                 pool_remove_first_target $POOL          || { rc=$? ; break; }
15377
15378                 local f=$test_path/$tfile
15379                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
15380                 pool_remove $POOL $f                    || { rc=$? ; break; }
15381                 break
15382         done
15383
15384         destroy_test_pools
15385
15386         return $rc
15387 }
15388 run_test 200 "OST pools"
15389
15390 # usage: default_attr <count | size | offset>
15391 default_attr() {
15392         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
15393 }
15394
15395 # usage: check_default_stripe_attr
15396 check_default_stripe_attr() {
15397         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
15398         case $1 in
15399         --stripe-count|-c)
15400                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
15401         --stripe-size|-S)
15402                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
15403         --stripe-index|-i)
15404                 EXPECTED=-1;;
15405         *)
15406                 error "unknown getstripe attr '$1'"
15407         esac
15408
15409         [ $ACTUAL == $EXPECTED ] ||
15410                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
15411 }
15412
15413 test_204a() {
15414         test_mkdir $DIR/$tdir
15415         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
15416
15417         check_default_stripe_attr --stripe-count
15418         check_default_stripe_attr --stripe-size
15419         check_default_stripe_attr --stripe-index
15420 }
15421 run_test 204a "Print default stripe attributes"
15422
15423 test_204b() {
15424         test_mkdir $DIR/$tdir
15425         $LFS setstripe --stripe-count 1 $DIR/$tdir
15426
15427         check_default_stripe_attr --stripe-size
15428         check_default_stripe_attr --stripe-index
15429 }
15430 run_test 204b "Print default stripe size and offset"
15431
15432 test_204c() {
15433         test_mkdir $DIR/$tdir
15434         $LFS setstripe --stripe-size 65536 $DIR/$tdir
15435
15436         check_default_stripe_attr --stripe-count
15437         check_default_stripe_attr --stripe-index
15438 }
15439 run_test 204c "Print default stripe count and offset"
15440
15441 test_204d() {
15442         test_mkdir $DIR/$tdir
15443         $LFS setstripe --stripe-index 0 $DIR/$tdir
15444
15445         check_default_stripe_attr --stripe-count
15446         check_default_stripe_attr --stripe-size
15447 }
15448 run_test 204d "Print default stripe count and size"
15449
15450 test_204e() {
15451         test_mkdir $DIR/$tdir
15452         $LFS setstripe -d $DIR/$tdir
15453
15454         check_default_stripe_attr --stripe-count --raw
15455         check_default_stripe_attr --stripe-size --raw
15456         check_default_stripe_attr --stripe-index --raw
15457 }
15458 run_test 204e "Print raw stripe attributes"
15459
15460 test_204f() {
15461         test_mkdir $DIR/$tdir
15462         $LFS setstripe --stripe-count 1 $DIR/$tdir
15463
15464         check_default_stripe_attr --stripe-size --raw
15465         check_default_stripe_attr --stripe-index --raw
15466 }
15467 run_test 204f "Print raw stripe size and offset"
15468
15469 test_204g() {
15470         test_mkdir $DIR/$tdir
15471         $LFS setstripe --stripe-size 65536 $DIR/$tdir
15472
15473         check_default_stripe_attr --stripe-count --raw
15474         check_default_stripe_attr --stripe-index --raw
15475 }
15476 run_test 204g "Print raw stripe count and offset"
15477
15478 test_204h() {
15479         test_mkdir $DIR/$tdir
15480         $LFS setstripe --stripe-index 0 $DIR/$tdir
15481
15482         check_default_stripe_attr --stripe-count --raw
15483         check_default_stripe_attr --stripe-size --raw
15484 }
15485 run_test 204h "Print raw stripe count and size"
15486
15487 # Figure out which job scheduler is being used, if any,
15488 # or use a fake one
15489 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
15490         JOBENV=SLURM_JOB_ID
15491 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
15492         JOBENV=LSB_JOBID
15493 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
15494         JOBENV=PBS_JOBID
15495 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
15496         JOBENV=LOADL_STEP_ID
15497 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
15498         JOBENV=JOB_ID
15499 else
15500         $LCTL list_param jobid_name > /dev/null 2>&1
15501         if [ $? -eq 0 ]; then
15502                 JOBENV=nodelocal
15503         else
15504                 JOBENV=FAKE_JOBID
15505         fi
15506 fi
15507 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
15508
15509 verify_jobstats() {
15510         local cmd=($1)
15511         shift
15512         local facets="$@"
15513
15514 # we don't really need to clear the stats for this test to work, since each
15515 # command has a unique jobid, but it makes debugging easier if needed.
15516 #       for facet in $facets; do
15517 #               local dev=$(convert_facet2label $facet)
15518 #               # clear old jobstats
15519 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
15520 #       done
15521
15522         # use a new JobID for each test, or we might see an old one
15523         [ "$JOBENV" = "FAKE_JOBID" ] &&
15524                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
15525
15526         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
15527
15528         [ "$JOBENV" = "nodelocal" ] && {
15529                 FAKE_JOBID=id.$testnum.%e.$RANDOM
15530                 $LCTL set_param jobid_name=$FAKE_JOBID
15531                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
15532         }
15533
15534         log "Test: ${cmd[*]}"
15535         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
15536
15537         if [ $JOBENV = "FAKE_JOBID" ]; then
15538                 FAKE_JOBID=$JOBVAL ${cmd[*]}
15539         else
15540                 ${cmd[*]}
15541         fi
15542
15543         # all files are created on OST0000
15544         for facet in $facets; do
15545                 local stats="*.$(convert_facet2label $facet).job_stats"
15546
15547                 # strip out libtool wrappers for in-tree executables
15548                 if [ $(do_facet $facet lctl get_param $stats |
15549                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
15550                         do_facet $facet lctl get_param $stats
15551                         error "No jobstats for $JOBVAL found on $facet::$stats"
15552                 fi
15553         done
15554 }
15555
15556 jobstats_set() {
15557         local new_jobenv=$1
15558
15559         set_persistent_param_and_check client "jobid_var" \
15560                 "$FSNAME.sys.jobid_var" $new_jobenv
15561 }
15562
15563 test_205a() { # Job stats
15564         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15565         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
15566                 skip "Need MDS version with at least 2.7.1"
15567         remote_mgs_nodsh && skip "remote MGS with nodsh"
15568         remote_mds_nodsh && skip "remote MDS with nodsh"
15569         remote_ost_nodsh && skip "remote OST with nodsh"
15570         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
15571                 skip "Server doesn't support jobstats"
15572         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
15573
15574         local old_jobenv=$($LCTL get_param -n jobid_var)
15575         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
15576
15577         if [[ $PERM_CMD == *"set_param -P"* ]]; then
15578                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
15579         else
15580                 stack_trap "do_facet mgs $PERM_CMD \
15581                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
15582         fi
15583         changelog_register
15584
15585         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
15586                                 mdt.*.job_cleanup_interval | head -n 1)
15587         local new_interval=5
15588         do_facet $SINGLEMDS \
15589                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
15590         stack_trap "do_facet $SINGLEMDS \
15591                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
15592         local start=$SECONDS
15593
15594         local cmd
15595         # mkdir
15596         cmd="mkdir $DIR/$tdir"
15597         verify_jobstats "$cmd" "$SINGLEMDS"
15598         # rmdir
15599         cmd="rmdir $DIR/$tdir"
15600         verify_jobstats "$cmd" "$SINGLEMDS"
15601         # mkdir on secondary MDT
15602         if [ $MDSCOUNT -gt 1 ]; then
15603                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
15604                 verify_jobstats "$cmd" "mds2"
15605         fi
15606         # mknod
15607         cmd="mknod $DIR/$tfile c 1 3"
15608         verify_jobstats "$cmd" "$SINGLEMDS"
15609         # unlink
15610         cmd="rm -f $DIR/$tfile"
15611         verify_jobstats "$cmd" "$SINGLEMDS"
15612         # create all files on OST0000 so verify_jobstats can find OST stats
15613         # open & close
15614         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
15615         verify_jobstats "$cmd" "$SINGLEMDS"
15616         # setattr
15617         cmd="touch $DIR/$tfile"
15618         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15619         # write
15620         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
15621         verify_jobstats "$cmd" "ost1"
15622         # read
15623         cancel_lru_locks osc
15624         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
15625         verify_jobstats "$cmd" "ost1"
15626         # truncate
15627         cmd="$TRUNCATE $DIR/$tfile 0"
15628         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15629         # rename
15630         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
15631         verify_jobstats "$cmd" "$SINGLEMDS"
15632         # jobstats expiry - sleep until old stats should be expired
15633         local left=$((new_interval + 5 - (SECONDS - start)))
15634         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
15635                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
15636                         "0" $left
15637         cmd="mkdir $DIR/$tdir.expire"
15638         verify_jobstats "$cmd" "$SINGLEMDS"
15639         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
15640             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
15641
15642         # Ensure that jobid are present in changelog (if supported by MDS)
15643         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
15644                 changelog_dump | tail -10
15645                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
15646                 [ $jobids -eq 9 ] ||
15647                         error "Wrong changelog jobid count $jobids != 9"
15648
15649                 # LU-5862
15650                 JOBENV="disable"
15651                 jobstats_set $JOBENV
15652                 touch $DIR/$tfile
15653                 changelog_dump | grep $tfile
15654                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
15655                 [ $jobids -eq 0 ] ||
15656                         error "Unexpected jobids when jobid_var=$JOBENV"
15657         fi
15658
15659         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
15660         JOBENV="JOBCOMPLEX"
15661         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
15662
15663         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
15664 }
15665 run_test 205a "Verify job stats"
15666
15667 # LU-13117
15668 test_205b() {
15669         $LCTL set_param jobid_var=USER jobid_name="%e.%u"
15670         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
15671         do_facet $SINGLEMDS $LCTL get_param mdt.*.job_stats |
15672                 grep job_id: | grep foolish &&
15673                         error "Unexpected jobid found"
15674         true
15675 }
15676 run_test 205b "Verify job stats jobid parsing"
15677
15678 # LU-1480, LU-1773 and LU-1657
15679 test_206() {
15680         mkdir -p $DIR/$tdir
15681         $LFS setstripe -c -1 $DIR/$tdir
15682 #define OBD_FAIL_LOV_INIT 0x1403
15683         $LCTL set_param fail_loc=0xa0001403
15684         $LCTL set_param fail_val=1
15685         touch $DIR/$tdir/$tfile || true
15686 }
15687 run_test 206 "fail lov_init_raid0() doesn't lbug"
15688
15689 test_207a() {
15690         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15691         local fsz=`stat -c %s $DIR/$tfile`
15692         cancel_lru_locks mdc
15693
15694         # do not return layout in getattr intent
15695 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
15696         $LCTL set_param fail_loc=0x170
15697         local sz=`stat -c %s $DIR/$tfile`
15698
15699         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
15700
15701         rm -rf $DIR/$tfile
15702 }
15703 run_test 207a "can refresh layout at glimpse"
15704
15705 test_207b() {
15706         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15707         local cksum=`md5sum $DIR/$tfile`
15708         local fsz=`stat -c %s $DIR/$tfile`
15709         cancel_lru_locks mdc
15710         cancel_lru_locks osc
15711
15712         # do not return layout in getattr intent
15713 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
15714         $LCTL set_param fail_loc=0x171
15715
15716         # it will refresh layout after the file is opened but before read issues
15717         echo checksum is "$cksum"
15718         echo "$cksum" |md5sum -c --quiet || error "file differs"
15719
15720         rm -rf $DIR/$tfile
15721 }
15722 run_test 207b "can refresh layout at open"
15723
15724 test_208() {
15725         # FIXME: in this test suite, only RD lease is used. This is okay
15726         # for now as only exclusive open is supported. After generic lease
15727         # is done, this test suite should be revised. - Jinshan
15728
15729         remote_mds_nodsh && skip "remote MDS with nodsh"
15730         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
15731                 skip "Need MDS version at least 2.4.52"
15732
15733         echo "==== test 1: verify get lease work"
15734         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
15735
15736         echo "==== test 2: verify lease can be broken by upcoming open"
15737         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15738         local PID=$!
15739         sleep 1
15740
15741         $MULTIOP $DIR/$tfile oO_RDONLY:c
15742         kill -USR1 $PID && wait $PID || error "break lease error"
15743
15744         echo "==== test 3: verify lease can't be granted if an open already exists"
15745         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
15746         local PID=$!
15747         sleep 1
15748
15749         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
15750         kill -USR1 $PID && wait $PID || error "open file error"
15751
15752         echo "==== test 4: lease can sustain over recovery"
15753         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
15754         PID=$!
15755         sleep 1
15756
15757         fail mds1
15758
15759         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
15760
15761         echo "==== test 5: lease broken can't be regained by replay"
15762         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15763         PID=$!
15764         sleep 1
15765
15766         # open file to break lease and then recovery
15767         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
15768         fail mds1
15769
15770         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
15771
15772         rm -f $DIR/$tfile
15773 }
15774 run_test 208 "Exclusive open"
15775
15776 test_209() {
15777         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
15778                 skip_env "must have disp_stripe"
15779
15780         touch $DIR/$tfile
15781         sync; sleep 5; sync;
15782
15783         echo 3 > /proc/sys/vm/drop_caches
15784         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15785
15786         # open/close 500 times
15787         for i in $(seq 500); do
15788                 cat $DIR/$tfile
15789         done
15790
15791         echo 3 > /proc/sys/vm/drop_caches
15792         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15793
15794         echo "before: $req_before, after: $req_after"
15795         [ $((req_after - req_before)) -ge 300 ] &&
15796                 error "open/close requests are not freed"
15797         return 0
15798 }
15799 run_test 209 "read-only open/close requests should be freed promptly"
15800
15801 test_212() {
15802         size=`date +%s`
15803         size=$((size % 8192 + 1))
15804         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
15805         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
15806         rm -f $DIR/f212 $DIR/f212.xyz
15807 }
15808 run_test 212 "Sendfile test ============================================"
15809
15810 test_213() {
15811         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
15812         cancel_lru_locks osc
15813         lctl set_param fail_loc=0x8000040f
15814         # generate a read lock
15815         cat $DIR/$tfile > /dev/null
15816         # write to the file, it will try to cancel the above read lock.
15817         cat /etc/hosts >> $DIR/$tfile
15818 }
15819 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
15820
15821 test_214() { # for bug 20133
15822         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
15823         for (( i=0; i < 340; i++ )) ; do
15824                 touch $DIR/$tdir/d214c/a$i
15825         done
15826
15827         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
15828         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
15829         ls $DIR/d214c || error "ls $DIR/d214c failed"
15830         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
15831         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
15832 }
15833 run_test 214 "hash-indexed directory test - bug 20133"
15834
15835 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
15836 create_lnet_proc_files() {
15837         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
15838 }
15839
15840 # counterpart of create_lnet_proc_files
15841 remove_lnet_proc_files() {
15842         rm -f $TMP/lnet_$1.sys
15843 }
15844
15845 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15846 # 3rd arg as regexp for body
15847 check_lnet_proc_stats() {
15848         local l=$(cat "$TMP/lnet_$1" |wc -l)
15849         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
15850
15851         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
15852 }
15853
15854 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15855 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
15856 # optional and can be regexp for 2nd line (lnet.routes case)
15857 check_lnet_proc_entry() {
15858         local blp=2          # blp stands for 'position of 1st line of body'
15859         [ -z "$5" ] || blp=3 # lnet.routes case
15860
15861         local l=$(cat "$TMP/lnet_$1" |wc -l)
15862         # subtracting one from $blp because the body can be empty
15863         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
15864
15865         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
15866                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
15867
15868         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
15869                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
15870
15871         # bail out if any unexpected line happened
15872         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
15873         [ "$?" != 0 ] || error "$2 misformatted"
15874 }
15875
15876 test_215() { # for bugs 18102, 21079, 21517
15877         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15878
15879         local N='(0|[1-9][0-9]*)'       # non-negative numeric
15880         local P='[1-9][0-9]*'           # positive numeric
15881         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
15882         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
15883         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
15884         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
15885
15886         local L1 # regexp for 1st line
15887         local L2 # regexp for 2nd line (optional)
15888         local BR # regexp for the rest (body)
15889
15890         # lnet.stats should look as 11 space-separated non-negative numerics
15891         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
15892         create_lnet_proc_files "stats"
15893         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
15894         remove_lnet_proc_files "stats"
15895
15896         # lnet.routes should look like this:
15897         # Routing disabled/enabled
15898         # net hops priority state router
15899         # where net is a string like tcp0, hops > 0, priority >= 0,
15900         # state is up/down,
15901         # router is a string like 192.168.1.1@tcp2
15902         L1="^Routing (disabled|enabled)$"
15903         L2="^net +hops +priority +state +router$"
15904         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
15905         create_lnet_proc_files "routes"
15906         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
15907         remove_lnet_proc_files "routes"
15908
15909         # lnet.routers should look like this:
15910         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
15911         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
15912         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
15913         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
15914         L1="^ref +rtr_ref +alive +router$"
15915         BR="^$P +$P +(up|down) +$NID$"
15916         create_lnet_proc_files "routers"
15917         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
15918         remove_lnet_proc_files "routers"
15919
15920         # lnet.peers should look like this:
15921         # nid refs state last max rtr min tx min queue
15922         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
15923         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
15924         # numeric (0 or >0 or <0), queue >= 0.
15925         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
15926         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
15927         create_lnet_proc_files "peers"
15928         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
15929         remove_lnet_proc_files "peers"
15930
15931         # lnet.buffers  should look like this:
15932         # pages count credits min
15933         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
15934         L1="^pages +count +credits +min$"
15935         BR="^ +$N +$N +$I +$I$"
15936         create_lnet_proc_files "buffers"
15937         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
15938         remove_lnet_proc_files "buffers"
15939
15940         # lnet.nis should look like this:
15941         # nid status alive refs peer rtr max tx min
15942         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
15943         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
15944         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
15945         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
15946         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
15947         create_lnet_proc_files "nis"
15948         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
15949         remove_lnet_proc_files "nis"
15950
15951         # can we successfully write to lnet.stats?
15952         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
15953 }
15954 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
15955
15956 test_216() { # bug 20317
15957         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15958         remote_ost_nodsh && skip "remote OST with nodsh"
15959
15960         local node
15961         local facets=$(get_facets OST)
15962         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15963
15964         save_lustre_params client "osc.*.contention_seconds" > $p
15965         save_lustre_params $facets \
15966                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
15967         save_lustre_params $facets \
15968                 "ldlm.namespaces.filter-*.contended_locks" >> $p
15969         save_lustre_params $facets \
15970                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
15971         clear_stats osc.*.osc_stats
15972
15973         # agressive lockless i/o settings
15974         do_nodes $(comma_list $(osts_nodes)) \
15975                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
15976                         ldlm.namespaces.filter-*.contended_locks=0 \
15977                         ldlm.namespaces.filter-*.contention_seconds=60"
15978         lctl set_param -n osc.*.contention_seconds=60
15979
15980         $DIRECTIO write $DIR/$tfile 0 10 4096
15981         $CHECKSTAT -s 40960 $DIR/$tfile
15982
15983         # disable lockless i/o
15984         do_nodes $(comma_list $(osts_nodes)) \
15985                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
15986                         ldlm.namespaces.filter-*.contended_locks=32 \
15987                         ldlm.namespaces.filter-*.contention_seconds=0"
15988         lctl set_param -n osc.*.contention_seconds=0
15989         clear_stats osc.*.osc_stats
15990
15991         dd if=/dev/zero of=$DIR/$tfile count=0
15992         $CHECKSTAT -s 0 $DIR/$tfile
15993
15994         restore_lustre_params <$p
15995         rm -f $p
15996         rm $DIR/$tfile
15997 }
15998 run_test 216 "check lockless direct write updates file size and kms correctly"
15999
16000 test_217() { # bug 22430
16001         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16002
16003         local node
16004         local nid
16005
16006         for node in $(nodes_list); do
16007                 nid=$(host_nids_address $node $NETTYPE)
16008                 if [[ $nid = *-* ]] ; then
16009                         echo "lctl ping $(h2nettype $nid)"
16010                         lctl ping $(h2nettype $nid)
16011                 else
16012                         echo "skipping $node (no hyphen detected)"
16013                 fi
16014         done
16015 }
16016 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
16017
16018 test_218() {
16019        # do directio so as not to populate the page cache
16020        log "creating a 10 Mb file"
16021        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
16022        log "starting reads"
16023        dd if=$DIR/$tfile of=/dev/null bs=4096 &
16024        log "truncating the file"
16025        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
16026        log "killing dd"
16027        kill %+ || true # reads might have finished
16028        echo "wait until dd is finished"
16029        wait
16030        log "removing the temporary file"
16031        rm -rf $DIR/$tfile || error "tmp file removal failed"
16032 }
16033 run_test 218 "parallel read and truncate should not deadlock"
16034
16035 test_219() {
16036         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16037
16038         # write one partial page
16039         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
16040         # set no grant so vvp_io_commit_write will do sync write
16041         $LCTL set_param fail_loc=0x411
16042         # write a full page at the end of file
16043         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
16044
16045         $LCTL set_param fail_loc=0
16046         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
16047         $LCTL set_param fail_loc=0x411
16048         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
16049
16050         # LU-4201
16051         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
16052         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
16053 }
16054 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
16055
16056 test_220() { #LU-325
16057         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16058         remote_ost_nodsh && skip "remote OST with nodsh"
16059         remote_mds_nodsh && skip "remote MDS with nodsh"
16060         remote_mgs_nodsh && skip "remote MGS with nodsh"
16061
16062         local OSTIDX=0
16063
16064         # create on MDT0000 so the last_id and next_id are correct
16065         mkdir $DIR/$tdir
16066         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
16067         OST=${OST%_UUID}
16068
16069         # on the mdt's osc
16070         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
16071         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
16072                         osp.$mdtosc_proc1.prealloc_last_id)
16073         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
16074                         osp.$mdtosc_proc1.prealloc_next_id)
16075
16076         $LFS df -i
16077
16078         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
16079         #define OBD_FAIL_OST_ENOINO              0x229
16080         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
16081         create_pool $FSNAME.$TESTNAME || return 1
16082         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
16083
16084         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
16085
16086         MDSOBJS=$((last_id - next_id))
16087         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
16088
16089         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
16090         echo "OST still has $count kbytes free"
16091
16092         echo "create $MDSOBJS files @next_id..."
16093         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
16094
16095         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
16096                         osp.$mdtosc_proc1.prealloc_last_id)
16097         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
16098                         osp.$mdtosc_proc1.prealloc_next_id)
16099
16100         echo "after creation, last_id=$last_id2, next_id=$next_id2"
16101         $LFS df -i
16102
16103         echo "cleanup..."
16104
16105         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
16106         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
16107
16108         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
16109                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
16110         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
16111                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
16112         echo "unlink $MDSOBJS files @$next_id..."
16113         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
16114 }
16115 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
16116
16117 test_221() {
16118         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16119
16120         dd if=`which date` of=$MOUNT/date oflag=sync
16121         chmod +x $MOUNT/date
16122
16123         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
16124         $LCTL set_param fail_loc=0x80001401
16125
16126         $MOUNT/date > /dev/null
16127         rm -f $MOUNT/date
16128 }
16129 run_test 221 "make sure fault and truncate race to not cause OOM"
16130
16131 test_222a () {
16132         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16133
16134         rm -rf $DIR/$tdir
16135         test_mkdir $DIR/$tdir
16136         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16137         createmany -o $DIR/$tdir/$tfile 10
16138         cancel_lru_locks mdc
16139         cancel_lru_locks osc
16140         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16141         $LCTL set_param fail_loc=0x31a
16142         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
16143         $LCTL set_param fail_loc=0
16144         rm -r $DIR/$tdir
16145 }
16146 run_test 222a "AGL for ls should not trigger CLIO lock failure"
16147
16148 test_222b () {
16149         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16150
16151         rm -rf $DIR/$tdir
16152         test_mkdir $DIR/$tdir
16153         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16154         createmany -o $DIR/$tdir/$tfile 10
16155         cancel_lru_locks mdc
16156         cancel_lru_locks osc
16157         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16158         $LCTL set_param fail_loc=0x31a
16159         rm -r $DIR/$tdir || error "AGL for rmdir failed"
16160         $LCTL set_param fail_loc=0
16161 }
16162 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
16163
16164 test_223 () {
16165         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16166
16167         rm -rf $DIR/$tdir
16168         test_mkdir $DIR/$tdir
16169         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16170         createmany -o $DIR/$tdir/$tfile 10
16171         cancel_lru_locks mdc
16172         cancel_lru_locks osc
16173         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
16174         $LCTL set_param fail_loc=0x31b
16175         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
16176         $LCTL set_param fail_loc=0
16177         rm -r $DIR/$tdir
16178 }
16179 run_test 223 "osc reenqueue if without AGL lock granted ======================="
16180
16181 test_224a() { # LU-1039, MRP-303
16182         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16183
16184         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
16185         $LCTL set_param fail_loc=0x508
16186         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
16187         $LCTL set_param fail_loc=0
16188         df $DIR
16189 }
16190 run_test 224a "Don't panic on bulk IO failure"
16191
16192 test_224b() { # LU-1039, MRP-303
16193         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16194
16195         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
16196         cancel_lru_locks osc
16197         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
16198         $LCTL set_param fail_loc=0x515
16199         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
16200         $LCTL set_param fail_loc=0
16201         df $DIR
16202 }
16203 run_test 224b "Don't panic on bulk IO failure"
16204
16205 test_224c() { # LU-6441
16206         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16207         remote_mds_nodsh && skip "remote MDS with nodsh"
16208
16209         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16210         save_writethrough $p
16211         set_cache writethrough on
16212
16213         local pages_per_rpc=$($LCTL get_param \
16214                                 osc.*.max_pages_per_rpc)
16215         local at_max=$($LCTL get_param -n at_max)
16216         local timeout=$($LCTL get_param -n timeout)
16217         local test_at="at_max"
16218         local param_at="$FSNAME.sys.at_max"
16219         local test_timeout="timeout"
16220         local param_timeout="$FSNAME.sys.timeout"
16221
16222         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
16223
16224         set_persistent_param_and_check client "$test_at" "$param_at" 0
16225         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
16226
16227         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
16228         do_facet ost1 "$LCTL set_param fail_loc=0x520"
16229         $LFS setstripe -c 1 -i 0 $DIR/$tfile
16230         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
16231         sync
16232         do_facet ost1 "$LCTL set_param fail_loc=0"
16233
16234         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
16235         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
16236                 $timeout
16237
16238         $LCTL set_param -n $pages_per_rpc
16239         restore_lustre_params < $p
16240         rm -f $p
16241 }
16242 run_test 224c "Don't hang if one of md lost during large bulk RPC"
16243
16244 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
16245 test_225a () {
16246         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16247         if [ -z ${MDSSURVEY} ]; then
16248                 skip_env "mds-survey not found"
16249         fi
16250         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16251                 skip "Need MDS version at least 2.2.51"
16252
16253         local mds=$(facet_host $SINGLEMDS)
16254         local target=$(do_nodes $mds 'lctl dl' |
16255                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16256
16257         local cmd1="file_count=1000 thrhi=4"
16258         local cmd2="dir_count=2 layer=mdd stripe_count=0"
16259         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16260         local cmd="$cmd1 $cmd2 $cmd3"
16261
16262         rm -f ${TMP}/mds_survey*
16263         echo + $cmd
16264         eval $cmd || error "mds-survey with zero-stripe failed"
16265         cat ${TMP}/mds_survey*
16266         rm -f ${TMP}/mds_survey*
16267 }
16268 run_test 225a "Metadata survey sanity with zero-stripe"
16269
16270 test_225b () {
16271         if [ -z ${MDSSURVEY} ]; then
16272                 skip_env "mds-survey not found"
16273         fi
16274         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16275                 skip "Need MDS version at least 2.2.51"
16276         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16277         remote_mds_nodsh && skip "remote MDS with nodsh"
16278         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
16279                 skip_env "Need to mount OST to test"
16280         fi
16281
16282         local mds=$(facet_host $SINGLEMDS)
16283         local target=$(do_nodes $mds 'lctl dl' |
16284                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16285
16286         local cmd1="file_count=1000 thrhi=4"
16287         local cmd2="dir_count=2 layer=mdd stripe_count=1"
16288         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16289         local cmd="$cmd1 $cmd2 $cmd3"
16290
16291         rm -f ${TMP}/mds_survey*
16292         echo + $cmd
16293         eval $cmd || error "mds-survey with stripe_count failed"
16294         cat ${TMP}/mds_survey*
16295         rm -f ${TMP}/mds_survey*
16296 }
16297 run_test 225b "Metadata survey sanity with stripe_count = 1"
16298
16299 mcreate_path2fid () {
16300         local mode=$1
16301         local major=$2
16302         local minor=$3
16303         local name=$4
16304         local desc=$5
16305         local path=$DIR/$tdir/$name
16306         local fid
16307         local rc
16308         local fid_path
16309
16310         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
16311                 error "cannot create $desc"
16312
16313         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
16314         rc=$?
16315         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
16316
16317         fid_path=$($LFS fid2path $MOUNT $fid)
16318         rc=$?
16319         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
16320
16321         [ "$path" == "$fid_path" ] ||
16322                 error "fid2path returned $fid_path, expected $path"
16323
16324         echo "pass with $path and $fid"
16325 }
16326
16327 test_226a () {
16328         rm -rf $DIR/$tdir
16329         mkdir -p $DIR/$tdir
16330
16331         mcreate_path2fid 0010666 0 0 fifo "FIFO"
16332         mcreate_path2fid 0020666 1 3 null "character special file (null)"
16333         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
16334         mcreate_path2fid 0040666 0 0 dir "directory"
16335         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
16336         mcreate_path2fid 0100666 0 0 file "regular file"
16337         mcreate_path2fid 0120666 0 0 link "symbolic link"
16338         mcreate_path2fid 0140666 0 0 sock "socket"
16339 }
16340 run_test 226a "call path2fid and fid2path on files of all type"
16341
16342 test_226b () {
16343         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16344
16345         local MDTIDX=1
16346
16347         rm -rf $DIR/$tdir
16348         mkdir -p $DIR/$tdir
16349         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
16350                 error "create remote directory failed"
16351         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
16352         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
16353                                 "character special file (null)"
16354         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
16355                                 "character special file (no device)"
16356         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
16357         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
16358                                 "block special file (loop)"
16359         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
16360         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
16361         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
16362 }
16363 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
16364
16365 # LU-1299 Executing or running ldd on a truncated executable does not
16366 # cause an out-of-memory condition.
16367 test_227() {
16368         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16369         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
16370
16371         dd if=$(which date) of=$MOUNT/date bs=1k count=1
16372         chmod +x $MOUNT/date
16373
16374         $MOUNT/date > /dev/null
16375         ldd $MOUNT/date > /dev/null
16376         rm -f $MOUNT/date
16377 }
16378 run_test 227 "running truncated executable does not cause OOM"
16379
16380 # LU-1512 try to reuse idle OI blocks
16381 test_228a() {
16382         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16383         remote_mds_nodsh && skip "remote MDS with nodsh"
16384         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16385
16386         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16387         local myDIR=$DIR/$tdir
16388
16389         mkdir -p $myDIR
16390         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16391         $LCTL set_param fail_loc=0x80001002
16392         createmany -o $myDIR/t- 10000
16393         $LCTL set_param fail_loc=0
16394         # The guard is current the largest FID holder
16395         touch $myDIR/guard
16396         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16397                     tr -d '[')
16398         local IDX=$(($SEQ % 64))
16399
16400         do_facet $SINGLEMDS sync
16401         # Make sure journal flushed.
16402         sleep 6
16403         local blk1=$(do_facet $SINGLEMDS \
16404                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16405                      grep Blockcount | awk '{print $4}')
16406
16407         # Remove old files, some OI blocks will become idle.
16408         unlinkmany $myDIR/t- 10000
16409         # Create new files, idle OI blocks should be reused.
16410         createmany -o $myDIR/t- 2000
16411         do_facet $SINGLEMDS sync
16412         # Make sure journal flushed.
16413         sleep 6
16414         local blk2=$(do_facet $SINGLEMDS \
16415                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16416                      grep Blockcount | awk '{print $4}')
16417
16418         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16419 }
16420 run_test 228a "try to reuse idle OI blocks"
16421
16422 test_228b() {
16423         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16424         remote_mds_nodsh && skip "remote MDS with nodsh"
16425         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16426
16427         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16428         local myDIR=$DIR/$tdir
16429
16430         mkdir -p $myDIR
16431         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16432         $LCTL set_param fail_loc=0x80001002
16433         createmany -o $myDIR/t- 10000
16434         $LCTL set_param fail_loc=0
16435         # The guard is current the largest FID holder
16436         touch $myDIR/guard
16437         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16438                     tr -d '[')
16439         local IDX=$(($SEQ % 64))
16440
16441         do_facet $SINGLEMDS sync
16442         # Make sure journal flushed.
16443         sleep 6
16444         local blk1=$(do_facet $SINGLEMDS \
16445                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16446                      grep Blockcount | awk '{print $4}')
16447
16448         # Remove old files, some OI blocks will become idle.
16449         unlinkmany $myDIR/t- 10000
16450
16451         # stop the MDT
16452         stop $SINGLEMDS || error "Fail to stop MDT."
16453         # remount the MDT
16454         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
16455
16456         df $MOUNT || error "Fail to df."
16457         # Create new files, idle OI blocks should be reused.
16458         createmany -o $myDIR/t- 2000
16459         do_facet $SINGLEMDS sync
16460         # Make sure journal flushed.
16461         sleep 6
16462         local blk2=$(do_facet $SINGLEMDS \
16463                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16464                      grep Blockcount | awk '{print $4}')
16465
16466         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16467 }
16468 run_test 228b "idle OI blocks can be reused after MDT restart"
16469
16470 #LU-1881
16471 test_228c() {
16472         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16473         remote_mds_nodsh && skip "remote MDS with nodsh"
16474         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16475
16476         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16477         local myDIR=$DIR/$tdir
16478
16479         mkdir -p $myDIR
16480         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16481         $LCTL set_param fail_loc=0x80001002
16482         # 20000 files can guarantee there are index nodes in the OI file
16483         createmany -o $myDIR/t- 20000
16484         $LCTL set_param fail_loc=0
16485         # The guard is current the largest FID holder
16486         touch $myDIR/guard
16487         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16488                     tr -d '[')
16489         local IDX=$(($SEQ % 64))
16490
16491         do_facet $SINGLEMDS sync
16492         # Make sure journal flushed.
16493         sleep 6
16494         local blk1=$(do_facet $SINGLEMDS \
16495                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16496                      grep Blockcount | awk '{print $4}')
16497
16498         # Remove old files, some OI blocks will become idle.
16499         unlinkmany $myDIR/t- 20000
16500         rm -f $myDIR/guard
16501         # The OI file should become empty now
16502
16503         # Create new files, idle OI blocks should be reused.
16504         createmany -o $myDIR/t- 2000
16505         do_facet $SINGLEMDS sync
16506         # Make sure journal flushed.
16507         sleep 6
16508         local blk2=$(do_facet $SINGLEMDS \
16509                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16510                      grep Blockcount | awk '{print $4}')
16511
16512         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16513 }
16514 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
16515
16516 test_229() { # LU-2482, LU-3448
16517         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16518         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
16519         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
16520                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
16521
16522         rm -f $DIR/$tfile
16523
16524         # Create a file with a released layout and stripe count 2.
16525         $MULTIOP $DIR/$tfile H2c ||
16526                 error "failed to create file with released layout"
16527
16528         $LFS getstripe -v $DIR/$tfile
16529
16530         local pattern=$($LFS getstripe -L $DIR/$tfile)
16531         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
16532
16533         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
16534                 error "getstripe"
16535         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
16536         stat $DIR/$tfile || error "failed to stat released file"
16537
16538         chown $RUNAS_ID $DIR/$tfile ||
16539                 error "chown $RUNAS_ID $DIR/$tfile failed"
16540
16541         chgrp $RUNAS_ID $DIR/$tfile ||
16542                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
16543
16544         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
16545         rm $DIR/$tfile || error "failed to remove released file"
16546 }
16547 run_test 229 "getstripe/stat/rm/attr changes work on released files"
16548
16549 test_230a() {
16550         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16551         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16552         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16553                 skip "Need MDS version at least 2.11.52"
16554
16555         local MDTIDX=1
16556
16557         test_mkdir $DIR/$tdir
16558         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
16559         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
16560         [ $mdt_idx -ne 0 ] &&
16561                 error "create local directory on wrong MDT $mdt_idx"
16562
16563         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
16564                         error "create remote directory failed"
16565         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
16566         [ $mdt_idx -ne $MDTIDX ] &&
16567                 error "create remote directory on wrong MDT $mdt_idx"
16568
16569         createmany -o $DIR/$tdir/test_230/t- 10 ||
16570                 error "create files on remote directory failed"
16571         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
16572         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
16573         rm -r $DIR/$tdir || error "unlink remote directory failed"
16574 }
16575 run_test 230a "Create remote directory and files under the remote directory"
16576
16577 test_230b() {
16578         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16579         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16580         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16581                 skip "Need MDS version at least 2.11.52"
16582
16583         local MDTIDX=1
16584         local mdt_index
16585         local i
16586         local file
16587         local pid
16588         local stripe_count
16589         local migrate_dir=$DIR/$tdir/migrate_dir
16590         local other_dir=$DIR/$tdir/other_dir
16591
16592         test_mkdir $DIR/$tdir
16593         test_mkdir -i0 -c1 $migrate_dir
16594         test_mkdir -i0 -c1 $other_dir
16595         for ((i=0; i<10; i++)); do
16596                 mkdir -p $migrate_dir/dir_${i}
16597                 createmany -o $migrate_dir/dir_${i}/f 10 ||
16598                         error "create files under remote dir failed $i"
16599         done
16600
16601         cp /etc/passwd $migrate_dir/$tfile
16602         cp /etc/passwd $other_dir/$tfile
16603         chattr +SAD $migrate_dir
16604         chattr +SAD $migrate_dir/$tfile
16605
16606         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16607         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16608         local old_dir_mode=$(stat -c%f $migrate_dir)
16609         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
16610
16611         mkdir -p $migrate_dir/dir_default_stripe2
16612         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
16613         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
16614
16615         mkdir -p $other_dir
16616         ln $migrate_dir/$tfile $other_dir/luna
16617         ln $migrate_dir/$tfile $migrate_dir/sofia
16618         ln $other_dir/$tfile $migrate_dir/david
16619         ln -s $migrate_dir/$tfile $other_dir/zachary
16620         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
16621         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
16622
16623         $LFS migrate -m $MDTIDX $migrate_dir ||
16624                 error "fails on migrating remote dir to MDT1"
16625
16626         echo "migratate to MDT1, then checking.."
16627         for ((i = 0; i < 10; i++)); do
16628                 for file in $(find $migrate_dir/dir_${i}); do
16629                         mdt_index=$($LFS getstripe -m $file)
16630                         [ $mdt_index == $MDTIDX ] ||
16631                                 error "$file is not on MDT${MDTIDX}"
16632                 done
16633         done
16634
16635         # the multiple link file should still in MDT0
16636         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
16637         [ $mdt_index == 0 ] ||
16638                 error "$file is not on MDT${MDTIDX}"
16639
16640         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16641         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16642                 error " expect $old_dir_flag get $new_dir_flag"
16643
16644         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16645         [ "$old_file_flag" = "$new_file_flag" ] ||
16646                 error " expect $old_file_flag get $new_file_flag"
16647
16648         local new_dir_mode=$(stat -c%f $migrate_dir)
16649         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16650                 error "expect mode $old_dir_mode get $new_dir_mode"
16651
16652         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16653         [ "$old_file_mode" = "$new_file_mode" ] ||
16654                 error "expect mode $old_file_mode get $new_file_mode"
16655
16656         diff /etc/passwd $migrate_dir/$tfile ||
16657                 error "$tfile different after migration"
16658
16659         diff /etc/passwd $other_dir/luna ||
16660                 error "luna different after migration"
16661
16662         diff /etc/passwd $migrate_dir/sofia ||
16663                 error "sofia different after migration"
16664
16665         diff /etc/passwd $migrate_dir/david ||
16666                 error "david different after migration"
16667
16668         diff /etc/passwd $other_dir/zachary ||
16669                 error "zachary different after migration"
16670
16671         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16672                 error "${tfile}_ln different after migration"
16673
16674         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16675                 error "${tfile}_ln_other different after migration"
16676
16677         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
16678         [ $stripe_count = 2 ] ||
16679                 error "dir strpe_count $d != 2 after migration."
16680
16681         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
16682         [ $stripe_count = 2 ] ||
16683                 error "file strpe_count $d != 2 after migration."
16684
16685         #migrate back to MDT0
16686         MDTIDX=0
16687
16688         $LFS migrate -m $MDTIDX $migrate_dir ||
16689                 error "fails on migrating remote dir to MDT0"
16690
16691         echo "migrate back to MDT0, checking.."
16692         for file in $(find $migrate_dir); do
16693                 mdt_index=$($LFS getstripe -m $file)
16694                 [ $mdt_index == $MDTIDX ] ||
16695                         error "$file is not on MDT${MDTIDX}"
16696         done
16697
16698         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16699         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16700                 error " expect $old_dir_flag get $new_dir_flag"
16701
16702         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16703         [ "$old_file_flag" = "$new_file_flag" ] ||
16704                 error " expect $old_file_flag get $new_file_flag"
16705
16706         local new_dir_mode=$(stat -c%f $migrate_dir)
16707         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16708                 error "expect mode $old_dir_mode get $new_dir_mode"
16709
16710         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16711         [ "$old_file_mode" = "$new_file_mode" ] ||
16712                 error "expect mode $old_file_mode get $new_file_mode"
16713
16714         diff /etc/passwd ${migrate_dir}/$tfile ||
16715                 error "$tfile different after migration"
16716
16717         diff /etc/passwd ${other_dir}/luna ||
16718                 error "luna different after migration"
16719
16720         diff /etc/passwd ${migrate_dir}/sofia ||
16721                 error "sofia different after migration"
16722
16723         diff /etc/passwd ${other_dir}/zachary ||
16724                 error "zachary different after migration"
16725
16726         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16727                 error "${tfile}_ln different after migration"
16728
16729         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16730                 error "${tfile}_ln_other different after migration"
16731
16732         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
16733         [ $stripe_count = 2 ] ||
16734                 error "dir strpe_count $d != 2 after migration."
16735
16736         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
16737         [ $stripe_count = 2 ] ||
16738                 error "file strpe_count $d != 2 after migration."
16739
16740         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16741 }
16742 run_test 230b "migrate directory"
16743
16744 test_230c() {
16745         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16746         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16747         remote_mds_nodsh && skip "remote MDS with nodsh"
16748         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16749                 skip "Need MDS version at least 2.11.52"
16750
16751         local MDTIDX=1
16752         local total=3
16753         local mdt_index
16754         local file
16755         local migrate_dir=$DIR/$tdir/migrate_dir
16756
16757         #If migrating directory fails in the middle, all entries of
16758         #the directory is still accessiable.
16759         test_mkdir $DIR/$tdir
16760         test_mkdir -i0 -c1 $migrate_dir
16761         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
16762         stat $migrate_dir
16763         createmany -o $migrate_dir/f $total ||
16764                 error "create files under ${migrate_dir} failed"
16765
16766         # fail after migrating top dir, and this will fail only once, so the
16767         # first sub file migration will fail (currently f3), others succeed.
16768         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
16769         do_facet mds1 lctl set_param fail_loc=0x1801
16770         local t=$(ls $migrate_dir | wc -l)
16771         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
16772                 error "migrate should fail"
16773         local u=$(ls $migrate_dir | wc -l)
16774         [ "$u" == "$t" ] || error "$u != $t during migration"
16775
16776         # add new dir/file should succeed
16777         mkdir $migrate_dir/dir ||
16778                 error "mkdir failed under migrating directory"
16779         touch $migrate_dir/file ||
16780                 error "create file failed under migrating directory"
16781
16782         # add file with existing name should fail
16783         for file in $migrate_dir/f*; do
16784                 stat $file > /dev/null || error "stat $file failed"
16785                 $OPENFILE -f O_CREAT:O_EXCL $file &&
16786                         error "open(O_CREAT|O_EXCL) $file should fail"
16787                 $MULTIOP $file m && error "create $file should fail"
16788                 touch $DIR/$tdir/remote_dir/$tfile ||
16789                         error "touch $tfile failed"
16790                 ln $DIR/$tdir/remote_dir/$tfile $file &&
16791                         error "link $file should fail"
16792                 mdt_index=$($LFS getstripe -m $file)
16793                 if [ $mdt_index == 0 ]; then
16794                         # file failed to migrate is not allowed to rename to
16795                         mv $DIR/$tdir/remote_dir/$tfile $file &&
16796                                 error "rename to $file should fail"
16797                 else
16798                         mv $DIR/$tdir/remote_dir/$tfile $file ||
16799                                 error "rename to $file failed"
16800                 fi
16801                 echo hello >> $file || error "write $file failed"
16802         done
16803
16804         # resume migration with different options should fail
16805         $LFS migrate -m 0 $migrate_dir &&
16806                 error "migrate -m 0 $migrate_dir should fail"
16807
16808         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
16809                 error "migrate -c 2 $migrate_dir should fail"
16810
16811         # resume migration should succeed
16812         $LFS migrate -m $MDTIDX $migrate_dir ||
16813                 error "migrate $migrate_dir failed"
16814
16815         echo "Finish migration, then checking.."
16816         for file in $(find $migrate_dir); do
16817                 mdt_index=$($LFS getstripe -m $file)
16818                 [ $mdt_index == $MDTIDX ] ||
16819                         error "$file is not on MDT${MDTIDX}"
16820         done
16821
16822         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16823 }
16824 run_test 230c "check directory accessiblity if migration failed"
16825
16826 test_230d() {
16827         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16828         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16829         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16830                 skip "Need MDS version at least 2.11.52"
16831         # LU-11235
16832         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
16833
16834         local migrate_dir=$DIR/$tdir/migrate_dir
16835         local old_index
16836         local new_index
16837         local old_count
16838         local new_count
16839         local new_hash
16840         local mdt_index
16841         local i
16842         local j
16843
16844         old_index=$((RANDOM % MDSCOUNT))
16845         old_count=$((MDSCOUNT - old_index))
16846         new_index=$((RANDOM % MDSCOUNT))
16847         new_count=$((MDSCOUNT - new_index))
16848         new_hash="all_char"
16849
16850         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
16851         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
16852
16853         test_mkdir $DIR/$tdir
16854         test_mkdir -i $old_index -c $old_count $migrate_dir
16855
16856         for ((i=0; i<100; i++)); do
16857                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
16858                 createmany -o $migrate_dir/dir_${i}/f 100 ||
16859                         error "create files under remote dir failed $i"
16860         done
16861
16862         echo -n "Migrate from MDT$old_index "
16863         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
16864         echo -n "to MDT$new_index"
16865         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
16866         echo
16867
16868         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
16869         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
16870                 error "migrate remote dir error"
16871
16872         echo "Finish migration, then checking.."
16873         for file in $(find $migrate_dir); do
16874                 mdt_index=$($LFS getstripe -m $file)
16875                 if [ $mdt_index -lt $new_index ] ||
16876                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
16877                         error "$file is on MDT$mdt_index"
16878                 fi
16879         done
16880
16881         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16882 }
16883 run_test 230d "check migrate big directory"
16884
16885 test_230e() {
16886         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16887         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16888         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16889                 skip "Need MDS version at least 2.11.52"
16890
16891         local i
16892         local j
16893         local a_fid
16894         local b_fid
16895
16896         mkdir -p $DIR/$tdir
16897         mkdir $DIR/$tdir/migrate_dir
16898         mkdir $DIR/$tdir/other_dir
16899         touch $DIR/$tdir/migrate_dir/a
16900         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
16901         ls $DIR/$tdir/other_dir
16902
16903         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16904                 error "migrate dir fails"
16905
16906         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16907         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16908
16909         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16910         [ $mdt_index == 0 ] || error "a is not on MDT0"
16911
16912         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
16913                 error "migrate dir fails"
16914
16915         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
16916         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
16917
16918         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16919         [ $mdt_index == 1 ] || error "a is not on MDT1"
16920
16921         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
16922         [ $mdt_index == 1 ] || error "b is not on MDT1"
16923
16924         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16925         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
16926
16927         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
16928
16929         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16930 }
16931 run_test 230e "migrate mulitple local link files"
16932
16933 test_230f() {
16934         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16935         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16936         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16937                 skip "Need MDS version at least 2.11.52"
16938
16939         local a_fid
16940         local ln_fid
16941
16942         mkdir -p $DIR/$tdir
16943         mkdir $DIR/$tdir/migrate_dir
16944         $LFS mkdir -i1 $DIR/$tdir/other_dir
16945         touch $DIR/$tdir/migrate_dir/a
16946         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
16947         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
16948         ls $DIR/$tdir/other_dir
16949
16950         # a should be migrated to MDT1, since no other links on MDT0
16951         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16952                 error "#1 migrate dir fails"
16953         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16954         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16955         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16956         [ $mdt_index == 1 ] || error "a is not on MDT1"
16957
16958         # a should stay on MDT1, because it is a mulitple link file
16959         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16960                 error "#2 migrate dir fails"
16961         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16962         [ $mdt_index == 1 ] || error "a is not on MDT1"
16963
16964         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16965                 error "#3 migrate dir fails"
16966
16967         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16968         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
16969         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
16970
16971         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
16972         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
16973
16974         # a should be migrated to MDT0, since no other links on MDT1
16975         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16976                 error "#4 migrate dir fails"
16977         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16978         [ $mdt_index == 0 ] || error "a is not on MDT0"
16979
16980         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16981 }
16982 run_test 230f "migrate mulitple remote link files"
16983
16984 test_230g() {
16985         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16986         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16987         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16988                 skip "Need MDS version at least 2.11.52"
16989
16990         mkdir -p $DIR/$tdir/migrate_dir
16991
16992         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
16993                 error "migrating dir to non-exist MDT succeeds"
16994         true
16995 }
16996 run_test 230g "migrate dir to non-exist MDT"
16997
16998 test_230h() {
16999         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17000         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17001         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17002                 skip "Need MDS version at least 2.11.52"
17003
17004         local mdt_index
17005
17006         mkdir -p $DIR/$tdir/migrate_dir
17007
17008         $LFS migrate -m1 $DIR &&
17009                 error "migrating mountpoint1 should fail"
17010
17011         $LFS migrate -m1 $DIR/$tdir/.. &&
17012                 error "migrating mountpoint2 should fail"
17013
17014         # same as mv
17015         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
17016                 error "migrating $tdir/migrate_dir/.. should fail"
17017
17018         true
17019 }
17020 run_test 230h "migrate .. and root"
17021
17022 test_230i() {
17023         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17024         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17025         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17026                 skip "Need MDS version at least 2.11.52"
17027
17028         mkdir -p $DIR/$tdir/migrate_dir
17029
17030         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
17031                 error "migration fails with a tailing slash"
17032
17033         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
17034                 error "migration fails with two tailing slashes"
17035 }
17036 run_test 230i "lfs migrate -m tolerates trailing slashes"
17037
17038 test_230j() {
17039         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17040         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17041                 skip "Need MDS version at least 2.11.52"
17042
17043         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
17044         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
17045                 error "create $tfile failed"
17046         cat /etc/passwd > $DIR/$tdir/$tfile
17047
17048         $LFS migrate -m 1 $DIR/$tdir
17049
17050         cmp /etc/passwd $DIR/$tdir/$tfile ||
17051                 error "DoM file mismatch after migration"
17052 }
17053 run_test 230j "DoM file data not changed after dir migration"
17054
17055 test_230k() {
17056         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
17057         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17058                 skip "Need MDS version at least 2.11.56"
17059
17060         local total=20
17061         local files_on_starting_mdt=0
17062
17063         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
17064         $LFS getdirstripe $DIR/$tdir
17065         for i in $(seq $total); do
17066                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
17067                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
17068                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17069         done
17070
17071         echo "$files_on_starting_mdt files on MDT0"
17072
17073         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
17074         $LFS getdirstripe $DIR/$tdir
17075
17076         files_on_starting_mdt=0
17077         for i in $(seq $total); do
17078                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
17079                         error "file $tfile.$i mismatch after migration"
17080                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
17081                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17082         done
17083
17084         echo "$files_on_starting_mdt files on MDT1 after migration"
17085         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
17086
17087         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
17088         $LFS getdirstripe $DIR/$tdir
17089
17090         files_on_starting_mdt=0
17091         for i in $(seq $total); do
17092                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
17093                         error "file $tfile.$i mismatch after 2nd migration"
17094                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
17095                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17096         done
17097
17098         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
17099         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
17100
17101         true
17102 }
17103 run_test 230k "file data not changed after dir migration"
17104
17105 test_230l() {
17106         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17107         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17108                 skip "Need MDS version at least 2.11.56"
17109
17110         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
17111         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
17112                 error "create files under remote dir failed $i"
17113         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
17114 }
17115 run_test 230l "readdir between MDTs won't crash"
17116
17117 test_230m() {
17118         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17119         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17120                 skip "Need MDS version at least 2.11.56"
17121
17122         local MDTIDX=1
17123         local mig_dir=$DIR/$tdir/migrate_dir
17124         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
17125         local shortstr="b"
17126         local val
17127
17128         echo "Creating files and dirs with xattrs"
17129         test_mkdir $DIR/$tdir
17130         test_mkdir -i0 -c1 $mig_dir
17131         mkdir $mig_dir/dir
17132         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
17133                 error "cannot set xattr attr1 on dir"
17134         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
17135                 error "cannot set xattr attr2 on dir"
17136         touch $mig_dir/dir/f0
17137         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
17138                 error "cannot set xattr attr1 on file"
17139         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
17140                 error "cannot set xattr attr2 on file"
17141         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17142         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17143         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
17144         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17145         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
17146         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17147         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
17148         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17149         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
17150
17151         echo "Migrating to MDT1"
17152         $LFS migrate -m $MDTIDX $mig_dir ||
17153                 error "fails on migrating dir to MDT1"
17154
17155         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17156         echo "Checking xattrs"
17157         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17158         [ "$val" = $longstr ] ||
17159                 error "expecting xattr1 $longstr on dir, found $val"
17160         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17161         [ "$val" = $shortstr ] ||
17162                 error "expecting xattr2 $shortstr on dir, found $val"
17163         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17164         [ "$val" = $longstr ] ||
17165                 error "expecting xattr1 $longstr on file, found $val"
17166         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17167         [ "$val" = $shortstr ] ||
17168                 error "expecting xattr2 $shortstr on file, found $val"
17169 }
17170 run_test 230m "xattrs not changed after dir migration"
17171
17172 test_231a()
17173 {
17174         # For simplicity this test assumes that max_pages_per_rpc
17175         # is the same across all OSCs
17176         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
17177         local bulk_size=$((max_pages * PAGE_SIZE))
17178         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
17179                                        head -n 1)
17180
17181         mkdir -p $DIR/$tdir
17182         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
17183                 error "failed to set stripe with -S ${brw_size}M option"
17184
17185         # clear the OSC stats
17186         $LCTL set_param osc.*.stats=0 &>/dev/null
17187         stop_writeback
17188
17189         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
17190         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
17191                 oflag=direct &>/dev/null || error "dd failed"
17192
17193         sync; sleep 1; sync # just to be safe
17194         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
17195         if [ x$nrpcs != "x1" ]; then
17196                 $LCTL get_param osc.*.stats
17197                 error "found $nrpcs ost_write RPCs, not 1 as expected"
17198         fi
17199
17200         start_writeback
17201         # Drop the OSC cache, otherwise we will read from it
17202         cancel_lru_locks osc
17203
17204         # clear the OSC stats
17205         $LCTL set_param osc.*.stats=0 &>/dev/null
17206
17207         # Client reads $bulk_size.
17208         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
17209                 iflag=direct &>/dev/null || error "dd failed"
17210
17211         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
17212         if [ x$nrpcs != "x1" ]; then
17213                 $LCTL get_param osc.*.stats
17214                 error "found $nrpcs ost_read RPCs, not 1 as expected"
17215         fi
17216 }
17217 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
17218
17219 test_231b() {
17220         mkdir -p $DIR/$tdir
17221         local i
17222         for i in {0..1023}; do
17223                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
17224                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
17225                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
17226         done
17227         sync
17228 }
17229 run_test 231b "must not assert on fully utilized OST request buffer"
17230
17231 test_232a() {
17232         mkdir -p $DIR/$tdir
17233         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17234
17235         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17236         do_facet ost1 $LCTL set_param fail_loc=0x31c
17237
17238         # ignore dd failure
17239         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
17240
17241         do_facet ost1 $LCTL set_param fail_loc=0
17242         umount_client $MOUNT || error "umount failed"
17243         mount_client $MOUNT || error "mount failed"
17244         stop ost1 || error "cannot stop ost1"
17245         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17246 }
17247 run_test 232a "failed lock should not block umount"
17248
17249 test_232b() {
17250         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
17251                 skip "Need MDS version at least 2.10.58"
17252
17253         mkdir -p $DIR/$tdir
17254         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17255         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
17256         sync
17257         cancel_lru_locks osc
17258
17259         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17260         do_facet ost1 $LCTL set_param fail_loc=0x31c
17261
17262         # ignore failure
17263         $LFS data_version $DIR/$tdir/$tfile || true
17264
17265         do_facet ost1 $LCTL set_param fail_loc=0
17266         umount_client $MOUNT || error "umount failed"
17267         mount_client $MOUNT || error "mount failed"
17268         stop ost1 || error "cannot stop ost1"
17269         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17270 }
17271 run_test 232b "failed data version lock should not block umount"
17272
17273 test_233a() {
17274         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
17275                 skip "Need MDS version at least 2.3.64"
17276         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17277
17278         local fid=$($LFS path2fid $MOUNT)
17279
17280         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17281                 error "cannot access $MOUNT using its FID '$fid'"
17282 }
17283 run_test 233a "checking that OBF of the FS root succeeds"
17284
17285 test_233b() {
17286         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
17287                 skip "Need MDS version at least 2.5.90"
17288         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17289
17290         local fid=$($LFS path2fid $MOUNT/.lustre)
17291
17292         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17293                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
17294
17295         fid=$($LFS path2fid $MOUNT/.lustre/fid)
17296         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17297                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
17298 }
17299 run_test 233b "checking that OBF of the FS .lustre succeeds"
17300
17301 test_234() {
17302         local p="$TMP/sanityN-$TESTNAME.parameters"
17303         save_lustre_params client "llite.*.xattr_cache" > $p
17304         lctl set_param llite.*.xattr_cache 1 ||
17305                 skip_env "xattr cache is not supported"
17306
17307         mkdir -p $DIR/$tdir || error "mkdir failed"
17308         touch $DIR/$tdir/$tfile || error "touch failed"
17309         # OBD_FAIL_LLITE_XATTR_ENOMEM
17310         $LCTL set_param fail_loc=0x1405
17311         getfattr -n user.attr $DIR/$tdir/$tfile &&
17312                 error "getfattr should have failed with ENOMEM"
17313         $LCTL set_param fail_loc=0x0
17314         rm -rf $DIR/$tdir
17315
17316         restore_lustre_params < $p
17317         rm -f $p
17318 }
17319 run_test 234 "xattr cache should not crash on ENOMEM"
17320
17321 test_235() {
17322         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
17323                 skip "Need MDS version at least 2.4.52"
17324
17325         flock_deadlock $DIR/$tfile
17326         local RC=$?
17327         case $RC in
17328                 0)
17329                 ;;
17330                 124) error "process hangs on a deadlock"
17331                 ;;
17332                 *) error "error executing flock_deadlock $DIR/$tfile"
17333                 ;;
17334         esac
17335 }
17336 run_test 235 "LU-1715: flock deadlock detection does not work properly"
17337
17338 #LU-2935
17339 test_236() {
17340         check_swap_layouts_support
17341
17342         local ref1=/etc/passwd
17343         local ref2=/etc/group
17344         local file1=$DIR/$tdir/f1
17345         local file2=$DIR/$tdir/f2
17346
17347         test_mkdir -c1 $DIR/$tdir
17348         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
17349         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
17350         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
17351         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
17352         local fd=$(free_fd)
17353         local cmd="exec $fd<>$file2"
17354         eval $cmd
17355         rm $file2
17356         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
17357                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
17358         cmd="exec $fd>&-"
17359         eval $cmd
17360         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
17361
17362         #cleanup
17363         rm -rf $DIR/$tdir
17364 }
17365 run_test 236 "Layout swap on open unlinked file"
17366
17367 # LU-4659 linkea consistency
17368 test_238() {
17369         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
17370                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
17371                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
17372                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
17373
17374         touch $DIR/$tfile
17375         ln $DIR/$tfile $DIR/$tfile.lnk
17376         touch $DIR/$tfile.new
17377         mv $DIR/$tfile.new $DIR/$tfile
17378         local fid1=$($LFS path2fid $DIR/$tfile)
17379         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
17380         local path1=$($LFS fid2path $FSNAME "$fid1")
17381         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
17382         local path2=$($LFS fid2path $FSNAME "$fid2")
17383         [ $tfile.lnk == $path2 ] ||
17384                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
17385         rm -f $DIR/$tfile*
17386 }
17387 run_test 238 "Verify linkea consistency"
17388
17389 test_239A() { # was test_239
17390         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
17391                 skip "Need MDS version at least 2.5.60"
17392
17393         local list=$(comma_list $(mdts_nodes))
17394
17395         mkdir -p $DIR/$tdir
17396         createmany -o $DIR/$tdir/f- 5000
17397         unlinkmany $DIR/$tdir/f- 5000
17398         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
17399                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
17400         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
17401                         osp.*MDT*.sync_in_flight" | calc_sum)
17402         [ "$changes" -eq 0 ] || error "$changes not synced"
17403 }
17404 run_test 239A "osp_sync test"
17405
17406 test_239a() { #LU-5297
17407         remote_mds_nodsh && skip "remote MDS with nodsh"
17408
17409         touch $DIR/$tfile
17410         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
17411         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
17412         chgrp $RUNAS_GID $DIR/$tfile
17413         wait_delete_completed
17414 }
17415 run_test 239a "process invalid osp sync record correctly"
17416
17417 test_239b() { #LU-5297
17418         remote_mds_nodsh && skip "remote MDS with nodsh"
17419
17420         touch $DIR/$tfile1
17421         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
17422         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
17423         chgrp $RUNAS_GID $DIR/$tfile1
17424         wait_delete_completed
17425         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17426         touch $DIR/$tfile2
17427         chgrp $RUNAS_GID $DIR/$tfile2
17428         wait_delete_completed
17429 }
17430 run_test 239b "process osp sync record with ENOMEM error correctly"
17431
17432 test_240() {
17433         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17434         remote_mds_nodsh && skip "remote MDS with nodsh"
17435
17436         mkdir -p $DIR/$tdir
17437
17438         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
17439                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
17440         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
17441                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
17442
17443         umount_client $MOUNT || error "umount failed"
17444         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
17445         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
17446         mount_client $MOUNT || error "failed to mount client"
17447
17448         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
17449         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
17450 }
17451 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
17452
17453 test_241_bio() {
17454         local count=$1
17455         local bsize=$2
17456
17457         for LOOP in $(seq $count); do
17458                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
17459                 cancel_lru_locks $OSC || true
17460         done
17461 }
17462
17463 test_241_dio() {
17464         local count=$1
17465         local bsize=$2
17466
17467         for LOOP in $(seq $1); do
17468                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
17469                         2>/dev/null
17470         done
17471 }
17472
17473 test_241a() { # was test_241
17474         local bsize=$PAGE_SIZE
17475
17476         (( bsize < 40960 )) && bsize=40960
17477         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17478         ls -la $DIR/$tfile
17479         cancel_lru_locks $OSC
17480         test_241_bio 1000 $bsize &
17481         PID=$!
17482         test_241_dio 1000 $bsize
17483         wait $PID
17484 }
17485 run_test 241a "bio vs dio"
17486
17487 test_241b() {
17488         local bsize=$PAGE_SIZE
17489
17490         (( bsize < 40960 )) && bsize=40960
17491         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17492         ls -la $DIR/$tfile
17493         test_241_dio 1000 $bsize &
17494         PID=$!
17495         test_241_dio 1000 $bsize
17496         wait $PID
17497 }
17498 run_test 241b "dio vs dio"
17499
17500 test_242() {
17501         remote_mds_nodsh && skip "remote MDS with nodsh"
17502
17503         mkdir -p $DIR/$tdir
17504         touch $DIR/$tdir/$tfile
17505
17506         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
17507         do_facet mds1 lctl set_param fail_loc=0x105
17508         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
17509
17510         do_facet mds1 lctl set_param fail_loc=0
17511         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
17512 }
17513 run_test 242 "mdt_readpage failure should not cause directory unreadable"
17514
17515 test_243()
17516 {
17517         test_mkdir $DIR/$tdir
17518         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
17519 }
17520 run_test 243 "various group lock tests"
17521
17522 test_244a()
17523 {
17524         test_mkdir $DIR/$tdir
17525         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
17526         sendfile_grouplock $DIR/$tdir/$tfile || \
17527                 error "sendfile+grouplock failed"
17528         rm -rf $DIR/$tdir
17529 }
17530 run_test 244a "sendfile with group lock tests"
17531
17532 test_244b()
17533 {
17534         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
17535
17536         local threads=50
17537         local size=$((1024*1024))
17538
17539         test_mkdir $DIR/$tdir
17540         for i in $(seq 1 $threads); do
17541                 local file=$DIR/$tdir/file_$((i / 10))
17542                 $MULTIOP $file OG1234w$size_$((i % 3))w$size_$((i % 4))g1234c &
17543                 local pids[$i]=$!
17544         done
17545         for i in $(seq 1 $threads); do
17546                 wait ${pids[$i]}
17547         done
17548 }
17549 run_test 244b "multi-threaded write with group lock"
17550
17551 test_245() {
17552         local flagname="multi_mod_rpcs"
17553         local connect_data_name="max_mod_rpcs"
17554         local out
17555
17556         # check if multiple modify RPCs flag is set
17557         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
17558                 grep "connect_flags:")
17559         echo "$out"
17560
17561         echo "$out" | grep -qw $flagname
17562         if [ $? -ne 0 ]; then
17563                 echo "connect flag $flagname is not set"
17564                 return
17565         fi
17566
17567         # check if multiple modify RPCs data is set
17568         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
17569         echo "$out"
17570
17571         echo "$out" | grep -qw $connect_data_name ||
17572                 error "import should have connect data $connect_data_name"
17573 }
17574 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
17575
17576 cleanup_247() {
17577         local submount=$1
17578
17579         trap 0
17580         umount_client $submount
17581         rmdir $submount
17582 }
17583
17584 test_247a() {
17585         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17586                 grep -q subtree ||
17587                 skip_env "Fileset feature is not supported"
17588
17589         local submount=${MOUNT}_$tdir
17590
17591         mkdir $MOUNT/$tdir
17592         mkdir -p $submount || error "mkdir $submount failed"
17593         FILESET="$FILESET/$tdir" mount_client $submount ||
17594                 error "mount $submount failed"
17595         trap "cleanup_247 $submount" EXIT
17596         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
17597         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
17598                 error "read $MOUNT/$tdir/$tfile failed"
17599         cleanup_247 $submount
17600 }
17601 run_test 247a "mount subdir as fileset"
17602
17603 test_247b() {
17604         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17605                 skip_env "Fileset feature is not supported"
17606
17607         local submount=${MOUNT}_$tdir
17608
17609         rm -rf $MOUNT/$tdir
17610         mkdir -p $submount || error "mkdir $submount failed"
17611         SKIP_FILESET=1
17612         FILESET="$FILESET/$tdir" mount_client $submount &&
17613                 error "mount $submount should fail"
17614         rmdir $submount
17615 }
17616 run_test 247b "mount subdir that dose not exist"
17617
17618 test_247c() {
17619         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17620                 skip_env "Fileset feature is not supported"
17621
17622         local submount=${MOUNT}_$tdir
17623
17624         mkdir -p $MOUNT/$tdir/dir1
17625         mkdir -p $submount || error "mkdir $submount failed"
17626         trap "cleanup_247 $submount" EXIT
17627         FILESET="$FILESET/$tdir" mount_client $submount ||
17628                 error "mount $submount failed"
17629         local fid=$($LFS path2fid $MOUNT/)
17630         $LFS fid2path $submount $fid && error "fid2path should fail"
17631         cleanup_247 $submount
17632 }
17633 run_test 247c "running fid2path outside root"
17634
17635 test_247d() {
17636         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17637                 skip "Fileset feature is not supported"
17638
17639         local submount=${MOUNT}_$tdir
17640
17641         mkdir -p $MOUNT/$tdir/dir1
17642         mkdir -p $submount || error "mkdir $submount failed"
17643         FILESET="$FILESET/$tdir" mount_client $submount ||
17644                 error "mount $submount failed"
17645         trap "cleanup_247 $submount" EXIT
17646         local fid=$($LFS path2fid $submount/dir1)
17647         $LFS fid2path $submount $fid || error "fid2path should succeed"
17648         cleanup_247 $submount
17649 }
17650 run_test 247d "running fid2path inside root"
17651
17652 # LU-8037
17653 test_247e() {
17654         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17655                 grep -q subtree ||
17656                 skip "Fileset feature is not supported"
17657
17658         local submount=${MOUNT}_$tdir
17659
17660         mkdir $MOUNT/$tdir
17661         mkdir -p $submount || error "mkdir $submount failed"
17662         FILESET="$FILESET/.." mount_client $submount &&
17663                 error "mount $submount should fail"
17664         rmdir $submount
17665 }
17666 run_test 247e "mount .. as fileset"
17667
17668 test_248a() {
17669         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
17670         [ -z "$fast_read_sav" ] && skip "no fast read support"
17671
17672         # create a large file for fast read verification
17673         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
17674
17675         # make sure the file is created correctly
17676         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
17677                 { rm -f $DIR/$tfile; skip "file creation error"; }
17678
17679         echo "Test 1: verify that fast read is 4 times faster on cache read"
17680
17681         # small read with fast read enabled
17682         $LCTL set_param -n llite.*.fast_read=1
17683         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17684                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17685                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17686         # small read with fast read disabled
17687         $LCTL set_param -n llite.*.fast_read=0
17688         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17689                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17690                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17691
17692         # verify that fast read is 4 times faster for cache read
17693         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
17694                 error_not_in_vm "fast read was not 4 times faster: " \
17695                            "$t_fast vs $t_slow"
17696
17697         echo "Test 2: verify the performance between big and small read"
17698         $LCTL set_param -n llite.*.fast_read=1
17699
17700         # 1k non-cache read
17701         cancel_lru_locks osc
17702         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17703                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17704                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17705
17706         # 1M non-cache read
17707         cancel_lru_locks osc
17708         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17709                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17710                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17711
17712         # verify that big IO is not 4 times faster than small IO
17713         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
17714                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
17715
17716         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
17717         rm -f $DIR/$tfile
17718 }
17719 run_test 248a "fast read verification"
17720
17721 test_248b() {
17722         # Default short_io_bytes=16384, try both smaller and larger sizes.
17723         # Lustre O_DIRECT read and write needs to be a multiple of PAGE_SIZE.
17724         # 6017024 = 2^12*13*113 = 47008*128 = 11752*512 = 4096*1469 = 53248*113
17725         echo "bs=53248 count=113 normal buffered write"
17726         dd if=/dev/urandom of=$TMP/$tfile.0 bs=53248 count=113 ||
17727                 error "dd of initial data file failed"
17728         stack_trap "rm -f $DIR/$tfile.[0-3] $TMP/$tfile.[0-3]" EXIT
17729
17730         echo "bs=47008 count=128 oflag=dsync normal write $tfile.0"
17731         dd if=$TMP/$tfile.0 of=$DIR/$tfile.0 bs=47008 count=128 oflag=dsync ||
17732                 error "dd with sync normal writes failed"
17733         cmp $TMP/$tfile.0 $DIR/$tfile.0 || error "compare $DIR/$tfile.0 failed"
17734
17735         echo "bs=11752 count=512 oflag=dsync small write $tfile.1"
17736         dd if=$TMP/$tfile.0 of=$DIR/$tfile.1 bs=11752 count=512 oflag=dsync ||
17737                 error "dd with sync small writes failed"
17738         cmp $TMP/$tfile.0 $DIR/$tfile.1 || error "compare $DIR/$tfile.1 failed"
17739
17740         cancel_lru_locks osc
17741
17742         # calculate the small O_DIRECT size and count for the client PAGE_SIZE
17743         local num=$((13 * 113 / (PAGE_SIZE / 4096)))
17744         echo "bs=$PAGE_SIZE count=$num iflag=direct small read $tfile.1"
17745         dd if=$DIR/$tfile.1 of=$TMP/$tfile.1 bs=$PAGE_SIZE count=$num \
17746                 iflag=direct || error "dd with O_DIRECT small read failed"
17747         # adjust bytes checked to handle larger PAGE_SIZE for ARM/PPC
17748         cmp --bytes=$((PAGE_SIZE * num)) $TMP/$tfile.0 $TMP/$tfile.1 ||
17749                 error "compare $TMP/$tfile.1 failed"
17750
17751         local save=$($LCTL get_param -n osc.*OST000*.short_io_bytes | head -n 1)
17752         stack_trap "$LCTL set_param osc.$FSNAME-*.short_io_bytes=$save" EXIT
17753
17754         # just to see what the maximum tunable value is, and test parsing
17755         echo "test invalid parameter 2MB"
17756         $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=2M &&
17757                 error "too-large short_io_bytes allowed"
17758         echo "test maximum parameter 512KB"
17759         # if we can set a larger short_io_bytes, run test regardless of version
17760         if ! $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=512K; then
17761                 # older clients may not allow setting it this large, that's OK
17762                 [ $CLIENT_VERSION -ge $(version_code 2.13.50) ] ||
17763                         skip "Need at least client version 2.13.50"
17764                 error "medium short_io_bytes failed"
17765         fi
17766         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
17767         size=$($LCTL get_param -n osc.$FSNAME-OST0000*.short_io_bytes)
17768
17769         echo "test large parameter 64KB"
17770         $LCTL set_param osc.$FSNAME-*.short_io_bytes=65536
17771         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
17772
17773         echo "bs=47008 count=128 oflag=dsync large write $tfile.2"
17774         dd if=$TMP/$tfile.0 of=$DIR/$tfile.2 bs=47008 count=128 oflag=dsync ||
17775                 error "dd with sync large writes failed"
17776         cmp $TMP/$tfile.0 $DIR/$tfile.2 || error "compare $DIR/$tfile.2 failed"
17777
17778         # calculate the large O_DIRECT size and count for the client PAGE_SIZE
17779         local size=$(((4096 * 13 + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE))
17780         num=$((113 * 4096 / PAGE_SIZE))
17781         echo "bs=$size count=$num oflag=direct large write $tfile.3"
17782         dd if=$TMP/$tfile.0 of=$DIR/$tfile.3 bs=$size count=$num oflag=direct ||
17783                 error "dd with O_DIRECT large writes failed"
17784         cmp --bytes=$((size * num)) $TMP/$tfile.0 $DIR/$tfile.3 ||
17785                 error "compare $DIR/$tfile.3 failed"
17786
17787         cancel_lru_locks osc
17788
17789         echo "bs=$size count=$num iflag=direct large read $tfile.2"
17790         dd if=$DIR/$tfile.2 of=$TMP/$tfile.2 bs=$size count=$num iflag=direct ||
17791                 error "dd with O_DIRECT large read failed"
17792         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.2 ||
17793                 error "compare $TMP/$tfile.2 failed"
17794
17795         echo "bs=$size count=$num iflag=direct large read $tfile.3"
17796         dd if=$DIR/$tfile.3 of=$TMP/$tfile.3 bs=$size count=$num iflag=direct ||
17797                 error "dd with O_DIRECT large read failed"
17798         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.3 ||
17799                 error "compare $TMP/$tfile.3 failed"
17800 }
17801 run_test 248b "test short_io read and write for both small and large sizes"
17802
17803 test_249() { # LU-7890
17804         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
17805                 skip "Need at least version 2.8.54"
17806
17807         rm -f $DIR/$tfile
17808         $LFS setstripe -c 1 $DIR/$tfile
17809         # Offset 2T == 4k * 512M
17810         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
17811                 error "dd to 2T offset failed"
17812 }
17813 run_test 249 "Write above 2T file size"
17814
17815 test_250() {
17816         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
17817          && skip "no 16TB file size limit on ZFS"
17818
17819         $LFS setstripe -c 1 $DIR/$tfile
17820         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
17821         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
17822         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
17823         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
17824                 conv=notrunc,fsync && error "append succeeded"
17825         return 0
17826 }
17827 run_test 250 "Write above 16T limit"
17828
17829 test_251() {
17830         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
17831
17832         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
17833         #Skip once - writing the first stripe will succeed
17834         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17835         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
17836                 error "short write happened"
17837
17838         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17839         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
17840                 error "short read happened"
17841
17842         rm -f $DIR/$tfile
17843 }
17844 run_test 251 "Handling short read and write correctly"
17845
17846 test_252() {
17847         remote_mds_nodsh && skip "remote MDS with nodsh"
17848         remote_ost_nodsh && skip "remote OST with nodsh"
17849         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
17850                 skip_env "ldiskfs only test"
17851         fi
17852
17853         local tgt
17854         local dev
17855         local out
17856         local uuid
17857         local num
17858         local gen
17859
17860         # check lr_reader on OST0000
17861         tgt=ost1
17862         dev=$(facet_device $tgt)
17863         out=$(do_facet $tgt $LR_READER $dev)
17864         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17865         echo "$out"
17866         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
17867         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
17868                 error "Invalid uuid returned by $LR_READER on target $tgt"
17869         echo -e "uuid returned by $LR_READER is '$uuid'\n"
17870
17871         # check lr_reader -c on MDT0000
17872         tgt=mds1
17873         dev=$(facet_device $tgt)
17874         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
17875                 skip "$LR_READER does not support additional options"
17876         fi
17877         out=$(do_facet $tgt $LR_READER -c $dev)
17878         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17879         echo "$out"
17880         num=$(echo "$out" | grep -c "mdtlov")
17881         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
17882                 error "Invalid number of mdtlov clients returned by $LR_READER"
17883         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
17884
17885         # check lr_reader -cr on MDT0000
17886         out=$(do_facet $tgt $LR_READER -cr $dev)
17887         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17888         echo "$out"
17889         echo "$out" | grep -q "^reply_data:$" ||
17890                 error "$LR_READER should have returned 'reply_data' section"
17891         num=$(echo "$out" | grep -c "client_generation")
17892         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
17893 }
17894 run_test 252 "check lr_reader tool"
17895
17896 test_253() {
17897         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17898         remote_mds_nodsh && skip "remote MDS with nodsh"
17899         remote_mgs_nodsh && skip "remote MGS with nodsh"
17900
17901         local ostidx=0
17902         local rc=0
17903         local ost_name=$(ostname_from_index $ostidx)
17904
17905         # on the mdt's osc
17906         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
17907         do_facet $SINGLEMDS $LCTL get_param -n \
17908                 osp.$mdtosc_proc1.reserved_mb_high ||
17909                 skip  "remote MDS does not support reserved_mb_high"
17910
17911         rm -rf $DIR/$tdir
17912         wait_mds_ost_sync
17913         wait_delete_completed
17914         mkdir $DIR/$tdir
17915
17916         pool_add $TESTNAME || error "Pool creation failed"
17917         pool_add_targets $TESTNAME 0 || error "Pool add targets failed"
17918
17919         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
17920                 error "Setstripe failed"
17921
17922         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M count=10
17923
17924         local wms=$(ost_watermarks_set_enospc $tfile $ostidx |
17925                     grep "watermarks")
17926         stack_trap "ost_watermarks_clear_enospc $tfile $ostidx $wms" EXIT
17927
17928         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17929                         osp.$mdtosc_proc1.prealloc_status)
17930         echo "prealloc_status $oa_status"
17931
17932         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1M count=1 &&
17933                 error "File creation should fail"
17934
17935         #object allocation was stopped, but we still able to append files
17936         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M seek=6 count=5 \
17937                 oflag=append || error "Append failed"
17938
17939         rm -f $DIR/$tdir/$tfile.0
17940
17941         # For this test, we want to delete the files we created to go out of
17942         # space but leave the watermark, so we remain nearly out of space
17943         ost_watermarks_enospc_delete_files $tfile $ostidx
17944
17945         wait_delete_completed
17946
17947         sleep_maxage
17948
17949         for i in $(seq 10 12); do
17950                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$i bs=1M count=1 \
17951                         2>/dev/null || error "File creation failed after rm"
17952         done
17953
17954         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17955                         osp.$mdtosc_proc1.prealloc_status)
17956         echo "prealloc_status $oa_status"
17957
17958         if (( oa_status != 0 )); then
17959                 error "Object allocation still disable after rm"
17960         fi
17961 }
17962 run_test 253 "Check object allocation limit"
17963
17964 test_254() {
17965         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17966         remote_mds_nodsh && skip "remote MDS with nodsh"
17967         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
17968                 skip "MDS does not support changelog_size"
17969
17970         local cl_user
17971         local MDT0=$(facet_svc $SINGLEMDS)
17972
17973         changelog_register || error "changelog_register failed"
17974
17975         changelog_clear 0 || error "changelog_clear failed"
17976
17977         local size1=$(do_facet $SINGLEMDS \
17978                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17979         echo "Changelog size $size1"
17980
17981         rm -rf $DIR/$tdir
17982         $LFS mkdir -i 0 $DIR/$tdir
17983         # change something
17984         mkdir -p $DIR/$tdir/pics/2008/zachy
17985         touch $DIR/$tdir/pics/2008/zachy/timestamp
17986         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
17987         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
17988         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
17989         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
17990         rm $DIR/$tdir/pics/desktop.jpg
17991
17992         local size2=$(do_facet $SINGLEMDS \
17993                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17994         echo "Changelog size after work $size2"
17995
17996         (( $size2 > $size1 )) ||
17997                 error "new Changelog size=$size2 less than old size=$size1"
17998 }
17999 run_test 254 "Check changelog size"
18000
18001 ladvise_no_type()
18002 {
18003         local type=$1
18004         local file=$2
18005
18006         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
18007                 awk -F: '{print $2}' | grep $type > /dev/null
18008         if [ $? -ne 0 ]; then
18009                 return 0
18010         fi
18011         return 1
18012 }
18013
18014 ladvise_no_ioctl()
18015 {
18016         local file=$1
18017
18018         lfs ladvise -a willread $file > /dev/null 2>&1
18019         if [ $? -eq 0 ]; then
18020                 return 1
18021         fi
18022
18023         lfs ladvise -a willread $file 2>&1 |
18024                 grep "Inappropriate ioctl for device" > /dev/null
18025         if [ $? -eq 0 ]; then
18026                 return 0
18027         fi
18028         return 1
18029 }
18030
18031 percent() {
18032         bc <<<"scale=2; ($1 - $2) * 100 / $2"
18033 }
18034
18035 # run a random read IO workload
18036 # usage: random_read_iops <filename> <filesize> <iosize>
18037 random_read_iops() {
18038         local file=$1
18039         local fsize=$2
18040         local iosize=${3:-4096}
18041
18042         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
18043                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
18044 }
18045
18046 drop_file_oss_cache() {
18047         local file="$1"
18048         local nodes="$2"
18049
18050         $LFS ladvise -a dontneed $file 2>/dev/null ||
18051                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
18052 }
18053
18054 ladvise_willread_performance()
18055 {
18056         local repeat=10
18057         local average_origin=0
18058         local average_cache=0
18059         local average_ladvise=0
18060
18061         for ((i = 1; i <= $repeat; i++)); do
18062                 echo "Iter $i/$repeat: reading without willread hint"
18063                 cancel_lru_locks osc
18064                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
18065                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
18066                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
18067                 average_origin=$(bc <<<"$average_origin + $speed_origin")
18068
18069                 cancel_lru_locks osc
18070                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
18071                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
18072                 average_cache=$(bc <<<"$average_cache + $speed_cache")
18073
18074                 cancel_lru_locks osc
18075                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
18076                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
18077                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
18078                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
18079                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
18080         done
18081         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
18082         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
18083         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
18084
18085         speedup_cache=$(percent $average_cache $average_origin)
18086         speedup_ladvise=$(percent $average_ladvise $average_origin)
18087
18088         echo "Average uncached read: $average_origin"
18089         echo "Average speedup with OSS cached read: " \
18090                 "$average_cache = +$speedup_cache%"
18091         echo "Average speedup with ladvise willread: " \
18092                 "$average_ladvise = +$speedup_ladvise%"
18093
18094         local lowest_speedup=20
18095         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
18096                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
18097                         "got $average_cache%. Skipping ladvise willread check."
18098                 return 0
18099         fi
18100
18101         # the test won't work on ZFS until it supports 'ladvise dontneed', but
18102         # it is still good to run until then to exercise 'ladvise willread'
18103         ! $LFS ladvise -a dontneed $DIR/$tfile &&
18104                 [ "$ost1_FSTYPE" = "zfs" ] &&
18105                 echo "osd-zfs does not support dontneed or drop_caches" &&
18106                 return 0
18107
18108         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
18109         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
18110                 error_not_in_vm "Speedup with willread is less than " \
18111                         "$lowest_speedup%, got $average_ladvise%"
18112 }
18113
18114 test_255a() {
18115         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
18116                 skip "lustre < 2.8.54 does not support ladvise "
18117         remote_ost_nodsh && skip "remote OST with nodsh"
18118
18119         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
18120
18121         ladvise_no_type willread $DIR/$tfile &&
18122                 skip "willread ladvise is not supported"
18123
18124         ladvise_no_ioctl $DIR/$tfile &&
18125                 skip "ladvise ioctl is not supported"
18126
18127         local size_mb=100
18128         local size=$((size_mb * 1048576))
18129         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
18130                 error "dd to $DIR/$tfile failed"
18131
18132         lfs ladvise -a willread $DIR/$tfile ||
18133                 error "Ladvise failed with no range argument"
18134
18135         lfs ladvise -a willread -s 0 $DIR/$tfile ||
18136                 error "Ladvise failed with no -l or -e argument"
18137
18138         lfs ladvise -a willread -e 1 $DIR/$tfile ||
18139                 error "Ladvise failed with only -e argument"
18140
18141         lfs ladvise -a willread -l 1 $DIR/$tfile ||
18142                 error "Ladvise failed with only -l argument"
18143
18144         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
18145                 error "End offset should not be smaller than start offset"
18146
18147         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
18148                 error "End offset should not be equal to start offset"
18149
18150         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
18151                 error "Ladvise failed with overflowing -s argument"
18152
18153         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
18154                 error "Ladvise failed with overflowing -e argument"
18155
18156         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
18157                 error "Ladvise failed with overflowing -l argument"
18158
18159         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
18160                 error "Ladvise succeeded with conflicting -l and -e arguments"
18161
18162         echo "Synchronous ladvise should wait"
18163         local delay=4
18164 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
18165         do_nodes $(comma_list $(osts_nodes)) \
18166                 $LCTL set_param fail_val=$delay fail_loc=0x237
18167
18168         local start_ts=$SECONDS
18169         lfs ladvise -a willread $DIR/$tfile ||
18170                 error "Ladvise failed with no range argument"
18171         local end_ts=$SECONDS
18172         local inteval_ts=$((end_ts - start_ts))
18173
18174         if [ $inteval_ts -lt $(($delay - 1)) ]; then
18175                 error "Synchronous advice didn't wait reply"
18176         fi
18177
18178         echo "Asynchronous ladvise shouldn't wait"
18179         local start_ts=$SECONDS
18180         lfs ladvise -a willread -b $DIR/$tfile ||
18181                 error "Ladvise failed with no range argument"
18182         local end_ts=$SECONDS
18183         local inteval_ts=$((end_ts - start_ts))
18184
18185         if [ $inteval_ts -gt $(($delay / 2)) ]; then
18186                 error "Asynchronous advice blocked"
18187         fi
18188
18189         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
18190         ladvise_willread_performance
18191 }
18192 run_test 255a "check 'lfs ladvise -a willread'"
18193
18194 facet_meminfo() {
18195         local facet=$1
18196         local info=$2
18197
18198         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
18199 }
18200
18201 test_255b() {
18202         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
18203                 skip "lustre < 2.8.54 does not support ladvise "
18204         remote_ost_nodsh && skip "remote OST with nodsh"
18205
18206         lfs setstripe -c 1 -i 0 $DIR/$tfile
18207
18208         ladvise_no_type dontneed $DIR/$tfile &&
18209                 skip "dontneed ladvise is not supported"
18210
18211         ladvise_no_ioctl $DIR/$tfile &&
18212                 skip "ladvise ioctl is not supported"
18213
18214         ! $LFS ladvise -a dontneed $DIR/$tfile &&
18215                 [ "$ost1_FSTYPE" = "zfs" ] &&
18216                 skip "zfs-osd does not support 'ladvise dontneed'"
18217
18218         local size_mb=100
18219         local size=$((size_mb * 1048576))
18220         # In order to prevent disturbance of other processes, only check 3/4
18221         # of the memory usage
18222         local kibibytes=$((size_mb * 1024 * 3 / 4))
18223
18224         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
18225                 error "dd to $DIR/$tfile failed"
18226
18227         #force write to complete before dropping OST cache & checking memory
18228         sync
18229
18230         local total=$(facet_meminfo ost1 MemTotal)
18231         echo "Total memory: $total KiB"
18232
18233         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
18234         local before_read=$(facet_meminfo ost1 Cached)
18235         echo "Cache used before read: $before_read KiB"
18236
18237         lfs ladvise -a willread $DIR/$tfile ||
18238                 error "Ladvise willread failed"
18239         local after_read=$(facet_meminfo ost1 Cached)
18240         echo "Cache used after read: $after_read KiB"
18241
18242         lfs ladvise -a dontneed $DIR/$tfile ||
18243                 error "Ladvise dontneed again failed"
18244         local no_read=$(facet_meminfo ost1 Cached)
18245         echo "Cache used after dontneed ladvise: $no_read KiB"
18246
18247         if [ $total -lt $((before_read + kibibytes)) ]; then
18248                 echo "Memory is too small, abort checking"
18249                 return 0
18250         fi
18251
18252         if [ $((before_read + kibibytes)) -gt $after_read ]; then
18253                 error "Ladvise willread should use more memory" \
18254                         "than $kibibytes KiB"
18255         fi
18256
18257         if [ $((no_read + kibibytes)) -gt $after_read ]; then
18258                 error "Ladvise dontneed should release more memory" \
18259                         "than $kibibytes KiB"
18260         fi
18261 }
18262 run_test 255b "check 'lfs ladvise -a dontneed'"
18263
18264 test_255c() {
18265         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
18266                 skip "lustre < 2.10.50 does not support lockahead"
18267
18268         local count
18269         local new_count
18270         local difference
18271         local i
18272         local rc
18273
18274         test_mkdir -p $DIR/$tdir
18275         $LFS setstripe -i 0 -c 1 $DIR/$tdir
18276
18277         #test 10 returns only success/failure
18278         i=10
18279         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18280         rc=$?
18281         if [ $rc -eq 255 ]; then
18282                 error "Ladvise test${i} failed, ${rc}"
18283         fi
18284
18285         #test 11 counts lock enqueue requests, all others count new locks
18286         i=11
18287         count=$(do_facet ost1 \
18288                 $LCTL get_param -n ost.OSS.ost.stats)
18289         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
18290
18291         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18292         rc=$?
18293         if [ $rc -eq 255 ]; then
18294                 error "Ladvise test${i} failed, ${rc}"
18295         fi
18296
18297         new_count=$(do_facet ost1 \
18298                 $LCTL get_param -n ost.OSS.ost.stats)
18299         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
18300                    awk '{ print $2 }')
18301
18302         difference="$((new_count - count))"
18303         if [ $difference -ne $rc ]; then
18304                 error "Ladvise test${i}, bad enqueue count, returned " \
18305                       "${rc}, actual ${difference}"
18306         fi
18307
18308         for i in $(seq 12 21); do
18309                 # If we do not do this, we run the risk of having too many
18310                 # locks and starting lock cancellation while we are checking
18311                 # lock counts.
18312                 cancel_lru_locks osc
18313
18314                 count=$($LCTL get_param -n \
18315                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
18316
18317                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
18318                 rc=$?
18319                 if [ $rc -eq 255 ]; then
18320                         error "Ladvise test ${i} failed, ${rc}"
18321                 fi
18322
18323                 new_count=$($LCTL get_param -n \
18324                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
18325                 difference="$((new_count - count))"
18326
18327                 # Test 15 output is divided by 100 to map down to valid return
18328                 if [ $i -eq 15 ]; then
18329                         rc="$((rc * 100))"
18330                 fi
18331
18332                 if [ $difference -ne $rc ]; then
18333                         error "Ladvise test ${i}, bad lock count, returned " \
18334                               "${rc}, actual ${difference}"
18335                 fi
18336         done
18337
18338         #test 22 returns only success/failure
18339         i=22
18340         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18341         rc=$?
18342         if [ $rc -eq 255 ]; then
18343                 error "Ladvise test${i} failed, ${rc}"
18344         fi
18345 }
18346 run_test 255c "suite of ladvise lockahead tests"
18347
18348 test_256() {
18349         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18350         remote_mds_nodsh && skip "remote MDS with nodsh"
18351         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18352         changelog_users $SINGLEMDS | grep "^cl" &&
18353                 skip "active changelog user"
18354
18355         local cl_user
18356         local cat_sl
18357         local mdt_dev
18358
18359         mdt_dev=$(mdsdevname 1)
18360         echo $mdt_dev
18361
18362         changelog_register || error "changelog_register failed"
18363
18364         rm -rf $DIR/$tdir
18365         mkdir -p $DIR/$tdir
18366
18367         changelog_clear 0 || error "changelog_clear failed"
18368
18369         # change something
18370         touch $DIR/$tdir/{1..10}
18371
18372         # stop the MDT
18373         stop $SINGLEMDS || error "Fail to stop MDT"
18374
18375         # remount the MDT
18376
18377         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
18378
18379         #after mount new plainllog is used
18380         touch $DIR/$tdir/{11..19}
18381         local tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
18382         stack_trap "rm -f $tmpfile"
18383         cat_sl=$(do_facet $SINGLEMDS "sync; \
18384                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18385                  llog_reader $tmpfile | grep -c type=1064553b")
18386         do_facet $SINGLEMDS llog_reader $tmpfile
18387
18388         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
18389
18390         changelog_clear 0 || error "changelog_clear failed"
18391
18392         cat_sl=$(do_facet $SINGLEMDS "sync; \
18393                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18394                  llog_reader $tmpfile | grep -c type=1064553b")
18395
18396         if (( cat_sl == 2 )); then
18397                 error "Empty plain llog was not deleted from changelog catalog"
18398         elif (( cat_sl != 1 )); then
18399                 error "Active plain llog shouldn't be deleted from catalog"
18400         fi
18401 }
18402 run_test 256 "Check llog delete for empty and not full state"
18403
18404 test_257() {
18405         remote_mds_nodsh && skip "remote MDS with nodsh"
18406         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
18407                 skip "Need MDS version at least 2.8.55"
18408
18409         test_mkdir $DIR/$tdir
18410
18411         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
18412                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
18413         stat $DIR/$tdir
18414
18415 #define OBD_FAIL_MDS_XATTR_REP                  0x161
18416         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18417         local facet=mds$((mdtidx + 1))
18418         set_nodes_failloc $(facet_active_host $facet) 0x80000161
18419         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
18420
18421         stop $facet || error "stop MDS failed"
18422         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
18423                 error "start MDS fail"
18424         wait_recovery_complete $facet
18425 }
18426 run_test 257 "xattr locks are not lost"
18427
18428 # Verify we take the i_mutex when security requires it
18429 test_258a() {
18430 #define OBD_FAIL_IMUTEX_SEC 0x141c
18431         $LCTL set_param fail_loc=0x141c
18432         touch $DIR/$tfile
18433         chmod u+s $DIR/$tfile
18434         chmod a+rwx $DIR/$tfile
18435         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18436         RC=$?
18437         if [ $RC -ne 0 ]; then
18438                 error "error, failed to take i_mutex, rc=$?"
18439         fi
18440         rm -f $DIR/$tfile
18441 }
18442 run_test 258a "verify i_mutex security behavior when suid attributes is set"
18443
18444 # Verify we do NOT take the i_mutex in the normal case
18445 test_258b() {
18446 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
18447         $LCTL set_param fail_loc=0x141d
18448         touch $DIR/$tfile
18449         chmod a+rwx $DIR
18450         chmod a+rw $DIR/$tfile
18451         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18452         RC=$?
18453         if [ $RC -ne 0 ]; then
18454                 error "error, took i_mutex unnecessarily, rc=$?"
18455         fi
18456         rm -f $DIR/$tfile
18457
18458 }
18459 run_test 258b "verify i_mutex security behavior"
18460
18461 test_259() {
18462         local file=$DIR/$tfile
18463         local before
18464         local after
18465
18466         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18467
18468         stack_trap "rm -f $file" EXIT
18469
18470         wait_delete_completed
18471         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18472         echo "before: $before"
18473
18474         $LFS setstripe -i 0 -c 1 $file
18475         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
18476         sync_all_data
18477         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18478         echo "after write: $after"
18479
18480 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
18481         do_facet ost1 $LCTL set_param fail_loc=0x2301
18482         $TRUNCATE $file 0
18483         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18484         echo "after truncate: $after"
18485
18486         stop ost1
18487         do_facet ost1 $LCTL set_param fail_loc=0
18488         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
18489         sleep 2
18490         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18491         echo "after restart: $after"
18492         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
18493                 error "missing truncate?"
18494
18495         return 0
18496 }
18497 run_test 259 "crash at delayed truncate"
18498
18499 test_260() {
18500 #define OBD_FAIL_MDC_CLOSE               0x806
18501         $LCTL set_param fail_loc=0x80000806
18502         touch $DIR/$tfile
18503
18504 }
18505 run_test 260 "Check mdc_close fail"
18506
18507 ### Data-on-MDT sanity tests ###
18508 test_270a() {
18509         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18510                 skip "Need MDS version at least 2.10.55 for DoM"
18511
18512         # create DoM file
18513         local dom=$DIR/$tdir/dom_file
18514         local tmp=$DIR/$tdir/tmp_file
18515
18516         mkdir -p $DIR/$tdir
18517
18518         # basic checks for DoM component creation
18519         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
18520                 error "Can set MDT layout to non-first entry"
18521
18522         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
18523                 error "Can define multiple entries as MDT layout"
18524
18525         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
18526
18527         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
18528         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
18529         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
18530
18531         local mdtidx=$($LFS getstripe -m $dom)
18532         local mdtname=MDT$(printf %04x $mdtidx)
18533         local facet=mds$((mdtidx + 1))
18534         local space_check=1
18535
18536         # Skip free space checks with ZFS
18537         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
18538
18539         # write
18540         sync
18541         local size_tmp=$((65536 * 3))
18542         local mdtfree1=$(do_facet $facet \
18543                          lctl get_param -n osd*.*$mdtname.kbytesfree)
18544
18545         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18546         # check also direct IO along write
18547         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
18548         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18549         sync
18550         cmp $tmp $dom || error "file data is different"
18551         [ $(stat -c%s $dom) == $size_tmp ] ||
18552                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18553         if [ $space_check == 1 ]; then
18554                 local mdtfree2=$(do_facet $facet \
18555                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
18556
18557                 # increase in usage from by $size_tmp
18558                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18559                         error "MDT free space wrong after write: " \
18560                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18561         fi
18562
18563         # truncate
18564         local size_dom=10000
18565
18566         $TRUNCATE $dom $size_dom
18567         [ $(stat -c%s $dom) == $size_dom ] ||
18568                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
18569         if [ $space_check == 1 ]; then
18570                 mdtfree1=$(do_facet $facet \
18571                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18572                 # decrease in usage from $size_tmp to new $size_dom
18573                 [ $(($mdtfree1 - $mdtfree2)) -ge \
18574                   $(((size_tmp - size_dom) / 1024)) ] ||
18575                         error "MDT free space is wrong after truncate: " \
18576                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
18577         fi
18578
18579         # append
18580         cat $tmp >> $dom
18581         sync
18582         size_dom=$((size_dom + size_tmp))
18583         [ $(stat -c%s $dom) == $size_dom ] ||
18584                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
18585         if [ $space_check == 1 ]; then
18586                 mdtfree2=$(do_facet $facet \
18587                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18588                 # increase in usage by $size_tmp from previous
18589                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18590                         error "MDT free space is wrong after append: " \
18591                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18592         fi
18593
18594         # delete
18595         rm $dom
18596         if [ $space_check == 1 ]; then
18597                 mdtfree1=$(do_facet $facet \
18598                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18599                 # decrease in usage by $size_dom from previous
18600                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
18601                         error "MDT free space is wrong after removal: " \
18602                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
18603         fi
18604
18605         # combined striping
18606         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
18607                 error "Can't create DoM + OST striping"
18608
18609         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
18610         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18611         # check also direct IO along write
18612         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18613         sync
18614         cmp $tmp $dom || error "file data is different"
18615         [ $(stat -c%s $dom) == $size_tmp ] ||
18616                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18617         rm $dom $tmp
18618
18619         return 0
18620 }
18621 run_test 270a "DoM: basic functionality tests"
18622
18623 test_270b() {
18624         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18625                 skip "Need MDS version at least 2.10.55"
18626
18627         local dom=$DIR/$tdir/dom_file
18628         local max_size=1048576
18629
18630         mkdir -p $DIR/$tdir
18631         $LFS setstripe -E $max_size -L mdt $dom
18632
18633         # truncate over the limit
18634         $TRUNCATE $dom $(($max_size + 1)) &&
18635                 error "successful truncate over the maximum size"
18636         # write over the limit
18637         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
18638                 error "successful write over the maximum size"
18639         # append over the limit
18640         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
18641         echo "12345" >> $dom && error "successful append over the maximum size"
18642         rm $dom
18643
18644         return 0
18645 }
18646 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
18647
18648 test_270c() {
18649         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18650                 skip "Need MDS version at least 2.10.55"
18651
18652         mkdir -p $DIR/$tdir
18653         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18654
18655         # check files inherit DoM EA
18656         touch $DIR/$tdir/first
18657         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
18658                 error "bad pattern"
18659         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
18660                 error "bad stripe count"
18661         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
18662                 error "bad stripe size"
18663
18664         # check directory inherits DoM EA and uses it as default
18665         mkdir $DIR/$tdir/subdir
18666         touch $DIR/$tdir/subdir/second
18667         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
18668                 error "bad pattern in sub-directory"
18669         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
18670                 error "bad stripe count in sub-directory"
18671         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
18672                 error "bad stripe size in sub-directory"
18673         return 0
18674 }
18675 run_test 270c "DoM: DoM EA inheritance tests"
18676
18677 test_270d() {
18678         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18679                 skip "Need MDS version at least 2.10.55"
18680
18681         mkdir -p $DIR/$tdir
18682         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18683
18684         # inherit default DoM striping
18685         mkdir $DIR/$tdir/subdir
18686         touch $DIR/$tdir/subdir/f1
18687
18688         # change default directory striping
18689         $LFS setstripe -c 1 $DIR/$tdir/subdir
18690         touch $DIR/$tdir/subdir/f2
18691         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
18692                 error "wrong default striping in file 2"
18693         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
18694                 error "bad pattern in file 2"
18695         return 0
18696 }
18697 run_test 270d "DoM: change striping from DoM to RAID0"
18698
18699 test_270e() {
18700         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18701                 skip "Need MDS version at least 2.10.55"
18702
18703         mkdir -p $DIR/$tdir/dom
18704         mkdir -p $DIR/$tdir/norm
18705         DOMFILES=20
18706         NORMFILES=10
18707         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
18708         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
18709
18710         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
18711         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
18712
18713         # find DoM files by layout
18714         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
18715         [ $NUM -eq  $DOMFILES ] ||
18716                 error "lfs find -L: found $NUM, expected $DOMFILES"
18717         echo "Test 1: lfs find 20 DOM files by layout: OK"
18718
18719         # there should be 1 dir with default DOM striping
18720         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
18721         [ $NUM -eq  1 ] ||
18722                 error "lfs find -L: found $NUM, expected 1 dir"
18723         echo "Test 2: lfs find 1 DOM dir by layout: OK"
18724
18725         # find DoM files by stripe size
18726         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
18727         [ $NUM -eq  $DOMFILES ] ||
18728                 error "lfs find -S: found $NUM, expected $DOMFILES"
18729         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
18730
18731         # find files by stripe offset except DoM files
18732         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
18733         [ $NUM -eq  $NORMFILES ] ||
18734                 error "lfs find -i: found $NUM, expected $NORMFILES"
18735         echo "Test 5: lfs find no DOM files by stripe index: OK"
18736         return 0
18737 }
18738 run_test 270e "DoM: lfs find with DoM files test"
18739
18740 test_270f() {
18741         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18742                 skip "Need MDS version at least 2.10.55"
18743
18744         local mdtname=${FSNAME}-MDT0000-mdtlov
18745         local dom=$DIR/$tdir/dom_file
18746         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
18747                                                 lod.$mdtname.dom_stripesize)
18748         local dom_limit=131072
18749
18750         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
18751         local dom_current=$(do_facet mds1 $LCTL get_param -n \
18752                                                 lod.$mdtname.dom_stripesize)
18753         [ ${dom_limit} -eq ${dom_current} ] ||
18754                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
18755
18756         $LFS mkdir -i 0 -c 1 $DIR/$tdir
18757         $LFS setstripe -d $DIR/$tdir
18758         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
18759                 error "Can't set directory default striping"
18760
18761         # exceed maximum stripe size
18762         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18763                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
18764         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
18765                 error "Able to create DoM component size more than LOD limit"
18766
18767         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
18768         dom_current=$(do_facet mds1 $LCTL get_param -n \
18769                                                 lod.$mdtname.dom_stripesize)
18770         [ 0 -eq ${dom_current} ] ||
18771                 error "Can't set zero DoM stripe limit"
18772         rm $dom
18773
18774         # attempt to create DoM file on server with disabled DoM should
18775         # remove DoM entry from layout and be succeed
18776         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
18777                 error "Can't create DoM file (DoM is disabled)"
18778         [ $($LFS getstripe -L $dom) == "mdt" ] &&
18779                 error "File has DoM component while DoM is disabled"
18780         rm $dom
18781
18782         # attempt to create DoM file with only DoM stripe should return error
18783         $LFS setstripe -E $dom_limit -L mdt $dom &&
18784                 error "Able to create DoM-only file while DoM is disabled"
18785
18786         # too low values to be aligned with smallest stripe size 64K
18787         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
18788         dom_current=$(do_facet mds1 $LCTL get_param -n \
18789                                                 lod.$mdtname.dom_stripesize)
18790         [ 30000 -eq ${dom_current} ] &&
18791                 error "Can set too small DoM stripe limit"
18792
18793         # 64K is a minimal stripe size in Lustre, expect limit of that size
18794         [ 65536 -eq ${dom_current} ] ||
18795                 error "Limit is not set to 64K but ${dom_current}"
18796
18797         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
18798         dom_current=$(do_facet mds1 $LCTL get_param -n \
18799                                                 lod.$mdtname.dom_stripesize)
18800         echo $dom_current
18801         [ 2147483648 -eq ${dom_current} ] &&
18802                 error "Can set too large DoM stripe limit"
18803
18804         do_facet mds1 $LCTL set_param -n \
18805                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
18806         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18807                 error "Can't create DoM component size after limit change"
18808         do_facet mds1 $LCTL set_param -n \
18809                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
18810         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
18811                 error "Can't create DoM file after limit decrease"
18812         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
18813                 error "Can create big DoM component after limit decrease"
18814         touch ${dom}_def ||
18815                 error "Can't create file with old default layout"
18816
18817         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
18818         return 0
18819 }
18820 run_test 270f "DoM: maximum DoM stripe size checks"
18821
18822 test_271a() {
18823         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18824                 skip "Need MDS version at least 2.10.55"
18825
18826         local dom=$DIR/$tdir/dom
18827
18828         mkdir -p $DIR/$tdir
18829
18830         $LFS setstripe -E 1024K -L mdt $dom
18831
18832         lctl set_param -n mdc.*.stats=clear
18833         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18834         cat $dom > /dev/null
18835         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
18836         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
18837         ls $dom
18838         rm -f $dom
18839 }
18840 run_test 271a "DoM: data is cached for read after write"
18841
18842 test_271b() {
18843         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18844                 skip "Need MDS version at least 2.10.55"
18845
18846         local dom=$DIR/$tdir/dom
18847
18848         mkdir -p $DIR/$tdir
18849
18850         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18851
18852         lctl set_param -n mdc.*.stats=clear
18853         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18854         cancel_lru_locks mdc
18855         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
18856         # second stat to check size is cached on client
18857         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
18858         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18859         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
18860         rm -f $dom
18861 }
18862 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
18863
18864 test_271ba() {
18865         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18866                 skip "Need MDS version at least 2.10.55"
18867
18868         local dom=$DIR/$tdir/dom
18869
18870         mkdir -p $DIR/$tdir
18871
18872         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18873
18874         lctl set_param -n mdc.*.stats=clear
18875         lctl set_param -n osc.*.stats=clear
18876         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
18877         cancel_lru_locks mdc
18878         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18879         # second stat to check size is cached on client
18880         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18881         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18882         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
18883         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
18884         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
18885         rm -f $dom
18886 }
18887 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
18888
18889
18890 get_mdc_stats() {
18891         local mdtidx=$1
18892         local param=$2
18893         local mdt=MDT$(printf %04x $mdtidx)
18894
18895         if [ -z $param ]; then
18896                 lctl get_param -n mdc.*$mdt*.stats
18897         else
18898                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
18899         fi
18900 }
18901
18902 test_271c() {
18903         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18904                 skip "Need MDS version at least 2.10.55"
18905
18906         local dom=$DIR/$tdir/dom
18907
18908         mkdir -p $DIR/$tdir
18909
18910         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18911
18912         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18913         local facet=mds$((mdtidx + 1))
18914
18915         cancel_lru_locks mdc
18916         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
18917         createmany -o $dom 1000
18918         lctl set_param -n mdc.*.stats=clear
18919         smalliomany -w $dom 1000 200
18920         get_mdc_stats $mdtidx
18921         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18922         # Each file has 1 open, 1 IO enqueues, total 2000
18923         # but now we have also +1 getxattr for security.capability, total 3000
18924         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
18925         unlinkmany $dom 1000
18926
18927         cancel_lru_locks mdc
18928         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
18929         createmany -o $dom 1000
18930         lctl set_param -n mdc.*.stats=clear
18931         smalliomany -w $dom 1000 200
18932         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18933         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
18934         # for OPEN and IO lock.
18935         [ $((enq - enq_2)) -ge 1000 ] ||
18936                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
18937         unlinkmany $dom 1000
18938         return 0
18939 }
18940 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
18941
18942 cleanup_271def_tests() {
18943         trap 0
18944         rm -f $1
18945 }
18946
18947 test_271d() {
18948         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
18949                 skip "Need MDS version at least 2.10.57"
18950
18951         local dom=$DIR/$tdir/dom
18952         local tmp=$TMP/$tfile
18953         trap "cleanup_271def_tests $tmp" EXIT
18954
18955         mkdir -p $DIR/$tdir
18956
18957         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18958
18959         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
18960
18961         cancel_lru_locks mdc
18962         dd if=/dev/urandom of=$tmp bs=1000 count=1
18963         dd if=$tmp of=$dom bs=1000 count=1
18964         cancel_lru_locks mdc
18965
18966         cat /etc/hosts >> $tmp
18967         lctl set_param -n mdc.*.stats=clear
18968
18969         # append data to the same file it should update local page
18970         echo "Append to the same page"
18971         cat /etc/hosts >> $dom
18972         local num=$(get_mdc_stats $mdtidx ost_read)
18973         local ra=$(get_mdc_stats $mdtidx req_active)
18974         local rw=$(get_mdc_stats $mdtidx req_waittime)
18975
18976         [ -z $num ] || error "$num READ RPC occured"
18977         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18978         echo "... DONE"
18979
18980         # compare content
18981         cmp $tmp $dom || error "file miscompare"
18982
18983         cancel_lru_locks mdc
18984         lctl set_param -n mdc.*.stats=clear
18985
18986         echo "Open and read file"
18987         cat $dom > /dev/null
18988         local num=$(get_mdc_stats $mdtidx ost_read)
18989         local ra=$(get_mdc_stats $mdtidx req_active)
18990         local rw=$(get_mdc_stats $mdtidx req_waittime)
18991
18992         [ -z $num ] || error "$num READ RPC occured"
18993         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18994         echo "... DONE"
18995
18996         # compare content
18997         cmp $tmp $dom || error "file miscompare"
18998
18999         return 0
19000 }
19001 run_test 271d "DoM: read on open (1K file in reply buffer)"
19002
19003 test_271f() {
19004         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
19005                 skip "Need MDS version at least 2.10.57"
19006
19007         local dom=$DIR/$tdir/dom
19008         local tmp=$TMP/$tfile
19009         trap "cleanup_271def_tests $tmp" EXIT
19010
19011         mkdir -p $DIR/$tdir
19012
19013         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19014
19015         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
19016
19017         cancel_lru_locks mdc
19018         dd if=/dev/urandom of=$tmp bs=265000 count=1
19019         dd if=$tmp of=$dom bs=265000 count=1
19020         cancel_lru_locks mdc
19021         cat /etc/hosts >> $tmp
19022         lctl set_param -n mdc.*.stats=clear
19023
19024         echo "Append to the same page"
19025         cat /etc/hosts >> $dom
19026         local num=$(get_mdc_stats $mdtidx ost_read)
19027         local ra=$(get_mdc_stats $mdtidx req_active)
19028         local rw=$(get_mdc_stats $mdtidx req_waittime)
19029
19030         [ -z $num ] || error "$num READ RPC occured"
19031         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19032         echo "... DONE"
19033
19034         # compare content
19035         cmp $tmp $dom || error "file miscompare"
19036
19037         cancel_lru_locks mdc
19038         lctl set_param -n mdc.*.stats=clear
19039
19040         echo "Open and read file"
19041         cat $dom > /dev/null
19042         local num=$(get_mdc_stats $mdtidx ost_read)
19043         local ra=$(get_mdc_stats $mdtidx req_active)
19044         local rw=$(get_mdc_stats $mdtidx req_waittime)
19045
19046         [ -z $num ] && num=0
19047         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
19048         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19049         echo "... DONE"
19050
19051         # compare content
19052         cmp $tmp $dom || error "file miscompare"
19053
19054         return 0
19055 }
19056 run_test 271f "DoM: read on open (200K file and read tail)"
19057
19058 test_271g() {
19059         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
19060                 skip "Skipping due to old client or server version"
19061
19062         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
19063         # to get layout
19064         $CHECKSTAT -t file $DIR1/$tfile
19065
19066         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
19067         MULTIOP_PID=$!
19068         sleep 1
19069         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
19070         $LCTL set_param fail_loc=0x80000314
19071         rm $DIR1/$tfile || error "Unlink fails"
19072         RC=$?
19073         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
19074         [ $RC -eq 0 ] || error "Failed write to stale object"
19075 }
19076 run_test 271g "Discard DoM data vs client flush race"
19077
19078 test_272a() {
19079         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19080                 skip "Need MDS version at least 2.11.50"
19081
19082         local dom=$DIR/$tdir/dom
19083         mkdir -p $DIR/$tdir
19084
19085         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
19086         dd if=/dev/urandom of=$dom bs=512K count=1 ||
19087                 error "failed to write data into $dom"
19088         local old_md5=$(md5sum $dom)
19089
19090         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
19091                 error "failed to migrate to the same DoM component"
19092
19093         local new_md5=$(md5sum $dom)
19094
19095         [ "$old_md5" == "$new_md5" ] ||
19096                 error "md5sum differ: $old_md5, $new_md5"
19097
19098         [ $($LFS getstripe -c $dom) -eq 2 ] ||
19099                 error "bad final stripe count: $($LFS getstripe -c $dom) != 2"
19100 }
19101 run_test 272a "DoM migration: new layout with the same DOM component"
19102
19103 test_272b() {
19104         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19105                 skip "Need MDS version at least 2.11.50"
19106
19107         local dom=$DIR/$tdir/dom
19108         mkdir -p $DIR/$tdir
19109         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19110
19111         local mdtidx=$($LFS getstripe -m $dom)
19112         local mdtname=MDT$(printf %04x $mdtidx)
19113         local facet=mds$((mdtidx + 1))
19114
19115         local mdtfree1=$(do_facet $facet \
19116                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19117         dd if=/dev/urandom of=$dom bs=2M count=1 ||
19118                 error "failed to write data into $dom"
19119         local old_md5=$(md5sum $dom)
19120         cancel_lru_locks mdc
19121         local mdtfree1=$(do_facet $facet \
19122                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19123
19124         $LFS migrate -c2 $dom ||
19125                 error "failed to migrate to the new composite layout"
19126         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19127                 error "MDT stripe was not removed"
19128
19129         cancel_lru_locks mdc
19130         local new_md5=$(md5sum $dom)
19131         [ "$old_md5" == "$new_md5" ] ||
19132                 error "$old_md5 != $new_md5"
19133
19134         # Skip free space checks with ZFS
19135         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19136                 local mdtfree2=$(do_facet $facet \
19137                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19138                 [ $mdtfree2 -gt $mdtfree1 ] ||
19139                         error "MDT space is not freed after migration"
19140         fi
19141         return 0
19142 }
19143 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
19144
19145 test_272c() {
19146         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19147                 skip "Need MDS version at least 2.11.50"
19148
19149         local dom=$DIR/$tdir/$tfile
19150         mkdir -p $DIR/$tdir
19151         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19152
19153         local mdtidx=$($LFS getstripe -m $dom)
19154         local mdtname=MDT$(printf %04x $mdtidx)
19155         local facet=mds$((mdtidx + 1))
19156
19157         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
19158                 error "failed to write data into $dom"
19159         local old_md5=$(md5sum $dom)
19160         cancel_lru_locks mdc
19161         local mdtfree1=$(do_facet $facet \
19162                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19163
19164         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
19165                 error "failed to migrate to the new composite layout"
19166         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
19167                 error "MDT stripe was not removed"
19168
19169         cancel_lru_locks mdc
19170         local new_md5=$(md5sum $dom)
19171         [ "$old_md5" == "$new_md5" ] ||
19172                 error "$old_md5 != $new_md5"
19173
19174         # Skip free space checks with ZFS
19175         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19176                 local mdtfree2=$(do_facet $facet \
19177                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19178                 [ $mdtfree2 -gt $mdtfree1 ] ||
19179                         error "MDS space is not freed after migration"
19180         fi
19181         return 0
19182 }
19183 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
19184
19185 test_272d() {
19186         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19187                 skip "Need MDS version at least 2.12.55"
19188
19189         local dom=$DIR/$tdir/$tfile
19190         mkdir -p $DIR/$tdir
19191         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19192
19193         local mdtidx=$($LFS getstripe -m $dom)
19194         local mdtname=MDT$(printf %04x $mdtidx)
19195         local facet=mds$((mdtidx + 1))
19196
19197         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
19198                 error "failed to write data into $dom"
19199         local old_md5=$(md5sum $dom)
19200         cancel_lru_locks mdc
19201         local mdtfree1=$(do_facet $facet \
19202                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19203
19204         $LFS mirror extend -N -E 2M -c1 -E -1 -c2 $dom ||
19205                 error "failed mirroring to the new composite layout"
19206         $LFS mirror resync $dom ||
19207                 error "failed mirror resync"
19208         $LFS mirror split --mirror-id 1 -d $dom ||
19209                 error "failed mirror split"
19210
19211         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19212                 error "MDT stripe was not removed"
19213
19214         cancel_lru_locks mdc
19215         local new_md5=$(md5sum $dom)
19216         [ "$old_md5" == "$new_md5" ] ||
19217                 error "$old_md5 != $new_md5"
19218
19219         # Skip free space checks with ZFS
19220         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19221                 local mdtfree2=$(do_facet $facet \
19222                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19223                 [ $mdtfree2 -gt $mdtfree1 ] ||
19224                         error "MDS space is not freed after DOM mirror deletion"
19225         fi
19226         return 0
19227 }
19228 run_test 272d "DoM mirroring: OST-striped mirror to DOM file"
19229
19230 test_272e() {
19231         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19232                 skip "Need MDS version at least 2.12.55"
19233
19234         local dom=$DIR/$tdir/$tfile
19235         mkdir -p $DIR/$tdir
19236         $LFS setstripe -c 2 $dom
19237
19238         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
19239                 error "failed to write data into $dom"
19240         local old_md5=$(md5sum $dom)
19241         cancel_lru_locks mdc
19242
19243         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 $dom ||
19244                 error "failed mirroring to the DOM layout"
19245         $LFS mirror resync $dom ||
19246                 error "failed mirror resync"
19247         $LFS mirror split --mirror-id 1 -d $dom ||
19248                 error "failed mirror split"
19249
19250         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19251                 error "MDT stripe was not removed"
19252
19253         cancel_lru_locks mdc
19254         local new_md5=$(md5sum $dom)
19255         [ "$old_md5" == "$new_md5" ] ||
19256                 error "$old_md5 != $new_md5"
19257
19258         return 0
19259 }
19260 run_test 272e "DoM mirroring: DOM mirror to the OST-striped file"
19261
19262 test_272f() {
19263         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19264                 skip "Need MDS version at least 2.12.55"
19265
19266         local dom=$DIR/$tdir/$tfile
19267         mkdir -p $DIR/$tdir
19268         $LFS setstripe -c 2 $dom
19269
19270         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
19271                 error "failed to write data into $dom"
19272         local old_md5=$(md5sum $dom)
19273         cancel_lru_locks mdc
19274
19275         $LFS migrate -E 1M -L mdt -E eof -c2 -v $dom ||
19276                 error "failed migrating to the DOM file"
19277
19278         cancel_lru_locks mdc
19279         local new_md5=$(md5sum $dom)
19280         [ "$old_md5" != "$new_md5" ] &&
19281                 error "$old_md5 != $new_md5"
19282
19283         return 0
19284 }
19285 run_test 272f "DoM migration: OST-striped file to DOM file"
19286
19287 test_273a() {
19288         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19289                 skip "Need MDS version at least 2.11.50"
19290
19291         # Layout swap cannot be done if either file has DOM component,
19292         # this will never be supported, migration should be used instead
19293
19294         local dom=$DIR/$tdir/$tfile
19295         mkdir -p $DIR/$tdir
19296
19297         $LFS setstripe -c2 ${dom}_plain
19298         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
19299         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
19300                 error "can swap layout with DoM component"
19301         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
19302                 error "can swap layout with DoM component"
19303
19304         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
19305         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
19306                 error "can swap layout with DoM component"
19307         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
19308                 error "can swap layout with DoM component"
19309         return 0
19310 }
19311 run_test 273a "DoM: layout swapping should fail with DOM"
19312
19313 test_275() {
19314         remote_ost_nodsh && skip "remote OST with nodsh"
19315         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
19316                 skip "Need OST version >= 2.10.57"
19317
19318         local file=$DIR/$tfile
19319         local oss
19320
19321         oss=$(comma_list $(osts_nodes))
19322
19323         dd if=/dev/urandom of=$file bs=1M count=2 ||
19324                 error "failed to create a file"
19325         cancel_lru_locks osc
19326
19327         #lock 1
19328         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
19329                 error "failed to read a file"
19330
19331 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
19332         $LCTL set_param fail_loc=0x8000031f
19333
19334         cancel_lru_locks osc &
19335         sleep 1
19336
19337 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
19338         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
19339         #IO takes another lock, but matches the PENDING one
19340         #and places it to the IO RPC
19341         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
19342                 error "failed to read a file with PENDING lock"
19343 }
19344 run_test 275 "Read on a canceled duplicate lock"
19345
19346 test_276() {
19347         remote_ost_nodsh && skip "remote OST with nodsh"
19348         local pid
19349
19350         do_facet ost1 "(while true; do \
19351                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
19352                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
19353         pid=$!
19354
19355         for LOOP in $(seq 20); do
19356                 stop ost1
19357                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
19358         done
19359         kill -9 $pid
19360         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
19361                 rm $TMP/sanity_276_pid"
19362 }
19363 run_test 276 "Race between mount and obd_statfs"
19364
19365 test_277() {
19366         $LCTL set_param ldlm.namespaces.*.lru_size=0
19367         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
19368         local cached_mb=$($LCTL get_param llite.*.max_cached_mb |
19369                         grep ^used_mb | awk '{print $2}')
19370         [ $cached_mb -eq 1 ] || error "expected mb 1 got $cached_mb"
19371         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 \
19372                 oflag=direct conv=notrunc
19373         cached_mb=$($LCTL get_param llite.*.max_cached_mb |
19374                         grep ^used_mb | awk '{print $2}')
19375         [ $cached_mb -eq 0 ] || error "expected mb 0 got $cached_mb"
19376 }
19377 run_test 277 "Direct IO shall drop page cache"
19378
19379 test_278() {
19380         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
19381         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
19382         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] &&
19383                 skip "needs the same host for mdt1 mdt2" && return
19384
19385         local pid1
19386         local pid2
19387
19388 #define OBD_FAIL_OBD_STOP_MDS_RACE     0x60b
19389         do_facet mds2 $LCTL set_param fail_loc=0x8000060c
19390         stop mds2 &
19391         pid2=$!
19392
19393         stop mds1
19394
19395         echo "Starting MDTs"
19396         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
19397         wait $pid2
19398 #For the error assertion will happen. lu_env_get_key(..., &mdt_thread_key)
19399 #will return NULL
19400         do_facet mds2 $LCTL set_param fail_loc=0
19401
19402         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS
19403         wait_recovery_complete mds2
19404 }
19405 run_test 278 "Race starting MDS between MDTs stop/start"
19406
19407 cleanup_test_300() {
19408         trap 0
19409         umask $SAVE_UMASK
19410 }
19411 test_striped_dir() {
19412         local mdt_index=$1
19413         local stripe_count
19414         local stripe_index
19415
19416         mkdir -p $DIR/$tdir
19417
19418         SAVE_UMASK=$(umask)
19419         trap cleanup_test_300 RETURN EXIT
19420
19421         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
19422                                                 $DIR/$tdir/striped_dir ||
19423                 error "set striped dir error"
19424
19425         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
19426         [ "$mode" = "755" ] || error "expect 755 got $mode"
19427
19428         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
19429                 error "getdirstripe failed"
19430         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
19431         if [ "$stripe_count" != "2" ]; then
19432                 error "1:stripe_count is $stripe_count, expect 2"
19433         fi
19434         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
19435         if [ "$stripe_count" != "2" ]; then
19436                 error "2:stripe_count is $stripe_count, expect 2"
19437         fi
19438
19439         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
19440         if [ "$stripe_index" != "$mdt_index" ]; then
19441                 error "stripe_index is $stripe_index, expect $mdt_index"
19442         fi
19443
19444         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19445                 error "nlink error after create striped dir"
19446
19447         mkdir $DIR/$tdir/striped_dir/a
19448         mkdir $DIR/$tdir/striped_dir/b
19449
19450         stat $DIR/$tdir/striped_dir/a ||
19451                 error "create dir under striped dir failed"
19452         stat $DIR/$tdir/striped_dir/b ||
19453                 error "create dir under striped dir failed"
19454
19455         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
19456                 error "nlink error after mkdir"
19457
19458         rmdir $DIR/$tdir/striped_dir/a
19459         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
19460                 error "nlink error after rmdir"
19461
19462         rmdir $DIR/$tdir/striped_dir/b
19463         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19464                 error "nlink error after rmdir"
19465
19466         chattr +i $DIR/$tdir/striped_dir
19467         createmany -o $DIR/$tdir/striped_dir/f 10 &&
19468                 error "immutable flags not working under striped dir!"
19469         chattr -i $DIR/$tdir/striped_dir
19470
19471         rmdir $DIR/$tdir/striped_dir ||
19472                 error "rmdir striped dir error"
19473
19474         cleanup_test_300
19475
19476         true
19477 }
19478
19479 test_300a() {
19480         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19481                 skip "skipped for lustre < 2.7.0"
19482         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19483         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19484
19485         test_striped_dir 0 || error "failed on striped dir on MDT0"
19486         test_striped_dir 1 || error "failed on striped dir on MDT0"
19487 }
19488 run_test 300a "basic striped dir sanity test"
19489
19490 test_300b() {
19491         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19492                 skip "skipped for lustre < 2.7.0"
19493         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19494         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19495
19496         local i
19497         local mtime1
19498         local mtime2
19499         local mtime3
19500
19501         test_mkdir $DIR/$tdir || error "mkdir fail"
19502         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19503                 error "set striped dir error"
19504         for i in {0..9}; do
19505                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
19506                 sleep 1
19507                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
19508                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
19509                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
19510                 sleep 1
19511                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
19512                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
19513                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
19514         done
19515         true
19516 }
19517 run_test 300b "check ctime/mtime for striped dir"
19518
19519 test_300c() {
19520         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19521                 skip "skipped for lustre < 2.7.0"
19522         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19523         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19524
19525         local file_count
19526
19527         mkdir -p $DIR/$tdir
19528         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
19529                 error "set striped dir error"
19530
19531         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
19532                 error "chown striped dir failed"
19533
19534         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
19535                 error "create 5k files failed"
19536
19537         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
19538
19539         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
19540
19541         rm -rf $DIR/$tdir
19542 }
19543 run_test 300c "chown && check ls under striped directory"
19544
19545 test_300d() {
19546         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19547                 skip "skipped for lustre < 2.7.0"
19548         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19549         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19550
19551         local stripe_count
19552         local file
19553
19554         mkdir -p $DIR/$tdir
19555         $LFS setstripe -c 2 $DIR/$tdir
19556
19557         #local striped directory
19558         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19559                 error "set striped dir error"
19560         createmany -o $DIR/$tdir/striped_dir/f 10 ||
19561                 error "create 10 files failed"
19562
19563         #remote striped directory
19564         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
19565                 error "set striped dir error"
19566         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
19567                 error "create 10 files failed"
19568
19569         for file in $(find $DIR/$tdir); do
19570                 stripe_count=$($LFS getstripe -c $file)
19571                 [ $stripe_count -eq 2 ] ||
19572                         error "wrong stripe $stripe_count for $file"
19573         done
19574
19575         rm -rf $DIR/$tdir
19576 }
19577 run_test 300d "check default stripe under striped directory"
19578
19579 test_300e() {
19580         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19581                 skip "Need MDS version at least 2.7.55"
19582         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19583         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19584
19585         local stripe_count
19586         local file
19587
19588         mkdir -p $DIR/$tdir
19589
19590         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19591                 error "set striped dir error"
19592
19593         touch $DIR/$tdir/striped_dir/a
19594         touch $DIR/$tdir/striped_dir/b
19595         touch $DIR/$tdir/striped_dir/c
19596
19597         mkdir $DIR/$tdir/striped_dir/dir_a
19598         mkdir $DIR/$tdir/striped_dir/dir_b
19599         mkdir $DIR/$tdir/striped_dir/dir_c
19600
19601         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
19602                 error "set striped adir under striped dir error"
19603
19604         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
19605                 error "set striped bdir under striped dir error"
19606
19607         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
19608                 error "set striped cdir under striped dir error"
19609
19610         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
19611                 error "rename dir under striped dir fails"
19612
19613         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
19614                 error "rename dir under different stripes fails"
19615
19616         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
19617                 error "rename file under striped dir should succeed"
19618
19619         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
19620                 error "rename dir under striped dir should succeed"
19621
19622         rm -rf $DIR/$tdir
19623 }
19624 run_test 300e "check rename under striped directory"
19625
19626 test_300f() {
19627         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19628         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19629         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19630                 skip "Need MDS version at least 2.7.55"
19631
19632         local stripe_count
19633         local file
19634
19635         rm -rf $DIR/$tdir
19636         mkdir -p $DIR/$tdir
19637
19638         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19639                 error "set striped dir error"
19640
19641         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
19642                 error "set striped dir error"
19643
19644         touch $DIR/$tdir/striped_dir/a
19645         mkdir $DIR/$tdir/striped_dir/dir_a
19646         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
19647                 error "create striped dir under striped dir fails"
19648
19649         touch $DIR/$tdir/striped_dir1/b
19650         mkdir $DIR/$tdir/striped_dir1/dir_b
19651         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
19652                 error "create striped dir under striped dir fails"
19653
19654         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
19655                 error "rename dir under different striped dir should fail"
19656
19657         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
19658                 error "rename striped dir under diff striped dir should fail"
19659
19660         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
19661                 error "rename file under diff striped dirs fails"
19662
19663         rm -rf $DIR/$tdir
19664 }
19665 run_test 300f "check rename cross striped directory"
19666
19667 test_300_check_default_striped_dir()
19668 {
19669         local dirname=$1
19670         local default_count=$2
19671         local default_index=$3
19672         local stripe_count
19673         local stripe_index
19674         local dir_stripe_index
19675         local dir
19676
19677         echo "checking $dirname $default_count $default_index"
19678         $LFS setdirstripe -D -c $default_count -i $default_index \
19679                                 -t all_char $DIR/$tdir/$dirname ||
19680                 error "set default stripe on striped dir error"
19681         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
19682         [ $stripe_count -eq $default_count ] ||
19683                 error "expect $default_count get $stripe_count for $dirname"
19684
19685         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
19686         [ $stripe_index -eq $default_index ] ||
19687                 error "expect $default_index get $stripe_index for $dirname"
19688
19689         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
19690                                                 error "create dirs failed"
19691
19692         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
19693         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
19694         for dir in $(find $DIR/$tdir/$dirname/*); do
19695                 stripe_count=$($LFS getdirstripe -c $dir)
19696                 [ $stripe_count -eq $default_count ] ||
19697                 [ $stripe_count -eq 0 ] || [ $default_count -eq 1 ] ||
19698                 error "stripe count $default_count != $stripe_count for $dir"
19699
19700                 stripe_index=$($LFS getdirstripe -i $dir)
19701                 [ $default_index -eq -1 ] ||
19702                         [ $stripe_index -eq $default_index ] ||
19703                         error "$stripe_index != $default_index for $dir"
19704
19705                 #check default stripe
19706                 stripe_count=$($LFS getdirstripe -D -c $dir)
19707                 [ $stripe_count -eq $default_count ] ||
19708                 error "default count $default_count != $stripe_count for $dir"
19709
19710                 stripe_index=$($LFS getdirstripe -D -i $dir)
19711                 [ $stripe_index -eq $default_index ] ||
19712                 error "default index $default_index != $stripe_index for $dir"
19713         done
19714         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
19715 }
19716
19717 test_300g() {
19718         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19719         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19720                 skip "Need MDS version at least 2.7.55"
19721
19722         local dir
19723         local stripe_count
19724         local stripe_index
19725
19726         mkdir $DIR/$tdir
19727         mkdir $DIR/$tdir/normal_dir
19728
19729         #Checking when client cache stripe index
19730         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
19731         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
19732                 error "create striped_dir failed"
19733
19734         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
19735                 error "create dir0 fails"
19736         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
19737         [ $stripe_index -eq 0 ] ||
19738                 error "dir0 expect index 0 got $stripe_index"
19739
19740         mkdir $DIR/$tdir/striped_dir/dir1 ||
19741                 error "create dir1 fails"
19742         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
19743         [ $stripe_index -eq 1 ] ||
19744                 error "dir1 expect index 1 got $stripe_index"
19745
19746         #check default stripe count/stripe index
19747         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
19748         test_300_check_default_striped_dir normal_dir 1 0
19749         test_300_check_default_striped_dir normal_dir 2 1
19750         test_300_check_default_striped_dir normal_dir 2 -1
19751
19752         #delete default stripe information
19753         echo "delete default stripeEA"
19754         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
19755                 error "set default stripe on striped dir error"
19756
19757         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
19758         for dir in $(find $DIR/$tdir/normal_dir/*); do
19759                 stripe_count=$($LFS getdirstripe -c $dir)
19760                 [ $stripe_count -eq 0 ] ||
19761                         error "expect 1 get $stripe_count for $dir"
19762                 stripe_index=$($LFS getdirstripe -i $dir)
19763                 [ $stripe_index -eq 0 ] ||
19764                         error "expect 0 get $stripe_index for $dir"
19765         done
19766 }
19767 run_test 300g "check default striped directory for normal directory"
19768
19769 test_300h() {
19770         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19771         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19772                 skip "Need MDS version at least 2.7.55"
19773
19774         local dir
19775         local stripe_count
19776
19777         mkdir $DIR/$tdir
19778         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19779                 error "set striped dir error"
19780
19781         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
19782         test_300_check_default_striped_dir striped_dir 1 0
19783         test_300_check_default_striped_dir striped_dir 2 1
19784         test_300_check_default_striped_dir striped_dir 2 -1
19785
19786         #delete default stripe information
19787         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
19788                 error "set default stripe on striped dir error"
19789
19790         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
19791         for dir in $(find $DIR/$tdir/striped_dir/*); do
19792                 stripe_count=$($LFS getdirstripe -c $dir)
19793                 [ $stripe_count -eq 0 ] ||
19794                         error "expect 1 get $stripe_count for $dir"
19795         done
19796 }
19797 run_test 300h "check default striped directory for striped directory"
19798
19799 test_300i() {
19800         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19801         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19802         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19803                 skip "Need MDS version at least 2.7.55"
19804
19805         local stripe_count
19806         local file
19807
19808         mkdir $DIR/$tdir
19809
19810         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19811                 error "set striped dir error"
19812
19813         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
19814                 error "create files under striped dir failed"
19815
19816         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
19817                 error "set striped hashdir error"
19818
19819         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
19820                 error "create dir0 under hash dir failed"
19821         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
19822                 error "create dir1 under hash dir failed"
19823
19824         # unfortunately, we need to umount to clear dir layout cache for now
19825         # once we fully implement dir layout, we can drop this
19826         umount_client $MOUNT || error "umount failed"
19827         mount_client $MOUNT || error "mount failed"
19828
19829         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
19830         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
19831         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
19832
19833         #set the stripe to be unknown hash type
19834         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
19835         $LCTL set_param fail_loc=0x1901
19836         for ((i = 0; i < 10; i++)); do
19837                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
19838                         error "stat f-$i failed"
19839                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
19840         done
19841
19842         touch $DIR/$tdir/striped_dir/f0 &&
19843                 error "create under striped dir with unknown hash should fail"
19844
19845         $LCTL set_param fail_loc=0
19846
19847         umount_client $MOUNT || error "umount failed"
19848         mount_client $MOUNT || error "mount failed"
19849
19850         return 0
19851 }
19852 run_test 300i "client handle unknown hash type striped directory"
19853
19854 test_300j() {
19855         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19856         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19857         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19858                 skip "Need MDS version at least 2.7.55"
19859
19860         local stripe_count
19861         local file
19862
19863         mkdir $DIR/$tdir
19864
19865         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
19866         $LCTL set_param fail_loc=0x1702
19867         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19868                 error "set striped dir error"
19869
19870         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
19871                 error "create files under striped dir failed"
19872
19873         $LCTL set_param fail_loc=0
19874
19875         rm -rf $DIR/$tdir || error "unlink striped dir fails"
19876
19877         return 0
19878 }
19879 run_test 300j "test large update record"
19880
19881 test_300k() {
19882         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19883         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19884         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19885                 skip "Need MDS version at least 2.7.55"
19886
19887         # this test needs a huge transaction
19888         local kb
19889         kb=$(do_facet $SINGLEMDS "$LCTL get_param -n \
19890              osd*.$FSNAME-MDT0000.kbytestotal")
19891         [ $kb -lt $((1024*1024)) ] && skip "MDT0 too small: $kb"
19892
19893         local stripe_count
19894         local file
19895
19896         mkdir $DIR/$tdir
19897
19898         #define OBD_FAIL_LARGE_STRIPE   0x1703
19899         $LCTL set_param fail_loc=0x1703
19900         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
19901                 error "set striped dir error"
19902         $LCTL set_param fail_loc=0
19903
19904         $LFS getdirstripe $DIR/$tdir/striped_dir ||
19905                 error "getstripeddir fails"
19906         rm -rf $DIR/$tdir/striped_dir ||
19907                 error "unlink striped dir fails"
19908
19909         return 0
19910 }
19911 run_test 300k "test large striped directory"
19912
19913 test_300l() {
19914         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19915         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19916         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19917                 skip "Need MDS version at least 2.7.55"
19918
19919         local stripe_index
19920
19921         test_mkdir -p $DIR/$tdir/striped_dir
19922         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
19923                         error "chown $RUNAS_ID failed"
19924         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
19925                 error "set default striped dir failed"
19926
19927         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
19928         $LCTL set_param fail_loc=0x80000158
19929         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
19930
19931         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
19932         [ $stripe_index -eq 1 ] ||
19933                 error "expect 1 get $stripe_index for $dir"
19934 }
19935 run_test 300l "non-root user to create dir under striped dir with stale layout"
19936
19937 test_300m() {
19938         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19939         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
19940         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19941                 skip "Need MDS version at least 2.7.55"
19942
19943         mkdir -p $DIR/$tdir/striped_dir
19944         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
19945                 error "set default stripes dir error"
19946
19947         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
19948
19949         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
19950         [ $stripe_count -eq 0 ] ||
19951                         error "expect 0 get $stripe_count for a"
19952
19953         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
19954                 error "set default stripes dir error"
19955
19956         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
19957
19958         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
19959         [ $stripe_count -eq 0 ] ||
19960                         error "expect 0 get $stripe_count for b"
19961
19962         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
19963                 error "set default stripes dir error"
19964
19965         mkdir $DIR/$tdir/striped_dir/c &&
19966                 error "default stripe_index is invalid, mkdir c should fails"
19967
19968         rm -rf $DIR/$tdir || error "rmdir fails"
19969 }
19970 run_test 300m "setstriped directory on single MDT FS"
19971
19972 cleanup_300n() {
19973         local list=$(comma_list $(mdts_nodes))
19974
19975         trap 0
19976         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19977 }
19978
19979 test_300n() {
19980         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19981         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19982         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19983                 skip "Need MDS version at least 2.7.55"
19984         remote_mds_nodsh && skip "remote MDS with nodsh"
19985
19986         local stripe_index
19987         local list=$(comma_list $(mdts_nodes))
19988
19989         trap cleanup_300n RETURN EXIT
19990         mkdir -p $DIR/$tdir
19991         chmod 777 $DIR/$tdir
19992         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
19993                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
19994                 error "create striped dir succeeds with gid=0"
19995
19996         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
19997         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
19998                 error "create striped dir fails with gid=-1"
19999
20000         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
20001         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
20002                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
20003                 error "set default striped dir succeeds with gid=0"
20004
20005
20006         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
20007         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
20008                 error "set default striped dir fails with gid=-1"
20009
20010
20011         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
20012         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
20013                                         error "create test_dir fails"
20014         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
20015                                         error "create test_dir1 fails"
20016         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
20017                                         error "create test_dir2 fails"
20018         cleanup_300n
20019 }
20020 run_test 300n "non-root user to create dir under striped dir with default EA"
20021
20022 test_300o() {
20023         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20024         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20025         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20026                 skip "Need MDS version at least 2.7.55"
20027
20028         local numfree1
20029         local numfree2
20030
20031         mkdir -p $DIR/$tdir
20032
20033         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
20034         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
20035         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
20036                 skip "not enough free inodes $numfree1 $numfree2"
20037         fi
20038
20039         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
20040         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
20041         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
20042                 skip "not enough free space $numfree1 $numfree2"
20043         fi
20044
20045         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
20046                 error "setdirstripe fails"
20047
20048         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
20049                 error "create dirs fails"
20050
20051         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
20052         ls $DIR/$tdir/striped_dir > /dev/null ||
20053                 error "ls striped dir fails"
20054         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
20055                 error "unlink big striped dir fails"
20056 }
20057 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
20058
20059 test_300p() {
20060         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20061         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20062         remote_mds_nodsh && skip "remote MDS with nodsh"
20063
20064         mkdir -p $DIR/$tdir
20065
20066         #define OBD_FAIL_OUT_ENOSPC     0x1704
20067         do_facet mds2 lctl set_param fail_loc=0x80001704
20068         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
20069                  && error "create striped directory should fail"
20070
20071         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
20072
20073         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
20074         true
20075 }
20076 run_test 300p "create striped directory without space"
20077
20078 test_300q() {
20079         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20080         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20081
20082         local fd=$(free_fd)
20083         local cmd="exec $fd<$tdir"
20084         cd $DIR
20085         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
20086         eval $cmd
20087         cmd="exec $fd<&-"
20088         trap "eval $cmd" EXIT
20089         cd $tdir || error "cd $tdir fails"
20090         rmdir  ../$tdir || error "rmdir $tdir fails"
20091         mkdir local_dir && error "create dir succeeds"
20092         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
20093         eval $cmd
20094         return 0
20095 }
20096 run_test 300q "create remote directory under orphan directory"
20097
20098 test_300r() {
20099         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
20100                 skip "Need MDS version at least 2.7.55" && return
20101         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
20102
20103         mkdir $DIR/$tdir
20104
20105         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
20106                 error "set striped dir error"
20107
20108         $LFS getdirstripe $DIR/$tdir/striped_dir ||
20109                 error "getstripeddir fails"
20110
20111         local stripe_count
20112         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
20113                       awk '/lmv_stripe_count:/ { print $2 }')
20114
20115         [ $MDSCOUNT -ne $stripe_count ] &&
20116                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
20117
20118         rm -rf $DIR/$tdir/striped_dir ||
20119                 error "unlink striped dir fails"
20120 }
20121 run_test 300r "test -1 striped directory"
20122
20123 prepare_remote_file() {
20124         mkdir $DIR/$tdir/src_dir ||
20125                 error "create remote source failed"
20126
20127         cp /etc/hosts $DIR/$tdir/src_dir/a ||
20128                  error "cp to remote source failed"
20129         touch $DIR/$tdir/src_dir/a
20130
20131         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
20132                 error "create remote target dir failed"
20133
20134         touch $DIR/$tdir/tgt_dir/b
20135
20136         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
20137                 error "rename dir cross MDT failed!"
20138
20139         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
20140                 error "src_child still exists after rename"
20141
20142         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
20143                 error "missing file(a) after rename"
20144
20145         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
20146                 error "diff after rename"
20147 }
20148
20149 test_310a() {
20150         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
20151         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20152
20153         local remote_file=$DIR/$tdir/tgt_dir/b
20154
20155         mkdir -p $DIR/$tdir
20156
20157         prepare_remote_file || error "prepare remote file failed"
20158
20159         #open-unlink file
20160         $OPENUNLINK $remote_file $remote_file ||
20161                 error "openunlink $remote_file failed"
20162         $CHECKSTAT -a $remote_file || error "$remote_file exists"
20163 }
20164 run_test 310a "open unlink remote file"
20165
20166 test_310b() {
20167         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
20168         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20169
20170         local remote_file=$DIR/$tdir/tgt_dir/b
20171
20172         mkdir -p $DIR/$tdir
20173
20174         prepare_remote_file || error "prepare remote file failed"
20175
20176         ln $remote_file $DIR/$tfile || error "link failed for remote file"
20177         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
20178         $CHECKSTAT -t file $remote_file || error "check file failed"
20179 }
20180 run_test 310b "unlink remote file with multiple links while open"
20181
20182 test_310c() {
20183         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20184         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
20185
20186         local remote_file=$DIR/$tdir/tgt_dir/b
20187
20188         mkdir -p $DIR/$tdir
20189
20190         prepare_remote_file || error "prepare remote file failed"
20191
20192         ln $remote_file $DIR/$tfile || error "link failed for remote file"
20193         multiop_bg_pause $remote_file O_uc ||
20194                         error "mulitop failed for remote file"
20195         MULTIPID=$!
20196         $MULTIOP $DIR/$tfile Ouc
20197         kill -USR1 $MULTIPID
20198         wait $MULTIPID
20199 }
20200 run_test 310c "open-unlink remote file with multiple links"
20201
20202 #LU-4825
20203 test_311() {
20204         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20205         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
20206         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
20207                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
20208         remote_mds_nodsh && skip "remote MDS with nodsh"
20209
20210         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
20211         local mdts=$(comma_list $(mdts_nodes))
20212
20213         mkdir -p $DIR/$tdir
20214         $LFS setstripe -i 0 -c 1 $DIR/$tdir
20215         createmany -o $DIR/$tdir/$tfile. 1000
20216
20217         # statfs data is not real time, let's just calculate it
20218         old_iused=$((old_iused + 1000))
20219
20220         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
20221                         osp.*OST0000*MDT0000.create_count")
20222         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
20223                                 osp.*OST0000*MDT0000.max_create_count")
20224         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
20225
20226         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
20227         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
20228         [ $index -ne 0 ] || error "$tfile stripe index is 0"
20229
20230         unlinkmany $DIR/$tdir/$tfile. 1000
20231
20232         do_nodes $mdts "$LCTL set_param -n \
20233                         osp.*OST0000*.max_create_count=$max_count"
20234         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
20235                 do_nodes $mdts "$LCTL set_param -n \
20236                                 osp.*OST0000*.create_count=$count"
20237         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
20238                         grep "=0" && error "create_count is zero"
20239
20240         local new_iused
20241         for i in $(seq 120); do
20242                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
20243                 # system may be too busy to destroy all objs in time, use
20244                 # a somewhat small value to not fail autotest
20245                 [ $((old_iused - new_iused)) -gt 400 ] && break
20246                 sleep 1
20247         done
20248
20249         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
20250         [ $((old_iused - new_iused)) -gt 400 ] ||
20251                 error "objs not destroyed after unlink"
20252 }
20253 run_test 311 "disable OSP precreate, and unlink should destroy objs"
20254
20255 zfs_oid_to_objid()
20256 {
20257         local ost=$1
20258         local objid=$2
20259
20260         local vdevdir=$(dirname $(facet_vdevice $ost))
20261         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
20262         local zfs_zapid=$(do_facet $ost $cmd |
20263                           grep -w "/O/0/d$((objid%32))" -C 5 |
20264                           awk '/Object/{getline; print $1}')
20265         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
20266                           awk "/$objid = /"'{printf $3}')
20267
20268         echo $zfs_objid
20269 }
20270
20271 zfs_object_blksz() {
20272         local ost=$1
20273         local objid=$2
20274
20275         local vdevdir=$(dirname $(facet_vdevice $ost))
20276         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
20277         local blksz=$(do_facet $ost $cmd $objid |
20278                       awk '/dblk/{getline; printf $4}')
20279
20280         case "${blksz: -1}" in
20281                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
20282                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
20283                 *) ;;
20284         esac
20285
20286         echo $blksz
20287 }
20288
20289 test_312() { # LU-4856
20290         remote_ost_nodsh && skip "remote OST with nodsh"
20291         [ "$ost1_FSTYPE" = "zfs" ] ||
20292                 skip_env "the test only applies to zfs"
20293
20294         local max_blksz=$(do_facet ost1 \
20295                           $ZFS get -p recordsize $(facet_device ost1) |
20296                           awk '!/VALUE/{print $3}')
20297
20298         # to make life a little bit easier
20299         $LFS mkdir -c 1 -i 0 $DIR/$tdir
20300         $LFS setstripe -c 1 -i 0 $DIR/$tdir
20301
20302         local tf=$DIR/$tdir/$tfile
20303         touch $tf
20304         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20305
20306         # Get ZFS object id
20307         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20308         # block size change by sequential overwrite
20309         local bs
20310
20311         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
20312                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
20313
20314                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
20315                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
20316         done
20317         rm -f $tf
20318
20319         # block size change by sequential append write
20320         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
20321         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20322         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20323         local count
20324
20325         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
20326                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
20327                         oflag=sync conv=notrunc
20328
20329                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
20330                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
20331                         error "blksz error, actual $blksz, " \
20332                                 "expected: 2 * $count * $PAGE_SIZE"
20333         done
20334         rm -f $tf
20335
20336         # random write
20337         touch $tf
20338         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20339         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20340
20341         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
20342         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20343         [ $blksz -eq $PAGE_SIZE ] ||
20344                 error "blksz error: $blksz, expected: $PAGE_SIZE"
20345
20346         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
20347         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20348         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
20349
20350         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
20351         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20352         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
20353 }
20354 run_test 312 "make sure ZFS adjusts its block size by write pattern"
20355
20356 test_313() {
20357         remote_ost_nodsh && skip "remote OST with nodsh"
20358
20359         local file=$DIR/$tfile
20360
20361         rm -f $file
20362         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
20363
20364         # define OBD_FAIL_TGT_RCVD_EIO           0x720
20365         do_facet ost1 "$LCTL set_param fail_loc=0x720"
20366         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
20367                 error "write should failed"
20368         do_facet ost1 "$LCTL set_param fail_loc=0"
20369         rm -f $file
20370 }
20371 run_test 313 "io should fail after last_rcvd update fail"
20372
20373 test_314() {
20374         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
20375
20376         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
20377         do_facet ost1 "$LCTL set_param fail_loc=0x720"
20378         rm -f $DIR/$tfile
20379         wait_delete_completed
20380         do_facet ost1 "$LCTL set_param fail_loc=0"
20381 }
20382 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
20383
20384 test_315() { # LU-618
20385         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
20386
20387         local file=$DIR/$tfile
20388         rm -f $file
20389
20390         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
20391                 error "multiop file write failed"
20392         $MULTIOP $file oO_RDONLY:r4063232_c &
20393         PID=$!
20394
20395         sleep 2
20396
20397         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
20398         kill -USR1 $PID
20399
20400         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
20401         rm -f $file
20402 }
20403 run_test 315 "read should be accounted"
20404
20405 test_316() {
20406         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20407         large_xattr_enabled || skip_env "ea_inode feature disabled"
20408
20409         rm -rf $DIR/$tdir/d
20410         mkdir -p $DIR/$tdir/d
20411         chown nobody $DIR/$tdir/d
20412         touch $DIR/$tdir/d/file
20413
20414         $LFS mv -M1 $DIR/$tdir/d || error "lfs mv failed"
20415 }
20416 run_test 316 "lfs mv"
20417
20418 test_317() {
20419         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
20420                 skip "Need MDS version at least 2.11.53"
20421         if [ "$ost1_FSTYPE" == "zfs" ]; then
20422                 skip "LU-10370: no implementation for ZFS"
20423         fi
20424
20425         local trunc_sz
20426         local grant_blk_size
20427
20428         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
20429                         awk '/grant_block_size:/ { print $2; exit; }')
20430         #
20431         # Create File of size 5M. Truncate it to below size's and verify
20432         # blocks count.
20433         #
20434         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
20435                 error "Create file $DIR/$tfile failed"
20436         stack_trap "rm -f $DIR/$tfile" EXIT
20437
20438         for trunc_sz in 2097152 4097 4000 509 0; do
20439                 $TRUNCATE $DIR/$tfile $trunc_sz ||
20440                         error "truncate $tfile to $trunc_sz failed"
20441                 local sz=$(stat --format=%s $DIR/$tfile)
20442                 local blk=$(stat --format=%b $DIR/$tfile)
20443                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
20444                                      grant_blk_size) * 8))
20445
20446                 if [[ $blk -ne $trunc_blk ]]; then
20447                         $(which stat) $DIR/$tfile
20448                         error "Expected Block $trunc_blk got $blk for $tfile"
20449                 fi
20450
20451                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20452                         error "Expected Size $trunc_sz got $sz for $tfile"
20453         done
20454
20455         #
20456         # sparse file test
20457         # Create file with a hole and write actual two blocks. Block count
20458         # must be 16.
20459         #
20460         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
20461                 conv=fsync || error "Create file : $DIR/$tfile"
20462
20463         # Calculate the final truncate size.
20464         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
20465
20466         #
20467         # truncate to size $trunc_sz bytes. Strip the last block
20468         # The block count must drop to 8
20469         #
20470         $TRUNCATE $DIR/$tfile $trunc_sz ||
20471                 error "truncate $tfile to $trunc_sz failed"
20472
20473         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
20474         sz=$(stat --format=%s $DIR/$tfile)
20475         blk=$(stat --format=%b $DIR/$tfile)
20476
20477         if [[ $blk -ne $trunc_bsz ]]; then
20478                 $(which stat) $DIR/$tfile
20479                 error "Expected Block $trunc_bsz got $blk for $tfile"
20480         fi
20481
20482         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20483                 error "Expected Size $trunc_sz got $sz for $tfile"
20484 }
20485 run_test 317 "Verify blocks get correctly update after truncate"
20486
20487 test_318() {
20488         local old_max_active=$($LCTL get_param -n \
20489                             llite.*.max_read_ahead_async_active 2>/dev/null)
20490
20491         $LCTL set_param llite.*.max_read_ahead_async_active=256
20492         local max_active=$($LCTL get_param -n \
20493                            llite.*.max_read_ahead_async_active 2>/dev/null)
20494         [ $max_active -ne 256 ] && error "expected 256 but got $max_active"
20495
20496         # currently reset to 0 is unsupported, leave it 512 for now.
20497         $LCTL set_param llite.*.max_read_ahead_async_active=0 &&
20498                 error "set max_read_ahead_async_active should fail"
20499
20500         $LCTL set_param llite.*.max_read_ahead_async_active=512
20501         max_active=$($LCTL get_param -n \
20502                      llite.*.max_read_ahead_async_active 2>/dev/null)
20503         [ $max_active -eq 512 ] || error "expected 512 but got $max_active"
20504
20505         # restore @max_active
20506         [ $old_max_active -ne 0 ] && $LCTL set_param \
20507                 llite.*.max_read_ahead_async_active=$old_max_active
20508
20509         local old_threshold=$($LCTL get_param -n \
20510                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
20511         local max_per_file_mb=$($LCTL get_param -n \
20512                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
20513
20514         local invalid=$(($max_per_file_mb + 1))
20515         $LCTL set_param \
20516                 llite.*.read_ahead_async_file_threshold_mb=$invalid\
20517                         && error "set $invalid should fail"
20518
20519         local valid=$(($invalid - 1))
20520         $LCTL set_param \
20521                 llite.*.read_ahead_async_file_threshold_mb=$valid ||
20522                         error "set $valid should succeed"
20523         local threshold=$($LCTL get_param -n \
20524                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
20525         [ $threshold -eq $valid ] || error \
20526                 "expect threshold $valid got $threshold"
20527         $LCTL set_param \
20528                 llite.*.read_ahead_async_file_threshold_mb=$old_threshold
20529 }
20530 run_test 318 "Verify async readahead tunables"
20531
20532 test_319() {
20533         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
20534
20535         local before=$(date +%s)
20536         local evict
20537         local mdir=$DIR/$tdir
20538         local file=$mdir/xxx
20539
20540         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
20541         touch $file
20542
20543 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
20544         $LCTL set_param fail_val=5 fail_loc=0x8000032c
20545         $LFS mv -m1 $file &
20546
20547         sleep 1
20548         dd if=$file of=/dev/null
20549         wait
20550         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
20551           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
20552
20553         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
20554 }
20555 run_test 319 "lost lease lock on migrate error"
20556
20557 test_fake_rw() {
20558         local read_write=$1
20559         if [ "$read_write" = "write" ]; then
20560                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
20561         elif [ "$read_write" = "read" ]; then
20562                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
20563         else
20564                 error "argument error"
20565         fi
20566
20567         # turn off debug for performance testing
20568         local saved_debug=$($LCTL get_param -n debug)
20569         $LCTL set_param debug=0
20570
20571         $LFS setstripe -c 1 -i 0 $DIR/$tfile
20572
20573         # get ost1 size - $FSNAME-OST0000
20574         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
20575         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
20576         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
20577
20578         if [ "$read_write" = "read" ]; then
20579                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
20580         fi
20581
20582         local start_time=$(date +%s.%N)
20583         $dd_cmd bs=1M count=$blocks oflag=sync ||
20584                 error "real dd $read_write error"
20585         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
20586
20587         if [ "$read_write" = "write" ]; then
20588                 rm -f $DIR/$tfile
20589         fi
20590
20591         # define OBD_FAIL_OST_FAKE_RW           0x238
20592         do_facet ost1 $LCTL set_param fail_loc=0x238
20593
20594         local start_time=$(date +%s.%N)
20595         $dd_cmd bs=1M count=$blocks oflag=sync ||
20596                 error "fake dd $read_write error"
20597         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
20598
20599         if [ "$read_write" = "write" ]; then
20600                 # verify file size
20601                 cancel_lru_locks osc
20602                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
20603                         error "$tfile size not $blocks MB"
20604         fi
20605         do_facet ost1 $LCTL set_param fail_loc=0
20606
20607         echo "fake $read_write $duration_fake vs. normal $read_write" \
20608                 "$duration in seconds"
20609         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
20610                 error_not_in_vm "fake write is slower"
20611
20612         $LCTL set_param -n debug="$saved_debug"
20613         rm -f $DIR/$tfile
20614 }
20615 test_399a() { # LU-7655 for OST fake write
20616         remote_ost_nodsh && skip "remote OST with nodsh"
20617
20618         test_fake_rw write
20619 }
20620 run_test 399a "fake write should not be slower than normal write"
20621
20622 test_399b() { # LU-8726 for OST fake read
20623         remote_ost_nodsh && skip "remote OST with nodsh"
20624         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
20625                 skip_env "ldiskfs only test"
20626         fi
20627
20628         test_fake_rw read
20629 }
20630 run_test 399b "fake read should not be slower than normal read"
20631
20632 test_400a() { # LU-1606, was conf-sanity test_74
20633         if ! which $CC > /dev/null 2>&1; then
20634                 skip_env "$CC is not installed"
20635         fi
20636
20637         local extra_flags=''
20638         local out=$TMP/$tfile
20639         local prefix=/usr/include/lustre
20640         local prog
20641
20642         if ! [[ -d $prefix ]]; then
20643                 # Assume we're running in tree and fixup the include path.
20644                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
20645                 extra_flags+=" -L$LUSTRE/utils/.lib"
20646         fi
20647
20648         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
20649                 $CC -Wall -Werror $extra_flags -o $out $prog -llustreapi ||
20650                         error "client api broken"
20651         done
20652         rm -f $out
20653 }
20654 run_test 400a "Lustre client api program can compile and link"
20655
20656 test_400b() { # LU-1606, LU-5011
20657         local header
20658         local out=$TMP/$tfile
20659         local prefix=/usr/include/linux/lustre
20660
20661         # We use a hard coded prefix so that this test will not fail
20662         # when run in tree. There are headers in lustre/include/lustre/
20663         # that are not packaged (like lustre_idl.h) and have more
20664         # complicated include dependencies (like config.h and lnet/types.h).
20665         # Since this test about correct packaging we just skip them when
20666         # they don't exist (see below) rather than try to fixup cppflags.
20667
20668         if ! which $CC > /dev/null 2>&1; then
20669                 skip_env "$CC is not installed"
20670         fi
20671
20672         for header in $prefix/*.h; do
20673                 if ! [[ -f "$header" ]]; then
20674                         continue
20675                 fi
20676
20677                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
20678                         continue # lustre_ioctl.h is internal header
20679                 fi
20680
20681                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
20682                         error "cannot compile '$header'"
20683         done
20684         rm -f $out
20685 }
20686 run_test 400b "packaged headers can be compiled"
20687
20688 test_401a() { #LU-7437
20689         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
20690         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
20691
20692         #count the number of parameters by "list_param -R"
20693         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
20694         #count the number of parameters by listing proc files
20695         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
20696         echo "proc_dirs='$proc_dirs'"
20697         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
20698         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
20699                       sort -u | wc -l)
20700
20701         [ $params -eq $procs ] ||
20702                 error "found $params parameters vs. $procs proc files"
20703
20704         # test the list_param -D option only returns directories
20705         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
20706         #count the number of parameters by listing proc directories
20707         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
20708                 sort -u | wc -l)
20709
20710         [ $params -eq $procs ] ||
20711                 error "found $params parameters vs. $procs proc files"
20712 }
20713 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
20714
20715 test_401b() {
20716         local save=$($LCTL get_param -n jobid_var)
20717         local tmp=testing
20718
20719         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
20720                 error "no error returned when setting bad parameters"
20721
20722         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
20723         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
20724
20725         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
20726         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
20727         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
20728 }
20729 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
20730
20731 test_401c() {
20732         local jobid_var_old=$($LCTL get_param -n jobid_var)
20733         local jobid_var_new
20734
20735         $LCTL set_param jobid_var= &&
20736                 error "no error returned for 'set_param a='"
20737
20738         jobid_var_new=$($LCTL get_param -n jobid_var)
20739         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
20740                 error "jobid_var was changed by setting without value"
20741
20742         $LCTL set_param jobid_var &&
20743                 error "no error returned for 'set_param a'"
20744
20745         jobid_var_new=$($LCTL get_param -n jobid_var)
20746         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
20747                 error "jobid_var was changed by setting without value"
20748 }
20749 run_test 401c "Verify 'lctl set_param' without value fails in either format."
20750
20751 test_401d() {
20752         local jobid_var_old=$($LCTL get_param -n jobid_var)
20753         local jobid_var_new
20754         local new_value="foo=bar"
20755
20756         $LCTL set_param jobid_var=$new_value ||
20757                 error "'set_param a=b' did not accept a value containing '='"
20758
20759         jobid_var_new=$($LCTL get_param -n jobid_var)
20760         [[ "$jobid_var_new" == "$new_value" ]] ||
20761                 error "'set_param a=b' failed on a value containing '='"
20762
20763         # Reset the jobid_var to test the other format
20764         $LCTL set_param jobid_var=$jobid_var_old
20765         jobid_var_new=$($LCTL get_param -n jobid_var)
20766         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
20767                 error "failed to reset jobid_var"
20768
20769         $LCTL set_param jobid_var $new_value ||
20770                 error "'set_param a b' did not accept a value containing '='"
20771
20772         jobid_var_new=$($LCTL get_param -n jobid_var)
20773         [[ "$jobid_var_new" == "$new_value" ]] ||
20774                 error "'set_param a b' failed on a value containing '='"
20775
20776         $LCTL set_param jobid_var $jobid_var_old
20777         jobid_var_new=$($LCTL get_param -n jobid_var)
20778         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
20779                 error "failed to reset jobid_var"
20780 }
20781 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
20782
20783 test_402() {
20784         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
20785         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
20786                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
20787         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
20788                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
20789                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
20790         remote_mds_nodsh && skip "remote MDS with nodsh"
20791
20792         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
20793 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
20794         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
20795         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
20796                 echo "Touch failed - OK"
20797 }
20798 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
20799
20800 test_403() {
20801         local file1=$DIR/$tfile.1
20802         local file2=$DIR/$tfile.2
20803         local tfile=$TMP/$tfile
20804
20805         rm -f $file1 $file2 $tfile
20806
20807         touch $file1
20808         ln $file1 $file2
20809
20810         # 30 sec OBD_TIMEOUT in ll_getattr()
20811         # right before populating st_nlink
20812         $LCTL set_param fail_loc=0x80001409
20813         stat -c %h $file1 > $tfile &
20814
20815         # create an alias, drop all locks and reclaim the dentry
20816         < $file2
20817         cancel_lru_locks mdc
20818         cancel_lru_locks osc
20819         sysctl -w vm.drop_caches=2
20820
20821         wait
20822
20823         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
20824
20825         rm -f $tfile $file1 $file2
20826 }
20827 run_test 403 "i_nlink should not drop to zero due to aliasing"
20828
20829 test_404() { # LU-6601
20830         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
20831                 skip "Need server version newer than 2.8.52"
20832         remote_mds_nodsh && skip "remote MDS with nodsh"
20833
20834         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
20835                 awk '/osp .*-osc-MDT/ { print $4}')
20836
20837         local osp
20838         for osp in $mosps; do
20839                 echo "Deactivate: " $osp
20840                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
20841                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
20842                         awk -vp=$osp '$4 == p { print $2 }')
20843                 [ $stat = IN ] || {
20844                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
20845                         error "deactivate error"
20846                 }
20847                 echo "Activate: " $osp
20848                 do_facet $SINGLEMDS $LCTL --device %$osp activate
20849                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
20850                         awk -vp=$osp '$4 == p { print $2 }')
20851                 [ $stat = UP ] || {
20852                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
20853                         error "activate error"
20854                 }
20855         done
20856 }
20857 run_test 404 "validate manual {de}activated works properly for OSPs"
20858
20859 test_405() {
20860         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
20861         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
20862                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
20863                         skip "Layout swap lock is not supported"
20864
20865         check_swap_layouts_support
20866
20867         test_mkdir $DIR/$tdir
20868         swap_lock_test -d $DIR/$tdir ||
20869                 error "One layout swap locked test failed"
20870 }
20871 run_test 405 "Various layout swap lock tests"
20872
20873 test_406() {
20874         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20875         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
20876         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
20877         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20878         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
20879                 skip "Need MDS version at least 2.8.50"
20880
20881         local def_stripe_size=$($LFS getstripe -S $MOUNT)
20882         local test_pool=$TESTNAME
20883
20884         pool_add $test_pool || error "pool_add failed"
20885         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
20886                 error "pool_add_targets failed"
20887
20888         save_layout_restore_at_exit $MOUNT
20889
20890         # parent set default stripe count only, child will stripe from both
20891         # parent and fs default
20892         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
20893                 error "setstripe $MOUNT failed"
20894         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
20895         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
20896         for i in $(seq 10); do
20897                 local f=$DIR/$tdir/$tfile.$i
20898                 touch $f || error "touch failed"
20899                 local count=$($LFS getstripe -c $f)
20900                 [ $count -eq $OSTCOUNT ] ||
20901                         error "$f stripe count $count != $OSTCOUNT"
20902                 local offset=$($LFS getstripe -i $f)
20903                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
20904                 local size=$($LFS getstripe -S $f)
20905                 [ $size -eq $((def_stripe_size * 2)) ] ||
20906                         error "$f stripe size $size != $((def_stripe_size * 2))"
20907                 local pool=$($LFS getstripe -p $f)
20908                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
20909         done
20910
20911         # change fs default striping, delete parent default striping, now child
20912         # will stripe from new fs default striping only
20913         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
20914                 error "change $MOUNT default stripe failed"
20915         $LFS setstripe -c 0 $DIR/$tdir ||
20916                 error "delete $tdir default stripe failed"
20917         for i in $(seq 11 20); do
20918                 local f=$DIR/$tdir/$tfile.$i
20919                 touch $f || error "touch $f failed"
20920                 local count=$($LFS getstripe -c $f)
20921                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
20922                 local offset=$($LFS getstripe -i $f)
20923                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
20924                 local size=$($LFS getstripe -S $f)
20925                 [ $size -eq $def_stripe_size ] ||
20926                         error "$f stripe size $size != $def_stripe_size"
20927                 local pool=$($LFS getstripe -p $f)
20928                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
20929         done
20930
20931         unlinkmany $DIR/$tdir/$tfile. 1 20
20932
20933         local f=$DIR/$tdir/$tfile
20934         pool_remove_all_targets $test_pool $f
20935         pool_remove $test_pool $f
20936 }
20937 run_test 406 "DNE support fs default striping"
20938
20939 test_407() {
20940         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20941         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
20942                 skip "Need MDS version at least 2.8.55"
20943         remote_mds_nodsh && skip "remote MDS with nodsh"
20944
20945         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
20946                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
20947         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
20948                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
20949         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
20950
20951         #define OBD_FAIL_DT_TXN_STOP    0x2019
20952         for idx in $(seq $MDSCOUNT); do
20953                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
20954         done
20955         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
20956         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
20957                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
20958         true
20959 }
20960 run_test 407 "transaction fail should cause operation fail"
20961
20962 test_408() {
20963         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
20964
20965         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
20966         lctl set_param fail_loc=0x8000040a
20967         # let ll_prepare_partial_page() fail
20968         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
20969
20970         rm -f $DIR/$tfile
20971
20972         # create at least 100 unused inodes so that
20973         # shrink_icache_memory(0) should not return 0
20974         touch $DIR/$tfile-{0..100}
20975         rm -f $DIR/$tfile-{0..100}
20976         sync
20977
20978         echo 2 > /proc/sys/vm/drop_caches
20979 }
20980 run_test 408 "drop_caches should not hang due to page leaks"
20981
20982 test_409()
20983 {
20984         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20985
20986         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
20987         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
20988         touch $DIR/$tdir/guard || error "(2) Fail to create"
20989
20990         local PREFIX=$(str_repeat 'A' 128)
20991         echo "Create 1K hard links start at $(date)"
20992         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
20993                 error "(3) Fail to hard link"
20994
20995         echo "Links count should be right although linkEA overflow"
20996         stat $DIR/$tdir/guard || error "(4) Fail to stat"
20997         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
20998         [ $linkcount -eq 1001 ] ||
20999                 error "(5) Unexpected hard links count: $linkcount"
21000
21001         echo "List all links start at $(date)"
21002         ls -l $DIR/$tdir/foo > /dev/null ||
21003                 error "(6) Fail to list $DIR/$tdir/foo"
21004
21005         echo "Unlink hard links start at $(date)"
21006         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
21007                 error "(7) Fail to unlink"
21008         echo "Unlink hard links finished at $(date)"
21009 }
21010 run_test 409 "Large amount of cross-MDTs hard links on the same file"
21011
21012 test_410()
21013 {
21014         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
21015                 skip "Need client version at least 2.9.59"
21016
21017         # Create a file, and stat it from the kernel
21018         local testfile=$DIR/$tfile
21019         touch $testfile
21020
21021         local run_id=$RANDOM
21022         local my_ino=$(stat --format "%i" $testfile)
21023
21024         # Try to insert the module. This will always fail as the
21025         # module is designed to not be inserted.
21026         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
21027             &> /dev/null
21028
21029         # Anything but success is a test failure
21030         dmesg | grep -q \
21031             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
21032             error "no inode match"
21033 }
21034 run_test 410 "Test inode number returned from kernel thread"
21035
21036 cleanup_test411_cgroup() {
21037         trap 0
21038         rmdir "$1"
21039 }
21040
21041 test_411() {
21042         local cg_basedir=/sys/fs/cgroup/memory
21043         # LU-9966
21044         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
21045                 skip "no setup for cgroup"
21046
21047         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
21048                 error "test file creation failed"
21049         cancel_lru_locks osc
21050
21051         # Create a very small memory cgroup to force a slab allocation error
21052         local cgdir=$cg_basedir/osc_slab_alloc
21053         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
21054         trap "cleanup_test411_cgroup $cgdir" EXIT
21055         echo 2M > $cgdir/memory.kmem.limit_in_bytes
21056         echo 1M > $cgdir/memory.limit_in_bytes
21057
21058         # Should not LBUG, just be killed by oom-killer
21059         # dd will return 0 even allocation failure in some environment.
21060         # So don't check return value
21061         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
21062         cleanup_test411_cgroup $cgdir
21063
21064         return 0
21065 }
21066 run_test 411 "Slab allocation error with cgroup does not LBUG"
21067
21068 test_412() {
21069         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21070         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
21071                 skip "Need server version at least 2.10.55"
21072         fi
21073
21074         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
21075                 error "mkdir failed"
21076         $LFS getdirstripe $DIR/$tdir
21077         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
21078         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
21079                 error "expect $((MDSCOUT - 1)) get $stripe_index"
21080         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
21081         [ $stripe_count -eq 2 ] ||
21082                 error "expect 2 get $stripe_count"
21083 }
21084 run_test 412 "mkdir on specific MDTs"
21085
21086 test_qos_mkdir() {
21087         local mkdir_cmd=$1
21088         local stripe_count=$2
21089         local mdts=$(comma_list $(mdts_nodes))
21090
21091         local testdir
21092         local lmv_qos_prio_free
21093         local lmv_qos_threshold_rr
21094         local lmv_qos_maxage
21095         local lod_qos_prio_free
21096         local lod_qos_threshold_rr
21097         local lod_qos_maxage
21098         local count
21099         local i
21100
21101         lmv_qos_prio_free=$($LCTL get_param -n lmv.*.qos_prio_free | head -n1)
21102         lmv_qos_prio_free=${lmv_qos_prio_free%%%}
21103         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
21104                 head -n1)
21105         lmv_qos_threshold_rr=${lmv_qos_threshold_rr%%%}
21106         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
21107         stack_trap "$LCTL set_param \
21108                 lmv.*.qos_prio_free=$lmv_qos_prio_free > /dev/null" EXIT
21109         stack_trap "$LCTL set_param \
21110                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null" EXIT
21111         stack_trap "$LCTL set_param \
21112                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null" EXIT
21113
21114         lod_qos_prio_free=$(do_facet mds1 $LCTL get_param -n \
21115                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_prio_free | head -n1)
21116         lod_qos_prio_free=${lod_qos_prio_free%%%}
21117         lod_qos_threshold_rr=$(do_facet mds1 $LCTL get_param -n \
21118                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_threshold_rr | head -n1)
21119         lod_qos_threshold_rr=${lod_qos_threshold_rr%%%}
21120         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
21121                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
21122         stack_trap "do_nodes $mdts $LCTL set_param \
21123                 lod.*.mdt_qos_prio_free=$lod_qos_prio_free > /dev/null" EXIT
21124         stack_trap "do_nodes $mdts $LCTL set_param \
21125                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null" \
21126                 EXIT
21127         stack_trap "do_nodes $mdts $LCTL set_param \
21128                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null" EXIT
21129
21130         echo
21131         echo "Mkdir (stripe_count $stripe_count) roundrobin:"
21132
21133         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
21134         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=100 > /dev/null
21135
21136         testdir=$DIR/$tdir-s$stripe_count/rr
21137
21138         for i in $(seq $((100 * MDSCOUNT))); do
21139                 eval $mkdir_cmd $testdir/subdir$i ||
21140                         error "$mkdir_cmd subdir$i failed"
21141         done
21142
21143         for i in $(seq $MDSCOUNT); do
21144                 count=$($LFS getdirstripe -i $testdir/* |
21145                                 grep ^$((i - 1))$ | wc -l)
21146                 echo "$count directories created on MDT$((i - 1))"
21147                 [ $count -eq 100 ] || error "subdirs are not evenly distributed"
21148
21149                 if [ $stripe_count -gt 1 ]; then
21150                         count=$($LFS getdirstripe $testdir/* |
21151                                 grep -P "^\s+$((i - 1))\t" | wc -l)
21152                         echo "$count stripes created on MDT$((i - 1))"
21153                         # deviation should < 5% of average
21154                         [ $count -lt $((95 * stripe_count)) ] ||
21155                         [ $count -gt $((105 * stripe_count)) ] &&
21156                                 error "stripes are not evenly distributed"
21157                 fi
21158         done
21159
21160         $LCTL set_param lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null
21161         do_nodes $mdts $LCTL set_param \
21162                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null
21163
21164         echo
21165         echo "Check for uneven MDTs: "
21166
21167         local ffree
21168         local bavail
21169         local max
21170         local min
21171         local max_index
21172         local min_index
21173         local tmp
21174
21175         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
21176         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
21177         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
21178
21179         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
21180         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
21181         max_index=0
21182         min_index=0
21183         for ((i = 1; i < ${#ffree[@]}; i++)); do
21184                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
21185                 if [ $tmp -gt $max ]; then
21186                         max=$tmp
21187                         max_index=$i
21188                 fi
21189                 if [ $tmp -lt $min ]; then
21190                         min=$tmp
21191                         min_index=$i
21192                 fi
21193         done
21194
21195         [ ${ffree[min_index]} -eq 0 ] &&
21196                 skip "no free files in MDT$min_index"
21197         [ ${ffree[min_index]} -gt 100000000 ] &&
21198                 skip "too much free files in MDT$min_index"
21199
21200         # Check if we need to generate uneven MDTs
21201         local threshold=50
21202         local diff=$(((max - min) * 100 / min))
21203         local value="$(generate_string 1024)"
21204
21205         while [ $diff -lt $threshold ]; do
21206                 # generate uneven MDTs, create till $threshold% diff
21207                 echo -n "weight diff=$diff% must be > $threshold% ..."
21208                 count=$((${ffree[min_index]} / 10))
21209                 # 50 sec per 10000 files in vm
21210                 [ $count -gt 40000 ] && [ "$SLOW" = "no" ] &&
21211                         skip "$count files to create"
21212                 echo "Fill MDT$min_index with $count files"
21213                 [ -d $DIR/$tdir-MDT$min_index ] ||
21214                         $LFS mkdir -i $min_index $DIR/$tdir-MDT$min_index ||
21215                         error "mkdir $tdir-MDT$min_index failed"
21216                 for i in $(seq $count); do
21217                         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE \
21218                                 $DIR/$tdir-MDT$min_index/f$j_$i > /dev/null ||
21219                                 error "create f$j_$i failed"
21220                         setfattr -n user.413b -v $value \
21221                                 $DIR/$tdir-MDT$min_index/f$j_$i ||
21222                                 error "setfattr f$j_$i failed"
21223                 done
21224
21225                 ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-*.filesfree))
21226                 bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-*.kbytesavail))
21227                 max=$(((${ffree[max_index]} >> 8) * \
21228                         (${bavail[max_index]} * bsize >> 16)))
21229                 min=$(((${ffree[min_index]} >> 8) * \
21230                         (${bavail[min_index]} * bsize >> 16)))
21231                 diff=$(((max - min) * 100 / min))
21232         done
21233
21234         echo "MDT filesfree available: ${ffree[@]}"
21235         echo "MDT blocks available: ${bavail[@]}"
21236         echo "weight diff=$diff%"
21237
21238         echo
21239         echo "Mkdir (stripe_count $stripe_count) with balanced space usage:"
21240
21241         $LCTL set_param lmv.*.qos_prio_free=100 > /dev/null
21242         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_prio_free=100 > /dev/null
21243         # decrease statfs age, so that it can be updated in time
21244         $LCTL set_param lmv.*.qos_maxage=1 > /dev/null
21245         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_maxage=1 > /dev/null
21246
21247         sleep 1
21248
21249         testdir=$DIR/$tdir-s$stripe_count/qos
21250
21251         for i in $(seq $((100 * MDSCOUNT))); do
21252                 eval $mkdir_cmd $testdir/subdir$i ||
21253                         error "$mkdir_cmd subdir$i failed"
21254         done
21255
21256         for i in $(seq $MDSCOUNT); do
21257                 count=$($LFS getdirstripe -i $testdir/* | grep ^$((i - 1))$ |
21258                         wc -l)
21259                 echo "$count directories created on MDT$((i - 1))"
21260
21261                 if [ $stripe_count -gt 1 ]; then
21262                         count=$($LFS getdirstripe $testdir/* |
21263                                 grep -P "^\s+$((i - 1))\t" | wc -l)
21264                         echo "$count stripes created on MDT$((i - 1))"
21265                 fi
21266         done
21267
21268         max=$($LFS getdirstripe -i $testdir/* | grep ^$max_index$ | wc -l)
21269         min=$($LFS getdirstripe -i $testdir/* | grep ^$min_index$ | wc -l)
21270
21271         # D-value should > 10% of averge
21272         [ $((max - min)) -lt 10 ] &&
21273                 error "subdirs shouldn't be evenly distributed"
21274
21275         # ditto
21276         if [ $stripe_count -gt 1 ]; then
21277                 max=$($LFS getdirstripe $testdir/* |
21278                         grep -P "^\s+$max_index\t" | wc -l)
21279                 min=$($LFS getdirstripe $testdir/* |
21280                         grep -P "^\s+$min_index\t" | wc -l)
21281                 [ $((max - min)) -le $((10 * stripe_count)) ] &&
21282                         error "stripes shouldn't be evenly distributed"|| true
21283         fi
21284 }
21285
21286 test_413a() {
21287         [ $MDSCOUNT -lt 2 ] &&
21288                 skip "We need at least 2 MDTs for this test"
21289
21290         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
21291                 skip "Need server version at least 2.12.52"
21292
21293         local stripe_count
21294
21295         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
21296                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
21297                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
21298                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
21299                 test_qos_mkdir "$LFS mkdir -c $stripe_count" $stripe_count
21300         done
21301 }
21302 run_test 413a "QoS mkdir with 'lfs mkdir -i -1'"
21303
21304 test_413b() {
21305         [ $MDSCOUNT -lt 2 ] &&
21306                 skip "We need at least 2 MDTs for this test"
21307
21308         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
21309                 skip "Need server version at least 2.12.52"
21310
21311         local stripe_count
21312
21313         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
21314                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
21315                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
21316                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
21317                 $LFS setdirstripe -D -c $stripe_count \
21318                         $DIR/$tdir-s$stripe_count/rr ||
21319                         error "setdirstripe failed"
21320                 $LFS setdirstripe -D -c $stripe_count \
21321                         $DIR/$tdir-s$stripe_count/qos ||
21322                         error "setdirstripe failed"
21323                 test_qos_mkdir "mkdir" $stripe_count
21324         done
21325 }
21326 run_test 413b "QoS mkdir under dir whose default LMV starting MDT offset is -1"
21327
21328 test_414() {
21329 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
21330         $LCTL set_param fail_loc=0x80000521
21331         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
21332         rm -f $DIR/$tfile
21333 }
21334 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
21335
21336 test_415() {
21337         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21338         [ $(lustre_version_code mds1) -lt $(version_code 2.11.52) ] &&
21339                 skip "Need server version at least 2.11.52"
21340
21341         # LU-11102
21342         local total
21343         local setattr_pid
21344         local start_time
21345         local end_time
21346         local duration
21347
21348         total=500
21349         # this test may be slow on ZFS
21350         [ "$mds1_FSTYPE" == "zfs" ] && total=100
21351
21352         # though this test is designed for striped directory, let's test normal
21353         # directory too since lock is always saved as CoS lock.
21354         test_mkdir $DIR/$tdir || error "mkdir $tdir"
21355         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
21356
21357         (
21358                 while true; do
21359                         touch $DIR/$tdir
21360                 done
21361         ) &
21362         setattr_pid=$!
21363
21364         start_time=$(date +%s)
21365         for i in $(seq $total); do
21366                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
21367                         > /dev/null
21368         done
21369         end_time=$(date +%s)
21370         duration=$((end_time - start_time))
21371
21372         kill -9 $setattr_pid
21373
21374         echo "rename $total files took $duration sec"
21375         [ $duration -lt 100 ] || error "rename took $duration sec"
21376 }
21377 run_test 415 "lock revoke is not missing"
21378
21379 test_416() {
21380         [ $(lustre_version_code mds1) -lt $(version_code 2.11.55) ] &&
21381                 skip "Need server version at least 2.11.55"
21382
21383         # define OBD_FAIL_OSD_TXN_START    0x19a
21384         do_facet mds1 lctl set_param fail_loc=0x19a
21385
21386         lfs mkdir -c $MDSCOUNT $DIR/$tdir
21387
21388         true
21389 }
21390 run_test 416 "transaction start failure won't cause system hung"
21391
21392 cleanup_417() {
21393         trap 0
21394         do_nodes $(comma_list $(mdts_nodes)) \
21395                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
21396         do_nodes $(comma_list $(mdts_nodes)) \
21397                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
21398         do_nodes $(comma_list $(mdts_nodes)) \
21399                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
21400 }
21401
21402 test_417() {
21403         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21404         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
21405                 skip "Need MDS version at least 2.11.56"
21406
21407         trap cleanup_417 RETURN EXIT
21408
21409         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
21410         do_nodes $(comma_list $(mdts_nodes)) \
21411                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
21412         $LFS migrate -m 0 $DIR/$tdir.1 &&
21413                 error "migrate dir $tdir.1 should fail"
21414
21415         do_nodes $(comma_list $(mdts_nodes)) \
21416                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
21417         $LFS mkdir -i 1 $DIR/$tdir.2 &&
21418                 error "create remote dir $tdir.2 should fail"
21419
21420         do_nodes $(comma_list $(mdts_nodes)) \
21421                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
21422         $LFS mkdir -c 2 $DIR/$tdir.3 &&
21423                 error "create striped dir $tdir.3 should fail"
21424         true
21425 }
21426 run_test 417 "disable remote dir, striped dir and dir migration"
21427
21428 # Checks that the outputs of df [-i] and lfs df [-i] match
21429 #
21430 # usage: check_lfs_df <blocks | inodes> <mountpoint>
21431 check_lfs_df() {
21432         local dir=$2
21433         local inodes
21434         local df_out
21435         local lfs_df_out
21436         local count
21437         local passed=false
21438
21439         # blocks or inodes
21440         [ "$1" == "blocks" ] && inodes= || inodes="-i"
21441
21442         for count in {1..100}; do
21443                 cancel_lru_locks
21444                 sync; sleep 0.2
21445
21446                 # read the lines of interest
21447                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
21448                         error "df $inodes $dir | tail -n +2 failed"
21449                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
21450                         error "lfs df $inodes $dir | grep summary: failed"
21451
21452                 # skip first substrings of each output as they are different
21453                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
21454                 # compare the two outputs
21455                 passed=true
21456                 for i in {1..5}; do
21457                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
21458                 done
21459                 $passed && break
21460         done
21461
21462         if ! $passed; then
21463                 df -P $inodes $dir
21464                 echo
21465                 lfs df $inodes $dir
21466                 error "df and lfs df $1 output mismatch: "      \
21467                       "df ${inodes}: ${df_out[*]}, "            \
21468                       "lfs df ${inodes}: ${lfs_df_out[*]}"
21469         fi
21470 }
21471
21472 test_418() {
21473         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21474
21475         local dir=$DIR/$tdir
21476         local numfiles=$((RANDOM % 4096 + 2))
21477         local numblocks=$((RANDOM % 256 + 1))
21478
21479         wait_delete_completed
21480         test_mkdir $dir
21481
21482         # check block output
21483         check_lfs_df blocks $dir
21484         # check inode output
21485         check_lfs_df inodes $dir
21486
21487         # create a single file and retest
21488         echo "Creating a single file and testing"
21489         createmany -o $dir/$tfile- 1 &>/dev/null ||
21490                 error "creating 1 file in $dir failed"
21491         check_lfs_df blocks $dir
21492         check_lfs_df inodes $dir
21493
21494         # create a random number of files
21495         echo "Creating $((numfiles - 1)) files and testing"
21496         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
21497                 error "creating $((numfiles - 1)) files in $dir failed"
21498
21499         # write a random number of blocks to the first test file
21500         echo "Writing $numblocks 4K blocks and testing"
21501         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
21502                 count=$numblocks &>/dev/null ||
21503                 error "dd to $dir/${tfile}-0 failed"
21504
21505         # retest
21506         check_lfs_df blocks $dir
21507         check_lfs_df inodes $dir
21508
21509         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
21510                 error "unlinking $numfiles files in $dir failed"
21511 }
21512 run_test 418 "df and lfs df outputs match"
21513
21514 test_419()
21515 {
21516         local dir=$DIR/$tdir
21517
21518         mkdir -p $dir
21519         touch $dir/file
21520
21521         cancel_lru_locks mdc
21522
21523         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
21524         $LCTL set_param fail_loc=0x1410
21525         cat $dir/file
21526         $LCTL set_param fail_loc=0
21527         rm -rf $dir
21528 }
21529 run_test 419 "Verify open file by name doesn't crash kernel"
21530
21531 test_420()
21532 {
21533         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
21534                 skip "Need MDS version at least 2.12.53"
21535
21536         local SAVE_UMASK=$(umask)
21537         local dir=$DIR/$tdir
21538         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
21539
21540         mkdir -p $dir
21541         umask 0000
21542         mkdir -m03777 $dir/testdir
21543         ls -dn $dir/testdir
21544         # Need to remove trailing '.' when SELinux is enabled
21545         local dirperms=$(ls -dn $dir/testdir |
21546                          awk '{ sub(/\.$/, "", $1); print $1}')
21547         [ $dirperms == "drwxrwsrwt" ] ||
21548                 error "incorrect perms on $dir/testdir"
21549
21550         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
21551                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
21552         ls -n $dir/testdir/testfile
21553         local fileperms=$(ls -n $dir/testdir/testfile |
21554                           awk '{ sub(/\.$/, "", $1); print $1}')
21555         [ $fileperms == "-rwxr-xr-x" ] ||
21556                 error "incorrect perms on $dir/testdir/testfile"
21557
21558         umask $SAVE_UMASK
21559 }
21560 run_test 420 "clear SGID bit on non-directories for non-members"
21561
21562 test_421a() {
21563         local cnt
21564         local fid1
21565         local fid2
21566
21567         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21568                 skip "Need MDS version at least 2.12.54"
21569
21570         test_mkdir $DIR/$tdir
21571         createmany -o $DIR/$tdir/f 3
21572         cnt=$(ls -1 $DIR/$tdir | wc -l)
21573         [ $cnt != 3 ] && error "unexpected #files: $cnt"
21574
21575         fid1=$(lfs path2fid $DIR/$tdir/f1)
21576         fid2=$(lfs path2fid $DIR/$tdir/f2)
21577         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
21578
21579         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
21580         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
21581
21582         cnt=$(ls -1 $DIR/$tdir | wc -l)
21583         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
21584
21585         rm -f $DIR/$tdir/f3 || error "can't remove f3"
21586         createmany -o $DIR/$tdir/f 3
21587         cnt=$(ls -1 $DIR/$tdir | wc -l)
21588         [ $cnt != 3 ] && error "unexpected #files: $cnt"
21589
21590         fid1=$(lfs path2fid $DIR/$tdir/f1)
21591         fid2=$(lfs path2fid $DIR/$tdir/f2)
21592         echo "remove using fsname $FSNAME"
21593         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
21594
21595         cnt=$(ls -1 $DIR/$tdir | wc -l)
21596         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
21597 }
21598 run_test 421a "simple rm by fid"
21599
21600 test_421b() {
21601         local cnt
21602         local FID1
21603         local FID2
21604
21605         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21606                 skip "Need MDS version at least 2.12.54"
21607
21608         test_mkdir $DIR/$tdir
21609         createmany -o $DIR/$tdir/f 3
21610         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
21611         MULTIPID=$!
21612
21613         FID1=$(lfs path2fid $DIR/$tdir/f1)
21614         FID2=$(lfs path2fid $DIR/$tdir/f2)
21615         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
21616
21617         kill -USR1 $MULTIPID
21618         wait
21619
21620         cnt=$(ls $DIR/$tdir | wc -l)
21621         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
21622 }
21623 run_test 421b "rm by fid on open file"
21624
21625 test_421c() {
21626         local cnt
21627         local FIDS
21628
21629         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21630                 skip "Need MDS version at least 2.12.54"
21631
21632         test_mkdir $DIR/$tdir
21633         createmany -o $DIR/$tdir/f 3
21634         touch $DIR/$tdir/$tfile
21635         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
21636         cnt=$(ls -1 $DIR/$tdir | wc -l)
21637         [ $cnt != 184 ] && error "unexpected #files: $cnt"
21638
21639         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
21640         $LFS rmfid $DIR $FID1 || error "rmfid failed"
21641
21642         cnt=$(ls $DIR/$tdir | wc -l)
21643         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
21644 }
21645 run_test 421c "rm by fid against hardlinked files"
21646
21647 test_421d() {
21648         local cnt
21649         local FIDS
21650
21651         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21652                 skip "Need MDS version at least 2.12.54"
21653
21654         test_mkdir $DIR/$tdir
21655         createmany -o $DIR/$tdir/f 4097
21656         cnt=$(ls -1 $DIR/$tdir | wc -l)
21657         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
21658
21659         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
21660         $LFS rmfid $DIR $FIDS || error "rmfid failed"
21661
21662         cnt=$(ls $DIR/$tdir | wc -l)
21663         rm -rf $DIR/$tdir
21664         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21665 }
21666 run_test 421d "rmfid en masse"
21667
21668 test_421e() {
21669         local cnt
21670         local FID
21671
21672         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21673         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21674                 skip "Need MDS version at least 2.12.54"
21675
21676         mkdir -p $DIR/$tdir
21677         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
21678         createmany -o $DIR/$tdir/striped_dir/f 512
21679         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21680         [ $cnt != 512 ] && error "unexpected #files: $cnt"
21681
21682         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
21683                 sed "s/[/][^:]*://g")
21684         $LFS rmfid $DIR $FIDS || error "rmfid failed"
21685
21686         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
21687         rm -rf $DIR/$tdir
21688         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21689 }
21690 run_test 421e "rmfid in DNE"
21691
21692 test_421f() {
21693         local cnt
21694         local FID
21695
21696         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21697                 skip "Need MDS version at least 2.12.54"
21698
21699         test_mkdir $DIR/$tdir
21700         touch $DIR/$tdir/f
21701         cnt=$(ls -1 $DIR/$tdir | wc -l)
21702         [ $cnt != 1 ] && error "unexpected #files: $cnt"
21703
21704         FID=$(lfs path2fid $DIR/$tdir/f)
21705         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
21706         # rmfid should fail
21707         cnt=$(ls -1 $DIR/$tdir | wc -l)
21708         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
21709
21710         chmod a+rw $DIR/$tdir
21711         ls -la $DIR/$tdir
21712         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
21713         # rmfid should fail
21714         cnt=$(ls -1 $DIR/$tdir | wc -l)
21715         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
21716
21717         rm -f $DIR/$tdir/f
21718         $RUNAS touch $DIR/$tdir/f
21719         FID=$(lfs path2fid $DIR/$tdir/f)
21720         echo "rmfid as root"
21721         $LFS rmfid $DIR $FID || error "rmfid as root failed"
21722         cnt=$(ls -1 $DIR/$tdir | wc -l)
21723         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
21724
21725         rm -f $DIR/$tdir/f
21726         $RUNAS touch $DIR/$tdir/f
21727         cnt=$(ls -1 $DIR/$tdir | wc -l)
21728         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
21729         FID=$(lfs path2fid $DIR/$tdir/f)
21730         # rmfid w/o user_fid2path mount option should fail
21731         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
21732         cnt=$(ls -1 $DIR/$tdir | wc -l)
21733         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
21734
21735         umount_client $MOUNT || error "failed to umount client"
21736         mount_client $MOUNT "$MOUNT_OPTS,user_fid2path" ||
21737                 error "failed to mount client'"
21738
21739         $RUNAS $LFS rmfid $DIR $FID || error "rmfid failed"
21740         # rmfid should succeed
21741         cnt=$(ls -1 $DIR/$tdir | wc -l)
21742         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
21743
21744         # rmfid shouldn't allow to remove files due to dir's permission
21745         chmod a+rwx $DIR/$tdir
21746         touch $DIR/$tdir/f
21747         ls -la $DIR/$tdir
21748         FID=$(lfs path2fid $DIR/$tdir/f)
21749         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail"
21750
21751         umount_client $MOUNT || error "failed to umount client"
21752         mount_client $MOUNT "$MOUNT_OPTS" ||
21753                 error "failed to mount client'"
21754
21755 }
21756 run_test 421f "rmfid checks permissions"
21757
21758 test_421g() {
21759         local cnt
21760         local FIDS
21761
21762         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21763         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21764                 skip "Need MDS version at least 2.12.54"
21765
21766         mkdir -p $DIR/$tdir
21767         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
21768         createmany -o $DIR/$tdir/striped_dir/f 512
21769         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21770         [ $cnt != 512 ] && error "unexpected #files: $cnt"
21771
21772         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
21773                 sed "s/[/][^:]*://g")
21774
21775         rm -f $DIR/$tdir/striped_dir/f1*
21776         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21777         removed=$((512 - cnt))
21778
21779         # few files have been just removed, so we expect
21780         # rmfid to fail on their fids
21781         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
21782         [ $removed != $errors ] && error "$errors != $removed"
21783
21784         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
21785         rm -rf $DIR/$tdir
21786         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21787 }
21788 run_test 421g "rmfid to return errors properly"
21789
21790 test_422() {
21791         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d1
21792         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d2
21793         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d3
21794         dd if=/dev/zero of=$DIR/$tdir/d1/file1 bs=1k count=1
21795         dd if=/dev/zero of=$DIR/$tdir/d2/file1 bs=1k count=1
21796
21797         local amc=$(at_max_get client)
21798         local amo=$(at_max_get mds1)
21799         local timeout=`lctl get_param -n timeout`
21800
21801         at_max_set 0 client
21802         at_max_set 0 mds1
21803
21804 #define OBD_FAIL_PTLRPC_PAUSE_REQ        0x50a
21805         do_facet mds1 $LCTL set_param fail_loc=0x8000050a \
21806                         fail_val=$(((2*timeout + 10)*1000))
21807         touch $DIR/$tdir/d3/file &
21808         sleep 2
21809 #define OBD_FAIL_TGT_REPLY_DATA_RACE     0x722
21810         do_facet mds1 $LCTL set_param fail_loc=0x80000722 \
21811                         fail_val=$((2*timeout + 5))
21812         mv $DIR/$tdir/d1/file1 $DIR/$tdir/d1/file2 &
21813         local pid=$!
21814         sleep 1
21815         kill -9 $pid
21816         sleep $((2 * timeout))
21817         echo kill $pid
21818         kill -9 $pid
21819         lctl mark touch
21820         touch $DIR/$tdir/d2/file3
21821         touch $DIR/$tdir/d2/file4
21822         touch $DIR/$tdir/d2/file5
21823
21824         wait
21825         at_max_set $amc client
21826         at_max_set $amo mds1
21827
21828         # LU-12838 - verify the ptlrpc thread watchdog is not always throttled
21829         do_facet mds1 "dmesg | grep 'Dumping the stack trace for debugging'" ||
21830                 error "Watchdog is always throttled"
21831 }
21832 run_test 422 "kill a process with RPC in progress"
21833
21834 prep_801() {
21835         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
21836         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
21837                 skip "Need server version at least 2.9.55"
21838
21839         start_full_debug_logging
21840 }
21841
21842 post_801() {
21843         stop_full_debug_logging
21844 }
21845
21846 barrier_stat() {
21847         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
21848                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
21849                            awk '/The barrier for/ { print $7 }')
21850                 echo $st
21851         else
21852                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
21853                 echo \'$st\'
21854         fi
21855 }
21856
21857 barrier_expired() {
21858         local expired
21859
21860         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
21861                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
21862                           awk '/will be expired/ { print $7 }')
21863         else
21864                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
21865         fi
21866
21867         echo $expired
21868 }
21869
21870 test_801a() {
21871         prep_801
21872
21873         echo "Start barrier_freeze at: $(date)"
21874         #define OBD_FAIL_BARRIER_DELAY          0x2202
21875         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
21876         # Do not reduce barrier time - See LU-11873
21877         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
21878
21879         sleep 2
21880         local b_status=$(barrier_stat)
21881         echo "Got barrier status at: $(date)"
21882         [ "$b_status" = "'freezing_p1'" ] ||
21883                 error "(1) unexpected barrier status $b_status"
21884
21885         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
21886         wait
21887         b_status=$(barrier_stat)
21888         [ "$b_status" = "'frozen'" ] ||
21889                 error "(2) unexpected barrier status $b_status"
21890
21891         local expired=$(barrier_expired)
21892         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
21893         sleep $((expired + 3))
21894
21895         b_status=$(barrier_stat)
21896         [ "$b_status" = "'expired'" ] ||
21897                 error "(3) unexpected barrier status $b_status"
21898
21899         # Do not reduce barrier time - See LU-11873
21900         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
21901                 error "(4) fail to freeze barrier"
21902
21903         b_status=$(barrier_stat)
21904         [ "$b_status" = "'frozen'" ] ||
21905                 error "(5) unexpected barrier status $b_status"
21906
21907         echo "Start barrier_thaw at: $(date)"
21908         #define OBD_FAIL_BARRIER_DELAY          0x2202
21909         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
21910         do_facet mgs $LCTL barrier_thaw $FSNAME &
21911
21912         sleep 2
21913         b_status=$(barrier_stat)
21914         echo "Got barrier status at: $(date)"
21915         [ "$b_status" = "'thawing'" ] ||
21916                 error "(6) unexpected barrier status $b_status"
21917
21918         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
21919         wait
21920         b_status=$(barrier_stat)
21921         [ "$b_status" = "'thawed'" ] ||
21922                 error "(7) unexpected barrier status $b_status"
21923
21924         #define OBD_FAIL_BARRIER_FAILURE        0x2203
21925         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
21926         do_facet mgs $LCTL barrier_freeze $FSNAME
21927
21928         b_status=$(barrier_stat)
21929         [ "$b_status" = "'failed'" ] ||
21930                 error "(8) unexpected barrier status $b_status"
21931
21932         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
21933         do_facet mgs $LCTL barrier_thaw $FSNAME
21934
21935         post_801
21936 }
21937 run_test 801a "write barrier user interfaces and stat machine"
21938
21939 test_801b() {
21940         prep_801
21941
21942         mkdir $DIR/$tdir || error "(1) fail to mkdir"
21943         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
21944         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
21945         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
21946         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
21947
21948         cancel_lru_locks mdc
21949
21950         # 180 seconds should be long enough
21951         do_facet mgs $LCTL barrier_freeze $FSNAME 180
21952
21953         local b_status=$(barrier_stat)
21954         [ "$b_status" = "'frozen'" ] ||
21955                 error "(6) unexpected barrier status $b_status"
21956
21957         mkdir $DIR/$tdir/d0/d10 &
21958         mkdir_pid=$!
21959
21960         touch $DIR/$tdir/d1/f13 &
21961         touch_pid=$!
21962
21963         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
21964         ln_pid=$!
21965
21966         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
21967         mv_pid=$!
21968
21969         rm -f $DIR/$tdir/d4/f12 &
21970         rm_pid=$!
21971
21972         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
21973
21974         # To guarantee taht the 'stat' is not blocked
21975         b_status=$(barrier_stat)
21976         [ "$b_status" = "'frozen'" ] ||
21977                 error "(8) unexpected barrier status $b_status"
21978
21979         # let above commands to run at background
21980         sleep 5
21981
21982         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
21983         ps -p $touch_pid || error "(10) touch should be blocked"
21984         ps -p $ln_pid || error "(11) link should be blocked"
21985         ps -p $mv_pid || error "(12) rename should be blocked"
21986         ps -p $rm_pid || error "(13) unlink should be blocked"
21987
21988         b_status=$(barrier_stat)
21989         [ "$b_status" = "'frozen'" ] ||
21990                 error "(14) unexpected barrier status $b_status"
21991
21992         do_facet mgs $LCTL barrier_thaw $FSNAME
21993         b_status=$(barrier_stat)
21994         [ "$b_status" = "'thawed'" ] ||
21995                 error "(15) unexpected barrier status $b_status"
21996
21997         wait $mkdir_pid || error "(16) mkdir should succeed"
21998         wait $touch_pid || error "(17) touch should succeed"
21999         wait $ln_pid || error "(18) link should succeed"
22000         wait $mv_pid || error "(19) rename should succeed"
22001         wait $rm_pid || error "(20) unlink should succeed"
22002
22003         post_801
22004 }
22005 run_test 801b "modification will be blocked by write barrier"
22006
22007 test_801c() {
22008         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22009
22010         prep_801
22011
22012         stop mds2 || error "(1) Fail to stop mds2"
22013
22014         do_facet mgs $LCTL barrier_freeze $FSNAME 30
22015
22016         local b_status=$(barrier_stat)
22017         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
22018                 do_facet mgs $LCTL barrier_thaw $FSNAME
22019                 error "(2) unexpected barrier status $b_status"
22020         }
22021
22022         do_facet mgs $LCTL barrier_rescan $FSNAME ||
22023                 error "(3) Fail to rescan barrier bitmap"
22024
22025         # Do not reduce barrier time - See LU-11873
22026         do_facet mgs $LCTL barrier_freeze $FSNAME 20
22027
22028         b_status=$(barrier_stat)
22029         [ "$b_status" = "'frozen'" ] ||
22030                 error "(4) unexpected barrier status $b_status"
22031
22032         do_facet mgs $LCTL barrier_thaw $FSNAME
22033         b_status=$(barrier_stat)
22034         [ "$b_status" = "'thawed'" ] ||
22035                 error "(5) unexpected barrier status $b_status"
22036
22037         local devname=$(mdsdevname 2)
22038
22039         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
22040
22041         do_facet mgs $LCTL barrier_rescan $FSNAME ||
22042                 error "(7) Fail to rescan barrier bitmap"
22043
22044         post_801
22045 }
22046 run_test 801c "rescan barrier bitmap"
22047
22048 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
22049 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
22050 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
22051 saved_MOUNT_OPTS=$MOUNT_OPTS
22052
22053 cleanup_802a() {
22054         trap 0
22055
22056         stopall
22057         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
22058         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
22059         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
22060         MOUNT_OPTS=$saved_MOUNT_OPTS
22061         setupall
22062 }
22063
22064 test_802a() {
22065         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
22066         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
22067         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
22068                 skip "Need server version at least 2.9.55"
22069
22070         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
22071
22072         mkdir $DIR/$tdir || error "(1) fail to mkdir"
22073
22074         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
22075                 error "(2) Fail to copy"
22076
22077         trap cleanup_802a EXIT
22078
22079         # sync by force before remount as readonly
22080         sync; sync_all_data; sleep 3; sync_all_data
22081
22082         stopall
22083
22084         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
22085         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
22086         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
22087
22088         echo "Mount the server as read only"
22089         setupall server_only || error "(3) Fail to start servers"
22090
22091         echo "Mount client without ro should fail"
22092         mount_client $MOUNT &&
22093                 error "(4) Mount client without 'ro' should fail"
22094
22095         echo "Mount client with ro should succeed"
22096         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
22097         mount_client $MOUNT ||
22098                 error "(5) Mount client with 'ro' should succeed"
22099
22100         echo "Modify should be refused"
22101         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
22102
22103         echo "Read should be allowed"
22104         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
22105                 error "(7) Read should succeed under ro mode"
22106
22107         cleanup_802a
22108 }
22109 run_test 802a "simulate readonly device"
22110
22111 test_802b() {
22112         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22113         remote_mds_nodsh && skip "remote MDS with nodsh"
22114
22115         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
22116                 skip "readonly option not available"
22117
22118         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
22119
22120         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
22121                 error "(2) Fail to copy"
22122
22123         # write back all cached data before setting MDT to readonly
22124         cancel_lru_locks
22125         sync_all_data
22126
22127         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
22128         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
22129
22130         echo "Modify should be refused"
22131         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
22132
22133         echo "Read should be allowed"
22134         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
22135                 error "(7) Read should succeed under ro mode"
22136
22137         # disable readonly
22138         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
22139 }
22140 run_test 802b "be able to set MDTs to readonly"
22141
22142 test_803() {
22143         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22144         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
22145                 skip "MDS needs to be newer than 2.10.54"
22146
22147         mkdir -p $DIR/$tdir
22148         # Create some objects on all MDTs to trigger related logs objects
22149         for idx in $(seq $MDSCOUNT); do
22150                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
22151                         $DIR/$tdir/dir${idx} ||
22152                         error "Fail to create $DIR/$tdir/dir${idx}"
22153         done
22154
22155         sync; sleep 3
22156         wait_delete_completed # ensure old test cleanups are finished
22157         echo "before create:"
22158         $LFS df -i $MOUNT
22159         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
22160
22161         for i in {1..10}; do
22162                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
22163                         error "Fail to create $DIR/$tdir/foo$i"
22164         done
22165
22166         sync; sleep 3
22167         echo "after create:"
22168         $LFS df -i $MOUNT
22169         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
22170
22171         # allow for an llog to be cleaned up during the test
22172         [ $after_used -ge $((before_used + 10 - 1)) ] ||
22173                 error "before ($before_used) + 10 > after ($after_used)"
22174
22175         for i in {1..10}; do
22176                 rm -rf $DIR/$tdir/foo$i ||
22177                         error "Fail to remove $DIR/$tdir/foo$i"
22178         done
22179
22180         sleep 3 # avoid MDT return cached statfs
22181         wait_delete_completed
22182         echo "after unlink:"
22183         $LFS df -i $MOUNT
22184         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
22185
22186         # allow for an llog to be created during the test
22187         [ $after_used -le $((before_used + 1)) ] ||
22188                 error "after ($after_used) > before ($before_used) + 1"
22189 }
22190 run_test 803 "verify agent object for remote object"
22191
22192 test_804() {
22193         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22194         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
22195                 skip "MDS needs to be newer than 2.10.54"
22196         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
22197
22198         mkdir -p $DIR/$tdir
22199         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
22200                 error "Fail to create $DIR/$tdir/dir0"
22201
22202         local fid=$($LFS path2fid $DIR/$tdir/dir0)
22203         local dev=$(mdsdevname 2)
22204
22205         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22206                 grep ${fid} || error "NOT found agent entry for dir0"
22207
22208         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
22209                 error "Fail to create $DIR/$tdir/dir1"
22210
22211         touch $DIR/$tdir/dir1/foo0 ||
22212                 error "Fail to create $DIR/$tdir/dir1/foo0"
22213         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
22214         local rc=0
22215
22216         for idx in $(seq $MDSCOUNT); do
22217                 dev=$(mdsdevname $idx)
22218                 do_facet mds${idx} \
22219                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22220                         grep ${fid} && rc=$idx
22221         done
22222
22223         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
22224                 error "Fail to rename foo0 to foo1"
22225         if [ $rc -eq 0 ]; then
22226                 for idx in $(seq $MDSCOUNT); do
22227                         dev=$(mdsdevname $idx)
22228                         do_facet mds${idx} \
22229                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22230                         grep ${fid} && rc=$idx
22231                 done
22232         fi
22233
22234         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
22235                 error "Fail to rename foo1 to foo2"
22236         if [ $rc -eq 0 ]; then
22237                 for idx in $(seq $MDSCOUNT); do
22238                         dev=$(mdsdevname $idx)
22239                         do_facet mds${idx} \
22240                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22241                         grep ${fid} && rc=$idx
22242                 done
22243         fi
22244
22245         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
22246
22247         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
22248                 error "Fail to link to $DIR/$tdir/dir1/foo2"
22249         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
22250                 error "Fail to rename foo2 to foo0"
22251         unlink $DIR/$tdir/dir1/foo0 ||
22252                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
22253         rm -rf $DIR/$tdir/dir0 ||
22254                 error "Fail to rm $DIR/$tdir/dir0"
22255
22256         for idx in $(seq $MDSCOUNT); do
22257                 dev=$(mdsdevname $idx)
22258                 rc=0
22259
22260                 stop mds${idx}
22261                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
22262                         rc=$?
22263                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
22264                         error "mount mds$idx failed"
22265                 df $MOUNT > /dev/null 2>&1
22266
22267                 # e2fsck should not return error
22268                 [ $rc -eq 0 ] ||
22269                         error "e2fsck detected error on MDT${idx}: rc=$rc"
22270         done
22271 }
22272 run_test 804 "verify agent entry for remote entry"
22273
22274 cleanup_805() {
22275         do_facet $SINGLEMDS zfs set quota=$old $fsset
22276         unlinkmany $DIR/$tdir/f- 1000000
22277         trap 0
22278 }
22279
22280 test_805() {
22281         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
22282         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
22283         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
22284                 skip "netfree not implemented before 0.7"
22285         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
22286                 skip "Need MDS version at least 2.10.57"
22287
22288         local fsset
22289         local freekb
22290         local usedkb
22291         local old
22292         local quota
22293         local pref="osd-zfs.$FSNAME-MDT0000."
22294
22295         # limit available space on MDS dataset to meet nospace issue
22296         # quickly. then ZFS 0.7.2 can use reserved space if asked
22297         # properly (using netfree flag in osd_declare_destroy()
22298         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
22299         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
22300                 gawk '{print $3}')
22301         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
22302         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
22303         let "usedkb=usedkb-freekb"
22304         let "freekb=freekb/2"
22305         if let "freekb > 5000"; then
22306                 let "freekb=5000"
22307         fi
22308         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
22309         trap cleanup_805 EXIT
22310         mkdir $DIR/$tdir
22311         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
22312         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
22313         rm -rf $DIR/$tdir || error "not able to remove"
22314         do_facet $SINGLEMDS zfs set quota=$old $fsset
22315         trap 0
22316 }
22317 run_test 805 "ZFS can remove from full fs"
22318
22319 # Size-on-MDS test
22320 check_lsom_data()
22321 {
22322         local file=$1
22323         local size=$($LFS getsom -s $file)
22324         local expect=$(stat -c %s $file)
22325
22326         [[ $size == $expect ]] ||
22327                 error "$file expected size: $expect, got: $size"
22328
22329         local blocks=$($LFS getsom -b $file)
22330         expect=$(stat -c %b $file)
22331         [[ $blocks == $expect ]] ||
22332                 error "$file expected blocks: $expect, got: $blocks"
22333 }
22334
22335 check_lsom_size()
22336 {
22337         local size=$($LFS getsom -s $1)
22338         local expect=$2
22339
22340         [[ $size == $expect ]] ||
22341                 error "$file expected size: $expect, got: $size"
22342 }
22343
22344 test_806() {
22345         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
22346                 skip "Need MDS version at least 2.11.52"
22347
22348         local bs=1048576
22349
22350         touch $DIR/$tfile || error "touch $tfile failed"
22351
22352         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
22353         save_lustre_params client "llite.*.xattr_cache" > $save
22354         lctl set_param llite.*.xattr_cache=0
22355         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
22356
22357         # single-threaded write
22358         echo "Test SOM for single-threaded write"
22359         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
22360                 error "write $tfile failed"
22361         check_lsom_size $DIR/$tfile $bs
22362
22363         local num=32
22364         local size=$(($num * $bs))
22365         local offset=0
22366         local i
22367
22368         echo "Test SOM for single client multi-threaded($num) write"
22369         $TRUNCATE $DIR/$tfile 0
22370         for ((i = 0; i < $num; i++)); do
22371                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22372                 local pids[$i]=$!
22373                 offset=$((offset + $bs))
22374         done
22375         for (( i=0; i < $num; i++ )); do
22376                 wait ${pids[$i]}
22377         done
22378         check_lsom_size $DIR/$tfile $size
22379
22380         $TRUNCATE $DIR/$tfile 0
22381         for ((i = 0; i < $num; i++)); do
22382                 offset=$((offset - $bs))
22383                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22384                 local pids[$i]=$!
22385         done
22386         for (( i=0; i < $num; i++ )); do
22387                 wait ${pids[$i]}
22388         done
22389         check_lsom_size $DIR/$tfile $size
22390
22391         # multi-client writes
22392         num=$(get_node_count ${CLIENTS//,/ })
22393         size=$(($num * $bs))
22394         offset=0
22395         i=0
22396
22397         echo "Test SOM for multi-client ($num) writes"
22398         $TRUNCATE $DIR/$tfile 0
22399         for client in ${CLIENTS//,/ }; do
22400                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22401                 local pids[$i]=$!
22402                 i=$((i + 1))
22403                 offset=$((offset + $bs))
22404         done
22405         for (( i=0; i < $num; i++ )); do
22406                 wait ${pids[$i]}
22407         done
22408         check_lsom_size $DIR/$tfile $offset
22409
22410         i=0
22411         $TRUNCATE $DIR/$tfile 0
22412         for client in ${CLIENTS//,/ }; do
22413                 offset=$((offset - $bs))
22414                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22415                 local pids[$i]=$!
22416                 i=$((i + 1))
22417         done
22418         for (( i=0; i < $num; i++ )); do
22419                 wait ${pids[$i]}
22420         done
22421         check_lsom_size $DIR/$tfile $size
22422
22423         # verify truncate
22424         echo "Test SOM for truncate"
22425         $TRUNCATE $DIR/$tfile 1048576
22426         check_lsom_size $DIR/$tfile 1048576
22427         $TRUNCATE $DIR/$tfile 1234
22428         check_lsom_size $DIR/$tfile 1234
22429
22430         # verify SOM blocks count
22431         echo "Verify SOM block count"
22432         $TRUNCATE $DIR/$tfile 0
22433         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
22434                 error "failed to write file $tfile"
22435         check_lsom_data $DIR/$tfile
22436 }
22437 run_test 806 "Verify Lazy Size on MDS"
22438
22439 test_807() {
22440         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
22441         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
22442                 skip "Need MDS version at least 2.11.52"
22443
22444         # Registration step
22445         changelog_register || error "changelog_register failed"
22446         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
22447         changelog_users $SINGLEMDS | grep -q $cl_user ||
22448                 error "User $cl_user not found in changelog_users"
22449
22450         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
22451         save_lustre_params client "llite.*.xattr_cache" > $save
22452         lctl set_param llite.*.xattr_cache=0
22453         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
22454
22455         rm -rf $DIR/$tdir || error "rm $tdir failed"
22456         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
22457         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
22458         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
22459         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
22460                 error "truncate $tdir/trunc failed"
22461
22462         local bs=1048576
22463         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 ||
22464                 error "write $tfile failed"
22465
22466         # multi-client wirtes
22467         local num=$(get_node_count ${CLIENTS//,/ })
22468         local offset=0
22469         local i=0
22470
22471         echo "Test SOM for multi-client ($num) writes"
22472         touch $DIR/$tfile || error "touch $tfile failed"
22473         $TRUNCATE $DIR/$tfile 0
22474         for client in ${CLIENTS//,/ }; do
22475                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22476                 local pids[$i]=$!
22477                 i=$((i + 1))
22478                 offset=$((offset + $bs))
22479         done
22480         for (( i=0; i < $num; i++ )); do
22481                 wait ${pids[$i]}
22482         done
22483
22484         sleep 5
22485         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
22486         check_lsom_data $DIR/$tdir/trunc
22487         check_lsom_data $DIR/$tdir/single_dd
22488         check_lsom_data $DIR/$tfile
22489
22490         rm -rf $DIR/$tdir
22491         # Deregistration step
22492         changelog_deregister || error "changelog_deregister failed"
22493 }
22494 run_test 807 "verify LSOM syncing tool"
22495
22496 check_som_nologged()
22497 {
22498         local lines=$($LFS changelog $FSNAME-MDT0000 |
22499                 grep 'x=trusted.som' | wc -l)
22500         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
22501 }
22502
22503 test_808() {
22504         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
22505                 skip "Need MDS version at least 2.11.55"
22506
22507         # Registration step
22508         changelog_register || error "changelog_register failed"
22509
22510         touch $DIR/$tfile || error "touch $tfile failed"
22511         check_som_nologged
22512
22513         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
22514                 error "write $tfile failed"
22515         check_som_nologged
22516
22517         $TRUNCATE $DIR/$tfile 1234
22518         check_som_nologged
22519
22520         $TRUNCATE $DIR/$tfile 1048576
22521         check_som_nologged
22522
22523         # Deregistration step
22524         changelog_deregister || error "changelog_deregister failed"
22525 }
22526 run_test 808 "Check trusted.som xattr not logged in Changelogs"
22527
22528 check_som_nodata()
22529 {
22530         $LFS getsom $1
22531         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
22532 }
22533
22534 test_809() {
22535         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
22536                 skip "Need MDS version at least 2.11.56"
22537
22538         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
22539                 error "failed to create DoM-only file $DIR/$tfile"
22540         touch $DIR/$tfile || error "touch $tfile failed"
22541         check_som_nodata $DIR/$tfile
22542
22543         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
22544                 error "write $tfile failed"
22545         check_som_nodata $DIR/$tfile
22546
22547         $TRUNCATE $DIR/$tfile 1234
22548         check_som_nodata $DIR/$tfile
22549
22550         $TRUNCATE $DIR/$tfile 4097
22551         check_som_nodata $DIR/$file
22552 }
22553 run_test 809 "Verify no SOM xattr store for DoM-only files"
22554
22555 test_810() {
22556         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22557         $GSS && skip_env "could not run with gss"
22558         [[ $OST1_VERSION -gt $(version_code 2.12.58) ]] ||
22559                 skip "OST < 2.12.58 doesn't align checksum"
22560
22561         set_checksums 1
22562         stack_trap "set_checksums $ORIG_CSUM" EXIT
22563         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
22564
22565         local csum
22566         local before
22567         local after
22568         for csum in $CKSUM_TYPES; do
22569                 #define OBD_FAIL_OSC_NO_GRANT   0x411
22570                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
22571                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
22572                         eval set -- $i
22573                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
22574                         before=$(md5sum $DIR/$tfile)
22575                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
22576                         after=$(md5sum $DIR/$tfile)
22577                         [ "$before" == "$after" ] ||
22578                                 error "$csum: $before != $after bs=$1 seek=$2"
22579                 done
22580         done
22581 }
22582 run_test 810 "partial page writes on ZFS (LU-11663)"
22583
22584 test_811() {
22585         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.56) ] &&
22586                 skip "Need MDS version at least 2.11.56"
22587
22588         #define OBD_FAIL_MDS_ORPHAN_DELETE      0x165
22589         do_facet mds1 $LCTL set_param fail_loc=0x165
22590         $MULTIOP $DIR/$tfile Ouc || error "multiop failed"
22591
22592         stop mds1
22593         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
22594
22595         sleep 5
22596         [[ $(do_facet mds1 pgrep orph_.*-MDD | wc -l) -eq 0 ]] ||
22597                 error "MDD orphan cleanup thread not quit"
22598 }
22599 run_test 811 "orphan name stub can be cleaned up in startup"
22600
22601 test_812a() {
22602         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
22603                 skip "OST < 2.12.51 doesn't support this fail_loc"
22604         [ "$SHARED_KEY" = true ] &&
22605                 skip "OSC connections never go IDLE with Shared-Keys enabled"
22606
22607         $LFS setstripe -c 1 -i 0 $DIR/$tfile
22608         # ensure ost1 is connected
22609         stat $DIR/$tfile >/dev/null || error "can't stat"
22610         wait_osc_import_state client ost1 FULL
22611         # no locks, no reqs to let the connection idle
22612         cancel_lru_locks osc
22613
22614         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
22615 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
22616         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
22617         wait_osc_import_state client ost1 CONNECTING
22618         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
22619
22620         stat $DIR/$tfile >/dev/null || error "can't stat file"
22621 }
22622 run_test 812a "do not drop reqs generated when imp is going to idle (LU-11951)"
22623
22624 test_812b() { # LU-12378
22625         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
22626                 skip "OST < 2.12.51 doesn't support this fail_loc"
22627         [ "$SHARED_KEY" = true ] &&
22628                 skip "OSC connections never go IDLE with Shared-Keys enabled"
22629
22630         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "setstripe failed"
22631         # ensure ost1 is connected
22632         stat $DIR/$tfile >/dev/null || error "can't stat"
22633         wait_osc_import_state client ost1 FULL
22634         # no locks, no reqs to let the connection idle
22635         cancel_lru_locks osc
22636
22637         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
22638 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
22639         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
22640         wait_osc_import_state client ost1 CONNECTING
22641         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
22642
22643         $LFS quota -u 0 $DIR/ || error "lfs quota should succeed"
22644         wait_osc_import_state client ost1 IDLE
22645 }
22646 run_test 812b "do not drop no resend request for idle connect"
22647
22648 test_813() {
22649         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
22650         [ -z "$file_heat_sav" ] && skip "no file heat support"
22651
22652         local readsample
22653         local writesample
22654         local readbyte
22655         local writebyte
22656         local readsample1
22657         local writesample1
22658         local readbyte1
22659         local writebyte1
22660
22661         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
22662         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
22663
22664         $LCTL set_param -n llite.*.file_heat=1
22665         echo "Turn on file heat"
22666         echo "Period second: $period_second, Decay percentage: $decay_pct"
22667
22668         echo "QQQQ" > $DIR/$tfile
22669         echo "QQQQ" > $DIR/$tfile
22670         echo "QQQQ" > $DIR/$tfile
22671         cat $DIR/$tfile > /dev/null
22672         cat $DIR/$tfile > /dev/null
22673         cat $DIR/$tfile > /dev/null
22674         cat $DIR/$tfile > /dev/null
22675
22676         local out=$($LFS heat_get $DIR/$tfile)
22677
22678         $LFS heat_get $DIR/$tfile
22679         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22680         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22681         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22682         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22683
22684         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
22685         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
22686         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
22687         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
22688
22689         sleep $((period_second + 3))
22690         echo "Sleep $((period_second + 3)) seconds..."
22691         # The recursion formula to calculate the heat of the file f is as
22692         # follow:
22693         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
22694         # Where Hi is the heat value in the period between time points i*I and
22695         # (i+1)*I; Ci is the access count in the period; the symbol P refers
22696         # to the weight of Ci.
22697         out=$($LFS heat_get $DIR/$tfile)
22698         $LFS heat_get $DIR/$tfile
22699         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22700         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22701         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22702         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22703
22704         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
22705                 error "read sample ($readsample) is wrong"
22706         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
22707                 error "write sample ($writesample) is wrong"
22708         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
22709                 error "read bytes ($readbyte) is wrong"
22710         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
22711                 error "write bytes ($writebyte) is wrong"
22712
22713         echo "QQQQ" > $DIR/$tfile
22714         echo "QQQQ" > $DIR/$tfile
22715         echo "QQQQ" > $DIR/$tfile
22716         cat $DIR/$tfile > /dev/null
22717         cat $DIR/$tfile > /dev/null
22718         cat $DIR/$tfile > /dev/null
22719         cat $DIR/$tfile > /dev/null
22720
22721         sleep $((period_second + 3))
22722         echo "Sleep $((period_second + 3)) seconds..."
22723
22724         out=$($LFS heat_get $DIR/$tfile)
22725         $LFS heat_get $DIR/$tfile
22726         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22727         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22728         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22729         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22730
22731         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
22732                 4 * $decay_pct) / 100") -eq 1 ] ||
22733                 error "read sample ($readsample1) is wrong"
22734         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
22735                 3 * $decay_pct) / 100") -eq 1 ] ||
22736                 error "write sample ($writesample1) is wrong"
22737         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
22738                 20 * $decay_pct) / 100") -eq 1 ] ||
22739                 error "read bytes ($readbyte1) is wrong"
22740         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
22741                 15 * $decay_pct) / 100") -eq 1 ] ||
22742                 error "write bytes ($writebyte1) is wrong"
22743
22744         echo "Turn off file heat for the file $DIR/$tfile"
22745         $LFS heat_set -o $DIR/$tfile
22746
22747         echo "QQQQ" > $DIR/$tfile
22748         echo "QQQQ" > $DIR/$tfile
22749         echo "QQQQ" > $DIR/$tfile
22750         cat $DIR/$tfile > /dev/null
22751         cat $DIR/$tfile > /dev/null
22752         cat $DIR/$tfile > /dev/null
22753         cat $DIR/$tfile > /dev/null
22754
22755         out=$($LFS heat_get $DIR/$tfile)
22756         $LFS heat_get $DIR/$tfile
22757         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22758         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22759         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22760         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22761
22762         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
22763         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
22764         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
22765         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
22766
22767         echo "Trun on file heat for the file $DIR/$tfile"
22768         $LFS heat_set -O $DIR/$tfile
22769
22770         echo "QQQQ" > $DIR/$tfile
22771         echo "QQQQ" > $DIR/$tfile
22772         echo "QQQQ" > $DIR/$tfile
22773         cat $DIR/$tfile > /dev/null
22774         cat $DIR/$tfile > /dev/null
22775         cat $DIR/$tfile > /dev/null
22776         cat $DIR/$tfile > /dev/null
22777
22778         out=$($LFS heat_get $DIR/$tfile)
22779         $LFS heat_get $DIR/$tfile
22780         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22781         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22782         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22783         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22784
22785         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
22786         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
22787         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
22788         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
22789
22790         $LFS heat_set -c $DIR/$tfile
22791         $LCTL set_param -n llite.*.file_heat=0
22792         echo "Turn off file heat support for the Lustre filesystem"
22793
22794         echo "QQQQ" > $DIR/$tfile
22795         echo "QQQQ" > $DIR/$tfile
22796         echo "QQQQ" > $DIR/$tfile
22797         cat $DIR/$tfile > /dev/null
22798         cat $DIR/$tfile > /dev/null
22799         cat $DIR/$tfile > /dev/null
22800         cat $DIR/$tfile > /dev/null
22801
22802         out=$($LFS heat_get $DIR/$tfile)
22803         $LFS heat_get $DIR/$tfile
22804         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22805         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22806         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22807         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22808
22809         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
22810         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
22811         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
22812         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
22813
22814         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
22815         rm -f $DIR/$tfile
22816 }
22817 run_test 813 "File heat verfication"
22818
22819 test_814()
22820 {
22821         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
22822         echo -n y >> $DIR/$tfile
22823         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
22824         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
22825 }
22826 run_test 814 "sparse cp works as expected (LU-12361)"
22827
22828 test_815()
22829 {
22830         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
22831         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
22832 }
22833 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
22834
22835 test_816() {
22836         [ "$SHARED_KEY" = true ] &&
22837                 skip "OSC connections never go IDLE with Shared-Keys enabled"
22838
22839         $LFS setstripe -c 1 -i 0 $DIR/$tfile
22840         # ensure ost1 is connected
22841         stat $DIR/$tfile >/dev/null || error "can't stat"
22842         wait_osc_import_state client ost1 FULL
22843         # no locks, no reqs to let the connection idle
22844         cancel_lru_locks osc
22845         lru_resize_disable osc
22846         local before
22847         local now
22848         before=$($LCTL get_param -n \
22849                  ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
22850
22851         wait_osc_import_state client ost1 IDLE
22852         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
22853         now=$($LCTL get_param -n \
22854               ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
22855         [ $before == $now ] || error "lru_size changed $before != $now"
22856 }
22857 run_test 816 "do not reset lru_resize on idle reconnect"
22858
22859 cleanup_817() {
22860         umount $tmpdir
22861         exportfs -u localhost:$DIR/nfsexp
22862         rm -rf $DIR/nfsexp
22863 }
22864
22865 test_817() {
22866         systemctl restart nfs-server.service || skip "failed to restart nfsd"
22867
22868         mkdir -p $DIR/nfsexp
22869         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
22870                 error "failed to export nfs"
22871
22872         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
22873         stack_trap cleanup_817 EXIT
22874
22875         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
22876                 error "failed to mount nfs to $tmpdir"
22877
22878         cp /bin/true $tmpdir
22879         $DIR/nfsexp/true || error "failed to execute 'true' command"
22880 }
22881 run_test 817 "nfsd won't cache write lock for exec file"
22882
22883 test_818() {
22884         mkdir $DIR/$tdir
22885         $LFS setstripe -c1 -i0 $DIR/$tfile
22886         $LFS setstripe -c1 -i1 $DIR/$tfile
22887         stop $SINGLEMDS
22888         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
22889         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
22890         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
22891                 error "start $SINGLEMDS failed"
22892         rm -rf $DIR/$tdir
22893 }
22894 run_test 818 "unlink with failed llog"
22895
22896 test_819a() {
22897         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22898         cancel_lru_locks osc
22899         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
22900         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
22901         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
22902         rm -f $TDIR/$tfile
22903 }
22904 run_test 819a "too big niobuf in read"
22905
22906 test_819b() {
22907         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
22908         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
22909         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22910         cancel_lru_locks osc
22911         sleep 1
22912         rm -f $TDIR/$tfile
22913 }
22914 run_test 819b "too big niobuf in write"
22915
22916 #
22917 # tests that do cleanup/setup should be run at the end
22918 #
22919
22920 test_900() {
22921         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22922         local ls
22923
22924         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
22925         $LCTL set_param fail_loc=0x903
22926
22927         cancel_lru_locks MGC
22928
22929         FAIL_ON_ERROR=true cleanup
22930         FAIL_ON_ERROR=true setup
22931 }
22932 run_test 900 "umount should not race with any mgc requeue thread"
22933
22934 # LUS-6253/LU-11185
22935 test_901() {
22936         local oldc
22937         local newc
22938         local olds
22939         local news
22940         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22941
22942         # some get_param have a bug to handle dot in param name
22943         cancel_lru_locks MGC
22944         oldc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
22945         olds=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
22946         umount_client $MOUNT || error "umount failed"
22947         mount_client $MOUNT || error "mount failed"
22948         cancel_lru_locks MGC
22949         newc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
22950         news=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
22951
22952         [ $oldc -lt $newc ] && error "mgc lock leak ($oldc != $newc)"
22953         [ $olds -lt $news ] && error "mgs lock leak ($olds != $news)"
22954
22955         return 0
22956 }
22957 run_test 901 "don't leak a mgc lock on client umount"
22958
22959 complete $SECONDS
22960 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
22961 check_and_cleanup_lustre
22962 if [ "$I_MOUNTED" != "yes" ]; then
22963         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
22964 fi
22965 exit_status