Whamcloud - gitweb
3ef952f8e4bd9bc2c4ed4c9102450e0ef41581cd
[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 LU-12781
49         ALWAYS_EXCEPT+=" 17n     60a     133g    300f    272a"
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 LU-4398
63         ALWAYS_EXCEPT+=" 45       317      817"
64 fi
65
66 #                                  5          12          (min)"
67 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 300o"
68
69 if [ "$mds1_FSTYPE" = "zfs" ]; then
70         # bug number for skipped test:
71         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  "
72         #                                               13    (min)"
73         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
74 fi
75
76 # Get the SLES distro version
77 #
78 # Returns a version string that should only be used in comparing
79 # strings returned by version_code()
80 sles_version_code()
81 {
82         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
83
84         # All SuSE Linux versions have one decimal. version_code expects two
85         local sles_version=$version.0
86         version_code $sles_version
87 }
88
89 # Check if we are running on Ubuntu or SLES so we can make decisions on
90 # what tests to run
91 if [ -r /etc/SuSE-release ]; then
92         sles_version=$(sles_version_code)
93         [ $sles_version -lt $(version_code 11.4.0) ] &&
94                 # bug number for skipped test: LU-4341
95                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
96         [ $sles_version -lt $(version_code 12.0.0) ] &&
97                 # bug number for skipped test: LU-3703
98                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
99 elif [ -r /etc/os-release ]; then
100         if grep -qi ubuntu /etc/os-release; then
101                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
102                                                 -e 's/^VERSION=//p' \
103                                                 /etc/os-release |
104                                                 awk '{ print $1 }'))
105
106                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
107                         # bug number for skipped test:
108                         #                LU-10334 LU-10366
109                         ALWAYS_EXCEPT+=" 103a     410"
110                 fi
111         fi
112 fi
113
114 build_test_filter
115 FAIL_ON_ERROR=false
116
117 cleanup() {
118         echo -n "cln.."
119         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
120         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
121 }
122 setup() {
123         echo -n "mnt.."
124         load_modules
125         setupall || exit 10
126         echo "done"
127 }
128
129 check_swap_layouts_support()
130 {
131         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
132                 skip "Does not support layout lock."
133 }
134
135 check_and_setup_lustre
136 DIR=${DIR:-$MOUNT}
137 assert_DIR
138
139 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
140
141 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
142 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
143 rm -rf $DIR/[Rdfs][0-9]*
144
145 # $RUNAS_ID may get set incorrectly somewhere else
146 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
147         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
148
149 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
150
151 if [ "${ONLY}" = "MOUNT" ] ; then
152         echo "Lustre is up, please go on"
153         exit
154 fi
155
156 echo "preparing for tests involving mounts"
157 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
158 touch $EXT2_DEV
159 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
160 echo # add a newline after mke2fs.
161
162 umask 077
163
164 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
165 lctl set_param debug=-1 2> /dev/null || true
166 test_0a() {
167         touch $DIR/$tfile
168         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
169         rm $DIR/$tfile
170         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
171 }
172 run_test 0a "touch; rm ====================="
173
174 test_0b() {
175         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
176         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
177 }
178 run_test 0b "chmod 0755 $DIR ============================="
179
180 test_0c() {
181         $LCTL get_param mdc.*.import | grep "state: FULL" ||
182                 error "import not FULL"
183         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
184                 error "bad target"
185 }
186 run_test 0c "check import proc"
187
188 test_0d() { # LU-3397
189         [ $MGS_VERSION -lt $(version_code 2.10.57) ] &&
190                 skip "proc exports not supported before 2.10.57"
191
192         local mgs_exp="mgs.MGS.exports"
193         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
194         local exp_client_nid
195         local exp_client_version
196         local exp_val
197         local imp_val
198         local temp_imp=$DIR/$tfile.import
199         local temp_exp=$DIR/$tfile.export
200
201         # save mgc import file to $temp_imp
202         $LCTL get_param mgc.*.import | tee $temp_imp
203         # Check if client uuid is found in MGS export
204         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
205                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
206                         $client_uuid ] &&
207                         break;
208         done
209         # save mgs export file to $temp_exp
210         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
211
212         # Compare the value of field "connect_flags"
213         imp_val=$(grep "connect_flags" $temp_imp)
214         exp_val=$(grep "connect_flags" $temp_exp)
215         [ "$exp_val" == "$imp_val" ] ||
216                 error "export flags '$exp_val' != import flags '$imp_val'"
217
218         # Compare the value of client version
219         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
220         exp_val=$(version_code $exp_client_version)
221         imp_val=$CLIENT_VERSION
222         [ "$exp_val" == "$imp_val" ] ||
223                 error "export client version '$exp_val' != '$imp_val'"
224 }
225 run_test 0d "check export proc ============================="
226
227 test_1() {
228         test_mkdir $DIR/$tdir
229         test_mkdir $DIR/$tdir/d2
230         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
231         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
232         rmdir $DIR/$tdir/d2
233         rmdir $DIR/$tdir
234         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
235 }
236 run_test 1 "mkdir; remkdir; rmdir"
237
238 test_2() {
239         test_mkdir $DIR/$tdir
240         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
241         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
242         rm -r $DIR/$tdir
243         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
244 }
245 run_test 2 "mkdir; touch; rmdir; check file"
246
247 test_3() {
248         test_mkdir $DIR/$tdir
249         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
250         touch $DIR/$tdir/$tfile
251         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
252         rm -r $DIR/$tdir
253         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
254 }
255 run_test 3 "mkdir; touch; rmdir; check dir"
256
257 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
258 test_4() {
259         test_mkdir -i 1 $DIR/$tdir
260
261         touch $DIR/$tdir/$tfile ||
262                 error "Create file under remote directory failed"
263
264         rmdir $DIR/$tdir &&
265                 error "Expect error removing in-use dir $DIR/$tdir"
266
267         test -d $DIR/$tdir || error "Remote directory disappeared"
268
269         rm -rf $DIR/$tdir || error "remove remote dir error"
270 }
271 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
272
273 test_5() {
274         test_mkdir $DIR/$tdir
275         test_mkdir $DIR/$tdir/d2
276         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
277         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
278         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
279 }
280 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
281
282 test_6a() {
283         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
284         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
285         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
286                 error "$tfile does not have perm 0666 or UID $UID"
287         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
288         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
289                 error "$tfile should be 0666 and owned by UID $UID"
290 }
291 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
292
293 test_6c() {
294         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
295
296         touch $DIR/$tfile
297         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
298         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
299                 error "$tfile should be owned by UID $RUNAS_ID"
300         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
301         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
302                 error "$tfile should be owned by UID $RUNAS_ID"
303 }
304 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
305
306 test_6e() {
307         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
308
309         touch $DIR/$tfile
310         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
311         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
312                 error "$tfile should be owned by GID $UID"
313         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
314         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
315                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
316 }
317 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
318
319 test_6g() {
320         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
321
322         test_mkdir $DIR/$tdir
323         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
324         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
325         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
326         test_mkdir $DIR/$tdir/d/subdir
327         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
328                 error "$tdir/d/subdir should be GID $RUNAS_GID"
329         if [[ $MDSCOUNT -gt 1 ]]; then
330                 # check remote dir sgid inherite
331                 $LFS mkdir -i 0 $DIR/$tdir.local ||
332                         error "mkdir $tdir.local failed"
333                 chmod g+s $DIR/$tdir.local ||
334                         error "chmod $tdir.local failed"
335                 chgrp $RUNAS_GID $DIR/$tdir.local ||
336                         error "chgrp $tdir.local failed"
337                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
338                         error "mkdir $tdir.remote failed"
339                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
340                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
341                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
342                         error "$tdir.remote should be mode 02755"
343         fi
344 }
345 run_test 6g "verify new dir in sgid dir inherits group"
346
347 test_6h() { # bug 7331
348         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
349
350         touch $DIR/$tfile || error "touch failed"
351         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
352         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
353                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
354         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
355                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
356 }
357 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
358
359 test_7a() {
360         test_mkdir $DIR/$tdir
361         $MCREATE $DIR/$tdir/$tfile
362         chmod 0666 $DIR/$tdir/$tfile
363         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
364                 error "$tdir/$tfile should be mode 0666"
365 }
366 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
367
368 test_7b() {
369         if [ ! -d $DIR/$tdir ]; then
370                 test_mkdir $DIR/$tdir
371         fi
372         $MCREATE $DIR/$tdir/$tfile
373         echo -n foo > $DIR/$tdir/$tfile
374         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
375         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
376 }
377 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
378
379 test_8() {
380         test_mkdir $DIR/$tdir
381         touch $DIR/$tdir/$tfile
382         chmod 0666 $DIR/$tdir/$tfile
383         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
384                 error "$tfile mode not 0666"
385 }
386 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
387
388 test_9() {
389         test_mkdir $DIR/$tdir
390         test_mkdir $DIR/$tdir/d2
391         test_mkdir $DIR/$tdir/d2/d3
392         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
393 }
394 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
395
396 test_10() {
397         test_mkdir $DIR/$tdir
398         test_mkdir $DIR/$tdir/d2
399         touch $DIR/$tdir/d2/$tfile
400         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
401                 error "$tdir/d2/$tfile not a file"
402 }
403 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
404
405 test_11() {
406         test_mkdir $DIR/$tdir
407         test_mkdir $DIR/$tdir/d2
408         chmod 0666 $DIR/$tdir/d2
409         chmod 0705 $DIR/$tdir/d2
410         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
411                 error "$tdir/d2 mode not 0705"
412 }
413 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
414
415 test_12() {
416         test_mkdir $DIR/$tdir
417         touch $DIR/$tdir/$tfile
418         chmod 0666 $DIR/$tdir/$tfile
419         chmod 0654 $DIR/$tdir/$tfile
420         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
421                 error "$tdir/d2 mode not 0654"
422 }
423 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
424
425 test_13() {
426         test_mkdir $DIR/$tdir
427         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
428         >  $DIR/$tdir/$tfile
429         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
430                 error "$tdir/$tfile size not 0 after truncate"
431 }
432 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
433
434 test_14() {
435         test_mkdir $DIR/$tdir
436         touch $DIR/$tdir/$tfile
437         rm $DIR/$tdir/$tfile
438         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
439 }
440 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
441
442 test_15() {
443         test_mkdir $DIR/$tdir
444         touch $DIR/$tdir/$tfile
445         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
446         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
447                 error "$tdir/${tfile_2} not a file after rename"
448         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
449 }
450 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
451
452 test_16() {
453         test_mkdir $DIR/$tdir
454         touch $DIR/$tdir/$tfile
455         rm -rf $DIR/$tdir/$tfile
456         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
457 }
458 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
459
460 test_17a() {
461         test_mkdir $DIR/$tdir
462         touch $DIR/$tdir/$tfile
463         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
464         ls -l $DIR/$tdir
465         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
466                 error "$tdir/l-exist not a symlink"
467         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
468                 error "$tdir/l-exist not referencing a file"
469         rm -f $DIR/$tdir/l-exist
470         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
471 }
472 run_test 17a "symlinks: create, remove (real)"
473
474 test_17b() {
475         test_mkdir $DIR/$tdir
476         ln -s no-such-file $DIR/$tdir/l-dangle
477         ls -l $DIR/$tdir
478         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
479                 error "$tdir/l-dangle not referencing no-such-file"
480         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
481                 error "$tdir/l-dangle not referencing non-existent file"
482         rm -f $DIR/$tdir/l-dangle
483         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
484 }
485 run_test 17b "symlinks: create, remove (dangling)"
486
487 test_17c() { # bug 3440 - don't save failed open RPC for replay
488         test_mkdir $DIR/$tdir
489         ln -s foo $DIR/$tdir/$tfile
490         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
491 }
492 run_test 17c "symlinks: open dangling (should return error)"
493
494 test_17d() {
495         test_mkdir $DIR/$tdir
496         ln -s foo $DIR/$tdir/$tfile
497         touch $DIR/$tdir/$tfile || error "creating to new symlink"
498 }
499 run_test 17d "symlinks: create dangling"
500
501 test_17e() {
502         test_mkdir $DIR/$tdir
503         local foo=$DIR/$tdir/$tfile
504         ln -s $foo $foo || error "create symlink failed"
505         ls -l $foo || error "ls -l failed"
506         ls $foo && error "ls not failed" || true
507 }
508 run_test 17e "symlinks: create recursive symlink (should return error)"
509
510 test_17f() {
511         test_mkdir $DIR/$tdir
512         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
513         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
514         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
515         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
516         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
517         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
518         ls -l  $DIR/$tdir
519 }
520 run_test 17f "symlinks: long and very long symlink name"
521
522 # str_repeat(S, N) generate a string that is string S repeated N times
523 str_repeat() {
524         local s=$1
525         local n=$2
526         local ret=''
527         while [ $((n -= 1)) -ge 0 ]; do
528                 ret=$ret$s
529         done
530         echo $ret
531 }
532
533 # Long symlinks and LU-2241
534 test_17g() {
535         test_mkdir $DIR/$tdir
536         local TESTS="59 60 61 4094 4095"
537
538         # Fix for inode size boundary in 2.1.4
539         [ $MDS1_VERSION -lt $(version_code 2.1.4) ] &&
540                 TESTS="4094 4095"
541
542         # Patch not applied to 2.2 or 2.3 branches
543         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
544         [ $MDS1_VERSION -le $(version_code 2.3.55) ] &&
545                 TESTS="4094 4095"
546
547         # skip long symlink name for rhel6.5.
548         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
549         grep -q '6.5' /etc/redhat-release &>/dev/null &&
550                 TESTS="59 60 61 4062 4063"
551
552         for i in $TESTS; do
553                 local SYMNAME=$(str_repeat 'x' $i)
554                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
555                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
556         done
557 }
558 run_test 17g "symlinks: really long symlink name and inode boundaries"
559
560 test_17h() { #bug 17378
561         [ $PARALLEL == "yes" ] && skip "skip parallel run"
562         remote_mds_nodsh && skip "remote MDS with nodsh"
563
564         local mdt_idx
565
566         test_mkdir $DIR/$tdir
567         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
568         $LFS setstripe -c -1 $DIR/$tdir
569         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
570         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
571         touch $DIR/$tdir/$tfile || true
572 }
573 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
574
575 test_17i() { #bug 20018
576         [ $PARALLEL == "yes" ] && skip "skip parallel run"
577         remote_mds_nodsh && skip "remote MDS with nodsh"
578
579         local foo=$DIR/$tdir/$tfile
580         local mdt_idx
581
582         test_mkdir -c1 $DIR/$tdir
583         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
584         ln -s $foo $foo || error "create symlink failed"
585 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
586         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
587         ls -l $foo && error "error not detected"
588         return 0
589 }
590 run_test 17i "don't panic on short symlink (should return error)"
591
592 test_17k() { #bug 22301
593         [ $PARALLEL == "yes" ] && skip "skip parallel run"
594         [[ -z "$(which rsync 2>/dev/null)" ]] &&
595                 skip "no rsync command"
596         rsync --help | grep -q xattr ||
597                 skip_env "$(rsync --version | head -n1) does not support xattrs"
598         test_mkdir $DIR/$tdir
599         test_mkdir $DIR/$tdir.new
600         touch $DIR/$tdir/$tfile
601         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
602         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
603                 error "rsync failed with xattrs enabled"
604 }
605 run_test 17k "symlinks: rsync with xattrs enabled"
606
607 test_17l() { # LU-279
608         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
609                 skip "no getfattr command"
610
611         test_mkdir $DIR/$tdir
612         touch $DIR/$tdir/$tfile
613         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
614         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
615                 # -h to not follow symlinks. -m '' to list all the xattrs.
616                 # grep to remove first line: '# file: $path'.
617                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
618                 do
619                         lgetxattr_size_check $path $xattr ||
620                                 error "lgetxattr_size_check $path $xattr failed"
621                 done
622         done
623 }
624 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
625
626 # LU-1540
627 test_17m() {
628         [ $PARALLEL == "yes" ] && skip "skip parallel run"
629         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
630         remote_mds_nodsh && skip "remote MDS with nodsh"
631         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
632         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
633                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
634
635         local short_sym="0123456789"
636         local wdir=$DIR/$tdir
637         local i
638
639         test_mkdir $wdir
640         long_sym=$short_sym
641         # create a long symlink file
642         for ((i = 0; i < 4; ++i)); do
643                 long_sym=${long_sym}${long_sym}
644         done
645
646         echo "create 512 short and long symlink files under $wdir"
647         for ((i = 0; i < 256; ++i)); do
648                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
649                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
650         done
651
652         echo "erase them"
653         rm -f $wdir/*
654         sync
655         wait_delete_completed
656
657         echo "recreate the 512 symlink files with a shorter string"
658         for ((i = 0; i < 512; ++i)); do
659                 # rewrite the symlink file with a shorter string
660                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
661                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
662         done
663
664         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
665         local devname=$(mdsdevname $mds_index)
666
667         echo "stop and checking mds${mds_index}:"
668         # e2fsck should not return error
669         stop mds${mds_index}
670         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
671         rc=$?
672
673         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
674                 error "start mds${mds_index} failed"
675         df $MOUNT > /dev/null 2>&1
676         [ $rc -eq 0 ] ||
677                 error "e2fsck detected error for short/long symlink: rc=$rc"
678         rm -f $wdir/*
679 }
680 run_test 17m "run e2fsck against MDT which contains short/long symlink"
681
682 check_fs_consistency_17n() {
683         local mdt_index
684         local rc=0
685
686         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
687         # so it only check MDT1/MDT2 instead of all of MDTs.
688         for mdt_index in 1 2; do
689                 local devname=$(mdsdevname $mdt_index)
690                 # e2fsck should not return error
691                 stop mds${mdt_index}
692                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
693                         rc=$((rc + $?))
694
695                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
696                         error "mount mds$mdt_index failed"
697                 df $MOUNT > /dev/null 2>&1
698         done
699         return $rc
700 }
701
702 test_17n() {
703         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
704         [ $PARALLEL == "yes" ] && skip "skip parallel run"
705         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
706         remote_mds_nodsh && skip "remote MDS with nodsh"
707         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
708         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
709                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
710
711         local i
712
713         test_mkdir $DIR/$tdir
714         for ((i=0; i<10; i++)); do
715                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
716                         error "create remote dir error $i"
717                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
718                         error "create files under remote dir failed $i"
719         done
720
721         check_fs_consistency_17n ||
722                 error "e2fsck report error after create files under remote dir"
723
724         for ((i = 0; i < 10; i++)); do
725                 rm -rf $DIR/$tdir/remote_dir_${i} ||
726                         error "destroy remote dir error $i"
727         done
728
729         check_fs_consistency_17n ||
730                 error "e2fsck report error after unlink files under remote dir"
731
732         [ $MDS1_VERSION -lt $(version_code 2.4.50) ] &&
733                 skip "lustre < 2.4.50 does not support migrate mv"
734
735         for ((i = 0; i < 10; i++)); do
736                 mkdir -p $DIR/$tdir/remote_dir_${i}
737                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
738                         error "create files under remote dir failed $i"
739                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
740                         error "migrate remote dir error $i"
741         done
742         check_fs_consistency_17n || error "e2fsck report error after migration"
743
744         for ((i = 0; i < 10; i++)); do
745                 rm -rf $DIR/$tdir/remote_dir_${i} ||
746                         error "destroy remote dir error $i"
747         done
748
749         check_fs_consistency_17n || error "e2fsck report error after unlink"
750 }
751 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
752
753 test_17o() {
754         remote_mds_nodsh && skip "remote MDS with nodsh"
755         [ $MDS1_VERSION -lt $(version_code 2.3.64) ] &&
756                 skip "Need MDS version at least 2.3.64"
757
758         local wdir=$DIR/${tdir}o
759         local mdt_index
760         local rc=0
761
762         test_mkdir $wdir
763         touch $wdir/$tfile
764         mdt_index=$($LFS getstripe -m $wdir/$tfile)
765         mdt_index=$((mdt_index + 1))
766
767         cancel_lru_locks mdc
768         #fail mds will wait the failover finish then set
769         #following fail_loc to avoid interfer the recovery process.
770         fail mds${mdt_index}
771
772         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
773         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
774         ls -l $wdir/$tfile && rc=1
775         do_facet mds${mdt_index} lctl set_param fail_loc=0
776         [[ $rc -eq 0 ]] || error "stat file should fail"
777 }
778 run_test 17o "stat file with incompat LMA feature"
779
780 test_18() {
781         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
782         ls $DIR || error "Failed to ls $DIR: $?"
783 }
784 run_test 18 "touch .../f ; ls ... =============================="
785
786 test_19a() {
787         touch $DIR/$tfile
788         ls -l $DIR
789         rm $DIR/$tfile
790         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
791 }
792 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
793
794 test_19b() {
795         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
796 }
797 run_test 19b "ls -l .../f19 (should return error) =============="
798
799 test_19c() {
800         [ $RUNAS_ID -eq $UID ] &&
801                 skip_env "RUNAS_ID = UID = $UID -- skipping"
802
803         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
804 }
805 run_test 19c "$RUNAS touch .../f19 (should return error) =="
806
807 test_19d() {
808         cat $DIR/f19 && error || true
809 }
810 run_test 19d "cat .../f19 (should return error) =============="
811
812 test_20() {
813         touch $DIR/$tfile
814         rm $DIR/$tfile
815         touch $DIR/$tfile
816         rm $DIR/$tfile
817         touch $DIR/$tfile
818         rm $DIR/$tfile
819         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
820 }
821 run_test 20 "touch .../f ; ls -l ..."
822
823 test_21() {
824         test_mkdir $DIR/$tdir
825         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
826         ln -s dangle $DIR/$tdir/link
827         echo foo >> $DIR/$tdir/link
828         cat $DIR/$tdir/dangle
829         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
830         $CHECKSTAT -f -t file $DIR/$tdir/link ||
831                 error "$tdir/link not linked to a file"
832 }
833 run_test 21 "write to dangling link"
834
835 test_22() {
836         local wdir=$DIR/$tdir
837         test_mkdir $wdir
838         chown $RUNAS_ID:$RUNAS_GID $wdir
839         (cd $wdir || error "cd $wdir failed";
840                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
841                 $RUNAS tar xf -)
842         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
843         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
844         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
845                 error "checkstat -u failed"
846 }
847 run_test 22 "unpack tar archive as non-root user"
848
849 # was test_23
850 test_23a() {
851         test_mkdir $DIR/$tdir
852         local file=$DIR/$tdir/$tfile
853
854         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
855         openfile -f O_CREAT:O_EXCL $file &&
856                 error "$file recreate succeeded" || true
857 }
858 run_test 23a "O_CREAT|O_EXCL in subdir"
859
860 test_23b() { # bug 18988
861         test_mkdir $DIR/$tdir
862         local file=$DIR/$tdir/$tfile
863
864         rm -f $file
865         echo foo > $file || error "write filed"
866         echo bar >> $file || error "append filed"
867         $CHECKSTAT -s 8 $file || error "wrong size"
868         rm $file
869 }
870 run_test 23b "O_APPEND check"
871
872 # LU-9409, size with O_APPEND and tiny writes
873 test_23c() {
874         local file=$DIR/$tfile
875
876         # single dd
877         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
878         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
879         rm -f $file
880
881         # racing tiny writes
882         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
883         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
884         wait
885         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
886         rm -f $file
887
888         #racing tiny & normal writes
889         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
890         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
891         wait
892         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
893         rm -f $file
894
895         #racing tiny & normal writes 2, ugly numbers
896         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
897         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
898         wait
899         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
900         rm -f $file
901 }
902 run_test 23c "O_APPEND size checks for tiny writes"
903
904 # LU-11069 file offset is correct after appending writes
905 test_23d() {
906         local file=$DIR/$tfile
907         local offset
908
909         echo CentaurHauls > $file
910         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
911         if ((offset != 26)); then
912                 error "wrong offset, expected 26, got '$offset'"
913         fi
914 }
915 run_test 23d "file offset is correct after appending writes"
916
917 # rename sanity
918 test_24a() {
919         echo '-- same directory rename'
920         test_mkdir $DIR/$tdir
921         touch $DIR/$tdir/$tfile.1
922         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
923         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
924 }
925 run_test 24a "rename file to non-existent target"
926
927 test_24b() {
928         test_mkdir $DIR/$tdir
929         touch $DIR/$tdir/$tfile.{1,2}
930         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
931         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
932         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
933 }
934 run_test 24b "rename file to existing target"
935
936 test_24c() {
937         test_mkdir $DIR/$tdir
938         test_mkdir $DIR/$tdir/d$testnum.1
939         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
940         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
941         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
942 }
943 run_test 24c "rename directory to non-existent target"
944
945 test_24d() {
946         test_mkdir -c1 $DIR/$tdir
947         test_mkdir -c1 $DIR/$tdir/d$testnum.1
948         test_mkdir -c1 $DIR/$tdir/d$testnum.2
949         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
950         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
951         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
952 }
953 run_test 24d "rename directory to existing target"
954
955 test_24e() {
956         echo '-- cross directory renames --'
957         test_mkdir $DIR/R5a
958         test_mkdir $DIR/R5b
959         touch $DIR/R5a/f
960         mv $DIR/R5a/f $DIR/R5b/g
961         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
962         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
963 }
964 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
965
966 test_24f() {
967         test_mkdir $DIR/R6a
968         test_mkdir $DIR/R6b
969         touch $DIR/R6a/f $DIR/R6b/g
970         mv $DIR/R6a/f $DIR/R6b/g
971         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
972         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
973 }
974 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
975
976 test_24g() {
977         test_mkdir $DIR/R7a
978         test_mkdir $DIR/R7b
979         test_mkdir $DIR/R7a/d
980         mv $DIR/R7a/d $DIR/R7b/e
981         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
982         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
983 }
984 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
985
986 test_24h() {
987         test_mkdir -c1 $DIR/R8a
988         test_mkdir -c1 $DIR/R8b
989         test_mkdir -c1 $DIR/R8a/d
990         test_mkdir -c1 $DIR/R8b/e
991         mrename $DIR/R8a/d $DIR/R8b/e
992         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
993         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
994 }
995 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
996
997 test_24i() {
998         echo "-- rename error cases"
999         test_mkdir $DIR/R9
1000         test_mkdir $DIR/R9/a
1001         touch $DIR/R9/f
1002         mrename $DIR/R9/f $DIR/R9/a
1003         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1004         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1005         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1006 }
1007 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1008
1009 test_24j() {
1010         test_mkdir $DIR/R10
1011         mrename $DIR/R10/f $DIR/R10/g
1012         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1013         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1014         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1015 }
1016 run_test 24j "source does not exist ============================"
1017
1018 test_24k() {
1019         test_mkdir $DIR/R11a
1020         test_mkdir $DIR/R11a/d
1021         touch $DIR/R11a/f
1022         mv $DIR/R11a/f $DIR/R11a/d
1023         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1024         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1025 }
1026 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1027
1028 # bug 2429 - rename foo foo foo creates invalid file
1029 test_24l() {
1030         f="$DIR/f24l"
1031         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1032 }
1033 run_test 24l "Renaming a file to itself ========================"
1034
1035 test_24m() {
1036         f="$DIR/f24m"
1037         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1038         # on ext3 this does not remove either the source or target files
1039         # though the "expected" operation would be to remove the source
1040         $CHECKSTAT -t file ${f} || error "${f} missing"
1041         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1042 }
1043 run_test 24m "Renaming a file to a hard link to itself ========="
1044
1045 test_24n() {
1046     f="$DIR/f24n"
1047     # this stats the old file after it was renamed, so it should fail
1048     touch ${f}
1049     $CHECKSTAT ${f} || error "${f} missing"
1050     mv ${f} ${f}.rename
1051     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1052     $CHECKSTAT -a ${f} || error "${f} exists"
1053 }
1054 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1055
1056 test_24o() {
1057         test_mkdir $DIR/$tdir
1058         rename_many -s random -v -n 10 $DIR/$tdir
1059 }
1060 run_test 24o "rename of files during htree split"
1061
1062 test_24p() {
1063         test_mkdir $DIR/R12a
1064         test_mkdir $DIR/R12b
1065         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1066         mrename $DIR/R12a $DIR/R12b
1067         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1068         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1069         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1070         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1071 }
1072 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1073
1074 cleanup_multiop_pause() {
1075         trap 0
1076         kill -USR1 $MULTIPID
1077 }
1078
1079 test_24q() {
1080         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1081
1082         test_mkdir $DIR/R13a
1083         test_mkdir $DIR/R13b
1084         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1085         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1086         MULTIPID=$!
1087
1088         trap cleanup_multiop_pause EXIT
1089         mrename $DIR/R13a $DIR/R13b
1090         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1091         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1092         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1093         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1094         cleanup_multiop_pause
1095         wait $MULTIPID || error "multiop close failed"
1096 }
1097 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1098
1099 test_24r() { #bug 3789
1100         test_mkdir $DIR/R14a
1101         test_mkdir $DIR/R14a/b
1102         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1103         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1104         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1105 }
1106 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1107
1108 test_24s() {
1109         test_mkdir $DIR/R15a
1110         test_mkdir $DIR/R15a/b
1111         test_mkdir $DIR/R15a/b/c
1112         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1113         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1114         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1115 }
1116 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1117 test_24t() {
1118         test_mkdir $DIR/R16a
1119         test_mkdir $DIR/R16a/b
1120         test_mkdir $DIR/R16a/b/c
1121         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1122         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1123         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1124 }
1125 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1126
1127 test_24u() { # bug12192
1128         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1129         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1130 }
1131 run_test 24u "create stripe file"
1132
1133 simple_cleanup_common() {
1134         local rc=0
1135         trap 0
1136         [ -z "$DIR" ] || [ -z "$tdir" ] && return 0
1137
1138         local start=$SECONDS
1139         rm -rf $DIR/$tdir
1140         rc=$?
1141         wait_delete_completed
1142         echo "cleanup time $((SECONDS - start))"
1143         return $rc
1144 }
1145
1146 max_pages_per_rpc() {
1147         local mdtname="$(printf "MDT%04x" ${1:-0})"
1148         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1149 }
1150
1151 test_24v() {
1152         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1153
1154         local nrfiles=${COUNT:-100000}
1155         local fname="$DIR/$tdir/$tfile"
1156
1157         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1158         [ "$mds1_FSTYPE" = "zfs" ] && nrfiles=${COUNT:-10000}
1159
1160         test_mkdir "$(dirname $fname)"
1161         # assume MDT0000 has the fewest inodes
1162         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1163         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1164         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1165
1166         trap simple_cleanup_common EXIT
1167
1168         createmany -m "$fname" $nrfiles
1169
1170         cancel_lru_locks mdc
1171         lctl set_param mdc.*.stats clear
1172
1173         # was previously test_24D: LU-6101
1174         # readdir() returns correct number of entries after cursor reload
1175         local num_ls=$(ls $DIR/$tdir | wc -l)
1176         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1177         local num_all=$(ls -a $DIR/$tdir | wc -l)
1178         if [ $num_ls -ne $nrfiles ] || [ $num_uniq -ne $nrfiles ] ||
1179                 [ $num_all -ne $((nrfiles + 2)) ]; then
1180                         error "Expected $nrfiles files, got $num_ls " \
1181                                 "($num_uniq unique $num_all .&..)"
1182         fi
1183         # LU-5 large readdir
1184         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1185         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1186         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1187         # take into account of overhead in lu_dirpage header and end mark in
1188         # each page, plus one in rpc_num calculation.
1189         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1190         local page_entries=$(((PAGE_SIZE - 24) / dirent_size))
1191         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1192         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1193         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1194         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1195         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1196         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1197                 error "large readdir doesn't take effect: " \
1198                       "$mds_readpage should be about $rpc_max"
1199
1200         simple_cleanup_common
1201 }
1202 run_test 24v "list large directory (test hash collision, b=17560)"
1203
1204 test_24w() { # bug21506
1205         SZ1=234852
1206         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1207         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1208         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1209         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1210         [[ "$SZ1" -eq "$SZ2" ]] ||
1211                 error "Error reading at the end of the file $tfile"
1212 }
1213 run_test 24w "Reading a file larger than 4Gb"
1214
1215 test_24x() {
1216         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1217         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1218         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1219                 skip "Need MDS version at least 2.7.56"
1220
1221         local MDTIDX=1
1222         local remote_dir=$DIR/$tdir/remote_dir
1223
1224         test_mkdir $DIR/$tdir
1225         $LFS mkdir -i $MDTIDX $remote_dir ||
1226                 error "create remote directory failed"
1227
1228         test_mkdir $DIR/$tdir/src_dir
1229         touch $DIR/$tdir/src_file
1230         test_mkdir $remote_dir/tgt_dir
1231         touch $remote_dir/tgt_file
1232
1233         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1234                 error "rename dir cross MDT failed!"
1235
1236         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1237                 error "rename file cross MDT failed!"
1238
1239         touch $DIR/$tdir/ln_file
1240         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1241                 error "ln file cross MDT failed"
1242
1243         rm -rf $DIR/$tdir || error "Can not delete directories"
1244 }
1245 run_test 24x "cross MDT rename/link"
1246
1247 test_24y() {
1248         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1249         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1250
1251         local remote_dir=$DIR/$tdir/remote_dir
1252         local mdtidx=1
1253
1254         test_mkdir $DIR/$tdir
1255         $LFS mkdir -i $mdtidx $remote_dir ||
1256                 error "create remote directory failed"
1257
1258         test_mkdir $remote_dir/src_dir
1259         touch $remote_dir/src_file
1260         test_mkdir $remote_dir/tgt_dir
1261         touch $remote_dir/tgt_file
1262
1263         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1264                 error "rename subdir in the same remote dir failed!"
1265
1266         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1267                 error "rename files in the same remote dir failed!"
1268
1269         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1270                 error "link files in the same remote dir failed!"
1271
1272         rm -rf $DIR/$tdir || error "Can not delete directories"
1273 }
1274 run_test 24y "rename/link on the same dir should succeed"
1275
1276 test_24z() {
1277         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1278         [[ $MDS1_VERSION -lt $(version_code 2.12.51) ]] &&
1279                 skip "Need MDS version at least 2.12.51"
1280
1281         local index
1282
1283         for index in 0 1; do
1284                 $LFS mkdir -i $index $DIR/$tdir.$index || error "mkdir failed"
1285                 touch $DIR/$tdir.0/$tfile.$index || error "touch failed"
1286         done
1287
1288         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1 || error "mv $tfile.0 failed"
1289
1290         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.0)
1291         [ $index -eq 0 ] || error "$tfile.0 is on MDT$index"
1292
1293         local mdts=$(comma_list $(mdts_nodes))
1294
1295         do_nodes $mdts $LCTL set_param mdt.*.enable_remote_rename=0
1296         stack_trap "do_nodes $mdts $LCTL \
1297                 set_param mdt.*.enable_remote_rename=1" EXIT
1298
1299         mv $DIR/$tdir.0/$tfile.1 $DIR/$tdir.1 || error "mv $tfile.1 failed"
1300
1301         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.1)
1302         [ $index -eq 1 ] || error "$tfile.1 is on MDT$index"
1303 }
1304 run_test 24z "cross-MDT rename is done as cp"
1305
1306 test_24A() { # LU-3182
1307         local NFILES=5000
1308
1309         rm -rf $DIR/$tdir
1310         test_mkdir $DIR/$tdir
1311         trap simple_cleanup_common EXIT
1312         createmany -m $DIR/$tdir/$tfile $NFILES
1313         local t=$(ls $DIR/$tdir | wc -l)
1314         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1315         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1316         if [ $t -ne $NFILES ] || [ $u -ne $NFILES ] ||
1317            [ $v -ne $((NFILES + 2)) ] ; then
1318                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1319         fi
1320
1321         simple_cleanup_common || error "Can not delete directories"
1322 }
1323 run_test 24A "readdir() returns correct number of entries."
1324
1325 test_24B() { # LU-4805
1326         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1327
1328         local count
1329
1330         test_mkdir $DIR/$tdir
1331         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1332                 error "create striped dir failed"
1333
1334         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1335         [ $count -eq 2 ] || error "Expected 2, got $count"
1336
1337         touch $DIR/$tdir/striped_dir/a
1338
1339         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1340         [ $count -eq 3 ] || error "Expected 3, got $count"
1341
1342         touch $DIR/$tdir/striped_dir/.f
1343
1344         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1345         [ $count -eq 4 ] || error "Expected 4, got $count"
1346
1347         rm -rf $DIR/$tdir || error "Can not delete directories"
1348 }
1349 run_test 24B "readdir for striped dir return correct number of entries"
1350
1351 test_24C() {
1352         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1353
1354         mkdir $DIR/$tdir
1355         mkdir $DIR/$tdir/d0
1356         mkdir $DIR/$tdir/d1
1357
1358         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1359                 error "create striped dir failed"
1360
1361         cd $DIR/$tdir/d0/striped_dir
1362
1363         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1364         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1365         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1366
1367         [ "$d0_ino" = "$parent_ino" ] ||
1368                 error ".. wrong, expect $d0_ino, get $parent_ino"
1369
1370         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1371                 error "mv striped dir failed"
1372
1373         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1374
1375         [ "$d1_ino" = "$parent_ino" ] ||
1376                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1377 }
1378 run_test 24C "check .. in striped dir"
1379
1380 test_24E() {
1381         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1382         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1383
1384         mkdir -p $DIR/$tdir
1385         mkdir $DIR/$tdir/src_dir
1386         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1387                 error "create remote source failed"
1388
1389         touch $DIR/$tdir/src_dir/src_child/a
1390
1391         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1392                 error "create remote target dir failed"
1393
1394         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1395                 error "create remote target child failed"
1396
1397         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1398                 error "rename dir cross MDT failed!"
1399
1400         find $DIR/$tdir
1401
1402         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1403                 error "src_child still exists after rename"
1404
1405         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1406                 error "missing file(a) after rename"
1407
1408         rm -rf $DIR/$tdir || error "Can not delete directories"
1409 }
1410 run_test 24E "cross MDT rename/link"
1411
1412 test_24F () {
1413         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
1414
1415         local repeats=1000
1416         [ "$SLOW" = "no" ] && repeats=100
1417
1418         mkdir -p $DIR/$tdir
1419
1420         echo "$repeats repeats"
1421         for ((i = 0; i < repeats; i++)); do
1422                 $LFS mkdir -i0 -c2 $DIR/$tdir/test || error "mkdir fails"
1423                 touch $DIR/$tdir/test/a || error "touch fails"
1424                 mkdir $DIR/$tdir/test/b || error "mkdir fails"
1425                 rm -rf $DIR/$tdir/test || error "rmdir fails"
1426         done
1427
1428         true
1429 }
1430 run_test 24F "hash order vs readdir (LU-11330)"
1431
1432 test_25a() {
1433         echo '== symlink sanity ============================================='
1434
1435         test_mkdir $DIR/d25
1436         ln -s d25 $DIR/s25
1437         touch $DIR/s25/foo ||
1438                 error "File creation in symlinked directory failed"
1439 }
1440 run_test 25a "create file in symlinked directory ==============="
1441
1442 test_25b() {
1443         [ ! -d $DIR/d25 ] && test_25a
1444         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1445 }
1446 run_test 25b "lookup file in symlinked directory ==============="
1447
1448 test_26a() {
1449         test_mkdir $DIR/d26
1450         test_mkdir $DIR/d26/d26-2
1451         ln -s d26/d26-2 $DIR/s26
1452         touch $DIR/s26/foo || error "File creation failed"
1453 }
1454 run_test 26a "multiple component symlink ======================="
1455
1456 test_26b() {
1457         test_mkdir -p $DIR/$tdir/d26-2
1458         ln -s $tdir/d26-2/foo $DIR/s26-2
1459         touch $DIR/s26-2 || error "File creation failed"
1460 }
1461 run_test 26b "multiple component symlink at end of lookup ======"
1462
1463 test_26c() {
1464         test_mkdir $DIR/d26.2
1465         touch $DIR/d26.2/foo
1466         ln -s d26.2 $DIR/s26.2-1
1467         ln -s s26.2-1 $DIR/s26.2-2
1468         ln -s s26.2-2 $DIR/s26.2-3
1469         chmod 0666 $DIR/s26.2-3/foo
1470 }
1471 run_test 26c "chain of symlinks"
1472
1473 # recursive symlinks (bug 439)
1474 test_26d() {
1475         ln -s d26-3/foo $DIR/d26-3
1476 }
1477 run_test 26d "create multiple component recursive symlink"
1478
1479 test_26e() {
1480         [ ! -h $DIR/d26-3 ] && test_26d
1481         rm $DIR/d26-3
1482 }
1483 run_test 26e "unlink multiple component recursive symlink"
1484
1485 # recursive symlinks (bug 7022)
1486 test_26f() {
1487         test_mkdir $DIR/$tdir
1488         test_mkdir $DIR/$tdir/$tfile
1489         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1490         test_mkdir -p lndir/bar1
1491         test_mkdir $DIR/$tdir/$tfile/$tfile
1492         cd $tfile                || error "cd $tfile failed"
1493         ln -s .. dotdot          || error "ln dotdot failed"
1494         ln -s dotdot/lndir lndir || error "ln lndir failed"
1495         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1496         output=`ls $tfile/$tfile/lndir/bar1`
1497         [ "$output" = bar1 ] && error "unexpected output"
1498         rm -r $tfile             || error "rm $tfile failed"
1499         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1500 }
1501 run_test 26f "rm -r of a directory which has recursive symlink"
1502
1503 test_27a() {
1504         test_mkdir $DIR/$tdir
1505         $LFS getstripe $DIR/$tdir
1506         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1507         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1508         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1509 }
1510 run_test 27a "one stripe file"
1511
1512 test_27b() {
1513         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1514
1515         test_mkdir $DIR/$tdir
1516         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1517         $LFS getstripe -c $DIR/$tdir/$tfile
1518         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1519                 error "two-stripe file doesn't have two stripes"
1520
1521         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1522 }
1523 run_test 27b "create and write to two stripe file"
1524
1525 # 27c family tests specific striping, setstripe -o
1526 test_27ca() {
1527         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1528         test_mkdir -p $DIR/$tdir
1529         local osts="1"
1530
1531         $LFS setstripe -o $osts $DIR/$tdir/$tfile  || error "setstripe failed"
1532         $LFS getstripe -i $DIR/$tdir/$tfile
1533         [ $($LFS getstripe -i $DIR/$tdir/$tfile ) -eq $osts ] ||
1534                 error "stripe not on specified OST"
1535
1536         dd if=/dev/zero of=$DIR/$tdir/$tfile  bs=1M count=4 || error "dd failed"
1537 }
1538 run_test 27ca "one stripe on specified OST"
1539
1540 test_27cb() {
1541         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1542         test_mkdir -p $DIR/$tdir
1543         local osts="1,0"
1544         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1545         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1546         echo "$getstripe"
1547
1548         # Strip getstripe output to a space separated list of OSTs
1549         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1550                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1551         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1552                 error "stripes not on specified OSTs"
1553
1554         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1555 }
1556 run_test 27cb "two stripes on specified OSTs"
1557
1558 test_27cc() {
1559         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1560         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1561                 skip "server does not support overstriping"
1562
1563         test_mkdir -p $DIR/$tdir
1564         local osts="0,0"
1565         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1566         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1567         echo "$getstripe"
1568
1569         # Strip getstripe output to a space separated list of OSTs
1570         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1571                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1572         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1573                 error "stripes not on specified OSTs"
1574
1575         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1576 }
1577 run_test 27cc "two stripes on the same OST"
1578
1579 test_27cd() {
1580         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1581         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1582                 skip "server does not support overstriping"
1583         test_mkdir -p $DIR/$tdir
1584         local osts="0,1,1,0"
1585         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1586         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1587         echo "$getstripe"
1588
1589         # Strip getstripe output to a space separated list of OSTs
1590         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1591                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1592         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1593                 error "stripes not on specified OSTs"
1594
1595         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1596 }
1597 run_test 27cd "four stripes on two OSTs"
1598
1599 test_27ce() {
1600         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
1601                 skip_env "too many osts, skipping"
1602         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1603                 skip "server does not support overstriping"
1604         # We do one more stripe than we have OSTs
1605         [ $OSTCOUNT -ge 159 ] || large_xattr_enabled ||
1606                 skip_env "ea_inode feature disabled"
1607
1608         test_mkdir -p $DIR/$tdir
1609         local osts=""
1610         for i in $(seq 0 $OSTCOUNT);
1611         do
1612                 osts=$osts"0"
1613                 if [ $i -ne $OSTCOUNT ]; then
1614                         osts=$osts","
1615                 fi
1616         done
1617         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1618         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1619         echo "$getstripe"
1620
1621         # Strip getstripe output to a space separated list of OSTs
1622         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1623                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1624         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1625                 error "stripes not on specified OSTs"
1626
1627         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1628 }
1629 run_test 27ce "more stripes than OSTs with -o"
1630
1631 test_27d() {
1632         test_mkdir $DIR/$tdir
1633         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1634                 error "setstripe failed"
1635         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1636         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1637 }
1638 run_test 27d "create file with default settings"
1639
1640 test_27e() {
1641         # LU-5839 adds check for existed layout before setting it
1642         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1643                 skip "Need MDS version at least 2.7.56"
1644
1645         test_mkdir $DIR/$tdir
1646         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1647         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1648         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1649 }
1650 run_test 27e "setstripe existing file (should return error)"
1651
1652 test_27f() {
1653         test_mkdir $DIR/$tdir
1654         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1655                 error "$LFS setstripe $DIR/$tdir/$tfile failed"
1656         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1657                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1658         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1659         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1660 }
1661 run_test 27f "setstripe with bad stripe size (should return error)"
1662
1663 test_27g() {
1664         test_mkdir $DIR/$tdir
1665         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1666         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1667                 error "$DIR/$tdir/$tfile has object"
1668 }
1669 run_test 27g "$LFS getstripe with no objects"
1670
1671 test_27ga() {
1672         test_mkdir $DIR/$tdir
1673         touch $DIR/$tdir/$tfile || error "touch failed"
1674         ln -s bogus $DIR/$tdir/$tfile.2 || error "ln failed"
1675         $LFS getstripe -m $DIR/$tdir/$tfile $DIR/$tdir/$tfile.2
1676         local rc=$?
1677         (( rc == 2 )) || error "getstripe did not return ENOENT"
1678 }
1679 run_test 27ga "$LFS getstripe with missing file (should return error)"
1680
1681 test_27i() {
1682         test_mkdir $DIR/$tdir
1683         touch $DIR/$tdir/$tfile || error "touch failed"
1684         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1685                 error "missing objects"
1686 }
1687 run_test 27i "$LFS getstripe with some objects"
1688
1689 test_27j() {
1690         test_mkdir $DIR/$tdir
1691         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1692                 error "setstripe failed" || true
1693 }
1694 run_test 27j "setstripe with bad stripe offset (should return error)"
1695
1696 test_27k() { # bug 2844
1697         test_mkdir $DIR/$tdir
1698         local file=$DIR/$tdir/$tfile
1699         local ll_max_blksize=$((4 * 1024 * 1024))
1700         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1701         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1702         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1703         dd if=/dev/zero of=$file bs=4k count=1
1704         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1705         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1706 }
1707 run_test 27k "limit i_blksize for broken user apps"
1708
1709 test_27l() {
1710         mcreate $DIR/$tfile || error "creating file"
1711         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1712                 error "setstripe should have failed" || true
1713 }
1714 run_test 27l "check setstripe permissions (should return error)"
1715
1716 test_27m() {
1717         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1718
1719         [ -n "$RCLIENTS" -o -n "$MOUNT_2" ] &&
1720                 skip_env "multiple clients -- skipping"
1721
1722         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1723                    head -n1)
1724         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1725                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1726         fi
1727         trap simple_cleanup_common EXIT
1728         test_mkdir $DIR/$tdir
1729         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1730         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1731                 error "dd should fill OST0"
1732         i=2
1733         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1734                 i=$((i + 1))
1735                 [ $i -gt 256 ] && break
1736         done
1737         i=$((i + 1))
1738         touch $DIR/$tdir/$tfile.$i
1739         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1740             awk '{print $1}'| grep -w "0") ] &&
1741                 error "OST0 was full but new created file still use it"
1742         i=$((i + 1))
1743         touch $DIR/$tdir/$tfile.$i
1744         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1745             awk '{print $1}'| grep -w "0") ] &&
1746                 error "OST0 was full but new created file still use it"
1747         simple_cleanup_common
1748 }
1749 run_test 27m "create file while OST0 was full"
1750
1751 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1752 # if the OST isn't full anymore.
1753 reset_enospc() {
1754         local OSTIDX=${1:-""}
1755
1756         local list=$(comma_list $(osts_nodes))
1757         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1758
1759         do_nodes $list lctl set_param fail_loc=0
1760         sync    # initiate all OST_DESTROYs from MDS to OST
1761         sleep_maxage
1762 }
1763
1764 exhaust_precreations() {
1765         local OSTIDX=$1
1766         local FAILLOC=$2
1767         local FAILIDX=${3:-$OSTIDX}
1768         local ofacet=ost$((OSTIDX + 1))
1769
1770         test_mkdir -p -c1 $DIR/$tdir
1771         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1772         local mfacet=mds$((mdtidx + 1))
1773         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1774
1775         local OST=$(ostname_from_index $OSTIDX)
1776
1777         # on the mdt's osc
1778         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1779         local last_id=$(do_facet $mfacet lctl get_param -n \
1780                         osp.$mdtosc_proc1.prealloc_last_id)
1781         local next_id=$(do_facet $mfacet lctl get_param -n \
1782                         osp.$mdtosc_proc1.prealloc_next_id)
1783
1784         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1785         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1786
1787         test_mkdir -p $DIR/$tdir/${OST}
1788         $LFS setstripe -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1789 #define OBD_FAIL_OST_ENOSPC              0x215
1790         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1791         echo "Creating to objid $last_id on ost $OST..."
1792         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1793         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1794         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1795         sleep_maxage
1796 }
1797
1798 exhaust_all_precreations() {
1799         local i
1800         for (( i=0; i < OSTCOUNT; i++ )) ; do
1801                 exhaust_precreations $i $1 -1
1802         done
1803 }
1804
1805 test_27n() {
1806         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1807         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1808         remote_mds_nodsh && skip "remote MDS with nodsh"
1809         remote_ost_nodsh && skip "remote OST with nodsh"
1810
1811         reset_enospc
1812         rm -f $DIR/$tdir/$tfile
1813         exhaust_precreations 0 0x80000215
1814         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1815         touch $DIR/$tdir/$tfile || error "touch failed"
1816         $LFS getstripe $DIR/$tdir/$tfile
1817         reset_enospc
1818 }
1819 run_test 27n "create file with some full OSTs"
1820
1821 test_27o() {
1822         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1823         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1824         remote_mds_nodsh && skip "remote MDS with nodsh"
1825         remote_ost_nodsh && skip "remote OST with nodsh"
1826
1827         reset_enospc
1828         rm -f $DIR/$tdir/$tfile
1829         exhaust_all_precreations 0x215
1830
1831         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1832
1833         reset_enospc
1834         rm -rf $DIR/$tdir/*
1835 }
1836 run_test 27o "create file with all full OSTs (should error)"
1837
1838 test_27p() {
1839         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1840         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1841         remote_mds_nodsh && skip "remote MDS with nodsh"
1842         remote_ost_nodsh && skip "remote OST with nodsh"
1843
1844         reset_enospc
1845         rm -f $DIR/$tdir/$tfile
1846         test_mkdir $DIR/$tdir
1847
1848         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1849         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1850         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1851
1852         exhaust_precreations 0 0x80000215
1853         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1854         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1855         $LFS getstripe $DIR/$tdir/$tfile
1856
1857         reset_enospc
1858 }
1859 run_test 27p "append to a truncated file with some full OSTs"
1860
1861 test_27q() {
1862         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1863         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1864         remote_mds_nodsh && skip "remote MDS with nodsh"
1865         remote_ost_nodsh && skip "remote OST with nodsh"
1866
1867         reset_enospc
1868         rm -f $DIR/$tdir/$tfile
1869
1870         test_mkdir $DIR/$tdir
1871         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1872         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1873                 error "truncate $DIR/$tdir/$tfile failed"
1874         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1875
1876         exhaust_all_precreations 0x215
1877
1878         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1879         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1880
1881         reset_enospc
1882 }
1883 run_test 27q "append to truncated file with all OSTs full (should error)"
1884
1885 test_27r() {
1886         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1887         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1888         remote_mds_nodsh && skip "remote MDS with nodsh"
1889         remote_ost_nodsh && skip "remote OST with nodsh"
1890
1891         reset_enospc
1892         rm -f $DIR/$tdir/$tfile
1893         exhaust_precreations 0 0x80000215
1894
1895         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1896
1897         reset_enospc
1898 }
1899 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1900
1901 test_27s() { # bug 10725
1902         test_mkdir $DIR/$tdir
1903         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1904         local stripe_count=0
1905         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1906         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1907                 error "stripe width >= 2^32 succeeded" || true
1908
1909 }
1910 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1911
1912 test_27t() { # bug 10864
1913         WDIR=$(pwd)
1914         WLFS=$(which lfs)
1915         cd $DIR
1916         touch $tfile
1917         $WLFS getstripe $tfile
1918         cd $WDIR
1919 }
1920 run_test 27t "check that utils parse path correctly"
1921
1922 test_27u() { # bug 4900
1923         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1924         remote_mds_nodsh && skip "remote MDS with nodsh"
1925
1926         local index
1927         local list=$(comma_list $(mdts_nodes))
1928
1929 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1930         do_nodes $list $LCTL set_param fail_loc=0x139
1931         test_mkdir -p $DIR/$tdir
1932         trap simple_cleanup_common EXIT
1933         createmany -o $DIR/$tdir/t- 1000
1934         do_nodes $list $LCTL set_param fail_loc=0
1935
1936         TLOG=$TMP/$tfile.getstripe
1937         $LFS getstripe $DIR/$tdir > $TLOG
1938         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1939         unlinkmany $DIR/$tdir/t- 1000
1940         trap 0
1941         [[ $OBJS -gt 0 ]] &&
1942                 error "$OBJS objects created on OST-0. See $TLOG" ||
1943                 rm -f $TLOG
1944 }
1945 run_test 27u "skip object creation on OSC w/o objects"
1946
1947 test_27v() { # bug 4900
1948         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1949         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1950         remote_mds_nodsh && skip "remote MDS with nodsh"
1951         remote_ost_nodsh && skip "remote OST with nodsh"
1952
1953         exhaust_all_precreations 0x215
1954         reset_enospc
1955
1956         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1957
1958         touch $DIR/$tdir/$tfile
1959         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1960         # all except ost1
1961         for (( i=1; i < OSTCOUNT; i++ )); do
1962                 do_facet ost$i lctl set_param fail_loc=0x705
1963         done
1964         local START=`date +%s`
1965         createmany -o $DIR/$tdir/$tfile 32
1966
1967         local FINISH=`date +%s`
1968         local TIMEOUT=`lctl get_param -n timeout`
1969         local PROCESS=$((FINISH - START))
1970         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1971                error "$FINISH - $START >= $TIMEOUT / 2"
1972         sleep $((TIMEOUT / 2 - PROCESS))
1973         reset_enospc
1974 }
1975 run_test 27v "skip object creation on slow OST"
1976
1977 test_27w() { # bug 10997
1978         test_mkdir $DIR/$tdir
1979         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1980         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1981                 error "stripe size $size != 65536" || true
1982         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1983                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1984 }
1985 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1986
1987 test_27wa() {
1988         [[ $OSTCOUNT -lt 2 ]] &&
1989                 skip_env "skipping multiple stripe count/offset test"
1990
1991         test_mkdir $DIR/$tdir
1992         for i in $(seq 1 $OSTCOUNT); do
1993                 offset=$((i - 1))
1994                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
1995                         error "setstripe -c $i -i $offset failed"
1996                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
1997                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
1998                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1999                 [ $index -ne $offset ] &&
2000                         error "stripe offset $index != $offset" || true
2001         done
2002 }
2003 run_test 27wa "check $LFS setstripe -c -i options"
2004
2005 test_27x() {
2006         remote_ost_nodsh && skip "remote OST with nodsh"
2007         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2008         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2009
2010         OFFSET=$(($OSTCOUNT - 1))
2011         OSTIDX=0
2012         local OST=$(ostname_from_index $OSTIDX)
2013
2014         test_mkdir $DIR/$tdir
2015         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
2016         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
2017         sleep_maxage
2018         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
2019         for i in $(seq 0 $OFFSET); do
2020                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
2021                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
2022                 error "OST0 was degraded but new created file still use it"
2023         done
2024         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
2025 }
2026 run_test 27x "create files while OST0 is degraded"
2027
2028 test_27y() {
2029         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2030         remote_mds_nodsh && skip "remote MDS with nodsh"
2031         remote_ost_nodsh && skip "remote OST with nodsh"
2032         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2033
2034         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
2035         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
2036                 osp.$mdtosc.prealloc_last_id)
2037         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
2038                 osp.$mdtosc.prealloc_next_id)
2039         local fcount=$((last_id - next_id))
2040         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
2041         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
2042
2043         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
2044                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
2045         local OST_DEACTIVE_IDX=-1
2046         local OSC
2047         local OSTIDX
2048         local OST
2049
2050         for OSC in $MDS_OSCS; do
2051                 OST=$(osc_to_ost $OSC)
2052                 OSTIDX=$(index_from_ostuuid $OST)
2053                 if [ $OST_DEACTIVE_IDX == -1 ]; then
2054                         OST_DEACTIVE_IDX=$OSTIDX
2055                 fi
2056                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
2057                         echo $OSC "is Deactivated:"
2058                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
2059                 fi
2060         done
2061
2062         OSTIDX=$(index_from_ostuuid $OST)
2063         test_mkdir $DIR/$tdir
2064         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
2065
2066         for OSC in $MDS_OSCS; do
2067                 OST=$(osc_to_ost $OSC)
2068                 OSTIDX=$(index_from_ostuuid $OST)
2069                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2070                         echo $OST "is degraded:"
2071                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2072                                                 obdfilter.$OST.degraded=1
2073                 fi
2074         done
2075
2076         sleep_maxage
2077         createmany -o $DIR/$tdir/$tfile $fcount
2078
2079         for OSC in $MDS_OSCS; do
2080                 OST=$(osc_to_ost $OSC)
2081                 OSTIDX=$(index_from_ostuuid $OST)
2082                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2083                         echo $OST "is recovered from degraded:"
2084                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2085                                                 obdfilter.$OST.degraded=0
2086                 else
2087                         do_facet $SINGLEMDS lctl --device %$OSC activate
2088                 fi
2089         done
2090
2091         # all osp devices get activated, hence -1 stripe count restored
2092         local stripe_count=0
2093
2094         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
2095         # devices get activated.
2096         sleep_maxage
2097         $LFS setstripe -c -1 $DIR/$tfile
2098         stripe_count=$($LFS getstripe -c $DIR/$tfile)
2099         rm -f $DIR/$tfile
2100         [ $stripe_count -ne $OSTCOUNT ] &&
2101                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
2102         return 0
2103 }
2104 run_test 27y "create files while OST0 is degraded and the rest inactive"
2105
2106 check_seq_oid()
2107 {
2108         log "check file $1"
2109
2110         lmm_count=$($LFS getstripe -c $1)
2111         lmm_seq=$($LFS getstripe -v $1 | awk '/lmm_seq/ { print $2 }')
2112         lmm_oid=$($LFS getstripe -v $1 | awk '/lmm_object_id/ { print $2 }')
2113
2114         local old_ifs="$IFS"
2115         IFS=$'[:]'
2116         fid=($($LFS path2fid $1))
2117         IFS="$old_ifs"
2118
2119         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
2120         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
2121
2122         # compare lmm_seq and lu_fid->f_seq
2123         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
2124         # compare lmm_object_id and lu_fid->oid
2125         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
2126
2127         # check the trusted.fid attribute of the OST objects of the file
2128         local have_obdidx=false
2129         local stripe_nr=0
2130         $LFS getstripe $1 | while read obdidx oid hex seq; do
2131                 # skip lines up to and including "obdidx"
2132                 [ -z "$obdidx" ] && break
2133                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
2134                 $have_obdidx || continue
2135
2136                 local ost=$((obdidx + 1))
2137                 local dev=$(ostdevname $ost)
2138                 local oid_hex
2139
2140                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
2141
2142                 seq=$(echo $seq | sed -e "s/^0x//g")
2143                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
2144                         oid_hex=$(echo $oid)
2145                 else
2146                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
2147                 fi
2148                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
2149
2150                 local ff=""
2151                 #
2152                 # Don't unmount/remount the OSTs if we don't need to do that.
2153                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2154                 # update too, until that use mount/ll_decode_filter_fid/mount.
2155                 # Re-enable when debugfs will understand new filter_fid.
2156                 #
2157                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2158                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2159                                 $dev 2>/dev/null" | grep "parent=")
2160                 fi
2161                 if [ -z "$ff" ]; then
2162                         stop ost$ost
2163                         mount_fstype ost$ost
2164                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2165                                 $(facet_mntpt ost$ost)/$obj_file)
2166                         unmount_fstype ost$ost
2167                         start ost$ost $dev $OST_MOUNT_OPTS
2168                         clients_up
2169                 fi
2170
2171                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2172
2173                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2174
2175                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2176                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2177                 #
2178                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2179                 #       stripe_size=1048576 component_id=1 component_start=0 \
2180                 #       component_end=33554432
2181                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2182                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2183                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2184                 local ff_pstripe
2185                 if grep -q 'stripe=' <<<$ff; then
2186                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2187                 else
2188                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2189                         # into f_ver in this case.  See comment on ff_parent.
2190                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2191                 fi
2192
2193                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2194                 [ $ff_pseq = $lmm_seq ] ||
2195                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2196                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2197                 [ $ff_poid = $lmm_oid ] ||
2198                         error "FF parent OID $ff_poid != $lmm_oid"
2199                 (($ff_pstripe == $stripe_nr)) ||
2200                         error "FF stripe $ff_pstripe != $stripe_nr"
2201
2202                 stripe_nr=$((stripe_nr + 1))
2203                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2204                         continue
2205                 if grep -q 'stripe_count=' <<<$ff; then
2206                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2207                                             -e 's/ .*//' <<<$ff)
2208                         [ $lmm_count = $ff_scnt ] ||
2209                                 error "FF stripe count $lmm_count != $ff_scnt"
2210                 fi
2211         done
2212 }
2213
2214 test_27z() {
2215         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2216         remote_ost_nodsh && skip "remote OST with nodsh"
2217
2218         test_mkdir $DIR/$tdir
2219         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2220                 { error "setstripe -c -1 failed"; return 1; }
2221         # We need to send a write to every object to get parent FID info set.
2222         # This _should_ also work for setattr, but does not currently.
2223         # touch $DIR/$tdir/$tfile-1 ||
2224         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2225                 { error "dd $tfile-1 failed"; return 2; }
2226         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2227                 { error "setstripe -c -1 failed"; return 3; }
2228         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2229                 { error "dd $tfile-2 failed"; return 4; }
2230
2231         # make sure write RPCs have been sent to OSTs
2232         sync; sleep 5; sync
2233
2234         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2235         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2236 }
2237 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2238
2239 test_27A() { # b=19102
2240         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2241
2242         save_layout_restore_at_exit $MOUNT
2243         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2244         wait_update $HOSTNAME "$LFS getstripe -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2245                 error "stripe count $($LFS getstripe -c $MOUNT) != 1"
2246         local default_size=$($LFS getstripe -S $MOUNT)
2247         local default_offset=$($LFS getstripe -i $MOUNT)
2248         local dsize=$(do_facet $SINGLEMDS \
2249                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2250         [ $default_size -eq $dsize ] ||
2251                 error "stripe size $default_size != $dsize"
2252         [ $default_offset -eq -1 ] ||
2253                 error "stripe offset $default_offset != -1"
2254 }
2255 run_test 27A "check filesystem-wide default LOV EA values"
2256
2257 test_27B() { # LU-2523
2258         test_mkdir $DIR/$tdir
2259         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2260         touch $DIR/$tdir/f0
2261         # open f1 with O_LOV_DELAY_CREATE
2262         # rename f0 onto f1
2263         # call setstripe ioctl on open file descriptor for f1
2264         # close
2265         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2266                 $DIR/$tdir/f0
2267
2268         rm -f $DIR/$tdir/f1
2269         # open f1 with O_LOV_DELAY_CREATE
2270         # unlink f1
2271         # call setstripe ioctl on open file descriptor for f1
2272         # close
2273         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2274
2275         # Allow multiop to fail in imitation of NFS's busted semantics.
2276         true
2277 }
2278 run_test 27B "call setstripe on open unlinked file/rename victim"
2279
2280 # 27C family tests full striping and overstriping
2281 test_27Ca() { #LU-2871
2282         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2283
2284         declare -a ost_idx
2285         local index
2286         local found
2287         local i
2288         local j
2289
2290         test_mkdir $DIR/$tdir
2291         cd $DIR/$tdir
2292         for i in $(seq 0 $((OSTCOUNT - 1))); do
2293                 # set stripe across all OSTs starting from OST$i
2294                 $LFS setstripe -i $i -c -1 $tfile$i
2295                 # get striping information
2296                 ost_idx=($($LFS getstripe $tfile$i |
2297                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2298                 echo ${ost_idx[@]}
2299
2300                 # check the layout
2301                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2302                         error "${#ost_idx[@]} != $OSTCOUNT"
2303
2304                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2305                         found=0
2306                         for j in $(echo ${ost_idx[@]}); do
2307                                 if [ $index -eq $j ]; then
2308                                         found=1
2309                                         break
2310                                 fi
2311                         done
2312                         [ $found = 1 ] ||
2313                                 error "Can not find $index in ${ost_idx[@]}"
2314                 done
2315         done
2316 }
2317 run_test 27Ca "check full striping across all OSTs"
2318
2319 test_27Cb() {
2320         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2321                 skip "server does not support overstriping"
2322         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2323                 skip_env "too many osts, skipping"
2324
2325         test_mkdir -p $DIR/$tdir
2326         local setcount=$(($OSTCOUNT * 2))
2327         [ $setcount -ge 160 ] || large_xattr_enabled ||
2328                 skip_env "ea_inode feature disabled"
2329
2330         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2331                 error "setstripe failed"
2332
2333         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2334         [ $count -eq $setcount ] ||
2335                 error "stripe count $count, should be $setcount"
2336
2337         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2338                 error "overstriped should be set in pattern"
2339
2340         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2341                 error "dd failed"
2342 }
2343 run_test 27Cb "more stripes than OSTs with -C"
2344
2345 test_27Cc() {
2346         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2347                 skip "server does not support overstriping"
2348         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2349
2350         test_mkdir -p $DIR/$tdir
2351         local setcount=$(($OSTCOUNT - 1))
2352
2353         [ $setcount -ge 160 ] || large_xattr_enabled ||
2354                 skip_env "ea_inode feature disabled"
2355
2356         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2357                 error "setstripe failed"
2358
2359         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2360         [ $count -eq $setcount ] ||
2361                 error "stripe count $count, should be $setcount"
2362
2363         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" &&
2364                 error "overstriped should not be set in pattern"
2365
2366         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2367                 error "dd failed"
2368 }
2369 run_test 27Cc "fewer stripes than OSTs does not set overstriping"
2370
2371 test_27Cd() {
2372         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2373                 skip "server does not support overstriping"
2374         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2375         large_xattr_enabled || skip_env "ea_inode feature disabled"
2376
2377         test_mkdir -p $DIR/$tdir
2378         local setcount=$LOV_MAX_STRIPE_COUNT
2379
2380         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2381                 error "setstripe failed"
2382
2383         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2384         [ $count -eq $setcount ] ||
2385                 error "stripe count $count, should be $setcount"
2386
2387         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2388                 error "overstriped should be set in pattern"
2389
2390         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2391                 error "dd failed"
2392
2393         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2394 }
2395 run_test 27Cd "test maximum stripe count"
2396
2397 test_27Ce() {
2398         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2399                 skip "server does not support overstriping"
2400         test_mkdir -p $DIR/$tdir
2401
2402         pool_add $TESTNAME || error "Pool creation failed"
2403         pool_add_targets $TESTNAME 0 || error "pool_add_targets failed"
2404
2405         local setcount=8
2406
2407         $LFS setstripe  -C $setcount -p "$TESTNAME" $DIR/$tdir/$tfile ||
2408                 error "setstripe failed"
2409
2410         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2411         [ $count -eq $setcount ] ||
2412                 error "stripe count $count, should be $setcount"
2413
2414         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2415                 error "overstriped should be set in pattern"
2416
2417         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2418                 error "dd failed"
2419
2420         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2421 }
2422 run_test 27Ce "test pool with overstriping"
2423
2424 test_27Cf() {
2425         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2426                 skip "server does not support overstriping"
2427         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2428                 skip_env "too many osts, skipping"
2429
2430         test_mkdir -p $DIR/$tdir
2431
2432         local setcount=$(($OSTCOUNT * 2))
2433         [ $setcount -ge 160 ] || large_xattr_enabled ||
2434                 skip_env "ea_inode feature disabled"
2435
2436         $LFS setstripe  -C $setcount $DIR/$tdir/ ||
2437                 error "setstripe failed"
2438
2439         echo 1 > $DIR/$tdir/$tfile
2440
2441         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2442         [ $count -eq $setcount ] ||
2443                 error "stripe count $count, should be $setcount"
2444
2445         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2446                 error "overstriped should be set in pattern"
2447
2448         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2449                 error "dd failed"
2450
2451         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2452 }
2453 run_test 27Cf "test default inheritance with overstriping"
2454
2455 test_27D() {
2456         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2457         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2458         remote_mds_nodsh && skip "remote MDS with nodsh"
2459
2460         local POOL=${POOL:-testpool}
2461         local first_ost=0
2462         local last_ost=$(($OSTCOUNT - 1))
2463         local ost_step=1
2464         local ost_list=$(seq $first_ost $ost_step $last_ost)
2465         local ost_range="$first_ost $last_ost $ost_step"
2466
2467         test_mkdir $DIR/$tdir
2468         pool_add $POOL || error "pool_add failed"
2469         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2470
2471         local skip27D
2472         [ $MDS1_VERSION -lt $(version_code 2.8.55) ] &&
2473                 skip27D+="-s 29"
2474         [ $MDS1_VERSION -lt $(version_code 2.9.55) ] ||
2475                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2476                         skip27D+=" -s 30,31"
2477         [[ ! $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ||
2478           $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2479                 skip27D+=" -s 32,33"
2480         [[ $MDS_VERSION -lt $(version_code $SEL_VER) ]] &&
2481                 skip27D+=" -s 34"
2482         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2483                 error "llapi_layout_test failed"
2484
2485         destroy_test_pools || error "destroy test pools failed"
2486 }
2487 run_test 27D "validate llapi_layout API"
2488
2489 # Verify that default_easize is increased from its initial value after
2490 # accessing a widely striped file.
2491 test_27E() {
2492         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2493         [ $CLIENT_VERSION -lt $(version_code 2.5.57) ] &&
2494                 skip "client does not have LU-3338 fix"
2495
2496         # 72 bytes is the minimum space required to store striping
2497         # information for a file striped across one OST:
2498         # (sizeof(struct lov_user_md_v3) +
2499         #  sizeof(struct lov_user_ost_data_v1))
2500         local min_easize=72
2501         $LCTL set_param -n llite.*.default_easize $min_easize ||
2502                 error "lctl set_param failed"
2503         local easize=$($LCTL get_param -n llite.*.default_easize)
2504
2505         [ $easize -eq $min_easize ] ||
2506                 error "failed to set default_easize"
2507
2508         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2509                 error "setstripe failed"
2510         # In order to ensure stat() call actually talks to MDS we need to
2511         # do something drastic to this file to shake off all lock, e.g.
2512         # rename it (kills lookup lock forcing cache cleaning)
2513         mv $DIR/$tfile $DIR/${tfile}-1
2514         ls -l $DIR/${tfile}-1
2515         rm $DIR/${tfile}-1
2516
2517         easize=$($LCTL get_param -n llite.*.default_easize)
2518
2519         [ $easize -gt $min_easize ] ||
2520                 error "default_easize not updated"
2521 }
2522 run_test 27E "check that default extended attribute size properly increases"
2523
2524 test_27F() { # LU-5346/LU-7975
2525         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2526         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2527         [[ $MDS1_VERSION -lt $(version_code 2.8.51) ]] &&
2528                 skip "Need MDS version at least 2.8.51"
2529         remote_ost_nodsh && skip "remote OST with nodsh"
2530
2531         test_mkdir $DIR/$tdir
2532         rm -f $DIR/$tdir/f0
2533         $LFS setstripe -c 2 $DIR/$tdir
2534
2535         # stop all OSTs to reproduce situation for LU-7975 ticket
2536         for num in $(seq $OSTCOUNT); do
2537                 stop ost$num
2538         done
2539
2540         # open/create f0 with O_LOV_DELAY_CREATE
2541         # truncate f0 to a non-0 size
2542         # close
2543         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2544
2545         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2546         # open/write it again to force delayed layout creation
2547         cat /etc/hosts > $DIR/$tdir/f0 &
2548         catpid=$!
2549
2550         # restart OSTs
2551         for num in $(seq $OSTCOUNT); do
2552                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2553                         error "ost$num failed to start"
2554         done
2555
2556         wait $catpid || error "cat failed"
2557
2558         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2559         [[ $($LFS getstripe -c $DIR/$tdir/f0) == 2 ]] ||
2560                 error "wrong stripecount"
2561
2562 }
2563 run_test 27F "Client resend delayed layout creation with non-zero size"
2564
2565 test_27G() { #LU-10629
2566         [ $MDS1_VERSION -lt $(version_code 2.11.51) ] &&
2567                 skip "Need MDS version at least 2.11.51"
2568         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2569         remote_mds_nodsh && skip "remote MDS with nodsh"
2570         local POOL=${POOL:-testpool}
2571         local ostrange="0 0 1"
2572
2573         test_mkdir $DIR/$tdir
2574         pool_add $POOL || error "pool_add failed"
2575         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2576         $LFS setstripe -p $POOL $DIR/$tdir
2577
2578         local pool=$($LFS getstripe -p $DIR/$tdir)
2579
2580         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2581
2582         $LFS setstripe -d $DIR/$tdir
2583
2584         pool=$($LFS getstripe -p $DIR/$tdir)
2585
2586         rmdir $DIR/$tdir
2587
2588         [ -z "$pool" ] || error "'$pool' is not empty"
2589 }
2590 run_test 27G "Clear OST pool from stripe"
2591
2592 test_27H() {
2593         [[ $MDS1_VERSION -le $(version_code 2.11.54) ]] &&
2594                 skip "Need MDS version newer than 2.11.54"
2595         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2596         test_mkdir $DIR/$tdir
2597         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2598         touch $DIR/$tdir/$tfile
2599         $LFS getstripe -c $DIR/$tdir/$tfile
2600         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2601                 error "two-stripe file doesn't have two stripes"
2602
2603         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2604         $LFS getstripe -y $DIR/$tdir/$tfile
2605         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2606              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2607                 error "expected l_ost_idx: [02]$ not matched"
2608
2609         # make sure ost list has been cleared
2610         local stripesize=$($LFS getstripe -S $DIR/$tdir)
2611         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2612                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2613         touch $DIR/$tdir/f3
2614         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2615 }
2616 run_test 27H "Set specific OSTs stripe"
2617
2618 test_27I() {
2619         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2620         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2621         [[ $MDS1_VERSION -gt $(version_code 2.12.52) ]] ||
2622                 skip "Need MDS version newer than 2.12.52"
2623         local pool=$TESTNAME
2624         local ostrange="1 1 1"
2625
2626         save_layout_restore_at_exit $MOUNT
2627         $LFS setstripe -c 2 -i 0 $MOUNT
2628         pool_add $pool || error "pool_add failed"
2629         pool_add_targets $pool $ostrange || "pool_add_targets failed"
2630         test_mkdir $DIR/$tdir
2631         $LFS setstripe -p $pool $DIR/$tdir
2632         $MULTIOP $DIR/$tdir/$tfile Oc || error "multiop failed"
2633         $LFS getstripe $DIR/$tdir/$tfile
2634 }
2635 run_test 27I "check that root dir striping does not break parent dir one"
2636
2637 test_27J() {
2638         [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.12.51) ]] &&
2639                 skip "Need MDS version newer than 2.12.51"
2640
2641         test_mkdir $DIR/$tdir
2642         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2643         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2644
2645         # create foreign file (raw way)
2646         create_foreign_file -f $DIR/$tdir/$tfile -x "${uuid1}@${uuid2}" \
2647                 -t 1 -F 0xda08 || error "create_foreign_file failed"
2648
2649         # verify foreign file (raw way)
2650         parse_foreign_file -f $DIR/$tdir/$tfile |
2651                 grep "lov_foreign_magic: 0x0BD70BD0" ||
2652                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign magic"
2653         parse_foreign_file -f $DIR/$tdir/$tfile | grep "lov_xattr_size: 89" ||
2654                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2655         parse_foreign_file -f $DIR/$tdir/$tfile |
2656                 grep "lov_foreign_size: 73" ||
2657                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2658         parse_foreign_file -f $DIR/$tdir/$tfile |
2659                 grep "lov_foreign_type: 1" ||
2660                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign type"
2661         parse_foreign_file -f $DIR/$tdir/$tfile |
2662                 grep "lov_foreign_flags: 0x0000DA08" ||
2663                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign flags"
2664         local lov=$(parse_foreign_file -f $DIR/$tdir/$tfile |
2665                 grep "lov_foreign_value: 0x" |
2666                 sed -e 's/lov_foreign_value: 0x//')
2667         local lov2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160)
2668         [[ $lov = ${lov2// /} ]] ||
2669                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign value"
2670
2671         # create foreign file (lfs + API)
2672         $LFS setstripe --foreign=daos --flags 0xda08 \
2673                 -x "${uuid1}@${uuid2}" $DIR/$tdir/${tfile}2 ||
2674                 error "$DIR/$tdir/${tfile}2: create failed"
2675
2676         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2677                 grep "lfm_magic:.*0x0BD70BD0" ||
2678                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign magic"
2679         # lfm_length is LOV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2680         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_length:.*73" ||
2681                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign size"
2682         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_type:.*daos" ||
2683                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign type"
2684         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2685                 grep "lfm_flags:.*0x0000DA08" ||
2686                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign flags"
2687         $LFS getstripe $DIR/$tdir/${tfile}2 |
2688                 grep "lfm_value:.*${uuid1}@${uuid2}" ||
2689                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign value"
2690
2691         # modify striping should fail
2692         $LFS setstripe -c 2 $DIR/$tdir/$tfile &&
2693                 error "$DIR/$tdir/$tfile: setstripe should fail"
2694         $LFS setstripe -c 2 $DIR/$tdir/${tfile}2 &&
2695                 error "$DIR/$tdir/${tfile}2: setstripe should fail"
2696
2697         # R/W should fail
2698         cat $DIR/$tdir/$tfile && error "$DIR/$tdir/$tfile: read should fail"
2699         cat $DIR/$tdir/${tfile}2 &&
2700                 error "$DIR/$tdir/${tfile}2: read should fail"
2701         cat /etc/passwd > $DIR/$tdir/$tfile &&
2702                 error "$DIR/$tdir/$tfile: write should fail"
2703         cat /etc/passwd > $DIR/$tdir/${tfile}2 &&
2704                 error "$DIR/$tdir/${tfile}2: write should fail"
2705
2706         # chmod should work
2707         chmod 222 $DIR/$tdir/$tfile ||
2708                 error "$DIR/$tdir/$tfile: chmod failed"
2709         chmod 222 $DIR/$tdir/${tfile}2 ||
2710                 error "$DIR/$tdir/${tfile}2: chmod failed"
2711
2712         # chown should work
2713         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tfile ||
2714                 error "$DIR/$tdir/$tfile: chown failed"
2715         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tfile}2 ||
2716                 error "$DIR/$tdir/${tfile}2: chown failed"
2717
2718         # rename should work
2719         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}.new ||
2720                 error "$DIR/$tdir/$tfile: rename of foreign file has failed"
2721         mv $DIR/$tdir/${tfile}2 $DIR/$tdir/${tfile}2.new ||
2722                 error "$DIR/$tdir/${tfile}2: rename of foreign file has failed"
2723
2724         #remove foreign file
2725         rm $DIR/$tdir/${tfile}.new ||
2726                 error "$DIR/$tdir/${tfile}.new: remove of foreign file has failed"
2727         rm $DIR/$tdir/${tfile}2.new ||
2728                 error "$DIR/$tdir/${tfile}2.new: remove of foreign file has failed"
2729 }
2730 run_test 27J "basic ops on file with foreign LOV"
2731
2732 test_27K() {
2733         [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.12.49) ]] &&
2734                 skip "Need MDS version newer than 2.12.49"
2735
2736         test_mkdir $DIR/$tdir
2737         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2738         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2739
2740         # create foreign dir (raw way)
2741         create_foreign_dir -d $DIR/$tdir/$tdir -x "${uuid1}@${uuid2}" -t 1 ||
2742                 error "create_foreign_dir FAILED"
2743
2744         # verify foreign dir (raw way)
2745         parse_foreign_dir -d $DIR/$tdir/$tdir |
2746                 grep "lmv_foreign_magic:.*0xcd50cd0" ||
2747                 error "$DIR/$tdir/$tfile: invalid LMV EA magic"
2748         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_xattr_size:.*89$" ||
2749                 error "$DIR/$tdir/$tdir: invalid LMV EA size"
2750         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_type: 1$" ||
2751                 error "$DIR/$tdir/$tdir: invalid LMV EA type"
2752         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_flags: 0$" ||
2753                 error "$DIR/$tdir/$tdir: invalid LMV EA flags"
2754         local lmv=$(parse_foreign_dir -d $DIR/$tdir/$tdir |
2755                 grep "lmv_foreign_value: 0x" |
2756                 sed 's/lmv_foreign_value: 0x//')
2757         local lmv2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160 |
2758                 sed 's/ //g')
2759         [[ $lmv == $lmv2 ]] || error "$DIR/$tdir/$tdir: invalid LMV EA value"
2760
2761         # create foreign dir (lfs + API)
2762         $LFS mkdir --foreign=daos --xattr="${uuid1}@${uuid2}" --flags=0xda05 \
2763                 $DIR/$tdir/${tdir}2 ||
2764                 error "$DIR/$tdir/${tdir}2: create failed"
2765
2766         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2767                 grep "lfm_magic:.*0x0CD50CD0" ||
2768                 error "$DIR/$tdir/${tdir}2: invalid LMV EA magic"
2769         # lfm_length is LMV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2770         # - sizeof(lfm_type) - sizeof(lfm_flags)
2771         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_length:.*73" ||
2772                 error "$DIR/$tdir/${tdir}2: invalid LMV EA size"
2773         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_type:.*daos" ||
2774                 error "$DIR/$tdir/${tdir}2: invalid LMV EA type"
2775         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2776                 grep "lfm_flags:.*0x0000DA05" ||
2777                 error "$DIR/$tdir/${tdir}2: invalid LMV EA flags"
2778         $LFS getdirstripe $DIR/$tdir/${tdir}2 |
2779                 grep "lfm_value.*${uuid1}@${uuid2}" ||
2780                 error "$DIR/$tdir/${tdir}2: invalid LMV EA value"
2781
2782         # file create in dir should fail
2783         touch $DIR/$tdir/$tdir/$tfile && "$DIR/$tdir: file create should fail"
2784         touch $DIR/$tdir/${tdir}2/$tfile &&
2785                 "$DIR/${tdir}2: file create should fail"
2786
2787         # chmod should work
2788         chmod 777 $DIR/$tdir/$tdir ||
2789                 error "$DIR/$tdir: chmod failed"
2790         chmod 777 $DIR/$tdir/${tdir}2 ||
2791                 error "$DIR/${tdir}2: chmod failed"
2792
2793         # chown should work
2794         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tdir ||
2795                 error "$DIR/$tdir: chown failed"
2796         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tdir}2 ||
2797                 error "$DIR/${tdir}2: chown failed"
2798
2799         # rename should work
2800         mv $DIR/$tdir/$tdir $DIR/$tdir/${tdir}.new ||
2801                 error "$DIR/$tdir/$tdir: rename of foreign dir has failed"
2802         mv $DIR/$tdir/${tdir}2 $DIR/$tdir/${tdir}2.new ||
2803                 error "$DIR/$tdir/${tdir}2: rename of foreign dir has failed"
2804
2805         #remove foreign dir
2806         rmdir $DIR/$tdir/${tdir}.new ||
2807                 error "$DIR/$tdir/${tdir}.new: remove of foreign dir has failed"
2808         rmdir $DIR/$tdir/${tdir}2.new ||
2809                 error "$DIR/$tdir/${tdir}2.new: remove of foreign dir has failed"
2810 }
2811 run_test 27K "basic ops on dir with foreign LMV"
2812
2813 test_27L() {
2814         remote_mds_nodsh && skip "remote MDS with nodsh"
2815
2816         local POOL=${POOL:-$TESTNAME}
2817
2818         pool_add $POOL || error "pool_add failed"
2819
2820         lfs pool_list $MOUNT | grep -Fx "${FSNAME}.${POOL}" ||
2821                  error "pool_list does not contain ${FSNAME}.${POOL}:" \
2822                        "$(lfs pool_list $MOUNT | grep -F "${POOL}")"
2823 }
2824 run_test 27L "lfs pool_list gives correct pool name"
2825
2826 test_27M() {
2827         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.12.57) ]] &&
2828                 skip "Need MDS version >= than 2.12.57"
2829         remote_mds_nodsh && skip "remote MDS with nodsh"
2830         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2831
2832         test_mkdir $DIR/$tdir
2833
2834         # Set default striping on directory
2835         $LFS setstripe -C 4 $DIR/$tdir
2836
2837         echo 1 > $DIR/$tdir/${tfile}.1
2838         local count=$($LFS getstripe -c $DIR/$tdir/${tfile}.1)
2839         local setcount=4
2840         [ $count -eq $setcount ] ||
2841                 error "(1) stripe count $count, should be $setcount"
2842
2843         # Capture existing append_stripe_count setting for restore
2844         local orig_count=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_stripe_count)
2845         local mdts=$(comma_list $(mdts_nodes))
2846         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=$orig_count" EXIT
2847
2848         local appendcount=$orig_count
2849         echo 1 >> $DIR/$tdir/${tfile}.2_append
2850         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.2_append)
2851         [ $count -eq $appendcount ] ||
2852                 error "(2)stripe count $count, should be $appendcount for append"
2853
2854         # Disable O_APPEND striping, verify it works
2855         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2856
2857         # Should now get the default striping, which is 4
2858         setcount=4
2859         echo 1 >> $DIR/$tdir/${tfile}.3_append
2860         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.3_append)
2861         [ $count -eq $setcount ] ||
2862                 error "(3) stripe count $count, should be $setcount"
2863
2864         # Try changing the stripe count for append files
2865         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=2
2866
2867         # Append striping is now 2 (directory default is still 4)
2868         appendcount=2
2869         echo 1 >> $DIR/$tdir/${tfile}.4_append
2870         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.4_append)
2871         [ $count -eq $appendcount ] ||
2872                 error "(4) stripe count $count, should be $appendcount for append"
2873
2874         # Test append stripe count of -1
2875         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=-1
2876         appendcount=$OSTCOUNT
2877         echo 1 >> $DIR/$tdir/${tfile}.5
2878         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.5)
2879         [ $count -eq $appendcount ] ||
2880                 error "(5) stripe count $count, should be $appendcount for append"
2881
2882         # Set append striping back to default of 1
2883         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=1
2884
2885         # Try a new default striping, PFL + DOM
2886         $LFS setstripe -L mdt -E 1M -E -1 -c 2 $DIR/$tdir
2887
2888         # Create normal DOM file, DOM returns stripe count == 0
2889         setcount=0
2890         touch $DIR/$tdir/${tfile}.6
2891         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.6)
2892         [ $count -eq $setcount ] ||
2893                 error "(6) stripe count $count, should be $setcount"
2894
2895         # Show
2896         appendcount=1
2897         echo 1 >> $DIR/$tdir/${tfile}.7_append
2898         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.7_append)
2899         [ $count -eq $appendcount ] ||
2900                 error "(7) stripe count $count, should be $appendcount for append"
2901
2902         # Clean up DOM layout
2903         $LFS setstripe -d $DIR/$tdir
2904
2905         # Now test that append striping works when layout is from root
2906         $LFS setstripe -c 2 $MOUNT
2907         # Make a special directory for this
2908         mkdir $DIR/${tdir}/${tdir}.2
2909         stack_trap "$LFS setstripe -d $MOUNT" EXIT
2910
2911         # Verify for normal file
2912         setcount=2
2913         echo 1 > $DIR/${tdir}/${tdir}.2/${tfile}.8
2914         count=$($LFS getstripe -c $DIR/$tdir/${tdir}.2/${tfile}.8)
2915         [ $count -eq $setcount ] ||
2916                 error "(8) stripe count $count, should be $setcount"
2917
2918         appendcount=1
2919         echo 1 >> $DIR/${tdir}/${tdir}.2/${tfile}.9_append
2920         count=$($LFS getstripe -c $DIR/${tdir}/${tdir}.2/${tfile}.9_append)
2921         [ $count -eq $appendcount ] ||
2922                 error "(9) stripe count $count, should be $appendcount for append"
2923
2924         # Now test O_APPEND striping with pools
2925         do_nodes $mdts $LCTL set_param mdd.*.append_pool="$TESTNAME"
2926         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'" EXIT
2927
2928         # Create the pool
2929         pool_add $TESTNAME || error "pool creation failed"
2930         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
2931
2932         echo 1 >> $DIR/$tdir/${tfile}.10_append
2933
2934         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.10_append)
2935         [ "$pool" = "$TESTNAME" ] || error "(10) incorrect pool: $pool"
2936
2937         # Check that count is still correct
2938         appendcount=1
2939         echo 1 >> $DIR/$tdir/${tfile}.11_append
2940         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.11_append)
2941         [ $count -eq $appendcount ] ||
2942                 error "(11) stripe count $count, should be $appendcount for append"
2943
2944         # Disable O_APPEND stripe count, verify pool works separately
2945         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2946
2947         echo 1 >> $DIR/$tdir/${tfile}.12_append
2948
2949         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.12_append)
2950         [ "$pool" = "$TESTNAME" ] || error "(12) incorrect pool: $pool"
2951
2952         # Remove pool setting, verify it's not applied
2953         do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'
2954
2955         echo 1 >> $DIR/$tdir/${tfile}.13_append
2956
2957         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.13_append)
2958         [ "$pool" = "" ] || error "(13) pool found: $pool"
2959 }
2960 run_test 27M "test O_APPEND striping"
2961
2962 test_27N() {
2963         combined_mgs_mds && skip "needs separate MGS/MDT"
2964
2965         pool_add $TESTNAME || error "pool_add failed"
2966         do_facet mgs "$LCTL pool_list $FSNAME" |
2967                 grep -Fx "${FSNAME}.${TESTNAME}" ||
2968                 error "lctl pool_list on MGS failed"
2969 }
2970 run_test 27N "lctl pool_list on separate MGS gives correct pool name"
2971
2972 # createtest also checks that device nodes are created and
2973 # then visible correctly (#2091)
2974 test_28() { # bug 2091
2975         test_mkdir $DIR/d28
2976         $CREATETEST $DIR/d28/ct || error "createtest failed"
2977 }
2978 run_test 28 "create/mknod/mkdir with bad file types ============"
2979
2980 test_29() {
2981         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2982
2983         sync; sleep 1; sync # flush out any dirty pages from previous tests
2984         cancel_lru_locks
2985         test_mkdir $DIR/d29
2986         touch $DIR/d29/foo
2987         log 'first d29'
2988         ls -l $DIR/d29
2989
2990         declare -i LOCKCOUNTORIG=0
2991         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2992                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2993         done
2994         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2995
2996         declare -i LOCKUNUSEDCOUNTORIG=0
2997         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2998                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2999         done
3000
3001         log 'second d29'
3002         ls -l $DIR/d29
3003         log 'done'
3004
3005         declare -i LOCKCOUNTCURRENT=0
3006         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3007                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
3008         done
3009
3010         declare -i LOCKUNUSEDCOUNTCURRENT=0
3011         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3012                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
3013         done
3014
3015         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
3016                 $LCTL set_param -n ldlm.dump_namespaces ""
3017                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
3018                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3019                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3020                 return 2
3021         fi
3022         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
3023                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
3024                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3025                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3026                 return 3
3027         fi
3028 }
3029 run_test 29 "IT_GETATTR regression  ============================"
3030
3031 test_30a() { # was test_30
3032         cp $(which ls) $DIR || cp /bin/ls $DIR
3033         $DIR/ls / || error "Can't execute binary from lustre"
3034         rm $DIR/ls
3035 }
3036 run_test 30a "execute binary from Lustre (execve) =============="
3037
3038 test_30b() {
3039         cp `which ls` $DIR || cp /bin/ls $DIR
3040         chmod go+rx $DIR/ls
3041         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
3042         rm $DIR/ls
3043 }
3044 run_test 30b "execute binary from Lustre as non-root ==========="
3045
3046 test_30c() { # b=22376
3047         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3048
3049         cp `which ls` $DIR || cp /bin/ls $DIR
3050         chmod a-rw $DIR/ls
3051         cancel_lru_locks mdc
3052         cancel_lru_locks osc
3053         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
3054         rm -f $DIR/ls
3055 }
3056 run_test 30c "execute binary from Lustre without read perms ===="
3057
3058 test_31a() {
3059         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
3060         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
3061 }
3062 run_test 31a "open-unlink file =================================="
3063
3064 test_31b() {
3065         touch $DIR/f31 || error "touch $DIR/f31 failed"
3066         ln $DIR/f31 $DIR/f31b || error "ln failed"
3067         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
3068         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
3069 }
3070 run_test 31b "unlink file with multiple links while open ======="
3071
3072 test_31c() {
3073         touch $DIR/f31 || error "touch $DIR/f31 failed"
3074         ln $DIR/f31 $DIR/f31c || error "ln failed"
3075         multiop_bg_pause $DIR/f31 O_uc ||
3076                 error "multiop_bg_pause for $DIR/f31 failed"
3077         MULTIPID=$!
3078         $MULTIOP $DIR/f31c Ouc
3079         kill -USR1 $MULTIPID
3080         wait $MULTIPID
3081 }
3082 run_test 31c "open-unlink file with multiple links ============="
3083
3084 test_31d() {
3085         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
3086         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
3087 }
3088 run_test 31d "remove of open directory ========================="
3089
3090 test_31e() { # bug 2904
3091         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
3092 }
3093 run_test 31e "remove of open non-empty directory ==============="
3094
3095 test_31f() { # bug 4554
3096         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3097
3098         set -vx
3099         test_mkdir $DIR/d31f
3100         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3101         cp /etc/hosts $DIR/d31f
3102         ls -l $DIR/d31f
3103         $LFS getstripe $DIR/d31f/hosts
3104         multiop_bg_pause $DIR/d31f D_c || return 1
3105         MULTIPID=$!
3106
3107         rm -rv $DIR/d31f || error "first of $DIR/d31f"
3108         test_mkdir $DIR/d31f
3109         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3110         cp /etc/hosts $DIR/d31f
3111         ls -l $DIR/d31f
3112         $LFS getstripe $DIR/d31f/hosts
3113         multiop_bg_pause $DIR/d31f D_c || return 1
3114         MULTIPID2=$!
3115
3116         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
3117         wait $MULTIPID || error "first opendir $MULTIPID failed"
3118
3119         sleep 6
3120
3121         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
3122         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
3123         set +vx
3124 }
3125 run_test 31f "remove of open directory with open-unlink file ==="
3126
3127 test_31g() {
3128         echo "-- cross directory link --"
3129         test_mkdir -c1 $DIR/${tdir}ga
3130         test_mkdir -c1 $DIR/${tdir}gb
3131         touch $DIR/${tdir}ga/f
3132         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
3133         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
3134         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
3135         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
3136         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
3137 }
3138 run_test 31g "cross directory link==============="
3139
3140 test_31h() {
3141         echo "-- cross directory link --"
3142         test_mkdir -c1 $DIR/${tdir}
3143         test_mkdir -c1 $DIR/${tdir}/dir
3144         touch $DIR/${tdir}/f
3145         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
3146         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
3147         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
3148         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
3149         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
3150 }
3151 run_test 31h "cross directory link under child==============="
3152
3153 test_31i() {
3154         echo "-- cross directory link --"
3155         test_mkdir -c1 $DIR/$tdir
3156         test_mkdir -c1 $DIR/$tdir/dir
3157         touch $DIR/$tdir/dir/f
3158         ln $DIR/$tdir/dir/f $DIR/$tdir/g
3159         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
3160         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
3161         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
3162         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
3163 }
3164 run_test 31i "cross directory link under parent==============="
3165
3166 test_31j() {
3167         test_mkdir -c1 -p $DIR/$tdir
3168         test_mkdir -c1 -p $DIR/$tdir/dir1
3169         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
3170         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
3171         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
3172         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
3173         return 0
3174 }
3175 run_test 31j "link for directory==============="
3176
3177 test_31k() {
3178         test_mkdir -c1 -p $DIR/$tdir
3179         touch $DIR/$tdir/s
3180         touch $DIR/$tdir/exist
3181         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
3182         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
3183         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
3184         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
3185         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
3186         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
3187         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
3188         return 0
3189 }
3190 run_test 31k "link to file: the same, non-existing, dir==============="
3191
3192 test_31m() {
3193         mkdir $DIR/d31m
3194         touch $DIR/d31m/s
3195         mkdir $DIR/d31m2
3196         touch $DIR/d31m2/exist
3197         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
3198         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
3199         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
3200         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
3201         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
3202         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
3203         return 0
3204 }
3205 run_test 31m "link to file: the same, non-existing, dir==============="
3206
3207 test_31n() {
3208         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
3209         nlink=$(stat --format=%h $DIR/$tfile)
3210         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3211         local fd=$(free_fd)
3212         local cmd="exec $fd<$DIR/$tfile"
3213         eval $cmd
3214         cmd="exec $fd<&-"
3215         trap "eval $cmd" EXIT
3216         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3217         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3218         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
3219         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3220         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
3221         eval $cmd
3222 }
3223 run_test 31n "check link count of unlinked file"
3224
3225 link_one() {
3226         local tempfile=$(mktemp $1_XXXXXX)
3227         mlink $tempfile $1 2> /dev/null &&
3228                 echo "$BASHPID: link $tempfile to $1 succeeded"
3229         munlink $tempfile
3230 }
3231
3232 test_31o() { # LU-2901
3233         test_mkdir $DIR/$tdir
3234         for LOOP in $(seq 100); do
3235                 rm -f $DIR/$tdir/$tfile*
3236                 for THREAD in $(seq 8); do
3237                         link_one $DIR/$tdir/$tfile.$LOOP &
3238                 done
3239                 wait
3240                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
3241                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
3242                         error "$LINKS duplicate links to $tfile.$LOOP" &&
3243                         break || true
3244         done
3245 }
3246 run_test 31o "duplicate hard links with same filename"
3247
3248 test_31p() {
3249         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3250
3251         test_mkdir $DIR/$tdir
3252         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3253         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
3254
3255         opendirunlink $DIR/$tdir/striped_dir/test1 ||
3256                 error "open unlink test1 failed"
3257         opendirunlink $DIR/$tdir/striped_dir/test2 ||
3258                 error "open unlink test2 failed"
3259
3260         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
3261                 error "test1 still exists"
3262         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
3263                 error "test2 still exists"
3264 }
3265 run_test 31p "remove of open striped directory"
3266
3267 cleanup_test32_mount() {
3268         local rc=0
3269         trap 0
3270         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
3271         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
3272         losetup -d $loopdev || true
3273         rm -rf $DIR/$tdir
3274         return $rc
3275 }
3276
3277 test_32a() {
3278         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3279
3280         echo "== more mountpoints and symlinks ================="
3281         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3282         trap cleanup_test32_mount EXIT
3283         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3284         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3285                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3286         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
3287                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
3288         cleanup_test32_mount
3289 }
3290 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
3291
3292 test_32b() {
3293         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3294
3295         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3296         trap cleanup_test32_mount EXIT
3297         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3298         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3299                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3300         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
3301                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
3302         cleanup_test32_mount
3303 }
3304 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
3305
3306 test_32c() {
3307         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3308
3309         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3310         trap cleanup_test32_mount EXIT
3311         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3312         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3313                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3314         test_mkdir -p $DIR/$tdir/d2/test_dir
3315         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3316                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
3317         cleanup_test32_mount
3318 }
3319 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
3320
3321 test_32d() {
3322         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3323
3324         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3325         trap cleanup_test32_mount EXIT
3326         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3327         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3328                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3329         test_mkdir -p $DIR/$tdir/d2/test_dir
3330         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3331                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
3332         cleanup_test32_mount
3333 }
3334 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
3335
3336 test_32e() {
3337         rm -fr $DIR/$tdir
3338         test_mkdir -p $DIR/$tdir/tmp
3339         local tmp_dir=$DIR/$tdir/tmp
3340         ln -s $DIR/$tdir $tmp_dir/symlink11
3341         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3342         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
3343         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
3344 }
3345 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
3346
3347 test_32f() {
3348         rm -fr $DIR/$tdir
3349         test_mkdir -p $DIR/$tdir/tmp
3350         local tmp_dir=$DIR/$tdir/tmp
3351         ln -s $DIR/$tdir $tmp_dir/symlink11
3352         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3353         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
3354         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
3355 }
3356 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
3357
3358 test_32g() {
3359         local tmp_dir=$DIR/$tdir/tmp
3360         test_mkdir -p $tmp_dir
3361         test_mkdir $DIR/${tdir}2
3362         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3363         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3364         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
3365         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
3366         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
3367         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
3368 }
3369 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3370
3371 test_32h() {
3372         rm -fr $DIR/$tdir $DIR/${tdir}2
3373         tmp_dir=$DIR/$tdir/tmp
3374         test_mkdir -p $tmp_dir
3375         test_mkdir $DIR/${tdir}2
3376         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3377         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3378         ls $tmp_dir/symlink12 || error "listing symlink12"
3379         ls $DIR/$tdir/symlink02  || error "listing symlink02"
3380 }
3381 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3382
3383 test_32i() {
3384         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3385
3386         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3387         trap cleanup_test32_mount EXIT
3388         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3389         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3390                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3391         touch $DIR/$tdir/test_file
3392         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
3393                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
3394         cleanup_test32_mount
3395 }
3396 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
3397
3398 test_32j() {
3399         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3400
3401         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3402         trap cleanup_test32_mount EXIT
3403         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3404         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3405                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3406         touch $DIR/$tdir/test_file
3407         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
3408                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
3409         cleanup_test32_mount
3410 }
3411 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
3412
3413 test_32k() {
3414         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3415
3416         rm -fr $DIR/$tdir
3417         trap cleanup_test32_mount EXIT
3418         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3419         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3420                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3421         test_mkdir -p $DIR/$tdir/d2
3422         touch $DIR/$tdir/d2/test_file || error "touch failed"
3423         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3424                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
3425         cleanup_test32_mount
3426 }
3427 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
3428
3429 test_32l() {
3430         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3431
3432         rm -fr $DIR/$tdir
3433         trap cleanup_test32_mount EXIT
3434         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3435         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3436                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3437         test_mkdir -p $DIR/$tdir/d2
3438         touch $DIR/$tdir/d2/test_file || error "touch failed"
3439         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3440                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
3441         cleanup_test32_mount
3442 }
3443 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
3444
3445 test_32m() {
3446         rm -fr $DIR/d32m
3447         test_mkdir -p $DIR/d32m/tmp
3448         TMP_DIR=$DIR/d32m/tmp
3449         ln -s $DIR $TMP_DIR/symlink11
3450         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3451         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
3452                 error "symlink11 not a link"
3453         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
3454                 error "symlink01 not a link"
3455 }
3456 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
3457
3458 test_32n() {
3459         rm -fr $DIR/d32n
3460         test_mkdir -p $DIR/d32n/tmp
3461         TMP_DIR=$DIR/d32n/tmp
3462         ln -s $DIR $TMP_DIR/symlink11
3463         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3464         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
3465         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
3466 }
3467 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
3468
3469 test_32o() {
3470         touch $DIR/$tfile
3471         test_mkdir -p $DIR/d32o/tmp
3472         TMP_DIR=$DIR/d32o/tmp
3473         ln -s $DIR/$tfile $TMP_DIR/symlink12
3474         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3475         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
3476                 error "symlink12 not a link"
3477         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
3478         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
3479                 error "$DIR/d32o/tmp/symlink12 not file type"
3480         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
3481                 error "$DIR/d32o/symlink02 not file type"
3482 }
3483 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
3484
3485 test_32p() {
3486         log 32p_1
3487         rm -fr $DIR/d32p
3488         log 32p_2
3489         rm -f $DIR/$tfile
3490         log 32p_3
3491         touch $DIR/$tfile
3492         log 32p_4
3493         test_mkdir -p $DIR/d32p/tmp
3494         log 32p_5
3495         TMP_DIR=$DIR/d32p/tmp
3496         log 32p_6
3497         ln -s $DIR/$tfile $TMP_DIR/symlink12
3498         log 32p_7
3499         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3500         log 32p_8
3501         cat $DIR/d32p/tmp/symlink12 ||
3502                 error "Can't open $DIR/d32p/tmp/symlink12"
3503         log 32p_9
3504         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
3505         log 32p_10
3506 }
3507 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
3508
3509 test_32q() {
3510         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3511
3512         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3513         trap cleanup_test32_mount EXIT
3514         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3515         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3516         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3517                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3518         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
3519         cleanup_test32_mount
3520 }
3521 run_test 32q "stat follows mountpoints in Lustre (should return error)"
3522
3523 test_32r() {
3524         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3525
3526         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3527         trap cleanup_test32_mount EXIT
3528         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3529         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3530         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3531                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3532         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
3533         cleanup_test32_mount
3534 }
3535 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
3536
3537 test_33aa() {
3538         rm -f $DIR/$tfile
3539         touch $DIR/$tfile
3540         chmod 444 $DIR/$tfile
3541         chown $RUNAS_ID $DIR/$tfile
3542         log 33_1
3543         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3544         log 33_2
3545 }
3546 run_test 33aa "write file with mode 444 (should return error)"
3547
3548 test_33a() {
3549         rm -fr $DIR/$tdir
3550         test_mkdir $DIR/$tdir
3551         chown $RUNAS_ID $DIR/$tdir
3552         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
3553                 error "$RUNAS create $tdir/$tfile failed"
3554         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
3555                 error "open RDWR" || true
3556 }
3557 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
3558
3559 test_33b() {
3560         rm -fr $DIR/$tdir
3561         test_mkdir $DIR/$tdir
3562         chown $RUNAS_ID $DIR/$tdir
3563         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
3564 }
3565 run_test 33b "test open file with malformed flags (No panic)"
3566
3567 test_33c() {
3568         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3569         remote_ost_nodsh && skip "remote OST with nodsh"
3570
3571         local ostnum
3572         local ostname
3573         local write_bytes
3574         local all_zeros
3575
3576         all_zeros=:
3577         rm -fr $DIR/$tdir
3578         test_mkdir $DIR/$tdir
3579         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
3580
3581         sync
3582         for ostnum in $(seq $OSTCOUNT); do
3583                 # test-framework's OST numbering is one-based, while Lustre's
3584                 # is zero-based
3585                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3586                 # Parsing llobdstat's output sucks; we could grep the /proc
3587                 # path, but that's likely to not be as portable as using the
3588                 # llobdstat utility.  So we parse lctl output instead.
3589                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3590                         obdfilter/$ostname/stats |
3591                         awk '/^write_bytes/ {print $7}' )
3592                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
3593                 if (( ${write_bytes:-0} > 0 ))
3594                 then
3595                         all_zeros=false
3596                         break;
3597                 fi
3598         done
3599
3600         $all_zeros || return 0
3601
3602         # Write four bytes
3603         echo foo > $DIR/$tdir/bar
3604         # Really write them
3605         sync
3606
3607         # Total up write_bytes after writing.  We'd better find non-zeros.
3608         for ostnum in $(seq $OSTCOUNT); do
3609                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3610                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3611                         obdfilter/$ostname/stats |
3612                         awk '/^write_bytes/ {print $7}' )
3613                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
3614                 if (( ${write_bytes:-0} > 0 ))
3615                 then
3616                         all_zeros=false
3617                         break;
3618                 fi
3619         done
3620
3621         if $all_zeros
3622         then
3623                 for ostnum in $(seq $OSTCOUNT); do
3624                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3625                         echo "Check that write_bytes is present in obdfilter/*/stats:"
3626                         do_facet ost$ostnum lctl get_param -n \
3627                                 obdfilter/$ostname/stats
3628                 done
3629                 error "OST not keeping write_bytes stats (b22312)"
3630         fi
3631 }
3632 run_test 33c "test llobdstat and write_bytes"
3633
3634 test_33d() {
3635         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3636         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3637
3638         local MDTIDX=1
3639         local remote_dir=$DIR/$tdir/remote_dir
3640
3641         test_mkdir $DIR/$tdir
3642         $LFS mkdir -i $MDTIDX $remote_dir ||
3643                 error "create remote directory failed"
3644
3645         touch $remote_dir/$tfile
3646         chmod 444 $remote_dir/$tfile
3647         chown $RUNAS_ID $remote_dir/$tfile
3648
3649         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3650
3651         chown $RUNAS_ID $remote_dir
3652         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
3653                                         error "create" || true
3654         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
3655                                     error "open RDWR" || true
3656         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
3657 }
3658 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
3659
3660 test_33e() {
3661         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3662
3663         mkdir $DIR/$tdir
3664
3665         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3666         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3667         mkdir $DIR/$tdir/local_dir
3668
3669         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3670         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3671         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3672
3673         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3674                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
3675
3676         rmdir $DIR/$tdir/* || error "rmdir failed"
3677
3678         umask 777
3679         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3680         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3681         mkdir $DIR/$tdir/local_dir
3682
3683         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3684         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3685         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3686
3687         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3688                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3689
3690         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3691
3692         umask 000
3693         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3694         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3695         mkdir $DIR/$tdir/local_dir
3696
3697         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3698         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3699         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3700
3701         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3702                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3703 }
3704 run_test 33e "mkdir and striped directory should have same mode"
3705
3706 cleanup_33f() {
3707         trap 0
3708         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3709 }
3710
3711 test_33f() {
3712         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3713         remote_mds_nodsh && skip "remote MDS with nodsh"
3714
3715         mkdir $DIR/$tdir
3716         chmod go+rwx $DIR/$tdir
3717         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3718         trap cleanup_33f EXIT
3719
3720         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3721                 error "cannot create striped directory"
3722
3723         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3724                 error "cannot create files in striped directory"
3725
3726         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3727                 error "cannot remove files in striped directory"
3728
3729         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3730                 error "cannot remove striped directory"
3731
3732         cleanup_33f
3733 }
3734 run_test 33f "nonroot user can create, access, and remove a striped directory"
3735
3736 test_33g() {
3737         mkdir -p $DIR/$tdir/dir2
3738
3739         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3740         echo $err
3741         [[ $err =~ "exists" ]] || error "Not exists error"
3742 }
3743 run_test 33g "nonroot user create already existing root created file"
3744
3745 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3746 test_34a() {
3747         rm -f $DIR/f34
3748         $MCREATE $DIR/f34 || error "mcreate failed"
3749         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3750                 error "getstripe failed"
3751         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3752         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3753                 error "getstripe failed"
3754         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3755                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3756 }
3757 run_test 34a "truncate file that has not been opened ==========="
3758
3759 test_34b() {
3760         [ ! -f $DIR/f34 ] && test_34a
3761         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3762                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3763         $OPENFILE -f O_RDONLY $DIR/f34
3764         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3765                 error "getstripe failed"
3766         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3767                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3768 }
3769 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3770
3771 test_34c() {
3772         [ ! -f $DIR/f34 ] && test_34a
3773         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3774                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3775         $OPENFILE -f O_RDWR $DIR/f34
3776         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" &&
3777                 error "$LFS getstripe failed"
3778         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3779                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3780 }
3781 run_test 34c "O_RDWR opening file-with-size works =============="
3782
3783 test_34d() {
3784         [ ! -f $DIR/f34 ] && test_34a
3785         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3786                 error "dd failed"
3787         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3788                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3789         rm $DIR/f34
3790 }
3791 run_test 34d "write to sparse file ============================="
3792
3793 test_34e() {
3794         rm -f $DIR/f34e
3795         $MCREATE $DIR/f34e || error "mcreate failed"
3796         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3797         $CHECKSTAT -s 1000 $DIR/f34e ||
3798                 error "Size of $DIR/f34e not equal to 1000 bytes"
3799         $OPENFILE -f O_RDWR $DIR/f34e
3800         $CHECKSTAT -s 1000 $DIR/f34e ||
3801                 error "Size of $DIR/f34e not equal to 1000 bytes"
3802 }
3803 run_test 34e "create objects, some with size and some without =="
3804
3805 test_34f() { # bug 6242, 6243
3806         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3807
3808         SIZE34F=48000
3809         rm -f $DIR/f34f
3810         $MCREATE $DIR/f34f || error "mcreate failed"
3811         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3812         dd if=$DIR/f34f of=$TMP/f34f
3813         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3814         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3815         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3816         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3817         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3818 }
3819 run_test 34f "read from a file with no objects until EOF ======="
3820
3821 test_34g() {
3822         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3823
3824         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3825                 error "dd failed"
3826         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3827         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3828                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3829         cancel_lru_locks osc
3830         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3831                 error "wrong size after lock cancel"
3832
3833         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3834         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3835                 error "expanding truncate failed"
3836         cancel_lru_locks osc
3837         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3838                 error "wrong expanded size after lock cancel"
3839 }
3840 run_test 34g "truncate long file ==============================="
3841
3842 test_34h() {
3843         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3844
3845         local gid=10
3846         local sz=1000
3847
3848         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3849         sync # Flush the cache so that multiop below does not block on cache
3850              # flush when getting the group lock
3851         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3852         MULTIPID=$!
3853
3854         # Since just timed wait is not good enough, let's do a sync write
3855         # that way we are sure enough time for a roundtrip + processing
3856         # passed + 2 seconds of extra margin.
3857         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
3858         rm $DIR/${tfile}-1
3859         sleep 2
3860
3861         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3862                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3863                 kill -9 $MULTIPID
3864         fi
3865         wait $MULTIPID
3866         local nsz=`stat -c %s $DIR/$tfile`
3867         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3868 }
3869 run_test 34h "ftruncate file under grouplock should not block"
3870
3871 test_35a() {
3872         cp /bin/sh $DIR/f35a
3873         chmod 444 $DIR/f35a
3874         chown $RUNAS_ID $DIR/f35a
3875         $RUNAS $DIR/f35a && error || true
3876         rm $DIR/f35a
3877 }
3878 run_test 35a "exec file with mode 444 (should return and not leak)"
3879
3880 test_36a() {
3881         rm -f $DIR/f36
3882         utime $DIR/f36 || error "utime failed for MDS"
3883 }
3884 run_test 36a "MDS utime check (mknod, utime)"
3885
3886 test_36b() {
3887         echo "" > $DIR/f36
3888         utime $DIR/f36 || error "utime failed for OST"
3889 }
3890 run_test 36b "OST utime check (open, utime)"
3891
3892 test_36c() {
3893         rm -f $DIR/d36/f36
3894         test_mkdir $DIR/d36
3895         chown $RUNAS_ID $DIR/d36
3896         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3897 }
3898 run_test 36c "non-root MDS utime check (mknod, utime)"
3899
3900 test_36d() {
3901         [ ! -d $DIR/d36 ] && test_36c
3902         echo "" > $DIR/d36/f36
3903         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3904 }
3905 run_test 36d "non-root OST utime check (open, utime)"
3906
3907 test_36e() {
3908         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3909
3910         test_mkdir $DIR/$tdir
3911         touch $DIR/$tdir/$tfile
3912         $RUNAS utime $DIR/$tdir/$tfile &&
3913                 error "utime worked, expected failure" || true
3914 }
3915 run_test 36e "utime on non-owned file (should return error)"
3916
3917 subr_36fh() {
3918         local fl="$1"
3919         local LANG_SAVE=$LANG
3920         local LC_LANG_SAVE=$LC_LANG
3921         export LANG=C LC_LANG=C # for date language
3922
3923         DATESTR="Dec 20  2000"
3924         test_mkdir $DIR/$tdir
3925         lctl set_param fail_loc=$fl
3926         date; date +%s
3927         cp /etc/hosts $DIR/$tdir/$tfile
3928         sync & # write RPC generated with "current" inode timestamp, but delayed
3929         sleep 1
3930         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3931         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3932         cancel_lru_locks $OSC
3933         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3934         date; date +%s
3935         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3936                 echo "BEFORE: $LS_BEFORE" && \
3937                 echo "AFTER : $LS_AFTER" && \
3938                 echo "WANT  : $DATESTR" && \
3939                 error "$DIR/$tdir/$tfile timestamps changed" || true
3940
3941         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3942 }
3943
3944 test_36f() {
3945         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3946
3947         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3948         subr_36fh "0x80000214"
3949 }
3950 run_test 36f "utime on file racing with OST BRW write =========="
3951
3952 test_36g() {
3953         remote_ost_nodsh && skip "remote OST with nodsh"
3954         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3955         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
3956                 skip "Need MDS version at least 2.12.51"
3957
3958         local fmd_max_age
3959         local fmd
3960         local facet="ost1"
3961         local tgt="obdfilter"
3962
3963         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
3964
3965         test_mkdir $DIR/$tdir
3966         fmd_max_age=$(do_facet $facet \
3967                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
3968                 head -n 1")
3969
3970         echo "FMD max age: ${fmd_max_age}s"
3971         touch $DIR/$tdir/$tfile
3972         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3973                 gawk '{cnt=cnt+$1}  END{print cnt}')
3974         echo "FMD before: $fmd"
3975         [[ $fmd == 0 ]] &&
3976                 error "FMD wasn't create by touch"
3977         sleep $((fmd_max_age + 12))
3978         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3979                 gawk '{cnt=cnt+$1}  END{print cnt}')
3980         echo "FMD after: $fmd"
3981         [[ $fmd == 0 ]] ||
3982                 error "FMD wasn't expired by ping"
3983 }
3984 run_test 36g "FMD cache expiry ====================="
3985
3986 test_36h() {
3987         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3988
3989         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3990         subr_36fh "0x80000227"
3991 }
3992 run_test 36h "utime on file racing with OST BRW write =========="
3993
3994 test_36i() {
3995         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3996
3997         test_mkdir $DIR/$tdir
3998         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3999
4000         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
4001         local new_mtime=$((mtime + 200))
4002
4003         #change Modify time of striped dir
4004         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
4005                         error "change mtime failed"
4006
4007         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
4008
4009         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
4010 }
4011 run_test 36i "change mtime on striped directory"
4012
4013 # test_37 - duplicate with tests 32q 32r
4014
4015 test_38() {
4016         local file=$DIR/$tfile
4017         touch $file
4018         openfile -f O_DIRECTORY $file
4019         local RC=$?
4020         local ENOTDIR=20
4021         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
4022         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
4023 }
4024 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
4025
4026 test_39a() { # was test_39
4027         touch $DIR/$tfile
4028         touch $DIR/${tfile}2
4029 #       ls -l  $DIR/$tfile $DIR/${tfile}2
4030 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
4031 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
4032         sleep 2
4033         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
4034         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
4035                 echo "mtime"
4036                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
4037                 echo "atime"
4038                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
4039                 echo "ctime"
4040                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
4041                 error "O_TRUNC didn't change timestamps"
4042         fi
4043 }
4044 run_test 39a "mtime changed on create"
4045
4046 test_39b() {
4047         test_mkdir -c1 $DIR/$tdir
4048         cp -p /etc/passwd $DIR/$tdir/fopen
4049         cp -p /etc/passwd $DIR/$tdir/flink
4050         cp -p /etc/passwd $DIR/$tdir/funlink
4051         cp -p /etc/passwd $DIR/$tdir/frename
4052         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
4053
4054         sleep 1
4055         echo "aaaaaa" >> $DIR/$tdir/fopen
4056         echo "aaaaaa" >> $DIR/$tdir/flink
4057         echo "aaaaaa" >> $DIR/$tdir/funlink
4058         echo "aaaaaa" >> $DIR/$tdir/frename
4059
4060         local open_new=`stat -c %Y $DIR/$tdir/fopen`
4061         local link_new=`stat -c %Y $DIR/$tdir/flink`
4062         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
4063         local rename_new=`stat -c %Y $DIR/$tdir/frename`
4064
4065         cat $DIR/$tdir/fopen > /dev/null
4066         ln $DIR/$tdir/flink $DIR/$tdir/flink2
4067         rm -f $DIR/$tdir/funlink2
4068         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
4069
4070         for (( i=0; i < 2; i++ )) ; do
4071                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
4072                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
4073                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
4074                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
4075
4076                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
4077                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
4078                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
4079                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
4080
4081                 cancel_lru_locks $OSC
4082                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4083         done
4084 }
4085 run_test 39b "mtime change on open, link, unlink, rename  ======"
4086
4087 # this should be set to past
4088 TEST_39_MTIME=`date -d "1 year ago" +%s`
4089
4090 # bug 11063
4091 test_39c() {
4092         touch $DIR1/$tfile
4093         sleep 2
4094         local mtime0=`stat -c %Y $DIR1/$tfile`
4095
4096         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4097         local mtime1=`stat -c %Y $DIR1/$tfile`
4098         [ "$mtime1" = $TEST_39_MTIME ] || \
4099                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
4100
4101         local d1=`date +%s`
4102         echo hello >> $DIR1/$tfile
4103         local d2=`date +%s`
4104         local mtime2=`stat -c %Y $DIR1/$tfile`
4105         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
4106                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
4107
4108         mv $DIR1/$tfile $DIR1/$tfile-1
4109
4110         for (( i=0; i < 2; i++ )) ; do
4111                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
4112                 [ "$mtime2" = "$mtime3" ] || \
4113                         error "mtime ($mtime2) changed (to $mtime3) on rename"
4114
4115                 cancel_lru_locks $OSC
4116                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4117         done
4118 }
4119 run_test 39c "mtime change on rename ==========================="
4120
4121 # bug 21114
4122 test_39d() {
4123         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4124
4125         touch $DIR1/$tfile
4126         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4127
4128         for (( i=0; i < 2; i++ )) ; do
4129                 local mtime=`stat -c %Y $DIR1/$tfile`
4130                 [ $mtime = $TEST_39_MTIME ] || \
4131                         error "mtime($mtime) is not set to $TEST_39_MTIME"
4132
4133                 cancel_lru_locks $OSC
4134                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4135         done
4136 }
4137 run_test 39d "create, utime, stat =============================="
4138
4139 # bug 21114
4140 test_39e() {
4141         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4142
4143         touch $DIR1/$tfile
4144         local mtime1=`stat -c %Y $DIR1/$tfile`
4145
4146         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4147
4148         for (( i=0; i < 2; i++ )) ; do
4149                 local mtime2=`stat -c %Y $DIR1/$tfile`
4150                 [ $mtime2 = $TEST_39_MTIME ] || \
4151                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4152
4153                 cancel_lru_locks $OSC
4154                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4155         done
4156 }
4157 run_test 39e "create, stat, utime, stat ========================"
4158
4159 # bug 21114
4160 test_39f() {
4161         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4162
4163         touch $DIR1/$tfile
4164         mtime1=`stat -c %Y $DIR1/$tfile`
4165
4166         sleep 2
4167         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4168
4169         for (( i=0; i < 2; i++ )) ; do
4170                 local mtime2=`stat -c %Y $DIR1/$tfile`
4171                 [ $mtime2 = $TEST_39_MTIME ] || \
4172                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4173
4174                 cancel_lru_locks $OSC
4175                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4176         done
4177 }
4178 run_test 39f "create, stat, sleep, utime, stat ================="
4179
4180 # bug 11063
4181 test_39g() {
4182         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4183
4184         echo hello >> $DIR1/$tfile
4185         local mtime1=`stat -c %Y $DIR1/$tfile`
4186
4187         sleep 2
4188         chmod o+r $DIR1/$tfile
4189
4190         for (( i=0; i < 2; i++ )) ; do
4191                 local mtime2=`stat -c %Y $DIR1/$tfile`
4192                 [ "$mtime1" = "$mtime2" ] || \
4193                         error "lost mtime: $mtime2, should be $mtime1"
4194
4195                 cancel_lru_locks $OSC
4196                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4197         done
4198 }
4199 run_test 39g "write, chmod, stat ==============================="
4200
4201 # bug 11063
4202 test_39h() {
4203         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4204
4205         touch $DIR1/$tfile
4206         sleep 1
4207
4208         local d1=`date`
4209         echo hello >> $DIR1/$tfile
4210         local mtime1=`stat -c %Y $DIR1/$tfile`
4211
4212         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4213         local d2=`date`
4214         if [ "$d1" != "$d2" ]; then
4215                 echo "write and touch not within one second"
4216         else
4217                 for (( i=0; i < 2; i++ )) ; do
4218                         local mtime2=`stat -c %Y $DIR1/$tfile`
4219                         [ "$mtime2" = $TEST_39_MTIME ] || \
4220                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
4221
4222                         cancel_lru_locks $OSC
4223                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4224                 done
4225         fi
4226 }
4227 run_test 39h "write, utime within one second, stat ============="
4228
4229 test_39i() {
4230         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4231
4232         touch $DIR1/$tfile
4233         sleep 1
4234
4235         echo hello >> $DIR1/$tfile
4236         local mtime1=`stat -c %Y $DIR1/$tfile`
4237
4238         mv $DIR1/$tfile $DIR1/$tfile-1
4239
4240         for (( i=0; i < 2; i++ )) ; do
4241                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4242
4243                 [ "$mtime1" = "$mtime2" ] || \
4244                         error "lost mtime: $mtime2, should be $mtime1"
4245
4246                 cancel_lru_locks $OSC
4247                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4248         done
4249 }
4250 run_test 39i "write, rename, stat =============================="
4251
4252 test_39j() {
4253         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4254
4255         start_full_debug_logging
4256         touch $DIR1/$tfile
4257         sleep 1
4258
4259         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
4260         lctl set_param fail_loc=0x80000412
4261         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
4262                 error "multiop failed"
4263         local multipid=$!
4264         local mtime1=`stat -c %Y $DIR1/$tfile`
4265
4266         mv $DIR1/$tfile $DIR1/$tfile-1
4267
4268         kill -USR1 $multipid
4269         wait $multipid || error "multiop close failed"
4270
4271         for (( i=0; i < 2; i++ )) ; do
4272                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4273                 [ "$mtime1" = "$mtime2" ] ||
4274                         error "mtime is lost on close: $mtime2, " \
4275                               "should be $mtime1"
4276
4277                 cancel_lru_locks
4278                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4279         done
4280         lctl set_param fail_loc=0
4281         stop_full_debug_logging
4282 }
4283 run_test 39j "write, rename, close, stat ======================="
4284
4285 test_39k() {
4286         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4287
4288         touch $DIR1/$tfile
4289         sleep 1
4290
4291         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
4292         local multipid=$!
4293         local mtime1=`stat -c %Y $DIR1/$tfile`
4294
4295         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4296
4297         kill -USR1 $multipid
4298         wait $multipid || error "multiop close failed"
4299
4300         for (( i=0; i < 2; i++ )) ; do
4301                 local mtime2=`stat -c %Y $DIR1/$tfile`
4302
4303                 [ "$mtime2" = $TEST_39_MTIME ] || \
4304                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
4305
4306                 cancel_lru_locks
4307                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4308         done
4309 }
4310 run_test 39k "write, utime, close, stat ========================"
4311
4312 # this should be set to future
4313 TEST_39_ATIME=`date -d "1 year" +%s`
4314
4315 test_39l() {
4316         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4317         remote_mds_nodsh && skip "remote MDS with nodsh"
4318
4319         local atime_diff=$(do_facet $SINGLEMDS \
4320                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4321         rm -rf $DIR/$tdir
4322         mkdir -p $DIR/$tdir
4323
4324         # test setting directory atime to future
4325         touch -a -d @$TEST_39_ATIME $DIR/$tdir
4326         local atime=$(stat -c %X $DIR/$tdir)
4327         [ "$atime" = $TEST_39_ATIME ] ||
4328                 error "atime is not set to future: $atime, $TEST_39_ATIME"
4329
4330         # test setting directory atime from future to now
4331         local now=$(date +%s)
4332         touch -a -d @$now $DIR/$tdir
4333
4334         atime=$(stat -c %X $DIR/$tdir)
4335         [ "$atime" -eq "$now"  ] ||
4336                 error "atime is not updated from future: $atime, $now"
4337
4338         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
4339         sleep 3
4340
4341         # test setting directory atime when now > dir atime + atime_diff
4342         local d1=$(date +%s)
4343         ls $DIR/$tdir
4344         local d2=$(date +%s)
4345         cancel_lru_locks mdc
4346         atime=$(stat -c %X $DIR/$tdir)
4347         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4348                 error "atime is not updated  : $atime, should be $d2"
4349
4350         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
4351         sleep 3
4352
4353         # test not setting directory atime when now < dir atime + atime_diff
4354         ls $DIR/$tdir
4355         cancel_lru_locks mdc
4356         atime=$(stat -c %X $DIR/$tdir)
4357         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4358                 error "atime is updated to $atime, should remain $d1<atime<$d2"
4359
4360         do_facet $SINGLEMDS \
4361                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4362 }
4363 run_test 39l "directory atime update ==========================="
4364
4365 test_39m() {
4366         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4367
4368         touch $DIR1/$tfile
4369         sleep 2
4370         local far_past_mtime=$(date -d "May 29 1953" +%s)
4371         local far_past_atime=$(date -d "Dec 17 1903" +%s)
4372
4373         touch -m -d @$far_past_mtime $DIR1/$tfile
4374         touch -a -d @$far_past_atime $DIR1/$tfile
4375
4376         for (( i=0; i < 2; i++ )) ; do
4377                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
4378                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
4379                         error "atime or mtime set incorrectly"
4380
4381                 cancel_lru_locks $OSC
4382                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4383         done
4384 }
4385 run_test 39m "test atime and mtime before 1970"
4386
4387 test_39n() { # LU-3832
4388         remote_mds_nodsh && skip "remote MDS with nodsh"
4389
4390         local atime_diff=$(do_facet $SINGLEMDS \
4391                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4392         local atime0
4393         local atime1
4394         local atime2
4395
4396         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
4397
4398         rm -rf $DIR/$tfile
4399         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
4400         atime0=$(stat -c %X $DIR/$tfile)
4401
4402         sleep 5
4403         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4404         atime1=$(stat -c %X $DIR/$tfile)
4405
4406         sleep 5
4407         cancel_lru_locks mdc
4408         cancel_lru_locks osc
4409         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4410         atime2=$(stat -c %X $DIR/$tfile)
4411
4412         do_facet $SINGLEMDS \
4413                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4414
4415         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
4416         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
4417 }
4418 run_test 39n "check that O_NOATIME is honored"
4419
4420 test_39o() {
4421         TESTDIR=$DIR/$tdir/$tfile
4422         [ -e $TESTDIR ] && rm -rf $TESTDIR
4423         mkdir -p $TESTDIR
4424         cd $TESTDIR
4425         links1=2
4426         ls
4427         mkdir a b
4428         ls
4429         links2=$(stat -c %h .)
4430         [ $(($links1 + 2)) != $links2 ] &&
4431                 error "wrong links count $(($links1 + 2)) != $links2"
4432         rmdir b
4433         links3=$(stat -c %h .)
4434         [ $(($links1 + 1)) != $links3 ] &&
4435                 error "wrong links count $links1 != $links3"
4436         return 0
4437 }
4438 run_test 39o "directory cached attributes updated after create"
4439
4440 test_39p() {
4441         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4442
4443         local MDTIDX=1
4444         TESTDIR=$DIR/$tdir/$tdir
4445         [ -e $TESTDIR ] && rm -rf $TESTDIR
4446         test_mkdir -p $TESTDIR
4447         cd $TESTDIR
4448         links1=2
4449         ls
4450         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
4451         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
4452         ls
4453         links2=$(stat -c %h .)
4454         [ $(($links1 + 2)) != $links2 ] &&
4455                 error "wrong links count $(($links1 + 2)) != $links2"
4456         rmdir remote_dir2
4457         links3=$(stat -c %h .)
4458         [ $(($links1 + 1)) != $links3 ] &&
4459                 error "wrong links count $links1 != $links3"
4460         return 0
4461 }
4462 run_test 39p "remote directory cached attributes updated after create ========"
4463
4464
4465 test_39q() { # LU-8041
4466         local testdir=$DIR/$tdir
4467         mkdir -p $testdir
4468         multiop_bg_pause $testdir D_c || error "multiop failed"
4469         local multipid=$!
4470         cancel_lru_locks mdc
4471         kill -USR1 $multipid
4472         local atime=$(stat -c %X $testdir)
4473         [ "$atime" -ne 0 ] || error "atime is zero"
4474 }
4475 run_test 39q "close won't zero out atime"
4476
4477 test_40() {
4478         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
4479         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
4480                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
4481         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
4482                 error "$tfile is not 4096 bytes in size"
4483 }
4484 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
4485
4486 test_41() {
4487         # bug 1553
4488         small_write $DIR/f41 18
4489 }
4490 run_test 41 "test small file write + fstat ====================="
4491
4492 count_ost_writes() {
4493         lctl get_param -n ${OSC}.*.stats |
4494                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
4495                         END { printf("%0.0f", writes) }'
4496 }
4497
4498 # decent default
4499 WRITEBACK_SAVE=500
4500 DIRTY_RATIO_SAVE=40
4501 MAX_DIRTY_RATIO=50
4502 BG_DIRTY_RATIO_SAVE=10
4503 MAX_BG_DIRTY_RATIO=25
4504
4505 start_writeback() {
4506         trap 0
4507         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
4508         # dirty_ratio, dirty_background_ratio
4509         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4510                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
4511                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
4512                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
4513         else
4514                 # if file not here, we are a 2.4 kernel
4515                 kill -CONT `pidof kupdated`
4516         fi
4517 }
4518
4519 stop_writeback() {
4520         # setup the trap first, so someone cannot exit the test at the
4521         # exact wrong time and mess up a machine
4522         trap start_writeback EXIT
4523         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
4524         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4525                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
4526                 sysctl -w vm.dirty_writeback_centisecs=0
4527                 sysctl -w vm.dirty_writeback_centisecs=0
4528                 # save and increase /proc/sys/vm/dirty_ratio
4529                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
4530                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
4531                 # save and increase /proc/sys/vm/dirty_background_ratio
4532                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
4533                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
4534         else
4535                 # if file not here, we are a 2.4 kernel
4536                 kill -STOP `pidof kupdated`
4537         fi
4538 }
4539
4540 # ensure that all stripes have some grant before we test client-side cache
4541 setup_test42() {
4542         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
4543                 dd if=/dev/zero of=$i bs=4k count=1
4544                 rm $i
4545         done
4546 }
4547
4548 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
4549 # file truncation, and file removal.
4550 test_42a() {
4551         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4552
4553         setup_test42
4554         cancel_lru_locks $OSC
4555         stop_writeback
4556         sync; sleep 1; sync # just to be safe
4557         BEFOREWRITES=`count_ost_writes`
4558         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
4559         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
4560         AFTERWRITES=`count_ost_writes`
4561         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
4562                 error "$BEFOREWRITES < $AFTERWRITES"
4563         start_writeback
4564 }
4565 run_test 42a "ensure that we don't flush on close"
4566
4567 test_42b() {
4568         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4569
4570         setup_test42
4571         cancel_lru_locks $OSC
4572         stop_writeback
4573         sync
4574         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
4575         BEFOREWRITES=$(count_ost_writes)
4576         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
4577         AFTERWRITES=$(count_ost_writes)
4578         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4579                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
4580         fi
4581         BEFOREWRITES=$(count_ost_writes)
4582         sync || error "sync: $?"
4583         AFTERWRITES=$(count_ost_writes)
4584         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4585                 error "$BEFOREWRITES < $AFTERWRITES on sync"
4586         fi
4587         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
4588         start_writeback
4589         return 0
4590 }
4591 run_test 42b "test destroy of file with cached dirty data ======"
4592
4593 # if these tests just want to test the effect of truncation,
4594 # they have to be very careful.  consider:
4595 # - the first open gets a {0,EOF}PR lock
4596 # - the first write conflicts and gets a {0, count-1}PW
4597 # - the rest of the writes are under {count,EOF}PW
4598 # - the open for truncate tries to match a {0,EOF}PR
4599 #   for the filesize and cancels the PWs.
4600 # any number of fixes (don't get {0,EOF} on open, match
4601 # composite locks, do smarter file size management) fix
4602 # this, but for now we want these tests to verify that
4603 # the cancellation with truncate intent works, so we
4604 # start the file with a full-file pw lock to match against
4605 # until the truncate.
4606 trunc_test() {
4607         test=$1
4608         file=$DIR/$test
4609         offset=$2
4610         cancel_lru_locks $OSC
4611         stop_writeback
4612         # prime the file with 0,EOF PW to match
4613         touch $file
4614         $TRUNCATE $file 0
4615         sync; sync
4616         # now the real test..
4617         dd if=/dev/zero of=$file bs=1024 count=100
4618         BEFOREWRITES=`count_ost_writes`
4619         $TRUNCATE $file $offset
4620         cancel_lru_locks $OSC
4621         AFTERWRITES=`count_ost_writes`
4622         start_writeback
4623 }
4624
4625 test_42c() {
4626         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4627
4628         trunc_test 42c 1024
4629         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
4630                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
4631         rm $file
4632 }
4633 run_test 42c "test partial truncate of file with cached dirty data"
4634
4635 test_42d() {
4636         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4637
4638         trunc_test 42d 0
4639         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
4640                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
4641         rm $file
4642 }
4643 run_test 42d "test complete truncate of file with cached dirty data"
4644
4645 test_42e() { # bug22074
4646         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4647
4648         local TDIR=$DIR/${tdir}e
4649         local pages=16 # hardcoded 16 pages, don't change it.
4650         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
4651         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
4652         local max_dirty_mb
4653         local warmup_files
4654
4655         test_mkdir $DIR/${tdir}e
4656         $LFS setstripe -c 1 $TDIR
4657         createmany -o $TDIR/f $files
4658
4659         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
4660
4661         # we assume that with $OSTCOUNT files, at least one of them will
4662         # be allocated on OST0.
4663         warmup_files=$((OSTCOUNT * max_dirty_mb))
4664         createmany -o $TDIR/w $warmup_files
4665
4666         # write a large amount of data into one file and sync, to get good
4667         # avail_grant number from OST.
4668         for ((i=0; i<$warmup_files; i++)); do
4669                 idx=$($LFS getstripe -i $TDIR/w$i)
4670                 [ $idx -ne 0 ] && continue
4671                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
4672                 break
4673         done
4674         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
4675         sync
4676         $LCTL get_param $proc_osc0/cur_dirty_bytes
4677         $LCTL get_param $proc_osc0/cur_grant_bytes
4678
4679         # create as much dirty pages as we can while not to trigger the actual
4680         # RPCs directly. but depends on the env, VFS may trigger flush during this
4681         # period, hopefully we are good.
4682         for ((i=0; i<$warmup_files; i++)); do
4683                 idx=$($LFS getstripe -i $TDIR/w$i)
4684                 [ $idx -ne 0 ] && continue
4685                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4686         done
4687         $LCTL get_param $proc_osc0/cur_dirty_bytes
4688         $LCTL get_param $proc_osc0/cur_grant_bytes
4689
4690         # perform the real test
4691         $LCTL set_param $proc_osc0/rpc_stats 0
4692         for ((;i<$files; i++)); do
4693                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
4694                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
4695         done
4696         sync
4697         $LCTL get_param $proc_osc0/rpc_stats
4698
4699         local percent=0
4700         local have_ppr=false
4701         $LCTL get_param $proc_osc0/rpc_stats |
4702                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4703                         # skip lines until we are at the RPC histogram data
4704                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4705                         $have_ppr || continue
4706
4707                         # we only want the percent stat for < 16 pages
4708                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4709
4710                         percent=$((percent + WPCT))
4711                         if [[ $percent -gt 15 ]]; then
4712                                 error "less than 16-pages write RPCs" \
4713                                       "$percent% > 15%"
4714                                 break
4715                         fi
4716                 done
4717         rm -rf $TDIR
4718 }
4719 run_test 42e "verify sub-RPC writes are not done synchronously"
4720
4721 test_43A() { # was test_43
4722         test_mkdir $DIR/$tdir
4723         cp -p /bin/ls $DIR/$tdir/$tfile
4724         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4725         pid=$!
4726         # give multiop a chance to open
4727         sleep 1
4728
4729         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4730         kill -USR1 $pid
4731 }
4732 run_test 43A "execution of file opened for write should return -ETXTBSY"
4733
4734 test_43a() {
4735         test_mkdir $DIR/$tdir
4736         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4737         $DIR/$tdir/sleep 60 &
4738         SLEEP_PID=$!
4739         # Make sure exec of $tdir/sleep wins race with truncate
4740         sleep 1
4741         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
4742         kill $SLEEP_PID
4743 }
4744 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4745
4746 test_43b() {
4747         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4748
4749         test_mkdir $DIR/$tdir
4750         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4751         $DIR/$tdir/sleep 60 &
4752         SLEEP_PID=$!
4753         # Make sure exec of $tdir/sleep wins race with truncate
4754         sleep 1
4755         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
4756         kill $SLEEP_PID
4757 }
4758 run_test 43b "truncate of file being executed should return -ETXTBSY"
4759
4760 test_43c() {
4761         local testdir="$DIR/$tdir"
4762         test_mkdir $testdir
4763         cp $SHELL $testdir/
4764         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4765                 ( cd $testdir && md5sum -c )
4766 }
4767 run_test 43c "md5sum of copy into lustre"
4768
4769 test_44A() { # was test_44
4770         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4771
4772         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4773         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4774 }
4775 run_test 44A "zero length read from a sparse stripe"
4776
4777 test_44a() {
4778         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4779                 awk '{ print $2 }')
4780         [ -z "$nstripe" ] && skip "can't get stripe info"
4781         [[ $nstripe -gt $OSTCOUNT ]] &&
4782                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4783
4784         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4785                 awk '{ print $2 }')
4786         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4787                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4788                         awk '{ print $2 }')
4789         fi
4790
4791         OFFSETS="0 $((stride/2)) $((stride-1))"
4792         for offset in $OFFSETS; do
4793                 for i in $(seq 0 $((nstripe-1))); do
4794                         local GLOBALOFFSETS=""
4795                         # size in Bytes
4796                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4797                         local myfn=$DIR/d44a-$size
4798                         echo "--------writing $myfn at $size"
4799                         ll_sparseness_write $myfn $size ||
4800                                 error "ll_sparseness_write"
4801                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4802                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4803                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4804
4805                         for j in $(seq 0 $((nstripe-1))); do
4806                                 # size in Bytes
4807                                 size=$((((j + $nstripe )*$stride + $offset)))
4808                                 ll_sparseness_write $myfn $size ||
4809                                         error "ll_sparseness_write"
4810                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4811                         done
4812                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4813                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4814                         rm -f $myfn
4815                 done
4816         done
4817 }
4818 run_test 44a "test sparse pwrite ==============================="
4819
4820 dirty_osc_total() {
4821         tot=0
4822         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4823                 tot=$(($tot + $d))
4824         done
4825         echo $tot
4826 }
4827 do_dirty_record() {
4828         before=`dirty_osc_total`
4829         echo executing "\"$*\""
4830         eval $*
4831         after=`dirty_osc_total`
4832         echo before $before, after $after
4833 }
4834 test_45() {
4835         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4836
4837         f="$DIR/f45"
4838         # Obtain grants from OST if it supports it
4839         echo blah > ${f}_grant
4840         stop_writeback
4841         sync
4842         do_dirty_record "echo blah > $f"
4843         [[ $before -eq $after ]] && error "write wasn't cached"
4844         do_dirty_record "> $f"
4845         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4846         do_dirty_record "echo blah > $f"
4847         [[ $before -eq $after ]] && error "write wasn't cached"
4848         do_dirty_record "sync"
4849         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4850         do_dirty_record "echo blah > $f"
4851         [[ $before -eq $after ]] && error "write wasn't cached"
4852         do_dirty_record "cancel_lru_locks osc"
4853         [[ $before -gt $after ]] ||
4854                 error "lock cancellation didn't lower dirty count"
4855         start_writeback
4856 }
4857 run_test 45 "osc io page accounting ============================"
4858
4859 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4860 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4861 # objects offset and an assert hit when an rpc was built with 1023's mapped
4862 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4863 test_46() {
4864         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4865
4866         f="$DIR/f46"
4867         stop_writeback
4868         sync
4869         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4870         sync
4871         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
4872         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4873         sync
4874         start_writeback
4875 }
4876 run_test 46 "dirtying a previously written page ================"
4877
4878 # test_47 is removed "Device nodes check" is moved to test_28
4879
4880 test_48a() { # bug 2399
4881         [ "$mds1_FSTYPE" = "zfs" ] &&
4882         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
4883                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
4884
4885         test_mkdir $DIR/$tdir
4886         cd $DIR/$tdir
4887         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
4888         test_mkdir $DIR/$tdir
4889         touch foo || error "'touch foo' failed after recreating cwd"
4890         test_mkdir bar
4891         touch .foo || error "'touch .foo' failed after recreating cwd"
4892         test_mkdir .bar
4893         ls . > /dev/null || error "'ls .' failed after recreating cwd"
4894         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4895         cd . || error "'cd .' failed after recreating cwd"
4896         mkdir . && error "'mkdir .' worked after recreating cwd"
4897         rmdir . && error "'rmdir .' worked after recreating cwd"
4898         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4899         cd .. || error "'cd ..' failed after recreating cwd"
4900 }
4901 run_test 48a "Access renamed working dir (should return errors)="
4902
4903 test_48b() { # bug 2399
4904         rm -rf $DIR/$tdir
4905         test_mkdir $DIR/$tdir
4906         cd $DIR/$tdir
4907         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4908         touch foo && error "'touch foo' worked after removing cwd"
4909         mkdir foo && error "'mkdir foo' worked after removing cwd"
4910         touch .foo && error "'touch .foo' worked after removing cwd"
4911         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4912         ls . > /dev/null && error "'ls .' worked after removing cwd"
4913         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4914         mkdir . && error "'mkdir .' worked after removing cwd"
4915         rmdir . && error "'rmdir .' worked after removing cwd"
4916         ln -s . foo && error "'ln -s .' worked after removing cwd"
4917         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4918 }
4919 run_test 48b "Access removed working dir (should return errors)="
4920
4921 test_48c() { # bug 2350
4922         #lctl set_param debug=-1
4923         #set -vx
4924         rm -rf $DIR/$tdir
4925         test_mkdir -p $DIR/$tdir/dir
4926         cd $DIR/$tdir/dir
4927         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4928         $TRACE touch foo && error "touch foo worked after removing cwd"
4929         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4930         touch .foo && error "touch .foo worked after removing cwd"
4931         mkdir .foo && error "mkdir .foo worked after removing cwd"
4932         $TRACE ls . && error "'ls .' worked after removing cwd"
4933         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4934         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4935         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4936         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4937         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4938 }
4939 run_test 48c "Access removed working subdir (should return errors)"
4940
4941 test_48d() { # bug 2350
4942         #lctl set_param debug=-1
4943         #set -vx
4944         rm -rf $DIR/$tdir
4945         test_mkdir -p $DIR/$tdir/dir
4946         cd $DIR/$tdir/dir
4947         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4948         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4949         $TRACE touch foo && error "'touch foo' worked after removing parent"
4950         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4951         touch .foo && error "'touch .foo' worked after removing parent"
4952         mkdir .foo && error "mkdir .foo worked after removing parent"
4953         $TRACE ls . && error "'ls .' worked after removing parent"
4954         $TRACE ls .. && error "'ls ..' worked after removing parent"
4955         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
4956         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4957         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4958         true
4959 }
4960 run_test 48d "Access removed parent subdir (should return errors)"
4961
4962 test_48e() { # bug 4134
4963         #lctl set_param debug=-1
4964         #set -vx
4965         rm -rf $DIR/$tdir
4966         test_mkdir -p $DIR/$tdir/dir
4967         cd $DIR/$tdir/dir
4968         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4969         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4970         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4971         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4972         # On a buggy kernel addition of "touch foo" after cd .. will
4973         # produce kernel oops in lookup_hash_it
4974         touch ../foo && error "'cd ..' worked after recreate parent"
4975         cd $DIR
4976         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4977 }
4978 run_test 48e "Access to recreated parent subdir (should return errors)"
4979
4980 test_49() { # LU-1030
4981         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4982         remote_ost_nodsh && skip "remote OST with nodsh"
4983
4984         # get ost1 size - $FSNAME-OST0000
4985         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4986                 awk '{ print $4 }')
4987         # write 800M at maximum
4988         [[ $ost1_size -lt 2 ]] && ost1_size=2
4989         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4990
4991         $LFS setstripe -c 1 -i 0 $DIR/$tfile
4992         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4993         local dd_pid=$!
4994
4995         # change max_pages_per_rpc while writing the file
4996         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4997         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4998         # loop until dd process exits
4999         while ps ax -opid | grep -wq $dd_pid; do
5000                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
5001                 sleep $((RANDOM % 5 + 1))
5002         done
5003         # restore original max_pages_per_rpc
5004         $LCTL set_param $osc1_mppc=$orig_mppc
5005         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
5006 }
5007 run_test 49 "Change max_pages_per_rpc won't break osc extent"
5008
5009 test_50() {
5010         # bug 1485
5011         test_mkdir $DIR/$tdir
5012         cd $DIR/$tdir
5013         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
5014 }
5015 run_test 50 "special situations: /proc symlinks  ==============="
5016
5017 test_51a() {    # was test_51
5018         # bug 1516 - create an empty entry right after ".." then split dir
5019         test_mkdir -c1 $DIR/$tdir
5020         touch $DIR/$tdir/foo
5021         $MCREATE $DIR/$tdir/bar
5022         rm $DIR/$tdir/foo
5023         createmany -m $DIR/$tdir/longfile 201
5024         FNUM=202
5025         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
5026                 $MCREATE $DIR/$tdir/longfile$FNUM
5027                 FNUM=$(($FNUM + 1))
5028                 echo -n "+"
5029         done
5030         echo
5031         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
5032 }
5033 run_test 51a "special situations: split htree with empty entry =="
5034
5035 cleanup_print_lfs_df () {
5036         trap 0
5037         $LFS df
5038         $LFS df -i
5039 }
5040
5041 test_51b() {
5042         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5043
5044         local dir=$DIR/$tdir
5045         local nrdirs=$((65536 + 100))
5046
5047         # cleanup the directory
5048         rm -fr $dir
5049
5050         test_mkdir -c1 $dir
5051
5052         $LFS df
5053         $LFS df -i
5054         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
5055         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
5056         [[ $numfree -lt $nrdirs ]] &&
5057                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
5058
5059         # need to check free space for the directories as well
5060         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
5061         numfree=$(( blkfree / $(fs_inode_ksize) ))
5062         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
5063
5064         trap cleanup_print_lfs_df EXIT
5065
5066         # create files
5067         createmany -d $dir/d $nrdirs || {
5068                 unlinkmany $dir/d $nrdirs
5069                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
5070         }
5071
5072         # really created :
5073         nrdirs=$(ls -U $dir | wc -l)
5074
5075         # unlink all but 100 subdirectories, then check it still works
5076         local left=100
5077         local delete=$((nrdirs - left))
5078
5079         $LFS df
5080         $LFS df -i
5081
5082         # for ldiskfs the nlink count should be 1, but this is OSD specific
5083         # and so this is listed for informational purposes only
5084         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
5085         unlinkmany -d $dir/d $delete ||
5086                 error "unlink of first $delete subdirs failed"
5087
5088         echo "nlink between: $(stat -c %h $dir)"
5089         local found=$(ls -U $dir | wc -l)
5090         [ $found -ne $left ] &&
5091                 error "can't find subdirs: found only $found, expected $left"
5092
5093         unlinkmany -d $dir/d $delete $left ||
5094                 error "unlink of second $left subdirs failed"
5095         # regardless of whether the backing filesystem tracks nlink accurately
5096         # or not, the nlink count shouldn't be more than "." and ".." here
5097         local after=$(stat -c %h $dir)
5098         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
5099                 echo "nlink after: $after"
5100
5101         cleanup_print_lfs_df
5102 }
5103 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
5104
5105 test_51d() {
5106         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5107         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
5108
5109         test_mkdir $DIR/$tdir
5110         createmany -o $DIR/$tdir/t- 1000
5111         $LFS getstripe $DIR/$tdir > $TMP/$tfile
5112         for N in $(seq 0 $((OSTCOUNT - 1))); do
5113                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
5114                         END { printf("%0.0f", objs) }' $TMP/$tfile)
5115                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
5116                         '($1 == '$N') { objs += 1 } \
5117                         END { printf("%0.0f", objs) }')
5118                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
5119         done
5120         unlinkmany $DIR/$tdir/t- 1000
5121
5122         NLAST=0
5123         for N in $(seq 1 $((OSTCOUNT - 1))); do
5124                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
5125                         error "OST $N has less objects vs OST $NLAST" \
5126                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5127                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
5128                         error "OST $N has less objects vs OST $NLAST" \
5129                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5130
5131                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
5132                         error "OST $N has less #0 objects vs OST $NLAST" \
5133                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5134                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
5135                         error "OST $N has less #0 objects vs OST $NLAST" \
5136                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5137                 NLAST=$N
5138         done
5139         rm -f $TMP/$tfile
5140 }
5141 run_test 51d "check object distribution"
5142
5143 test_51e() {
5144         if [ "$mds1_FSTYPE" != ldiskfs ]; then
5145                 skip_env "ldiskfs only test"
5146         fi
5147
5148         test_mkdir -c1 $DIR/$tdir
5149         test_mkdir -c1 $DIR/$tdir/d0
5150
5151         touch $DIR/$tdir/d0/foo
5152         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
5153                 error "file exceed 65000 nlink limit!"
5154         unlinkmany $DIR/$tdir/d0/f- 65001
5155         return 0
5156 }
5157 run_test 51e "check file nlink limit"
5158
5159 test_51f() {
5160         test_mkdir $DIR/$tdir
5161
5162         local max=100000
5163         local ulimit_old=$(ulimit -n)
5164         local spare=20 # number of spare fd's for scripts/libraries, etc.
5165         local mdt=$($LFS getstripe -m $DIR/$tdir)
5166         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
5167
5168         echo "MDT$mdt numfree=$numfree, max=$max"
5169         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
5170         if [ $((numfree + spare)) -gt $ulimit_old ]; then
5171                 while ! ulimit -n $((numfree + spare)); do
5172                         numfree=$((numfree * 3 / 4))
5173                 done
5174                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
5175         else
5176                 echo "left ulimit at $ulimit_old"
5177         fi
5178
5179         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
5180                 unlinkmany $DIR/$tdir/f $numfree
5181                 error "create+open $numfree files in $DIR/$tdir failed"
5182         }
5183         ulimit -n $ulimit_old
5184
5185         # if createmany exits at 120s there will be fewer than $numfree files
5186         unlinkmany $DIR/$tdir/f $numfree || true
5187 }
5188 run_test 51f "check many open files limit"
5189
5190 test_52a() {
5191         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
5192         test_mkdir $DIR/$tdir
5193         touch $DIR/$tdir/foo
5194         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
5195         echo bar >> $DIR/$tdir/foo || error "append bar failed"
5196         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5197         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5198         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5199                                         error "link worked"
5200         echo foo >> $DIR/$tdir/foo || error "append foo failed"
5201         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5202         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
5203                                                      error "lsattr"
5204         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
5205         cp -r $DIR/$tdir $TMP/
5206         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
5207 }
5208 run_test 52a "append-only flag test (should return errors)"
5209
5210 test_52b() {
5211         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
5212         test_mkdir $DIR/$tdir
5213         touch $DIR/$tdir/foo
5214         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
5215         cat test > $DIR/$tdir/foo && error "cat test worked"
5216         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5217         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5218         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5219                                         error "link worked"
5220         echo foo >> $DIR/$tdir/foo && error "echo worked"
5221         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5222         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
5223         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
5224         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
5225                                                         error "lsattr"
5226         chattr -i $DIR/$tdir/foo || error "chattr failed"
5227
5228         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
5229 }
5230 run_test 52b "immutable flag test (should return errors) ======="
5231
5232 test_53() {
5233         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5234         remote_mds_nodsh && skip "remote MDS with nodsh"
5235         remote_ost_nodsh && skip "remote OST with nodsh"
5236
5237         local param
5238         local param_seq
5239         local ostname
5240         local mds_last
5241         local mds_last_seq
5242         local ost_last
5243         local ost_last_seq
5244         local ost_last_id
5245         local ostnum
5246         local node
5247         local found=false
5248         local support_last_seq=true
5249
5250         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
5251                 support_last_seq=false
5252
5253         # only test MDT0000
5254         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
5255         local value
5256         for value in $(do_facet $SINGLEMDS \
5257                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
5258                 param=$(echo ${value[0]} | cut -d "=" -f1)
5259                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
5260
5261                 if $support_last_seq; then
5262                         param_seq=$(echo $param |
5263                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
5264                         mds_last_seq=$(do_facet $SINGLEMDS \
5265                                        $LCTL get_param -n $param_seq)
5266                 fi
5267                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
5268
5269                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
5270                 node=$(facet_active_host ost$((ostnum+1)))
5271                 param="obdfilter.$ostname.last_id"
5272                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
5273                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
5274                         ost_last_id=$ost_last
5275
5276                         if $support_last_seq; then
5277                                 ost_last_id=$(echo $ost_last |
5278                                               awk -F':' '{print $2}' |
5279                                               sed -e "s/^0x//g")
5280                                 ost_last_seq=$(echo $ost_last |
5281                                                awk -F':' '{print $1}')
5282                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
5283                         fi
5284
5285                         if [[ $ost_last_id != $mds_last ]]; then
5286                                 error "$ost_last_id != $mds_last"
5287                         else
5288                                 found=true
5289                                 break
5290                         fi
5291                 done
5292         done
5293         $found || error "can not match last_seq/last_id for $mdtosc"
5294         return 0
5295 }
5296 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
5297
5298 test_54a() {
5299         perl -MSocket -e ';' || skip "no Socket perl module installed"
5300
5301         $SOCKETSERVER $DIR/socket ||
5302                 error "$SOCKETSERVER $DIR/socket failed: $?"
5303         $SOCKETCLIENT $DIR/socket ||
5304                 error "$SOCKETCLIENT $DIR/socket failed: $?"
5305         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
5306 }
5307 run_test 54a "unix domain socket test =========================="
5308
5309 test_54b() {
5310         f="$DIR/f54b"
5311         mknod $f c 1 3
5312         chmod 0666 $f
5313         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
5314 }
5315 run_test 54b "char device works in lustre ======================"
5316
5317 find_loop_dev() {
5318         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
5319         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
5320         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
5321
5322         for i in $(seq 3 7); do
5323                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
5324                 LOOPDEV=$LOOPBASE$i
5325                 LOOPNUM=$i
5326                 break
5327         done
5328 }
5329
5330 cleanup_54c() {
5331         local rc=0
5332         loopdev="$DIR/loop54c"
5333
5334         trap 0
5335         $UMOUNT $DIR/$tdir || rc=$?
5336         losetup -d $loopdev || true
5337         losetup -d $LOOPDEV || true
5338         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
5339         return $rc
5340 }
5341
5342 test_54c() {
5343         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5344
5345         loopdev="$DIR/loop54c"
5346
5347         find_loop_dev
5348         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
5349         trap cleanup_54c EXIT
5350         mknod $loopdev b 7 $LOOPNUM
5351         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
5352         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
5353         losetup $loopdev $DIR/$tfile ||
5354                 error "can't set up $loopdev for $DIR/$tfile"
5355         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
5356         test_mkdir $DIR/$tdir
5357         mount -t ext2 $loopdev $DIR/$tdir ||
5358                 error "error mounting $loopdev on $DIR/$tdir"
5359         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
5360                 error "dd write"
5361         df $DIR/$tdir
5362         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
5363                 error "dd read"
5364         cleanup_54c
5365 }
5366 run_test 54c "block device works in lustre ====================="
5367
5368 test_54d() {
5369         f="$DIR/f54d"
5370         string="aaaaaa"
5371         mknod $f p
5372         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
5373 }
5374 run_test 54d "fifo device works in lustre ======================"
5375
5376 test_54e() {
5377         f="$DIR/f54e"
5378         string="aaaaaa"
5379         cp -aL /dev/console $f
5380         echo $string > $f || error "echo $string to $f failed"
5381 }
5382 run_test 54e "console/tty device works in lustre ======================"
5383
5384 test_56a() {
5385         local numfiles=3
5386         local dir=$DIR/$tdir
5387
5388         rm -rf $dir
5389         test_mkdir -p $dir/dir
5390         for i in $(seq $numfiles); do
5391                 touch $dir/file$i
5392                 touch $dir/dir/file$i
5393         done
5394
5395         local numcomp=$($LFS getstripe --component-count $dir)
5396
5397         [[ $numcomp == 0 ]] && numcomp=1
5398
5399         # test lfs getstripe with --recursive
5400         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
5401
5402         [[ $filenum -eq $((numfiles * 2)) ]] ||
5403                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
5404         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
5405         [[ $filenum -eq $numfiles ]] ||
5406                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
5407         echo "$LFS getstripe showed obdidx or l_ost_idx"
5408
5409         # test lfs getstripe with file instead of dir
5410         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
5411         [[ $filenum -eq 1 ]] ||
5412                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
5413         echo "$LFS getstripe file1 passed"
5414
5415         #test lfs getstripe with --verbose
5416         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
5417         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5418                 error "$LFS getstripe --verbose $dir: "\
5419                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
5420         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
5421                 error "$LFS getstripe $dir: showed lmm_magic"
5422
5423         #test lfs getstripe with -v prints lmm_fid
5424         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
5425         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5426                 error "$LFS getstripe -v $dir: "\
5427                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
5428         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
5429                 error "$LFS getstripe $dir: showed lmm_fid by default"
5430         echo "$LFS getstripe --verbose passed"
5431
5432         #check for FID information
5433         local fid1=$($LFS getstripe --fid $dir/file1)
5434         local fid2=$($LFS getstripe --verbose $dir/file1 |
5435                      awk '/lmm_fid: / { print $2; exit; }')
5436         local fid3=$($LFS path2fid $dir/file1)
5437
5438         [ "$fid1" != "$fid2" ] &&
5439                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
5440         [ "$fid1" != "$fid3" ] &&
5441                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
5442         echo "$LFS getstripe --fid passed"
5443
5444         #test lfs getstripe with --obd
5445         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
5446                 error "$LFS getstripe --obd wrong_uuid: should return error"
5447
5448         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5449
5450         local ostidx=1
5451         local obduuid=$(ostuuid_from_index $ostidx)
5452         local found=$($LFS getstripe -r --obd $obduuid $dir |
5453                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
5454
5455         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
5456         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
5457                 ((filenum--))
5458         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
5459                 ((filenum--))
5460
5461         [[ $found -eq $filenum ]] ||
5462                 error "$LFS getstripe --obd: found $found expect $filenum"
5463         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
5464                 sed '/^[         ]*'${ostidx}'[  ]/d' |
5465                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
5466                 error "$LFS getstripe --obd: should not show file on other obd"
5467         echo "$LFS getstripe --obd passed"
5468 }
5469 run_test 56a "check $LFS getstripe"
5470
5471 test_56b() {
5472         local dir=$DIR/$tdir
5473         local numdirs=3
5474
5475         test_mkdir $dir
5476         for i in $(seq $numdirs); do
5477                 test_mkdir $dir/dir$i
5478         done
5479
5480         # test lfs getdirstripe default mode is non-recursion, which is
5481         # different from lfs getstripe
5482         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
5483
5484         [[ $dircnt -eq 1 ]] ||
5485                 error "$LFS getdirstripe: found $dircnt, not 1"
5486         dircnt=$($LFS getdirstripe --recursive $dir |
5487                 grep -c lmv_stripe_count)
5488         [[ $dircnt -eq $((numdirs + 1)) ]] ||
5489                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
5490 }
5491 run_test 56b "check $LFS getdirstripe"
5492
5493 test_56c() {
5494         remote_ost_nodsh && skip "remote OST with nodsh"
5495
5496         local ost_idx=0
5497         local ost_name=$(ostname_from_index $ost_idx)
5498         local old_status=$(ost_dev_status $ost_idx)
5499
5500         [[ -z "$old_status" ]] ||
5501                 skip_env "OST $ost_name is in $old_status status"
5502
5503         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
5504         [[ $OST1_VERSION -ge $(version_code 2.12.55) ]] && do_facet ost1 \
5505                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
5506         sleep_maxage
5507
5508         local new_status=$(ost_dev_status $ost_idx)
5509
5510         [[ "$new_status" =~ "D" ]] ||
5511                 error "$ost_name status is '$new_status', missing 'D'"
5512         if [[ $OST1_VERSION -ge $(version_code 2.12.55) ]]; then
5513                 [[ "$new_status" =~ "N" ]] ||
5514                         error "$ost_name status is '$new_status', missing 'N'"
5515         fi
5516
5517         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
5518         [[ $OST1_VERSION -ge $(version_code 2.12.55) ]] && do_facet ost1 \
5519                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
5520         sleep_maxage
5521
5522         new_status=$(ost_dev_status $ost_idx)
5523         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
5524                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
5525 }
5526 run_test 56c "check 'lfs df' showing device status"
5527
5528 NUMFILES=3
5529 NUMDIRS=3
5530 setup_56() {
5531         local local_tdir="$1"
5532         local local_numfiles="$2"
5533         local local_numdirs="$3"
5534         local dir_params="$4"
5535         local dir_stripe_params="$5"
5536
5537         if [ ! -d "$local_tdir" ] ; then
5538                 test_mkdir -p $dir_stripe_params $local_tdir
5539                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
5540                 for i in $(seq $local_numfiles) ; do
5541                         touch $local_tdir/file$i
5542                 done
5543                 for i in $(seq $local_numdirs) ; do
5544                         test_mkdir $dir_stripe_params $local_tdir/dir$i
5545                         for j in $(seq $local_numfiles) ; do
5546                                 touch $local_tdir/dir$i/file$j
5547                         done
5548                 done
5549         fi
5550 }
5551
5552 setup_56_special() {
5553         local local_tdir=$1
5554         local local_numfiles=$2
5555         local local_numdirs=$3
5556
5557         setup_56 $local_tdir $local_numfiles $local_numdirs
5558
5559         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
5560                 for i in $(seq $local_numfiles) ; do
5561                         mknod $local_tdir/loop${i}b b 7 $i
5562                         mknod $local_tdir/null${i}c c 1 3
5563                         ln -s $local_tdir/file1 $local_tdir/link${i}
5564                 done
5565                 for i in $(seq $local_numdirs) ; do
5566                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
5567                         mknod $local_tdir/dir$i/null${i}c c 1 3
5568                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
5569                 done
5570         fi
5571 }
5572
5573 test_56g() {
5574         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5575         local expected=$(($NUMDIRS + 2))
5576
5577         setup_56 $dir $NUMFILES $NUMDIRS
5578
5579         # test lfs find with -name
5580         for i in $(seq $NUMFILES) ; do
5581                 local nums=$($LFS find -name "*$i" $dir | wc -l)
5582
5583                 [ $nums -eq $expected ] ||
5584                         error "lfs find -name '*$i' $dir wrong: "\
5585                               "found $nums, expected $expected"
5586         done
5587 }
5588 run_test 56g "check lfs find -name"
5589
5590 test_56h() {
5591         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5592         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
5593
5594         setup_56 $dir $NUMFILES $NUMDIRS
5595
5596         # test lfs find with ! -name
5597         for i in $(seq $NUMFILES) ; do
5598                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
5599
5600                 [ $nums -eq $expected ] ||
5601                         error "lfs find ! -name '*$i' $dir wrong: "\
5602                               "found $nums, expected $expected"
5603         done
5604 }
5605 run_test 56h "check lfs find ! -name"
5606
5607 test_56i() {
5608         local dir=$DIR/$tdir
5609
5610         test_mkdir $dir
5611
5612         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
5613         local out=$($cmd)
5614
5615         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
5616 }
5617 run_test 56i "check 'lfs find -ost UUID' skips directories"
5618
5619 test_56j() {
5620         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5621
5622         setup_56_special $dir $NUMFILES $NUMDIRS
5623
5624         local expected=$((NUMDIRS + 1))
5625         local cmd="$LFS find -type d $dir"
5626         local nums=$($cmd | wc -l)
5627
5628         [ $nums -eq $expected ] ||
5629                 error "'$cmd' wrong: found $nums, expected $expected"
5630 }
5631 run_test 56j "check lfs find -type d"
5632
5633 test_56k() {
5634         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5635
5636         setup_56_special $dir $NUMFILES $NUMDIRS
5637
5638         local expected=$(((NUMDIRS + 1) * NUMFILES))
5639         local cmd="$LFS find -type f $dir"
5640         local nums=$($cmd | wc -l)
5641
5642         [ $nums -eq $expected ] ||
5643                 error "'$cmd' wrong: found $nums, expected $expected"
5644 }
5645 run_test 56k "check lfs find -type f"
5646
5647 test_56l() {
5648         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5649
5650         setup_56_special $dir $NUMFILES $NUMDIRS
5651
5652         local expected=$((NUMDIRS + NUMFILES))
5653         local cmd="$LFS find -type b $dir"
5654         local nums=$($cmd | wc -l)
5655
5656         [ $nums -eq $expected ] ||
5657                 error "'$cmd' wrong: found $nums, expected $expected"
5658 }
5659 run_test 56l "check lfs find -type b"
5660
5661 test_56m() {
5662         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5663
5664         setup_56_special $dir $NUMFILES $NUMDIRS
5665
5666         local expected=$((NUMDIRS + NUMFILES))
5667         local cmd="$LFS find -type c $dir"
5668         local nums=$($cmd | wc -l)
5669         [ $nums -eq $expected ] ||
5670                 error "'$cmd' wrong: found $nums, expected $expected"
5671 }
5672 run_test 56m "check lfs find -type c"
5673
5674 test_56n() {
5675         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5676         setup_56_special $dir $NUMFILES $NUMDIRS
5677
5678         local expected=$((NUMDIRS + NUMFILES))
5679         local cmd="$LFS find -type l $dir"
5680         local nums=$($cmd | wc -l)
5681
5682         [ $nums -eq $expected ] ||
5683                 error "'$cmd' wrong: found $nums, expected $expected"
5684 }
5685 run_test 56n "check lfs find -type l"
5686
5687 test_56o() {
5688         local dir=$DIR/$tdir
5689
5690         setup_56 $dir $NUMFILES $NUMDIRS
5691         utime $dir/file1 > /dev/null || error "utime (1)"
5692         utime $dir/file2 > /dev/null || error "utime (2)"
5693         utime $dir/dir1 > /dev/null || error "utime (3)"
5694         utime $dir/dir2 > /dev/null || error "utime (4)"
5695         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5696         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5697
5698         local expected=4
5699         local nums=$($LFS find -mtime +0 $dir | wc -l)
5700
5701         [ $nums -eq $expected ] ||
5702                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5703
5704         expected=12
5705         cmd="$LFS find -mtime 0 $dir"
5706         nums=$($cmd | wc -l)
5707         [ $nums -eq $expected ] ||
5708                 error "'$cmd' wrong: found $nums, expected $expected"
5709 }
5710 run_test 56o "check lfs find -mtime for old files"
5711
5712 test_56ob() {
5713         local dir=$DIR/$tdir
5714         local expected=1
5715         local count=0
5716
5717         # just to make sure there is something that won't be found
5718         test_mkdir $dir
5719         touch $dir/$tfile.now
5720
5721         for age in year week day hour min; do
5722                 count=$((count + 1))
5723
5724                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
5725                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
5726                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
5727
5728                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
5729                 local nums=$($cmd | wc -l)
5730                 [ $nums -eq $expected ] ||
5731                         error "'$cmd' wrong: found $nums, expected $expected"
5732
5733                 cmd="$LFS find $dir -atime $count${age:0:1}"
5734                 nums=$($cmd | wc -l)
5735                 [ $nums -eq $expected ] ||
5736                         error "'$cmd' wrong: found $nums, expected $expected"
5737         done
5738
5739         sleep 2
5740         cmd="$LFS find $dir -ctime +1s -type f"
5741         nums=$($cmd | wc -l)
5742         (( $nums == $count * 2 + 1)) ||
5743                 error "'$cmd' wrong: found $nums, expected $((expected*2+1))"
5744 }
5745 run_test 56ob "check lfs find -atime -mtime -ctime with units"
5746
5747 test_newerXY_base() {
5748         local x=$1
5749         local y=$2
5750         local dir=$DIR/$tdir
5751         local ref
5752         local negref
5753
5754         if [ $y == "t" ]; then
5755                 ref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5756         else
5757                 ref=$DIR/$tfile.newer
5758                 touch $ref || error "touch $ref failed"
5759         fi
5760         sleep 2
5761         setup_56 $dir $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
5762         sleep 2
5763         if [ $y == "t" ]; then
5764                 negref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5765         else
5766                 negref=$DIR/$tfile.newerneg
5767                 touch $negref || error "touch $negref failed"
5768         fi
5769
5770         local cmd="$LFS find $dir -newer$x$y $ref"
5771         local nums=$(eval $cmd | wc -l)
5772         local expected=$(((NUMFILES + 2) * NUMDIRS + 1))
5773
5774         [ $nums -eq $expected ] ||
5775                 error "'$cmd' wrong: found $nums, expected $expected"
5776
5777         cmd="$LFS find $dir ! -newer$x$y $negref"
5778         nums=$(eval $cmd | wc -l)
5779         [ $nums -eq $expected ] ||
5780                 error "'$cmd' wrong: found $nums, expected $expected"
5781
5782         cmd="$LFS find $dir -newer$x$y $ref ! -newer$x$y $negref"
5783         nums=$(eval $cmd | wc -l)
5784         [ $nums -eq $expected ] ||
5785                 error "'$cmd' wrong: found $nums, expected $expected"
5786
5787         rm -rf $DIR/*
5788 }
5789
5790 test_56oc() {
5791         test_newerXY_base "a" "a"
5792         test_newerXY_base "a" "m"
5793         test_newerXY_base "a" "c"
5794         test_newerXY_base "m" "a"
5795         test_newerXY_base "m" "m"
5796         test_newerXY_base "m" "c"
5797         test_newerXY_base "c" "a"
5798         test_newerXY_base "c" "m"
5799         test_newerXY_base "c" "c"
5800         test_newerXY_base "a" "t"
5801         test_newerXY_base "m" "t"
5802         test_newerXY_base "c" "t"
5803 }
5804 run_test 56oc "check lfs find -newerXY work"
5805
5806 test_56p() {
5807         [ $RUNAS_ID -eq $UID ] &&
5808                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5809
5810         local dir=$DIR/$tdir
5811
5812         setup_56 $dir $NUMFILES $NUMDIRS
5813         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
5814
5815         local expected=$NUMFILES
5816         local cmd="$LFS find -uid $RUNAS_ID $dir"
5817         local nums=$($cmd | wc -l)
5818
5819         [ $nums -eq $expected ] ||
5820                 error "'$cmd' wrong: found $nums, expected $expected"
5821
5822         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
5823         cmd="$LFS find ! -uid $RUNAS_ID $dir"
5824         nums=$($cmd | wc -l)
5825         [ $nums -eq $expected ] ||
5826                 error "'$cmd' wrong: found $nums, expected $expected"
5827 }
5828 run_test 56p "check lfs find -uid and ! -uid"
5829
5830 test_56q() {
5831         [ $RUNAS_ID -eq $UID ] &&
5832                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5833
5834         local dir=$DIR/$tdir
5835
5836         setup_56 $dir $NUMFILES $NUMDIRS
5837         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
5838
5839         local expected=$NUMFILES
5840         local cmd="$LFS find -gid $RUNAS_GID $dir"
5841         local nums=$($cmd | wc -l)
5842
5843         [ $nums -eq $expected ] ||
5844                 error "'$cmd' wrong: found $nums, expected $expected"
5845
5846         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
5847         cmd="$LFS find ! -gid $RUNAS_GID $dir"
5848         nums=$($cmd | wc -l)
5849         [ $nums -eq $expected ] ||
5850                 error "'$cmd' wrong: found $nums, expected $expected"
5851 }
5852 run_test 56q "check lfs find -gid and ! -gid"
5853
5854 test_56r() {
5855         local dir=$DIR/$tdir
5856
5857         setup_56 $dir $NUMFILES $NUMDIRS
5858
5859         local expected=12
5860         local cmd="$LFS find -size 0 -type f -lazy $dir"
5861         local nums=$($cmd | wc -l)
5862
5863         [ $nums -eq $expected ] ||
5864                 error "'$cmd' wrong: found $nums, expected $expected"
5865         cmd="$LFS find -size 0 -type f $dir"
5866         nums=$($cmd | wc -l)
5867         [ $nums -eq $expected ] ||
5868                 error "'$cmd' wrong: found $nums, expected $expected"
5869
5870         expected=0
5871         cmd="$LFS find ! -size 0 -type f -lazy $dir"
5872         nums=$($cmd | wc -l)
5873         [ $nums -eq $expected ] ||
5874                 error "'$cmd' wrong: found $nums, expected $expected"
5875         cmd="$LFS find ! -size 0 -type f $dir"
5876         nums=$($cmd | wc -l)
5877         [ $nums -eq $expected ] ||
5878                 error "'$cmd' wrong: found $nums, expected $expected"
5879
5880         echo "test" > $dir/$tfile
5881         echo "test2" > $dir/$tfile.2 && sync
5882         expected=1
5883         cmd="$LFS find -size 5 -type f -lazy $dir"
5884         nums=$($cmd | wc -l)
5885         [ $nums -eq $expected ] ||
5886                 error "'$cmd' wrong: found $nums, expected $expected"
5887         cmd="$LFS find -size 5 -type f $dir"
5888         nums=$($cmd | wc -l)
5889         [ $nums -eq $expected ] ||
5890                 error "'$cmd' wrong: found $nums, expected $expected"
5891
5892         expected=1
5893         cmd="$LFS find -size +5 -type f -lazy $dir"
5894         nums=$($cmd | wc -l)
5895         [ $nums -eq $expected ] ||
5896                 error "'$cmd' wrong: found $nums, expected $expected"
5897         cmd="$LFS find -size +5 -type f $dir"
5898         nums=$($cmd | wc -l)
5899         [ $nums -eq $expected ] ||
5900                 error "'$cmd' wrong: found $nums, expected $expected"
5901
5902         expected=2
5903         cmd="$LFS find -size +0 -type f -lazy $dir"
5904         nums=$($cmd | wc -l)
5905         [ $nums -eq $expected ] ||
5906                 error "'$cmd' wrong: found $nums, expected $expected"
5907         cmd="$LFS find -size +0 -type f $dir"
5908         nums=$($cmd | wc -l)
5909         [ $nums -eq $expected ] ||
5910                 error "'$cmd' wrong: found $nums, expected $expected"
5911
5912         expected=2
5913         cmd="$LFS find ! -size -5 -type f -lazy $dir"
5914         nums=$($cmd | wc -l)
5915         [ $nums -eq $expected ] ||
5916                 error "'$cmd' wrong: found $nums, expected $expected"
5917         cmd="$LFS find ! -size -5 -type f $dir"
5918         nums=$($cmd | wc -l)
5919         [ $nums -eq $expected ] ||
5920                 error "'$cmd' wrong: found $nums, expected $expected"
5921
5922         expected=12
5923         cmd="$LFS find -size -5 -type f -lazy $dir"
5924         nums=$($cmd | wc -l)
5925         [ $nums -eq $expected ] ||
5926                 error "'$cmd' wrong: found $nums, expected $expected"
5927         cmd="$LFS find -size -5 -type f $dir"
5928         nums=$($cmd | wc -l)
5929         [ $nums -eq $expected ] ||
5930                 error "'$cmd' wrong: found $nums, expected $expected"
5931 }
5932 run_test 56r "check lfs find -size works"
5933
5934 test_56ra() {
5935         [[ $MDS1_VERSION -gt $(version_code 2.12.58) ]] ||
5936                 skip "MDS < 2.12.58 doesn't return LSOM data"
5937         local dir=$DIR/$tdir
5938
5939         [[ $OSC == "mdc" ]] && skip "DoM files" && return
5940
5941         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
5942
5943         cancel_lru_locks $OSC
5944
5945         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5946         local expected=12
5947         local cmd="$LFS find -size 0 -type f -lazy $dir"
5948         local nums=$($cmd | wc -l)
5949
5950         [ $nums -eq $expected ] ||
5951                 error "'$cmd' wrong: found $nums, expected $expected"
5952
5953         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5954         [ $rpcs_before -eq $rpcs_after ] ||
5955                 error "'$cmd' should not send glimpse RPCs to OST"
5956         cmd="$LFS find -size 0 -type f $dir"
5957         nums=$($cmd | wc -l)
5958         [ $nums -eq $expected ] ||
5959                 error "'$cmd' wrong: found $nums, expected $expected"
5960         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5961         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
5962         $LCTL get_param osc.*.stats
5963         [ $rpcs_after -eq $((rpcs_before + 12)) ] ||
5964                 error "'$cmd' should send 12 glimpse RPCs to OST"
5965
5966         cancel_lru_locks $OSC
5967         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5968         expected=0
5969         cmd="$LFS find ! -size 0 -type f -lazy $dir"
5970         nums=$($cmd | wc -l)
5971         [ $nums -eq $expected ] ||
5972                 error "'$cmd' wrong: found $nums, expected $expected"
5973         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5974         $LCTL get_param mdc.*.stats
5975         [ $rpcs_before -eq $rpcs_after ] ||
5976                 error "'$cmd' should not send glimpse RPCs to OST"
5977         cmd="$LFS find ! -size 0 -type f $dir"
5978         nums=$($cmd | wc -l)
5979         [ $nums -eq $expected ] ||
5980                 error "'$cmd' wrong: found $nums, expected $expected"
5981         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5982         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
5983         [ $rpcs_after -eq $((rpcs_before + 12)) ] ||
5984                 error "'$cmd' should send 12 glimpse RPCs to OST"
5985
5986         echo "test" > $dir/$tfile
5987         echo "test2" > $dir/$tfile.2 && sync
5988         cancel_lru_locks $OSC
5989         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5990         expected=1
5991         cmd="$LFS find -size 5 -type f -lazy $dir"
5992         nums=$($cmd | wc -l)
5993         [ $nums -eq $expected ] ||
5994                 error "'$cmd' wrong: found $nums, expected $expected"
5995         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5996         [ $rpcs_before -eq $rpcs_after ] ||
5997                 error "'$cmd' should not send glimpse RPCs to OST"
5998         cmd="$LFS find -size 5 -type f $dir"
5999         nums=$($cmd | wc -l)
6000         [ $nums -eq $expected ] ||
6001                 error "'$cmd' wrong: found $nums, expected $expected"
6002         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6003         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6004         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6005                 error "'$cmd' should send 14 glimpse RPCs to OST"
6006
6007         cancel_lru_locks $OSC
6008         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6009         expected=1
6010         cmd="$LFS find -size +5 -type f -lazy $dir"
6011         nums=$($cmd | wc -l)
6012         [ $nums -eq $expected ] ||
6013                 error "'$cmd' wrong: found $nums, expected $expected"
6014         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6015         [ $rpcs_before -eq $rpcs_after ] ||
6016                 error "'$cmd' should not send glimpse RPCs to OST"
6017         cmd="$LFS find -size +5 -type f $dir"
6018         nums=$($cmd | wc -l)
6019         [ $nums -eq $expected ] ||
6020                 error "'$cmd' wrong: found $nums, expected $expected"
6021         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6022         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6023         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6024                 error "'$cmd' should send 14 glimpse RPCs to OST"
6025
6026         cancel_lru_locks $OSC
6027         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6028         expected=2
6029         cmd="$LFS find -size +0 -type f -lazy $dir"
6030         nums=$($cmd | wc -l)
6031         [ $nums -eq $expected ] ||
6032                 error "'$cmd' wrong: found $nums, expected $expected"
6033         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6034         [ $rpcs_before -eq $rpcs_after ] ||
6035                 error "'$cmd' should not send glimpse RPCs to OST"
6036         cmd="$LFS find -size +0 -type f $dir"
6037         nums=$($cmd | wc -l)
6038         [ $nums -eq $expected ] ||
6039                 error "'$cmd' wrong: found $nums, expected $expected"
6040         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6041         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6042         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6043                 error "'$cmd' should send 14 glimpse RPCs to OST"
6044
6045         cancel_lru_locks $OSC
6046         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6047         expected=2
6048         cmd="$LFS find ! -size -5 -type f -lazy $dir"
6049         nums=$($cmd | wc -l)
6050         [ $nums -eq $expected ] ||
6051                 error "'$cmd' wrong: found $nums, expected $expected"
6052         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6053         [ $rpcs_before -eq $rpcs_after ] ||
6054                 error "'$cmd' should not send glimpse RPCs to OST"
6055         cmd="$LFS find ! -size -5 -type f $dir"
6056         nums=$($cmd | wc -l)
6057         [ $nums -eq $expected ] ||
6058                 error "'$cmd' wrong: found $nums, expected $expected"
6059         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6060         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6061         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6062                 error "'$cmd' should send 14 glimpse RPCs to OST"
6063
6064         cancel_lru_locks $OSC
6065         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6066         expected=12
6067         cmd="$LFS find -size -5 -type f -lazy $dir"
6068         nums=$($cmd | wc -l)
6069         [ $nums -eq $expected ] ||
6070                 error "'$cmd' wrong: found $nums, expected $expected"
6071         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6072         [ $rpcs_before -eq $rpcs_after ] ||
6073                 error "'$cmd' should not send glimpse RPCs to OST"
6074         cmd="$LFS find -size -5 -type f $dir"
6075         nums=$($cmd | wc -l)
6076         [ $nums -eq $expected ] ||
6077                 error "'$cmd' wrong: found $nums, expected $expected"
6078         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6079         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6080         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6081                 error "'$cmd' should send 14 glimpse RPCs to OST"
6082 }
6083 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
6084
6085 test_56s() { # LU-611 #LU-9369
6086         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
6087
6088         local dir=$DIR/$tdir
6089         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
6090
6091         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6092         for i in $(seq $NUMDIRS); do
6093                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
6094         done
6095
6096         local expected=$NUMDIRS
6097         local cmd="$LFS find -c $OSTCOUNT $dir"
6098         local nums=$($cmd | wc -l)
6099
6100         [ $nums -eq $expected ] || {
6101                 $LFS getstripe -R $dir
6102                 error "'$cmd' wrong: found $nums, expected $expected"
6103         }
6104
6105         expected=$((NUMDIRS + onestripe))
6106         cmd="$LFS find -stripe-count +0 -type f $dir"
6107         nums=$($cmd | wc -l)
6108         [ $nums -eq $expected ] || {
6109                 $LFS getstripe -R $dir
6110                 error "'$cmd' wrong: found $nums, expected $expected"
6111         }
6112
6113         expected=$onestripe
6114         cmd="$LFS find -stripe-count 1 -type f $dir"
6115         nums=$($cmd | wc -l)
6116         [ $nums -eq $expected ] || {
6117                 $LFS getstripe -R $dir
6118                 error "'$cmd' wrong: found $nums, expected $expected"
6119         }
6120
6121         cmd="$LFS find -stripe-count -2 -type f $dir"
6122         nums=$($cmd | wc -l)
6123         [ $nums -eq $expected ] || {
6124                 $LFS getstripe -R $dir
6125                 error "'$cmd' wrong: found $nums, expected $expected"
6126         }
6127
6128         expected=0
6129         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
6130         nums=$($cmd | wc -l)
6131         [ $nums -eq $expected ] || {
6132                 $LFS getstripe -R $dir
6133                 error "'$cmd' wrong: found $nums, expected $expected"
6134         }
6135 }
6136 run_test 56s "check lfs find -stripe-count works"
6137
6138 test_56t() { # LU-611 #LU-9369
6139         local dir=$DIR/$tdir
6140
6141         setup_56 $dir 0 $NUMDIRS
6142         for i in $(seq $NUMDIRS); do
6143                 $LFS setstripe -S 8M $dir/dir$i/$tfile
6144         done
6145
6146         local expected=$NUMDIRS
6147         local cmd="$LFS find -S 8M $dir"
6148         local nums=$($cmd | wc -l)
6149
6150         [ $nums -eq $expected ] || {
6151                 $LFS getstripe -R $dir
6152                 error "'$cmd' wrong: found $nums, expected $expected"
6153         }
6154         rm -rf $dir
6155
6156         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
6157
6158         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
6159
6160         expected=$(((NUMDIRS + 1) * NUMFILES))
6161         cmd="$LFS find -stripe-size 512k -type f $dir"
6162         nums=$($cmd | wc -l)
6163         [ $nums -eq $expected ] ||
6164                 error "'$cmd' wrong: found $nums, expected $expected"
6165
6166         cmd="$LFS find -stripe-size +320k -type f $dir"
6167         nums=$($cmd | wc -l)
6168         [ $nums -eq $expected ] ||
6169                 error "'$cmd' wrong: found $nums, expected $expected"
6170
6171         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
6172         cmd="$LFS find -stripe-size +200k -type f $dir"
6173         nums=$($cmd | wc -l)
6174         [ $nums -eq $expected ] ||
6175                 error "'$cmd' wrong: found $nums, expected $expected"
6176
6177         cmd="$LFS find -stripe-size -640k -type f $dir"
6178         nums=$($cmd | wc -l)
6179         [ $nums -eq $expected ] ||
6180                 error "'$cmd' wrong: found $nums, expected $expected"
6181
6182         expected=4
6183         cmd="$LFS find -stripe-size 256k -type f $dir"
6184         nums=$($cmd | wc -l)
6185         [ $nums -eq $expected ] ||
6186                 error "'$cmd' wrong: found $nums, expected $expected"
6187
6188         cmd="$LFS find -stripe-size -320k -type f $dir"
6189         nums=$($cmd | wc -l)
6190         [ $nums -eq $expected ] ||
6191                 error "'$cmd' wrong: found $nums, expected $expected"
6192
6193         expected=0
6194         cmd="$LFS find -stripe-size 1024k -type f $dir"
6195         nums=$($cmd | wc -l)
6196         [ $nums -eq $expected ] ||
6197                 error "'$cmd' wrong: found $nums, expected $expected"
6198 }
6199 run_test 56t "check lfs find -stripe-size works"
6200
6201 test_56u() { # LU-611
6202         local dir=$DIR/$tdir
6203
6204         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
6205
6206         if [[ $OSTCOUNT -gt 1 ]]; then
6207                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
6208                 onestripe=4
6209         else
6210                 onestripe=0
6211         fi
6212
6213         local expected=$(((NUMDIRS + 1) * NUMFILES))
6214         local cmd="$LFS find -stripe-index 0 -type f $dir"
6215         local nums=$($cmd | wc -l)
6216
6217         [ $nums -eq $expected ] ||
6218                 error "'$cmd' wrong: found $nums, expected $expected"
6219
6220         expected=$onestripe
6221         cmd="$LFS find -stripe-index 1 -type f $dir"
6222         nums=$($cmd | wc -l)
6223         [ $nums -eq $expected ] ||
6224                 error "'$cmd' wrong: found $nums, expected $expected"
6225
6226         cmd="$LFS find ! -stripe-index 0 -type f $dir"
6227         nums=$($cmd | wc -l)
6228         [ $nums -eq $expected ] ||
6229                 error "'$cmd' wrong: found $nums, expected $expected"
6230
6231         expected=0
6232         # This should produce an error and not return any files
6233         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
6234         nums=$($cmd 2>/dev/null | wc -l)
6235         [ $nums -eq $expected ] ||
6236                 error "'$cmd' wrong: found $nums, expected $expected"
6237
6238         if [[ $OSTCOUNT -gt 1 ]]; then
6239                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
6240                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
6241                 nums=$($cmd | wc -l)
6242                 [ $nums -eq $expected ] ||
6243                         error "'$cmd' wrong: found $nums, expected $expected"
6244         fi
6245 }
6246 run_test 56u "check lfs find -stripe-index works"
6247
6248 test_56v() {
6249         local mdt_idx=0
6250         local dir=$DIR/$tdir
6251
6252         setup_56 $dir $NUMFILES $NUMDIRS
6253
6254         UUID=$(mdtuuid_from_index $mdt_idx $dir)
6255         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
6256
6257         for file in $($LFS find -m $UUID $dir); do
6258                 file_midx=$($LFS getstripe -m $file)
6259                 [ $file_midx -eq $mdt_idx ] ||
6260                         error "lfs find -m $UUID != getstripe -m $file_midx"
6261         done
6262 }
6263 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
6264
6265 test_56w() {
6266         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6267         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6268
6269         local dir=$DIR/$tdir
6270
6271         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
6272
6273         local stripe_size=$($LFS getstripe -S -d $dir) ||
6274                 error "$LFS getstripe -S -d $dir failed"
6275         stripe_size=${stripe_size%% *}
6276
6277         local file_size=$((stripe_size * OSTCOUNT))
6278         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
6279         local required_space=$((file_num * file_size))
6280         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
6281                            head -n1)
6282         [[ $free_space -le $((required_space / 1024)) ]] &&
6283                 skip_env "need $required_space, have $free_space kbytes"
6284
6285         local dd_bs=65536
6286         local dd_count=$((file_size / dd_bs))
6287
6288         # write data into the files
6289         local i
6290         local j
6291         local file
6292
6293         for i in $(seq $NUMFILES); do
6294                 file=$dir/file$i
6295                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6296                         error "write data into $file failed"
6297         done
6298         for i in $(seq $NUMDIRS); do
6299                 for j in $(seq $NUMFILES); do
6300                         file=$dir/dir$i/file$j
6301                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6302                                 error "write data into $file failed"
6303                 done
6304         done
6305
6306         # $LFS_MIGRATE will fail if hard link migration is unsupported
6307         if [[ $MDS1_VERSION -gt $(version_code 2.5.55) ]]; then
6308                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
6309                         error "creating links to $dir/dir1/file1 failed"
6310         fi
6311
6312         local expected=-1
6313
6314         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
6315
6316         # lfs_migrate file
6317         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
6318
6319         echo "$cmd"
6320         eval $cmd || error "$cmd failed"
6321
6322         check_stripe_count $dir/file1 $expected
6323
6324         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
6325         then
6326                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
6327                 # OST 1 if it is on OST 0. This file is small enough to
6328                 # be on only one stripe.
6329                 file=$dir/migr_1_ost
6330                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
6331                         error "write data into $file failed"
6332                 local obdidx=$($LFS getstripe -i $file)
6333                 local oldmd5=$(md5sum $file)
6334                 local newobdidx=0
6335
6336                 [[ $obdidx -eq 0 ]] && newobdidx=1
6337                 cmd="$LFS migrate -i $newobdidx $file"
6338                 echo $cmd
6339                 eval $cmd || error "$cmd failed"
6340
6341                 local realobdix=$($LFS getstripe -i $file)
6342                 local newmd5=$(md5sum $file)
6343
6344                 [[ $newobdidx -ne $realobdix ]] &&
6345                         error "new OST is different (was=$obdidx, "\
6346                               "wanted=$newobdidx, got=$realobdix)"
6347                 [[ "$oldmd5" != "$newmd5" ]] &&
6348                         error "md5sum differ: $oldmd5, $newmd5"
6349         fi
6350
6351         # lfs_migrate dir
6352         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
6353         echo "$cmd"
6354         eval $cmd || error "$cmd failed"
6355
6356         for j in $(seq $NUMFILES); do
6357                 check_stripe_count $dir/dir1/file$j $expected
6358         done
6359
6360         # lfs_migrate works with lfs find
6361         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
6362              $LFS_MIGRATE -y -c $expected"
6363         echo "$cmd"
6364         eval $cmd || error "$cmd failed"
6365
6366         for i in $(seq 2 $NUMFILES); do
6367                 check_stripe_count $dir/file$i $expected
6368         done
6369         for i in $(seq 2 $NUMDIRS); do
6370                 for j in $(seq $NUMFILES); do
6371                 check_stripe_count $dir/dir$i/file$j $expected
6372                 done
6373         done
6374 }
6375 run_test 56w "check lfs_migrate -c stripe_count works"
6376
6377 test_56wb() {
6378         local file1=$DIR/$tdir/file1
6379         local create_pool=false
6380         local initial_pool=$($LFS getstripe -p $DIR)
6381         local pool_list=()
6382         local pool=""
6383
6384         echo -n "Creating test dir..."
6385         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6386         echo "done."
6387
6388         echo -n "Creating test file..."
6389         touch $file1 || error "cannot create file"
6390         echo "done."
6391
6392         echo -n "Detecting existing pools..."
6393         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
6394
6395         if [ ${#pool_list[@]} -gt 0 ]; then
6396                 echo "${pool_list[@]}"
6397                 for thispool in "${pool_list[@]}"; do
6398                         if [[ -z "$initial_pool" ||
6399                               "$initial_pool" != "$thispool" ]]; then
6400                                 pool="$thispool"
6401                                 echo "Using existing pool '$pool'"
6402                                 break
6403                         fi
6404                 done
6405         else
6406                 echo "none detected."
6407         fi
6408         if [ -z "$pool" ]; then
6409                 pool=${POOL:-testpool}
6410                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
6411                 echo -n "Creating pool '$pool'..."
6412                 create_pool=true
6413                 pool_add $pool &> /dev/null ||
6414                         error "pool_add failed"
6415                 echo "done."
6416
6417                 echo -n "Adding target to pool..."
6418                 pool_add_targets $pool 0 0 1 &> /dev/null ||
6419                         error "pool_add_targets failed"
6420                 echo "done."
6421         fi
6422
6423         echo -n "Setting pool using -p option..."
6424         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
6425                 error "migrate failed rc = $?"
6426         echo "done."
6427
6428         echo -n "Verifying test file is in pool after migrating..."
6429         [ "$($LFS getstripe -p $file1)" = $pool ] ||
6430                 error "file was not migrated to pool $pool"
6431         echo "done."
6432
6433         echo -n "Removing test file from pool '$pool'..."
6434         $LFS migrate $file1 &> /dev/null ||
6435                 error "cannot remove from pool"
6436         [ "$($LFS getstripe -p $file1)" ] &&
6437                 error "pool still set"
6438         echo "done."
6439
6440         echo -n "Setting pool using --pool option..."
6441         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
6442                 error "migrate failed rc = $?"
6443         echo "done."
6444
6445         # Clean up
6446         rm -f $file1
6447         if $create_pool; then
6448                 destroy_test_pools 2> /dev/null ||
6449                         error "destroy test pools failed"
6450         fi
6451 }
6452 run_test 56wb "check lfs_migrate pool support"
6453
6454 test_56wc() {
6455         local file1="$DIR/$tdir/file1"
6456
6457         echo -n "Creating test dir..."
6458         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6459         local def_stripe_size=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
6460         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
6461                 error "cannot set stripe"
6462         echo "done"
6463
6464         echo -n "Setting initial stripe for test file..."
6465         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
6466                 error "cannot set stripe"
6467         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
6468                 error "stripe size not set"
6469         echo "done."
6470
6471         # File currently set to -S 512K -c 1
6472
6473         # Ensure -c and -S options are rejected when -R is set
6474         echo -n "Verifying incompatible options are detected..."
6475         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
6476                 error "incompatible -c and -R options not detected"
6477         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
6478                 error "incompatible -S and -R options not detected"
6479         echo "done."
6480
6481         # Ensure unrecognized options are passed through to 'lfs migrate'
6482         echo -n "Verifying -S option is passed through to lfs migrate..."
6483         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
6484                 error "migration failed"
6485         [ $($LFS getstripe -S "$file1") -eq 1048576 ] ||
6486                 error "file was not restriped"
6487         echo "done."
6488
6489         # File currently set to -S 1M -c 1
6490
6491         # Ensure long options are supported
6492         echo -n "Verifying long options supported..."
6493         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
6494                 error "long option without argument not supported"
6495         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
6496                 error "long option with argument not supported"
6497         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
6498                 error "file not restriped with --stripe-size option"
6499         echo "done."
6500
6501         # File currently set to -S 512K -c 1
6502
6503         if [ "$OSTCOUNT" -gt 1 ]; then
6504                 echo -n "Verifying explicit stripe count can be set..."
6505                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
6506                         error "migrate failed"
6507                 [ $($LFS getstripe -c "$file1") -eq 2 ] ||
6508                         error "file not restriped to explicit count"
6509                 echo "done."
6510         fi
6511
6512         # File currently set to -S 512K -c 1 or -S 512K -c 2
6513
6514         # Ensure parent striping is used if -R is set, and no stripe
6515         # count or size is specified
6516         echo -n "Setting stripe for parent directory..."
6517         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
6518                 error "cannot set stripe"
6519         echo "done."
6520
6521         echo -n "Verifying restripe option uses parent stripe settings..."
6522         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
6523                 error "migrate failed"
6524         [ $($LFS getstripe -S "$file1") -eq $def_stripe_size ] ||
6525                 error "file not restriped to parent settings"
6526         [ $($LFS getstripe -c "$file1") -eq 1 ] ||
6527                 error "file not restriped to parent settings"
6528         echo "done."
6529
6530         # File currently set to -S 1M -c 1
6531
6532         # Ensure striping is preserved if -R is not set, and no stripe
6533         # count or size is specified
6534         echo -n "Verifying striping size preserved when not specified..."
6535         local orig_stripe_size=$($LFS getstripe -S "$file1" 2>/dev/null)
6536         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
6537                 error "cannot set stripe on parent directory"
6538         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6539                 error "migrate failed"
6540         [ $($LFS getstripe -S "$file1") -eq $orig_stripe_size ] ||
6541                 error "file was restriped"
6542         echo "done."
6543
6544         # Ensure file name properly detected when final option has no argument
6545         echo -n "Verifying file name properly detected..."
6546         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6547                 error "file name interpreted as option argument"
6548         echo "done."
6549
6550         # Clean up
6551         rm -f "$file1"
6552 }
6553 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
6554
6555 test_56wd() {
6556         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6557
6558         local file1=$DIR/$tdir/file1
6559
6560         echo -n "Creating test dir..."
6561         test_mkdir $DIR/$tdir || error "cannot create dir"
6562         echo "done."
6563
6564         echo -n "Creating test file..."
6565         touch $file1
6566         echo "done."
6567
6568         # Ensure 'lfs migrate' will fail by using a non-existent option,
6569         # and make sure rsync is not called to recover
6570         echo -n "Make sure --no-rsync option works..."
6571         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
6572                 grep -q 'refusing to fall back to rsync' ||
6573                 error "rsync was called with --no-rsync set"
6574         echo "done."
6575
6576         # Ensure rsync is called without trying 'lfs migrate' first
6577         echo -n "Make sure --rsync option works..."
6578         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
6579                 grep -q 'falling back to rsync' &&
6580                 error "lfs migrate was called with --rsync set"
6581         echo "done."
6582
6583         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
6584         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
6585                 grep -q 'at the same time' ||
6586                 error "--rsync and --no-rsync accepted concurrently"
6587         echo "done."
6588
6589         # Clean up
6590         rm -f $file1
6591 }
6592 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
6593
6594 test_56x() {
6595         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6596         check_swap_layouts_support
6597
6598         local dir=$DIR/$tdir
6599         local ref1=/etc/passwd
6600         local file1=$dir/file1
6601
6602         test_mkdir $dir || error "creating dir $dir"
6603         $LFS setstripe -c 2 $file1
6604         cp $ref1 $file1
6605         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
6606         stripe=$($LFS getstripe -c $file1)
6607         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6608         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6609
6610         # clean up
6611         rm -f $file1
6612 }
6613 run_test 56x "lfs migration support"
6614
6615 test_56xa() {
6616         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6617         check_swap_layouts_support
6618
6619         local dir=$DIR/$tdir/$testnum
6620
6621         test_mkdir -p $dir
6622
6623         local ref1=/etc/passwd
6624         local file1=$dir/file1
6625
6626         $LFS setstripe -c 2 $file1
6627         cp $ref1 $file1
6628         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
6629
6630         local stripe=$($LFS getstripe -c $file1)
6631
6632         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6633         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6634
6635         # clean up
6636         rm -f $file1
6637 }
6638 run_test 56xa "lfs migration --block support"
6639
6640 check_migrate_links() {
6641         local dir="$1"
6642         local file1="$dir/file1"
6643         local begin="$2"
6644         local count="$3"
6645         local runas="$4"
6646         local total_count=$(($begin + $count - 1))
6647         local symlink_count=10
6648         local uniq_count=10
6649
6650         if [ ! -f "$file1" ]; then
6651                 echo -n "creating initial file..."
6652                 $LFS setstripe -c 1 -S "512k" "$file1" ||
6653                         error "cannot setstripe initial file"
6654                 echo "done"
6655
6656                 echo -n "creating symlinks..."
6657                 for s in $(seq 1 $symlink_count); do
6658                         ln -s "$file1" "$dir/slink$s" ||
6659                                 error "cannot create symlinks"
6660                 done
6661                 echo "done"
6662
6663                 echo -n "creating nonlinked files..."
6664                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
6665                         error "cannot create nonlinked files"
6666                 echo "done"
6667         fi
6668
6669         # create hard links
6670         if [ ! -f "$dir/file$total_count" ]; then
6671                 echo -n "creating hard links $begin:$total_count..."
6672                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
6673                         /dev/null || error "cannot create hard links"
6674                 echo "done"
6675         fi
6676
6677         echo -n "checking number of hard links listed in xattrs..."
6678         local fid=$($LFS getstripe -F "$file1")
6679         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
6680
6681         echo "${#paths[*]}"
6682         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
6683                         skip "hard link list has unexpected size, skipping test"
6684         fi
6685         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
6686                         error "link names should exceed xattrs size"
6687         fi
6688
6689         echo -n "migrating files..."
6690         local migrate_out=$($runas $LFS_MIGRATE -y -S '1m' $dir)
6691         local rc=$?
6692         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
6693         echo "done"
6694
6695         # make sure all links have been properly migrated
6696         echo -n "verifying files..."
6697         fid=$($LFS getstripe -F "$file1") ||
6698                 error "cannot get fid for file $file1"
6699         for i in $(seq 2 $total_count); do
6700                 local fid2=$($LFS getstripe -F $dir/file$i)
6701
6702                 [ "$fid2" == "$fid" ] ||
6703                         error "migrated hard link has mismatched FID"
6704         done
6705
6706         # make sure hard links were properly detected, and migration was
6707         # performed only once for the entire link set; nonlinked files should
6708         # also be migrated
6709         local actual=$(grep -c 'done' <<< "$migrate_out")
6710         local expected=$(($uniq_count + 1))
6711
6712         [ "$actual" -eq  "$expected" ] ||
6713                 error "hard links individually migrated ($actual != $expected)"
6714
6715         # make sure the correct number of hard links are present
6716         local hardlinks=$(stat -c '%h' "$file1")
6717
6718         [ $hardlinks -eq $total_count ] ||
6719                 error "num hard links $hardlinks != $total_count"
6720         echo "done"
6721
6722         return 0
6723 }
6724
6725 test_56xb() {
6726         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
6727                 skip "Need MDS version at least 2.10.55"
6728
6729         local dir="$DIR/$tdir"
6730
6731         test_mkdir "$dir" || error "cannot create dir $dir"
6732
6733         echo "testing lfs migrate mode when all links fit within xattrs"
6734         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
6735
6736         echo "testing rsync mode when all links fit within xattrs"
6737         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
6738
6739         echo "testing lfs migrate mode when all links do not fit within xattrs"
6740         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
6741
6742         echo "testing rsync mode when all links do not fit within xattrs"
6743         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
6744
6745         chown -R $RUNAS_ID $dir
6746         echo "testing non-root lfs migrate mode when not all links are in xattr"
6747         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100 "$RUNAS"
6748
6749         # clean up
6750         rm -rf $dir
6751 }
6752 run_test 56xb "lfs migration hard link support"
6753
6754 test_56xc() {
6755         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6756
6757         local dir="$DIR/$tdir"
6758
6759         test_mkdir "$dir" || error "cannot create dir $dir"
6760
6761         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
6762         echo -n "Setting initial stripe for 20MB test file..."
6763         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
6764                 error "cannot setstripe 20MB file"
6765         echo "done"
6766         echo -n "Sizing 20MB test file..."
6767         truncate "$dir/20mb" 20971520 || error "cannot create 20MB test file"
6768         echo "done"
6769         echo -n "Verifying small file autostripe count is 1..."
6770         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
6771                 error "cannot migrate 20MB file"
6772         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
6773                 error "cannot get stripe for $dir/20mb"
6774         [ $stripe_count -eq 1 ] ||
6775                 error "unexpected stripe count $stripe_count for 20MB file"
6776         rm -f "$dir/20mb"
6777         echo "done"
6778
6779         # Test 2: File is small enough to fit within the available space on
6780         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
6781         # have at least an additional 1KB for each desired stripe for test 3
6782         echo -n "Setting stripe for 1GB test file..."
6783         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
6784         echo "done"
6785         echo -n "Sizing 1GB test file..."
6786         # File size is 1GB + 3KB
6787         truncate "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
6788         echo "done"
6789
6790         # need at least 512MB per OST for 1GB file to fit in 2 stripes
6791         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
6792         if (( avail > 524288 * OSTCOUNT )); then
6793                 echo -n "Migrating 1GB file..."
6794                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
6795                         error "cannot migrate 1GB file"
6796                 echo "done"
6797                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
6798                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
6799                         error "cannot getstripe for 1GB file"
6800                 [ $stripe_count -eq 2 ] ||
6801                         error "unexpected stripe count $stripe_count != 2"
6802                 echo "done"
6803         fi
6804
6805         # Test 3: File is too large to fit within the available space on
6806         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
6807         if [ $OSTCOUNT -ge 3 ]; then
6808                 # The required available space is calculated as
6809                 # file size (1GB + 3KB) / OST count (3).
6810                 local kb_per_ost=349526
6811
6812                 echo -n "Migrating 1GB file with limit..."
6813                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
6814                         error "cannot migrate 1GB file with limit"
6815                 echo "done"
6816
6817                 stripe_count=$($LFS getstripe -c "$dir/1gb")
6818                 echo -n "Verifying 1GB autostripe count with limited space..."
6819                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
6820                         error "unexpected stripe count $stripe_count (min 3)"
6821                 echo "done"
6822         fi
6823
6824         # clean up
6825         rm -rf $dir
6826 }
6827 run_test 56xc "lfs migration autostripe"
6828
6829 test_56y() {
6830         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
6831                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
6832
6833         local res=""
6834         local dir=$DIR/$tdir
6835         local f1=$dir/file1
6836         local f2=$dir/file2
6837
6838         test_mkdir -p $dir || error "creating dir $dir"
6839         touch $f1 || error "creating std file $f1"
6840         $MULTIOP $f2 H2c || error "creating released file $f2"
6841
6842         # a directory can be raid0, so ask only for files
6843         res=$($LFS find $dir -L raid0 -type f | wc -l)
6844         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
6845
6846         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
6847         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
6848
6849         # only files can be released, so no need to force file search
6850         res=$($LFS find $dir -L released)
6851         [[ $res == $f2 ]] || error "search released: found $res != $f2"
6852
6853         res=$($LFS find $dir -type f \! -L released)
6854         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
6855 }
6856 run_test 56y "lfs find -L raid0|released"
6857
6858 test_56z() { # LU-4824
6859         # This checks to make sure 'lfs find' continues after errors
6860         # There are two classes of errors that should be caught:
6861         # - If multiple paths are provided, all should be searched even if one
6862         #   errors out
6863         # - If errors are encountered during the search, it should not terminate
6864         #   early
6865         local dir=$DIR/$tdir
6866         local i
6867
6868         test_mkdir $dir
6869         for i in d{0..9}; do
6870                 test_mkdir $dir/$i
6871                 touch $dir/$i/$tfile
6872         done
6873         $LFS find $DIR/non_existent_dir $dir &&
6874                 error "$LFS find did not return an error"
6875         # Make a directory unsearchable. This should NOT be the last entry in
6876         # directory order.  Arbitrarily pick the 6th entry
6877         chmod 700 $($LFS find $dir -type d | sed '6!d')
6878
6879         $RUNAS $LFS find $DIR/non_existent $dir
6880         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
6881
6882         # The user should be able to see 10 directories and 9 files
6883         (( count == 19 )) ||
6884                 error "$LFS find found $count != 19 entries after error"
6885 }
6886 run_test 56z "lfs find should continue after an error"
6887
6888 test_56aa() { # LU-5937
6889         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
6890
6891         local dir=$DIR/$tdir
6892
6893         mkdir $dir
6894         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
6895
6896         createmany -o $dir/striped_dir/${tfile}- 1024
6897         local dirs=$($LFS find --size +8k $dir/)
6898
6899         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
6900 }
6901 run_test 56aa "lfs find --size under striped dir"
6902
6903 test_56ab() { # LU-10705
6904         test_mkdir $DIR/$tdir
6905         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
6906         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
6907         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
6908         # Flush writes to ensure valid blocks.  Need to be more thorough for
6909         # ZFS, since blocks are not allocated/returned to client immediately.
6910         sync_all_data
6911         wait_zfs_commit ost1 2
6912         cancel_lru_locks osc
6913         ls -ls $DIR/$tdir
6914
6915         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
6916
6917         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
6918
6919         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
6920         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
6921
6922         rm -f $DIR/$tdir/$tfile.[123]
6923 }
6924 run_test 56ab "lfs find --blocks"
6925
6926 test_56ba() {
6927         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
6928                 skip "Need MDS version at least 2.10.50"
6929
6930         # Create composite files with one component
6931         local dir=$DIR/$tdir
6932
6933         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
6934         # Create composite files with three components
6935         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
6936         # Create non-composite files
6937         createmany -o $dir/${tfile}- 10
6938
6939         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
6940
6941         [[ $nfiles == 10 ]] ||
6942                 error "lfs find -E 1M found $nfiles != 10 files"
6943
6944         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
6945         [[ $nfiles == 25 ]] ||
6946                 error "lfs find ! -E 1M found $nfiles != 25 files"
6947
6948         # All files have a component that starts at 0
6949         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
6950         [[ $nfiles == 35 ]] ||
6951                 error "lfs find --component-start 0 - $nfiles != 35 files"
6952
6953         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
6954         [[ $nfiles == 15 ]] ||
6955                 error "lfs find --component-start 2M - $nfiles != 15 files"
6956
6957         # All files created here have a componenet that does not starts at 2M
6958         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
6959         [[ $nfiles == 35 ]] ||
6960                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
6961
6962         # Find files with a specified number of components
6963         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
6964         [[ $nfiles == 15 ]] ||
6965                 error "lfs find --component-count 3 - $nfiles != 15 files"
6966
6967         # Remember non-composite files have a component count of zero
6968         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
6969         [[ $nfiles == 10 ]] ||
6970                 error "lfs find --component-count 0 - $nfiles != 10 files"
6971
6972         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
6973         [[ $nfiles == 20 ]] ||
6974                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
6975
6976         # All files have a flag called "init"
6977         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
6978         [[ $nfiles == 35 ]] ||
6979                 error "lfs find --component-flags init - $nfiles != 35 files"
6980
6981         # Multi-component files will have a component not initialized
6982         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
6983         [[ $nfiles == 15 ]] ||
6984                 error "lfs find !--component-flags init - $nfiles != 15 files"
6985
6986         rm -rf $dir
6987
6988 }
6989 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
6990
6991 test_56ca() {
6992         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
6993                 skip "Need MDS version at least 2.10.57"
6994
6995         local td=$DIR/$tdir
6996         local tf=$td/$tfile
6997         local dir
6998         local nfiles
6999         local cmd
7000         local i
7001         local j
7002
7003         # create mirrored directories and mirrored files
7004         mkdir $td || error "mkdir $td failed"
7005         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
7006         createmany -o $tf- 10 || error "create $tf- failed"
7007
7008         for i in $(seq 2); do
7009                 dir=$td/dir$i
7010                 mkdir $dir || error "mkdir $dir failed"
7011                 $LFS mirror create -N$((3 + i)) $dir ||
7012                         error "create mirrored dir $dir failed"
7013                 createmany -o $dir/$tfile- 10 ||
7014                         error "create $dir/$tfile- failed"
7015         done
7016
7017         # change the states of some mirrored files
7018         echo foo > $tf-6
7019         for i in $(seq 2); do
7020                 dir=$td/dir$i
7021                 for j in $(seq 4 9); do
7022                         echo foo > $dir/$tfile-$j
7023                 done
7024         done
7025
7026         # find mirrored files with specific mirror count
7027         cmd="$LFS find --mirror-count 3 --type f $td"
7028         nfiles=$($cmd | wc -l)
7029         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
7030
7031         cmd="$LFS find ! --mirror-count 3 --type f $td"
7032         nfiles=$($cmd | wc -l)
7033         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
7034
7035         cmd="$LFS find --mirror-count +2 --type f $td"
7036         nfiles=$($cmd | wc -l)
7037         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7038
7039         cmd="$LFS find --mirror-count -6 --type f $td"
7040         nfiles=$($cmd | wc -l)
7041         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7042
7043         # find mirrored files with specific file state
7044         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
7045         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
7046
7047         cmd="$LFS find --mirror-state=ro --type f $td"
7048         nfiles=$($cmd | wc -l)
7049         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
7050
7051         cmd="$LFS find ! --mirror-state=ro --type f $td"
7052         nfiles=$($cmd | wc -l)
7053         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7054
7055         cmd="$LFS find --mirror-state=wp --type f $td"
7056         nfiles=$($cmd | wc -l)
7057         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7058
7059         cmd="$LFS find ! --mirror-state=sp --type f $td"
7060         nfiles=$($cmd | wc -l)
7061         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7062 }
7063 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
7064
7065 test_57a() {
7066         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7067         # note test will not do anything if MDS is not local
7068         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7069                 skip_env "ldiskfs only test"
7070         fi
7071         remote_mds_nodsh && skip "remote MDS with nodsh"
7072
7073         local MNTDEV="osd*.*MDT*.mntdev"
7074         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
7075         [ -z "$DEV" ] && error "can't access $MNTDEV"
7076         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
7077                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
7078                         error "can't access $DEV"
7079                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
7080                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
7081                 rm $TMP/t57a.dump
7082         done
7083 }
7084 run_test 57a "verify MDS filesystem created with large inodes =="
7085
7086 test_57b() {
7087         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7088         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7089                 skip_env "ldiskfs only test"
7090         fi
7091         remote_mds_nodsh && skip "remote MDS with nodsh"
7092
7093         local dir=$DIR/$tdir
7094         local filecount=100
7095         local file1=$dir/f1
7096         local fileN=$dir/f$filecount
7097
7098         rm -rf $dir || error "removing $dir"
7099         test_mkdir -c1 $dir
7100         local mdtidx=$($LFS getstripe -m $dir)
7101         local mdtname=MDT$(printf %04x $mdtidx)
7102         local facet=mds$((mdtidx + 1))
7103
7104         echo "mcreating $filecount files"
7105         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
7106
7107         # verify that files do not have EAs yet
7108         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
7109                 error "$file1 has an EA"
7110         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
7111                 error "$fileN has an EA"
7112
7113         sync
7114         sleep 1
7115         df $dir  #make sure we get new statfs data
7116         local mdsfree=$(do_facet $facet \
7117                         lctl get_param -n osd*.*$mdtname.kbytesfree)
7118         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7119         local file
7120
7121         echo "opening files to create objects/EAs"
7122         for file in $(seq -f $dir/f%g 1 $filecount); do
7123                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
7124                         error "opening $file"
7125         done
7126
7127         # verify that files have EAs now
7128         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
7129         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
7130
7131         sleep 1  #make sure we get new statfs data
7132         df $dir
7133         local mdsfree2=$(do_facet $facet \
7134                          lctl get_param -n osd*.*$mdtname.kbytesfree)
7135         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7136
7137         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
7138                 if [ "$mdsfree" != "$mdsfree2" ]; then
7139                         error "MDC before $mdcfree != after $mdcfree2"
7140                 else
7141                         echo "MDC before $mdcfree != after $mdcfree2"
7142                         echo "unable to confirm if MDS has large inodes"
7143                 fi
7144         fi
7145         rm -rf $dir
7146 }
7147 run_test 57b "default LOV EAs are stored inside large inodes ==="
7148
7149 test_58() {
7150         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7151         [ -z "$(which wiretest 2>/dev/null)" ] &&
7152                         skip_env "could not find wiretest"
7153
7154         wiretest
7155 }
7156 run_test 58 "verify cross-platform wire constants =============="
7157
7158 test_59() {
7159         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7160
7161         echo "touch 130 files"
7162         createmany -o $DIR/f59- 130
7163         echo "rm 130 files"
7164         unlinkmany $DIR/f59- 130
7165         sync
7166         # wait for commitment of removal
7167         wait_delete_completed
7168 }
7169 run_test 59 "verify cancellation of llog records async ========="
7170
7171 TEST60_HEAD="test_60 run $RANDOM"
7172 test_60a() {
7173         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7174         remote_mgs_nodsh && skip "remote MGS with nodsh"
7175         do_facet mgs "! which run-llog.sh &> /dev/null" &&
7176                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
7177                         skip_env "missing subtest run-llog.sh"
7178
7179         log "$TEST60_HEAD - from kernel mode"
7180         do_facet mgs "$LCTL dk > /dev/null"
7181         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
7182         do_facet mgs $LCTL dk > $TMP/$tfile
7183
7184         # LU-6388: test llog_reader
7185         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
7186         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
7187         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
7188                         skip_env "missing llog_reader"
7189         local fstype=$(facet_fstype mgs)
7190         [ $fstype != ldiskfs -a $fstype != zfs ] &&
7191                 skip_env "Only for ldiskfs or zfs type mgs"
7192
7193         local mntpt=$(facet_mntpt mgs)
7194         local mgsdev=$(mgsdevname 1)
7195         local fid_list
7196         local fid
7197         local rec_list
7198         local rec
7199         local rec_type
7200         local obj_file
7201         local path
7202         local seq
7203         local oid
7204         local pass=true
7205
7206         #get fid and record list
7207         fid_list=($(awk '/9_sub.*record/ { print $NF }' $TMP/$tfile |
7208                 tail -n 4))
7209         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' $TMP/$tfile |
7210                 tail -n 4))
7211         #remount mgs as ldiskfs or zfs type
7212         stop mgs || error "stop mgs failed"
7213         mount_fstype mgs || error "remount mgs failed"
7214         for ((i = 0; i < ${#fid_list[@]}; i++)); do
7215                 fid=${fid_list[i]}
7216                 rec=${rec_list[i]}
7217                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
7218                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
7219                 oid=$((16#$oid))
7220
7221                 case $fstype in
7222                         ldiskfs )
7223                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
7224                         zfs )
7225                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
7226                 esac
7227                 echo "obj_file is $obj_file"
7228                 do_facet mgs $llog_reader $obj_file
7229
7230                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
7231                         awk '{ print $3 }' | sed -e "s/^type=//g")
7232                 if [ $rec_type != $rec ]; then
7233                         echo "FAILED test_60a wrong record type $rec_type," \
7234                               "should be $rec"
7235                         pass=false
7236                         break
7237                 fi
7238
7239                 #check obj path if record type is LLOG_LOGID_MAGIC
7240                 if [ "$rec" == "1064553b" ]; then
7241                         path=$(do_facet mgs $llog_reader $obj_file |
7242                                 grep "path=" | awk '{ print $NF }' |
7243                                 sed -e "s/^path=//g")
7244                         if [ $obj_file != $mntpt/$path ]; then
7245                                 echo "FAILED test_60a wrong obj path" \
7246                                       "$montpt/$path, should be $obj_file"
7247                                 pass=false
7248                                 break
7249                         fi
7250                 fi
7251         done
7252         rm -f $TMP/$tfile
7253         #restart mgs before "error", otherwise it will block the next test
7254         stop mgs || error "stop mgs failed"
7255         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
7256         $pass || error "test failed, see FAILED test_60a messages for specifics"
7257 }
7258 run_test 60a "llog_test run from kernel module and test llog_reader"
7259
7260 test_60b() { # bug 6411
7261         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7262
7263         dmesg > $DIR/$tfile
7264         LLOG_COUNT=$(do_facet mgs dmesg |
7265                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
7266                           /llog_[a-z]*.c:[0-9]/ {
7267                                 if (marker)
7268                                         from_marker++
7269                                 from_begin++
7270                           }
7271                           END {
7272                                 if (marker)
7273                                         print from_marker
7274                                 else
7275                                         print from_begin
7276                           }")
7277
7278         [[ $LLOG_COUNT -gt 120 ]] &&
7279                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
7280 }
7281 run_test 60b "limit repeated messages from CERROR/CWARN"
7282
7283 test_60c() {
7284         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7285
7286         echo "create 5000 files"
7287         createmany -o $DIR/f60c- 5000
7288 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
7289         lctl set_param fail_loc=0x80000137
7290         unlinkmany $DIR/f60c- 5000
7291         lctl set_param fail_loc=0
7292 }
7293 run_test 60c "unlink file when mds full"
7294
7295 test_60d() {
7296         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7297
7298         SAVEPRINTK=$(lctl get_param -n printk)
7299         # verify "lctl mark" is even working"
7300         MESSAGE="test message ID $RANDOM $$"
7301         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7302         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
7303
7304         lctl set_param printk=0 || error "set lnet.printk failed"
7305         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
7306         MESSAGE="new test message ID $RANDOM $$"
7307         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
7308         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7309         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
7310
7311         lctl set_param -n printk="$SAVEPRINTK"
7312 }
7313 run_test 60d "test printk console message masking"
7314
7315 test_60e() {
7316         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7317         remote_mds_nodsh && skip "remote MDS with nodsh"
7318
7319         touch $DIR/$tfile
7320 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
7321         do_facet mds1 lctl set_param fail_loc=0x15b
7322         rm $DIR/$tfile
7323 }
7324 run_test 60e "no space while new llog is being created"
7325
7326 test_60g() {
7327         local pid
7328         local i
7329
7330         test_mkdir -c $MDSCOUNT $DIR/$tdir
7331
7332         (
7333                 local index=0
7334                 while true; do
7335                         $LFS setdirstripe -i $(($index % $MDSCOUNT)) \
7336                                 -c $MDSCOUNT $DIR/$tdir/subdir$index \
7337                                 2>/dev/null
7338                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
7339                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
7340                         index=$((index + 1))
7341                 done
7342         ) &
7343
7344         pid=$!
7345
7346         for i in {0..100}; do
7347                 # define OBD_FAIL_OSD_TXN_START    0x19a
7348                 local index=$((i % MDSCOUNT + 1))
7349
7350                 do_facet mds$index $LCTL set_param fail_loc=0x8000019a \
7351                         > /dev/null
7352                 usleep 100
7353         done
7354
7355         kill -9 $pid
7356
7357         for i in $(seq $MDSCOUNT); do
7358                 do_facet mds$i $LCTL set_param fail_loc=0 > /dev/null
7359         done
7360
7361         mkdir $DIR/$tdir/new || error "mkdir failed"
7362         rmdir $DIR/$tdir/new || error "rmdir failed"
7363
7364         do_facet mds1 $LCTL lfsck_start -M $(facet_svc mds1) -A -C \
7365                 -t namespace
7366         for i in $(seq $MDSCOUNT); do
7367                 wait_update_facet mds$i "$LCTL get_param -n \
7368                         mdd.$(facet_svc mds$i).lfsck_namespace |
7369                         awk '/^status/ { print \\\$2 }'" "completed"
7370         done
7371
7372         ls -R $DIR/$tdir || error "ls failed"
7373         rm -rf $DIR/$tdir || error "rmdir failed"
7374 }
7375 run_test 60g "transaction abort won't cause MDT hung"
7376
7377 test_60h() {
7378         [ $MDS1_VERSION -le $(version_code 2.12.52) ] &&
7379                 skip "Need MDS version at least 2.12.52"
7380         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
7381
7382         local f
7383
7384         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
7385         #define OBD_FAIL_MDS_STRIPE_FID          0x189
7386         for fail_loc in 0x80000188 0x80000189; do
7387                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
7388                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
7389                         error "mkdir $dir-$fail_loc failed"
7390                 for i in {0..10}; do
7391                         # create may fail on missing stripe
7392                         echo $i > $DIR/$tdir-$fail_loc/$i
7393                 done
7394                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7395                         error "getdirstripe $tdir-$fail_loc failed"
7396                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
7397                         error "migrate $tdir-$fail_loc failed"
7398                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7399                         error "getdirstripe $tdir-$fail_loc failed"
7400                 pushd $DIR/$tdir-$fail_loc
7401                 for f in *; do
7402                         echo $f | cmp $f - || error "$f data mismatch"
7403                 done
7404                 popd
7405                 rm -rf $DIR/$tdir-$fail_loc
7406         done
7407 }
7408 run_test 60h "striped directory with missing stripes can be accessed"
7409
7410 test_61a() {
7411         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7412
7413         f="$DIR/f61"
7414         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
7415         cancel_lru_locks osc
7416         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
7417         sync
7418 }
7419 run_test 61a "mmap() writes don't make sync hang ================"
7420
7421 test_61b() {
7422         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
7423 }
7424 run_test 61b "mmap() of unstriped file is successful"
7425
7426 # bug 2330 - insufficient obd_match error checking causes LBUG
7427 test_62() {
7428         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7429
7430         f="$DIR/f62"
7431         echo foo > $f
7432         cancel_lru_locks osc
7433         lctl set_param fail_loc=0x405
7434         cat $f && error "cat succeeded, expect -EIO"
7435         lctl set_param fail_loc=0
7436 }
7437 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
7438 # match every page all of the time.
7439 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
7440
7441 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
7442 # Though this test is irrelevant anymore, it helped to reveal some
7443 # other grant bugs (LU-4482), let's keep it.
7444 test_63a() {   # was test_63
7445         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7446
7447         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
7448
7449         for i in `seq 10` ; do
7450                 dd if=/dev/zero of=$DIR/f63 bs=8k &
7451                 sleep 5
7452                 kill $!
7453                 sleep 1
7454         done
7455
7456         rm -f $DIR/f63 || true
7457 }
7458 run_test 63a "Verify oig_wait interruption does not crash ======="
7459
7460 # bug 2248 - async write errors didn't return to application on sync
7461 # bug 3677 - async write errors left page locked
7462 test_63b() {
7463         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7464
7465         debugsave
7466         lctl set_param debug=-1
7467
7468         # ensure we have a grant to do async writes
7469         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
7470         rm $DIR/$tfile
7471
7472         sync    # sync lest earlier test intercept the fail_loc
7473
7474         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7475         lctl set_param fail_loc=0x80000406
7476         $MULTIOP $DIR/$tfile Owy && \
7477                 error "sync didn't return ENOMEM"
7478         sync; sleep 2; sync     # do a real sync this time to flush page
7479         lctl get_param -n llite.*.dump_page_cache | grep locked && \
7480                 error "locked page left in cache after async error" || true
7481         debugrestore
7482 }
7483 run_test 63b "async write errors should be returned to fsync ==="
7484
7485 test_64a () {
7486         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7487
7488         df $DIR
7489         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
7490 }
7491 run_test 64a "verify filter grant calculations (in kernel) ====="
7492
7493 test_64b () {
7494         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7495
7496         sh oos.sh $MOUNT || error "oos.sh failed: $?"
7497 }
7498 run_test 64b "check out-of-space detection on client"
7499
7500 test_64c() {
7501         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
7502 }
7503 run_test 64c "verify grant shrink"
7504
7505 # this does exactly what osc_request.c:osc_announce_cached() does in
7506 # order to calculate max amount of grants to ask from server
7507 want_grant() {
7508         local tgt=$1
7509
7510         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
7511         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
7512
7513         ((rpc_in_flight ++));
7514         nrpages=$((nrpages * rpc_in_flight))
7515
7516         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
7517
7518         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
7519
7520         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
7521         local undirty=$((nrpages * PAGE_SIZE))
7522
7523         local max_extent_pages
7524         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
7525             grep grant_max_extent_size | awk '{print $2}')
7526         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
7527         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
7528         local grant_extent_tax
7529         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7530             grep grant_extent_tax | awk '{print $2}')
7531
7532         undirty=$((undirty + nrextents * grant_extent_tax))
7533
7534         echo $undirty
7535 }
7536
7537 # this is size of unit for grant allocation. It should be equal to
7538 # what tgt_grant.c:tgt_grant_chunk() calculates
7539 grant_chunk() {
7540         local tgt=$1
7541         local max_brw_size
7542         local grant_extent_tax
7543
7544         max_brw_size=$($LCTL get_param osc.${tgt}.import |
7545             grep max_brw_size | awk '{print $2}')
7546
7547         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7548             grep grant_extent_tax | awk '{print $2}')
7549
7550         echo $(((max_brw_size + grant_extent_tax) * 2))
7551 }
7552
7553 test_64d() {
7554         [ $OST1_VERSION -lt $(version_code 2.10.56) ] &&
7555                 skip "OST < 2.10.55 doesn't limit grants enough"
7556
7557         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
7558         local file=$DIR/$tfile
7559
7560         [[ $($LCTL get_param osc.${tgt}.import |
7561              grep "connect_flags:.*grant_param") ]] ||
7562                 skip "no grant_param connect flag"
7563
7564         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
7565
7566         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
7567
7568         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
7569         stack_trap "rm -f $file" EXIT
7570
7571         $LFS setstripe $file -i 0 -c 1
7572         dd if=/dev/zero of=$file bs=1M count=1000 &
7573         ddpid=$!
7574
7575         while true
7576         do
7577                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
7578                 if [[ $cur_grant -gt $max_cur_granted ]]
7579                 then
7580                         kill $ddpid
7581                         error "cur_grant $cur_grant > $max_cur_granted"
7582                 fi
7583                 kill -0 $ddpid
7584                 [[ $? -ne 0 ]] && break;
7585                 sleep 2
7586         done
7587
7588         rm -f $DIR/$tfile
7589         wait_delete_completed
7590         $LCTL set_param debug="$olddebug" 2> /dev/null || true
7591 }
7592 run_test 64d "check grant limit exceed"
7593
7594 # bug 1414 - set/get directories' stripe info
7595 test_65a() {
7596         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7597
7598         test_mkdir $DIR/$tdir
7599         touch $DIR/$tdir/f1
7600         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
7601 }
7602 run_test 65a "directory with no stripe info"
7603
7604 test_65b() {
7605         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7606
7607         test_mkdir $DIR/$tdir
7608         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7609
7610         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7611                                                 error "setstripe"
7612         touch $DIR/$tdir/f2
7613         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
7614 }
7615 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
7616
7617 test_65c() {
7618         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7619         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
7620
7621         test_mkdir $DIR/$tdir
7622         local stripesize=$($LFS getstripe -S $DIR/$tdir)
7623
7624         $LFS setstripe -S $((stripesize * 4)) -i 1 \
7625                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
7626         touch $DIR/$tdir/f3
7627         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
7628 }
7629 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
7630
7631 test_65d() {
7632         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7633
7634         test_mkdir $DIR/$tdir
7635         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
7636         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7637
7638         if [[ $STRIPECOUNT -le 0 ]]; then
7639                 sc=1
7640         elif [[ $STRIPECOUNT -gt $LOV_MAX_STRIPE_COUNT ]]; then
7641                 [[ $OSTCOUNT -gt $LOV_MAX_STRIPE_COUNT ]] &&
7642                         sc=$LOV_MAX_STRIPE_COUNT || sc=$(($OSTCOUNT - 1))
7643         else
7644                 sc=$(($STRIPECOUNT - 1))
7645         fi
7646         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
7647         touch $DIR/$tdir/f4 $DIR/$tdir/f5
7648         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
7649                 error "lverify failed"
7650 }
7651 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
7652
7653 test_65e() {
7654         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7655
7656         test_mkdir $DIR/$tdir
7657
7658         $LFS setstripe $DIR/$tdir || error "setstripe"
7659         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7660                                         error "no stripe info failed"
7661         touch $DIR/$tdir/f6
7662         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
7663 }
7664 run_test 65e "directory setstripe defaults"
7665
7666 test_65f() {
7667         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7668
7669         test_mkdir $DIR/${tdir}f
7670         $RUNAS $LFS setstripe $DIR/${tdir}f &&
7671                 error "setstripe succeeded" || true
7672 }
7673 run_test 65f "dir setstripe permission (should return error) ==="
7674
7675 test_65g() {
7676         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7677
7678         test_mkdir $DIR/$tdir
7679         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7680
7681         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7682                 error "setstripe -S failed"
7683         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
7684         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7685                 error "delete default stripe failed"
7686 }
7687 run_test 65g "directory setstripe -d"
7688
7689 test_65h() {
7690         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7691
7692         test_mkdir $DIR/$tdir
7693         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7694
7695         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7696                 error "setstripe -S failed"
7697         test_mkdir $DIR/$tdir/dd1
7698         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
7699                 error "stripe info inherit failed"
7700 }
7701 run_test 65h "directory stripe info inherit ===================="
7702
7703 test_65i() {
7704         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7705
7706         save_layout_restore_at_exit $MOUNT
7707
7708         # bug6367: set non-default striping on root directory
7709         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
7710
7711         # bug12836: getstripe on -1 default directory striping
7712         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
7713
7714         # bug12836: getstripe -v on -1 default directory striping
7715         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
7716
7717         # bug12836: new find on -1 default directory striping
7718         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
7719 }
7720 run_test 65i "various tests to set root directory striping"
7721
7722 test_65j() { # bug6367
7723         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7724
7725         sync; sleep 1
7726
7727         # if we aren't already remounting for each test, do so for this test
7728         if [ "$I_MOUNTED" = "yes" ]; then
7729                 cleanup || error "failed to unmount"
7730                 setup
7731         fi
7732
7733         save_layout_restore_at_exit $MOUNT
7734
7735         $LFS setstripe -d $MOUNT || error "setstripe failed"
7736 }
7737 run_test 65j "set default striping on root directory (bug 6367)="
7738
7739 cleanup_65k() {
7740         rm -rf $DIR/$tdir
7741         wait_delete_completed
7742         do_facet $SINGLEMDS "lctl set_param -n \
7743                 osp.$ost*MDT0000.max_create_count=$max_count"
7744         do_facet $SINGLEMDS "lctl set_param -n \
7745                 osp.$ost*MDT0000.create_count=$count"
7746         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7747         echo $INACTIVE_OSC "is Activate"
7748
7749         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7750 }
7751
7752 test_65k() { # bug11679
7753         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7754         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7755         remote_mds_nodsh && skip "remote MDS with nodsh"
7756
7757         local disable_precreate=true
7758         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
7759                 disable_precreate=false
7760
7761         echo "Check OST status: "
7762         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
7763                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
7764
7765         for OSC in $MDS_OSCS; do
7766                 echo $OSC "is active"
7767                 do_facet $SINGLEMDS lctl --device %$OSC activate
7768         done
7769
7770         for INACTIVE_OSC in $MDS_OSCS; do
7771                 local ost=$(osc_to_ost $INACTIVE_OSC)
7772                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
7773                                lov.*md*.target_obd |
7774                                awk -F: /$ost/'{ print $1 }' | head -n 1)
7775
7776                 mkdir -p $DIR/$tdir
7777                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
7778                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
7779
7780                 echo "Deactivate: " $INACTIVE_OSC
7781                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
7782
7783                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
7784                               osp.$ost*MDT0000.create_count")
7785                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
7786                                   osp.$ost*MDT0000.max_create_count")
7787                 $disable_precreate &&
7788                         do_facet $SINGLEMDS "lctl set_param -n \
7789                                 osp.$ost*MDT0000.max_create_count=0"
7790
7791                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
7792                         [ -f $DIR/$tdir/$idx ] && continue
7793                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
7794                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
7795                                 { cleanup_65k;
7796                                   error "setstripe $idx should succeed"; }
7797                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
7798                 done
7799                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
7800                 rmdir $DIR/$tdir
7801
7802                 do_facet $SINGLEMDS "lctl set_param -n \
7803                         osp.$ost*MDT0000.max_create_count=$max_count"
7804                 do_facet $SINGLEMDS "lctl set_param -n \
7805                         osp.$ost*MDT0000.create_count=$count"
7806                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7807                 echo $INACTIVE_OSC "is Activate"
7808
7809                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7810         done
7811 }
7812 run_test 65k "validate manual striping works properly with deactivated OSCs"
7813
7814 test_65l() { # bug 12836
7815         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7816
7817         test_mkdir -p $DIR/$tdir/test_dir
7818         $LFS setstripe -c -1 $DIR/$tdir/test_dir
7819         $LFS find -mtime -1 $DIR/$tdir >/dev/null
7820 }
7821 run_test 65l "lfs find on -1 stripe dir ========================"
7822
7823 test_65m() {
7824         local layout=$(save_layout $MOUNT)
7825         $RUNAS $LFS setstripe -c 2 $MOUNT && {
7826                 restore_layout $MOUNT $layout
7827                 error "setstripe should fail by non-root users"
7828         }
7829         true
7830 }
7831 run_test 65m "normal user can't set filesystem default stripe"
7832
7833 test_65n() {
7834         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
7835         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.12.50) ]] ||
7836                 skip "Need MDS version at least 2.12.50"
7837         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
7838
7839         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7840         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
7841         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
7842
7843         local root_layout=$(save_layout $MOUNT)
7844         stack_trap "restore_layout $MOUNT $root_layout" EXIT
7845
7846         # new subdirectory under root directory should not inherit
7847         # the default layout from root
7848         local dir1=$MOUNT/$tdir-1
7849         mkdir $dir1 || error "mkdir $dir1 failed"
7850         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
7851                 error "$dir1 shouldn't have LOV EA"
7852
7853         # delete the default layout on root directory
7854         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
7855
7856         local dir2=$MOUNT/$tdir-2
7857         mkdir $dir2 || error "mkdir $dir2 failed"
7858         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
7859                 error "$dir2 shouldn't have LOV EA"
7860
7861         # set a new striping pattern on root directory
7862         local def_stripe_size=$($LFS getstripe -S $MOUNT)
7863         local new_def_stripe_size=$((def_stripe_size * 2))
7864         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
7865                 error "set stripe size on $MOUNT failed"
7866
7867         # new file created in $dir2 should inherit the new stripe size from
7868         # the filesystem default
7869         local file2=$dir2/$tfile-2
7870         touch $file2 || error "touch $file2 failed"
7871
7872         local file2_stripe_size=$($LFS getstripe -S $file2)
7873         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
7874                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
7875
7876         local dir3=$MOUNT/$tdir-3
7877         mkdir $dir3 || error "mkdir $dir3 failed"
7878         ! getfattr -n trusted.lov $dir3 &> /dev/null ||
7879                 error "$dir3 shouldn't have LOV EA"
7880
7881         # set OST pool on root directory
7882         local pool=$TESTNAME
7883         pool_add $pool || error "add $pool failed"
7884         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
7885                 error "add targets to $pool failed"
7886
7887         $LFS setstripe -p $pool $MOUNT ||
7888                 error "set OST pool on $MOUNT failed"
7889
7890         # new file created in $dir3 should inherit the pool from
7891         # the filesystem default
7892         local file3=$dir3/$tfile-3
7893         touch $file3 || error "touch $file3 failed"
7894
7895         local file3_pool=$($LFS getstripe -p $file3)
7896         [[ "$file3_pool" = "$pool" ]] ||
7897                 error "$file3 didn't inherit OST pool $pool"
7898
7899         local dir4=$MOUNT/$tdir-4
7900         mkdir $dir4 || error "mkdir $dir4 failed"
7901         ! getfattr -n trusted.lov $dir4 &> /dev/null ||
7902                 error "$dir4 shouldn't have LOV EA"
7903
7904         # new file created in $dir4 should inherit the pool from
7905         # the filesystem default
7906         local file4=$dir4/$tfile-4
7907         touch $file4 || error "touch $file4 failed"
7908
7909         local file4_pool=$($LFS getstripe -p $file4)
7910         [[ "$file4_pool" = "$pool" ]] ||
7911                 error "$file4 didn't inherit OST pool $pool"
7912
7913         # new subdirectory under non-root directory should inherit
7914         # the default layout from its parent directory
7915         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
7916                 error "set directory layout on $dir4 failed"
7917
7918         local dir5=$dir4/$tdir-5
7919         mkdir $dir5 || error "mkdir $dir5 failed"
7920
7921         local dir4_layout=$(get_layout_param $dir4)
7922         local dir5_layout=$(get_layout_param $dir5)
7923         [[ "$dir4_layout" = "$dir5_layout" ]] ||
7924                 error "$dir5 should inherit the default layout from $dir4"
7925
7926         # though subdir under ROOT doesn't inherit default layout, but
7927         # its sub dir/file should be created with default layout.
7928         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
7929         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
7930                 skip "Need MDS version at least 2.12.59"
7931
7932         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
7933         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
7934         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
7935
7936         if [ $default_lmv_hash == "none" ]; then
7937                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
7938         else
7939                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
7940                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
7941         fi
7942
7943         $LFS setdirstripe -D -c 2 $MOUNT ||
7944                 error "setdirstripe -D -c 2 failed"
7945         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
7946         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
7947         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
7948 }
7949 run_test 65n "don't inherit default layout from root for new subdirectories"
7950
7951 # bug 2543 - update blocks count on client
7952 test_66() {
7953         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7954
7955         COUNT=${COUNT:-8}
7956         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
7957         sync; sync_all_data; sync; sync_all_data
7958         cancel_lru_locks osc
7959         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
7960         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
7961 }
7962 run_test 66 "update inode blocks count on client ==============="
7963
7964 meminfo() {
7965         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
7966 }
7967
7968 swap_used() {
7969         swapon -s | awk '($1 == "'$1'") { print $4 }'
7970 }
7971
7972 # bug5265, obdfilter oa2dentry return -ENOENT
7973 # #define OBD_FAIL_SRV_ENOENT 0x217
7974 test_69() {
7975         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7976         remote_ost_nodsh && skip "remote OST with nodsh"
7977
7978         f="$DIR/$tfile"
7979         $LFS setstripe -c 1 -i 0 $f
7980
7981         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
7982
7983         do_facet ost1 lctl set_param fail_loc=0x217
7984         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
7985         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
7986
7987         do_facet ost1 lctl set_param fail_loc=0
7988         $DIRECTIO write $f 0 2 || error "write error"
7989
7990         cancel_lru_locks osc
7991         $DIRECTIO read $f 0 1 || error "read error"
7992
7993         do_facet ost1 lctl set_param fail_loc=0x217
7994         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
7995
7996         do_facet ost1 lctl set_param fail_loc=0
7997         rm -f $f
7998 }
7999 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
8000
8001 test_71() {
8002         test_mkdir $DIR/$tdir
8003         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
8004         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
8005 }
8006 run_test 71 "Running dbench on lustre (don't segment fault) ===="
8007
8008 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
8009         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8010         [ "$RUNAS_ID" = "$UID" ] &&
8011                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8012         # Check that testing environment is properly set up. Skip if not
8013         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
8014                 skip_env "User $RUNAS_ID does not exist - skipping"
8015
8016         touch $DIR/$tfile
8017         chmod 777 $DIR/$tfile
8018         chmod ug+s $DIR/$tfile
8019         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
8020                 error "$RUNAS dd $DIR/$tfile failed"
8021         # See if we are still setuid/sgid
8022         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8023                 error "S/gid is not dropped on write"
8024         # Now test that MDS is updated too
8025         cancel_lru_locks mdc
8026         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8027                 error "S/gid is not dropped on MDS"
8028         rm -f $DIR/$tfile
8029 }
8030 run_test 72a "Test that remove suid works properly (bug5695) ===="
8031
8032 test_72b() { # bug 24226 -- keep mode setting when size is not changing
8033         local perm
8034
8035         [ "$RUNAS_ID" = "$UID" ] &&
8036                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8037         [ "$RUNAS_ID" -eq 0 ] &&
8038                 skip_env "RUNAS_ID = 0 -- skipping"
8039         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8040         # Check that testing environment is properly set up. Skip if not
8041         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
8042                 skip_env "User $RUNAS_ID does not exist - skipping"
8043
8044         touch $DIR/${tfile}-f{g,u}
8045         test_mkdir $DIR/${tfile}-dg
8046         test_mkdir $DIR/${tfile}-du
8047         chmod 770 $DIR/${tfile}-{f,d}{g,u}
8048         chmod g+s $DIR/${tfile}-{f,d}g
8049         chmod u+s $DIR/${tfile}-{f,d}u
8050         for perm in 777 2777 4777; do
8051                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
8052                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
8053                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
8054                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
8055         done
8056         true
8057 }
8058 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
8059
8060 # bug 3462 - multiple simultaneous MDC requests
8061 test_73() {
8062         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8063
8064         test_mkdir $DIR/d73-1
8065         test_mkdir $DIR/d73-2
8066         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
8067         pid1=$!
8068
8069         lctl set_param fail_loc=0x80000129
8070         $MULTIOP $DIR/d73-1/f73-2 Oc &
8071         sleep 1
8072         lctl set_param fail_loc=0
8073
8074         $MULTIOP $DIR/d73-2/f73-3 Oc &
8075         pid3=$!
8076
8077         kill -USR1 $pid1
8078         wait $pid1 || return 1
8079
8080         sleep 25
8081
8082         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
8083         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
8084         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
8085
8086         rm -rf $DIR/d73-*
8087 }
8088 run_test 73 "multiple MDC requests (should not deadlock)"
8089
8090 test_74a() { # bug 6149, 6184
8091         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8092
8093         touch $DIR/f74a
8094         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8095         #
8096         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8097         # will spin in a tight reconnection loop
8098         $LCTL set_param fail_loc=0x8000030e
8099         # get any lock that won't be difficult - lookup works.
8100         ls $DIR/f74a
8101         $LCTL set_param fail_loc=0
8102         rm -f $DIR/f74a
8103         true
8104 }
8105 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
8106
8107 test_74b() { # bug 13310
8108         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8109
8110         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8111         #
8112         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8113         # will spin in a tight reconnection loop
8114         $LCTL set_param fail_loc=0x8000030e
8115         # get a "difficult" lock
8116         touch $DIR/f74b
8117         $LCTL set_param fail_loc=0
8118         rm -f $DIR/f74b
8119         true
8120 }
8121 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
8122
8123 test_74c() {
8124         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8125
8126         #define OBD_FAIL_LDLM_NEW_LOCK
8127         $LCTL set_param fail_loc=0x319
8128         touch $DIR/$tfile && error "touch successful"
8129         $LCTL set_param fail_loc=0
8130         true
8131 }
8132 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
8133
8134 num_inodes() {
8135         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
8136 }
8137
8138 test_76() { # Now for bug 20433, added originally in bug 1443
8139         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8140
8141         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
8142
8143         cancel_lru_locks osc
8144         BEFORE_INODES=$(num_inodes)
8145         echo "before inodes: $BEFORE_INODES"
8146         local COUNT=1000
8147         [ "$SLOW" = "no" ] && COUNT=100
8148         for i in $(seq $COUNT); do
8149                 touch $DIR/$tfile
8150                 rm -f $DIR/$tfile
8151         done
8152         cancel_lru_locks osc
8153         AFTER_INODES=$(num_inodes)
8154         echo "after inodes: $AFTER_INODES"
8155         local wait=0
8156         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
8157                 sleep 2
8158                 AFTER_INODES=$(num_inodes)
8159                 wait=$((wait+2))
8160                 echo "wait $wait seconds inodes: $AFTER_INODES"
8161                 if [ $wait -gt 30 ]; then
8162                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
8163                 fi
8164         done
8165 }
8166 run_test 76 "confirm clients recycle inodes properly ===="
8167
8168
8169 export ORIG_CSUM=""
8170 set_checksums()
8171 {
8172         # Note: in sptlrpc modes which enable its own bulk checksum, the
8173         # original crc32_le bulk checksum will be automatically disabled,
8174         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
8175         # will be checked by sptlrpc code against sptlrpc bulk checksum.
8176         # In this case set_checksums() will not be no-op, because sptlrpc
8177         # bulk checksum will be enabled all through the test.
8178
8179         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
8180         lctl set_param -n osc.*.checksums $1
8181         return 0
8182 }
8183
8184 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8185                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
8186 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8187                              tr -d [] | head -n1)}
8188 set_checksum_type()
8189 {
8190         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
8191         rc=$?
8192         log "set checksum type to $1, rc = $rc"
8193         return $rc
8194 }
8195
8196 get_osc_checksum_type()
8197 {
8198         # arugment 1: OST name, like OST0000
8199         ost=$1
8200         checksum_type=$(lctl get_param -n osc.*${ost}-osc-[^mM]*.checksum_type |
8201                         sed 's/.*\[\(.*\)\].*/\1/g')
8202         rc=$?
8203         [ $rc -ne 0 ] && error "failed to get checksum type of $ost, rc = $rc, output = $checksum_type"
8204         echo $checksum_type
8205 }
8206
8207 F77_TMP=$TMP/f77-temp
8208 F77SZ=8
8209 setup_f77() {
8210         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
8211                 error "error writing to $F77_TMP"
8212 }
8213
8214 test_77a() { # bug 10889
8215         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8216         $GSS && skip_env "could not run with gss"
8217
8218         [ ! -f $F77_TMP ] && setup_f77
8219         set_checksums 1
8220         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
8221         set_checksums 0
8222         rm -f $DIR/$tfile
8223 }
8224 run_test 77a "normal checksum read/write operation"
8225
8226 test_77b() { # bug 10889
8227         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8228         $GSS && skip_env "could not run with gss"
8229
8230         [ ! -f $F77_TMP ] && setup_f77
8231         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8232         $LCTL set_param fail_loc=0x80000409
8233         set_checksums 1
8234
8235         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8236                 error "dd error: $?"
8237         $LCTL set_param fail_loc=0
8238
8239         for algo in $CKSUM_TYPES; do
8240                 cancel_lru_locks osc
8241                 set_checksum_type $algo
8242                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8243                 $LCTL set_param fail_loc=0x80000408
8244                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
8245                 $LCTL set_param fail_loc=0
8246         done
8247         set_checksums 0
8248         set_checksum_type $ORIG_CSUM_TYPE
8249         rm -f $DIR/$tfile
8250 }
8251 run_test 77b "checksum error on client write, read"
8252
8253 cleanup_77c() {
8254         trap 0
8255         set_checksums 0
8256         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
8257         $check_ost &&
8258                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
8259         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
8260         $check_ost && [ -n "$ost_file_prefix" ] &&
8261                 do_facet ost1 rm -f ${ost_file_prefix}\*
8262 }
8263
8264 test_77c() {
8265         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8266         $GSS && skip_env "could not run with gss"
8267         remote_ost_nodsh && skip "remote OST with nodsh"
8268
8269         local bad1
8270         local osc_file_prefix
8271         local osc_file
8272         local check_ost=false
8273         local ost_file_prefix
8274         local ost_file
8275         local orig_cksum
8276         local dump_cksum
8277         local fid
8278
8279         # ensure corruption will occur on first OSS/OST
8280         $LFS setstripe -i 0 $DIR/$tfile
8281
8282         [ ! -f $F77_TMP ] && setup_f77
8283         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8284                 error "dd write error: $?"
8285         fid=$($LFS path2fid $DIR/$tfile)
8286
8287         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
8288         then
8289                 check_ost=true
8290                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
8291                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
8292         else
8293                 echo "OSS do not support bulk pages dump upon error"
8294         fi
8295
8296         osc_file_prefix=$($LCTL get_param -n debug_path)
8297         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
8298
8299         trap cleanup_77c EXIT
8300
8301         set_checksums 1
8302         # enable bulk pages dump upon error on Client
8303         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
8304         # enable bulk pages dump upon error on OSS
8305         $check_ost &&
8306                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
8307
8308         # flush Client cache to allow next read to reach OSS
8309         cancel_lru_locks osc
8310
8311         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
8312         $LCTL set_param fail_loc=0x80000408
8313         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
8314         $LCTL set_param fail_loc=0
8315
8316         rm -f $DIR/$tfile
8317
8318         # check cksum dump on Client
8319         osc_file=$(ls ${osc_file_prefix}*)
8320         [ -n "$osc_file" ] || error "no checksum dump file on Client"
8321         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
8322         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
8323         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
8324         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
8325                      cksum)
8326         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
8327         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8328                 error "dump content does not match on Client"
8329
8330         $check_ost || skip "No need to check cksum dump on OSS"
8331
8332         # check cksum dump on OSS
8333         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
8334         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
8335         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
8336         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
8337         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8338                 error "dump content does not match on OSS"
8339
8340         cleanup_77c
8341 }
8342 run_test 77c "checksum error on client read with debug"
8343
8344 test_77d() { # bug 10889
8345         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8346         $GSS && skip_env "could not run with gss"
8347
8348         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8349         $LCTL set_param fail_loc=0x80000409
8350         set_checksums 1
8351         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8352                 error "direct write: rc=$?"
8353         $LCTL set_param fail_loc=0
8354         set_checksums 0
8355
8356         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8357         $LCTL set_param fail_loc=0x80000408
8358         set_checksums 1
8359         cancel_lru_locks osc
8360         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8361                 error "direct read: rc=$?"
8362         $LCTL set_param fail_loc=0
8363         set_checksums 0
8364 }
8365 run_test 77d "checksum error on OST direct write, read"
8366
8367 test_77f() { # bug 10889
8368         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8369         $GSS && skip_env "could not run with gss"
8370
8371         set_checksums 1
8372         for algo in $CKSUM_TYPES; do
8373                 cancel_lru_locks osc
8374                 set_checksum_type $algo
8375                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8376                 $LCTL set_param fail_loc=0x409
8377                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
8378                         error "direct write succeeded"
8379                 $LCTL set_param fail_loc=0
8380         done
8381         set_checksum_type $ORIG_CSUM_TYPE
8382         set_checksums 0
8383 }
8384 run_test 77f "repeat checksum error on write (expect error)"
8385
8386 test_77g() { # bug 10889
8387         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8388         $GSS && skip_env "could not run with gss"
8389         remote_ost_nodsh && skip "remote OST with nodsh"
8390
8391         [ ! -f $F77_TMP ] && setup_f77
8392
8393         local file=$DIR/$tfile
8394         stack_trap "rm -f $file" EXIT
8395
8396         $LFS setstripe -c 1 -i 0 $file
8397         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
8398         do_facet ost1 lctl set_param fail_loc=0x8000021a
8399         set_checksums 1
8400         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
8401                 error "write error: rc=$?"
8402         do_facet ost1 lctl set_param fail_loc=0
8403         set_checksums 0
8404
8405         cancel_lru_locks osc
8406         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
8407         do_facet ost1 lctl set_param fail_loc=0x8000021b
8408         set_checksums 1
8409         cmp $F77_TMP $file || error "file compare failed"
8410         do_facet ost1 lctl set_param fail_loc=0
8411         set_checksums 0
8412 }
8413 run_test 77g "checksum error on OST write, read"
8414
8415 test_77k() { # LU-10906
8416         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8417         $GSS && skip_env "could not run with gss"
8418
8419         local cksum_param="osc.$FSNAME*.checksums"
8420         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
8421         local checksum
8422         local i
8423
8424         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
8425         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
8426         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
8427                 EXIT
8428
8429         for i in 0 1; do
8430                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
8431                         error "failed to set checksum=$i on MGS"
8432                 wait_update $HOSTNAME "$get_checksum" $i
8433                 #remount
8434                 echo "remount client, checksum should be $i"
8435                 remount_client $MOUNT || error "failed to remount client"
8436                 checksum=$(eval $get_checksum)
8437                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8438         done
8439         # remove persistent param to avoid races with checksum mountopt below
8440         do_facet mgs $LCTL set_param -P -d $cksum_param ||
8441                 error "failed to delete checksum on MGS"
8442
8443         for opt in "checksum" "nochecksum"; do
8444                 #remount with mount option
8445                 echo "remount client with option $opt, checksum should be $i"
8446                 umount_client $MOUNT || error "failed to umount client"
8447                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
8448                         error "failed to mount client with option '$opt'"
8449                 checksum=$(eval $get_checksum)
8450                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8451                 i=$((i - 1))
8452         done
8453
8454         remount_client $MOUNT || error "failed to remount client"
8455 }
8456 run_test 77k "enable/disable checksum correctly"
8457
8458 test_77l() {
8459         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8460         $GSS && skip_env "could not run with gss"
8461
8462         set_checksums 1
8463         stack_trap "set_checksums $ORIG_CSUM" EXIT
8464         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
8465
8466         set_checksum_type invalid && error "unexpected success of invalid checksum type"
8467
8468         $LFS setstripe -c 1 -i 0 $DIR/$tfile
8469         for algo in $CKSUM_TYPES; do
8470                 set_checksum_type $algo || error "fail to set checksum type $algo"
8471                 osc_algo=$(get_osc_checksum_type OST0000)
8472                 [ "$osc_algo" != "$algo" ] && error "checksum type is $osc_algo after setting it to $algo"
8473
8474                 # no locks, no reqs to let the connection idle
8475                 cancel_lru_locks osc
8476                 lru_resize_disable osc
8477                 wait_osc_import_state client ost1 IDLE
8478
8479                 # ensure ost1 is connected
8480                 stat $DIR/$tfile >/dev/null || error "can't stat"
8481                 wait_osc_import_state client ost1 FULL
8482
8483                 osc_algo=$(get_osc_checksum_type OST0000)
8484                 [ "$osc_algo" != "$algo" ] && error "checksum type changed from $algo to $osc_algo after reconnection"
8485         done
8486         return 0
8487 }
8488 run_test 77l "preferred checksum type is remembered after reconnected"
8489
8490 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
8491 rm -f $F77_TMP
8492 unset F77_TMP
8493
8494 cleanup_test_78() {
8495         trap 0
8496         rm -f $DIR/$tfile
8497 }
8498
8499 test_78() { # bug 10901
8500         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8501         remote_ost || skip_env "local OST"
8502
8503         NSEQ=5
8504         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
8505         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
8506         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
8507         echo "MemTotal: $MEMTOTAL"
8508
8509         # reserve 256MB of memory for the kernel and other running processes,
8510         # and then take 1/2 of the remaining memory for the read/write buffers.
8511         if [ $MEMTOTAL -gt 512 ] ;then
8512                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
8513         else
8514                 # for those poor memory-starved high-end clusters...
8515                 MEMTOTAL=$((MEMTOTAL / 2))
8516         fi
8517         echo "Mem to use for directio: $MEMTOTAL"
8518
8519         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
8520         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
8521         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
8522         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
8523                 head -n1)
8524         echo "Smallest OST: $SMALLESTOST"
8525         [[ $SMALLESTOST -lt 10240 ]] &&
8526                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
8527
8528         trap cleanup_test_78 EXIT
8529
8530         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
8531                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
8532
8533         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
8534         echo "File size: $F78SIZE"
8535         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
8536         for i in $(seq 1 $NSEQ); do
8537                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
8538                 echo directIO rdwr round $i of $NSEQ
8539                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
8540         done
8541
8542         cleanup_test_78
8543 }
8544 run_test 78 "handle large O_DIRECT writes correctly ============"
8545
8546 test_79() { # bug 12743
8547         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8548
8549         wait_delete_completed
8550
8551         BKTOTAL=$(calc_osc_kbytes kbytestotal)
8552         BKFREE=$(calc_osc_kbytes kbytesfree)
8553         BKAVAIL=$(calc_osc_kbytes kbytesavail)
8554
8555         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
8556         DFTOTAL=`echo $STRING | cut -d, -f1`
8557         DFUSED=`echo $STRING  | cut -d, -f2`
8558         DFAVAIL=`echo $STRING | cut -d, -f3`
8559         DFFREE=$(($DFTOTAL - $DFUSED))
8560
8561         ALLOWANCE=$((64 * $OSTCOUNT))
8562
8563         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
8564            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
8565                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
8566         fi
8567         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
8568            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
8569                 error "df free($DFFREE) mismatch OST free($BKFREE)"
8570         fi
8571         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
8572            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
8573                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
8574         fi
8575 }
8576 run_test 79 "df report consistency check ======================="
8577
8578 test_80() { # bug 10718
8579         remote_ost_nodsh && skip "remote OST with nodsh"
8580         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8581
8582         # relax strong synchronous semantics for slow backends like ZFS
8583         local soc="obdfilter.*.sync_on_lock_cancel"
8584         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
8585         local hosts=
8586         if [ "$soc_old" != "never" ] &&
8587                 [ "$ost1_FSTYPE" != "ldiskfs" ]; then
8588                         hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
8589                                 facet_active_host $host; done | sort -u)
8590                         do_nodes $hosts lctl set_param $soc=never
8591         fi
8592
8593         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
8594         sync; sleep 1; sync
8595         local BEFORE=`date +%s`
8596         cancel_lru_locks osc
8597         local AFTER=`date +%s`
8598         local DIFF=$((AFTER-BEFORE))
8599         if [ $DIFF -gt 1 ] ; then
8600                 error "elapsed for 1M@1T = $DIFF"
8601         fi
8602
8603         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
8604
8605         rm -f $DIR/$tfile
8606 }
8607 run_test 80 "Page eviction is equally fast at high offsets too  ===="
8608
8609 test_81a() { # LU-456
8610         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8611         remote_ost_nodsh && skip "remote OST with nodsh"
8612
8613         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8614         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
8615         do_facet ost1 lctl set_param fail_loc=0x80000228
8616
8617         # write should trigger a retry and success
8618         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8619         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8620         RC=$?
8621         if [ $RC -ne 0 ] ; then
8622                 error "write should success, but failed for $RC"
8623         fi
8624 }
8625 run_test 81a "OST should retry write when get -ENOSPC ==============="
8626
8627 test_81b() { # LU-456
8628         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8629         remote_ost_nodsh && skip "remote OST with nodsh"
8630
8631         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8632         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
8633         do_facet ost1 lctl set_param fail_loc=0x228
8634
8635         # write should retry several times and return -ENOSPC finally
8636         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8637         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8638         RC=$?
8639         ENOSPC=28
8640         if [ $RC -ne $ENOSPC ] ; then
8641                 error "dd should fail for -ENOSPC, but succeed."
8642         fi
8643 }
8644 run_test 81b "OST should return -ENOSPC when retry still fails ======="
8645
8646 test_82() { # LU-1031
8647         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
8648         local gid1=14091995
8649         local gid2=16022000
8650
8651         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
8652         local MULTIPID1=$!
8653         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
8654         local MULTIPID2=$!
8655         kill -USR1 $MULTIPID2
8656         sleep 2
8657         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
8658                 error "First grouplock does not block second one"
8659         else
8660                 echo "Second grouplock blocks first one"
8661         fi
8662         kill -USR1 $MULTIPID1
8663         wait $MULTIPID1
8664         wait $MULTIPID2
8665 }
8666 run_test 82 "Basic grouplock test"
8667
8668 test_99() {
8669         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
8670
8671         test_mkdir $DIR/$tdir.cvsroot
8672         chown $RUNAS_ID $DIR/$tdir.cvsroot
8673
8674         cd $TMP
8675         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
8676
8677         cd /etc/init.d
8678         # some versions of cvs import exit(1) when asked to import links or
8679         # files they can't read.  ignore those files.
8680         local toignore=$(find . -type l -printf '-I %f\n' -o \
8681                          ! -perm /4 -printf '-I %f\n')
8682         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
8683                 $tdir.reposname vtag rtag
8684
8685         cd $DIR
8686         test_mkdir $DIR/$tdir.reposname
8687         chown $RUNAS_ID $DIR/$tdir.reposname
8688         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
8689
8690         cd $DIR/$tdir.reposname
8691         $RUNAS touch foo99
8692         $RUNAS cvs add -m 'addmsg' foo99
8693         $RUNAS cvs update
8694         $RUNAS cvs commit -m 'nomsg' foo99
8695         rm -fr $DIR/$tdir.cvsroot
8696 }
8697 run_test 99 "cvs strange file/directory operations"
8698
8699 test_100() {
8700         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8701         [[ "$NETTYPE" =~ tcp ]] ||
8702                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
8703         remote_ost_nodsh && skip "remote OST with nodsh"
8704         remote_mds_nodsh && skip "remote MDS with nodsh"
8705         remote_servers ||
8706                 skip "useless for local single node setup"
8707
8708         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
8709                 [ "$PROT" != "tcp" ] && continue
8710                 RPORT=$(echo $REMOTE | cut -d: -f2)
8711                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
8712
8713                 rc=0
8714                 LPORT=`echo $LOCAL | cut -d: -f2`
8715                 if [ $LPORT -ge 1024 ]; then
8716                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
8717                         netstat -tna
8718                         error_exit "local: $LPORT > 1024, remote: $RPORT"
8719                 fi
8720         done
8721         [ "$rc" = 0 ] || error_exit "privileged port not found" )
8722 }
8723 run_test 100 "check local port using privileged port ==========="
8724
8725 function get_named_value()
8726 {
8727     local tag
8728
8729     tag=$1
8730     while read ;do
8731         line=$REPLY
8732         case $line in
8733         $tag*)
8734             echo $line | sed "s/^$tag[ ]*//"
8735             break
8736             ;;
8737         esac
8738     done
8739 }
8740
8741 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
8742                    awk '/^max_cached_mb/ { print $2 }')
8743
8744 cleanup_101a() {
8745         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
8746         trap 0
8747 }
8748
8749 test_101a() {
8750         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8751
8752         local s
8753         local discard
8754         local nreads=10000
8755         local cache_limit=32
8756
8757         $LCTL set_param -n osc.*-osc*.rpc_stats 0
8758         trap cleanup_101a EXIT
8759         $LCTL set_param -n llite.*.read_ahead_stats 0
8760         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
8761
8762         #
8763         # randomly read 10000 of 64K chunks from file 3x 32MB in size
8764         #
8765         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
8766         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
8767
8768         discard=0
8769         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
8770                 get_named_value 'read but discarded' | cut -d" " -f1); do
8771                         discard=$(($discard + $s))
8772         done
8773         cleanup_101a
8774
8775         $LCTL get_param osc.*-osc*.rpc_stats
8776         $LCTL get_param llite.*.read_ahead_stats
8777
8778         # Discard is generally zero, but sometimes a few random reads line up
8779         # and trigger larger readahead, which is wasted & leads to discards.
8780         if [[ $(($discard)) -gt $nreads ]]; then
8781                 error "too many ($discard) discarded pages"
8782         fi
8783         rm -f $DIR/$tfile || true
8784 }
8785 run_test 101a "check read-ahead for random reads"
8786
8787 setup_test101bc() {
8788         test_mkdir $DIR/$tdir
8789         local ssize=$1
8790         local FILE_LENGTH=$2
8791         STRIPE_OFFSET=0
8792
8793         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
8794
8795         local list=$(comma_list $(osts_nodes))
8796         set_osd_param $list '' read_cache_enable 0
8797         set_osd_param $list '' writethrough_cache_enable 0
8798
8799         trap cleanup_test101bc EXIT
8800         # prepare the read-ahead file
8801         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
8802
8803         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
8804                                 count=$FILE_SIZE_MB 2> /dev/null
8805
8806 }
8807
8808 cleanup_test101bc() {
8809         trap 0
8810         rm -rf $DIR/$tdir
8811         rm -f $DIR/$tfile
8812
8813         local list=$(comma_list $(osts_nodes))
8814         set_osd_param $list '' read_cache_enable 1
8815         set_osd_param $list '' writethrough_cache_enable 1
8816 }
8817
8818 calc_total() {
8819         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
8820 }
8821
8822 ra_check_101() {
8823         local READ_SIZE=$1
8824         local STRIPE_SIZE=$2
8825         local FILE_LENGTH=$3
8826         local RA_INC=1048576
8827         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
8828         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
8829                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
8830         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
8831                         get_named_value 'read but discarded' |
8832                         cut -d" " -f1 | calc_total)
8833         if [[ $DISCARD -gt $discard_limit ]]; then
8834                 $LCTL get_param llite.*.read_ahead_stats
8835                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
8836         else
8837                 echo "Read-ahead success for size ${READ_SIZE}"
8838         fi
8839 }
8840
8841 test_101b() {
8842         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8843         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8844
8845         local STRIPE_SIZE=1048576
8846         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
8847
8848         if [ $SLOW == "yes" ]; then
8849                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
8850         else
8851                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
8852         fi
8853
8854         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
8855
8856         # prepare the read-ahead file
8857         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8858         cancel_lru_locks osc
8859         for BIDX in 2 4 8 16 32 64 128 256
8860         do
8861                 local BSIZE=$((BIDX*4096))
8862                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
8863                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
8864                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
8865                 $LCTL set_param -n llite.*.read_ahead_stats 0
8866                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
8867                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
8868                 cancel_lru_locks osc
8869                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
8870         done
8871         cleanup_test101bc
8872         true
8873 }
8874 run_test 101b "check stride-io mode read-ahead ================="
8875
8876 test_101c() {
8877         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8878
8879         local STRIPE_SIZE=1048576
8880         local FILE_LENGTH=$((STRIPE_SIZE*100))
8881         local nreads=10000
8882         local rsize=65536
8883         local osc_rpc_stats
8884
8885         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8886
8887         cancel_lru_locks osc
8888         $LCTL set_param osc.*.rpc_stats 0
8889         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
8890         $LCTL get_param osc.*.rpc_stats
8891         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
8892                 local stats=$($LCTL get_param -n $osc_rpc_stats)
8893                 local lines=$(echo "$stats" | awk 'END {print NR;}')
8894                 local size
8895
8896                 if [ $lines -le 20 ]; then
8897                         echo "continue debug"
8898                         continue
8899                 fi
8900                 for size in 1 2 4 8; do
8901                         local rpc=$(echo "$stats" |
8902                                     awk '($1 == "'$size':") {print $2; exit; }')
8903                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
8904                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
8905                 done
8906                 echo "$osc_rpc_stats check passed!"
8907         done
8908         cleanup_test101bc
8909         true
8910 }
8911 run_test 101c "check stripe_size aligned read-ahead ================="
8912
8913 set_read_ahead() {
8914         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
8915         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
8916 }
8917
8918 test_101d() {
8919         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8920
8921         local file=$DIR/$tfile
8922         local sz_MB=${FILESIZE_101d:-500}
8923         local ra_MB=${READAHEAD_MB:-40}
8924
8925         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
8926         [ $free_MB -lt $sz_MB ] &&
8927                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
8928
8929         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
8930         $LFS setstripe -c -1 $file || error "setstripe failed"
8931
8932         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
8933         echo Cancel LRU locks on lustre client to flush the client cache
8934         cancel_lru_locks osc
8935
8936         echo Disable read-ahead
8937         local old_READAHEAD=$(set_read_ahead 0)
8938
8939         echo Reading the test file $file with read-ahead disabled
8940         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8941
8942         echo Cancel LRU locks on lustre client to flush the client cache
8943         cancel_lru_locks osc
8944         echo Enable read-ahead with ${ra_MB}MB
8945         set_read_ahead $ra_MB
8946
8947         echo Reading the test file $file with read-ahead enabled
8948         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8949
8950         echo "read-ahead disabled time read $raOFF"
8951         echo "read-ahead enabled  time read $raON"
8952
8953         set_read_ahead $old_READAHEAD
8954         rm -f $file
8955         wait_delete_completed
8956
8957         [ $raOFF -le 1 ] || [ $raON -lt $raOFF ] ||
8958                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
8959 }
8960 run_test 101d "file read with and without read-ahead enabled"
8961
8962 test_101e() {
8963         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8964
8965         local file=$DIR/$tfile
8966         local size_KB=500  #KB
8967         local count=100
8968         local bsize=1024
8969
8970         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
8971         local need_KB=$((count * size_KB))
8972         [[ $free_KB -le $need_KB ]] &&
8973                 skip_env "Need free space $need_KB, have $free_KB"
8974
8975         echo "Creating $count ${size_KB}K test files"
8976         for ((i = 0; i < $count; i++)); do
8977                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
8978         done
8979
8980         echo "Cancel LRU locks on lustre client to flush the client cache"
8981         cancel_lru_locks $OSC
8982
8983         echo "Reset readahead stats"
8984         $LCTL set_param -n llite.*.read_ahead_stats 0
8985
8986         for ((i = 0; i < $count; i++)); do
8987                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
8988         done
8989
8990         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
8991                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
8992
8993         for ((i = 0; i < $count; i++)); do
8994                 rm -rf $file.$i 2>/dev/null
8995         done
8996
8997         #10000 means 20% reads are missing in readahead
8998         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
8999 }
9000 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
9001
9002 test_101f() {
9003         which iozone || skip_env "no iozone installed"
9004
9005         local old_debug=$($LCTL get_param debug)
9006         old_debug=${old_debug#*=}
9007         $LCTL set_param debug="reada mmap"
9008
9009         # create a test file
9010         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
9011
9012         echo Cancel LRU locks on lustre client to flush the client cache
9013         cancel_lru_locks osc
9014
9015         echo Reset readahead stats
9016         $LCTL set_param -n llite.*.read_ahead_stats 0
9017
9018         echo mmap read the file with small block size
9019         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
9020                 > /dev/null 2>&1
9021
9022         echo checking missing pages
9023         $LCTL get_param llite.*.read_ahead_stats
9024         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9025                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9026
9027         $LCTL set_param debug="$old_debug"
9028         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
9029         rm -f $DIR/$tfile
9030 }
9031 run_test 101f "check mmap read performance"
9032
9033 test_101g_brw_size_test() {
9034         local mb=$1
9035         local pages=$((mb * 1048576 / PAGE_SIZE))
9036         local file=$DIR/$tfile
9037
9038         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
9039                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
9040         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
9041                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
9042                         return 2
9043         done
9044
9045         stack_trap "rm -f $file" EXIT
9046         $LCTL set_param -n osc.*.rpc_stats=0
9047
9048         # 10 RPCs should be enough for the test
9049         local count=10
9050         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
9051                 { error "dd write ${mb} MB blocks failed"; return 3; }
9052         cancel_lru_locks osc
9053         dd of=/dev/null if=$file bs=${mb}M count=$count ||
9054                 { error "dd write ${mb} MB blocks failed"; return 4; }
9055
9056         # calculate number of full-sized read and write RPCs
9057         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
9058                 sed -n '/pages per rpc/,/^$/p' |
9059                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
9060                 END { print reads,writes }'))
9061         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
9062                 return 5
9063         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
9064                 return 6
9065
9066         return 0
9067 }
9068
9069 test_101g() {
9070         remote_ost_nodsh && skip "remote OST with nodsh"
9071
9072         local rpcs
9073         local osts=$(get_facets OST)
9074         local list=$(comma_list $(osts_nodes))
9075         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
9076         local brw_size="obdfilter.*.brw_size"
9077
9078         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9079
9080         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
9081
9082         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
9083                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
9084                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
9085            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
9086                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
9087                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
9088
9089                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
9090                         suffix="M"
9091
9092                 if [[ $orig_mb -lt 16 ]]; then
9093                         save_lustre_params $osts "$brw_size" > $p
9094                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
9095                                 error "set 16MB RPC size failed"
9096
9097                         echo "remount client to enable new RPC size"
9098                         remount_client $MOUNT || error "remount_client failed"
9099                 fi
9100
9101                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
9102                 # should be able to set brw_size=12, but no rpc_stats for that
9103                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
9104         fi
9105
9106         test_101g_brw_size_test 4 || error "4MB RPC test failed"
9107
9108         if [[ $orig_mb -lt 16 ]]; then
9109                 restore_lustre_params < $p
9110                 remount_client $MOUNT || error "remount_client restore failed"
9111         fi
9112
9113         rm -f $p $DIR/$tfile
9114 }
9115 run_test 101g "Big bulk(4/16 MiB) readahead"
9116
9117 test_101h() {
9118         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9119
9120         dd if=/dev/zero of=$DIR/$tfile bs=1M count=70 ||
9121                 error "dd 70M file failed"
9122         echo Cancel LRU locks on lustre client to flush the client cache
9123         cancel_lru_locks osc
9124
9125         echo "Reset readahead stats"
9126         $LCTL set_param -n llite.*.read_ahead_stats 0
9127
9128         echo "Read 10M of data but cross 64M bundary"
9129         dd if=$DIR/$tfile of=/dev/null bs=10M skip=6 count=1
9130         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9131                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9132         [ $miss -eq 1 ] || error "expected miss 1 but got $miss"
9133         rm -f $p $DIR/$tfile
9134 }
9135 run_test 101h "Readahead should cover current read window"
9136
9137 setup_test102() {
9138         test_mkdir $DIR/$tdir
9139         chown $RUNAS_ID $DIR/$tdir
9140         STRIPE_SIZE=65536
9141         STRIPE_OFFSET=1
9142         STRIPE_COUNT=$OSTCOUNT
9143         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
9144
9145         trap cleanup_test102 EXIT
9146         cd $DIR
9147         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
9148         cd $DIR/$tdir
9149         for num in 1 2 3 4; do
9150                 for count in $(seq 1 $STRIPE_COUNT); do
9151                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
9152                                 local size=`expr $STRIPE_SIZE \* $num`
9153                                 local file=file"$num-$idx-$count"
9154                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
9155                         done
9156                 done
9157         done
9158
9159         cd $DIR
9160         $1 tar cf $TMP/f102.tar $tdir --xattrs
9161 }
9162
9163 cleanup_test102() {
9164         trap 0
9165         rm -f $TMP/f102.tar
9166         rm -rf $DIR/d0.sanity/d102
9167 }
9168
9169 test_102a() {
9170         [ "$UID" != 0 ] && skip "must run as root"
9171         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
9172                 skip_env "must have user_xattr"
9173
9174         [ -z "$(which setfattr 2>/dev/null)" ] &&
9175                 skip_env "could not find setfattr"
9176
9177         local testfile=$DIR/$tfile
9178
9179         touch $testfile
9180         echo "set/get xattr..."
9181         setfattr -n trusted.name1 -v value1 $testfile ||
9182                 error "setfattr -n trusted.name1=value1 $testfile failed"
9183         getfattr -n trusted.name1 $testfile 2> /dev/null |
9184           grep "trusted.name1=.value1" ||
9185                 error "$testfile missing trusted.name1=value1"
9186
9187         setfattr -n user.author1 -v author1 $testfile ||
9188                 error "setfattr -n user.author1=author1 $testfile failed"
9189         getfattr -n user.author1 $testfile 2> /dev/null |
9190           grep "user.author1=.author1" ||
9191                 error "$testfile missing trusted.author1=author1"
9192
9193         echo "listxattr..."
9194         setfattr -n trusted.name2 -v value2 $testfile ||
9195                 error "$testfile unable to set trusted.name2"
9196         setfattr -n trusted.name3 -v value3 $testfile ||
9197                 error "$testfile unable to set trusted.name3"
9198         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
9199             grep "trusted.name" | wc -l) -eq 3 ] ||
9200                 error "$testfile missing 3 trusted.name xattrs"
9201
9202         setfattr -n user.author2 -v author2 $testfile ||
9203                 error "$testfile unable to set user.author2"
9204         setfattr -n user.author3 -v author3 $testfile ||
9205                 error "$testfile unable to set user.author3"
9206         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
9207             grep "user.author" | wc -l) -eq 3 ] ||
9208                 error "$testfile missing 3 user.author xattrs"
9209
9210         echo "remove xattr..."
9211         setfattr -x trusted.name1 $testfile ||
9212                 error "$testfile error deleting trusted.name1"
9213         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
9214                 error "$testfile did not delete trusted.name1 xattr"
9215
9216         setfattr -x user.author1 $testfile ||
9217                 error "$testfile error deleting user.author1"
9218         echo "set lustre special xattr ..."
9219         $LFS setstripe -c1 $testfile
9220         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
9221                 awk -F "=" '/trusted.lov/ { print $2 }' )
9222         setfattr -n "trusted.lov" -v $lovea $testfile ||
9223                 error "$testfile doesn't ignore setting trusted.lov again"
9224         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
9225                 error "$testfile allow setting invalid trusted.lov"
9226         rm -f $testfile
9227 }
9228 run_test 102a "user xattr test =================================="
9229
9230 test_102b() {
9231         [ -z "$(which setfattr 2>/dev/null)" ] &&
9232                 skip_env "could not find setfattr"
9233         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9234
9235         # b10930: get/set/list trusted.lov xattr
9236         echo "get/set/list trusted.lov xattr ..."
9237         local testfile=$DIR/$tfile
9238         $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
9239                 error "setstripe failed"
9240         local STRIPECOUNT=$($LFS getstripe -c $testfile) ||
9241                 error "getstripe failed"
9242         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
9243                 error "can't get trusted.lov from $testfile"
9244
9245         local testfile2=${testfile}2
9246         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
9247                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
9248
9249         $MCREATE $testfile2
9250         setfattr -n trusted.lov -v $value $testfile2
9251         local stripe_size=$($LFS getstripe -S $testfile2)
9252         local stripe_count=$($LFS getstripe -c $testfile2)
9253         [[ $stripe_size -eq 65536 ]] ||
9254                 error "stripe size $stripe_size != 65536"
9255         [[ $stripe_count -eq $STRIPECOUNT ]] ||
9256                 error "stripe count $stripe_count != $STRIPECOUNT"
9257         rm -f $DIR/$tfile
9258 }
9259 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
9260
9261 test_102c() {
9262         [ -z "$(which setfattr 2>/dev/null)" ] &&
9263                 skip_env "could not find setfattr"
9264         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9265
9266         # b10930: get/set/list lustre.lov xattr
9267         echo "get/set/list lustre.lov xattr ..."
9268         test_mkdir $DIR/$tdir
9269         chown $RUNAS_ID $DIR/$tdir
9270         local testfile=$DIR/$tdir/$tfile
9271         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
9272                 error "setstripe failed"
9273         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
9274                 error "getstripe failed"
9275         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
9276         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
9277
9278         local testfile2=${testfile}2
9279         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
9280                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
9281
9282         $RUNAS $MCREATE $testfile2
9283         $RUNAS setfattr -n lustre.lov -v $value $testfile2
9284         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
9285         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
9286         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
9287         [ $stripe_count -eq $STRIPECOUNT ] ||
9288                 error "stripe count $stripe_count != $STRIPECOUNT"
9289 }
9290 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
9291
9292 compare_stripe_info1() {
9293         local stripe_index_all_zero=true
9294
9295         for num in 1 2 3 4; do
9296                 for count in $(seq 1 $STRIPE_COUNT); do
9297                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
9298                                 local size=$((STRIPE_SIZE * num))
9299                                 local file=file"$num-$offset-$count"
9300                                 stripe_size=$($LFS getstripe -S $PWD/$file)
9301                                 [[ $stripe_size -ne $size ]] &&
9302                                     error "$file: size $stripe_size != $size"
9303                                 stripe_count=$($LFS getstripe -c $PWD/$file)
9304                                 # allow fewer stripes to be created, ORI-601
9305                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
9306                                     error "$file: count $stripe_count != $count"
9307                                 stripe_index=$($LFS getstripe -i $PWD/$file)
9308                                 [[ $stripe_index -ne 0 ]] &&
9309                                         stripe_index_all_zero=false
9310                         done
9311                 done
9312         done
9313         $stripe_index_all_zero &&
9314                 error "all files are being extracted starting from OST index 0"
9315         return 0
9316 }
9317
9318 have_xattrs_include() {
9319         tar --help | grep -q xattrs-include &&
9320                 echo --xattrs-include="lustre.*"
9321 }
9322
9323 test_102d() {
9324         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9325         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9326
9327         XINC=$(have_xattrs_include)
9328         setup_test102
9329         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9330         cd $DIR/$tdir/$tdir
9331         compare_stripe_info1
9332 }
9333 run_test 102d "tar restore stripe info from tarfile,not keep osts"
9334
9335 test_102f() {
9336         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9337         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9338
9339         XINC=$(have_xattrs_include)
9340         setup_test102
9341         test_mkdir $DIR/$tdir.restore
9342         cd $DIR
9343         tar cf - --xattrs $tdir | tar xf - \
9344                 -C $DIR/$tdir.restore --xattrs $XINC
9345         cd $DIR/$tdir.restore/$tdir
9346         compare_stripe_info1
9347 }
9348 run_test 102f "tar copy files, not keep osts"
9349
9350 grow_xattr() {
9351         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
9352                 skip "must have user_xattr"
9353         [ -z "$(which setfattr 2>/dev/null)" ] &&
9354                 skip_env "could not find setfattr"
9355         [ -z "$(which getfattr 2>/dev/null)" ] &&
9356                 skip_env "could not find getfattr"
9357
9358         local xsize=${1:-1024}  # in bytes
9359         local file=$DIR/$tfile
9360         local value="$(generate_string $xsize)"
9361         local xbig=trusted.big
9362         local toobig=$2
9363
9364         touch $file
9365         log "save $xbig on $file"
9366         if [ -z "$toobig" ]
9367         then
9368                 setfattr -n $xbig -v $value $file ||
9369                         error "saving $xbig on $file failed"
9370         else
9371                 setfattr -n $xbig -v $value $file &&
9372                         error "saving $xbig on $file succeeded"
9373                 return 0
9374         fi
9375
9376         local orig=$(get_xattr_value $xbig $file)
9377         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
9378
9379         local xsml=trusted.sml
9380         log "save $xsml on $file"
9381         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
9382
9383         local new=$(get_xattr_value $xbig $file)
9384         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
9385
9386         log "grow $xsml on $file"
9387         setfattr -n $xsml -v "$value" $file ||
9388                 error "growing $xsml on $file failed"
9389
9390         new=$(get_xattr_value $xbig $file)
9391         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
9392         log "$xbig still valid after growing $xsml"
9393
9394         rm -f $file
9395 }
9396
9397 test_102h() { # bug 15777
9398         grow_xattr 1024
9399 }
9400 run_test 102h "grow xattr from inside inode to external block"
9401
9402 test_102ha() {
9403         large_xattr_enabled || skip_env "ea_inode feature disabled"
9404
9405         echo "setting xattr of max xattr size: $(max_xattr_size)"
9406         grow_xattr $(max_xattr_size)
9407
9408         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
9409         echo "This should fail:"
9410         grow_xattr $(($(max_xattr_size) + 10)) 1
9411 }
9412 run_test 102ha "grow xattr from inside inode to external inode"
9413
9414 test_102i() { # bug 17038
9415         [ -z "$(which getfattr 2>/dev/null)" ] &&
9416                 skip "could not find getfattr"
9417
9418         touch $DIR/$tfile
9419         ln -s $DIR/$tfile $DIR/${tfile}link
9420         getfattr -n trusted.lov $DIR/$tfile ||
9421                 error "lgetxattr on $DIR/$tfile failed"
9422         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
9423                 grep -i "no such attr" ||
9424                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
9425         rm -f $DIR/$tfile $DIR/${tfile}link
9426 }
9427 run_test 102i "lgetxattr test on symbolic link ============"
9428
9429 test_102j() {
9430         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9431         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9432
9433         XINC=$(have_xattrs_include)
9434         setup_test102 "$RUNAS"
9435         chown $RUNAS_ID $DIR/$tdir
9436         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9437         cd $DIR/$tdir/$tdir
9438         compare_stripe_info1 "$RUNAS"
9439 }
9440 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
9441
9442 test_102k() {
9443         [ -z "$(which setfattr 2>/dev/null)" ] &&
9444                 skip "could not find setfattr"
9445
9446         touch $DIR/$tfile
9447         # b22187 just check that does not crash for regular file.
9448         setfattr -n trusted.lov $DIR/$tfile
9449         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
9450         local test_kdir=$DIR/$tdir
9451         test_mkdir $test_kdir
9452         local default_size=$($LFS getstripe -S $test_kdir)
9453         local default_count=$($LFS getstripe -c $test_kdir)
9454         local default_offset=$($LFS getstripe -i $test_kdir)
9455         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
9456                 error 'dir setstripe failed'
9457         setfattr -n trusted.lov $test_kdir
9458         local stripe_size=$($LFS getstripe -S $test_kdir)
9459         local stripe_count=$($LFS getstripe -c $test_kdir)
9460         local stripe_offset=$($LFS getstripe -i $test_kdir)
9461         [ $stripe_size -eq $default_size ] ||
9462                 error "stripe size $stripe_size != $default_size"
9463         [ $stripe_count -eq $default_count ] ||
9464                 error "stripe count $stripe_count != $default_count"
9465         [ $stripe_offset -eq $default_offset ] ||
9466                 error "stripe offset $stripe_offset != $default_offset"
9467         rm -rf $DIR/$tfile $test_kdir
9468 }
9469 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
9470
9471 test_102l() {
9472         [ -z "$(which getfattr 2>/dev/null)" ] &&
9473                 skip "could not find getfattr"
9474
9475         # LU-532 trusted. xattr is invisible to non-root
9476         local testfile=$DIR/$tfile
9477
9478         touch $testfile
9479
9480         echo "listxattr as user..."
9481         chown $RUNAS_ID $testfile
9482         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
9483             grep -q "trusted" &&
9484                 error "$testfile trusted xattrs are user visible"
9485
9486         return 0;
9487 }
9488 run_test 102l "listxattr size test =================================="
9489
9490 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
9491         local path=$DIR/$tfile
9492         touch $path
9493
9494         listxattr_size_check $path || error "listattr_size_check $path failed"
9495 }
9496 run_test 102m "Ensure listxattr fails on small bufffer ========"
9497
9498 cleanup_test102
9499
9500 getxattr() { # getxattr path name
9501         # Return the base64 encoding of the value of xattr name on path.
9502         local path=$1
9503         local name=$2
9504
9505         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
9506         # file: $path
9507         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9508         #
9509         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9510
9511         getfattr --absolute-names --encoding=base64 --name=$name $path |
9512                 awk -F= -v name=$name '$1 == name {
9513                         print substr($0, index($0, "=") + 1);
9514         }'
9515 }
9516
9517 test_102n() { # LU-4101 mdt: protect internal xattrs
9518         [ -z "$(which setfattr 2>/dev/null)" ] &&
9519                 skip "could not find setfattr"
9520         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
9521         then
9522                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
9523         fi
9524
9525         local file0=$DIR/$tfile.0
9526         local file1=$DIR/$tfile.1
9527         local xattr0=$TMP/$tfile.0
9528         local xattr1=$TMP/$tfile.1
9529         local namelist="lov lma lmv link fid version som hsm"
9530         local name
9531         local value
9532
9533         rm -rf $file0 $file1 $xattr0 $xattr1
9534         touch $file0 $file1
9535
9536         # Get 'before' xattrs of $file1.
9537         getfattr --absolute-names --dump --match=- $file1 > $xattr0
9538
9539         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
9540                 namelist+=" lfsck_namespace"
9541         for name in $namelist; do
9542                 # Try to copy xattr from $file0 to $file1.
9543                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9544
9545                 setfattr --name=trusted.$name --value="$value" $file1 ||
9546                         error "setxattr 'trusted.$name' failed"
9547
9548                 # Try to set a garbage xattr.
9549                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9550
9551                 if [[ x$name == "xlov" ]]; then
9552                         setfattr --name=trusted.lov --value="$value" $file1 &&
9553                         error "setxattr invalid 'trusted.lov' success"
9554                 else
9555                         setfattr --name=trusted.$name --value="$value" $file1 ||
9556                                 error "setxattr invalid 'trusted.$name' failed"
9557                 fi
9558
9559                 # Try to remove the xattr from $file1. We don't care if this
9560                 # appears to succeed or fail, we just don't want there to be
9561                 # any changes or crashes.
9562                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9563         done
9564
9565         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
9566         then
9567                 name="lfsck_ns"
9568                 # Try to copy xattr from $file0 to $file1.
9569                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9570
9571                 setfattr --name=trusted.$name --value="$value" $file1 ||
9572                         error "setxattr 'trusted.$name' failed"
9573
9574                 # Try to set a garbage xattr.
9575                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9576
9577                 setfattr --name=trusted.$name --value="$value" $file1 ||
9578                         error "setxattr 'trusted.$name' failed"
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         fi
9585
9586         # Get 'after' xattrs of file1.
9587         getfattr --absolute-names --dump --match=- $file1 > $xattr1
9588
9589         if ! diff $xattr0 $xattr1; then
9590                 error "before and after xattrs of '$file1' differ"
9591         fi
9592
9593         rm -rf $file0 $file1 $xattr0 $xattr1
9594
9595         return 0
9596 }
9597 run_test 102n "silently ignore setxattr on internal trusted xattrs"
9598
9599 test_102p() { # LU-4703 setxattr did not check ownership
9600         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
9601                 skip "MDS needs to be at least 2.5.56"
9602
9603         local testfile=$DIR/$tfile
9604
9605         touch $testfile
9606
9607         echo "setfacl as user..."
9608         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
9609         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
9610
9611         echo "setfattr as user..."
9612         setfacl -m "u:$RUNAS_ID:---" $testfile
9613         $RUNAS setfattr -x system.posix_acl_access $testfile
9614         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
9615 }
9616 run_test 102p "check setxattr(2) correctly fails without permission"
9617
9618 test_102q() {
9619         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
9620                 skip "MDS needs to be at least 2.6.92"
9621
9622         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
9623 }
9624 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
9625
9626 test_102r() {
9627         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
9628                 skip "MDS needs to be at least 2.6.93"
9629
9630         touch $DIR/$tfile || error "touch"
9631         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
9632         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
9633         rm $DIR/$tfile || error "rm"
9634
9635         #normal directory
9636         mkdir -p $DIR/$tdir || error "mkdir"
9637         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9638         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9639         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9640                 error "$testfile error deleting user.author1"
9641         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9642                 grep "user.$(basename $tdir)" &&
9643                 error "$tdir did not delete user.$(basename $tdir)"
9644         rmdir $DIR/$tdir || error "rmdir"
9645
9646         #striped directory
9647         test_mkdir $DIR/$tdir
9648         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9649         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9650         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9651                 error "$testfile error deleting user.author1"
9652         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9653                 grep "user.$(basename $tdir)" &&
9654                 error "$tdir did not delete user.$(basename $tdir)"
9655         rmdir $DIR/$tdir || error "rm striped dir"
9656 }
9657 run_test 102r "set EAs with empty values"
9658
9659 test_102s() {
9660         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9661                 skip "MDS needs to be at least 2.11.52"
9662
9663         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9664
9665         save_lustre_params client "llite.*.xattr_cache" > $save
9666
9667         for cache in 0 1; do
9668                 lctl set_param llite.*.xattr_cache=$cache
9669
9670                 rm -f $DIR/$tfile
9671                 touch $DIR/$tfile || error "touch"
9672                 for prefix in lustre security system trusted user; do
9673                         # Note getxattr() may fail with 'Operation not
9674                         # supported' or 'No such attribute' depending
9675                         # on prefix and cache.
9676                         getfattr -n $prefix.n102s $DIR/$tfile &&
9677                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
9678                 done
9679         done
9680
9681         restore_lustre_params < $save
9682 }
9683 run_test 102s "getting nonexistent xattrs should fail"
9684
9685 test_102t() {
9686         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9687                 skip "MDS needs to be at least 2.11.52"
9688
9689         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9690
9691         save_lustre_params client "llite.*.xattr_cache" > $save
9692
9693         for cache in 0 1; do
9694                 lctl set_param llite.*.xattr_cache=$cache
9695
9696                 for buf_size in 0 256; do
9697                         rm -f $DIR/$tfile
9698                         touch $DIR/$tfile || error "touch"
9699                         setfattr -n user.multiop $DIR/$tfile
9700                         $MULTIOP $DIR/$tfile oa$buf_size ||
9701                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
9702                 done
9703         done
9704
9705         restore_lustre_params < $save
9706 }
9707 run_test 102t "zero length xattr values handled correctly"
9708
9709 run_acl_subtest()
9710 {
9711     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
9712     return $?
9713 }
9714
9715 test_103a() {
9716         [ "$UID" != 0 ] && skip "must run as root"
9717         $GSS && skip_env "could not run under gss"
9718         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
9719                 skip_env "must have acl enabled"
9720         [ -z "$(which setfacl 2>/dev/null)" ] &&
9721                 skip_env "could not find setfacl"
9722         remote_mds_nodsh && skip "remote MDS with nodsh"
9723
9724         gpasswd -a daemon bin                           # LU-5641
9725         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
9726
9727         declare -a identity_old
9728
9729         for num in $(seq $MDSCOUNT); do
9730                 switch_identity $num true || identity_old[$num]=$?
9731         done
9732
9733         SAVE_UMASK=$(umask)
9734         umask 0022
9735         mkdir -p $DIR/$tdir
9736         cd $DIR/$tdir
9737
9738         echo "performing cp ..."
9739         run_acl_subtest cp || error "run_acl_subtest cp failed"
9740         echo "performing getfacl-noacl..."
9741         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
9742         echo "performing misc..."
9743         run_acl_subtest misc || error  "misc test failed"
9744         echo "performing permissions..."
9745         run_acl_subtest permissions || error "permissions failed"
9746         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
9747         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
9748                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
9749                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
9750         then
9751                 echo "performing permissions xattr..."
9752                 run_acl_subtest permissions_xattr ||
9753                         error "permissions_xattr failed"
9754         fi
9755         echo "performing setfacl..."
9756         run_acl_subtest setfacl || error  "setfacl test failed"
9757
9758         # inheritance test got from HP
9759         echo "performing inheritance..."
9760         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
9761         chmod +x make-tree || error "chmod +x failed"
9762         run_acl_subtest inheritance || error "inheritance test failed"
9763         rm -f make-tree
9764
9765         echo "LU-974 ignore umask when acl is enabled..."
9766         run_acl_subtest 974 || error "LU-974 umask test failed"
9767         if [ $MDSCOUNT -ge 2 ]; then
9768                 run_acl_subtest 974_remote ||
9769                         error "LU-974 umask test failed under remote dir"
9770         fi
9771
9772         echo "LU-2561 newly created file is same size as directory..."
9773         if [ "$mds1_FSTYPE" != "zfs" ]; then
9774                 run_acl_subtest 2561 || error "LU-2561 test failed"
9775         else
9776                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
9777         fi
9778
9779         run_acl_subtest 4924 || error "LU-4924 test failed"
9780
9781         cd $SAVE_PWD
9782         umask $SAVE_UMASK
9783
9784         for num in $(seq $MDSCOUNT); do
9785                 if [ "${identity_old[$num]}" = 1 ]; then
9786                         switch_identity $num false || identity_old[$num]=$?
9787                 fi
9788         done
9789 }
9790 run_test 103a "acl test"
9791
9792 test_103b() {
9793         declare -a pids
9794         local U
9795
9796         for U in {0..511}; do
9797                 {
9798                 local O=$(printf "%04o" $U)
9799
9800                 umask $(printf "%04o" $((511 ^ $O)))
9801                 $LFS setstripe -c 1 $DIR/$tfile.s$O
9802                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
9803
9804                 (( $S == ($O & 0666) )) ||
9805                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
9806
9807                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
9808                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
9809                 (( $S == ($O & 0666) )) ||
9810                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
9811
9812                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
9813                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
9814                 (( $S == ($O & 0666) )) ||
9815                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
9816                 rm -f $DIR/$tfile.[smp]$0
9817                 } &
9818                 local pid=$!
9819
9820                 # limit the concurrently running threads to 64. LU-11878
9821                 local idx=$((U % 64))
9822                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
9823                 pids[idx]=$pid
9824         done
9825         wait
9826 }
9827 run_test 103b "umask lfs setstripe"
9828
9829 test_103c() {
9830         mkdir -p $DIR/$tdir
9831         cp -rp $DIR/$tdir $DIR/$tdir.bak
9832
9833         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
9834                 error "$DIR/$tdir shouldn't contain default ACL"
9835         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
9836                 error "$DIR/$tdir.bak shouldn't contain default ACL"
9837         true
9838 }
9839 run_test 103c "'cp -rp' won't set empty acl"
9840
9841 test_104a() {
9842         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9843
9844         touch $DIR/$tfile
9845         lfs df || error "lfs df failed"
9846         lfs df -ih || error "lfs df -ih failed"
9847         lfs df -h $DIR || error "lfs df -h $DIR failed"
9848         lfs df -i $DIR || error "lfs df -i $DIR failed"
9849         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
9850         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
9851
9852         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
9853         lctl --device %$OSC deactivate
9854         lfs df || error "lfs df with deactivated OSC failed"
9855         lctl --device %$OSC activate
9856         # wait the osc back to normal
9857         wait_osc_import_ready client ost
9858
9859         lfs df || error "lfs df with reactivated OSC failed"
9860         rm -f $DIR/$tfile
9861 }
9862 run_test 104a "lfs df [-ih] [path] test ========================="
9863
9864 test_104b() {
9865         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9866         [ $RUNAS_ID -eq $UID ] &&
9867                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9868
9869         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
9870                         grep "Permission denied" | wc -l)))
9871         if [ $denied_cnt -ne 0 ]; then
9872                 error "lfs check servers test failed"
9873         fi
9874 }
9875 run_test 104b "$RUNAS lfs check servers test ===================="
9876
9877 test_105a() {
9878         # doesn't work on 2.4 kernels
9879         touch $DIR/$tfile
9880         if $(flock_is_enabled); then
9881                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
9882         else
9883                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
9884         fi
9885         rm -f $DIR/$tfile
9886 }
9887 run_test 105a "flock when mounted without -o flock test ========"
9888
9889 test_105b() {
9890         touch $DIR/$tfile
9891         if $(flock_is_enabled); then
9892                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
9893         else
9894                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
9895         fi
9896         rm -f $DIR/$tfile
9897 }
9898 run_test 105b "fcntl when mounted without -o flock test ========"
9899
9900 test_105c() {
9901         touch $DIR/$tfile
9902         if $(flock_is_enabled); then
9903                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
9904         else
9905                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
9906         fi
9907         rm -f $DIR/$tfile
9908 }
9909 run_test 105c "lockf when mounted without -o flock test"
9910
9911 test_105d() { # bug 15924
9912         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9913
9914         test_mkdir $DIR/$tdir
9915         flock_is_enabled || skip_env "mount w/o flock enabled"
9916         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
9917         $LCTL set_param fail_loc=0x80000315
9918         flocks_test 2 $DIR/$tdir
9919 }
9920 run_test 105d "flock race (should not freeze) ========"
9921
9922 test_105e() { # bug 22660 && 22040
9923         flock_is_enabled || skip_env "mount w/o flock enabled"
9924
9925         touch $DIR/$tfile
9926         flocks_test 3 $DIR/$tfile
9927 }
9928 run_test 105e "Two conflicting flocks from same process"
9929
9930 test_106() { #bug 10921
9931         test_mkdir $DIR/$tdir
9932         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
9933         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
9934 }
9935 run_test 106 "attempt exec of dir followed by chown of that dir"
9936
9937 test_107() {
9938         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9939
9940         CDIR=`pwd`
9941         local file=core
9942
9943         cd $DIR
9944         rm -f $file
9945
9946         local save_pattern=$(sysctl -n kernel.core_pattern)
9947         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
9948         sysctl -w kernel.core_pattern=$file
9949         sysctl -w kernel.core_uses_pid=0
9950
9951         ulimit -c unlimited
9952         sleep 60 &
9953         SLEEPPID=$!
9954
9955         sleep 1
9956
9957         kill -s 11 $SLEEPPID
9958         wait $SLEEPPID
9959         if [ -e $file ]; then
9960                 size=`stat -c%s $file`
9961                 [ $size -eq 0 ] && error "Fail to create core file $file"
9962         else
9963                 error "Fail to create core file $file"
9964         fi
9965         rm -f $file
9966         sysctl -w kernel.core_pattern=$save_pattern
9967         sysctl -w kernel.core_uses_pid=$save_uses_pid
9968         cd $CDIR
9969 }
9970 run_test 107 "Coredump on SIG"
9971
9972 test_110() {
9973         test_mkdir $DIR/$tdir
9974         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
9975         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
9976                 error "mkdir with 256 char should fail, but did not"
9977         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
9978                 error "create with 255 char failed"
9979         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
9980                 error "create with 256 char should fail, but did not"
9981
9982         ls -l $DIR/$tdir
9983         rm -rf $DIR/$tdir
9984 }
9985 run_test 110 "filename length checking"
9986
9987 #
9988 # Purpose: To verify dynamic thread (OSS) creation.
9989 #
9990 test_115() {
9991         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9992         remote_ost_nodsh && skip "remote OST with nodsh"
9993
9994         # Lustre does not stop service threads once they are started.
9995         # Reset number of running threads to default.
9996         stopall
9997         setupall
9998
9999         local OSTIO_pre
10000         local save_params="$TMP/sanity-$TESTNAME.parameters"
10001
10002         # Get ll_ost_io count before I/O
10003         OSTIO_pre=$(do_facet ost1 \
10004                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
10005         # Exit if lustre is not running (ll_ost_io not running).
10006         [ -z "$OSTIO_pre" ] && error "no OSS threads"
10007
10008         echo "Starting with $OSTIO_pre threads"
10009         local thread_max=$((OSTIO_pre * 2))
10010         local rpc_in_flight=$((thread_max * 2))
10011         # Number of I/O Process proposed to be started.
10012         local nfiles
10013         local facets=$(get_facets OST)
10014
10015         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
10016         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
10017
10018         # Set in_flight to $rpc_in_flight
10019         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
10020                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
10021         nfiles=${rpc_in_flight}
10022         # Set ost thread_max to $thread_max
10023         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
10024
10025         # 5 Minutes should be sufficient for max number of OSS
10026         # threads(thread_max) to be created.
10027         local timeout=300
10028
10029         # Start I/O.
10030         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
10031         test_mkdir $DIR/$tdir
10032         for i in $(seq $nfiles); do
10033                 local file=$DIR/$tdir/${tfile}-$i
10034                 $LFS setstripe -c -1 -i 0 $file
10035                 ($WTL $file $timeout)&
10036         done
10037
10038         # I/O Started - Wait for thread_started to reach thread_max or report
10039         # error if thread_started is more than thread_max.
10040         echo "Waiting for thread_started to reach thread_max"
10041         local thread_started=0
10042         local end_time=$((SECONDS + timeout))
10043
10044         while [ $SECONDS -le $end_time ] ; do
10045                 echo -n "."
10046                 # Get ost i/o thread_started count.
10047                 thread_started=$(do_facet ost1 \
10048                         "$LCTL get_param \
10049                         ost.OSS.ost_io.threads_started | cut -d= -f2")
10050                 # Break out if thread_started is equal/greater than thread_max
10051                 if [[ $thread_started -ge $thread_max ]]; then
10052                         echo ll_ost_io thread_started $thread_started, \
10053                                 equal/greater than thread_max $thread_max
10054                         break
10055                 fi
10056                 sleep 1
10057         done
10058
10059         # Cleanup - We have the numbers, Kill i/o jobs if running.
10060         jobcount=($(jobs -p))
10061         for i in $(seq 0 $((${#jobcount[@]}-1)))
10062         do
10063                 kill -9 ${jobcount[$i]}
10064                 if [ $? -ne 0 ] ; then
10065                         echo Warning: \
10066                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
10067                 fi
10068         done
10069
10070         # Cleanup files left by WTL binary.
10071         for i in $(seq $nfiles); do
10072                 local file=$DIR/$tdir/${tfile}-$i
10073                 rm -rf $file
10074                 if [ $? -ne 0 ] ; then
10075                         echo "Warning: Failed to delete file $file"
10076                 fi
10077         done
10078
10079         restore_lustre_params <$save_params
10080         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
10081
10082         # Error out if no new thread has started or Thread started is greater
10083         # than thread max.
10084         if [[ $thread_started -le $OSTIO_pre ||
10085                         $thread_started -gt $thread_max ]]; then
10086                 error "ll_ost_io: thread_started $thread_started" \
10087                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
10088                       "No new thread started or thread started greater " \
10089                       "than thread_max."
10090         fi
10091 }
10092 run_test 115 "verify dynamic thread creation===================="
10093
10094 free_min_max () {
10095         wait_delete_completed
10096         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
10097         echo "OST kbytes available: ${AVAIL[@]}"
10098         MAXV=${AVAIL[0]}
10099         MAXI=0
10100         MINV=${AVAIL[0]}
10101         MINI=0
10102         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
10103                 #echo OST $i: ${AVAIL[i]}kb
10104                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
10105                         MAXV=${AVAIL[i]}
10106                         MAXI=$i
10107                 fi
10108                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
10109                         MINV=${AVAIL[i]}
10110                         MINI=$i
10111                 fi
10112         done
10113         echo "Min free space: OST $MINI: $MINV"
10114         echo "Max free space: OST $MAXI: $MAXV"
10115 }
10116
10117 test_116a() { # was previously test_116()
10118         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10119         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10120         remote_mds_nodsh && skip "remote MDS with nodsh"
10121
10122         echo -n "Free space priority "
10123         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
10124                 head -n1
10125         declare -a AVAIL
10126         free_min_max
10127
10128         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
10129         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
10130         trap simple_cleanup_common EXIT
10131
10132         # Check if we need to generate uneven OSTs
10133         test_mkdir -p $DIR/$tdir/OST${MINI}
10134         local FILL=$((MINV / 4))
10135         local DIFF=$((MAXV - MINV))
10136         local DIFF2=$((DIFF * 100 / MINV))
10137
10138         local threshold=$(do_facet $SINGLEMDS \
10139                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
10140         threshold=${threshold%%%}
10141         echo -n "Check for uneven OSTs: "
10142         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
10143
10144         if [[ $DIFF2 -gt $threshold ]]; then
10145                 echo "ok"
10146                 echo "Don't need to fill OST$MINI"
10147         else
10148                 # generate uneven OSTs. Write 2% over the QOS threshold value
10149                 echo "no"
10150                 DIFF=$((threshold - DIFF2 + 2))
10151                 DIFF2=$((MINV * DIFF / 100))
10152                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
10153                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
10154                         error "setstripe failed"
10155                 DIFF=$((DIFF2 / 2048))
10156                 i=0
10157                 while [ $i -lt $DIFF ]; do
10158                         i=$((i + 1))
10159                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
10160                                 bs=2M count=1 2>/dev/null
10161                         echo -n .
10162                 done
10163                 echo .
10164                 sync
10165                 sleep_maxage
10166                 free_min_max
10167         fi
10168
10169         DIFF=$((MAXV - MINV))
10170         DIFF2=$((DIFF * 100 / MINV))
10171         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
10172         if [ $DIFF2 -gt $threshold ]; then
10173                 echo "ok"
10174         else
10175                 echo "failed - QOS mode won't be used"
10176                 simple_cleanup_common
10177                 skip "QOS imbalance criteria not met"
10178         fi
10179
10180         MINI1=$MINI
10181         MINV1=$MINV
10182         MAXI1=$MAXI
10183         MAXV1=$MAXV
10184
10185         # now fill using QOS
10186         $LFS setstripe -c 1 $DIR/$tdir
10187         FILL=$((FILL / 200))
10188         if [ $FILL -gt 600 ]; then
10189                 FILL=600
10190         fi
10191         echo "writing $FILL files to QOS-assigned OSTs"
10192         i=0
10193         while [ $i -lt $FILL ]; do
10194                 i=$((i + 1))
10195                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
10196                         count=1 2>/dev/null
10197                 echo -n .
10198         done
10199         echo "wrote $i 200k files"
10200         sync
10201         sleep_maxage
10202
10203         echo "Note: free space may not be updated, so measurements might be off"
10204         free_min_max
10205         DIFF2=$((MAXV - MINV))
10206         echo "free space delta: orig $DIFF final $DIFF2"
10207         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
10208         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
10209         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
10210         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
10211         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
10212         if [[ $DIFF -gt 0 ]]; then
10213                 FILL=$((DIFF2 * 100 / DIFF - 100))
10214                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
10215         fi
10216
10217         # Figure out which files were written where
10218         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10219                awk '/'$MINI1': / {print $2; exit}')
10220         echo $UUID
10221         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10222         echo "$MINC files created on smaller OST $MINI1"
10223         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10224                awk '/'$MAXI1': / {print $2; exit}')
10225         echo $UUID
10226         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10227         echo "$MAXC files created on larger OST $MAXI1"
10228         if [[ $MINC -gt 0 ]]; then
10229                 FILL=$((MAXC * 100 / MINC - 100))
10230                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
10231         fi
10232         [[ $MAXC -gt $MINC ]] ||
10233                 error_ignore LU-9 "stripe QOS didn't balance free space"
10234         simple_cleanup_common
10235 }
10236 run_test 116a "stripe QOS: free space balance ==================="
10237
10238 test_116b() { # LU-2093
10239         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10240         remote_mds_nodsh && skip "remote MDS with nodsh"
10241
10242 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
10243         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
10244                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
10245         [ -z "$old_rr" ] && skip "no QOS"
10246         do_facet $SINGLEMDS lctl set_param \
10247                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
10248         mkdir -p $DIR/$tdir
10249         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
10250         createmany -o $DIR/$tdir/f- 20 || error "can't create"
10251         do_facet $SINGLEMDS lctl set_param fail_loc=0
10252         rm -rf $DIR/$tdir
10253         do_facet $SINGLEMDS lctl set_param \
10254                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
10255 }
10256 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
10257
10258 test_117() # bug 10891
10259 {
10260         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10261
10262         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
10263         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
10264         lctl set_param fail_loc=0x21e
10265         > $DIR/$tfile || error "truncate failed"
10266         lctl set_param fail_loc=0
10267         echo "Truncate succeeded."
10268         rm -f $DIR/$tfile
10269 }
10270 run_test 117 "verify osd extend =========="
10271
10272 NO_SLOW_RESENDCOUNT=4
10273 export OLD_RESENDCOUNT=""
10274 set_resend_count () {
10275         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
10276         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
10277         lctl set_param -n $PROC_RESENDCOUNT $1
10278         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
10279 }
10280
10281 # for reduce test_118* time (b=14842)
10282 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10283
10284 # Reset async IO behavior after error case
10285 reset_async() {
10286         FILE=$DIR/reset_async
10287
10288         # Ensure all OSCs are cleared
10289         $LFS setstripe -c -1 $FILE
10290         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
10291         sync
10292         rm $FILE
10293 }
10294
10295 test_118a() #bug 11710
10296 {
10297         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10298
10299         reset_async
10300
10301         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10302         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10303         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10304
10305         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10306                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10307                 return 1;
10308         fi
10309         rm -f $DIR/$tfile
10310 }
10311 run_test 118a "verify O_SYNC works =========="
10312
10313 test_118b()
10314 {
10315         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10316         remote_ost_nodsh && skip "remote OST with nodsh"
10317
10318         reset_async
10319
10320         #define OBD_FAIL_SRV_ENOENT 0x217
10321         set_nodes_failloc "$(osts_nodes)" 0x217
10322         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10323         RC=$?
10324         set_nodes_failloc "$(osts_nodes)" 0
10325         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10326         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10327                     grep -c writeback)
10328
10329         if [[ $RC -eq 0 ]]; then
10330                 error "Must return error due to dropped pages, rc=$RC"
10331                 return 1;
10332         fi
10333
10334         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10335                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10336                 return 1;
10337         fi
10338
10339         echo "Dirty pages not leaked on ENOENT"
10340
10341         # Due to the above error the OSC will issue all RPCs syncronously
10342         # until a subsequent RPC completes successfully without error.
10343         $MULTIOP $DIR/$tfile Ow4096yc
10344         rm -f $DIR/$tfile
10345
10346         return 0
10347 }
10348 run_test 118b "Reclaim dirty pages on fatal error =========="
10349
10350 test_118c()
10351 {
10352         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10353
10354         # for 118c, restore the original resend count, LU-1940
10355         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
10356                                 set_resend_count $OLD_RESENDCOUNT
10357         remote_ost_nodsh && skip "remote OST with nodsh"
10358
10359         reset_async
10360
10361         #define OBD_FAIL_OST_EROFS               0x216
10362         set_nodes_failloc "$(osts_nodes)" 0x216
10363
10364         # multiop should block due to fsync until pages are written
10365         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10366         MULTIPID=$!
10367         sleep 1
10368
10369         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10370                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10371         fi
10372
10373         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10374                     grep -c writeback)
10375         if [[ $WRITEBACK -eq 0 ]]; then
10376                 error "No page in writeback, writeback=$WRITEBACK"
10377         fi
10378
10379         set_nodes_failloc "$(osts_nodes)" 0
10380         wait $MULTIPID
10381         RC=$?
10382         if [[ $RC -ne 0 ]]; then
10383                 error "Multiop fsync failed, rc=$RC"
10384         fi
10385
10386         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10387         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10388                     grep -c writeback)
10389         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10390                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10391         fi
10392
10393         rm -f $DIR/$tfile
10394         echo "Dirty pages flushed via fsync on EROFS"
10395         return 0
10396 }
10397 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
10398
10399 # continue to use small resend count to reduce test_118* time (b=14842)
10400 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10401
10402 test_118d()
10403 {
10404         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10405         remote_ost_nodsh && skip "remote OST with nodsh"
10406
10407         reset_async
10408
10409         #define OBD_FAIL_OST_BRW_PAUSE_BULK
10410         set_nodes_failloc "$(osts_nodes)" 0x214
10411         # multiop should block due to fsync until pages are written
10412         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10413         MULTIPID=$!
10414         sleep 1
10415
10416         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10417                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10418         fi
10419
10420         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10421                     grep -c writeback)
10422         if [[ $WRITEBACK -eq 0 ]]; then
10423                 error "No page in writeback, writeback=$WRITEBACK"
10424         fi
10425
10426         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
10427         set_nodes_failloc "$(osts_nodes)" 0
10428
10429         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10430         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10431                     grep -c writeback)
10432         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10433                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10434         fi
10435
10436         rm -f $DIR/$tfile
10437         echo "Dirty pages gaurenteed flushed via fsync"
10438         return 0
10439 }
10440 run_test 118d "Fsync validation inject a delay of the bulk =========="
10441
10442 test_118f() {
10443         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10444
10445         reset_async
10446
10447         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
10448         lctl set_param fail_loc=0x8000040a
10449
10450         # Should simulate EINVAL error which is fatal
10451         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10452         RC=$?
10453         if [[ $RC -eq 0 ]]; then
10454                 error "Must return error due to dropped pages, rc=$RC"
10455         fi
10456
10457         lctl set_param fail_loc=0x0
10458
10459         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10460         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10461         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10462                     grep -c writeback)
10463         if [[ $LOCKED -ne 0 ]]; then
10464                 error "Locked pages remain in cache, locked=$LOCKED"
10465         fi
10466
10467         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10468                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10469         fi
10470
10471         rm -f $DIR/$tfile
10472         echo "No pages locked after fsync"
10473
10474         reset_async
10475         return 0
10476 }
10477 run_test 118f "Simulate unrecoverable OSC side error =========="
10478
10479 test_118g() {
10480         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10481
10482         reset_async
10483
10484         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
10485         lctl set_param fail_loc=0x406
10486
10487         # simulate local -ENOMEM
10488         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10489         RC=$?
10490
10491         lctl set_param fail_loc=0
10492         if [[ $RC -eq 0 ]]; then
10493                 error "Must return error due to dropped pages, rc=$RC"
10494         fi
10495
10496         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10497         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10498         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10499                         grep -c writeback)
10500         if [[ $LOCKED -ne 0 ]]; then
10501                 error "Locked pages remain in cache, locked=$LOCKED"
10502         fi
10503
10504         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10505                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10506         fi
10507
10508         rm -f $DIR/$tfile
10509         echo "No pages locked after fsync"
10510
10511         reset_async
10512         return 0
10513 }
10514 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
10515
10516 test_118h() {
10517         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10518         remote_ost_nodsh && skip "remote OST with nodsh"
10519
10520         reset_async
10521
10522         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10523         set_nodes_failloc "$(osts_nodes)" 0x20e
10524         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10525         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10526         RC=$?
10527
10528         set_nodes_failloc "$(osts_nodes)" 0
10529         if [[ $RC -eq 0 ]]; then
10530                 error "Must return error due to dropped pages, rc=$RC"
10531         fi
10532
10533         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10534         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10535         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10536                     grep -c writeback)
10537         if [[ $LOCKED -ne 0 ]]; then
10538                 error "Locked pages remain in cache, locked=$LOCKED"
10539         fi
10540
10541         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10542                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10543         fi
10544
10545         rm -f $DIR/$tfile
10546         echo "No pages locked after fsync"
10547
10548         return 0
10549 }
10550 run_test 118h "Verify timeout in handling recoverables errors  =========="
10551
10552 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10553
10554 test_118i() {
10555         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10556         remote_ost_nodsh && skip "remote OST with nodsh"
10557
10558         reset_async
10559
10560         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10561         set_nodes_failloc "$(osts_nodes)" 0x20e
10562
10563         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10564         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10565         PID=$!
10566         sleep 5
10567         set_nodes_failloc "$(osts_nodes)" 0
10568
10569         wait $PID
10570         RC=$?
10571         if [[ $RC -ne 0 ]]; then
10572                 error "got error, but should be not, rc=$RC"
10573         fi
10574
10575         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10576         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10577         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10578         if [[ $LOCKED -ne 0 ]]; then
10579                 error "Locked pages remain in cache, locked=$LOCKED"
10580         fi
10581
10582         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10583                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10584         fi
10585
10586         rm -f $DIR/$tfile
10587         echo "No pages locked after fsync"
10588
10589         return 0
10590 }
10591 run_test 118i "Fix error before timeout in recoverable error  =========="
10592
10593 [ "$SLOW" = "no" ] && set_resend_count 4
10594
10595 test_118j() {
10596         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10597         remote_ost_nodsh && skip "remote OST with nodsh"
10598
10599         reset_async
10600
10601         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
10602         set_nodes_failloc "$(osts_nodes)" 0x220
10603
10604         # return -EIO from OST
10605         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10606         RC=$?
10607         set_nodes_failloc "$(osts_nodes)" 0x0
10608         if [[ $RC -eq 0 ]]; then
10609                 error "Must return error due to dropped pages, rc=$RC"
10610         fi
10611
10612         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10613         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10614         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10615         if [[ $LOCKED -ne 0 ]]; then
10616                 error "Locked pages remain in cache, locked=$LOCKED"
10617         fi
10618
10619         # in recoverable error on OST we want resend and stay until it finished
10620         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10621                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10622         fi
10623
10624         rm -f $DIR/$tfile
10625         echo "No pages locked after fsync"
10626
10627         return 0
10628 }
10629 run_test 118j "Simulate unrecoverable OST side error =========="
10630
10631 test_118k()
10632 {
10633         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10634         remote_ost_nodsh && skip "remote OSTs with nodsh"
10635
10636         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10637         set_nodes_failloc "$(osts_nodes)" 0x20e
10638         test_mkdir $DIR/$tdir
10639
10640         for ((i=0;i<10;i++)); do
10641                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
10642                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
10643                 SLEEPPID=$!
10644                 sleep 0.500s
10645                 kill $SLEEPPID
10646                 wait $SLEEPPID
10647         done
10648
10649         set_nodes_failloc "$(osts_nodes)" 0
10650         rm -rf $DIR/$tdir
10651 }
10652 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
10653
10654 test_118l() # LU-646
10655 {
10656         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10657
10658         test_mkdir $DIR/$tdir
10659         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
10660         rm -rf $DIR/$tdir
10661 }
10662 run_test 118l "fsync dir"
10663
10664 test_118m() # LU-3066
10665 {
10666         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10667
10668         test_mkdir $DIR/$tdir
10669         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
10670         rm -rf $DIR/$tdir
10671 }
10672 run_test 118m "fdatasync dir ========="
10673
10674 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10675
10676 test_118n()
10677 {
10678         local begin
10679         local end
10680
10681         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10682         remote_ost_nodsh && skip "remote OSTs with nodsh"
10683
10684         # Sleep to avoid a cached response.
10685         #define OBD_STATFS_CACHE_SECONDS 1
10686         sleep 2
10687
10688         # Inject a 10 second delay in the OST_STATFS handler.
10689         #define OBD_FAIL_OST_STATFS_DELAY 0x242
10690         set_nodes_failloc "$(osts_nodes)" 0x242
10691
10692         begin=$SECONDS
10693         stat --file-system $MOUNT > /dev/null
10694         end=$SECONDS
10695
10696         set_nodes_failloc "$(osts_nodes)" 0
10697
10698         if ((end - begin > 20)); then
10699             error "statfs took $((end - begin)) seconds, expected 10"
10700         fi
10701 }
10702 run_test 118n "statfs() sends OST_STATFS requests in parallel"
10703
10704 test_119a() # bug 11737
10705 {
10706         BSIZE=$((512 * 1024))
10707         directio write $DIR/$tfile 0 1 $BSIZE
10708         # We ask to read two blocks, which is more than a file size.
10709         # directio will indicate an error when requested and actual
10710         # sizes aren't equeal (a normal situation in this case) and
10711         # print actual read amount.
10712         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
10713         if [ "$NOB" != "$BSIZE" ]; then
10714                 error "read $NOB bytes instead of $BSIZE"
10715         fi
10716         rm -f $DIR/$tfile
10717 }
10718 run_test 119a "Short directIO read must return actual read amount"
10719
10720 test_119b() # bug 11737
10721 {
10722         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10723
10724         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
10725         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
10726         sync
10727         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
10728                 error "direct read failed"
10729         rm -f $DIR/$tfile
10730 }
10731 run_test 119b "Sparse directIO read must return actual read amount"
10732
10733 test_119c() # bug 13099
10734 {
10735         BSIZE=1048576
10736         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
10737         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
10738         rm -f $DIR/$tfile
10739 }
10740 run_test 119c "Testing for direct read hitting hole"
10741
10742 test_119d() # bug 15950
10743 {
10744         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10745
10746         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
10747         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
10748         BSIZE=1048576
10749         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
10750         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
10751         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
10752         lctl set_param fail_loc=0x40d
10753         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
10754         pid_dio=$!
10755         sleep 1
10756         cat $DIR/$tfile > /dev/null &
10757         lctl set_param fail_loc=0
10758         pid_reads=$!
10759         wait $pid_dio
10760         log "the DIO writes have completed, now wait for the reads (should not block very long)"
10761         sleep 2
10762         [ -n "`ps h -p $pid_reads -o comm`" ] && \
10763         error "the read rpcs have not completed in 2s"
10764         rm -f $DIR/$tfile
10765         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
10766 }
10767 run_test 119d "The DIO path should try to send a new rpc once one is completed"
10768
10769 test_120a() {
10770         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10771         remote_mds_nodsh && skip "remote MDS with nodsh"
10772         test_mkdir -i0 -c1 $DIR/$tdir
10773         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10774                 skip_env "no early lock cancel on server"
10775
10776         lru_resize_disable mdc
10777         lru_resize_disable osc
10778         cancel_lru_locks mdc
10779         # asynchronous object destroy at MDT could cause bl ast to client
10780         cancel_lru_locks osc
10781
10782         stat $DIR/$tdir > /dev/null
10783         can1=$(do_facet mds1 \
10784                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10785                awk '/ldlm_cancel/ {print $2}')
10786         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10787                awk '/ldlm_bl_callback/ {print $2}')
10788         test_mkdir -i0 -c1 $DIR/$tdir/d1
10789         can2=$(do_facet mds1 \
10790                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10791                awk '/ldlm_cancel/ {print $2}')
10792         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10793                awk '/ldlm_bl_callback/ {print $2}')
10794         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10795         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10796         lru_resize_enable mdc
10797         lru_resize_enable osc
10798 }
10799 run_test 120a "Early Lock Cancel: mkdir test"
10800
10801 test_120b() {
10802         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10803         remote_mds_nodsh && skip "remote MDS with nodsh"
10804         test_mkdir $DIR/$tdir
10805         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10806                 skip_env "no early lock cancel on server"
10807
10808         lru_resize_disable mdc
10809         lru_resize_disable osc
10810         cancel_lru_locks mdc
10811         stat $DIR/$tdir > /dev/null
10812         can1=$(do_facet $SINGLEMDS \
10813                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10814                awk '/ldlm_cancel/ {print $2}')
10815         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10816                awk '/ldlm_bl_callback/ {print $2}')
10817         touch $DIR/$tdir/f1
10818         can2=$(do_facet $SINGLEMDS \
10819                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10820                awk '/ldlm_cancel/ {print $2}')
10821         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10822                awk '/ldlm_bl_callback/ {print $2}')
10823         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10824         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10825         lru_resize_enable mdc
10826         lru_resize_enable osc
10827 }
10828 run_test 120b "Early Lock Cancel: create test"
10829
10830 test_120c() {
10831         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10832         remote_mds_nodsh && skip "remote MDS with nodsh"
10833         test_mkdir -i0 -c1 $DIR/$tdir
10834         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10835                 skip "no early lock cancel on server"
10836
10837         lru_resize_disable mdc
10838         lru_resize_disable osc
10839         test_mkdir -i0 -c1 $DIR/$tdir/d1
10840         test_mkdir -i0 -c1 $DIR/$tdir/d2
10841         touch $DIR/$tdir/d1/f1
10842         cancel_lru_locks mdc
10843         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
10844         can1=$(do_facet mds1 \
10845                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10846                awk '/ldlm_cancel/ {print $2}')
10847         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10848                awk '/ldlm_bl_callback/ {print $2}')
10849         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10850         can2=$(do_facet mds1 \
10851                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10852                awk '/ldlm_cancel/ {print $2}')
10853         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10854                awk '/ldlm_bl_callback/ {print $2}')
10855         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10856         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10857         lru_resize_enable mdc
10858         lru_resize_enable osc
10859 }
10860 run_test 120c "Early Lock Cancel: link test"
10861
10862 test_120d() {
10863         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10864         remote_mds_nodsh && skip "remote MDS with nodsh"
10865         test_mkdir -i0 -c1 $DIR/$tdir
10866         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10867                 skip_env "no early lock cancel on server"
10868
10869         lru_resize_disable mdc
10870         lru_resize_disable osc
10871         touch $DIR/$tdir
10872         cancel_lru_locks mdc
10873         stat $DIR/$tdir > /dev/null
10874         can1=$(do_facet mds1 \
10875                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10876                awk '/ldlm_cancel/ {print $2}')
10877         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10878                awk '/ldlm_bl_callback/ {print $2}')
10879         chmod a+x $DIR/$tdir
10880         can2=$(do_facet mds1 \
10881                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10882                awk '/ldlm_cancel/ {print $2}')
10883         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10884                awk '/ldlm_bl_callback/ {print $2}')
10885         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10886         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10887         lru_resize_enable mdc
10888         lru_resize_enable osc
10889 }
10890 run_test 120d "Early Lock Cancel: setattr test"
10891
10892 test_120e() {
10893         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10894         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10895                 skip_env "no early lock cancel on server"
10896         remote_mds_nodsh && skip "remote MDS with nodsh"
10897
10898         local dlmtrace_set=false
10899
10900         test_mkdir -i0 -c1 $DIR/$tdir
10901         lru_resize_disable mdc
10902         lru_resize_disable osc
10903         ! $LCTL get_param debug | grep -q dlmtrace &&
10904                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
10905         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
10906         cancel_lru_locks mdc
10907         cancel_lru_locks osc
10908         dd if=$DIR/$tdir/f1 of=/dev/null
10909         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
10910         # XXX client can not do early lock cancel of OST lock
10911         # during unlink (LU-4206), so cancel osc lock now.
10912         sleep 2
10913         cancel_lru_locks osc
10914         can1=$(do_facet mds1 \
10915                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10916                awk '/ldlm_cancel/ {print $2}')
10917         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10918                awk '/ldlm_bl_callback/ {print $2}')
10919         unlink $DIR/$tdir/f1
10920         sleep 5
10921         can2=$(do_facet mds1 \
10922                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10923                awk '/ldlm_cancel/ {print $2}')
10924         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10925                awk '/ldlm_bl_callback/ {print $2}')
10926         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
10927                 $LCTL dk $TMP/cancel.debug.txt
10928         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
10929                 $LCTL dk $TMP/blocking.debug.txt
10930         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
10931         lru_resize_enable mdc
10932         lru_resize_enable osc
10933 }
10934 run_test 120e "Early Lock Cancel: unlink test"
10935
10936 test_120f() {
10937         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10938         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10939                 skip_env "no early lock cancel on server"
10940         remote_mds_nodsh && skip "remote MDS with nodsh"
10941
10942         test_mkdir -i0 -c1 $DIR/$tdir
10943         lru_resize_disable mdc
10944         lru_resize_disable osc
10945         test_mkdir -i0 -c1 $DIR/$tdir/d1
10946         test_mkdir -i0 -c1 $DIR/$tdir/d2
10947         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
10948         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
10949         cancel_lru_locks mdc
10950         cancel_lru_locks osc
10951         dd if=$DIR/$tdir/d1/f1 of=/dev/null
10952         dd if=$DIR/$tdir/d2/f2 of=/dev/null
10953         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
10954         # XXX client can not do early lock cancel of OST lock
10955         # during rename (LU-4206), so cancel osc lock now.
10956         sleep 2
10957         cancel_lru_locks osc
10958         can1=$(do_facet mds1 \
10959                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10960                awk '/ldlm_cancel/ {print $2}')
10961         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10962                awk '/ldlm_bl_callback/ {print $2}')
10963         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10964         sleep 5
10965         can2=$(do_facet mds1 \
10966                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10967                awk '/ldlm_cancel/ {print $2}')
10968         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10969                awk '/ldlm_bl_callback/ {print $2}')
10970         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10971         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10972         lru_resize_enable mdc
10973         lru_resize_enable osc
10974 }
10975 run_test 120f "Early Lock Cancel: rename test"
10976
10977 test_120g() {
10978         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10979         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10980                 skip_env "no early lock cancel on server"
10981         remote_mds_nodsh && skip "remote MDS with nodsh"
10982
10983         lru_resize_disable mdc
10984         lru_resize_disable osc
10985         count=10000
10986         echo create $count files
10987         test_mkdir $DIR/$tdir
10988         cancel_lru_locks mdc
10989         cancel_lru_locks osc
10990         t0=$(date +%s)
10991
10992         can0=$(do_facet $SINGLEMDS \
10993                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10994                awk '/ldlm_cancel/ {print $2}')
10995         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10996                awk '/ldlm_bl_callback/ {print $2}')
10997         createmany -o $DIR/$tdir/f $count
10998         sync
10999         can1=$(do_facet $SINGLEMDS \
11000                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11001                awk '/ldlm_cancel/ {print $2}')
11002         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11003                awk '/ldlm_bl_callback/ {print $2}')
11004         t1=$(date +%s)
11005         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
11006         echo rm $count files
11007         rm -r $DIR/$tdir
11008         sync
11009         can2=$(do_facet $SINGLEMDS \
11010                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11011                awk '/ldlm_cancel/ {print $2}')
11012         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11013                awk '/ldlm_bl_callback/ {print $2}')
11014         t2=$(date +%s)
11015         echo total: $count removes in $((t2-t1))
11016         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
11017         sleep 2
11018         # wait for commitment of removal
11019         lru_resize_enable mdc
11020         lru_resize_enable osc
11021 }
11022 run_test 120g "Early Lock Cancel: performance test"
11023
11024 test_121() { #bug #10589
11025         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11026
11027         rm -rf $DIR/$tfile
11028         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
11029 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
11030         lctl set_param fail_loc=0x310
11031         cancel_lru_locks osc > /dev/null
11032         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
11033         lctl set_param fail_loc=0
11034         [[ $reads -eq $writes ]] ||
11035                 error "read $reads blocks, must be $writes blocks"
11036 }
11037 run_test 121 "read cancel race ========="
11038
11039 test_123a() { # was test 123, statahead(bug 11401)
11040         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11041
11042         SLOWOK=0
11043         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
11044                 log "testing UP system. Performance may be lower than expected."
11045                 SLOWOK=1
11046         fi
11047
11048         rm -rf $DIR/$tdir
11049         test_mkdir $DIR/$tdir
11050         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
11051         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
11052         MULT=10
11053         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
11054                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
11055
11056                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
11057                 lctl set_param -n llite.*.statahead_max 0
11058                 lctl get_param llite.*.statahead_max
11059                 cancel_lru_locks mdc
11060                 cancel_lru_locks osc
11061                 stime=`date +%s`
11062                 time ls -l $DIR/$tdir | wc -l
11063                 etime=`date +%s`
11064                 delta=$((etime - stime))
11065                 log "ls $i files without statahead: $delta sec"
11066                 lctl set_param llite.*.statahead_max=$max
11067
11068                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
11069                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
11070                 cancel_lru_locks mdc
11071                 cancel_lru_locks osc
11072                 stime=`date +%s`
11073                 time ls -l $DIR/$tdir | wc -l
11074                 etime=`date +%s`
11075                 delta_sa=$((etime - stime))
11076                 log "ls $i files with statahead: $delta_sa sec"
11077                 lctl get_param -n llite.*.statahead_stats
11078                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
11079
11080                 [[ $swrong -lt $ewrong ]] &&
11081                         log "statahead was stopped, maybe too many locks held!"
11082                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
11083
11084                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
11085                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
11086                     lctl set_param -n llite.*.statahead_max 0
11087                     lctl get_param llite.*.statahead_max
11088                     cancel_lru_locks mdc
11089                     cancel_lru_locks osc
11090                     stime=`date +%s`
11091                     time ls -l $DIR/$tdir | wc -l
11092                     etime=`date +%s`
11093                     delta=$((etime - stime))
11094                     log "ls $i files again without statahead: $delta sec"
11095                     lctl set_param llite.*.statahead_max=$max
11096                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
11097                         if [  $SLOWOK -eq 0 ]; then
11098                                 error "ls $i files is slower with statahead!"
11099                         else
11100                                 log "ls $i files is slower with statahead!"
11101                         fi
11102                         break
11103                     fi
11104                 fi
11105
11106                 [ $delta -gt 20 ] && break
11107                 [ $delta -gt 8 ] && MULT=$((50 / delta))
11108                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
11109         done
11110         log "ls done"
11111
11112         stime=`date +%s`
11113         rm -r $DIR/$tdir
11114         sync
11115         etime=`date +%s`
11116         delta=$((etime - stime))
11117         log "rm -r $DIR/$tdir/: $delta seconds"
11118         log "rm done"
11119         lctl get_param -n llite.*.statahead_stats
11120 }
11121 run_test 123a "verify statahead work"
11122
11123 test_123b () { # statahead(bug 15027)
11124         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11125
11126         test_mkdir $DIR/$tdir
11127         createmany -o $DIR/$tdir/$tfile-%d 1000
11128
11129         cancel_lru_locks mdc
11130         cancel_lru_locks osc
11131
11132 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
11133         lctl set_param fail_loc=0x80000803
11134         ls -lR $DIR/$tdir > /dev/null
11135         log "ls done"
11136         lctl set_param fail_loc=0x0
11137         lctl get_param -n llite.*.statahead_stats
11138         rm -r $DIR/$tdir
11139         sync
11140
11141 }
11142 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
11143
11144 test_124a() {
11145         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11146         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11147                 skip_env "no lru resize on server"
11148
11149         local NR=2000
11150
11151         test_mkdir $DIR/$tdir
11152
11153         log "create $NR files at $DIR/$tdir"
11154         createmany -o $DIR/$tdir/f $NR ||
11155                 error "failed to create $NR files in $DIR/$tdir"
11156
11157         cancel_lru_locks mdc
11158         ls -l $DIR/$tdir > /dev/null
11159
11160         local NSDIR=""
11161         local LRU_SIZE=0
11162         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
11163                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
11164                 LRU_SIZE=$($LCTL get_param -n $PARAM)
11165                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
11166                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
11167                         log "NSDIR=$NSDIR"
11168                         log "NS=$(basename $NSDIR)"
11169                         break
11170                 fi
11171         done
11172
11173         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
11174                 skip "Not enough cached locks created!"
11175         fi
11176         log "LRU=$LRU_SIZE"
11177
11178         local SLEEP=30
11179
11180         # We know that lru resize allows one client to hold $LIMIT locks
11181         # for 10h. After that locks begin to be killed by client.
11182         local MAX_HRS=10
11183         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
11184         log "LIMIT=$LIMIT"
11185         if [ $LIMIT -lt $LRU_SIZE ]; then
11186                 skip "Limit is too small $LIMIT"
11187         fi
11188
11189         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
11190         # killing locks. Some time was spent for creating locks. This means
11191         # that up to the moment of sleep finish we must have killed some of
11192         # them (10-100 locks). This depends on how fast ther were created.
11193         # Many of them were touched in almost the same moment and thus will
11194         # be killed in groups.
11195         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
11196
11197         # Use $LRU_SIZE_B here to take into account real number of locks
11198         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
11199         local LRU_SIZE_B=$LRU_SIZE
11200         log "LVF=$LVF"
11201         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
11202         log "OLD_LVF=$OLD_LVF"
11203         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
11204
11205         # Let's make sure that we really have some margin. Client checks
11206         # cached locks every 10 sec.
11207         SLEEP=$((SLEEP+20))
11208         log "Sleep ${SLEEP} sec"
11209         local SEC=0
11210         while ((SEC<$SLEEP)); do
11211                 echo -n "..."
11212                 sleep 5
11213                 SEC=$((SEC+5))
11214                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
11215                 echo -n "$LRU_SIZE"
11216         done
11217         echo ""
11218         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
11219         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
11220
11221         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
11222                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
11223                 unlinkmany $DIR/$tdir/f $NR
11224                 return
11225         }
11226
11227         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
11228         log "unlink $NR files at $DIR/$tdir"
11229         unlinkmany $DIR/$tdir/f $NR
11230 }
11231 run_test 124a "lru resize ======================================="
11232
11233 get_max_pool_limit()
11234 {
11235         local limit=$($LCTL get_param \
11236                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
11237         local max=0
11238         for l in $limit; do
11239                 if [[ $l -gt $max ]]; then
11240                         max=$l
11241                 fi
11242         done
11243         echo $max
11244 }
11245
11246 test_124b() {
11247         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11248         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11249                 skip_env "no lru resize on server"
11250
11251         LIMIT=$(get_max_pool_limit)
11252
11253         NR=$(($(default_lru_size)*20))
11254         if [[ $NR -gt $LIMIT ]]; then
11255                 log "Limit lock number by $LIMIT locks"
11256                 NR=$LIMIT
11257         fi
11258
11259         IFree=$(mdsrate_inodes_available)
11260         if [ $IFree -lt $NR ]; then
11261                 log "Limit lock number by $IFree inodes"
11262                 NR=$IFree
11263         fi
11264
11265         lru_resize_disable mdc
11266         test_mkdir -p $DIR/$tdir/disable_lru_resize
11267
11268         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
11269         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
11270         cancel_lru_locks mdc
11271         stime=`date +%s`
11272         PID=""
11273         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11274         PID="$PID $!"
11275         sleep 2
11276         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11277         PID="$PID $!"
11278         sleep 2
11279         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11280         PID="$PID $!"
11281         wait $PID
11282         etime=`date +%s`
11283         nolruresize_delta=$((etime-stime))
11284         log "ls -la time: $nolruresize_delta seconds"
11285         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11286         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
11287
11288         lru_resize_enable mdc
11289         test_mkdir -p $DIR/$tdir/enable_lru_resize
11290
11291         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
11292         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
11293         cancel_lru_locks mdc
11294         stime=`date +%s`
11295         PID=""
11296         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11297         PID="$PID $!"
11298         sleep 2
11299         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11300         PID="$PID $!"
11301         sleep 2
11302         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11303         PID="$PID $!"
11304         wait $PID
11305         etime=`date +%s`
11306         lruresize_delta=$((etime-stime))
11307         log "ls -la time: $lruresize_delta seconds"
11308         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11309
11310         if [ $lruresize_delta -gt $nolruresize_delta ]; then
11311                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
11312         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
11313                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
11314         else
11315                 log "lru resize performs the same with no lru resize"
11316         fi
11317         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
11318 }
11319 run_test 124b "lru resize (performance test) ======================="
11320
11321 test_124c() {
11322         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11323         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11324                 skip_env "no lru resize on server"
11325
11326         # cache ununsed locks on client
11327         local nr=100
11328         cancel_lru_locks mdc
11329         test_mkdir $DIR/$tdir
11330         createmany -o $DIR/$tdir/f $nr ||
11331                 error "failed to create $nr files in $DIR/$tdir"
11332         ls -l $DIR/$tdir > /dev/null
11333
11334         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11335         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11336         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11337         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11338         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11339
11340         # set lru_max_age to 1 sec
11341         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11342         echo "sleep $((recalc_p * 2)) seconds..."
11343         sleep $((recalc_p * 2))
11344
11345         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11346         # restore lru_max_age
11347         $LCTL set_param -n $nsdir.lru_max_age $max_age
11348         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11349         unlinkmany $DIR/$tdir/f $nr
11350 }
11351 run_test 124c "LRUR cancel very aged locks"
11352
11353 test_124d() {
11354         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11355         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11356                 skip_env "no lru resize on server"
11357
11358         # cache ununsed locks on client
11359         local nr=100
11360
11361         lru_resize_disable mdc
11362         stack_trap "lru_resize_enable mdc" EXIT
11363
11364         cancel_lru_locks mdc
11365
11366         # asynchronous object destroy at MDT could cause bl ast to client
11367         test_mkdir $DIR/$tdir
11368         createmany -o $DIR/$tdir/f $nr ||
11369                 error "failed to create $nr files in $DIR/$tdir"
11370         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
11371
11372         ls -l $DIR/$tdir > /dev/null
11373
11374         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11375         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11376         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11377         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11378
11379         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11380
11381         # set lru_max_age to 1 sec
11382         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11383         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
11384
11385         echo "sleep $((recalc_p * 2)) seconds..."
11386         sleep $((recalc_p * 2))
11387
11388         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11389
11390         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11391 }
11392 run_test 124d "cancel very aged locks if lru-resize diasbaled"
11393
11394 test_125() { # 13358
11395         $LCTL get_param -n llite.*.client_type | grep -q local ||
11396                 skip "must run as local client"
11397         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
11398                 skip_env "must have acl enabled"
11399         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
11400
11401         test_mkdir $DIR/$tdir
11402         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
11403         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
11404         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
11405 }
11406 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
11407
11408 test_126() { # bug 12829/13455
11409         $GSS && skip_env "must run as gss disabled"
11410         $LCTL get_param -n llite.*.client_type | grep -q local ||
11411                 skip "must run as local client"
11412         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
11413
11414         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
11415         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
11416         rm -f $DIR/$tfile
11417         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
11418 }
11419 run_test 126 "check that the fsgid provided by the client is taken into account"
11420
11421 test_127a() { # bug 15521
11422         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11423         local name count samp unit min max sum sumsq
11424
11425         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
11426         echo "stats before reset"
11427         $LCTL get_param osc.*.stats
11428         $LCTL set_param osc.*.stats=0
11429         local fsize=$((2048 * 1024))
11430
11431         dd if=/dev/zero of=$DIR/$tfile bs=$fsize count=1
11432         cancel_lru_locks osc
11433         dd if=$DIR/$tfile of=/dev/null bs=$fsize
11434
11435         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/$tfile.tmp
11436         stack_trap "rm -f $TMP/$tfile.tmp"
11437         while read name count samp unit min max sum sumsq; do
11438                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11439                 [ ! $min ] && error "Missing min value for $name proc entry"
11440                 eval $name=$count || error "Wrong proc format"
11441
11442                 case $name in
11443                 read_bytes|write_bytes)
11444                         [[ "$unit" =~ "bytes" ]] ||
11445                                 error "unit is not 'bytes': $unit"
11446                         (( $min >= 4096 )) || error "min is too small: $min"
11447                         (( $min <= $fsize )) || error "min is too big: $min"
11448                         (( $max >= 4096 )) || error "max is too small: $max"
11449                         (( $max <= $fsize )) || error "max is too big: $max"
11450                         (( $sum == $fsize )) || error "sum is wrong: $sum"
11451                         (( $sumsq >= ($fsize / 4096) * (4096 * 4096) )) ||
11452                                 error "sumsquare is too small: $sumsq"
11453                         (( $sumsq <= $fsize * $fsize )) ||
11454                                 error "sumsquare is too big: $sumsq"
11455                         ;;
11456                 ost_read|ost_write)
11457                         [[ "$unit" =~ "usec" ]] ||
11458                                 error "unit is not 'usec': $unit"
11459                         ;;
11460                 *)      ;;
11461                 esac
11462         done < $DIR/$tfile.tmp
11463
11464         #check that we actually got some stats
11465         [ "$read_bytes" ] || error "Missing read_bytes stats"
11466         [ "$write_bytes" ] || error "Missing write_bytes stats"
11467         [ "$read_bytes" != 0 ] || error "no read done"
11468         [ "$write_bytes" != 0 ] || error "no write done"
11469 }
11470 run_test 127a "verify the client stats are sane"
11471
11472 test_127b() { # bug LU-333
11473         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11474         local name count samp unit min max sum sumsq
11475
11476         echo "stats before reset"
11477         $LCTL get_param llite.*.stats
11478         $LCTL set_param llite.*.stats=0
11479
11480         # perform 2 reads and writes so MAX is different from SUM.
11481         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11482         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11483         cancel_lru_locks osc
11484         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11485         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11486
11487         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
11488         stack_trap "rm -f $TMP/$tfile.tmp"
11489         while read name count samp unit min max sum sumsq; do
11490                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11491                 eval $name=$count || error "Wrong proc format"
11492
11493                 case $name in
11494                 read_bytes|write_bytes)
11495                         [[ "$unit" =~ "bytes" ]] ||
11496                                 error "unit is not 'bytes': $unit"
11497                         (( $count == 2 )) || error "count is not 2: $count"
11498                         (( $min == $PAGE_SIZE )) ||
11499                                 error "min is not $PAGE_SIZE: $min"
11500                         (( $max == $PAGE_SIZE )) ||
11501                                 error "max is not $PAGE_SIZE: $max"
11502                         (( $sum == $PAGE_SIZE * 2 )) ||
11503                                 error "sum is not $((PAGE_SIZE * 2)): $sum"
11504                         ;;
11505                 read|write)
11506                         [[ "$unit" =~ "usec" ]] ||
11507                                 error "unit is not 'usec': $unit"
11508                         ;;
11509                 *)      ;;
11510                 esac
11511         done < $TMP/$tfile.tmp
11512
11513         #check that we actually got some stats
11514         [ "$read_bytes" ] || error "Missing read_bytes stats"
11515         [ "$write_bytes" ] || error "Missing write_bytes stats"
11516         [ "$read_bytes" != 0 ] || error "no read done"
11517         [ "$write_bytes" != 0 ] || error "no write done"
11518 }
11519 run_test 127b "verify the llite client stats are sane"
11520
11521 test_127c() { # LU-12394
11522         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11523         local size
11524         local bsize
11525         local reads
11526         local writes
11527         local count
11528
11529         $LCTL set_param llite.*.extents_stats=1
11530         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
11531
11532         # Use two stripes so there is enough space in default config
11533         $LFS setstripe -c 2 $DIR/$tfile
11534
11535         # Extent stats start at 0-4K and go in power of two buckets
11536         # LL_HIST_START = 12 --> 2^12 = 4K
11537         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
11538         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
11539         # small configs
11540         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
11541                 do
11542                 # Write and read, 2x each, second time at a non-zero offset
11543                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
11544                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
11545                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
11546                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
11547                 rm -f $DIR/$tfile
11548         done
11549
11550         $LCTL get_param llite.*.extents_stats
11551
11552         count=2
11553         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
11554                 do
11555                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
11556                                 grep -m 1 $bsize)
11557                 reads=$(echo $bucket | awk '{print $5}')
11558                 writes=$(echo $bucket | awk '{print $9}')
11559                 [ "$reads" -eq $count ] ||
11560                         error "$reads reads in < $bsize bucket, expect $count"
11561                 [ "$writes" -eq $count ] ||
11562                         error "$writes writes in < $bsize bucket, expect $count"
11563         done
11564
11565         # Test mmap write and read
11566         $LCTL set_param llite.*.extents_stats=c
11567         size=512
11568         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
11569         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
11570         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
11571
11572         $LCTL get_param llite.*.extents_stats
11573
11574         count=$(((size*1024) / PAGE_SIZE))
11575
11576         bsize=$((2 * PAGE_SIZE / 1024))K
11577
11578         bucket=$($LCTL get_param -n llite.*.extents_stats |
11579                         grep -m 1 $bsize)
11580         reads=$(echo $bucket | awk '{print $5}')
11581         writes=$(echo $bucket | awk '{print $9}')
11582         # mmap writes fault in the page first, creating an additonal read
11583         [ "$reads" -eq $((2 * count)) ] ||
11584                 error "$reads reads in < $bsize bucket, expect $count"
11585         [ "$writes" -eq $count ] ||
11586                 error "$writes writes in < $bsize bucket, expect $count"
11587 }
11588 run_test 127c "test llite extent stats with regular & mmap i/o"
11589
11590 test_128() { # bug 15212
11591         touch $DIR/$tfile
11592         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
11593                 find $DIR/$tfile
11594                 find $DIR/$tfile
11595         EOF
11596
11597         result=$(grep error $TMP/$tfile.log)
11598         rm -f $DIR/$tfile $TMP/$tfile.log
11599         [ -z "$result" ] ||
11600                 error "consecutive find's under interactive lfs failed"
11601 }
11602 run_test 128 "interactive lfs for 2 consecutive find's"
11603
11604 set_dir_limits () {
11605         local mntdev
11606         local canondev
11607         local node
11608
11609         local ldproc=/proc/fs/ldiskfs
11610         local facets=$(get_facets MDS)
11611
11612         for facet in ${facets//,/ }; do
11613                 canondev=$(ldiskfs_canon \
11614                            *.$(convert_facet2label $facet).mntdev $facet)
11615                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
11616                         ldproc=/sys/fs/ldiskfs
11617                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
11618                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
11619         done
11620 }
11621
11622 check_mds_dmesg() {
11623         local facets=$(get_facets MDS)
11624         for facet in ${facets//,/ }; do
11625                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
11626         done
11627         return 1
11628 }
11629
11630 test_129() {
11631         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11632         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
11633                 skip "Need MDS version with at least 2.5.56"
11634         if [ "$mds1_FSTYPE" != ldiskfs ]; then
11635                 skip_env "ldiskfs only test"
11636         fi
11637         remote_mds_nodsh && skip "remote MDS with nodsh"
11638
11639         local ENOSPC=28
11640         local EFBIG=27
11641         local has_warning=false
11642
11643         rm -rf $DIR/$tdir
11644         mkdir -p $DIR/$tdir
11645
11646         # block size of mds1
11647         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
11648         set_dir_limits $maxsize $maxsize
11649         local dirsize=$(stat -c%s "$DIR/$tdir")
11650         local nfiles=0
11651         while [[ $dirsize -le $maxsize ]]; do
11652                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
11653                 rc=$?
11654                 if ! $has_warning; then
11655                         check_mds_dmesg '"is approaching"' && has_warning=true
11656                 fi
11657                 # check two errors:
11658                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
11659                 # EFBIG for previous versions included in ldiskfs series
11660                 if [ $rc -eq $EFBIG ] || [ $rc -eq $ENOSPC ]; then
11661                         set_dir_limits 0 0
11662                         echo "return code $rc received as expected"
11663
11664                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
11665                                 error_exit "create failed w/o dir size limit"
11666
11667                         check_mds_dmesg '"has reached"' ||
11668                                 error_exit "reached message should be output"
11669
11670                         [ $has_warning = "false" ] &&
11671                                 error_exit "warning message should be output"
11672
11673                         dirsize=$(stat -c%s "$DIR/$tdir")
11674
11675                         [[ $dirsize -ge $maxsize ]] && return 0
11676                         error_exit "current dir size $dirsize, " \
11677                                    "previous limit $maxsize"
11678                 elif [ $rc -ne 0 ]; then
11679                         set_dir_limits 0 0
11680                         error_exit "return $rc received instead of expected " \
11681                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
11682                 fi
11683                 nfiles=$((nfiles + 1))
11684                 dirsize=$(stat -c%s "$DIR/$tdir")
11685         done
11686
11687         set_dir_limits 0 0
11688         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
11689 }
11690 run_test 129 "test directory size limit ========================"
11691
11692 OLDIFS="$IFS"
11693 cleanup_130() {
11694         trap 0
11695         IFS="$OLDIFS"
11696 }
11697
11698 test_130a() {
11699         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11700         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11701
11702         trap cleanup_130 EXIT RETURN
11703
11704         local fm_file=$DIR/$tfile
11705         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
11706         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
11707                 error "dd failed for $fm_file"
11708
11709         # LU-1795: test filefrag/FIEMAP once, even if unsupported
11710         filefrag -ves $fm_file
11711         RC=$?
11712         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11713                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11714         [ $RC != 0 ] && error "filefrag $fm_file failed"
11715
11716         filefrag_op=$(filefrag -ve -k $fm_file |
11717                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11718         lun=$($LFS getstripe -i $fm_file)
11719
11720         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
11721         IFS=$'\n'
11722         tot_len=0
11723         for line in $filefrag_op
11724         do
11725                 frag_lun=`echo $line | cut -d: -f5`
11726                 ext_len=`echo $line | cut -d: -f4`
11727                 if (( $frag_lun != $lun )); then
11728                         cleanup_130
11729                         error "FIEMAP on 1-stripe file($fm_file) failed"
11730                         return
11731                 fi
11732                 (( tot_len += ext_len ))
11733         done
11734
11735         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
11736                 cleanup_130
11737                 error "FIEMAP on 1-stripe file($fm_file) failed;"
11738                 return
11739         fi
11740
11741         cleanup_130
11742
11743         echo "FIEMAP on single striped file succeeded"
11744 }
11745 run_test 130a "FIEMAP (1-stripe file)"
11746
11747 test_130b() {
11748         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11749
11750         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11751         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11752
11753         trap cleanup_130 EXIT RETURN
11754
11755         local fm_file=$DIR/$tfile
11756         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
11757                         error "setstripe on $fm_file"
11758         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11759                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11760
11761         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
11762                 error "dd failed on $fm_file"
11763
11764         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11765         filefrag_op=$(filefrag -ve -k $fm_file |
11766                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11767
11768         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11769                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11770
11771         IFS=$'\n'
11772         tot_len=0
11773         num_luns=1
11774         for line in $filefrag_op
11775         do
11776                 frag_lun=$(echo $line | cut -d: -f5 |
11777                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11778                 ext_len=$(echo $line | cut -d: -f4)
11779                 if (( $frag_lun != $last_lun )); then
11780                         if (( tot_len != 1024 )); then
11781                                 cleanup_130
11782                                 error "FIEMAP on $fm_file failed; returned " \
11783                                 "len $tot_len for OST $last_lun instead of 1024"
11784                                 return
11785                         else
11786                                 (( num_luns += 1 ))
11787                                 tot_len=0
11788                         fi
11789                 fi
11790                 (( tot_len += ext_len ))
11791                 last_lun=$frag_lun
11792         done
11793         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
11794                 cleanup_130
11795                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11796                         "luns or wrong len for OST $last_lun"
11797                 return
11798         fi
11799
11800         cleanup_130
11801
11802         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
11803 }
11804 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
11805
11806 test_130c() {
11807         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11808
11809         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11810         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11811
11812         trap cleanup_130 EXIT RETURN
11813
11814         local fm_file=$DIR/$tfile
11815         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
11816         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11817                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11818
11819         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
11820                         error "dd failed on $fm_file"
11821
11822         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11823         filefrag_op=$(filefrag -ve -k $fm_file |
11824                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11825
11826         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11827                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11828
11829         IFS=$'\n'
11830         tot_len=0
11831         num_luns=1
11832         for line in $filefrag_op
11833         do
11834                 frag_lun=$(echo $line | cut -d: -f5 |
11835                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11836                 ext_len=$(echo $line | cut -d: -f4)
11837                 if (( $frag_lun != $last_lun )); then
11838                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
11839                         if (( logical != 512 )); then
11840                                 cleanup_130
11841                                 error "FIEMAP on $fm_file failed; returned " \
11842                                 "logical start for lun $logical instead of 512"
11843                                 return
11844                         fi
11845                         if (( tot_len != 512 )); then
11846                                 cleanup_130
11847                                 error "FIEMAP on $fm_file failed; returned " \
11848                                 "len $tot_len for OST $last_lun instead of 1024"
11849                                 return
11850                         else
11851                                 (( num_luns += 1 ))
11852                                 tot_len=0
11853                         fi
11854                 fi
11855                 (( tot_len += ext_len ))
11856                 last_lun=$frag_lun
11857         done
11858         if (( num_luns != 2 || tot_len != 512 )); then
11859                 cleanup_130
11860                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11861                         "luns or wrong len for OST $last_lun"
11862                 return
11863         fi
11864
11865         cleanup_130
11866
11867         echo "FIEMAP on 2-stripe file with hole succeeded"
11868 }
11869 run_test 130c "FIEMAP (2-stripe file with hole)"
11870
11871 test_130d() {
11872         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
11873
11874         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11875         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11876
11877         trap cleanup_130 EXIT RETURN
11878
11879         local fm_file=$DIR/$tfile
11880         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
11881                         error "setstripe on $fm_file"
11882         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11883                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11884
11885         local actual_stripe_count=$($LFS getstripe -c $fm_file)
11886         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
11887                 error "dd failed on $fm_file"
11888
11889         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11890         filefrag_op=$(filefrag -ve -k $fm_file |
11891                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11892
11893         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11894                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11895
11896         IFS=$'\n'
11897         tot_len=0
11898         num_luns=1
11899         for line in $filefrag_op
11900         do
11901                 frag_lun=$(echo $line | cut -d: -f5 |
11902                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11903                 ext_len=$(echo $line | cut -d: -f4)
11904                 if (( $frag_lun != $last_lun )); then
11905                         if (( tot_len != 1024 )); then
11906                                 cleanup_130
11907                                 error "FIEMAP on $fm_file failed; returned " \
11908                                 "len $tot_len for OST $last_lun instead of 1024"
11909                                 return
11910                         else
11911                                 (( num_luns += 1 ))
11912                                 tot_len=0
11913                         fi
11914                 fi
11915                 (( tot_len += ext_len ))
11916                 last_lun=$frag_lun
11917         done
11918         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
11919                 cleanup_130
11920                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11921                         "luns or wrong len for OST $last_lun"
11922                 return
11923         fi
11924
11925         cleanup_130
11926
11927         echo "FIEMAP on N-stripe file succeeded"
11928 }
11929 run_test 130d "FIEMAP (N-stripe file)"
11930
11931 test_130e() {
11932         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11933
11934         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11935         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11936
11937         trap cleanup_130 EXIT RETURN
11938
11939         local fm_file=$DIR/$tfile
11940         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
11941         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11942                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11943
11944         NUM_BLKS=512
11945         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
11946         for ((i = 0; i < $NUM_BLKS; i++))
11947         do
11948                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
11949         done
11950
11951         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11952         filefrag_op=$(filefrag -ve -k $fm_file |
11953                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11954
11955         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11956                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11957
11958         IFS=$'\n'
11959         tot_len=0
11960         num_luns=1
11961         for line in $filefrag_op
11962         do
11963                 frag_lun=$(echo $line | cut -d: -f5 |
11964                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11965                 ext_len=$(echo $line | cut -d: -f4)
11966                 if (( $frag_lun != $last_lun )); then
11967                         if (( tot_len != $EXPECTED_LEN )); then
11968                                 cleanup_130
11969                                 error "FIEMAP on $fm_file failed; returned " \
11970                                 "len $tot_len for OST $last_lun instead " \
11971                                 "of $EXPECTED_LEN"
11972                                 return
11973                         else
11974                                 (( num_luns += 1 ))
11975                                 tot_len=0
11976                         fi
11977                 fi
11978                 (( tot_len += ext_len ))
11979                 last_lun=$frag_lun
11980         done
11981         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
11982                 cleanup_130
11983                 error "FIEMAP on $fm_file failed; returned wrong number " \
11984                         "of luns or wrong len for OST $last_lun"
11985                 return
11986         fi
11987
11988         cleanup_130
11989
11990         echo "FIEMAP with continuation calls succeeded"
11991 }
11992 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
11993
11994 test_130f() {
11995         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11996         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11997
11998         local fm_file=$DIR/$tfile
11999         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
12000                 error "multiop create with lov_delay_create on $fm_file"
12001
12002         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12003         filefrag_extents=$(filefrag -vek $fm_file |
12004                            awk '/extents? found/ { print $2 }')
12005         if [[ "$filefrag_extents" != "0" ]]; then
12006                 error "FIEMAP on $fm_file failed; " \
12007                       "returned $filefrag_extents expected 0"
12008         fi
12009
12010         rm -f $fm_file
12011 }
12012 run_test 130f "FIEMAP (unstriped file)"
12013
12014 # Test for writev/readv
12015 test_131a() {
12016         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
12017                 error "writev test failed"
12018         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
12019                 error "readv failed"
12020         rm -f $DIR/$tfile
12021 }
12022 run_test 131a "test iov's crossing stripe boundary for writev/readv"
12023
12024 test_131b() {
12025         local fsize=$((524288 + 1048576 + 1572864))
12026         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
12027                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12028                         error "append writev test failed"
12029
12030         ((fsize += 1572864 + 1048576))
12031         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
12032                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12033                         error "append writev test failed"
12034         rm -f $DIR/$tfile
12035 }
12036 run_test 131b "test append writev"
12037
12038 test_131c() {
12039         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
12040         error "NOT PASS"
12041 }
12042 run_test 131c "test read/write on file w/o objects"
12043
12044 test_131d() {
12045         rwv -f $DIR/$tfile -w -n 1 1572864
12046         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
12047         if [ "$NOB" != 1572864 ]; then
12048                 error "Short read filed: read $NOB bytes instead of 1572864"
12049         fi
12050         rm -f $DIR/$tfile
12051 }
12052 run_test 131d "test short read"
12053
12054 test_131e() {
12055         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
12056         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
12057         error "read hitting hole failed"
12058         rm -f $DIR/$tfile
12059 }
12060 run_test 131e "test read hitting hole"
12061
12062 check_stats() {
12063         local facet=$1
12064         local op=$2
12065         local want=${3:-0}
12066         local res
12067
12068         case $facet in
12069         mds*) res=$(do_facet $facet \
12070                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
12071                  ;;
12072         ost*) res=$(do_facet $facet \
12073                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
12074                  ;;
12075         *) error "Wrong facet '$facet'" ;;
12076         esac
12077         [ "$res" ] || error "The counter for $op on $facet was not incremented"
12078         # if the argument $3 is zero, it means any stat increment is ok.
12079         if [[ $want -gt 0 ]]; then
12080                 local count=$(echo $res | awk '{ print $2 }')
12081                 [[ $count -ne $want ]] &&
12082                         error "The $op counter on $facet is $count, not $want"
12083         fi
12084 }
12085
12086 test_133a() {
12087         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12088         remote_ost_nodsh && skip "remote OST with nodsh"
12089         remote_mds_nodsh && skip "remote MDS with nodsh"
12090         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12091                 skip_env "MDS doesn't support rename stats"
12092
12093         local testdir=$DIR/${tdir}/stats_testdir
12094
12095         mkdir -p $DIR/${tdir}
12096
12097         # clear stats.
12098         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12099         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12100
12101         # verify mdt stats first.
12102         mkdir ${testdir} || error "mkdir failed"
12103         check_stats $SINGLEMDS "mkdir" 1
12104         touch ${testdir}/${tfile} || error "touch failed"
12105         check_stats $SINGLEMDS "open" 1
12106         check_stats $SINGLEMDS "close" 1
12107         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
12108                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
12109                 check_stats $SINGLEMDS "mknod" 2
12110         }
12111         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
12112         check_stats $SINGLEMDS "unlink" 1
12113         rm -f ${testdir}/${tfile} || error "file remove failed"
12114         check_stats $SINGLEMDS "unlink" 2
12115
12116         # remove working dir and check mdt stats again.
12117         rmdir ${testdir} || error "rmdir failed"
12118         check_stats $SINGLEMDS "rmdir" 1
12119
12120         local testdir1=$DIR/${tdir}/stats_testdir1
12121         mkdir -p ${testdir}
12122         mkdir -p ${testdir1}
12123         touch ${testdir1}/test1
12124         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
12125         check_stats $SINGLEMDS "crossdir_rename" 1
12126
12127         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
12128         check_stats $SINGLEMDS "samedir_rename" 1
12129
12130         rm -rf $DIR/${tdir}
12131 }
12132 run_test 133a "Verifying MDT stats ========================================"
12133
12134 test_133b() {
12135         local res
12136
12137         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12138         remote_ost_nodsh && skip "remote OST with nodsh"
12139         remote_mds_nodsh && skip "remote MDS with nodsh"
12140
12141         local testdir=$DIR/${tdir}/stats_testdir
12142
12143         mkdir -p ${testdir} || error "mkdir failed"
12144         touch ${testdir}/${tfile} || error "touch failed"
12145         cancel_lru_locks mdc
12146
12147         # clear stats.
12148         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12149         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12150
12151         # extra mdt stats verification.
12152         chmod 444 ${testdir}/${tfile} || error "chmod failed"
12153         check_stats $SINGLEMDS "setattr" 1
12154         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12155         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
12156         then            # LU-1740
12157                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
12158                 check_stats $SINGLEMDS "getattr" 1
12159         fi
12160         rm -rf $DIR/${tdir}
12161
12162         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
12163         # so the check below is not reliable
12164         [ $MDSCOUNT -eq 1 ] || return 0
12165
12166         # Sleep to avoid a cached response.
12167         #define OBD_STATFS_CACHE_SECONDS 1
12168         sleep 2
12169         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12170         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12171         $LFS df || error "lfs failed"
12172         check_stats $SINGLEMDS "statfs" 1
12173
12174         # check aggregated statfs (LU-10018)
12175         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
12176                 return 0
12177         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
12178                 return 0
12179         sleep 2
12180         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12181         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12182         df $DIR
12183         check_stats $SINGLEMDS "statfs" 1
12184
12185         # We want to check that the client didn't send OST_STATFS to
12186         # ost1 but the MDT also uses OST_STATFS for precreate. So some
12187         # extra care is needed here.
12188         if remote_mds; then
12189                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
12190                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
12191
12192                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
12193                 [ "$res" ] && error "OST got STATFS"
12194         fi
12195
12196         return 0
12197 }
12198 run_test 133b "Verifying extra MDT stats =================================="
12199
12200 test_133c() {
12201         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12202         remote_ost_nodsh && skip "remote OST with nodsh"
12203         remote_mds_nodsh && skip "remote MDS with nodsh"
12204
12205         local testdir=$DIR/$tdir/stats_testdir
12206
12207         test_mkdir -p $testdir
12208
12209         # verify obdfilter stats.
12210         $LFS setstripe -c 1 -i 0 $testdir/$tfile
12211         sync
12212         cancel_lru_locks osc
12213         wait_delete_completed
12214
12215         # clear stats.
12216         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12217         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12218
12219         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
12220                 error "dd failed"
12221         sync
12222         cancel_lru_locks osc
12223         check_stats ost1 "write" 1
12224
12225         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
12226         check_stats ost1 "read" 1
12227
12228         > $testdir/$tfile || error "truncate failed"
12229         check_stats ost1 "punch" 1
12230
12231         rm -f $testdir/$tfile || error "file remove failed"
12232         wait_delete_completed
12233         check_stats ost1 "destroy" 1
12234
12235         rm -rf $DIR/$tdir
12236 }
12237 run_test 133c "Verifying OST stats ========================================"
12238
12239 order_2() {
12240         local value=$1
12241         local orig=$value
12242         local order=1
12243
12244         while [ $value -ge 2 ]; do
12245                 order=$((order*2))
12246                 value=$((value/2))
12247         done
12248
12249         if [ $orig -gt $order ]; then
12250                 order=$((order*2))
12251         fi
12252         echo $order
12253 }
12254
12255 size_in_KMGT() {
12256     local value=$1
12257     local size=('K' 'M' 'G' 'T');
12258     local i=0
12259     local size_string=$value
12260
12261     while [ $value -ge 1024 ]; do
12262         if [ $i -gt 3 ]; then
12263             #T is the biggest unit we get here, if that is bigger,
12264             #just return XXXT
12265             size_string=${value}T
12266             break
12267         fi
12268         value=$((value >> 10))
12269         if [ $value -lt 1024 ]; then
12270             size_string=${value}${size[$i]}
12271             break
12272         fi
12273         i=$((i + 1))
12274     done
12275
12276     echo $size_string
12277 }
12278
12279 get_rename_size() {
12280         local size=$1
12281         local context=${2:-.}
12282         local sample=$(do_facet $SINGLEMDS $LCTL \
12283                 get_param mdt.$FSNAME-MDT0000.rename_stats |
12284                 grep -A1 $context |
12285                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
12286         echo $sample
12287 }
12288
12289 test_133d() {
12290         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12291         remote_ost_nodsh && skip "remote OST with nodsh"
12292         remote_mds_nodsh && skip "remote MDS with nodsh"
12293         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12294                 skip_env "MDS doesn't support rename stats"
12295
12296         local testdir1=$DIR/${tdir}/stats_testdir1
12297         local testdir2=$DIR/${tdir}/stats_testdir2
12298         mkdir -p $DIR/${tdir}
12299
12300         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12301
12302         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
12303         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
12304
12305         createmany -o $testdir1/test 512 || error "createmany failed"
12306
12307         # check samedir rename size
12308         mv ${testdir1}/test0 ${testdir1}/test_0
12309
12310         local testdir1_size=$(ls -l $DIR/${tdir} |
12311                 awk '/stats_testdir1/ {print $5}')
12312         local testdir2_size=$(ls -l $DIR/${tdir} |
12313                 awk '/stats_testdir2/ {print $5}')
12314
12315         testdir1_size=$(order_2 $testdir1_size)
12316         testdir2_size=$(order_2 $testdir2_size)
12317
12318         testdir1_size=$(size_in_KMGT $testdir1_size)
12319         testdir2_size=$(size_in_KMGT $testdir2_size)
12320
12321         echo "source rename dir size: ${testdir1_size}"
12322         echo "target rename dir size: ${testdir2_size}"
12323
12324         local cmd="do_facet $SINGLEMDS $LCTL "
12325         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
12326
12327         eval $cmd || error "$cmd failed"
12328         local samedir=$($cmd | grep 'same_dir')
12329         local same_sample=$(get_rename_size $testdir1_size)
12330         [ -z "$samedir" ] && error "samedir_rename_size count error"
12331         [[ $same_sample -eq 1 ]] ||
12332                 error "samedir_rename_size error $same_sample"
12333         echo "Check same dir rename stats success"
12334
12335         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12336
12337         # check crossdir rename size
12338         mv ${testdir1}/test_0 ${testdir2}/test_0
12339
12340         testdir1_size=$(ls -l $DIR/${tdir} |
12341                 awk '/stats_testdir1/ {print $5}')
12342         testdir2_size=$(ls -l $DIR/${tdir} |
12343                 awk '/stats_testdir2/ {print $5}')
12344
12345         testdir1_size=$(order_2 $testdir1_size)
12346         testdir2_size=$(order_2 $testdir2_size)
12347
12348         testdir1_size=$(size_in_KMGT $testdir1_size)
12349         testdir2_size=$(size_in_KMGT $testdir2_size)
12350
12351         echo "source rename dir size: ${testdir1_size}"
12352         echo "target rename dir size: ${testdir2_size}"
12353
12354         eval $cmd || error "$cmd failed"
12355         local crossdir=$($cmd | grep 'crossdir')
12356         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
12357         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
12358         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
12359         [[ $src_sample -eq 1 ]] ||
12360                 error "crossdir_rename_size error $src_sample"
12361         [[ $tgt_sample -eq 1 ]] ||
12362                 error "crossdir_rename_size error $tgt_sample"
12363         echo "Check cross dir rename stats success"
12364         rm -rf $DIR/${tdir}
12365 }
12366 run_test 133d "Verifying rename_stats ========================================"
12367
12368 test_133e() {
12369         remote_mds_nodsh && skip "remote MDS with nodsh"
12370         remote_ost_nodsh && skip "remote OST with nodsh"
12371         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12372
12373         local testdir=$DIR/${tdir}/stats_testdir
12374         local ctr f0 f1 bs=32768 count=42 sum
12375
12376         mkdir -p ${testdir} || error "mkdir failed"
12377
12378         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
12379
12380         for ctr in {write,read}_bytes; do
12381                 sync
12382                 cancel_lru_locks osc
12383
12384                 do_facet ost1 $LCTL set_param -n \
12385                         "obdfilter.*.exports.clear=clear"
12386
12387                 if [ $ctr = write_bytes ]; then
12388                         f0=/dev/zero
12389                         f1=${testdir}/${tfile}
12390                 else
12391                         f0=${testdir}/${tfile}
12392                         f1=/dev/null
12393                 fi
12394
12395                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
12396                         error "dd failed"
12397                 sync
12398                 cancel_lru_locks osc
12399
12400                 sum=$(do_facet ost1 $LCTL get_param \
12401                         "obdfilter.*.exports.*.stats" |
12402                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
12403                                 $1 == ctr { sum += $7 }
12404                                 END { printf("%0.0f", sum) }')
12405
12406                 if ((sum != bs * count)); then
12407                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
12408                 fi
12409         done
12410
12411         rm -rf $DIR/${tdir}
12412 }
12413 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
12414
12415 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
12416
12417 # Some versions of find (4.5.11, 4.5.14) included in CentOS 7.3-7.5 do
12418 # not honor the -ignore_readdir_race option correctly. So we call
12419 # error_ignore() rather than error() in these cases. See LU-11152.
12420 error_133() {
12421         if (find --version; do_facet mds1 find --version) |
12422                 grep -q '\b4\.5\.1[1-4]\b'; then
12423                 error_ignore LU-11152 "$@"
12424         else
12425                 error "$@"
12426         fi
12427 }
12428
12429 test_133f() {
12430         # First without trusting modes.
12431         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
12432         echo "proc_dirs='$proc_dirs'"
12433         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
12434         find $proc_dirs -exec cat '{}' \; &> /dev/null
12435
12436         # Second verifying readability.
12437         $LCTL get_param -R '*' &> /dev/null
12438
12439         # Verifing writability with badarea_io.
12440         find $proc_dirs \
12441                 -ignore_readdir_race \
12442                 -type f \
12443                 -not -name force_lbug \
12444                 -not -name changelog_mask \
12445                 -exec badarea_io '{}' \; ||
12446                         error_133 "find $proc_dirs failed"
12447 }
12448 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
12449
12450 test_133g() {
12451         remote_mds_nodsh && skip "remote MDS with nodsh"
12452         remote_ost_nodsh && skip "remote OST with nodsh"
12453
12454         # eventually, this can also be replaced with "lctl get_param -R",
12455         # but not until that option is always available on the server
12456         local facet
12457         for facet in mds1 ost1; do
12458                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
12459                         skip_noexit "Too old lustre on $facet"
12460                 local facet_proc_dirs=$(do_facet $facet \
12461                                         \\\ls -d $proc_regexp 2>/dev/null)
12462                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12463                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12464                 do_facet $facet find $facet_proc_dirs \
12465                         ! -name req_history \
12466                         -exec cat '{}' \\\; &> /dev/null
12467
12468                 do_facet $facet find $facet_proc_dirs \
12469                         ! -name req_history \
12470                         -type f \
12471                         -exec cat '{}' \\\; &> /dev/null ||
12472                                 error "proc file read failed"
12473
12474                 do_facet $facet find $facet_proc_dirs \
12475                         -ignore_readdir_race \
12476                         -type f \
12477                         -not -name force_lbug \
12478                         -not -name changelog_mask \
12479                         -exec badarea_io '{}' \\\; ||
12480                                 error_133 "$facet find $facet_proc_dirs failed"
12481         done
12482
12483         # remount the FS in case writes/reads /proc break the FS
12484         cleanup || error "failed to unmount"
12485         setup || error "failed to setup"
12486         true
12487 }
12488 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
12489
12490 test_133h() {
12491         remote_mds_nodsh && skip "remote MDS with nodsh"
12492         remote_ost_nodsh && skip "remote OST with nodsh"
12493         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
12494                 skip "Need MDS version at least 2.9.54"
12495
12496         local facet
12497
12498         for facet in client mds1 ost1; do
12499                 local facet_proc_dirs=$(do_facet $facet \
12500                                         \\\ls -d $proc_regexp 2> /dev/null)
12501                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12502                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12503                 # Get the list of files that are missing the terminating newline
12504                 local missing=($(do_facet $facet \
12505                         find ${facet_proc_dirs} -type f \|              \
12506                                 while read F\; do                       \
12507                                         awk -v FS='\v' -v RS='\v\v'     \
12508                                         "'END { if(NR>0 &&              \
12509                                         \\\$NF !~ /.*\\\n\$/)           \
12510                                                 print FILENAME}'"       \
12511                                         '\$F'\;                         \
12512                                 done 2>/dev/null))
12513                 [ ${#missing[*]} -eq 0 ] ||
12514                         error "files do not end with newline: ${missing[*]}"
12515         done
12516 }
12517 run_test 133h "Proc files should end with newlines"
12518
12519 test_134a() {
12520         remote_mds_nodsh && skip "remote MDS with nodsh"
12521         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12522                 skip "Need MDS version at least 2.7.54"
12523
12524         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12525         cancel_lru_locks mdc
12526
12527         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
12528         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12529         [ $unused -eq 0 ] || error "$unused locks are not cleared"
12530
12531         local nr=1000
12532         createmany -o $DIR/$tdir/f $nr ||
12533                 error "failed to create $nr files in $DIR/$tdir"
12534         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12535
12536         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
12537         do_facet mds1 $LCTL set_param fail_loc=0x327
12538         do_facet mds1 $LCTL set_param fail_val=500
12539         touch $DIR/$tdir/m
12540
12541         echo "sleep 10 seconds ..."
12542         sleep 10
12543         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
12544
12545         do_facet mds1 $LCTL set_param fail_loc=0
12546         do_facet mds1 $LCTL set_param fail_val=0
12547         [ $lck_cnt -lt $unused ] ||
12548                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
12549
12550         rm $DIR/$tdir/m
12551         unlinkmany $DIR/$tdir/f $nr
12552 }
12553 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
12554
12555 test_134b() {
12556         remote_mds_nodsh && skip "remote MDS with nodsh"
12557         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12558                 skip "Need MDS version at least 2.7.54"
12559
12560         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12561         cancel_lru_locks mdc
12562
12563         local low_wm=$(do_facet mds1 $LCTL get_param -n \
12564                         ldlm.lock_reclaim_threshold_mb)
12565         # disable reclaim temporarily
12566         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
12567
12568         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
12569         do_facet mds1 $LCTL set_param fail_loc=0x328
12570         do_facet mds1 $LCTL set_param fail_val=500
12571
12572         $LCTL set_param debug=+trace
12573
12574         local nr=600
12575         createmany -o $DIR/$tdir/f $nr &
12576         local create_pid=$!
12577
12578         echo "Sleep $TIMEOUT seconds ..."
12579         sleep $TIMEOUT
12580         if ! ps -p $create_pid  > /dev/null 2>&1; then
12581                 do_facet mds1 $LCTL set_param fail_loc=0
12582                 do_facet mds1 $LCTL set_param fail_val=0
12583                 do_facet mds1 $LCTL set_param \
12584                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
12585                 error "createmany finished incorrectly!"
12586         fi
12587         do_facet mds1 $LCTL set_param fail_loc=0
12588         do_facet mds1 $LCTL set_param fail_val=0
12589         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
12590         wait $create_pid || return 1
12591
12592         unlinkmany $DIR/$tdir/f $nr
12593 }
12594 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
12595
12596 test_140() { #bug-17379
12597         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12598
12599         test_mkdir $DIR/$tdir
12600         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
12601         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
12602
12603         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
12604         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
12605         local i=0
12606         while i=$((i + 1)); do
12607                 test_mkdir $i
12608                 cd $i || error "Changing to $i"
12609                 ln -s ../stat stat || error "Creating stat symlink"
12610                 # Read the symlink until ELOOP present,
12611                 # not LBUGing the system is considered success,
12612                 # we didn't overrun the stack.
12613                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
12614                 if [ $ret -ne 0 ]; then
12615                         if [ $ret -eq 40 ]; then
12616                                 break  # -ELOOP
12617                         else
12618                                 error "Open stat symlink"
12619                                         return
12620                         fi
12621                 fi
12622         done
12623         i=$((i - 1))
12624         echo "The symlink depth = $i"
12625         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
12626                 error "Invalid symlink depth"
12627
12628         # Test recursive symlink
12629         ln -s symlink_self symlink_self
12630         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
12631         echo "open symlink_self returns $ret"
12632         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
12633 }
12634 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
12635
12636 test_150() {
12637         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12638
12639         local TF="$TMP/$tfile"
12640
12641         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12642         cp $TF $DIR/$tfile
12643         cancel_lru_locks $OSC
12644         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
12645         remount_client $MOUNT
12646         df -P $MOUNT
12647         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
12648
12649         $TRUNCATE $TF 6000
12650         $TRUNCATE $DIR/$tfile 6000
12651         cancel_lru_locks $OSC
12652         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
12653
12654         echo "12345" >>$TF
12655         echo "12345" >>$DIR/$tfile
12656         cancel_lru_locks $OSC
12657         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
12658
12659         echo "12345" >>$TF
12660         echo "12345" >>$DIR/$tfile
12661         cancel_lru_locks $OSC
12662         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
12663
12664         rm -f $TF
12665         true
12666 }
12667 run_test 150 "truncate/append tests"
12668
12669 #LU-2902 roc_hit was not able to read all values from lproc
12670 function roc_hit_init() {
12671         local list=$(comma_list $(osts_nodes))
12672         local dir=$DIR/$tdir-check
12673         local file=$dir/$tfile
12674         local BEFORE
12675         local AFTER
12676         local idx
12677
12678         test_mkdir $dir
12679         #use setstripe to do a write to every ost
12680         for i in $(seq 0 $((OSTCOUNT-1))); do
12681                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
12682                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
12683                 idx=$(printf %04x $i)
12684                 BEFORE=$(get_osd_param $list *OST*$idx stats |
12685                         awk '$1 == "cache_access" {sum += $7}
12686                                 END { printf("%0.0f", sum) }')
12687
12688                 cancel_lru_locks osc
12689                 cat $file >/dev/null
12690
12691                 AFTER=$(get_osd_param $list *OST*$idx stats |
12692                         awk '$1 == "cache_access" {sum += $7}
12693                                 END { printf("%0.0f", sum) }')
12694
12695                 echo BEFORE:$BEFORE AFTER:$AFTER
12696                 if ! let "AFTER - BEFORE == 4"; then
12697                         rm -rf $dir
12698                         error "roc_hit is not safe to use"
12699                 fi
12700                 rm $file
12701         done
12702
12703         rm -rf $dir
12704 }
12705
12706 function roc_hit() {
12707         local list=$(comma_list $(osts_nodes))
12708         echo $(get_osd_param $list '' stats |
12709                 awk '$1 == "cache_hit" {sum += $7}
12710                         END { printf("%0.0f", sum) }')
12711 }
12712
12713 function set_cache() {
12714         local on=1
12715
12716         if [ "$2" == "off" ]; then
12717                 on=0;
12718         fi
12719         local list=$(comma_list $(osts_nodes))
12720         set_osd_param $list '' $1_cache_enable $on
12721
12722         cancel_lru_locks osc
12723 }
12724
12725 test_151() {
12726         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12727         remote_ost_nodsh && skip "remote OST with nodsh"
12728
12729         local CPAGES=3
12730         local list=$(comma_list $(osts_nodes))
12731
12732         # check whether obdfilter is cache capable at all
12733         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
12734                 skip "not cache-capable obdfilter"
12735         fi
12736
12737         # check cache is enabled on all obdfilters
12738         if get_osd_param $list '' read_cache_enable | grep 0; then
12739                 skip "oss cache is disabled"
12740         fi
12741
12742         set_osd_param $list '' writethrough_cache_enable 1
12743
12744         # check write cache is enabled on all obdfilters
12745         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
12746                 skip "oss write cache is NOT enabled"
12747         fi
12748
12749         roc_hit_init
12750
12751         #define OBD_FAIL_OBD_NO_LRU  0x609
12752         do_nodes $list $LCTL set_param fail_loc=0x609
12753
12754         # pages should be in the case right after write
12755         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
12756                 error "dd failed"
12757
12758         local BEFORE=$(roc_hit)
12759         cancel_lru_locks osc
12760         cat $DIR/$tfile >/dev/null
12761         local AFTER=$(roc_hit)
12762
12763         do_nodes $list $LCTL set_param fail_loc=0
12764
12765         if ! let "AFTER - BEFORE == CPAGES"; then
12766                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12767         fi
12768
12769         cancel_lru_locks osc
12770         # invalidates OST cache
12771         do_nodes $list "echo 1 > /proc/sys/vm/drop_caches"
12772         set_osd_param $list '' read_cache_enable 0
12773         cat $DIR/$tfile >/dev/null
12774
12775         # now data shouldn't be found in the cache
12776         BEFORE=$(roc_hit)
12777         cancel_lru_locks osc
12778         cat $DIR/$tfile >/dev/null
12779         AFTER=$(roc_hit)
12780         if let "AFTER - BEFORE != 0"; then
12781                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12782         fi
12783
12784         set_osd_param $list '' read_cache_enable 1
12785         rm -f $DIR/$tfile
12786 }
12787 run_test 151 "test cache on oss and controls ==============================="
12788
12789 test_152() {
12790         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12791
12792         local TF="$TMP/$tfile"
12793
12794         # simulate ENOMEM during write
12795 #define OBD_FAIL_OST_NOMEM      0x226
12796         lctl set_param fail_loc=0x80000226
12797         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12798         cp $TF $DIR/$tfile
12799         sync || error "sync failed"
12800         lctl set_param fail_loc=0
12801
12802         # discard client's cache
12803         cancel_lru_locks osc
12804
12805         # simulate ENOMEM during read
12806         lctl set_param fail_loc=0x80000226
12807         cmp $TF $DIR/$tfile || error "cmp failed"
12808         lctl set_param fail_loc=0
12809
12810         rm -f $TF
12811 }
12812 run_test 152 "test read/write with enomem ============================"
12813
12814 test_153() {
12815         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
12816 }
12817 run_test 153 "test if fdatasync does not crash ======================="
12818
12819 dot_lustre_fid_permission_check() {
12820         local fid=$1
12821         local ffid=$MOUNT/.lustre/fid/$fid
12822         local test_dir=$2
12823
12824         echo "stat fid $fid"
12825         stat $ffid > /dev/null || error "stat $ffid failed."
12826         echo "touch fid $fid"
12827         touch $ffid || error "touch $ffid failed."
12828         echo "write to fid $fid"
12829         cat /etc/hosts > $ffid || error "write $ffid failed."
12830         echo "read fid $fid"
12831         diff /etc/hosts $ffid || error "read $ffid failed."
12832         echo "append write to fid $fid"
12833         cat /etc/hosts >> $ffid || error "append write $ffid failed."
12834         echo "rename fid $fid"
12835         mv $ffid $test_dir/$tfile.1 &&
12836                 error "rename $ffid to $tfile.1 should fail."
12837         touch $test_dir/$tfile.1
12838         mv $test_dir/$tfile.1 $ffid &&
12839                 error "rename $tfile.1 to $ffid should fail."
12840         rm -f $test_dir/$tfile.1
12841         echo "truncate fid $fid"
12842         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
12843         echo "link fid $fid"
12844         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
12845         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
12846                 echo "setfacl fid $fid"
12847                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
12848                 echo "getfacl fid $fid"
12849                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
12850         fi
12851         echo "unlink fid $fid"
12852         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
12853         echo "mknod fid $fid"
12854         mknod $ffid c 1 3 && error "mknod $ffid should fail."
12855
12856         fid=[0xf00000400:0x1:0x0]
12857         ffid=$MOUNT/.lustre/fid/$fid
12858
12859         echo "stat non-exist fid $fid"
12860         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
12861         echo "write to non-exist fid $fid"
12862         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
12863         echo "link new fid $fid"
12864         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
12865
12866         mkdir -p $test_dir/$tdir
12867         touch $test_dir/$tdir/$tfile
12868         fid=$($LFS path2fid $test_dir/$tdir)
12869         rc=$?
12870         [ $rc -ne 0 ] &&
12871                 error "error: could not get fid for $test_dir/$dir/$tfile."
12872
12873         ffid=$MOUNT/.lustre/fid/$fid
12874
12875         echo "ls $fid"
12876         ls $ffid > /dev/null || error "ls $ffid failed."
12877         echo "touch $fid/$tfile.1"
12878         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
12879
12880         echo "touch $MOUNT/.lustre/fid/$tfile"
12881         touch $MOUNT/.lustre/fid/$tfile && \
12882                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
12883
12884         echo "setxattr to $MOUNT/.lustre/fid"
12885         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
12886
12887         echo "listxattr for $MOUNT/.lustre/fid"
12888         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
12889
12890         echo "delxattr from $MOUNT/.lustre/fid"
12891         setfattr -x trusted.name1 $MOUNT/.lustre/fid
12892
12893         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
12894         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
12895                 error "touch invalid fid should fail."
12896
12897         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
12898         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
12899                 error "touch non-normal fid should fail."
12900
12901         echo "rename $tdir to $MOUNT/.lustre/fid"
12902         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
12903                 error "rename to $MOUNT/.lustre/fid should fail."
12904
12905         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
12906         then            # LU-3547
12907                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
12908                 local new_obf_mode=777
12909
12910                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
12911                 chmod $new_obf_mode $DIR/.lustre/fid ||
12912                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
12913
12914                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
12915                 [ $obf_mode -eq $new_obf_mode ] ||
12916                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
12917
12918                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
12919                 chmod $old_obf_mode $DIR/.lustre/fid ||
12920                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
12921         fi
12922
12923         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
12924         fid=$($LFS path2fid $test_dir/$tfile-2)
12925
12926         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
12927         then # LU-5424
12928                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
12929                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
12930                         error "create lov data thru .lustre failed"
12931         fi
12932         echo "cp /etc/passwd $test_dir/$tfile-2"
12933         cp /etc/passwd $test_dir/$tfile-2 ||
12934                 error "copy to $test_dir/$tfile-2 failed."
12935         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
12936         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
12937                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
12938
12939         rm -rf $test_dir/tfile.lnk
12940         rm -rf $test_dir/$tfile-2
12941 }
12942
12943 test_154A() {
12944         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12945                 skip "Need MDS version at least 2.4.1"
12946
12947         local tf=$DIR/$tfile
12948         touch $tf
12949
12950         local fid=$($LFS path2fid $tf)
12951         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
12952
12953         # check that we get the same pathname back
12954         local found=$($LFS fid2path $MOUNT "$fid")
12955         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
12956         [ "$found" == "$tf" ] ||
12957                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
12958 }
12959 run_test 154A "lfs path2fid and fid2path basic checks"
12960
12961 test_154B() {
12962         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12963                 skip "Need MDS version at least 2.4.1"
12964
12965         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
12966         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
12967         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
12968         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
12969
12970         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
12971         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
12972
12973         # check that we get the same pathname
12974         echo "PFID: $PFID, name: $name"
12975         local FOUND=$($LFS fid2path $MOUNT "$PFID")
12976         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
12977         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
12978                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
12979
12980         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
12981 }
12982 run_test 154B "verify the ll_decode_linkea tool"
12983
12984 test_154a() {
12985         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12986         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12987         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
12988                 skip "Need MDS version at least 2.2.51"
12989         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
12990
12991         cp /etc/hosts $DIR/$tfile
12992
12993         fid=$($LFS path2fid $DIR/$tfile)
12994         rc=$?
12995         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
12996
12997         dot_lustre_fid_permission_check "$fid" $DIR ||
12998                 error "dot lustre permission check $fid failed"
12999
13000         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
13001
13002         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
13003
13004         touch $MOUNT/.lustre/file &&
13005                 error "creation is not allowed under .lustre"
13006
13007         mkdir $MOUNT/.lustre/dir &&
13008                 error "mkdir is not allowed under .lustre"
13009
13010         rm -rf $DIR/$tfile
13011 }
13012 run_test 154a "Open-by-FID"
13013
13014 test_154b() {
13015         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13016         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13017         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13018         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
13019                 skip "Need MDS version at least 2.2.51"
13020
13021         local remote_dir=$DIR/$tdir/remote_dir
13022         local MDTIDX=1
13023         local rc=0
13024
13025         mkdir -p $DIR/$tdir
13026         $LFS mkdir -i $MDTIDX $remote_dir ||
13027                 error "create remote directory failed"
13028
13029         cp /etc/hosts $remote_dir/$tfile
13030
13031         fid=$($LFS path2fid $remote_dir/$tfile)
13032         rc=$?
13033         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
13034
13035         dot_lustre_fid_permission_check "$fid" $remote_dir ||
13036                 error "dot lustre permission check $fid failed"
13037         rm -rf $DIR/$tdir
13038 }
13039 run_test 154b "Open-by-FID for remote directory"
13040
13041 test_154c() {
13042         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13043                 skip "Need MDS version at least 2.4.1"
13044
13045         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
13046         local FID1=$($LFS path2fid $DIR/$tfile.1)
13047         local FID2=$($LFS path2fid $DIR/$tfile.2)
13048         local FID3=$($LFS path2fid $DIR/$tfile.3)
13049
13050         local N=1
13051         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
13052                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
13053                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
13054                 local want=FID$N
13055                 [ "$FID" = "${!want}" ] ||
13056                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
13057                 N=$((N + 1))
13058         done
13059
13060         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
13061         do
13062                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
13063                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
13064                 N=$((N + 1))
13065         done
13066 }
13067 run_test 154c "lfs path2fid and fid2path multiple arguments"
13068
13069 test_154d() {
13070         remote_mds_nodsh && skip "remote MDS with nodsh"
13071         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
13072                 skip "Need MDS version at least 2.5.53"
13073
13074         if remote_mds; then
13075                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
13076         else
13077                 nid="0@lo"
13078         fi
13079         local proc_ofile="mdt.*.exports.'$nid'.open_files"
13080         local fd
13081         local cmd
13082
13083         rm -f $DIR/$tfile
13084         touch $DIR/$tfile
13085
13086         local fid=$($LFS path2fid $DIR/$tfile)
13087         # Open the file
13088         fd=$(free_fd)
13089         cmd="exec $fd<$DIR/$tfile"
13090         eval $cmd
13091         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
13092         echo "$fid_list" | grep "$fid"
13093         rc=$?
13094
13095         cmd="exec $fd>/dev/null"
13096         eval $cmd
13097         if [ $rc -ne 0 ]; then
13098                 error "FID $fid not found in open files list $fid_list"
13099         fi
13100 }
13101 run_test 154d "Verify open file fid"
13102
13103 test_154e()
13104 {
13105         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
13106                 skip "Need MDS version at least 2.6.50"
13107
13108         if ls -a $MOUNT | grep -q '^\.lustre$'; then
13109                 error ".lustre returned by readdir"
13110         fi
13111 }
13112 run_test 154e ".lustre is not returned by readdir"
13113
13114 test_154f() {
13115         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13116
13117         # create parent directory on a single MDT to avoid cross-MDT hardlinks
13118         test_mkdir -p -c1 $DIR/$tdir/d
13119         # test dirs inherit from its stripe
13120         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
13121         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
13122         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
13123         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
13124         touch $DIR/f
13125
13126         # get fid of parents
13127         local FID0=$($LFS path2fid $DIR/$tdir/d)
13128         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
13129         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
13130         local FID3=$($LFS path2fid $DIR)
13131
13132         # check that path2fid --parents returns expected <parent_fid>/name
13133         # 1) test for a directory (single parent)
13134         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
13135         [ "$parent" == "$FID0/foo1" ] ||
13136                 error "expected parent: $FID0/foo1, got: $parent"
13137
13138         # 2) test for a file with nlink > 1 (multiple parents)
13139         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
13140         echo "$parent" | grep -F "$FID1/$tfile" ||
13141                 error "$FID1/$tfile not returned in parent list"
13142         echo "$parent" | grep -F "$FID2/link" ||
13143                 error "$FID2/link not returned in parent list"
13144
13145         # 3) get parent by fid
13146         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
13147         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13148         echo "$parent" | grep -F "$FID1/$tfile" ||
13149                 error "$FID1/$tfile not returned in parent list (by fid)"
13150         echo "$parent" | grep -F "$FID2/link" ||
13151                 error "$FID2/link not returned in parent list (by fid)"
13152
13153         # 4) test for entry in root directory
13154         parent=$($LFS path2fid --parents $DIR/f)
13155         echo "$parent" | grep -F "$FID3/f" ||
13156                 error "$FID3/f not returned in parent list"
13157
13158         # 5) test it on root directory
13159         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
13160                 error "$MOUNT should not have parents"
13161
13162         # enable xattr caching and check that linkea is correctly updated
13163         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
13164         save_lustre_params client "llite.*.xattr_cache" > $save
13165         lctl set_param llite.*.xattr_cache 1
13166
13167         # 6.1) linkea update on rename
13168         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
13169
13170         # get parents by fid
13171         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13172         # foo1 should no longer be returned in parent list
13173         echo "$parent" | grep -F "$FID1" &&
13174                 error "$FID1 should no longer be in parent list"
13175         # the new path should appear
13176         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
13177                 error "$FID2/$tfile.moved is not in parent list"
13178
13179         # 6.2) linkea update on unlink
13180         rm -f $DIR/$tdir/d/foo2/link
13181         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13182         # foo2/link should no longer be returned in parent list
13183         echo "$parent" | grep -F "$FID2/link" &&
13184                 error "$FID2/link should no longer be in parent list"
13185         true
13186
13187         rm -f $DIR/f
13188         restore_lustre_params < $save
13189         rm -f $save
13190 }
13191 run_test 154f "get parent fids by reading link ea"
13192
13193 test_154g()
13194 {
13195         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13196         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
13197            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
13198                 skip "Need MDS version at least 2.6.92"
13199
13200         mkdir -p $DIR/$tdir
13201         llapi_fid_test -d $DIR/$tdir
13202 }
13203 run_test 154g "various llapi FID tests"
13204
13205 test_155_small_load() {
13206     local temp=$TMP/$tfile
13207     local file=$DIR/$tfile
13208
13209     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
13210         error "dd of=$temp bs=6096 count=1 failed"
13211     cp $temp $file
13212     cancel_lru_locks $OSC
13213     cmp $temp $file || error "$temp $file differ"
13214
13215     $TRUNCATE $temp 6000
13216     $TRUNCATE $file 6000
13217     cmp $temp $file || error "$temp $file differ (truncate1)"
13218
13219     echo "12345" >>$temp
13220     echo "12345" >>$file
13221     cmp $temp $file || error "$temp $file differ (append1)"
13222
13223     echo "12345" >>$temp
13224     echo "12345" >>$file
13225     cmp $temp $file || error "$temp $file differ (append2)"
13226
13227     rm -f $temp $file
13228     true
13229 }
13230
13231 test_155_big_load() {
13232         remote_ost_nodsh && skip "remote OST with nodsh"
13233
13234         local temp=$TMP/$tfile
13235         local file=$DIR/$tfile
13236
13237         free_min_max
13238         local cache_size=$(do_facet ost$((MAXI+1)) \
13239                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
13240         local large_file_size=$((cache_size * 2))
13241
13242         echo "OSS cache size: $cache_size KB"
13243         echo "Large file size: $large_file_size KB"
13244
13245         [ $MAXV -le $large_file_size ] &&
13246                 skip_env "max available OST size needs > $large_file_size KB"
13247
13248         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
13249
13250         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
13251                 error "dd of=$temp bs=$large_file_size count=1k failed"
13252         cp $temp $file
13253         ls -lh $temp $file
13254         cancel_lru_locks osc
13255         cmp $temp $file || error "$temp $file differ"
13256
13257         rm -f $temp $file
13258         true
13259 }
13260
13261 save_writethrough() {
13262         local facets=$(get_facets OST)
13263
13264         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
13265 }
13266
13267 test_155a() {
13268         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13269
13270         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13271
13272         save_writethrough $p
13273
13274         set_cache read on
13275         set_cache writethrough on
13276         test_155_small_load
13277         restore_lustre_params < $p
13278         rm -f $p
13279 }
13280 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
13281
13282 test_155b() {
13283         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13284
13285         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13286
13287         save_writethrough $p
13288
13289         set_cache read on
13290         set_cache writethrough off
13291         test_155_small_load
13292         restore_lustre_params < $p
13293         rm -f $p
13294 }
13295 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
13296
13297 test_155c() {
13298         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13299
13300         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13301
13302         save_writethrough $p
13303
13304         set_cache read off
13305         set_cache writethrough on
13306         test_155_small_load
13307         restore_lustre_params < $p
13308         rm -f $p
13309 }
13310 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
13311
13312 test_155d() {
13313         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13314
13315         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13316
13317         save_writethrough $p
13318
13319         set_cache read off
13320         set_cache writethrough off
13321         test_155_small_load
13322         restore_lustre_params < $p
13323         rm -f $p
13324 }
13325 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
13326
13327 test_155e() {
13328         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13329
13330         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13331
13332         save_writethrough $p
13333
13334         set_cache read on
13335         set_cache writethrough on
13336         test_155_big_load
13337         restore_lustre_params < $p
13338         rm -f $p
13339 }
13340 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
13341
13342 test_155f() {
13343         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13344
13345         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13346
13347         save_writethrough $p
13348
13349         set_cache read on
13350         set_cache writethrough off
13351         test_155_big_load
13352         restore_lustre_params < $p
13353         rm -f $p
13354 }
13355 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
13356
13357 test_155g() {
13358         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13359
13360         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13361
13362         save_writethrough $p
13363
13364         set_cache read off
13365         set_cache writethrough on
13366         test_155_big_load
13367         restore_lustre_params < $p
13368         rm -f $p
13369 }
13370 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
13371
13372 test_155h() {
13373         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13374
13375         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13376
13377         save_writethrough $p
13378
13379         set_cache read off
13380         set_cache writethrough off
13381         test_155_big_load
13382         restore_lustre_params < $p
13383         rm -f $p
13384 }
13385 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
13386
13387 test_156() {
13388         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13389         remote_ost_nodsh && skip "remote OST with nodsh"
13390         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
13391                 skip "stats not implemented on old servers"
13392         [ "$ost1_FSTYPE" = "zfs" ] &&
13393                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
13394
13395         local CPAGES=3
13396         local BEFORE
13397         local AFTER
13398         local file="$DIR/$tfile"
13399         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13400
13401         save_writethrough $p
13402         roc_hit_init
13403
13404         log "Turn on read and write cache"
13405         set_cache read on
13406         set_cache writethrough on
13407
13408         log "Write data and read it back."
13409         log "Read should be satisfied from the cache."
13410         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13411         BEFORE=$(roc_hit)
13412         cancel_lru_locks osc
13413         cat $file >/dev/null
13414         AFTER=$(roc_hit)
13415         if ! let "AFTER - BEFORE == CPAGES"; then
13416                 error "NOT IN CACHE (2): before: $BEFORE, after: $AFTER"
13417         else
13418                 log "cache hits: before: $BEFORE, after: $AFTER"
13419         fi
13420
13421         log "Read again; it should be satisfied from the cache."
13422         BEFORE=$AFTER
13423         cancel_lru_locks osc
13424         cat $file >/dev/null
13425         AFTER=$(roc_hit)
13426         if ! let "AFTER - BEFORE == CPAGES"; then
13427                 error "NOT IN CACHE (3): before: $BEFORE, after: $AFTER"
13428         else
13429                 log "cache hits:: before: $BEFORE, after: $AFTER"
13430         fi
13431
13432         log "Turn off the read cache and turn on the write cache"
13433         set_cache read off
13434         set_cache writethrough on
13435
13436         log "Read again; it should be satisfied from the cache."
13437         BEFORE=$(roc_hit)
13438         cancel_lru_locks osc
13439         cat $file >/dev/null
13440         AFTER=$(roc_hit)
13441         if ! let "AFTER - BEFORE == CPAGES"; then
13442                 error "NOT IN CACHE (4): before: $BEFORE, after: $AFTER"
13443         else
13444                 log "cache hits:: before: $BEFORE, after: $AFTER"
13445         fi
13446
13447         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
13448                 # > 2.12.56 uses pagecache if cached
13449                 log "Read again; it should not be satisfied from the cache."
13450                 BEFORE=$AFTER
13451                 cancel_lru_locks osc
13452                 cat $file >/dev/null
13453                 AFTER=$(roc_hit)
13454                 if ! let "AFTER - BEFORE == 0"; then
13455                         error "IN CACHE (5): before: $BEFORE, after: $AFTER"
13456                 else
13457                         log "cache hits:: before: $BEFORE, after: $AFTER"
13458                 fi
13459         fi
13460
13461         log "Write data and read it back."
13462         log "Read should be satisfied from the cache."
13463         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13464         BEFORE=$(roc_hit)
13465         cancel_lru_locks osc
13466         cat $file >/dev/null
13467         AFTER=$(roc_hit)
13468         if ! let "AFTER - BEFORE == CPAGES"; then
13469                 error "NOT IN CACHE (6): before: $BEFORE, after: $AFTER"
13470         else
13471                 log "cache hits:: before: $BEFORE, after: $AFTER"
13472         fi
13473
13474         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
13475                 # > 2.12.56 uses pagecache if cached
13476                 log "Read again; it should not be satisfied from the cache."
13477                 BEFORE=$AFTER
13478                 cancel_lru_locks osc
13479                 cat $file >/dev/null
13480                 AFTER=$(roc_hit)
13481                 if ! let "AFTER - BEFORE == 0"; then
13482                         error "IN CACHE (7): before: $BEFORE, after: $AFTER"
13483                 else
13484                         log "cache hits:: before: $BEFORE, after: $AFTER"
13485                 fi
13486         fi
13487
13488         log "Turn off read and write cache"
13489         set_cache read off
13490         set_cache writethrough off
13491
13492         log "Write data and read it back"
13493         log "It should not be satisfied from the cache."
13494         rm -f $file
13495         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13496         cancel_lru_locks osc
13497         BEFORE=$(roc_hit)
13498         cat $file >/dev/null
13499         AFTER=$(roc_hit)
13500         if ! let "AFTER - BEFORE == 0"; then
13501                 error_ignore bz20762 "IN CACHE (8):before:$BEFORE,after:$AFTER"
13502         else
13503                 log "cache hits:: before: $BEFORE, after: $AFTER"
13504         fi
13505
13506         log "Turn on the read cache and turn off the write cache"
13507         set_cache read on
13508         set_cache writethrough off
13509
13510         log "Write data and read it back"
13511         log "It should not be satisfied from the cache."
13512         rm -f $file
13513         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13514         BEFORE=$(roc_hit)
13515         cancel_lru_locks osc
13516         cat $file >/dev/null
13517         AFTER=$(roc_hit)
13518         if ! let "AFTER - BEFORE == 0"; then
13519                 error_ignore bz20762 "IN CACHE (9):before:$BEFORE,after:$AFTER"
13520         else
13521                 log "cache hits:: before: $BEFORE, after: $AFTER"
13522         fi
13523
13524         log "Read again; it should be satisfied from the cache."
13525         BEFORE=$(roc_hit)
13526         cancel_lru_locks osc
13527         cat $file >/dev/null
13528         AFTER=$(roc_hit)
13529         if ! let "AFTER - BEFORE == CPAGES"; then
13530                 error "NOT IN CACHE (1): before: $BEFORE, after: $AFTER"
13531         else
13532                 log "cache hits:: before: $BEFORE, after: $AFTER"
13533         fi
13534
13535         restore_lustre_params < $p
13536         rm -f $p $file
13537 }
13538 run_test 156 "Verification of tunables"
13539
13540 test_160a() {
13541         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13542         remote_mds_nodsh && skip "remote MDS with nodsh"
13543         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13544                 skip "Need MDS version at least 2.2.0"
13545
13546         changelog_register || error "changelog_register failed"
13547         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13548         changelog_users $SINGLEMDS | grep -q $cl_user ||
13549                 error "User $cl_user not found in changelog_users"
13550
13551         # change something
13552         test_mkdir -p $DIR/$tdir/pics/2008/zachy
13553         changelog_clear 0 || error "changelog_clear failed"
13554         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
13555         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
13556         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
13557         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
13558         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
13559         rm $DIR/$tdir/pics/desktop.jpg
13560
13561         changelog_dump | tail -10
13562
13563         echo "verifying changelog mask"
13564         changelog_chmask "-MKDIR"
13565         changelog_chmask "-CLOSE"
13566
13567         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
13568         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
13569
13570         changelog_chmask "+MKDIR"
13571         changelog_chmask "+CLOSE"
13572
13573         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
13574         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
13575
13576         changelog_dump | tail -10
13577         MKDIRS=$(changelog_dump | grep -c "MKDIR")
13578         CLOSES=$(changelog_dump | grep -c "CLOSE")
13579         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
13580         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
13581
13582         # verify contents
13583         echo "verifying target fid"
13584         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
13585         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
13586         [ "$fidc" == "$fidf" ] ||
13587                 error "changelog '$tfile' fid $fidc != file fid $fidf"
13588         echo "verifying parent fid"
13589         # The FID returned from the Changelog may be the directory shard on
13590         # a different MDT, and not the FID returned by path2fid on the parent.
13591         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
13592         # since this is what will matter when recreating this file in the tree.
13593         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
13594         local pathp=$($LFS fid2path $MOUNT "$fidp")
13595         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
13596                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
13597
13598         echo "getting records for $cl_user"
13599         changelog_users $SINGLEMDS
13600         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
13601         local nclr=3
13602         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
13603                 error "changelog_clear failed"
13604         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
13605         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
13606         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
13607                 error "user index expect $user_rec1 + $nclr != $user_rec2"
13608
13609         local min0_rec=$(changelog_users $SINGLEMDS |
13610                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
13611         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
13612                           awk '{ print $1; exit; }')
13613
13614         changelog_dump | tail -n 5
13615         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
13616         [ $first_rec == $((min0_rec + 1)) ] ||
13617                 error "first index should be $min0_rec + 1 not $first_rec"
13618
13619         # LU-3446 changelog index reset on MDT restart
13620         local cur_rec1=$(changelog_users $SINGLEMDS |
13621                          awk '/^current.index:/ { print $NF }')
13622         changelog_clear 0 ||
13623                 error "clear all changelog records for $cl_user failed"
13624         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
13625         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
13626                 error "Fail to start $SINGLEMDS"
13627         local cur_rec2=$(changelog_users $SINGLEMDS |
13628                          awk '/^current.index:/ { print $NF }')
13629         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
13630         [ $cur_rec1 == $cur_rec2 ] ||
13631                 error "current index should be $cur_rec1 not $cur_rec2"
13632
13633         echo "verifying users from this test are deregistered"
13634         changelog_deregister || error "changelog_deregister failed"
13635         changelog_users $SINGLEMDS | grep -q $cl_user &&
13636                 error "User '$cl_user' still in changelog_users"
13637
13638         # lctl get_param -n mdd.*.changelog_users
13639         # current index: 144
13640         # ID    index (idle seconds)
13641         # cl3   144 (2)
13642         if ! changelog_users $SINGLEMDS | grep "^cl"; then
13643                 # this is the normal case where all users were deregistered
13644                 # make sure no new records are added when no users are present
13645                 local last_rec1=$(changelog_users $SINGLEMDS |
13646                                   awk '/^current.index:/ { print $NF }')
13647                 touch $DIR/$tdir/chloe
13648                 local last_rec2=$(changelog_users $SINGLEMDS |
13649                                   awk '/^current.index:/ { print $NF }')
13650                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
13651                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
13652         else
13653                 # any changelog users must be leftovers from a previous test
13654                 changelog_users $SINGLEMDS
13655                 echo "other changelog users; can't verify off"
13656         fi
13657 }
13658 run_test 160a "changelog sanity"
13659
13660 test_160b() { # LU-3587
13661         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13662         remote_mds_nodsh && skip "remote MDS with nodsh"
13663         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13664                 skip "Need MDS version at least 2.2.0"
13665
13666         changelog_register || error "changelog_register failed"
13667         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13668         changelog_users $SINGLEMDS | grep -q $cl_user ||
13669                 error "User '$cl_user' not found in changelog_users"
13670
13671         local longname1=$(str_repeat a 255)
13672         local longname2=$(str_repeat b 255)
13673
13674         cd $DIR
13675         echo "creating very long named file"
13676         touch $longname1 || error "create of '$longname1' failed"
13677         echo "renaming very long named file"
13678         mv $longname1 $longname2
13679
13680         changelog_dump | grep RENME | tail -n 5
13681         rm -f $longname2
13682 }
13683 run_test 160b "Verify that very long rename doesn't crash in changelog"
13684
13685 test_160c() {
13686         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13687         remote_mds_nodsh && skip "remote MDS with nodsh"
13688
13689         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
13690                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
13691                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
13692                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
13693
13694         local rc=0
13695
13696         # Registration step
13697         changelog_register || error "changelog_register failed"
13698
13699         rm -rf $DIR/$tdir
13700         mkdir -p $DIR/$tdir
13701         $MCREATE $DIR/$tdir/foo_160c
13702         changelog_chmask "-TRUNC"
13703         $TRUNCATE $DIR/$tdir/foo_160c 200
13704         changelog_chmask "+TRUNC"
13705         $TRUNCATE $DIR/$tdir/foo_160c 199
13706         changelog_dump | tail -n 5
13707         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
13708         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
13709 }
13710 run_test 160c "verify that changelog log catch the truncate event"
13711
13712 test_160d() {
13713         remote_mds_nodsh && skip "remote MDS with nodsh"
13714         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13715         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13716         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
13717                 skip "Need MDS version at least 2.7.60"
13718
13719         # Registration step
13720         changelog_register || error "changelog_register failed"
13721
13722         mkdir -p $DIR/$tdir/migrate_dir
13723         changelog_clear 0 || error "changelog_clear failed"
13724
13725         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
13726         changelog_dump | tail -n 5
13727         local migrates=$(changelog_dump | grep -c "MIGRT")
13728         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
13729 }
13730 run_test 160d "verify that changelog log catch the migrate event"
13731
13732 test_160e() {
13733         remote_mds_nodsh && skip "remote MDS with nodsh"
13734
13735         # Create a user
13736         changelog_register || error "changelog_register failed"
13737
13738         # Delete a future user (expect fail)
13739         local MDT0=$(facet_svc $SINGLEMDS)
13740         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
13741         local rc=$?
13742
13743         if [ $rc -eq 0 ]; then
13744                 error "Deleted non-existant user cl77"
13745         elif [ $rc -ne 2 ]; then
13746                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
13747         fi
13748
13749         # Clear to a bad index (1 billion should be safe)
13750         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
13751         rc=$?
13752
13753         if [ $rc -eq 0 ]; then
13754                 error "Successfully cleared to invalid CL index"
13755         elif [ $rc -ne 22 ]; then
13756                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
13757         fi
13758 }
13759 run_test 160e "changelog negative testing (should return errors)"
13760
13761 test_160f() {
13762         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13763         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13764                 skip "Need MDS version at least 2.10.56"
13765
13766         local mdts=$(comma_list $(mdts_nodes))
13767
13768         # Create a user
13769         changelog_register || error "first changelog_register failed"
13770         changelog_register || error "second changelog_register failed"
13771         local cl_users
13772         declare -A cl_user1
13773         declare -A cl_user2
13774         local user_rec1
13775         local user_rec2
13776         local i
13777
13778         # generate some changelog records to accumulate on each MDT
13779         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
13780         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13781                 error "create $DIR/$tdir/$tfile failed"
13782
13783         # check changelogs have been generated
13784         local nbcl=$(changelog_dump | wc -l)
13785         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13786
13787         for param in "changelog_max_idle_time=10" \
13788                      "changelog_gc=1" \
13789                      "changelog_min_gc_interval=2" \
13790                      "changelog_min_free_cat_entries=3"; do
13791                 local MDT0=$(facet_svc $SINGLEMDS)
13792                 local var="${param%=*}"
13793                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13794
13795                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13796                 do_nodes $mdts $LCTL set_param mdd.*.$param
13797         done
13798
13799         # force cl_user2 to be idle (1st part)
13800         sleep 9
13801
13802         # simulate changelog catalog almost full
13803         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
13804         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13805
13806         for i in $(seq $MDSCOUNT); do
13807                 cl_users=(${CL_USERS[mds$i]})
13808                 cl_user1[mds$i]="${cl_users[0]}"
13809                 cl_user2[mds$i]="${cl_users[1]}"
13810
13811                 [ -n "${cl_user1[mds$i]}" ] ||
13812                         error "mds$i: no user registered"
13813                 [ -n "${cl_user2[mds$i]}" ] ||
13814                         error "mds$i: only ${cl_user2[mds$i]} is registered"
13815
13816                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13817                 [ -n "$user_rec1" ] ||
13818                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13819                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13820                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13821                 [ -n "$user_rec2" ] ||
13822                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13823                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13824                      "$user_rec1 + 2 == $user_rec2"
13825                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13826                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13827                               "$user_rec1 + 2, but is $user_rec2"
13828                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13829                 [ -n "$user_rec2" ] ||
13830                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13831                 [ $user_rec1 == $user_rec2 ] ||
13832                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13833                               "$user_rec1, but is $user_rec2"
13834         done
13835
13836         # force cl_user2 to be idle (2nd part) and to reach
13837         # changelog_max_idle_time
13838         sleep 2
13839
13840         # generate one more changelog to trigger fail_loc
13841         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13842                 error "create $DIR/$tdir/${tfile}bis failed"
13843
13844         # ensure gc thread is done
13845         for i in $(mdts_nodes); do
13846                 wait_update $i \
13847                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13848                         error "$i: GC-thread not done"
13849         done
13850
13851         local first_rec
13852         for i in $(seq $MDSCOUNT); do
13853                 # check cl_user1 still registered
13854                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13855                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13856                 # check cl_user2 unregistered
13857                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13858                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13859
13860                 # check changelogs are present and starting at $user_rec1 + 1
13861                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13862                 [ -n "$user_rec1" ] ||
13863                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13864                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13865                             awk '{ print $1; exit; }')
13866
13867                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13868                 [ $((user_rec1 + 1)) == $first_rec ] ||
13869                         error "mds$i: first index should be $user_rec1 + 1, " \
13870                               "but is $first_rec"
13871         done
13872 }
13873 run_test 160f "changelog garbage collect (timestamped users)"
13874
13875 test_160g() {
13876         remote_mds_nodsh && skip "remote MDS with nodsh"
13877         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13878                 skip "Need MDS version at least 2.10.56"
13879
13880         local mdts=$(comma_list $(mdts_nodes))
13881
13882         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
13883         do_nodes $mdts $LCTL set_param fail_loc=0x1314
13884
13885         # Create a user
13886         changelog_register || error "first changelog_register failed"
13887         changelog_register || error "second changelog_register failed"
13888         local cl_users
13889         declare -A cl_user1
13890         declare -A cl_user2
13891         local user_rec1
13892         local user_rec2
13893         local i
13894
13895         # generate some changelog records to accumulate on each MDT
13896         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13897         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13898                 error "create $DIR/$tdir/$tfile failed"
13899
13900         # check changelogs have been generated
13901         local nbcl=$(changelog_dump | wc -l)
13902         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13903
13904         # reduce the max_idle_indexes value to make sure we exceed it
13905         max_ndx=$((nbcl / 2 - 1))
13906
13907         for param in "changelog_max_idle_indexes=$max_ndx" \
13908                      "changelog_gc=1" \
13909                      "changelog_min_gc_interval=2" \
13910                      "changelog_min_free_cat_entries=3"; do
13911                 local MDT0=$(facet_svc $SINGLEMDS)
13912                 local var="${param%=*}"
13913                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13914
13915                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13916                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
13917                         error "unable to set mdd.*.$param"
13918         done
13919
13920         # simulate changelog catalog almost full
13921         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
13922         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13923
13924         for i in $(seq $MDSCOUNT); do
13925                 cl_users=(${CL_USERS[mds$i]})
13926                 cl_user1[mds$i]="${cl_users[0]}"
13927                 cl_user2[mds$i]="${cl_users[1]}"
13928
13929                 [ -n "${cl_user1[mds$i]}" ] ||
13930                         error "mds$i: no user registered"
13931                 [ -n "${cl_user2[mds$i]}" ] ||
13932                         error "mds$i: only ${cl_user1[mds$i]} is registered"
13933
13934                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13935                 [ -n "$user_rec1" ] ||
13936                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13937                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13938                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13939                 [ -n "$user_rec2" ] ||
13940                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13941                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13942                      "$user_rec1 + 2 == $user_rec2"
13943                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13944                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13945                               "$user_rec1 + 2, but is $user_rec2"
13946                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13947                 [ -n "$user_rec2" ] ||
13948                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13949                 [ $user_rec1 == $user_rec2 ] ||
13950                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13951                               "$user_rec1, but is $user_rec2"
13952         done
13953
13954         # ensure we are past the previous changelog_min_gc_interval set above
13955         sleep 2
13956
13957         # generate one more changelog to trigger fail_loc
13958         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13959                 error "create $DIR/$tdir/${tfile}bis failed"
13960
13961         # ensure gc thread is done
13962         for i in $(mdts_nodes); do
13963                 wait_update $i \
13964                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13965                         error "$i: GC-thread not done"
13966         done
13967
13968         local first_rec
13969         for i in $(seq $MDSCOUNT); do
13970                 # check cl_user1 still registered
13971                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13972                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13973                 # check cl_user2 unregistered
13974                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13975                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13976
13977                 # check changelogs are present and starting at $user_rec1 + 1
13978                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13979                 [ -n "$user_rec1" ] ||
13980                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13981                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13982                             awk '{ print $1; exit; }')
13983
13984                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13985                 [ $((user_rec1 + 1)) == $first_rec ] ||
13986                         error "mds$i: first index should be $user_rec1 + 1, " \
13987                               "but is $first_rec"
13988         done
13989 }
13990 run_test 160g "changelog garbage collect (old users)"
13991
13992 test_160h() {
13993         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13994         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13995                 skip "Need MDS version at least 2.10.56"
13996
13997         local mdts=$(comma_list $(mdts_nodes))
13998
13999         # Create a user
14000         changelog_register || error "first changelog_register failed"
14001         changelog_register || error "second changelog_register failed"
14002         local cl_users
14003         declare -A cl_user1
14004         declare -A cl_user2
14005         local user_rec1
14006         local user_rec2
14007         local i
14008
14009         # generate some changelog records to accumulate on each MDT
14010         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
14011         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14012                 error "create $DIR/$tdir/$tfile failed"
14013
14014         # check changelogs have been generated
14015         local nbcl=$(changelog_dump | wc -l)
14016         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14017
14018         for param in "changelog_max_idle_time=10" \
14019                      "changelog_gc=1" \
14020                      "changelog_min_gc_interval=2"; do
14021                 local MDT0=$(facet_svc $SINGLEMDS)
14022                 local var="${param%=*}"
14023                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14024
14025                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14026                 do_nodes $mdts $LCTL set_param mdd.*.$param
14027         done
14028
14029         # force cl_user2 to be idle (1st part)
14030         sleep 9
14031
14032         for i in $(seq $MDSCOUNT); do
14033                 cl_users=(${CL_USERS[mds$i]})
14034                 cl_user1[mds$i]="${cl_users[0]}"
14035                 cl_user2[mds$i]="${cl_users[1]}"
14036
14037                 [ -n "${cl_user1[mds$i]}" ] ||
14038                         error "mds$i: no user registered"
14039                 [ -n "${cl_user2[mds$i]}" ] ||
14040                         error "mds$i: only ${cl_user2[mds$i]} is registered"
14041
14042                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14043                 [ -n "$user_rec1" ] ||
14044                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14045                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14046                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14047                 [ -n "$user_rec2" ] ||
14048                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14049                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14050                      "$user_rec1 + 2 == $user_rec2"
14051                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14052                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14053                               "$user_rec1 + 2, but is $user_rec2"
14054                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14055                 [ -n "$user_rec2" ] ||
14056                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14057                 [ $user_rec1 == $user_rec2 ] ||
14058                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14059                               "$user_rec1, but is $user_rec2"
14060         done
14061
14062         # force cl_user2 to be idle (2nd part) and to reach
14063         # changelog_max_idle_time
14064         sleep 2
14065
14066         # force each GC-thread start and block then
14067         # one per MDT/MDD, set fail_val accordingly
14068         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
14069         do_nodes $mdts $LCTL set_param fail_loc=0x1316
14070
14071         # generate more changelogs to trigger fail_loc
14072         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14073                 error "create $DIR/$tdir/${tfile}bis failed"
14074
14075         # stop MDT to stop GC-thread, should be done in back-ground as it will
14076         # block waiting for the thread to be released and exit
14077         declare -A stop_pids
14078         for i in $(seq $MDSCOUNT); do
14079                 stop mds$i &
14080                 stop_pids[mds$i]=$!
14081         done
14082
14083         for i in $(mdts_nodes); do
14084                 local facet
14085                 local nb=0
14086                 local facets=$(facets_up_on_host $i)
14087
14088                 for facet in ${facets//,/ }; do
14089                         if [[ $facet == mds* ]]; then
14090                                 nb=$((nb + 1))
14091                         fi
14092                 done
14093                 # ensure each MDS's gc threads are still present and all in "R"
14094                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
14095                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
14096                         error "$i: expected $nb GC-thread"
14097                 wait_update $i \
14098                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
14099                         "R" 20 ||
14100                         error "$i: GC-thread not found in R-state"
14101                 # check umounts of each MDT on MDS have reached kthread_stop()
14102                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
14103                         error "$i: expected $nb umount"
14104                 wait_update $i \
14105                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
14106                         error "$i: umount not found in D-state"
14107         done
14108
14109         # release all GC-threads
14110         do_nodes $mdts $LCTL set_param fail_loc=0
14111
14112         # wait for MDT stop to complete
14113         for i in $(seq $MDSCOUNT); do
14114                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
14115         done
14116
14117         # XXX
14118         # may try to check if any orphan changelog records are present
14119         # via ldiskfs/zfs and llog_reader...
14120
14121         # re-start/mount MDTs
14122         for i in $(seq $MDSCOUNT); do
14123                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
14124                         error "Fail to start mds$i"
14125         done
14126
14127         local first_rec
14128         for i in $(seq $MDSCOUNT); do
14129                 # check cl_user1 still registered
14130                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14131                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14132                 # check cl_user2 unregistered
14133                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14134                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14135
14136                 # check changelogs are present and starting at $user_rec1 + 1
14137                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14138                 [ -n "$user_rec1" ] ||
14139                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14140                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14141                             awk '{ print $1; exit; }')
14142
14143                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14144                 [ $((user_rec1 + 1)) == $first_rec ] ||
14145                         error "mds$i: first index should be $user_rec1 + 1, " \
14146                               "but is $first_rec"
14147         done
14148 }
14149 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
14150               "during mount"
14151
14152 test_160i() {
14153
14154         local mdts=$(comma_list $(mdts_nodes))
14155
14156         changelog_register || error "first changelog_register failed"
14157
14158         # generate some changelog records to accumulate on each MDT
14159         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
14160         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14161                 error "create $DIR/$tdir/$tfile failed"
14162
14163         # check changelogs have been generated
14164         local nbcl=$(changelog_dump | wc -l)
14165         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14166
14167         # simulate race between register and unregister
14168         # XXX as fail_loc is set per-MDS, with DNE configs the race
14169         # simulation will only occur for one MDT per MDS and for the
14170         # others the normal race scenario will take place
14171         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
14172         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
14173         do_nodes $mdts $LCTL set_param fail_val=1
14174
14175         # unregister 1st user
14176         changelog_deregister &
14177         local pid1=$!
14178         # wait some time for deregister work to reach race rdv
14179         sleep 2
14180         # register 2nd user
14181         changelog_register || error "2nd user register failed"
14182
14183         wait $pid1 || error "1st user deregister failed"
14184
14185         local i
14186         local last_rec
14187         declare -A LAST_REC
14188         for i in $(seq $MDSCOUNT); do
14189                 if changelog_users mds$i | grep "^cl"; then
14190                         # make sure new records are added with one user present
14191                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
14192                                           awk '/^current.index:/ { print $NF }')
14193                 else
14194                         error "mds$i has no user registered"
14195                 fi
14196         done
14197
14198         # generate more changelog records to accumulate on each MDT
14199         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14200                 error "create $DIR/$tdir/${tfile}bis failed"
14201
14202         for i in $(seq $MDSCOUNT); do
14203                 last_rec=$(changelog_users $SINGLEMDS |
14204                            awk '/^current.index:/ { print $NF }')
14205                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
14206                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
14207                         error "changelogs are off on mds$i"
14208         done
14209 }
14210 run_test 160i "changelog user register/unregister race"
14211
14212 test_160j() {
14213         remote_mds_nodsh && skip "remote MDS with nodsh"
14214         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
14215                 skip "Need MDS version at least 2.12.56"
14216
14217         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
14218
14219         changelog_register || error "first changelog_register failed"
14220
14221         # generate some changelog
14222         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
14223         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14224                 error "create $DIR/$tdir/${tfile}bis failed"
14225
14226         # open the changelog device
14227         exec 3>/dev/changelog-$FSNAME-MDT0000
14228         exec 4</dev/changelog-$FSNAME-MDT0000
14229
14230         # umount the first lustre mount
14231         umount $MOUNT
14232
14233         # read changelog
14234         cat <&4 >/dev/null || error "read changelog failed"
14235
14236         # clear changelog
14237         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14238         changelog_users $SINGLEMDS | grep -q $cl_user ||
14239                 error "User $cl_user not found in changelog_users"
14240
14241         printf 'clear:'$cl_user':0' >&3
14242
14243         # close
14244         exec 3>&-
14245         exec 4<&-
14246
14247         # cleanup
14248         changelog_deregister || error "changelog_deregister failed"
14249
14250         umount $MOUNT2
14251         mount_client $MOUNT || error "mount_client on $MOUNT failed"
14252 }
14253 run_test 160j "client can be umounted  while its chanangelog is being used"
14254
14255 test_160k() {
14256         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14257         remote_mds_nodsh && skip "remote MDS with nodsh"
14258
14259         mkdir -p $DIR/$tdir/1/1
14260
14261         changelog_register || error "changelog_register failed"
14262         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14263
14264         changelog_users $SINGLEMDS | grep -q $cl_user ||
14265                 error "User '$cl_user' not found in changelog_users"
14266 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
14267         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
14268         rmdir $DIR/$tdir/1/1 & sleep 1
14269         mkdir $DIR/$tdir/2
14270         touch $DIR/$tdir/2/2
14271         rm -rf $DIR/$tdir/2
14272
14273         wait
14274         sleep 4
14275
14276         changelog_dump | grep rmdir || error "rmdir not recorded"
14277
14278         rm -rf $DIR/$tdir
14279         changelog_deregister
14280 }
14281 run_test 160k "Verify that changelog records are not lost"
14282
14283 test_161a() {
14284         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14285
14286         test_mkdir -c1 $DIR/$tdir
14287         cp /etc/hosts $DIR/$tdir/$tfile
14288         test_mkdir -c1 $DIR/$tdir/foo1
14289         test_mkdir -c1 $DIR/$tdir/foo2
14290         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
14291         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
14292         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
14293         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
14294         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
14295         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14296                 $LFS fid2path $DIR $FID
14297                 error "bad link ea"
14298         fi
14299         # middle
14300         rm $DIR/$tdir/foo2/zachary
14301         # last
14302         rm $DIR/$tdir/foo2/thor
14303         # first
14304         rm $DIR/$tdir/$tfile
14305         # rename
14306         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
14307         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
14308                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
14309         rm $DIR/$tdir/foo2/maggie
14310
14311         # overflow the EA
14312         local longname=$tfile.avg_len_is_thirty_two_
14313         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
14314                 error_noexit 'failed to unlink many hardlinks'" EXIT
14315         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
14316                 error "failed to hardlink many files"
14317         links=$($LFS fid2path $DIR $FID | wc -l)
14318         echo -n "${links}/1000 links in link EA"
14319         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
14320 }
14321 run_test 161a "link ea sanity"
14322
14323 test_161b() {
14324         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14325         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
14326
14327         local MDTIDX=1
14328         local remote_dir=$DIR/$tdir/remote_dir
14329
14330         mkdir -p $DIR/$tdir
14331         $LFS mkdir -i $MDTIDX $remote_dir ||
14332                 error "create remote directory failed"
14333
14334         cp /etc/hosts $remote_dir/$tfile
14335         mkdir -p $remote_dir/foo1
14336         mkdir -p $remote_dir/foo2
14337         ln $remote_dir/$tfile $remote_dir/foo1/sofia
14338         ln $remote_dir/$tfile $remote_dir/foo2/zachary
14339         ln $remote_dir/$tfile $remote_dir/foo1/luna
14340         ln $remote_dir/$tfile $remote_dir/foo2/thor
14341
14342         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
14343                      tr -d ']')
14344         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14345                 $LFS fid2path $DIR $FID
14346                 error "bad link ea"
14347         fi
14348         # middle
14349         rm $remote_dir/foo2/zachary
14350         # last
14351         rm $remote_dir/foo2/thor
14352         # first
14353         rm $remote_dir/$tfile
14354         # rename
14355         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
14356         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
14357         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
14358                 $LFS fid2path $DIR $FID
14359                 error "bad link rename"
14360         fi
14361         rm $remote_dir/foo2/maggie
14362
14363         # overflow the EA
14364         local longname=filename_avg_len_is_thirty_two_
14365         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
14366                 error "failed to hardlink many files"
14367         links=$($LFS fid2path $DIR $FID | wc -l)
14368         echo -n "${links}/1000 links in link EA"
14369         [[ ${links} -gt 60 ]] ||
14370                 error "expected at least 60 links in link EA"
14371         unlinkmany $remote_dir/foo2/$longname 1000 ||
14372         error "failed to unlink many hardlinks"
14373 }
14374 run_test 161b "link ea sanity under remote directory"
14375
14376 test_161c() {
14377         remote_mds_nodsh && skip "remote MDS with nodsh"
14378         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14379         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
14380                 skip "Need MDS version at least 2.1.5"
14381
14382         # define CLF_RENAME_LAST 0x0001
14383         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
14384         changelog_register || error "changelog_register failed"
14385
14386         rm -rf $DIR/$tdir
14387         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
14388         touch $DIR/$tdir/foo_161c
14389         touch $DIR/$tdir/bar_161c
14390         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14391         changelog_dump | grep RENME | tail -n 5
14392         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14393         changelog_clear 0 || error "changelog_clear failed"
14394         if [ x$flags != "x0x1" ]; then
14395                 error "flag $flags is not 0x1"
14396         fi
14397
14398         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
14399         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
14400         touch $DIR/$tdir/foo_161c
14401         touch $DIR/$tdir/bar_161c
14402         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14403         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14404         changelog_dump | grep RENME | tail -n 5
14405         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14406         changelog_clear 0 || error "changelog_clear failed"
14407         if [ x$flags != "x0x0" ]; then
14408                 error "flag $flags is not 0x0"
14409         fi
14410         echo "rename overwrite a target having nlink > 1," \
14411                 "changelog record has flags of $flags"
14412
14413         # rename doesn't overwrite a target (changelog flag 0x0)
14414         touch $DIR/$tdir/foo_161c
14415         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
14416         changelog_dump | grep RENME | tail -n 5
14417         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
14418         changelog_clear 0 || error "changelog_clear failed"
14419         if [ x$flags != "x0x0" ]; then
14420                 error "flag $flags is not 0x0"
14421         fi
14422         echo "rename doesn't overwrite a target," \
14423                 "changelog record has flags of $flags"
14424
14425         # define CLF_UNLINK_LAST 0x0001
14426         # unlink a file having nlink = 1 (changelog flag 0x1)
14427         rm -f $DIR/$tdir/foo2_161c
14428         changelog_dump | grep UNLNK | tail -n 5
14429         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14430         changelog_clear 0 || error "changelog_clear failed"
14431         if [ x$flags != "x0x1" ]; then
14432                 error "flag $flags is not 0x1"
14433         fi
14434         echo "unlink a file having nlink = 1," \
14435                 "changelog record has flags of $flags"
14436
14437         # unlink a file having nlink > 1 (changelog flag 0x0)
14438         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14439         rm -f $DIR/$tdir/foobar_161c
14440         changelog_dump | grep UNLNK | tail -n 5
14441         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14442         changelog_clear 0 || error "changelog_clear failed"
14443         if [ x$flags != "x0x0" ]; then
14444                 error "flag $flags is not 0x0"
14445         fi
14446         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
14447 }
14448 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
14449
14450 test_161d() {
14451         remote_mds_nodsh && skip "remote MDS with nodsh"
14452         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
14453
14454         local pid
14455         local fid
14456
14457         changelog_register || error "changelog_register failed"
14458
14459         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
14460         # interfer with $MOUNT/.lustre/fid/ access
14461         mkdir $DIR/$tdir
14462         [[ $? -eq 0 ]] || error "mkdir failed"
14463
14464         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
14465         $LCTL set_param fail_loc=0x8000140c
14466         # 5s pause
14467         $LCTL set_param fail_val=5
14468
14469         # create file
14470         echo foofoo > $DIR/$tdir/$tfile &
14471         pid=$!
14472
14473         # wait for create to be delayed
14474         sleep 2
14475
14476         ps -p $pid
14477         [[ $? -eq 0 ]] || error "create should be blocked"
14478
14479         local tempfile="$(mktemp --tmpdir $tfile.XXXXXX)"
14480         stack_trap "rm -f $tempfile"
14481         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
14482         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
14483         # some delay may occur during ChangeLog publishing and file read just
14484         # above, that could allow file write to happen finally
14485         [[ -s $tempfile ]] && echo "file should be empty"
14486
14487         $LCTL set_param fail_loc=0
14488
14489         wait $pid
14490         [[ $? -eq 0 ]] || error "create failed"
14491 }
14492 run_test 161d "create with concurrent .lustre/fid access"
14493
14494 check_path() {
14495         local expected="$1"
14496         shift
14497         local fid="$2"
14498
14499         local path
14500         path=$($LFS fid2path "$@")
14501         local rc=$?
14502
14503         if [ $rc -ne 0 ]; then
14504                 error "path looked up of '$expected' failed: rc=$rc"
14505         elif [ "$path" != "$expected" ]; then
14506                 error "path looked up '$path' instead of '$expected'"
14507         else
14508                 echo "FID '$fid' resolves to path '$path' as expected"
14509         fi
14510 }
14511
14512 test_162a() { # was test_162
14513         test_mkdir -p -c1 $DIR/$tdir/d2
14514         touch $DIR/$tdir/d2/$tfile
14515         touch $DIR/$tdir/d2/x1
14516         touch $DIR/$tdir/d2/x2
14517         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
14518         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
14519         # regular file
14520         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
14521         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
14522
14523         # softlink
14524         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
14525         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
14526         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
14527
14528         # softlink to wrong file
14529         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
14530         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
14531         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
14532
14533         # hardlink
14534         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
14535         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
14536         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
14537         # fid2path dir/fsname should both work
14538         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
14539         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
14540
14541         # hardlink count: check that there are 2 links
14542         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
14543         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
14544
14545         # hardlink indexing: remove the first link
14546         rm $DIR/$tdir/d2/p/q/r/hlink
14547         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
14548 }
14549 run_test 162a "path lookup sanity"
14550
14551 test_162b() {
14552         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14553         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14554
14555         mkdir $DIR/$tdir
14556         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
14557                                 error "create striped dir failed"
14558
14559         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
14560                                         tail -n 1 | awk '{print $2}')
14561         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
14562
14563         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
14564         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
14565
14566         # regular file
14567         for ((i=0;i<5;i++)); do
14568                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
14569                         error "get fid for f$i failed"
14570                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
14571
14572                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
14573                         error "get fid for d$i failed"
14574                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
14575         done
14576
14577         return 0
14578 }
14579 run_test 162b "striped directory path lookup sanity"
14580
14581 # LU-4239: Verify fid2path works with paths 100 or more directories deep
14582 test_162c() {
14583         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
14584                 skip "Need MDS version at least 2.7.51"
14585
14586         local lpath=$tdir.local
14587         local rpath=$tdir.remote
14588
14589         test_mkdir $DIR/$lpath
14590         test_mkdir $DIR/$rpath
14591
14592         for ((i = 0; i <= 101; i++)); do
14593                 lpath="$lpath/$i"
14594                 mkdir $DIR/$lpath
14595                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
14596                         error "get fid for local directory $DIR/$lpath failed"
14597                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
14598
14599                 rpath="$rpath/$i"
14600                 test_mkdir $DIR/$rpath
14601                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
14602                         error "get fid for remote directory $DIR/$rpath failed"
14603                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
14604         done
14605
14606         return 0
14607 }
14608 run_test 162c "fid2path works with paths 100 or more directories deep"
14609
14610 test_169() {
14611         # do directio so as not to populate the page cache
14612         log "creating a 10 Mb file"
14613         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
14614         log "starting reads"
14615         dd if=$DIR/$tfile of=/dev/null bs=4096 &
14616         log "truncating the file"
14617         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
14618         log "killing dd"
14619         kill %+ || true # reads might have finished
14620         echo "wait until dd is finished"
14621         wait
14622         log "removing the temporary file"
14623         rm -rf $DIR/$tfile || error "tmp file removal failed"
14624 }
14625 run_test 169 "parallel read and truncate should not deadlock"
14626
14627 test_170() {
14628         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14629
14630         $LCTL clear     # bug 18514
14631         $LCTL debug_daemon start $TMP/${tfile}_log_good
14632         touch $DIR/$tfile
14633         $LCTL debug_daemon stop
14634         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
14635                 error "sed failed to read log_good"
14636
14637         $LCTL debug_daemon start $TMP/${tfile}_log_good
14638         rm -rf $DIR/$tfile
14639         $LCTL debug_daemon stop
14640
14641         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
14642                error "lctl df log_bad failed"
14643
14644         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14645         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14646
14647         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
14648         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
14649
14650         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
14651                 error "bad_line good_line1 good_line2 are empty"
14652
14653         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14654         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
14655         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14656
14657         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
14658         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14659         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14660
14661         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
14662                 error "bad_line_new good_line_new are empty"
14663
14664         local expected_good=$((good_line1 + good_line2*2))
14665
14666         rm -f $TMP/${tfile}*
14667         # LU-231, short malformed line may not be counted into bad lines
14668         if [ $bad_line -ne $bad_line_new ] &&
14669                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
14670                 error "expected $bad_line bad lines, but got $bad_line_new"
14671                 return 1
14672         fi
14673
14674         if [ $expected_good -ne $good_line_new ]; then
14675                 error "expected $expected_good good lines, but got $good_line_new"
14676                 return 2
14677         fi
14678         true
14679 }
14680 run_test 170 "test lctl df to handle corrupted log ====================="
14681
14682 test_171() { # bug20592
14683         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14684
14685         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
14686         $LCTL set_param fail_loc=0x50e
14687         $LCTL set_param fail_val=3000
14688         multiop_bg_pause $DIR/$tfile O_s || true
14689         local MULTIPID=$!
14690         kill -USR1 $MULTIPID
14691         # cause log dump
14692         sleep 3
14693         wait $MULTIPID
14694         if dmesg | grep "recursive fault"; then
14695                 error "caught a recursive fault"
14696         fi
14697         $LCTL set_param fail_loc=0
14698         true
14699 }
14700 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
14701
14702 # it would be good to share it with obdfilter-survey/iokit-libecho code
14703 setup_obdecho_osc () {
14704         local rc=0
14705         local ost_nid=$1
14706         local obdfilter_name=$2
14707         echo "Creating new osc for $obdfilter_name on $ost_nid"
14708         # make sure we can find loopback nid
14709         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
14710
14711         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
14712                            ${obdfilter_name}_osc_UUID || rc=2; }
14713         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
14714                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
14715         return $rc
14716 }
14717
14718 cleanup_obdecho_osc () {
14719         local obdfilter_name=$1
14720         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
14721         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
14722         return 0
14723 }
14724
14725 obdecho_test() {
14726         local OBD=$1
14727         local node=$2
14728         local pages=${3:-64}
14729         local rc=0
14730         local id
14731
14732         local count=10
14733         local obd_size=$(get_obd_size $node $OBD)
14734         local page_size=$(get_page_size $node)
14735         if [[ -n "$obd_size" ]]; then
14736                 local new_count=$((obd_size / (pages * page_size / 1024)))
14737                 [[ $new_count -ge $count ]] || count=$new_count
14738         fi
14739
14740         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
14741         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
14742                            rc=2; }
14743         if [ $rc -eq 0 ]; then
14744             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
14745             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
14746         fi
14747         echo "New object id is $id"
14748         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
14749                            rc=4; }
14750         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
14751                            "test_brw $count w v $pages $id" || rc=4; }
14752         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
14753                            rc=4; }
14754         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
14755                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
14756         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
14757                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
14758         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
14759         return $rc
14760 }
14761
14762 test_180a() {
14763         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14764
14765         if ! module_loaded obdecho; then
14766                 load_module obdecho/obdecho &&
14767                         stack_trap "rmmod obdecho" EXIT ||
14768                         error "unable to load obdecho on client"
14769         fi
14770
14771         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
14772         local host=$($LCTL get_param -n osc.$osc.import |
14773                      awk '/current_connection:/ { print $2 }' )
14774         local target=$($LCTL get_param -n osc.$osc.import |
14775                        awk '/target:/ { print $2 }' )
14776         target=${target%_UUID}
14777
14778         if [ -n "$target" ]; then
14779                 setup_obdecho_osc $host $target &&
14780                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
14781                         { error "obdecho setup failed with $?"; return; }
14782
14783                 obdecho_test ${target}_osc client ||
14784                         error "obdecho_test failed on ${target}_osc"
14785         else
14786                 $LCTL get_param osc.$osc.import
14787                 error "there is no osc.$osc.import target"
14788         fi
14789 }
14790 run_test 180a "test obdecho on osc"
14791
14792 test_180b() {
14793         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14794         remote_ost_nodsh && skip "remote OST with nodsh"
14795
14796         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14797                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14798                 error "failed to load module obdecho"
14799
14800         local target=$(do_facet ost1 $LCTL dl |
14801                        awk '/obdfilter/ { print $4; exit; }')
14802
14803         if [ -n "$target" ]; then
14804                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
14805         else
14806                 do_facet ost1 $LCTL dl
14807                 error "there is no obdfilter target on ost1"
14808         fi
14809 }
14810 run_test 180b "test obdecho directly on obdfilter"
14811
14812 test_180c() { # LU-2598
14813         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14814         remote_ost_nodsh && skip "remote OST with nodsh"
14815         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
14816                 skip "Need MDS version at least 2.4.0"
14817
14818         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14819                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14820                 error "failed to load module obdecho"
14821
14822         local target=$(do_facet ost1 $LCTL dl |
14823                        awk '/obdfilter/ { print $4; exit; }')
14824
14825         if [ -n "$target" ]; then
14826                 local pages=16384 # 64MB bulk I/O RPC size
14827
14828                 obdecho_test "$target" ost1 "$pages" ||
14829                         error "obdecho_test with pages=$pages failed with $?"
14830         else
14831                 do_facet ost1 $LCTL dl
14832                 error "there is no obdfilter target on ost1"
14833         fi
14834 }
14835 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
14836
14837 test_181() { # bug 22177
14838         test_mkdir $DIR/$tdir
14839         # create enough files to index the directory
14840         createmany -o $DIR/$tdir/foobar 4000
14841         # print attributes for debug purpose
14842         lsattr -d .
14843         # open dir
14844         multiop_bg_pause $DIR/$tdir D_Sc || return 1
14845         MULTIPID=$!
14846         # remove the files & current working dir
14847         unlinkmany $DIR/$tdir/foobar 4000
14848         rmdir $DIR/$tdir
14849         kill -USR1 $MULTIPID
14850         wait $MULTIPID
14851         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
14852         return 0
14853 }
14854 run_test 181 "Test open-unlinked dir ========================"
14855
14856 test_182() {
14857         local fcount=1000
14858         local tcount=10
14859
14860         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14861
14862         $LCTL set_param mdc.*.rpc_stats=clear
14863
14864         for (( i = 0; i < $tcount; i++ )) ; do
14865                 mkdir $DIR/$tdir/$i
14866         done
14867
14868         for (( i = 0; i < $tcount; i++ )) ; do
14869                 createmany -o $DIR/$tdir/$i/f- $fcount &
14870         done
14871         wait
14872
14873         for (( i = 0; i < $tcount; i++ )) ; do
14874                 unlinkmany $DIR/$tdir/$i/f- $fcount &
14875         done
14876         wait
14877
14878         $LCTL get_param mdc.*.rpc_stats
14879
14880         rm -rf $DIR/$tdir
14881 }
14882 run_test 182 "Test parallel modify metadata operations ================"
14883
14884 test_183() { # LU-2275
14885         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14886         remote_mds_nodsh && skip "remote MDS with nodsh"
14887         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
14888                 skip "Need MDS version at least 2.3.56"
14889
14890         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14891         echo aaa > $DIR/$tdir/$tfile
14892
14893 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
14894         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
14895
14896         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
14897         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
14898
14899         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
14900
14901         # Flush negative dentry cache
14902         touch $DIR/$tdir/$tfile
14903
14904         # We are not checking for any leaked references here, they'll
14905         # become evident next time we do cleanup with module unload.
14906         rm -rf $DIR/$tdir
14907 }
14908 run_test 183 "No crash or request leak in case of strange dispositions ========"
14909
14910 # test suite 184 is for LU-2016, LU-2017
14911 test_184a() {
14912         check_swap_layouts_support
14913
14914         dir0=$DIR/$tdir/$testnum
14915         test_mkdir -p -c1 $dir0
14916         ref1=/etc/passwd
14917         ref2=/etc/group
14918         file1=$dir0/f1
14919         file2=$dir0/f2
14920         $LFS setstripe -c1 $file1
14921         cp $ref1 $file1
14922         $LFS setstripe -c2 $file2
14923         cp $ref2 $file2
14924         gen1=$($LFS getstripe -g $file1)
14925         gen2=$($LFS getstripe -g $file2)
14926
14927         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
14928         gen=$($LFS getstripe -g $file1)
14929         [[ $gen1 != $gen ]] ||
14930                 "Layout generation on $file1 does not change"
14931         gen=$($LFS getstripe -g $file2)
14932         [[ $gen2 != $gen ]] ||
14933                 "Layout generation on $file2 does not change"
14934
14935         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
14936         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
14937
14938         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
14939 }
14940 run_test 184a "Basic layout swap"
14941
14942 test_184b() {
14943         check_swap_layouts_support
14944
14945         dir0=$DIR/$tdir/$testnum
14946         mkdir -p $dir0 || error "creating dir $dir0"
14947         file1=$dir0/f1
14948         file2=$dir0/f2
14949         file3=$dir0/f3
14950         dir1=$dir0/d1
14951         dir2=$dir0/d2
14952         mkdir $dir1 $dir2
14953         $LFS setstripe -c1 $file1
14954         $LFS setstripe -c2 $file2
14955         $LFS setstripe -c1 $file3
14956         chown $RUNAS_ID $file3
14957         gen1=$($LFS getstripe -g $file1)
14958         gen2=$($LFS getstripe -g $file2)
14959
14960         $LFS swap_layouts $dir1 $dir2 &&
14961                 error "swap of directories layouts should fail"
14962         $LFS swap_layouts $dir1 $file1 &&
14963                 error "swap of directory and file layouts should fail"
14964         $RUNAS $LFS swap_layouts $file1 $file2 &&
14965                 error "swap of file we cannot write should fail"
14966         $LFS swap_layouts $file1 $file3 &&
14967                 error "swap of file with different owner should fail"
14968         /bin/true # to clear error code
14969 }
14970 run_test 184b "Forbidden layout swap (will generate errors)"
14971
14972 test_184c() {
14973         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
14974         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
14975         check_swap_layouts_support
14976
14977         local dir0=$DIR/$tdir/$testnum
14978         mkdir -p $dir0 || error "creating dir $dir0"
14979
14980         local ref1=$dir0/ref1
14981         local ref2=$dir0/ref2
14982         local file1=$dir0/file1
14983         local file2=$dir0/file2
14984         # create a file large enough for the concurrent test
14985         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
14986         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
14987         echo "ref file size: ref1($(stat -c %s $ref1))," \
14988              "ref2($(stat -c %s $ref2))"
14989
14990         cp $ref2 $file2
14991         dd if=$ref1 of=$file1 bs=16k &
14992         local DD_PID=$!
14993
14994         # Make sure dd starts to copy file
14995         while [ ! -f $file1 ]; do sleep 0.1; done
14996
14997         $LFS swap_layouts $file1 $file2
14998         local rc=$?
14999         wait $DD_PID
15000         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
15001         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
15002
15003         # how many bytes copied before swapping layout
15004         local copied=$(stat -c %s $file2)
15005         local remaining=$(stat -c %s $ref1)
15006         remaining=$((remaining - copied))
15007         echo "Copied $copied bytes before swapping layout..."
15008
15009         cmp -n $copied $file1 $ref2 | grep differ &&
15010                 error "Content mismatch [0, $copied) of ref2 and file1"
15011         cmp -n $copied $file2 $ref1 ||
15012                 error "Content mismatch [0, $copied) of ref1 and file2"
15013         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
15014                 error "Content mismatch [$copied, EOF) of ref1 and file1"
15015
15016         # clean up
15017         rm -f $ref1 $ref2 $file1 $file2
15018 }
15019 run_test 184c "Concurrent write and layout swap"
15020
15021 test_184d() {
15022         check_swap_layouts_support
15023         [ -z "$(which getfattr 2>/dev/null)" ] &&
15024                 skip_env "no getfattr command"
15025
15026         local file1=$DIR/$tdir/$tfile-1
15027         local file2=$DIR/$tdir/$tfile-2
15028         local file3=$DIR/$tdir/$tfile-3
15029         local lovea1
15030         local lovea2
15031
15032         mkdir -p $DIR/$tdir
15033         touch $file1 || error "create $file1 failed"
15034         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15035                 error "create $file2 failed"
15036         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15037                 error "create $file3 failed"
15038         lovea1=$(get_layout_param $file1)
15039
15040         $LFS swap_layouts $file2 $file3 ||
15041                 error "swap $file2 $file3 layouts failed"
15042         $LFS swap_layouts $file1 $file2 ||
15043                 error "swap $file1 $file2 layouts failed"
15044
15045         lovea2=$(get_layout_param $file2)
15046         echo "$lovea1"
15047         echo "$lovea2"
15048         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
15049
15050         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15051         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
15052 }
15053 run_test 184d "allow stripeless layouts swap"
15054
15055 test_184e() {
15056         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
15057                 skip "Need MDS version at least 2.6.94"
15058         check_swap_layouts_support
15059         [ -z "$(which getfattr 2>/dev/null)" ] &&
15060                 skip_env "no getfattr command"
15061
15062         local file1=$DIR/$tdir/$tfile-1
15063         local file2=$DIR/$tdir/$tfile-2
15064         local file3=$DIR/$tdir/$tfile-3
15065         local lovea
15066
15067         mkdir -p $DIR/$tdir
15068         touch $file1 || error "create $file1 failed"
15069         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15070                 error "create $file2 failed"
15071         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15072                 error "create $file3 failed"
15073
15074         $LFS swap_layouts $file1 $file2 ||
15075                 error "swap $file1 $file2 layouts failed"
15076
15077         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15078         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
15079
15080         echo 123 > $file1 || error "Should be able to write into $file1"
15081
15082         $LFS swap_layouts $file1 $file3 ||
15083                 error "swap $file1 $file3 layouts failed"
15084
15085         echo 123 > $file1 || error "Should be able to write into $file1"
15086
15087         rm -rf $file1 $file2 $file3
15088 }
15089 run_test 184e "Recreate layout after stripeless layout swaps"
15090
15091 test_184f() {
15092         # Create a file with name longer than sizeof(struct stat) ==
15093         # 144 to see if we can get chars from the file name to appear
15094         # in the returned striping. Note that 'f' == 0x66.
15095         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
15096
15097         mkdir -p $DIR/$tdir
15098         mcreate $DIR/$tdir/$file
15099         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
15100                 error "IOC_MDC_GETFILEINFO returned garbage striping"
15101         fi
15102 }
15103 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
15104
15105 test_185() { # LU-2441
15106         # LU-3553 - no volatile file support in old servers
15107         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
15108                 skip "Need MDS version at least 2.3.60"
15109
15110         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15111         touch $DIR/$tdir/spoo
15112         local mtime1=$(stat -c "%Y" $DIR/$tdir)
15113         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
15114                 error "cannot create/write a volatile file"
15115         [ "$FILESET" == "" ] &&
15116         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
15117                 error "FID is still valid after close"
15118
15119         multiop_bg_pause $DIR/$tdir vVw4096_c
15120         local multi_pid=$!
15121
15122         local OLD_IFS=$IFS
15123         IFS=":"
15124         local fidv=($fid)
15125         IFS=$OLD_IFS
15126         # assume that the next FID for this client is sequential, since stdout
15127         # is unfortunately eaten by multiop_bg_pause
15128         local n=$((${fidv[1]} + 1))
15129         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
15130         if [ "$FILESET" == "" ]; then
15131                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
15132                         error "FID is missing before close"
15133         fi
15134         kill -USR1 $multi_pid
15135         # 1 second delay, so if mtime change we will see it
15136         sleep 1
15137         local mtime2=$(stat -c "%Y" $DIR/$tdir)
15138         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
15139 }
15140 run_test 185 "Volatile file support"
15141
15142 function create_check_volatile() {
15143         local idx=$1
15144         local tgt
15145
15146         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
15147         local PID=$!
15148         sleep 1
15149         local FID=$(cat /tmp/${tfile}.fid)
15150         [ "$FID" == "" ] && error "can't get FID for volatile"
15151         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
15152         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
15153         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
15154         kill -USR1 $PID
15155         wait
15156         sleep 1
15157         cancel_lru_locks mdc # flush opencache
15158         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
15159         return 0
15160 }
15161
15162 test_185a(){
15163         # LU-12516 - volatile creation via .lustre
15164         [[ $MDS1_VERSION -ge $(version_code 2.12.55) ]] ||
15165                 skip "Need MDS version at least 2.3.55"
15166
15167         create_check_volatile 0
15168         [ $MDSCOUNT -lt 2 ] && return 0
15169
15170         # DNE case
15171         create_check_volatile 1
15172
15173         return 0
15174 }
15175 run_test 185a "Volatile file creation in .lustre/fid/"
15176
15177 test_187a() {
15178         remote_mds_nodsh && skip "remote MDS with nodsh"
15179         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15180                 skip "Need MDS version at least 2.3.0"
15181
15182         local dir0=$DIR/$tdir/$testnum
15183         mkdir -p $dir0 || error "creating dir $dir0"
15184
15185         local file=$dir0/file1
15186         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
15187         local dv1=$($LFS data_version $file)
15188         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
15189         local dv2=$($LFS data_version $file)
15190         [[ $dv1 != $dv2 ]] ||
15191                 error "data version did not change on write $dv1 == $dv2"
15192
15193         # clean up
15194         rm -f $file1
15195 }
15196 run_test 187a "Test data version change"
15197
15198 test_187b() {
15199         remote_mds_nodsh && skip "remote MDS with nodsh"
15200         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15201                 skip "Need MDS version at least 2.3.0"
15202
15203         local dir0=$DIR/$tdir/$testnum
15204         mkdir -p $dir0 || error "creating dir $dir0"
15205
15206         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
15207         [[ ${DV[0]} != ${DV[1]} ]] ||
15208                 error "data version did not change on write"\
15209                       " ${DV[0]} == ${DV[1]}"
15210
15211         # clean up
15212         rm -f $file1
15213 }
15214 run_test 187b "Test data version change on volatile file"
15215
15216 test_200() {
15217         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15218         remote_mgs_nodsh && skip "remote MGS with nodsh"
15219         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15220
15221         local POOL=${POOL:-cea1}
15222         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
15223         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
15224         # Pool OST targets
15225         local first_ost=0
15226         local last_ost=$(($OSTCOUNT - 1))
15227         local ost_step=2
15228         local ost_list=$(seq $first_ost $ost_step $last_ost)
15229         local ost_range="$first_ost $last_ost $ost_step"
15230         local test_path=$POOL_ROOT/$POOL_DIR_NAME
15231         local file_dir=$POOL_ROOT/file_tst
15232         local subdir=$test_path/subdir
15233         local rc=0
15234
15235         while : ; do
15236                 # former test_200a test_200b
15237                 pool_add $POOL                          || { rc=$? ; break; }
15238                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
15239                 # former test_200c test_200d
15240                 mkdir -p $test_path
15241                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
15242                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
15243                 mkdir -p $subdir
15244                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
15245                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
15246                                                         || { rc=$? ; break; }
15247                 # former test_200e test_200f
15248                 local files=$((OSTCOUNT*3))
15249                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
15250                                                         || { rc=$? ; break; }
15251                 pool_create_files $POOL $file_dir $files "$ost_list" \
15252                                                         || { rc=$? ; break; }
15253                 # former test_200g test_200h
15254                 pool_lfs_df $POOL                       || { rc=$? ; break; }
15255                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
15256
15257                 # former test_201a test_201b test_201c
15258                 pool_remove_first_target $POOL          || { rc=$? ; break; }
15259
15260                 local f=$test_path/$tfile
15261                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
15262                 pool_remove $POOL $f                    || { rc=$? ; break; }
15263                 break
15264         done
15265
15266         destroy_test_pools
15267
15268         return $rc
15269 }
15270 run_test 200 "OST pools"
15271
15272 # usage: default_attr <count | size | offset>
15273 default_attr() {
15274         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
15275 }
15276
15277 # usage: check_default_stripe_attr
15278 check_default_stripe_attr() {
15279         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
15280         case $1 in
15281         --stripe-count|-c)
15282                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
15283         --stripe-size|-S)
15284                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
15285         --stripe-index|-i)
15286                 EXPECTED=-1;;
15287         *)
15288                 error "unknown getstripe attr '$1'"
15289         esac
15290
15291         [ $ACTUAL == $EXPECTED ] ||
15292                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
15293 }
15294
15295 test_204a() {
15296         test_mkdir $DIR/$tdir
15297         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
15298
15299         check_default_stripe_attr --stripe-count
15300         check_default_stripe_attr --stripe-size
15301         check_default_stripe_attr --stripe-index
15302 }
15303 run_test 204a "Print default stripe attributes"
15304
15305 test_204b() {
15306         test_mkdir $DIR/$tdir
15307         $LFS setstripe --stripe-count 1 $DIR/$tdir
15308
15309         check_default_stripe_attr --stripe-size
15310         check_default_stripe_attr --stripe-index
15311 }
15312 run_test 204b "Print default stripe size and offset"
15313
15314 test_204c() {
15315         test_mkdir $DIR/$tdir
15316         $LFS setstripe --stripe-size 65536 $DIR/$tdir
15317
15318         check_default_stripe_attr --stripe-count
15319         check_default_stripe_attr --stripe-index
15320 }
15321 run_test 204c "Print default stripe count and offset"
15322
15323 test_204d() {
15324         test_mkdir $DIR/$tdir
15325         $LFS setstripe --stripe-index 0 $DIR/$tdir
15326
15327         check_default_stripe_attr --stripe-count
15328         check_default_stripe_attr --stripe-size
15329 }
15330 run_test 204d "Print default stripe count and size"
15331
15332 test_204e() {
15333         test_mkdir $DIR/$tdir
15334         $LFS setstripe -d $DIR/$tdir
15335
15336         check_default_stripe_attr --stripe-count --raw
15337         check_default_stripe_attr --stripe-size --raw
15338         check_default_stripe_attr --stripe-index --raw
15339 }
15340 run_test 204e "Print raw stripe attributes"
15341
15342 test_204f() {
15343         test_mkdir $DIR/$tdir
15344         $LFS setstripe --stripe-count 1 $DIR/$tdir
15345
15346         check_default_stripe_attr --stripe-size --raw
15347         check_default_stripe_attr --stripe-index --raw
15348 }
15349 run_test 204f "Print raw stripe size and offset"
15350
15351 test_204g() {
15352         test_mkdir $DIR/$tdir
15353         $LFS setstripe --stripe-size 65536 $DIR/$tdir
15354
15355         check_default_stripe_attr --stripe-count --raw
15356         check_default_stripe_attr --stripe-index --raw
15357 }
15358 run_test 204g "Print raw stripe count and offset"
15359
15360 test_204h() {
15361         test_mkdir $DIR/$tdir
15362         $LFS setstripe --stripe-index 0 $DIR/$tdir
15363
15364         check_default_stripe_attr --stripe-count --raw
15365         check_default_stripe_attr --stripe-size --raw
15366 }
15367 run_test 204h "Print raw stripe count and size"
15368
15369 # Figure out which job scheduler is being used, if any,
15370 # or use a fake one
15371 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
15372         JOBENV=SLURM_JOB_ID
15373 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
15374         JOBENV=LSB_JOBID
15375 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
15376         JOBENV=PBS_JOBID
15377 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
15378         JOBENV=LOADL_STEP_ID
15379 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
15380         JOBENV=JOB_ID
15381 else
15382         $LCTL list_param jobid_name > /dev/null 2>&1
15383         if [ $? -eq 0 ]; then
15384                 JOBENV=nodelocal
15385         else
15386                 JOBENV=FAKE_JOBID
15387         fi
15388 fi
15389 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
15390
15391 verify_jobstats() {
15392         local cmd=($1)
15393         shift
15394         local facets="$@"
15395
15396 # we don't really need to clear the stats for this test to work, since each
15397 # command has a unique jobid, but it makes debugging easier if needed.
15398 #       for facet in $facets; do
15399 #               local dev=$(convert_facet2label $facet)
15400 #               # clear old jobstats
15401 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
15402 #       done
15403
15404         # use a new JobID for each test, or we might see an old one
15405         [ "$JOBENV" = "FAKE_JOBID" ] &&
15406                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
15407
15408         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
15409
15410         [ "$JOBENV" = "nodelocal" ] && {
15411                 FAKE_JOBID=id.$testnum.%e.$RANDOM
15412                 $LCTL set_param jobid_name=$FAKE_JOBID
15413                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
15414         }
15415
15416         log "Test: ${cmd[*]}"
15417         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
15418
15419         if [ $JOBENV = "FAKE_JOBID" ]; then
15420                 FAKE_JOBID=$JOBVAL ${cmd[*]}
15421         else
15422                 ${cmd[*]}
15423         fi
15424
15425         # all files are created on OST0000
15426         for facet in $facets; do
15427                 local stats="*.$(convert_facet2label $facet).job_stats"
15428
15429                 # strip out libtool wrappers for in-tree executables
15430                 if [ $(do_facet $facet lctl get_param $stats |
15431                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
15432                         do_facet $facet lctl get_param $stats
15433                         error "No jobstats for $JOBVAL found on $facet::$stats"
15434                 fi
15435         done
15436 }
15437
15438 jobstats_set() {
15439         local new_jobenv=$1
15440
15441         set_persistent_param_and_check client "jobid_var" \
15442                 "$FSNAME.sys.jobid_var" $new_jobenv
15443 }
15444
15445 test_205() { # Job stats
15446         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15447         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
15448                 skip "Need MDS version with at least 2.7.1"
15449         remote_mgs_nodsh && skip "remote MGS with nodsh"
15450         remote_mds_nodsh && skip "remote MDS with nodsh"
15451         remote_ost_nodsh && skip "remote OST with nodsh"
15452         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
15453                 skip "Server doesn't support jobstats"
15454         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
15455
15456         local old_jobenv=$($LCTL get_param -n jobid_var)
15457         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
15458
15459         if [[ $PERM_CMD == *"set_param -P"* ]]; then
15460                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
15461         else
15462                 stack_trap "do_facet mgs $PERM_CMD \
15463                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
15464         fi
15465         changelog_register
15466
15467         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
15468                                 mdt.*.job_cleanup_interval | head -n 1)
15469         local new_interval=5
15470         do_facet $SINGLEMDS \
15471                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
15472         stack_trap "do_facet $SINGLEMDS \
15473                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
15474         local start=$SECONDS
15475
15476         local cmd
15477         # mkdir
15478         cmd="mkdir $DIR/$tdir"
15479         verify_jobstats "$cmd" "$SINGLEMDS"
15480         # rmdir
15481         cmd="rmdir $DIR/$tdir"
15482         verify_jobstats "$cmd" "$SINGLEMDS"
15483         # mkdir on secondary MDT
15484         if [ $MDSCOUNT -gt 1 ]; then
15485                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
15486                 verify_jobstats "$cmd" "mds2"
15487         fi
15488         # mknod
15489         cmd="mknod $DIR/$tfile c 1 3"
15490         verify_jobstats "$cmd" "$SINGLEMDS"
15491         # unlink
15492         cmd="rm -f $DIR/$tfile"
15493         verify_jobstats "$cmd" "$SINGLEMDS"
15494         # create all files on OST0000 so verify_jobstats can find OST stats
15495         # open & close
15496         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
15497         verify_jobstats "$cmd" "$SINGLEMDS"
15498         # setattr
15499         cmd="touch $DIR/$tfile"
15500         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15501         # write
15502         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
15503         verify_jobstats "$cmd" "ost1"
15504         # read
15505         cancel_lru_locks osc
15506         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
15507         verify_jobstats "$cmd" "ost1"
15508         # truncate
15509         cmd="$TRUNCATE $DIR/$tfile 0"
15510         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15511         # rename
15512         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
15513         verify_jobstats "$cmd" "$SINGLEMDS"
15514         # jobstats expiry - sleep until old stats should be expired
15515         local left=$((new_interval + 5 - (SECONDS - start)))
15516         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
15517                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
15518                         "0" $left
15519         cmd="mkdir $DIR/$tdir.expire"
15520         verify_jobstats "$cmd" "$SINGLEMDS"
15521         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
15522             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
15523
15524         # Ensure that jobid are present in changelog (if supported by MDS)
15525         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
15526                 changelog_dump | tail -10
15527                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
15528                 [ $jobids -eq 9 ] ||
15529                         error "Wrong changelog jobid count $jobids != 9"
15530
15531                 # LU-5862
15532                 JOBENV="disable"
15533                 jobstats_set $JOBENV
15534                 touch $DIR/$tfile
15535                 changelog_dump | grep $tfile
15536                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
15537                 [ $jobids -eq 0 ] ||
15538                         error "Unexpected jobids when jobid_var=$JOBENV"
15539         fi
15540
15541         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
15542         JOBENV="JOBCOMPLEX"
15543         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
15544
15545         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
15546 }
15547 run_test 205 "Verify job stats"
15548
15549 # LU-1480, LU-1773 and LU-1657
15550 test_206() {
15551         mkdir -p $DIR/$tdir
15552         $LFS setstripe -c -1 $DIR/$tdir
15553 #define OBD_FAIL_LOV_INIT 0x1403
15554         $LCTL set_param fail_loc=0xa0001403
15555         $LCTL set_param fail_val=1
15556         touch $DIR/$tdir/$tfile || true
15557 }
15558 run_test 206 "fail lov_init_raid0() doesn't lbug"
15559
15560 test_207a() {
15561         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15562         local fsz=`stat -c %s $DIR/$tfile`
15563         cancel_lru_locks mdc
15564
15565         # do not return layout in getattr intent
15566 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
15567         $LCTL set_param fail_loc=0x170
15568         local sz=`stat -c %s $DIR/$tfile`
15569
15570         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
15571
15572         rm -rf $DIR/$tfile
15573 }
15574 run_test 207a "can refresh layout at glimpse"
15575
15576 test_207b() {
15577         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15578         local cksum=`md5sum $DIR/$tfile`
15579         local fsz=`stat -c %s $DIR/$tfile`
15580         cancel_lru_locks mdc
15581         cancel_lru_locks osc
15582
15583         # do not return layout in getattr intent
15584 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
15585         $LCTL set_param fail_loc=0x171
15586
15587         # it will refresh layout after the file is opened but before read issues
15588         echo checksum is "$cksum"
15589         echo "$cksum" |md5sum -c --quiet || error "file differs"
15590
15591         rm -rf $DIR/$tfile
15592 }
15593 run_test 207b "can refresh layout at open"
15594
15595 test_208() {
15596         # FIXME: in this test suite, only RD lease is used. This is okay
15597         # for now as only exclusive open is supported. After generic lease
15598         # is done, this test suite should be revised. - Jinshan
15599
15600         remote_mds_nodsh && skip "remote MDS with nodsh"
15601         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
15602                 skip "Need MDS version at least 2.4.52"
15603
15604         echo "==== test 1: verify get lease work"
15605         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
15606
15607         echo "==== test 2: verify lease can be broken by upcoming open"
15608         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15609         local PID=$!
15610         sleep 1
15611
15612         $MULTIOP $DIR/$tfile oO_RDONLY:c
15613         kill -USR1 $PID && wait $PID || error "break lease error"
15614
15615         echo "==== test 3: verify lease can't be granted if an open already exists"
15616         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
15617         local PID=$!
15618         sleep 1
15619
15620         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
15621         kill -USR1 $PID && wait $PID || error "open file error"
15622
15623         echo "==== test 4: lease can sustain over recovery"
15624         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
15625         PID=$!
15626         sleep 1
15627
15628         fail mds1
15629
15630         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
15631
15632         echo "==== test 5: lease broken can't be regained by replay"
15633         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15634         PID=$!
15635         sleep 1
15636
15637         # open file to break lease and then recovery
15638         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
15639         fail mds1
15640
15641         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
15642
15643         rm -f $DIR/$tfile
15644 }
15645 run_test 208 "Exclusive open"
15646
15647 test_209() {
15648         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
15649                 skip_env "must have disp_stripe"
15650
15651         touch $DIR/$tfile
15652         sync; sleep 5; sync;
15653
15654         echo 3 > /proc/sys/vm/drop_caches
15655         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15656
15657         # open/close 500 times
15658         for i in $(seq 500); do
15659                 cat $DIR/$tfile
15660         done
15661
15662         echo 3 > /proc/sys/vm/drop_caches
15663         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15664
15665         echo "before: $req_before, after: $req_after"
15666         [ $((req_after - req_before)) -ge 300 ] &&
15667                 error "open/close requests are not freed"
15668         return 0
15669 }
15670 run_test 209 "read-only open/close requests should be freed promptly"
15671
15672 test_212() {
15673         size=`date +%s`
15674         size=$((size % 8192 + 1))
15675         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
15676         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
15677         rm -f $DIR/f212 $DIR/f212.xyz
15678 }
15679 run_test 212 "Sendfile test ============================================"
15680
15681 test_213() {
15682         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
15683         cancel_lru_locks osc
15684         lctl set_param fail_loc=0x8000040f
15685         # generate a read lock
15686         cat $DIR/$tfile > /dev/null
15687         # write to the file, it will try to cancel the above read lock.
15688         cat /etc/hosts >> $DIR/$tfile
15689 }
15690 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
15691
15692 test_214() { # for bug 20133
15693         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
15694         for (( i=0; i < 340; i++ )) ; do
15695                 touch $DIR/$tdir/d214c/a$i
15696         done
15697
15698         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
15699         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
15700         ls $DIR/d214c || error "ls $DIR/d214c failed"
15701         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
15702         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
15703 }
15704 run_test 214 "hash-indexed directory test - bug 20133"
15705
15706 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
15707 create_lnet_proc_files() {
15708         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
15709 }
15710
15711 # counterpart of create_lnet_proc_files
15712 remove_lnet_proc_files() {
15713         rm -f $TMP/lnet_$1.sys
15714 }
15715
15716 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15717 # 3rd arg as regexp for body
15718 check_lnet_proc_stats() {
15719         local l=$(cat "$TMP/lnet_$1" |wc -l)
15720         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
15721
15722         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
15723 }
15724
15725 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15726 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
15727 # optional and can be regexp for 2nd line (lnet.routes case)
15728 check_lnet_proc_entry() {
15729         local blp=2          # blp stands for 'position of 1st line of body'
15730         [ -z "$5" ] || blp=3 # lnet.routes case
15731
15732         local l=$(cat "$TMP/lnet_$1" |wc -l)
15733         # subtracting one from $blp because the body can be empty
15734         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
15735
15736         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
15737                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
15738
15739         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
15740                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
15741
15742         # bail out if any unexpected line happened
15743         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
15744         [ "$?" != 0 ] || error "$2 misformatted"
15745 }
15746
15747 test_215() { # for bugs 18102, 21079, 21517
15748         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15749
15750         local N='(0|[1-9][0-9]*)'       # non-negative numeric
15751         local P='[1-9][0-9]*'           # positive numeric
15752         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
15753         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
15754         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
15755         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
15756
15757         local L1 # regexp for 1st line
15758         local L2 # regexp for 2nd line (optional)
15759         local BR # regexp for the rest (body)
15760
15761         # lnet.stats should look as 11 space-separated non-negative numerics
15762         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
15763         create_lnet_proc_files "stats"
15764         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
15765         remove_lnet_proc_files "stats"
15766
15767         # lnet.routes should look like this:
15768         # Routing disabled/enabled
15769         # net hops priority state router
15770         # where net is a string like tcp0, hops > 0, priority >= 0,
15771         # state is up/down,
15772         # router is a string like 192.168.1.1@tcp2
15773         L1="^Routing (disabled|enabled)$"
15774         L2="^net +hops +priority +state +router$"
15775         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
15776         create_lnet_proc_files "routes"
15777         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
15778         remove_lnet_proc_files "routes"
15779
15780         # lnet.routers should look like this:
15781         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
15782         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
15783         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
15784         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
15785         L1="^ref +rtr_ref +alive +router$"
15786         BR="^$P +$P +(up|down) +$NID$"
15787         create_lnet_proc_files "routers"
15788         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
15789         remove_lnet_proc_files "routers"
15790
15791         # lnet.peers should look like this:
15792         # nid refs state last max rtr min tx min queue
15793         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
15794         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
15795         # numeric (0 or >0 or <0), queue >= 0.
15796         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
15797         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
15798         create_lnet_proc_files "peers"
15799         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
15800         remove_lnet_proc_files "peers"
15801
15802         # lnet.buffers  should look like this:
15803         # pages count credits min
15804         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
15805         L1="^pages +count +credits +min$"
15806         BR="^ +$N +$N +$I +$I$"
15807         create_lnet_proc_files "buffers"
15808         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
15809         remove_lnet_proc_files "buffers"
15810
15811         # lnet.nis should look like this:
15812         # nid status alive refs peer rtr max tx min
15813         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
15814         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
15815         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
15816         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
15817         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
15818         create_lnet_proc_files "nis"
15819         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
15820         remove_lnet_proc_files "nis"
15821
15822         # can we successfully write to lnet.stats?
15823         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
15824 }
15825 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
15826
15827 test_216() { # bug 20317
15828         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15829         remote_ost_nodsh && skip "remote OST with nodsh"
15830
15831         local node
15832         local facets=$(get_facets OST)
15833         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15834
15835         save_lustre_params client "osc.*.contention_seconds" > $p
15836         save_lustre_params $facets \
15837                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
15838         save_lustre_params $facets \
15839                 "ldlm.namespaces.filter-*.contended_locks" >> $p
15840         save_lustre_params $facets \
15841                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
15842         clear_stats osc.*.osc_stats
15843
15844         # agressive lockless i/o settings
15845         do_nodes $(comma_list $(osts_nodes)) \
15846                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
15847                         ldlm.namespaces.filter-*.contended_locks=0 \
15848                         ldlm.namespaces.filter-*.contention_seconds=60"
15849         lctl set_param -n osc.*.contention_seconds=60
15850
15851         $DIRECTIO write $DIR/$tfile 0 10 4096
15852         $CHECKSTAT -s 40960 $DIR/$tfile
15853
15854         # disable lockless i/o
15855         do_nodes $(comma_list $(osts_nodes)) \
15856                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
15857                         ldlm.namespaces.filter-*.contended_locks=32 \
15858                         ldlm.namespaces.filter-*.contention_seconds=0"
15859         lctl set_param -n osc.*.contention_seconds=0
15860         clear_stats osc.*.osc_stats
15861
15862         dd if=/dev/zero of=$DIR/$tfile count=0
15863         $CHECKSTAT -s 0 $DIR/$tfile
15864
15865         restore_lustre_params <$p
15866         rm -f $p
15867         rm $DIR/$tfile
15868 }
15869 run_test 216 "check lockless direct write updates file size and kms correctly"
15870
15871 test_217() { # bug 22430
15872         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15873
15874         local node
15875         local nid
15876
15877         for node in $(nodes_list); do
15878                 nid=$(host_nids_address $node $NETTYPE)
15879                 if [[ $nid = *-* ]] ; then
15880                         echo "lctl ping $(h2nettype $nid)"
15881                         lctl ping $(h2nettype $nid)
15882                 else
15883                         echo "skipping $node (no hyphen detected)"
15884                 fi
15885         done
15886 }
15887 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
15888
15889 test_218() {
15890        # do directio so as not to populate the page cache
15891        log "creating a 10 Mb file"
15892        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
15893        log "starting reads"
15894        dd if=$DIR/$tfile of=/dev/null bs=4096 &
15895        log "truncating the file"
15896        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
15897        log "killing dd"
15898        kill %+ || true # reads might have finished
15899        echo "wait until dd is finished"
15900        wait
15901        log "removing the temporary file"
15902        rm -rf $DIR/$tfile || error "tmp file removal failed"
15903 }
15904 run_test 218 "parallel read and truncate should not deadlock"
15905
15906 test_219() {
15907         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15908
15909         # write one partial page
15910         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
15911         # set no grant so vvp_io_commit_write will do sync write
15912         $LCTL set_param fail_loc=0x411
15913         # write a full page at the end of file
15914         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
15915
15916         $LCTL set_param fail_loc=0
15917         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
15918         $LCTL set_param fail_loc=0x411
15919         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
15920
15921         # LU-4201
15922         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
15923         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
15924 }
15925 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
15926
15927 test_220() { #LU-325
15928         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15929         remote_ost_nodsh && skip "remote OST with nodsh"
15930         remote_mds_nodsh && skip "remote MDS with nodsh"
15931         remote_mgs_nodsh && skip "remote MGS with nodsh"
15932
15933         local OSTIDX=0
15934
15935         # create on MDT0000 so the last_id and next_id are correct
15936         mkdir $DIR/$tdir
15937         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
15938         OST=${OST%_UUID}
15939
15940         # on the mdt's osc
15941         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
15942         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
15943                         osp.$mdtosc_proc1.prealloc_last_id)
15944         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
15945                         osp.$mdtosc_proc1.prealloc_next_id)
15946
15947         $LFS df -i
15948
15949         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
15950         #define OBD_FAIL_OST_ENOINO              0x229
15951         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
15952         create_pool $FSNAME.$TESTNAME || return 1
15953         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
15954
15955         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
15956
15957         MDSOBJS=$((last_id - next_id))
15958         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
15959
15960         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
15961         echo "OST still has $count kbytes free"
15962
15963         echo "create $MDSOBJS files @next_id..."
15964         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
15965
15966         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
15967                         osp.$mdtosc_proc1.prealloc_last_id)
15968         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
15969                         osp.$mdtosc_proc1.prealloc_next_id)
15970
15971         echo "after creation, last_id=$last_id2, next_id=$next_id2"
15972         $LFS df -i
15973
15974         echo "cleanup..."
15975
15976         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
15977         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
15978
15979         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
15980                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
15981         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
15982                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
15983         echo "unlink $MDSOBJS files @$next_id..."
15984         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
15985 }
15986 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
15987
15988 test_221() {
15989         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15990
15991         dd if=`which date` of=$MOUNT/date oflag=sync
15992         chmod +x $MOUNT/date
15993
15994         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
15995         $LCTL set_param fail_loc=0x80001401
15996
15997         $MOUNT/date > /dev/null
15998         rm -f $MOUNT/date
15999 }
16000 run_test 221 "make sure fault and truncate race to not cause OOM"
16001
16002 test_222a () {
16003         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16004
16005         rm -rf $DIR/$tdir
16006         test_mkdir $DIR/$tdir
16007         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16008         createmany -o $DIR/$tdir/$tfile 10
16009         cancel_lru_locks mdc
16010         cancel_lru_locks osc
16011         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16012         $LCTL set_param fail_loc=0x31a
16013         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
16014         $LCTL set_param fail_loc=0
16015         rm -r $DIR/$tdir
16016 }
16017 run_test 222a "AGL for ls should not trigger CLIO lock failure"
16018
16019 test_222b () {
16020         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16021
16022         rm -rf $DIR/$tdir
16023         test_mkdir $DIR/$tdir
16024         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16025         createmany -o $DIR/$tdir/$tfile 10
16026         cancel_lru_locks mdc
16027         cancel_lru_locks osc
16028         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16029         $LCTL set_param fail_loc=0x31a
16030         rm -r $DIR/$tdir || error "AGL for rmdir failed"
16031         $LCTL set_param fail_loc=0
16032 }
16033 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
16034
16035 test_223 () {
16036         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16037
16038         rm -rf $DIR/$tdir
16039         test_mkdir $DIR/$tdir
16040         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16041         createmany -o $DIR/$tdir/$tfile 10
16042         cancel_lru_locks mdc
16043         cancel_lru_locks osc
16044         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
16045         $LCTL set_param fail_loc=0x31b
16046         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
16047         $LCTL set_param fail_loc=0
16048         rm -r $DIR/$tdir
16049 }
16050 run_test 223 "osc reenqueue if without AGL lock granted ======================="
16051
16052 test_224a() { # LU-1039, MRP-303
16053         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16054
16055         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
16056         $LCTL set_param fail_loc=0x508
16057         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
16058         $LCTL set_param fail_loc=0
16059         df $DIR
16060 }
16061 run_test 224a "Don't panic on bulk IO failure"
16062
16063 test_224b() { # LU-1039, MRP-303
16064         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16065
16066         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
16067         cancel_lru_locks osc
16068         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
16069         $LCTL set_param fail_loc=0x515
16070         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
16071         $LCTL set_param fail_loc=0
16072         df $DIR
16073 }
16074 run_test 224b "Don't panic on bulk IO failure"
16075
16076 test_224c() { # LU-6441
16077         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16078         remote_mds_nodsh && skip "remote MDS with nodsh"
16079
16080         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16081         save_writethrough $p
16082         set_cache writethrough on
16083
16084         local pages_per_rpc=$($LCTL get_param \
16085                                 osc.*.max_pages_per_rpc)
16086         local at_max=$($LCTL get_param -n at_max)
16087         local timeout=$($LCTL get_param -n timeout)
16088         local test_at="at_max"
16089         local param_at="$FSNAME.sys.at_max"
16090         local test_timeout="timeout"
16091         local param_timeout="$FSNAME.sys.timeout"
16092
16093         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
16094
16095         set_persistent_param_and_check client "$test_at" "$param_at" 0
16096         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
16097
16098         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
16099         do_facet ost1 "$LCTL set_param fail_loc=0x520"
16100         $LFS setstripe -c 1 -i 0 $DIR/$tfile
16101         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
16102         sync
16103         do_facet ost1 "$LCTL set_param fail_loc=0"
16104
16105         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
16106         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
16107                 $timeout
16108
16109         $LCTL set_param -n $pages_per_rpc
16110         restore_lustre_params < $p
16111         rm -f $p
16112 }
16113 run_test 224c "Don't hang if one of md lost during large bulk RPC"
16114
16115 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
16116 test_225a () {
16117         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16118         if [ -z ${MDSSURVEY} ]; then
16119                 skip_env "mds-survey not found"
16120         fi
16121         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16122                 skip "Need MDS version at least 2.2.51"
16123
16124         local mds=$(facet_host $SINGLEMDS)
16125         local target=$(do_nodes $mds 'lctl dl' |
16126                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16127
16128         local cmd1="file_count=1000 thrhi=4"
16129         local cmd2="dir_count=2 layer=mdd stripe_count=0"
16130         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16131         local cmd="$cmd1 $cmd2 $cmd3"
16132
16133         rm -f ${TMP}/mds_survey*
16134         echo + $cmd
16135         eval $cmd || error "mds-survey with zero-stripe failed"
16136         cat ${TMP}/mds_survey*
16137         rm -f ${TMP}/mds_survey*
16138 }
16139 run_test 225a "Metadata survey sanity with zero-stripe"
16140
16141 test_225b () {
16142         if [ -z ${MDSSURVEY} ]; then
16143                 skip_env "mds-survey not found"
16144         fi
16145         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16146                 skip "Need MDS version at least 2.2.51"
16147         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16148         remote_mds_nodsh && skip "remote MDS with nodsh"
16149         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
16150                 skip_env "Need to mount OST to test"
16151         fi
16152
16153         local mds=$(facet_host $SINGLEMDS)
16154         local target=$(do_nodes $mds 'lctl dl' |
16155                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16156
16157         local cmd1="file_count=1000 thrhi=4"
16158         local cmd2="dir_count=2 layer=mdd stripe_count=1"
16159         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16160         local cmd="$cmd1 $cmd2 $cmd3"
16161
16162         rm -f ${TMP}/mds_survey*
16163         echo + $cmd
16164         eval $cmd || error "mds-survey with stripe_count failed"
16165         cat ${TMP}/mds_survey*
16166         rm -f ${TMP}/mds_survey*
16167 }
16168 run_test 225b "Metadata survey sanity with stripe_count = 1"
16169
16170 mcreate_path2fid () {
16171         local mode=$1
16172         local major=$2
16173         local minor=$3
16174         local name=$4
16175         local desc=$5
16176         local path=$DIR/$tdir/$name
16177         local fid
16178         local rc
16179         local fid_path
16180
16181         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
16182                 error "cannot create $desc"
16183
16184         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
16185         rc=$?
16186         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
16187
16188         fid_path=$($LFS fid2path $MOUNT $fid)
16189         rc=$?
16190         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
16191
16192         [ "$path" == "$fid_path" ] ||
16193                 error "fid2path returned $fid_path, expected $path"
16194
16195         echo "pass with $path and $fid"
16196 }
16197
16198 test_226a () {
16199         rm -rf $DIR/$tdir
16200         mkdir -p $DIR/$tdir
16201
16202         mcreate_path2fid 0010666 0 0 fifo "FIFO"
16203         mcreate_path2fid 0020666 1 3 null "character special file (null)"
16204         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
16205         mcreate_path2fid 0040666 0 0 dir "directory"
16206         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
16207         mcreate_path2fid 0100666 0 0 file "regular file"
16208         mcreate_path2fid 0120666 0 0 link "symbolic link"
16209         mcreate_path2fid 0140666 0 0 sock "socket"
16210 }
16211 run_test 226a "call path2fid and fid2path on files of all type"
16212
16213 test_226b () {
16214         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16215
16216         local MDTIDX=1
16217
16218         rm -rf $DIR/$tdir
16219         mkdir -p $DIR/$tdir
16220         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
16221                 error "create remote directory failed"
16222         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
16223         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
16224                                 "character special file (null)"
16225         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
16226                                 "character special file (no device)"
16227         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
16228         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
16229                                 "block special file (loop)"
16230         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
16231         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
16232         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
16233 }
16234 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
16235
16236 # LU-1299 Executing or running ldd on a truncated executable does not
16237 # cause an out-of-memory condition.
16238 test_227() {
16239         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16240         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
16241
16242         dd if=$(which date) of=$MOUNT/date bs=1k count=1
16243         chmod +x $MOUNT/date
16244
16245         $MOUNT/date > /dev/null
16246         ldd $MOUNT/date > /dev/null
16247         rm -f $MOUNT/date
16248 }
16249 run_test 227 "running truncated executable does not cause OOM"
16250
16251 # LU-1512 try to reuse idle OI blocks
16252 test_228a() {
16253         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16254         remote_mds_nodsh && skip "remote MDS with nodsh"
16255         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16256
16257         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16258         local myDIR=$DIR/$tdir
16259
16260         mkdir -p $myDIR
16261         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16262         $LCTL set_param fail_loc=0x80001002
16263         createmany -o $myDIR/t- 10000
16264         $LCTL set_param fail_loc=0
16265         # The guard is current the largest FID holder
16266         touch $myDIR/guard
16267         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16268                     tr -d '[')
16269         local IDX=$(($SEQ % 64))
16270
16271         do_facet $SINGLEMDS sync
16272         # Make sure journal flushed.
16273         sleep 6
16274         local blk1=$(do_facet $SINGLEMDS \
16275                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16276                      grep Blockcount | awk '{print $4}')
16277
16278         # Remove old files, some OI blocks will become idle.
16279         unlinkmany $myDIR/t- 10000
16280         # Create new files, idle OI blocks should be reused.
16281         createmany -o $myDIR/t- 2000
16282         do_facet $SINGLEMDS sync
16283         # Make sure journal flushed.
16284         sleep 6
16285         local blk2=$(do_facet $SINGLEMDS \
16286                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16287                      grep Blockcount | awk '{print $4}')
16288
16289         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16290 }
16291 run_test 228a "try to reuse idle OI blocks"
16292
16293 test_228b() {
16294         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16295         remote_mds_nodsh && skip "remote MDS with nodsh"
16296         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16297
16298         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16299         local myDIR=$DIR/$tdir
16300
16301         mkdir -p $myDIR
16302         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16303         $LCTL set_param fail_loc=0x80001002
16304         createmany -o $myDIR/t- 10000
16305         $LCTL set_param fail_loc=0
16306         # The guard is current the largest FID holder
16307         touch $myDIR/guard
16308         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16309                     tr -d '[')
16310         local IDX=$(($SEQ % 64))
16311
16312         do_facet $SINGLEMDS sync
16313         # Make sure journal flushed.
16314         sleep 6
16315         local blk1=$(do_facet $SINGLEMDS \
16316                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16317                      grep Blockcount | awk '{print $4}')
16318
16319         # Remove old files, some OI blocks will become idle.
16320         unlinkmany $myDIR/t- 10000
16321
16322         # stop the MDT
16323         stop $SINGLEMDS || error "Fail to stop MDT."
16324         # remount the MDT
16325         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
16326
16327         df $MOUNT || error "Fail to df."
16328         # Create new files, idle OI blocks should be reused.
16329         createmany -o $myDIR/t- 2000
16330         do_facet $SINGLEMDS sync
16331         # Make sure journal flushed.
16332         sleep 6
16333         local blk2=$(do_facet $SINGLEMDS \
16334                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16335                      grep Blockcount | awk '{print $4}')
16336
16337         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16338 }
16339 run_test 228b "idle OI blocks can be reused after MDT restart"
16340
16341 #LU-1881
16342 test_228c() {
16343         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16344         remote_mds_nodsh && skip "remote MDS with nodsh"
16345         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16346
16347         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16348         local myDIR=$DIR/$tdir
16349
16350         mkdir -p $myDIR
16351         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16352         $LCTL set_param fail_loc=0x80001002
16353         # 20000 files can guarantee there are index nodes in the OI file
16354         createmany -o $myDIR/t- 20000
16355         $LCTL set_param fail_loc=0
16356         # The guard is current the largest FID holder
16357         touch $myDIR/guard
16358         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16359                     tr -d '[')
16360         local IDX=$(($SEQ % 64))
16361
16362         do_facet $SINGLEMDS sync
16363         # Make sure journal flushed.
16364         sleep 6
16365         local blk1=$(do_facet $SINGLEMDS \
16366                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16367                      grep Blockcount | awk '{print $4}')
16368
16369         # Remove old files, some OI blocks will become idle.
16370         unlinkmany $myDIR/t- 20000
16371         rm -f $myDIR/guard
16372         # The OI file should become empty now
16373
16374         # Create new files, idle OI blocks should be reused.
16375         createmany -o $myDIR/t- 2000
16376         do_facet $SINGLEMDS sync
16377         # Make sure journal flushed.
16378         sleep 6
16379         local blk2=$(do_facet $SINGLEMDS \
16380                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16381                      grep Blockcount | awk '{print $4}')
16382
16383         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16384 }
16385 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
16386
16387 test_229() { # LU-2482, LU-3448
16388         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16389         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
16390         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
16391                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
16392
16393         rm -f $DIR/$tfile
16394
16395         # Create a file with a released layout and stripe count 2.
16396         $MULTIOP $DIR/$tfile H2c ||
16397                 error "failed to create file with released layout"
16398
16399         $LFS getstripe -v $DIR/$tfile
16400
16401         local pattern=$($LFS getstripe -L $DIR/$tfile)
16402         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
16403
16404         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
16405                 error "getstripe"
16406         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
16407         stat $DIR/$tfile || error "failed to stat released file"
16408
16409         chown $RUNAS_ID $DIR/$tfile ||
16410                 error "chown $RUNAS_ID $DIR/$tfile failed"
16411
16412         chgrp $RUNAS_ID $DIR/$tfile ||
16413                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
16414
16415         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
16416         rm $DIR/$tfile || error "failed to remove released file"
16417 }
16418 run_test 229 "getstripe/stat/rm/attr changes work on released files"
16419
16420 test_230a() {
16421         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16422         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16423         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16424                 skip "Need MDS version at least 2.11.52"
16425
16426         local MDTIDX=1
16427
16428         test_mkdir $DIR/$tdir
16429         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
16430         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
16431         [ $mdt_idx -ne 0 ] &&
16432                 error "create local directory on wrong MDT $mdt_idx"
16433
16434         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
16435                         error "create remote directory failed"
16436         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
16437         [ $mdt_idx -ne $MDTIDX ] &&
16438                 error "create remote directory on wrong MDT $mdt_idx"
16439
16440         createmany -o $DIR/$tdir/test_230/t- 10 ||
16441                 error "create files on remote directory failed"
16442         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
16443         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
16444         rm -r $DIR/$tdir || error "unlink remote directory failed"
16445 }
16446 run_test 230a "Create remote directory and files under the remote directory"
16447
16448 test_230b() {
16449         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16450         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16451         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16452                 skip "Need MDS version at least 2.11.52"
16453
16454         local MDTIDX=1
16455         local mdt_index
16456         local i
16457         local file
16458         local pid
16459         local stripe_count
16460         local migrate_dir=$DIR/$tdir/migrate_dir
16461         local other_dir=$DIR/$tdir/other_dir
16462
16463         test_mkdir $DIR/$tdir
16464         test_mkdir -i0 -c1 $migrate_dir
16465         test_mkdir -i0 -c1 $other_dir
16466         for ((i=0; i<10; i++)); do
16467                 mkdir -p $migrate_dir/dir_${i}
16468                 createmany -o $migrate_dir/dir_${i}/f 10 ||
16469                         error "create files under remote dir failed $i"
16470         done
16471
16472         cp /etc/passwd $migrate_dir/$tfile
16473         cp /etc/passwd $other_dir/$tfile
16474         chattr +SAD $migrate_dir
16475         chattr +SAD $migrate_dir/$tfile
16476
16477         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16478         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16479         local old_dir_mode=$(stat -c%f $migrate_dir)
16480         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
16481
16482         mkdir -p $migrate_dir/dir_default_stripe2
16483         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
16484         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
16485
16486         mkdir -p $other_dir
16487         ln $migrate_dir/$tfile $other_dir/luna
16488         ln $migrate_dir/$tfile $migrate_dir/sofia
16489         ln $other_dir/$tfile $migrate_dir/david
16490         ln -s $migrate_dir/$tfile $other_dir/zachary
16491         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
16492         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
16493
16494         $LFS migrate -m $MDTIDX $migrate_dir ||
16495                 error "fails on migrating remote dir to MDT1"
16496
16497         echo "migratate to MDT1, then checking.."
16498         for ((i = 0; i < 10; i++)); do
16499                 for file in $(find $migrate_dir/dir_${i}); do
16500                         mdt_index=$($LFS getstripe -m $file)
16501                         [ $mdt_index == $MDTIDX ] ||
16502                                 error "$file is not on MDT${MDTIDX}"
16503                 done
16504         done
16505
16506         # the multiple link file should still in MDT0
16507         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
16508         [ $mdt_index == 0 ] ||
16509                 error "$file is not on MDT${MDTIDX}"
16510
16511         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16512         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16513                 error " expect $old_dir_flag get $new_dir_flag"
16514
16515         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16516         [ "$old_file_flag" = "$new_file_flag" ] ||
16517                 error " expect $old_file_flag get $new_file_flag"
16518
16519         local new_dir_mode=$(stat -c%f $migrate_dir)
16520         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16521                 error "expect mode $old_dir_mode get $new_dir_mode"
16522
16523         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16524         [ "$old_file_mode" = "$new_file_mode" ] ||
16525                 error "expect mode $old_file_mode get $new_file_mode"
16526
16527         diff /etc/passwd $migrate_dir/$tfile ||
16528                 error "$tfile different after migration"
16529
16530         diff /etc/passwd $other_dir/luna ||
16531                 error "luna different after migration"
16532
16533         diff /etc/passwd $migrate_dir/sofia ||
16534                 error "sofia different after migration"
16535
16536         diff /etc/passwd $migrate_dir/david ||
16537                 error "david different after migration"
16538
16539         diff /etc/passwd $other_dir/zachary ||
16540                 error "zachary different after migration"
16541
16542         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16543                 error "${tfile}_ln different after migration"
16544
16545         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16546                 error "${tfile}_ln_other different after migration"
16547
16548         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
16549         [ $stripe_count = 2 ] ||
16550                 error "dir strpe_count $d != 2 after migration."
16551
16552         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
16553         [ $stripe_count = 2 ] ||
16554                 error "file strpe_count $d != 2 after migration."
16555
16556         #migrate back to MDT0
16557         MDTIDX=0
16558
16559         $LFS migrate -m $MDTIDX $migrate_dir ||
16560                 error "fails on migrating remote dir to MDT0"
16561
16562         echo "migrate back to MDT0, checking.."
16563         for file in $(find $migrate_dir); do
16564                 mdt_index=$($LFS getstripe -m $file)
16565                 [ $mdt_index == $MDTIDX ] ||
16566                         error "$file is not on MDT${MDTIDX}"
16567         done
16568
16569         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16570         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16571                 error " expect $old_dir_flag get $new_dir_flag"
16572
16573         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16574         [ "$old_file_flag" = "$new_file_flag" ] ||
16575                 error " expect $old_file_flag get $new_file_flag"
16576
16577         local new_dir_mode=$(stat -c%f $migrate_dir)
16578         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16579                 error "expect mode $old_dir_mode get $new_dir_mode"
16580
16581         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16582         [ "$old_file_mode" = "$new_file_mode" ] ||
16583                 error "expect mode $old_file_mode get $new_file_mode"
16584
16585         diff /etc/passwd ${migrate_dir}/$tfile ||
16586                 error "$tfile different after migration"
16587
16588         diff /etc/passwd ${other_dir}/luna ||
16589                 error "luna different after migration"
16590
16591         diff /etc/passwd ${migrate_dir}/sofia ||
16592                 error "sofia different after migration"
16593
16594         diff /etc/passwd ${other_dir}/zachary ||
16595                 error "zachary different after migration"
16596
16597         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16598                 error "${tfile}_ln different after migration"
16599
16600         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16601                 error "${tfile}_ln_other different after migration"
16602
16603         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
16604         [ $stripe_count = 2 ] ||
16605                 error "dir strpe_count $d != 2 after migration."
16606
16607         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
16608         [ $stripe_count = 2 ] ||
16609                 error "file strpe_count $d != 2 after migration."
16610
16611         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16612 }
16613 run_test 230b "migrate directory"
16614
16615 test_230c() {
16616         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16617         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16618         remote_mds_nodsh && skip "remote MDS with nodsh"
16619         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16620                 skip "Need MDS version at least 2.11.52"
16621
16622         local MDTIDX=1
16623         local total=3
16624         local mdt_index
16625         local file
16626         local migrate_dir=$DIR/$tdir/migrate_dir
16627
16628         #If migrating directory fails in the middle, all entries of
16629         #the directory is still accessiable.
16630         test_mkdir $DIR/$tdir
16631         test_mkdir -i0 -c1 $migrate_dir
16632         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
16633         stat $migrate_dir
16634         createmany -o $migrate_dir/f $total ||
16635                 error "create files under ${migrate_dir} failed"
16636
16637         # fail after migrating top dir, and this will fail only once, so the
16638         # first sub file migration will fail (currently f3), others succeed.
16639         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
16640         do_facet mds1 lctl set_param fail_loc=0x1801
16641         local t=$(ls $migrate_dir | wc -l)
16642         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
16643                 error "migrate should fail"
16644         local u=$(ls $migrate_dir | wc -l)
16645         [ "$u" == "$t" ] || error "$u != $t during migration"
16646
16647         # add new dir/file should succeed
16648         mkdir $migrate_dir/dir ||
16649                 error "mkdir failed under migrating directory"
16650         touch $migrate_dir/file ||
16651                 error "create file failed under migrating directory"
16652
16653         # add file with existing name should fail
16654         for file in $migrate_dir/f*; do
16655                 stat $file > /dev/null || error "stat $file failed"
16656                 $OPENFILE -f O_CREAT:O_EXCL $file &&
16657                         error "open(O_CREAT|O_EXCL) $file should fail"
16658                 $MULTIOP $file m && error "create $file should fail"
16659                 touch $DIR/$tdir/remote_dir/$tfile ||
16660                         error "touch $tfile failed"
16661                 ln $DIR/$tdir/remote_dir/$tfile $file &&
16662                         error "link $file should fail"
16663                 mdt_index=$($LFS getstripe -m $file)
16664                 if [ $mdt_index == 0 ]; then
16665                         # file failed to migrate is not allowed to rename to
16666                         mv $DIR/$tdir/remote_dir/$tfile $file &&
16667                                 error "rename to $file should fail"
16668                 else
16669                         mv $DIR/$tdir/remote_dir/$tfile $file ||
16670                                 error "rename to $file failed"
16671                 fi
16672                 echo hello >> $file || error "write $file failed"
16673         done
16674
16675         # resume migration with different options should fail
16676         $LFS migrate -m 0 $migrate_dir &&
16677                 error "migrate -m 0 $migrate_dir should fail"
16678
16679         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
16680                 error "migrate -c 2 $migrate_dir should fail"
16681
16682         # resume migration should succeed
16683         $LFS migrate -m $MDTIDX $migrate_dir ||
16684                 error "migrate $migrate_dir failed"
16685
16686         echo "Finish migration, then checking.."
16687         for file in $(find $migrate_dir); do
16688                 mdt_index=$($LFS getstripe -m $file)
16689                 [ $mdt_index == $MDTIDX ] ||
16690                         error "$file is not on MDT${MDTIDX}"
16691         done
16692
16693         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16694 }
16695 run_test 230c "check directory accessiblity if migration failed"
16696
16697 test_230d() {
16698         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16699         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16700         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16701                 skip "Need MDS version at least 2.11.52"
16702         # LU-11235
16703         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
16704
16705         local migrate_dir=$DIR/$tdir/migrate_dir
16706         local old_index
16707         local new_index
16708         local old_count
16709         local new_count
16710         local new_hash
16711         local mdt_index
16712         local i
16713         local j
16714
16715         old_index=$((RANDOM % MDSCOUNT))
16716         old_count=$((MDSCOUNT - old_index))
16717         new_index=$((RANDOM % MDSCOUNT))
16718         new_count=$((MDSCOUNT - new_index))
16719         new_hash="all_char"
16720
16721         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
16722         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
16723
16724         test_mkdir $DIR/$tdir
16725         test_mkdir -i $old_index -c $old_count $migrate_dir
16726
16727         for ((i=0; i<100; i++)); do
16728                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
16729                 createmany -o $migrate_dir/dir_${i}/f 100 ||
16730                         error "create files under remote dir failed $i"
16731         done
16732
16733         echo -n "Migrate from MDT$old_index "
16734         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
16735         echo -n "to MDT$new_index"
16736         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
16737         echo
16738
16739         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
16740         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
16741                 error "migrate remote dir error"
16742
16743         echo "Finish migration, then checking.."
16744         for file in $(find $migrate_dir); do
16745                 mdt_index=$($LFS getstripe -m $file)
16746                 if [ $mdt_index -lt $new_index ] ||
16747                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
16748                         error "$file is on MDT$mdt_index"
16749                 fi
16750         done
16751
16752         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16753 }
16754 run_test 230d "check migrate big directory"
16755
16756 test_230e() {
16757         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16758         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16759         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16760                 skip "Need MDS version at least 2.11.52"
16761
16762         local i
16763         local j
16764         local a_fid
16765         local b_fid
16766
16767         mkdir -p $DIR/$tdir
16768         mkdir $DIR/$tdir/migrate_dir
16769         mkdir $DIR/$tdir/other_dir
16770         touch $DIR/$tdir/migrate_dir/a
16771         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
16772         ls $DIR/$tdir/other_dir
16773
16774         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16775                 error "migrate dir fails"
16776
16777         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16778         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16779
16780         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16781         [ $mdt_index == 0 ] || error "a is not on MDT0"
16782
16783         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
16784                 error "migrate dir fails"
16785
16786         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
16787         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
16788
16789         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16790         [ $mdt_index == 1 ] || error "a is not on MDT1"
16791
16792         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
16793         [ $mdt_index == 1 ] || error "b is not on MDT1"
16794
16795         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16796         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
16797
16798         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
16799
16800         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16801 }
16802 run_test 230e "migrate mulitple local link files"
16803
16804 test_230f() {
16805         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16806         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16807         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16808                 skip "Need MDS version at least 2.11.52"
16809
16810         local a_fid
16811         local ln_fid
16812
16813         mkdir -p $DIR/$tdir
16814         mkdir $DIR/$tdir/migrate_dir
16815         $LFS mkdir -i1 $DIR/$tdir/other_dir
16816         touch $DIR/$tdir/migrate_dir/a
16817         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
16818         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
16819         ls $DIR/$tdir/other_dir
16820
16821         # a should be migrated to MDT1, since no other links on MDT0
16822         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16823                 error "#1 migrate dir fails"
16824         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16825         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16826         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16827         [ $mdt_index == 1 ] || error "a is not on MDT1"
16828
16829         # a should stay on MDT1, because it is a mulitple link file
16830         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16831                 error "#2 migrate dir fails"
16832         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16833         [ $mdt_index == 1 ] || error "a is not on MDT1"
16834
16835         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16836                 error "#3 migrate dir fails"
16837
16838         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16839         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
16840         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
16841
16842         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
16843         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
16844
16845         # a should be migrated to MDT0, since no other links on MDT1
16846         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16847                 error "#4 migrate dir fails"
16848         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16849         [ $mdt_index == 0 ] || error "a is not on MDT0"
16850
16851         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16852 }
16853 run_test 230f "migrate mulitple remote link files"
16854
16855 test_230g() {
16856         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16857         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16858         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16859                 skip "Need MDS version at least 2.11.52"
16860
16861         mkdir -p $DIR/$tdir/migrate_dir
16862
16863         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
16864                 error "migrating dir to non-exist MDT succeeds"
16865         true
16866 }
16867 run_test 230g "migrate dir to non-exist MDT"
16868
16869 test_230h() {
16870         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16871         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16872         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16873                 skip "Need MDS version at least 2.11.52"
16874
16875         local mdt_index
16876
16877         mkdir -p $DIR/$tdir/migrate_dir
16878
16879         $LFS migrate -m1 $DIR &&
16880                 error "migrating mountpoint1 should fail"
16881
16882         $LFS migrate -m1 $DIR/$tdir/.. &&
16883                 error "migrating mountpoint2 should fail"
16884
16885         # same as mv
16886         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
16887                 error "migrating $tdir/migrate_dir/.. should fail"
16888
16889         true
16890 }
16891 run_test 230h "migrate .. and root"
16892
16893 test_230i() {
16894         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16895         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16896         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16897                 skip "Need MDS version at least 2.11.52"
16898
16899         mkdir -p $DIR/$tdir/migrate_dir
16900
16901         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
16902                 error "migration fails with a tailing slash"
16903
16904         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
16905                 error "migration fails with two tailing slashes"
16906 }
16907 run_test 230i "lfs migrate -m tolerates trailing slashes"
16908
16909 test_230j() {
16910         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16911         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16912                 skip "Need MDS version at least 2.11.52"
16913
16914         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
16915         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
16916                 error "create $tfile failed"
16917         cat /etc/passwd > $DIR/$tdir/$tfile
16918
16919         $LFS migrate -m 1 $DIR/$tdir
16920
16921         cmp /etc/passwd $DIR/$tdir/$tfile ||
16922                 error "DoM file mismatch after migration"
16923 }
16924 run_test 230j "DoM file data not changed after dir migration"
16925
16926 test_230k() {
16927         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
16928         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16929                 skip "Need MDS version at least 2.11.56"
16930
16931         local total=20
16932         local files_on_starting_mdt=0
16933
16934         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
16935         $LFS getdirstripe $DIR/$tdir
16936         for i in $(seq $total); do
16937                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
16938                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
16939                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16940         done
16941
16942         echo "$files_on_starting_mdt files on MDT0"
16943
16944         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
16945         $LFS getdirstripe $DIR/$tdir
16946
16947         files_on_starting_mdt=0
16948         for i in $(seq $total); do
16949                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
16950                         error "file $tfile.$i mismatch after migration"
16951                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
16952                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16953         done
16954
16955         echo "$files_on_starting_mdt files on MDT1 after migration"
16956         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
16957
16958         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
16959         $LFS getdirstripe $DIR/$tdir
16960
16961         files_on_starting_mdt=0
16962         for i in $(seq $total); do
16963                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
16964                         error "file $tfile.$i mismatch after 2nd migration"
16965                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
16966                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16967         done
16968
16969         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
16970         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
16971
16972         true
16973 }
16974 run_test 230k "file data not changed after dir migration"
16975
16976 test_230l() {
16977         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16978         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16979                 skip "Need MDS version at least 2.11.56"
16980
16981         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
16982         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
16983                 error "create files under remote dir failed $i"
16984         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
16985 }
16986 run_test 230l "readdir between MDTs won't crash"
16987
16988 test_230m() {
16989         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16990         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16991                 skip "Need MDS version at least 2.11.56"
16992
16993         local MDTIDX=1
16994         local mig_dir=$DIR/$tdir/migrate_dir
16995         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
16996         local shortstr="b"
16997         local val
16998
16999         echo "Creating files and dirs with xattrs"
17000         test_mkdir $DIR/$tdir
17001         test_mkdir -i0 -c1 $mig_dir
17002         mkdir $mig_dir/dir
17003         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
17004                 error "cannot set xattr attr1 on dir"
17005         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
17006                 error "cannot set xattr attr2 on dir"
17007         touch $mig_dir/dir/f0
17008         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
17009                 error "cannot set xattr attr1 on file"
17010         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
17011                 error "cannot set xattr attr2 on file"
17012         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17013         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17014         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
17015         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17016         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
17017         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17018         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
17019         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17020         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
17021
17022         echo "Migrating to MDT1"
17023         $LFS migrate -m $MDTIDX $mig_dir ||
17024                 error "fails on migrating dir to MDT1"
17025
17026         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17027         echo "Checking xattrs"
17028         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17029         [ "$val" = $longstr ] ||
17030                 error "expecting xattr1 $longstr on dir, found $val"
17031         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17032         [ "$val" = $shortstr ] ||
17033                 error "expecting xattr2 $shortstr on dir, found $val"
17034         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17035         [ "$val" = $longstr ] ||
17036                 error "expecting xattr1 $longstr on file, found $val"
17037         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17038         [ "$val" = $shortstr ] ||
17039                 error "expecting xattr2 $shortstr on file, found $val"
17040 }
17041 run_test 230m "xattrs not changed after dir migration"
17042
17043 test_231a()
17044 {
17045         # For simplicity this test assumes that max_pages_per_rpc
17046         # is the same across all OSCs
17047         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
17048         local bulk_size=$((max_pages * PAGE_SIZE))
17049         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
17050                                        head -n 1)
17051
17052         mkdir -p $DIR/$tdir
17053         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
17054                 error "failed to set stripe with -S ${brw_size}M option"
17055
17056         # clear the OSC stats
17057         $LCTL set_param osc.*.stats=0 &>/dev/null
17058         stop_writeback
17059
17060         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
17061         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
17062                 oflag=direct &>/dev/null || error "dd failed"
17063
17064         sync; sleep 1; sync # just to be safe
17065         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
17066         if [ x$nrpcs != "x1" ]; then
17067                 $LCTL get_param osc.*.stats
17068                 error "found $nrpcs ost_write RPCs, not 1 as expected"
17069         fi
17070
17071         start_writeback
17072         # Drop the OSC cache, otherwise we will read from it
17073         cancel_lru_locks osc
17074
17075         # clear the OSC stats
17076         $LCTL set_param osc.*.stats=0 &>/dev/null
17077
17078         # Client reads $bulk_size.
17079         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
17080                 iflag=direct &>/dev/null || error "dd failed"
17081
17082         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
17083         if [ x$nrpcs != "x1" ]; then
17084                 $LCTL get_param osc.*.stats
17085                 error "found $nrpcs ost_read RPCs, not 1 as expected"
17086         fi
17087 }
17088 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
17089
17090 test_231b() {
17091         mkdir -p $DIR/$tdir
17092         local i
17093         for i in {0..1023}; do
17094                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
17095                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
17096                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
17097         done
17098         sync
17099 }
17100 run_test 231b "must not assert on fully utilized OST request buffer"
17101
17102 test_232a() {
17103         mkdir -p $DIR/$tdir
17104         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17105
17106         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17107         do_facet ost1 $LCTL set_param fail_loc=0x31c
17108
17109         # ignore dd failure
17110         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
17111
17112         do_facet ost1 $LCTL set_param fail_loc=0
17113         umount_client $MOUNT || error "umount failed"
17114         mount_client $MOUNT || error "mount failed"
17115         stop ost1 || error "cannot stop ost1"
17116         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17117 }
17118 run_test 232a "failed lock should not block umount"
17119
17120 test_232b() {
17121         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
17122                 skip "Need MDS version at least 2.10.58"
17123
17124         mkdir -p $DIR/$tdir
17125         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17126         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
17127         sync
17128         cancel_lru_locks osc
17129
17130         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17131         do_facet ost1 $LCTL set_param fail_loc=0x31c
17132
17133         # ignore failure
17134         $LFS data_version $DIR/$tdir/$tfile || true
17135
17136         do_facet ost1 $LCTL set_param fail_loc=0
17137         umount_client $MOUNT || error "umount failed"
17138         mount_client $MOUNT || error "mount failed"
17139         stop ost1 || error "cannot stop ost1"
17140         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17141 }
17142 run_test 232b "failed data version lock should not block umount"
17143
17144 test_233a() {
17145         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
17146                 skip "Need MDS version at least 2.3.64"
17147         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17148
17149         local fid=$($LFS path2fid $MOUNT)
17150
17151         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17152                 error "cannot access $MOUNT using its FID '$fid'"
17153 }
17154 run_test 233a "checking that OBF of the FS root succeeds"
17155
17156 test_233b() {
17157         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
17158                 skip "Need MDS version at least 2.5.90"
17159         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17160
17161         local fid=$($LFS path2fid $MOUNT/.lustre)
17162
17163         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17164                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
17165
17166         fid=$($LFS path2fid $MOUNT/.lustre/fid)
17167         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17168                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
17169 }
17170 run_test 233b "checking that OBF of the FS .lustre succeeds"
17171
17172 test_234() {
17173         local p="$TMP/sanityN-$TESTNAME.parameters"
17174         save_lustre_params client "llite.*.xattr_cache" > $p
17175         lctl set_param llite.*.xattr_cache 1 ||
17176                 skip_env "xattr cache is not supported"
17177
17178         mkdir -p $DIR/$tdir || error "mkdir failed"
17179         touch $DIR/$tdir/$tfile || error "touch failed"
17180         # OBD_FAIL_LLITE_XATTR_ENOMEM
17181         $LCTL set_param fail_loc=0x1405
17182         getfattr -n user.attr $DIR/$tdir/$tfile &&
17183                 error "getfattr should have failed with ENOMEM"
17184         $LCTL set_param fail_loc=0x0
17185         rm -rf $DIR/$tdir
17186
17187         restore_lustre_params < $p
17188         rm -f $p
17189 }
17190 run_test 234 "xattr cache should not crash on ENOMEM"
17191
17192 test_235() {
17193         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
17194                 skip "Need MDS version at least 2.4.52"
17195
17196         flock_deadlock $DIR/$tfile
17197         local RC=$?
17198         case $RC in
17199                 0)
17200                 ;;
17201                 124) error "process hangs on a deadlock"
17202                 ;;
17203                 *) error "error executing flock_deadlock $DIR/$tfile"
17204                 ;;
17205         esac
17206 }
17207 run_test 235 "LU-1715: flock deadlock detection does not work properly"
17208
17209 #LU-2935
17210 test_236() {
17211         check_swap_layouts_support
17212
17213         local ref1=/etc/passwd
17214         local ref2=/etc/group
17215         local file1=$DIR/$tdir/f1
17216         local file2=$DIR/$tdir/f2
17217
17218         test_mkdir -c1 $DIR/$tdir
17219         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
17220         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
17221         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
17222         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
17223         local fd=$(free_fd)
17224         local cmd="exec $fd<>$file2"
17225         eval $cmd
17226         rm $file2
17227         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
17228                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
17229         cmd="exec $fd>&-"
17230         eval $cmd
17231         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
17232
17233         #cleanup
17234         rm -rf $DIR/$tdir
17235 }
17236 run_test 236 "Layout swap on open unlinked file"
17237
17238 # LU-4659 linkea consistency
17239 test_238() {
17240         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
17241                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
17242                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
17243                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
17244
17245         touch $DIR/$tfile
17246         ln $DIR/$tfile $DIR/$tfile.lnk
17247         touch $DIR/$tfile.new
17248         mv $DIR/$tfile.new $DIR/$tfile
17249         local fid1=$($LFS path2fid $DIR/$tfile)
17250         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
17251         local path1=$($LFS fid2path $FSNAME "$fid1")
17252         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
17253         local path2=$($LFS fid2path $FSNAME "$fid2")
17254         [ $tfile.lnk == $path2 ] ||
17255                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
17256         rm -f $DIR/$tfile*
17257 }
17258 run_test 238 "Verify linkea consistency"
17259
17260 test_239A() { # was test_239
17261         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
17262                 skip "Need MDS version at least 2.5.60"
17263
17264         local list=$(comma_list $(mdts_nodes))
17265
17266         mkdir -p $DIR/$tdir
17267         createmany -o $DIR/$tdir/f- 5000
17268         unlinkmany $DIR/$tdir/f- 5000
17269         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
17270                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
17271         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
17272                         osp.*MDT*.sync_in_flight" | calc_sum)
17273         [ "$changes" -eq 0 ] || error "$changes not synced"
17274 }
17275 run_test 239A "osp_sync test"
17276
17277 test_239a() { #LU-5297
17278         remote_mds_nodsh && skip "remote MDS with nodsh"
17279
17280         touch $DIR/$tfile
17281         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
17282         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
17283         chgrp $RUNAS_GID $DIR/$tfile
17284         wait_delete_completed
17285 }
17286 run_test 239a "process invalid osp sync record correctly"
17287
17288 test_239b() { #LU-5297
17289         remote_mds_nodsh && skip "remote MDS with nodsh"
17290
17291         touch $DIR/$tfile1
17292         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
17293         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
17294         chgrp $RUNAS_GID $DIR/$tfile1
17295         wait_delete_completed
17296         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17297         touch $DIR/$tfile2
17298         chgrp $RUNAS_GID $DIR/$tfile2
17299         wait_delete_completed
17300 }
17301 run_test 239b "process osp sync record with ENOMEM error correctly"
17302
17303 test_240() {
17304         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17305         remote_mds_nodsh && skip "remote MDS with nodsh"
17306
17307         mkdir -p $DIR/$tdir
17308
17309         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
17310                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
17311         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
17312                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
17313
17314         umount_client $MOUNT || error "umount failed"
17315         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
17316         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
17317         mount_client $MOUNT || error "failed to mount client"
17318
17319         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
17320         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
17321 }
17322 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
17323
17324 test_241_bio() {
17325         local count=$1
17326         local bsize=$2
17327
17328         for LOOP in $(seq $count); do
17329                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
17330                 cancel_lru_locks $OSC || true
17331         done
17332 }
17333
17334 test_241_dio() {
17335         local count=$1
17336         local bsize=$2
17337
17338         for LOOP in $(seq $1); do
17339                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
17340                         2>/dev/null
17341         done
17342 }
17343
17344 test_241a() { # was test_241
17345         local bsize=$PAGE_SIZE
17346
17347         (( bsize < 40960 )) && bsize=40960
17348         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17349         ls -la $DIR/$tfile
17350         cancel_lru_locks $OSC
17351         test_241_bio 1000 $bsize &
17352         PID=$!
17353         test_241_dio 1000 $bsize
17354         wait $PID
17355 }
17356 run_test 241a "bio vs dio"
17357
17358 test_241b() {
17359         local bsize=$PAGE_SIZE
17360
17361         (( bsize < 40960 )) && bsize=40960
17362         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17363         ls -la $DIR/$tfile
17364         test_241_dio 1000 $bsize &
17365         PID=$!
17366         test_241_dio 1000 $bsize
17367         wait $PID
17368 }
17369 run_test 241b "dio vs dio"
17370
17371 test_242() {
17372         remote_mds_nodsh && skip "remote MDS with nodsh"
17373
17374         mkdir -p $DIR/$tdir
17375         touch $DIR/$tdir/$tfile
17376
17377         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
17378         do_facet mds1 lctl set_param fail_loc=0x105
17379         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
17380
17381         do_facet mds1 lctl set_param fail_loc=0
17382         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
17383 }
17384 run_test 242 "mdt_readpage failure should not cause directory unreadable"
17385
17386 test_243()
17387 {
17388         test_mkdir $DIR/$tdir
17389         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
17390 }
17391 run_test 243 "various group lock tests"
17392
17393 test_244a()
17394 {
17395         test_mkdir $DIR/$tdir
17396         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
17397         sendfile_grouplock $DIR/$tdir/$tfile || \
17398                 error "sendfile+grouplock failed"
17399         rm -rf $DIR/$tdir
17400 }
17401 run_test 244a "sendfile with group lock tests"
17402
17403 test_244b()
17404 {
17405         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
17406
17407         local threads=50
17408         local size=$((1024*1024))
17409
17410         test_mkdir $DIR/$tdir
17411         for i in $(seq 1 $threads); do
17412                 local file=$DIR/$tdir/file_$((i / 10))
17413                 $MULTIOP $file OG1234w$size_$((i % 3))w$size_$((i % 4))g1234c &
17414                 local pids[$i]=$!
17415         done
17416         for i in $(seq 1 $threads); do
17417                 wait ${pids[$i]}
17418         done
17419 }
17420 run_test 244b "multi-threaded write with group lock"
17421
17422 test_245() {
17423         local flagname="multi_mod_rpcs"
17424         local connect_data_name="max_mod_rpcs"
17425         local out
17426
17427         # check if multiple modify RPCs flag is set
17428         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
17429                 grep "connect_flags:")
17430         echo "$out"
17431
17432         echo "$out" | grep -qw $flagname
17433         if [ $? -ne 0 ]; then
17434                 echo "connect flag $flagname is not set"
17435                 return
17436         fi
17437
17438         # check if multiple modify RPCs data is set
17439         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
17440         echo "$out"
17441
17442         echo "$out" | grep -qw $connect_data_name ||
17443                 error "import should have connect data $connect_data_name"
17444 }
17445 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
17446
17447 cleanup_247() {
17448         local submount=$1
17449
17450         trap 0
17451         umount_client $submount
17452         rmdir $submount
17453 }
17454
17455 test_247a() {
17456         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17457                 grep -q subtree ||
17458                 skip_env "Fileset feature is not supported"
17459
17460         local submount=${MOUNT}_$tdir
17461
17462         mkdir $MOUNT/$tdir
17463         mkdir -p $submount || error "mkdir $submount failed"
17464         FILESET="$FILESET/$tdir" mount_client $submount ||
17465                 error "mount $submount failed"
17466         trap "cleanup_247 $submount" EXIT
17467         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
17468         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
17469                 error "read $MOUNT/$tdir/$tfile failed"
17470         cleanup_247 $submount
17471 }
17472 run_test 247a "mount subdir as fileset"
17473
17474 test_247b() {
17475         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17476                 skip_env "Fileset feature is not supported"
17477
17478         local submount=${MOUNT}_$tdir
17479
17480         rm -rf $MOUNT/$tdir
17481         mkdir -p $submount || error "mkdir $submount failed"
17482         SKIP_FILESET=1
17483         FILESET="$FILESET/$tdir" mount_client $submount &&
17484                 error "mount $submount should fail"
17485         rmdir $submount
17486 }
17487 run_test 247b "mount subdir that dose not exist"
17488
17489 test_247c() {
17490         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17491                 skip_env "Fileset feature is not supported"
17492
17493         local submount=${MOUNT}_$tdir
17494
17495         mkdir -p $MOUNT/$tdir/dir1
17496         mkdir -p $submount || error "mkdir $submount failed"
17497         trap "cleanup_247 $submount" EXIT
17498         FILESET="$FILESET/$tdir" mount_client $submount ||
17499                 error "mount $submount failed"
17500         local fid=$($LFS path2fid $MOUNT/)
17501         $LFS fid2path $submount $fid && error "fid2path should fail"
17502         cleanup_247 $submount
17503 }
17504 run_test 247c "running fid2path outside root"
17505
17506 test_247d() {
17507         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17508                 skip "Fileset feature is not supported"
17509
17510         local submount=${MOUNT}_$tdir
17511
17512         mkdir -p $MOUNT/$tdir/dir1
17513         mkdir -p $submount || error "mkdir $submount failed"
17514         FILESET="$FILESET/$tdir" mount_client $submount ||
17515                 error "mount $submount failed"
17516         trap "cleanup_247 $submount" EXIT
17517         local fid=$($LFS path2fid $submount/dir1)
17518         $LFS fid2path $submount $fid || error "fid2path should succeed"
17519         cleanup_247 $submount
17520 }
17521 run_test 247d "running fid2path inside root"
17522
17523 # LU-8037
17524 test_247e() {
17525         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17526                 grep -q subtree ||
17527                 skip "Fileset feature is not supported"
17528
17529         local submount=${MOUNT}_$tdir
17530
17531         mkdir $MOUNT/$tdir
17532         mkdir -p $submount || error "mkdir $submount failed"
17533         FILESET="$FILESET/.." mount_client $submount &&
17534                 error "mount $submount should fail"
17535         rmdir $submount
17536 }
17537 run_test 247e "mount .. as fileset"
17538
17539 test_248a() {
17540         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
17541         [ -z "$fast_read_sav" ] && skip "no fast read support"
17542
17543         # create a large file for fast read verification
17544         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
17545
17546         # make sure the file is created correctly
17547         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
17548                 { rm -f $DIR/$tfile; skip "file creation error"; }
17549
17550         echo "Test 1: verify that fast read is 4 times faster on cache read"
17551
17552         # small read with fast read enabled
17553         $LCTL set_param -n llite.*.fast_read=1
17554         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17555                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17556                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17557         # small read with fast read disabled
17558         $LCTL set_param -n llite.*.fast_read=0
17559         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17560                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17561                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17562
17563         # verify that fast read is 4 times faster for cache read
17564         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
17565                 error_not_in_vm "fast read was not 4 times faster: " \
17566                            "$t_fast vs $t_slow"
17567
17568         echo "Test 2: verify the performance between big and small read"
17569         $LCTL set_param -n llite.*.fast_read=1
17570
17571         # 1k non-cache read
17572         cancel_lru_locks osc
17573         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17574                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17575                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17576
17577         # 1M non-cache read
17578         cancel_lru_locks osc
17579         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17580                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17581                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17582
17583         # verify that big IO is not 4 times faster than small IO
17584         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
17585                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
17586
17587         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
17588         rm -f $DIR/$tfile
17589 }
17590 run_test 248a "fast read verification"
17591
17592 test_248b() {
17593         # Default short_io_bytes=16384, try both smaller and larger sizes.
17594         # Lustre O_DIRECT read and write needs to be a multiple of PAGE_SIZE.
17595         # 6017024 = 2^12*13*113 = 47008*128 = 11752*512 = 4096*1469 = 53248*113
17596         echo "bs=53248 count=113 normal buffered write"
17597         dd if=/dev/urandom of=$TMP/$tfile.0 bs=53248 count=113 ||
17598                 error "dd of initial data file failed"
17599         stack_trap "rm -f $DIR/$tfile.[0-3] $TMP/$tfile.[0-3]" EXIT
17600
17601         echo "bs=47008 count=128 oflag=dsync normal write $tfile.0"
17602         dd if=$TMP/$tfile.0 of=$DIR/$tfile.0 bs=47008 count=128 oflag=dsync ||
17603                 error "dd with sync normal writes failed"
17604         cmp $TMP/$tfile.0 $DIR/$tfile.0 || error "compare $DIR/$tfile.0 failed"
17605
17606         echo "bs=11752 count=512 oflag=dsync small write $tfile.1"
17607         dd if=$TMP/$tfile.0 of=$DIR/$tfile.1 bs=11752 count=512 oflag=dsync ||
17608                 error "dd with sync small writes failed"
17609         cmp $TMP/$tfile.0 $DIR/$tfile.1 || error "compare $DIR/$tfile.1 failed"
17610
17611         cancel_lru_locks osc
17612
17613         # calculate the small O_DIRECT size and count for the client PAGE_SIZE
17614         local num=$((13 * 113 / (PAGE_SIZE / 4096)))
17615         echo "bs=$PAGE_SIZE count=$num iflag=direct small read $tfile.1"
17616         dd if=$DIR/$tfile.1 of=$TMP/$tfile.1 bs=$PAGE_SIZE count=$num \
17617                 iflag=direct || error "dd with O_DIRECT small read failed"
17618         # adjust bytes checked to handle larger PAGE_SIZE for ARM/PPC
17619         cmp --bytes=$((PAGE_SIZE * num)) $TMP/$tfile.0 $TMP/$tfile.1 ||
17620                 error "compare $TMP/$tfile.1 failed"
17621
17622         local save=$($LCTL get_param -n osc.*OST000*.short_io_bytes | head -n 1)
17623         stack_trap "$LCTL set_param osc.$FSNAME-*.short_io_bytes=$save" EXIT
17624
17625         # just to see what the maximum tunable value is, and test parsing
17626         echo "test invalid parameter 2MB"
17627         $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=2M &&
17628                 error "too-large short_io_bytes allowed"
17629         echo "test maximum parameter 512KB"
17630         # if we can set a larger short_io_bytes, run test regardless of version
17631         if ! $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=512K; then
17632                 # older clients may not allow setting it this large, that's OK
17633                 [ $CLIENT_VERSION -ge $(version_code 2.13.50) ] ||
17634                         skip "Need at least client version 2.13.50"
17635                 error "medium short_io_bytes failed"
17636         fi
17637         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
17638         size=$($LCTL get_param -n osc.$FSNAME-OST0000*.short_io_bytes)
17639
17640         echo "test large parameter 64KB"
17641         $LCTL set_param osc.$FSNAME-*.short_io_bytes=65536
17642         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
17643
17644         echo "bs=47008 count=128 oflag=dsync large write $tfile.2"
17645         dd if=$TMP/$tfile.0 of=$DIR/$tfile.2 bs=47008 count=128 oflag=dsync ||
17646                 error "dd with sync large writes failed"
17647         cmp $TMP/$tfile.0 $DIR/$tfile.2 || error "compare $DIR/$tfile.2 failed"
17648
17649         # calculate the large O_DIRECT size and count for the client PAGE_SIZE
17650         local size=$(((4096 * 13 + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE))
17651         num=$((113 * 4096 / PAGE_SIZE))
17652         echo "bs=$size count=$num oflag=direct large write $tfile.3"
17653         dd if=$TMP/$tfile.0 of=$DIR/$tfile.3 bs=$size count=$num oflag=direct ||
17654                 error "dd with O_DIRECT large writes failed"
17655         cmp --bytes=$((size * num)) $TMP/$tfile.0 $DIR/$tfile.3 ||
17656                 error "compare $DIR/$tfile.3 failed"
17657
17658         cancel_lru_locks osc
17659
17660         echo "bs=$size count=$num iflag=direct large read $tfile.2"
17661         dd if=$DIR/$tfile.2 of=$TMP/$tfile.2 bs=$size count=$num iflag=direct ||
17662                 error "dd with O_DIRECT large read failed"
17663         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.2 ||
17664                 error "compare $TMP/$tfile.2 failed"
17665
17666         echo "bs=$size count=$num iflag=direct large read $tfile.3"
17667         dd if=$DIR/$tfile.3 of=$TMP/$tfile.3 bs=$size count=$num iflag=direct ||
17668                 error "dd with O_DIRECT large read failed"
17669         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.3 ||
17670                 error "compare $TMP/$tfile.3 failed"
17671 }
17672 run_test 248b "test short_io read and write for both small and large sizes"
17673
17674 test_249() { # LU-7890
17675         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
17676                 skip "Need at least version 2.8.54"
17677
17678         rm -f $DIR/$tfile
17679         $LFS setstripe -c 1 $DIR/$tfile
17680         # Offset 2T == 4k * 512M
17681         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
17682                 error "dd to 2T offset failed"
17683 }
17684 run_test 249 "Write above 2T file size"
17685
17686 test_250() {
17687         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
17688          && skip "no 16TB file size limit on ZFS"
17689
17690         $LFS setstripe -c 1 $DIR/$tfile
17691         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
17692         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
17693         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
17694         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
17695                 conv=notrunc,fsync && error "append succeeded"
17696         return 0
17697 }
17698 run_test 250 "Write above 16T limit"
17699
17700 test_251() {
17701         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
17702
17703         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
17704         #Skip once - writing the first stripe will succeed
17705         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17706         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
17707                 error "short write happened"
17708
17709         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17710         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
17711                 error "short read happened"
17712
17713         rm -f $DIR/$tfile
17714 }
17715 run_test 251 "Handling short read and write correctly"
17716
17717 test_252() {
17718         remote_mds_nodsh && skip "remote MDS with nodsh"
17719         remote_ost_nodsh && skip "remote OST with nodsh"
17720         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
17721                 skip_env "ldiskfs only test"
17722         fi
17723
17724         local tgt
17725         local dev
17726         local out
17727         local uuid
17728         local num
17729         local gen
17730
17731         # check lr_reader on OST0000
17732         tgt=ost1
17733         dev=$(facet_device $tgt)
17734         out=$(do_facet $tgt $LR_READER $dev)
17735         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17736         echo "$out"
17737         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
17738         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
17739                 error "Invalid uuid returned by $LR_READER on target $tgt"
17740         echo -e "uuid returned by $LR_READER is '$uuid'\n"
17741
17742         # check lr_reader -c on MDT0000
17743         tgt=mds1
17744         dev=$(facet_device $tgt)
17745         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
17746                 skip "$LR_READER does not support additional options"
17747         fi
17748         out=$(do_facet $tgt $LR_READER -c $dev)
17749         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17750         echo "$out"
17751         num=$(echo "$out" | grep -c "mdtlov")
17752         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
17753                 error "Invalid number of mdtlov clients returned by $LR_READER"
17754         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
17755
17756         # check lr_reader -cr on MDT0000
17757         out=$(do_facet $tgt $LR_READER -cr $dev)
17758         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17759         echo "$out"
17760         echo "$out" | grep -q "^reply_data:$" ||
17761                 error "$LR_READER should have returned 'reply_data' section"
17762         num=$(echo "$out" | grep -c "client_generation")
17763         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
17764 }
17765 run_test 252 "check lr_reader tool"
17766
17767 test_253() {
17768         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17769         remote_mds_nodsh && skip "remote MDS with nodsh"
17770         remote_mgs_nodsh && skip "remote MGS with nodsh"
17771
17772         local ostidx=0
17773         local rc=0
17774         local ost_name=$(ostname_from_index $ostidx)
17775
17776         # on the mdt's osc
17777         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
17778         do_facet $SINGLEMDS $LCTL get_param -n \
17779                 osp.$mdtosc_proc1.reserved_mb_high ||
17780                 skip  "remote MDS does not support reserved_mb_high"
17781
17782         rm -rf $DIR/$tdir
17783         wait_mds_ost_sync
17784         wait_delete_completed
17785         mkdir $DIR/$tdir
17786
17787         pool_add $TESTNAME || error "Pool creation failed"
17788         pool_add_targets $TESTNAME 0 || error "Pool add targets failed"
17789
17790         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
17791                 error "Setstripe failed"
17792
17793         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M count=10
17794
17795         local wms=$(ost_watermarks_set_enospc $tfile $ostidx |
17796                     grep "watermarks")
17797         stack_trap "ost_watermarks_clear_enospc $tfile $ostidx $wms" EXIT
17798
17799         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17800                         osp.$mdtosc_proc1.prealloc_status)
17801         echo "prealloc_status $oa_status"
17802
17803         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1M count=1 &&
17804                 error "File creation should fail"
17805
17806         #object allocation was stopped, but we still able to append files
17807         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M seek=6 count=5 \
17808                 oflag=append || error "Append failed"
17809
17810         rm -f $DIR/$tdir/$tfile.0
17811
17812         # For this test, we want to delete the files we created to go out of
17813         # space but leave the watermark, so we remain nearly out of space
17814         ost_watermarks_enospc_delete_files $tfile $ostidx
17815
17816         wait_delete_completed
17817
17818         sleep_maxage
17819
17820         for i in $(seq 10 12); do
17821                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$i bs=1M count=1 \
17822                         2>/dev/null || error "File creation failed after rm"
17823         done
17824
17825         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17826                         osp.$mdtosc_proc1.prealloc_status)
17827         echo "prealloc_status $oa_status"
17828
17829         if (( oa_status != 0 )); then
17830                 error "Object allocation still disable after rm"
17831         fi
17832 }
17833 run_test 253 "Check object allocation limit"
17834
17835 test_254() {
17836         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17837         remote_mds_nodsh && skip "remote MDS with nodsh"
17838         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
17839                 skip "MDS does not support changelog_size"
17840
17841         local cl_user
17842         local MDT0=$(facet_svc $SINGLEMDS)
17843
17844         changelog_register || error "changelog_register failed"
17845
17846         changelog_clear 0 || error "changelog_clear failed"
17847
17848         local size1=$(do_facet $SINGLEMDS \
17849                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17850         echo "Changelog size $size1"
17851
17852         rm -rf $DIR/$tdir
17853         $LFS mkdir -i 0 $DIR/$tdir
17854         # change something
17855         mkdir -p $DIR/$tdir/pics/2008/zachy
17856         touch $DIR/$tdir/pics/2008/zachy/timestamp
17857         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
17858         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
17859         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
17860         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
17861         rm $DIR/$tdir/pics/desktop.jpg
17862
17863         local size2=$(do_facet $SINGLEMDS \
17864                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17865         echo "Changelog size after work $size2"
17866
17867         (( $size2 > $size1 )) ||
17868                 error "new Changelog size=$size2 less than old size=$size1"
17869 }
17870 run_test 254 "Check changelog size"
17871
17872 ladvise_no_type()
17873 {
17874         local type=$1
17875         local file=$2
17876
17877         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
17878                 awk -F: '{print $2}' | grep $type > /dev/null
17879         if [ $? -ne 0 ]; then
17880                 return 0
17881         fi
17882         return 1
17883 }
17884
17885 ladvise_no_ioctl()
17886 {
17887         local file=$1
17888
17889         lfs ladvise -a willread $file > /dev/null 2>&1
17890         if [ $? -eq 0 ]; then
17891                 return 1
17892         fi
17893
17894         lfs ladvise -a willread $file 2>&1 |
17895                 grep "Inappropriate ioctl for device" > /dev/null
17896         if [ $? -eq 0 ]; then
17897                 return 0
17898         fi
17899         return 1
17900 }
17901
17902 percent() {
17903         bc <<<"scale=2; ($1 - $2) * 100 / $2"
17904 }
17905
17906 # run a random read IO workload
17907 # usage: random_read_iops <filename> <filesize> <iosize>
17908 random_read_iops() {
17909         local file=$1
17910         local fsize=$2
17911         local iosize=${3:-4096}
17912
17913         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
17914                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
17915 }
17916
17917 drop_file_oss_cache() {
17918         local file="$1"
17919         local nodes="$2"
17920
17921         $LFS ladvise -a dontneed $file 2>/dev/null ||
17922                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
17923 }
17924
17925 ladvise_willread_performance()
17926 {
17927         local repeat=10
17928         local average_origin=0
17929         local average_cache=0
17930         local average_ladvise=0
17931
17932         for ((i = 1; i <= $repeat; i++)); do
17933                 echo "Iter $i/$repeat: reading without willread hint"
17934                 cancel_lru_locks osc
17935                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
17936                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
17937                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
17938                 average_origin=$(bc <<<"$average_origin + $speed_origin")
17939
17940                 cancel_lru_locks osc
17941                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
17942                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
17943                 average_cache=$(bc <<<"$average_cache + $speed_cache")
17944
17945                 cancel_lru_locks osc
17946                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
17947                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
17948                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
17949                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
17950                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
17951         done
17952         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
17953         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
17954         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
17955
17956         speedup_cache=$(percent $average_cache $average_origin)
17957         speedup_ladvise=$(percent $average_ladvise $average_origin)
17958
17959         echo "Average uncached read: $average_origin"
17960         echo "Average speedup with OSS cached read: " \
17961                 "$average_cache = +$speedup_cache%"
17962         echo "Average speedup with ladvise willread: " \
17963                 "$average_ladvise = +$speedup_ladvise%"
17964
17965         local lowest_speedup=20
17966         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
17967                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
17968                         "got $average_cache%. Skipping ladvise willread check."
17969                 return 0
17970         fi
17971
17972         # the test won't work on ZFS until it supports 'ladvise dontneed', but
17973         # it is still good to run until then to exercise 'ladvise willread'
17974         ! $LFS ladvise -a dontneed $DIR/$tfile &&
17975                 [ "$ost1_FSTYPE" = "zfs" ] &&
17976                 echo "osd-zfs does not support dontneed or drop_caches" &&
17977                 return 0
17978
17979         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
17980         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
17981                 error_not_in_vm "Speedup with willread is less than " \
17982                         "$lowest_speedup%, got $average_ladvise%"
17983 }
17984
17985 test_255a() {
17986         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
17987                 skip "lustre < 2.8.54 does not support ladvise "
17988         remote_ost_nodsh && skip "remote OST with nodsh"
17989
17990         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
17991
17992         ladvise_no_type willread $DIR/$tfile &&
17993                 skip "willread ladvise is not supported"
17994
17995         ladvise_no_ioctl $DIR/$tfile &&
17996                 skip "ladvise ioctl is not supported"
17997
17998         local size_mb=100
17999         local size=$((size_mb * 1048576))
18000         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
18001                 error "dd to $DIR/$tfile failed"
18002
18003         lfs ladvise -a willread $DIR/$tfile ||
18004                 error "Ladvise failed with no range argument"
18005
18006         lfs ladvise -a willread -s 0 $DIR/$tfile ||
18007                 error "Ladvise failed with no -l or -e argument"
18008
18009         lfs ladvise -a willread -e 1 $DIR/$tfile ||
18010                 error "Ladvise failed with only -e argument"
18011
18012         lfs ladvise -a willread -l 1 $DIR/$tfile ||
18013                 error "Ladvise failed with only -l argument"
18014
18015         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
18016                 error "End offset should not be smaller than start offset"
18017
18018         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
18019                 error "End offset should not be equal to start offset"
18020
18021         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
18022                 error "Ladvise failed with overflowing -s argument"
18023
18024         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
18025                 error "Ladvise failed with overflowing -e argument"
18026
18027         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
18028                 error "Ladvise failed with overflowing -l argument"
18029
18030         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
18031                 error "Ladvise succeeded with conflicting -l and -e arguments"
18032
18033         echo "Synchronous ladvise should wait"
18034         local delay=4
18035 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
18036         do_nodes $(comma_list $(osts_nodes)) \
18037                 $LCTL set_param fail_val=$delay fail_loc=0x237
18038
18039         local start_ts=$SECONDS
18040         lfs ladvise -a willread $DIR/$tfile ||
18041                 error "Ladvise failed with no range argument"
18042         local end_ts=$SECONDS
18043         local inteval_ts=$((end_ts - start_ts))
18044
18045         if [ $inteval_ts -lt $(($delay - 1)) ]; then
18046                 error "Synchronous advice didn't wait reply"
18047         fi
18048
18049         echo "Asynchronous ladvise shouldn't wait"
18050         local start_ts=$SECONDS
18051         lfs ladvise -a willread -b $DIR/$tfile ||
18052                 error "Ladvise failed with no range argument"
18053         local end_ts=$SECONDS
18054         local inteval_ts=$((end_ts - start_ts))
18055
18056         if [ $inteval_ts -gt $(($delay / 2)) ]; then
18057                 error "Asynchronous advice blocked"
18058         fi
18059
18060         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
18061         ladvise_willread_performance
18062 }
18063 run_test 255a "check 'lfs ladvise -a willread'"
18064
18065 facet_meminfo() {
18066         local facet=$1
18067         local info=$2
18068
18069         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
18070 }
18071
18072 test_255b() {
18073         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
18074                 skip "lustre < 2.8.54 does not support ladvise "
18075         remote_ost_nodsh && skip "remote OST with nodsh"
18076
18077         lfs setstripe -c 1 -i 0 $DIR/$tfile
18078
18079         ladvise_no_type dontneed $DIR/$tfile &&
18080                 skip "dontneed ladvise is not supported"
18081
18082         ladvise_no_ioctl $DIR/$tfile &&
18083                 skip "ladvise ioctl is not supported"
18084
18085         ! $LFS ladvise -a dontneed $DIR/$tfile &&
18086                 [ "$ost1_FSTYPE" = "zfs" ] &&
18087                 skip "zfs-osd does not support 'ladvise dontneed'"
18088
18089         local size_mb=100
18090         local size=$((size_mb * 1048576))
18091         # In order to prevent disturbance of other processes, only check 3/4
18092         # of the memory usage
18093         local kibibytes=$((size_mb * 1024 * 3 / 4))
18094
18095         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
18096                 error "dd to $DIR/$tfile failed"
18097
18098         #force write to complete before dropping OST cache & checking memory
18099         sync
18100
18101         local total=$(facet_meminfo ost1 MemTotal)
18102         echo "Total memory: $total KiB"
18103
18104         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
18105         local before_read=$(facet_meminfo ost1 Cached)
18106         echo "Cache used before read: $before_read KiB"
18107
18108         lfs ladvise -a willread $DIR/$tfile ||
18109                 error "Ladvise willread failed"
18110         local after_read=$(facet_meminfo ost1 Cached)
18111         echo "Cache used after read: $after_read KiB"
18112
18113         lfs ladvise -a dontneed $DIR/$tfile ||
18114                 error "Ladvise dontneed again failed"
18115         local no_read=$(facet_meminfo ost1 Cached)
18116         echo "Cache used after dontneed ladvise: $no_read KiB"
18117
18118         if [ $total -lt $((before_read + kibibytes)) ]; then
18119                 echo "Memory is too small, abort checking"
18120                 return 0
18121         fi
18122
18123         if [ $((before_read + kibibytes)) -gt $after_read ]; then
18124                 error "Ladvise willread should use more memory" \
18125                         "than $kibibytes KiB"
18126         fi
18127
18128         if [ $((no_read + kibibytes)) -gt $after_read ]; then
18129                 error "Ladvise dontneed should release more memory" \
18130                         "than $kibibytes KiB"
18131         fi
18132 }
18133 run_test 255b "check 'lfs ladvise -a dontneed'"
18134
18135 test_255c() {
18136         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
18137                 skip "lustre < 2.10.50 does not support lockahead"
18138
18139         local count
18140         local new_count
18141         local difference
18142         local i
18143         local rc
18144
18145         test_mkdir -p $DIR/$tdir
18146         $LFS setstripe -i 0 -c 1 $DIR/$tdir
18147
18148         #test 10 returns only success/failure
18149         i=10
18150         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18151         rc=$?
18152         if [ $rc -eq 255 ]; then
18153                 error "Ladvise test${i} failed, ${rc}"
18154         fi
18155
18156         #test 11 counts lock enqueue requests, all others count new locks
18157         i=11
18158         count=$(do_facet ost1 \
18159                 $LCTL get_param -n ost.OSS.ost.stats)
18160         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
18161
18162         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18163         rc=$?
18164         if [ $rc -eq 255 ]; then
18165                 error "Ladvise test${i} failed, ${rc}"
18166         fi
18167
18168         new_count=$(do_facet ost1 \
18169                 $LCTL get_param -n ost.OSS.ost.stats)
18170         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
18171                    awk '{ print $2 }')
18172
18173         difference="$((new_count - count))"
18174         if [ $difference -ne $rc ]; then
18175                 error "Ladvise test${i}, bad enqueue count, returned " \
18176                       "${rc}, actual ${difference}"
18177         fi
18178
18179         for i in $(seq 12 21); do
18180                 # If we do not do this, we run the risk of having too many
18181                 # locks and starting lock cancellation while we are checking
18182                 # lock counts.
18183                 cancel_lru_locks osc
18184
18185                 count=$($LCTL get_param -n \
18186                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
18187
18188                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
18189                 rc=$?
18190                 if [ $rc -eq 255 ]; then
18191                         error "Ladvise test ${i} failed, ${rc}"
18192                 fi
18193
18194                 new_count=$($LCTL get_param -n \
18195                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
18196                 difference="$((new_count - count))"
18197
18198                 # Test 15 output is divided by 100 to map down to valid return
18199                 if [ $i -eq 15 ]; then
18200                         rc="$((rc * 100))"
18201                 fi
18202
18203                 if [ $difference -ne $rc ]; then
18204                         error "Ladvise test ${i}, bad lock count, returned " \
18205                               "${rc}, actual ${difference}"
18206                 fi
18207         done
18208
18209         #test 22 returns only success/failure
18210         i=22
18211         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18212         rc=$?
18213         if [ $rc -eq 255 ]; then
18214                 error "Ladvise test${i} failed, ${rc}"
18215         fi
18216 }
18217 run_test 255c "suite of ladvise lockahead tests"
18218
18219 test_256() {
18220         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18221         remote_mds_nodsh && skip "remote MDS with nodsh"
18222         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18223         changelog_users $SINGLEMDS | grep "^cl" &&
18224                 skip "active changelog user"
18225
18226         local cl_user
18227         local cat_sl
18228         local mdt_dev
18229
18230         mdt_dev=$(mdsdevname 1)
18231         echo $mdt_dev
18232
18233         changelog_register || error "changelog_register failed"
18234
18235         rm -rf $DIR/$tdir
18236         mkdir -p $DIR/$tdir
18237
18238         changelog_clear 0 || error "changelog_clear failed"
18239
18240         # change something
18241         touch $DIR/$tdir/{1..10}
18242
18243         # stop the MDT
18244         stop $SINGLEMDS || error "Fail to stop MDT"
18245
18246         # remount the MDT
18247
18248         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
18249
18250         #after mount new plainllog is used
18251         touch $DIR/$tdir/{11..19}
18252         local tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
18253         stack_trap "rm -f $tmpfile"
18254         cat_sl=$(do_facet $SINGLEMDS "sync; \
18255                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18256                  llog_reader $tmpfile | grep -c type=1064553b")
18257         do_facet $SINGLEMDS llog_reader $tmpfile
18258
18259         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
18260
18261         changelog_clear 0 || error "changelog_clear failed"
18262
18263         cat_sl=$(do_facet $SINGLEMDS "sync; \
18264                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18265                  llog_reader $tmpfile | grep -c type=1064553b")
18266
18267         if (( cat_sl == 2 )); then
18268                 error "Empty plain llog was not deleted from changelog catalog"
18269         elif (( cat_sl != 1 )); then
18270                 error "Active plain llog shouldn't be deleted from catalog"
18271         fi
18272 }
18273 run_test 256 "Check llog delete for empty and not full state"
18274
18275 test_257() {
18276         remote_mds_nodsh && skip "remote MDS with nodsh"
18277         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
18278                 skip "Need MDS version at least 2.8.55"
18279
18280         test_mkdir $DIR/$tdir
18281
18282         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
18283                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
18284         stat $DIR/$tdir
18285
18286 #define OBD_FAIL_MDS_XATTR_REP                  0x161
18287         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18288         local facet=mds$((mdtidx + 1))
18289         set_nodes_failloc $(facet_active_host $facet) 0x80000161
18290         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
18291
18292         stop $facet || error "stop MDS failed"
18293         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
18294                 error "start MDS fail"
18295         wait_recovery_complete $facet
18296 }
18297 run_test 257 "xattr locks are not lost"
18298
18299 # Verify we take the i_mutex when security requires it
18300 test_258a() {
18301 #define OBD_FAIL_IMUTEX_SEC 0x141c
18302         $LCTL set_param fail_loc=0x141c
18303         touch $DIR/$tfile
18304         chmod u+s $DIR/$tfile
18305         chmod a+rwx $DIR/$tfile
18306         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18307         RC=$?
18308         if [ $RC -ne 0 ]; then
18309                 error "error, failed to take i_mutex, rc=$?"
18310         fi
18311         rm -f $DIR/$tfile
18312 }
18313 run_test 258a "verify i_mutex security behavior when suid attributes is set"
18314
18315 # Verify we do NOT take the i_mutex in the normal case
18316 test_258b() {
18317 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
18318         $LCTL set_param fail_loc=0x141d
18319         touch $DIR/$tfile
18320         chmod a+rwx $DIR
18321         chmod a+rw $DIR/$tfile
18322         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18323         RC=$?
18324         if [ $RC -ne 0 ]; then
18325                 error "error, took i_mutex unnecessarily, rc=$?"
18326         fi
18327         rm -f $DIR/$tfile
18328
18329 }
18330 run_test 258b "verify i_mutex security behavior"
18331
18332 test_259() {
18333         local file=$DIR/$tfile
18334         local before
18335         local after
18336
18337         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18338
18339         stack_trap "rm -f $file" EXIT
18340
18341         wait_delete_completed
18342         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18343         echo "before: $before"
18344
18345         $LFS setstripe -i 0 -c 1 $file
18346         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
18347         sync_all_data
18348         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18349         echo "after write: $after"
18350
18351 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
18352         do_facet ost1 $LCTL set_param fail_loc=0x2301
18353         $TRUNCATE $file 0
18354         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18355         echo "after truncate: $after"
18356
18357         stop ost1
18358         do_facet ost1 $LCTL set_param fail_loc=0
18359         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
18360         sleep 2
18361         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18362         echo "after restart: $after"
18363         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
18364                 error "missing truncate?"
18365
18366         return 0
18367 }
18368 run_test 259 "crash at delayed truncate"
18369
18370 test_260() {
18371 #define OBD_FAIL_MDC_CLOSE               0x806
18372         $LCTL set_param fail_loc=0x80000806
18373         touch $DIR/$tfile
18374
18375 }
18376 run_test 260 "Check mdc_close fail"
18377
18378 ### Data-on-MDT sanity tests ###
18379 test_270a() {
18380         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18381                 skip "Need MDS version at least 2.10.55 for DoM"
18382
18383         # create DoM file
18384         local dom=$DIR/$tdir/dom_file
18385         local tmp=$DIR/$tdir/tmp_file
18386
18387         mkdir -p $DIR/$tdir
18388
18389         # basic checks for DoM component creation
18390         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
18391                 error "Can set MDT layout to non-first entry"
18392
18393         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
18394                 error "Can define multiple entries as MDT layout"
18395
18396         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
18397
18398         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
18399         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
18400         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
18401
18402         local mdtidx=$($LFS getstripe -m $dom)
18403         local mdtname=MDT$(printf %04x $mdtidx)
18404         local facet=mds$((mdtidx + 1))
18405         local space_check=1
18406
18407         # Skip free space checks with ZFS
18408         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
18409
18410         # write
18411         sync
18412         local size_tmp=$((65536 * 3))
18413         local mdtfree1=$(do_facet $facet \
18414                          lctl get_param -n osd*.*$mdtname.kbytesfree)
18415
18416         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18417         # check also direct IO along write
18418         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
18419         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18420         sync
18421         cmp $tmp $dom || error "file data is different"
18422         [ $(stat -c%s $dom) == $size_tmp ] ||
18423                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18424         if [ $space_check == 1 ]; then
18425                 local mdtfree2=$(do_facet $facet \
18426                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
18427
18428                 # increase in usage from by $size_tmp
18429                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18430                         error "MDT free space wrong after write: " \
18431                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18432         fi
18433
18434         # truncate
18435         local size_dom=10000
18436
18437         $TRUNCATE $dom $size_dom
18438         [ $(stat -c%s $dom) == $size_dom ] ||
18439                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
18440         if [ $space_check == 1 ]; then
18441                 mdtfree1=$(do_facet $facet \
18442                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18443                 # decrease in usage from $size_tmp to new $size_dom
18444                 [ $(($mdtfree1 - $mdtfree2)) -ge \
18445                   $(((size_tmp - size_dom) / 1024)) ] ||
18446                         error "MDT free space is wrong after truncate: " \
18447                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
18448         fi
18449
18450         # append
18451         cat $tmp >> $dom
18452         sync
18453         size_dom=$((size_dom + size_tmp))
18454         [ $(stat -c%s $dom) == $size_dom ] ||
18455                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
18456         if [ $space_check == 1 ]; then
18457                 mdtfree2=$(do_facet $facet \
18458                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18459                 # increase in usage by $size_tmp from previous
18460                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18461                         error "MDT free space is wrong after append: " \
18462                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18463         fi
18464
18465         # delete
18466         rm $dom
18467         if [ $space_check == 1 ]; then
18468                 mdtfree1=$(do_facet $facet \
18469                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18470                 # decrease in usage by $size_dom from previous
18471                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
18472                         error "MDT free space is wrong after removal: " \
18473                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
18474         fi
18475
18476         # combined striping
18477         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
18478                 error "Can't create DoM + OST striping"
18479
18480         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
18481         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18482         # check also direct IO along write
18483         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18484         sync
18485         cmp $tmp $dom || error "file data is different"
18486         [ $(stat -c%s $dom) == $size_tmp ] ||
18487                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18488         rm $dom $tmp
18489
18490         return 0
18491 }
18492 run_test 270a "DoM: basic functionality tests"
18493
18494 test_270b() {
18495         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18496                 skip "Need MDS version at least 2.10.55"
18497
18498         local dom=$DIR/$tdir/dom_file
18499         local max_size=1048576
18500
18501         mkdir -p $DIR/$tdir
18502         $LFS setstripe -E $max_size -L mdt $dom
18503
18504         # truncate over the limit
18505         $TRUNCATE $dom $(($max_size + 1)) &&
18506                 error "successful truncate over the maximum size"
18507         # write over the limit
18508         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
18509                 error "successful write over the maximum size"
18510         # append over the limit
18511         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
18512         echo "12345" >> $dom && error "successful append over the maximum size"
18513         rm $dom
18514
18515         return 0
18516 }
18517 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
18518
18519 test_270c() {
18520         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18521                 skip "Need MDS version at least 2.10.55"
18522
18523         mkdir -p $DIR/$tdir
18524         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18525
18526         # check files inherit DoM EA
18527         touch $DIR/$tdir/first
18528         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
18529                 error "bad pattern"
18530         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
18531                 error "bad stripe count"
18532         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
18533                 error "bad stripe size"
18534
18535         # check directory inherits DoM EA and uses it as default
18536         mkdir $DIR/$tdir/subdir
18537         touch $DIR/$tdir/subdir/second
18538         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
18539                 error "bad pattern in sub-directory"
18540         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
18541                 error "bad stripe count in sub-directory"
18542         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
18543                 error "bad stripe size in sub-directory"
18544         return 0
18545 }
18546 run_test 270c "DoM: DoM EA inheritance tests"
18547
18548 test_270d() {
18549         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18550                 skip "Need MDS version at least 2.10.55"
18551
18552         mkdir -p $DIR/$tdir
18553         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18554
18555         # inherit default DoM striping
18556         mkdir $DIR/$tdir/subdir
18557         touch $DIR/$tdir/subdir/f1
18558
18559         # change default directory striping
18560         $LFS setstripe -c 1 $DIR/$tdir/subdir
18561         touch $DIR/$tdir/subdir/f2
18562         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
18563                 error "wrong default striping in file 2"
18564         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
18565                 error "bad pattern in file 2"
18566         return 0
18567 }
18568 run_test 270d "DoM: change striping from DoM to RAID0"
18569
18570 test_270e() {
18571         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18572                 skip "Need MDS version at least 2.10.55"
18573
18574         mkdir -p $DIR/$tdir/dom
18575         mkdir -p $DIR/$tdir/norm
18576         DOMFILES=20
18577         NORMFILES=10
18578         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
18579         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
18580
18581         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
18582         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
18583
18584         # find DoM files by layout
18585         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
18586         [ $NUM -eq  $DOMFILES ] ||
18587                 error "lfs find -L: found $NUM, expected $DOMFILES"
18588         echo "Test 1: lfs find 20 DOM files by layout: OK"
18589
18590         # there should be 1 dir with default DOM striping
18591         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
18592         [ $NUM -eq  1 ] ||
18593                 error "lfs find -L: found $NUM, expected 1 dir"
18594         echo "Test 2: lfs find 1 DOM dir by layout: OK"
18595
18596         # find DoM files by stripe size
18597         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
18598         [ $NUM -eq  $DOMFILES ] ||
18599                 error "lfs find -S: found $NUM, expected $DOMFILES"
18600         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
18601
18602         # find files by stripe offset except DoM files
18603         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
18604         [ $NUM -eq  $NORMFILES ] ||
18605                 error "lfs find -i: found $NUM, expected $NORMFILES"
18606         echo "Test 5: lfs find no DOM files by stripe index: OK"
18607         return 0
18608 }
18609 run_test 270e "DoM: lfs find with DoM files test"
18610
18611 test_270f() {
18612         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18613                 skip "Need MDS version at least 2.10.55"
18614
18615         local mdtname=${FSNAME}-MDT0000-mdtlov
18616         local dom=$DIR/$tdir/dom_file
18617         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
18618                                                 lod.$mdtname.dom_stripesize)
18619         local dom_limit=131072
18620
18621         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
18622         local dom_current=$(do_facet mds1 $LCTL get_param -n \
18623                                                 lod.$mdtname.dom_stripesize)
18624         [ ${dom_limit} -eq ${dom_current} ] ||
18625                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
18626
18627         $LFS mkdir -i 0 -c 1 $DIR/$tdir
18628         $LFS setstripe -d $DIR/$tdir
18629         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
18630                 error "Can't set directory default striping"
18631
18632         # exceed maximum stripe size
18633         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18634                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
18635         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
18636                 error "Able to create DoM component size more than LOD limit"
18637
18638         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
18639         dom_current=$(do_facet mds1 $LCTL get_param -n \
18640                                                 lod.$mdtname.dom_stripesize)
18641         [ 0 -eq ${dom_current} ] ||
18642                 error "Can't set zero DoM stripe limit"
18643         rm $dom
18644
18645         # attempt to create DoM file on server with disabled DoM should
18646         # remove DoM entry from layout and be succeed
18647         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
18648                 error "Can't create DoM file (DoM is disabled)"
18649         [ $($LFS getstripe -L $dom) == "mdt" ] &&
18650                 error "File has DoM component while DoM is disabled"
18651         rm $dom
18652
18653         # attempt to create DoM file with only DoM stripe should return error
18654         $LFS setstripe -E $dom_limit -L mdt $dom &&
18655                 error "Able to create DoM-only file while DoM is disabled"
18656
18657         # too low values to be aligned with smallest stripe size 64K
18658         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
18659         dom_current=$(do_facet mds1 $LCTL get_param -n \
18660                                                 lod.$mdtname.dom_stripesize)
18661         [ 30000 -eq ${dom_current} ] &&
18662                 error "Can set too small DoM stripe limit"
18663
18664         # 64K is a minimal stripe size in Lustre, expect limit of that size
18665         [ 65536 -eq ${dom_current} ] ||
18666                 error "Limit is not set to 64K but ${dom_current}"
18667
18668         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
18669         dom_current=$(do_facet mds1 $LCTL get_param -n \
18670                                                 lod.$mdtname.dom_stripesize)
18671         echo $dom_current
18672         [ 2147483648 -eq ${dom_current} ] &&
18673                 error "Can set too large DoM stripe limit"
18674
18675         do_facet mds1 $LCTL set_param -n \
18676                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
18677         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18678                 error "Can't create DoM component size after limit change"
18679         do_facet mds1 $LCTL set_param -n \
18680                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
18681         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
18682                 error "Can't create DoM file after limit decrease"
18683         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
18684                 error "Can create big DoM component after limit decrease"
18685         touch ${dom}_def ||
18686                 error "Can't create file with old default layout"
18687
18688         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
18689         return 0
18690 }
18691 run_test 270f "DoM: maximum DoM stripe size checks"
18692
18693 test_271a() {
18694         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18695                 skip "Need MDS version at least 2.10.55"
18696
18697         local dom=$DIR/$tdir/dom
18698
18699         mkdir -p $DIR/$tdir
18700
18701         $LFS setstripe -E 1024K -L mdt $dom
18702
18703         lctl set_param -n mdc.*.stats=clear
18704         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18705         cat $dom > /dev/null
18706         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
18707         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
18708         ls $dom
18709         rm -f $dom
18710 }
18711 run_test 271a "DoM: data is cached for read after write"
18712
18713 test_271b() {
18714         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18715                 skip "Need MDS version at least 2.10.55"
18716
18717         local dom=$DIR/$tdir/dom
18718
18719         mkdir -p $DIR/$tdir
18720
18721         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18722
18723         lctl set_param -n mdc.*.stats=clear
18724         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18725         cancel_lru_locks mdc
18726         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
18727         # second stat to check size is cached on client
18728         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
18729         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18730         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
18731         rm -f $dom
18732 }
18733 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
18734
18735 test_271ba() {
18736         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18737                 skip "Need MDS version at least 2.10.55"
18738
18739         local dom=$DIR/$tdir/dom
18740
18741         mkdir -p $DIR/$tdir
18742
18743         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18744
18745         lctl set_param -n mdc.*.stats=clear
18746         lctl set_param -n osc.*.stats=clear
18747         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
18748         cancel_lru_locks mdc
18749         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18750         # second stat to check size is cached on client
18751         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18752         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18753         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
18754         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
18755         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
18756         rm -f $dom
18757 }
18758 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
18759
18760
18761 get_mdc_stats() {
18762         local mdtidx=$1
18763         local param=$2
18764         local mdt=MDT$(printf %04x $mdtidx)
18765
18766         if [ -z $param ]; then
18767                 lctl get_param -n mdc.*$mdt*.stats
18768         else
18769                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
18770         fi
18771 }
18772
18773 test_271c() {
18774         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18775                 skip "Need MDS version at least 2.10.55"
18776
18777         local dom=$DIR/$tdir/dom
18778
18779         mkdir -p $DIR/$tdir
18780
18781         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18782
18783         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18784         local facet=mds$((mdtidx + 1))
18785
18786         cancel_lru_locks mdc
18787         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
18788         createmany -o $dom 1000
18789         lctl set_param -n mdc.*.stats=clear
18790         smalliomany -w $dom 1000 200
18791         get_mdc_stats $mdtidx
18792         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18793         # Each file has 1 open, 1 IO enqueues, total 2000
18794         # but now we have also +1 getxattr for security.capability, total 3000
18795         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
18796         unlinkmany $dom 1000
18797
18798         cancel_lru_locks mdc
18799         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
18800         createmany -o $dom 1000
18801         lctl set_param -n mdc.*.stats=clear
18802         smalliomany -w $dom 1000 200
18803         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18804         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
18805         # for OPEN and IO lock.
18806         [ $((enq - enq_2)) -ge 1000 ] ||
18807                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
18808         unlinkmany $dom 1000
18809         return 0
18810 }
18811 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
18812
18813 cleanup_271def_tests() {
18814         trap 0
18815         rm -f $1
18816 }
18817
18818 test_271d() {
18819         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
18820                 skip "Need MDS version at least 2.10.57"
18821
18822         local dom=$DIR/$tdir/dom
18823         local tmp=$TMP/$tfile
18824         trap "cleanup_271def_tests $tmp" EXIT
18825
18826         mkdir -p $DIR/$tdir
18827
18828         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18829
18830         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
18831
18832         cancel_lru_locks mdc
18833         dd if=/dev/urandom of=$tmp bs=1000 count=1
18834         dd if=$tmp of=$dom bs=1000 count=1
18835         cancel_lru_locks mdc
18836
18837         cat /etc/hosts >> $tmp
18838         lctl set_param -n mdc.*.stats=clear
18839
18840         # append data to the same file it should update local page
18841         echo "Append to the same page"
18842         cat /etc/hosts >> $dom
18843         local num=$(get_mdc_stats $mdtidx ost_read)
18844         local ra=$(get_mdc_stats $mdtidx req_active)
18845         local rw=$(get_mdc_stats $mdtidx req_waittime)
18846
18847         [ -z $num ] || error "$num READ RPC occured"
18848         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18849         echo "... DONE"
18850
18851         # compare content
18852         cmp $tmp $dom || error "file miscompare"
18853
18854         cancel_lru_locks mdc
18855         lctl set_param -n mdc.*.stats=clear
18856
18857         echo "Open and read file"
18858         cat $dom > /dev/null
18859         local num=$(get_mdc_stats $mdtidx ost_read)
18860         local ra=$(get_mdc_stats $mdtidx req_active)
18861         local rw=$(get_mdc_stats $mdtidx req_waittime)
18862
18863         [ -z $num ] || error "$num READ RPC occured"
18864         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18865         echo "... DONE"
18866
18867         # compare content
18868         cmp $tmp $dom || error "file miscompare"
18869
18870         return 0
18871 }
18872 run_test 271d "DoM: read on open (1K file in reply buffer)"
18873
18874 test_271f() {
18875         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
18876                 skip "Need MDS version at least 2.10.57"
18877
18878         local dom=$DIR/$tdir/dom
18879         local tmp=$TMP/$tfile
18880         trap "cleanup_271def_tests $tmp" EXIT
18881
18882         mkdir -p $DIR/$tdir
18883
18884         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18885
18886         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
18887
18888         cancel_lru_locks mdc
18889         dd if=/dev/urandom of=$tmp bs=265000 count=1
18890         dd if=$tmp of=$dom bs=265000 count=1
18891         cancel_lru_locks mdc
18892         cat /etc/hosts >> $tmp
18893         lctl set_param -n mdc.*.stats=clear
18894
18895         echo "Append to the same page"
18896         cat /etc/hosts >> $dom
18897         local num=$(get_mdc_stats $mdtidx ost_read)
18898         local ra=$(get_mdc_stats $mdtidx req_active)
18899         local rw=$(get_mdc_stats $mdtidx req_waittime)
18900
18901         [ -z $num ] || error "$num READ RPC occured"
18902         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18903         echo "... DONE"
18904
18905         # compare content
18906         cmp $tmp $dom || error "file miscompare"
18907
18908         cancel_lru_locks mdc
18909         lctl set_param -n mdc.*.stats=clear
18910
18911         echo "Open and read file"
18912         cat $dom > /dev/null
18913         local num=$(get_mdc_stats $mdtidx ost_read)
18914         local ra=$(get_mdc_stats $mdtidx req_active)
18915         local rw=$(get_mdc_stats $mdtidx req_waittime)
18916
18917         [ -z $num ] && num=0
18918         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
18919         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18920         echo "... DONE"
18921
18922         # compare content
18923         cmp $tmp $dom || error "file miscompare"
18924
18925         return 0
18926 }
18927 run_test 271f "DoM: read on open (200K file and read tail)"
18928
18929 test_271g() {
18930         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
18931                 skip "Skipping due to old client or server version"
18932
18933         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
18934         # to get layout
18935         $CHECKSTAT -t file $DIR1/$tfile
18936
18937         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
18938         MULTIOP_PID=$!
18939         sleep 1
18940         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
18941         $LCTL set_param fail_loc=0x80000314
18942         rm $DIR1/$tfile || error "Unlink fails"
18943         RC=$?
18944         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
18945         [ $RC -eq 0 ] || error "Failed write to stale object"
18946 }
18947 run_test 271g "Discard DoM data vs client flush race"
18948
18949 test_272a() {
18950         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18951                 skip "Need MDS version at least 2.11.50"
18952
18953         local dom=$DIR/$tdir/dom
18954         mkdir -p $DIR/$tdir
18955
18956         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
18957         dd if=/dev/urandom of=$dom bs=512K count=1 ||
18958                 error "failed to write data into $dom"
18959         local old_md5=$(md5sum $dom)
18960
18961         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
18962                 error "failed to migrate to the same DoM component"
18963
18964         local new_md5=$(md5sum $dom)
18965
18966         [ "$old_md5" == "$new_md5" ] ||
18967                 error "md5sum differ: $old_md5, $new_md5"
18968
18969         [ $($LFS getstripe -c $dom) -eq 2 ] ||
18970                 error "migrate stripe count bad: $(LFS getstripe -c $dom) != 2"
18971 }
18972 run_test 272a "DoM migration: new layout with the same DOM component"
18973
18974 test_272b() {
18975         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18976                 skip "Need MDS version at least 2.11.50"
18977
18978         local dom=$DIR/$tdir/dom
18979         mkdir -p $DIR/$tdir
18980         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
18981
18982         local mdtidx=$($LFS getstripe -m $dom)
18983         local mdtname=MDT$(printf %04x $mdtidx)
18984         local facet=mds$((mdtidx + 1))
18985
18986         local mdtfree1=$(do_facet $facet \
18987                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18988         dd if=/dev/urandom of=$dom bs=2M count=1 ||
18989                 error "failed to write data into $dom"
18990         local old_md5=$(md5sum $dom)
18991         cancel_lru_locks mdc
18992         local mdtfree1=$(do_facet $facet \
18993                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18994
18995         $LFS migrate -c2 $dom ||
18996                 error "failed to migrate to the new composite layout"
18997         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
18998                 error "MDT stripe was not removed"
18999
19000         cancel_lru_locks mdc
19001         local new_md5=$(md5sum $dom)
19002         [ "$old_md5" == "$new_md5" ] ||
19003                 error "$old_md5 != $new_md5"
19004
19005         # Skip free space checks with ZFS
19006         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19007                 local mdtfree2=$(do_facet $facet \
19008                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19009                 [ $mdtfree2 -gt $mdtfree1 ] ||
19010                         error "MDT space is not freed after migration"
19011         fi
19012         return 0
19013 }
19014 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
19015
19016 test_272c() {
19017         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19018                 skip "Need MDS version at least 2.11.50"
19019
19020         local dom=$DIR/$tdir/$tfile
19021         mkdir -p $DIR/$tdir
19022         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19023
19024         local mdtidx=$($LFS getstripe -m $dom)
19025         local mdtname=MDT$(printf %04x $mdtidx)
19026         local facet=mds$((mdtidx + 1))
19027
19028         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
19029                 error "failed to write data into $dom"
19030         local old_md5=$(md5sum $dom)
19031         cancel_lru_locks mdc
19032         local mdtfree1=$(do_facet $facet \
19033                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19034
19035         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
19036                 error "failed to migrate to the new composite layout"
19037         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
19038                 error "MDT stripe was not removed"
19039
19040         cancel_lru_locks mdc
19041         local new_md5=$(md5sum $dom)
19042         [ "$old_md5" == "$new_md5" ] ||
19043                 error "$old_md5 != $new_md5"
19044
19045         # Skip free space checks with ZFS
19046         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19047                 local mdtfree2=$(do_facet $facet \
19048                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19049                 [ $mdtfree2 -gt $mdtfree1 ] ||
19050                         error "MDS space is not freed after migration"
19051         fi
19052         return 0
19053 }
19054 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
19055
19056 test_272d() {
19057         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19058                 skip "Need MDS version at least 2.12.55"
19059
19060         local dom=$DIR/$tdir/$tfile
19061         mkdir -p $DIR/$tdir
19062         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19063
19064         local mdtidx=$($LFS getstripe -m $dom)
19065         local mdtname=MDT$(printf %04x $mdtidx)
19066         local facet=mds$((mdtidx + 1))
19067
19068         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
19069                 error "failed to write data into $dom"
19070         local old_md5=$(md5sum $dom)
19071         cancel_lru_locks mdc
19072         local mdtfree1=$(do_facet $facet \
19073                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19074
19075         $LFS mirror extend -N -E 2M -c1 -E -1 -c2 $dom ||
19076                 error "failed mirroring to the new composite layout"
19077         $LFS mirror resync $dom ||
19078                 error "failed mirror resync"
19079         $LFS mirror split --mirror-id 1 -d $dom ||
19080                 error "failed mirror split"
19081
19082         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19083                 error "MDT stripe was not removed"
19084
19085         cancel_lru_locks mdc
19086         local new_md5=$(md5sum $dom)
19087         [ "$old_md5" == "$new_md5" ] ||
19088                 error "$old_md5 != $new_md5"
19089
19090         # Skip free space checks with ZFS
19091         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19092                 local mdtfree2=$(do_facet $facet \
19093                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19094                 [ $mdtfree2 -gt $mdtfree1 ] ||
19095                         error "MDS space is not freed after DOM mirror deletion"
19096         fi
19097         return 0
19098 }
19099 run_test 272d "DoM mirroring: OST-striped mirror to DOM file"
19100
19101 test_272e() {
19102         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19103                 skip "Need MDS version at least 2.12.55"
19104
19105         local dom=$DIR/$tdir/$tfile
19106         mkdir -p $DIR/$tdir
19107         $LFS setstripe -c 2 $dom
19108
19109         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
19110                 error "failed to write data into $dom"
19111         local old_md5=$(md5sum $dom)
19112         cancel_lru_locks mdc
19113
19114         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 $dom ||
19115                 error "failed mirroring to the DOM layout"
19116         $LFS mirror resync $dom ||
19117                 error "failed mirror resync"
19118         $LFS mirror split --mirror-id 1 -d $dom ||
19119                 error "failed mirror split"
19120
19121         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19122                 error "MDT stripe was not removed"
19123
19124         cancel_lru_locks mdc
19125         local new_md5=$(md5sum $dom)
19126         [ "$old_md5" == "$new_md5" ] ||
19127                 error "$old_md5 != $new_md5"
19128
19129         return 0
19130 }
19131 run_test 272e "DoM mirroring: DOM mirror to the OST-striped file"
19132
19133 test_272f() {
19134         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19135                 skip "Need MDS version at least 2.12.55"
19136
19137         local dom=$DIR/$tdir/$tfile
19138         mkdir -p $DIR/$tdir
19139         $LFS setstripe -c 2 $dom
19140
19141         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
19142                 error "failed to write data into $dom"
19143         local old_md5=$(md5sum $dom)
19144         cancel_lru_locks mdc
19145
19146         $LFS migrate -E 1M -L mdt -E eof -c2 -v $dom ||
19147                 error "failed migrating to the DOM file"
19148
19149         cancel_lru_locks mdc
19150         local new_md5=$(md5sum $dom)
19151         [ "$old_md5" != "$new_md5" ] &&
19152                 error "$old_md5 != $new_md5"
19153
19154         return 0
19155 }
19156 run_test 272f "DoM migration: OST-striped file to DOM file"
19157
19158 test_273a() {
19159         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19160                 skip "Need MDS version at least 2.11.50"
19161
19162         # Layout swap cannot be done if either file has DOM component,
19163         # this will never be supported, migration should be used instead
19164
19165         local dom=$DIR/$tdir/$tfile
19166         mkdir -p $DIR/$tdir
19167
19168         $LFS setstripe -c2 ${dom}_plain
19169         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
19170         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
19171                 error "can swap layout with DoM component"
19172         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
19173                 error "can swap layout with DoM component"
19174
19175         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
19176         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
19177                 error "can swap layout with DoM component"
19178         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
19179                 error "can swap layout with DoM component"
19180         return 0
19181 }
19182 run_test 273a "DoM: layout swapping should fail with DOM"
19183
19184 test_275() {
19185         remote_ost_nodsh && skip "remote OST with nodsh"
19186         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
19187                 skip "Need OST version >= 2.10.57"
19188
19189         local file=$DIR/$tfile
19190         local oss
19191
19192         oss=$(comma_list $(osts_nodes))
19193
19194         dd if=/dev/urandom of=$file bs=1M count=2 ||
19195                 error "failed to create a file"
19196         cancel_lru_locks osc
19197
19198         #lock 1
19199         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
19200                 error "failed to read a file"
19201
19202 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
19203         $LCTL set_param fail_loc=0x8000031f
19204
19205         cancel_lru_locks osc &
19206         sleep 1
19207
19208 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
19209         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
19210         #IO takes another lock, but matches the PENDING one
19211         #and places it to the IO RPC
19212         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
19213                 error "failed to read a file with PENDING lock"
19214 }
19215 run_test 275 "Read on a canceled duplicate lock"
19216
19217 test_276() {
19218         remote_ost_nodsh && skip "remote OST with nodsh"
19219         local pid
19220
19221         do_facet ost1 "(while true; do \
19222                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
19223                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
19224         pid=$!
19225
19226         for LOOP in $(seq 20); do
19227                 stop ost1
19228                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
19229         done
19230         kill -9 $pid
19231         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
19232                 rm $TMP/sanity_276_pid"
19233 }
19234 run_test 276 "Race between mount and obd_statfs"
19235
19236 test_277() {
19237         $LCTL set_param ldlm.namespaces.*.lru_size=0
19238         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
19239         local cached_mb=$($LCTL get_param llite.*.max_cached_mb |
19240                         grep ^used_mb | awk '{print $2}')
19241         [ $cached_mb -eq 1 ] || error "expected mb 1 got $cached_mb"
19242         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 \
19243                 oflag=direct conv=notrunc
19244         cached_mb=$($LCTL get_param llite.*.max_cached_mb |
19245                         grep ^used_mb | awk '{print $2}')
19246         [ $cached_mb -eq 0 ] || error "expected mb 0 got $cached_mb"
19247 }
19248 run_test 277 "Direct IO shall drop page cache"
19249
19250 test_278() {
19251         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
19252         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
19253         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] &&
19254                 skip "needs the same host for mdt1 mdt2" && return
19255
19256         local pid1
19257         local pid2
19258
19259 #define OBD_FAIL_OBD_STOP_MDS_RACE     0x60b
19260         do_facet mds2 $LCTL set_param fail_loc=0x8000060c
19261         stop mds2 &
19262         pid2=$!
19263
19264         stop mds1
19265
19266         echo "Starting MDTs"
19267         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
19268         wait $pid2
19269 #For the error assertion will happen. lu_env_get_key(..., &mdt_thread_key)
19270 #will return NULL
19271         do_facet mds2 $LCTL set_param fail_loc=0
19272
19273         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS
19274         wait_recovery_complete mds2
19275 }
19276 run_test 278 "Race starting MDS between MDTs stop/start"
19277
19278 cleanup_test_300() {
19279         trap 0
19280         umask $SAVE_UMASK
19281 }
19282 test_striped_dir() {
19283         local mdt_index=$1
19284         local stripe_count
19285         local stripe_index
19286
19287         mkdir -p $DIR/$tdir
19288
19289         SAVE_UMASK=$(umask)
19290         trap cleanup_test_300 RETURN EXIT
19291
19292         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
19293                                                 $DIR/$tdir/striped_dir ||
19294                 error "set striped dir error"
19295
19296         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
19297         [ "$mode" = "755" ] || error "expect 755 got $mode"
19298
19299         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
19300                 error "getdirstripe failed"
19301         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
19302         if [ "$stripe_count" != "2" ]; then
19303                 error "1:stripe_count is $stripe_count, expect 2"
19304         fi
19305         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
19306         if [ "$stripe_count" != "2" ]; then
19307                 error "2:stripe_count is $stripe_count, expect 2"
19308         fi
19309
19310         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
19311         if [ "$stripe_index" != "$mdt_index" ]; then
19312                 error "stripe_index is $stripe_index, expect $mdt_index"
19313         fi
19314
19315         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19316                 error "nlink error after create striped dir"
19317
19318         mkdir $DIR/$tdir/striped_dir/a
19319         mkdir $DIR/$tdir/striped_dir/b
19320
19321         stat $DIR/$tdir/striped_dir/a ||
19322                 error "create dir under striped dir failed"
19323         stat $DIR/$tdir/striped_dir/b ||
19324                 error "create dir under striped dir failed"
19325
19326         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
19327                 error "nlink error after mkdir"
19328
19329         rmdir $DIR/$tdir/striped_dir/a
19330         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
19331                 error "nlink error after rmdir"
19332
19333         rmdir $DIR/$tdir/striped_dir/b
19334         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19335                 error "nlink error after rmdir"
19336
19337         chattr +i $DIR/$tdir/striped_dir
19338         createmany -o $DIR/$tdir/striped_dir/f 10 &&
19339                 error "immutable flags not working under striped dir!"
19340         chattr -i $DIR/$tdir/striped_dir
19341
19342         rmdir $DIR/$tdir/striped_dir ||
19343                 error "rmdir striped dir error"
19344
19345         cleanup_test_300
19346
19347         true
19348 }
19349
19350 test_300a() {
19351         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19352                 skip "skipped for lustre < 2.7.0"
19353         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19354         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19355
19356         test_striped_dir 0 || error "failed on striped dir on MDT0"
19357         test_striped_dir 1 || error "failed on striped dir on MDT0"
19358 }
19359 run_test 300a "basic striped dir sanity test"
19360
19361 test_300b() {
19362         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19363                 skip "skipped for lustre < 2.7.0"
19364         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19365         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19366
19367         local i
19368         local mtime1
19369         local mtime2
19370         local mtime3
19371
19372         test_mkdir $DIR/$tdir || error "mkdir fail"
19373         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19374                 error "set striped dir error"
19375         for i in {0..9}; do
19376                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
19377                 sleep 1
19378                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
19379                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
19380                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
19381                 sleep 1
19382                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
19383                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
19384                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
19385         done
19386         true
19387 }
19388 run_test 300b "check ctime/mtime for striped dir"
19389
19390 test_300c() {
19391         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19392                 skip "skipped for lustre < 2.7.0"
19393         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19394         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19395
19396         local file_count
19397
19398         mkdir -p $DIR/$tdir
19399         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
19400                 error "set striped dir error"
19401
19402         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
19403                 error "chown striped dir failed"
19404
19405         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
19406                 error "create 5k files failed"
19407
19408         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
19409
19410         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
19411
19412         rm -rf $DIR/$tdir
19413 }
19414 run_test 300c "chown && check ls under striped directory"
19415
19416 test_300d() {
19417         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19418                 skip "skipped for lustre < 2.7.0"
19419         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19420         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19421
19422         local stripe_count
19423         local file
19424
19425         mkdir -p $DIR/$tdir
19426         $LFS setstripe -c 2 $DIR/$tdir
19427
19428         #local striped directory
19429         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19430                 error "set striped dir error"
19431         createmany -o $DIR/$tdir/striped_dir/f 10 ||
19432                 error "create 10 files failed"
19433
19434         #remote striped directory
19435         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
19436                 error "set striped dir error"
19437         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
19438                 error "create 10 files failed"
19439
19440         for file in $(find $DIR/$tdir); do
19441                 stripe_count=$($LFS getstripe -c $file)
19442                 [ $stripe_count -eq 2 ] ||
19443                         error "wrong stripe $stripe_count for $file"
19444         done
19445
19446         rm -rf $DIR/$tdir
19447 }
19448 run_test 300d "check default stripe under striped directory"
19449
19450 test_300e() {
19451         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19452                 skip "Need MDS version at least 2.7.55"
19453         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19454         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19455
19456         local stripe_count
19457         local file
19458
19459         mkdir -p $DIR/$tdir
19460
19461         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19462                 error "set striped dir error"
19463
19464         touch $DIR/$tdir/striped_dir/a
19465         touch $DIR/$tdir/striped_dir/b
19466         touch $DIR/$tdir/striped_dir/c
19467
19468         mkdir $DIR/$tdir/striped_dir/dir_a
19469         mkdir $DIR/$tdir/striped_dir/dir_b
19470         mkdir $DIR/$tdir/striped_dir/dir_c
19471
19472         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
19473                 error "set striped adir under striped dir error"
19474
19475         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
19476                 error "set striped bdir under striped dir error"
19477
19478         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
19479                 error "set striped cdir under striped dir error"
19480
19481         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
19482                 error "rename dir under striped dir fails"
19483
19484         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
19485                 error "rename dir under different stripes fails"
19486
19487         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
19488                 error "rename file under striped dir should succeed"
19489
19490         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
19491                 error "rename dir under striped dir should succeed"
19492
19493         rm -rf $DIR/$tdir
19494 }
19495 run_test 300e "check rename under striped directory"
19496
19497 test_300f() {
19498         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19499         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19500         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19501                 skip "Need MDS version at least 2.7.55"
19502
19503         local stripe_count
19504         local file
19505
19506         rm -rf $DIR/$tdir
19507         mkdir -p $DIR/$tdir
19508
19509         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19510                 error "set striped dir error"
19511
19512         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
19513                 error "set striped dir error"
19514
19515         touch $DIR/$tdir/striped_dir/a
19516         mkdir $DIR/$tdir/striped_dir/dir_a
19517         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
19518                 error "create striped dir under striped dir fails"
19519
19520         touch $DIR/$tdir/striped_dir1/b
19521         mkdir $DIR/$tdir/striped_dir1/dir_b
19522         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
19523                 error "create striped dir under striped dir fails"
19524
19525         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
19526                 error "rename dir under different striped dir should fail"
19527
19528         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
19529                 error "rename striped dir under diff striped dir should fail"
19530
19531         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
19532                 error "rename file under diff striped dirs fails"
19533
19534         rm -rf $DIR/$tdir
19535 }
19536 run_test 300f "check rename cross striped directory"
19537
19538 test_300_check_default_striped_dir()
19539 {
19540         local dirname=$1
19541         local default_count=$2
19542         local default_index=$3
19543         local stripe_count
19544         local stripe_index
19545         local dir_stripe_index
19546         local dir
19547
19548         echo "checking $dirname $default_count $default_index"
19549         $LFS setdirstripe -D -c $default_count -i $default_index \
19550                                 -t all_char $DIR/$tdir/$dirname ||
19551                 error "set default stripe on striped dir error"
19552         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
19553         [ $stripe_count -eq $default_count ] ||
19554                 error "expect $default_count get $stripe_count for $dirname"
19555
19556         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
19557         [ $stripe_index -eq $default_index ] ||
19558                 error "expect $default_index get $stripe_index for $dirname"
19559
19560         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
19561                                                 error "create dirs failed"
19562
19563         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
19564         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
19565         for dir in $(find $DIR/$tdir/$dirname/*); do
19566                 stripe_count=$($LFS getdirstripe -c $dir)
19567                 [ $stripe_count -eq $default_count ] ||
19568                 [ $stripe_count -eq 0 ] || [ $default_count -eq 1 ] ||
19569                 error "stripe count $default_count != $stripe_count for $dir"
19570
19571                 stripe_index=$($LFS getdirstripe -i $dir)
19572                 [ $default_index -eq -1 ] ||
19573                         [ $stripe_index -eq $default_index ] ||
19574                         error "$stripe_index != $default_index for $dir"
19575
19576                 #check default stripe
19577                 stripe_count=$($LFS getdirstripe -D -c $dir)
19578                 [ $stripe_count -eq $default_count ] ||
19579                 error "default count $default_count != $stripe_count for $dir"
19580
19581                 stripe_index=$($LFS getdirstripe -D -i $dir)
19582                 [ $stripe_index -eq $default_index ] ||
19583                 error "default index $default_index != $stripe_index for $dir"
19584         done
19585         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
19586 }
19587
19588 test_300g() {
19589         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19590         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19591                 skip "Need MDS version at least 2.7.55"
19592
19593         local dir
19594         local stripe_count
19595         local stripe_index
19596
19597         mkdir $DIR/$tdir
19598         mkdir $DIR/$tdir/normal_dir
19599
19600         #Checking when client cache stripe index
19601         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
19602         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
19603                 error "create striped_dir failed"
19604
19605         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
19606                 error "create dir0 fails"
19607         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
19608         [ $stripe_index -eq 0 ] ||
19609                 error "dir0 expect index 0 got $stripe_index"
19610
19611         mkdir $DIR/$tdir/striped_dir/dir1 ||
19612                 error "create dir1 fails"
19613         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
19614         [ $stripe_index -eq 1 ] ||
19615                 error "dir1 expect index 1 got $stripe_index"
19616
19617         #check default stripe count/stripe index
19618         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
19619         test_300_check_default_striped_dir normal_dir 1 0
19620         test_300_check_default_striped_dir normal_dir 2 1
19621         test_300_check_default_striped_dir normal_dir 2 -1
19622
19623         #delete default stripe information
19624         echo "delete default stripeEA"
19625         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
19626                 error "set default stripe on striped dir error"
19627
19628         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
19629         for dir in $(find $DIR/$tdir/normal_dir/*); do
19630                 stripe_count=$($LFS getdirstripe -c $dir)
19631                 [ $stripe_count -eq 0 ] ||
19632                         error "expect 1 get $stripe_count for $dir"
19633                 stripe_index=$($LFS getdirstripe -i $dir)
19634                 [ $stripe_index -eq 0 ] ||
19635                         error "expect 0 get $stripe_index for $dir"
19636         done
19637 }
19638 run_test 300g "check default striped directory for normal directory"
19639
19640 test_300h() {
19641         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19642         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19643                 skip "Need MDS version at least 2.7.55"
19644
19645         local dir
19646         local stripe_count
19647
19648         mkdir $DIR/$tdir
19649         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19650                 error "set striped dir error"
19651
19652         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
19653         test_300_check_default_striped_dir striped_dir 1 0
19654         test_300_check_default_striped_dir striped_dir 2 1
19655         test_300_check_default_striped_dir striped_dir 2 -1
19656
19657         #delete default stripe information
19658         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
19659                 error "set default stripe on striped dir error"
19660
19661         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
19662         for dir in $(find $DIR/$tdir/striped_dir/*); do
19663                 stripe_count=$($LFS getdirstripe -c $dir)
19664                 [ $stripe_count -eq 0 ] ||
19665                         error "expect 1 get $stripe_count for $dir"
19666         done
19667 }
19668 run_test 300h "check default striped directory for striped directory"
19669
19670 test_300i() {
19671         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19672         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19673         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19674                 skip "Need MDS version at least 2.7.55"
19675
19676         local stripe_count
19677         local file
19678
19679         mkdir $DIR/$tdir
19680
19681         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19682                 error "set striped dir error"
19683
19684         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
19685                 error "create files under striped dir failed"
19686
19687         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
19688                 error "set striped hashdir error"
19689
19690         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
19691                 error "create dir0 under hash dir failed"
19692         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
19693                 error "create dir1 under hash dir failed"
19694
19695         # unfortunately, we need to umount to clear dir layout cache for now
19696         # once we fully implement dir layout, we can drop this
19697         umount_client $MOUNT || error "umount failed"
19698         mount_client $MOUNT || error "mount failed"
19699
19700         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
19701         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
19702         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
19703
19704         #set the stripe to be unknown hash type
19705         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
19706         $LCTL set_param fail_loc=0x1901
19707         for ((i = 0; i < 10; i++)); do
19708                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
19709                         error "stat f-$i failed"
19710                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
19711         done
19712
19713         touch $DIR/$tdir/striped_dir/f0 &&
19714                 error "create under striped dir with unknown hash should fail"
19715
19716         $LCTL set_param fail_loc=0
19717
19718         umount_client $MOUNT || error "umount failed"
19719         mount_client $MOUNT || error "mount failed"
19720
19721         return 0
19722 }
19723 run_test 300i "client handle unknown hash type striped directory"
19724
19725 test_300j() {
19726         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19727         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19728         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19729                 skip "Need MDS version at least 2.7.55"
19730
19731         local stripe_count
19732         local file
19733
19734         mkdir $DIR/$tdir
19735
19736         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
19737         $LCTL set_param fail_loc=0x1702
19738         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19739                 error "set striped dir error"
19740
19741         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
19742                 error "create files under striped dir failed"
19743
19744         $LCTL set_param fail_loc=0
19745
19746         rm -rf $DIR/$tdir || error "unlink striped dir fails"
19747
19748         return 0
19749 }
19750 run_test 300j "test large update record"
19751
19752 test_300k() {
19753         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19754         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19755         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19756                 skip "Need MDS version at least 2.7.55"
19757
19758         # this test needs a huge transaction
19759         local kb
19760         kb=$(do_facet $SINGLEMDS "$LCTL get_param -n \
19761              osd*.$FSNAME-MDT0000.kbytestotal")
19762         [ $kb -lt $((1024*1024)) ] && skip "MDT0 too small: $kb"
19763
19764         local stripe_count
19765         local file
19766
19767         mkdir $DIR/$tdir
19768
19769         #define OBD_FAIL_LARGE_STRIPE   0x1703
19770         $LCTL set_param fail_loc=0x1703
19771         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
19772                 error "set striped dir error"
19773         $LCTL set_param fail_loc=0
19774
19775         $LFS getdirstripe $DIR/$tdir/striped_dir ||
19776                 error "getstripeddir fails"
19777         rm -rf $DIR/$tdir/striped_dir ||
19778                 error "unlink striped dir fails"
19779
19780         return 0
19781 }
19782 run_test 300k "test large striped directory"
19783
19784 test_300l() {
19785         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19786         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19787         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19788                 skip "Need MDS version at least 2.7.55"
19789
19790         local stripe_index
19791
19792         test_mkdir -p $DIR/$tdir/striped_dir
19793         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
19794                         error "chown $RUNAS_ID failed"
19795         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
19796                 error "set default striped dir failed"
19797
19798         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
19799         $LCTL set_param fail_loc=0x80000158
19800         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
19801
19802         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
19803         [ $stripe_index -eq 1 ] ||
19804                 error "expect 1 get $stripe_index for $dir"
19805 }
19806 run_test 300l "non-root user to create dir under striped dir with stale layout"
19807
19808 test_300m() {
19809         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19810         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
19811         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19812                 skip "Need MDS version at least 2.7.55"
19813
19814         mkdir -p $DIR/$tdir/striped_dir
19815         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
19816                 error "set default stripes dir error"
19817
19818         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
19819
19820         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
19821         [ $stripe_count -eq 0 ] ||
19822                         error "expect 0 get $stripe_count for a"
19823
19824         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
19825                 error "set default stripes dir error"
19826
19827         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
19828
19829         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
19830         [ $stripe_count -eq 0 ] ||
19831                         error "expect 0 get $stripe_count for b"
19832
19833         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
19834                 error "set default stripes dir error"
19835
19836         mkdir $DIR/$tdir/striped_dir/c &&
19837                 error "default stripe_index is invalid, mkdir c should fails"
19838
19839         rm -rf $DIR/$tdir || error "rmdir fails"
19840 }
19841 run_test 300m "setstriped directory on single MDT FS"
19842
19843 cleanup_300n() {
19844         local list=$(comma_list $(mdts_nodes))
19845
19846         trap 0
19847         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19848 }
19849
19850 test_300n() {
19851         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19852         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19853         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19854                 skip "Need MDS version at least 2.7.55"
19855         remote_mds_nodsh && skip "remote MDS with nodsh"
19856
19857         local stripe_index
19858         local list=$(comma_list $(mdts_nodes))
19859
19860         trap cleanup_300n RETURN EXIT
19861         mkdir -p $DIR/$tdir
19862         chmod 777 $DIR/$tdir
19863         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
19864                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
19865                 error "create striped dir succeeds with gid=0"
19866
19867         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
19868         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
19869                 error "create striped dir fails with gid=-1"
19870
19871         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19872         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
19873                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
19874                 error "set default striped dir succeeds with gid=0"
19875
19876
19877         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
19878         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
19879                 error "set default striped dir fails with gid=-1"
19880
19881
19882         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19883         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
19884                                         error "create test_dir fails"
19885         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
19886                                         error "create test_dir1 fails"
19887         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
19888                                         error "create test_dir2 fails"
19889         cleanup_300n
19890 }
19891 run_test 300n "non-root user to create dir under striped dir with default EA"
19892
19893 test_300o() {
19894         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19895         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19896         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19897                 skip "Need MDS version at least 2.7.55"
19898
19899         local numfree1
19900         local numfree2
19901
19902         mkdir -p $DIR/$tdir
19903
19904         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
19905         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
19906         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
19907                 skip "not enough free inodes $numfree1 $numfree2"
19908         fi
19909
19910         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
19911         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
19912         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
19913                 skip "not enough free space $numfree1 $numfree2"
19914         fi
19915
19916         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
19917                 error "setdirstripe fails"
19918
19919         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
19920                 error "create dirs fails"
19921
19922         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
19923         ls $DIR/$tdir/striped_dir > /dev/null ||
19924                 error "ls striped dir fails"
19925         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
19926                 error "unlink big striped dir fails"
19927 }
19928 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
19929
19930 test_300p() {
19931         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19932         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19933         remote_mds_nodsh && skip "remote MDS with nodsh"
19934
19935         mkdir -p $DIR/$tdir
19936
19937         #define OBD_FAIL_OUT_ENOSPC     0x1704
19938         do_facet mds2 lctl set_param fail_loc=0x80001704
19939         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
19940                  && error "create striped directory should fail"
19941
19942         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
19943
19944         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
19945         true
19946 }
19947 run_test 300p "create striped directory without space"
19948
19949 test_300q() {
19950         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19951         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19952
19953         local fd=$(free_fd)
19954         local cmd="exec $fd<$tdir"
19955         cd $DIR
19956         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
19957         eval $cmd
19958         cmd="exec $fd<&-"
19959         trap "eval $cmd" EXIT
19960         cd $tdir || error "cd $tdir fails"
19961         rmdir  ../$tdir || error "rmdir $tdir fails"
19962         mkdir local_dir && error "create dir succeeds"
19963         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
19964         eval $cmd
19965         return 0
19966 }
19967 run_test 300q "create remote directory under orphan directory"
19968
19969 test_300r() {
19970         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
19971                 skip "Need MDS version at least 2.7.55" && return
19972         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
19973
19974         mkdir $DIR/$tdir
19975
19976         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
19977                 error "set striped dir error"
19978
19979         $LFS getdirstripe $DIR/$tdir/striped_dir ||
19980                 error "getstripeddir fails"
19981
19982         local stripe_count
19983         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
19984                       awk '/lmv_stripe_count:/ { print $2 }')
19985
19986         [ $MDSCOUNT -ne $stripe_count ] &&
19987                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
19988
19989         rm -rf $DIR/$tdir/striped_dir ||
19990                 error "unlink striped dir fails"
19991 }
19992 run_test 300r "test -1 striped directory"
19993
19994 prepare_remote_file() {
19995         mkdir $DIR/$tdir/src_dir ||
19996                 error "create remote source failed"
19997
19998         cp /etc/hosts $DIR/$tdir/src_dir/a ||
19999                  error "cp to remote source failed"
20000         touch $DIR/$tdir/src_dir/a
20001
20002         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
20003                 error "create remote target dir failed"
20004
20005         touch $DIR/$tdir/tgt_dir/b
20006
20007         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
20008                 error "rename dir cross MDT failed!"
20009
20010         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
20011                 error "src_child still exists after rename"
20012
20013         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
20014                 error "missing file(a) after rename"
20015
20016         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
20017                 error "diff after rename"
20018 }
20019
20020 test_310a() {
20021         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
20022         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20023
20024         local remote_file=$DIR/$tdir/tgt_dir/b
20025
20026         mkdir -p $DIR/$tdir
20027
20028         prepare_remote_file || error "prepare remote file failed"
20029
20030         #open-unlink file
20031         $OPENUNLINK $remote_file $remote_file ||
20032                 error "openunlink $remote_file failed"
20033         $CHECKSTAT -a $remote_file || error "$remote_file exists"
20034 }
20035 run_test 310a "open unlink remote file"
20036
20037 test_310b() {
20038         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
20039         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20040
20041         local remote_file=$DIR/$tdir/tgt_dir/b
20042
20043         mkdir -p $DIR/$tdir
20044
20045         prepare_remote_file || error "prepare remote file failed"
20046
20047         ln $remote_file $DIR/$tfile || error "link failed for remote file"
20048         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
20049         $CHECKSTAT -t file $remote_file || error "check file failed"
20050 }
20051 run_test 310b "unlink remote file with multiple links while open"
20052
20053 test_310c() {
20054         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20055         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
20056
20057         local remote_file=$DIR/$tdir/tgt_dir/b
20058
20059         mkdir -p $DIR/$tdir
20060
20061         prepare_remote_file || error "prepare remote file failed"
20062
20063         ln $remote_file $DIR/$tfile || error "link failed for remote file"
20064         multiop_bg_pause $remote_file O_uc ||
20065                         error "mulitop failed for remote file"
20066         MULTIPID=$!
20067         $MULTIOP $DIR/$tfile Ouc
20068         kill -USR1 $MULTIPID
20069         wait $MULTIPID
20070 }
20071 run_test 310c "open-unlink remote file with multiple links"
20072
20073 #LU-4825
20074 test_311() {
20075         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20076         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
20077         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
20078                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
20079         remote_mds_nodsh && skip "remote MDS with nodsh"
20080
20081         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
20082         local mdts=$(comma_list $(mdts_nodes))
20083
20084         mkdir -p $DIR/$tdir
20085         $LFS setstripe -i 0 -c 1 $DIR/$tdir
20086         createmany -o $DIR/$tdir/$tfile. 1000
20087
20088         # statfs data is not real time, let's just calculate it
20089         old_iused=$((old_iused + 1000))
20090
20091         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
20092                         osp.*OST0000*MDT0000.create_count")
20093         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
20094                                 osp.*OST0000*MDT0000.max_create_count")
20095         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
20096
20097         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
20098         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
20099         [ $index -ne 0 ] || error "$tfile stripe index is 0"
20100
20101         unlinkmany $DIR/$tdir/$tfile. 1000
20102
20103         do_nodes $mdts "$LCTL set_param -n \
20104                         osp.*OST0000*.max_create_count=$max_count"
20105         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
20106                 do_nodes $mdts "$LCTL set_param -n \
20107                                 osp.*OST0000*.create_count=$count"
20108         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
20109                         grep "=0" && error "create_count is zero"
20110
20111         local new_iused
20112         for i in $(seq 120); do
20113                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
20114                 # system may be too busy to destroy all objs in time, use
20115                 # a somewhat small value to not fail autotest
20116                 [ $((old_iused - new_iused)) -gt 400 ] && break
20117                 sleep 1
20118         done
20119
20120         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
20121         [ $((old_iused - new_iused)) -gt 400 ] ||
20122                 error "objs not destroyed after unlink"
20123 }
20124 run_test 311 "disable OSP precreate, and unlink should destroy objs"
20125
20126 zfs_oid_to_objid()
20127 {
20128         local ost=$1
20129         local objid=$2
20130
20131         local vdevdir=$(dirname $(facet_vdevice $ost))
20132         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
20133         local zfs_zapid=$(do_facet $ost $cmd |
20134                           grep -w "/O/0/d$((objid%32))" -C 5 |
20135                           awk '/Object/{getline; print $1}')
20136         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
20137                           awk "/$objid = /"'{printf $3}')
20138
20139         echo $zfs_objid
20140 }
20141
20142 zfs_object_blksz() {
20143         local ost=$1
20144         local objid=$2
20145
20146         local vdevdir=$(dirname $(facet_vdevice $ost))
20147         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
20148         local blksz=$(do_facet $ost $cmd $objid |
20149                       awk '/dblk/{getline; printf $4}')
20150
20151         case "${blksz: -1}" in
20152                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
20153                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
20154                 *) ;;
20155         esac
20156
20157         echo $blksz
20158 }
20159
20160 test_312() { # LU-4856
20161         remote_ost_nodsh && skip "remote OST with nodsh"
20162         [ "$ost1_FSTYPE" = "zfs" ] ||
20163                 skip_env "the test only applies to zfs"
20164
20165         local max_blksz=$(do_facet ost1 \
20166                           $ZFS get -p recordsize $(facet_device ost1) |
20167                           awk '!/VALUE/{print $3}')
20168
20169         # to make life a little bit easier
20170         $LFS mkdir -c 1 -i 0 $DIR/$tdir
20171         $LFS setstripe -c 1 -i 0 $DIR/$tdir
20172
20173         local tf=$DIR/$tdir/$tfile
20174         touch $tf
20175         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20176
20177         # Get ZFS object id
20178         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20179         # block size change by sequential overwrite
20180         local bs
20181
20182         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
20183                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
20184
20185                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
20186                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
20187         done
20188         rm -f $tf
20189
20190         # block size change by sequential append write
20191         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
20192         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20193         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20194         local count
20195
20196         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
20197                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
20198                         oflag=sync conv=notrunc
20199
20200                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
20201                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
20202                         error "blksz error, actual $blksz, " \
20203                                 "expected: 2 * $count * $PAGE_SIZE"
20204         done
20205         rm -f $tf
20206
20207         # random write
20208         touch $tf
20209         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20210         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20211
20212         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
20213         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20214         [ $blksz -eq $PAGE_SIZE ] ||
20215                 error "blksz error: $blksz, expected: $PAGE_SIZE"
20216
20217         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
20218         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20219         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
20220
20221         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
20222         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20223         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
20224 }
20225 run_test 312 "make sure ZFS adjusts its block size by write pattern"
20226
20227 test_313() {
20228         remote_ost_nodsh && skip "remote OST with nodsh"
20229
20230         local file=$DIR/$tfile
20231
20232         rm -f $file
20233         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
20234
20235         # define OBD_FAIL_TGT_RCVD_EIO           0x720
20236         do_facet ost1 "$LCTL set_param fail_loc=0x720"
20237         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
20238                 error "write should failed"
20239         do_facet ost1 "$LCTL set_param fail_loc=0"
20240         rm -f $file
20241 }
20242 run_test 313 "io should fail after last_rcvd update fail"
20243
20244 test_314() {
20245         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
20246
20247         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
20248         do_facet ost1 "$LCTL set_param fail_loc=0x720"
20249         rm -f $DIR/$tfile
20250         wait_delete_completed
20251         do_facet ost1 "$LCTL set_param fail_loc=0"
20252 }
20253 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
20254
20255 test_315() { # LU-618
20256         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
20257
20258         local file=$DIR/$tfile
20259         rm -f $file
20260
20261         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
20262                 error "multiop file write failed"
20263         $MULTIOP $file oO_RDONLY:r4063232_c &
20264         PID=$!
20265
20266         sleep 2
20267
20268         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
20269         kill -USR1 $PID
20270
20271         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
20272         rm -f $file
20273 }
20274 run_test 315 "read should be accounted"
20275
20276 test_316() {
20277         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20278         large_xattr_enabled || skip_env "ea_inode feature disabled"
20279
20280         rm -rf $DIR/$tdir/d
20281         mkdir -p $DIR/$tdir/d
20282         chown nobody $DIR/$tdir/d
20283         touch $DIR/$tdir/d/file
20284
20285         $LFS mv -M1 $DIR/$tdir/d || error "lfs mv failed"
20286 }
20287 run_test 316 "lfs mv"
20288
20289 test_317() {
20290         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
20291                 skip "Need MDS version at least 2.11.53"
20292         if [ "$ost1_FSTYPE" == "zfs" ]; then
20293                 skip "LU-10370: no implementation for ZFS"
20294         fi
20295
20296         local trunc_sz
20297         local grant_blk_size
20298
20299         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
20300                         awk '/grant_block_size:/ { print $2; exit; }')
20301         #
20302         # Create File of size 5M. Truncate it to below size's and verify
20303         # blocks count.
20304         #
20305         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
20306                 error "Create file $DIR/$tfile failed"
20307         stack_trap "rm -f $DIR/$tfile" EXIT
20308
20309         for trunc_sz in 2097152 4097 4000 509 0; do
20310                 $TRUNCATE $DIR/$tfile $trunc_sz ||
20311                         error "truncate $tfile to $trunc_sz failed"
20312                 local sz=$(stat --format=%s $DIR/$tfile)
20313                 local blk=$(stat --format=%b $DIR/$tfile)
20314                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
20315                                      grant_blk_size) * 8))
20316
20317                 if [[ $blk -ne $trunc_blk ]]; then
20318                         $(which stat) $DIR/$tfile
20319                         error "Expected Block $trunc_blk got $blk for $tfile"
20320                 fi
20321
20322                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20323                         error "Expected Size $trunc_sz got $sz for $tfile"
20324         done
20325
20326         #
20327         # sparse file test
20328         # Create file with a hole and write actual two blocks. Block count
20329         # must be 16.
20330         #
20331         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
20332                 conv=fsync || error "Create file : $DIR/$tfile"
20333
20334         # Calculate the final truncate size.
20335         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
20336
20337         #
20338         # truncate to size $trunc_sz bytes. Strip the last block
20339         # The block count must drop to 8
20340         #
20341         $TRUNCATE $DIR/$tfile $trunc_sz ||
20342                 error "truncate $tfile to $trunc_sz failed"
20343
20344         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
20345         sz=$(stat --format=%s $DIR/$tfile)
20346         blk=$(stat --format=%b $DIR/$tfile)
20347
20348         if [[ $blk -ne $trunc_bsz ]]; then
20349                 $(which stat) $DIR/$tfile
20350                 error "Expected Block $trunc_bsz got $blk for $tfile"
20351         fi
20352
20353         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20354                 error "Expected Size $trunc_sz got $sz for $tfile"
20355 }
20356 run_test 317 "Verify blocks get correctly update after truncate"
20357
20358 test_318() {
20359         local old_max_active=$($LCTL get_param -n \
20360                             llite.*.max_read_ahead_async_active 2>/dev/null)
20361
20362         $LCTL set_param llite.*.max_read_ahead_async_active=256
20363         local max_active=$($LCTL get_param -n \
20364                            llite.*.max_read_ahead_async_active 2>/dev/null)
20365         [ $max_active -ne 256 ] && error "expected 256 but got $max_active"
20366
20367         # currently reset to 0 is unsupported, leave it 512 for now.
20368         $LCTL set_param llite.*.max_read_ahead_async_active=0 &&
20369                 error "set max_read_ahead_async_active should fail"
20370
20371         $LCTL set_param llite.*.max_read_ahead_async_active=512
20372         max_active=$($LCTL get_param -n \
20373                      llite.*.max_read_ahead_async_active 2>/dev/null)
20374         [ $max_active -eq 512 ] || error "expected 512 but got $max_active"
20375
20376         # restore @max_active
20377         [ $old_max_active -ne 0 ] && $LCTL set_param \
20378                 llite.*.max_read_ahead_async_active=$old_max_active
20379
20380         local old_threshold=$($LCTL get_param -n \
20381                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
20382         local max_per_file_mb=$($LCTL get_param -n \
20383                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
20384
20385         local invalid=$(($max_per_file_mb + 1))
20386         $LCTL set_param \
20387                 llite.*.read_ahead_async_file_threshold_mb=$invalid\
20388                         && error "set $invalid should fail"
20389
20390         local valid=$(($invalid - 1))
20391         $LCTL set_param \
20392                 llite.*.read_ahead_async_file_threshold_mb=$valid ||
20393                         error "set $valid should succeed"
20394         local threshold=$($LCTL get_param -n \
20395                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
20396         [ $threshold -eq $valid ] || error \
20397                 "expect threshold $valid got $threshold"
20398         $LCTL set_param \
20399                 llite.*.read_ahead_async_file_threshold_mb=$old_threshold
20400 }
20401 run_test 318 "Verify async readahead tunables"
20402
20403 test_319() {
20404         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
20405
20406         local before=$(date +%s)
20407         local evict
20408         local mdir=$DIR/$tdir
20409         local file=$mdir/xxx
20410
20411         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
20412         touch $file
20413
20414 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
20415         $LCTL set_param fail_val=5 fail_loc=0x8000032c
20416         $LFS mv -m1 $file &
20417
20418         sleep 1
20419         dd if=$file of=/dev/null
20420         wait
20421         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
20422           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
20423
20424         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
20425 }
20426 run_test 319 "lost lease lock on migrate error"
20427
20428 test_fake_rw() {
20429         local read_write=$1
20430         if [ "$read_write" = "write" ]; then
20431                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
20432         elif [ "$read_write" = "read" ]; then
20433                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
20434         else
20435                 error "argument error"
20436         fi
20437
20438         # turn off debug for performance testing
20439         local saved_debug=$($LCTL get_param -n debug)
20440         $LCTL set_param debug=0
20441
20442         $LFS setstripe -c 1 -i 0 $DIR/$tfile
20443
20444         # get ost1 size - $FSNAME-OST0000
20445         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
20446         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
20447         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
20448
20449         if [ "$read_write" = "read" ]; then
20450                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
20451         fi
20452
20453         local start_time=$(date +%s.%N)
20454         $dd_cmd bs=1M count=$blocks oflag=sync ||
20455                 error "real dd $read_write error"
20456         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
20457
20458         if [ "$read_write" = "write" ]; then
20459                 rm -f $DIR/$tfile
20460         fi
20461
20462         # define OBD_FAIL_OST_FAKE_RW           0x238
20463         do_facet ost1 $LCTL set_param fail_loc=0x238
20464
20465         local start_time=$(date +%s.%N)
20466         $dd_cmd bs=1M count=$blocks oflag=sync ||
20467                 error "fake dd $read_write error"
20468         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
20469
20470         if [ "$read_write" = "write" ]; then
20471                 # verify file size
20472                 cancel_lru_locks osc
20473                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
20474                         error "$tfile size not $blocks MB"
20475         fi
20476         do_facet ost1 $LCTL set_param fail_loc=0
20477
20478         echo "fake $read_write $duration_fake vs. normal $read_write" \
20479                 "$duration in seconds"
20480         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
20481                 error_not_in_vm "fake write is slower"
20482
20483         $LCTL set_param -n debug="$saved_debug"
20484         rm -f $DIR/$tfile
20485 }
20486 test_399a() { # LU-7655 for OST fake write
20487         remote_ost_nodsh && skip "remote OST with nodsh"
20488
20489         test_fake_rw write
20490 }
20491 run_test 399a "fake write should not be slower than normal write"
20492
20493 test_399b() { # LU-8726 for OST fake read
20494         remote_ost_nodsh && skip "remote OST with nodsh"
20495         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
20496                 skip_env "ldiskfs only test"
20497         fi
20498
20499         test_fake_rw read
20500 }
20501 run_test 399b "fake read should not be slower than normal read"
20502
20503 test_400a() { # LU-1606, was conf-sanity test_74
20504         if ! which $CC > /dev/null 2>&1; then
20505                 skip_env "$CC is not installed"
20506         fi
20507
20508         local extra_flags=''
20509         local out=$TMP/$tfile
20510         local prefix=/usr/include/lustre
20511         local prog
20512
20513         if ! [[ -d $prefix ]]; then
20514                 # Assume we're running in tree and fixup the include path.
20515                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
20516                 extra_flags+=" -L$LUSTRE/utils/.lib"
20517         fi
20518
20519         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
20520                 $CC -Wall -Werror $extra_flags -o $out $prog -llustreapi ||
20521                         error "client api broken"
20522         done
20523         rm -f $out
20524 }
20525 run_test 400a "Lustre client api program can compile and link"
20526
20527 test_400b() { # LU-1606, LU-5011
20528         local header
20529         local out=$TMP/$tfile
20530         local prefix=/usr/include/linux/lustre
20531
20532         # We use a hard coded prefix so that this test will not fail
20533         # when run in tree. There are headers in lustre/include/lustre/
20534         # that are not packaged (like lustre_idl.h) and have more
20535         # complicated include dependencies (like config.h and lnet/types.h).
20536         # Since this test about correct packaging we just skip them when
20537         # they don't exist (see below) rather than try to fixup cppflags.
20538
20539         if ! which $CC > /dev/null 2>&1; then
20540                 skip_env "$CC is not installed"
20541         fi
20542
20543         for header in $prefix/*.h; do
20544                 if ! [[ -f "$header" ]]; then
20545                         continue
20546                 fi
20547
20548                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
20549                         continue # lustre_ioctl.h is internal header
20550                 fi
20551
20552                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
20553                         error "cannot compile '$header'"
20554         done
20555         rm -f $out
20556 }
20557 run_test 400b "packaged headers can be compiled"
20558
20559 test_401a() { #LU-7437
20560         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
20561         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
20562
20563         #count the number of parameters by "list_param -R"
20564         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
20565         #count the number of parameters by listing proc files
20566         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
20567         echo "proc_dirs='$proc_dirs'"
20568         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
20569         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
20570                       sort -u | wc -l)
20571
20572         [ $params -eq $procs ] ||
20573                 error "found $params parameters vs. $procs proc files"
20574
20575         # test the list_param -D option only returns directories
20576         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
20577         #count the number of parameters by listing proc directories
20578         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
20579                 sort -u | wc -l)
20580
20581         [ $params -eq $procs ] ||
20582                 error "found $params parameters vs. $procs proc files"
20583 }
20584 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
20585
20586 test_401b() {
20587         local save=$($LCTL get_param -n jobid_var)
20588         local tmp=testing
20589
20590         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
20591                 error "no error returned when setting bad parameters"
20592
20593         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
20594         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
20595
20596         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
20597         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
20598         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
20599 }
20600 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
20601
20602 test_401c() {
20603         local jobid_var_old=$($LCTL get_param -n jobid_var)
20604         local jobid_var_new
20605
20606         $LCTL set_param jobid_var= &&
20607                 error "no error returned for 'set_param a='"
20608
20609         jobid_var_new=$($LCTL get_param -n jobid_var)
20610         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
20611                 error "jobid_var was changed by setting without value"
20612
20613         $LCTL set_param jobid_var &&
20614                 error "no error returned for 'set_param a'"
20615
20616         jobid_var_new=$($LCTL get_param -n jobid_var)
20617         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
20618                 error "jobid_var was changed by setting without value"
20619 }
20620 run_test 401c "Verify 'lctl set_param' without value fails in either format."
20621
20622 test_401d() {
20623         local jobid_var_old=$($LCTL get_param -n jobid_var)
20624         local jobid_var_new
20625         local new_value="foo=bar"
20626
20627         $LCTL set_param jobid_var=$new_value ||
20628                 error "'set_param a=b' did not accept a value containing '='"
20629
20630         jobid_var_new=$($LCTL get_param -n jobid_var)
20631         [[ "$jobid_var_new" == "$new_value" ]] ||
20632                 error "'set_param a=b' failed on a value containing '='"
20633
20634         # Reset the jobid_var to test the other format
20635         $LCTL set_param jobid_var=$jobid_var_old
20636         jobid_var_new=$($LCTL get_param -n jobid_var)
20637         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
20638                 error "failed to reset jobid_var"
20639
20640         $LCTL set_param jobid_var $new_value ||
20641                 error "'set_param a b' did not accept a value containing '='"
20642
20643         jobid_var_new=$($LCTL get_param -n jobid_var)
20644         [[ "$jobid_var_new" == "$new_value" ]] ||
20645                 error "'set_param a b' failed on a value containing '='"
20646
20647         $LCTL set_param jobid_var $jobid_var_old
20648         jobid_var_new=$($LCTL get_param -n jobid_var)
20649         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
20650                 error "failed to reset jobid_var"
20651 }
20652 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
20653
20654 test_402() {
20655         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
20656         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
20657                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
20658         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
20659                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
20660                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
20661         remote_mds_nodsh && skip "remote MDS with nodsh"
20662
20663         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
20664 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
20665         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
20666         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
20667                 echo "Touch failed - OK"
20668 }
20669 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
20670
20671 test_403() {
20672         local file1=$DIR/$tfile.1
20673         local file2=$DIR/$tfile.2
20674         local tfile=$TMP/$tfile
20675
20676         rm -f $file1 $file2 $tfile
20677
20678         touch $file1
20679         ln $file1 $file2
20680
20681         # 30 sec OBD_TIMEOUT in ll_getattr()
20682         # right before populating st_nlink
20683         $LCTL set_param fail_loc=0x80001409
20684         stat -c %h $file1 > $tfile &
20685
20686         # create an alias, drop all locks and reclaim the dentry
20687         < $file2
20688         cancel_lru_locks mdc
20689         cancel_lru_locks osc
20690         sysctl -w vm.drop_caches=2
20691
20692         wait
20693
20694         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
20695
20696         rm -f $tfile $file1 $file2
20697 }
20698 run_test 403 "i_nlink should not drop to zero due to aliasing"
20699
20700 test_404() { # LU-6601
20701         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
20702                 skip "Need server version newer than 2.8.52"
20703         remote_mds_nodsh && skip "remote MDS with nodsh"
20704
20705         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
20706                 awk '/osp .*-osc-MDT/ { print $4}')
20707
20708         local osp
20709         for osp in $mosps; do
20710                 echo "Deactivate: " $osp
20711                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
20712                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
20713                         awk -vp=$osp '$4 == p { print $2 }')
20714                 [ $stat = IN ] || {
20715                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
20716                         error "deactivate error"
20717                 }
20718                 echo "Activate: " $osp
20719                 do_facet $SINGLEMDS $LCTL --device %$osp activate
20720                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
20721                         awk -vp=$osp '$4 == p { print $2 }')
20722                 [ $stat = UP ] || {
20723                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
20724                         error "activate error"
20725                 }
20726         done
20727 }
20728 run_test 404 "validate manual {de}activated works properly for OSPs"
20729
20730 test_405() {
20731         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
20732         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
20733                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
20734                         skip "Layout swap lock is not supported"
20735
20736         check_swap_layouts_support
20737
20738         test_mkdir $DIR/$tdir
20739         swap_lock_test -d $DIR/$tdir ||
20740                 error "One layout swap locked test failed"
20741 }
20742 run_test 405 "Various layout swap lock tests"
20743
20744 test_406() {
20745         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20746         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
20747         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
20748         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20749         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
20750                 skip "Need MDS version at least 2.8.50"
20751
20752         local def_stripe_size=$($LFS getstripe -S $MOUNT)
20753         local test_pool=$TESTNAME
20754
20755         pool_add $test_pool || error "pool_add failed"
20756         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
20757                 error "pool_add_targets failed"
20758
20759         save_layout_restore_at_exit $MOUNT
20760
20761         # parent set default stripe count only, child will stripe from both
20762         # parent and fs default
20763         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
20764                 error "setstripe $MOUNT failed"
20765         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
20766         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
20767         for i in $(seq 10); do
20768                 local f=$DIR/$tdir/$tfile.$i
20769                 touch $f || error "touch failed"
20770                 local count=$($LFS getstripe -c $f)
20771                 [ $count -eq $OSTCOUNT ] ||
20772                         error "$f stripe count $count != $OSTCOUNT"
20773                 local offset=$($LFS getstripe -i $f)
20774                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
20775                 local size=$($LFS getstripe -S $f)
20776                 [ $size -eq $((def_stripe_size * 2)) ] ||
20777                         error "$f stripe size $size != $((def_stripe_size * 2))"
20778                 local pool=$($LFS getstripe -p $f)
20779                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
20780         done
20781
20782         # change fs default striping, delete parent default striping, now child
20783         # will stripe from new fs default striping only
20784         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
20785                 error "change $MOUNT default stripe failed"
20786         $LFS setstripe -c 0 $DIR/$tdir ||
20787                 error "delete $tdir default stripe failed"
20788         for i in $(seq 11 20); do
20789                 local f=$DIR/$tdir/$tfile.$i
20790                 touch $f || error "touch $f failed"
20791                 local count=$($LFS getstripe -c $f)
20792                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
20793                 local offset=$($LFS getstripe -i $f)
20794                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
20795                 local size=$($LFS getstripe -S $f)
20796                 [ $size -eq $def_stripe_size ] ||
20797                         error "$f stripe size $size != $def_stripe_size"
20798                 local pool=$($LFS getstripe -p $f)
20799                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
20800         done
20801
20802         unlinkmany $DIR/$tdir/$tfile. 1 20
20803
20804         local f=$DIR/$tdir/$tfile
20805         pool_remove_all_targets $test_pool $f
20806         pool_remove $test_pool $f
20807 }
20808 run_test 406 "DNE support fs default striping"
20809
20810 test_407() {
20811         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20812         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
20813                 skip "Need MDS version at least 2.8.55"
20814         remote_mds_nodsh && skip "remote MDS with nodsh"
20815
20816         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
20817                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
20818         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
20819                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
20820         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
20821
20822         #define OBD_FAIL_DT_TXN_STOP    0x2019
20823         for idx in $(seq $MDSCOUNT); do
20824                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
20825         done
20826         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
20827         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
20828                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
20829         true
20830 }
20831 run_test 407 "transaction fail should cause operation fail"
20832
20833 test_408() {
20834         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
20835
20836         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
20837         lctl set_param fail_loc=0x8000040a
20838         # let ll_prepare_partial_page() fail
20839         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
20840
20841         rm -f $DIR/$tfile
20842
20843         # create at least 100 unused inodes so that
20844         # shrink_icache_memory(0) should not return 0
20845         touch $DIR/$tfile-{0..100}
20846         rm -f $DIR/$tfile-{0..100}
20847         sync
20848
20849         echo 2 > /proc/sys/vm/drop_caches
20850 }
20851 run_test 408 "drop_caches should not hang due to page leaks"
20852
20853 test_409()
20854 {
20855         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20856
20857         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
20858         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
20859         touch $DIR/$tdir/guard || error "(2) Fail to create"
20860
20861         local PREFIX=$(str_repeat 'A' 128)
20862         echo "Create 1K hard links start at $(date)"
20863         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
20864                 error "(3) Fail to hard link"
20865
20866         echo "Links count should be right although linkEA overflow"
20867         stat $DIR/$tdir/guard || error "(4) Fail to stat"
20868         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
20869         [ $linkcount -eq 1001 ] ||
20870                 error "(5) Unexpected hard links count: $linkcount"
20871
20872         echo "List all links start at $(date)"
20873         ls -l $DIR/$tdir/foo > /dev/null ||
20874                 error "(6) Fail to list $DIR/$tdir/foo"
20875
20876         echo "Unlink hard links start at $(date)"
20877         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
20878                 error "(7) Fail to unlink"
20879         echo "Unlink hard links finished at $(date)"
20880 }
20881 run_test 409 "Large amount of cross-MDTs hard links on the same file"
20882
20883 test_410()
20884 {
20885         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
20886                 skip "Need client version at least 2.9.59"
20887
20888         # Create a file, and stat it from the kernel
20889         local testfile=$DIR/$tfile
20890         touch $testfile
20891
20892         local run_id=$RANDOM
20893         local my_ino=$(stat --format "%i" $testfile)
20894
20895         # Try to insert the module. This will always fail as the
20896         # module is designed to not be inserted.
20897         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
20898             &> /dev/null
20899
20900         # Anything but success is a test failure
20901         dmesg | grep -q \
20902             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
20903             error "no inode match"
20904 }
20905 run_test 410 "Test inode number returned from kernel thread"
20906
20907 cleanup_test411_cgroup() {
20908         trap 0
20909         rmdir "$1"
20910 }
20911
20912 test_411() {
20913         local cg_basedir=/sys/fs/cgroup/memory
20914         # LU-9966
20915         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
20916                 skip "no setup for cgroup"
20917
20918         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
20919                 error "test file creation failed"
20920         cancel_lru_locks osc
20921
20922         # Create a very small memory cgroup to force a slab allocation error
20923         local cgdir=$cg_basedir/osc_slab_alloc
20924         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
20925         trap "cleanup_test411_cgroup $cgdir" EXIT
20926         echo 2M > $cgdir/memory.kmem.limit_in_bytes
20927         echo 1M > $cgdir/memory.limit_in_bytes
20928
20929         # Should not LBUG, just be killed by oom-killer
20930         # dd will return 0 even allocation failure in some environment.
20931         # So don't check return value
20932         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
20933         cleanup_test411_cgroup $cgdir
20934
20935         return 0
20936 }
20937 run_test 411 "Slab allocation error with cgroup does not LBUG"
20938
20939 test_412() {
20940         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20941         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
20942                 skip "Need server version at least 2.10.55"
20943         fi
20944
20945         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
20946                 error "mkdir failed"
20947         $LFS getdirstripe $DIR/$tdir
20948         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
20949         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
20950                 error "expect $((MDSCOUT - 1)) get $stripe_index"
20951         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
20952         [ $stripe_count -eq 2 ] ||
20953                 error "expect 2 get $stripe_count"
20954 }
20955 run_test 412 "mkdir on specific MDTs"
20956
20957 test_qos_mkdir() {
20958         local mkdir_cmd=$1
20959         local stripe_count=$2
20960         local mdts=$(comma_list $(mdts_nodes))
20961
20962         local testdir
20963         local lmv_qos_prio_free
20964         local lmv_qos_threshold_rr
20965         local lmv_qos_maxage
20966         local lod_qos_prio_free
20967         local lod_qos_threshold_rr
20968         local lod_qos_maxage
20969         local count
20970         local i
20971
20972         lmv_qos_prio_free=$($LCTL get_param -n lmv.*.qos_prio_free | head -n1)
20973         lmv_qos_prio_free=${lmv_qos_prio_free%%%}
20974         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
20975                 head -n1)
20976         lmv_qos_threshold_rr=${lmv_qos_threshold_rr%%%}
20977         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
20978         stack_trap "$LCTL set_param \
20979                 lmv.*.qos_prio_free=$lmv_qos_prio_free > /dev/null" EXIT
20980         stack_trap "$LCTL set_param \
20981                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null" EXIT
20982         stack_trap "$LCTL set_param \
20983                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null" EXIT
20984
20985         lod_qos_prio_free=$(do_facet mds1 $LCTL get_param -n \
20986                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_prio_free | head -n1)
20987         lod_qos_prio_free=${lod_qos_prio_free%%%}
20988         lod_qos_threshold_rr=$(do_facet mds1 $LCTL get_param -n \
20989                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_threshold_rr | head -n1)
20990         lod_qos_threshold_rr=${lod_qos_threshold_rr%%%}
20991         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
20992                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
20993         stack_trap "do_nodes $mdts $LCTL set_param \
20994                 lod.*.mdt_qos_prio_free=$lod_qos_prio_free > /dev/null" EXIT
20995         stack_trap "do_nodes $mdts $LCTL set_param \
20996                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null" \
20997                 EXIT
20998         stack_trap "do_nodes $mdts $LCTL set_param \
20999                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null" EXIT
21000
21001         echo
21002         echo "Mkdir (stripe_count $stripe_count) roundrobin:"
21003
21004         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
21005         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=100 > /dev/null
21006
21007         testdir=$DIR/$tdir-s$stripe_count/rr
21008
21009         for i in $(seq $((100 * MDSCOUNT))); do
21010                 eval $mkdir_cmd $testdir/subdir$i ||
21011                         error "$mkdir_cmd subdir$i failed"
21012         done
21013
21014         for i in $(seq $MDSCOUNT); do
21015                 count=$($LFS getdirstripe -i $testdir/* |
21016                                 grep ^$((i - 1))$ | wc -l)
21017                 echo "$count directories created on MDT$((i - 1))"
21018                 [ $count -eq 100 ] || error "subdirs are not evenly distributed"
21019
21020                 if [ $stripe_count -gt 1 ]; then
21021                         count=$($LFS getdirstripe $testdir/* |
21022                                 grep -P "^\s+$((i - 1))\t" | wc -l)
21023                         echo "$count stripes created on MDT$((i - 1))"
21024                         # deviation should < 5% of average
21025                         [ $count -lt $((95 * stripe_count)) ] ||
21026                         [ $count -gt $((105 * stripe_count)) ] &&
21027                                 error "stripes are not evenly distributed"
21028                 fi
21029         done
21030
21031         $LCTL set_param lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null
21032         do_nodes $mdts $LCTL set_param \
21033                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null
21034
21035         echo
21036         echo "Check for uneven MDTs: "
21037
21038         local ffree
21039         local bavail
21040         local max
21041         local min
21042         local max_index
21043         local min_index
21044         local tmp
21045
21046         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
21047         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
21048         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
21049
21050         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
21051         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
21052         max_index=0
21053         min_index=0
21054         for ((i = 1; i < ${#ffree[@]}; i++)); do
21055                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
21056                 if [ $tmp -gt $max ]; then
21057                         max=$tmp
21058                         max_index=$i
21059                 fi
21060                 if [ $tmp -lt $min ]; then
21061                         min=$tmp
21062                         min_index=$i
21063                 fi
21064         done
21065
21066         [ ${ffree[min_index]} -eq 0 ] &&
21067                 skip "no free files in MDT$min_index"
21068         [ ${ffree[min_index]} -gt 100000000 ] &&
21069                 skip "too much free files in MDT$min_index"
21070
21071         # Check if we need to generate uneven MDTs
21072         local threshold=50
21073         local diff=$(((max - min) * 100 / min))
21074         local value="$(generate_string 1024)"
21075
21076         while [ $diff -lt $threshold ]; do
21077                 # generate uneven MDTs, create till $threshold% diff
21078                 echo -n "weight diff=$diff% must be > $threshold% ..."
21079                 count=$((${ffree[min_index]} / 10))
21080                 # 50 sec per 10000 files in vm
21081                 [ $count -gt 40000 ] && [ "$SLOW" = "no" ] &&
21082                         skip "$count files to create"
21083                 echo "Fill MDT$min_index with $count files"
21084                 [ -d $DIR/$tdir-MDT$min_index ] ||
21085                         $LFS mkdir -i $min_index $DIR/$tdir-MDT$min_index ||
21086                         error "mkdir $tdir-MDT$min_index failed"
21087                 for i in $(seq $count); do
21088                         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE \
21089                                 $DIR/$tdir-MDT$min_index/f$j_$i > /dev/null ||
21090                                 error "create f$j_$i failed"
21091                         setfattr -n user.413b -v $value \
21092                                 $DIR/$tdir-MDT$min_index/f$j_$i ||
21093                                 error "setfattr f$j_$i failed"
21094                 done
21095
21096                 ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-*.filesfree))
21097                 bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-*.kbytesavail))
21098                 max=$(((${ffree[max_index]} >> 8) * \
21099                         (${bavail[max_index]} * bsize >> 16)))
21100                 min=$(((${ffree[min_index]} >> 8) * \
21101                         (${bavail[min_index]} * bsize >> 16)))
21102                 diff=$(((max - min) * 100 / min))
21103         done
21104
21105         echo "MDT filesfree available: ${ffree[@]}"
21106         echo "MDT blocks available: ${bavail[@]}"
21107         echo "weight diff=$diff%"
21108
21109         echo
21110         echo "Mkdir (stripe_count $stripe_count) with balanced space usage:"
21111
21112         $LCTL set_param lmv.*.qos_prio_free=100 > /dev/null
21113         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_prio_free=100 > /dev/null
21114         # decrease statfs age, so that it can be updated in time
21115         $LCTL set_param lmv.*.qos_maxage=1 > /dev/null
21116         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_maxage=1 > /dev/null
21117
21118         sleep 1
21119
21120         testdir=$DIR/$tdir-s$stripe_count/qos
21121
21122         for i in $(seq $((100 * MDSCOUNT))); do
21123                 eval $mkdir_cmd $testdir/subdir$i ||
21124                         error "$mkdir_cmd subdir$i failed"
21125         done
21126
21127         for i in $(seq $MDSCOUNT); do
21128                 count=$($LFS getdirstripe -i $testdir/* | grep ^$((i - 1))$ |
21129                         wc -l)
21130                 echo "$count directories created on MDT$((i - 1))"
21131
21132                 if [ $stripe_count -gt 1 ]; then
21133                         count=$($LFS getdirstripe $testdir/* |
21134                                 grep -P "^\s+$((i - 1))\t" | wc -l)
21135                         echo "$count stripes created on MDT$((i - 1))"
21136                 fi
21137         done
21138
21139         max=$($LFS getdirstripe -i $testdir/* | grep ^$max_index$ | wc -l)
21140         min=$($LFS getdirstripe -i $testdir/* | grep ^$min_index$ | wc -l)
21141
21142         # D-value should > 10% of averge
21143         [ $((max - min)) -lt 10 ] &&
21144                 error "subdirs shouldn't be evenly distributed"
21145
21146         # ditto
21147         if [ $stripe_count -gt 1 ]; then
21148                 max=$($LFS getdirstripe $testdir/* |
21149                         grep -P "^\s+$max_index\t" | wc -l)
21150                 min=$($LFS getdirstripe $testdir/* |
21151                         grep -P "^\s+$min_index\t" | wc -l)
21152                 [ $((max - min)) -le $((10 * stripe_count)) ] &&
21153                         error "stripes shouldn't be evenly distributed"|| true
21154         fi
21155 }
21156
21157 test_413a() {
21158         [ $MDSCOUNT -lt 2 ] &&
21159                 skip "We need at least 2 MDTs for this test"
21160
21161         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
21162                 skip "Need server version at least 2.12.52"
21163
21164         local stripe_count
21165
21166         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
21167                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
21168                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
21169                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
21170                 test_qos_mkdir "$LFS mkdir -c $stripe_count" $stripe_count
21171         done
21172 }
21173 run_test 413a "QoS mkdir with 'lfs mkdir -i -1'"
21174
21175 test_413b() {
21176         [ $MDSCOUNT -lt 2 ] &&
21177                 skip "We need at least 2 MDTs for this test"
21178
21179         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
21180                 skip "Need server version at least 2.12.52"
21181
21182         local stripe_count
21183
21184         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
21185                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
21186                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
21187                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
21188                 $LFS setdirstripe -D -c $stripe_count \
21189                         $DIR/$tdir-s$stripe_count/rr ||
21190                         error "setdirstripe failed"
21191                 $LFS setdirstripe -D -c $stripe_count \
21192                         $DIR/$tdir-s$stripe_count/qos ||
21193                         error "setdirstripe failed"
21194                 test_qos_mkdir "mkdir" $stripe_count
21195         done
21196 }
21197 run_test 413b "QoS mkdir under dir whose default LMV starting MDT offset is -1"
21198
21199 test_414() {
21200 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
21201         $LCTL set_param fail_loc=0x80000521
21202         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
21203         rm -f $DIR/$tfile
21204 }
21205 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
21206
21207 test_415() {
21208         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21209         [ $(lustre_version_code mds1) -lt $(version_code 2.11.52) ] &&
21210                 skip "Need server version at least 2.11.52"
21211
21212         # LU-11102
21213         local total
21214         local setattr_pid
21215         local start_time
21216         local end_time
21217         local duration
21218
21219         total=500
21220         # this test may be slow on ZFS
21221         [ "$mds1_FSTYPE" == "zfs" ] && total=100
21222
21223         # though this test is designed for striped directory, let's test normal
21224         # directory too since lock is always saved as CoS lock.
21225         test_mkdir $DIR/$tdir || error "mkdir $tdir"
21226         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
21227
21228         (
21229                 while true; do
21230                         touch $DIR/$tdir
21231                 done
21232         ) &
21233         setattr_pid=$!
21234
21235         start_time=$(date +%s)
21236         for i in $(seq $total); do
21237                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
21238                         > /dev/null
21239         done
21240         end_time=$(date +%s)
21241         duration=$((end_time - start_time))
21242
21243         kill -9 $setattr_pid
21244
21245         echo "rename $total files took $duration sec"
21246         [ $duration -lt 100 ] || error "rename took $duration sec"
21247 }
21248 run_test 415 "lock revoke is not missing"
21249
21250 test_416() {
21251         [ $(lustre_version_code mds1) -lt $(version_code 2.11.55) ] &&
21252                 skip "Need server version at least 2.11.55"
21253
21254         # define OBD_FAIL_OSD_TXN_START    0x19a
21255         do_facet mds1 lctl set_param fail_loc=0x19a
21256
21257         lfs mkdir -c $MDSCOUNT $DIR/$tdir
21258
21259         true
21260 }
21261 run_test 416 "transaction start failure won't cause system hung"
21262
21263 cleanup_417() {
21264         trap 0
21265         do_nodes $(comma_list $(mdts_nodes)) \
21266                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
21267         do_nodes $(comma_list $(mdts_nodes)) \
21268                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
21269         do_nodes $(comma_list $(mdts_nodes)) \
21270                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
21271 }
21272
21273 test_417() {
21274         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21275         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
21276                 skip "Need MDS version at least 2.11.56"
21277
21278         trap cleanup_417 RETURN EXIT
21279
21280         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
21281         do_nodes $(comma_list $(mdts_nodes)) \
21282                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
21283         $LFS migrate -m 0 $DIR/$tdir.1 &&
21284                 error "migrate dir $tdir.1 should fail"
21285
21286         do_nodes $(comma_list $(mdts_nodes)) \
21287                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
21288         $LFS mkdir -i 1 $DIR/$tdir.2 &&
21289                 error "create remote dir $tdir.2 should fail"
21290
21291         do_nodes $(comma_list $(mdts_nodes)) \
21292                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
21293         $LFS mkdir -c 2 $DIR/$tdir.3 &&
21294                 error "create striped dir $tdir.3 should fail"
21295         true
21296 }
21297 run_test 417 "disable remote dir, striped dir and dir migration"
21298
21299 # Checks that the outputs of df [-i] and lfs df [-i] match
21300 #
21301 # usage: check_lfs_df <blocks | inodes> <mountpoint>
21302 check_lfs_df() {
21303         local dir=$2
21304         local inodes
21305         local df_out
21306         local lfs_df_out
21307         local count
21308         local passed=false
21309
21310         # blocks or inodes
21311         [ "$1" == "blocks" ] && inodes= || inodes="-i"
21312
21313         for count in {1..100}; do
21314                 cancel_lru_locks
21315                 sync; sleep 0.2
21316
21317                 # read the lines of interest
21318                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
21319                         error "df $inodes $dir | tail -n +2 failed"
21320                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
21321                         error "lfs df $inodes $dir | grep summary: failed"
21322
21323                 # skip first substrings of each output as they are different
21324                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
21325                 # compare the two outputs
21326                 passed=true
21327                 for i in {1..5}; do
21328                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
21329                 done
21330                 $passed && break
21331         done
21332
21333         if ! $passed; then
21334                 df -P $inodes $dir
21335                 echo
21336                 lfs df $inodes $dir
21337                 error "df and lfs df $1 output mismatch: "      \
21338                       "df ${inodes}: ${df_out[*]}, "            \
21339                       "lfs df ${inodes}: ${lfs_df_out[*]}"
21340         fi
21341 }
21342
21343 test_418() {
21344         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21345
21346         local dir=$DIR/$tdir
21347         local numfiles=$((RANDOM % 4096 + 2))
21348         local numblocks=$((RANDOM % 256 + 1))
21349
21350         wait_delete_completed
21351         test_mkdir $dir
21352
21353         # check block output
21354         check_lfs_df blocks $dir
21355         # check inode output
21356         check_lfs_df inodes $dir
21357
21358         # create a single file and retest
21359         echo "Creating a single file and testing"
21360         createmany -o $dir/$tfile- 1 &>/dev/null ||
21361                 error "creating 1 file in $dir failed"
21362         check_lfs_df blocks $dir
21363         check_lfs_df inodes $dir
21364
21365         # create a random number of files
21366         echo "Creating $((numfiles - 1)) files and testing"
21367         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
21368                 error "creating $((numfiles - 1)) files in $dir failed"
21369
21370         # write a random number of blocks to the first test file
21371         echo "Writing $numblocks 4K blocks and testing"
21372         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
21373                 count=$numblocks &>/dev/null ||
21374                 error "dd to $dir/${tfile}-0 failed"
21375
21376         # retest
21377         check_lfs_df blocks $dir
21378         check_lfs_df inodes $dir
21379
21380         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
21381                 error "unlinking $numfiles files in $dir failed"
21382 }
21383 run_test 418 "df and lfs df outputs match"
21384
21385 test_419()
21386 {
21387         local dir=$DIR/$tdir
21388
21389         mkdir -p $dir
21390         touch $dir/file
21391
21392         cancel_lru_locks mdc
21393
21394         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
21395         $LCTL set_param fail_loc=0x1410
21396         cat $dir/file
21397         $LCTL set_param fail_loc=0
21398         rm -rf $dir
21399 }
21400 run_test 419 "Verify open file by name doesn't crash kernel"
21401
21402 test_420()
21403 {
21404         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
21405                 skip "Need MDS version at least 2.12.53"
21406
21407         local SAVE_UMASK=$(umask)
21408         local dir=$DIR/$tdir
21409         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
21410
21411         mkdir -p $dir
21412         umask 0000
21413         mkdir -m03777 $dir/testdir
21414         ls -dn $dir/testdir
21415         # Need to remove trailing '.' when SELinux is enabled
21416         local dirperms=$(ls -dn $dir/testdir |
21417                          awk '{ sub(/\.$/, "", $1); print $1}')
21418         [ $dirperms == "drwxrwsrwt" ] ||
21419                 error "incorrect perms on $dir/testdir"
21420
21421         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
21422                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
21423         ls -n $dir/testdir/testfile
21424         local fileperms=$(ls -n $dir/testdir/testfile |
21425                           awk '{ sub(/\.$/, "", $1); print $1}')
21426         [ $fileperms == "-rwxr-xr-x" ] ||
21427                 error "incorrect perms on $dir/testdir/testfile"
21428
21429         umask $SAVE_UMASK
21430 }
21431 run_test 420 "clear SGID bit on non-directories for non-members"
21432
21433 test_421a() {
21434         local cnt
21435         local fid1
21436         local fid2
21437
21438         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21439                 skip "Need MDS version at least 2.12.54"
21440
21441         test_mkdir $DIR/$tdir
21442         createmany -o $DIR/$tdir/f 3
21443         cnt=$(ls -1 $DIR/$tdir | wc -l)
21444         [ $cnt != 3 ] && error "unexpected #files: $cnt"
21445
21446         fid1=$(lfs path2fid $DIR/$tdir/f1)
21447         fid2=$(lfs path2fid $DIR/$tdir/f2)
21448         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
21449
21450         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
21451         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
21452
21453         cnt=$(ls -1 $DIR/$tdir | wc -l)
21454         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
21455
21456         rm -f $DIR/$tdir/f3 || error "can't remove f3"
21457         createmany -o $DIR/$tdir/f 3
21458         cnt=$(ls -1 $DIR/$tdir | wc -l)
21459         [ $cnt != 3 ] && error "unexpected #files: $cnt"
21460
21461         fid1=$(lfs path2fid $DIR/$tdir/f1)
21462         fid2=$(lfs path2fid $DIR/$tdir/f2)
21463         echo "remove using fsname $FSNAME"
21464         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
21465
21466         cnt=$(ls -1 $DIR/$tdir | wc -l)
21467         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
21468 }
21469 run_test 421a "simple rm by fid"
21470
21471 test_421b() {
21472         local cnt
21473         local FID1
21474         local FID2
21475
21476         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21477                 skip "Need MDS version at least 2.12.54"
21478
21479         test_mkdir $DIR/$tdir
21480         createmany -o $DIR/$tdir/f 3
21481         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
21482         MULTIPID=$!
21483
21484         FID1=$(lfs path2fid $DIR/$tdir/f1)
21485         FID2=$(lfs path2fid $DIR/$tdir/f2)
21486         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
21487
21488         kill -USR1 $MULTIPID
21489         wait
21490
21491         cnt=$(ls $DIR/$tdir | wc -l)
21492         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
21493 }
21494 run_test 421b "rm by fid on open file"
21495
21496 test_421c() {
21497         local cnt
21498         local FIDS
21499
21500         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21501                 skip "Need MDS version at least 2.12.54"
21502
21503         test_mkdir $DIR/$tdir
21504         createmany -o $DIR/$tdir/f 3
21505         touch $DIR/$tdir/$tfile
21506         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
21507         cnt=$(ls -1 $DIR/$tdir | wc -l)
21508         [ $cnt != 184 ] && error "unexpected #files: $cnt"
21509
21510         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
21511         $LFS rmfid $DIR $FID1 || error "rmfid failed"
21512
21513         cnt=$(ls $DIR/$tdir | wc -l)
21514         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
21515 }
21516 run_test 421c "rm by fid against hardlinked files"
21517
21518 test_421d() {
21519         local cnt
21520         local FIDS
21521
21522         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21523                 skip "Need MDS version at least 2.12.54"
21524
21525         test_mkdir $DIR/$tdir
21526         createmany -o $DIR/$tdir/f 4097
21527         cnt=$(ls -1 $DIR/$tdir | wc -l)
21528         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
21529
21530         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
21531         $LFS rmfid $DIR $FIDS || error "rmfid failed"
21532
21533         cnt=$(ls $DIR/$tdir | wc -l)
21534         rm -rf $DIR/$tdir
21535         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21536 }
21537 run_test 421d "rmfid en masse"
21538
21539 test_421e() {
21540         local cnt
21541         local FID
21542
21543         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21544         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21545                 skip "Need MDS version at least 2.12.54"
21546
21547         mkdir -p $DIR/$tdir
21548         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
21549         createmany -o $DIR/$tdir/striped_dir/f 512
21550         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21551         [ $cnt != 512 ] && error "unexpected #files: $cnt"
21552
21553         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
21554                 sed "s/[/][^:]*://g")
21555         $LFS rmfid $DIR $FIDS || error "rmfid failed"
21556
21557         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
21558         rm -rf $DIR/$tdir
21559         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21560 }
21561 run_test 421e "rmfid in DNE"
21562
21563 test_421f() {
21564         local cnt
21565         local FID
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         touch $DIR/$tdir/f
21572         cnt=$(ls -1 $DIR/$tdir | wc -l)
21573         [ $cnt != 1 ] && error "unexpected #files: $cnt"
21574
21575         FID=$(lfs path2fid $DIR/$tdir/f)
21576         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
21577         # rmfid should fail
21578         cnt=$(ls -1 $DIR/$tdir | wc -l)
21579         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
21580
21581         chmod a+rw $DIR/$tdir
21582         ls -la $DIR/$tdir
21583         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
21584         # rmfid should fail
21585         cnt=$(ls -1 $DIR/$tdir | wc -l)
21586         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
21587
21588         rm -f $DIR/$tdir/f
21589         $RUNAS touch $DIR/$tdir/f
21590         FID=$(lfs path2fid $DIR/$tdir/f)
21591         echo "rmfid as root"
21592         $LFS rmfid $DIR $FID || error "rmfid as root failed"
21593         cnt=$(ls -1 $DIR/$tdir | wc -l)
21594         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
21595
21596         rm -f $DIR/$tdir/f
21597         $RUNAS touch $DIR/$tdir/f
21598         cnt=$(ls -1 $DIR/$tdir | wc -l)
21599         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
21600         FID=$(lfs path2fid $DIR/$tdir/f)
21601         # rmfid w/o user_fid2path mount option should fail
21602         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
21603         cnt=$(ls -1 $DIR/$tdir | wc -l)
21604         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
21605
21606         umount_client $MOUNT || error "failed to umount client"
21607         mount_client $MOUNT "$MOUNT_OPTS,user_fid2path" ||
21608                 error "failed to mount client'"
21609
21610         $RUNAS $LFS rmfid $DIR $FID || error "rmfid failed"
21611         # rmfid should succeed
21612         cnt=$(ls -1 $DIR/$tdir | wc -l)
21613         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
21614
21615         # rmfid shouldn't allow to remove files due to dir's permission
21616         chmod a+rwx $DIR/$tdir
21617         touch $DIR/$tdir/f
21618         ls -la $DIR/$tdir
21619         FID=$(lfs path2fid $DIR/$tdir/f)
21620         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail"
21621
21622         umount_client $MOUNT || error "failed to umount client"
21623         mount_client $MOUNT "$MOUNT_OPTS" ||
21624                 error "failed to mount client'"
21625
21626 }
21627 run_test 421f "rmfid checks permissions"
21628
21629 test_421g() {
21630         local cnt
21631         local FIDS
21632
21633         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21634         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21635                 skip "Need MDS version at least 2.12.54"
21636
21637         mkdir -p $DIR/$tdir
21638         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
21639         createmany -o $DIR/$tdir/striped_dir/f 512
21640         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21641         [ $cnt != 512 ] && error "unexpected #files: $cnt"
21642
21643         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
21644                 sed "s/[/][^:]*://g")
21645
21646         rm -f $DIR/$tdir/striped_dir/f1*
21647         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21648         removed=$((512 - cnt))
21649
21650         # few files have been just removed, so we expect
21651         # rmfid to fail on their fids
21652         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
21653         [ $removed != $errors ] && error "$errors != $removed"
21654
21655         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
21656         rm -rf $DIR/$tdir
21657         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21658 }
21659 run_test 421g "rmfid to return errors properly"
21660
21661 test_422() {
21662         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d1
21663         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d2
21664         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d3
21665         dd if=/dev/zero of=$DIR/$tdir/d1/file1 bs=1k count=1
21666         dd if=/dev/zero of=$DIR/$tdir/d2/file1 bs=1k count=1
21667
21668         local amc=$(at_max_get client)
21669         local amo=$(at_max_get mds1)
21670         local timeout=`lctl get_param -n timeout`
21671
21672         at_max_set 0 client
21673         at_max_set 0 mds1
21674
21675 #define OBD_FAIL_PTLRPC_PAUSE_REQ        0x50a
21676         do_facet mds1 $LCTL set_param fail_loc=0x8000050a \
21677                         fail_val=$(((2*timeout + 10)*1000))
21678         touch $DIR/$tdir/d3/file &
21679         sleep 2
21680 #define OBD_FAIL_TGT_REPLY_DATA_RACE     0x722
21681         do_facet mds1 $LCTL set_param fail_loc=0x80000722 \
21682                         fail_val=$((2*timeout + 5))
21683         mv $DIR/$tdir/d1/file1 $DIR/$tdir/d1/file2 &
21684         local pid=$!
21685         sleep 1
21686         kill -9 $pid
21687         sleep $((2 * timeout))
21688         echo kill $pid
21689         kill -9 $pid
21690         lctl mark touch
21691         touch $DIR/$tdir/d2/file3
21692         touch $DIR/$tdir/d2/file4
21693         touch $DIR/$tdir/d2/file5
21694
21695         wait
21696         at_max_set $amc client
21697         at_max_set $amo mds1
21698
21699         # LU-12838 - verify the ptlrpc thread watchdog is not always throttled
21700         do_facet mds1 "dmesg | grep 'Dumping the stack trace for debugging'" ||
21701                 error "Watchdog is always throttled"
21702 }
21703 run_test 422 "kill a process with RPC in progress"
21704
21705 prep_801() {
21706         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
21707         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
21708                 skip "Need server version at least 2.9.55"
21709
21710         start_full_debug_logging
21711 }
21712
21713 post_801() {
21714         stop_full_debug_logging
21715 }
21716
21717 barrier_stat() {
21718         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
21719                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
21720                            awk '/The barrier for/ { print $7 }')
21721                 echo $st
21722         else
21723                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
21724                 echo \'$st\'
21725         fi
21726 }
21727
21728 barrier_expired() {
21729         local expired
21730
21731         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
21732                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
21733                           awk '/will be expired/ { print $7 }')
21734         else
21735                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
21736         fi
21737
21738         echo $expired
21739 }
21740
21741 test_801a() {
21742         prep_801
21743
21744         echo "Start barrier_freeze at: $(date)"
21745         #define OBD_FAIL_BARRIER_DELAY          0x2202
21746         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
21747         # Do not reduce barrier time - See LU-11873
21748         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
21749
21750         sleep 2
21751         local b_status=$(barrier_stat)
21752         echo "Got barrier status at: $(date)"
21753         [ "$b_status" = "'freezing_p1'" ] ||
21754                 error "(1) unexpected barrier status $b_status"
21755
21756         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
21757         wait
21758         b_status=$(barrier_stat)
21759         [ "$b_status" = "'frozen'" ] ||
21760                 error "(2) unexpected barrier status $b_status"
21761
21762         local expired=$(barrier_expired)
21763         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
21764         sleep $((expired + 3))
21765
21766         b_status=$(barrier_stat)
21767         [ "$b_status" = "'expired'" ] ||
21768                 error "(3) unexpected barrier status $b_status"
21769
21770         # Do not reduce barrier time - See LU-11873
21771         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
21772                 error "(4) fail to freeze barrier"
21773
21774         b_status=$(barrier_stat)
21775         [ "$b_status" = "'frozen'" ] ||
21776                 error "(5) unexpected barrier status $b_status"
21777
21778         echo "Start barrier_thaw at: $(date)"
21779         #define OBD_FAIL_BARRIER_DELAY          0x2202
21780         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
21781         do_facet mgs $LCTL barrier_thaw $FSNAME &
21782
21783         sleep 2
21784         b_status=$(barrier_stat)
21785         echo "Got barrier status at: $(date)"
21786         [ "$b_status" = "'thawing'" ] ||
21787                 error "(6) unexpected barrier status $b_status"
21788
21789         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
21790         wait
21791         b_status=$(barrier_stat)
21792         [ "$b_status" = "'thawed'" ] ||
21793                 error "(7) unexpected barrier status $b_status"
21794
21795         #define OBD_FAIL_BARRIER_FAILURE        0x2203
21796         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
21797         do_facet mgs $LCTL barrier_freeze $FSNAME
21798
21799         b_status=$(barrier_stat)
21800         [ "$b_status" = "'failed'" ] ||
21801                 error "(8) unexpected barrier status $b_status"
21802
21803         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
21804         do_facet mgs $LCTL barrier_thaw $FSNAME
21805
21806         post_801
21807 }
21808 run_test 801a "write barrier user interfaces and stat machine"
21809
21810 test_801b() {
21811         prep_801
21812
21813         mkdir $DIR/$tdir || error "(1) fail to mkdir"
21814         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
21815         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
21816         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
21817         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
21818
21819         cancel_lru_locks mdc
21820
21821         # 180 seconds should be long enough
21822         do_facet mgs $LCTL barrier_freeze $FSNAME 180
21823
21824         local b_status=$(barrier_stat)
21825         [ "$b_status" = "'frozen'" ] ||
21826                 error "(6) unexpected barrier status $b_status"
21827
21828         mkdir $DIR/$tdir/d0/d10 &
21829         mkdir_pid=$!
21830
21831         touch $DIR/$tdir/d1/f13 &
21832         touch_pid=$!
21833
21834         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
21835         ln_pid=$!
21836
21837         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
21838         mv_pid=$!
21839
21840         rm -f $DIR/$tdir/d4/f12 &
21841         rm_pid=$!
21842
21843         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
21844
21845         # To guarantee taht the 'stat' is not blocked
21846         b_status=$(barrier_stat)
21847         [ "$b_status" = "'frozen'" ] ||
21848                 error "(8) unexpected barrier status $b_status"
21849
21850         # let above commands to run at background
21851         sleep 5
21852
21853         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
21854         ps -p $touch_pid || error "(10) touch should be blocked"
21855         ps -p $ln_pid || error "(11) link should be blocked"
21856         ps -p $mv_pid || error "(12) rename should be blocked"
21857         ps -p $rm_pid || error "(13) unlink should be blocked"
21858
21859         b_status=$(barrier_stat)
21860         [ "$b_status" = "'frozen'" ] ||
21861                 error "(14) unexpected barrier status $b_status"
21862
21863         do_facet mgs $LCTL barrier_thaw $FSNAME
21864         b_status=$(barrier_stat)
21865         [ "$b_status" = "'thawed'" ] ||
21866                 error "(15) unexpected barrier status $b_status"
21867
21868         wait $mkdir_pid || error "(16) mkdir should succeed"
21869         wait $touch_pid || error "(17) touch should succeed"
21870         wait $ln_pid || error "(18) link should succeed"
21871         wait $mv_pid || error "(19) rename should succeed"
21872         wait $rm_pid || error "(20) unlink should succeed"
21873
21874         post_801
21875 }
21876 run_test 801b "modification will be blocked by write barrier"
21877
21878 test_801c() {
21879         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21880
21881         prep_801
21882
21883         stop mds2 || error "(1) Fail to stop mds2"
21884
21885         do_facet mgs $LCTL barrier_freeze $FSNAME 30
21886
21887         local b_status=$(barrier_stat)
21888         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
21889                 do_facet mgs $LCTL barrier_thaw $FSNAME
21890                 error "(2) unexpected barrier status $b_status"
21891         }
21892
21893         do_facet mgs $LCTL barrier_rescan $FSNAME ||
21894                 error "(3) Fail to rescan barrier bitmap"
21895
21896         # Do not reduce barrier time - See LU-11873
21897         do_facet mgs $LCTL barrier_freeze $FSNAME 20
21898
21899         b_status=$(barrier_stat)
21900         [ "$b_status" = "'frozen'" ] ||
21901                 error "(4) unexpected barrier status $b_status"
21902
21903         do_facet mgs $LCTL barrier_thaw $FSNAME
21904         b_status=$(barrier_stat)
21905         [ "$b_status" = "'thawed'" ] ||
21906                 error "(5) unexpected barrier status $b_status"
21907
21908         local devname=$(mdsdevname 2)
21909
21910         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
21911
21912         do_facet mgs $LCTL barrier_rescan $FSNAME ||
21913                 error "(7) Fail to rescan barrier bitmap"
21914
21915         post_801
21916 }
21917 run_test 801c "rescan barrier bitmap"
21918
21919 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
21920 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
21921 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
21922 saved_MOUNT_OPTS=$MOUNT_OPTS
21923
21924 cleanup_802a() {
21925         trap 0
21926
21927         stopall
21928         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
21929         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
21930         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
21931         MOUNT_OPTS=$saved_MOUNT_OPTS
21932         setupall
21933 }
21934
21935 test_802a() {
21936         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
21937         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
21938         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
21939                 skip "Need server version at least 2.9.55"
21940
21941         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
21942
21943         mkdir $DIR/$tdir || error "(1) fail to mkdir"
21944
21945         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
21946                 error "(2) Fail to copy"
21947
21948         trap cleanup_802a EXIT
21949
21950         # sync by force before remount as readonly
21951         sync; sync_all_data; sleep 3; sync_all_data
21952
21953         stopall
21954
21955         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
21956         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
21957         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
21958
21959         echo "Mount the server as read only"
21960         setupall server_only || error "(3) Fail to start servers"
21961
21962         echo "Mount client without ro should fail"
21963         mount_client $MOUNT &&
21964                 error "(4) Mount client without 'ro' should fail"
21965
21966         echo "Mount client with ro should succeed"
21967         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
21968         mount_client $MOUNT ||
21969                 error "(5) Mount client with 'ro' should succeed"
21970
21971         echo "Modify should be refused"
21972         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
21973
21974         echo "Read should be allowed"
21975         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
21976                 error "(7) Read should succeed under ro mode"
21977
21978         cleanup_802a
21979 }
21980 run_test 802a "simulate readonly device"
21981
21982 test_802b() {
21983         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21984         remote_mds_nodsh && skip "remote MDS with nodsh"
21985
21986         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
21987                 skip "readonly option not available"
21988
21989         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
21990
21991         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
21992                 error "(2) Fail to copy"
21993
21994         # write back all cached data before setting MDT to readonly
21995         cancel_lru_locks
21996         sync_all_data
21997
21998         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
21999         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
22000
22001         echo "Modify should be refused"
22002         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
22003
22004         echo "Read should be allowed"
22005         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
22006                 error "(7) Read should succeed under ro mode"
22007
22008         # disable readonly
22009         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
22010 }
22011 run_test 802b "be able to set MDTs to readonly"
22012
22013 test_803() {
22014         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22015         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
22016                 skip "MDS needs to be newer than 2.10.54"
22017
22018         mkdir -p $DIR/$tdir
22019         # Create some objects on all MDTs to trigger related logs objects
22020         for idx in $(seq $MDSCOUNT); do
22021                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
22022                         $DIR/$tdir/dir${idx} ||
22023                         error "Fail to create $DIR/$tdir/dir${idx}"
22024         done
22025
22026         sync; sleep 3
22027         wait_delete_completed # ensure old test cleanups are finished
22028         echo "before create:"
22029         $LFS df -i $MOUNT
22030         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
22031
22032         for i in {1..10}; do
22033                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
22034                         error "Fail to create $DIR/$tdir/foo$i"
22035         done
22036
22037         sync; sleep 3
22038         echo "after create:"
22039         $LFS df -i $MOUNT
22040         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
22041
22042         # allow for an llog to be cleaned up during the test
22043         [ $after_used -ge $((before_used + 10 - 1)) ] ||
22044                 error "before ($before_used) + 10 > after ($after_used)"
22045
22046         for i in {1..10}; do
22047                 rm -rf $DIR/$tdir/foo$i ||
22048                         error "Fail to remove $DIR/$tdir/foo$i"
22049         done
22050
22051         sleep 3 # avoid MDT return cached statfs
22052         wait_delete_completed
22053         echo "after unlink:"
22054         $LFS df -i $MOUNT
22055         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
22056
22057         # allow for an llog to be created during the test
22058         [ $after_used -le $((before_used + 1)) ] ||
22059                 error "after ($after_used) > before ($before_used) + 1"
22060 }
22061 run_test 803 "verify agent object for remote object"
22062
22063 test_804() {
22064         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22065         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
22066                 skip "MDS needs to be newer than 2.10.54"
22067         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
22068
22069         mkdir -p $DIR/$tdir
22070         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
22071                 error "Fail to create $DIR/$tdir/dir0"
22072
22073         local fid=$($LFS path2fid $DIR/$tdir/dir0)
22074         local dev=$(mdsdevname 2)
22075
22076         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22077                 grep ${fid} || error "NOT found agent entry for dir0"
22078
22079         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
22080                 error "Fail to create $DIR/$tdir/dir1"
22081
22082         touch $DIR/$tdir/dir1/foo0 ||
22083                 error "Fail to create $DIR/$tdir/dir1/foo0"
22084         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
22085         local rc=0
22086
22087         for idx in $(seq $MDSCOUNT); do
22088                 dev=$(mdsdevname $idx)
22089                 do_facet mds${idx} \
22090                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22091                         grep ${fid} && rc=$idx
22092         done
22093
22094         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
22095                 error "Fail to rename foo0 to foo1"
22096         if [ $rc -eq 0 ]; then
22097                 for idx in $(seq $MDSCOUNT); do
22098                         dev=$(mdsdevname $idx)
22099                         do_facet mds${idx} \
22100                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22101                         grep ${fid} && rc=$idx
22102                 done
22103         fi
22104
22105         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
22106                 error "Fail to rename foo1 to foo2"
22107         if [ $rc -eq 0 ]; then
22108                 for idx in $(seq $MDSCOUNT); do
22109                         dev=$(mdsdevname $idx)
22110                         do_facet mds${idx} \
22111                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22112                         grep ${fid} && rc=$idx
22113                 done
22114         fi
22115
22116         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
22117
22118         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
22119                 error "Fail to link to $DIR/$tdir/dir1/foo2"
22120         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
22121                 error "Fail to rename foo2 to foo0"
22122         unlink $DIR/$tdir/dir1/foo0 ||
22123                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
22124         rm -rf $DIR/$tdir/dir0 ||
22125                 error "Fail to rm $DIR/$tdir/dir0"
22126
22127         for idx in $(seq $MDSCOUNT); do
22128                 dev=$(mdsdevname $idx)
22129                 rc=0
22130
22131                 stop mds${idx}
22132                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
22133                         rc=$?
22134                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
22135                         error "mount mds$idx failed"
22136                 df $MOUNT > /dev/null 2>&1
22137
22138                 # e2fsck should not return error
22139                 [ $rc -eq 0 ] ||
22140                         error "e2fsck detected error on MDT${idx}: rc=$rc"
22141         done
22142 }
22143 run_test 804 "verify agent entry for remote entry"
22144
22145 cleanup_805() {
22146         do_facet $SINGLEMDS zfs set quota=$old $fsset
22147         unlinkmany $DIR/$tdir/f- 1000000
22148         trap 0
22149 }
22150
22151 test_805() {
22152         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
22153         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
22154         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
22155                 skip "netfree not implemented before 0.7"
22156         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
22157                 skip "Need MDS version at least 2.10.57"
22158
22159         local fsset
22160         local freekb
22161         local usedkb
22162         local old
22163         local quota
22164         local pref="osd-zfs.$FSNAME-MDT0000."
22165
22166         # limit available space on MDS dataset to meet nospace issue
22167         # quickly. then ZFS 0.7.2 can use reserved space if asked
22168         # properly (using netfree flag in osd_declare_destroy()
22169         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
22170         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
22171                 gawk '{print $3}')
22172         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
22173         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
22174         let "usedkb=usedkb-freekb"
22175         let "freekb=freekb/2"
22176         if let "freekb > 5000"; then
22177                 let "freekb=5000"
22178         fi
22179         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
22180         trap cleanup_805 EXIT
22181         mkdir $DIR/$tdir
22182         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
22183         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
22184         rm -rf $DIR/$tdir || error "not able to remove"
22185         do_facet $SINGLEMDS zfs set quota=$old $fsset
22186         trap 0
22187 }
22188 run_test 805 "ZFS can remove from full fs"
22189
22190 # Size-on-MDS test
22191 check_lsom_data()
22192 {
22193         local file=$1
22194         local size=$($LFS getsom -s $file)
22195         local expect=$(stat -c %s $file)
22196
22197         [[ $size == $expect ]] ||
22198                 error "$file expected size: $expect, got: $size"
22199
22200         local blocks=$($LFS getsom -b $file)
22201         expect=$(stat -c %b $file)
22202         [[ $blocks == $expect ]] ||
22203                 error "$file expected blocks: $expect, got: $blocks"
22204 }
22205
22206 check_lsom_size()
22207 {
22208         local size=$($LFS getsom -s $1)
22209         local expect=$2
22210
22211         [[ $size == $expect ]] ||
22212                 error "$file expected size: $expect, got: $size"
22213 }
22214
22215 test_806() {
22216         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
22217                 skip "Need MDS version at least 2.11.52"
22218
22219         local bs=1048576
22220
22221         touch $DIR/$tfile || error "touch $tfile failed"
22222
22223         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
22224         save_lustre_params client "llite.*.xattr_cache" > $save
22225         lctl set_param llite.*.xattr_cache=0
22226         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
22227
22228         # single-threaded write
22229         echo "Test SOM for single-threaded write"
22230         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
22231                 error "write $tfile failed"
22232         check_lsom_size $DIR/$tfile $bs
22233
22234         local num=32
22235         local size=$(($num * $bs))
22236         local offset=0
22237         local i
22238
22239         echo "Test SOM for single client multi-threaded($num) write"
22240         $TRUNCATE $DIR/$tfile 0
22241         for ((i = 0; i < $num; i++)); do
22242                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22243                 local pids[$i]=$!
22244                 offset=$((offset + $bs))
22245         done
22246         for (( i=0; i < $num; i++ )); do
22247                 wait ${pids[$i]}
22248         done
22249         check_lsom_size $DIR/$tfile $size
22250
22251         $TRUNCATE $DIR/$tfile 0
22252         for ((i = 0; i < $num; i++)); do
22253                 offset=$((offset - $bs))
22254                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22255                 local pids[$i]=$!
22256         done
22257         for (( i=0; i < $num; i++ )); do
22258                 wait ${pids[$i]}
22259         done
22260         check_lsom_size $DIR/$tfile $size
22261
22262         # multi-client writes
22263         num=$(get_node_count ${CLIENTS//,/ })
22264         size=$(($num * $bs))
22265         offset=0
22266         i=0
22267
22268         echo "Test SOM for multi-client ($num) writes"
22269         $TRUNCATE $DIR/$tfile 0
22270         for client in ${CLIENTS//,/ }; do
22271                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22272                 local pids[$i]=$!
22273                 i=$((i + 1))
22274                 offset=$((offset + $bs))
22275         done
22276         for (( i=0; i < $num; i++ )); do
22277                 wait ${pids[$i]}
22278         done
22279         check_lsom_size $DIR/$tfile $offset
22280
22281         i=0
22282         $TRUNCATE $DIR/$tfile 0
22283         for client in ${CLIENTS//,/ }; do
22284                 offset=$((offset - $bs))
22285                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22286                 local pids[$i]=$!
22287                 i=$((i + 1))
22288         done
22289         for (( i=0; i < $num; i++ )); do
22290                 wait ${pids[$i]}
22291         done
22292         check_lsom_size $DIR/$tfile $size
22293
22294         # verify truncate
22295         echo "Test SOM for truncate"
22296         $TRUNCATE $DIR/$tfile 1048576
22297         check_lsom_size $DIR/$tfile 1048576
22298         $TRUNCATE $DIR/$tfile 1234
22299         check_lsom_size $DIR/$tfile 1234
22300
22301         # verify SOM blocks count
22302         echo "Verify SOM block count"
22303         $TRUNCATE $DIR/$tfile 0
22304         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
22305                 error "failed to write file $tfile"
22306         check_lsom_data $DIR/$tfile
22307 }
22308 run_test 806 "Verify Lazy Size on MDS"
22309
22310 test_807() {
22311         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
22312         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
22313                 skip "Need MDS version at least 2.11.52"
22314
22315         # Registration step
22316         changelog_register || error "changelog_register failed"
22317         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
22318         changelog_users $SINGLEMDS | grep -q $cl_user ||
22319                 error "User $cl_user not found in changelog_users"
22320
22321         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
22322         save_lustre_params client "llite.*.xattr_cache" > $save
22323         lctl set_param llite.*.xattr_cache=0
22324         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
22325
22326         rm -rf $DIR/$tdir || error "rm $tdir failed"
22327         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
22328         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
22329         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
22330         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
22331                 error "truncate $tdir/trunc failed"
22332
22333         local bs=1048576
22334         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 ||
22335                 error "write $tfile failed"
22336
22337         # multi-client wirtes
22338         local num=$(get_node_count ${CLIENTS//,/ })
22339         local offset=0
22340         local i=0
22341
22342         echo "Test SOM for multi-client ($num) writes"
22343         touch $DIR/$tfile || error "touch $tfile failed"
22344         $TRUNCATE $DIR/$tfile 0
22345         for client in ${CLIENTS//,/ }; do
22346                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22347                 local pids[$i]=$!
22348                 i=$((i + 1))
22349                 offset=$((offset + $bs))
22350         done
22351         for (( i=0; i < $num; i++ )); do
22352                 wait ${pids[$i]}
22353         done
22354
22355         sleep 5
22356         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
22357         check_lsom_data $DIR/$tdir/trunc
22358         check_lsom_data $DIR/$tdir/single_dd
22359         check_lsom_data $DIR/$tfile
22360
22361         rm -rf $DIR/$tdir
22362         # Deregistration step
22363         changelog_deregister || error "changelog_deregister failed"
22364 }
22365 run_test 807 "verify LSOM syncing tool"
22366
22367 check_som_nologged()
22368 {
22369         local lines=$($LFS changelog $FSNAME-MDT0000 |
22370                 grep 'x=trusted.som' | wc -l)
22371         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
22372 }
22373
22374 test_808() {
22375         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
22376                 skip "Need MDS version at least 2.11.55"
22377
22378         # Registration step
22379         changelog_register || error "changelog_register failed"
22380
22381         touch $DIR/$tfile || error "touch $tfile failed"
22382         check_som_nologged
22383
22384         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
22385                 error "write $tfile failed"
22386         check_som_nologged
22387
22388         $TRUNCATE $DIR/$tfile 1234
22389         check_som_nologged
22390
22391         $TRUNCATE $DIR/$tfile 1048576
22392         check_som_nologged
22393
22394         # Deregistration step
22395         changelog_deregister || error "changelog_deregister failed"
22396 }
22397 run_test 808 "Check trusted.som xattr not logged in Changelogs"
22398
22399 check_som_nodata()
22400 {
22401         $LFS getsom $1
22402         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
22403 }
22404
22405 test_809() {
22406         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
22407                 skip "Need MDS version at least 2.11.56"
22408
22409         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
22410                 error "failed to create DoM-only file $DIR/$tfile"
22411         touch $DIR/$tfile || error "touch $tfile failed"
22412         check_som_nodata $DIR/$tfile
22413
22414         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
22415                 error "write $tfile failed"
22416         check_som_nodata $DIR/$tfile
22417
22418         $TRUNCATE $DIR/$tfile 1234
22419         check_som_nodata $DIR/$tfile
22420
22421         $TRUNCATE $DIR/$tfile 4097
22422         check_som_nodata $DIR/$file
22423 }
22424 run_test 809 "Verify no SOM xattr store for DoM-only files"
22425
22426 test_810() {
22427         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22428         $GSS && skip_env "could not run with gss"
22429         [[ $OST1_VERSION -gt $(version_code 2.12.58) ]] ||
22430                 skip "OST < 2.12.58 doesn't align checksum"
22431
22432         set_checksums 1
22433         stack_trap "set_checksums $ORIG_CSUM" EXIT
22434         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
22435
22436         local csum
22437         local before
22438         local after
22439         for csum in $CKSUM_TYPES; do
22440                 #define OBD_FAIL_OSC_NO_GRANT   0x411
22441                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
22442                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
22443                         eval set -- $i
22444                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
22445                         before=$(md5sum $DIR/$tfile)
22446                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
22447                         after=$(md5sum $DIR/$tfile)
22448                         [ "$before" == "$after" ] ||
22449                                 error "$csum: $before != $after bs=$1 seek=$2"
22450                 done
22451         done
22452 }
22453 run_test 810 "partial page writes on ZFS (LU-11663)"
22454
22455 test_811() {
22456         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.56) ] &&
22457                 skip "Need MDS version at least 2.11.56"
22458
22459         #define OBD_FAIL_MDS_ORPHAN_DELETE      0x165
22460         do_facet mds1 $LCTL set_param fail_loc=0x165
22461         $MULTIOP $DIR/$tfile Ouc || error "multiop failed"
22462
22463         stop mds1
22464         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
22465
22466         sleep 5
22467         [[ $(do_facet mds1 pgrep orph_.*-MDD | wc -l) -eq 0 ]] ||
22468                 error "MDD orphan cleanup thread not quit"
22469 }
22470 run_test 811 "orphan name stub can be cleaned up in startup"
22471
22472 test_812a() {
22473         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
22474                 skip "OST < 2.12.51 doesn't support this fail_loc"
22475         [ "$SHARED_KEY" = true ] &&
22476                 skip "OSC connections never go IDLE with Shared-Keys enabled"
22477
22478         $LFS setstripe -c 1 -i 0 $DIR/$tfile
22479         # ensure ost1 is connected
22480         stat $DIR/$tfile >/dev/null || error "can't stat"
22481         wait_osc_import_state client ost1 FULL
22482         # no locks, no reqs to let the connection idle
22483         cancel_lru_locks osc
22484
22485         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
22486 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
22487         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
22488         wait_osc_import_state client ost1 CONNECTING
22489         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
22490
22491         stat $DIR/$tfile >/dev/null || error "can't stat file"
22492 }
22493 run_test 812a "do not drop reqs generated when imp is going to idle (LU-11951)"
22494
22495 test_812b() { # LU-12378
22496         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
22497                 skip "OST < 2.12.51 doesn't support this fail_loc"
22498         [ "$SHARED_KEY" = true ] &&
22499                 skip "OSC connections never go IDLE with Shared-Keys enabled"
22500
22501         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "setstripe failed"
22502         # ensure ost1 is connected
22503         stat $DIR/$tfile >/dev/null || error "can't stat"
22504         wait_osc_import_state client ost1 FULL
22505         # no locks, no reqs to let the connection idle
22506         cancel_lru_locks osc
22507
22508         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
22509 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
22510         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
22511         wait_osc_import_state client ost1 CONNECTING
22512         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
22513
22514         $LFS quota -u 0 $DIR/ || error "lfs quota should succeed"
22515         wait_osc_import_state client ost1 IDLE
22516 }
22517 run_test 812b "do not drop no resend request for idle connect"
22518
22519 test_813() {
22520         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
22521         [ -z "$file_heat_sav" ] && skip "no file heat support"
22522
22523         local readsample
22524         local writesample
22525         local readbyte
22526         local writebyte
22527         local readsample1
22528         local writesample1
22529         local readbyte1
22530         local writebyte1
22531
22532         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
22533         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
22534
22535         $LCTL set_param -n llite.*.file_heat=1
22536         echo "Turn on file heat"
22537         echo "Period second: $period_second, Decay percentage: $decay_pct"
22538
22539         echo "QQQQ" > $DIR/$tfile
22540         echo "QQQQ" > $DIR/$tfile
22541         echo "QQQQ" > $DIR/$tfile
22542         cat $DIR/$tfile > /dev/null
22543         cat $DIR/$tfile > /dev/null
22544         cat $DIR/$tfile > /dev/null
22545         cat $DIR/$tfile > /dev/null
22546
22547         local out=$($LFS heat_get $DIR/$tfile)
22548
22549         $LFS heat_get $DIR/$tfile
22550         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22551         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22552         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22553         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22554
22555         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
22556         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
22557         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
22558         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
22559
22560         sleep $((period_second + 3))
22561         echo "Sleep $((period_second + 3)) seconds..."
22562         # The recursion formula to calculate the heat of the file f is as
22563         # follow:
22564         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
22565         # Where Hi is the heat value in the period between time points i*I and
22566         # (i+1)*I; Ci is the access count in the period; the symbol P refers
22567         # to the weight of Ci.
22568         out=$($LFS heat_get $DIR/$tfile)
22569         $LFS heat_get $DIR/$tfile
22570         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22571         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22572         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22573         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22574
22575         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
22576                 error "read sample ($readsample) is wrong"
22577         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
22578                 error "write sample ($writesample) is wrong"
22579         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
22580                 error "read bytes ($readbyte) is wrong"
22581         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
22582                 error "write bytes ($writebyte) is wrong"
22583
22584         echo "QQQQ" > $DIR/$tfile
22585         echo "QQQQ" > $DIR/$tfile
22586         echo "QQQQ" > $DIR/$tfile
22587         cat $DIR/$tfile > /dev/null
22588         cat $DIR/$tfile > /dev/null
22589         cat $DIR/$tfile > /dev/null
22590         cat $DIR/$tfile > /dev/null
22591
22592         sleep $((period_second + 3))
22593         echo "Sleep $((period_second + 3)) seconds..."
22594
22595         out=$($LFS heat_get $DIR/$tfile)
22596         $LFS heat_get $DIR/$tfile
22597         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22598         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22599         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22600         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22601
22602         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
22603                 4 * $decay_pct) / 100") -eq 1 ] ||
22604                 error "read sample ($readsample1) is wrong"
22605         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
22606                 3 * $decay_pct) / 100") -eq 1 ] ||
22607                 error "write sample ($writesample1) is wrong"
22608         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
22609                 20 * $decay_pct) / 100") -eq 1 ] ||
22610                 error "read bytes ($readbyte1) is wrong"
22611         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
22612                 15 * $decay_pct) / 100") -eq 1 ] ||
22613                 error "write bytes ($writebyte1) is wrong"
22614
22615         echo "Turn off file heat for the file $DIR/$tfile"
22616         $LFS heat_set -o $DIR/$tfile
22617
22618         echo "QQQQ" > $DIR/$tfile
22619         echo "QQQQ" > $DIR/$tfile
22620         echo "QQQQ" > $DIR/$tfile
22621         cat $DIR/$tfile > /dev/null
22622         cat $DIR/$tfile > /dev/null
22623         cat $DIR/$tfile > /dev/null
22624         cat $DIR/$tfile > /dev/null
22625
22626         out=$($LFS heat_get $DIR/$tfile)
22627         $LFS heat_get $DIR/$tfile
22628         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22629         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22630         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22631         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22632
22633         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
22634         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
22635         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
22636         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
22637
22638         echo "Trun on file heat for the file $DIR/$tfile"
22639         $LFS heat_set -O $DIR/$tfile
22640
22641         echo "QQQQ" > $DIR/$tfile
22642         echo "QQQQ" > $DIR/$tfile
22643         echo "QQQQ" > $DIR/$tfile
22644         cat $DIR/$tfile > /dev/null
22645         cat $DIR/$tfile > /dev/null
22646         cat $DIR/$tfile > /dev/null
22647         cat $DIR/$tfile > /dev/null
22648
22649         out=$($LFS heat_get $DIR/$tfile)
22650         $LFS heat_get $DIR/$tfile
22651         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22652         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22653         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22654         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22655
22656         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
22657         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
22658         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
22659         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
22660
22661         $LFS heat_set -c $DIR/$tfile
22662         $LCTL set_param -n llite.*.file_heat=0
22663         echo "Turn off file heat support for the Lustre filesystem"
22664
22665         echo "QQQQ" > $DIR/$tfile
22666         echo "QQQQ" > $DIR/$tfile
22667         echo "QQQQ" > $DIR/$tfile
22668         cat $DIR/$tfile > /dev/null
22669         cat $DIR/$tfile > /dev/null
22670         cat $DIR/$tfile > /dev/null
22671         cat $DIR/$tfile > /dev/null
22672
22673         out=$($LFS heat_get $DIR/$tfile)
22674         $LFS heat_get $DIR/$tfile
22675         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22676         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22677         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22678         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22679
22680         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
22681         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
22682         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
22683         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
22684
22685         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
22686         rm -f $DIR/$tfile
22687 }
22688 run_test 813 "File heat verfication"
22689
22690 test_814()
22691 {
22692         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
22693         echo -n y >> $DIR/$tfile
22694         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
22695         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
22696 }
22697 run_test 814 "sparse cp works as expected (LU-12361)"
22698
22699 test_815()
22700 {
22701         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
22702         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
22703 }
22704 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
22705
22706 test_816() {
22707         [ "$SHARED_KEY" = true ] &&
22708                 skip "OSC connections never go IDLE with Shared-Keys enabled"
22709
22710         $LFS setstripe -c 1 -i 0 $DIR/$tfile
22711         # ensure ost1 is connected
22712         stat $DIR/$tfile >/dev/null || error "can't stat"
22713         wait_osc_import_state client ost1 FULL
22714         # no locks, no reqs to let the connection idle
22715         cancel_lru_locks osc
22716         lru_resize_disable osc
22717         local before
22718         local now
22719         before=$($LCTL get_param -n \
22720                  ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
22721
22722         wait_osc_import_state client ost1 IDLE
22723         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
22724         now=$($LCTL get_param -n \
22725               ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
22726         [ $before == $now ] || error "lru_size changed $before != $now"
22727 }
22728 run_test 816 "do not reset lru_resize on idle reconnect"
22729
22730 cleanup_817() {
22731         umount $tmpdir
22732         exportfs -u localhost:$DIR/nfsexp
22733         rm -rf $DIR/nfsexp
22734 }
22735
22736 test_817() {
22737         systemctl restart nfs-server.service || skip "failed to restart nfsd"
22738
22739         mkdir -p $DIR/nfsexp
22740         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
22741                 error "failed to export nfs"
22742
22743         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
22744         stack_trap cleanup_817 EXIT
22745
22746         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
22747                 error "failed to mount nfs to $tmpdir"
22748
22749         cp /bin/true $tmpdir
22750         $DIR/nfsexp/true || error "failed to execute 'true' command"
22751 }
22752 run_test 817 "nfsd won't cache write lock for exec file"
22753
22754 test_818() {
22755         mkdir $DIR/$tdir
22756         $LFS setstripe -c1 -i0 $DIR/$tfile
22757         $LFS setstripe -c1 -i1 $DIR/$tfile
22758         stop $SINGLEMDS
22759         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
22760         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
22761         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
22762                 error "start $SINGLEMDS failed"
22763         rm -rf $DIR/$tdir
22764 }
22765 run_test 818 "unlink with failed llog"
22766
22767 test_819a() {
22768         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22769         cancel_lru_locks osc
22770         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
22771         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
22772         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
22773         rm -f $TDIR/$tfile
22774 }
22775 run_test 819a "too big niobuf in read"
22776
22777 test_819b() {
22778         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
22779         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
22780         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22781         cancel_lru_locks osc
22782         sleep 1
22783         rm -f $TDIR/$tfile
22784 }
22785 run_test 819b "too big niobuf in write"
22786
22787 #
22788 # tests that do cleanup/setup should be run at the end
22789 #
22790
22791 test_900() {
22792         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22793         local ls
22794
22795         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
22796         $LCTL set_param fail_loc=0x903
22797
22798         cancel_lru_locks MGC
22799
22800         FAIL_ON_ERROR=true cleanup
22801         FAIL_ON_ERROR=true setup
22802 }
22803 run_test 900 "umount should not race with any mgc requeue thread"
22804
22805 # LUS-6253/LU-11185
22806 test_901() {
22807         local oldc
22808         local newc
22809         local olds
22810         local news
22811         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22812
22813         # some get_param have a bug to handle dot in param name
22814         cancel_lru_locks MGC
22815         oldc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
22816         olds=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
22817         umount_client $MOUNT || error "umount failed"
22818         mount_client $MOUNT || error "mount failed"
22819         cancel_lru_locks MGC
22820         newc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
22821         news=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
22822
22823         [ $oldc -lt $newc ] && error "mgc lock leak ($oldc != $newc)"
22824         [ $olds -lt $news ] && error "mgs lock leak ($olds != $news)"
22825
22826         return 0
22827 }
22828 run_test 901 "don't leak a mgc lock on client umount"
22829
22830 complete $SECONDS
22831 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
22832 check_and_cleanup_lustre
22833 if [ "$I_MOUNTED" != "yes" ]; then
22834         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
22835 fi
22836 exit_status