Whamcloud - gitweb
LU-13314 test: add 56ob to ALWAYS_EXCEPT for now
[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 LU-13314
45 ALWAYS_EXCEPT+=" 407     312    56ob"
46
47 if $SHARED_KEY; then
48         # bug number:    LU-9795 LU-9795 LU-9795 LU-9795
49         ALWAYS_EXCEPT+=" 17n     60a     133g    300f"
50 fi
51
52 selinux_status=$(getenforce)
53 if [ "$selinux_status" != "Disabled" ]; then
54         # bug number:
55         ALWAYS_EXCEPT+=""
56 fi
57
58 # skip the grant tests for ARM until they are fixed
59 if [[ $(uname -m) = aarch64 ]]; then
60         # bug number:    LU-11596
61         ALWAYS_EXCEPT+=" $GRANT_CHECK_LIST"
62         # bug number:    LU-11671 LU-11667
63         ALWAYS_EXCEPT+=" 45       317"
64 fi
65
66 # skip nfs tests on kernels >= 4.14.0 until they are fixed
67 if [ $LINUX_VERSION_CODE -ge $(version_code 4.14.0) ]; then
68         # bug number:   LU-12661
69         ALWAYS_EXCEPT+=" 817"
70 fi
71 # skip cgroup tests on RHEL8.1 kernels until they are fixed
72 if (( $LINUX_VERSION_CODE >= $(version_code 4.18.0) &&
73       $LINUX_VERSION_CODE <  $(version_code 5.4.0) )); then
74         # bug number:   LU-13063
75         ALWAYS_EXCEPT+=" 411"
76 fi
77
78 #                                  5          12          (min)"
79 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 135 136 300o"
80
81 if [ "$mds1_FSTYPE" = "zfs" ]; then
82         # bug number for skipped test:
83         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  "
84         #                                               13    (min)"
85         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
86 fi
87
88 # Get the SLES distro version
89 #
90 # Returns a version string that should only be used in comparing
91 # strings returned by version_code()
92 sles_version_code()
93 {
94         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
95
96         # All SuSE Linux versions have one decimal. version_code expects two
97         local sles_version=$version.0
98         version_code $sles_version
99 }
100
101 # Check if we are running on Ubuntu or SLES so we can make decisions on
102 # what tests to run
103 if [ -r /etc/SuSE-release ]; then
104         sles_version=$(sles_version_code)
105         [ $sles_version -lt $(version_code 11.4.0) ] &&
106                 # bug number for skipped test: LU-4341
107                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
108         [ $sles_version -lt $(version_code 12.0.0) ] &&
109                 # bug number for skipped test: LU-3703
110                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
111 elif [ -r /etc/os-release ]; then
112         if grep -qi ubuntu /etc/os-release; then
113                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
114                                                 -e 's/^VERSION=//p' \
115                                                 /etc/os-release |
116                                                 awk '{ print $1 }'))
117
118                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
119                         # bug number for skipped test:
120                         #                LU-10334 LU-10366
121                         ALWAYS_EXCEPT+=" 103a     410"
122                 fi
123         fi
124 fi
125
126 build_test_filter
127 FAIL_ON_ERROR=false
128
129 cleanup() {
130         echo -n "cln.."
131         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
132         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
133 }
134 setup() {
135         echo -n "mnt.."
136         load_modules
137         setupall || exit 10
138         echo "done"
139 }
140
141 check_swap_layouts_support()
142 {
143         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
144                 skip "Does not support layout lock."
145 }
146
147 check_and_setup_lustre
148 DIR=${DIR:-$MOUNT}
149 assert_DIR
150
151 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
152
153 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
154 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
155 rm -rf $DIR/[Rdfs][0-9]*
156
157 # $RUNAS_ID may get set incorrectly somewhere else
158 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
159         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
160
161 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
162
163 if [ "${ONLY}" = "MOUNT" ] ; then
164         echo "Lustre is up, please go on"
165         exit
166 fi
167
168 echo "preparing for tests involving mounts"
169 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
170 touch $EXT2_DEV
171 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
172 echo # add a newline after mke2fs.
173
174 umask 077
175
176 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
177 lctl set_param debug=-1 2> /dev/null || true
178 test_0a() {
179         touch $DIR/$tfile
180         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
181         rm $DIR/$tfile
182         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
183 }
184 run_test 0a "touch; rm ====================="
185
186 test_0b() {
187         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
188         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
189 }
190 run_test 0b "chmod 0755 $DIR ============================="
191
192 test_0c() {
193         $LCTL get_param mdc.*.import | grep "state: FULL" ||
194                 error "import not FULL"
195         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
196                 error "bad target"
197 }
198 run_test 0c "check import proc"
199
200 test_0d() { # LU-3397
201         [ $MGS_VERSION -lt $(version_code 2.10.57) ] &&
202                 skip "proc exports not supported before 2.10.57"
203
204         local mgs_exp="mgs.MGS.exports"
205         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
206         local exp_client_nid
207         local exp_client_version
208         local exp_val
209         local imp_val
210         local temp_imp=$DIR/$tfile.import
211         local temp_exp=$DIR/$tfile.export
212
213         # save mgc import file to $temp_imp
214         $LCTL get_param mgc.*.import | tee $temp_imp
215         # Check if client uuid is found in MGS export
216         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
217                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
218                         $client_uuid ] &&
219                         break;
220         done
221         # save mgs export file to $temp_exp
222         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
223
224         # Compare the value of field "connect_flags"
225         imp_val=$(grep "connect_flags" $temp_imp)
226         exp_val=$(grep "connect_flags" $temp_exp)
227         [ "$exp_val" == "$imp_val" ] ||
228                 error "export flags '$exp_val' != import flags '$imp_val'"
229
230         # Compare the value of client version
231         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
232         exp_val=$(version_code $exp_client_version)
233         imp_val=$CLIENT_VERSION
234         [ "$exp_val" == "$imp_val" ] ||
235                 error "export client version '$exp_val' != '$imp_val'"
236 }
237 run_test 0d "check export proc ============================="
238
239 test_1() {
240         test_mkdir $DIR/$tdir
241         test_mkdir $DIR/$tdir/d2
242         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
243         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
244         rmdir $DIR/$tdir/d2
245         rmdir $DIR/$tdir
246         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
247 }
248 run_test 1 "mkdir; remkdir; rmdir"
249
250 test_2() {
251         test_mkdir $DIR/$tdir
252         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
253         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
254         rm -r $DIR/$tdir
255         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
256 }
257 run_test 2 "mkdir; touch; rmdir; check file"
258
259 test_3() {
260         test_mkdir $DIR/$tdir
261         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
262         touch $DIR/$tdir/$tfile
263         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
264         rm -r $DIR/$tdir
265         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
266 }
267 run_test 3 "mkdir; touch; rmdir; check dir"
268
269 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
270 test_4() {
271         test_mkdir -i 1 $DIR/$tdir
272
273         touch $DIR/$tdir/$tfile ||
274                 error "Create file under remote directory failed"
275
276         rmdir $DIR/$tdir &&
277                 error "Expect error removing in-use dir $DIR/$tdir"
278
279         test -d $DIR/$tdir || error "Remote directory disappeared"
280
281         rm -rf $DIR/$tdir || error "remove remote dir error"
282 }
283 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
284
285 test_5() {
286         test_mkdir $DIR/$tdir
287         test_mkdir $DIR/$tdir/d2
288         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
289         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
290         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
291 }
292 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
293
294 test_6a() {
295         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
296         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
297         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
298                 error "$tfile does not have perm 0666 or UID $UID"
299         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
300         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
301                 error "$tfile should be 0666 and owned by UID $UID"
302 }
303 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
304
305 test_6c() {
306         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
307
308         touch $DIR/$tfile
309         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
310         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
311                 error "$tfile should be owned by UID $RUNAS_ID"
312         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
313         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
314                 error "$tfile should be owned by UID $RUNAS_ID"
315 }
316 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
317
318 test_6e() {
319         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
320
321         touch $DIR/$tfile
322         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
323         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
324                 error "$tfile should be owned by GID $UID"
325         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
326         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
327                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
328 }
329 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
330
331 test_6g() {
332         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
333
334         test_mkdir $DIR/$tdir
335         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
336         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
337         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
338         test_mkdir $DIR/$tdir/d/subdir
339         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
340                 error "$tdir/d/subdir should be GID $RUNAS_GID"
341         if [[ $MDSCOUNT -gt 1 ]]; then
342                 # check remote dir sgid inherite
343                 $LFS mkdir -i 0 $DIR/$tdir.local ||
344                         error "mkdir $tdir.local failed"
345                 chmod g+s $DIR/$tdir.local ||
346                         error "chmod $tdir.local failed"
347                 chgrp $RUNAS_GID $DIR/$tdir.local ||
348                         error "chgrp $tdir.local failed"
349                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
350                         error "mkdir $tdir.remote failed"
351                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
352                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
353                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
354                         error "$tdir.remote should be mode 02755"
355         fi
356 }
357 run_test 6g "verify new dir in sgid dir inherits group"
358
359 test_6h() { # bug 7331
360         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
361
362         touch $DIR/$tfile || error "touch failed"
363         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
364         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
365                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
366         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
367                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
368 }
369 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
370
371 test_7a() {
372         test_mkdir $DIR/$tdir
373         $MCREATE $DIR/$tdir/$tfile
374         chmod 0666 $DIR/$tdir/$tfile
375         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
376                 error "$tdir/$tfile should be mode 0666"
377 }
378 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
379
380 test_7b() {
381         if [ ! -d $DIR/$tdir ]; then
382                 test_mkdir $DIR/$tdir
383         fi
384         $MCREATE $DIR/$tdir/$tfile
385         echo -n foo > $DIR/$tdir/$tfile
386         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
387         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
388 }
389 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
390
391 test_8() {
392         test_mkdir $DIR/$tdir
393         touch $DIR/$tdir/$tfile
394         chmod 0666 $DIR/$tdir/$tfile
395         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
396                 error "$tfile mode not 0666"
397 }
398 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
399
400 test_9() {
401         test_mkdir $DIR/$tdir
402         test_mkdir $DIR/$tdir/d2
403         test_mkdir $DIR/$tdir/d2/d3
404         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
405 }
406 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
407
408 test_10() {
409         test_mkdir $DIR/$tdir
410         test_mkdir $DIR/$tdir/d2
411         touch $DIR/$tdir/d2/$tfile
412         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
413                 error "$tdir/d2/$tfile not a file"
414 }
415 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
416
417 test_11() {
418         test_mkdir $DIR/$tdir
419         test_mkdir $DIR/$tdir/d2
420         chmod 0666 $DIR/$tdir/d2
421         chmod 0705 $DIR/$tdir/d2
422         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
423                 error "$tdir/d2 mode not 0705"
424 }
425 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
426
427 test_12() {
428         test_mkdir $DIR/$tdir
429         touch $DIR/$tdir/$tfile
430         chmod 0666 $DIR/$tdir/$tfile
431         chmod 0654 $DIR/$tdir/$tfile
432         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
433                 error "$tdir/d2 mode not 0654"
434 }
435 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
436
437 test_13() {
438         test_mkdir $DIR/$tdir
439         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
440         >  $DIR/$tdir/$tfile
441         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
442                 error "$tdir/$tfile size not 0 after truncate"
443 }
444 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
445
446 test_14() {
447         test_mkdir $DIR/$tdir
448         touch $DIR/$tdir/$tfile
449         rm $DIR/$tdir/$tfile
450         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
451 }
452 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
453
454 test_15() {
455         test_mkdir $DIR/$tdir
456         touch $DIR/$tdir/$tfile
457         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
458         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
459                 error "$tdir/${tfile_2} not a file after rename"
460         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
461 }
462 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
463
464 test_16() {
465         test_mkdir $DIR/$tdir
466         touch $DIR/$tdir/$tfile
467         rm -rf $DIR/$tdir/$tfile
468         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
469 }
470 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
471
472 test_17a() {
473         test_mkdir $DIR/$tdir
474         touch $DIR/$tdir/$tfile
475         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
476         ls -l $DIR/$tdir
477         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
478                 error "$tdir/l-exist not a symlink"
479         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
480                 error "$tdir/l-exist not referencing a file"
481         rm -f $DIR/$tdir/l-exist
482         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
483 }
484 run_test 17a "symlinks: create, remove (real)"
485
486 test_17b() {
487         test_mkdir $DIR/$tdir
488         ln -s no-such-file $DIR/$tdir/l-dangle
489         ls -l $DIR/$tdir
490         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
491                 error "$tdir/l-dangle not referencing no-such-file"
492         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
493                 error "$tdir/l-dangle not referencing non-existent file"
494         rm -f $DIR/$tdir/l-dangle
495         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
496 }
497 run_test 17b "symlinks: create, remove (dangling)"
498
499 test_17c() { # bug 3440 - don't save failed open RPC for replay
500         test_mkdir $DIR/$tdir
501         ln -s foo $DIR/$tdir/$tfile
502         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
503 }
504 run_test 17c "symlinks: open dangling (should return error)"
505
506 test_17d() {
507         test_mkdir $DIR/$tdir
508         ln -s foo $DIR/$tdir/$tfile
509         touch $DIR/$tdir/$tfile || error "creating to new symlink"
510 }
511 run_test 17d "symlinks: create dangling"
512
513 test_17e() {
514         test_mkdir $DIR/$tdir
515         local foo=$DIR/$tdir/$tfile
516         ln -s $foo $foo || error "create symlink failed"
517         ls -l $foo || error "ls -l failed"
518         ls $foo && error "ls not failed" || true
519 }
520 run_test 17e "symlinks: create recursive symlink (should return error)"
521
522 test_17f() {
523         test_mkdir $DIR/$tdir
524         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
525         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
526         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
527         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
528         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
529         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/$tdir/666
530         ls -l  $DIR/$tdir
531 }
532 run_test 17f "symlinks: long and very long symlink name"
533
534 # str_repeat(S, N) generate a string that is string S repeated N times
535 str_repeat() {
536         local s=$1
537         local n=$2
538         local ret=''
539         while [ $((n -= 1)) -ge 0 ]; do
540                 ret=$ret$s
541         done
542         echo $ret
543 }
544
545 # Long symlinks and LU-2241
546 test_17g() {
547         test_mkdir $DIR/$tdir
548         local TESTS="59 60 61 4094 4095"
549
550         # Fix for inode size boundary in 2.1.4
551         [ $MDS1_VERSION -lt $(version_code 2.1.4) ] &&
552                 TESTS="4094 4095"
553
554         # Patch not applied to 2.2 or 2.3 branches
555         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
556         [ $MDS1_VERSION -le $(version_code 2.3.55) ] &&
557                 TESTS="4094 4095"
558
559         for i in $TESTS; do
560                 local SYMNAME=$(str_repeat 'x' $i)
561                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
562                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
563         done
564 }
565 run_test 17g "symlinks: really long symlink name and inode boundaries"
566
567 test_17h() { #bug 17378
568         [ $PARALLEL == "yes" ] && skip "skip parallel run"
569         remote_mds_nodsh && skip "remote MDS with nodsh"
570
571         local mdt_idx
572
573         test_mkdir $DIR/$tdir
574         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
575         $LFS setstripe -c -1 $DIR/$tdir
576         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
577         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
578         touch $DIR/$tdir/$tfile || true
579 }
580 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
581
582 test_17i() { #bug 20018
583         [ $PARALLEL == "yes" ] && skip "skip parallel run"
584         remote_mds_nodsh && skip "remote MDS with nodsh"
585
586         local foo=$DIR/$tdir/$tfile
587         local mdt_idx
588
589         test_mkdir -c1 $DIR/$tdir
590         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
591         ln -s $foo $foo || error "create symlink failed"
592 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
593         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
594         ls -l $foo && error "error not detected"
595         return 0
596 }
597 run_test 17i "don't panic on short symlink (should return error)"
598
599 test_17k() { #bug 22301
600         [ $PARALLEL == "yes" ] && skip "skip parallel run"
601         [[ -z "$(which rsync 2>/dev/null)" ]] &&
602                 skip "no rsync command"
603         rsync --help | grep -q xattr ||
604                 skip_env "$(rsync --version | head -n1) does not support xattrs"
605         test_mkdir $DIR/$tdir
606         test_mkdir $DIR/$tdir.new
607         touch $DIR/$tdir/$tfile
608         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
609         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
610                 error "rsync failed with xattrs enabled"
611 }
612 run_test 17k "symlinks: rsync with xattrs enabled"
613
614 test_17l() { # LU-279
615         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
616                 skip "no getfattr command"
617
618         test_mkdir $DIR/$tdir
619         touch $DIR/$tdir/$tfile
620         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
621         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
622                 # -h to not follow symlinks. -m '' to list all the xattrs.
623                 # grep to remove first line: '# file: $path'.
624                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
625                 do
626                         lgetxattr_size_check $path $xattr ||
627                                 error "lgetxattr_size_check $path $xattr failed"
628                 done
629         done
630 }
631 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
632
633 # LU-1540
634 test_17m() {
635         [ $PARALLEL == "yes" ] && skip "skip parallel run"
636         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
637         remote_mds_nodsh && skip "remote MDS with nodsh"
638         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
639         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
640                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
641
642         local short_sym="0123456789"
643         local wdir=$DIR/$tdir
644         local i
645
646         test_mkdir $wdir
647         long_sym=$short_sym
648         # create a long symlink file
649         for ((i = 0; i < 4; ++i)); do
650                 long_sym=${long_sym}${long_sym}
651         done
652
653         echo "create 512 short and long symlink files under $wdir"
654         for ((i = 0; i < 256; ++i)); do
655                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
656                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
657         done
658
659         echo "erase them"
660         rm -f $wdir/*
661         sync
662         wait_delete_completed
663
664         echo "recreate the 512 symlink files with a shorter string"
665         for ((i = 0; i < 512; ++i)); do
666                 # rewrite the symlink file with a shorter string
667                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
668                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
669         done
670
671         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
672         local devname=$(mdsdevname $mds_index)
673
674         echo "stop and checking mds${mds_index}:"
675         # e2fsck should not return error
676         stop mds${mds_index}
677         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
678         rc=$?
679
680         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
681                 error "start mds${mds_index} failed"
682         df $MOUNT > /dev/null 2>&1
683         [ $rc -eq 0 ] ||
684                 error "e2fsck detected error for short/long symlink: rc=$rc"
685         rm -f $wdir/*
686 }
687 run_test 17m "run e2fsck against MDT which contains short/long symlink"
688
689 check_fs_consistency_17n() {
690         local mdt_index
691         local rc=0
692
693         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
694         # so it only check MDT1/MDT2 instead of all of MDTs.
695         for mdt_index in 1 2; do
696                 local devname=$(mdsdevname $mdt_index)
697                 # e2fsck should not return error
698                 stop mds${mdt_index}
699                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
700                         rc=$((rc + $?))
701
702                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
703                         error "mount mds$mdt_index failed"
704                 df $MOUNT > /dev/null 2>&1
705         done
706         return $rc
707 }
708
709 test_17n() {
710         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
711         [ $PARALLEL == "yes" ] && skip "skip parallel run"
712         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
713         remote_mds_nodsh && skip "remote MDS with nodsh"
714         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
715         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
716                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
717
718         local i
719
720         test_mkdir $DIR/$tdir
721         for ((i=0; i<10; i++)); do
722                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
723                         error "create remote dir error $i"
724                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
725                         error "create files under remote dir failed $i"
726         done
727
728         check_fs_consistency_17n ||
729                 error "e2fsck report error after create files under remote dir"
730
731         for ((i = 0; i < 10; i++)); do
732                 rm -rf $DIR/$tdir/remote_dir_${i} ||
733                         error "destroy remote dir error $i"
734         done
735
736         check_fs_consistency_17n ||
737                 error "e2fsck report error after unlink files under remote dir"
738
739         [ $MDS1_VERSION -lt $(version_code 2.4.50) ] &&
740                 skip "lustre < 2.4.50 does not support migrate mv"
741
742         for ((i = 0; i < 10; i++)); do
743                 mkdir -p $DIR/$tdir/remote_dir_${i}
744                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
745                         error "create files under remote dir failed $i"
746                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
747                         error "migrate remote dir error $i"
748         done
749         check_fs_consistency_17n || error "e2fsck report error after migration"
750
751         for ((i = 0; i < 10; i++)); do
752                 rm -rf $DIR/$tdir/remote_dir_${i} ||
753                         error "destroy remote dir error $i"
754         done
755
756         check_fs_consistency_17n || error "e2fsck report error after unlink"
757 }
758 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
759
760 test_17o() {
761         remote_mds_nodsh && skip "remote MDS with nodsh"
762         [ $MDS1_VERSION -lt $(version_code 2.3.64) ] &&
763                 skip "Need MDS version at least 2.3.64"
764
765         local wdir=$DIR/${tdir}o
766         local mdt_index
767         local rc=0
768
769         test_mkdir $wdir
770         touch $wdir/$tfile
771         mdt_index=$($LFS getstripe -m $wdir/$tfile)
772         mdt_index=$((mdt_index + 1))
773
774         cancel_lru_locks mdc
775         #fail mds will wait the failover finish then set
776         #following fail_loc to avoid interfer the recovery process.
777         fail mds${mdt_index}
778
779         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
780         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
781         ls -l $wdir/$tfile && rc=1
782         do_facet mds${mdt_index} lctl set_param fail_loc=0
783         [[ $rc -eq 0 ]] || error "stat file should fail"
784 }
785 run_test 17o "stat file with incompat LMA feature"
786
787 test_18() {
788         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
789         ls $DIR || error "Failed to ls $DIR: $?"
790 }
791 run_test 18 "touch .../f ; ls ... =============================="
792
793 test_19a() {
794         touch $DIR/$tfile
795         ls -l $DIR
796         rm $DIR/$tfile
797         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
798 }
799 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
800
801 test_19b() {
802         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
803 }
804 run_test 19b "ls -l .../f19 (should return error) =============="
805
806 test_19c() {
807         [ $RUNAS_ID -eq $UID ] &&
808                 skip_env "RUNAS_ID = UID = $UID -- skipping"
809
810         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
811 }
812 run_test 19c "$RUNAS touch .../f19 (should return error) =="
813
814 test_19d() {
815         cat $DIR/f19 && error || true
816 }
817 run_test 19d "cat .../f19 (should return error) =============="
818
819 test_20() {
820         touch $DIR/$tfile
821         rm $DIR/$tfile
822         touch $DIR/$tfile
823         rm $DIR/$tfile
824         touch $DIR/$tfile
825         rm $DIR/$tfile
826         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
827 }
828 run_test 20 "touch .../f ; ls -l ..."
829
830 test_21() {
831         test_mkdir $DIR/$tdir
832         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
833         ln -s dangle $DIR/$tdir/link
834         echo foo >> $DIR/$tdir/link
835         cat $DIR/$tdir/dangle
836         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
837         $CHECKSTAT -f -t file $DIR/$tdir/link ||
838                 error "$tdir/link not linked to a file"
839 }
840 run_test 21 "write to dangling link"
841
842 test_22() {
843         local wdir=$DIR/$tdir
844         test_mkdir $wdir
845         chown $RUNAS_ID:$RUNAS_GID $wdir
846         (cd $wdir || error "cd $wdir failed";
847                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
848                 $RUNAS tar xf -)
849         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
850         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
851         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
852                 error "checkstat -u failed"
853 }
854 run_test 22 "unpack tar archive as non-root user"
855
856 # was test_23
857 test_23a() {
858         test_mkdir $DIR/$tdir
859         local file=$DIR/$tdir/$tfile
860
861         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
862         openfile -f O_CREAT:O_EXCL $file &&
863                 error "$file recreate succeeded" || true
864 }
865 run_test 23a "O_CREAT|O_EXCL in subdir"
866
867 test_23b() { # bug 18988
868         test_mkdir $DIR/$tdir
869         local file=$DIR/$tdir/$tfile
870
871         rm -f $file
872         echo foo > $file || error "write filed"
873         echo bar >> $file || error "append filed"
874         $CHECKSTAT -s 8 $file || error "wrong size"
875         rm $file
876 }
877 run_test 23b "O_APPEND check"
878
879 # LU-9409, size with O_APPEND and tiny writes
880 test_23c() {
881         local file=$DIR/$tfile
882
883         # single dd
884         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
885         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
886         rm -f $file
887
888         # racing tiny writes
889         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
890         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
891         wait
892         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
893         rm -f $file
894
895         #racing tiny & normal writes
896         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
897         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
898         wait
899         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
900         rm -f $file
901
902         #racing tiny & normal writes 2, ugly numbers
903         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
904         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
905         wait
906         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
907         rm -f $file
908 }
909 run_test 23c "O_APPEND size checks for tiny writes"
910
911 # LU-11069 file offset is correct after appending writes
912 test_23d() {
913         local file=$DIR/$tfile
914         local offset
915
916         echo CentaurHauls > $file
917         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
918         if ((offset != 26)); then
919                 error "wrong offset, expected 26, got '$offset'"
920         fi
921 }
922 run_test 23d "file offset is correct after appending writes"
923
924 # rename sanity
925 test_24a() {
926         echo '-- same directory rename'
927         test_mkdir $DIR/$tdir
928         touch $DIR/$tdir/$tfile.1
929         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
930         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
931 }
932 run_test 24a "rename file to non-existent target"
933
934 test_24b() {
935         test_mkdir $DIR/$tdir
936         touch $DIR/$tdir/$tfile.{1,2}
937         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
938         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
939         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
940 }
941 run_test 24b "rename file to existing target"
942
943 test_24c() {
944         test_mkdir $DIR/$tdir
945         test_mkdir $DIR/$tdir/d$testnum.1
946         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
947         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
948         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
949 }
950 run_test 24c "rename directory to non-existent target"
951
952 test_24d() {
953         test_mkdir -c1 $DIR/$tdir
954         test_mkdir -c1 $DIR/$tdir/d$testnum.1
955         test_mkdir -c1 $DIR/$tdir/d$testnum.2
956         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
957         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
958         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
959 }
960 run_test 24d "rename directory to existing target"
961
962 test_24e() {
963         echo '-- cross directory renames --'
964         test_mkdir $DIR/R5a
965         test_mkdir $DIR/R5b
966         touch $DIR/R5a/f
967         mv $DIR/R5a/f $DIR/R5b/g
968         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
969         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
970 }
971 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
972
973 test_24f() {
974         test_mkdir $DIR/R6a
975         test_mkdir $DIR/R6b
976         touch $DIR/R6a/f $DIR/R6b/g
977         mv $DIR/R6a/f $DIR/R6b/g
978         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
979         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
980 }
981 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
982
983 test_24g() {
984         test_mkdir $DIR/R7a
985         test_mkdir $DIR/R7b
986         test_mkdir $DIR/R7a/d
987         mv $DIR/R7a/d $DIR/R7b/e
988         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
989         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
990 }
991 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
992
993 test_24h() {
994         test_mkdir -c1 $DIR/R8a
995         test_mkdir -c1 $DIR/R8b
996         test_mkdir -c1 $DIR/R8a/d
997         test_mkdir -c1 $DIR/R8b/e
998         mrename $DIR/R8a/d $DIR/R8b/e
999         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
1000         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
1001 }
1002 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
1003
1004 test_24i() {
1005         echo "-- rename error cases"
1006         test_mkdir $DIR/R9
1007         test_mkdir $DIR/R9/a
1008         touch $DIR/R9/f
1009         mrename $DIR/R9/f $DIR/R9/a
1010         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1011         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1012         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1013 }
1014 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1015
1016 test_24j() {
1017         test_mkdir $DIR/R10
1018         mrename $DIR/R10/f $DIR/R10/g
1019         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1020         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1021         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1022 }
1023 run_test 24j "source does not exist ============================"
1024
1025 test_24k() {
1026         test_mkdir $DIR/R11a
1027         test_mkdir $DIR/R11a/d
1028         touch $DIR/R11a/f
1029         mv $DIR/R11a/f $DIR/R11a/d
1030         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1031         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1032 }
1033 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1034
1035 # bug 2429 - rename foo foo foo creates invalid file
1036 test_24l() {
1037         f="$DIR/f24l"
1038         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1039 }
1040 run_test 24l "Renaming a file to itself ========================"
1041
1042 test_24m() {
1043         f="$DIR/f24m"
1044         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1045         # on ext3 this does not remove either the source or target files
1046         # though the "expected" operation would be to remove the source
1047         $CHECKSTAT -t file ${f} || error "${f} missing"
1048         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1049 }
1050 run_test 24m "Renaming a file to a hard link to itself ========="
1051
1052 test_24n() {
1053     f="$DIR/f24n"
1054     # this stats the old file after it was renamed, so it should fail
1055     touch ${f}
1056     $CHECKSTAT ${f} || error "${f} missing"
1057     mv ${f} ${f}.rename
1058     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1059     $CHECKSTAT -a ${f} || error "${f} exists"
1060 }
1061 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1062
1063 test_24o() {
1064         test_mkdir $DIR/$tdir
1065         rename_many -s random -v -n 10 $DIR/$tdir
1066 }
1067 run_test 24o "rename of files during htree split"
1068
1069 test_24p() {
1070         test_mkdir $DIR/R12a
1071         test_mkdir $DIR/R12b
1072         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1073         mrename $DIR/R12a $DIR/R12b
1074         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1075         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1076         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1077         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1078 }
1079 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1080
1081 cleanup_multiop_pause() {
1082         trap 0
1083         kill -USR1 $MULTIPID
1084 }
1085
1086 test_24q() {
1087         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1088
1089         test_mkdir $DIR/R13a
1090         test_mkdir $DIR/R13b
1091         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1092         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1093         MULTIPID=$!
1094
1095         trap cleanup_multiop_pause EXIT
1096         mrename $DIR/R13a $DIR/R13b
1097         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1098         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1099         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1100         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1101         cleanup_multiop_pause
1102         wait $MULTIPID || error "multiop close failed"
1103 }
1104 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1105
1106 test_24r() { #bug 3789
1107         test_mkdir $DIR/R14a
1108         test_mkdir $DIR/R14a/b
1109         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1110         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1111         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1112 }
1113 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1114
1115 test_24s() {
1116         test_mkdir $DIR/R15a
1117         test_mkdir $DIR/R15a/b
1118         test_mkdir $DIR/R15a/b/c
1119         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1120         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1121         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1122 }
1123 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1124 test_24t() {
1125         test_mkdir $DIR/R16a
1126         test_mkdir $DIR/R16a/b
1127         test_mkdir $DIR/R16a/b/c
1128         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1129         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1130         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1131 }
1132 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1133
1134 test_24u() { # bug12192
1135         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1136         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1137 }
1138 run_test 24u "create stripe file"
1139
1140 simple_cleanup_common() {
1141         local rc=0
1142         trap 0
1143         [ -z "$DIR" ] || [ -z "$tdir" ] && return 0
1144
1145         local start=$SECONDS
1146         rm -rf $DIR/$tdir
1147         rc=$?
1148         wait_delete_completed
1149         echo "cleanup time $((SECONDS - start))"
1150         return $rc
1151 }
1152
1153 max_pages_per_rpc() {
1154         local mdtname="$(printf "MDT%04x" ${1:-0})"
1155         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1156 }
1157
1158 test_24v() {
1159         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1160
1161         local nrfiles=${COUNT:-100000}
1162         local fname="$DIR/$tdir/$tfile"
1163
1164         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1165         [ "$mds1_FSTYPE" = "zfs" ] && nrfiles=${COUNT:-10000}
1166
1167         test_mkdir "$(dirname $fname)"
1168         # assume MDT0000 has the fewest inodes
1169         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1170         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1171         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1172
1173         trap simple_cleanup_common EXIT
1174
1175         createmany -m "$fname" $nrfiles
1176
1177         cancel_lru_locks mdc
1178         lctl set_param mdc.*.stats clear
1179
1180         # was previously test_24D: LU-6101
1181         # readdir() returns correct number of entries after cursor reload
1182         local num_ls=$(ls $DIR/$tdir | wc -l)
1183         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1184         local num_all=$(ls -a $DIR/$tdir | wc -l)
1185         if [ $num_ls -ne $nrfiles ] || [ $num_uniq -ne $nrfiles ] ||
1186                 [ $num_all -ne $((nrfiles + 2)) ]; then
1187                         error "Expected $nrfiles files, got $num_ls " \
1188                                 "($num_uniq unique $num_all .&..)"
1189         fi
1190         # LU-5 large readdir
1191         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1192         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1193         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1194         # take into account of overhead in lu_dirpage header and end mark in
1195         # each page, plus one in rpc_num calculation.
1196         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1197         local page_entries=$(((PAGE_SIZE - 24) / dirent_size))
1198         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1199         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1200         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1201         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1202         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1203         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1204                 error "large readdir doesn't take effect: " \
1205                       "$mds_readpage should be about $rpc_max"
1206
1207         simple_cleanup_common
1208 }
1209 run_test 24v "list large directory (test hash collision, b=17560)"
1210
1211 test_24w() { # bug21506
1212         SZ1=234852
1213         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1214         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1215         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1216         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1217         [[ "$SZ1" -eq "$SZ2" ]] ||
1218                 error "Error reading at the end of the file $tfile"
1219 }
1220 run_test 24w "Reading a file larger than 4Gb"
1221
1222 test_24x() {
1223         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1224         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1225         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1226                 skip "Need MDS version at least 2.7.56"
1227
1228         local MDTIDX=1
1229         local remote_dir=$DIR/$tdir/remote_dir
1230
1231         test_mkdir $DIR/$tdir
1232         $LFS mkdir -i $MDTIDX $remote_dir ||
1233                 error "create remote directory failed"
1234
1235         test_mkdir $DIR/$tdir/src_dir
1236         touch $DIR/$tdir/src_file
1237         test_mkdir $remote_dir/tgt_dir
1238         touch $remote_dir/tgt_file
1239
1240         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1241                 error "rename dir cross MDT failed!"
1242
1243         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1244                 error "rename file cross MDT failed!"
1245
1246         touch $DIR/$tdir/ln_file
1247         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1248                 error "ln file cross MDT failed"
1249
1250         rm -rf $DIR/$tdir || error "Can not delete directories"
1251 }
1252 run_test 24x "cross MDT rename/link"
1253
1254 test_24y() {
1255         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1256         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1257
1258         local remote_dir=$DIR/$tdir/remote_dir
1259         local mdtidx=1
1260
1261         test_mkdir $DIR/$tdir
1262         $LFS mkdir -i $mdtidx $remote_dir ||
1263                 error "create remote directory failed"
1264
1265         test_mkdir $remote_dir/src_dir
1266         touch $remote_dir/src_file
1267         test_mkdir $remote_dir/tgt_dir
1268         touch $remote_dir/tgt_file
1269
1270         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1271                 error "rename subdir in the same remote dir failed!"
1272
1273         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1274                 error "rename files in the same remote dir failed!"
1275
1276         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1277                 error "link files in the same remote dir failed!"
1278
1279         rm -rf $DIR/$tdir || error "Can not delete directories"
1280 }
1281 run_test 24y "rename/link on the same dir should succeed"
1282
1283 test_24z() {
1284         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1285         [[ $MDS1_VERSION -lt $(version_code 2.12.51) ]] &&
1286                 skip "Need MDS version at least 2.12.51"
1287
1288         local index
1289
1290         for index in 0 1; do
1291                 $LFS mkdir -i $index $DIR/$tdir.$index || error "mkdir failed"
1292                 touch $DIR/$tdir.0/$tfile.$index || error "touch failed"
1293         done
1294
1295         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1 || error "mv $tfile.0 failed"
1296
1297         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.0)
1298         [ $index -eq 0 ] || error "$tfile.0 is on MDT$index"
1299
1300         local mdts=$(comma_list $(mdts_nodes))
1301
1302         do_nodes $mdts $LCTL set_param mdt.*.enable_remote_rename=0
1303         stack_trap "do_nodes $mdts $LCTL \
1304                 set_param mdt.*.enable_remote_rename=1" EXIT
1305
1306         mv $DIR/$tdir.0/$tfile.1 $DIR/$tdir.1 || error "mv $tfile.1 failed"
1307
1308         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.1)
1309         [ $index -eq 1 ] || error "$tfile.1 is on MDT$index"
1310 }
1311 run_test 24z "cross-MDT rename is done as cp"
1312
1313 test_24A() { # LU-3182
1314         local NFILES=5000
1315
1316         rm -rf $DIR/$tdir
1317         test_mkdir $DIR/$tdir
1318         trap simple_cleanup_common EXIT
1319         createmany -m $DIR/$tdir/$tfile $NFILES
1320         local t=$(ls $DIR/$tdir | wc -l)
1321         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1322         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1323         if [ $t -ne $NFILES ] || [ $u -ne $NFILES ] ||
1324            [ $v -ne $((NFILES + 2)) ] ; then
1325                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1326         fi
1327
1328         simple_cleanup_common || error "Can not delete directories"
1329 }
1330 run_test 24A "readdir() returns correct number of entries."
1331
1332 test_24B() { # LU-4805
1333         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1334
1335         local count
1336
1337         test_mkdir $DIR/$tdir
1338         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1339                 error "create striped dir failed"
1340
1341         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1342         [ $count -eq 2 ] || error "Expected 2, got $count"
1343
1344         touch $DIR/$tdir/striped_dir/a
1345
1346         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1347         [ $count -eq 3 ] || error "Expected 3, got $count"
1348
1349         touch $DIR/$tdir/striped_dir/.f
1350
1351         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1352         [ $count -eq 4 ] || error "Expected 4, got $count"
1353
1354         rm -rf $DIR/$tdir || error "Can not delete directories"
1355 }
1356 run_test 24B "readdir for striped dir return correct number of entries"
1357
1358 test_24C() {
1359         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1360
1361         mkdir $DIR/$tdir
1362         mkdir $DIR/$tdir/d0
1363         mkdir $DIR/$tdir/d1
1364
1365         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1366                 error "create striped dir failed"
1367
1368         cd $DIR/$tdir/d0/striped_dir
1369
1370         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1371         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1372         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1373
1374         [ "$d0_ino" = "$parent_ino" ] ||
1375                 error ".. wrong, expect $d0_ino, get $parent_ino"
1376
1377         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1378                 error "mv striped dir failed"
1379
1380         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1381
1382         [ "$d1_ino" = "$parent_ino" ] ||
1383                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1384 }
1385 run_test 24C "check .. in striped dir"
1386
1387 test_24E() {
1388         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1389         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1390
1391         mkdir -p $DIR/$tdir
1392         mkdir $DIR/$tdir/src_dir
1393         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1394                 error "create remote source failed"
1395
1396         touch $DIR/$tdir/src_dir/src_child/a
1397
1398         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1399                 error "create remote target dir failed"
1400
1401         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1402                 error "create remote target child failed"
1403
1404         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1405                 error "rename dir cross MDT failed!"
1406
1407         find $DIR/$tdir
1408
1409         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1410                 error "src_child still exists after rename"
1411
1412         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1413                 error "missing file(a) after rename"
1414
1415         rm -rf $DIR/$tdir || error "Can not delete directories"
1416 }
1417 run_test 24E "cross MDT rename/link"
1418
1419 test_24F () {
1420         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
1421
1422         local repeats=1000
1423         [ "$SLOW" = "no" ] && repeats=100
1424
1425         mkdir -p $DIR/$tdir
1426
1427         echo "$repeats repeats"
1428         for ((i = 0; i < repeats; i++)); do
1429                 $LFS mkdir -i0 -c2 $DIR/$tdir/test || error "mkdir fails"
1430                 touch $DIR/$tdir/test/a || error "touch fails"
1431                 mkdir $DIR/$tdir/test/b || error "mkdir fails"
1432                 rm -rf $DIR/$tdir/test || error "rmdir fails"
1433         done
1434
1435         true
1436 }
1437 run_test 24F "hash order vs readdir (LU-11330)"
1438
1439 test_25a() {
1440         echo '== symlink sanity ============================================='
1441
1442         test_mkdir $DIR/d25
1443         ln -s d25 $DIR/s25
1444         touch $DIR/s25/foo ||
1445                 error "File creation in symlinked directory failed"
1446 }
1447 run_test 25a "create file in symlinked directory ==============="
1448
1449 test_25b() {
1450         [ ! -d $DIR/d25 ] && test_25a
1451         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1452 }
1453 run_test 25b "lookup file in symlinked directory ==============="
1454
1455 test_26a() {
1456         test_mkdir $DIR/d26
1457         test_mkdir $DIR/d26/d26-2
1458         ln -s d26/d26-2 $DIR/s26
1459         touch $DIR/s26/foo || error "File creation failed"
1460 }
1461 run_test 26a "multiple component symlink ======================="
1462
1463 test_26b() {
1464         test_mkdir -p $DIR/$tdir/d26-2
1465         ln -s $tdir/d26-2/foo $DIR/s26-2
1466         touch $DIR/s26-2 || error "File creation failed"
1467 }
1468 run_test 26b "multiple component symlink at end of lookup ======"
1469
1470 test_26c() {
1471         test_mkdir $DIR/d26.2
1472         touch $DIR/d26.2/foo
1473         ln -s d26.2 $DIR/s26.2-1
1474         ln -s s26.2-1 $DIR/s26.2-2
1475         ln -s s26.2-2 $DIR/s26.2-3
1476         chmod 0666 $DIR/s26.2-3/foo
1477 }
1478 run_test 26c "chain of symlinks"
1479
1480 # recursive symlinks (bug 439)
1481 test_26d() {
1482         ln -s d26-3/foo $DIR/d26-3
1483 }
1484 run_test 26d "create multiple component recursive symlink"
1485
1486 test_26e() {
1487         [ ! -h $DIR/d26-3 ] && test_26d
1488         rm $DIR/d26-3
1489 }
1490 run_test 26e "unlink multiple component recursive symlink"
1491
1492 # recursive symlinks (bug 7022)
1493 test_26f() {
1494         test_mkdir $DIR/$tdir
1495         test_mkdir $DIR/$tdir/$tfile
1496         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1497         test_mkdir -p lndir/bar1
1498         test_mkdir $DIR/$tdir/$tfile/$tfile
1499         cd $tfile                || error "cd $tfile failed"
1500         ln -s .. dotdot          || error "ln dotdot failed"
1501         ln -s dotdot/lndir lndir || error "ln lndir failed"
1502         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1503         output=`ls $tfile/$tfile/lndir/bar1`
1504         [ "$output" = bar1 ] && error "unexpected output"
1505         rm -r $tfile             || error "rm $tfile failed"
1506         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1507 }
1508 run_test 26f "rm -r of a directory which has recursive symlink"
1509
1510 test_27a() {
1511         test_mkdir $DIR/$tdir
1512         $LFS getstripe $DIR/$tdir
1513         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1514         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1515         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1516 }
1517 run_test 27a "one stripe file"
1518
1519 test_27b() {
1520         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1521
1522         test_mkdir $DIR/$tdir
1523         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1524         $LFS getstripe -c $DIR/$tdir/$tfile
1525         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1526                 error "two-stripe file doesn't have two stripes"
1527
1528         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1529 }
1530 run_test 27b "create and write to two stripe file"
1531
1532 # 27c family tests specific striping, setstripe -o
1533 test_27ca() {
1534         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1535         test_mkdir -p $DIR/$tdir
1536         local osts="1"
1537
1538         $LFS setstripe -o $osts $DIR/$tdir/$tfile  || error "setstripe failed"
1539         $LFS getstripe -i $DIR/$tdir/$tfile
1540         [ $($LFS getstripe -i $DIR/$tdir/$tfile ) -eq $osts ] ||
1541                 error "stripe not on specified OST"
1542
1543         dd if=/dev/zero of=$DIR/$tdir/$tfile  bs=1M count=4 || error "dd failed"
1544 }
1545 run_test 27ca "one stripe on specified OST"
1546
1547 test_27cb() {
1548         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1549         test_mkdir -p $DIR/$tdir
1550         local osts="1,0"
1551         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1552         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1553         echo "$getstripe"
1554
1555         # Strip getstripe output to a space separated list of OSTs
1556         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1557                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1558         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1559                 error "stripes not on specified OSTs"
1560
1561         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1562 }
1563 run_test 27cb "two stripes on specified OSTs"
1564
1565 test_27cc() {
1566         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1567         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1568                 skip "server does not support overstriping"
1569
1570         test_mkdir -p $DIR/$tdir
1571         local osts="0,0"
1572         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1573         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1574         echo "$getstripe"
1575
1576         # Strip getstripe output to a space separated list of OSTs
1577         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1578                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1579         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1580                 error "stripes not on specified OSTs"
1581
1582         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1583 }
1584 run_test 27cc "two stripes on the same OST"
1585
1586 test_27cd() {
1587         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1588         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1589                 skip "server does not support overstriping"
1590         test_mkdir -p $DIR/$tdir
1591         local osts="0,1,1,0"
1592         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1593         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1594         echo "$getstripe"
1595
1596         # Strip getstripe output to a space separated list of OSTs
1597         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1598                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1599         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1600                 error "stripes not on specified OSTs"
1601
1602         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1603 }
1604 run_test 27cd "four stripes on two OSTs"
1605
1606 test_27ce() {
1607         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
1608                 skip_env "too many osts, skipping"
1609         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1610                 skip "server does not support overstriping"
1611         # We do one more stripe than we have OSTs
1612         [ $OSTCOUNT -ge 159 ] || large_xattr_enabled ||
1613                 skip_env "ea_inode feature disabled"
1614
1615         test_mkdir -p $DIR/$tdir
1616         local osts=""
1617         for i in $(seq 0 $OSTCOUNT);
1618         do
1619                 osts=$osts"0"
1620                 if [ $i -ne $OSTCOUNT ]; then
1621                         osts=$osts","
1622                 fi
1623         done
1624         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1625         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1626         echo "$getstripe"
1627
1628         # Strip getstripe output to a space separated list of OSTs
1629         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1630                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1631         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1632                 error "stripes not on specified OSTs"
1633
1634         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1635 }
1636 run_test 27ce "more stripes than OSTs with -o"
1637
1638 test_27d() {
1639         test_mkdir $DIR/$tdir
1640         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1641                 error "setstripe failed"
1642         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1643         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1644 }
1645 run_test 27d "create file with default settings"
1646
1647 test_27e() {
1648         # LU-5839 adds check for existed layout before setting it
1649         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1650                 skip "Need MDS version at least 2.7.56"
1651
1652         test_mkdir $DIR/$tdir
1653         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1654         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1655         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1656 }
1657 run_test 27e "setstripe existing file (should return error)"
1658
1659 test_27f() {
1660         test_mkdir $DIR/$tdir
1661         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1662                 error "$LFS setstripe $DIR/$tdir/$tfile failed"
1663         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1664                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1665         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1666         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1667 }
1668 run_test 27f "setstripe with bad stripe size (should return error)"
1669
1670 test_27g() {
1671         test_mkdir $DIR/$tdir
1672         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1673         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1674                 error "$DIR/$tdir/$tfile has object"
1675 }
1676 run_test 27g "$LFS getstripe with no objects"
1677
1678 test_27ga() {
1679         test_mkdir $DIR/$tdir
1680         touch $DIR/$tdir/$tfile || error "touch failed"
1681         ln -s bogus $DIR/$tdir/$tfile.2 || error "ln failed"
1682         $LFS getstripe -m $DIR/$tdir/$tfile $DIR/$tdir/$tfile.2
1683         local rc=$?
1684         (( rc == 2 )) || error "getstripe did not return ENOENT"
1685 }
1686 run_test 27ga "$LFS getstripe with missing file (should return error)"
1687
1688 test_27i() {
1689         test_mkdir $DIR/$tdir
1690         touch $DIR/$tdir/$tfile || error "touch failed"
1691         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1692                 error "missing objects"
1693 }
1694 run_test 27i "$LFS getstripe with some objects"
1695
1696 test_27j() {
1697         test_mkdir $DIR/$tdir
1698         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1699                 error "setstripe failed" || true
1700 }
1701 run_test 27j "setstripe with bad stripe offset (should return error)"
1702
1703 test_27k() { # bug 2844
1704         test_mkdir $DIR/$tdir
1705         local file=$DIR/$tdir/$tfile
1706         local ll_max_blksize=$((4 * 1024 * 1024))
1707         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1708         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1709         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1710         dd if=/dev/zero of=$file bs=4k count=1
1711         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1712         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1713 }
1714 run_test 27k "limit i_blksize for broken user apps"
1715
1716 test_27l() {
1717         mcreate $DIR/$tfile || error "creating file"
1718         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1719                 error "setstripe should have failed" || true
1720 }
1721 run_test 27l "check setstripe permissions (should return error)"
1722
1723 test_27m() {
1724         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1725
1726         [ -n "$RCLIENTS" -o -n "$MOUNT_2" ] &&
1727                 skip_env "multiple clients -- skipping"
1728
1729         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1730                    head -n1)
1731         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1732                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1733         fi
1734         trap simple_cleanup_common EXIT
1735         test_mkdir $DIR/$tdir
1736         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1737         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1738                 error "dd should fill OST0"
1739         i=2
1740         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1741                 i=$((i + 1))
1742                 [ $i -gt 256 ] && break
1743         done
1744         i=$((i + 1))
1745         touch $DIR/$tdir/$tfile.$i
1746         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1747             awk '{print $1}'| grep -w "0") ] &&
1748                 error "OST0 was full but new created file still use it"
1749         i=$((i + 1))
1750         touch $DIR/$tdir/$tfile.$i
1751         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1752             awk '{print $1}'| grep -w "0") ] &&
1753                 error "OST0 was full but new created file still use it"
1754         simple_cleanup_common
1755 }
1756 run_test 27m "create file while OST0 was full"
1757
1758 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1759 # if the OST isn't full anymore.
1760 reset_enospc() {
1761         local OSTIDX=${1:-""}
1762
1763         local list=$(comma_list $(osts_nodes))
1764         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1765
1766         do_nodes $list lctl set_param fail_loc=0
1767         sync    # initiate all OST_DESTROYs from MDS to OST
1768         sleep_maxage
1769 }
1770
1771 exhaust_precreations() {
1772         local OSTIDX=$1
1773         local FAILLOC=$2
1774         local FAILIDX=${3:-$OSTIDX}
1775         local ofacet=ost$((OSTIDX + 1))
1776
1777         test_mkdir -p -c1 $DIR/$tdir
1778         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1779         local mfacet=mds$((mdtidx + 1))
1780         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1781
1782         local OST=$(ostname_from_index $OSTIDX)
1783
1784         # on the mdt's osc
1785         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1786         local last_id=$(do_facet $mfacet lctl get_param -n \
1787                         osp.$mdtosc_proc1.prealloc_last_id)
1788         local next_id=$(do_facet $mfacet lctl get_param -n \
1789                         osp.$mdtosc_proc1.prealloc_next_id)
1790
1791         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1792         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1793
1794         test_mkdir -p $DIR/$tdir/${OST}
1795         $LFS setstripe -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1796 #define OBD_FAIL_OST_ENOSPC              0x215
1797         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1798         echo "Creating to objid $last_id on ost $OST..."
1799         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1800         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1801         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1802         sleep_maxage
1803 }
1804
1805 exhaust_all_precreations() {
1806         local i
1807         for (( i=0; i < OSTCOUNT; i++ )) ; do
1808                 exhaust_precreations $i $1 -1
1809         done
1810 }
1811
1812 test_27n() {
1813         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1814         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1815         remote_mds_nodsh && skip "remote MDS with nodsh"
1816         remote_ost_nodsh && skip "remote OST with nodsh"
1817
1818         reset_enospc
1819         rm -f $DIR/$tdir/$tfile
1820         exhaust_precreations 0 0x80000215
1821         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1822         touch $DIR/$tdir/$tfile || error "touch failed"
1823         $LFS getstripe $DIR/$tdir/$tfile
1824         reset_enospc
1825 }
1826 run_test 27n "create file with some full OSTs"
1827
1828 test_27o() {
1829         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1830         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1831         remote_mds_nodsh && skip "remote MDS with nodsh"
1832         remote_ost_nodsh && skip "remote OST with nodsh"
1833
1834         reset_enospc
1835         rm -f $DIR/$tdir/$tfile
1836         exhaust_all_precreations 0x215
1837
1838         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1839
1840         reset_enospc
1841         rm -rf $DIR/$tdir/*
1842 }
1843 run_test 27o "create file with all full OSTs (should error)"
1844
1845 test_27p() {
1846         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1847         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1848         remote_mds_nodsh && skip "remote MDS with nodsh"
1849         remote_ost_nodsh && skip "remote OST with nodsh"
1850
1851         reset_enospc
1852         rm -f $DIR/$tdir/$tfile
1853         test_mkdir $DIR/$tdir
1854
1855         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1856         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1857         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1858
1859         exhaust_precreations 0 0x80000215
1860         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1861         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1862         $LFS getstripe $DIR/$tdir/$tfile
1863
1864         reset_enospc
1865 }
1866 run_test 27p "append to a truncated file with some full OSTs"
1867
1868 test_27q() {
1869         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1870         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1871         remote_mds_nodsh && skip "remote MDS with nodsh"
1872         remote_ost_nodsh && skip "remote OST with nodsh"
1873
1874         reset_enospc
1875         rm -f $DIR/$tdir/$tfile
1876
1877         test_mkdir $DIR/$tdir
1878         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1879         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1880                 error "truncate $DIR/$tdir/$tfile failed"
1881         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1882
1883         exhaust_all_precreations 0x215
1884
1885         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1886         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1887
1888         reset_enospc
1889 }
1890 run_test 27q "append to truncated file with all OSTs full (should error)"
1891
1892 test_27r() {
1893         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1894         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1895         remote_mds_nodsh && skip "remote MDS with nodsh"
1896         remote_ost_nodsh && skip "remote OST with nodsh"
1897
1898         reset_enospc
1899         rm -f $DIR/$tdir/$tfile
1900         exhaust_precreations 0 0x80000215
1901
1902         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1903
1904         reset_enospc
1905 }
1906 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1907
1908 test_27s() { # bug 10725
1909         test_mkdir $DIR/$tdir
1910         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1911         local stripe_count=0
1912         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1913         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1914                 error "stripe width >= 2^32 succeeded" || true
1915
1916 }
1917 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1918
1919 test_27t() { # bug 10864
1920         WDIR=$(pwd)
1921         WLFS=$(which lfs)
1922         cd $DIR
1923         touch $tfile
1924         $WLFS getstripe $tfile
1925         cd $WDIR
1926 }
1927 run_test 27t "check that utils parse path correctly"
1928
1929 test_27u() { # bug 4900
1930         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1931         remote_mds_nodsh && skip "remote MDS with nodsh"
1932
1933         local index
1934         local list=$(comma_list $(mdts_nodes))
1935
1936 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1937         do_nodes $list $LCTL set_param fail_loc=0x139
1938         test_mkdir -p $DIR/$tdir
1939         trap simple_cleanup_common EXIT
1940         createmany -o $DIR/$tdir/t- 1000
1941         do_nodes $list $LCTL set_param fail_loc=0
1942
1943         TLOG=$TMP/$tfile.getstripe
1944         $LFS getstripe $DIR/$tdir > $TLOG
1945         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1946         unlinkmany $DIR/$tdir/t- 1000
1947         trap 0
1948         [[ $OBJS -gt 0 ]] &&
1949                 error "$OBJS objects created on OST-0. See $TLOG" ||
1950                 rm -f $TLOG
1951 }
1952 run_test 27u "skip object creation on OSC w/o objects"
1953
1954 test_27v() { # bug 4900
1955         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1956         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1957         remote_mds_nodsh && skip "remote MDS with nodsh"
1958         remote_ost_nodsh && skip "remote OST with nodsh"
1959
1960         exhaust_all_precreations 0x215
1961         reset_enospc
1962
1963         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1964
1965         touch $DIR/$tdir/$tfile
1966         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1967         # all except ost1
1968         for (( i=1; i < OSTCOUNT; i++ )); do
1969                 do_facet ost$i lctl set_param fail_loc=0x705
1970         done
1971         local START=`date +%s`
1972         createmany -o $DIR/$tdir/$tfile 32
1973
1974         local FINISH=`date +%s`
1975         local TIMEOUT=`lctl get_param -n timeout`
1976         local PROCESS=$((FINISH - START))
1977         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1978                error "$FINISH - $START >= $TIMEOUT / 2"
1979         sleep $((TIMEOUT / 2 - PROCESS))
1980         reset_enospc
1981 }
1982 run_test 27v "skip object creation on slow OST"
1983
1984 test_27w() { # bug 10997
1985         test_mkdir $DIR/$tdir
1986         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1987         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1988                 error "stripe size $size != 65536" || true
1989         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1990                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1991 }
1992 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1993
1994 test_27wa() {
1995         [[ $OSTCOUNT -lt 2 ]] &&
1996                 skip_env "skipping multiple stripe count/offset test"
1997
1998         test_mkdir $DIR/$tdir
1999         for i in $(seq 1 $OSTCOUNT); do
2000                 offset=$((i - 1))
2001                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
2002                         error "setstripe -c $i -i $offset failed"
2003                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
2004                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
2005                 [ $count -ne $i ] && error "stripe count $count != $i" || true
2006                 [ $index -ne $offset ] &&
2007                         error "stripe offset $index != $offset" || true
2008         done
2009 }
2010 run_test 27wa "check $LFS setstripe -c -i options"
2011
2012 test_27x() {
2013         remote_ost_nodsh && skip "remote OST with nodsh"
2014         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2015         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2016
2017         OFFSET=$(($OSTCOUNT - 1))
2018         OSTIDX=0
2019         local OST=$(ostname_from_index $OSTIDX)
2020
2021         test_mkdir $DIR/$tdir
2022         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
2023         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
2024         sleep_maxage
2025         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
2026         for i in $(seq 0 $OFFSET); do
2027                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
2028                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
2029                 error "OST0 was degraded but new created file still use it"
2030         done
2031         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
2032 }
2033 run_test 27x "create files while OST0 is degraded"
2034
2035 test_27y() {
2036         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2037         remote_mds_nodsh && skip "remote MDS with nodsh"
2038         remote_ost_nodsh && skip "remote OST with nodsh"
2039         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2040
2041         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
2042         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
2043                 osp.$mdtosc.prealloc_last_id)
2044         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
2045                 osp.$mdtosc.prealloc_next_id)
2046         local fcount=$((last_id - next_id))
2047         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
2048         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
2049
2050         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
2051                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
2052         local OST_DEACTIVE_IDX=-1
2053         local OSC
2054         local OSTIDX
2055         local OST
2056
2057         for OSC in $MDS_OSCS; do
2058                 OST=$(osc_to_ost $OSC)
2059                 OSTIDX=$(index_from_ostuuid $OST)
2060                 if [ $OST_DEACTIVE_IDX == -1 ]; then
2061                         OST_DEACTIVE_IDX=$OSTIDX
2062                 fi
2063                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
2064                         echo $OSC "is Deactivated:"
2065                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
2066                 fi
2067         done
2068
2069         OSTIDX=$(index_from_ostuuid $OST)
2070         test_mkdir $DIR/$tdir
2071         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
2072
2073         for OSC in $MDS_OSCS; do
2074                 OST=$(osc_to_ost $OSC)
2075                 OSTIDX=$(index_from_ostuuid $OST)
2076                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2077                         echo $OST "is degraded:"
2078                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2079                                                 obdfilter.$OST.degraded=1
2080                 fi
2081         done
2082
2083         sleep_maxage
2084         createmany -o $DIR/$tdir/$tfile $fcount
2085
2086         for OSC in $MDS_OSCS; do
2087                 OST=$(osc_to_ost $OSC)
2088                 OSTIDX=$(index_from_ostuuid $OST)
2089                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2090                         echo $OST "is recovered from degraded:"
2091                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2092                                                 obdfilter.$OST.degraded=0
2093                 else
2094                         do_facet $SINGLEMDS lctl --device %$OSC activate
2095                 fi
2096         done
2097
2098         # all osp devices get activated, hence -1 stripe count restored
2099         local stripe_count=0
2100
2101         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
2102         # devices get activated.
2103         sleep_maxage
2104         $LFS setstripe -c -1 $DIR/$tfile
2105         stripe_count=$($LFS getstripe -c $DIR/$tfile)
2106         rm -f $DIR/$tfile
2107         [ $stripe_count -ne $OSTCOUNT ] &&
2108                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
2109         return 0
2110 }
2111 run_test 27y "create files while OST0 is degraded and the rest inactive"
2112
2113 check_seq_oid()
2114 {
2115         log "check file $1"
2116
2117         lmm_count=$($LFS getstripe -c $1)
2118         lmm_seq=$($LFS getstripe -v $1 | awk '/lmm_seq/ { print $2 }')
2119         lmm_oid=$($LFS getstripe -v $1 | awk '/lmm_object_id/ { print $2 }')
2120
2121         local old_ifs="$IFS"
2122         IFS=$'[:]'
2123         fid=($($LFS path2fid $1))
2124         IFS="$old_ifs"
2125
2126         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
2127         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
2128
2129         # compare lmm_seq and lu_fid->f_seq
2130         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
2131         # compare lmm_object_id and lu_fid->oid
2132         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
2133
2134         # check the trusted.fid attribute of the OST objects of the file
2135         local have_obdidx=false
2136         local stripe_nr=0
2137         $LFS getstripe $1 | while read obdidx oid hex seq; do
2138                 # skip lines up to and including "obdidx"
2139                 [ -z "$obdidx" ] && break
2140                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
2141                 $have_obdidx || continue
2142
2143                 local ost=$((obdidx + 1))
2144                 local dev=$(ostdevname $ost)
2145                 local oid_hex
2146
2147                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
2148
2149                 seq=$(echo $seq | sed -e "s/^0x//g")
2150                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
2151                         oid_hex=$(echo $oid)
2152                 else
2153                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
2154                 fi
2155                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
2156
2157                 local ff=""
2158                 #
2159                 # Don't unmount/remount the OSTs if we don't need to do that.
2160                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2161                 # update too, until that use mount/ll_decode_filter_fid/mount.
2162                 # Re-enable when debugfs will understand new filter_fid.
2163                 #
2164                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2165                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2166                                 $dev 2>/dev/null" | grep "parent=")
2167                 fi
2168                 if [ -z "$ff" ]; then
2169                         stop ost$ost
2170                         mount_fstype ost$ost
2171                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2172                                 $(facet_mntpt ost$ost)/$obj_file)
2173                         unmount_fstype ost$ost
2174                         start ost$ost $dev $OST_MOUNT_OPTS
2175                         clients_up
2176                 fi
2177
2178                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2179
2180                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2181
2182                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2183                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2184                 #
2185                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2186                 #       stripe_size=1048576 component_id=1 component_start=0 \
2187                 #       component_end=33554432
2188                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2189                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2190                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2191                 local ff_pstripe
2192                 if grep -q 'stripe=' <<<$ff; then
2193                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2194                 else
2195                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2196                         # into f_ver in this case.  See comment on ff_parent.
2197                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2198                 fi
2199
2200                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2201                 [ $ff_pseq = $lmm_seq ] ||
2202                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2203                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2204                 [ $ff_poid = $lmm_oid ] ||
2205                         error "FF parent OID $ff_poid != $lmm_oid"
2206                 (($ff_pstripe == $stripe_nr)) ||
2207                         error "FF stripe $ff_pstripe != $stripe_nr"
2208
2209                 stripe_nr=$((stripe_nr + 1))
2210                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2211                         continue
2212                 if grep -q 'stripe_count=' <<<$ff; then
2213                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2214                                             -e 's/ .*//' <<<$ff)
2215                         [ $lmm_count = $ff_scnt ] ||
2216                                 error "FF stripe count $lmm_count != $ff_scnt"
2217                 fi
2218         done
2219 }
2220
2221 test_27z() {
2222         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2223         remote_ost_nodsh && skip "remote OST with nodsh"
2224
2225         test_mkdir $DIR/$tdir
2226         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2227                 { error "setstripe -c -1 failed"; return 1; }
2228         # We need to send a write to every object to get parent FID info set.
2229         # This _should_ also work for setattr, but does not currently.
2230         # touch $DIR/$tdir/$tfile-1 ||
2231         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2232                 { error "dd $tfile-1 failed"; return 2; }
2233         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2234                 { error "setstripe -c -1 failed"; return 3; }
2235         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2236                 { error "dd $tfile-2 failed"; return 4; }
2237
2238         # make sure write RPCs have been sent to OSTs
2239         sync; sleep 5; sync
2240
2241         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2242         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2243 }
2244 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2245
2246 test_27A() { # b=19102
2247         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2248
2249         save_layout_restore_at_exit $MOUNT
2250         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2251         wait_update $HOSTNAME "$LFS getstripe -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2252                 error "stripe count $($LFS getstripe -c $MOUNT) != 1"
2253         local default_size=$($LFS getstripe -S $MOUNT)
2254         local default_offset=$($LFS getstripe -i $MOUNT)
2255         local dsize=$(do_facet $SINGLEMDS \
2256                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2257         [ $default_size -eq $dsize ] ||
2258                 error "stripe size $default_size != $dsize"
2259         [ $default_offset -eq -1 ] ||
2260                 error "stripe offset $default_offset != -1"
2261 }
2262 run_test 27A "check filesystem-wide default LOV EA values"
2263
2264 test_27B() { # LU-2523
2265         test_mkdir $DIR/$tdir
2266         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2267         touch $DIR/$tdir/f0
2268         # open f1 with O_LOV_DELAY_CREATE
2269         # rename f0 onto f1
2270         # call setstripe ioctl on open file descriptor for f1
2271         # close
2272         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2273                 $DIR/$tdir/f0
2274
2275         rm -f $DIR/$tdir/f1
2276         # open f1 with O_LOV_DELAY_CREATE
2277         # unlink f1
2278         # call setstripe ioctl on open file descriptor for f1
2279         # close
2280         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2281
2282         # Allow multiop to fail in imitation of NFS's busted semantics.
2283         true
2284 }
2285 run_test 27B "call setstripe on open unlinked file/rename victim"
2286
2287 # 27C family tests full striping and overstriping
2288 test_27Ca() { #LU-2871
2289         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2290
2291         declare -a ost_idx
2292         local index
2293         local found
2294         local i
2295         local j
2296
2297         test_mkdir $DIR/$tdir
2298         cd $DIR/$tdir
2299         for i in $(seq 0 $((OSTCOUNT - 1))); do
2300                 # set stripe across all OSTs starting from OST$i
2301                 $LFS setstripe -i $i -c -1 $tfile$i
2302                 # get striping information
2303                 ost_idx=($($LFS getstripe $tfile$i |
2304                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2305                 echo ${ost_idx[@]}
2306
2307                 # check the layout
2308                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2309                         error "${#ost_idx[@]} != $OSTCOUNT"
2310
2311                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2312                         found=0
2313                         for j in $(echo ${ost_idx[@]}); do
2314                                 if [ $index -eq $j ]; then
2315                                         found=1
2316                                         break
2317                                 fi
2318                         done
2319                         [ $found = 1 ] ||
2320                                 error "Can not find $index in ${ost_idx[@]}"
2321                 done
2322         done
2323 }
2324 run_test 27Ca "check full striping across all OSTs"
2325
2326 test_27Cb() {
2327         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2328                 skip "server does not support overstriping"
2329         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2330                 skip_env "too many osts, skipping"
2331
2332         test_mkdir -p $DIR/$tdir
2333         local setcount=$(($OSTCOUNT * 2))
2334         [ $setcount -ge 160 ] || large_xattr_enabled ||
2335                 skip_env "ea_inode feature disabled"
2336
2337         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2338                 error "setstripe failed"
2339
2340         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2341         [ $count -eq $setcount ] ||
2342                 error "stripe count $count, should be $setcount"
2343
2344         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2345                 error "overstriped should be set in pattern"
2346
2347         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2348                 error "dd failed"
2349 }
2350 run_test 27Cb "more stripes than OSTs with -C"
2351
2352 test_27Cc() {
2353         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2354                 skip "server does not support overstriping"
2355         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2356
2357         test_mkdir -p $DIR/$tdir
2358         local setcount=$(($OSTCOUNT - 1))
2359
2360         [ $setcount -ge 160 ] || large_xattr_enabled ||
2361                 skip_env "ea_inode feature disabled"
2362
2363         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2364                 error "setstripe failed"
2365
2366         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2367         [ $count -eq $setcount ] ||
2368                 error "stripe count $count, should be $setcount"
2369
2370         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" &&
2371                 error "overstriped should not be set in pattern"
2372
2373         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2374                 error "dd failed"
2375 }
2376 run_test 27Cc "fewer stripes than OSTs does not set overstriping"
2377
2378 test_27Cd() {
2379         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2380                 skip "server does not support overstriping"
2381         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2382         large_xattr_enabled || skip_env "ea_inode feature disabled"
2383
2384         test_mkdir -p $DIR/$tdir
2385         local setcount=$LOV_MAX_STRIPE_COUNT
2386
2387         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2388                 error "setstripe failed"
2389
2390         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2391         [ $count -eq $setcount ] ||
2392                 error "stripe count $count, should be $setcount"
2393
2394         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2395                 error "overstriped should be set in pattern"
2396
2397         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2398                 error "dd failed"
2399
2400         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2401 }
2402 run_test 27Cd "test maximum stripe count"
2403
2404 test_27Ce() {
2405         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2406                 skip "server does not support overstriping"
2407         test_mkdir -p $DIR/$tdir
2408
2409         pool_add $TESTNAME || error "Pool creation failed"
2410         pool_add_targets $TESTNAME 0 || error "pool_add_targets failed"
2411
2412         local setcount=8
2413
2414         $LFS setstripe  -C $setcount -p "$TESTNAME" $DIR/$tdir/$tfile ||
2415                 error "setstripe failed"
2416
2417         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2418         [ $count -eq $setcount ] ||
2419                 error "stripe count $count, should be $setcount"
2420
2421         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2422                 error "overstriped should be set in pattern"
2423
2424         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2425                 error "dd failed"
2426
2427         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2428 }
2429 run_test 27Ce "test pool with overstriping"
2430
2431 test_27Cf() {
2432         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2433                 skip "server does not support overstriping"
2434         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2435                 skip_env "too many osts, skipping"
2436
2437         test_mkdir -p $DIR/$tdir
2438
2439         local setcount=$(($OSTCOUNT * 2))
2440         [ $setcount -ge 160 ] || large_xattr_enabled ||
2441                 skip_env "ea_inode feature disabled"
2442
2443         $LFS setstripe  -C $setcount $DIR/$tdir/ ||
2444                 error "setstripe failed"
2445
2446         echo 1 > $DIR/$tdir/$tfile
2447
2448         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2449         [ $count -eq $setcount ] ||
2450                 error "stripe count $count, should be $setcount"
2451
2452         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2453                 error "overstriped should be set in pattern"
2454
2455         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2456                 error "dd failed"
2457
2458         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2459 }
2460 run_test 27Cf "test default inheritance with overstriping"
2461
2462 test_27D() {
2463         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2464         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2465         remote_mds_nodsh && skip "remote MDS with nodsh"
2466
2467         local POOL=${POOL:-testpool}
2468         local first_ost=0
2469         local last_ost=$(($OSTCOUNT - 1))
2470         local ost_step=1
2471         local ost_list=$(seq $first_ost $ost_step $last_ost)
2472         local ost_range="$first_ost $last_ost $ost_step"
2473
2474         test_mkdir $DIR/$tdir
2475         pool_add $POOL || error "pool_add failed"
2476         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2477
2478         local skip27D
2479         [ $MDS1_VERSION -lt $(version_code 2.8.55) ] &&
2480                 skip27D+="-s 29"
2481         [ $MDS1_VERSION -lt $(version_code 2.9.55) ] ||
2482                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2483                         skip27D+=" -s 30,31"
2484         [[ ! $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ||
2485           $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2486                 skip27D+=" -s 32,33"
2487         [[ $MDS_VERSION -lt $(version_code $SEL_VER) ]] &&
2488                 skip27D+=" -s 34"
2489         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2490                 error "llapi_layout_test failed"
2491
2492         destroy_test_pools || error "destroy test pools failed"
2493 }
2494 run_test 27D "validate llapi_layout API"
2495
2496 # Verify that default_easize is increased from its initial value after
2497 # accessing a widely striped file.
2498 test_27E() {
2499         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2500         [ $CLIENT_VERSION -lt $(version_code 2.5.57) ] &&
2501                 skip "client does not have LU-3338 fix"
2502
2503         # 72 bytes is the minimum space required to store striping
2504         # information for a file striped across one OST:
2505         # (sizeof(struct lov_user_md_v3) +
2506         #  sizeof(struct lov_user_ost_data_v1))
2507         local min_easize=72
2508         $LCTL set_param -n llite.*.default_easize $min_easize ||
2509                 error "lctl set_param failed"
2510         local easize=$($LCTL get_param -n llite.*.default_easize)
2511
2512         [ $easize -eq $min_easize ] ||
2513                 error "failed to set default_easize"
2514
2515         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2516                 error "setstripe failed"
2517         # In order to ensure stat() call actually talks to MDS we need to
2518         # do something drastic to this file to shake off all lock, e.g.
2519         # rename it (kills lookup lock forcing cache cleaning)
2520         mv $DIR/$tfile $DIR/${tfile}-1
2521         ls -l $DIR/${tfile}-1
2522         rm $DIR/${tfile}-1
2523
2524         easize=$($LCTL get_param -n llite.*.default_easize)
2525
2526         [ $easize -gt $min_easize ] ||
2527                 error "default_easize not updated"
2528 }
2529 run_test 27E "check that default extended attribute size properly increases"
2530
2531 test_27F() { # LU-5346/LU-7975
2532         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2533         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2534         [[ $MDS1_VERSION -lt $(version_code 2.8.51) ]] &&
2535                 skip "Need MDS version at least 2.8.51"
2536         remote_ost_nodsh && skip "remote OST with nodsh"
2537
2538         test_mkdir $DIR/$tdir
2539         rm -f $DIR/$tdir/f0
2540         $LFS setstripe -c 2 $DIR/$tdir
2541
2542         # stop all OSTs to reproduce situation for LU-7975 ticket
2543         for num in $(seq $OSTCOUNT); do
2544                 stop ost$num
2545         done
2546
2547         # open/create f0 with O_LOV_DELAY_CREATE
2548         # truncate f0 to a non-0 size
2549         # close
2550         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2551
2552         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2553         # open/write it again to force delayed layout creation
2554         cat /etc/hosts > $DIR/$tdir/f0 &
2555         catpid=$!
2556
2557         # restart OSTs
2558         for num in $(seq $OSTCOUNT); do
2559                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2560                         error "ost$num failed to start"
2561         done
2562
2563         wait $catpid || error "cat failed"
2564
2565         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2566         [[ $($LFS getstripe -c $DIR/$tdir/f0) == 2 ]] ||
2567                 error "wrong stripecount"
2568
2569 }
2570 run_test 27F "Client resend delayed layout creation with non-zero size"
2571
2572 test_27G() { #LU-10629
2573         [ $MDS1_VERSION -lt $(version_code 2.11.51) ] &&
2574                 skip "Need MDS version at least 2.11.51"
2575         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2576         remote_mds_nodsh && skip "remote MDS with nodsh"
2577         local POOL=${POOL:-testpool}
2578         local ostrange="0 0 1"
2579
2580         test_mkdir $DIR/$tdir
2581         pool_add $POOL || error "pool_add failed"
2582         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2583         $LFS setstripe -p $POOL $DIR/$tdir
2584
2585         local pool=$($LFS getstripe -p $DIR/$tdir)
2586
2587         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2588
2589         $LFS setstripe -d $DIR/$tdir
2590
2591         pool=$($LFS getstripe -p $DIR/$tdir)
2592
2593         rmdir $DIR/$tdir
2594
2595         [ -z "$pool" ] || error "'$pool' is not empty"
2596 }
2597 run_test 27G "Clear OST pool from stripe"
2598
2599 test_27H() {
2600         [[ $MDS1_VERSION -le $(version_code 2.11.54) ]] &&
2601                 skip "Need MDS version newer than 2.11.54"
2602         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2603         test_mkdir $DIR/$tdir
2604         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2605         touch $DIR/$tdir/$tfile
2606         $LFS getstripe -c $DIR/$tdir/$tfile
2607         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2608                 error "two-stripe file doesn't have two stripes"
2609
2610         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2611         $LFS getstripe -y $DIR/$tdir/$tfile
2612         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2613              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2614                 error "expected l_ost_idx: [02]$ not matched"
2615
2616         # make sure ost list has been cleared
2617         local stripesize=$($LFS getstripe -S $DIR/$tdir)
2618         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2619                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2620         touch $DIR/$tdir/f3
2621         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2622 }
2623 run_test 27H "Set specific OSTs stripe"
2624
2625 test_27I() {
2626         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2627         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2628         [[ $MDS1_VERSION -gt $(version_code 2.12.52) ]] ||
2629                 skip "Need MDS version newer than 2.12.52"
2630         local pool=$TESTNAME
2631         local ostrange="1 1 1"
2632
2633         save_layout_restore_at_exit $MOUNT
2634         $LFS setstripe -c 2 -i 0 $MOUNT
2635         pool_add $pool || error "pool_add failed"
2636         pool_add_targets $pool $ostrange || "pool_add_targets failed"
2637         test_mkdir $DIR/$tdir
2638         $LFS setstripe -p $pool $DIR/$tdir
2639         $MULTIOP $DIR/$tdir/$tfile Oc || error "multiop failed"
2640         $LFS getstripe $DIR/$tdir/$tfile
2641 }
2642 run_test 27I "check that root dir striping does not break parent dir one"
2643
2644 test_27J() {
2645         [[ $MDS1_VERSION -le $(version_code 2.12.51) ]] &&
2646                 skip "Need MDS version newer than 2.12.51"
2647
2648         test_mkdir $DIR/$tdir
2649         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2650         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2651
2652         # create foreign file (raw way)
2653         create_foreign_file -f $DIR/$tdir/$tfile -x "${uuid1}@${uuid2}" \
2654                 -t 1 -F 0xda08 || error "create_foreign_file failed"
2655
2656         # verify foreign file (raw way)
2657         parse_foreign_file -f $DIR/$tdir/$tfile |
2658                 grep "lov_foreign_magic: 0x0BD70BD0" ||
2659                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign magic"
2660         parse_foreign_file -f $DIR/$tdir/$tfile | grep "lov_xattr_size: 89" ||
2661                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2662         parse_foreign_file -f $DIR/$tdir/$tfile |
2663                 grep "lov_foreign_size: 73" ||
2664                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2665         parse_foreign_file -f $DIR/$tdir/$tfile |
2666                 grep "lov_foreign_type: 1" ||
2667                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign type"
2668         parse_foreign_file -f $DIR/$tdir/$tfile |
2669                 grep "lov_foreign_flags: 0x0000DA08" ||
2670                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign flags"
2671         local lov=$(parse_foreign_file -f $DIR/$tdir/$tfile |
2672                 grep "lov_foreign_value: 0x" |
2673                 sed -e 's/lov_foreign_value: 0x//')
2674         local lov2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160)
2675         [[ $lov = ${lov2// /} ]] ||
2676                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign value"
2677
2678         # create foreign file (lfs + API)
2679         $LFS setstripe --foreign=daos --flags 0xda08 \
2680                 -x "${uuid1}@${uuid2}" $DIR/$tdir/${tfile}2 ||
2681                 error "$DIR/$tdir/${tfile}2: create failed"
2682
2683         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2684                 grep "lfm_magic:.*0x0BD70BD0" ||
2685                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign magic"
2686         # lfm_length is LOV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2687         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_length:.*73" ||
2688                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign size"
2689         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_type:.*daos" ||
2690                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign type"
2691         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2692                 grep "lfm_flags:.*0x0000DA08" ||
2693                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign flags"
2694         $LFS getstripe $DIR/$tdir/${tfile}2 |
2695                 grep "lfm_value:.*${uuid1}@${uuid2}" ||
2696                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign value"
2697
2698         # modify striping should fail
2699         $LFS setstripe -c 2 $DIR/$tdir/$tfile &&
2700                 error "$DIR/$tdir/$tfile: setstripe should fail"
2701         $LFS setstripe -c 2 $DIR/$tdir/${tfile}2 &&
2702                 error "$DIR/$tdir/${tfile}2: setstripe should fail"
2703
2704         # R/W should fail
2705         cat $DIR/$tdir/$tfile && error "$DIR/$tdir/$tfile: read should fail"
2706         cat $DIR/$tdir/${tfile}2 &&
2707                 error "$DIR/$tdir/${tfile}2: read should fail"
2708         cat /etc/passwd > $DIR/$tdir/$tfile &&
2709                 error "$DIR/$tdir/$tfile: write should fail"
2710         cat /etc/passwd > $DIR/$tdir/${tfile}2 &&
2711                 error "$DIR/$tdir/${tfile}2: write should fail"
2712
2713         # chmod should work
2714         chmod 222 $DIR/$tdir/$tfile ||
2715                 error "$DIR/$tdir/$tfile: chmod failed"
2716         chmod 222 $DIR/$tdir/${tfile}2 ||
2717                 error "$DIR/$tdir/${tfile}2: chmod failed"
2718
2719         # chown should work
2720         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tfile ||
2721                 error "$DIR/$tdir/$tfile: chown failed"
2722         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tfile}2 ||
2723                 error "$DIR/$tdir/${tfile}2: chown failed"
2724
2725         # rename should work
2726         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}.new ||
2727                 error "$DIR/$tdir/$tfile: rename of foreign file has failed"
2728         mv $DIR/$tdir/${tfile}2 $DIR/$tdir/${tfile}2.new ||
2729                 error "$DIR/$tdir/${tfile}2: rename of foreign file has failed"
2730
2731         #remove foreign file
2732         rm $DIR/$tdir/${tfile}.new ||
2733                 error "$DIR/$tdir/${tfile}.new: remove of foreign file has failed"
2734         rm $DIR/$tdir/${tfile}2.new ||
2735                 error "$DIR/$tdir/${tfile}2.new: remove of foreign file has failed"
2736 }
2737 run_test 27J "basic ops on file with foreign LOV"
2738
2739 test_27K() {
2740         [[ $MDS1_VERSION -le $(version_code 2.12.49) ]] &&
2741                 skip "Need MDS version newer than 2.12.49"
2742
2743         test_mkdir $DIR/$tdir
2744         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2745         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2746
2747         # create foreign dir (raw way)
2748         create_foreign_dir -d $DIR/$tdir/$tdir -x "${uuid1}@${uuid2}" -t 1 ||
2749                 error "create_foreign_dir FAILED"
2750
2751         # verify foreign dir (raw way)
2752         parse_foreign_dir -d $DIR/$tdir/$tdir |
2753                 grep "lmv_foreign_magic:.*0xcd50cd0" ||
2754                 error "$DIR/$tdir/$tfile: invalid LMV EA magic"
2755         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_xattr_size:.*89$" ||
2756                 error "$DIR/$tdir/$tdir: invalid LMV EA size"
2757         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_type: 1$" ||
2758                 error "$DIR/$tdir/$tdir: invalid LMV EA type"
2759         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_flags: 0$" ||
2760                 error "$DIR/$tdir/$tdir: invalid LMV EA flags"
2761         local lmv=$(parse_foreign_dir -d $DIR/$tdir/$tdir |
2762                 grep "lmv_foreign_value: 0x" |
2763                 sed 's/lmv_foreign_value: 0x//')
2764         local lmv2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160 |
2765                 sed 's/ //g')
2766         [[ $lmv == $lmv2 ]] || error "$DIR/$tdir/$tdir: invalid LMV EA value"
2767
2768         # create foreign dir (lfs + API)
2769         $LFS mkdir --foreign=daos --xattr="${uuid1}@${uuid2}" --flags=0xda05 \
2770                 $DIR/$tdir/${tdir}2 ||
2771                 error "$DIR/$tdir/${tdir}2: create failed"
2772
2773         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2774                 grep "lfm_magic:.*0x0CD50CD0" ||
2775                 error "$DIR/$tdir/${tdir}2: invalid LMV EA magic"
2776         # lfm_length is LMV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2777         # - sizeof(lfm_type) - sizeof(lfm_flags)
2778         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_length:.*73" ||
2779                 error "$DIR/$tdir/${tdir}2: invalid LMV EA size"
2780         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_type:.*daos" ||
2781                 error "$DIR/$tdir/${tdir}2: invalid LMV EA type"
2782         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2783                 grep "lfm_flags:.*0x0000DA05" ||
2784                 error "$DIR/$tdir/${tdir}2: invalid LMV EA flags"
2785         $LFS getdirstripe $DIR/$tdir/${tdir}2 |
2786                 grep "lfm_value.*${uuid1}@${uuid2}" ||
2787                 error "$DIR/$tdir/${tdir}2: invalid LMV EA value"
2788
2789         # file create in dir should fail
2790         touch $DIR/$tdir/$tdir/$tfile && "$DIR/$tdir: file create should fail"
2791         touch $DIR/$tdir/${tdir}2/$tfile &&
2792                 "$DIR/${tdir}2: file create should fail"
2793
2794         # chmod should work
2795         chmod 777 $DIR/$tdir/$tdir ||
2796                 error "$DIR/$tdir: chmod failed"
2797         chmod 777 $DIR/$tdir/${tdir}2 ||
2798                 error "$DIR/${tdir}2: chmod failed"
2799
2800         # chown should work
2801         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tdir ||
2802                 error "$DIR/$tdir: chown failed"
2803         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tdir}2 ||
2804                 error "$DIR/${tdir}2: chown failed"
2805
2806         # rename should work
2807         mv $DIR/$tdir/$tdir $DIR/$tdir/${tdir}.new ||
2808                 error "$DIR/$tdir/$tdir: rename of foreign dir has failed"
2809         mv $DIR/$tdir/${tdir}2 $DIR/$tdir/${tdir}2.new ||
2810                 error "$DIR/$tdir/${tdir}2: rename of foreign dir has failed"
2811
2812         #remove foreign dir
2813         rmdir $DIR/$tdir/${tdir}.new ||
2814                 error "$DIR/$tdir/${tdir}.new: remove of foreign dir has failed"
2815         rmdir $DIR/$tdir/${tdir}2.new ||
2816                 error "$DIR/$tdir/${tdir}2.new: remove of foreign dir has failed"
2817 }
2818 run_test 27K "basic ops on dir with foreign LMV"
2819
2820 test_27L() {
2821         remote_mds_nodsh && skip "remote MDS with nodsh"
2822
2823         local POOL=${POOL:-$TESTNAME}
2824
2825         pool_add $POOL || error "pool_add failed"
2826
2827         lfs pool_list $MOUNT | grep -Fx "${FSNAME}.${POOL}" ||
2828                  error "pool_list does not contain ${FSNAME}.${POOL}:" \
2829                        "$(lfs pool_list $MOUNT | grep -F "${POOL}")"
2830 }
2831 run_test 27L "lfs pool_list gives correct pool name"
2832
2833 test_27M() {
2834         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.12.57) ]] &&
2835                 skip "Need MDS version >= than 2.12.57"
2836         remote_mds_nodsh && skip "remote MDS with nodsh"
2837         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2838
2839         test_mkdir $DIR/$tdir
2840
2841         # Set default striping on directory
2842         $LFS setstripe -C 4 $DIR/$tdir
2843
2844         echo 1 > $DIR/$tdir/${tfile}.1
2845         local count=$($LFS getstripe -c $DIR/$tdir/${tfile}.1)
2846         local setcount=4
2847         [ $count -eq $setcount ] ||
2848                 error "(1) stripe count $count, should be $setcount"
2849
2850         # Capture existing append_stripe_count setting for restore
2851         local orig_count=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_stripe_count)
2852         local mdts=$(comma_list $(mdts_nodes))
2853         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=$orig_count" EXIT
2854
2855         local appendcount=$orig_count
2856         echo 1 >> $DIR/$tdir/${tfile}.2_append
2857         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.2_append)
2858         [ $count -eq $appendcount ] ||
2859                 error "(2)stripe count $count, should be $appendcount for append"
2860
2861         # Disable O_APPEND striping, verify it works
2862         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2863
2864         # Should now get the default striping, which is 4
2865         setcount=4
2866         echo 1 >> $DIR/$tdir/${tfile}.3_append
2867         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.3_append)
2868         [ $count -eq $setcount ] ||
2869                 error "(3) stripe count $count, should be $setcount"
2870
2871         # Try changing the stripe count for append files
2872         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=2
2873
2874         # Append striping is now 2 (directory default is still 4)
2875         appendcount=2
2876         echo 1 >> $DIR/$tdir/${tfile}.4_append
2877         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.4_append)
2878         [ $count -eq $appendcount ] ||
2879                 error "(4) stripe count $count, should be $appendcount for append"
2880
2881         # Test append stripe count of -1
2882         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=-1
2883         appendcount=$OSTCOUNT
2884         echo 1 >> $DIR/$tdir/${tfile}.5
2885         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.5)
2886         [ $count -eq $appendcount ] ||
2887                 error "(5) stripe count $count, should be $appendcount for append"
2888
2889         # Set append striping back to default of 1
2890         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=1
2891
2892         # Try a new default striping, PFL + DOM
2893         $LFS setstripe -L mdt -E 1M -E -1 -c 2 $DIR/$tdir
2894
2895         # Create normal DOM file, DOM returns stripe count == 0
2896         setcount=0
2897         touch $DIR/$tdir/${tfile}.6
2898         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.6)
2899         [ $count -eq $setcount ] ||
2900                 error "(6) stripe count $count, should be $setcount"
2901
2902         # Show
2903         appendcount=1
2904         echo 1 >> $DIR/$tdir/${tfile}.7_append
2905         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.7_append)
2906         [ $count -eq $appendcount ] ||
2907                 error "(7) stripe count $count, should be $appendcount for append"
2908
2909         # Clean up DOM layout
2910         $LFS setstripe -d $DIR/$tdir
2911
2912         # Now test that append striping works when layout is from root
2913         $LFS setstripe -c 2 $MOUNT
2914         # Make a special directory for this
2915         mkdir $DIR/${tdir}/${tdir}.2
2916         stack_trap "$LFS setstripe -d $MOUNT" EXIT
2917
2918         # Verify for normal file
2919         setcount=2
2920         echo 1 > $DIR/${tdir}/${tdir}.2/${tfile}.8
2921         count=$($LFS getstripe -c $DIR/$tdir/${tdir}.2/${tfile}.8)
2922         [ $count -eq $setcount ] ||
2923                 error "(8) stripe count $count, should be $setcount"
2924
2925         appendcount=1
2926         echo 1 >> $DIR/${tdir}/${tdir}.2/${tfile}.9_append
2927         count=$($LFS getstripe -c $DIR/${tdir}/${tdir}.2/${tfile}.9_append)
2928         [ $count -eq $appendcount ] ||
2929                 error "(9) stripe count $count, should be $appendcount for append"
2930
2931         # Now test O_APPEND striping with pools
2932         do_nodes $mdts $LCTL set_param mdd.*.append_pool="$TESTNAME"
2933         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'" EXIT
2934
2935         # Create the pool
2936         pool_add $TESTNAME || error "pool creation failed"
2937         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
2938
2939         echo 1 >> $DIR/$tdir/${tfile}.10_append
2940
2941         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.10_append)
2942         [ "$pool" = "$TESTNAME" ] || error "(10) incorrect pool: $pool"
2943
2944         # Check that count is still correct
2945         appendcount=1
2946         echo 1 >> $DIR/$tdir/${tfile}.11_append
2947         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.11_append)
2948         [ $count -eq $appendcount ] ||
2949                 error "(11) stripe count $count, should be $appendcount for append"
2950
2951         # Disable O_APPEND stripe count, verify pool works separately
2952         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2953
2954         echo 1 >> $DIR/$tdir/${tfile}.12_append
2955
2956         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.12_append)
2957         [ "$pool" = "$TESTNAME" ] || error "(12) incorrect pool: $pool"
2958
2959         # Remove pool setting, verify it's not applied
2960         do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'
2961
2962         echo 1 >> $DIR/$tdir/${tfile}.13_append
2963
2964         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.13_append)
2965         [ "$pool" = "" ] || error "(13) pool found: $pool"
2966 }
2967 run_test 27M "test O_APPEND striping"
2968
2969 test_27N() {
2970         combined_mgs_mds && skip "needs separate MGS/MDT"
2971
2972         pool_add $TESTNAME || error "pool_add failed"
2973         do_facet mgs "$LCTL pool_list $FSNAME" |
2974                 grep -Fx "${FSNAME}.${TESTNAME}" ||
2975                 error "lctl pool_list on MGS failed"
2976 }
2977 run_test 27N "lctl pool_list on separate MGS gives correct pool name"
2978
2979 # createtest also checks that device nodes are created and
2980 # then visible correctly (#2091)
2981 test_28() { # bug 2091
2982         test_mkdir $DIR/d28
2983         $CREATETEST $DIR/d28/ct || error "createtest failed"
2984 }
2985 run_test 28 "create/mknod/mkdir with bad file types ============"
2986
2987 test_29() {
2988         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2989
2990         sync; sleep 1; sync # flush out any dirty pages from previous tests
2991         cancel_lru_locks
2992         test_mkdir $DIR/d29
2993         touch $DIR/d29/foo
2994         log 'first d29'
2995         ls -l $DIR/d29
2996
2997         declare -i LOCKCOUNTORIG=0
2998         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2999                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
3000         done
3001         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
3002
3003         declare -i LOCKUNUSEDCOUNTORIG=0
3004         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3005                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
3006         done
3007
3008         log 'second d29'
3009         ls -l $DIR/d29
3010         log 'done'
3011
3012         declare -i LOCKCOUNTCURRENT=0
3013         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3014                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
3015         done
3016
3017         declare -i LOCKUNUSEDCOUNTCURRENT=0
3018         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3019                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
3020         done
3021
3022         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
3023                 $LCTL set_param -n ldlm.dump_namespaces ""
3024                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
3025                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3026                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3027                 return 2
3028         fi
3029         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
3030                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
3031                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3032                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3033                 return 3
3034         fi
3035 }
3036 run_test 29 "IT_GETATTR regression  ============================"
3037
3038 test_30a() { # was test_30
3039         cp $(which ls) $DIR || cp /bin/ls $DIR
3040         $DIR/ls / || error "Can't execute binary from lustre"
3041         rm $DIR/ls
3042 }
3043 run_test 30a "execute binary from Lustre (execve) =============="
3044
3045 test_30b() {
3046         cp `which ls` $DIR || cp /bin/ls $DIR
3047         chmod go+rx $DIR/ls
3048         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
3049         rm $DIR/ls
3050 }
3051 run_test 30b "execute binary from Lustre as non-root ==========="
3052
3053 test_30c() { # b=22376
3054         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3055
3056         cp `which ls` $DIR || cp /bin/ls $DIR
3057         chmod a-rw $DIR/ls
3058         cancel_lru_locks mdc
3059         cancel_lru_locks osc
3060         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
3061         rm -f $DIR/ls
3062 }
3063 run_test 30c "execute binary from Lustre without read perms ===="
3064
3065 test_31a() {
3066         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
3067         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
3068 }
3069 run_test 31a "open-unlink file =================================="
3070
3071 test_31b() {
3072         touch $DIR/f31 || error "touch $DIR/f31 failed"
3073         ln $DIR/f31 $DIR/f31b || error "ln failed"
3074         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
3075         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
3076 }
3077 run_test 31b "unlink file with multiple links while open ======="
3078
3079 test_31c() {
3080         touch $DIR/f31 || error "touch $DIR/f31 failed"
3081         ln $DIR/f31 $DIR/f31c || error "ln failed"
3082         multiop_bg_pause $DIR/f31 O_uc ||
3083                 error "multiop_bg_pause for $DIR/f31 failed"
3084         MULTIPID=$!
3085         $MULTIOP $DIR/f31c Ouc
3086         kill -USR1 $MULTIPID
3087         wait $MULTIPID
3088 }
3089 run_test 31c "open-unlink file with multiple links ============="
3090
3091 test_31d() {
3092         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
3093         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
3094 }
3095 run_test 31d "remove of open directory ========================="
3096
3097 test_31e() { # bug 2904
3098         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
3099 }
3100 run_test 31e "remove of open non-empty directory ==============="
3101
3102 test_31f() { # bug 4554
3103         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3104
3105         set -vx
3106         test_mkdir $DIR/d31f
3107         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3108         cp /etc/hosts $DIR/d31f
3109         ls -l $DIR/d31f
3110         $LFS getstripe $DIR/d31f/hosts
3111         multiop_bg_pause $DIR/d31f D_c || return 1
3112         MULTIPID=$!
3113
3114         rm -rv $DIR/d31f || error "first of $DIR/d31f"
3115         test_mkdir $DIR/d31f
3116         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3117         cp /etc/hosts $DIR/d31f
3118         ls -l $DIR/d31f
3119         $LFS getstripe $DIR/d31f/hosts
3120         multiop_bg_pause $DIR/d31f D_c || return 1
3121         MULTIPID2=$!
3122
3123         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
3124         wait $MULTIPID || error "first opendir $MULTIPID failed"
3125
3126         sleep 6
3127
3128         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
3129         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
3130         set +vx
3131 }
3132 run_test 31f "remove of open directory with open-unlink file ==="
3133
3134 test_31g() {
3135         echo "-- cross directory link --"
3136         test_mkdir -c1 $DIR/${tdir}ga
3137         test_mkdir -c1 $DIR/${tdir}gb
3138         touch $DIR/${tdir}ga/f
3139         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
3140         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
3141         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
3142         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
3143         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
3144 }
3145 run_test 31g "cross directory link==============="
3146
3147 test_31h() {
3148         echo "-- cross directory link --"
3149         test_mkdir -c1 $DIR/${tdir}
3150         test_mkdir -c1 $DIR/${tdir}/dir
3151         touch $DIR/${tdir}/f
3152         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
3153         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
3154         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
3155         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
3156         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
3157 }
3158 run_test 31h "cross directory link under child==============="
3159
3160 test_31i() {
3161         echo "-- cross directory link --"
3162         test_mkdir -c1 $DIR/$tdir
3163         test_mkdir -c1 $DIR/$tdir/dir
3164         touch $DIR/$tdir/dir/f
3165         ln $DIR/$tdir/dir/f $DIR/$tdir/g
3166         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
3167         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
3168         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
3169         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
3170 }
3171 run_test 31i "cross directory link under parent==============="
3172
3173 test_31j() {
3174         test_mkdir -c1 -p $DIR/$tdir
3175         test_mkdir -c1 -p $DIR/$tdir/dir1
3176         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
3177         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
3178         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
3179         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
3180         return 0
3181 }
3182 run_test 31j "link for directory==============="
3183
3184 test_31k() {
3185         test_mkdir -c1 -p $DIR/$tdir
3186         touch $DIR/$tdir/s
3187         touch $DIR/$tdir/exist
3188         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
3189         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
3190         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
3191         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
3192         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
3193         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
3194         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
3195         return 0
3196 }
3197 run_test 31k "link to file: the same, non-existing, dir==============="
3198
3199 test_31m() {
3200         mkdir $DIR/d31m
3201         touch $DIR/d31m/s
3202         mkdir $DIR/d31m2
3203         touch $DIR/d31m2/exist
3204         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
3205         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
3206         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
3207         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
3208         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
3209         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
3210         return 0
3211 }
3212 run_test 31m "link to file: the same, non-existing, dir==============="
3213
3214 test_31n() {
3215         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
3216         nlink=$(stat --format=%h $DIR/$tfile)
3217         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3218         local fd=$(free_fd)
3219         local cmd="exec $fd<$DIR/$tfile"
3220         eval $cmd
3221         cmd="exec $fd<&-"
3222         trap "eval $cmd" EXIT
3223         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3224         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3225         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
3226         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3227         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
3228         eval $cmd
3229 }
3230 run_test 31n "check link count of unlinked file"
3231
3232 link_one() {
3233         local tempfile=$(mktemp $1_XXXXXX)
3234         mlink $tempfile $1 2> /dev/null &&
3235                 echo "$BASHPID: link $tempfile to $1 succeeded"
3236         munlink $tempfile
3237 }
3238
3239 test_31o() { # LU-2901
3240         test_mkdir $DIR/$tdir
3241         for LOOP in $(seq 100); do
3242                 rm -f $DIR/$tdir/$tfile*
3243                 for THREAD in $(seq 8); do
3244                         link_one $DIR/$tdir/$tfile.$LOOP &
3245                 done
3246                 wait
3247                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
3248                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
3249                         error "$LINKS duplicate links to $tfile.$LOOP" &&
3250                         break || true
3251         done
3252 }
3253 run_test 31o "duplicate hard links with same filename"
3254
3255 test_31p() {
3256         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3257
3258         test_mkdir $DIR/$tdir
3259         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3260         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
3261
3262         opendirunlink $DIR/$tdir/striped_dir/test1 ||
3263                 error "open unlink test1 failed"
3264         opendirunlink $DIR/$tdir/striped_dir/test2 ||
3265                 error "open unlink test2 failed"
3266
3267         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
3268                 error "test1 still exists"
3269         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
3270                 error "test2 still exists"
3271 }
3272 run_test 31p "remove of open striped directory"
3273
3274 cleanup_test32_mount() {
3275         local rc=0
3276         trap 0
3277         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
3278         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
3279         losetup -d $loopdev || true
3280         rm -rf $DIR/$tdir
3281         return $rc
3282 }
3283
3284 test_32a() {
3285         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3286
3287         echo "== more mountpoints and symlinks ================="
3288         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3289         trap cleanup_test32_mount EXIT
3290         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3291         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3292                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3293         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
3294                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
3295         cleanup_test32_mount
3296 }
3297 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
3298
3299 test_32b() {
3300         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3301
3302         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3303         trap cleanup_test32_mount EXIT
3304         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3305         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3306                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3307         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
3308                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
3309         cleanup_test32_mount
3310 }
3311 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
3312
3313 test_32c() {
3314         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3315
3316         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3317         trap cleanup_test32_mount EXIT
3318         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3319         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3320                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3321         test_mkdir -p $DIR/$tdir/d2/test_dir
3322         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3323                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
3324         cleanup_test32_mount
3325 }
3326 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
3327
3328 test_32d() {
3329         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3330
3331         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3332         trap cleanup_test32_mount EXIT
3333         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3334         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3335                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3336         test_mkdir -p $DIR/$tdir/d2/test_dir
3337         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3338                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
3339         cleanup_test32_mount
3340 }
3341 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
3342
3343 test_32e() {
3344         rm -fr $DIR/$tdir
3345         test_mkdir -p $DIR/$tdir/tmp
3346         local tmp_dir=$DIR/$tdir/tmp
3347         ln -s $DIR/$tdir $tmp_dir/symlink11
3348         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3349         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
3350         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
3351 }
3352 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
3353
3354 test_32f() {
3355         rm -fr $DIR/$tdir
3356         test_mkdir -p $DIR/$tdir/tmp
3357         local tmp_dir=$DIR/$tdir/tmp
3358         ln -s $DIR/$tdir $tmp_dir/symlink11
3359         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3360         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
3361         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
3362 }
3363 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
3364
3365 test_32g() {
3366         local tmp_dir=$DIR/$tdir/tmp
3367         test_mkdir -p $tmp_dir
3368         test_mkdir $DIR/${tdir}2
3369         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3370         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3371         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
3372         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
3373         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
3374         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
3375 }
3376 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3377
3378 test_32h() {
3379         rm -fr $DIR/$tdir $DIR/${tdir}2
3380         tmp_dir=$DIR/$tdir/tmp
3381         test_mkdir -p $tmp_dir
3382         test_mkdir $DIR/${tdir}2
3383         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3384         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3385         ls $tmp_dir/symlink12 || error "listing symlink12"
3386         ls $DIR/$tdir/symlink02  || error "listing symlink02"
3387 }
3388 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3389
3390 test_32i() {
3391         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3392
3393         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3394         trap cleanup_test32_mount EXIT
3395         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3396         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3397                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3398         touch $DIR/$tdir/test_file
3399         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
3400                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
3401         cleanup_test32_mount
3402 }
3403 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
3404
3405 test_32j() {
3406         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3407
3408         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3409         trap cleanup_test32_mount EXIT
3410         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3411         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3412                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3413         touch $DIR/$tdir/test_file
3414         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
3415                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
3416         cleanup_test32_mount
3417 }
3418 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
3419
3420 test_32k() {
3421         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3422
3423         rm -fr $DIR/$tdir
3424         trap cleanup_test32_mount EXIT
3425         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3426         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3427                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3428         test_mkdir -p $DIR/$tdir/d2
3429         touch $DIR/$tdir/d2/test_file || error "touch failed"
3430         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3431                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
3432         cleanup_test32_mount
3433 }
3434 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
3435
3436 test_32l() {
3437         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3438
3439         rm -fr $DIR/$tdir
3440         trap cleanup_test32_mount EXIT
3441         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3442         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3443                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3444         test_mkdir -p $DIR/$tdir/d2
3445         touch $DIR/$tdir/d2/test_file || error "touch failed"
3446         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3447                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
3448         cleanup_test32_mount
3449 }
3450 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
3451
3452 test_32m() {
3453         rm -fr $DIR/d32m
3454         test_mkdir -p $DIR/d32m/tmp
3455         TMP_DIR=$DIR/d32m/tmp
3456         ln -s $DIR $TMP_DIR/symlink11
3457         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3458         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
3459                 error "symlink11 not a link"
3460         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
3461                 error "symlink01 not a link"
3462 }
3463 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
3464
3465 test_32n() {
3466         rm -fr $DIR/d32n
3467         test_mkdir -p $DIR/d32n/tmp
3468         TMP_DIR=$DIR/d32n/tmp
3469         ln -s $DIR $TMP_DIR/symlink11
3470         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3471         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
3472         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
3473 }
3474 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
3475
3476 test_32o() {
3477         touch $DIR/$tfile
3478         test_mkdir -p $DIR/d32o/tmp
3479         TMP_DIR=$DIR/d32o/tmp
3480         ln -s $DIR/$tfile $TMP_DIR/symlink12
3481         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3482         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
3483                 error "symlink12 not a link"
3484         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
3485         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
3486                 error "$DIR/d32o/tmp/symlink12 not file type"
3487         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
3488                 error "$DIR/d32o/symlink02 not file type"
3489 }
3490 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
3491
3492 test_32p() {
3493         log 32p_1
3494         rm -fr $DIR/d32p
3495         log 32p_2
3496         rm -f $DIR/$tfile
3497         log 32p_3
3498         touch $DIR/$tfile
3499         log 32p_4
3500         test_mkdir -p $DIR/d32p/tmp
3501         log 32p_5
3502         TMP_DIR=$DIR/d32p/tmp
3503         log 32p_6
3504         ln -s $DIR/$tfile $TMP_DIR/symlink12
3505         log 32p_7
3506         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3507         log 32p_8
3508         cat $DIR/d32p/tmp/symlink12 ||
3509                 error "Can't open $DIR/d32p/tmp/symlink12"
3510         log 32p_9
3511         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
3512         log 32p_10
3513 }
3514 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
3515
3516 test_32q() {
3517         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3518
3519         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3520         trap cleanup_test32_mount EXIT
3521         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3522         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3523         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3524                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3525         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
3526         cleanup_test32_mount
3527 }
3528 run_test 32q "stat follows mountpoints in Lustre (should return error)"
3529
3530 test_32r() {
3531         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3532
3533         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3534         trap cleanup_test32_mount EXIT
3535         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3536         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3537         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3538                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3539         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
3540         cleanup_test32_mount
3541 }
3542 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
3543
3544 test_33aa() {
3545         rm -f $DIR/$tfile
3546         touch $DIR/$tfile
3547         chmod 444 $DIR/$tfile
3548         chown $RUNAS_ID $DIR/$tfile
3549         log 33_1
3550         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3551         log 33_2
3552 }
3553 run_test 33aa "write file with mode 444 (should return error)"
3554
3555 test_33a() {
3556         rm -fr $DIR/$tdir
3557         test_mkdir $DIR/$tdir
3558         chown $RUNAS_ID $DIR/$tdir
3559         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
3560                 error "$RUNAS create $tdir/$tfile failed"
3561         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
3562                 error "open RDWR" || true
3563 }
3564 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
3565
3566 test_33b() {
3567         rm -fr $DIR/$tdir
3568         test_mkdir $DIR/$tdir
3569         chown $RUNAS_ID $DIR/$tdir
3570         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
3571 }
3572 run_test 33b "test open file with malformed flags (No panic)"
3573
3574 test_33c() {
3575         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3576         remote_ost_nodsh && skip "remote OST with nodsh"
3577
3578         local ostnum
3579         local ostname
3580         local write_bytes
3581         local all_zeros
3582
3583         all_zeros=:
3584         rm -fr $DIR/$tdir
3585         test_mkdir $DIR/$tdir
3586         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
3587
3588         sync
3589         for ostnum in $(seq $OSTCOUNT); do
3590                 # test-framework's OST numbering is one-based, while Lustre's
3591                 # is zero-based
3592                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3593                 # Parsing llobdstat's output sucks; we could grep the /proc
3594                 # path, but that's likely to not be as portable as using the
3595                 # llobdstat utility.  So we parse lctl output instead.
3596                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3597                         obdfilter/$ostname/stats |
3598                         awk '/^write_bytes/ {print $7}' )
3599                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
3600                 if (( ${write_bytes:-0} > 0 ))
3601                 then
3602                         all_zeros=false
3603                         break;
3604                 fi
3605         done
3606
3607         $all_zeros || return 0
3608
3609         # Write four bytes
3610         echo foo > $DIR/$tdir/bar
3611         # Really write them
3612         sync
3613
3614         # Total up write_bytes after writing.  We'd better find non-zeros.
3615         for ostnum in $(seq $OSTCOUNT); do
3616                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3617                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3618                         obdfilter/$ostname/stats |
3619                         awk '/^write_bytes/ {print $7}' )
3620                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
3621                 if (( ${write_bytes:-0} > 0 ))
3622                 then
3623                         all_zeros=false
3624                         break;
3625                 fi
3626         done
3627
3628         if $all_zeros
3629         then
3630                 for ostnum in $(seq $OSTCOUNT); do
3631                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3632                         echo "Check that write_bytes is present in obdfilter/*/stats:"
3633                         do_facet ost$ostnum lctl get_param -n \
3634                                 obdfilter/$ostname/stats
3635                 done
3636                 error "OST not keeping write_bytes stats (b22312)"
3637         fi
3638 }
3639 run_test 33c "test llobdstat and write_bytes"
3640
3641 test_33d() {
3642         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3643         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3644
3645         local MDTIDX=1
3646         local remote_dir=$DIR/$tdir/remote_dir
3647
3648         test_mkdir $DIR/$tdir
3649         $LFS mkdir -i $MDTIDX $remote_dir ||
3650                 error "create remote directory failed"
3651
3652         touch $remote_dir/$tfile
3653         chmod 444 $remote_dir/$tfile
3654         chown $RUNAS_ID $remote_dir/$tfile
3655
3656         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3657
3658         chown $RUNAS_ID $remote_dir
3659         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
3660                                         error "create" || true
3661         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
3662                                     error "open RDWR" || true
3663         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
3664 }
3665 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
3666
3667 test_33e() {
3668         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3669
3670         mkdir $DIR/$tdir
3671
3672         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3673         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3674         mkdir $DIR/$tdir/local_dir
3675
3676         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3677         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3678         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3679
3680         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3681                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
3682
3683         rmdir $DIR/$tdir/* || error "rmdir failed"
3684
3685         umask 777
3686         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3687         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3688         mkdir $DIR/$tdir/local_dir
3689
3690         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3691         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3692         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3693
3694         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3695                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3696
3697         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3698
3699         umask 000
3700         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3701         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3702         mkdir $DIR/$tdir/local_dir
3703
3704         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3705         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3706         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3707
3708         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3709                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3710 }
3711 run_test 33e "mkdir and striped directory should have same mode"
3712
3713 cleanup_33f() {
3714         trap 0
3715         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3716 }
3717
3718 test_33f() {
3719         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3720         remote_mds_nodsh && skip "remote MDS with nodsh"
3721
3722         mkdir $DIR/$tdir
3723         chmod go+rwx $DIR/$tdir
3724         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3725         trap cleanup_33f EXIT
3726
3727         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3728                 error "cannot create striped directory"
3729
3730         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3731                 error "cannot create files in striped directory"
3732
3733         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3734                 error "cannot remove files in striped directory"
3735
3736         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3737                 error "cannot remove striped directory"
3738
3739         cleanup_33f
3740 }
3741 run_test 33f "nonroot user can create, access, and remove a striped directory"
3742
3743 test_33g() {
3744         mkdir -p $DIR/$tdir/dir2
3745
3746         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3747         echo $err
3748         [[ $err =~ "exists" ]] || error "Not exists error"
3749 }
3750 run_test 33g "nonroot user create already existing root created file"
3751
3752 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3753 test_34a() {
3754         rm -f $DIR/f34
3755         $MCREATE $DIR/f34 || error "mcreate failed"
3756         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3757                 error "getstripe failed"
3758         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3759         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3760                 error "getstripe failed"
3761         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3762                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3763 }
3764 run_test 34a "truncate file that has not been opened ==========="
3765
3766 test_34b() {
3767         [ ! -f $DIR/f34 ] && test_34a
3768         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3769                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3770         $OPENFILE -f O_RDONLY $DIR/f34
3771         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3772                 error "getstripe failed"
3773         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3774                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3775 }
3776 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3777
3778 test_34c() {
3779         [ ! -f $DIR/f34 ] && test_34a
3780         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3781                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3782         $OPENFILE -f O_RDWR $DIR/f34
3783         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" &&
3784                 error "$LFS getstripe failed"
3785         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3786                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3787 }
3788 run_test 34c "O_RDWR opening file-with-size works =============="
3789
3790 test_34d() {
3791         [ ! -f $DIR/f34 ] && test_34a
3792         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3793                 error "dd failed"
3794         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3795                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3796         rm $DIR/f34
3797 }
3798 run_test 34d "write to sparse file ============================="
3799
3800 test_34e() {
3801         rm -f $DIR/f34e
3802         $MCREATE $DIR/f34e || error "mcreate failed"
3803         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3804         $CHECKSTAT -s 1000 $DIR/f34e ||
3805                 error "Size of $DIR/f34e not equal to 1000 bytes"
3806         $OPENFILE -f O_RDWR $DIR/f34e
3807         $CHECKSTAT -s 1000 $DIR/f34e ||
3808                 error "Size of $DIR/f34e not equal to 1000 bytes"
3809 }
3810 run_test 34e "create objects, some with size and some without =="
3811
3812 test_34f() { # bug 6242, 6243
3813         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3814
3815         SIZE34F=48000
3816         rm -f $DIR/f34f
3817         $MCREATE $DIR/f34f || error "mcreate failed"
3818         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3819         dd if=$DIR/f34f of=$TMP/f34f
3820         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3821         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3822         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3823         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3824         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3825 }
3826 run_test 34f "read from a file with no objects until EOF ======="
3827
3828 test_34g() {
3829         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3830
3831         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3832                 error "dd failed"
3833         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3834         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3835                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3836         cancel_lru_locks osc
3837         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3838                 error "wrong size after lock cancel"
3839
3840         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3841         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3842                 error "expanding truncate failed"
3843         cancel_lru_locks osc
3844         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3845                 error "wrong expanded size after lock cancel"
3846 }
3847 run_test 34g "truncate long file ==============================="
3848
3849 test_34h() {
3850         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3851
3852         local gid=10
3853         local sz=1000
3854
3855         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3856         sync # Flush the cache so that multiop below does not block on cache
3857              # flush when getting the group lock
3858         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3859         MULTIPID=$!
3860
3861         # Since just timed wait is not good enough, let's do a sync write
3862         # that way we are sure enough time for a roundtrip + processing
3863         # passed + 2 seconds of extra margin.
3864         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
3865         rm $DIR/${tfile}-1
3866         sleep 2
3867
3868         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3869                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3870                 kill -9 $MULTIPID
3871         fi
3872         wait $MULTIPID
3873         local nsz=`stat -c %s $DIR/$tfile`
3874         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3875 }
3876 run_test 34h "ftruncate file under grouplock should not block"
3877
3878 test_35a() {
3879         cp /bin/sh $DIR/f35a
3880         chmod 444 $DIR/f35a
3881         chown $RUNAS_ID $DIR/f35a
3882         $RUNAS $DIR/f35a && error || true
3883         rm $DIR/f35a
3884 }
3885 run_test 35a "exec file with mode 444 (should return and not leak)"
3886
3887 test_36a() {
3888         rm -f $DIR/f36
3889         utime $DIR/f36 || error "utime failed for MDS"
3890 }
3891 run_test 36a "MDS utime check (mknod, utime)"
3892
3893 test_36b() {
3894         echo "" > $DIR/f36
3895         utime $DIR/f36 || error "utime failed for OST"
3896 }
3897 run_test 36b "OST utime check (open, utime)"
3898
3899 test_36c() {
3900         rm -f $DIR/d36/f36
3901         test_mkdir $DIR/d36
3902         chown $RUNAS_ID $DIR/d36
3903         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3904 }
3905 run_test 36c "non-root MDS utime check (mknod, utime)"
3906
3907 test_36d() {
3908         [ ! -d $DIR/d36 ] && test_36c
3909         echo "" > $DIR/d36/f36
3910         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3911 }
3912 run_test 36d "non-root OST utime check (open, utime)"
3913
3914 test_36e() {
3915         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3916
3917         test_mkdir $DIR/$tdir
3918         touch $DIR/$tdir/$tfile
3919         $RUNAS utime $DIR/$tdir/$tfile &&
3920                 error "utime worked, expected failure" || true
3921 }
3922 run_test 36e "utime on non-owned file (should return error)"
3923
3924 subr_36fh() {
3925         local fl="$1"
3926         local LANG_SAVE=$LANG
3927         local LC_LANG_SAVE=$LC_LANG
3928         export LANG=C LC_LANG=C # for date language
3929
3930         DATESTR="Dec 20  2000"
3931         test_mkdir $DIR/$tdir
3932         lctl set_param fail_loc=$fl
3933         date; date +%s
3934         cp /etc/hosts $DIR/$tdir/$tfile
3935         sync & # write RPC generated with "current" inode timestamp, but delayed
3936         sleep 1
3937         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3938         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3939         cancel_lru_locks $OSC
3940         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3941         date; date +%s
3942         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3943                 echo "BEFORE: $LS_BEFORE" && \
3944                 echo "AFTER : $LS_AFTER" && \
3945                 echo "WANT  : $DATESTR" && \
3946                 error "$DIR/$tdir/$tfile timestamps changed" || true
3947
3948         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3949 }
3950
3951 test_36f() {
3952         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3953
3954         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3955         subr_36fh "0x80000214"
3956 }
3957 run_test 36f "utime on file racing with OST BRW write =========="
3958
3959 test_36g() {
3960         remote_ost_nodsh && skip "remote OST with nodsh"
3961         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3962         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
3963                 skip "Need MDS version at least 2.12.51"
3964
3965         local fmd_max_age
3966         local fmd
3967         local facet="ost1"
3968         local tgt="obdfilter"
3969
3970         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
3971
3972         test_mkdir $DIR/$tdir
3973         fmd_max_age=$(do_facet $facet \
3974                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
3975                 head -n 1")
3976
3977         echo "FMD max age: ${fmd_max_age}s"
3978         touch $DIR/$tdir/$tfile
3979         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3980                 gawk '{cnt=cnt+$1}  END{print cnt}')
3981         echo "FMD before: $fmd"
3982         [[ $fmd == 0 ]] &&
3983                 error "FMD wasn't create by touch"
3984         sleep $((fmd_max_age + 12))
3985         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3986                 gawk '{cnt=cnt+$1}  END{print cnt}')
3987         echo "FMD after: $fmd"
3988         [[ $fmd == 0 ]] ||
3989                 error "FMD wasn't expired by ping"
3990 }
3991 run_test 36g "FMD cache expiry ====================="
3992
3993 test_36h() {
3994         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3995
3996         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3997         subr_36fh "0x80000227"
3998 }
3999 run_test 36h "utime on file racing with OST BRW write =========="
4000
4001 test_36i() {
4002         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4003
4004         test_mkdir $DIR/$tdir
4005         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
4006
4007         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
4008         local new_mtime=$((mtime + 200))
4009
4010         #change Modify time of striped dir
4011         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
4012                         error "change mtime failed"
4013
4014         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
4015
4016         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
4017 }
4018 run_test 36i "change mtime on striped directory"
4019
4020 # test_37 - duplicate with tests 32q 32r
4021
4022 test_38() {
4023         local file=$DIR/$tfile
4024         touch $file
4025         openfile -f O_DIRECTORY $file
4026         local RC=$?
4027         local ENOTDIR=20
4028         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
4029         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
4030 }
4031 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
4032
4033 test_39a() { # was test_39
4034         touch $DIR/$tfile
4035         touch $DIR/${tfile}2
4036 #       ls -l  $DIR/$tfile $DIR/${tfile}2
4037 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
4038 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
4039         sleep 2
4040         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
4041         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
4042                 echo "mtime"
4043                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
4044                 echo "atime"
4045                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
4046                 echo "ctime"
4047                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
4048                 error "O_TRUNC didn't change timestamps"
4049         fi
4050 }
4051 run_test 39a "mtime changed on create"
4052
4053 test_39b() {
4054         test_mkdir -c1 $DIR/$tdir
4055         cp -p /etc/passwd $DIR/$tdir/fopen
4056         cp -p /etc/passwd $DIR/$tdir/flink
4057         cp -p /etc/passwd $DIR/$tdir/funlink
4058         cp -p /etc/passwd $DIR/$tdir/frename
4059         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
4060
4061         sleep 1
4062         echo "aaaaaa" >> $DIR/$tdir/fopen
4063         echo "aaaaaa" >> $DIR/$tdir/flink
4064         echo "aaaaaa" >> $DIR/$tdir/funlink
4065         echo "aaaaaa" >> $DIR/$tdir/frename
4066
4067         local open_new=`stat -c %Y $DIR/$tdir/fopen`
4068         local link_new=`stat -c %Y $DIR/$tdir/flink`
4069         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
4070         local rename_new=`stat -c %Y $DIR/$tdir/frename`
4071
4072         cat $DIR/$tdir/fopen > /dev/null
4073         ln $DIR/$tdir/flink $DIR/$tdir/flink2
4074         rm -f $DIR/$tdir/funlink2
4075         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
4076
4077         for (( i=0; i < 2; i++ )) ; do
4078                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
4079                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
4080                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
4081                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
4082
4083                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
4084                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
4085                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
4086                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
4087
4088                 cancel_lru_locks $OSC
4089                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4090         done
4091 }
4092 run_test 39b "mtime change on open, link, unlink, rename  ======"
4093
4094 # this should be set to past
4095 TEST_39_MTIME=`date -d "1 year ago" +%s`
4096
4097 # bug 11063
4098 test_39c() {
4099         touch $DIR1/$tfile
4100         sleep 2
4101         local mtime0=`stat -c %Y $DIR1/$tfile`
4102
4103         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4104         local mtime1=`stat -c %Y $DIR1/$tfile`
4105         [ "$mtime1" = $TEST_39_MTIME ] || \
4106                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
4107
4108         local d1=`date +%s`
4109         echo hello >> $DIR1/$tfile
4110         local d2=`date +%s`
4111         local mtime2=`stat -c %Y $DIR1/$tfile`
4112         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
4113                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
4114
4115         mv $DIR1/$tfile $DIR1/$tfile-1
4116
4117         for (( i=0; i < 2; i++ )) ; do
4118                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
4119                 [ "$mtime2" = "$mtime3" ] || \
4120                         error "mtime ($mtime2) changed (to $mtime3) on rename"
4121
4122                 cancel_lru_locks $OSC
4123                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4124         done
4125 }
4126 run_test 39c "mtime change on rename ==========================="
4127
4128 # bug 21114
4129 test_39d() {
4130         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4131
4132         touch $DIR1/$tfile
4133         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4134
4135         for (( i=0; i < 2; i++ )) ; do
4136                 local mtime=`stat -c %Y $DIR1/$tfile`
4137                 [ $mtime = $TEST_39_MTIME ] || \
4138                         error "mtime($mtime) is not set to $TEST_39_MTIME"
4139
4140                 cancel_lru_locks $OSC
4141                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4142         done
4143 }
4144 run_test 39d "create, utime, stat =============================="
4145
4146 # bug 21114
4147 test_39e() {
4148         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4149
4150         touch $DIR1/$tfile
4151         local mtime1=`stat -c %Y $DIR1/$tfile`
4152
4153         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4154
4155         for (( i=0; i < 2; i++ )) ; do
4156                 local mtime2=`stat -c %Y $DIR1/$tfile`
4157                 [ $mtime2 = $TEST_39_MTIME ] || \
4158                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4159
4160                 cancel_lru_locks $OSC
4161                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4162         done
4163 }
4164 run_test 39e "create, stat, utime, stat ========================"
4165
4166 # bug 21114
4167 test_39f() {
4168         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4169
4170         touch $DIR1/$tfile
4171         mtime1=`stat -c %Y $DIR1/$tfile`
4172
4173         sleep 2
4174         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4175
4176         for (( i=0; i < 2; i++ )) ; do
4177                 local mtime2=`stat -c %Y $DIR1/$tfile`
4178                 [ $mtime2 = $TEST_39_MTIME ] || \
4179                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4180
4181                 cancel_lru_locks $OSC
4182                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4183         done
4184 }
4185 run_test 39f "create, stat, sleep, utime, stat ================="
4186
4187 # bug 11063
4188 test_39g() {
4189         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4190
4191         echo hello >> $DIR1/$tfile
4192         local mtime1=`stat -c %Y $DIR1/$tfile`
4193
4194         sleep 2
4195         chmod o+r $DIR1/$tfile
4196
4197         for (( i=0; i < 2; i++ )) ; do
4198                 local mtime2=`stat -c %Y $DIR1/$tfile`
4199                 [ "$mtime1" = "$mtime2" ] || \
4200                         error "lost mtime: $mtime2, should be $mtime1"
4201
4202                 cancel_lru_locks $OSC
4203                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4204         done
4205 }
4206 run_test 39g "write, chmod, stat ==============================="
4207
4208 # bug 11063
4209 test_39h() {
4210         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4211
4212         touch $DIR1/$tfile
4213         sleep 1
4214
4215         local d1=`date`
4216         echo hello >> $DIR1/$tfile
4217         local mtime1=`stat -c %Y $DIR1/$tfile`
4218
4219         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4220         local d2=`date`
4221         if [ "$d1" != "$d2" ]; then
4222                 echo "write and touch not within one second"
4223         else
4224                 for (( i=0; i < 2; i++ )) ; do
4225                         local mtime2=`stat -c %Y $DIR1/$tfile`
4226                         [ "$mtime2" = $TEST_39_MTIME ] || \
4227                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
4228
4229                         cancel_lru_locks $OSC
4230                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4231                 done
4232         fi
4233 }
4234 run_test 39h "write, utime within one second, stat ============="
4235
4236 test_39i() {
4237         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4238
4239         touch $DIR1/$tfile
4240         sleep 1
4241
4242         echo hello >> $DIR1/$tfile
4243         local mtime1=`stat -c %Y $DIR1/$tfile`
4244
4245         mv $DIR1/$tfile $DIR1/$tfile-1
4246
4247         for (( i=0; i < 2; i++ )) ; do
4248                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4249
4250                 [ "$mtime1" = "$mtime2" ] || \
4251                         error "lost mtime: $mtime2, should be $mtime1"
4252
4253                 cancel_lru_locks $OSC
4254                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4255         done
4256 }
4257 run_test 39i "write, rename, stat =============================="
4258
4259 test_39j() {
4260         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4261
4262         start_full_debug_logging
4263         touch $DIR1/$tfile
4264         sleep 1
4265
4266         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
4267         lctl set_param fail_loc=0x80000412
4268         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
4269                 error "multiop failed"
4270         local multipid=$!
4271         local mtime1=`stat -c %Y $DIR1/$tfile`
4272
4273         mv $DIR1/$tfile $DIR1/$tfile-1
4274
4275         kill -USR1 $multipid
4276         wait $multipid || error "multiop close failed"
4277
4278         for (( i=0; i < 2; i++ )) ; do
4279                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4280                 [ "$mtime1" = "$mtime2" ] ||
4281                         error "mtime is lost on close: $mtime2, " \
4282                               "should be $mtime1"
4283
4284                 cancel_lru_locks
4285                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4286         done
4287         lctl set_param fail_loc=0
4288         stop_full_debug_logging
4289 }
4290 run_test 39j "write, rename, close, stat ======================="
4291
4292 test_39k() {
4293         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4294
4295         touch $DIR1/$tfile
4296         sleep 1
4297
4298         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
4299         local multipid=$!
4300         local mtime1=`stat -c %Y $DIR1/$tfile`
4301
4302         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4303
4304         kill -USR1 $multipid
4305         wait $multipid || error "multiop close failed"
4306
4307         for (( i=0; i < 2; i++ )) ; do
4308                 local mtime2=`stat -c %Y $DIR1/$tfile`
4309
4310                 [ "$mtime2" = $TEST_39_MTIME ] || \
4311                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
4312
4313                 cancel_lru_locks
4314                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4315         done
4316 }
4317 run_test 39k "write, utime, close, stat ========================"
4318
4319 # this should be set to future
4320 TEST_39_ATIME=`date -d "1 year" +%s`
4321
4322 test_39l() {
4323         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4324         remote_mds_nodsh && skip "remote MDS with nodsh"
4325
4326         local atime_diff=$(do_facet $SINGLEMDS \
4327                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4328         rm -rf $DIR/$tdir
4329         mkdir -p $DIR/$tdir
4330
4331         # test setting directory atime to future
4332         touch -a -d @$TEST_39_ATIME $DIR/$tdir
4333         local atime=$(stat -c %X $DIR/$tdir)
4334         [ "$atime" = $TEST_39_ATIME ] ||
4335                 error "atime is not set to future: $atime, $TEST_39_ATIME"
4336
4337         # test setting directory atime from future to now
4338         local now=$(date +%s)
4339         touch -a -d @$now $DIR/$tdir
4340
4341         atime=$(stat -c %X $DIR/$tdir)
4342         [ "$atime" -eq "$now"  ] ||
4343                 error "atime is not updated from future: $atime, $now"
4344
4345         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
4346         sleep 3
4347
4348         # test setting directory atime when now > dir atime + atime_diff
4349         local d1=$(date +%s)
4350         ls $DIR/$tdir
4351         local d2=$(date +%s)
4352         cancel_lru_locks mdc
4353         atime=$(stat -c %X $DIR/$tdir)
4354         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4355                 error "atime is not updated  : $atime, should be $d2"
4356
4357         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
4358         sleep 3
4359
4360         # test not setting directory atime when now < dir atime + atime_diff
4361         ls $DIR/$tdir
4362         cancel_lru_locks mdc
4363         atime=$(stat -c %X $DIR/$tdir)
4364         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4365                 error "atime is updated to $atime, should remain $d1<atime<$d2"
4366
4367         do_facet $SINGLEMDS \
4368                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4369 }
4370 run_test 39l "directory atime update ==========================="
4371
4372 test_39m() {
4373         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4374
4375         touch $DIR1/$tfile
4376         sleep 2
4377         local far_past_mtime=$(date -d "May 29 1953" +%s)
4378         local far_past_atime=$(date -d "Dec 17 1903" +%s)
4379
4380         touch -m -d @$far_past_mtime $DIR1/$tfile
4381         touch -a -d @$far_past_atime $DIR1/$tfile
4382
4383         for (( i=0; i < 2; i++ )) ; do
4384                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
4385                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
4386                         error "atime or mtime set incorrectly"
4387
4388                 cancel_lru_locks $OSC
4389                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4390         done
4391 }
4392 run_test 39m "test atime and mtime before 1970"
4393
4394 test_39n() { # LU-3832
4395         remote_mds_nodsh && skip "remote MDS with nodsh"
4396
4397         local atime_diff=$(do_facet $SINGLEMDS \
4398                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4399         local atime0
4400         local atime1
4401         local atime2
4402
4403         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
4404
4405         rm -rf $DIR/$tfile
4406         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
4407         atime0=$(stat -c %X $DIR/$tfile)
4408
4409         sleep 5
4410         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4411         atime1=$(stat -c %X $DIR/$tfile)
4412
4413         sleep 5
4414         cancel_lru_locks mdc
4415         cancel_lru_locks osc
4416         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4417         atime2=$(stat -c %X $DIR/$tfile)
4418
4419         do_facet $SINGLEMDS \
4420                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4421
4422         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
4423         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
4424 }
4425 run_test 39n "check that O_NOATIME is honored"
4426
4427 test_39o() {
4428         TESTDIR=$DIR/$tdir/$tfile
4429         [ -e $TESTDIR ] && rm -rf $TESTDIR
4430         mkdir -p $TESTDIR
4431         cd $TESTDIR
4432         links1=2
4433         ls
4434         mkdir a b
4435         ls
4436         links2=$(stat -c %h .)
4437         [ $(($links1 + 2)) != $links2 ] &&
4438                 error "wrong links count $(($links1 + 2)) != $links2"
4439         rmdir b
4440         links3=$(stat -c %h .)
4441         [ $(($links1 + 1)) != $links3 ] &&
4442                 error "wrong links count $links1 != $links3"
4443         return 0
4444 }
4445 run_test 39o "directory cached attributes updated after create"
4446
4447 test_39p() {
4448         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4449
4450         local MDTIDX=1
4451         TESTDIR=$DIR/$tdir/$tdir
4452         [ -e $TESTDIR ] && rm -rf $TESTDIR
4453         test_mkdir -p $TESTDIR
4454         cd $TESTDIR
4455         links1=2
4456         ls
4457         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
4458         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
4459         ls
4460         links2=$(stat -c %h .)
4461         [ $(($links1 + 2)) != $links2 ] &&
4462                 error "wrong links count $(($links1 + 2)) != $links2"
4463         rmdir remote_dir2
4464         links3=$(stat -c %h .)
4465         [ $(($links1 + 1)) != $links3 ] &&
4466                 error "wrong links count $links1 != $links3"
4467         return 0
4468 }
4469 run_test 39p "remote directory cached attributes updated after create ========"
4470
4471
4472 test_39q() { # LU-8041
4473         local testdir=$DIR/$tdir
4474         mkdir -p $testdir
4475         multiop_bg_pause $testdir D_c || error "multiop failed"
4476         local multipid=$!
4477         cancel_lru_locks mdc
4478         kill -USR1 $multipid
4479         local atime=$(stat -c %X $testdir)
4480         [ "$atime" -ne 0 ] || error "atime is zero"
4481 }
4482 run_test 39q "close won't zero out atime"
4483
4484 test_40() {
4485         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
4486         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
4487                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
4488         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
4489                 error "$tfile is not 4096 bytes in size"
4490 }
4491 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
4492
4493 test_41() {
4494         # bug 1553
4495         small_write $DIR/f41 18
4496 }
4497 run_test 41 "test small file write + fstat ====================="
4498
4499 count_ost_writes() {
4500         lctl get_param -n ${OSC}.*.stats |
4501                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
4502                         END { printf("%0.0f", writes) }'
4503 }
4504
4505 # decent default
4506 WRITEBACK_SAVE=500
4507 DIRTY_RATIO_SAVE=40
4508 MAX_DIRTY_RATIO=50
4509 BG_DIRTY_RATIO_SAVE=10
4510 MAX_BG_DIRTY_RATIO=25
4511
4512 start_writeback() {
4513         trap 0
4514         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
4515         # dirty_ratio, dirty_background_ratio
4516         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4517                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
4518                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
4519                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
4520         else
4521                 # if file not here, we are a 2.4 kernel
4522                 kill -CONT `pidof kupdated`
4523         fi
4524 }
4525
4526 stop_writeback() {
4527         # setup the trap first, so someone cannot exit the test at the
4528         # exact wrong time and mess up a machine
4529         trap start_writeback EXIT
4530         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
4531         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4532                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
4533                 sysctl -w vm.dirty_writeback_centisecs=0
4534                 sysctl -w vm.dirty_writeback_centisecs=0
4535                 # save and increase /proc/sys/vm/dirty_ratio
4536                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
4537                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
4538                 # save and increase /proc/sys/vm/dirty_background_ratio
4539                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
4540                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
4541         else
4542                 # if file not here, we are a 2.4 kernel
4543                 kill -STOP `pidof kupdated`
4544         fi
4545 }
4546
4547 # ensure that all stripes have some grant before we test client-side cache
4548 setup_test42() {
4549         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
4550                 dd if=/dev/zero of=$i bs=4k count=1
4551                 rm $i
4552         done
4553 }
4554
4555 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
4556 # file truncation, and file removal.
4557 test_42a() {
4558         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4559
4560         setup_test42
4561         cancel_lru_locks $OSC
4562         stop_writeback
4563         sync; sleep 1; sync # just to be safe
4564         BEFOREWRITES=`count_ost_writes`
4565         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
4566         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
4567         AFTERWRITES=`count_ost_writes`
4568         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
4569                 error "$BEFOREWRITES < $AFTERWRITES"
4570         start_writeback
4571 }
4572 run_test 42a "ensure that we don't flush on close"
4573
4574 test_42b() {
4575         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4576
4577         setup_test42
4578         cancel_lru_locks $OSC
4579         stop_writeback
4580         sync
4581         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
4582         BEFOREWRITES=$(count_ost_writes)
4583         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
4584         AFTERWRITES=$(count_ost_writes)
4585         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4586                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
4587         fi
4588         BEFOREWRITES=$(count_ost_writes)
4589         sync || error "sync: $?"
4590         AFTERWRITES=$(count_ost_writes)
4591         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4592                 error "$BEFOREWRITES < $AFTERWRITES on sync"
4593         fi
4594         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
4595         start_writeback
4596         return 0
4597 }
4598 run_test 42b "test destroy of file with cached dirty data ======"
4599
4600 # if these tests just want to test the effect of truncation,
4601 # they have to be very careful.  consider:
4602 # - the first open gets a {0,EOF}PR lock
4603 # - the first write conflicts and gets a {0, count-1}PW
4604 # - the rest of the writes are under {count,EOF}PW
4605 # - the open for truncate tries to match a {0,EOF}PR
4606 #   for the filesize and cancels the PWs.
4607 # any number of fixes (don't get {0,EOF} on open, match
4608 # composite locks, do smarter file size management) fix
4609 # this, but for now we want these tests to verify that
4610 # the cancellation with truncate intent works, so we
4611 # start the file with a full-file pw lock to match against
4612 # until the truncate.
4613 trunc_test() {
4614         test=$1
4615         file=$DIR/$test
4616         offset=$2
4617         cancel_lru_locks $OSC
4618         stop_writeback
4619         # prime the file with 0,EOF PW to match
4620         touch $file
4621         $TRUNCATE $file 0
4622         sync; sync
4623         # now the real test..
4624         dd if=/dev/zero of=$file bs=1024 count=100
4625         BEFOREWRITES=`count_ost_writes`
4626         $TRUNCATE $file $offset
4627         cancel_lru_locks $OSC
4628         AFTERWRITES=`count_ost_writes`
4629         start_writeback
4630 }
4631
4632 test_42c() {
4633         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4634
4635         trunc_test 42c 1024
4636         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
4637                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
4638         rm $file
4639 }
4640 run_test 42c "test partial truncate of file with cached dirty data"
4641
4642 test_42d() {
4643         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4644
4645         trunc_test 42d 0
4646         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
4647                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
4648         rm $file
4649 }
4650 run_test 42d "test complete truncate of file with cached dirty data"
4651
4652 test_42e() { # bug22074
4653         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4654
4655         local TDIR=$DIR/${tdir}e
4656         local pages=16 # hardcoded 16 pages, don't change it.
4657         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
4658         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
4659         local max_dirty_mb
4660         local warmup_files
4661
4662         test_mkdir $DIR/${tdir}e
4663         $LFS setstripe -c 1 $TDIR
4664         createmany -o $TDIR/f $files
4665
4666         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
4667
4668         # we assume that with $OSTCOUNT files, at least one of them will
4669         # be allocated on OST0.
4670         warmup_files=$((OSTCOUNT * max_dirty_mb))
4671         createmany -o $TDIR/w $warmup_files
4672
4673         # write a large amount of data into one file and sync, to get good
4674         # avail_grant number from OST.
4675         for ((i=0; i<$warmup_files; i++)); do
4676                 idx=$($LFS getstripe -i $TDIR/w$i)
4677                 [ $idx -ne 0 ] && continue
4678                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
4679                 break
4680         done
4681         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
4682         sync
4683         $LCTL get_param $proc_osc0/cur_dirty_bytes
4684         $LCTL get_param $proc_osc0/cur_grant_bytes
4685
4686         # create as much dirty pages as we can while not to trigger the actual
4687         # RPCs directly. but depends on the env, VFS may trigger flush during this
4688         # period, hopefully we are good.
4689         for ((i=0; i<$warmup_files; i++)); do
4690                 idx=$($LFS getstripe -i $TDIR/w$i)
4691                 [ $idx -ne 0 ] && continue
4692                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4693         done
4694         $LCTL get_param $proc_osc0/cur_dirty_bytes
4695         $LCTL get_param $proc_osc0/cur_grant_bytes
4696
4697         # perform the real test
4698         $LCTL set_param $proc_osc0/rpc_stats 0
4699         for ((;i<$files; i++)); do
4700                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
4701                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
4702         done
4703         sync
4704         $LCTL get_param $proc_osc0/rpc_stats
4705
4706         local percent=0
4707         local have_ppr=false
4708         $LCTL get_param $proc_osc0/rpc_stats |
4709                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4710                         # skip lines until we are at the RPC histogram data
4711                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4712                         $have_ppr || continue
4713
4714                         # we only want the percent stat for < 16 pages
4715                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4716
4717                         percent=$((percent + WPCT))
4718                         if [[ $percent -gt 15 ]]; then
4719                                 error "less than 16-pages write RPCs" \
4720                                       "$percent% > 15%"
4721                                 break
4722                         fi
4723                 done
4724         rm -rf $TDIR
4725 }
4726 run_test 42e "verify sub-RPC writes are not done synchronously"
4727
4728 test_43A() { # was test_43
4729         test_mkdir $DIR/$tdir
4730         cp -p /bin/ls $DIR/$tdir/$tfile
4731         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4732         pid=$!
4733         # give multiop a chance to open
4734         sleep 1
4735
4736         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4737         kill -USR1 $pid
4738 }
4739 run_test 43A "execution of file opened for write should return -ETXTBSY"
4740
4741 test_43a() {
4742         test_mkdir $DIR/$tdir
4743         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4744         $DIR/$tdir/sleep 60 &
4745         SLEEP_PID=$!
4746         # Make sure exec of $tdir/sleep wins race with truncate
4747         sleep 1
4748         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
4749         kill $SLEEP_PID
4750 }
4751 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4752
4753 test_43b() {
4754         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4755
4756         test_mkdir $DIR/$tdir
4757         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4758         $DIR/$tdir/sleep 60 &
4759         SLEEP_PID=$!
4760         # Make sure exec of $tdir/sleep wins race with truncate
4761         sleep 1
4762         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
4763         kill $SLEEP_PID
4764 }
4765 run_test 43b "truncate of file being executed should return -ETXTBSY"
4766
4767 test_43c() {
4768         local testdir="$DIR/$tdir"
4769         test_mkdir $testdir
4770         cp $SHELL $testdir/
4771         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4772                 ( cd $testdir && md5sum -c )
4773 }
4774 run_test 43c "md5sum of copy into lustre"
4775
4776 test_44A() { # was test_44
4777         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4778
4779         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4780         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4781 }
4782 run_test 44A "zero length read from a sparse stripe"
4783
4784 test_44a() {
4785         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4786                 awk '{ print $2 }')
4787         [ -z "$nstripe" ] && skip "can't get stripe info"
4788         [[ $nstripe -gt $OSTCOUNT ]] &&
4789                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4790
4791         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4792                 awk '{ print $2 }')
4793         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4794                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4795                         awk '{ print $2 }')
4796         fi
4797
4798         OFFSETS="0 $((stride/2)) $((stride-1))"
4799         for offset in $OFFSETS; do
4800                 for i in $(seq 0 $((nstripe-1))); do
4801                         local GLOBALOFFSETS=""
4802                         # size in Bytes
4803                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4804                         local myfn=$DIR/d44a-$size
4805                         echo "--------writing $myfn at $size"
4806                         ll_sparseness_write $myfn $size ||
4807                                 error "ll_sparseness_write"
4808                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4809                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4810                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4811
4812                         for j in $(seq 0 $((nstripe-1))); do
4813                                 # size in Bytes
4814                                 size=$((((j + $nstripe )*$stride + $offset)))
4815                                 ll_sparseness_write $myfn $size ||
4816                                         error "ll_sparseness_write"
4817                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4818                         done
4819                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4820                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4821                         rm -f $myfn
4822                 done
4823         done
4824 }
4825 run_test 44a "test sparse pwrite ==============================="
4826
4827 dirty_osc_total() {
4828         tot=0
4829         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4830                 tot=$(($tot + $d))
4831         done
4832         echo $tot
4833 }
4834 do_dirty_record() {
4835         before=`dirty_osc_total`
4836         echo executing "\"$*\""
4837         eval $*
4838         after=`dirty_osc_total`
4839         echo before $before, after $after
4840 }
4841 test_45() {
4842         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4843
4844         f="$DIR/f45"
4845         # Obtain grants from OST if it supports it
4846         echo blah > ${f}_grant
4847         stop_writeback
4848         sync
4849         do_dirty_record "echo blah > $f"
4850         [[ $before -eq $after ]] && error "write wasn't cached"
4851         do_dirty_record "> $f"
4852         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4853         do_dirty_record "echo blah > $f"
4854         [[ $before -eq $after ]] && error "write wasn't cached"
4855         do_dirty_record "sync"
4856         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4857         do_dirty_record "echo blah > $f"
4858         [[ $before -eq $after ]] && error "write wasn't cached"
4859         do_dirty_record "cancel_lru_locks osc"
4860         [[ $before -gt $after ]] ||
4861                 error "lock cancellation didn't lower dirty count"
4862         start_writeback
4863 }
4864 run_test 45 "osc io page accounting ============================"
4865
4866 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4867 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4868 # objects offset and an assert hit when an rpc was built with 1023's mapped
4869 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4870 test_46() {
4871         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4872
4873         f="$DIR/f46"
4874         stop_writeback
4875         sync
4876         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4877         sync
4878         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
4879         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4880         sync
4881         start_writeback
4882 }
4883 run_test 46 "dirtying a previously written page ================"
4884
4885 # test_47 is removed "Device nodes check" is moved to test_28
4886
4887 test_48a() { # bug 2399
4888         [ "$mds1_FSTYPE" = "zfs" ] &&
4889         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
4890                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
4891
4892         test_mkdir $DIR/$tdir
4893         cd $DIR/$tdir
4894         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
4895         test_mkdir $DIR/$tdir
4896         touch foo || error "'touch foo' failed after recreating cwd"
4897         test_mkdir bar
4898         touch .foo || error "'touch .foo' failed after recreating cwd"
4899         test_mkdir .bar
4900         ls . > /dev/null || error "'ls .' failed after recreating cwd"
4901         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4902         cd . || error "'cd .' failed after recreating cwd"
4903         mkdir . && error "'mkdir .' worked after recreating cwd"
4904         rmdir . && error "'rmdir .' worked after recreating cwd"
4905         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4906         cd .. || error "'cd ..' failed after recreating cwd"
4907 }
4908 run_test 48a "Access renamed working dir (should return errors)="
4909
4910 test_48b() { # bug 2399
4911         rm -rf $DIR/$tdir
4912         test_mkdir $DIR/$tdir
4913         cd $DIR/$tdir
4914         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4915         touch foo && error "'touch foo' worked after removing cwd"
4916         mkdir foo && error "'mkdir foo' worked after removing cwd"
4917         touch .foo && error "'touch .foo' worked after removing cwd"
4918         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4919         ls . > /dev/null && error "'ls .' worked after removing cwd"
4920         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4921         mkdir . && error "'mkdir .' worked after removing cwd"
4922         rmdir . && error "'rmdir .' worked after removing cwd"
4923         ln -s . foo && error "'ln -s .' worked after removing cwd"
4924         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4925 }
4926 run_test 48b "Access removed working dir (should return errors)="
4927
4928 test_48c() { # bug 2350
4929         #lctl set_param debug=-1
4930         #set -vx
4931         rm -rf $DIR/$tdir
4932         test_mkdir -p $DIR/$tdir/dir
4933         cd $DIR/$tdir/dir
4934         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4935         $TRACE touch foo && error "touch foo worked after removing cwd"
4936         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4937         touch .foo && error "touch .foo worked after removing cwd"
4938         mkdir .foo && error "mkdir .foo worked after removing cwd"
4939         $TRACE ls . && error "'ls .' worked after removing cwd"
4940         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4941         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4942         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4943         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4944         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4945 }
4946 run_test 48c "Access removed working subdir (should return errors)"
4947
4948 test_48d() { # bug 2350
4949         #lctl set_param debug=-1
4950         #set -vx
4951         rm -rf $DIR/$tdir
4952         test_mkdir -p $DIR/$tdir/dir
4953         cd $DIR/$tdir/dir
4954         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4955         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4956         $TRACE touch foo && error "'touch foo' worked after removing parent"
4957         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4958         touch .foo && error "'touch .foo' worked after removing parent"
4959         mkdir .foo && error "mkdir .foo worked after removing parent"
4960         $TRACE ls . && error "'ls .' worked after removing parent"
4961         $TRACE ls .. && error "'ls ..' worked after removing parent"
4962         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
4963         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4964         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4965         true
4966 }
4967 run_test 48d "Access removed parent subdir (should return errors)"
4968
4969 test_48e() { # bug 4134
4970         #lctl set_param debug=-1
4971         #set -vx
4972         rm -rf $DIR/$tdir
4973         test_mkdir -p $DIR/$tdir/dir
4974         cd $DIR/$tdir/dir
4975         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4976         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4977         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4978         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4979         # On a buggy kernel addition of "touch foo" after cd .. will
4980         # produce kernel oops in lookup_hash_it
4981         touch ../foo && error "'cd ..' worked after recreate parent"
4982         cd $DIR
4983         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4984 }
4985 run_test 48e "Access to recreated parent subdir (should return errors)"
4986
4987 test_49() { # LU-1030
4988         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4989         remote_ost_nodsh && skip "remote OST with nodsh"
4990
4991         # get ost1 size - $FSNAME-OST0000
4992         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4993                 awk '{ print $4 }')
4994         # write 800M at maximum
4995         [[ $ost1_size -lt 2 ]] && ost1_size=2
4996         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4997
4998         $LFS setstripe -c 1 -i 0 $DIR/$tfile
4999         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
5000         local dd_pid=$!
5001
5002         # change max_pages_per_rpc while writing the file
5003         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
5004         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
5005         # loop until dd process exits
5006         while ps ax -opid | grep -wq $dd_pid; do
5007                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
5008                 sleep $((RANDOM % 5 + 1))
5009         done
5010         # restore original max_pages_per_rpc
5011         $LCTL set_param $osc1_mppc=$orig_mppc
5012         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
5013 }
5014 run_test 49 "Change max_pages_per_rpc won't break osc extent"
5015
5016 test_50() {
5017         # bug 1485
5018         test_mkdir $DIR/$tdir
5019         cd $DIR/$tdir
5020         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
5021 }
5022 run_test 50 "special situations: /proc symlinks  ==============="
5023
5024 test_51a() {    # was test_51
5025         # bug 1516 - create an empty entry right after ".." then split dir
5026         test_mkdir -c1 $DIR/$tdir
5027         touch $DIR/$tdir/foo
5028         $MCREATE $DIR/$tdir/bar
5029         rm $DIR/$tdir/foo
5030         createmany -m $DIR/$tdir/longfile 201
5031         FNUM=202
5032         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
5033                 $MCREATE $DIR/$tdir/longfile$FNUM
5034                 FNUM=$(($FNUM + 1))
5035                 echo -n "+"
5036         done
5037         echo
5038         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
5039 }
5040 run_test 51a "special situations: split htree with empty entry =="
5041
5042 cleanup_print_lfs_df () {
5043         trap 0
5044         $LFS df
5045         $LFS df -i
5046 }
5047
5048 test_51b() {
5049         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5050
5051         local dir=$DIR/$tdir
5052         local nrdirs=$((65536 + 100))
5053
5054         # cleanup the directory
5055         rm -fr $dir
5056
5057         test_mkdir -c1 $dir
5058
5059         $LFS df
5060         $LFS df -i
5061         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
5062         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
5063         [[ $numfree -lt $nrdirs ]] &&
5064                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
5065
5066         # need to check free space for the directories as well
5067         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
5068         numfree=$(( blkfree / $(fs_inode_ksize) ))
5069         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
5070
5071         trap cleanup_print_lfs_df EXIT
5072
5073         # create files
5074         createmany -d $dir/d $nrdirs || {
5075                 unlinkmany $dir/d $nrdirs
5076                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
5077         }
5078
5079         # really created :
5080         nrdirs=$(ls -U $dir | wc -l)
5081
5082         # unlink all but 100 subdirectories, then check it still works
5083         local left=100
5084         local delete=$((nrdirs - left))
5085
5086         $LFS df
5087         $LFS df -i
5088
5089         # for ldiskfs the nlink count should be 1, but this is OSD specific
5090         # and so this is listed for informational purposes only
5091         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
5092         unlinkmany -d $dir/d $delete ||
5093                 error "unlink of first $delete subdirs failed"
5094
5095         echo "nlink between: $(stat -c %h $dir)"
5096         local found=$(ls -U $dir | wc -l)
5097         [ $found -ne $left ] &&
5098                 error "can't find subdirs: found only $found, expected $left"
5099
5100         unlinkmany -d $dir/d $delete $left ||
5101                 error "unlink of second $left subdirs failed"
5102         # regardless of whether the backing filesystem tracks nlink accurately
5103         # or not, the nlink count shouldn't be more than "." and ".." here
5104         local after=$(stat -c %h $dir)
5105         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
5106                 echo "nlink after: $after"
5107
5108         cleanup_print_lfs_df
5109 }
5110 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
5111
5112 test_51d() {
5113         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5114         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
5115
5116         test_mkdir $DIR/$tdir
5117         createmany -o $DIR/$tdir/t- 1000
5118         $LFS getstripe $DIR/$tdir > $TMP/$tfile
5119         for N in $(seq 0 $((OSTCOUNT - 1))); do
5120                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
5121                         END { printf("%0.0f", objs) }' $TMP/$tfile)
5122                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
5123                         '($1 == '$N') { objs += 1 } \
5124                         END { printf("%0.0f", objs) }')
5125                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
5126         done
5127         unlinkmany $DIR/$tdir/t- 1000
5128
5129         NLAST=0
5130         for N in $(seq 1 $((OSTCOUNT - 1))); do
5131                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
5132                         error "OST $N has less objects vs OST $NLAST" \
5133                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5134                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
5135                         error "OST $N has less objects vs OST $NLAST" \
5136                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5137
5138                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
5139                         error "OST $N has less #0 objects vs OST $NLAST" \
5140                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5141                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
5142                         error "OST $N has less #0 objects vs OST $NLAST" \
5143                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5144                 NLAST=$N
5145         done
5146         rm -f $TMP/$tfile
5147 }
5148 run_test 51d "check object distribution"
5149
5150 test_51e() {
5151         if [ "$mds1_FSTYPE" != ldiskfs ]; then
5152                 skip_env "ldiskfs only test"
5153         fi
5154
5155         test_mkdir -c1 $DIR/$tdir
5156         test_mkdir -c1 $DIR/$tdir/d0
5157
5158         touch $DIR/$tdir/d0/foo
5159         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
5160                 error "file exceed 65000 nlink limit!"
5161         unlinkmany $DIR/$tdir/d0/f- 65001
5162         return 0
5163 }
5164 run_test 51e "check file nlink limit"
5165
5166 test_51f() {
5167         test_mkdir $DIR/$tdir
5168
5169         local max=100000
5170         local ulimit_old=$(ulimit -n)
5171         local spare=20 # number of spare fd's for scripts/libraries, etc.
5172         local mdt=$($LFS getstripe -m $DIR/$tdir)
5173         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
5174
5175         echo "MDT$mdt numfree=$numfree, max=$max"
5176         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
5177         if [ $((numfree + spare)) -gt $ulimit_old ]; then
5178                 while ! ulimit -n $((numfree + spare)); do
5179                         numfree=$((numfree * 3 / 4))
5180                 done
5181                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
5182         else
5183                 echo "left ulimit at $ulimit_old"
5184         fi
5185
5186         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
5187                 unlinkmany $DIR/$tdir/f $numfree
5188                 error "create+open $numfree files in $DIR/$tdir failed"
5189         }
5190         ulimit -n $ulimit_old
5191
5192         # if createmany exits at 120s there will be fewer than $numfree files
5193         unlinkmany $DIR/$tdir/f $numfree || true
5194 }
5195 run_test 51f "check many open files limit"
5196
5197 test_52a() {
5198         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
5199         test_mkdir $DIR/$tdir
5200         touch $DIR/$tdir/foo
5201         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
5202         echo bar >> $DIR/$tdir/foo || error "append bar failed"
5203         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5204         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5205         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5206                                         error "link worked"
5207         echo foo >> $DIR/$tdir/foo || error "append foo failed"
5208         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5209         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
5210                                                      error "lsattr"
5211         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
5212         cp -r $DIR/$tdir $TMP/
5213         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
5214 }
5215 run_test 52a "append-only flag test (should return errors)"
5216
5217 test_52b() {
5218         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
5219         test_mkdir $DIR/$tdir
5220         touch $DIR/$tdir/foo
5221         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
5222         cat test > $DIR/$tdir/foo && error "cat test worked"
5223         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5224         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5225         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5226                                         error "link worked"
5227         echo foo >> $DIR/$tdir/foo && error "echo worked"
5228         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5229         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
5230         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
5231         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
5232                                                         error "lsattr"
5233         chattr -i $DIR/$tdir/foo || error "chattr failed"
5234
5235         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
5236 }
5237 run_test 52b "immutable flag test (should return errors) ======="
5238
5239 test_53() {
5240         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5241         remote_mds_nodsh && skip "remote MDS with nodsh"
5242         remote_ost_nodsh && skip "remote OST with nodsh"
5243
5244         local param
5245         local param_seq
5246         local ostname
5247         local mds_last
5248         local mds_last_seq
5249         local ost_last
5250         local ost_last_seq
5251         local ost_last_id
5252         local ostnum
5253         local node
5254         local found=false
5255         local support_last_seq=true
5256
5257         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
5258                 support_last_seq=false
5259
5260         # only test MDT0000
5261         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
5262         local value
5263         for value in $(do_facet $SINGLEMDS \
5264                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
5265                 param=$(echo ${value[0]} | cut -d "=" -f1)
5266                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
5267
5268                 if $support_last_seq; then
5269                         param_seq=$(echo $param |
5270                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
5271                         mds_last_seq=$(do_facet $SINGLEMDS \
5272                                        $LCTL get_param -n $param_seq)
5273                 fi
5274                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
5275
5276                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
5277                 node=$(facet_active_host ost$((ostnum+1)))
5278                 param="obdfilter.$ostname.last_id"
5279                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
5280                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
5281                         ost_last_id=$ost_last
5282
5283                         if $support_last_seq; then
5284                                 ost_last_id=$(echo $ost_last |
5285                                               awk -F':' '{print $2}' |
5286                                               sed -e "s/^0x//g")
5287                                 ost_last_seq=$(echo $ost_last |
5288                                                awk -F':' '{print $1}')
5289                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
5290                         fi
5291
5292                         if [[ $ost_last_id != $mds_last ]]; then
5293                                 error "$ost_last_id != $mds_last"
5294                         else
5295                                 found=true
5296                                 break
5297                         fi
5298                 done
5299         done
5300         $found || error "can not match last_seq/last_id for $mdtosc"
5301         return 0
5302 }
5303 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
5304
5305 test_54a() {
5306         perl -MSocket -e ';' || skip "no Socket perl module installed"
5307
5308         $SOCKETSERVER $DIR/socket ||
5309                 error "$SOCKETSERVER $DIR/socket failed: $?"
5310         $SOCKETCLIENT $DIR/socket ||
5311                 error "$SOCKETCLIENT $DIR/socket failed: $?"
5312         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
5313 }
5314 run_test 54a "unix domain socket test =========================="
5315
5316 test_54b() {
5317         f="$DIR/f54b"
5318         mknod $f c 1 3
5319         chmod 0666 $f
5320         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
5321 }
5322 run_test 54b "char device works in lustre ======================"
5323
5324 find_loop_dev() {
5325         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
5326         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
5327         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
5328
5329         for i in $(seq 3 7); do
5330                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
5331                 LOOPDEV=$LOOPBASE$i
5332                 LOOPNUM=$i
5333                 break
5334         done
5335 }
5336
5337 cleanup_54c() {
5338         local rc=0
5339         loopdev="$DIR/loop54c"
5340
5341         trap 0
5342         $UMOUNT $DIR/$tdir || rc=$?
5343         losetup -d $loopdev || true
5344         losetup -d $LOOPDEV || true
5345         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
5346         return $rc
5347 }
5348
5349 test_54c() {
5350         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5351
5352         loopdev="$DIR/loop54c"
5353
5354         find_loop_dev
5355         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
5356         trap cleanup_54c EXIT
5357         mknod $loopdev b 7 $LOOPNUM
5358         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
5359         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
5360         losetup $loopdev $DIR/$tfile ||
5361                 error "can't set up $loopdev for $DIR/$tfile"
5362         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
5363         test_mkdir $DIR/$tdir
5364         mount -t ext2 $loopdev $DIR/$tdir ||
5365                 error "error mounting $loopdev on $DIR/$tdir"
5366         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
5367                 error "dd write"
5368         df $DIR/$tdir
5369         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
5370                 error "dd read"
5371         cleanup_54c
5372 }
5373 run_test 54c "block device works in lustre ====================="
5374
5375 test_54d() {
5376         f="$DIR/f54d"
5377         string="aaaaaa"
5378         mknod $f p
5379         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
5380 }
5381 run_test 54d "fifo device works in lustre ======================"
5382
5383 test_54e() {
5384         f="$DIR/f54e"
5385         string="aaaaaa"
5386         cp -aL /dev/console $f
5387         echo $string > $f || error "echo $string to $f failed"
5388 }
5389 run_test 54e "console/tty device works in lustre ======================"
5390
5391 test_56a() {
5392         local numfiles=3
5393         local dir=$DIR/$tdir
5394
5395         rm -rf $dir
5396         test_mkdir -p $dir/dir
5397         for i in $(seq $numfiles); do
5398                 touch $dir/file$i
5399                 touch $dir/dir/file$i
5400         done
5401
5402         local numcomp=$($LFS getstripe --component-count $dir)
5403
5404         [[ $numcomp == 0 ]] && numcomp=1
5405
5406         # test lfs getstripe with --recursive
5407         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
5408
5409         [[ $filenum -eq $((numfiles * 2)) ]] ||
5410                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
5411         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
5412         [[ $filenum -eq $numfiles ]] ||
5413                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
5414         echo "$LFS getstripe showed obdidx or l_ost_idx"
5415
5416         # test lfs getstripe with file instead of dir
5417         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
5418         [[ $filenum -eq 1 ]] ||
5419                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
5420         echo "$LFS getstripe file1 passed"
5421
5422         #test lfs getstripe with --verbose
5423         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
5424         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5425                 error "$LFS getstripe --verbose $dir: "\
5426                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
5427         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
5428                 error "$LFS getstripe $dir: showed lmm_magic"
5429
5430         #test lfs getstripe with -v prints lmm_fid
5431         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
5432         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5433                 error "$LFS getstripe -v $dir: "\
5434                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
5435         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
5436                 error "$LFS getstripe $dir: showed lmm_fid by default"
5437         echo "$LFS getstripe --verbose passed"
5438
5439         #check for FID information
5440         local fid1=$($LFS getstripe --fid $dir/file1)
5441         local fid2=$($LFS getstripe --verbose $dir/file1 |
5442                      awk '/lmm_fid: / { print $2; exit; }')
5443         local fid3=$($LFS path2fid $dir/file1)
5444
5445         [ "$fid1" != "$fid2" ] &&
5446                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
5447         [ "$fid1" != "$fid3" ] &&
5448                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
5449         echo "$LFS getstripe --fid passed"
5450
5451         #test lfs getstripe with --obd
5452         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
5453                 error "$LFS getstripe --obd wrong_uuid: should return error"
5454
5455         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5456
5457         local ostidx=1
5458         local obduuid=$(ostuuid_from_index $ostidx)
5459         local found=$($LFS getstripe -r --obd $obduuid $dir |
5460                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
5461
5462         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
5463         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
5464                 ((filenum--))
5465         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
5466                 ((filenum--))
5467
5468         [[ $found -eq $filenum ]] ||
5469                 error "$LFS getstripe --obd: found $found expect $filenum"
5470         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
5471                 sed '/^[         ]*'${ostidx}'[  ]/d' |
5472                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
5473                 error "$LFS getstripe --obd: should not show file on other obd"
5474         echo "$LFS getstripe --obd passed"
5475 }
5476 run_test 56a "check $LFS getstripe"
5477
5478 test_56b() {
5479         local dir=$DIR/$tdir
5480         local numdirs=3
5481
5482         test_mkdir $dir
5483         for i in $(seq $numdirs); do
5484                 test_mkdir $dir/dir$i
5485         done
5486
5487         # test lfs getdirstripe default mode is non-recursion, which is
5488         # different from lfs getstripe
5489         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
5490
5491         [[ $dircnt -eq 1 ]] ||
5492                 error "$LFS getdirstripe: found $dircnt, not 1"
5493         dircnt=$($LFS getdirstripe --recursive $dir |
5494                 grep -c lmv_stripe_count)
5495         [[ $dircnt -eq $((numdirs + 1)) ]] ||
5496                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
5497 }
5498 run_test 56b "check $LFS getdirstripe"
5499
5500 test_56c() {
5501         remote_ost_nodsh && skip "remote OST with nodsh"
5502
5503         local ost_idx=0
5504         local ost_name=$(ostname_from_index $ost_idx)
5505         local old_status=$(ost_dev_status $ost_idx)
5506
5507         [[ -z "$old_status" ]] ||
5508                 skip_env "OST $ost_name is in $old_status status"
5509
5510         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
5511         [[ $OST1_VERSION -ge $(version_code 2.12.55) ]] && do_facet ost1 \
5512                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
5513         sleep_maxage
5514
5515         local new_status=$(ost_dev_status $ost_idx)
5516
5517         [[ "$new_status" =~ "D" ]] ||
5518                 error "$ost_name status is '$new_status', missing 'D'"
5519         if [[ $OST1_VERSION -ge $(version_code 2.12.55) ]]; then
5520                 [[ "$new_status" =~ "N" ]] ||
5521                         error "$ost_name status is '$new_status', missing 'N'"
5522         fi
5523
5524         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
5525         [[ $OST1_VERSION -ge $(version_code 2.12.55) ]] && do_facet ost1 \
5526                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
5527         sleep_maxage
5528
5529         new_status=$(ost_dev_status $ost_idx)
5530         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
5531                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
5532 }
5533 run_test 56c "check 'lfs df' showing device status"
5534
5535 NUMFILES=3
5536 NUMDIRS=3
5537 setup_56() {
5538         local local_tdir="$1"
5539         local local_numfiles="$2"
5540         local local_numdirs="$3"
5541         local dir_params="$4"
5542         local dir_stripe_params="$5"
5543
5544         if [ ! -d "$local_tdir" ] ; then
5545                 test_mkdir -p $dir_stripe_params $local_tdir
5546                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
5547                 for i in $(seq $local_numfiles) ; do
5548                         touch $local_tdir/file$i
5549                 done
5550                 for i in $(seq $local_numdirs) ; do
5551                         test_mkdir $dir_stripe_params $local_tdir/dir$i
5552                         for j in $(seq $local_numfiles) ; do
5553                                 touch $local_tdir/dir$i/file$j
5554                         done
5555                 done
5556         fi
5557 }
5558
5559 setup_56_special() {
5560         local local_tdir=$1
5561         local local_numfiles=$2
5562         local local_numdirs=$3
5563
5564         setup_56 $local_tdir $local_numfiles $local_numdirs
5565
5566         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
5567                 for i in $(seq $local_numfiles) ; do
5568                         mknod $local_tdir/loop${i}b b 7 $i
5569                         mknod $local_tdir/null${i}c c 1 3
5570                         ln -s $local_tdir/file1 $local_tdir/link${i}
5571                 done
5572                 for i in $(seq $local_numdirs) ; do
5573                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
5574                         mknod $local_tdir/dir$i/null${i}c c 1 3
5575                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
5576                 done
5577         fi
5578 }
5579
5580 test_56g() {
5581         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5582         local expected=$(($NUMDIRS + 2))
5583
5584         setup_56 $dir $NUMFILES $NUMDIRS
5585
5586         # test lfs find with -name
5587         for i in $(seq $NUMFILES) ; do
5588                 local nums=$($LFS find -name "*$i" $dir | wc -l)
5589
5590                 [ $nums -eq $expected ] ||
5591                         error "lfs find -name '*$i' $dir wrong: "\
5592                               "found $nums, expected $expected"
5593         done
5594 }
5595 run_test 56g "check lfs find -name"
5596
5597 test_56h() {
5598         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5599         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
5600
5601         setup_56 $dir $NUMFILES $NUMDIRS
5602
5603         # test lfs find with ! -name
5604         for i in $(seq $NUMFILES) ; do
5605                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
5606
5607                 [ $nums -eq $expected ] ||
5608                         error "lfs find ! -name '*$i' $dir wrong: "\
5609                               "found $nums, expected $expected"
5610         done
5611 }
5612 run_test 56h "check lfs find ! -name"
5613
5614 test_56i() {
5615         local dir=$DIR/$tdir
5616
5617         test_mkdir $dir
5618
5619         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
5620         local out=$($cmd)
5621
5622         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
5623 }
5624 run_test 56i "check 'lfs find -ost UUID' skips directories"
5625
5626 test_56j() {
5627         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5628
5629         setup_56_special $dir $NUMFILES $NUMDIRS
5630
5631         local expected=$((NUMDIRS + 1))
5632         local cmd="$LFS find -type d $dir"
5633         local nums=$($cmd | wc -l)
5634
5635         [ $nums -eq $expected ] ||
5636                 error "'$cmd' wrong: found $nums, expected $expected"
5637 }
5638 run_test 56j "check lfs find -type d"
5639
5640 test_56k() {
5641         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5642
5643         setup_56_special $dir $NUMFILES $NUMDIRS
5644
5645         local expected=$(((NUMDIRS + 1) * NUMFILES))
5646         local cmd="$LFS find -type f $dir"
5647         local nums=$($cmd | wc -l)
5648
5649         [ $nums -eq $expected ] ||
5650                 error "'$cmd' wrong: found $nums, expected $expected"
5651 }
5652 run_test 56k "check lfs find -type f"
5653
5654 test_56l() {
5655         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5656
5657         setup_56_special $dir $NUMFILES $NUMDIRS
5658
5659         local expected=$((NUMDIRS + NUMFILES))
5660         local cmd="$LFS find -type b $dir"
5661         local nums=$($cmd | wc -l)
5662
5663         [ $nums -eq $expected ] ||
5664                 error "'$cmd' wrong: found $nums, expected $expected"
5665 }
5666 run_test 56l "check lfs find -type b"
5667
5668 test_56m() {
5669         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5670
5671         setup_56_special $dir $NUMFILES $NUMDIRS
5672
5673         local expected=$((NUMDIRS + NUMFILES))
5674         local cmd="$LFS find -type c $dir"
5675         local nums=$($cmd | wc -l)
5676         [ $nums -eq $expected ] ||
5677                 error "'$cmd' wrong: found $nums, expected $expected"
5678 }
5679 run_test 56m "check lfs find -type c"
5680
5681 test_56n() {
5682         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5683         setup_56_special $dir $NUMFILES $NUMDIRS
5684
5685         local expected=$((NUMDIRS + NUMFILES))
5686         local cmd="$LFS find -type l $dir"
5687         local nums=$($cmd | wc -l)
5688
5689         [ $nums -eq $expected ] ||
5690                 error "'$cmd' wrong: found $nums, expected $expected"
5691 }
5692 run_test 56n "check lfs find -type l"
5693
5694 test_56o() {
5695         local dir=$DIR/$tdir
5696
5697         setup_56 $dir $NUMFILES $NUMDIRS
5698         utime $dir/file1 > /dev/null || error "utime (1)"
5699         utime $dir/file2 > /dev/null || error "utime (2)"
5700         utime $dir/dir1 > /dev/null || error "utime (3)"
5701         utime $dir/dir2 > /dev/null || error "utime (4)"
5702         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5703         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5704
5705         local expected=4
5706         local nums=$($LFS find -mtime +0 $dir | wc -l)
5707
5708         [ $nums -eq $expected ] ||
5709                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5710
5711         expected=12
5712         cmd="$LFS find -mtime 0 $dir"
5713         nums=$($cmd | wc -l)
5714         [ $nums -eq $expected ] ||
5715                 error "'$cmd' wrong: found $nums, expected $expected"
5716 }
5717 run_test 56o "check lfs find -mtime for old files"
5718
5719 test_56ob() {
5720         local dir=$DIR/$tdir
5721         local expected=1
5722         local count=0
5723
5724         # just to make sure there is something that won't be found
5725         test_mkdir $dir
5726         touch $dir/$tfile.now
5727
5728         for age in year week day hour min; do
5729                 count=$((count + 1))
5730
5731                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
5732                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
5733                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
5734
5735                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
5736                 local nums=$($cmd | wc -l)
5737                 [ $nums -eq $expected ] ||
5738                         error "'$cmd' wrong: found $nums, expected $expected"
5739
5740                 cmd="$LFS find $dir -atime $count${age:0:1}"
5741                 nums=$($cmd | wc -l)
5742                 [ $nums -eq $expected ] ||
5743                         error "'$cmd' wrong: found $nums, expected $expected"
5744         done
5745
5746         sleep 2
5747         cmd="$LFS find $dir -ctime +1s -type f"
5748         nums=$($cmd | wc -l)
5749         (( $nums == $count * 2 + 1)) ||
5750                 error "'$cmd' wrong: found $nums, expected $((expected*2+1))"
5751 }
5752 run_test 56ob "check lfs find -atime -mtime -ctime with units"
5753
5754 test_newerXY_base() {
5755         local x=$1
5756         local y=$2
5757         local dir=$DIR/$tdir
5758         local ref
5759         local negref
5760
5761         if [ $y == "t" ]; then
5762                 ref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5763         else
5764                 ref=$DIR/$tfile.newer
5765                 touch $ref || error "touch $ref failed"
5766         fi
5767         sleep 2
5768         setup_56 $dir $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
5769         sleep 2
5770         if [ $y == "t" ]; then
5771                 negref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5772         else
5773                 negref=$DIR/$tfile.newerneg
5774                 touch $negref || error "touch $negref failed"
5775         fi
5776
5777         local cmd="$LFS find $dir -newer$x$y $ref"
5778         local nums=$(eval $cmd | wc -l)
5779         local expected=$(((NUMFILES + 2) * NUMDIRS + 1))
5780
5781         [ $nums -eq $expected ] ||
5782                 error "'$cmd' wrong: found $nums, expected $expected"
5783
5784         cmd="$LFS find $dir ! -newer$x$y $negref"
5785         nums=$(eval $cmd | wc -l)
5786         [ $nums -eq $expected ] ||
5787                 error "'$cmd' wrong: found $nums, expected $expected"
5788
5789         cmd="$LFS find $dir -newer$x$y $ref ! -newer$x$y $negref"
5790         nums=$(eval $cmd | wc -l)
5791         [ $nums -eq $expected ] ||
5792                 error "'$cmd' wrong: found $nums, expected $expected"
5793
5794         rm -rf $DIR/*
5795 }
5796
5797 test_56oc() {
5798         test_newerXY_base "a" "a"
5799         test_newerXY_base "a" "m"
5800         test_newerXY_base "a" "c"
5801         test_newerXY_base "m" "a"
5802         test_newerXY_base "m" "m"
5803         test_newerXY_base "m" "c"
5804         test_newerXY_base "c" "a"
5805         test_newerXY_base "c" "m"
5806         test_newerXY_base "c" "c"
5807         test_newerXY_base "a" "t"
5808         test_newerXY_base "m" "t"
5809         test_newerXY_base "c" "t"
5810 }
5811 run_test 56oc "check lfs find -newerXY work"
5812
5813 test_56p() {
5814         [ $RUNAS_ID -eq $UID ] &&
5815                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5816
5817         local dir=$DIR/$tdir
5818
5819         setup_56 $dir $NUMFILES $NUMDIRS
5820         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
5821
5822         local expected=$NUMFILES
5823         local cmd="$LFS find -uid $RUNAS_ID $dir"
5824         local nums=$($cmd | wc -l)
5825
5826         [ $nums -eq $expected ] ||
5827                 error "'$cmd' wrong: found $nums, expected $expected"
5828
5829         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
5830         cmd="$LFS find ! -uid $RUNAS_ID $dir"
5831         nums=$($cmd | wc -l)
5832         [ $nums -eq $expected ] ||
5833                 error "'$cmd' wrong: found $nums, expected $expected"
5834 }
5835 run_test 56p "check lfs find -uid and ! -uid"
5836
5837 test_56q() {
5838         [ $RUNAS_ID -eq $UID ] &&
5839                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5840
5841         local dir=$DIR/$tdir
5842
5843         setup_56 $dir $NUMFILES $NUMDIRS
5844         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
5845
5846         local expected=$NUMFILES
5847         local cmd="$LFS find -gid $RUNAS_GID $dir"
5848         local nums=$($cmd | wc -l)
5849
5850         [ $nums -eq $expected ] ||
5851                 error "'$cmd' wrong: found $nums, expected $expected"
5852
5853         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
5854         cmd="$LFS find ! -gid $RUNAS_GID $dir"
5855         nums=$($cmd | wc -l)
5856         [ $nums -eq $expected ] ||
5857                 error "'$cmd' wrong: found $nums, expected $expected"
5858 }
5859 run_test 56q "check lfs find -gid and ! -gid"
5860
5861 test_56r() {
5862         local dir=$DIR/$tdir
5863
5864         setup_56 $dir $NUMFILES $NUMDIRS
5865
5866         local expected=12
5867         local cmd="$LFS find -size 0 -type f -lazy $dir"
5868         local nums=$($cmd | wc -l)
5869
5870         [ $nums -eq $expected ] ||
5871                 error "'$cmd' wrong: found $nums, expected $expected"
5872         cmd="$LFS find -size 0 -type f $dir"
5873         nums=$($cmd | wc -l)
5874         [ $nums -eq $expected ] ||
5875                 error "'$cmd' wrong: found $nums, expected $expected"
5876
5877         expected=0
5878         cmd="$LFS find ! -size 0 -type f -lazy $dir"
5879         nums=$($cmd | wc -l)
5880         [ $nums -eq $expected ] ||
5881                 error "'$cmd' wrong: found $nums, expected $expected"
5882         cmd="$LFS find ! -size 0 -type f $dir"
5883         nums=$($cmd | wc -l)
5884         [ $nums -eq $expected ] ||
5885                 error "'$cmd' wrong: found $nums, expected $expected"
5886
5887         echo "test" > $dir/$tfile
5888         echo "test2" > $dir/$tfile.2 && sync
5889         expected=1
5890         cmd="$LFS find -size 5 -type f -lazy $dir"
5891         nums=$($cmd | wc -l)
5892         [ $nums -eq $expected ] ||
5893                 error "'$cmd' wrong: found $nums, expected $expected"
5894         cmd="$LFS find -size 5 -type f $dir"
5895         nums=$($cmd | wc -l)
5896         [ $nums -eq $expected ] ||
5897                 error "'$cmd' wrong: found $nums, expected $expected"
5898
5899         expected=1
5900         cmd="$LFS find -size +5 -type f -lazy $dir"
5901         nums=$($cmd | wc -l)
5902         [ $nums -eq $expected ] ||
5903                 error "'$cmd' wrong: found $nums, expected $expected"
5904         cmd="$LFS find -size +5 -type f $dir"
5905         nums=$($cmd | wc -l)
5906         [ $nums -eq $expected ] ||
5907                 error "'$cmd' wrong: found $nums, expected $expected"
5908
5909         expected=2
5910         cmd="$LFS find -size +0 -type f -lazy $dir"
5911         nums=$($cmd | wc -l)
5912         [ $nums -eq $expected ] ||
5913                 error "'$cmd' wrong: found $nums, expected $expected"
5914         cmd="$LFS find -size +0 -type f $dir"
5915         nums=$($cmd | wc -l)
5916         [ $nums -eq $expected ] ||
5917                 error "'$cmd' wrong: found $nums, expected $expected"
5918
5919         expected=2
5920         cmd="$LFS find ! -size -5 -type f -lazy $dir"
5921         nums=$($cmd | wc -l)
5922         [ $nums -eq $expected ] ||
5923                 error "'$cmd' wrong: found $nums, expected $expected"
5924         cmd="$LFS find ! -size -5 -type f $dir"
5925         nums=$($cmd | wc -l)
5926         [ $nums -eq $expected ] ||
5927                 error "'$cmd' wrong: found $nums, expected $expected"
5928
5929         expected=12
5930         cmd="$LFS find -size -5 -type f -lazy $dir"
5931         nums=$($cmd | wc -l)
5932         [ $nums -eq $expected ] ||
5933                 error "'$cmd' wrong: found $nums, expected $expected"
5934         cmd="$LFS find -size -5 -type f $dir"
5935         nums=$($cmd | wc -l)
5936         [ $nums -eq $expected ] ||
5937                 error "'$cmd' wrong: found $nums, expected $expected"
5938 }
5939 run_test 56r "check lfs find -size works"
5940
5941 test_56ra() {
5942         [[ $MDS1_VERSION -gt $(version_code 2.12.58) ]] ||
5943                 skip "MDS < 2.12.58 doesn't return LSOM data"
5944         local dir=$DIR/$tdir
5945
5946         [[ $OSC == "mdc" ]] && skip "DoM files" && return
5947
5948         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
5949
5950         cancel_lru_locks $OSC
5951
5952         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5953         local expected=12
5954         local cmd="$LFS find -size 0 -type f -lazy $dir"
5955         local nums=$($cmd | wc -l)
5956
5957         [ $nums -eq $expected ] ||
5958                 error "'$cmd' wrong: found $nums, expected $expected"
5959
5960         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5961         [ $rpcs_before -eq $rpcs_after ] ||
5962                 error "'$cmd' should not send glimpse RPCs to OST"
5963         cmd="$LFS find -size 0 -type f $dir"
5964         nums=$($cmd | wc -l)
5965         [ $nums -eq $expected ] ||
5966                 error "'$cmd' wrong: found $nums, expected $expected"
5967         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5968         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
5969         $LCTL get_param osc.*.stats
5970         [ $rpcs_after -eq $((rpcs_before + 12)) ] ||
5971                 error "'$cmd' should send 12 glimpse RPCs to OST"
5972
5973         cancel_lru_locks $OSC
5974         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5975         expected=0
5976         cmd="$LFS find ! -size 0 -type f -lazy $dir"
5977         nums=$($cmd | wc -l)
5978         [ $nums -eq $expected ] ||
5979                 error "'$cmd' wrong: found $nums, expected $expected"
5980         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5981         $LCTL get_param mdc.*.stats
5982         [ $rpcs_before -eq $rpcs_after ] ||
5983                 error "'$cmd' should not send glimpse RPCs to OST"
5984         cmd="$LFS find ! -size 0 -type f $dir"
5985         nums=$($cmd | wc -l)
5986         [ $nums -eq $expected ] ||
5987                 error "'$cmd' wrong: found $nums, expected $expected"
5988         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5989         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
5990         [ $rpcs_after -eq $((rpcs_before + 12)) ] ||
5991                 error "'$cmd' should send 12 glimpse RPCs to OST"
5992
5993         echo "test" > $dir/$tfile
5994         echo "test2" > $dir/$tfile.2 && sync
5995         cancel_lru_locks $OSC
5996         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5997         expected=1
5998         cmd="$LFS find -size 5 -type f -lazy $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         [ $rpcs_before -eq $rpcs_after ] ||
6004                 error "'$cmd' should not send glimpse RPCs to OST"
6005         cmd="$LFS find -size 5 -type f $dir"
6006         nums=$($cmd | wc -l)
6007         [ $nums -eq $expected ] ||
6008                 error "'$cmd' wrong: found $nums, expected $expected"
6009         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6010         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6011         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6012                 error "'$cmd' should send 14 glimpse RPCs to OST"
6013
6014         cancel_lru_locks $OSC
6015         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6016         expected=1
6017         cmd="$LFS find -size +5 -type f -lazy $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         [ $rpcs_before -eq $rpcs_after ] ||
6023                 error "'$cmd' should not send glimpse RPCs to OST"
6024         cmd="$LFS find -size +5 -type f $dir"
6025         nums=$($cmd | wc -l)
6026         [ $nums -eq $expected ] ||
6027                 error "'$cmd' wrong: found $nums, expected $expected"
6028         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6029         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6030         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6031                 error "'$cmd' should send 14 glimpse RPCs to OST"
6032
6033         cancel_lru_locks $OSC
6034         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6035         expected=2
6036         cmd="$LFS find -size +0 -type f -lazy $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         [ $rpcs_before -eq $rpcs_after ] ||
6042                 error "'$cmd' should not send glimpse RPCs to OST"
6043         cmd="$LFS find -size +0 -type f $dir"
6044         nums=$($cmd | wc -l)
6045         [ $nums -eq $expected ] ||
6046                 error "'$cmd' wrong: found $nums, expected $expected"
6047         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6048         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6049         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6050                 error "'$cmd' should send 14 glimpse RPCs to OST"
6051
6052         cancel_lru_locks $OSC
6053         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6054         expected=2
6055         cmd="$LFS find ! -size -5 -type f -lazy $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         [ $rpcs_before -eq $rpcs_after ] ||
6061                 error "'$cmd' should not send glimpse RPCs to OST"
6062         cmd="$LFS find ! -size -5 -type f $dir"
6063         nums=$($cmd | wc -l)
6064         [ $nums -eq $expected ] ||
6065                 error "'$cmd' wrong: found $nums, expected $expected"
6066         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6067         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6068         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6069                 error "'$cmd' should send 14 glimpse RPCs to OST"
6070
6071         cancel_lru_locks $OSC
6072         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6073         expected=12
6074         cmd="$LFS find -size -5 -type f -lazy $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         [ $rpcs_before -eq $rpcs_after ] ||
6080                 error "'$cmd' should not send glimpse RPCs to OST"
6081         cmd="$LFS find -size -5 -type f $dir"
6082         nums=$($cmd | wc -l)
6083         [ $nums -eq $expected ] ||
6084                 error "'$cmd' wrong: found $nums, expected $expected"
6085         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6086         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6087         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6088                 error "'$cmd' should send 14 glimpse RPCs to OST"
6089 }
6090 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
6091
6092 test_56s() { # LU-611 #LU-9369
6093         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
6094
6095         local dir=$DIR/$tdir
6096         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
6097
6098         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6099         for i in $(seq $NUMDIRS); do
6100                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
6101         done
6102
6103         local expected=$NUMDIRS
6104         local cmd="$LFS find -c $OSTCOUNT $dir"
6105         local nums=$($cmd | wc -l)
6106
6107         [ $nums -eq $expected ] || {
6108                 $LFS getstripe -R $dir
6109                 error "'$cmd' wrong: found $nums, expected $expected"
6110         }
6111
6112         expected=$((NUMDIRS + onestripe))
6113         cmd="$LFS find -stripe-count +0 -type f $dir"
6114         nums=$($cmd | wc -l)
6115         [ $nums -eq $expected ] || {
6116                 $LFS getstripe -R $dir
6117                 error "'$cmd' wrong: found $nums, expected $expected"
6118         }
6119
6120         expected=$onestripe
6121         cmd="$LFS find -stripe-count 1 -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         cmd="$LFS find -stripe-count -2 -type f $dir"
6129         nums=$($cmd | wc -l)
6130         [ $nums -eq $expected ] || {
6131                 $LFS getstripe -R $dir
6132                 error "'$cmd' wrong: found $nums, expected $expected"
6133         }
6134
6135         expected=0
6136         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
6137         nums=$($cmd | wc -l)
6138         [ $nums -eq $expected ] || {
6139                 $LFS getstripe -R $dir
6140                 error "'$cmd' wrong: found $nums, expected $expected"
6141         }
6142 }
6143 run_test 56s "check lfs find -stripe-count works"
6144
6145 test_56t() { # LU-611 #LU-9369
6146         local dir=$DIR/$tdir
6147
6148         setup_56 $dir 0 $NUMDIRS
6149         for i in $(seq $NUMDIRS); do
6150                 $LFS setstripe -S 8M $dir/dir$i/$tfile
6151         done
6152
6153         local expected=$NUMDIRS
6154         local cmd="$LFS find -S 8M $dir"
6155         local nums=$($cmd | wc -l)
6156
6157         [ $nums -eq $expected ] || {
6158                 $LFS getstripe -R $dir
6159                 error "'$cmd' wrong: found $nums, expected $expected"
6160         }
6161         rm -rf $dir
6162
6163         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
6164
6165         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
6166
6167         expected=$(((NUMDIRS + 1) * NUMFILES))
6168         cmd="$LFS find -stripe-size 512k -type f $dir"
6169         nums=$($cmd | wc -l)
6170         [ $nums -eq $expected ] ||
6171                 error "'$cmd' wrong: found $nums, expected $expected"
6172
6173         cmd="$LFS find -stripe-size +320k -type f $dir"
6174         nums=$($cmd | wc -l)
6175         [ $nums -eq $expected ] ||
6176                 error "'$cmd' wrong: found $nums, expected $expected"
6177
6178         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
6179         cmd="$LFS find -stripe-size +200k -type f $dir"
6180         nums=$($cmd | wc -l)
6181         [ $nums -eq $expected ] ||
6182                 error "'$cmd' wrong: found $nums, expected $expected"
6183
6184         cmd="$LFS find -stripe-size -640k -type f $dir"
6185         nums=$($cmd | wc -l)
6186         [ $nums -eq $expected ] ||
6187                 error "'$cmd' wrong: found $nums, expected $expected"
6188
6189         expected=4
6190         cmd="$LFS find -stripe-size 256k -type f $dir"
6191         nums=$($cmd | wc -l)
6192         [ $nums -eq $expected ] ||
6193                 error "'$cmd' wrong: found $nums, expected $expected"
6194
6195         cmd="$LFS find -stripe-size -320k -type f $dir"
6196         nums=$($cmd | wc -l)
6197         [ $nums -eq $expected ] ||
6198                 error "'$cmd' wrong: found $nums, expected $expected"
6199
6200         expected=0
6201         cmd="$LFS find -stripe-size 1024k -type f $dir"
6202         nums=$($cmd | wc -l)
6203         [ $nums -eq $expected ] ||
6204                 error "'$cmd' wrong: found $nums, expected $expected"
6205 }
6206 run_test 56t "check lfs find -stripe-size works"
6207
6208 test_56u() { # LU-611
6209         local dir=$DIR/$tdir
6210
6211         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
6212
6213         if [[ $OSTCOUNT -gt 1 ]]; then
6214                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
6215                 onestripe=4
6216         else
6217                 onestripe=0
6218         fi
6219
6220         local expected=$(((NUMDIRS + 1) * NUMFILES))
6221         local cmd="$LFS find -stripe-index 0 -type f $dir"
6222         local nums=$($cmd | wc -l)
6223
6224         [ $nums -eq $expected ] ||
6225                 error "'$cmd' wrong: found $nums, expected $expected"
6226
6227         expected=$onestripe
6228         cmd="$LFS find -stripe-index 1 -type f $dir"
6229         nums=$($cmd | wc -l)
6230         [ $nums -eq $expected ] ||
6231                 error "'$cmd' wrong: found $nums, expected $expected"
6232
6233         cmd="$LFS find ! -stripe-index 0 -type f $dir"
6234         nums=$($cmd | wc -l)
6235         [ $nums -eq $expected ] ||
6236                 error "'$cmd' wrong: found $nums, expected $expected"
6237
6238         expected=0
6239         # This should produce an error and not return any files
6240         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
6241         nums=$($cmd 2>/dev/null | wc -l)
6242         [ $nums -eq $expected ] ||
6243                 error "'$cmd' wrong: found $nums, expected $expected"
6244
6245         if [[ $OSTCOUNT -gt 1 ]]; then
6246                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
6247                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
6248                 nums=$($cmd | wc -l)
6249                 [ $nums -eq $expected ] ||
6250                         error "'$cmd' wrong: found $nums, expected $expected"
6251         fi
6252 }
6253 run_test 56u "check lfs find -stripe-index works"
6254
6255 test_56v() {
6256         local mdt_idx=0
6257         local dir=$DIR/$tdir
6258
6259         setup_56 $dir $NUMFILES $NUMDIRS
6260
6261         UUID=$(mdtuuid_from_index $mdt_idx $dir)
6262         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
6263
6264         for file in $($LFS find -m $UUID $dir); do
6265                 file_midx=$($LFS getstripe -m $file)
6266                 [ $file_midx -eq $mdt_idx ] ||
6267                         error "lfs find -m $UUID != getstripe -m $file_midx"
6268         done
6269 }
6270 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
6271
6272 test_56w() {
6273         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6274         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6275
6276         local dir=$DIR/$tdir
6277
6278         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
6279
6280         local stripe_size=$($LFS getstripe -S -d $dir) ||
6281                 error "$LFS getstripe -S -d $dir failed"
6282         stripe_size=${stripe_size%% *}
6283
6284         local file_size=$((stripe_size * OSTCOUNT))
6285         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
6286         local required_space=$((file_num * file_size))
6287         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
6288                            head -n1)
6289         [[ $free_space -le $((required_space / 1024)) ]] &&
6290                 skip_env "need $required_space, have $free_space kbytes"
6291
6292         local dd_bs=65536
6293         local dd_count=$((file_size / dd_bs))
6294
6295         # write data into the files
6296         local i
6297         local j
6298         local file
6299
6300         for i in $(seq $NUMFILES); do
6301                 file=$dir/file$i
6302                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6303                         error "write data into $file failed"
6304         done
6305         for i in $(seq $NUMDIRS); do
6306                 for j in $(seq $NUMFILES); do
6307                         file=$dir/dir$i/file$j
6308                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6309                                 error "write data into $file failed"
6310                 done
6311         done
6312
6313         # $LFS_MIGRATE will fail if hard link migration is unsupported
6314         if [[ $MDS1_VERSION -gt $(version_code 2.5.55) ]]; then
6315                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
6316                         error "creating links to $dir/dir1/file1 failed"
6317         fi
6318
6319         local expected=-1
6320
6321         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
6322
6323         # lfs_migrate file
6324         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
6325
6326         echo "$cmd"
6327         eval $cmd || error "$cmd failed"
6328
6329         check_stripe_count $dir/file1 $expected
6330
6331         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
6332         then
6333                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
6334                 # OST 1 if it is on OST 0. This file is small enough to
6335                 # be on only one stripe.
6336                 file=$dir/migr_1_ost
6337                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
6338                         error "write data into $file failed"
6339                 local obdidx=$($LFS getstripe -i $file)
6340                 local oldmd5=$(md5sum $file)
6341                 local newobdidx=0
6342
6343                 [[ $obdidx -eq 0 ]] && newobdidx=1
6344                 cmd="$LFS migrate -i $newobdidx $file"
6345                 echo $cmd
6346                 eval $cmd || error "$cmd failed"
6347
6348                 local realobdix=$($LFS getstripe -i $file)
6349                 local newmd5=$(md5sum $file)
6350
6351                 [[ $newobdidx -ne $realobdix ]] &&
6352                         error "new OST is different (was=$obdidx, "\
6353                               "wanted=$newobdidx, got=$realobdix)"
6354                 [[ "$oldmd5" != "$newmd5" ]] &&
6355                         error "md5sum differ: $oldmd5, $newmd5"
6356         fi
6357
6358         # lfs_migrate dir
6359         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
6360         echo "$cmd"
6361         eval $cmd || error "$cmd failed"
6362
6363         for j in $(seq $NUMFILES); do
6364                 check_stripe_count $dir/dir1/file$j $expected
6365         done
6366
6367         # lfs_migrate works with lfs find
6368         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
6369              $LFS_MIGRATE -y -c $expected"
6370         echo "$cmd"
6371         eval $cmd || error "$cmd failed"
6372
6373         for i in $(seq 2 $NUMFILES); do
6374                 check_stripe_count $dir/file$i $expected
6375         done
6376         for i in $(seq 2 $NUMDIRS); do
6377                 for j in $(seq $NUMFILES); do
6378                 check_stripe_count $dir/dir$i/file$j $expected
6379                 done
6380         done
6381 }
6382 run_test 56w "check lfs_migrate -c stripe_count works"
6383
6384 test_56wb() {
6385         local file1=$DIR/$tdir/file1
6386         local create_pool=false
6387         local initial_pool=$($LFS getstripe -p $DIR)
6388         local pool_list=()
6389         local pool=""
6390
6391         echo -n "Creating test dir..."
6392         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6393         echo "done."
6394
6395         echo -n "Creating test file..."
6396         touch $file1 || error "cannot create file"
6397         echo "done."
6398
6399         echo -n "Detecting existing pools..."
6400         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
6401
6402         if [ ${#pool_list[@]} -gt 0 ]; then
6403                 echo "${pool_list[@]}"
6404                 for thispool in "${pool_list[@]}"; do
6405                         if [[ -z "$initial_pool" ||
6406                               "$initial_pool" != "$thispool" ]]; then
6407                                 pool="$thispool"
6408                                 echo "Using existing pool '$pool'"
6409                                 break
6410                         fi
6411                 done
6412         else
6413                 echo "none detected."
6414         fi
6415         if [ -z "$pool" ]; then
6416                 pool=${POOL:-testpool}
6417                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
6418                 echo -n "Creating pool '$pool'..."
6419                 create_pool=true
6420                 pool_add $pool &> /dev/null ||
6421                         error "pool_add failed"
6422                 echo "done."
6423
6424                 echo -n "Adding target to pool..."
6425                 pool_add_targets $pool 0 0 1 &> /dev/null ||
6426                         error "pool_add_targets failed"
6427                 echo "done."
6428         fi
6429
6430         echo -n "Setting pool using -p option..."
6431         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
6432                 error "migrate failed rc = $?"
6433         echo "done."
6434
6435         echo -n "Verifying test file is in pool after migrating..."
6436         [ "$($LFS getstripe -p $file1)" = $pool ] ||
6437                 error "file was not migrated to pool $pool"
6438         echo "done."
6439
6440         echo -n "Removing test file from pool '$pool'..."
6441         $LFS migrate $file1 &> /dev/null ||
6442                 error "cannot remove from pool"
6443         [ "$($LFS getstripe -p $file1)" ] &&
6444                 error "pool still set"
6445         echo "done."
6446
6447         echo -n "Setting pool using --pool option..."
6448         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
6449                 error "migrate failed rc = $?"
6450         echo "done."
6451
6452         # Clean up
6453         rm -f $file1
6454         if $create_pool; then
6455                 destroy_test_pools 2> /dev/null ||
6456                         error "destroy test pools failed"
6457         fi
6458 }
6459 run_test 56wb "check lfs_migrate pool support"
6460
6461 test_56wc() {
6462         local file1="$DIR/$tdir/file1"
6463
6464         echo -n "Creating test dir..."
6465         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6466         local def_stripe_size=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
6467         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
6468                 error "cannot set stripe"
6469         echo "done"
6470
6471         echo -n "Setting initial stripe for test file..."
6472         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
6473                 error "cannot set stripe"
6474         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
6475                 error "stripe size not set"
6476         echo "done."
6477
6478         # File currently set to -S 512K -c 1
6479
6480         # Ensure -c and -S options are rejected when -R is set
6481         echo -n "Verifying incompatible options are detected..."
6482         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
6483                 error "incompatible -c and -R options not detected"
6484         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
6485                 error "incompatible -S and -R options not detected"
6486         echo "done."
6487
6488         # Ensure unrecognized options are passed through to 'lfs migrate'
6489         echo -n "Verifying -S option is passed through to lfs migrate..."
6490         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
6491                 error "migration failed"
6492         [ $($LFS getstripe -S "$file1") -eq 1048576 ] ||
6493                 error "file was not restriped"
6494         echo "done."
6495
6496         # File currently set to -S 1M -c 1
6497
6498         # Ensure long options are supported
6499         echo -n "Verifying long options supported..."
6500         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
6501                 error "long option without argument not supported"
6502         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
6503                 error "long option with argument not supported"
6504         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
6505                 error "file not restriped with --stripe-size option"
6506         echo "done."
6507
6508         # File currently set to -S 512K -c 1
6509
6510         if [ "$OSTCOUNT" -gt 1 ]; then
6511                 echo -n "Verifying explicit stripe count can be set..."
6512                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
6513                         error "migrate failed"
6514                 [ $($LFS getstripe -c "$file1") -eq 2 ] ||
6515                         error "file not restriped to explicit count"
6516                 echo "done."
6517         fi
6518
6519         # File currently set to -S 512K -c 1 or -S 512K -c 2
6520
6521         # Ensure parent striping is used if -R is set, and no stripe
6522         # count or size is specified
6523         echo -n "Setting stripe for parent directory..."
6524         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
6525                 error "cannot set stripe"
6526         echo "done."
6527
6528         echo -n "Verifying restripe option uses parent stripe settings..."
6529         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
6530                 error "migrate failed"
6531         [ $($LFS getstripe -S "$file1") -eq $def_stripe_size ] ||
6532                 error "file not restriped to parent settings"
6533         [ $($LFS getstripe -c "$file1") -eq 1 ] ||
6534                 error "file not restriped to parent settings"
6535         echo "done."
6536
6537         # File currently set to -S 1M -c 1
6538
6539         # Ensure striping is preserved if -R is not set, and no stripe
6540         # count or size is specified
6541         echo -n "Verifying striping size preserved when not specified..."
6542         local orig_stripe_size=$($LFS getstripe -S "$file1" 2>/dev/null)
6543         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
6544                 error "cannot set stripe on parent directory"
6545         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6546                 error "migrate failed"
6547         [ $($LFS getstripe -S "$file1") -eq $orig_stripe_size ] ||
6548                 error "file was restriped"
6549         echo "done."
6550
6551         # Ensure file name properly detected when final option has no argument
6552         echo -n "Verifying file name properly detected..."
6553         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6554                 error "file name interpreted as option argument"
6555         echo "done."
6556
6557         # Clean up
6558         rm -f "$file1"
6559 }
6560 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
6561
6562 test_56wd() {
6563         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6564
6565         local file1=$DIR/$tdir/file1
6566
6567         echo -n "Creating test dir..."
6568         test_mkdir $DIR/$tdir || error "cannot create dir"
6569         echo "done."
6570
6571         echo -n "Creating test file..."
6572         touch $file1
6573         echo "done."
6574
6575         # Ensure 'lfs migrate' will fail by using a non-existent option,
6576         # and make sure rsync is not called to recover
6577         echo -n "Make sure --no-rsync option works..."
6578         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
6579                 grep -q 'refusing to fall back to rsync' ||
6580                 error "rsync was called with --no-rsync set"
6581         echo "done."
6582
6583         # Ensure rsync is called without trying 'lfs migrate' first
6584         echo -n "Make sure --rsync option works..."
6585         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
6586                 grep -q 'falling back to rsync' &&
6587                 error "lfs migrate was called with --rsync set"
6588         echo "done."
6589
6590         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
6591         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
6592                 grep -q 'at the same time' ||
6593                 error "--rsync and --no-rsync accepted concurrently"
6594         echo "done."
6595
6596         # Clean up
6597         rm -f $file1
6598 }
6599 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
6600
6601 test_56x() {
6602         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6603         check_swap_layouts_support
6604
6605         local dir=$DIR/$tdir
6606         local ref1=/etc/passwd
6607         local file1=$dir/file1
6608
6609         test_mkdir $dir || error "creating dir $dir"
6610         $LFS setstripe -c 2 $file1
6611         cp $ref1 $file1
6612         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
6613         stripe=$($LFS getstripe -c $file1)
6614         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6615         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6616
6617         # clean up
6618         rm -f $file1
6619 }
6620 run_test 56x "lfs migration support"
6621
6622 test_56xa() {
6623         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6624         check_swap_layouts_support
6625
6626         local dir=$DIR/$tdir/$testnum
6627
6628         test_mkdir -p $dir
6629
6630         local ref1=/etc/passwd
6631         local file1=$dir/file1
6632
6633         $LFS setstripe -c 2 $file1
6634         cp $ref1 $file1
6635         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
6636
6637         local stripe=$($LFS getstripe -c $file1)
6638
6639         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6640         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6641
6642         # clean up
6643         rm -f $file1
6644 }
6645 run_test 56xa "lfs migration --block support"
6646
6647 check_migrate_links() {
6648         local dir="$1"
6649         local file1="$dir/file1"
6650         local begin="$2"
6651         local count="$3"
6652         local runas="$4"
6653         local total_count=$(($begin + $count - 1))
6654         local symlink_count=10
6655         local uniq_count=10
6656
6657         if [ ! -f "$file1" ]; then
6658                 echo -n "creating initial file..."
6659                 $LFS setstripe -c 1 -S "512k" "$file1" ||
6660                         error "cannot setstripe initial file"
6661                 echo "done"
6662
6663                 echo -n "creating symlinks..."
6664                 for s in $(seq 1 $symlink_count); do
6665                         ln -s "$file1" "$dir/slink$s" ||
6666                                 error "cannot create symlinks"
6667                 done
6668                 echo "done"
6669
6670                 echo -n "creating nonlinked files..."
6671                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
6672                         error "cannot create nonlinked files"
6673                 echo "done"
6674         fi
6675
6676         # create hard links
6677         if [ ! -f "$dir/file$total_count" ]; then
6678                 echo -n "creating hard links $begin:$total_count..."
6679                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
6680                         /dev/null || error "cannot create hard links"
6681                 echo "done"
6682         fi
6683
6684         echo -n "checking number of hard links listed in xattrs..."
6685         local fid=$($LFS getstripe -F "$file1")
6686         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
6687
6688         echo "${#paths[*]}"
6689         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
6690                         skip "hard link list has unexpected size, skipping test"
6691         fi
6692         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
6693                         error "link names should exceed xattrs size"
6694         fi
6695
6696         echo -n "migrating files..."
6697         local migrate_out=$($runas $LFS_MIGRATE -y -S '1m' $dir)
6698         local rc=$?
6699         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
6700         echo "done"
6701
6702         # make sure all links have been properly migrated
6703         echo -n "verifying files..."
6704         fid=$($LFS getstripe -F "$file1") ||
6705                 error "cannot get fid for file $file1"
6706         for i in $(seq 2 $total_count); do
6707                 local fid2=$($LFS getstripe -F $dir/file$i)
6708
6709                 [ "$fid2" == "$fid" ] ||
6710                         error "migrated hard link has mismatched FID"
6711         done
6712
6713         # make sure hard links were properly detected, and migration was
6714         # performed only once for the entire link set; nonlinked files should
6715         # also be migrated
6716         local actual=$(grep -c 'done' <<< "$migrate_out")
6717         local expected=$(($uniq_count + 1))
6718
6719         [ "$actual" -eq  "$expected" ] ||
6720                 error "hard links individually migrated ($actual != $expected)"
6721
6722         # make sure the correct number of hard links are present
6723         local hardlinks=$(stat -c '%h' "$file1")
6724
6725         [ $hardlinks -eq $total_count ] ||
6726                 error "num hard links $hardlinks != $total_count"
6727         echo "done"
6728
6729         return 0
6730 }
6731
6732 test_56xb() {
6733         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
6734                 skip "Need MDS version at least 2.10.55"
6735
6736         local dir="$DIR/$tdir"
6737
6738         test_mkdir "$dir" || error "cannot create dir $dir"
6739
6740         echo "testing lfs migrate mode when all links fit within xattrs"
6741         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
6742
6743         echo "testing rsync mode when all links fit within xattrs"
6744         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
6745
6746         echo "testing lfs migrate mode when all links do not fit within xattrs"
6747         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
6748
6749         echo "testing rsync mode when all links do not fit within xattrs"
6750         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
6751
6752         chown -R $RUNAS_ID $dir
6753         echo "testing non-root lfs migrate mode when not all links are in xattr"
6754         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100 "$RUNAS"
6755
6756         # clean up
6757         rm -rf $dir
6758 }
6759 run_test 56xb "lfs migration hard link support"
6760
6761 test_56xc() {
6762         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6763
6764         local dir="$DIR/$tdir"
6765
6766         test_mkdir "$dir" || error "cannot create dir $dir"
6767
6768         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
6769         echo -n "Setting initial stripe for 20MB test file..."
6770         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
6771                 error "cannot setstripe 20MB file"
6772         echo "done"
6773         echo -n "Sizing 20MB test file..."
6774         truncate "$dir/20mb" 20971520 || error "cannot create 20MB test file"
6775         echo "done"
6776         echo -n "Verifying small file autostripe count is 1..."
6777         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
6778                 error "cannot migrate 20MB file"
6779         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
6780                 error "cannot get stripe for $dir/20mb"
6781         [ $stripe_count -eq 1 ] ||
6782                 error "unexpected stripe count $stripe_count for 20MB file"
6783         rm -f "$dir/20mb"
6784         echo "done"
6785
6786         # Test 2: File is small enough to fit within the available space on
6787         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
6788         # have at least an additional 1KB for each desired stripe for test 3
6789         echo -n "Setting stripe for 1GB test file..."
6790         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
6791         echo "done"
6792         echo -n "Sizing 1GB test file..."
6793         # File size is 1GB + 3KB
6794         truncate "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
6795         echo "done"
6796
6797         # need at least 512MB per OST for 1GB file to fit in 2 stripes
6798         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
6799         if (( avail > 524288 * OSTCOUNT )); then
6800                 echo -n "Migrating 1GB file..."
6801                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
6802                         error "cannot migrate 1GB file"
6803                 echo "done"
6804                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
6805                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
6806                         error "cannot getstripe for 1GB file"
6807                 [ $stripe_count -eq 2 ] ||
6808                         error "unexpected stripe count $stripe_count != 2"
6809                 echo "done"
6810         fi
6811
6812         # Test 3: File is too large to fit within the available space on
6813         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
6814         if [ $OSTCOUNT -ge 3 ]; then
6815                 # The required available space is calculated as
6816                 # file size (1GB + 3KB) / OST count (3).
6817                 local kb_per_ost=349526
6818
6819                 echo -n "Migrating 1GB file with limit..."
6820                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
6821                         error "cannot migrate 1GB file with limit"
6822                 echo "done"
6823
6824                 stripe_count=$($LFS getstripe -c "$dir/1gb")
6825                 echo -n "Verifying 1GB autostripe count with limited space..."
6826                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
6827                         error "unexpected stripe count $stripe_count (min 3)"
6828                 echo "done"
6829         fi
6830
6831         # clean up
6832         rm -rf $dir
6833 }
6834 run_test 56xc "lfs migration autostripe"
6835
6836 test_56y() {
6837         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
6838                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
6839
6840         local res=""
6841         local dir=$DIR/$tdir
6842         local f1=$dir/file1
6843         local f2=$dir/file2
6844
6845         test_mkdir -p $dir || error "creating dir $dir"
6846         touch $f1 || error "creating std file $f1"
6847         $MULTIOP $f2 H2c || error "creating released file $f2"
6848
6849         # a directory can be raid0, so ask only for files
6850         res=$($LFS find $dir -L raid0 -type f | wc -l)
6851         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
6852
6853         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
6854         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
6855
6856         # only files can be released, so no need to force file search
6857         res=$($LFS find $dir -L released)
6858         [[ $res == $f2 ]] || error "search released: found $res != $f2"
6859
6860         res=$($LFS find $dir -type f \! -L released)
6861         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
6862 }
6863 run_test 56y "lfs find -L raid0|released"
6864
6865 test_56z() { # LU-4824
6866         # This checks to make sure 'lfs find' continues after errors
6867         # There are two classes of errors that should be caught:
6868         # - If multiple paths are provided, all should be searched even if one
6869         #   errors out
6870         # - If errors are encountered during the search, it should not terminate
6871         #   early
6872         local dir=$DIR/$tdir
6873         local i
6874
6875         test_mkdir $dir
6876         for i in d{0..9}; do
6877                 test_mkdir $dir/$i
6878                 touch $dir/$i/$tfile
6879         done
6880         $LFS find $DIR/non_existent_dir $dir &&
6881                 error "$LFS find did not return an error"
6882         # Make a directory unsearchable. This should NOT be the last entry in
6883         # directory order.  Arbitrarily pick the 6th entry
6884         chmod 700 $($LFS find $dir -type d | sed '6!d')
6885
6886         $RUNAS $LFS find $DIR/non_existent $dir
6887         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
6888
6889         # The user should be able to see 10 directories and 9 files
6890         (( count == 19 )) ||
6891                 error "$LFS find found $count != 19 entries after error"
6892 }
6893 run_test 56z "lfs find should continue after an error"
6894
6895 test_56aa() { # LU-5937
6896         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
6897
6898         local dir=$DIR/$tdir
6899
6900         mkdir $dir
6901         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
6902
6903         createmany -o $dir/striped_dir/${tfile}- 1024
6904         local dirs=$($LFS find --size +8k $dir/)
6905
6906         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
6907 }
6908 run_test 56aa "lfs find --size under striped dir"
6909
6910 test_56ab() { # LU-10705
6911         test_mkdir $DIR/$tdir
6912         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
6913         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
6914         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
6915         # Flush writes to ensure valid blocks.  Need to be more thorough for
6916         # ZFS, since blocks are not allocated/returned to client immediately.
6917         sync_all_data
6918         wait_zfs_commit ost1 2
6919         cancel_lru_locks osc
6920         ls -ls $DIR/$tdir
6921
6922         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
6923
6924         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
6925
6926         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
6927         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
6928
6929         rm -f $DIR/$tdir/$tfile.[123]
6930 }
6931 run_test 56ab "lfs find --blocks"
6932
6933 test_56ba() {
6934         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
6935                 skip "Need MDS version at least 2.10.50"
6936
6937         # Create composite files with one component
6938         local dir=$DIR/$tdir
6939
6940         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
6941         # Create composite files with three components
6942         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
6943         # Create non-composite files
6944         createmany -o $dir/${tfile}- 10
6945
6946         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
6947
6948         [[ $nfiles == 10 ]] ||
6949                 error "lfs find -E 1M found $nfiles != 10 files"
6950
6951         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
6952         [[ $nfiles == 25 ]] ||
6953                 error "lfs find ! -E 1M found $nfiles != 25 files"
6954
6955         # All files have a component that starts at 0
6956         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
6957         [[ $nfiles == 35 ]] ||
6958                 error "lfs find --component-start 0 - $nfiles != 35 files"
6959
6960         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
6961         [[ $nfiles == 15 ]] ||
6962                 error "lfs find --component-start 2M - $nfiles != 15 files"
6963
6964         # All files created here have a componenet that does not starts at 2M
6965         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
6966         [[ $nfiles == 35 ]] ||
6967                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
6968
6969         # Find files with a specified number of components
6970         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
6971         [[ $nfiles == 15 ]] ||
6972                 error "lfs find --component-count 3 - $nfiles != 15 files"
6973
6974         # Remember non-composite files have a component count of zero
6975         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
6976         [[ $nfiles == 10 ]] ||
6977                 error "lfs find --component-count 0 - $nfiles != 10 files"
6978
6979         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
6980         [[ $nfiles == 20 ]] ||
6981                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
6982
6983         # All files have a flag called "init"
6984         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
6985         [[ $nfiles == 35 ]] ||
6986                 error "lfs find --component-flags init - $nfiles != 35 files"
6987
6988         # Multi-component files will have a component not initialized
6989         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
6990         [[ $nfiles == 15 ]] ||
6991                 error "lfs find !--component-flags init - $nfiles != 15 files"
6992
6993         rm -rf $dir
6994
6995 }
6996 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
6997
6998 test_56ca() {
6999         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
7000                 skip "Need MDS version at least 2.10.57"
7001
7002         local td=$DIR/$tdir
7003         local tf=$td/$tfile
7004         local dir
7005         local nfiles
7006         local cmd
7007         local i
7008         local j
7009
7010         # create mirrored directories and mirrored files
7011         mkdir $td || error "mkdir $td failed"
7012         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
7013         createmany -o $tf- 10 || error "create $tf- failed"
7014
7015         for i in $(seq 2); do
7016                 dir=$td/dir$i
7017                 mkdir $dir || error "mkdir $dir failed"
7018                 $LFS mirror create -N$((3 + i)) $dir ||
7019                         error "create mirrored dir $dir failed"
7020                 createmany -o $dir/$tfile- 10 ||
7021                         error "create $dir/$tfile- failed"
7022         done
7023
7024         # change the states of some mirrored files
7025         echo foo > $tf-6
7026         for i in $(seq 2); do
7027                 dir=$td/dir$i
7028                 for j in $(seq 4 9); do
7029                         echo foo > $dir/$tfile-$j
7030                 done
7031         done
7032
7033         # find mirrored files with specific mirror count
7034         cmd="$LFS find --mirror-count 3 --type f $td"
7035         nfiles=$($cmd | wc -l)
7036         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
7037
7038         cmd="$LFS find ! --mirror-count 3 --type f $td"
7039         nfiles=$($cmd | wc -l)
7040         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
7041
7042         cmd="$LFS find --mirror-count +2 --type f $td"
7043         nfiles=$($cmd | wc -l)
7044         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7045
7046         cmd="$LFS find --mirror-count -6 --type f $td"
7047         nfiles=$($cmd | wc -l)
7048         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7049
7050         # find mirrored files with specific file state
7051         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
7052         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
7053
7054         cmd="$LFS find --mirror-state=ro --type f $td"
7055         nfiles=$($cmd | wc -l)
7056         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
7057
7058         cmd="$LFS find ! --mirror-state=ro --type f $td"
7059         nfiles=$($cmd | wc -l)
7060         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7061
7062         cmd="$LFS find --mirror-state=wp --type f $td"
7063         nfiles=$($cmd | wc -l)
7064         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7065
7066         cmd="$LFS find ! --mirror-state=sp --type f $td"
7067         nfiles=$($cmd | wc -l)
7068         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7069 }
7070 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
7071
7072 test_57a() {
7073         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7074         # note test will not do anything if MDS is not local
7075         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7076                 skip_env "ldiskfs only test"
7077         fi
7078         remote_mds_nodsh && skip "remote MDS with nodsh"
7079
7080         local MNTDEV="osd*.*MDT*.mntdev"
7081         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
7082         [ -z "$DEV" ] && error "can't access $MNTDEV"
7083         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
7084                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
7085                         error "can't access $DEV"
7086                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
7087                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
7088                 rm $TMP/t57a.dump
7089         done
7090 }
7091 run_test 57a "verify MDS filesystem created with large inodes =="
7092
7093 test_57b() {
7094         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7095         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7096                 skip_env "ldiskfs only test"
7097         fi
7098         remote_mds_nodsh && skip "remote MDS with nodsh"
7099
7100         local dir=$DIR/$tdir
7101         local filecount=100
7102         local file1=$dir/f1
7103         local fileN=$dir/f$filecount
7104
7105         rm -rf $dir || error "removing $dir"
7106         test_mkdir -c1 $dir
7107         local mdtidx=$($LFS getstripe -m $dir)
7108         local mdtname=MDT$(printf %04x $mdtidx)
7109         local facet=mds$((mdtidx + 1))
7110
7111         echo "mcreating $filecount files"
7112         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
7113
7114         # verify that files do not have EAs yet
7115         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
7116                 error "$file1 has an EA"
7117         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
7118                 error "$fileN has an EA"
7119
7120         sync
7121         sleep 1
7122         df $dir  #make sure we get new statfs data
7123         local mdsfree=$(do_facet $facet \
7124                         lctl get_param -n osd*.*$mdtname.kbytesfree)
7125         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7126         local file
7127
7128         echo "opening files to create objects/EAs"
7129         for file in $(seq -f $dir/f%g 1 $filecount); do
7130                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
7131                         error "opening $file"
7132         done
7133
7134         # verify that files have EAs now
7135         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
7136         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
7137
7138         sleep 1  #make sure we get new statfs data
7139         df $dir
7140         local mdsfree2=$(do_facet $facet \
7141                          lctl get_param -n osd*.*$mdtname.kbytesfree)
7142         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7143
7144         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
7145                 if [ "$mdsfree" != "$mdsfree2" ]; then
7146                         error "MDC before $mdcfree != after $mdcfree2"
7147                 else
7148                         echo "MDC before $mdcfree != after $mdcfree2"
7149                         echo "unable to confirm if MDS has large inodes"
7150                 fi
7151         fi
7152         rm -rf $dir
7153 }
7154 run_test 57b "default LOV EAs are stored inside large inodes ==="
7155
7156 test_58() {
7157         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7158         [ -z "$(which wiretest 2>/dev/null)" ] &&
7159                         skip_env "could not find wiretest"
7160
7161         wiretest
7162 }
7163 run_test 58 "verify cross-platform wire constants =============="
7164
7165 test_59() {
7166         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7167
7168         echo "touch 130 files"
7169         createmany -o $DIR/f59- 130
7170         echo "rm 130 files"
7171         unlinkmany $DIR/f59- 130
7172         sync
7173         # wait for commitment of removal
7174         wait_delete_completed
7175 }
7176 run_test 59 "verify cancellation of llog records async ========="
7177
7178 TEST60_HEAD="test_60 run $RANDOM"
7179 test_60a() {
7180         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7181         remote_mgs_nodsh && skip "remote MGS with nodsh"
7182         do_facet mgs "! which run-llog.sh &> /dev/null" &&
7183                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
7184                         skip_env "missing subtest run-llog.sh"
7185
7186         log "$TEST60_HEAD - from kernel mode"
7187         do_facet mgs "$LCTL dk > /dev/null"
7188         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
7189         do_facet mgs $LCTL dk > $TMP/$tfile
7190
7191         # LU-6388: test llog_reader
7192         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
7193         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
7194         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
7195                         skip_env "missing llog_reader"
7196         local fstype=$(facet_fstype mgs)
7197         [ $fstype != ldiskfs -a $fstype != zfs ] &&
7198                 skip_env "Only for ldiskfs or zfs type mgs"
7199
7200         local mntpt=$(facet_mntpt mgs)
7201         local mgsdev=$(mgsdevname 1)
7202         local fid_list
7203         local fid
7204         local rec_list
7205         local rec
7206         local rec_type
7207         local obj_file
7208         local path
7209         local seq
7210         local oid
7211         local pass=true
7212
7213         #get fid and record list
7214         fid_list=($(awk '/9_sub.*record/ { print $NF }' $TMP/$tfile |
7215                 tail -n 4))
7216         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' $TMP/$tfile |
7217                 tail -n 4))
7218         #remount mgs as ldiskfs or zfs type
7219         stop mgs || error "stop mgs failed"
7220         mount_fstype mgs || error "remount mgs failed"
7221         for ((i = 0; i < ${#fid_list[@]}; i++)); do
7222                 fid=${fid_list[i]}
7223                 rec=${rec_list[i]}
7224                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
7225                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
7226                 oid=$((16#$oid))
7227
7228                 case $fstype in
7229                         ldiskfs )
7230                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
7231                         zfs )
7232                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
7233                 esac
7234                 echo "obj_file is $obj_file"
7235                 do_facet mgs $llog_reader $obj_file
7236
7237                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
7238                         awk '{ print $3 }' | sed -e "s/^type=//g")
7239                 if [ $rec_type != $rec ]; then
7240                         echo "FAILED test_60a wrong record type $rec_type," \
7241                               "should be $rec"
7242                         pass=false
7243                         break
7244                 fi
7245
7246                 #check obj path if record type is LLOG_LOGID_MAGIC
7247                 if [ "$rec" == "1064553b" ]; then
7248                         path=$(do_facet mgs $llog_reader $obj_file |
7249                                 grep "path=" | awk '{ print $NF }' |
7250                                 sed -e "s/^path=//g")
7251                         if [ $obj_file != $mntpt/$path ]; then
7252                                 echo "FAILED test_60a wrong obj path" \
7253                                       "$montpt/$path, should be $obj_file"
7254                                 pass=false
7255                                 break
7256                         fi
7257                 fi
7258         done
7259         rm -f $TMP/$tfile
7260         #restart mgs before "error", otherwise it will block the next test
7261         stop mgs || error "stop mgs failed"
7262         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
7263         $pass || error "test failed, see FAILED test_60a messages for specifics"
7264 }
7265 run_test 60a "llog_test run from kernel module and test llog_reader"
7266
7267 test_60b() { # bug 6411
7268         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7269
7270         dmesg > $DIR/$tfile
7271         LLOG_COUNT=$(do_facet mgs dmesg |
7272                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
7273                           /llog_[a-z]*.c:[0-9]/ {
7274                                 if (marker)
7275                                         from_marker++
7276                                 from_begin++
7277                           }
7278                           END {
7279                                 if (marker)
7280                                         print from_marker
7281                                 else
7282                                         print from_begin
7283                           }")
7284
7285         [[ $LLOG_COUNT -gt 120 ]] &&
7286                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
7287 }
7288 run_test 60b "limit repeated messages from CERROR/CWARN"
7289
7290 test_60c() {
7291         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7292
7293         echo "create 5000 files"
7294         createmany -o $DIR/f60c- 5000
7295 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
7296         lctl set_param fail_loc=0x80000137
7297         unlinkmany $DIR/f60c- 5000
7298         lctl set_param fail_loc=0
7299 }
7300 run_test 60c "unlink file when mds full"
7301
7302 test_60d() {
7303         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7304
7305         SAVEPRINTK=$(lctl get_param -n printk)
7306         # verify "lctl mark" is even working"
7307         MESSAGE="test message ID $RANDOM $$"
7308         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7309         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
7310
7311         lctl set_param printk=0 || error "set lnet.printk failed"
7312         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
7313         MESSAGE="new test message ID $RANDOM $$"
7314         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
7315         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7316         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
7317
7318         lctl set_param -n printk="$SAVEPRINTK"
7319 }
7320 run_test 60d "test printk console message masking"
7321
7322 test_60e() {
7323         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7324         remote_mds_nodsh && skip "remote MDS with nodsh"
7325
7326         touch $DIR/$tfile
7327 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
7328         do_facet mds1 lctl set_param fail_loc=0x15b
7329         rm $DIR/$tfile
7330 }
7331 run_test 60e "no space while new llog is being created"
7332
7333 test_60g() {
7334         local pid
7335         local i
7336
7337         test_mkdir -c $MDSCOUNT $DIR/$tdir
7338
7339         (
7340                 local index=0
7341                 while true; do
7342                         $LFS setdirstripe -i $(($index % $MDSCOUNT)) \
7343                                 -c $MDSCOUNT $DIR/$tdir/subdir$index \
7344                                 2>/dev/null
7345                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
7346                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
7347                         index=$((index + 1))
7348                 done
7349         ) &
7350
7351         pid=$!
7352
7353         for i in {0..100}; do
7354                 # define OBD_FAIL_OSD_TXN_START    0x19a
7355                 local index=$((i % MDSCOUNT + 1))
7356
7357                 do_facet mds$index $LCTL set_param fail_loc=0x8000019a \
7358                         > /dev/null
7359                 usleep 100
7360         done
7361
7362         kill -9 $pid
7363
7364         for i in $(seq $MDSCOUNT); do
7365                 do_facet mds$i $LCTL set_param fail_loc=0 > /dev/null
7366         done
7367
7368         mkdir $DIR/$tdir/new || error "mkdir failed"
7369         rmdir $DIR/$tdir/new || error "rmdir failed"
7370
7371         do_facet mds1 $LCTL lfsck_start -M $(facet_svc mds1) -A -C \
7372                 -t namespace
7373         for i in $(seq $MDSCOUNT); do
7374                 wait_update_facet mds$i "$LCTL get_param -n \
7375                         mdd.$(facet_svc mds$i).lfsck_namespace |
7376                         awk '/^status/ { print \\\$2 }'" "completed"
7377         done
7378
7379         ls -R $DIR/$tdir || error "ls failed"
7380         rm -rf $DIR/$tdir || error "rmdir failed"
7381 }
7382 run_test 60g "transaction abort won't cause MDT hung"
7383
7384 test_60h() {
7385         [ $MDS1_VERSION -le $(version_code 2.12.52) ] &&
7386                 skip "Need MDS version at least 2.12.52"
7387         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
7388
7389         local f
7390
7391         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
7392         #define OBD_FAIL_MDS_STRIPE_FID          0x189
7393         for fail_loc in 0x80000188 0x80000189; do
7394                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
7395                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
7396                         error "mkdir $dir-$fail_loc failed"
7397                 for i in {0..10}; do
7398                         # create may fail on missing stripe
7399                         echo $i > $DIR/$tdir-$fail_loc/$i
7400                 done
7401                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7402                         error "getdirstripe $tdir-$fail_loc failed"
7403                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
7404                         error "migrate $tdir-$fail_loc failed"
7405                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7406                         error "getdirstripe $tdir-$fail_loc failed"
7407                 pushd $DIR/$tdir-$fail_loc
7408                 for f in *; do
7409                         echo $f | cmp $f - || error "$f data mismatch"
7410                 done
7411                 popd
7412                 rm -rf $DIR/$tdir-$fail_loc
7413         done
7414 }
7415 run_test 60h "striped directory with missing stripes can be accessed"
7416
7417 test_61a() {
7418         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7419
7420         f="$DIR/f61"
7421         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
7422         cancel_lru_locks osc
7423         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
7424         sync
7425 }
7426 run_test 61a "mmap() writes don't make sync hang ================"
7427
7428 test_61b() {
7429         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
7430 }
7431 run_test 61b "mmap() of unstriped file is successful"
7432
7433 # bug 2330 - insufficient obd_match error checking causes LBUG
7434 test_62() {
7435         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7436
7437         f="$DIR/f62"
7438         echo foo > $f
7439         cancel_lru_locks osc
7440         lctl set_param fail_loc=0x405
7441         cat $f && error "cat succeeded, expect -EIO"
7442         lctl set_param fail_loc=0
7443 }
7444 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
7445 # match every page all of the time.
7446 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
7447
7448 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
7449 # Though this test is irrelevant anymore, it helped to reveal some
7450 # other grant bugs (LU-4482), let's keep it.
7451 test_63a() {   # was test_63
7452         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7453
7454         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
7455
7456         for i in `seq 10` ; do
7457                 dd if=/dev/zero of=$DIR/f63 bs=8k &
7458                 sleep 5
7459                 kill $!
7460                 sleep 1
7461         done
7462
7463         rm -f $DIR/f63 || true
7464 }
7465 run_test 63a "Verify oig_wait interruption does not crash ======="
7466
7467 # bug 2248 - async write errors didn't return to application on sync
7468 # bug 3677 - async write errors left page locked
7469 test_63b() {
7470         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7471
7472         debugsave
7473         lctl set_param debug=-1
7474
7475         # ensure we have a grant to do async writes
7476         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
7477         rm $DIR/$tfile
7478
7479         sync    # sync lest earlier test intercept the fail_loc
7480
7481         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7482         lctl set_param fail_loc=0x80000406
7483         $MULTIOP $DIR/$tfile Owy && \
7484                 error "sync didn't return ENOMEM"
7485         sync; sleep 2; sync     # do a real sync this time to flush page
7486         lctl get_param -n llite.*.dump_page_cache | grep locked && \
7487                 error "locked page left in cache after async error" || true
7488         debugrestore
7489 }
7490 run_test 63b "async write errors should be returned to fsync ==="
7491
7492 test_64a () {
7493         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7494
7495         df $DIR
7496         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
7497 }
7498 run_test 64a "verify filter grant calculations (in kernel) ====="
7499
7500 test_64b () {
7501         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7502
7503         sh oos.sh $MOUNT || error "oos.sh failed: $?"
7504 }
7505 run_test 64b "check out-of-space detection on client"
7506
7507 test_64c() {
7508         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
7509 }
7510 run_test 64c "verify grant shrink"
7511
7512 # this does exactly what osc_request.c:osc_announce_cached() does in
7513 # order to calculate max amount of grants to ask from server
7514 want_grant() {
7515         local tgt=$1
7516
7517         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
7518         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
7519
7520         ((rpc_in_flight ++));
7521         nrpages=$((nrpages * rpc_in_flight))
7522
7523         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
7524
7525         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
7526
7527         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
7528         local undirty=$((nrpages * PAGE_SIZE))
7529
7530         local max_extent_pages
7531         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
7532             grep grant_max_extent_size | awk '{print $2}')
7533         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
7534         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
7535         local grant_extent_tax
7536         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7537             grep grant_extent_tax | awk '{print $2}')
7538
7539         undirty=$((undirty + nrextents * grant_extent_tax))
7540
7541         echo $undirty
7542 }
7543
7544 # this is size of unit for grant allocation. It should be equal to
7545 # what tgt_grant.c:tgt_grant_chunk() calculates
7546 grant_chunk() {
7547         local tgt=$1
7548         local max_brw_size
7549         local grant_extent_tax
7550
7551         max_brw_size=$($LCTL get_param osc.${tgt}.import |
7552             grep max_brw_size | awk '{print $2}')
7553
7554         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7555             grep grant_extent_tax | awk '{print $2}')
7556
7557         echo $(((max_brw_size + grant_extent_tax) * 2))
7558 }
7559
7560 test_64d() {
7561         [ $OST1_VERSION -lt $(version_code 2.10.56) ] &&
7562                 skip "OST < 2.10.55 doesn't limit grants enough"
7563
7564         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
7565         local file=$DIR/$tfile
7566
7567         [[ $($LCTL get_param osc.${tgt}.import |
7568              grep "connect_flags:.*grant_param") ]] ||
7569                 skip "no grant_param connect flag"
7570
7571         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
7572
7573         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
7574
7575         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
7576         stack_trap "rm -f $file" EXIT
7577
7578         $LFS setstripe $file -i 0 -c 1
7579         dd if=/dev/zero of=$file bs=1M count=1000 &
7580         ddpid=$!
7581
7582         while true
7583         do
7584                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
7585                 if [[ $cur_grant -gt $max_cur_granted ]]
7586                 then
7587                         kill $ddpid
7588                         error "cur_grant $cur_grant > $max_cur_granted"
7589                 fi
7590                 kill -0 $ddpid
7591                 [[ $? -ne 0 ]] && break;
7592                 sleep 2
7593         done
7594
7595         rm -f $DIR/$tfile
7596         wait_delete_completed
7597         $LCTL set_param debug="$olddebug" 2> /dev/null || true
7598 }
7599 run_test 64d "check grant limit exceed"
7600
7601 # bug 1414 - set/get directories' stripe info
7602 test_65a() {
7603         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7604
7605         test_mkdir $DIR/$tdir
7606         touch $DIR/$tdir/f1
7607         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
7608 }
7609 run_test 65a "directory with no stripe info"
7610
7611 test_65b() {
7612         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7613
7614         test_mkdir $DIR/$tdir
7615         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7616
7617         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7618                                                 error "setstripe"
7619         touch $DIR/$tdir/f2
7620         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
7621 }
7622 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
7623
7624 test_65c() {
7625         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7626         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
7627
7628         test_mkdir $DIR/$tdir
7629         local stripesize=$($LFS getstripe -S $DIR/$tdir)
7630
7631         $LFS setstripe -S $((stripesize * 4)) -i 1 \
7632                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
7633         touch $DIR/$tdir/f3
7634         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
7635 }
7636 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
7637
7638 test_65d() {
7639         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7640
7641         test_mkdir $DIR/$tdir
7642         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
7643         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7644
7645         if [[ $STRIPECOUNT -le 0 ]]; then
7646                 sc=1
7647         elif [[ $STRIPECOUNT -gt $LOV_MAX_STRIPE_COUNT ]]; then
7648                 [[ $OSTCOUNT -gt $LOV_MAX_STRIPE_COUNT ]] &&
7649                         sc=$LOV_MAX_STRIPE_COUNT || sc=$(($OSTCOUNT - 1))
7650         else
7651                 sc=$(($STRIPECOUNT - 1))
7652         fi
7653         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
7654         touch $DIR/$tdir/f4 $DIR/$tdir/f5
7655         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
7656                 error "lverify failed"
7657 }
7658 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
7659
7660 test_65e() {
7661         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7662
7663         test_mkdir $DIR/$tdir
7664
7665         $LFS setstripe $DIR/$tdir || error "setstripe"
7666         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7667                                         error "no stripe info failed"
7668         touch $DIR/$tdir/f6
7669         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
7670 }
7671 run_test 65e "directory setstripe defaults"
7672
7673 test_65f() {
7674         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7675
7676         test_mkdir $DIR/${tdir}f
7677         $RUNAS $LFS setstripe $DIR/${tdir}f &&
7678                 error "setstripe succeeded" || true
7679 }
7680 run_test 65f "dir setstripe permission (should return error) ==="
7681
7682 test_65g() {
7683         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7684
7685         test_mkdir $DIR/$tdir
7686         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7687
7688         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7689                 error "setstripe -S failed"
7690         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
7691         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7692                 error "delete default stripe failed"
7693 }
7694 run_test 65g "directory setstripe -d"
7695
7696 test_65h() {
7697         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7698
7699         test_mkdir $DIR/$tdir
7700         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7701
7702         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7703                 error "setstripe -S failed"
7704         test_mkdir $DIR/$tdir/dd1
7705         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
7706                 error "stripe info inherit failed"
7707 }
7708 run_test 65h "directory stripe info inherit ===================="
7709
7710 test_65i() {
7711         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7712
7713         save_layout_restore_at_exit $MOUNT
7714
7715         # bug6367: set non-default striping on root directory
7716         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
7717
7718         # bug12836: getstripe on -1 default directory striping
7719         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
7720
7721         # bug12836: getstripe -v on -1 default directory striping
7722         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
7723
7724         # bug12836: new find on -1 default directory striping
7725         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
7726 }
7727 run_test 65i "various tests to set root directory striping"
7728
7729 test_65j() { # bug6367
7730         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7731
7732         sync; sleep 1
7733
7734         # if we aren't already remounting for each test, do so for this test
7735         if [ "$I_MOUNTED" = "yes" ]; then
7736                 cleanup || error "failed to unmount"
7737                 setup
7738         fi
7739
7740         save_layout_restore_at_exit $MOUNT
7741
7742         $LFS setstripe -d $MOUNT || error "setstripe failed"
7743 }
7744 run_test 65j "set default striping on root directory (bug 6367)="
7745
7746 cleanup_65k() {
7747         rm -rf $DIR/$tdir
7748         wait_delete_completed
7749         do_facet $SINGLEMDS "lctl set_param -n \
7750                 osp.$ost*MDT0000.max_create_count=$max_count"
7751         do_facet $SINGLEMDS "lctl set_param -n \
7752                 osp.$ost*MDT0000.create_count=$count"
7753         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7754         echo $INACTIVE_OSC "is Activate"
7755
7756         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7757 }
7758
7759 test_65k() { # bug11679
7760         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7761         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7762         remote_mds_nodsh && skip "remote MDS with nodsh"
7763
7764         local disable_precreate=true
7765         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
7766                 disable_precreate=false
7767
7768         echo "Check OST status: "
7769         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
7770                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
7771
7772         for OSC in $MDS_OSCS; do
7773                 echo $OSC "is active"
7774                 do_facet $SINGLEMDS lctl --device %$OSC activate
7775         done
7776
7777         for INACTIVE_OSC in $MDS_OSCS; do
7778                 local ost=$(osc_to_ost $INACTIVE_OSC)
7779                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
7780                                lov.*md*.target_obd |
7781                                awk -F: /$ost/'{ print $1 }' | head -n 1)
7782
7783                 mkdir -p $DIR/$tdir
7784                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
7785                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
7786
7787                 echo "Deactivate: " $INACTIVE_OSC
7788                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
7789
7790                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
7791                               osp.$ost*MDT0000.create_count")
7792                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
7793                                   osp.$ost*MDT0000.max_create_count")
7794                 $disable_precreate &&
7795                         do_facet $SINGLEMDS "lctl set_param -n \
7796                                 osp.$ost*MDT0000.max_create_count=0"
7797
7798                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
7799                         [ -f $DIR/$tdir/$idx ] && continue
7800                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
7801                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
7802                                 { cleanup_65k;
7803                                   error "setstripe $idx should succeed"; }
7804                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
7805                 done
7806                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
7807                 rmdir $DIR/$tdir
7808
7809                 do_facet $SINGLEMDS "lctl set_param -n \
7810                         osp.$ost*MDT0000.max_create_count=$max_count"
7811                 do_facet $SINGLEMDS "lctl set_param -n \
7812                         osp.$ost*MDT0000.create_count=$count"
7813                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7814                 echo $INACTIVE_OSC "is Activate"
7815
7816                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7817         done
7818 }
7819 run_test 65k "validate manual striping works properly with deactivated OSCs"
7820
7821 test_65l() { # bug 12836
7822         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7823
7824         test_mkdir -p $DIR/$tdir/test_dir
7825         $LFS setstripe -c -1 $DIR/$tdir/test_dir
7826         $LFS find -mtime -1 $DIR/$tdir >/dev/null
7827 }
7828 run_test 65l "lfs find on -1 stripe dir ========================"
7829
7830 test_65m() {
7831         local layout=$(save_layout $MOUNT)
7832         $RUNAS $LFS setstripe -c 2 $MOUNT && {
7833                 restore_layout $MOUNT $layout
7834                 error "setstripe should fail by non-root users"
7835         }
7836         true
7837 }
7838 run_test 65m "normal user can't set filesystem default stripe"
7839
7840 test_65n() {
7841         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
7842         [[ $MDS1_VERSION -ge $(version_code 2.12.50) ]] ||
7843                 skip "Need MDS version at least 2.12.50"
7844         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
7845
7846         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7847         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
7848         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
7849
7850         local root_layout=$(save_layout $MOUNT)
7851         stack_trap "restore_layout $MOUNT $root_layout" EXIT
7852
7853         # new subdirectory under root directory should not inherit
7854         # the default layout from root
7855         local dir1=$MOUNT/$tdir-1
7856         mkdir $dir1 || error "mkdir $dir1 failed"
7857         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
7858                 error "$dir1 shouldn't have LOV EA"
7859
7860         # delete the default layout on root directory
7861         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
7862
7863         local dir2=$MOUNT/$tdir-2
7864         mkdir $dir2 || error "mkdir $dir2 failed"
7865         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
7866                 error "$dir2 shouldn't have LOV EA"
7867
7868         # set a new striping pattern on root directory
7869         local def_stripe_size=$($LFS getstripe -S $MOUNT)
7870         local new_def_stripe_size=$((def_stripe_size * 2))
7871         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
7872                 error "set stripe size on $MOUNT failed"
7873
7874         # new file created in $dir2 should inherit the new stripe size from
7875         # the filesystem default
7876         local file2=$dir2/$tfile-2
7877         touch $file2 || error "touch $file2 failed"
7878
7879         local file2_stripe_size=$($LFS getstripe -S $file2)
7880         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
7881                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
7882
7883         local dir3=$MOUNT/$tdir-3
7884         mkdir $dir3 || error "mkdir $dir3 failed"
7885         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
7886         # the root layout, which is the actual default layout that will be used
7887         # when new files are created in $dir3.
7888         local dir3_layout=$(get_layout_param $dir3)
7889         local root_dir_layout=$(get_layout_param $MOUNT)
7890         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
7891                 error "$dir3 should show the default layout from $MOUNT"
7892
7893         # set OST pool on root directory
7894         local pool=$TESTNAME
7895         pool_add $pool || error "add $pool failed"
7896         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
7897                 error "add targets to $pool failed"
7898
7899         $LFS setstripe -p $pool $MOUNT ||
7900                 error "set OST pool on $MOUNT failed"
7901
7902         # new file created in $dir3 should inherit the pool from
7903         # the filesystem default
7904         local file3=$dir3/$tfile-3
7905         touch $file3 || error "touch $file3 failed"
7906
7907         local file3_pool=$($LFS getstripe -p $file3)
7908         [[ "$file3_pool" = "$pool" ]] ||
7909                 error "$file3 didn't inherit OST pool $pool"
7910
7911         local dir4=$MOUNT/$tdir-4
7912         mkdir $dir4 || error "mkdir $dir4 failed"
7913         local dir4_layout=$(get_layout_param $dir4)
7914         root_dir_layout=$(get_layout_param $MOUNT)
7915         echo "$LFS getstripe -d $dir4"
7916         $LFS getstripe -d $dir4
7917         echo "$LFS getstripe -d $MOUNT"
7918         $LFS getstripe -d $MOUNT
7919         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
7920                 error "$dir4 should show the default layout from $MOUNT"
7921
7922         # new file created in $dir4 should inherit the pool from
7923         # the filesystem default
7924         local file4=$dir4/$tfile-4
7925         touch $file4 || error "touch $file4 failed"
7926
7927         local file4_pool=$($LFS getstripe -p $file4)
7928         [[ "$file4_pool" = "$pool" ]] ||
7929                 error "$file4 didn't inherit OST pool $pool"
7930
7931         # new subdirectory under non-root directory should inherit
7932         # the default layout from its parent directory
7933         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
7934                 error "set directory layout on $dir4 failed"
7935
7936         local dir5=$dir4/$tdir-5
7937         mkdir $dir5 || error "mkdir $dir5 failed"
7938
7939         dir4_layout=$(get_layout_param $dir4)
7940         local dir5_layout=$(get_layout_param $dir5)
7941         [[ "$dir4_layout" = "$dir5_layout" ]] ||
7942                 error "$dir5 should inherit the default layout from $dir4"
7943
7944         # though subdir under ROOT doesn't inherit default layout, but
7945         # its sub dir/file should be created with default layout.
7946         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
7947         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
7948                 skip "Need MDS version at least 2.12.59"
7949
7950         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
7951         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
7952         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
7953
7954         if [ $default_lmv_hash == "none" ]; then
7955                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
7956         else
7957                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
7958                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
7959         fi
7960
7961         $LFS setdirstripe -D -c 2 $MOUNT ||
7962                 error "setdirstripe -D -c 2 failed"
7963         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
7964         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
7965         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
7966 }
7967 run_test 65n "don't inherit default layout from root for new subdirectories"
7968
7969 # bug 2543 - update blocks count on client
7970 test_66() {
7971         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7972
7973         COUNT=${COUNT:-8}
7974         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
7975         sync; sync_all_data; sync; sync_all_data
7976         cancel_lru_locks osc
7977         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
7978         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
7979 }
7980 run_test 66 "update inode blocks count on client ==============="
7981
7982 meminfo() {
7983         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
7984 }
7985
7986 swap_used() {
7987         swapon -s | awk '($1 == "'$1'") { print $4 }'
7988 }
7989
7990 # bug5265, obdfilter oa2dentry return -ENOENT
7991 # #define OBD_FAIL_SRV_ENOENT 0x217
7992 test_69() {
7993         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7994         remote_ost_nodsh && skip "remote OST with nodsh"
7995
7996         f="$DIR/$tfile"
7997         $LFS setstripe -c 1 -i 0 $f
7998
7999         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
8000
8001         do_facet ost1 lctl set_param fail_loc=0x217
8002         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
8003         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
8004
8005         do_facet ost1 lctl set_param fail_loc=0
8006         $DIRECTIO write $f 0 2 || error "write error"
8007
8008         cancel_lru_locks osc
8009         $DIRECTIO read $f 0 1 || error "read error"
8010
8011         do_facet ost1 lctl set_param fail_loc=0x217
8012         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
8013
8014         do_facet ost1 lctl set_param fail_loc=0
8015         rm -f $f
8016 }
8017 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
8018
8019 test_71() {
8020         test_mkdir $DIR/$tdir
8021         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
8022         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
8023 }
8024 run_test 71 "Running dbench on lustre (don't segment fault) ===="
8025
8026 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
8027         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8028         [ "$RUNAS_ID" = "$UID" ] &&
8029                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8030         # Check that testing environment is properly set up. Skip if not
8031         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
8032                 skip_env "User $RUNAS_ID does not exist - skipping"
8033
8034         touch $DIR/$tfile
8035         chmod 777 $DIR/$tfile
8036         chmod ug+s $DIR/$tfile
8037         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
8038                 error "$RUNAS dd $DIR/$tfile failed"
8039         # See if we are still setuid/sgid
8040         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8041                 error "S/gid is not dropped on write"
8042         # Now test that MDS is updated too
8043         cancel_lru_locks mdc
8044         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8045                 error "S/gid is not dropped on MDS"
8046         rm -f $DIR/$tfile
8047 }
8048 run_test 72a "Test that remove suid works properly (bug5695) ===="
8049
8050 test_72b() { # bug 24226 -- keep mode setting when size is not changing
8051         local perm
8052
8053         [ "$RUNAS_ID" = "$UID" ] &&
8054                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8055         [ "$RUNAS_ID" -eq 0 ] &&
8056                 skip_env "RUNAS_ID = 0 -- skipping"
8057         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8058         # Check that testing environment is properly set up. Skip if not
8059         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
8060                 skip_env "User $RUNAS_ID does not exist - skipping"
8061
8062         touch $DIR/${tfile}-f{g,u}
8063         test_mkdir $DIR/${tfile}-dg
8064         test_mkdir $DIR/${tfile}-du
8065         chmod 770 $DIR/${tfile}-{f,d}{g,u}
8066         chmod g+s $DIR/${tfile}-{f,d}g
8067         chmod u+s $DIR/${tfile}-{f,d}u
8068         for perm in 777 2777 4777; do
8069                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
8070                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
8071                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
8072                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
8073         done
8074         true
8075 }
8076 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
8077
8078 # bug 3462 - multiple simultaneous MDC requests
8079 test_73() {
8080         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8081
8082         test_mkdir $DIR/d73-1
8083         test_mkdir $DIR/d73-2
8084         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
8085         pid1=$!
8086
8087         lctl set_param fail_loc=0x80000129
8088         $MULTIOP $DIR/d73-1/f73-2 Oc &
8089         sleep 1
8090         lctl set_param fail_loc=0
8091
8092         $MULTIOP $DIR/d73-2/f73-3 Oc &
8093         pid3=$!
8094
8095         kill -USR1 $pid1
8096         wait $pid1 || return 1
8097
8098         sleep 25
8099
8100         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
8101         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
8102         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
8103
8104         rm -rf $DIR/d73-*
8105 }
8106 run_test 73 "multiple MDC requests (should not deadlock)"
8107
8108 test_74a() { # bug 6149, 6184
8109         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8110
8111         touch $DIR/f74a
8112         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8113         #
8114         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8115         # will spin in a tight reconnection loop
8116         $LCTL set_param fail_loc=0x8000030e
8117         # get any lock that won't be difficult - lookup works.
8118         ls $DIR/f74a
8119         $LCTL set_param fail_loc=0
8120         rm -f $DIR/f74a
8121         true
8122 }
8123 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
8124
8125 test_74b() { # bug 13310
8126         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8127
8128         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8129         #
8130         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8131         # will spin in a tight reconnection loop
8132         $LCTL set_param fail_loc=0x8000030e
8133         # get a "difficult" lock
8134         touch $DIR/f74b
8135         $LCTL set_param fail_loc=0
8136         rm -f $DIR/f74b
8137         true
8138 }
8139 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
8140
8141 test_74c() {
8142         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8143
8144         #define OBD_FAIL_LDLM_NEW_LOCK
8145         $LCTL set_param fail_loc=0x319
8146         touch $DIR/$tfile && error "touch successful"
8147         $LCTL set_param fail_loc=0
8148         true
8149 }
8150 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
8151
8152 num_inodes() {
8153         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
8154 }
8155
8156 test_76() { # Now for bug 20433, added originally in bug 1443
8157         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8158
8159         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
8160
8161         cancel_lru_locks osc
8162         BEFORE_INODES=$(num_inodes)
8163         echo "before inodes: $BEFORE_INODES"
8164         local COUNT=1000
8165         [ "$SLOW" = "no" ] && COUNT=100
8166         for i in $(seq $COUNT); do
8167                 touch $DIR/$tfile
8168                 rm -f $DIR/$tfile
8169         done
8170         cancel_lru_locks osc
8171         AFTER_INODES=$(num_inodes)
8172         echo "after inodes: $AFTER_INODES"
8173         local wait=0
8174         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
8175                 sleep 2
8176                 AFTER_INODES=$(num_inodes)
8177                 wait=$((wait+2))
8178                 echo "wait $wait seconds inodes: $AFTER_INODES"
8179                 if [ $wait -gt 30 ]; then
8180                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
8181                 fi
8182         done
8183 }
8184 run_test 76 "confirm clients recycle inodes properly ===="
8185
8186
8187 export ORIG_CSUM=""
8188 set_checksums()
8189 {
8190         # Note: in sptlrpc modes which enable its own bulk checksum, the
8191         # original crc32_le bulk checksum will be automatically disabled,
8192         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
8193         # will be checked by sptlrpc code against sptlrpc bulk checksum.
8194         # In this case set_checksums() will not be no-op, because sptlrpc
8195         # bulk checksum will be enabled all through the test.
8196
8197         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
8198         lctl set_param -n osc.*.checksums $1
8199         return 0
8200 }
8201
8202 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8203                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
8204 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8205                              tr -d [] | head -n1)}
8206 set_checksum_type()
8207 {
8208         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
8209         rc=$?
8210         log "set checksum type to $1, rc = $rc"
8211         return $rc
8212 }
8213
8214 get_osc_checksum_type()
8215 {
8216         # arugment 1: OST name, like OST0000
8217         ost=$1
8218         checksum_type=$(lctl get_param -n osc.*${ost}-osc-[^mM]*.checksum_type |
8219                         sed 's/.*\[\(.*\)\].*/\1/g')
8220         rc=$?
8221         [ $rc -ne 0 ] && error "failed to get checksum type of $ost, rc = $rc, output = $checksum_type"
8222         echo $checksum_type
8223 }
8224
8225 F77_TMP=$TMP/f77-temp
8226 F77SZ=8
8227 setup_f77() {
8228         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
8229                 error "error writing to $F77_TMP"
8230 }
8231
8232 test_77a() { # bug 10889
8233         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8234         $GSS && skip_env "could not run with gss"
8235
8236         [ ! -f $F77_TMP ] && setup_f77
8237         set_checksums 1
8238         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
8239         set_checksums 0
8240         rm -f $DIR/$tfile
8241 }
8242 run_test 77a "normal checksum read/write operation"
8243
8244 test_77b() { # bug 10889
8245         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8246         $GSS && skip_env "could not run with gss"
8247
8248         [ ! -f $F77_TMP ] && setup_f77
8249         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8250         $LCTL set_param fail_loc=0x80000409
8251         set_checksums 1
8252
8253         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8254                 error "dd error: $?"
8255         $LCTL set_param fail_loc=0
8256
8257         for algo in $CKSUM_TYPES; do
8258                 cancel_lru_locks osc
8259                 set_checksum_type $algo
8260                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8261                 $LCTL set_param fail_loc=0x80000408
8262                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
8263                 $LCTL set_param fail_loc=0
8264         done
8265         set_checksums 0
8266         set_checksum_type $ORIG_CSUM_TYPE
8267         rm -f $DIR/$tfile
8268 }
8269 run_test 77b "checksum error on client write, read"
8270
8271 cleanup_77c() {
8272         trap 0
8273         set_checksums 0
8274         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
8275         $check_ost &&
8276                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
8277         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
8278         $check_ost && [ -n "$ost_file_prefix" ] &&
8279                 do_facet ost1 rm -f ${ost_file_prefix}\*
8280 }
8281
8282 test_77c() {
8283         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8284         $GSS && skip_env "could not run with gss"
8285         remote_ost_nodsh && skip "remote OST with nodsh"
8286
8287         local bad1
8288         local osc_file_prefix
8289         local osc_file
8290         local check_ost=false
8291         local ost_file_prefix
8292         local ost_file
8293         local orig_cksum
8294         local dump_cksum
8295         local fid
8296
8297         # ensure corruption will occur on first OSS/OST
8298         $LFS setstripe -i 0 $DIR/$tfile
8299
8300         [ ! -f $F77_TMP ] && setup_f77
8301         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8302                 error "dd write error: $?"
8303         fid=$($LFS path2fid $DIR/$tfile)
8304
8305         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
8306         then
8307                 check_ost=true
8308                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
8309                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
8310         else
8311                 echo "OSS do not support bulk pages dump upon error"
8312         fi
8313
8314         osc_file_prefix=$($LCTL get_param -n debug_path)
8315         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
8316
8317         trap cleanup_77c EXIT
8318
8319         set_checksums 1
8320         # enable bulk pages dump upon error on Client
8321         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
8322         # enable bulk pages dump upon error on OSS
8323         $check_ost &&
8324                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
8325
8326         # flush Client cache to allow next read to reach OSS
8327         cancel_lru_locks osc
8328
8329         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
8330         $LCTL set_param fail_loc=0x80000408
8331         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
8332         $LCTL set_param fail_loc=0
8333
8334         rm -f $DIR/$tfile
8335
8336         # check cksum dump on Client
8337         osc_file=$(ls ${osc_file_prefix}*)
8338         [ -n "$osc_file" ] || error "no checksum dump file on Client"
8339         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
8340         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
8341         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
8342         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
8343                      cksum)
8344         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
8345         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8346                 error "dump content does not match on Client"
8347
8348         $check_ost || skip "No need to check cksum dump on OSS"
8349
8350         # check cksum dump on OSS
8351         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
8352         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
8353         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
8354         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
8355         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8356                 error "dump content does not match on OSS"
8357
8358         cleanup_77c
8359 }
8360 run_test 77c "checksum error on client read with debug"
8361
8362 test_77d() { # bug 10889
8363         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8364         $GSS && skip_env "could not run with gss"
8365
8366         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8367         $LCTL set_param fail_loc=0x80000409
8368         set_checksums 1
8369         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8370                 error "direct write: rc=$?"
8371         $LCTL set_param fail_loc=0
8372         set_checksums 0
8373
8374         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8375         $LCTL set_param fail_loc=0x80000408
8376         set_checksums 1
8377         cancel_lru_locks osc
8378         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8379                 error "direct read: rc=$?"
8380         $LCTL set_param fail_loc=0
8381         set_checksums 0
8382 }
8383 run_test 77d "checksum error on OST direct write, read"
8384
8385 test_77f() { # bug 10889
8386         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8387         $GSS && skip_env "could not run with gss"
8388
8389         set_checksums 1
8390         for algo in $CKSUM_TYPES; do
8391                 cancel_lru_locks osc
8392                 set_checksum_type $algo
8393                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8394                 $LCTL set_param fail_loc=0x409
8395                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
8396                         error "direct write succeeded"
8397                 $LCTL set_param fail_loc=0
8398         done
8399         set_checksum_type $ORIG_CSUM_TYPE
8400         set_checksums 0
8401 }
8402 run_test 77f "repeat checksum error on write (expect error)"
8403
8404 test_77g() { # bug 10889
8405         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8406         $GSS && skip_env "could not run with gss"
8407         remote_ost_nodsh && skip "remote OST with nodsh"
8408
8409         [ ! -f $F77_TMP ] && setup_f77
8410
8411         local file=$DIR/$tfile
8412         stack_trap "rm -f $file" EXIT
8413
8414         $LFS setstripe -c 1 -i 0 $file
8415         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
8416         do_facet ost1 lctl set_param fail_loc=0x8000021a
8417         set_checksums 1
8418         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
8419                 error "write error: rc=$?"
8420         do_facet ost1 lctl set_param fail_loc=0
8421         set_checksums 0
8422
8423         cancel_lru_locks osc
8424         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
8425         do_facet ost1 lctl set_param fail_loc=0x8000021b
8426         set_checksums 1
8427         cmp $F77_TMP $file || error "file compare failed"
8428         do_facet ost1 lctl set_param fail_loc=0
8429         set_checksums 0
8430 }
8431 run_test 77g "checksum error on OST write, read"
8432
8433 test_77k() { # LU-10906
8434         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8435         $GSS && skip_env "could not run with gss"
8436
8437         local cksum_param="osc.$FSNAME*.checksums"
8438         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
8439         local checksum
8440         local i
8441
8442         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
8443         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
8444         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
8445                 EXIT
8446
8447         for i in 0 1; do
8448                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
8449                         error "failed to set checksum=$i on MGS"
8450                 wait_update $HOSTNAME "$get_checksum" $i
8451                 #remount
8452                 echo "remount client, checksum should be $i"
8453                 remount_client $MOUNT || error "failed to remount client"
8454                 checksum=$(eval $get_checksum)
8455                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8456         done
8457         # remove persistent param to avoid races with checksum mountopt below
8458         do_facet mgs $LCTL set_param -P -d $cksum_param ||
8459                 error "failed to delete checksum on MGS"
8460
8461         for opt in "checksum" "nochecksum"; do
8462                 #remount with mount option
8463                 echo "remount client with option $opt, checksum should be $i"
8464                 umount_client $MOUNT || error "failed to umount client"
8465                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
8466                         error "failed to mount client with option '$opt'"
8467                 checksum=$(eval $get_checksum)
8468                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8469                 i=$((i - 1))
8470         done
8471
8472         remount_client $MOUNT || error "failed to remount client"
8473 }
8474 run_test 77k "enable/disable checksum correctly"
8475
8476 test_77l() {
8477         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8478         $GSS && skip_env "could not run with gss"
8479
8480         set_checksums 1
8481         stack_trap "set_checksums $ORIG_CSUM" EXIT
8482         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
8483
8484         set_checksum_type invalid && error "unexpected success of invalid checksum type"
8485
8486         $LFS setstripe -c 1 -i 0 $DIR/$tfile
8487         for algo in $CKSUM_TYPES; do
8488                 set_checksum_type $algo || error "fail to set checksum type $algo"
8489                 osc_algo=$(get_osc_checksum_type OST0000)
8490                 [ "$osc_algo" != "$algo" ] && error "checksum type is $osc_algo after setting it to $algo"
8491
8492                 # no locks, no reqs to let the connection idle
8493                 cancel_lru_locks osc
8494                 lru_resize_disable osc
8495                 wait_osc_import_state client ost1 IDLE
8496
8497                 # ensure ost1 is connected
8498                 stat $DIR/$tfile >/dev/null || error "can't stat"
8499                 wait_osc_import_state client ost1 FULL
8500
8501                 osc_algo=$(get_osc_checksum_type OST0000)
8502                 [ "$osc_algo" != "$algo" ] && error "checksum type changed from $algo to $osc_algo after reconnection"
8503         done
8504         return 0
8505 }
8506 run_test 77l "preferred checksum type is remembered after reconnected"
8507
8508 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
8509 rm -f $F77_TMP
8510 unset F77_TMP
8511
8512 cleanup_test_78() {
8513         trap 0
8514         rm -f $DIR/$tfile
8515 }
8516
8517 test_78() { # bug 10901
8518         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8519         remote_ost || skip_env "local OST"
8520
8521         NSEQ=5
8522         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
8523         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
8524         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
8525         echo "MemTotal: $MEMTOTAL"
8526
8527         # reserve 256MB of memory for the kernel and other running processes,
8528         # and then take 1/2 of the remaining memory for the read/write buffers.
8529         if [ $MEMTOTAL -gt 512 ] ;then
8530                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
8531         else
8532                 # for those poor memory-starved high-end clusters...
8533                 MEMTOTAL=$((MEMTOTAL / 2))
8534         fi
8535         echo "Mem to use for directio: $MEMTOTAL"
8536
8537         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
8538         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
8539         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
8540         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
8541                 head -n1)
8542         echo "Smallest OST: $SMALLESTOST"
8543         [[ $SMALLESTOST -lt 10240 ]] &&
8544                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
8545
8546         trap cleanup_test_78 EXIT
8547
8548         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
8549                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
8550
8551         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
8552         echo "File size: $F78SIZE"
8553         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
8554         for i in $(seq 1 $NSEQ); do
8555                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
8556                 echo directIO rdwr round $i of $NSEQ
8557                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
8558         done
8559
8560         cleanup_test_78
8561 }
8562 run_test 78 "handle large O_DIRECT writes correctly ============"
8563
8564 test_79() { # bug 12743
8565         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8566
8567         wait_delete_completed
8568
8569         BKTOTAL=$(calc_osc_kbytes kbytestotal)
8570         BKFREE=$(calc_osc_kbytes kbytesfree)
8571         BKAVAIL=$(calc_osc_kbytes kbytesavail)
8572
8573         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
8574         DFTOTAL=`echo $STRING | cut -d, -f1`
8575         DFUSED=`echo $STRING  | cut -d, -f2`
8576         DFAVAIL=`echo $STRING | cut -d, -f3`
8577         DFFREE=$(($DFTOTAL - $DFUSED))
8578
8579         ALLOWANCE=$((64 * $OSTCOUNT))
8580
8581         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
8582            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
8583                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
8584         fi
8585         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
8586            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
8587                 error "df free($DFFREE) mismatch OST free($BKFREE)"
8588         fi
8589         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
8590            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
8591                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
8592         fi
8593 }
8594 run_test 79 "df report consistency check ======================="
8595
8596 test_80() { # bug 10718
8597         remote_ost_nodsh && skip "remote OST with nodsh"
8598         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8599
8600         # relax strong synchronous semantics for slow backends like ZFS
8601         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
8602                 local soc="obdfilter.*.sync_lock_cancel"
8603                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8604
8605                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
8606                 if [ -z "$save" ]; then
8607                         soc="obdfilter.*.sync_on_lock_cancel"
8608                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8609                 fi
8610
8611                 if [ "$save" != "never" ]; then
8612                         local hosts=$(comma_list $(osts_nodes))
8613
8614                         do_nodes $hosts $LCTL set_param $soc=never
8615                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
8616                 fi
8617         fi
8618
8619         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
8620         sync; sleep 1; sync
8621         local before=$(date +%s)
8622         cancel_lru_locks osc
8623         local after=$(date +%s)
8624         local diff=$((after - before))
8625         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
8626
8627         rm -f $DIR/$tfile
8628 }
8629 run_test 80 "Page eviction is equally fast at high offsets too"
8630
8631 test_81a() { # LU-456
8632         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8633         remote_ost_nodsh && skip "remote OST with nodsh"
8634
8635         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8636         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
8637         do_facet ost1 lctl set_param fail_loc=0x80000228
8638
8639         # write should trigger a retry and success
8640         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8641         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8642         RC=$?
8643         if [ $RC -ne 0 ] ; then
8644                 error "write should success, but failed for $RC"
8645         fi
8646 }
8647 run_test 81a "OST should retry write when get -ENOSPC ==============="
8648
8649 test_81b() { # LU-456
8650         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8651         remote_ost_nodsh && skip "remote OST with nodsh"
8652
8653         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8654         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
8655         do_facet ost1 lctl set_param fail_loc=0x228
8656
8657         # write should retry several times and return -ENOSPC finally
8658         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8659         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8660         RC=$?
8661         ENOSPC=28
8662         if [ $RC -ne $ENOSPC ] ; then
8663                 error "dd should fail for -ENOSPC, but succeed."
8664         fi
8665 }
8666 run_test 81b "OST should return -ENOSPC when retry still fails ======="
8667
8668 test_82() { # LU-1031
8669         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
8670         local gid1=14091995
8671         local gid2=16022000
8672
8673         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
8674         local MULTIPID1=$!
8675         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
8676         local MULTIPID2=$!
8677         kill -USR1 $MULTIPID2
8678         sleep 2
8679         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
8680                 error "First grouplock does not block second one"
8681         else
8682                 echo "Second grouplock blocks first one"
8683         fi
8684         kill -USR1 $MULTIPID1
8685         wait $MULTIPID1
8686         wait $MULTIPID2
8687 }
8688 run_test 82 "Basic grouplock test"
8689
8690 test_99() {
8691         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
8692
8693         test_mkdir $DIR/$tdir.cvsroot
8694         chown $RUNAS_ID $DIR/$tdir.cvsroot
8695
8696         cd $TMP
8697         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
8698
8699         cd /etc/init.d
8700         # some versions of cvs import exit(1) when asked to import links or
8701         # files they can't read.  ignore those files.
8702         local toignore=$(find . -type l -printf '-I %f\n' -o \
8703                          ! -perm /4 -printf '-I %f\n')
8704         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
8705                 $tdir.reposname vtag rtag
8706
8707         cd $DIR
8708         test_mkdir $DIR/$tdir.reposname
8709         chown $RUNAS_ID $DIR/$tdir.reposname
8710         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
8711
8712         cd $DIR/$tdir.reposname
8713         $RUNAS touch foo99
8714         $RUNAS cvs add -m 'addmsg' foo99
8715         $RUNAS cvs update
8716         $RUNAS cvs commit -m 'nomsg' foo99
8717         rm -fr $DIR/$tdir.cvsroot
8718 }
8719 run_test 99 "cvs strange file/directory operations"
8720
8721 test_100() {
8722         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8723         [[ "$NETTYPE" =~ tcp ]] ||
8724                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
8725         remote_ost_nodsh && skip "remote OST with nodsh"
8726         remote_mds_nodsh && skip "remote MDS with nodsh"
8727         remote_servers ||
8728                 skip "useless for local single node setup"
8729
8730         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
8731                 [ "$PROT" != "tcp" ] && continue
8732                 RPORT=$(echo $REMOTE | cut -d: -f2)
8733                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
8734
8735                 rc=0
8736                 LPORT=`echo $LOCAL | cut -d: -f2`
8737                 if [ $LPORT -ge 1024 ]; then
8738                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
8739                         netstat -tna
8740                         error_exit "local: $LPORT > 1024, remote: $RPORT"
8741                 fi
8742         done
8743         [ "$rc" = 0 ] || error_exit "privileged port not found" )
8744 }
8745 run_test 100 "check local port using privileged port ==========="
8746
8747 function get_named_value()
8748 {
8749     local tag
8750
8751     tag=$1
8752     while read ;do
8753         line=$REPLY
8754         case $line in
8755         $tag*)
8756             echo $line | sed "s/^$tag[ ]*//"
8757             break
8758             ;;
8759         esac
8760     done
8761 }
8762
8763 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
8764                    awk '/^max_cached_mb/ { print $2 }')
8765
8766 cleanup_101a() {
8767         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
8768         trap 0
8769 }
8770
8771 test_101a() {
8772         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8773
8774         local s
8775         local discard
8776         local nreads=10000
8777         local cache_limit=32
8778
8779         $LCTL set_param -n osc.*-osc*.rpc_stats 0
8780         trap cleanup_101a EXIT
8781         $LCTL set_param -n llite.*.read_ahead_stats 0
8782         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
8783
8784         #
8785         # randomly read 10000 of 64K chunks from file 3x 32MB in size
8786         #
8787         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
8788         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
8789
8790         discard=0
8791         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
8792                 get_named_value 'read but discarded' | cut -d" " -f1); do
8793                         discard=$(($discard + $s))
8794         done
8795         cleanup_101a
8796
8797         $LCTL get_param osc.*-osc*.rpc_stats
8798         $LCTL get_param llite.*.read_ahead_stats
8799
8800         # Discard is generally zero, but sometimes a few random reads line up
8801         # and trigger larger readahead, which is wasted & leads to discards.
8802         if [[ $(($discard)) -gt $nreads ]]; then
8803                 error "too many ($discard) discarded pages"
8804         fi
8805         rm -f $DIR/$tfile || true
8806 }
8807 run_test 101a "check read-ahead for random reads"
8808
8809 setup_test101bc() {
8810         test_mkdir $DIR/$tdir
8811         local ssize=$1
8812         local FILE_LENGTH=$2
8813         STRIPE_OFFSET=0
8814
8815         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
8816
8817         local list=$(comma_list $(osts_nodes))
8818         set_osd_param $list '' read_cache_enable 0
8819         set_osd_param $list '' writethrough_cache_enable 0
8820
8821         trap cleanup_test101bc EXIT
8822         # prepare the read-ahead file
8823         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
8824
8825         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
8826                                 count=$FILE_SIZE_MB 2> /dev/null
8827
8828 }
8829
8830 cleanup_test101bc() {
8831         trap 0
8832         rm -rf $DIR/$tdir
8833         rm -f $DIR/$tfile
8834
8835         local list=$(comma_list $(osts_nodes))
8836         set_osd_param $list '' read_cache_enable 1
8837         set_osd_param $list '' writethrough_cache_enable 1
8838 }
8839
8840 calc_total() {
8841         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
8842 }
8843
8844 ra_check_101() {
8845         local READ_SIZE=$1
8846         local STRIPE_SIZE=$2
8847         local FILE_LENGTH=$3
8848         local RA_INC=1048576
8849         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
8850         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
8851                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
8852         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
8853                         get_named_value 'read but discarded' |
8854                         cut -d" " -f1 | calc_total)
8855         if [[ $DISCARD -gt $discard_limit ]]; then
8856                 $LCTL get_param llite.*.read_ahead_stats
8857                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
8858         else
8859                 echo "Read-ahead success for size ${READ_SIZE}"
8860         fi
8861 }
8862
8863 test_101b() {
8864         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8865         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8866
8867         local STRIPE_SIZE=1048576
8868         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
8869
8870         if [ $SLOW == "yes" ]; then
8871                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
8872         else
8873                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
8874         fi
8875
8876         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
8877
8878         # prepare the read-ahead file
8879         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8880         cancel_lru_locks osc
8881         for BIDX in 2 4 8 16 32 64 128 256
8882         do
8883                 local BSIZE=$((BIDX*4096))
8884                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
8885                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
8886                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
8887                 $LCTL set_param -n llite.*.read_ahead_stats 0
8888                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
8889                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
8890                 cancel_lru_locks osc
8891                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
8892         done
8893         cleanup_test101bc
8894         true
8895 }
8896 run_test 101b "check stride-io mode read-ahead ================="
8897
8898 test_101c() {
8899         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8900
8901         local STRIPE_SIZE=1048576
8902         local FILE_LENGTH=$((STRIPE_SIZE*100))
8903         local nreads=10000
8904         local rsize=65536
8905         local osc_rpc_stats
8906
8907         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8908
8909         cancel_lru_locks osc
8910         $LCTL set_param osc.*.rpc_stats 0
8911         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
8912         $LCTL get_param osc.*.rpc_stats
8913         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
8914                 local stats=$($LCTL get_param -n $osc_rpc_stats)
8915                 local lines=$(echo "$stats" | awk 'END {print NR;}')
8916                 local size
8917
8918                 if [ $lines -le 20 ]; then
8919                         echo "continue debug"
8920                         continue
8921                 fi
8922                 for size in 1 2 4 8; do
8923                         local rpc=$(echo "$stats" |
8924                                     awk '($1 == "'$size':") {print $2; exit; }')
8925                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
8926                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
8927                 done
8928                 echo "$osc_rpc_stats check passed!"
8929         done
8930         cleanup_test101bc
8931         true
8932 }
8933 run_test 101c "check stripe_size aligned read-ahead ================="
8934
8935 test_101d() {
8936         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8937
8938         local file=$DIR/$tfile
8939         local sz_MB=${FILESIZE_101d:-500}
8940         local ra_MB=${READAHEAD_MB:-40}
8941
8942         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
8943         [ $free_MB -lt $sz_MB ] &&
8944                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
8945
8946         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
8947         $LFS setstripe -c -1 $file || error "setstripe failed"
8948
8949         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
8950         echo Cancel LRU locks on lustre client to flush the client cache
8951         cancel_lru_locks osc
8952
8953         echo Disable read-ahead
8954         local old_RA=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
8955         $LCTL set_param -n llite.*.max_read_ahead_mb=0
8956         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_RA" EXIT
8957         $LCTL get_param -n llite.*.max_read_ahead_mb
8958
8959         echo Reading the test file $file with read-ahead disabled
8960         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8961
8962         echo Cancel LRU locks on lustre client to flush the client cache
8963         cancel_lru_locks osc
8964         echo Enable read-ahead with ${ra_MB}MB
8965         $LCTL set_param -n llite.*.max_read_ahead_mb=$ra_MB
8966
8967         echo Reading the test file $file with read-ahead enabled
8968         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8969
8970         echo "read-ahead disabled time read $raOFF"
8971         echo "read-ahead enabled  time read $raON"
8972
8973         rm -f $file
8974         wait_delete_completed
8975
8976         [ $raOFF -le 1 ] || [ $raON -lt $raOFF ] ||
8977                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
8978 }
8979 run_test 101d "file read with and without read-ahead enabled"
8980
8981 test_101e() {
8982         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8983
8984         local file=$DIR/$tfile
8985         local size_KB=500  #KB
8986         local count=100
8987         local bsize=1024
8988
8989         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
8990         local need_KB=$((count * size_KB))
8991         [[ $free_KB -le $need_KB ]] &&
8992                 skip_env "Need free space $need_KB, have $free_KB"
8993
8994         echo "Creating $count ${size_KB}K test files"
8995         for ((i = 0; i < $count; i++)); do
8996                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
8997         done
8998
8999         echo "Cancel LRU locks on lustre client to flush the client cache"
9000         cancel_lru_locks $OSC
9001
9002         echo "Reset readahead stats"
9003         $LCTL set_param -n llite.*.read_ahead_stats 0
9004
9005         for ((i = 0; i < $count; i++)); do
9006                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
9007         done
9008
9009         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9010                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
9011
9012         for ((i = 0; i < $count; i++)); do
9013                 rm -rf $file.$i 2>/dev/null
9014         done
9015
9016         #10000 means 20% reads are missing in readahead
9017         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
9018 }
9019 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
9020
9021 test_101f() {
9022         which iozone || skip_env "no iozone installed"
9023
9024         local old_debug=$($LCTL get_param debug)
9025         old_debug=${old_debug#*=}
9026         $LCTL set_param debug="reada mmap"
9027
9028         # create a test file
9029         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
9030
9031         echo Cancel LRU locks on lustre client to flush the client cache
9032         cancel_lru_locks osc
9033
9034         echo Reset readahead stats
9035         $LCTL set_param -n llite.*.read_ahead_stats 0
9036
9037         echo mmap read the file with small block size
9038         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
9039                 > /dev/null 2>&1
9040
9041         echo checking missing pages
9042         $LCTL get_param llite.*.read_ahead_stats
9043         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9044                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9045
9046         $LCTL set_param debug="$old_debug"
9047         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
9048         rm -f $DIR/$tfile
9049 }
9050 run_test 101f "check mmap read performance"
9051
9052 test_101g_brw_size_test() {
9053         local mb=$1
9054         local pages=$((mb * 1048576 / PAGE_SIZE))
9055         local file=$DIR/$tfile
9056
9057         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
9058                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
9059         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
9060                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
9061                         return 2
9062         done
9063
9064         stack_trap "rm -f $file" EXIT
9065         $LCTL set_param -n osc.*.rpc_stats=0
9066
9067         # 10 RPCs should be enough for the test
9068         local count=10
9069         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
9070                 { error "dd write ${mb} MB blocks failed"; return 3; }
9071         cancel_lru_locks osc
9072         dd of=/dev/null if=$file bs=${mb}M count=$count ||
9073                 { error "dd write ${mb} MB blocks failed"; return 4; }
9074
9075         # calculate number of full-sized read and write RPCs
9076         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
9077                 sed -n '/pages per rpc/,/^$/p' |
9078                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
9079                 END { print reads,writes }'))
9080         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
9081                 return 5
9082         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
9083                 return 6
9084
9085         return 0
9086 }
9087
9088 test_101g() {
9089         remote_ost_nodsh && skip "remote OST with nodsh"
9090
9091         local rpcs
9092         local osts=$(get_facets OST)
9093         local list=$(comma_list $(osts_nodes))
9094         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
9095         local brw_size="obdfilter.*.brw_size"
9096
9097         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9098
9099         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
9100
9101         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
9102                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
9103                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
9104            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
9105                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
9106                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
9107
9108                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
9109                         suffix="M"
9110
9111                 if [[ $orig_mb -lt 16 ]]; then
9112                         save_lustre_params $osts "$brw_size" > $p
9113                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
9114                                 error "set 16MB RPC size failed"
9115
9116                         echo "remount client to enable new RPC size"
9117                         remount_client $MOUNT || error "remount_client failed"
9118                 fi
9119
9120                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
9121                 # should be able to set brw_size=12, but no rpc_stats for that
9122                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
9123         fi
9124
9125         test_101g_brw_size_test 4 || error "4MB RPC test failed"
9126
9127         if [[ $orig_mb -lt 16 ]]; then
9128                 restore_lustre_params < $p
9129                 remount_client $MOUNT || error "remount_client restore failed"
9130         fi
9131
9132         rm -f $p $DIR/$tfile
9133 }
9134 run_test 101g "Big bulk(4/16 MiB) readahead"
9135
9136 test_101h() {
9137         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9138
9139         dd if=/dev/zero of=$DIR/$tfile bs=1M count=70 ||
9140                 error "dd 70M file failed"
9141         echo Cancel LRU locks on lustre client to flush the client cache
9142         cancel_lru_locks osc
9143
9144         echo "Reset readahead stats"
9145         $LCTL set_param -n llite.*.read_ahead_stats 0
9146
9147         echo "Read 10M of data but cross 64M bundary"
9148         dd if=$DIR/$tfile of=/dev/null bs=10M skip=6 count=1
9149         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9150                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9151         [ $miss -eq 1 ] || error "expected miss 1 but got $miss"
9152         rm -f $p $DIR/$tfile
9153 }
9154 run_test 101h "Readahead should cover current read window"
9155
9156 setup_test102() {
9157         test_mkdir $DIR/$tdir
9158         chown $RUNAS_ID $DIR/$tdir
9159         STRIPE_SIZE=65536
9160         STRIPE_OFFSET=1
9161         STRIPE_COUNT=$OSTCOUNT
9162         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
9163
9164         trap cleanup_test102 EXIT
9165         cd $DIR
9166         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
9167         cd $DIR/$tdir
9168         for num in 1 2 3 4; do
9169                 for count in $(seq 1 $STRIPE_COUNT); do
9170                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
9171                                 local size=`expr $STRIPE_SIZE \* $num`
9172                                 local file=file"$num-$idx-$count"
9173                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
9174                         done
9175                 done
9176         done
9177
9178         cd $DIR
9179         $1 tar cf $TMP/f102.tar $tdir --xattrs
9180 }
9181
9182 cleanup_test102() {
9183         trap 0
9184         rm -f $TMP/f102.tar
9185         rm -rf $DIR/d0.sanity/d102
9186 }
9187
9188 test_102a() {
9189         [ "$UID" != 0 ] && skip "must run as root"
9190         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
9191                 skip_env "must have user_xattr"
9192
9193         [ -z "$(which setfattr 2>/dev/null)" ] &&
9194                 skip_env "could not find setfattr"
9195
9196         local testfile=$DIR/$tfile
9197
9198         touch $testfile
9199         echo "set/get xattr..."
9200         setfattr -n trusted.name1 -v value1 $testfile ||
9201                 error "setfattr -n trusted.name1=value1 $testfile failed"
9202         getfattr -n trusted.name1 $testfile 2> /dev/null |
9203           grep "trusted.name1=.value1" ||
9204                 error "$testfile missing trusted.name1=value1"
9205
9206         setfattr -n user.author1 -v author1 $testfile ||
9207                 error "setfattr -n user.author1=author1 $testfile failed"
9208         getfattr -n user.author1 $testfile 2> /dev/null |
9209           grep "user.author1=.author1" ||
9210                 error "$testfile missing trusted.author1=author1"
9211
9212         echo "listxattr..."
9213         setfattr -n trusted.name2 -v value2 $testfile ||
9214                 error "$testfile unable to set trusted.name2"
9215         setfattr -n trusted.name3 -v value3 $testfile ||
9216                 error "$testfile unable to set trusted.name3"
9217         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
9218             grep "trusted.name" | wc -l) -eq 3 ] ||
9219                 error "$testfile missing 3 trusted.name xattrs"
9220
9221         setfattr -n user.author2 -v author2 $testfile ||
9222                 error "$testfile unable to set user.author2"
9223         setfattr -n user.author3 -v author3 $testfile ||
9224                 error "$testfile unable to set user.author3"
9225         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
9226             grep "user.author" | wc -l) -eq 3 ] ||
9227                 error "$testfile missing 3 user.author xattrs"
9228
9229         echo "remove xattr..."
9230         setfattr -x trusted.name1 $testfile ||
9231                 error "$testfile error deleting trusted.name1"
9232         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
9233                 error "$testfile did not delete trusted.name1 xattr"
9234
9235         setfattr -x user.author1 $testfile ||
9236                 error "$testfile error deleting user.author1"
9237         echo "set lustre special xattr ..."
9238         $LFS setstripe -c1 $testfile
9239         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
9240                 awk -F "=" '/trusted.lov/ { print $2 }' )
9241         setfattr -n "trusted.lov" -v $lovea $testfile ||
9242                 error "$testfile doesn't ignore setting trusted.lov again"
9243         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
9244                 error "$testfile allow setting invalid trusted.lov"
9245         rm -f $testfile
9246 }
9247 run_test 102a "user xattr test =================================="
9248
9249 test_102b() {
9250         [ -z "$(which setfattr 2>/dev/null)" ] &&
9251                 skip_env "could not find setfattr"
9252         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9253
9254         # b10930: get/set/list trusted.lov xattr
9255         echo "get/set/list trusted.lov xattr ..."
9256         local testfile=$DIR/$tfile
9257         $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
9258                 error "setstripe failed"
9259         local STRIPECOUNT=$($LFS getstripe -c $testfile) ||
9260                 error "getstripe failed"
9261         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
9262                 error "can't get trusted.lov from $testfile"
9263
9264         local testfile2=${testfile}2
9265         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
9266                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
9267
9268         $MCREATE $testfile2
9269         setfattr -n trusted.lov -v $value $testfile2
9270         local stripe_size=$($LFS getstripe -S $testfile2)
9271         local stripe_count=$($LFS getstripe -c $testfile2)
9272         [[ $stripe_size -eq 65536 ]] ||
9273                 error "stripe size $stripe_size != 65536"
9274         [[ $stripe_count -eq $STRIPECOUNT ]] ||
9275                 error "stripe count $stripe_count != $STRIPECOUNT"
9276         rm -f $DIR/$tfile
9277 }
9278 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
9279
9280 test_102c() {
9281         [ -z "$(which setfattr 2>/dev/null)" ] &&
9282                 skip_env "could not find setfattr"
9283         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9284
9285         # b10930: get/set/list lustre.lov xattr
9286         echo "get/set/list lustre.lov xattr ..."
9287         test_mkdir $DIR/$tdir
9288         chown $RUNAS_ID $DIR/$tdir
9289         local testfile=$DIR/$tdir/$tfile
9290         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
9291                 error "setstripe failed"
9292         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
9293                 error "getstripe failed"
9294         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
9295         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
9296
9297         local testfile2=${testfile}2
9298         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
9299                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
9300
9301         $RUNAS $MCREATE $testfile2
9302         $RUNAS setfattr -n lustre.lov -v $value $testfile2
9303         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
9304         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
9305         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
9306         [ $stripe_count -eq $STRIPECOUNT ] ||
9307                 error "stripe count $stripe_count != $STRIPECOUNT"
9308 }
9309 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
9310
9311 compare_stripe_info1() {
9312         local stripe_index_all_zero=true
9313
9314         for num in 1 2 3 4; do
9315                 for count in $(seq 1 $STRIPE_COUNT); do
9316                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
9317                                 local size=$((STRIPE_SIZE * num))
9318                                 local file=file"$num-$offset-$count"
9319                                 stripe_size=$($LFS getstripe -S $PWD/$file)
9320                                 [[ $stripe_size -ne $size ]] &&
9321                                     error "$file: size $stripe_size != $size"
9322                                 stripe_count=$($LFS getstripe -c $PWD/$file)
9323                                 # allow fewer stripes to be created, ORI-601
9324                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
9325                                     error "$file: count $stripe_count != $count"
9326                                 stripe_index=$($LFS getstripe -i $PWD/$file)
9327                                 [[ $stripe_index -ne 0 ]] &&
9328                                         stripe_index_all_zero=false
9329                         done
9330                 done
9331         done
9332         $stripe_index_all_zero &&
9333                 error "all files are being extracted starting from OST index 0"
9334         return 0
9335 }
9336
9337 have_xattrs_include() {
9338         tar --help | grep -q xattrs-include &&
9339                 echo --xattrs-include="lustre.*"
9340 }
9341
9342 test_102d() {
9343         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9344         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9345
9346         XINC=$(have_xattrs_include)
9347         setup_test102
9348         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9349         cd $DIR/$tdir/$tdir
9350         compare_stripe_info1
9351 }
9352 run_test 102d "tar restore stripe info from tarfile,not keep osts"
9353
9354 test_102f() {
9355         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9356         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9357
9358         XINC=$(have_xattrs_include)
9359         setup_test102
9360         test_mkdir $DIR/$tdir.restore
9361         cd $DIR
9362         tar cf - --xattrs $tdir | tar xf - \
9363                 -C $DIR/$tdir.restore --xattrs $XINC
9364         cd $DIR/$tdir.restore/$tdir
9365         compare_stripe_info1
9366 }
9367 run_test 102f "tar copy files, not keep osts"
9368
9369 grow_xattr() {
9370         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
9371                 skip "must have user_xattr"
9372         [ -z "$(which setfattr 2>/dev/null)" ] &&
9373                 skip_env "could not find setfattr"
9374         [ -z "$(which getfattr 2>/dev/null)" ] &&
9375                 skip_env "could not find getfattr"
9376
9377         local xsize=${1:-1024}  # in bytes
9378         local file=$DIR/$tfile
9379         local value="$(generate_string $xsize)"
9380         local xbig=trusted.big
9381         local toobig=$2
9382
9383         touch $file
9384         log "save $xbig on $file"
9385         if [ -z "$toobig" ]
9386         then
9387                 setfattr -n $xbig -v $value $file ||
9388                         error "saving $xbig on $file failed"
9389         else
9390                 setfattr -n $xbig -v $value $file &&
9391                         error "saving $xbig on $file succeeded"
9392                 return 0
9393         fi
9394
9395         local orig=$(get_xattr_value $xbig $file)
9396         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
9397
9398         local xsml=trusted.sml
9399         log "save $xsml on $file"
9400         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
9401
9402         local new=$(get_xattr_value $xbig $file)
9403         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
9404
9405         log "grow $xsml on $file"
9406         setfattr -n $xsml -v "$value" $file ||
9407                 error "growing $xsml on $file failed"
9408
9409         new=$(get_xattr_value $xbig $file)
9410         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
9411         log "$xbig still valid after growing $xsml"
9412
9413         rm -f $file
9414 }
9415
9416 test_102h() { # bug 15777
9417         grow_xattr 1024
9418 }
9419 run_test 102h "grow xattr from inside inode to external block"
9420
9421 test_102ha() {
9422         large_xattr_enabled || skip_env "ea_inode feature disabled"
9423
9424         echo "setting xattr of max xattr size: $(max_xattr_size)"
9425         grow_xattr $(max_xattr_size)
9426
9427         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
9428         echo "This should fail:"
9429         grow_xattr $(($(max_xattr_size) + 10)) 1
9430 }
9431 run_test 102ha "grow xattr from inside inode to external inode"
9432
9433 test_102i() { # bug 17038
9434         [ -z "$(which getfattr 2>/dev/null)" ] &&
9435                 skip "could not find getfattr"
9436
9437         touch $DIR/$tfile
9438         ln -s $DIR/$tfile $DIR/${tfile}link
9439         getfattr -n trusted.lov $DIR/$tfile ||
9440                 error "lgetxattr on $DIR/$tfile failed"
9441         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
9442                 grep -i "no such attr" ||
9443                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
9444         rm -f $DIR/$tfile $DIR/${tfile}link
9445 }
9446 run_test 102i "lgetxattr test on symbolic link ============"
9447
9448 test_102j() {
9449         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9450         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9451
9452         XINC=$(have_xattrs_include)
9453         setup_test102 "$RUNAS"
9454         chown $RUNAS_ID $DIR/$tdir
9455         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9456         cd $DIR/$tdir/$tdir
9457         compare_stripe_info1 "$RUNAS"
9458 }
9459 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
9460
9461 test_102k() {
9462         [ -z "$(which setfattr 2>/dev/null)" ] &&
9463                 skip "could not find setfattr"
9464
9465         touch $DIR/$tfile
9466         # b22187 just check that does not crash for regular file.
9467         setfattr -n trusted.lov $DIR/$tfile
9468         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
9469         local test_kdir=$DIR/$tdir
9470         test_mkdir $test_kdir
9471         local default_size=$($LFS getstripe -S $test_kdir)
9472         local default_count=$($LFS getstripe -c $test_kdir)
9473         local default_offset=$($LFS getstripe -i $test_kdir)
9474         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
9475                 error 'dir setstripe failed'
9476         setfattr -n trusted.lov $test_kdir
9477         local stripe_size=$($LFS getstripe -S $test_kdir)
9478         local stripe_count=$($LFS getstripe -c $test_kdir)
9479         local stripe_offset=$($LFS getstripe -i $test_kdir)
9480         [ $stripe_size -eq $default_size ] ||
9481                 error "stripe size $stripe_size != $default_size"
9482         [ $stripe_count -eq $default_count ] ||
9483                 error "stripe count $stripe_count != $default_count"
9484         [ $stripe_offset -eq $default_offset ] ||
9485                 error "stripe offset $stripe_offset != $default_offset"
9486         rm -rf $DIR/$tfile $test_kdir
9487 }
9488 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
9489
9490 test_102l() {
9491         [ -z "$(which getfattr 2>/dev/null)" ] &&
9492                 skip "could not find getfattr"
9493
9494         # LU-532 trusted. xattr is invisible to non-root
9495         local testfile=$DIR/$tfile
9496
9497         touch $testfile
9498
9499         echo "listxattr as user..."
9500         chown $RUNAS_ID $testfile
9501         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
9502             grep -q "trusted" &&
9503                 error "$testfile trusted xattrs are user visible"
9504
9505         return 0;
9506 }
9507 run_test 102l "listxattr size test =================================="
9508
9509 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
9510         local path=$DIR/$tfile
9511         touch $path
9512
9513         listxattr_size_check $path || error "listattr_size_check $path failed"
9514 }
9515 run_test 102m "Ensure listxattr fails on small bufffer ========"
9516
9517 cleanup_test102
9518
9519 getxattr() { # getxattr path name
9520         # Return the base64 encoding of the value of xattr name on path.
9521         local path=$1
9522         local name=$2
9523
9524         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
9525         # file: $path
9526         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9527         #
9528         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9529
9530         getfattr --absolute-names --encoding=base64 --name=$name $path |
9531                 awk -F= -v name=$name '$1 == name {
9532                         print substr($0, index($0, "=") + 1);
9533         }'
9534 }
9535
9536 test_102n() { # LU-4101 mdt: protect internal xattrs
9537         [ -z "$(which setfattr 2>/dev/null)" ] &&
9538                 skip "could not find setfattr"
9539         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
9540         then
9541                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
9542         fi
9543
9544         local file0=$DIR/$tfile.0
9545         local file1=$DIR/$tfile.1
9546         local xattr0=$TMP/$tfile.0
9547         local xattr1=$TMP/$tfile.1
9548         local namelist="lov lma lmv link fid version som hsm"
9549         local name
9550         local value
9551
9552         rm -rf $file0 $file1 $xattr0 $xattr1
9553         touch $file0 $file1
9554
9555         # Get 'before' xattrs of $file1.
9556         getfattr --absolute-names --dump --match=- $file1 > $xattr0
9557
9558         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
9559                 namelist+=" lfsck_namespace"
9560         for name in $namelist; do
9561                 # Try to copy xattr from $file0 to $file1.
9562                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9563
9564                 setfattr --name=trusted.$name --value="$value" $file1 ||
9565                         error "setxattr 'trusted.$name' failed"
9566
9567                 # Try to set a garbage xattr.
9568                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9569
9570                 if [[ x$name == "xlov" ]]; then
9571                         setfattr --name=trusted.lov --value="$value" $file1 &&
9572                         error "setxattr invalid 'trusted.lov' success"
9573                 else
9574                         setfattr --name=trusted.$name --value="$value" $file1 ||
9575                                 error "setxattr invalid 'trusted.$name' failed"
9576                 fi
9577
9578                 # Try to remove the xattr from $file1. We don't care if this
9579                 # appears to succeed or fail, we just don't want there to be
9580                 # any changes or crashes.
9581                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9582         done
9583
9584         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
9585         then
9586                 name="lfsck_ns"
9587                 # Try to copy xattr from $file0 to $file1.
9588                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9589
9590                 setfattr --name=trusted.$name --value="$value" $file1 ||
9591                         error "setxattr 'trusted.$name' failed"
9592
9593                 # Try to set a garbage xattr.
9594                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9595
9596                 setfattr --name=trusted.$name --value="$value" $file1 ||
9597                         error "setxattr 'trusted.$name' failed"
9598
9599                 # Try to remove the xattr from $file1. We don't care if this
9600                 # appears to succeed or fail, we just don't want there to be
9601                 # any changes or crashes.
9602                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9603         fi
9604
9605         # Get 'after' xattrs of file1.
9606         getfattr --absolute-names --dump --match=- $file1 > $xattr1
9607
9608         if ! diff $xattr0 $xattr1; then
9609                 error "before and after xattrs of '$file1' differ"
9610         fi
9611
9612         rm -rf $file0 $file1 $xattr0 $xattr1
9613
9614         return 0
9615 }
9616 run_test 102n "silently ignore setxattr on internal trusted xattrs"
9617
9618 test_102p() { # LU-4703 setxattr did not check ownership
9619         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
9620                 skip "MDS needs to be at least 2.5.56"
9621
9622         local testfile=$DIR/$tfile
9623
9624         touch $testfile
9625
9626         echo "setfacl as user..."
9627         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
9628         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
9629
9630         echo "setfattr as user..."
9631         setfacl -m "u:$RUNAS_ID:---" $testfile
9632         $RUNAS setfattr -x system.posix_acl_access $testfile
9633         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
9634 }
9635 run_test 102p "check setxattr(2) correctly fails without permission"
9636
9637 test_102q() {
9638         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
9639                 skip "MDS needs to be at least 2.6.92"
9640
9641         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
9642 }
9643 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
9644
9645 test_102r() {
9646         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
9647                 skip "MDS needs to be at least 2.6.93"
9648
9649         touch $DIR/$tfile || error "touch"
9650         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
9651         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
9652         rm $DIR/$tfile || error "rm"
9653
9654         #normal directory
9655         mkdir -p $DIR/$tdir || error "mkdir"
9656         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9657         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9658         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9659                 error "$testfile error deleting user.author1"
9660         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9661                 grep "user.$(basename $tdir)" &&
9662                 error "$tdir did not delete user.$(basename $tdir)"
9663         rmdir $DIR/$tdir || error "rmdir"
9664
9665         #striped directory
9666         test_mkdir $DIR/$tdir
9667         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9668         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9669         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9670                 error "$testfile error deleting user.author1"
9671         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9672                 grep "user.$(basename $tdir)" &&
9673                 error "$tdir did not delete user.$(basename $tdir)"
9674         rmdir $DIR/$tdir || error "rm striped dir"
9675 }
9676 run_test 102r "set EAs with empty values"
9677
9678 test_102s() {
9679         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9680                 skip "MDS needs to be at least 2.11.52"
9681
9682         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9683
9684         save_lustre_params client "llite.*.xattr_cache" > $save
9685
9686         for cache in 0 1; do
9687                 lctl set_param llite.*.xattr_cache=$cache
9688
9689                 rm -f $DIR/$tfile
9690                 touch $DIR/$tfile || error "touch"
9691                 for prefix in lustre security system trusted user; do
9692                         # Note getxattr() may fail with 'Operation not
9693                         # supported' or 'No such attribute' depending
9694                         # on prefix and cache.
9695                         getfattr -n $prefix.n102s $DIR/$tfile &&
9696                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
9697                 done
9698         done
9699
9700         restore_lustre_params < $save
9701 }
9702 run_test 102s "getting nonexistent xattrs should fail"
9703
9704 test_102t() {
9705         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9706                 skip "MDS needs to be at least 2.11.52"
9707
9708         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9709
9710         save_lustre_params client "llite.*.xattr_cache" > $save
9711
9712         for cache in 0 1; do
9713                 lctl set_param llite.*.xattr_cache=$cache
9714
9715                 for buf_size in 0 256; do
9716                         rm -f $DIR/$tfile
9717                         touch $DIR/$tfile || error "touch"
9718                         setfattr -n user.multiop $DIR/$tfile
9719                         $MULTIOP $DIR/$tfile oa$buf_size ||
9720                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
9721                 done
9722         done
9723
9724         restore_lustre_params < $save
9725 }
9726 run_test 102t "zero length xattr values handled correctly"
9727
9728 run_acl_subtest()
9729 {
9730     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
9731     return $?
9732 }
9733
9734 test_103a() {
9735         [ "$UID" != 0 ] && skip "must run as root"
9736         $GSS && skip_env "could not run under gss"
9737         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
9738                 skip_env "must have acl enabled"
9739         [ -z "$(which setfacl 2>/dev/null)" ] &&
9740                 skip_env "could not find setfacl"
9741         remote_mds_nodsh && skip "remote MDS with nodsh"
9742
9743         gpasswd -a daemon bin                           # LU-5641
9744         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
9745
9746         declare -a identity_old
9747
9748         for num in $(seq $MDSCOUNT); do
9749                 switch_identity $num true || identity_old[$num]=$?
9750         done
9751
9752         SAVE_UMASK=$(umask)
9753         umask 0022
9754         mkdir -p $DIR/$tdir
9755         cd $DIR/$tdir
9756
9757         echo "performing cp ..."
9758         run_acl_subtest cp || error "run_acl_subtest cp failed"
9759         echo "performing getfacl-noacl..."
9760         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
9761         echo "performing misc..."
9762         run_acl_subtest misc || error  "misc test failed"
9763         echo "performing permissions..."
9764         run_acl_subtest permissions || error "permissions failed"
9765         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
9766         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
9767                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
9768                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
9769         then
9770                 echo "performing permissions xattr..."
9771                 run_acl_subtest permissions_xattr ||
9772                         error "permissions_xattr failed"
9773         fi
9774         echo "performing setfacl..."
9775         run_acl_subtest setfacl || error  "setfacl test failed"
9776
9777         # inheritance test got from HP
9778         echo "performing inheritance..."
9779         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
9780         chmod +x make-tree || error "chmod +x failed"
9781         run_acl_subtest inheritance || error "inheritance test failed"
9782         rm -f make-tree
9783
9784         echo "LU-974 ignore umask when acl is enabled..."
9785         run_acl_subtest 974 || error "LU-974 umask test failed"
9786         if [ $MDSCOUNT -ge 2 ]; then
9787                 run_acl_subtest 974_remote ||
9788                         error "LU-974 umask test failed under remote dir"
9789         fi
9790
9791         echo "LU-2561 newly created file is same size as directory..."
9792         if [ "$mds1_FSTYPE" != "zfs" ]; then
9793                 run_acl_subtest 2561 || error "LU-2561 test failed"
9794         else
9795                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
9796         fi
9797
9798         run_acl_subtest 4924 || error "LU-4924 test failed"
9799
9800         cd $SAVE_PWD
9801         umask $SAVE_UMASK
9802
9803         for num in $(seq $MDSCOUNT); do
9804                 if [ "${identity_old[$num]}" = 1 ]; then
9805                         switch_identity $num false || identity_old[$num]=$?
9806                 fi
9807         done
9808 }
9809 run_test 103a "acl test"
9810
9811 test_103b() {
9812         declare -a pids
9813         local U
9814
9815         for U in {0..511}; do
9816                 {
9817                 local O=$(printf "%04o" $U)
9818
9819                 umask $(printf "%04o" $((511 ^ $O)))
9820                 $LFS setstripe -c 1 $DIR/$tfile.s$O
9821                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
9822
9823                 (( $S == ($O & 0666) )) ||
9824                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
9825
9826                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
9827                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
9828                 (( $S == ($O & 0666) )) ||
9829                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
9830
9831                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
9832                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
9833                 (( $S == ($O & 0666) )) ||
9834                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
9835                 rm -f $DIR/$tfile.[smp]$0
9836                 } &
9837                 local pid=$!
9838
9839                 # limit the concurrently running threads to 64. LU-11878
9840                 local idx=$((U % 64))
9841                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
9842                 pids[idx]=$pid
9843         done
9844         wait
9845 }
9846 run_test 103b "umask lfs setstripe"
9847
9848 test_103c() {
9849         mkdir -p $DIR/$tdir
9850         cp -rp $DIR/$tdir $DIR/$tdir.bak
9851
9852         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
9853                 error "$DIR/$tdir shouldn't contain default ACL"
9854         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
9855                 error "$DIR/$tdir.bak shouldn't contain default ACL"
9856         true
9857 }
9858 run_test 103c "'cp -rp' won't set empty acl"
9859
9860 test_104a() {
9861         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9862
9863         touch $DIR/$tfile
9864         lfs df || error "lfs df failed"
9865         lfs df -ih || error "lfs df -ih failed"
9866         lfs df -h $DIR || error "lfs df -h $DIR failed"
9867         lfs df -i $DIR || error "lfs df -i $DIR failed"
9868         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
9869         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
9870
9871         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
9872         lctl --device %$OSC deactivate
9873         lfs df || error "lfs df with deactivated OSC failed"
9874         lctl --device %$OSC activate
9875         # wait the osc back to normal
9876         wait_osc_import_ready client ost
9877
9878         lfs df || error "lfs df with reactivated OSC failed"
9879         rm -f $DIR/$tfile
9880 }
9881 run_test 104a "lfs df [-ih] [path] test ========================="
9882
9883 test_104b() {
9884         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9885         [ $RUNAS_ID -eq $UID ] &&
9886                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9887
9888         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
9889                         grep "Permission denied" | wc -l)))
9890         if [ $denied_cnt -ne 0 ]; then
9891                 error "lfs check servers test failed"
9892         fi
9893 }
9894 run_test 104b "$RUNAS lfs check servers test ===================="
9895
9896 test_105a() {
9897         # doesn't work on 2.4 kernels
9898         touch $DIR/$tfile
9899         if $(flock_is_enabled); then
9900                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
9901         else
9902                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
9903         fi
9904         rm -f $DIR/$tfile
9905 }
9906 run_test 105a "flock when mounted without -o flock test ========"
9907
9908 test_105b() {
9909         touch $DIR/$tfile
9910         if $(flock_is_enabled); then
9911                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
9912         else
9913                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
9914         fi
9915         rm -f $DIR/$tfile
9916 }
9917 run_test 105b "fcntl when mounted without -o flock test ========"
9918
9919 test_105c() {
9920         touch $DIR/$tfile
9921         if $(flock_is_enabled); then
9922                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
9923         else
9924                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
9925         fi
9926         rm -f $DIR/$tfile
9927 }
9928 run_test 105c "lockf when mounted without -o flock test"
9929
9930 test_105d() { # bug 15924
9931         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9932
9933         test_mkdir $DIR/$tdir
9934         flock_is_enabled || skip_env "mount w/o flock enabled"
9935         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
9936         $LCTL set_param fail_loc=0x80000315
9937         flocks_test 2 $DIR/$tdir
9938 }
9939 run_test 105d "flock race (should not freeze) ========"
9940
9941 test_105e() { # bug 22660 && 22040
9942         flock_is_enabled || skip_env "mount w/o flock enabled"
9943
9944         touch $DIR/$tfile
9945         flocks_test 3 $DIR/$tfile
9946 }
9947 run_test 105e "Two conflicting flocks from same process"
9948
9949 test_106() { #bug 10921
9950         test_mkdir $DIR/$tdir
9951         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
9952         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
9953 }
9954 run_test 106 "attempt exec of dir followed by chown of that dir"
9955
9956 test_107() {
9957         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9958
9959         CDIR=`pwd`
9960         local file=core
9961
9962         cd $DIR
9963         rm -f $file
9964
9965         local save_pattern=$(sysctl -n kernel.core_pattern)
9966         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
9967         sysctl -w kernel.core_pattern=$file
9968         sysctl -w kernel.core_uses_pid=0
9969
9970         ulimit -c unlimited
9971         sleep 60 &
9972         SLEEPPID=$!
9973
9974         sleep 1
9975
9976         kill -s 11 $SLEEPPID
9977         wait $SLEEPPID
9978         if [ -e $file ]; then
9979                 size=`stat -c%s $file`
9980                 [ $size -eq 0 ] && error "Fail to create core file $file"
9981         else
9982                 error "Fail to create core file $file"
9983         fi
9984         rm -f $file
9985         sysctl -w kernel.core_pattern=$save_pattern
9986         sysctl -w kernel.core_uses_pid=$save_uses_pid
9987         cd $CDIR
9988 }
9989 run_test 107 "Coredump on SIG"
9990
9991 test_110() {
9992         test_mkdir $DIR/$tdir
9993         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
9994         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
9995                 error "mkdir with 256 char should fail, but did not"
9996         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
9997                 error "create with 255 char failed"
9998         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
9999                 error "create with 256 char should fail, but did not"
10000
10001         ls -l $DIR/$tdir
10002         rm -rf $DIR/$tdir
10003 }
10004 run_test 110 "filename length checking"
10005
10006 #
10007 # Purpose: To verify dynamic thread (OSS) creation.
10008 #
10009 test_115() {
10010         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10011         remote_ost_nodsh && skip "remote OST with nodsh"
10012
10013         # Lustre does not stop service threads once they are started.
10014         # Reset number of running threads to default.
10015         stopall
10016         setupall
10017
10018         local OSTIO_pre
10019         local save_params="$TMP/sanity-$TESTNAME.parameters"
10020
10021         # Get ll_ost_io count before I/O
10022         OSTIO_pre=$(do_facet ost1 \
10023                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
10024         # Exit if lustre is not running (ll_ost_io not running).
10025         [ -z "$OSTIO_pre" ] && error "no OSS threads"
10026
10027         echo "Starting with $OSTIO_pre threads"
10028         local thread_max=$((OSTIO_pre * 2))
10029         local rpc_in_flight=$((thread_max * 2))
10030         # Number of I/O Process proposed to be started.
10031         local nfiles
10032         local facets=$(get_facets OST)
10033
10034         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
10035         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
10036
10037         # Set in_flight to $rpc_in_flight
10038         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
10039                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
10040         nfiles=${rpc_in_flight}
10041         # Set ost thread_max to $thread_max
10042         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
10043
10044         # 5 Minutes should be sufficient for max number of OSS
10045         # threads(thread_max) to be created.
10046         local timeout=300
10047
10048         # Start I/O.
10049         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
10050         test_mkdir $DIR/$tdir
10051         for i in $(seq $nfiles); do
10052                 local file=$DIR/$tdir/${tfile}-$i
10053                 $LFS setstripe -c -1 -i 0 $file
10054                 ($WTL $file $timeout)&
10055         done
10056
10057         # I/O Started - Wait for thread_started to reach thread_max or report
10058         # error if thread_started is more than thread_max.
10059         echo "Waiting for thread_started to reach thread_max"
10060         local thread_started=0
10061         local end_time=$((SECONDS + timeout))
10062
10063         while [ $SECONDS -le $end_time ] ; do
10064                 echo -n "."
10065                 # Get ost i/o thread_started count.
10066                 thread_started=$(do_facet ost1 \
10067                         "$LCTL get_param \
10068                         ost.OSS.ost_io.threads_started | cut -d= -f2")
10069                 # Break out if thread_started is equal/greater than thread_max
10070                 if [[ $thread_started -ge $thread_max ]]; then
10071                         echo ll_ost_io thread_started $thread_started, \
10072                                 equal/greater than thread_max $thread_max
10073                         break
10074                 fi
10075                 sleep 1
10076         done
10077
10078         # Cleanup - We have the numbers, Kill i/o jobs if running.
10079         jobcount=($(jobs -p))
10080         for i in $(seq 0 $((${#jobcount[@]}-1)))
10081         do
10082                 kill -9 ${jobcount[$i]}
10083                 if [ $? -ne 0 ] ; then
10084                         echo Warning: \
10085                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
10086                 fi
10087         done
10088
10089         # Cleanup files left by WTL binary.
10090         for i in $(seq $nfiles); do
10091                 local file=$DIR/$tdir/${tfile}-$i
10092                 rm -rf $file
10093                 if [ $? -ne 0 ] ; then
10094                         echo "Warning: Failed to delete file $file"
10095                 fi
10096         done
10097
10098         restore_lustre_params <$save_params
10099         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
10100
10101         # Error out if no new thread has started or Thread started is greater
10102         # than thread max.
10103         if [[ $thread_started -le $OSTIO_pre ||
10104                         $thread_started -gt $thread_max ]]; then
10105                 error "ll_ost_io: thread_started $thread_started" \
10106                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
10107                       "No new thread started or thread started greater " \
10108                       "than thread_max."
10109         fi
10110 }
10111 run_test 115 "verify dynamic thread creation===================="
10112
10113 free_min_max () {
10114         wait_delete_completed
10115         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
10116         echo "OST kbytes available: ${AVAIL[@]}"
10117         MAXV=${AVAIL[0]}
10118         MAXI=0
10119         MINV=${AVAIL[0]}
10120         MINI=0
10121         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
10122                 #echo OST $i: ${AVAIL[i]}kb
10123                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
10124                         MAXV=${AVAIL[i]}
10125                         MAXI=$i
10126                 fi
10127                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
10128                         MINV=${AVAIL[i]}
10129                         MINI=$i
10130                 fi
10131         done
10132         echo "Min free space: OST $MINI: $MINV"
10133         echo "Max free space: OST $MAXI: $MAXV"
10134 }
10135
10136 test_116a() { # was previously test_116()
10137         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10138         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10139         remote_mds_nodsh && skip "remote MDS with nodsh"
10140
10141         echo -n "Free space priority "
10142         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
10143                 head -n1
10144         declare -a AVAIL
10145         free_min_max
10146
10147         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
10148         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
10149         trap simple_cleanup_common EXIT
10150
10151         # Check if we need to generate uneven OSTs
10152         test_mkdir -p $DIR/$tdir/OST${MINI}
10153         local FILL=$((MINV / 4))
10154         local DIFF=$((MAXV - MINV))
10155         local DIFF2=$((DIFF * 100 / MINV))
10156
10157         local threshold=$(do_facet $SINGLEMDS \
10158                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
10159         threshold=${threshold%%%}
10160         echo -n "Check for uneven OSTs: "
10161         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
10162
10163         if [[ $DIFF2 -gt $threshold ]]; then
10164                 echo "ok"
10165                 echo "Don't need to fill OST$MINI"
10166         else
10167                 # generate uneven OSTs. Write 2% over the QOS threshold value
10168                 echo "no"
10169                 DIFF=$((threshold - DIFF2 + 2))
10170                 DIFF2=$((MINV * DIFF / 100))
10171                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
10172                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
10173                         error "setstripe failed"
10174                 DIFF=$((DIFF2 / 2048))
10175                 i=0
10176                 while [ $i -lt $DIFF ]; do
10177                         i=$((i + 1))
10178                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
10179                                 bs=2M count=1 2>/dev/null
10180                         echo -n .
10181                 done
10182                 echo .
10183                 sync
10184                 sleep_maxage
10185                 free_min_max
10186         fi
10187
10188         DIFF=$((MAXV - MINV))
10189         DIFF2=$((DIFF * 100 / MINV))
10190         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
10191         if [ $DIFF2 -gt $threshold ]; then
10192                 echo "ok"
10193         else
10194                 echo "failed - QOS mode won't be used"
10195                 simple_cleanup_common
10196                 skip "QOS imbalance criteria not met"
10197         fi
10198
10199         MINI1=$MINI
10200         MINV1=$MINV
10201         MAXI1=$MAXI
10202         MAXV1=$MAXV
10203
10204         # now fill using QOS
10205         $LFS setstripe -c 1 $DIR/$tdir
10206         FILL=$((FILL / 200))
10207         if [ $FILL -gt 600 ]; then
10208                 FILL=600
10209         fi
10210         echo "writing $FILL files to QOS-assigned OSTs"
10211         i=0
10212         while [ $i -lt $FILL ]; do
10213                 i=$((i + 1))
10214                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
10215                         count=1 2>/dev/null
10216                 echo -n .
10217         done
10218         echo "wrote $i 200k files"
10219         sync
10220         sleep_maxage
10221
10222         echo "Note: free space may not be updated, so measurements might be off"
10223         free_min_max
10224         DIFF2=$((MAXV - MINV))
10225         echo "free space delta: orig $DIFF final $DIFF2"
10226         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
10227         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
10228         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
10229         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
10230         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
10231         if [[ $DIFF -gt 0 ]]; then
10232                 FILL=$((DIFF2 * 100 / DIFF - 100))
10233                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
10234         fi
10235
10236         # Figure out which files were written where
10237         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10238                awk '/'$MINI1': / {print $2; exit}')
10239         echo $UUID
10240         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10241         echo "$MINC files created on smaller OST $MINI1"
10242         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10243                awk '/'$MAXI1': / {print $2; exit}')
10244         echo $UUID
10245         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10246         echo "$MAXC files created on larger OST $MAXI1"
10247         if [[ $MINC -gt 0 ]]; then
10248                 FILL=$((MAXC * 100 / MINC - 100))
10249                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
10250         fi
10251         [[ $MAXC -gt $MINC ]] ||
10252                 error_ignore LU-9 "stripe QOS didn't balance free space"
10253         simple_cleanup_common
10254 }
10255 run_test 116a "stripe QOS: free space balance ==================="
10256
10257 test_116b() { # LU-2093
10258         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10259         remote_mds_nodsh && skip "remote MDS with nodsh"
10260
10261 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
10262         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
10263                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
10264         [ -z "$old_rr" ] && skip "no QOS"
10265         do_facet $SINGLEMDS lctl set_param \
10266                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
10267         mkdir -p $DIR/$tdir
10268         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
10269         createmany -o $DIR/$tdir/f- 20 || error "can't create"
10270         do_facet $SINGLEMDS lctl set_param fail_loc=0
10271         rm -rf $DIR/$tdir
10272         do_facet $SINGLEMDS lctl set_param \
10273                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
10274 }
10275 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
10276
10277 test_117() # bug 10891
10278 {
10279         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10280
10281         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
10282         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
10283         lctl set_param fail_loc=0x21e
10284         > $DIR/$tfile || error "truncate failed"
10285         lctl set_param fail_loc=0
10286         echo "Truncate succeeded."
10287         rm -f $DIR/$tfile
10288 }
10289 run_test 117 "verify osd extend =========="
10290
10291 NO_SLOW_RESENDCOUNT=4
10292 export OLD_RESENDCOUNT=""
10293 set_resend_count () {
10294         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
10295         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
10296         lctl set_param -n $PROC_RESENDCOUNT $1
10297         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
10298 }
10299
10300 # for reduce test_118* time (b=14842)
10301 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10302
10303 # Reset async IO behavior after error case
10304 reset_async() {
10305         FILE=$DIR/reset_async
10306
10307         # Ensure all OSCs are cleared
10308         $LFS setstripe -c -1 $FILE
10309         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
10310         sync
10311         rm $FILE
10312 }
10313
10314 test_118a() #bug 11710
10315 {
10316         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10317
10318         reset_async
10319
10320         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10321         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10322         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10323
10324         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10325                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10326                 return 1;
10327         fi
10328         rm -f $DIR/$tfile
10329 }
10330 run_test 118a "verify O_SYNC works =========="
10331
10332 test_118b()
10333 {
10334         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10335         remote_ost_nodsh && skip "remote OST with nodsh"
10336
10337         reset_async
10338
10339         #define OBD_FAIL_SRV_ENOENT 0x217
10340         set_nodes_failloc "$(osts_nodes)" 0x217
10341         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10342         RC=$?
10343         set_nodes_failloc "$(osts_nodes)" 0
10344         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10345         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10346                     grep -c writeback)
10347
10348         if [[ $RC -eq 0 ]]; then
10349                 error "Must return error due to dropped pages, rc=$RC"
10350                 return 1;
10351         fi
10352
10353         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10354                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10355                 return 1;
10356         fi
10357
10358         echo "Dirty pages not leaked on ENOENT"
10359
10360         # Due to the above error the OSC will issue all RPCs syncronously
10361         # until a subsequent RPC completes successfully without error.
10362         $MULTIOP $DIR/$tfile Ow4096yc
10363         rm -f $DIR/$tfile
10364
10365         return 0
10366 }
10367 run_test 118b "Reclaim dirty pages on fatal error =========="
10368
10369 test_118c()
10370 {
10371         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10372
10373         # for 118c, restore the original resend count, LU-1940
10374         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
10375                                 set_resend_count $OLD_RESENDCOUNT
10376         remote_ost_nodsh && skip "remote OST with nodsh"
10377
10378         reset_async
10379
10380         #define OBD_FAIL_OST_EROFS               0x216
10381         set_nodes_failloc "$(osts_nodes)" 0x216
10382
10383         # multiop should block due to fsync until pages are written
10384         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10385         MULTIPID=$!
10386         sleep 1
10387
10388         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10389                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10390         fi
10391
10392         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10393                     grep -c writeback)
10394         if [[ $WRITEBACK -eq 0 ]]; then
10395                 error "No page in writeback, writeback=$WRITEBACK"
10396         fi
10397
10398         set_nodes_failloc "$(osts_nodes)" 0
10399         wait $MULTIPID
10400         RC=$?
10401         if [[ $RC -ne 0 ]]; then
10402                 error "Multiop fsync failed, rc=$RC"
10403         fi
10404
10405         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10406         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10407                     grep -c writeback)
10408         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10409                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10410         fi
10411
10412         rm -f $DIR/$tfile
10413         echo "Dirty pages flushed via fsync on EROFS"
10414         return 0
10415 }
10416 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
10417
10418 # continue to use small resend count to reduce test_118* time (b=14842)
10419 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10420
10421 test_118d()
10422 {
10423         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10424         remote_ost_nodsh && skip "remote OST with nodsh"
10425
10426         reset_async
10427
10428         #define OBD_FAIL_OST_BRW_PAUSE_BULK
10429         set_nodes_failloc "$(osts_nodes)" 0x214
10430         # multiop should block due to fsync until pages are written
10431         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10432         MULTIPID=$!
10433         sleep 1
10434
10435         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10436                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10437         fi
10438
10439         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10440                     grep -c writeback)
10441         if [[ $WRITEBACK -eq 0 ]]; then
10442                 error "No page in writeback, writeback=$WRITEBACK"
10443         fi
10444
10445         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
10446         set_nodes_failloc "$(osts_nodes)" 0
10447
10448         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10449         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10450                     grep -c writeback)
10451         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10452                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10453         fi
10454
10455         rm -f $DIR/$tfile
10456         echo "Dirty pages gaurenteed flushed via fsync"
10457         return 0
10458 }
10459 run_test 118d "Fsync validation inject a delay of the bulk =========="
10460
10461 test_118f() {
10462         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10463
10464         reset_async
10465
10466         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
10467         lctl set_param fail_loc=0x8000040a
10468
10469         # Should simulate EINVAL error which is fatal
10470         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10471         RC=$?
10472         if [[ $RC -eq 0 ]]; then
10473                 error "Must return error due to dropped pages, rc=$RC"
10474         fi
10475
10476         lctl set_param fail_loc=0x0
10477
10478         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10479         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10480         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10481                     grep -c writeback)
10482         if [[ $LOCKED -ne 0 ]]; then
10483                 error "Locked pages remain in cache, locked=$LOCKED"
10484         fi
10485
10486         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10487                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10488         fi
10489
10490         rm -f $DIR/$tfile
10491         echo "No pages locked after fsync"
10492
10493         reset_async
10494         return 0
10495 }
10496 run_test 118f "Simulate unrecoverable OSC side error =========="
10497
10498 test_118g() {
10499         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10500
10501         reset_async
10502
10503         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
10504         lctl set_param fail_loc=0x406
10505
10506         # simulate local -ENOMEM
10507         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10508         RC=$?
10509
10510         lctl set_param fail_loc=0
10511         if [[ $RC -eq 0 ]]; then
10512                 error "Must return error due to dropped pages, rc=$RC"
10513         fi
10514
10515         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10516         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10517         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10518                         grep -c writeback)
10519         if [[ $LOCKED -ne 0 ]]; then
10520                 error "Locked pages remain in cache, locked=$LOCKED"
10521         fi
10522
10523         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10524                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10525         fi
10526
10527         rm -f $DIR/$tfile
10528         echo "No pages locked after fsync"
10529
10530         reset_async
10531         return 0
10532 }
10533 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
10534
10535 test_118h() {
10536         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10537         remote_ost_nodsh && skip "remote OST with nodsh"
10538
10539         reset_async
10540
10541         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10542         set_nodes_failloc "$(osts_nodes)" 0x20e
10543         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10544         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10545         RC=$?
10546
10547         set_nodes_failloc "$(osts_nodes)" 0
10548         if [[ $RC -eq 0 ]]; then
10549                 error "Must return error due to dropped pages, rc=$RC"
10550         fi
10551
10552         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10553         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10554         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10555                     grep -c writeback)
10556         if [[ $LOCKED -ne 0 ]]; then
10557                 error "Locked pages remain in cache, locked=$LOCKED"
10558         fi
10559
10560         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10561                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10562         fi
10563
10564         rm -f $DIR/$tfile
10565         echo "No pages locked after fsync"
10566
10567         return 0
10568 }
10569 run_test 118h "Verify timeout in handling recoverables errors  =========="
10570
10571 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10572
10573 test_118i() {
10574         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10575         remote_ost_nodsh && skip "remote OST with nodsh"
10576
10577         reset_async
10578
10579         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10580         set_nodes_failloc "$(osts_nodes)" 0x20e
10581
10582         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10583         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10584         PID=$!
10585         sleep 5
10586         set_nodes_failloc "$(osts_nodes)" 0
10587
10588         wait $PID
10589         RC=$?
10590         if [[ $RC -ne 0 ]]; then
10591                 error "got error, but should be not, rc=$RC"
10592         fi
10593
10594         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10595         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10596         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10597         if [[ $LOCKED -ne 0 ]]; then
10598                 error "Locked pages remain in cache, locked=$LOCKED"
10599         fi
10600
10601         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10602                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10603         fi
10604
10605         rm -f $DIR/$tfile
10606         echo "No pages locked after fsync"
10607
10608         return 0
10609 }
10610 run_test 118i "Fix error before timeout in recoverable error  =========="
10611
10612 [ "$SLOW" = "no" ] && set_resend_count 4
10613
10614 test_118j() {
10615         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10616         remote_ost_nodsh && skip "remote OST with nodsh"
10617
10618         reset_async
10619
10620         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
10621         set_nodes_failloc "$(osts_nodes)" 0x220
10622
10623         # return -EIO from OST
10624         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10625         RC=$?
10626         set_nodes_failloc "$(osts_nodes)" 0x0
10627         if [[ $RC -eq 0 ]]; then
10628                 error "Must return error due to dropped pages, rc=$RC"
10629         fi
10630
10631         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10632         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10633         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10634         if [[ $LOCKED -ne 0 ]]; then
10635                 error "Locked pages remain in cache, locked=$LOCKED"
10636         fi
10637
10638         # in recoverable error on OST we want resend and stay until it finished
10639         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10640                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10641         fi
10642
10643         rm -f $DIR/$tfile
10644         echo "No pages locked after fsync"
10645
10646         return 0
10647 }
10648 run_test 118j "Simulate unrecoverable OST side error =========="
10649
10650 test_118k()
10651 {
10652         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10653         remote_ost_nodsh && skip "remote OSTs with nodsh"
10654
10655         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10656         set_nodes_failloc "$(osts_nodes)" 0x20e
10657         test_mkdir $DIR/$tdir
10658
10659         for ((i=0;i<10;i++)); do
10660                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
10661                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
10662                 SLEEPPID=$!
10663                 sleep 0.500s
10664                 kill $SLEEPPID
10665                 wait $SLEEPPID
10666         done
10667
10668         set_nodes_failloc "$(osts_nodes)" 0
10669         rm -rf $DIR/$tdir
10670 }
10671 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
10672
10673 test_118l() # LU-646
10674 {
10675         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10676
10677         test_mkdir $DIR/$tdir
10678         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
10679         rm -rf $DIR/$tdir
10680 }
10681 run_test 118l "fsync dir"
10682
10683 test_118m() # LU-3066
10684 {
10685         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10686
10687         test_mkdir $DIR/$tdir
10688         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
10689         rm -rf $DIR/$tdir
10690 }
10691 run_test 118m "fdatasync dir ========="
10692
10693 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10694
10695 test_118n()
10696 {
10697         local begin
10698         local end
10699
10700         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10701         remote_ost_nodsh && skip "remote OSTs with nodsh"
10702
10703         # Sleep to avoid a cached response.
10704         #define OBD_STATFS_CACHE_SECONDS 1
10705         sleep 2
10706
10707         # Inject a 10 second delay in the OST_STATFS handler.
10708         #define OBD_FAIL_OST_STATFS_DELAY 0x242
10709         set_nodes_failloc "$(osts_nodes)" 0x242
10710
10711         begin=$SECONDS
10712         stat --file-system $MOUNT > /dev/null
10713         end=$SECONDS
10714
10715         set_nodes_failloc "$(osts_nodes)" 0
10716
10717         if ((end - begin > 20)); then
10718             error "statfs took $((end - begin)) seconds, expected 10"
10719         fi
10720 }
10721 run_test 118n "statfs() sends OST_STATFS requests in parallel"
10722
10723 test_119a() # bug 11737
10724 {
10725         BSIZE=$((512 * 1024))
10726         directio write $DIR/$tfile 0 1 $BSIZE
10727         # We ask to read two blocks, which is more than a file size.
10728         # directio will indicate an error when requested and actual
10729         # sizes aren't equeal (a normal situation in this case) and
10730         # print actual read amount.
10731         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
10732         if [ "$NOB" != "$BSIZE" ]; then
10733                 error "read $NOB bytes instead of $BSIZE"
10734         fi
10735         rm -f $DIR/$tfile
10736 }
10737 run_test 119a "Short directIO read must return actual read amount"
10738
10739 test_119b() # bug 11737
10740 {
10741         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10742
10743         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
10744         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
10745         sync
10746         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
10747                 error "direct read failed"
10748         rm -f $DIR/$tfile
10749 }
10750 run_test 119b "Sparse directIO read must return actual read amount"
10751
10752 test_119c() # bug 13099
10753 {
10754         BSIZE=1048576
10755         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
10756         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
10757         rm -f $DIR/$tfile
10758 }
10759 run_test 119c "Testing for direct read hitting hole"
10760
10761 test_119d() # bug 15950
10762 {
10763         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10764
10765         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
10766         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
10767         BSIZE=1048576
10768         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
10769         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
10770         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
10771         lctl set_param fail_loc=0x40d
10772         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
10773         pid_dio=$!
10774         sleep 1
10775         cat $DIR/$tfile > /dev/null &
10776         lctl set_param fail_loc=0
10777         pid_reads=$!
10778         wait $pid_dio
10779         log "the DIO writes have completed, now wait for the reads (should not block very long)"
10780         sleep 2
10781         [ -n "`ps h -p $pid_reads -o comm`" ] && \
10782         error "the read rpcs have not completed in 2s"
10783         rm -f $DIR/$tfile
10784         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
10785 }
10786 run_test 119d "The DIO path should try to send a new rpc once one is completed"
10787
10788 test_120a() {
10789         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10790         remote_mds_nodsh && skip "remote MDS with nodsh"
10791         test_mkdir -i0 -c1 $DIR/$tdir
10792         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10793                 skip_env "no early lock cancel on server"
10794
10795         lru_resize_disable mdc
10796         lru_resize_disable osc
10797         cancel_lru_locks mdc
10798         # asynchronous object destroy at MDT could cause bl ast to client
10799         cancel_lru_locks osc
10800
10801         stat $DIR/$tdir > /dev/null
10802         can1=$(do_facet mds1 \
10803                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10804                awk '/ldlm_cancel/ {print $2}')
10805         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10806                awk '/ldlm_bl_callback/ {print $2}')
10807         test_mkdir -i0 -c1 $DIR/$tdir/d1
10808         can2=$(do_facet mds1 \
10809                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10810                awk '/ldlm_cancel/ {print $2}')
10811         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10812                awk '/ldlm_bl_callback/ {print $2}')
10813         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10814         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10815         lru_resize_enable mdc
10816         lru_resize_enable osc
10817 }
10818 run_test 120a "Early Lock Cancel: mkdir test"
10819
10820 test_120b() {
10821         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10822         remote_mds_nodsh && skip "remote MDS with nodsh"
10823         test_mkdir $DIR/$tdir
10824         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10825                 skip_env "no early lock cancel on server"
10826
10827         lru_resize_disable mdc
10828         lru_resize_disable osc
10829         cancel_lru_locks mdc
10830         stat $DIR/$tdir > /dev/null
10831         can1=$(do_facet $SINGLEMDS \
10832                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10833                awk '/ldlm_cancel/ {print $2}')
10834         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10835                awk '/ldlm_bl_callback/ {print $2}')
10836         touch $DIR/$tdir/f1
10837         can2=$(do_facet $SINGLEMDS \
10838                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10839                awk '/ldlm_cancel/ {print $2}')
10840         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10841                awk '/ldlm_bl_callback/ {print $2}')
10842         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10843         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10844         lru_resize_enable mdc
10845         lru_resize_enable osc
10846 }
10847 run_test 120b "Early Lock Cancel: create test"
10848
10849 test_120c() {
10850         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10851         remote_mds_nodsh && skip "remote MDS with nodsh"
10852         test_mkdir -i0 -c1 $DIR/$tdir
10853         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10854                 skip "no early lock cancel on server"
10855
10856         lru_resize_disable mdc
10857         lru_resize_disable osc
10858         test_mkdir -i0 -c1 $DIR/$tdir/d1
10859         test_mkdir -i0 -c1 $DIR/$tdir/d2
10860         touch $DIR/$tdir/d1/f1
10861         cancel_lru_locks mdc
10862         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
10863         can1=$(do_facet mds1 \
10864                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10865                awk '/ldlm_cancel/ {print $2}')
10866         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10867                awk '/ldlm_bl_callback/ {print $2}')
10868         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10869         can2=$(do_facet mds1 \
10870                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10871                awk '/ldlm_cancel/ {print $2}')
10872         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10873                awk '/ldlm_bl_callback/ {print $2}')
10874         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10875         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10876         lru_resize_enable mdc
10877         lru_resize_enable osc
10878 }
10879 run_test 120c "Early Lock Cancel: link test"
10880
10881 test_120d() {
10882         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10883         remote_mds_nodsh && skip "remote MDS with nodsh"
10884         test_mkdir -i0 -c1 $DIR/$tdir
10885         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10886                 skip_env "no early lock cancel on server"
10887
10888         lru_resize_disable mdc
10889         lru_resize_disable osc
10890         touch $DIR/$tdir
10891         cancel_lru_locks mdc
10892         stat $DIR/$tdir > /dev/null
10893         can1=$(do_facet mds1 \
10894                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10895                awk '/ldlm_cancel/ {print $2}')
10896         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10897                awk '/ldlm_bl_callback/ {print $2}')
10898         chmod a+x $DIR/$tdir
10899         can2=$(do_facet mds1 \
10900                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10901                awk '/ldlm_cancel/ {print $2}')
10902         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10903                awk '/ldlm_bl_callback/ {print $2}')
10904         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10905         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10906         lru_resize_enable mdc
10907         lru_resize_enable osc
10908 }
10909 run_test 120d "Early Lock Cancel: setattr test"
10910
10911 test_120e() {
10912         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10913         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10914                 skip_env "no early lock cancel on server"
10915         remote_mds_nodsh && skip "remote MDS with nodsh"
10916
10917         local dlmtrace_set=false
10918
10919         test_mkdir -i0 -c1 $DIR/$tdir
10920         lru_resize_disable mdc
10921         lru_resize_disable osc
10922         ! $LCTL get_param debug | grep -q dlmtrace &&
10923                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
10924         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
10925         cancel_lru_locks mdc
10926         cancel_lru_locks osc
10927         dd if=$DIR/$tdir/f1 of=/dev/null
10928         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
10929         # XXX client can not do early lock cancel of OST lock
10930         # during unlink (LU-4206), so cancel osc lock now.
10931         sleep 2
10932         cancel_lru_locks osc
10933         can1=$(do_facet mds1 \
10934                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10935                awk '/ldlm_cancel/ {print $2}')
10936         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10937                awk '/ldlm_bl_callback/ {print $2}')
10938         unlink $DIR/$tdir/f1
10939         sleep 5
10940         can2=$(do_facet mds1 \
10941                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10942                awk '/ldlm_cancel/ {print $2}')
10943         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10944                awk '/ldlm_bl_callback/ {print $2}')
10945         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
10946                 $LCTL dk $TMP/cancel.debug.txt
10947         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
10948                 $LCTL dk $TMP/blocking.debug.txt
10949         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
10950         lru_resize_enable mdc
10951         lru_resize_enable osc
10952 }
10953 run_test 120e "Early Lock Cancel: unlink test"
10954
10955 test_120f() {
10956         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10957         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10958                 skip_env "no early lock cancel on server"
10959         remote_mds_nodsh && skip "remote MDS with nodsh"
10960
10961         test_mkdir -i0 -c1 $DIR/$tdir
10962         lru_resize_disable mdc
10963         lru_resize_disable osc
10964         test_mkdir -i0 -c1 $DIR/$tdir/d1
10965         test_mkdir -i0 -c1 $DIR/$tdir/d2
10966         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
10967         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
10968         cancel_lru_locks mdc
10969         cancel_lru_locks osc
10970         dd if=$DIR/$tdir/d1/f1 of=/dev/null
10971         dd if=$DIR/$tdir/d2/f2 of=/dev/null
10972         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
10973         # XXX client can not do early lock cancel of OST lock
10974         # during rename (LU-4206), so cancel osc lock now.
10975         sleep 2
10976         cancel_lru_locks osc
10977         can1=$(do_facet mds1 \
10978                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10979                awk '/ldlm_cancel/ {print $2}')
10980         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10981                awk '/ldlm_bl_callback/ {print $2}')
10982         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10983         sleep 5
10984         can2=$(do_facet mds1 \
10985                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10986                awk '/ldlm_cancel/ {print $2}')
10987         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10988                awk '/ldlm_bl_callback/ {print $2}')
10989         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10990         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10991         lru_resize_enable mdc
10992         lru_resize_enable osc
10993 }
10994 run_test 120f "Early Lock Cancel: rename test"
10995
10996 test_120g() {
10997         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10998         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10999                 skip_env "no early lock cancel on server"
11000         remote_mds_nodsh && skip "remote MDS with nodsh"
11001
11002         lru_resize_disable mdc
11003         lru_resize_disable osc
11004         count=10000
11005         echo create $count files
11006         test_mkdir $DIR/$tdir
11007         cancel_lru_locks mdc
11008         cancel_lru_locks osc
11009         t0=$(date +%s)
11010
11011         can0=$(do_facet $SINGLEMDS \
11012                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11013                awk '/ldlm_cancel/ {print $2}')
11014         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11015                awk '/ldlm_bl_callback/ {print $2}')
11016         createmany -o $DIR/$tdir/f $count
11017         sync
11018         can1=$(do_facet $SINGLEMDS \
11019                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11020                awk '/ldlm_cancel/ {print $2}')
11021         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11022                awk '/ldlm_bl_callback/ {print $2}')
11023         t1=$(date +%s)
11024         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
11025         echo rm $count files
11026         rm -r $DIR/$tdir
11027         sync
11028         can2=$(do_facet $SINGLEMDS \
11029                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11030                awk '/ldlm_cancel/ {print $2}')
11031         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11032                awk '/ldlm_bl_callback/ {print $2}')
11033         t2=$(date +%s)
11034         echo total: $count removes in $((t2-t1))
11035         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
11036         sleep 2
11037         # wait for commitment of removal
11038         lru_resize_enable mdc
11039         lru_resize_enable osc
11040 }
11041 run_test 120g "Early Lock Cancel: performance test"
11042
11043 test_121() { #bug #10589
11044         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11045
11046         rm -rf $DIR/$tfile
11047         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
11048 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
11049         lctl set_param fail_loc=0x310
11050         cancel_lru_locks osc > /dev/null
11051         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
11052         lctl set_param fail_loc=0
11053         [[ $reads -eq $writes ]] ||
11054                 error "read $reads blocks, must be $writes blocks"
11055 }
11056 run_test 121 "read cancel race ========="
11057
11058 test_123a() { # was test 123, statahead(bug 11401)
11059         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11060
11061         SLOWOK=0
11062         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
11063                 log "testing UP system. Performance may be lower than expected."
11064                 SLOWOK=1
11065         fi
11066
11067         rm -rf $DIR/$tdir
11068         test_mkdir $DIR/$tdir
11069         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
11070         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
11071         MULT=10
11072         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
11073                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
11074
11075                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
11076                 lctl set_param -n llite.*.statahead_max 0
11077                 lctl get_param llite.*.statahead_max
11078                 cancel_lru_locks mdc
11079                 cancel_lru_locks osc
11080                 stime=`date +%s`
11081                 time ls -l $DIR/$tdir | wc -l
11082                 etime=`date +%s`
11083                 delta=$((etime - stime))
11084                 log "ls $i files without statahead: $delta sec"
11085                 lctl set_param llite.*.statahead_max=$max
11086
11087                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
11088                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
11089                 cancel_lru_locks mdc
11090                 cancel_lru_locks osc
11091                 stime=`date +%s`
11092                 time ls -l $DIR/$tdir | wc -l
11093                 etime=`date +%s`
11094                 delta_sa=$((etime - stime))
11095                 log "ls $i files with statahead: $delta_sa sec"
11096                 lctl get_param -n llite.*.statahead_stats
11097                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
11098
11099                 [[ $swrong -lt $ewrong ]] &&
11100                         log "statahead was stopped, maybe too many locks held!"
11101                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
11102
11103                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
11104                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
11105                     lctl set_param -n llite.*.statahead_max 0
11106                     lctl get_param llite.*.statahead_max
11107                     cancel_lru_locks mdc
11108                     cancel_lru_locks osc
11109                     stime=`date +%s`
11110                     time ls -l $DIR/$tdir | wc -l
11111                     etime=`date +%s`
11112                     delta=$((etime - stime))
11113                     log "ls $i files again without statahead: $delta sec"
11114                     lctl set_param llite.*.statahead_max=$max
11115                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
11116                         if [  $SLOWOK -eq 0 ]; then
11117                                 error "ls $i files is slower with statahead!"
11118                         else
11119                                 log "ls $i files is slower with statahead!"
11120                         fi
11121                         break
11122                     fi
11123                 fi
11124
11125                 [ $delta -gt 20 ] && break
11126                 [ $delta -gt 8 ] && MULT=$((50 / delta))
11127                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
11128         done
11129         log "ls done"
11130
11131         stime=`date +%s`
11132         rm -r $DIR/$tdir
11133         sync
11134         etime=`date +%s`
11135         delta=$((etime - stime))
11136         log "rm -r $DIR/$tdir/: $delta seconds"
11137         log "rm done"
11138         lctl get_param -n llite.*.statahead_stats
11139 }
11140 run_test 123a "verify statahead work"
11141
11142 test_123b () { # statahead(bug 15027)
11143         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11144
11145         test_mkdir $DIR/$tdir
11146         createmany -o $DIR/$tdir/$tfile-%d 1000
11147
11148         cancel_lru_locks mdc
11149         cancel_lru_locks osc
11150
11151 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
11152         lctl set_param fail_loc=0x80000803
11153         ls -lR $DIR/$tdir > /dev/null
11154         log "ls done"
11155         lctl set_param fail_loc=0x0
11156         lctl get_param -n llite.*.statahead_stats
11157         rm -r $DIR/$tdir
11158         sync
11159
11160 }
11161 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
11162
11163 test_123c() {
11164         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
11165
11166         test_mkdir -i 0 -c 1 $DIR/$tdir.0
11167         test_mkdir -i 1 -c 1 $DIR/$tdir.1
11168         touch $DIR/$tdir.1/{1..3}
11169         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
11170
11171         remount_client $MOUNT
11172
11173         $MULTIOP $DIR/$tdir.0 Q
11174
11175         # let statahead to complete
11176         ls -l $DIR/$tdir.0 > /dev/null
11177
11178         testid=$(echo $TESTNAME | tr '_' ' ')
11179         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
11180                 error "statahead warning" || true
11181 }
11182 run_test 123c "Can not initialize inode warning on DNE statahead"
11183
11184 test_124a() {
11185         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11186         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11187                 skip_env "no lru resize on server"
11188
11189         local NR=2000
11190
11191         test_mkdir $DIR/$tdir
11192
11193         log "create $NR files at $DIR/$tdir"
11194         createmany -o $DIR/$tdir/f $NR ||
11195                 error "failed to create $NR files in $DIR/$tdir"
11196
11197         cancel_lru_locks mdc
11198         ls -l $DIR/$tdir > /dev/null
11199
11200         local NSDIR=""
11201         local LRU_SIZE=0
11202         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
11203                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
11204                 LRU_SIZE=$($LCTL get_param -n $PARAM)
11205                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
11206                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
11207                         log "NSDIR=$NSDIR"
11208                         log "NS=$(basename $NSDIR)"
11209                         break
11210                 fi
11211         done
11212
11213         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
11214                 skip "Not enough cached locks created!"
11215         fi
11216         log "LRU=$LRU_SIZE"
11217
11218         local SLEEP=30
11219
11220         # We know that lru resize allows one client to hold $LIMIT locks
11221         # for 10h. After that locks begin to be killed by client.
11222         local MAX_HRS=10
11223         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
11224         log "LIMIT=$LIMIT"
11225         if [ $LIMIT -lt $LRU_SIZE ]; then
11226                 skip "Limit is too small $LIMIT"
11227         fi
11228
11229         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
11230         # killing locks. Some time was spent for creating locks. This means
11231         # that up to the moment of sleep finish we must have killed some of
11232         # them (10-100 locks). This depends on how fast ther were created.
11233         # Many of them were touched in almost the same moment and thus will
11234         # be killed in groups.
11235         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
11236
11237         # Use $LRU_SIZE_B here to take into account real number of locks
11238         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
11239         local LRU_SIZE_B=$LRU_SIZE
11240         log "LVF=$LVF"
11241         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
11242         log "OLD_LVF=$OLD_LVF"
11243         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
11244
11245         # Let's make sure that we really have some margin. Client checks
11246         # cached locks every 10 sec.
11247         SLEEP=$((SLEEP+20))
11248         log "Sleep ${SLEEP} sec"
11249         local SEC=0
11250         while ((SEC<$SLEEP)); do
11251                 echo -n "..."
11252                 sleep 5
11253                 SEC=$((SEC+5))
11254                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
11255                 echo -n "$LRU_SIZE"
11256         done
11257         echo ""
11258         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
11259         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
11260
11261         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
11262                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
11263                 unlinkmany $DIR/$tdir/f $NR
11264                 return
11265         }
11266
11267         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
11268         log "unlink $NR files at $DIR/$tdir"
11269         unlinkmany $DIR/$tdir/f $NR
11270 }
11271 run_test 124a "lru resize ======================================="
11272
11273 get_max_pool_limit()
11274 {
11275         local limit=$($LCTL get_param \
11276                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
11277         local max=0
11278         for l in $limit; do
11279                 if [[ $l -gt $max ]]; then
11280                         max=$l
11281                 fi
11282         done
11283         echo $max
11284 }
11285
11286 test_124b() {
11287         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11288         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11289                 skip_env "no lru resize on server"
11290
11291         LIMIT=$(get_max_pool_limit)
11292
11293         NR=$(($(default_lru_size)*20))
11294         if [[ $NR -gt $LIMIT ]]; then
11295                 log "Limit lock number by $LIMIT locks"
11296                 NR=$LIMIT
11297         fi
11298
11299         IFree=$(mdsrate_inodes_available)
11300         if [ $IFree -lt $NR ]; then
11301                 log "Limit lock number by $IFree inodes"
11302                 NR=$IFree
11303         fi
11304
11305         lru_resize_disable mdc
11306         test_mkdir -p $DIR/$tdir/disable_lru_resize
11307
11308         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
11309         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
11310         cancel_lru_locks mdc
11311         stime=`date +%s`
11312         PID=""
11313         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11314         PID="$PID $!"
11315         sleep 2
11316         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11317         PID="$PID $!"
11318         sleep 2
11319         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11320         PID="$PID $!"
11321         wait $PID
11322         etime=`date +%s`
11323         nolruresize_delta=$((etime-stime))
11324         log "ls -la time: $nolruresize_delta seconds"
11325         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11326         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
11327
11328         lru_resize_enable mdc
11329         test_mkdir -p $DIR/$tdir/enable_lru_resize
11330
11331         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
11332         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
11333         cancel_lru_locks mdc
11334         stime=`date +%s`
11335         PID=""
11336         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11337         PID="$PID $!"
11338         sleep 2
11339         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11340         PID="$PID $!"
11341         sleep 2
11342         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11343         PID="$PID $!"
11344         wait $PID
11345         etime=`date +%s`
11346         lruresize_delta=$((etime-stime))
11347         log "ls -la time: $lruresize_delta seconds"
11348         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11349
11350         if [ $lruresize_delta -gt $nolruresize_delta ]; then
11351                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
11352         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
11353                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
11354         else
11355                 log "lru resize performs the same with no lru resize"
11356         fi
11357         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
11358 }
11359 run_test 124b "lru resize (performance test) ======================="
11360
11361 test_124c() {
11362         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11363         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11364                 skip_env "no lru resize on server"
11365
11366         # cache ununsed locks on client
11367         local nr=100
11368         cancel_lru_locks mdc
11369         test_mkdir $DIR/$tdir
11370         createmany -o $DIR/$tdir/f $nr ||
11371                 error "failed to create $nr files in $DIR/$tdir"
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         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11379
11380         # set lru_max_age to 1 sec
11381         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11382         echo "sleep $((recalc_p * 2)) seconds..."
11383         sleep $((recalc_p * 2))
11384
11385         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11386         # restore lru_max_age
11387         $LCTL set_param -n $nsdir.lru_max_age $max_age
11388         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11389         unlinkmany $DIR/$tdir/f $nr
11390 }
11391 run_test 124c "LRUR cancel very aged locks"
11392
11393 test_124d() {
11394         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11395         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11396                 skip_env "no lru resize on server"
11397
11398         # cache ununsed locks on client
11399         local nr=100
11400
11401         lru_resize_disable mdc
11402         stack_trap "lru_resize_enable mdc" EXIT
11403
11404         cancel_lru_locks mdc
11405
11406         # asynchronous object destroy at MDT could cause bl ast to client
11407         test_mkdir $DIR/$tdir
11408         createmany -o $DIR/$tdir/f $nr ||
11409                 error "failed to create $nr files in $DIR/$tdir"
11410         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
11411
11412         ls -l $DIR/$tdir > /dev/null
11413
11414         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11415         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11416         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11417         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11418
11419         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11420
11421         # set lru_max_age to 1 sec
11422         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11423         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
11424
11425         echo "sleep $((recalc_p * 2)) seconds..."
11426         sleep $((recalc_p * 2))
11427
11428         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11429
11430         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11431 }
11432 run_test 124d "cancel very aged locks if lru-resize diasbaled"
11433
11434 test_125() { # 13358
11435         $LCTL get_param -n llite.*.client_type | grep -q local ||
11436                 skip "must run as local client"
11437         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
11438                 skip_env "must have acl enabled"
11439         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
11440
11441         test_mkdir $DIR/$tdir
11442         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
11443         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
11444         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
11445 }
11446 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
11447
11448 test_126() { # bug 12829/13455
11449         $GSS && skip_env "must run as gss disabled"
11450         $LCTL get_param -n llite.*.client_type | grep -q local ||
11451                 skip "must run as local client"
11452         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
11453
11454         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
11455         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
11456         rm -f $DIR/$tfile
11457         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
11458 }
11459 run_test 126 "check that the fsgid provided by the client is taken into account"
11460
11461 test_127a() { # bug 15521
11462         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11463         local name count samp unit min max sum sumsq
11464
11465         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
11466         echo "stats before reset"
11467         $LCTL get_param osc.*.stats
11468         $LCTL set_param osc.*.stats=0
11469         local fsize=$((2048 * 1024))
11470
11471         dd if=/dev/zero of=$DIR/$tfile bs=$fsize count=1
11472         cancel_lru_locks osc
11473         dd if=$DIR/$tfile of=/dev/null bs=$fsize
11474
11475         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/$tfile.tmp
11476         stack_trap "rm -f $TMP/$tfile.tmp"
11477         while read name count samp unit min max sum sumsq; do
11478                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11479                 [ ! $min ] && error "Missing min value for $name proc entry"
11480                 eval $name=$count || error "Wrong proc format"
11481
11482                 case $name in
11483                 read_bytes|write_bytes)
11484                         [[ "$unit" =~ "bytes" ]] ||
11485                                 error "unit is not 'bytes': $unit"
11486                         (( $min >= 4096 )) || error "min is too small: $min"
11487                         (( $min <= $fsize )) || error "min is too big: $min"
11488                         (( $max >= 4096 )) || error "max is too small: $max"
11489                         (( $max <= $fsize )) || error "max is too big: $max"
11490                         (( $sum == $fsize )) || error "sum is wrong: $sum"
11491                         (( $sumsq >= ($fsize / 4096) * (4096 * 4096) )) ||
11492                                 error "sumsquare is too small: $sumsq"
11493                         (( $sumsq <= $fsize * $fsize )) ||
11494                                 error "sumsquare is too big: $sumsq"
11495                         ;;
11496                 ost_read|ost_write)
11497                         [[ "$unit" =~ "usec" ]] ||
11498                                 error "unit is not 'usec': $unit"
11499                         ;;
11500                 *)      ;;
11501                 esac
11502         done < $DIR/$tfile.tmp
11503
11504         #check that we actually got some stats
11505         [ "$read_bytes" ] || error "Missing read_bytes stats"
11506         [ "$write_bytes" ] || error "Missing write_bytes stats"
11507         [ "$read_bytes" != 0 ] || error "no read done"
11508         [ "$write_bytes" != 0 ] || error "no write done"
11509 }
11510 run_test 127a "verify the client stats are sane"
11511
11512 test_127b() { # bug LU-333
11513         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11514         local name count samp unit min max sum sumsq
11515
11516         echo "stats before reset"
11517         $LCTL get_param llite.*.stats
11518         $LCTL set_param llite.*.stats=0
11519
11520         # perform 2 reads and writes so MAX is different from SUM.
11521         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11522         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11523         cancel_lru_locks osc
11524         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11525         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11526
11527         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
11528         stack_trap "rm -f $TMP/$tfile.tmp"
11529         while read name count samp unit min max sum sumsq; do
11530                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11531                 eval $name=$count || error "Wrong proc format"
11532
11533                 case $name in
11534                 read_bytes|write_bytes)
11535                         [[ "$unit" =~ "bytes" ]] ||
11536                                 error "unit is not 'bytes': $unit"
11537                         (( $count == 2 )) || error "count is not 2: $count"
11538                         (( $min == $PAGE_SIZE )) ||
11539                                 error "min is not $PAGE_SIZE: $min"
11540                         (( $max == $PAGE_SIZE )) ||
11541                                 error "max is not $PAGE_SIZE: $max"
11542                         (( $sum == $PAGE_SIZE * 2 )) ||
11543                                 error "sum is not $((PAGE_SIZE * 2)): $sum"
11544                         ;;
11545                 read|write)
11546                         [[ "$unit" =~ "usec" ]] ||
11547                                 error "unit is not 'usec': $unit"
11548                         ;;
11549                 *)      ;;
11550                 esac
11551         done < $TMP/$tfile.tmp
11552
11553         #check that we actually got some stats
11554         [ "$read_bytes" ] || error "Missing read_bytes stats"
11555         [ "$write_bytes" ] || error "Missing write_bytes stats"
11556         [ "$read_bytes" != 0 ] || error "no read done"
11557         [ "$write_bytes" != 0 ] || error "no write done"
11558 }
11559 run_test 127b "verify the llite client stats are sane"
11560
11561 test_127c() { # LU-12394
11562         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11563         local size
11564         local bsize
11565         local reads
11566         local writes
11567         local count
11568
11569         $LCTL set_param llite.*.extents_stats=1
11570         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
11571
11572         # Use two stripes so there is enough space in default config
11573         $LFS setstripe -c 2 $DIR/$tfile
11574
11575         # Extent stats start at 0-4K and go in power of two buckets
11576         # LL_HIST_START = 12 --> 2^12 = 4K
11577         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
11578         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
11579         # small configs
11580         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
11581                 do
11582                 # Write and read, 2x each, second time at a non-zero offset
11583                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
11584                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
11585                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
11586                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
11587                 rm -f $DIR/$tfile
11588         done
11589
11590         $LCTL get_param llite.*.extents_stats
11591
11592         count=2
11593         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
11594                 do
11595                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
11596                                 grep -m 1 $bsize)
11597                 reads=$(echo $bucket | awk '{print $5}')
11598                 writes=$(echo $bucket | awk '{print $9}')
11599                 [ "$reads" -eq $count ] ||
11600                         error "$reads reads in < $bsize bucket, expect $count"
11601                 [ "$writes" -eq $count ] ||
11602                         error "$writes writes in < $bsize bucket, expect $count"
11603         done
11604
11605         # Test mmap write and read
11606         $LCTL set_param llite.*.extents_stats=c
11607         size=512
11608         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
11609         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
11610         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
11611
11612         $LCTL get_param llite.*.extents_stats
11613
11614         count=$(((size*1024) / PAGE_SIZE))
11615
11616         bsize=$((2 * PAGE_SIZE / 1024))K
11617
11618         bucket=$($LCTL get_param -n llite.*.extents_stats |
11619                         grep -m 1 $bsize)
11620         reads=$(echo $bucket | awk '{print $5}')
11621         writes=$(echo $bucket | awk '{print $9}')
11622         # mmap writes fault in the page first, creating an additonal read
11623         [ "$reads" -eq $((2 * count)) ] ||
11624                 error "$reads reads in < $bsize bucket, expect $count"
11625         [ "$writes" -eq $count ] ||
11626                 error "$writes writes in < $bsize bucket, expect $count"
11627 }
11628 run_test 127c "test llite extent stats with regular & mmap i/o"
11629
11630 test_128() { # bug 15212
11631         touch $DIR/$tfile
11632         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
11633                 find $DIR/$tfile
11634                 find $DIR/$tfile
11635         EOF
11636
11637         result=$(grep error $TMP/$tfile.log)
11638         rm -f $DIR/$tfile $TMP/$tfile.log
11639         [ -z "$result" ] ||
11640                 error "consecutive find's under interactive lfs failed"
11641 }
11642 run_test 128 "interactive lfs for 2 consecutive find's"
11643
11644 set_dir_limits () {
11645         local mntdev
11646         local canondev
11647         local node
11648
11649         local ldproc=/proc/fs/ldiskfs
11650         local facets=$(get_facets MDS)
11651
11652         for facet in ${facets//,/ }; do
11653                 canondev=$(ldiskfs_canon \
11654                            *.$(convert_facet2label $facet).mntdev $facet)
11655                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
11656                         ldproc=/sys/fs/ldiskfs
11657                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
11658                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
11659         done
11660 }
11661
11662 check_mds_dmesg() {
11663         local facets=$(get_facets MDS)
11664         for facet in ${facets//,/ }; do
11665                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
11666         done
11667         return 1
11668 }
11669
11670 test_129() {
11671         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11672         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
11673                 skip "Need MDS version with at least 2.5.56"
11674         if [ "$mds1_FSTYPE" != ldiskfs ]; then
11675                 skip_env "ldiskfs only test"
11676         fi
11677         remote_mds_nodsh && skip "remote MDS with nodsh"
11678
11679         local ENOSPC=28
11680         local EFBIG=27
11681         local has_warning=false
11682
11683         rm -rf $DIR/$tdir
11684         mkdir -p $DIR/$tdir
11685
11686         # block size of mds1
11687         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
11688         set_dir_limits $maxsize $maxsize
11689         local dirsize=$(stat -c%s "$DIR/$tdir")
11690         local nfiles=0
11691         while [[ $dirsize -le $maxsize ]]; do
11692                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
11693                 rc=$?
11694                 if ! $has_warning; then
11695                         check_mds_dmesg '"is approaching"' && has_warning=true
11696                 fi
11697                 # check two errors:
11698                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
11699                 # EFBIG for previous versions included in ldiskfs series
11700                 if [ $rc -eq $EFBIG ] || [ $rc -eq $ENOSPC ]; then
11701                         set_dir_limits 0 0
11702                         echo "return code $rc received as expected"
11703
11704                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
11705                                 error_exit "create failed w/o dir size limit"
11706
11707                         check_mds_dmesg '"has reached"' ||
11708                                 error_exit "reached message should be output"
11709
11710                         [ $has_warning = "false" ] &&
11711                                 error_exit "warning message should be output"
11712
11713                         dirsize=$(stat -c%s "$DIR/$tdir")
11714
11715                         [[ $dirsize -ge $maxsize ]] && return 0
11716                         error_exit "current dir size $dirsize, " \
11717                                    "previous limit $maxsize"
11718                 elif [ $rc -ne 0 ]; then
11719                         set_dir_limits 0 0
11720                         error_exit "return $rc received instead of expected " \
11721                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
11722                 fi
11723                 nfiles=$((nfiles + 1))
11724                 dirsize=$(stat -c%s "$DIR/$tdir")
11725         done
11726
11727         set_dir_limits 0 0
11728         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
11729 }
11730 run_test 129 "test directory size limit ========================"
11731
11732 OLDIFS="$IFS"
11733 cleanup_130() {
11734         trap 0
11735         IFS="$OLDIFS"
11736 }
11737
11738 test_130a() {
11739         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11740         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11741
11742         trap cleanup_130 EXIT RETURN
11743
11744         local fm_file=$DIR/$tfile
11745         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
11746         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
11747                 error "dd failed for $fm_file"
11748
11749         # LU-1795: test filefrag/FIEMAP once, even if unsupported
11750         filefrag -ves $fm_file
11751         RC=$?
11752         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11753                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11754         [ $RC != 0 ] && error "filefrag $fm_file failed"
11755
11756         filefrag_op=$(filefrag -ve -k $fm_file |
11757                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11758         lun=$($LFS getstripe -i $fm_file)
11759
11760         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
11761         IFS=$'\n'
11762         tot_len=0
11763         for line in $filefrag_op
11764         do
11765                 frag_lun=`echo $line | cut -d: -f5`
11766                 ext_len=`echo $line | cut -d: -f4`
11767                 if (( $frag_lun != $lun )); then
11768                         cleanup_130
11769                         error "FIEMAP on 1-stripe file($fm_file) failed"
11770                         return
11771                 fi
11772                 (( tot_len += ext_len ))
11773         done
11774
11775         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
11776                 cleanup_130
11777                 error "FIEMAP on 1-stripe file($fm_file) failed;"
11778                 return
11779         fi
11780
11781         cleanup_130
11782
11783         echo "FIEMAP on single striped file succeeded"
11784 }
11785 run_test 130a "FIEMAP (1-stripe file)"
11786
11787 test_130b() {
11788         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11789
11790         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11791         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11792
11793         trap cleanup_130 EXIT RETURN
11794
11795         local fm_file=$DIR/$tfile
11796         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
11797                         error "setstripe on $fm_file"
11798         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11799                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11800
11801         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
11802                 error "dd failed on $fm_file"
11803
11804         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11805         filefrag_op=$(filefrag -ve -k $fm_file |
11806                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11807
11808         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11809                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11810
11811         IFS=$'\n'
11812         tot_len=0
11813         num_luns=1
11814         for line in $filefrag_op
11815         do
11816                 frag_lun=$(echo $line | cut -d: -f5 |
11817                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11818                 ext_len=$(echo $line | cut -d: -f4)
11819                 if (( $frag_lun != $last_lun )); then
11820                         if (( tot_len != 1024 )); then
11821                                 cleanup_130
11822                                 error "FIEMAP on $fm_file failed; returned " \
11823                                 "len $tot_len for OST $last_lun instead of 1024"
11824                                 return
11825                         else
11826                                 (( num_luns += 1 ))
11827                                 tot_len=0
11828                         fi
11829                 fi
11830                 (( tot_len += ext_len ))
11831                 last_lun=$frag_lun
11832         done
11833         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
11834                 cleanup_130
11835                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11836                         "luns or wrong len for OST $last_lun"
11837                 return
11838         fi
11839
11840         cleanup_130
11841
11842         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
11843 }
11844 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
11845
11846 test_130c() {
11847         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11848
11849         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11850         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11851
11852         trap cleanup_130 EXIT RETURN
11853
11854         local fm_file=$DIR/$tfile
11855         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
11856         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11857                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11858
11859         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
11860                         error "dd failed on $fm_file"
11861
11862         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11863         filefrag_op=$(filefrag -ve -k $fm_file |
11864                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11865
11866         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11867                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11868
11869         IFS=$'\n'
11870         tot_len=0
11871         num_luns=1
11872         for line in $filefrag_op
11873         do
11874                 frag_lun=$(echo $line | cut -d: -f5 |
11875                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11876                 ext_len=$(echo $line | cut -d: -f4)
11877                 if (( $frag_lun != $last_lun )); then
11878                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
11879                         if (( logical != 512 )); then
11880                                 cleanup_130
11881                                 error "FIEMAP on $fm_file failed; returned " \
11882                                 "logical start for lun $logical instead of 512"
11883                                 return
11884                         fi
11885                         if (( tot_len != 512 )); then
11886                                 cleanup_130
11887                                 error "FIEMAP on $fm_file failed; returned " \
11888                                 "len $tot_len for OST $last_lun instead of 1024"
11889                                 return
11890                         else
11891                                 (( num_luns += 1 ))
11892                                 tot_len=0
11893                         fi
11894                 fi
11895                 (( tot_len += ext_len ))
11896                 last_lun=$frag_lun
11897         done
11898         if (( num_luns != 2 || tot_len != 512 )); then
11899                 cleanup_130
11900                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11901                         "luns or wrong len for OST $last_lun"
11902                 return
11903         fi
11904
11905         cleanup_130
11906
11907         echo "FIEMAP on 2-stripe file with hole succeeded"
11908 }
11909 run_test 130c "FIEMAP (2-stripe file with hole)"
11910
11911 test_130d() {
11912         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
11913
11914         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11915         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11916
11917         trap cleanup_130 EXIT RETURN
11918
11919         local fm_file=$DIR/$tfile
11920         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
11921                         error "setstripe on $fm_file"
11922         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11923                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11924
11925         local actual_stripe_count=$($LFS getstripe -c $fm_file)
11926         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
11927                 error "dd failed on $fm_file"
11928
11929         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11930         filefrag_op=$(filefrag -ve -k $fm_file |
11931                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11932
11933         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11934                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11935
11936         IFS=$'\n'
11937         tot_len=0
11938         num_luns=1
11939         for line in $filefrag_op
11940         do
11941                 frag_lun=$(echo $line | cut -d: -f5 |
11942                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11943                 ext_len=$(echo $line | cut -d: -f4)
11944                 if (( $frag_lun != $last_lun )); then
11945                         if (( tot_len != 1024 )); then
11946                                 cleanup_130
11947                                 error "FIEMAP on $fm_file failed; returned " \
11948                                 "len $tot_len for OST $last_lun instead of 1024"
11949                                 return
11950                         else
11951                                 (( num_luns += 1 ))
11952                                 tot_len=0
11953                         fi
11954                 fi
11955                 (( tot_len += ext_len ))
11956                 last_lun=$frag_lun
11957         done
11958         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
11959                 cleanup_130
11960                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11961                         "luns or wrong len for OST $last_lun"
11962                 return
11963         fi
11964
11965         cleanup_130
11966
11967         echo "FIEMAP on N-stripe file succeeded"
11968 }
11969 run_test 130d "FIEMAP (N-stripe file)"
11970
11971 test_130e() {
11972         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11973
11974         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11975         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11976
11977         trap cleanup_130 EXIT RETURN
11978
11979         local fm_file=$DIR/$tfile
11980         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
11981         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11982                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11983
11984         NUM_BLKS=512
11985         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
11986         for ((i = 0; i < $NUM_BLKS; i++))
11987         do
11988                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
11989         done
11990
11991         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11992         filefrag_op=$(filefrag -ve -k $fm_file |
11993                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11994
11995         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11996                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11997
11998         IFS=$'\n'
11999         tot_len=0
12000         num_luns=1
12001         for line in $filefrag_op
12002         do
12003                 frag_lun=$(echo $line | cut -d: -f5 |
12004                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12005                 ext_len=$(echo $line | cut -d: -f4)
12006                 if (( $frag_lun != $last_lun )); then
12007                         if (( tot_len != $EXPECTED_LEN )); then
12008                                 cleanup_130
12009                                 error "FIEMAP on $fm_file failed; returned " \
12010                                 "len $tot_len for OST $last_lun instead " \
12011                                 "of $EXPECTED_LEN"
12012                                 return
12013                         else
12014                                 (( num_luns += 1 ))
12015                                 tot_len=0
12016                         fi
12017                 fi
12018                 (( tot_len += ext_len ))
12019                 last_lun=$frag_lun
12020         done
12021         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
12022                 cleanup_130
12023                 error "FIEMAP on $fm_file failed; returned wrong number " \
12024                         "of luns or wrong len for OST $last_lun"
12025                 return
12026         fi
12027
12028         cleanup_130
12029
12030         echo "FIEMAP with continuation calls succeeded"
12031 }
12032 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
12033
12034 test_130f() {
12035         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12036         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12037
12038         local fm_file=$DIR/$tfile
12039         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
12040                 error "multiop create with lov_delay_create on $fm_file"
12041
12042         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12043         filefrag_extents=$(filefrag -vek $fm_file |
12044                            awk '/extents? found/ { print $2 }')
12045         if [[ "$filefrag_extents" != "0" ]]; then
12046                 error "FIEMAP on $fm_file failed; " \
12047                       "returned $filefrag_extents expected 0"
12048         fi
12049
12050         rm -f $fm_file
12051 }
12052 run_test 130f "FIEMAP (unstriped file)"
12053
12054 # Test for writev/readv
12055 test_131a() {
12056         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
12057                 error "writev test failed"
12058         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
12059                 error "readv failed"
12060         rm -f $DIR/$tfile
12061 }
12062 run_test 131a "test iov's crossing stripe boundary for writev/readv"
12063
12064 test_131b() {
12065         local fsize=$((524288 + 1048576 + 1572864))
12066         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
12067                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12068                         error "append writev test failed"
12069
12070         ((fsize += 1572864 + 1048576))
12071         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
12072                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12073                         error "append writev test failed"
12074         rm -f $DIR/$tfile
12075 }
12076 run_test 131b "test append writev"
12077
12078 test_131c() {
12079         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
12080         error "NOT PASS"
12081 }
12082 run_test 131c "test read/write on file w/o objects"
12083
12084 test_131d() {
12085         rwv -f $DIR/$tfile -w -n 1 1572864
12086         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
12087         if [ "$NOB" != 1572864 ]; then
12088                 error "Short read filed: read $NOB bytes instead of 1572864"
12089         fi
12090         rm -f $DIR/$tfile
12091 }
12092 run_test 131d "test short read"
12093
12094 test_131e() {
12095         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
12096         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
12097         error "read hitting hole failed"
12098         rm -f $DIR/$tfile
12099 }
12100 run_test 131e "test read hitting hole"
12101
12102 check_stats() {
12103         local facet=$1
12104         local op=$2
12105         local want=${3:-0}
12106         local res
12107
12108         case $facet in
12109         mds*) res=$(do_facet $facet \
12110                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
12111                  ;;
12112         ost*) res=$(do_facet $facet \
12113                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
12114                  ;;
12115         *) error "Wrong facet '$facet'" ;;
12116         esac
12117         [ "$res" ] || error "The counter for $op on $facet was not incremented"
12118         # if the argument $3 is zero, it means any stat increment is ok.
12119         if [[ $want -gt 0 ]]; then
12120                 local count=$(echo $res | awk '{ print $2 }')
12121                 [[ $count -ne $want ]] &&
12122                         error "The $op counter on $facet is $count, not $want"
12123         fi
12124 }
12125
12126 test_133a() {
12127         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12128         remote_ost_nodsh && skip "remote OST with nodsh"
12129         remote_mds_nodsh && skip "remote MDS with nodsh"
12130         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12131                 skip_env "MDS doesn't support rename stats"
12132
12133         local testdir=$DIR/${tdir}/stats_testdir
12134
12135         mkdir -p $DIR/${tdir}
12136
12137         # clear stats.
12138         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12139         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12140
12141         # verify mdt stats first.
12142         mkdir ${testdir} || error "mkdir failed"
12143         check_stats $SINGLEMDS "mkdir" 1
12144         touch ${testdir}/${tfile} || error "touch failed"
12145         check_stats $SINGLEMDS "open" 1
12146         check_stats $SINGLEMDS "close" 1
12147         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
12148                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
12149                 check_stats $SINGLEMDS "mknod" 2
12150         }
12151         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
12152         check_stats $SINGLEMDS "unlink" 1
12153         rm -f ${testdir}/${tfile} || error "file remove failed"
12154         check_stats $SINGLEMDS "unlink" 2
12155
12156         # remove working dir and check mdt stats again.
12157         rmdir ${testdir} || error "rmdir failed"
12158         check_stats $SINGLEMDS "rmdir" 1
12159
12160         local testdir1=$DIR/${tdir}/stats_testdir1
12161         mkdir -p ${testdir}
12162         mkdir -p ${testdir1}
12163         touch ${testdir1}/test1
12164         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
12165         check_stats $SINGLEMDS "crossdir_rename" 1
12166
12167         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
12168         check_stats $SINGLEMDS "samedir_rename" 1
12169
12170         rm -rf $DIR/${tdir}
12171 }
12172 run_test 133a "Verifying MDT stats ========================================"
12173
12174 test_133b() {
12175         local res
12176
12177         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12178         remote_ost_nodsh && skip "remote OST with nodsh"
12179         remote_mds_nodsh && skip "remote MDS with nodsh"
12180
12181         local testdir=$DIR/${tdir}/stats_testdir
12182
12183         mkdir -p ${testdir} || error "mkdir failed"
12184         touch ${testdir}/${tfile} || error "touch failed"
12185         cancel_lru_locks mdc
12186
12187         # clear stats.
12188         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12189         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12190
12191         # extra mdt stats verification.
12192         chmod 444 ${testdir}/${tfile} || error "chmod failed"
12193         check_stats $SINGLEMDS "setattr" 1
12194         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12195         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
12196         then            # LU-1740
12197                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
12198                 check_stats $SINGLEMDS "getattr" 1
12199         fi
12200         rm -rf $DIR/${tdir}
12201
12202         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
12203         # so the check below is not reliable
12204         [ $MDSCOUNT -eq 1 ] || return 0
12205
12206         # Sleep to avoid a cached response.
12207         #define OBD_STATFS_CACHE_SECONDS 1
12208         sleep 2
12209         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12210         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12211         $LFS df || error "lfs failed"
12212         check_stats $SINGLEMDS "statfs" 1
12213
12214         # check aggregated statfs (LU-10018)
12215         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
12216                 return 0
12217         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
12218                 return 0
12219         sleep 2
12220         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12221         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12222         df $DIR
12223         check_stats $SINGLEMDS "statfs" 1
12224
12225         # We want to check that the client didn't send OST_STATFS to
12226         # ost1 but the MDT also uses OST_STATFS for precreate. So some
12227         # extra care is needed here.
12228         if remote_mds; then
12229                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
12230                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
12231
12232                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
12233                 [ "$res" ] && error "OST got STATFS"
12234         fi
12235
12236         return 0
12237 }
12238 run_test 133b "Verifying extra MDT stats =================================="
12239
12240 test_133c() {
12241         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12242         remote_ost_nodsh && skip "remote OST with nodsh"
12243         remote_mds_nodsh && skip "remote MDS with nodsh"
12244
12245         local testdir=$DIR/$tdir/stats_testdir
12246
12247         test_mkdir -p $testdir
12248
12249         # verify obdfilter stats.
12250         $LFS setstripe -c 1 -i 0 $testdir/$tfile
12251         sync
12252         cancel_lru_locks osc
12253         wait_delete_completed
12254
12255         # clear stats.
12256         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12257         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12258
12259         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
12260                 error "dd failed"
12261         sync
12262         cancel_lru_locks osc
12263         check_stats ost1 "write" 1
12264
12265         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
12266         check_stats ost1 "read" 1
12267
12268         > $testdir/$tfile || error "truncate failed"
12269         check_stats ost1 "punch" 1
12270
12271         rm -f $testdir/$tfile || error "file remove failed"
12272         wait_delete_completed
12273         check_stats ost1 "destroy" 1
12274
12275         rm -rf $DIR/$tdir
12276 }
12277 run_test 133c "Verifying OST stats ========================================"
12278
12279 order_2() {
12280         local value=$1
12281         local orig=$value
12282         local order=1
12283
12284         while [ $value -ge 2 ]; do
12285                 order=$((order*2))
12286                 value=$((value/2))
12287         done
12288
12289         if [ $orig -gt $order ]; then
12290                 order=$((order*2))
12291         fi
12292         echo $order
12293 }
12294
12295 size_in_KMGT() {
12296     local value=$1
12297     local size=('K' 'M' 'G' 'T');
12298     local i=0
12299     local size_string=$value
12300
12301     while [ $value -ge 1024 ]; do
12302         if [ $i -gt 3 ]; then
12303             #T is the biggest unit we get here, if that is bigger,
12304             #just return XXXT
12305             size_string=${value}T
12306             break
12307         fi
12308         value=$((value >> 10))
12309         if [ $value -lt 1024 ]; then
12310             size_string=${value}${size[$i]}
12311             break
12312         fi
12313         i=$((i + 1))
12314     done
12315
12316     echo $size_string
12317 }
12318
12319 get_rename_size() {
12320         local size=$1
12321         local context=${2:-.}
12322         local sample=$(do_facet $SINGLEMDS $LCTL \
12323                 get_param mdt.$FSNAME-MDT0000.rename_stats |
12324                 grep -A1 $context |
12325                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
12326         echo $sample
12327 }
12328
12329 test_133d() {
12330         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12331         remote_ost_nodsh && skip "remote OST with nodsh"
12332         remote_mds_nodsh && skip "remote MDS with nodsh"
12333         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12334                 skip_env "MDS doesn't support rename stats"
12335
12336         local testdir1=$DIR/${tdir}/stats_testdir1
12337         local testdir2=$DIR/${tdir}/stats_testdir2
12338         mkdir -p $DIR/${tdir}
12339
12340         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12341
12342         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
12343         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
12344
12345         createmany -o $testdir1/test 512 || error "createmany failed"
12346
12347         # check samedir rename size
12348         mv ${testdir1}/test0 ${testdir1}/test_0
12349
12350         local testdir1_size=$(ls -l $DIR/${tdir} |
12351                 awk '/stats_testdir1/ {print $5}')
12352         local testdir2_size=$(ls -l $DIR/${tdir} |
12353                 awk '/stats_testdir2/ {print $5}')
12354
12355         testdir1_size=$(order_2 $testdir1_size)
12356         testdir2_size=$(order_2 $testdir2_size)
12357
12358         testdir1_size=$(size_in_KMGT $testdir1_size)
12359         testdir2_size=$(size_in_KMGT $testdir2_size)
12360
12361         echo "source rename dir size: ${testdir1_size}"
12362         echo "target rename dir size: ${testdir2_size}"
12363
12364         local cmd="do_facet $SINGLEMDS $LCTL "
12365         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
12366
12367         eval $cmd || error "$cmd failed"
12368         local samedir=$($cmd | grep 'same_dir')
12369         local same_sample=$(get_rename_size $testdir1_size)
12370         [ -z "$samedir" ] && error "samedir_rename_size count error"
12371         [[ $same_sample -eq 1 ]] ||
12372                 error "samedir_rename_size error $same_sample"
12373         echo "Check same dir rename stats success"
12374
12375         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12376
12377         # check crossdir rename size
12378         mv ${testdir1}/test_0 ${testdir2}/test_0
12379
12380         testdir1_size=$(ls -l $DIR/${tdir} |
12381                 awk '/stats_testdir1/ {print $5}')
12382         testdir2_size=$(ls -l $DIR/${tdir} |
12383                 awk '/stats_testdir2/ {print $5}')
12384
12385         testdir1_size=$(order_2 $testdir1_size)
12386         testdir2_size=$(order_2 $testdir2_size)
12387
12388         testdir1_size=$(size_in_KMGT $testdir1_size)
12389         testdir2_size=$(size_in_KMGT $testdir2_size)
12390
12391         echo "source rename dir size: ${testdir1_size}"
12392         echo "target rename dir size: ${testdir2_size}"
12393
12394         eval $cmd || error "$cmd failed"
12395         local crossdir=$($cmd | grep 'crossdir')
12396         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
12397         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
12398         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
12399         [[ $src_sample -eq 1 ]] ||
12400                 error "crossdir_rename_size error $src_sample"
12401         [[ $tgt_sample -eq 1 ]] ||
12402                 error "crossdir_rename_size error $tgt_sample"
12403         echo "Check cross dir rename stats success"
12404         rm -rf $DIR/${tdir}
12405 }
12406 run_test 133d "Verifying rename_stats ========================================"
12407
12408 test_133e() {
12409         remote_mds_nodsh && skip "remote MDS with nodsh"
12410         remote_ost_nodsh && skip "remote OST with nodsh"
12411         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12412
12413         local testdir=$DIR/${tdir}/stats_testdir
12414         local ctr f0 f1 bs=32768 count=42 sum
12415
12416         mkdir -p ${testdir} || error "mkdir failed"
12417
12418         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
12419
12420         for ctr in {write,read}_bytes; do
12421                 sync
12422                 cancel_lru_locks osc
12423
12424                 do_facet ost1 $LCTL set_param -n \
12425                         "obdfilter.*.exports.clear=clear"
12426
12427                 if [ $ctr = write_bytes ]; then
12428                         f0=/dev/zero
12429                         f1=${testdir}/${tfile}
12430                 else
12431                         f0=${testdir}/${tfile}
12432                         f1=/dev/null
12433                 fi
12434
12435                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
12436                         error "dd failed"
12437                 sync
12438                 cancel_lru_locks osc
12439
12440                 sum=$(do_facet ost1 $LCTL get_param \
12441                         "obdfilter.*.exports.*.stats" |
12442                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
12443                                 $1 == ctr { sum += $7 }
12444                                 END { printf("%0.0f", sum) }')
12445
12446                 if ((sum != bs * count)); then
12447                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
12448                 fi
12449         done
12450
12451         rm -rf $DIR/${tdir}
12452 }
12453 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
12454
12455 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
12456
12457 # Some versions of find (4.5.11, 4.5.14) included in CentOS 7.3-7.5 do
12458 # not honor the -ignore_readdir_race option correctly. So we call
12459 # error_ignore() rather than error() in these cases. See LU-11152.
12460 error_133() {
12461         if (find --version; do_facet mds1 find --version) |
12462                 grep -q '\b4\.5\.1[1-4]\b'; then
12463                 error_ignore LU-11152 "$@"
12464         else
12465                 error "$@"
12466         fi
12467 }
12468
12469 test_133f() {
12470         # First without trusting modes.
12471         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
12472         echo "proc_dirs='$proc_dirs'"
12473         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
12474         find $proc_dirs -exec cat '{}' \; &> /dev/null
12475
12476         # Second verifying readability.
12477         $LCTL get_param -R '*' &> /dev/null
12478
12479         # Verifing writability with badarea_io.
12480         find $proc_dirs \
12481                 -ignore_readdir_race \
12482                 -type f \
12483                 -not -name force_lbug \
12484                 -not -name changelog_mask \
12485                 -exec badarea_io '{}' \; ||
12486                         error_133 "find $proc_dirs failed"
12487 }
12488 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
12489
12490 test_133g() {
12491         remote_mds_nodsh && skip "remote MDS with nodsh"
12492         remote_ost_nodsh && skip "remote OST with nodsh"
12493
12494         local facet
12495         for facet in mds1 ost1; do
12496                 local facet_ver=$(lustre_version_code $facet)
12497                 if [ $facet_ver -ge $(version_code 2.7.65) ]; then
12498                         do_facet $facet "$LCTL get_param -R '*'" &> /dev/null
12499                 else
12500                         log "$facet: too old lustre for get_param -R"
12501                 fi
12502                 if [ $facet_ver -ge $(version_code 2.5.54) ]; then
12503                         do_facet $facet "$LCTL list_param -R '*' | grep '=' |
12504                                 tr -d= | egrep -v 'force_lbug|changelog_mask' |
12505                                 xargs badarea_io" ||
12506                                         error_133 "$facet badarea_io failed"
12507                 else
12508                         skip_noexit "$facet: too old lustre for get_param -R"
12509                 fi
12510         done
12511
12512         # remount the FS in case writes/reads /proc break the FS
12513         cleanup || error "failed to unmount"
12514         setup || error "failed to setup"
12515         true
12516 }
12517 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
12518
12519 test_133h() {
12520         remote_mds_nodsh && skip "remote MDS with nodsh"
12521         remote_ost_nodsh && skip "remote OST with nodsh"
12522         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
12523                 skip "Need MDS version at least 2.9.54"
12524
12525         local facet
12526
12527         for facet in client mds1 ost1; do
12528                 local facet_proc_dirs=$(do_facet $facet \
12529                                         \\\ls -d $proc_regexp 2> /dev/null)
12530                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12531                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12532                 # Get the list of files that are missing the terminating newline
12533                 local missing=($(do_facet $facet \
12534                         find ${facet_proc_dirs} -type f \|              \
12535                                 while read F\; do                       \
12536                                         awk -v FS='\v' -v RS='\v\v'     \
12537                                         "'END { if(NR>0 &&              \
12538                                         \\\$NF !~ /.*\\\n\$/)           \
12539                                                 print FILENAME}'"       \
12540                                         '\$F'\;                         \
12541                                 done 2>/dev/null))
12542                 [ ${#missing[*]} -eq 0 ] ||
12543                         error "files do not end with newline: ${missing[*]}"
12544         done
12545 }
12546 run_test 133h "Proc files should end with newlines"
12547
12548 test_134a() {
12549         remote_mds_nodsh && skip "remote MDS with nodsh"
12550         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12551                 skip "Need MDS version at least 2.7.54"
12552
12553         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12554         cancel_lru_locks mdc
12555
12556         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
12557         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12558         [ $unused -eq 0 ] || error "$unused locks are not cleared"
12559
12560         local nr=1000
12561         createmany -o $DIR/$tdir/f $nr ||
12562                 error "failed to create $nr files in $DIR/$tdir"
12563         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12564
12565         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
12566         do_facet mds1 $LCTL set_param fail_loc=0x327
12567         do_facet mds1 $LCTL set_param fail_val=500
12568         touch $DIR/$tdir/m
12569
12570         echo "sleep 10 seconds ..."
12571         sleep 10
12572         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
12573
12574         do_facet mds1 $LCTL set_param fail_loc=0
12575         do_facet mds1 $LCTL set_param fail_val=0
12576         [ $lck_cnt -lt $unused ] ||
12577                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
12578
12579         rm $DIR/$tdir/m
12580         unlinkmany $DIR/$tdir/f $nr
12581 }
12582 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
12583
12584 test_134b() {
12585         remote_mds_nodsh && skip "remote MDS with nodsh"
12586         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12587                 skip "Need MDS version at least 2.7.54"
12588
12589         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12590         cancel_lru_locks mdc
12591
12592         local low_wm=$(do_facet mds1 $LCTL get_param -n \
12593                         ldlm.lock_reclaim_threshold_mb)
12594         # disable reclaim temporarily
12595         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
12596
12597         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
12598         do_facet mds1 $LCTL set_param fail_loc=0x328
12599         do_facet mds1 $LCTL set_param fail_val=500
12600
12601         $LCTL set_param debug=+trace
12602
12603         local nr=600
12604         createmany -o $DIR/$tdir/f $nr &
12605         local create_pid=$!
12606
12607         echo "Sleep $TIMEOUT seconds ..."
12608         sleep $TIMEOUT
12609         if ! ps -p $create_pid  > /dev/null 2>&1; then
12610                 do_facet mds1 $LCTL set_param fail_loc=0
12611                 do_facet mds1 $LCTL set_param fail_val=0
12612                 do_facet mds1 $LCTL set_param \
12613                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
12614                 error "createmany finished incorrectly!"
12615         fi
12616         do_facet mds1 $LCTL set_param fail_loc=0
12617         do_facet mds1 $LCTL set_param fail_val=0
12618         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
12619         wait $create_pid || return 1
12620
12621         unlinkmany $DIR/$tdir/f $nr
12622 }
12623 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
12624
12625 test_135() {
12626         remote_mds_nodsh && skip "remote MDS with nodsh"
12627         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
12628                 skip "Need MDS version at least 2.13.50"
12629         local fname
12630
12631         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12632
12633 #define OBD_FAIL_PLAIN_RECORDS 0x1319
12634         #set only one record at plain llog
12635         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1319 fail_val=1
12636
12637         #fill already existed plain llog each 64767
12638         #wrapping whole catalog
12639         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
12640
12641         createmany -o $DIR/$tdir/$tfile_ 64700
12642         for (( i = 0; i < 64700; i = i + 2 ))
12643         do
12644                 rm $DIR/$tdir/$tfile_$i &
12645                 rm $DIR/$tdir/$tfile_$((i + 1)) &
12646                 local pid=$!
12647                 wait $pid
12648         done
12649
12650         #waiting osp synchronization
12651         wait_delete_completed
12652 }
12653 run_test 135 "Race catalog processing"
12654
12655 test_136() {
12656         remote_mds_nodsh && skip "remote MDS with nodsh"
12657         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
12658                 skip "Need MDS version at least 2.13.50"
12659         local fname
12660
12661         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12662         $SETSTRIPE -c 1 -i 0 $DIR/$tdir || error "failed to set striping"
12663         #set only one record at plain llog
12664 #define OBD_FAIL_CATALOG_FULL_CHECK                0x131a
12665         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x131a fail_val=1
12666
12667         #fill already existed 2 plain llogs each 64767
12668         #wrapping whole catalog
12669         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
12670         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 3 / 2))
12671         wait_delete_completed
12672
12673         createmany -o $DIR/$tdir/$tfile_ 10
12674         sleep 25
12675
12676         do_facet $SINGLEMDS $LCTL set_param fail_val=3
12677         for (( i = 0; i < 10; i = i + 3 ))
12678         do
12679                 rm $DIR/$tdir/$tfile_$i &
12680                 rm $DIR/$tdir/$tfile_$((i + 1)) &
12681                 local pid=$!
12682                 wait $pid
12683                 sleep 7
12684                 rm $DIR/$tdir/$tfile_$((i + 2)) &
12685         done
12686
12687         #waiting osp synchronization
12688         wait_delete_completed
12689 }
12690 run_test 136 "Race catalog processing 2"
12691
12692 test_140() { #bug-17379
12693         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12694
12695         test_mkdir $DIR/$tdir
12696         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
12697         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
12698
12699         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
12700         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
12701         local i=0
12702         while i=$((i + 1)); do
12703                 test_mkdir $i
12704                 cd $i || error "Changing to $i"
12705                 ln -s ../stat stat || error "Creating stat symlink"
12706                 # Read the symlink until ELOOP present,
12707                 # not LBUGing the system is considered success,
12708                 # we didn't overrun the stack.
12709                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
12710                 if [ $ret -ne 0 ]; then
12711                         if [ $ret -eq 40 ]; then
12712                                 break  # -ELOOP
12713                         else
12714                                 error "Open stat symlink"
12715                                         return
12716                         fi
12717                 fi
12718         done
12719         i=$((i - 1))
12720         echo "The symlink depth = $i"
12721         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
12722                 error "Invalid symlink depth"
12723
12724         # Test recursive symlink
12725         ln -s symlink_self symlink_self
12726         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
12727         echo "open symlink_self returns $ret"
12728         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
12729 }
12730 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
12731
12732 test_150() {
12733         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12734
12735         local TF="$TMP/$tfile"
12736
12737         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12738         cp $TF $DIR/$tfile
12739         cancel_lru_locks $OSC
12740         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
12741         remount_client $MOUNT
12742         df -P $MOUNT
12743         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
12744
12745         $TRUNCATE $TF 6000
12746         $TRUNCATE $DIR/$tfile 6000
12747         cancel_lru_locks $OSC
12748         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
12749
12750         echo "12345" >>$TF
12751         echo "12345" >>$DIR/$tfile
12752         cancel_lru_locks $OSC
12753         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
12754
12755         echo "12345" >>$TF
12756         echo "12345" >>$DIR/$tfile
12757         cancel_lru_locks $OSC
12758         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
12759
12760         rm -f $TF
12761         true
12762 }
12763 run_test 150 "truncate/append tests"
12764
12765 #LU-2902 roc_hit was not able to read all values from lproc
12766 function roc_hit_init() {
12767         local list=$(comma_list $(osts_nodes))
12768         local dir=$DIR/$tdir-check
12769         local file=$dir/$tfile
12770         local BEFORE
12771         local AFTER
12772         local idx
12773
12774         test_mkdir $dir
12775         #use setstripe to do a write to every ost
12776         for i in $(seq 0 $((OSTCOUNT-1))); do
12777                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
12778                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
12779                 idx=$(printf %04x $i)
12780                 BEFORE=$(get_osd_param $list *OST*$idx stats |
12781                         awk '$1 == "cache_access" {sum += $7}
12782                                 END { printf("%0.0f", sum) }')
12783
12784                 cancel_lru_locks osc
12785                 cat $file >/dev/null
12786
12787                 AFTER=$(get_osd_param $list *OST*$idx stats |
12788                         awk '$1 == "cache_access" {sum += $7}
12789                                 END { printf("%0.0f", sum) }')
12790
12791                 echo BEFORE:$BEFORE AFTER:$AFTER
12792                 if ! let "AFTER - BEFORE == 4"; then
12793                         rm -rf $dir
12794                         error "roc_hit is not safe to use"
12795                 fi
12796                 rm $file
12797         done
12798
12799         rm -rf $dir
12800 }
12801
12802 function roc_hit() {
12803         local list=$(comma_list $(osts_nodes))
12804         echo $(get_osd_param $list '' stats |
12805                 awk '$1 == "cache_hit" {sum += $7}
12806                         END { printf("%0.0f", sum) }')
12807 }
12808
12809 function set_cache() {
12810         local on=1
12811
12812         if [ "$2" == "off" ]; then
12813                 on=0;
12814         fi
12815         local list=$(comma_list $(osts_nodes))
12816         set_osd_param $list '' $1_cache_enable $on
12817
12818         cancel_lru_locks osc
12819 }
12820
12821 test_151() {
12822         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12823         remote_ost_nodsh && skip "remote OST with nodsh"
12824
12825         local CPAGES=3
12826         local list=$(comma_list $(osts_nodes))
12827
12828         # check whether obdfilter is cache capable at all
12829         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
12830                 skip "not cache-capable obdfilter"
12831         fi
12832
12833         # check cache is enabled on all obdfilters
12834         if get_osd_param $list '' read_cache_enable | grep 0; then
12835                 skip "oss cache is disabled"
12836         fi
12837
12838         set_osd_param $list '' writethrough_cache_enable 1
12839
12840         # check write cache is enabled on all obdfilters
12841         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
12842                 skip "oss write cache is NOT enabled"
12843         fi
12844
12845         roc_hit_init
12846
12847         #define OBD_FAIL_OBD_NO_LRU  0x609
12848         do_nodes $list $LCTL set_param fail_loc=0x609
12849
12850         # pages should be in the case right after write
12851         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
12852                 error "dd failed"
12853
12854         local BEFORE=$(roc_hit)
12855         cancel_lru_locks osc
12856         cat $DIR/$tfile >/dev/null
12857         local AFTER=$(roc_hit)
12858
12859         do_nodes $list $LCTL set_param fail_loc=0
12860
12861         if ! let "AFTER - BEFORE == CPAGES"; then
12862                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12863         fi
12864
12865         cancel_lru_locks osc
12866         # invalidates OST cache
12867         do_nodes $list "echo 1 > /proc/sys/vm/drop_caches"
12868         set_osd_param $list '' read_cache_enable 0
12869         cat $DIR/$tfile >/dev/null
12870
12871         # now data shouldn't be found in the cache
12872         BEFORE=$(roc_hit)
12873         cancel_lru_locks osc
12874         cat $DIR/$tfile >/dev/null
12875         AFTER=$(roc_hit)
12876         if let "AFTER - BEFORE != 0"; then
12877                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12878         fi
12879
12880         set_osd_param $list '' read_cache_enable 1
12881         rm -f $DIR/$tfile
12882 }
12883 run_test 151 "test cache on oss and controls ==============================="
12884
12885 test_152() {
12886         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12887
12888         local TF="$TMP/$tfile"
12889
12890         # simulate ENOMEM during write
12891 #define OBD_FAIL_OST_NOMEM      0x226
12892         lctl set_param fail_loc=0x80000226
12893         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12894         cp $TF $DIR/$tfile
12895         sync || error "sync failed"
12896         lctl set_param fail_loc=0
12897
12898         # discard client's cache
12899         cancel_lru_locks osc
12900
12901         # simulate ENOMEM during read
12902         lctl set_param fail_loc=0x80000226
12903         cmp $TF $DIR/$tfile || error "cmp failed"
12904         lctl set_param fail_loc=0
12905
12906         rm -f $TF
12907 }
12908 run_test 152 "test read/write with enomem ============================"
12909
12910 test_153() {
12911         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
12912 }
12913 run_test 153 "test if fdatasync does not crash ======================="
12914
12915 dot_lustre_fid_permission_check() {
12916         local fid=$1
12917         local ffid=$MOUNT/.lustre/fid/$fid
12918         local test_dir=$2
12919
12920         echo "stat fid $fid"
12921         stat $ffid > /dev/null || error "stat $ffid failed."
12922         echo "touch fid $fid"
12923         touch $ffid || error "touch $ffid failed."
12924         echo "write to fid $fid"
12925         cat /etc/hosts > $ffid || error "write $ffid failed."
12926         echo "read fid $fid"
12927         diff /etc/hosts $ffid || error "read $ffid failed."
12928         echo "append write to fid $fid"
12929         cat /etc/hosts >> $ffid || error "append write $ffid failed."
12930         echo "rename fid $fid"
12931         mv $ffid $test_dir/$tfile.1 &&
12932                 error "rename $ffid to $tfile.1 should fail."
12933         touch $test_dir/$tfile.1
12934         mv $test_dir/$tfile.1 $ffid &&
12935                 error "rename $tfile.1 to $ffid should fail."
12936         rm -f $test_dir/$tfile.1
12937         echo "truncate fid $fid"
12938         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
12939         echo "link fid $fid"
12940         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
12941         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
12942                 echo "setfacl fid $fid"
12943                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
12944                 echo "getfacl fid $fid"
12945                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
12946         fi
12947         echo "unlink fid $fid"
12948         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
12949         echo "mknod fid $fid"
12950         mknod $ffid c 1 3 && error "mknod $ffid should fail."
12951
12952         fid=[0xf00000400:0x1:0x0]
12953         ffid=$MOUNT/.lustre/fid/$fid
12954
12955         echo "stat non-exist fid $fid"
12956         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
12957         echo "write to non-exist fid $fid"
12958         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
12959         echo "link new fid $fid"
12960         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
12961
12962         mkdir -p $test_dir/$tdir
12963         touch $test_dir/$tdir/$tfile
12964         fid=$($LFS path2fid $test_dir/$tdir)
12965         rc=$?
12966         [ $rc -ne 0 ] &&
12967                 error "error: could not get fid for $test_dir/$dir/$tfile."
12968
12969         ffid=$MOUNT/.lustre/fid/$fid
12970
12971         echo "ls $fid"
12972         ls $ffid > /dev/null || error "ls $ffid failed."
12973         echo "touch $fid/$tfile.1"
12974         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
12975
12976         echo "touch $MOUNT/.lustre/fid/$tfile"
12977         touch $MOUNT/.lustre/fid/$tfile && \
12978                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
12979
12980         echo "setxattr to $MOUNT/.lustre/fid"
12981         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
12982
12983         echo "listxattr for $MOUNT/.lustre/fid"
12984         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
12985
12986         echo "delxattr from $MOUNT/.lustre/fid"
12987         setfattr -x trusted.name1 $MOUNT/.lustre/fid
12988
12989         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
12990         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
12991                 error "touch invalid fid should fail."
12992
12993         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
12994         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
12995                 error "touch non-normal fid should fail."
12996
12997         echo "rename $tdir to $MOUNT/.lustre/fid"
12998         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
12999                 error "rename to $MOUNT/.lustre/fid should fail."
13000
13001         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
13002         then            # LU-3547
13003                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
13004                 local new_obf_mode=777
13005
13006                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
13007                 chmod $new_obf_mode $DIR/.lustre/fid ||
13008                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
13009
13010                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
13011                 [ $obf_mode -eq $new_obf_mode ] ||
13012                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
13013
13014                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
13015                 chmod $old_obf_mode $DIR/.lustre/fid ||
13016                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
13017         fi
13018
13019         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
13020         fid=$($LFS path2fid $test_dir/$tfile-2)
13021
13022         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
13023         then # LU-5424
13024                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
13025                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
13026                         error "create lov data thru .lustre failed"
13027         fi
13028         echo "cp /etc/passwd $test_dir/$tfile-2"
13029         cp /etc/passwd $test_dir/$tfile-2 ||
13030                 error "copy to $test_dir/$tfile-2 failed."
13031         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
13032         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
13033                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
13034
13035         rm -rf $test_dir/tfile.lnk
13036         rm -rf $test_dir/$tfile-2
13037 }
13038
13039 test_154A() {
13040         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13041                 skip "Need MDS version at least 2.4.1"
13042
13043         local tf=$DIR/$tfile
13044         touch $tf
13045
13046         local fid=$($LFS path2fid $tf)
13047         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
13048
13049         # check that we get the same pathname back
13050         local found=$($LFS fid2path $MOUNT "$fid")
13051         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
13052         [ "$found" == "$tf" ] ||
13053                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
13054 }
13055 run_test 154A "lfs path2fid and fid2path basic checks"
13056
13057 test_154B() {
13058         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13059                 skip "Need MDS version at least 2.4.1"
13060
13061         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
13062         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
13063         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
13064         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
13065
13066         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
13067         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
13068
13069         # check that we get the same pathname
13070         echo "PFID: $PFID, name: $name"
13071         local FOUND=$($LFS fid2path $MOUNT "$PFID")
13072         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
13073         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
13074                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
13075
13076         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
13077 }
13078 run_test 154B "verify the ll_decode_linkea tool"
13079
13080 test_154a() {
13081         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13082         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13083         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
13084                 skip "Need MDS version at least 2.2.51"
13085         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
13086
13087         cp /etc/hosts $DIR/$tfile
13088
13089         fid=$($LFS path2fid $DIR/$tfile)
13090         rc=$?
13091         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
13092
13093         dot_lustre_fid_permission_check "$fid" $DIR ||
13094                 error "dot lustre permission check $fid failed"
13095
13096         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
13097
13098         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
13099
13100         touch $MOUNT/.lustre/file &&
13101                 error "creation is not allowed under .lustre"
13102
13103         mkdir $MOUNT/.lustre/dir &&
13104                 error "mkdir is not allowed under .lustre"
13105
13106         rm -rf $DIR/$tfile
13107 }
13108 run_test 154a "Open-by-FID"
13109
13110 test_154b() {
13111         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13112         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13113         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13114         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
13115                 skip "Need MDS version at least 2.2.51"
13116
13117         local remote_dir=$DIR/$tdir/remote_dir
13118         local MDTIDX=1
13119         local rc=0
13120
13121         mkdir -p $DIR/$tdir
13122         $LFS mkdir -i $MDTIDX $remote_dir ||
13123                 error "create remote directory failed"
13124
13125         cp /etc/hosts $remote_dir/$tfile
13126
13127         fid=$($LFS path2fid $remote_dir/$tfile)
13128         rc=$?
13129         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
13130
13131         dot_lustre_fid_permission_check "$fid" $remote_dir ||
13132                 error "dot lustre permission check $fid failed"
13133         rm -rf $DIR/$tdir
13134 }
13135 run_test 154b "Open-by-FID for remote directory"
13136
13137 test_154c() {
13138         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13139                 skip "Need MDS version at least 2.4.1"
13140
13141         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
13142         local FID1=$($LFS path2fid $DIR/$tfile.1)
13143         local FID2=$($LFS path2fid $DIR/$tfile.2)
13144         local FID3=$($LFS path2fid $DIR/$tfile.3)
13145
13146         local N=1
13147         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
13148                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
13149                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
13150                 local want=FID$N
13151                 [ "$FID" = "${!want}" ] ||
13152                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
13153                 N=$((N + 1))
13154         done
13155
13156         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
13157         do
13158                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
13159                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
13160                 N=$((N + 1))
13161         done
13162 }
13163 run_test 154c "lfs path2fid and fid2path multiple arguments"
13164
13165 test_154d() {
13166         remote_mds_nodsh && skip "remote MDS with nodsh"
13167         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
13168                 skip "Need MDS version at least 2.5.53"
13169
13170         if remote_mds; then
13171                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
13172         else
13173                 nid="0@lo"
13174         fi
13175         local proc_ofile="mdt.*.exports.'$nid'.open_files"
13176         local fd
13177         local cmd
13178
13179         rm -f $DIR/$tfile
13180         touch $DIR/$tfile
13181
13182         local fid=$($LFS path2fid $DIR/$tfile)
13183         # Open the file
13184         fd=$(free_fd)
13185         cmd="exec $fd<$DIR/$tfile"
13186         eval $cmd
13187         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
13188         echo "$fid_list" | grep "$fid"
13189         rc=$?
13190
13191         cmd="exec $fd>/dev/null"
13192         eval $cmd
13193         if [ $rc -ne 0 ]; then
13194                 error "FID $fid not found in open files list $fid_list"
13195         fi
13196 }
13197 run_test 154d "Verify open file fid"
13198
13199 test_154e()
13200 {
13201         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
13202                 skip "Need MDS version at least 2.6.50"
13203
13204         if ls -a $MOUNT | grep -q '^\.lustre$'; then
13205                 error ".lustre returned by readdir"
13206         fi
13207 }
13208 run_test 154e ".lustre is not returned by readdir"
13209
13210 test_154f() {
13211         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13212
13213         # create parent directory on a single MDT to avoid cross-MDT hardlinks
13214         test_mkdir -p -c1 $DIR/$tdir/d
13215         # test dirs inherit from its stripe
13216         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
13217         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
13218         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
13219         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
13220         touch $DIR/f
13221
13222         # get fid of parents
13223         local FID0=$($LFS path2fid $DIR/$tdir/d)
13224         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
13225         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
13226         local FID3=$($LFS path2fid $DIR)
13227
13228         # check that path2fid --parents returns expected <parent_fid>/name
13229         # 1) test for a directory (single parent)
13230         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
13231         [ "$parent" == "$FID0/foo1" ] ||
13232                 error "expected parent: $FID0/foo1, got: $parent"
13233
13234         # 2) test for a file with nlink > 1 (multiple parents)
13235         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
13236         echo "$parent" | grep -F "$FID1/$tfile" ||
13237                 error "$FID1/$tfile not returned in parent list"
13238         echo "$parent" | grep -F "$FID2/link" ||
13239                 error "$FID2/link not returned in parent list"
13240
13241         # 3) get parent by fid
13242         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
13243         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13244         echo "$parent" | grep -F "$FID1/$tfile" ||
13245                 error "$FID1/$tfile not returned in parent list (by fid)"
13246         echo "$parent" | grep -F "$FID2/link" ||
13247                 error "$FID2/link not returned in parent list (by fid)"
13248
13249         # 4) test for entry in root directory
13250         parent=$($LFS path2fid --parents $DIR/f)
13251         echo "$parent" | grep -F "$FID3/f" ||
13252                 error "$FID3/f not returned in parent list"
13253
13254         # 5) test it on root directory
13255         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
13256                 error "$MOUNT should not have parents"
13257
13258         # enable xattr caching and check that linkea is correctly updated
13259         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
13260         save_lustre_params client "llite.*.xattr_cache" > $save
13261         lctl set_param llite.*.xattr_cache 1
13262
13263         # 6.1) linkea update on rename
13264         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
13265
13266         # get parents by fid
13267         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13268         # foo1 should no longer be returned in parent list
13269         echo "$parent" | grep -F "$FID1" &&
13270                 error "$FID1 should no longer be in parent list"
13271         # the new path should appear
13272         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
13273                 error "$FID2/$tfile.moved is not in parent list"
13274
13275         # 6.2) linkea update on unlink
13276         rm -f $DIR/$tdir/d/foo2/link
13277         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13278         # foo2/link should no longer be returned in parent list
13279         echo "$parent" | grep -F "$FID2/link" &&
13280                 error "$FID2/link should no longer be in parent list"
13281         true
13282
13283         rm -f $DIR/f
13284         restore_lustre_params < $save
13285         rm -f $save
13286 }
13287 run_test 154f "get parent fids by reading link ea"
13288
13289 test_154g()
13290 {
13291         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13292         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
13293            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
13294                 skip "Need MDS version at least 2.6.92"
13295
13296         mkdir -p $DIR/$tdir
13297         llapi_fid_test -d $DIR/$tdir
13298 }
13299 run_test 154g "various llapi FID tests"
13300
13301 test_155_small_load() {
13302     local temp=$TMP/$tfile
13303     local file=$DIR/$tfile
13304
13305     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
13306         error "dd of=$temp bs=6096 count=1 failed"
13307     cp $temp $file
13308     cancel_lru_locks $OSC
13309     cmp $temp $file || error "$temp $file differ"
13310
13311     $TRUNCATE $temp 6000
13312     $TRUNCATE $file 6000
13313     cmp $temp $file || error "$temp $file differ (truncate1)"
13314
13315     echo "12345" >>$temp
13316     echo "12345" >>$file
13317     cmp $temp $file || error "$temp $file differ (append1)"
13318
13319     echo "12345" >>$temp
13320     echo "12345" >>$file
13321     cmp $temp $file || error "$temp $file differ (append2)"
13322
13323     rm -f $temp $file
13324     true
13325 }
13326
13327 test_155_big_load() {
13328         remote_ost_nodsh && skip "remote OST with nodsh"
13329
13330         local temp=$TMP/$tfile
13331         local file=$DIR/$tfile
13332
13333         free_min_max
13334         local cache_size=$(do_facet ost$((MAXI+1)) \
13335                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
13336         local large_file_size=$((cache_size * 2))
13337
13338         echo "OSS cache size: $cache_size KB"
13339         echo "Large file size: $large_file_size KB"
13340
13341         [ $MAXV -le $large_file_size ] &&
13342                 skip_env "max available OST size needs > $large_file_size KB"
13343
13344         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
13345
13346         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
13347                 error "dd of=$temp bs=$large_file_size count=1k failed"
13348         cp $temp $file
13349         ls -lh $temp $file
13350         cancel_lru_locks osc
13351         cmp $temp $file || error "$temp $file differ"
13352
13353         rm -f $temp $file
13354         true
13355 }
13356
13357 save_writethrough() {
13358         local facets=$(get_facets OST)
13359
13360         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
13361 }
13362
13363 test_155a() {
13364         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13365
13366         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13367
13368         save_writethrough $p
13369
13370         set_cache read on
13371         set_cache writethrough on
13372         test_155_small_load
13373         restore_lustre_params < $p
13374         rm -f $p
13375 }
13376 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
13377
13378 test_155b() {
13379         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13380
13381         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13382
13383         save_writethrough $p
13384
13385         set_cache read on
13386         set_cache writethrough off
13387         test_155_small_load
13388         restore_lustre_params < $p
13389         rm -f $p
13390 }
13391 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
13392
13393 test_155c() {
13394         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13395
13396         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13397
13398         save_writethrough $p
13399
13400         set_cache read off
13401         set_cache writethrough on
13402         test_155_small_load
13403         restore_lustre_params < $p
13404         rm -f $p
13405 }
13406 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
13407
13408 test_155d() {
13409         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13410
13411         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13412
13413         save_writethrough $p
13414
13415         set_cache read off
13416         set_cache writethrough off
13417         test_155_small_load
13418         restore_lustre_params < $p
13419         rm -f $p
13420 }
13421 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
13422
13423 test_155e() {
13424         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13425
13426         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13427
13428         save_writethrough $p
13429
13430         set_cache read on
13431         set_cache writethrough on
13432         test_155_big_load
13433         restore_lustre_params < $p
13434         rm -f $p
13435 }
13436 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
13437
13438 test_155f() {
13439         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13440
13441         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13442
13443         save_writethrough $p
13444
13445         set_cache read on
13446         set_cache writethrough off
13447         test_155_big_load
13448         restore_lustre_params < $p
13449         rm -f $p
13450 }
13451 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
13452
13453 test_155g() {
13454         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13455
13456         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13457
13458         save_writethrough $p
13459
13460         set_cache read off
13461         set_cache writethrough on
13462         test_155_big_load
13463         restore_lustre_params < $p
13464         rm -f $p
13465 }
13466 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
13467
13468 test_155h() {
13469         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13470
13471         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13472
13473         save_writethrough $p
13474
13475         set_cache read off
13476         set_cache writethrough off
13477         test_155_big_load
13478         restore_lustre_params < $p
13479         rm -f $p
13480 }
13481 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
13482
13483 test_156() {
13484         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13485         remote_ost_nodsh && skip "remote OST with nodsh"
13486         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
13487                 skip "stats not implemented on old servers"
13488         [ "$ost1_FSTYPE" = "zfs" ] &&
13489                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
13490
13491         local CPAGES=3
13492         local BEFORE
13493         local AFTER
13494         local file="$DIR/$tfile"
13495         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13496
13497         save_writethrough $p
13498         roc_hit_init
13499
13500         log "Turn on read and write cache"
13501         set_cache read on
13502         set_cache writethrough on
13503
13504         log "Write data and read it back."
13505         log "Read should be satisfied from the cache."
13506         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13507         BEFORE=$(roc_hit)
13508         cancel_lru_locks osc
13509         cat $file >/dev/null
13510         AFTER=$(roc_hit)
13511         if ! let "AFTER - BEFORE == CPAGES"; then
13512                 error "NOT IN CACHE (2): before: $BEFORE, after: $AFTER"
13513         else
13514                 log "cache hits: before: $BEFORE, after: $AFTER"
13515         fi
13516
13517         log "Read again; it should be satisfied from the cache."
13518         BEFORE=$AFTER
13519         cancel_lru_locks osc
13520         cat $file >/dev/null
13521         AFTER=$(roc_hit)
13522         if ! let "AFTER - BEFORE == CPAGES"; then
13523                 error "NOT IN CACHE (3): before: $BEFORE, after: $AFTER"
13524         else
13525                 log "cache hits:: before: $BEFORE, after: $AFTER"
13526         fi
13527
13528         log "Turn off the read cache and turn on the write cache"
13529         set_cache read off
13530         set_cache writethrough on
13531
13532         log "Read again; it should be satisfied from the cache."
13533         BEFORE=$(roc_hit)
13534         cancel_lru_locks osc
13535         cat $file >/dev/null
13536         AFTER=$(roc_hit)
13537         if ! let "AFTER - BEFORE == CPAGES"; then
13538                 error "NOT IN CACHE (4): before: $BEFORE, after: $AFTER"
13539         else
13540                 log "cache hits:: before: $BEFORE, after: $AFTER"
13541         fi
13542
13543         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
13544                 # > 2.12.56 uses pagecache if cached
13545                 log "Read again; it should not be satisfied from the cache."
13546                 BEFORE=$AFTER
13547                 cancel_lru_locks osc
13548                 cat $file >/dev/null
13549                 AFTER=$(roc_hit)
13550                 if ! let "AFTER - BEFORE == 0"; then
13551                         error "IN CACHE (5): before: $BEFORE, after: $AFTER"
13552                 else
13553                         log "cache hits:: before: $BEFORE, after: $AFTER"
13554                 fi
13555         fi
13556
13557         log "Write data and read it back."
13558         log "Read should be satisfied from the cache."
13559         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13560         BEFORE=$(roc_hit)
13561         cancel_lru_locks osc
13562         cat $file >/dev/null
13563         AFTER=$(roc_hit)
13564         if ! let "AFTER - BEFORE == CPAGES"; then
13565                 error "NOT IN CACHE (6): before: $BEFORE, after: $AFTER"
13566         else
13567                 log "cache hits:: before: $BEFORE, after: $AFTER"
13568         fi
13569
13570         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
13571                 # > 2.12.56 uses pagecache if cached
13572                 log "Read again; it should not be satisfied from the cache."
13573                 BEFORE=$AFTER
13574                 cancel_lru_locks osc
13575                 cat $file >/dev/null
13576                 AFTER=$(roc_hit)
13577                 if ! let "AFTER - BEFORE == 0"; then
13578                         error "IN CACHE (7): before: $BEFORE, after: $AFTER"
13579                 else
13580                         log "cache hits:: before: $BEFORE, after: $AFTER"
13581                 fi
13582         fi
13583
13584         log "Turn off read and write cache"
13585         set_cache read off
13586         set_cache writethrough off
13587
13588         log "Write data and read it back"
13589         log "It should not be satisfied from the cache."
13590         rm -f $file
13591         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13592         cancel_lru_locks osc
13593         BEFORE=$(roc_hit)
13594         cat $file >/dev/null
13595         AFTER=$(roc_hit)
13596         if ! let "AFTER - BEFORE == 0"; then
13597                 error_ignore bz20762 "IN CACHE (8):before:$BEFORE,after:$AFTER"
13598         else
13599                 log "cache hits:: before: $BEFORE, after: $AFTER"
13600         fi
13601
13602         log "Turn on the read cache and turn off the write cache"
13603         set_cache read on
13604         set_cache writethrough off
13605
13606         log "Write data and read it back"
13607         log "It should not be satisfied from the cache."
13608         rm -f $file
13609         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13610         BEFORE=$(roc_hit)
13611         cancel_lru_locks osc
13612         cat $file >/dev/null
13613         AFTER=$(roc_hit)
13614         if ! let "AFTER - BEFORE == 0"; then
13615                 error_ignore bz20762 "IN CACHE (9):before:$BEFORE,after:$AFTER"
13616         else
13617                 log "cache hits:: before: $BEFORE, after: $AFTER"
13618         fi
13619
13620         log "Read again; it should be satisfied from the cache."
13621         BEFORE=$(roc_hit)
13622         cancel_lru_locks osc
13623         cat $file >/dev/null
13624         AFTER=$(roc_hit)
13625         if ! let "AFTER - BEFORE == CPAGES"; then
13626                 error "NOT IN CACHE (1): before: $BEFORE, after: $AFTER"
13627         else
13628                 log "cache hits:: before: $BEFORE, after: $AFTER"
13629         fi
13630
13631         restore_lustre_params < $p
13632         rm -f $p $file
13633 }
13634 run_test 156 "Verification of tunables"
13635
13636 test_160a() {
13637         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13638         remote_mds_nodsh && skip "remote MDS with nodsh"
13639         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13640                 skip "Need MDS version at least 2.2.0"
13641
13642         changelog_register || error "changelog_register failed"
13643         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13644         changelog_users $SINGLEMDS | grep -q $cl_user ||
13645                 error "User $cl_user not found in changelog_users"
13646
13647         # change something
13648         test_mkdir -p $DIR/$tdir/pics/2008/zachy
13649         changelog_clear 0 || error "changelog_clear failed"
13650         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
13651         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
13652         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
13653         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
13654         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
13655         rm $DIR/$tdir/pics/desktop.jpg
13656
13657         changelog_dump | tail -10
13658
13659         echo "verifying changelog mask"
13660         changelog_chmask "-MKDIR"
13661         changelog_chmask "-CLOSE"
13662
13663         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
13664         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
13665
13666         changelog_chmask "+MKDIR"
13667         changelog_chmask "+CLOSE"
13668
13669         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
13670         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
13671
13672         changelog_dump | tail -10
13673         MKDIRS=$(changelog_dump | grep -c "MKDIR")
13674         CLOSES=$(changelog_dump | grep -c "CLOSE")
13675         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
13676         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
13677
13678         # verify contents
13679         echo "verifying target fid"
13680         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
13681         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
13682         [ "$fidc" == "$fidf" ] ||
13683                 error "changelog '$tfile' fid $fidc != file fid $fidf"
13684         echo "verifying parent fid"
13685         # The FID returned from the Changelog may be the directory shard on
13686         # a different MDT, and not the FID returned by path2fid on the parent.
13687         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
13688         # since this is what will matter when recreating this file in the tree.
13689         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
13690         local pathp=$($LFS fid2path $MOUNT "$fidp")
13691         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
13692                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
13693
13694         echo "getting records for $cl_user"
13695         changelog_users $SINGLEMDS
13696         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
13697         local nclr=3
13698         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
13699                 error "changelog_clear failed"
13700         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
13701         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
13702         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
13703                 error "user index expect $user_rec1 + $nclr != $user_rec2"
13704
13705         local min0_rec=$(changelog_users $SINGLEMDS |
13706                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
13707         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
13708                           awk '{ print $1; exit; }')
13709
13710         changelog_dump | tail -n 5
13711         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
13712         [ $first_rec == $((min0_rec + 1)) ] ||
13713                 error "first index should be $min0_rec + 1 not $first_rec"
13714
13715         # LU-3446 changelog index reset on MDT restart
13716         local cur_rec1=$(changelog_users $SINGLEMDS |
13717                          awk '/^current.index:/ { print $NF }')
13718         changelog_clear 0 ||
13719                 error "clear all changelog records for $cl_user failed"
13720         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
13721         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
13722                 error "Fail to start $SINGLEMDS"
13723         local cur_rec2=$(changelog_users $SINGLEMDS |
13724                          awk '/^current.index:/ { print $NF }')
13725         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
13726         [ $cur_rec1 == $cur_rec2 ] ||
13727                 error "current index should be $cur_rec1 not $cur_rec2"
13728
13729         echo "verifying users from this test are deregistered"
13730         changelog_deregister || error "changelog_deregister failed"
13731         changelog_users $SINGLEMDS | grep -q $cl_user &&
13732                 error "User '$cl_user' still in changelog_users"
13733
13734         # lctl get_param -n mdd.*.changelog_users
13735         # current index: 144
13736         # ID    index (idle seconds)
13737         # cl3   144 (2)
13738         if ! changelog_users $SINGLEMDS | grep "^cl"; then
13739                 # this is the normal case where all users were deregistered
13740                 # make sure no new records are added when no users are present
13741                 local last_rec1=$(changelog_users $SINGLEMDS |
13742                                   awk '/^current.index:/ { print $NF }')
13743                 touch $DIR/$tdir/chloe
13744                 local last_rec2=$(changelog_users $SINGLEMDS |
13745                                   awk '/^current.index:/ { print $NF }')
13746                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
13747                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
13748         else
13749                 # any changelog users must be leftovers from a previous test
13750                 changelog_users $SINGLEMDS
13751                 echo "other changelog users; can't verify off"
13752         fi
13753 }
13754 run_test 160a "changelog sanity"
13755
13756 test_160b() { # LU-3587
13757         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13758         remote_mds_nodsh && skip "remote MDS with nodsh"
13759         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13760                 skip "Need MDS version at least 2.2.0"
13761
13762         changelog_register || error "changelog_register failed"
13763         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13764         changelog_users $SINGLEMDS | grep -q $cl_user ||
13765                 error "User '$cl_user' not found in changelog_users"
13766
13767         local longname1=$(str_repeat a 255)
13768         local longname2=$(str_repeat b 255)
13769
13770         cd $DIR
13771         echo "creating very long named file"
13772         touch $longname1 || error "create of '$longname1' failed"
13773         echo "renaming very long named file"
13774         mv $longname1 $longname2
13775
13776         changelog_dump | grep RENME | tail -n 5
13777         rm -f $longname2
13778 }
13779 run_test 160b "Verify that very long rename doesn't crash in changelog"
13780
13781 test_160c() {
13782         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13783         remote_mds_nodsh && skip "remote MDS with nodsh"
13784
13785         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
13786                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
13787                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
13788                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
13789
13790         local rc=0
13791
13792         # Registration step
13793         changelog_register || error "changelog_register failed"
13794
13795         rm -rf $DIR/$tdir
13796         mkdir -p $DIR/$tdir
13797         $MCREATE $DIR/$tdir/foo_160c
13798         changelog_chmask "-TRUNC"
13799         $TRUNCATE $DIR/$tdir/foo_160c 200
13800         changelog_chmask "+TRUNC"
13801         $TRUNCATE $DIR/$tdir/foo_160c 199
13802         changelog_dump | tail -n 5
13803         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
13804         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
13805 }
13806 run_test 160c "verify that changelog log catch the truncate event"
13807
13808 test_160d() {
13809         remote_mds_nodsh && skip "remote MDS with nodsh"
13810         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13811         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13812         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
13813                 skip "Need MDS version at least 2.7.60"
13814
13815         # Registration step
13816         changelog_register || error "changelog_register failed"
13817
13818         mkdir -p $DIR/$tdir/migrate_dir
13819         changelog_clear 0 || error "changelog_clear failed"
13820
13821         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
13822         changelog_dump | tail -n 5
13823         local migrates=$(changelog_dump | grep -c "MIGRT")
13824         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
13825 }
13826 run_test 160d "verify that changelog log catch the migrate event"
13827
13828 test_160e() {
13829         remote_mds_nodsh && skip "remote MDS with nodsh"
13830
13831         # Create a user
13832         changelog_register || error "changelog_register failed"
13833
13834         # Delete a future user (expect fail)
13835         local MDT0=$(facet_svc $SINGLEMDS)
13836         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
13837         local rc=$?
13838
13839         if [ $rc -eq 0 ]; then
13840                 error "Deleted non-existant user cl77"
13841         elif [ $rc -ne 2 ]; then
13842                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
13843         fi
13844
13845         # Clear to a bad index (1 billion should be safe)
13846         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
13847         rc=$?
13848
13849         if [ $rc -eq 0 ]; then
13850                 error "Successfully cleared to invalid CL index"
13851         elif [ $rc -ne 22 ]; then
13852                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
13853         fi
13854 }
13855 run_test 160e "changelog negative testing (should return errors)"
13856
13857 test_160f() {
13858         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13859         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13860                 skip "Need MDS version at least 2.10.56"
13861
13862         local mdts=$(comma_list $(mdts_nodes))
13863
13864         # Create a user
13865         changelog_register || error "first changelog_register failed"
13866         changelog_register || error "second changelog_register failed"
13867         local cl_users
13868         declare -A cl_user1
13869         declare -A cl_user2
13870         local user_rec1
13871         local user_rec2
13872         local i
13873
13874         # generate some changelog records to accumulate on each MDT
13875         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
13876         log "$(date +%s): creating first files"
13877         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13878                 error "create $DIR/$tdir/$tfile failed"
13879
13880         # check changelogs have been generated
13881         local start=$SECONDS
13882         local idle_time=$((MDSCOUNT * 5 + 5))
13883         local nbcl=$(changelog_dump | wc -l)
13884         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13885
13886         for param in "changelog_max_idle_time=$idle_time" \
13887                      "changelog_gc=1" \
13888                      "changelog_min_gc_interval=2" \
13889                      "changelog_min_free_cat_entries=3"; do
13890                 local MDT0=$(facet_svc $SINGLEMDS)
13891                 local var="${param%=*}"
13892                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13893
13894                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13895                 do_nodes $mdts $LCTL set_param mdd.*.$param
13896         done
13897
13898         # force cl_user2 to be idle (1st part), but also cancel the
13899         # cl_user1 records so that it is not evicted later in the test.
13900         local sleep1=$((idle_time / 2))
13901         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
13902         sleep $sleep1
13903
13904         # simulate changelog catalog almost full
13905         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
13906         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13907
13908         for i in $(seq $MDSCOUNT); do
13909                 cl_users=(${CL_USERS[mds$i]})
13910                 cl_user1[mds$i]="${cl_users[0]}"
13911                 cl_user2[mds$i]="${cl_users[1]}"
13912
13913                 [ -n "${cl_user1[mds$i]}" ] ||
13914                         error "mds$i: no user registered"
13915                 [ -n "${cl_user2[mds$i]}" ] ||
13916                         error "mds$i: only ${cl_user2[mds$i]} is registered"
13917
13918                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13919                 [ -n "$user_rec1" ] ||
13920                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13921                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13922                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13923                 [ -n "$user_rec2" ] ||
13924                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13925                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13926                      "$user_rec1 + 2 == $user_rec2"
13927                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13928                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13929                               "$user_rec1 + 2, but is $user_rec2"
13930                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13931                 [ -n "$user_rec2" ] ||
13932                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13933                 [ $user_rec1 == $user_rec2 ] ||
13934                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13935                               "$user_rec1, but is $user_rec2"
13936         done
13937
13938         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
13939         local sleep2=$((idle_time - (SECONDS - start) + 1))
13940         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
13941         sleep $sleep2
13942
13943         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
13944         # cl_user1 should be OK because it recently processed records.
13945         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
13946         createmany -m $DIR/$tdir/${tfile}b $((MDSCOUNT * 2)) ||
13947                 error "create $DIR/$tdir/${tfile}b failed"
13948
13949         # ensure gc thread is done
13950         for i in $(mdts_nodes); do
13951                 wait_update $i \
13952                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13953                         error "$i: GC-thread not done"
13954         done
13955
13956         local first_rec
13957         for i in $(seq $MDSCOUNT); do
13958                 # check cl_user1 still registered
13959                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13960                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13961                 # check cl_user2 unregistered
13962                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13963                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13964
13965                 # check changelogs are present and starting at $user_rec1 + 1
13966                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13967                 [ -n "$user_rec1" ] ||
13968                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13969                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13970                             awk '{ print $1; exit; }')
13971
13972                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13973                 [ $((user_rec1 + 1)) == $first_rec ] ||
13974                         error "mds$i: first index should be $user_rec1 + 1, " \
13975                               "but is $first_rec"
13976         done
13977 }
13978 run_test 160f "changelog garbage collect (timestamped users)"
13979
13980 test_160g() {
13981         remote_mds_nodsh && skip "remote MDS with nodsh"
13982         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13983                 skip "Need MDS version at least 2.10.56"
13984
13985         local mdts=$(comma_list $(mdts_nodes))
13986
13987         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
13988         do_nodes $mdts $LCTL set_param fail_loc=0x1314
13989
13990         # Create a user
13991         changelog_register || error "first changelog_register failed"
13992         changelog_register || error "second changelog_register failed"
13993         local cl_users
13994         declare -A cl_user1
13995         declare -A cl_user2
13996         local user_rec1
13997         local user_rec2
13998         local i
13999
14000         # generate some changelog records to accumulate on each MDT
14001         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
14002         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14003                 error "create $DIR/$tdir/$tfile failed"
14004
14005         # check changelogs have been generated
14006         local nbcl=$(changelog_dump | wc -l)
14007         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14008
14009         # reduce the max_idle_indexes value to make sure we exceed it
14010         max_ndx=$((nbcl / 2 - 1))
14011
14012         for param in "changelog_max_idle_indexes=$max_ndx" \
14013                      "changelog_gc=1" \
14014                      "changelog_min_gc_interval=2" \
14015                      "changelog_min_free_cat_entries=3"; do
14016                 local MDT0=$(facet_svc $SINGLEMDS)
14017                 local var="${param%=*}"
14018                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14019
14020                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14021                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
14022                         error "unable to set mdd.*.$param"
14023         done
14024
14025         # simulate changelog catalog almost full
14026         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
14027         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
14028
14029         for i in $(seq $MDSCOUNT); do
14030                 cl_users=(${CL_USERS[mds$i]})
14031                 cl_user1[mds$i]="${cl_users[0]}"
14032                 cl_user2[mds$i]="${cl_users[1]}"
14033
14034                 [ -n "${cl_user1[mds$i]}" ] ||
14035                         error "mds$i: no user registered"
14036                 [ -n "${cl_user2[mds$i]}" ] ||
14037                         error "mds$i: only ${cl_user1[mds$i]} is registered"
14038
14039                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14040                 [ -n "$user_rec1" ] ||
14041                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14042                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14043                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14044                 [ -n "$user_rec2" ] ||
14045                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14046                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14047                      "$user_rec1 + 2 == $user_rec2"
14048                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14049                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14050                               "$user_rec1 + 2, but is $user_rec2"
14051                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14052                 [ -n "$user_rec2" ] ||
14053                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14054                 [ $user_rec1 == $user_rec2 ] ||
14055                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14056                               "$user_rec1, but is $user_rec2"
14057         done
14058
14059         # ensure we are past the previous changelog_min_gc_interval set above
14060         sleep 2
14061
14062         # generate one more changelog to trigger fail_loc
14063         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14064                 error "create $DIR/$tdir/${tfile}bis failed"
14065
14066         # ensure gc thread is done
14067         for i in $(mdts_nodes); do
14068                 wait_update $i \
14069                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
14070                         error "$i: GC-thread not done"
14071         done
14072
14073         local first_rec
14074         for i in $(seq $MDSCOUNT); do
14075                 # check cl_user1 still registered
14076                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14077                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14078                 # check cl_user2 unregistered
14079                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14080                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14081
14082                 # check changelogs are present and starting at $user_rec1 + 1
14083                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14084                 [ -n "$user_rec1" ] ||
14085                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14086                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14087                             awk '{ print $1; exit; }')
14088
14089                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14090                 [ $((user_rec1 + 1)) == $first_rec ] ||
14091                         error "mds$i: first index should be $user_rec1 + 1, " \
14092                               "but is $first_rec"
14093         done
14094 }
14095 run_test 160g "changelog garbage collect (old users)"
14096
14097 test_160h() {
14098         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14099         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14100                 skip "Need MDS version at least 2.10.56"
14101
14102         local mdts=$(comma_list $(mdts_nodes))
14103
14104         # Create a user
14105         changelog_register || error "first changelog_register failed"
14106         changelog_register || error "second changelog_register failed"
14107         local cl_users
14108         declare -A cl_user1
14109         declare -A cl_user2
14110         local user_rec1
14111         local user_rec2
14112         local i
14113
14114         # generate some changelog records to accumulate on each MDT
14115         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
14116         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14117                 error "create $DIR/$tdir/$tfile failed"
14118
14119         # check changelogs have been generated
14120         local nbcl=$(changelog_dump | wc -l)
14121         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14122
14123         for param in "changelog_max_idle_time=10" \
14124                      "changelog_gc=1" \
14125                      "changelog_min_gc_interval=2"; do
14126                 local MDT0=$(facet_svc $SINGLEMDS)
14127                 local var="${param%=*}"
14128                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14129
14130                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14131                 do_nodes $mdts $LCTL set_param mdd.*.$param
14132         done
14133
14134         # force cl_user2 to be idle (1st part)
14135         sleep 9
14136
14137         for i in $(seq $MDSCOUNT); do
14138                 cl_users=(${CL_USERS[mds$i]})
14139                 cl_user1[mds$i]="${cl_users[0]}"
14140                 cl_user2[mds$i]="${cl_users[1]}"
14141
14142                 [ -n "${cl_user1[mds$i]}" ] ||
14143                         error "mds$i: no user registered"
14144                 [ -n "${cl_user2[mds$i]}" ] ||
14145                         error "mds$i: only ${cl_user2[mds$i]} is registered"
14146
14147                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14148                 [ -n "$user_rec1" ] ||
14149                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14150                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14151                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14152                 [ -n "$user_rec2" ] ||
14153                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14154                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14155                      "$user_rec1 + 2 == $user_rec2"
14156                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14157                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14158                               "$user_rec1 + 2, but is $user_rec2"
14159                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14160                 [ -n "$user_rec2" ] ||
14161                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14162                 [ $user_rec1 == $user_rec2 ] ||
14163                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14164                               "$user_rec1, but is $user_rec2"
14165         done
14166
14167         # force cl_user2 to be idle (2nd part) and to reach
14168         # changelog_max_idle_time
14169         sleep 2
14170
14171         # force each GC-thread start and block then
14172         # one per MDT/MDD, set fail_val accordingly
14173         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
14174         do_nodes $mdts $LCTL set_param fail_loc=0x1316
14175
14176         # generate more changelogs to trigger fail_loc
14177         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14178                 error "create $DIR/$tdir/${tfile}bis failed"
14179
14180         # stop MDT to stop GC-thread, should be done in back-ground as it will
14181         # block waiting for the thread to be released and exit
14182         declare -A stop_pids
14183         for i in $(seq $MDSCOUNT); do
14184                 stop mds$i &
14185                 stop_pids[mds$i]=$!
14186         done
14187
14188         for i in $(mdts_nodes); do
14189                 local facet
14190                 local nb=0
14191                 local facets=$(facets_up_on_host $i)
14192
14193                 for facet in ${facets//,/ }; do
14194                         if [[ $facet == mds* ]]; then
14195                                 nb=$((nb + 1))
14196                         fi
14197                 done
14198                 # ensure each MDS's gc threads are still present and all in "R"
14199                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
14200                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
14201                         error "$i: expected $nb GC-thread"
14202                 wait_update $i \
14203                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
14204                         "R" 20 ||
14205                         error "$i: GC-thread not found in R-state"
14206                 # check umounts of each MDT on MDS have reached kthread_stop()
14207                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
14208                         error "$i: expected $nb umount"
14209                 wait_update $i \
14210                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
14211                         error "$i: umount not found in D-state"
14212         done
14213
14214         # release all GC-threads
14215         do_nodes $mdts $LCTL set_param fail_loc=0
14216
14217         # wait for MDT stop to complete
14218         for i in $(seq $MDSCOUNT); do
14219                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
14220         done
14221
14222         # XXX
14223         # may try to check if any orphan changelog records are present
14224         # via ldiskfs/zfs and llog_reader...
14225
14226         # re-start/mount MDTs
14227         for i in $(seq $MDSCOUNT); do
14228                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
14229                         error "Fail to start mds$i"
14230         done
14231
14232         local first_rec
14233         for i in $(seq $MDSCOUNT); do
14234                 # check cl_user1 still registered
14235                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14236                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14237                 # check cl_user2 unregistered
14238                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14239                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14240
14241                 # check changelogs are present and starting at $user_rec1 + 1
14242                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14243                 [ -n "$user_rec1" ] ||
14244                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14245                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14246                             awk '{ print $1; exit; }')
14247
14248                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14249                 [ $((user_rec1 + 1)) == $first_rec ] ||
14250                         error "mds$i: first index should be $user_rec1 + 1, " \
14251                               "but is $first_rec"
14252         done
14253 }
14254 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
14255               "during mount"
14256
14257 test_160i() {
14258
14259         local mdts=$(comma_list $(mdts_nodes))
14260
14261         changelog_register || error "first changelog_register failed"
14262
14263         # generate some changelog records to accumulate on each MDT
14264         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
14265         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14266                 error "create $DIR/$tdir/$tfile failed"
14267
14268         # check changelogs have been generated
14269         local nbcl=$(changelog_dump | wc -l)
14270         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14271
14272         # simulate race between register and unregister
14273         # XXX as fail_loc is set per-MDS, with DNE configs the race
14274         # simulation will only occur for one MDT per MDS and for the
14275         # others the normal race scenario will take place
14276         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
14277         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
14278         do_nodes $mdts $LCTL set_param fail_val=1
14279
14280         # unregister 1st user
14281         changelog_deregister &
14282         local pid1=$!
14283         # wait some time for deregister work to reach race rdv
14284         sleep 2
14285         # register 2nd user
14286         changelog_register || error "2nd user register failed"
14287
14288         wait $pid1 || error "1st user deregister failed"
14289
14290         local i
14291         local last_rec
14292         declare -A LAST_REC
14293         for i in $(seq $MDSCOUNT); do
14294                 if changelog_users mds$i | grep "^cl"; then
14295                         # make sure new records are added with one user present
14296                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
14297                                           awk '/^current.index:/ { print $NF }')
14298                 else
14299                         error "mds$i has no user registered"
14300                 fi
14301         done
14302
14303         # generate more changelog records to accumulate on each MDT
14304         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14305                 error "create $DIR/$tdir/${tfile}bis failed"
14306
14307         for i in $(seq $MDSCOUNT); do
14308                 last_rec=$(changelog_users $SINGLEMDS |
14309                            awk '/^current.index:/ { print $NF }')
14310                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
14311                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
14312                         error "changelogs are off on mds$i"
14313         done
14314 }
14315 run_test 160i "changelog user register/unregister race"
14316
14317 test_160j() {
14318         remote_mds_nodsh && skip "remote MDS with nodsh"
14319         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
14320                 skip "Need MDS version at least 2.12.56"
14321
14322         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
14323         stack_trap "umount $MOUNT2" EXIT
14324
14325         changelog_register || error "first changelog_register failed"
14326         stack_trap "changelog_deregister" EXIT
14327
14328         # generate some changelog
14329         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
14330         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14331                 error "create $DIR/$tdir/${tfile}bis failed"
14332
14333         # open the changelog device
14334         exec 3>/dev/changelog-$FSNAME-MDT0000
14335         stack_trap "exec 3>&-" EXIT
14336         exec 4</dev/changelog-$FSNAME-MDT0000
14337         stack_trap "exec 4<&-" EXIT
14338
14339         # umount the first lustre mount
14340         umount $MOUNT
14341         stack_trap "mount_client $MOUNT" EXIT
14342
14343         # read changelog
14344         cat <&4 >/dev/null || error "read changelog failed"
14345
14346         # clear changelog
14347         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14348         changelog_users $SINGLEMDS | grep -q $cl_user ||
14349                 error "User $cl_user not found in changelog_users"
14350
14351         printf 'clear:'$cl_user':0' >&3
14352
14353 }
14354 run_test 160j "client can be umounted  while its chanangelog is being used"
14355
14356 test_160k() {
14357         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14358         remote_mds_nodsh && skip "remote MDS with nodsh"
14359
14360         mkdir -p $DIR/$tdir/1/1
14361
14362         changelog_register || error "changelog_register failed"
14363         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14364
14365         changelog_users $SINGLEMDS | grep -q $cl_user ||
14366                 error "User '$cl_user' not found in changelog_users"
14367 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
14368         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
14369         rmdir $DIR/$tdir/1/1 & sleep 1
14370         mkdir $DIR/$tdir/2
14371         touch $DIR/$tdir/2/2
14372         rm -rf $DIR/$tdir/2
14373
14374         wait
14375         sleep 4
14376
14377         changelog_dump | grep rmdir || error "rmdir not recorded"
14378
14379         rm -rf $DIR/$tdir
14380         changelog_deregister
14381 }
14382 run_test 160k "Verify that changelog records are not lost"
14383
14384 test_161a() {
14385         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14386
14387         test_mkdir -c1 $DIR/$tdir
14388         cp /etc/hosts $DIR/$tdir/$tfile
14389         test_mkdir -c1 $DIR/$tdir/foo1
14390         test_mkdir -c1 $DIR/$tdir/foo2
14391         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
14392         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
14393         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
14394         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
14395         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
14396         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14397                 $LFS fid2path $DIR $FID
14398                 error "bad link ea"
14399         fi
14400         # middle
14401         rm $DIR/$tdir/foo2/zachary
14402         # last
14403         rm $DIR/$tdir/foo2/thor
14404         # first
14405         rm $DIR/$tdir/$tfile
14406         # rename
14407         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
14408         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
14409                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
14410         rm $DIR/$tdir/foo2/maggie
14411
14412         # overflow the EA
14413         local longname=$tfile.avg_len_is_thirty_two_
14414         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
14415                 error_noexit 'failed to unlink many hardlinks'" EXIT
14416         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
14417                 error "failed to hardlink many files"
14418         links=$($LFS fid2path $DIR $FID | wc -l)
14419         echo -n "${links}/1000 links in link EA"
14420         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
14421 }
14422 run_test 161a "link ea sanity"
14423
14424 test_161b() {
14425         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14426         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
14427
14428         local MDTIDX=1
14429         local remote_dir=$DIR/$tdir/remote_dir
14430
14431         mkdir -p $DIR/$tdir
14432         $LFS mkdir -i $MDTIDX $remote_dir ||
14433                 error "create remote directory failed"
14434
14435         cp /etc/hosts $remote_dir/$tfile
14436         mkdir -p $remote_dir/foo1
14437         mkdir -p $remote_dir/foo2
14438         ln $remote_dir/$tfile $remote_dir/foo1/sofia
14439         ln $remote_dir/$tfile $remote_dir/foo2/zachary
14440         ln $remote_dir/$tfile $remote_dir/foo1/luna
14441         ln $remote_dir/$tfile $remote_dir/foo2/thor
14442
14443         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
14444                      tr -d ']')
14445         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14446                 $LFS fid2path $DIR $FID
14447                 error "bad link ea"
14448         fi
14449         # middle
14450         rm $remote_dir/foo2/zachary
14451         # last
14452         rm $remote_dir/foo2/thor
14453         # first
14454         rm $remote_dir/$tfile
14455         # rename
14456         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
14457         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
14458         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
14459                 $LFS fid2path $DIR $FID
14460                 error "bad link rename"
14461         fi
14462         rm $remote_dir/foo2/maggie
14463
14464         # overflow the EA
14465         local longname=filename_avg_len_is_thirty_two_
14466         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
14467                 error "failed to hardlink many files"
14468         links=$($LFS fid2path $DIR $FID | wc -l)
14469         echo -n "${links}/1000 links in link EA"
14470         [[ ${links} -gt 60 ]] ||
14471                 error "expected at least 60 links in link EA"
14472         unlinkmany $remote_dir/foo2/$longname 1000 ||
14473         error "failed to unlink many hardlinks"
14474 }
14475 run_test 161b "link ea sanity under remote directory"
14476
14477 test_161c() {
14478         remote_mds_nodsh && skip "remote MDS with nodsh"
14479         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14480         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
14481                 skip "Need MDS version at least 2.1.5"
14482
14483         # define CLF_RENAME_LAST 0x0001
14484         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
14485         changelog_register || error "changelog_register failed"
14486
14487         rm -rf $DIR/$tdir
14488         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
14489         touch $DIR/$tdir/foo_161c
14490         touch $DIR/$tdir/bar_161c
14491         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14492         changelog_dump | grep RENME | tail -n 5
14493         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14494         changelog_clear 0 || error "changelog_clear failed"
14495         if [ x$flags != "x0x1" ]; then
14496                 error "flag $flags is not 0x1"
14497         fi
14498
14499         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
14500         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
14501         touch $DIR/$tdir/foo_161c
14502         touch $DIR/$tdir/bar_161c
14503         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14504         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14505         changelog_dump | grep RENME | tail -n 5
14506         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14507         changelog_clear 0 || error "changelog_clear failed"
14508         if [ x$flags != "x0x0" ]; then
14509                 error "flag $flags is not 0x0"
14510         fi
14511         echo "rename overwrite a target having nlink > 1," \
14512                 "changelog record has flags of $flags"
14513
14514         # rename doesn't overwrite a target (changelog flag 0x0)
14515         touch $DIR/$tdir/foo_161c
14516         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
14517         changelog_dump | grep RENME | tail -n 5
14518         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
14519         changelog_clear 0 || error "changelog_clear failed"
14520         if [ x$flags != "x0x0" ]; then
14521                 error "flag $flags is not 0x0"
14522         fi
14523         echo "rename doesn't overwrite a target," \
14524                 "changelog record has flags of $flags"
14525
14526         # define CLF_UNLINK_LAST 0x0001
14527         # unlink a file having nlink = 1 (changelog flag 0x1)
14528         rm -f $DIR/$tdir/foo2_161c
14529         changelog_dump | grep UNLNK | tail -n 5
14530         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14531         changelog_clear 0 || error "changelog_clear failed"
14532         if [ x$flags != "x0x1" ]; then
14533                 error "flag $flags is not 0x1"
14534         fi
14535         echo "unlink a file having nlink = 1," \
14536                 "changelog record has flags of $flags"
14537
14538         # unlink a file having nlink > 1 (changelog flag 0x0)
14539         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14540         rm -f $DIR/$tdir/foobar_161c
14541         changelog_dump | grep UNLNK | tail -n 5
14542         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14543         changelog_clear 0 || error "changelog_clear failed"
14544         if [ x$flags != "x0x0" ]; then
14545                 error "flag $flags is not 0x0"
14546         fi
14547         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
14548 }
14549 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
14550
14551 test_161d() {
14552         remote_mds_nodsh && skip "remote MDS with nodsh"
14553         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
14554
14555         local pid
14556         local fid
14557
14558         changelog_register || error "changelog_register failed"
14559
14560         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
14561         # interfer with $MOUNT/.lustre/fid/ access
14562         mkdir $DIR/$tdir
14563         [[ $? -eq 0 ]] || error "mkdir failed"
14564
14565         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
14566         $LCTL set_param fail_loc=0x8000140c
14567         # 5s pause
14568         $LCTL set_param fail_val=5
14569
14570         # create file
14571         echo foofoo > $DIR/$tdir/$tfile &
14572         pid=$!
14573
14574         # wait for create to be delayed
14575         sleep 2
14576
14577         ps -p $pid
14578         [[ $? -eq 0 ]] || error "create should be blocked"
14579
14580         local tempfile="$(mktemp --tmpdir $tfile.XXXXXX)"
14581         stack_trap "rm -f $tempfile"
14582         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
14583         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
14584         # some delay may occur during ChangeLog publishing and file read just
14585         # above, that could allow file write to happen finally
14586         [[ -s $tempfile ]] && echo "file should be empty"
14587
14588         $LCTL set_param fail_loc=0
14589
14590         wait $pid
14591         [[ $? -eq 0 ]] || error "create failed"
14592 }
14593 run_test 161d "create with concurrent .lustre/fid access"
14594
14595 check_path() {
14596         local expected="$1"
14597         shift
14598         local fid="$2"
14599
14600         local path
14601         path=$($LFS fid2path "$@")
14602         local rc=$?
14603
14604         if [ $rc -ne 0 ]; then
14605                 error "path looked up of '$expected' failed: rc=$rc"
14606         elif [ "$path" != "$expected" ]; then
14607                 error "path looked up '$path' instead of '$expected'"
14608         else
14609                 echo "FID '$fid' resolves to path '$path' as expected"
14610         fi
14611 }
14612
14613 test_162a() { # was test_162
14614         test_mkdir -p -c1 $DIR/$tdir/d2
14615         touch $DIR/$tdir/d2/$tfile
14616         touch $DIR/$tdir/d2/x1
14617         touch $DIR/$tdir/d2/x2
14618         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
14619         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
14620         # regular file
14621         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
14622         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
14623
14624         # softlink
14625         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
14626         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
14627         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
14628
14629         # softlink to wrong file
14630         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
14631         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
14632         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
14633
14634         # hardlink
14635         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
14636         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
14637         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
14638         # fid2path dir/fsname should both work
14639         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
14640         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
14641
14642         # hardlink count: check that there are 2 links
14643         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
14644         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
14645
14646         # hardlink indexing: remove the first link
14647         rm $DIR/$tdir/d2/p/q/r/hlink
14648         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
14649 }
14650 run_test 162a "path lookup sanity"
14651
14652 test_162b() {
14653         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14654         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14655
14656         mkdir $DIR/$tdir
14657         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
14658                                 error "create striped dir failed"
14659
14660         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
14661                                         tail -n 1 | awk '{print $2}')
14662         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
14663
14664         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
14665         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
14666
14667         # regular file
14668         for ((i=0;i<5;i++)); do
14669                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
14670                         error "get fid for f$i failed"
14671                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
14672
14673                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
14674                         error "get fid for d$i failed"
14675                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
14676         done
14677
14678         return 0
14679 }
14680 run_test 162b "striped directory path lookup sanity"
14681
14682 # LU-4239: Verify fid2path works with paths 100 or more directories deep
14683 test_162c() {
14684         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
14685                 skip "Need MDS version at least 2.7.51"
14686
14687         local lpath=$tdir.local
14688         local rpath=$tdir.remote
14689
14690         test_mkdir $DIR/$lpath
14691         test_mkdir $DIR/$rpath
14692
14693         for ((i = 0; i <= 101; i++)); do
14694                 lpath="$lpath/$i"
14695                 mkdir $DIR/$lpath
14696                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
14697                         error "get fid for local directory $DIR/$lpath failed"
14698                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
14699
14700                 rpath="$rpath/$i"
14701                 test_mkdir $DIR/$rpath
14702                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
14703                         error "get fid for remote directory $DIR/$rpath failed"
14704                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
14705         done
14706
14707         return 0
14708 }
14709 run_test 162c "fid2path works with paths 100 or more directories deep"
14710
14711 test_169() {
14712         # do directio so as not to populate the page cache
14713         log "creating a 10 Mb file"
14714         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
14715         log "starting reads"
14716         dd if=$DIR/$tfile of=/dev/null bs=4096 &
14717         log "truncating the file"
14718         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
14719         log "killing dd"
14720         kill %+ || true # reads might have finished
14721         echo "wait until dd is finished"
14722         wait
14723         log "removing the temporary file"
14724         rm -rf $DIR/$tfile || error "tmp file removal failed"
14725 }
14726 run_test 169 "parallel read and truncate should not deadlock"
14727
14728 test_170() {
14729         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14730
14731         $LCTL clear     # bug 18514
14732         $LCTL debug_daemon start $TMP/${tfile}_log_good
14733         touch $DIR/$tfile
14734         $LCTL debug_daemon stop
14735         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
14736                 error "sed failed to read log_good"
14737
14738         $LCTL debug_daemon start $TMP/${tfile}_log_good
14739         rm -rf $DIR/$tfile
14740         $LCTL debug_daemon stop
14741
14742         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
14743                error "lctl df log_bad failed"
14744
14745         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14746         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14747
14748         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
14749         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
14750
14751         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
14752                 error "bad_line good_line1 good_line2 are empty"
14753
14754         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14755         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
14756         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14757
14758         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
14759         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14760         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14761
14762         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
14763                 error "bad_line_new good_line_new are empty"
14764
14765         local expected_good=$((good_line1 + good_line2*2))
14766
14767         rm -f $TMP/${tfile}*
14768         # LU-231, short malformed line may not be counted into bad lines
14769         if [ $bad_line -ne $bad_line_new ] &&
14770                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
14771                 error "expected $bad_line bad lines, but got $bad_line_new"
14772                 return 1
14773         fi
14774
14775         if [ $expected_good -ne $good_line_new ]; then
14776                 error "expected $expected_good good lines, but got $good_line_new"
14777                 return 2
14778         fi
14779         true
14780 }
14781 run_test 170 "test lctl df to handle corrupted log ====================="
14782
14783 test_171() { # bug20592
14784         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14785
14786         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
14787         $LCTL set_param fail_loc=0x50e
14788         $LCTL set_param fail_val=3000
14789         multiop_bg_pause $DIR/$tfile O_s || true
14790         local MULTIPID=$!
14791         kill -USR1 $MULTIPID
14792         # cause log dump
14793         sleep 3
14794         wait $MULTIPID
14795         if dmesg | grep "recursive fault"; then
14796                 error "caught a recursive fault"
14797         fi
14798         $LCTL set_param fail_loc=0
14799         true
14800 }
14801 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
14802
14803 # it would be good to share it with obdfilter-survey/iokit-libecho code
14804 setup_obdecho_osc () {
14805         local rc=0
14806         local ost_nid=$1
14807         local obdfilter_name=$2
14808         echo "Creating new osc for $obdfilter_name on $ost_nid"
14809         # make sure we can find loopback nid
14810         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
14811
14812         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
14813                            ${obdfilter_name}_osc_UUID || rc=2; }
14814         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
14815                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
14816         return $rc
14817 }
14818
14819 cleanup_obdecho_osc () {
14820         local obdfilter_name=$1
14821         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
14822         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
14823         return 0
14824 }
14825
14826 obdecho_test() {
14827         local OBD=$1
14828         local node=$2
14829         local pages=${3:-64}
14830         local rc=0
14831         local id
14832
14833         local count=10
14834         local obd_size=$(get_obd_size $node $OBD)
14835         local page_size=$(get_page_size $node)
14836         if [[ -n "$obd_size" ]]; then
14837                 local new_count=$((obd_size / (pages * page_size / 1024)))
14838                 [[ $new_count -ge $count ]] || count=$new_count
14839         fi
14840
14841         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
14842         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
14843                            rc=2; }
14844         if [ $rc -eq 0 ]; then
14845             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
14846             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
14847         fi
14848         echo "New object id is $id"
14849         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
14850                            rc=4; }
14851         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
14852                            "test_brw $count w v $pages $id" || rc=4; }
14853         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
14854                            rc=4; }
14855         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
14856                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
14857         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
14858                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
14859         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
14860         return $rc
14861 }
14862
14863 test_180a() {
14864         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14865
14866         if ! module_loaded obdecho; then
14867                 load_module obdecho/obdecho &&
14868                         stack_trap "rmmod obdecho" EXIT ||
14869                         error "unable to load obdecho on client"
14870         fi
14871
14872         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
14873         local host=$($LCTL get_param -n osc.$osc.import |
14874                      awk '/current_connection:/ { print $2 }' )
14875         local target=$($LCTL get_param -n osc.$osc.import |
14876                        awk '/target:/ { print $2 }' )
14877         target=${target%_UUID}
14878
14879         if [ -n "$target" ]; then
14880                 setup_obdecho_osc $host $target &&
14881                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
14882                         { error "obdecho setup failed with $?"; return; }
14883
14884                 obdecho_test ${target}_osc client ||
14885                         error "obdecho_test failed on ${target}_osc"
14886         else
14887                 $LCTL get_param osc.$osc.import
14888                 error "there is no osc.$osc.import target"
14889         fi
14890 }
14891 run_test 180a "test obdecho on osc"
14892
14893 test_180b() {
14894         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14895         remote_ost_nodsh && skip "remote OST with nodsh"
14896
14897         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14898                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14899                 error "failed to load module obdecho"
14900
14901         local target=$(do_facet ost1 $LCTL dl |
14902                        awk '/obdfilter/ { print $4; exit; }')
14903
14904         if [ -n "$target" ]; then
14905                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
14906         else
14907                 do_facet ost1 $LCTL dl
14908                 error "there is no obdfilter target on ost1"
14909         fi
14910 }
14911 run_test 180b "test obdecho directly on obdfilter"
14912
14913 test_180c() { # LU-2598
14914         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14915         remote_ost_nodsh && skip "remote OST with nodsh"
14916         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
14917                 skip "Need MDS version at least 2.4.0"
14918
14919         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14920                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14921                 error "failed to load module obdecho"
14922
14923         local target=$(do_facet ost1 $LCTL dl |
14924                        awk '/obdfilter/ { print $4; exit; }')
14925
14926         if [ -n "$target" ]; then
14927                 local pages=16384 # 64MB bulk I/O RPC size
14928
14929                 obdecho_test "$target" ost1 "$pages" ||
14930                         error "obdecho_test with pages=$pages failed with $?"
14931         else
14932                 do_facet ost1 $LCTL dl
14933                 error "there is no obdfilter target on ost1"
14934         fi
14935 }
14936 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
14937
14938 test_181() { # bug 22177
14939         test_mkdir $DIR/$tdir
14940         # create enough files to index the directory
14941         createmany -o $DIR/$tdir/foobar 4000
14942         # print attributes for debug purpose
14943         lsattr -d .
14944         # open dir
14945         multiop_bg_pause $DIR/$tdir D_Sc || return 1
14946         MULTIPID=$!
14947         # remove the files & current working dir
14948         unlinkmany $DIR/$tdir/foobar 4000
14949         rmdir $DIR/$tdir
14950         kill -USR1 $MULTIPID
14951         wait $MULTIPID
14952         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
14953         return 0
14954 }
14955 run_test 181 "Test open-unlinked dir ========================"
14956
14957 test_182() {
14958         local fcount=1000
14959         local tcount=10
14960
14961         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14962
14963         $LCTL set_param mdc.*.rpc_stats=clear
14964
14965         for (( i = 0; i < $tcount; i++ )) ; do
14966                 mkdir $DIR/$tdir/$i
14967         done
14968
14969         for (( i = 0; i < $tcount; i++ )) ; do
14970                 createmany -o $DIR/$tdir/$i/f- $fcount &
14971         done
14972         wait
14973
14974         for (( i = 0; i < $tcount; i++ )) ; do
14975                 unlinkmany $DIR/$tdir/$i/f- $fcount &
14976         done
14977         wait
14978
14979         $LCTL get_param mdc.*.rpc_stats
14980
14981         rm -rf $DIR/$tdir
14982 }
14983 run_test 182 "Test parallel modify metadata operations ================"
14984
14985 test_183() { # LU-2275
14986         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14987         remote_mds_nodsh && skip "remote MDS with nodsh"
14988         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
14989                 skip "Need MDS version at least 2.3.56"
14990
14991         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14992         echo aaa > $DIR/$tdir/$tfile
14993
14994 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
14995         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
14996
14997         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
14998         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
14999
15000         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
15001
15002         # Flush negative dentry cache
15003         touch $DIR/$tdir/$tfile
15004
15005         # We are not checking for any leaked references here, they'll
15006         # become evident next time we do cleanup with module unload.
15007         rm -rf $DIR/$tdir
15008 }
15009 run_test 183 "No crash or request leak in case of strange dispositions ========"
15010
15011 # test suite 184 is for LU-2016, LU-2017
15012 test_184a() {
15013         check_swap_layouts_support
15014
15015         dir0=$DIR/$tdir/$testnum
15016         test_mkdir -p -c1 $dir0
15017         ref1=/etc/passwd
15018         ref2=/etc/group
15019         file1=$dir0/f1
15020         file2=$dir0/f2
15021         $LFS setstripe -c1 $file1
15022         cp $ref1 $file1
15023         $LFS setstripe -c2 $file2
15024         cp $ref2 $file2
15025         gen1=$($LFS getstripe -g $file1)
15026         gen2=$($LFS getstripe -g $file2)
15027
15028         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
15029         gen=$($LFS getstripe -g $file1)
15030         [[ $gen1 != $gen ]] ||
15031                 "Layout generation on $file1 does not change"
15032         gen=$($LFS getstripe -g $file2)
15033         [[ $gen2 != $gen ]] ||
15034                 "Layout generation on $file2 does not change"
15035
15036         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
15037         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
15038
15039         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
15040 }
15041 run_test 184a "Basic layout swap"
15042
15043 test_184b() {
15044         check_swap_layouts_support
15045
15046         dir0=$DIR/$tdir/$testnum
15047         mkdir -p $dir0 || error "creating dir $dir0"
15048         file1=$dir0/f1
15049         file2=$dir0/f2
15050         file3=$dir0/f3
15051         dir1=$dir0/d1
15052         dir2=$dir0/d2
15053         mkdir $dir1 $dir2
15054         $LFS setstripe -c1 $file1
15055         $LFS setstripe -c2 $file2
15056         $LFS setstripe -c1 $file3
15057         chown $RUNAS_ID $file3
15058         gen1=$($LFS getstripe -g $file1)
15059         gen2=$($LFS getstripe -g $file2)
15060
15061         $LFS swap_layouts $dir1 $dir2 &&
15062                 error "swap of directories layouts should fail"
15063         $LFS swap_layouts $dir1 $file1 &&
15064                 error "swap of directory and file layouts should fail"
15065         $RUNAS $LFS swap_layouts $file1 $file2 &&
15066                 error "swap of file we cannot write should fail"
15067         $LFS swap_layouts $file1 $file3 &&
15068                 error "swap of file with different owner should fail"
15069         /bin/true # to clear error code
15070 }
15071 run_test 184b "Forbidden layout swap (will generate errors)"
15072
15073 test_184c() {
15074         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
15075         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
15076         check_swap_layouts_support
15077
15078         local dir0=$DIR/$tdir/$testnum
15079         mkdir -p $dir0 || error "creating dir $dir0"
15080
15081         local ref1=$dir0/ref1
15082         local ref2=$dir0/ref2
15083         local file1=$dir0/file1
15084         local file2=$dir0/file2
15085         # create a file large enough for the concurrent test
15086         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
15087         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
15088         echo "ref file size: ref1($(stat -c %s $ref1))," \
15089              "ref2($(stat -c %s $ref2))"
15090
15091         cp $ref2 $file2
15092         dd if=$ref1 of=$file1 bs=16k &
15093         local DD_PID=$!
15094
15095         # Make sure dd starts to copy file
15096         while [ ! -f $file1 ]; do sleep 0.1; done
15097
15098         $LFS swap_layouts $file1 $file2
15099         local rc=$?
15100         wait $DD_PID
15101         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
15102         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
15103
15104         # how many bytes copied before swapping layout
15105         local copied=$(stat -c %s $file2)
15106         local remaining=$(stat -c %s $ref1)
15107         remaining=$((remaining - copied))
15108         echo "Copied $copied bytes before swapping layout..."
15109
15110         cmp -n $copied $file1 $ref2 | grep differ &&
15111                 error "Content mismatch [0, $copied) of ref2 and file1"
15112         cmp -n $copied $file2 $ref1 ||
15113                 error "Content mismatch [0, $copied) of ref1 and file2"
15114         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
15115                 error "Content mismatch [$copied, EOF) of ref1 and file1"
15116
15117         # clean up
15118         rm -f $ref1 $ref2 $file1 $file2
15119 }
15120 run_test 184c "Concurrent write and layout swap"
15121
15122 test_184d() {
15123         check_swap_layouts_support
15124         [ -z "$(which getfattr 2>/dev/null)" ] &&
15125                 skip_env "no getfattr command"
15126
15127         local file1=$DIR/$tdir/$tfile-1
15128         local file2=$DIR/$tdir/$tfile-2
15129         local file3=$DIR/$tdir/$tfile-3
15130         local lovea1
15131         local lovea2
15132
15133         mkdir -p $DIR/$tdir
15134         touch $file1 || error "create $file1 failed"
15135         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15136                 error "create $file2 failed"
15137         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15138                 error "create $file3 failed"
15139         lovea1=$(get_layout_param $file1)
15140
15141         $LFS swap_layouts $file2 $file3 ||
15142                 error "swap $file2 $file3 layouts failed"
15143         $LFS swap_layouts $file1 $file2 ||
15144                 error "swap $file1 $file2 layouts failed"
15145
15146         lovea2=$(get_layout_param $file2)
15147         echo "$lovea1"
15148         echo "$lovea2"
15149         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
15150
15151         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15152         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
15153 }
15154 run_test 184d "allow stripeless layouts swap"
15155
15156 test_184e() {
15157         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
15158                 skip "Need MDS version at least 2.6.94"
15159         check_swap_layouts_support
15160         [ -z "$(which getfattr 2>/dev/null)" ] &&
15161                 skip_env "no getfattr command"
15162
15163         local file1=$DIR/$tdir/$tfile-1
15164         local file2=$DIR/$tdir/$tfile-2
15165         local file3=$DIR/$tdir/$tfile-3
15166         local lovea
15167
15168         mkdir -p $DIR/$tdir
15169         touch $file1 || error "create $file1 failed"
15170         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15171                 error "create $file2 failed"
15172         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15173                 error "create $file3 failed"
15174
15175         $LFS swap_layouts $file1 $file2 ||
15176                 error "swap $file1 $file2 layouts failed"
15177
15178         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15179         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
15180
15181         echo 123 > $file1 || error "Should be able to write into $file1"
15182
15183         $LFS swap_layouts $file1 $file3 ||
15184                 error "swap $file1 $file3 layouts failed"
15185
15186         echo 123 > $file1 || error "Should be able to write into $file1"
15187
15188         rm -rf $file1 $file2 $file3
15189 }
15190 run_test 184e "Recreate layout after stripeless layout swaps"
15191
15192 test_184f() {
15193         # Create a file with name longer than sizeof(struct stat) ==
15194         # 144 to see if we can get chars from the file name to appear
15195         # in the returned striping. Note that 'f' == 0x66.
15196         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
15197
15198         mkdir -p $DIR/$tdir
15199         mcreate $DIR/$tdir/$file
15200         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
15201                 error "IOC_MDC_GETFILEINFO returned garbage striping"
15202         fi
15203 }
15204 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
15205
15206 test_185() { # LU-2441
15207         # LU-3553 - no volatile file support in old servers
15208         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
15209                 skip "Need MDS version at least 2.3.60"
15210
15211         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15212         touch $DIR/$tdir/spoo
15213         local mtime1=$(stat -c "%Y" $DIR/$tdir)
15214         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
15215                 error "cannot create/write a volatile file"
15216         [ "$FILESET" == "" ] &&
15217         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
15218                 error "FID is still valid after close"
15219
15220         multiop_bg_pause $DIR/$tdir vVw4096_c
15221         local multi_pid=$!
15222
15223         local OLD_IFS=$IFS
15224         IFS=":"
15225         local fidv=($fid)
15226         IFS=$OLD_IFS
15227         # assume that the next FID for this client is sequential, since stdout
15228         # is unfortunately eaten by multiop_bg_pause
15229         local n=$((${fidv[1]} + 1))
15230         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
15231         if [ "$FILESET" == "" ]; then
15232                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
15233                         error "FID is missing before close"
15234         fi
15235         kill -USR1 $multi_pid
15236         # 1 second delay, so if mtime change we will see it
15237         sleep 1
15238         local mtime2=$(stat -c "%Y" $DIR/$tdir)
15239         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
15240 }
15241 run_test 185 "Volatile file support"
15242
15243 function create_check_volatile() {
15244         local idx=$1
15245         local tgt
15246
15247         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
15248         local PID=$!
15249         sleep 1
15250         local FID=$(cat /tmp/${tfile}.fid)
15251         [ "$FID" == "" ] && error "can't get FID for volatile"
15252         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
15253         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
15254         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
15255         kill -USR1 $PID
15256         wait
15257         sleep 1
15258         cancel_lru_locks mdc # flush opencache
15259         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
15260         return 0
15261 }
15262
15263 test_185a(){
15264         # LU-12516 - volatile creation via .lustre
15265         [[ $MDS1_VERSION -ge $(version_code 2.12.55) ]] ||
15266                 skip "Need MDS version at least 2.3.55"
15267
15268         create_check_volatile 0
15269         [ $MDSCOUNT -lt 2 ] && return 0
15270
15271         # DNE case
15272         create_check_volatile 1
15273
15274         return 0
15275 }
15276 run_test 185a "Volatile file creation in .lustre/fid/"
15277
15278 test_187a() {
15279         remote_mds_nodsh && skip "remote MDS with nodsh"
15280         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15281                 skip "Need MDS version at least 2.3.0"
15282
15283         local dir0=$DIR/$tdir/$testnum
15284         mkdir -p $dir0 || error "creating dir $dir0"
15285
15286         local file=$dir0/file1
15287         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
15288         local dv1=$($LFS data_version $file)
15289         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
15290         local dv2=$($LFS data_version $file)
15291         [[ $dv1 != $dv2 ]] ||
15292                 error "data version did not change on write $dv1 == $dv2"
15293
15294         # clean up
15295         rm -f $file1
15296 }
15297 run_test 187a "Test data version change"
15298
15299 test_187b() {
15300         remote_mds_nodsh && skip "remote MDS with nodsh"
15301         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15302                 skip "Need MDS version at least 2.3.0"
15303
15304         local dir0=$DIR/$tdir/$testnum
15305         mkdir -p $dir0 || error "creating dir $dir0"
15306
15307         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
15308         [[ ${DV[0]} != ${DV[1]} ]] ||
15309                 error "data version did not change on write"\
15310                       " ${DV[0]} == ${DV[1]}"
15311
15312         # clean up
15313         rm -f $file1
15314 }
15315 run_test 187b "Test data version change on volatile file"
15316
15317 test_200() {
15318         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15319         remote_mgs_nodsh && skip "remote MGS with nodsh"
15320         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15321
15322         local POOL=${POOL:-cea1}
15323         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
15324         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
15325         # Pool OST targets
15326         local first_ost=0
15327         local last_ost=$(($OSTCOUNT - 1))
15328         local ost_step=2
15329         local ost_list=$(seq $first_ost $ost_step $last_ost)
15330         local ost_range="$first_ost $last_ost $ost_step"
15331         local test_path=$POOL_ROOT/$POOL_DIR_NAME
15332         local file_dir=$POOL_ROOT/file_tst
15333         local subdir=$test_path/subdir
15334         local rc=0
15335
15336         while : ; do
15337                 # former test_200a test_200b
15338                 pool_add $POOL                          || { rc=$? ; break; }
15339                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
15340                 # former test_200c test_200d
15341                 mkdir -p $test_path
15342                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
15343                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
15344                 mkdir -p $subdir
15345                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
15346                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
15347                                                         || { rc=$? ; break; }
15348                 # former test_200e test_200f
15349                 local files=$((OSTCOUNT*3))
15350                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
15351                                                         || { rc=$? ; break; }
15352                 pool_create_files $POOL $file_dir $files "$ost_list" \
15353                                                         || { rc=$? ; break; }
15354                 # former test_200g test_200h
15355                 pool_lfs_df $POOL                       || { rc=$? ; break; }
15356                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
15357
15358                 # former test_201a test_201b test_201c
15359                 pool_remove_first_target $POOL          || { rc=$? ; break; }
15360
15361                 local f=$test_path/$tfile
15362                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
15363                 pool_remove $POOL $f                    || { rc=$? ; break; }
15364                 break
15365         done
15366
15367         destroy_test_pools
15368
15369         return $rc
15370 }
15371 run_test 200 "OST pools"
15372
15373 # usage: default_attr <count | size | offset>
15374 default_attr() {
15375         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
15376 }
15377
15378 # usage: check_default_stripe_attr
15379 check_default_stripe_attr() {
15380         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
15381         case $1 in
15382         --stripe-count|-c)
15383                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
15384         --stripe-size|-S)
15385                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
15386         --stripe-index|-i)
15387                 EXPECTED=-1;;
15388         *)
15389                 error "unknown getstripe attr '$1'"
15390         esac
15391
15392         [ $ACTUAL == $EXPECTED ] ||
15393                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
15394 }
15395
15396 test_204a() {
15397         test_mkdir $DIR/$tdir
15398         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
15399
15400         check_default_stripe_attr --stripe-count
15401         check_default_stripe_attr --stripe-size
15402         check_default_stripe_attr --stripe-index
15403 }
15404 run_test 204a "Print default stripe attributes"
15405
15406 test_204b() {
15407         test_mkdir $DIR/$tdir
15408         $LFS setstripe --stripe-count 1 $DIR/$tdir
15409
15410         check_default_stripe_attr --stripe-size
15411         check_default_stripe_attr --stripe-index
15412 }
15413 run_test 204b "Print default stripe size and offset"
15414
15415 test_204c() {
15416         test_mkdir $DIR/$tdir
15417         $LFS setstripe --stripe-size 65536 $DIR/$tdir
15418
15419         check_default_stripe_attr --stripe-count
15420         check_default_stripe_attr --stripe-index
15421 }
15422 run_test 204c "Print default stripe count and offset"
15423
15424 test_204d() {
15425         test_mkdir $DIR/$tdir
15426         $LFS setstripe --stripe-index 0 $DIR/$tdir
15427
15428         check_default_stripe_attr --stripe-count
15429         check_default_stripe_attr --stripe-size
15430 }
15431 run_test 204d "Print default stripe count and size"
15432
15433 test_204e() {
15434         test_mkdir $DIR/$tdir
15435         $LFS setstripe -d $DIR/$tdir
15436
15437         check_default_stripe_attr --stripe-count --raw
15438         check_default_stripe_attr --stripe-size --raw
15439         check_default_stripe_attr --stripe-index --raw
15440 }
15441 run_test 204e "Print raw stripe attributes"
15442
15443 test_204f() {
15444         test_mkdir $DIR/$tdir
15445         $LFS setstripe --stripe-count 1 $DIR/$tdir
15446
15447         check_default_stripe_attr --stripe-size --raw
15448         check_default_stripe_attr --stripe-index --raw
15449 }
15450 run_test 204f "Print raw stripe size and offset"
15451
15452 test_204g() {
15453         test_mkdir $DIR/$tdir
15454         $LFS setstripe --stripe-size 65536 $DIR/$tdir
15455
15456         check_default_stripe_attr --stripe-count --raw
15457         check_default_stripe_attr --stripe-index --raw
15458 }
15459 run_test 204g "Print raw stripe count and offset"
15460
15461 test_204h() {
15462         test_mkdir $DIR/$tdir
15463         $LFS setstripe --stripe-index 0 $DIR/$tdir
15464
15465         check_default_stripe_attr --stripe-count --raw
15466         check_default_stripe_attr --stripe-size --raw
15467 }
15468 run_test 204h "Print raw stripe count and size"
15469
15470 # Figure out which job scheduler is being used, if any,
15471 # or use a fake one
15472 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
15473         JOBENV=SLURM_JOB_ID
15474 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
15475         JOBENV=LSB_JOBID
15476 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
15477         JOBENV=PBS_JOBID
15478 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
15479         JOBENV=LOADL_STEP_ID
15480 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
15481         JOBENV=JOB_ID
15482 else
15483         $LCTL list_param jobid_name > /dev/null 2>&1
15484         if [ $? -eq 0 ]; then
15485                 JOBENV=nodelocal
15486         else
15487                 JOBENV=FAKE_JOBID
15488         fi
15489 fi
15490 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
15491
15492 verify_jobstats() {
15493         local cmd=($1)
15494         shift
15495         local facets="$@"
15496
15497 # we don't really need to clear the stats for this test to work, since each
15498 # command has a unique jobid, but it makes debugging easier if needed.
15499 #       for facet in $facets; do
15500 #               local dev=$(convert_facet2label $facet)
15501 #               # clear old jobstats
15502 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
15503 #       done
15504
15505         # use a new JobID for each test, or we might see an old one
15506         [ "$JOBENV" = "FAKE_JOBID" ] &&
15507                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
15508
15509         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
15510
15511         [ "$JOBENV" = "nodelocal" ] && {
15512                 FAKE_JOBID=id.$testnum.%e.$RANDOM
15513                 $LCTL set_param jobid_name=$FAKE_JOBID
15514                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
15515         }
15516
15517         log "Test: ${cmd[*]}"
15518         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
15519
15520         if [ $JOBENV = "FAKE_JOBID" ]; then
15521                 FAKE_JOBID=$JOBVAL ${cmd[*]}
15522         else
15523                 ${cmd[*]}
15524         fi
15525
15526         # all files are created on OST0000
15527         for facet in $facets; do
15528                 local stats="*.$(convert_facet2label $facet).job_stats"
15529
15530                 # strip out libtool wrappers for in-tree executables
15531                 if [ $(do_facet $facet lctl get_param $stats |
15532                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
15533                         do_facet $facet lctl get_param $stats
15534                         error "No jobstats for $JOBVAL found on $facet::$stats"
15535                 fi
15536         done
15537 }
15538
15539 jobstats_set() {
15540         local new_jobenv=$1
15541
15542         set_persistent_param_and_check client "jobid_var" \
15543                 "$FSNAME.sys.jobid_var" $new_jobenv
15544 }
15545
15546 test_205a() { # Job stats
15547         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15548         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
15549                 skip "Need MDS version with at least 2.7.1"
15550         remote_mgs_nodsh && skip "remote MGS with nodsh"
15551         remote_mds_nodsh && skip "remote MDS with nodsh"
15552         remote_ost_nodsh && skip "remote OST with nodsh"
15553         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
15554                 skip "Server doesn't support jobstats"
15555         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
15556
15557         local old_jobenv=$($LCTL get_param -n jobid_var)
15558         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
15559
15560         if [[ $PERM_CMD == *"set_param -P"* ]]; then
15561                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
15562         else
15563                 stack_trap "do_facet mgs $PERM_CMD \
15564                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
15565         fi
15566         changelog_register
15567
15568         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
15569                                 mdt.*.job_cleanup_interval | head -n 1)
15570         local new_interval=5
15571         do_facet $SINGLEMDS \
15572                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
15573         stack_trap "do_facet $SINGLEMDS \
15574                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
15575         local start=$SECONDS
15576
15577         local cmd
15578         # mkdir
15579         cmd="mkdir $DIR/$tdir"
15580         verify_jobstats "$cmd" "$SINGLEMDS"
15581         # rmdir
15582         cmd="rmdir $DIR/$tdir"
15583         verify_jobstats "$cmd" "$SINGLEMDS"
15584         # mkdir on secondary MDT
15585         if [ $MDSCOUNT -gt 1 ]; then
15586                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
15587                 verify_jobstats "$cmd" "mds2"
15588         fi
15589         # mknod
15590         cmd="mknod $DIR/$tfile c 1 3"
15591         verify_jobstats "$cmd" "$SINGLEMDS"
15592         # unlink
15593         cmd="rm -f $DIR/$tfile"
15594         verify_jobstats "$cmd" "$SINGLEMDS"
15595         # create all files on OST0000 so verify_jobstats can find OST stats
15596         # open & close
15597         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
15598         verify_jobstats "$cmd" "$SINGLEMDS"
15599         # setattr
15600         cmd="touch $DIR/$tfile"
15601         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15602         # write
15603         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
15604         verify_jobstats "$cmd" "ost1"
15605         # read
15606         cancel_lru_locks osc
15607         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
15608         verify_jobstats "$cmd" "ost1"
15609         # truncate
15610         cmd="$TRUNCATE $DIR/$tfile 0"
15611         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15612         # rename
15613         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
15614         verify_jobstats "$cmd" "$SINGLEMDS"
15615         # jobstats expiry - sleep until old stats should be expired
15616         local left=$((new_interval + 5 - (SECONDS - start)))
15617         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
15618                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
15619                         "0" $left
15620         cmd="mkdir $DIR/$tdir.expire"
15621         verify_jobstats "$cmd" "$SINGLEMDS"
15622         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
15623             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
15624
15625         # Ensure that jobid are present in changelog (if supported by MDS)
15626         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
15627                 changelog_dump | tail -10
15628                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
15629                 [ $jobids -eq 9 ] ||
15630                         error "Wrong changelog jobid count $jobids != 9"
15631
15632                 # LU-5862
15633                 JOBENV="disable"
15634                 jobstats_set $JOBENV
15635                 touch $DIR/$tfile
15636                 changelog_dump | grep $tfile
15637                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
15638                 [ $jobids -eq 0 ] ||
15639                         error "Unexpected jobids when jobid_var=$JOBENV"
15640         fi
15641
15642         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
15643         JOBENV="JOBCOMPLEX"
15644         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
15645
15646         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
15647 }
15648 run_test 205a "Verify job stats"
15649
15650 # LU-13117
15651 test_205b() {
15652         $LCTL set_param jobid_var=USER jobid_name="%e.%u"
15653         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
15654         do_facet $SINGLEMDS $LCTL get_param mdt.*.job_stats |
15655                 grep job_id: | grep foolish &&
15656                         error "Unexpected jobid found"
15657         true
15658 }
15659 run_test 205b "Verify job stats jobid parsing"
15660
15661 # LU-1480, LU-1773 and LU-1657
15662 test_206() {
15663         mkdir -p $DIR/$tdir
15664         $LFS setstripe -c -1 $DIR/$tdir
15665 #define OBD_FAIL_LOV_INIT 0x1403
15666         $LCTL set_param fail_loc=0xa0001403
15667         $LCTL set_param fail_val=1
15668         touch $DIR/$tdir/$tfile || true
15669 }
15670 run_test 206 "fail lov_init_raid0() doesn't lbug"
15671
15672 test_207a() {
15673         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15674         local fsz=`stat -c %s $DIR/$tfile`
15675         cancel_lru_locks mdc
15676
15677         # do not return layout in getattr intent
15678 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
15679         $LCTL set_param fail_loc=0x170
15680         local sz=`stat -c %s $DIR/$tfile`
15681
15682         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
15683
15684         rm -rf $DIR/$tfile
15685 }
15686 run_test 207a "can refresh layout at glimpse"
15687
15688 test_207b() {
15689         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15690         local cksum=`md5sum $DIR/$tfile`
15691         local fsz=`stat -c %s $DIR/$tfile`
15692         cancel_lru_locks mdc
15693         cancel_lru_locks osc
15694
15695         # do not return layout in getattr intent
15696 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
15697         $LCTL set_param fail_loc=0x171
15698
15699         # it will refresh layout after the file is opened but before read issues
15700         echo checksum is "$cksum"
15701         echo "$cksum" |md5sum -c --quiet || error "file differs"
15702
15703         rm -rf $DIR/$tfile
15704 }
15705 run_test 207b "can refresh layout at open"
15706
15707 test_208() {
15708         # FIXME: in this test suite, only RD lease is used. This is okay
15709         # for now as only exclusive open is supported. After generic lease
15710         # is done, this test suite should be revised. - Jinshan
15711
15712         remote_mds_nodsh && skip "remote MDS with nodsh"
15713         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
15714                 skip "Need MDS version at least 2.4.52"
15715
15716         echo "==== test 1: verify get lease work"
15717         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
15718
15719         echo "==== test 2: verify lease can be broken by upcoming open"
15720         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15721         local PID=$!
15722         sleep 1
15723
15724         $MULTIOP $DIR/$tfile oO_RDONLY:c
15725         kill -USR1 $PID && wait $PID || error "break lease error"
15726
15727         echo "==== test 3: verify lease can't be granted if an open already exists"
15728         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
15729         local PID=$!
15730         sleep 1
15731
15732         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
15733         kill -USR1 $PID && wait $PID || error "open file error"
15734
15735         echo "==== test 4: lease can sustain over recovery"
15736         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
15737         PID=$!
15738         sleep 1
15739
15740         fail mds1
15741
15742         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
15743
15744         echo "==== test 5: lease broken can't be regained by replay"
15745         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15746         PID=$!
15747         sleep 1
15748
15749         # open file to break lease and then recovery
15750         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
15751         fail mds1
15752
15753         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
15754
15755         rm -f $DIR/$tfile
15756 }
15757 run_test 208 "Exclusive open"
15758
15759 test_209() {
15760         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
15761                 skip_env "must have disp_stripe"
15762
15763         touch $DIR/$tfile
15764         sync; sleep 5; sync;
15765
15766         echo 3 > /proc/sys/vm/drop_caches
15767         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15768
15769         # open/close 500 times
15770         for i in $(seq 500); do
15771                 cat $DIR/$tfile
15772         done
15773
15774         echo 3 > /proc/sys/vm/drop_caches
15775         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15776
15777         echo "before: $req_before, after: $req_after"
15778         [ $((req_after - req_before)) -ge 300 ] &&
15779                 error "open/close requests are not freed"
15780         return 0
15781 }
15782 run_test 209 "read-only open/close requests should be freed promptly"
15783
15784 test_212() {
15785         size=`date +%s`
15786         size=$((size % 8192 + 1))
15787         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
15788         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
15789         rm -f $DIR/f212 $DIR/f212.xyz
15790 }
15791 run_test 212 "Sendfile test ============================================"
15792
15793 test_213() {
15794         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
15795         cancel_lru_locks osc
15796         lctl set_param fail_loc=0x8000040f
15797         # generate a read lock
15798         cat $DIR/$tfile > /dev/null
15799         # write to the file, it will try to cancel the above read lock.
15800         cat /etc/hosts >> $DIR/$tfile
15801 }
15802 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
15803
15804 test_214() { # for bug 20133
15805         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
15806         for (( i=0; i < 340; i++ )) ; do
15807                 touch $DIR/$tdir/d214c/a$i
15808         done
15809
15810         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
15811         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
15812         ls $DIR/d214c || error "ls $DIR/d214c failed"
15813         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
15814         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
15815 }
15816 run_test 214 "hash-indexed directory test - bug 20133"
15817
15818 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
15819 create_lnet_proc_files() {
15820         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
15821 }
15822
15823 # counterpart of create_lnet_proc_files
15824 remove_lnet_proc_files() {
15825         rm -f $TMP/lnet_$1.sys
15826 }
15827
15828 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15829 # 3rd arg as regexp for body
15830 check_lnet_proc_stats() {
15831         local l=$(cat "$TMP/lnet_$1" |wc -l)
15832         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
15833
15834         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
15835 }
15836
15837 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15838 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
15839 # optional and can be regexp for 2nd line (lnet.routes case)
15840 check_lnet_proc_entry() {
15841         local blp=2          # blp stands for 'position of 1st line of body'
15842         [ -z "$5" ] || blp=3 # lnet.routes case
15843
15844         local l=$(cat "$TMP/lnet_$1" |wc -l)
15845         # subtracting one from $blp because the body can be empty
15846         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
15847
15848         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
15849                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
15850
15851         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
15852                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
15853
15854         # bail out if any unexpected line happened
15855         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
15856         [ "$?" != 0 ] || error "$2 misformatted"
15857 }
15858
15859 test_215() { # for bugs 18102, 21079, 21517
15860         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15861
15862         local N='(0|[1-9][0-9]*)'       # non-negative numeric
15863         local P='[1-9][0-9]*'           # positive numeric
15864         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
15865         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
15866         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
15867         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
15868
15869         local L1 # regexp for 1st line
15870         local L2 # regexp for 2nd line (optional)
15871         local BR # regexp for the rest (body)
15872
15873         # lnet.stats should look as 11 space-separated non-negative numerics
15874         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
15875         create_lnet_proc_files "stats"
15876         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
15877         remove_lnet_proc_files "stats"
15878
15879         # lnet.routes should look like this:
15880         # Routing disabled/enabled
15881         # net hops priority state router
15882         # where net is a string like tcp0, hops > 0, priority >= 0,
15883         # state is up/down,
15884         # router is a string like 192.168.1.1@tcp2
15885         L1="^Routing (disabled|enabled)$"
15886         L2="^net +hops +priority +state +router$"
15887         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
15888         create_lnet_proc_files "routes"
15889         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
15890         remove_lnet_proc_files "routes"
15891
15892         # lnet.routers should look like this:
15893         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
15894         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
15895         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
15896         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
15897         L1="^ref +rtr_ref +alive +router$"
15898         BR="^$P +$P +(up|down) +$NID$"
15899         create_lnet_proc_files "routers"
15900         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
15901         remove_lnet_proc_files "routers"
15902
15903         # lnet.peers should look like this:
15904         # nid refs state last max rtr min tx min queue
15905         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
15906         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
15907         # numeric (0 or >0 or <0), queue >= 0.
15908         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
15909         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
15910         create_lnet_proc_files "peers"
15911         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
15912         remove_lnet_proc_files "peers"
15913
15914         # lnet.buffers  should look like this:
15915         # pages count credits min
15916         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
15917         L1="^pages +count +credits +min$"
15918         BR="^ +$N +$N +$I +$I$"
15919         create_lnet_proc_files "buffers"
15920         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
15921         remove_lnet_proc_files "buffers"
15922
15923         # lnet.nis should look like this:
15924         # nid status alive refs peer rtr max tx min
15925         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
15926         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
15927         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
15928         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
15929         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
15930         create_lnet_proc_files "nis"
15931         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
15932         remove_lnet_proc_files "nis"
15933
15934         # can we successfully write to lnet.stats?
15935         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
15936 }
15937 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
15938
15939 test_216() { # bug 20317
15940         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15941         remote_ost_nodsh && skip "remote OST with nodsh"
15942
15943         local node
15944         local facets=$(get_facets OST)
15945         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15946
15947         save_lustre_params client "osc.*.contention_seconds" > $p
15948         save_lustre_params $facets \
15949                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
15950         save_lustre_params $facets \
15951                 "ldlm.namespaces.filter-*.contended_locks" >> $p
15952         save_lustre_params $facets \
15953                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
15954         clear_stats osc.*.osc_stats
15955
15956         # agressive lockless i/o settings
15957         do_nodes $(comma_list $(osts_nodes)) \
15958                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
15959                         ldlm.namespaces.filter-*.contended_locks=0 \
15960                         ldlm.namespaces.filter-*.contention_seconds=60"
15961         lctl set_param -n osc.*.contention_seconds=60
15962
15963         $DIRECTIO write $DIR/$tfile 0 10 4096
15964         $CHECKSTAT -s 40960 $DIR/$tfile
15965
15966         # disable lockless i/o
15967         do_nodes $(comma_list $(osts_nodes)) \
15968                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
15969                         ldlm.namespaces.filter-*.contended_locks=32 \
15970                         ldlm.namespaces.filter-*.contention_seconds=0"
15971         lctl set_param -n osc.*.contention_seconds=0
15972         clear_stats osc.*.osc_stats
15973
15974         dd if=/dev/zero of=$DIR/$tfile count=0
15975         $CHECKSTAT -s 0 $DIR/$tfile
15976
15977         restore_lustre_params <$p
15978         rm -f $p
15979         rm $DIR/$tfile
15980 }
15981 run_test 216 "check lockless direct write updates file size and kms correctly"
15982
15983 test_217() { # bug 22430
15984         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15985
15986         local node
15987         local nid
15988
15989         for node in $(nodes_list); do
15990                 nid=$(host_nids_address $node $NETTYPE)
15991                 if [[ $nid = *-* ]] ; then
15992                         echo "lctl ping $(h2nettype $nid)"
15993                         lctl ping $(h2nettype $nid)
15994                 else
15995                         echo "skipping $node (no hyphen detected)"
15996                 fi
15997         done
15998 }
15999 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
16000
16001 test_218() {
16002        # do directio so as not to populate the page cache
16003        log "creating a 10 Mb file"
16004        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
16005        log "starting reads"
16006        dd if=$DIR/$tfile of=/dev/null bs=4096 &
16007        log "truncating the file"
16008        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
16009        log "killing dd"
16010        kill %+ || true # reads might have finished
16011        echo "wait until dd is finished"
16012        wait
16013        log "removing the temporary file"
16014        rm -rf $DIR/$tfile || error "tmp file removal failed"
16015 }
16016 run_test 218 "parallel read and truncate should not deadlock"
16017
16018 test_219() {
16019         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16020
16021         # write one partial page
16022         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
16023         # set no grant so vvp_io_commit_write will do sync write
16024         $LCTL set_param fail_loc=0x411
16025         # write a full page at the end of file
16026         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
16027
16028         $LCTL set_param fail_loc=0
16029         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
16030         $LCTL set_param fail_loc=0x411
16031         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
16032
16033         # LU-4201
16034         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
16035         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
16036 }
16037 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
16038
16039 test_220() { #LU-325
16040         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16041         remote_ost_nodsh && skip "remote OST with nodsh"
16042         remote_mds_nodsh && skip "remote MDS with nodsh"
16043         remote_mgs_nodsh && skip "remote MGS with nodsh"
16044
16045         local OSTIDX=0
16046
16047         # create on MDT0000 so the last_id and next_id are correct
16048         mkdir $DIR/$tdir
16049         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
16050         OST=${OST%_UUID}
16051
16052         # on the mdt's osc
16053         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
16054         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
16055                         osp.$mdtosc_proc1.prealloc_last_id)
16056         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
16057                         osp.$mdtosc_proc1.prealloc_next_id)
16058
16059         $LFS df -i
16060
16061         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
16062         #define OBD_FAIL_OST_ENOINO              0x229
16063         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
16064         create_pool $FSNAME.$TESTNAME || return 1
16065         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
16066
16067         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
16068
16069         MDSOBJS=$((last_id - next_id))
16070         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
16071
16072         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
16073         echo "OST still has $count kbytes free"
16074
16075         echo "create $MDSOBJS files @next_id..."
16076         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
16077
16078         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
16079                         osp.$mdtosc_proc1.prealloc_last_id)
16080         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
16081                         osp.$mdtosc_proc1.prealloc_next_id)
16082
16083         echo "after creation, last_id=$last_id2, next_id=$next_id2"
16084         $LFS df -i
16085
16086         echo "cleanup..."
16087
16088         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
16089         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
16090
16091         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
16092                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
16093         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
16094                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
16095         echo "unlink $MDSOBJS files @$next_id..."
16096         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
16097 }
16098 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
16099
16100 test_221() {
16101         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16102
16103         dd if=`which date` of=$MOUNT/date oflag=sync
16104         chmod +x $MOUNT/date
16105
16106         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
16107         $LCTL set_param fail_loc=0x80001401
16108
16109         $MOUNT/date > /dev/null
16110         rm -f $MOUNT/date
16111 }
16112 run_test 221 "make sure fault and truncate race to not cause OOM"
16113
16114 test_222a () {
16115         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16116
16117         rm -rf $DIR/$tdir
16118         test_mkdir $DIR/$tdir
16119         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16120         createmany -o $DIR/$tdir/$tfile 10
16121         cancel_lru_locks mdc
16122         cancel_lru_locks osc
16123         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16124         $LCTL set_param fail_loc=0x31a
16125         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
16126         $LCTL set_param fail_loc=0
16127         rm -r $DIR/$tdir
16128 }
16129 run_test 222a "AGL for ls should not trigger CLIO lock failure"
16130
16131 test_222b () {
16132         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16133
16134         rm -rf $DIR/$tdir
16135         test_mkdir $DIR/$tdir
16136         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16137         createmany -o $DIR/$tdir/$tfile 10
16138         cancel_lru_locks mdc
16139         cancel_lru_locks osc
16140         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16141         $LCTL set_param fail_loc=0x31a
16142         rm -r $DIR/$tdir || error "AGL for rmdir failed"
16143         $LCTL set_param fail_loc=0
16144 }
16145 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
16146
16147 test_223 () {
16148         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16149
16150         rm -rf $DIR/$tdir
16151         test_mkdir $DIR/$tdir
16152         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16153         createmany -o $DIR/$tdir/$tfile 10
16154         cancel_lru_locks mdc
16155         cancel_lru_locks osc
16156         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
16157         $LCTL set_param fail_loc=0x31b
16158         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
16159         $LCTL set_param fail_loc=0
16160         rm -r $DIR/$tdir
16161 }
16162 run_test 223 "osc reenqueue if without AGL lock granted ======================="
16163
16164 test_224a() { # LU-1039, MRP-303
16165         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16166
16167         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
16168         $LCTL set_param fail_loc=0x508
16169         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
16170         $LCTL set_param fail_loc=0
16171         df $DIR
16172 }
16173 run_test 224a "Don't panic on bulk IO failure"
16174
16175 test_224b() { # LU-1039, MRP-303
16176         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16177
16178         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
16179         cancel_lru_locks osc
16180         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
16181         $LCTL set_param fail_loc=0x515
16182         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
16183         $LCTL set_param fail_loc=0
16184         df $DIR
16185 }
16186 run_test 224b "Don't panic on bulk IO failure"
16187
16188 test_224c() { # LU-6441
16189         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16190         remote_mds_nodsh && skip "remote MDS with nodsh"
16191
16192         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16193         save_writethrough $p
16194         set_cache writethrough on
16195
16196         local pages_per_rpc=$($LCTL get_param osc.*.max_pages_per_rpc)
16197         local at_max=$($LCTL get_param -n at_max)
16198         local timeout=$($LCTL get_param -n timeout)
16199         local test_at="at_max"
16200         local param_at="$FSNAME.sys.at_max"
16201         local test_timeout="timeout"
16202         local param_timeout="$FSNAME.sys.timeout"
16203
16204         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
16205
16206         set_persistent_param_and_check client "$test_at" "$param_at" 0
16207         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
16208
16209         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
16210         do_facet ost1 "$LCTL set_param fail_loc=0x520"
16211         $LFS setstripe -c 1 -i 0 $DIR/$tfile
16212         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
16213         sync
16214         do_facet ost1 "$LCTL set_param fail_loc=0"
16215
16216         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
16217         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
16218                 $timeout
16219
16220         $LCTL set_param -n $pages_per_rpc
16221         restore_lustre_params < $p
16222         rm -f $p
16223 }
16224 run_test 224c "Don't hang if one of md lost during large bulk RPC"
16225
16226 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
16227 test_225a () {
16228         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16229         if [ -z ${MDSSURVEY} ]; then
16230                 skip_env "mds-survey not found"
16231         fi
16232         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16233                 skip "Need MDS version at least 2.2.51"
16234
16235         local mds=$(facet_host $SINGLEMDS)
16236         local target=$(do_nodes $mds 'lctl dl' |
16237                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16238
16239         local cmd1="file_count=1000 thrhi=4"
16240         local cmd2="dir_count=2 layer=mdd stripe_count=0"
16241         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16242         local cmd="$cmd1 $cmd2 $cmd3"
16243
16244         rm -f ${TMP}/mds_survey*
16245         echo + $cmd
16246         eval $cmd || error "mds-survey with zero-stripe failed"
16247         cat ${TMP}/mds_survey*
16248         rm -f ${TMP}/mds_survey*
16249 }
16250 run_test 225a "Metadata survey sanity with zero-stripe"
16251
16252 test_225b () {
16253         if [ -z ${MDSSURVEY} ]; then
16254                 skip_env "mds-survey not found"
16255         fi
16256         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16257                 skip "Need MDS version at least 2.2.51"
16258         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16259         remote_mds_nodsh && skip "remote MDS with nodsh"
16260         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
16261                 skip_env "Need to mount OST to test"
16262         fi
16263
16264         local mds=$(facet_host $SINGLEMDS)
16265         local target=$(do_nodes $mds 'lctl dl' |
16266                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16267
16268         local cmd1="file_count=1000 thrhi=4"
16269         local cmd2="dir_count=2 layer=mdd stripe_count=1"
16270         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16271         local cmd="$cmd1 $cmd2 $cmd3"
16272
16273         rm -f ${TMP}/mds_survey*
16274         echo + $cmd
16275         eval $cmd || error "mds-survey with stripe_count failed"
16276         cat ${TMP}/mds_survey*
16277         rm -f ${TMP}/mds_survey*
16278 }
16279 run_test 225b "Metadata survey sanity with stripe_count = 1"
16280
16281 mcreate_path2fid () {
16282         local mode=$1
16283         local major=$2
16284         local minor=$3
16285         local name=$4
16286         local desc=$5
16287         local path=$DIR/$tdir/$name
16288         local fid
16289         local rc
16290         local fid_path
16291
16292         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
16293                 error "cannot create $desc"
16294
16295         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
16296         rc=$?
16297         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
16298
16299         fid_path=$($LFS fid2path $MOUNT $fid)
16300         rc=$?
16301         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
16302
16303         [ "$path" == "$fid_path" ] ||
16304                 error "fid2path returned $fid_path, expected $path"
16305
16306         echo "pass with $path and $fid"
16307 }
16308
16309 test_226a () {
16310         rm -rf $DIR/$tdir
16311         mkdir -p $DIR/$tdir
16312
16313         mcreate_path2fid 0010666 0 0 fifo "FIFO"
16314         mcreate_path2fid 0020666 1 3 null "character special file (null)"
16315         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
16316         mcreate_path2fid 0040666 0 0 dir "directory"
16317         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
16318         mcreate_path2fid 0100666 0 0 file "regular file"
16319         mcreate_path2fid 0120666 0 0 link "symbolic link"
16320         mcreate_path2fid 0140666 0 0 sock "socket"
16321 }
16322 run_test 226a "call path2fid and fid2path on files of all type"
16323
16324 test_226b () {
16325         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16326
16327         local MDTIDX=1
16328
16329         rm -rf $DIR/$tdir
16330         mkdir -p $DIR/$tdir
16331         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
16332                 error "create remote directory failed"
16333         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
16334         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
16335                                 "character special file (null)"
16336         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
16337                                 "character special file (no device)"
16338         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
16339         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
16340                                 "block special file (loop)"
16341         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
16342         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
16343         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
16344 }
16345 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
16346
16347 # LU-1299 Executing or running ldd on a truncated executable does not
16348 # cause an out-of-memory condition.
16349 test_227() {
16350         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16351         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
16352
16353         dd if=$(which date) of=$MOUNT/date bs=1k count=1
16354         chmod +x $MOUNT/date
16355
16356         $MOUNT/date > /dev/null
16357         ldd $MOUNT/date > /dev/null
16358         rm -f $MOUNT/date
16359 }
16360 run_test 227 "running truncated executable does not cause OOM"
16361
16362 # LU-1512 try to reuse idle OI blocks
16363 test_228a() {
16364         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16365         remote_mds_nodsh && skip "remote MDS with nodsh"
16366         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16367
16368         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16369         local myDIR=$DIR/$tdir
16370
16371         mkdir -p $myDIR
16372         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16373         $LCTL set_param fail_loc=0x80001002
16374         createmany -o $myDIR/t- 10000
16375         $LCTL set_param fail_loc=0
16376         # The guard is current the largest FID holder
16377         touch $myDIR/guard
16378         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16379                     tr -d '[')
16380         local IDX=$(($SEQ % 64))
16381
16382         do_facet $SINGLEMDS sync
16383         # Make sure journal flushed.
16384         sleep 6
16385         local blk1=$(do_facet $SINGLEMDS \
16386                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16387                      grep Blockcount | awk '{print $4}')
16388
16389         # Remove old files, some OI blocks will become idle.
16390         unlinkmany $myDIR/t- 10000
16391         # Create new files, idle OI blocks should be reused.
16392         createmany -o $myDIR/t- 2000
16393         do_facet $SINGLEMDS sync
16394         # Make sure journal flushed.
16395         sleep 6
16396         local blk2=$(do_facet $SINGLEMDS \
16397                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16398                      grep Blockcount | awk '{print $4}')
16399
16400         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16401 }
16402 run_test 228a "try to reuse idle OI blocks"
16403
16404 test_228b() {
16405         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16406         remote_mds_nodsh && skip "remote MDS with nodsh"
16407         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16408
16409         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16410         local myDIR=$DIR/$tdir
16411
16412         mkdir -p $myDIR
16413         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16414         $LCTL set_param fail_loc=0x80001002
16415         createmany -o $myDIR/t- 10000
16416         $LCTL set_param fail_loc=0
16417         # The guard is current the largest FID holder
16418         touch $myDIR/guard
16419         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16420                     tr -d '[')
16421         local IDX=$(($SEQ % 64))
16422
16423         do_facet $SINGLEMDS sync
16424         # Make sure journal flushed.
16425         sleep 6
16426         local blk1=$(do_facet $SINGLEMDS \
16427                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16428                      grep Blockcount | awk '{print $4}')
16429
16430         # Remove old files, some OI blocks will become idle.
16431         unlinkmany $myDIR/t- 10000
16432
16433         # stop the MDT
16434         stop $SINGLEMDS || error "Fail to stop MDT."
16435         # remount the MDT
16436         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
16437
16438         df $MOUNT || error "Fail to df."
16439         # Create new files, idle OI blocks should be reused.
16440         createmany -o $myDIR/t- 2000
16441         do_facet $SINGLEMDS sync
16442         # Make sure journal flushed.
16443         sleep 6
16444         local blk2=$(do_facet $SINGLEMDS \
16445                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16446                      grep Blockcount | awk '{print $4}')
16447
16448         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16449 }
16450 run_test 228b "idle OI blocks can be reused after MDT restart"
16451
16452 #LU-1881
16453 test_228c() {
16454         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16455         remote_mds_nodsh && skip "remote MDS with nodsh"
16456         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16457
16458         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16459         local myDIR=$DIR/$tdir
16460
16461         mkdir -p $myDIR
16462         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16463         $LCTL set_param fail_loc=0x80001002
16464         # 20000 files can guarantee there are index nodes in the OI file
16465         createmany -o $myDIR/t- 20000
16466         $LCTL set_param fail_loc=0
16467         # The guard is current the largest FID holder
16468         touch $myDIR/guard
16469         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16470                     tr -d '[')
16471         local IDX=$(($SEQ % 64))
16472
16473         do_facet $SINGLEMDS sync
16474         # Make sure journal flushed.
16475         sleep 6
16476         local blk1=$(do_facet $SINGLEMDS \
16477                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16478                      grep Blockcount | awk '{print $4}')
16479
16480         # Remove old files, some OI blocks will become idle.
16481         unlinkmany $myDIR/t- 20000
16482         rm -f $myDIR/guard
16483         # The OI file should become empty now
16484
16485         # Create new files, idle OI blocks should be reused.
16486         createmany -o $myDIR/t- 2000
16487         do_facet $SINGLEMDS sync
16488         # Make sure journal flushed.
16489         sleep 6
16490         local blk2=$(do_facet $SINGLEMDS \
16491                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16492                      grep Blockcount | awk '{print $4}')
16493
16494         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16495 }
16496 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
16497
16498 test_229() { # LU-2482, LU-3448
16499         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16500         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
16501         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
16502                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
16503
16504         rm -f $DIR/$tfile
16505
16506         # Create a file with a released layout and stripe count 2.
16507         $MULTIOP $DIR/$tfile H2c ||
16508                 error "failed to create file with released layout"
16509
16510         $LFS getstripe -v $DIR/$tfile
16511
16512         local pattern=$($LFS getstripe -L $DIR/$tfile)
16513         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
16514
16515         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
16516                 error "getstripe"
16517         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
16518         stat $DIR/$tfile || error "failed to stat released file"
16519
16520         chown $RUNAS_ID $DIR/$tfile ||
16521                 error "chown $RUNAS_ID $DIR/$tfile failed"
16522
16523         chgrp $RUNAS_ID $DIR/$tfile ||
16524                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
16525
16526         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
16527         rm $DIR/$tfile || error "failed to remove released file"
16528 }
16529 run_test 229 "getstripe/stat/rm/attr changes work on released files"
16530
16531 test_230a() {
16532         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16533         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16534         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16535                 skip "Need MDS version at least 2.11.52"
16536
16537         local MDTIDX=1
16538
16539         test_mkdir $DIR/$tdir
16540         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
16541         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
16542         [ $mdt_idx -ne 0 ] &&
16543                 error "create local directory on wrong MDT $mdt_idx"
16544
16545         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
16546                         error "create remote directory failed"
16547         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
16548         [ $mdt_idx -ne $MDTIDX ] &&
16549                 error "create remote directory on wrong MDT $mdt_idx"
16550
16551         createmany -o $DIR/$tdir/test_230/t- 10 ||
16552                 error "create files on remote directory failed"
16553         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
16554         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
16555         rm -r $DIR/$tdir || error "unlink remote directory failed"
16556 }
16557 run_test 230a "Create remote directory and files under the remote directory"
16558
16559 test_230b() {
16560         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16561         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16562         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16563                 skip "Need MDS version at least 2.11.52"
16564
16565         local MDTIDX=1
16566         local mdt_index
16567         local i
16568         local file
16569         local pid
16570         local stripe_count
16571         local migrate_dir=$DIR/$tdir/migrate_dir
16572         local other_dir=$DIR/$tdir/other_dir
16573
16574         test_mkdir $DIR/$tdir
16575         test_mkdir -i0 -c1 $migrate_dir
16576         test_mkdir -i0 -c1 $other_dir
16577         for ((i=0; i<10; i++)); do
16578                 mkdir -p $migrate_dir/dir_${i}
16579                 createmany -o $migrate_dir/dir_${i}/f 10 ||
16580                         error "create files under remote dir failed $i"
16581         done
16582
16583         cp /etc/passwd $migrate_dir/$tfile
16584         cp /etc/passwd $other_dir/$tfile
16585         chattr +SAD $migrate_dir
16586         chattr +SAD $migrate_dir/$tfile
16587
16588         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16589         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16590         local old_dir_mode=$(stat -c%f $migrate_dir)
16591         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
16592
16593         mkdir -p $migrate_dir/dir_default_stripe2
16594         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
16595         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
16596
16597         mkdir -p $other_dir
16598         ln $migrate_dir/$tfile $other_dir/luna
16599         ln $migrate_dir/$tfile $migrate_dir/sofia
16600         ln $other_dir/$tfile $migrate_dir/david
16601         ln -s $migrate_dir/$tfile $other_dir/zachary
16602         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
16603         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
16604
16605         $LFS migrate -m $MDTIDX $migrate_dir ||
16606                 error "fails on migrating remote dir to MDT1"
16607
16608         echo "migratate to MDT1, then checking.."
16609         for ((i = 0; i < 10; i++)); do
16610                 for file in $(find $migrate_dir/dir_${i}); do
16611                         mdt_index=$($LFS getstripe -m $file)
16612                         [ $mdt_index == $MDTIDX ] ||
16613                                 error "$file is not on MDT${MDTIDX}"
16614                 done
16615         done
16616
16617         # the multiple link file should still in MDT0
16618         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
16619         [ $mdt_index == 0 ] ||
16620                 error "$file is not on MDT${MDTIDX}"
16621
16622         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16623         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16624                 error " expect $old_dir_flag get $new_dir_flag"
16625
16626         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16627         [ "$old_file_flag" = "$new_file_flag" ] ||
16628                 error " expect $old_file_flag get $new_file_flag"
16629
16630         local new_dir_mode=$(stat -c%f $migrate_dir)
16631         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16632                 error "expect mode $old_dir_mode get $new_dir_mode"
16633
16634         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16635         [ "$old_file_mode" = "$new_file_mode" ] ||
16636                 error "expect mode $old_file_mode get $new_file_mode"
16637
16638         diff /etc/passwd $migrate_dir/$tfile ||
16639                 error "$tfile different after migration"
16640
16641         diff /etc/passwd $other_dir/luna ||
16642                 error "luna different after migration"
16643
16644         diff /etc/passwd $migrate_dir/sofia ||
16645                 error "sofia different after migration"
16646
16647         diff /etc/passwd $migrate_dir/david ||
16648                 error "david different after migration"
16649
16650         diff /etc/passwd $other_dir/zachary ||
16651                 error "zachary different after migration"
16652
16653         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16654                 error "${tfile}_ln different after migration"
16655
16656         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16657                 error "${tfile}_ln_other different after migration"
16658
16659         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
16660         [ $stripe_count = 2 ] ||
16661                 error "dir strpe_count $d != 2 after migration."
16662
16663         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
16664         [ $stripe_count = 2 ] ||
16665                 error "file strpe_count $d != 2 after migration."
16666
16667         #migrate back to MDT0
16668         MDTIDX=0
16669
16670         $LFS migrate -m $MDTIDX $migrate_dir ||
16671                 error "fails on migrating remote dir to MDT0"
16672
16673         echo "migrate back to MDT0, checking.."
16674         for file in $(find $migrate_dir); do
16675                 mdt_index=$($LFS getstripe -m $file)
16676                 [ $mdt_index == $MDTIDX ] ||
16677                         error "$file is not on MDT${MDTIDX}"
16678         done
16679
16680         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16681         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16682                 error " expect $old_dir_flag get $new_dir_flag"
16683
16684         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16685         [ "$old_file_flag" = "$new_file_flag" ] ||
16686                 error " expect $old_file_flag get $new_file_flag"
16687
16688         local new_dir_mode=$(stat -c%f $migrate_dir)
16689         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16690                 error "expect mode $old_dir_mode get $new_dir_mode"
16691
16692         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16693         [ "$old_file_mode" = "$new_file_mode" ] ||
16694                 error "expect mode $old_file_mode get $new_file_mode"
16695
16696         diff /etc/passwd ${migrate_dir}/$tfile ||
16697                 error "$tfile different after migration"
16698
16699         diff /etc/passwd ${other_dir}/luna ||
16700                 error "luna different after migration"
16701
16702         diff /etc/passwd ${migrate_dir}/sofia ||
16703                 error "sofia different after migration"
16704
16705         diff /etc/passwd ${other_dir}/zachary ||
16706                 error "zachary different after migration"
16707
16708         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16709                 error "${tfile}_ln different after migration"
16710
16711         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16712                 error "${tfile}_ln_other different after migration"
16713
16714         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
16715         [ $stripe_count = 2 ] ||
16716                 error "dir strpe_count $d != 2 after migration."
16717
16718         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
16719         [ $stripe_count = 2 ] ||
16720                 error "file strpe_count $d != 2 after migration."
16721
16722         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16723 }
16724 run_test 230b "migrate directory"
16725
16726 test_230c() {
16727         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16728         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16729         remote_mds_nodsh && skip "remote MDS with nodsh"
16730         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16731                 skip "Need MDS version at least 2.11.52"
16732
16733         local MDTIDX=1
16734         local total=3
16735         local mdt_index
16736         local file
16737         local migrate_dir=$DIR/$tdir/migrate_dir
16738
16739         #If migrating directory fails in the middle, all entries of
16740         #the directory is still accessiable.
16741         test_mkdir $DIR/$tdir
16742         test_mkdir -i0 -c1 $migrate_dir
16743         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
16744         stat $migrate_dir
16745         createmany -o $migrate_dir/f $total ||
16746                 error "create files under ${migrate_dir} failed"
16747
16748         # fail after migrating top dir, and this will fail only once, so the
16749         # first sub file migration will fail (currently f3), others succeed.
16750         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
16751         do_facet mds1 lctl set_param fail_loc=0x1801
16752         local t=$(ls $migrate_dir | wc -l)
16753         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
16754                 error "migrate should fail"
16755         local u=$(ls $migrate_dir | wc -l)
16756         [ "$u" == "$t" ] || error "$u != $t during migration"
16757
16758         # add new dir/file should succeed
16759         mkdir $migrate_dir/dir ||
16760                 error "mkdir failed under migrating directory"
16761         touch $migrate_dir/file ||
16762                 error "create file failed under migrating directory"
16763
16764         # add file with existing name should fail
16765         for file in $migrate_dir/f*; do
16766                 stat $file > /dev/null || error "stat $file failed"
16767                 $OPENFILE -f O_CREAT:O_EXCL $file &&
16768                         error "open(O_CREAT|O_EXCL) $file should fail"
16769                 $MULTIOP $file m && error "create $file should fail"
16770                 touch $DIR/$tdir/remote_dir/$tfile ||
16771                         error "touch $tfile failed"
16772                 ln $DIR/$tdir/remote_dir/$tfile $file &&
16773                         error "link $file should fail"
16774                 mdt_index=$($LFS getstripe -m $file)
16775                 if [ $mdt_index == 0 ]; then
16776                         # file failed to migrate is not allowed to rename to
16777                         mv $DIR/$tdir/remote_dir/$tfile $file &&
16778                                 error "rename to $file should fail"
16779                 else
16780                         mv $DIR/$tdir/remote_dir/$tfile $file ||
16781                                 error "rename to $file failed"
16782                 fi
16783                 echo hello >> $file || error "write $file failed"
16784         done
16785
16786         # resume migration with different options should fail
16787         $LFS migrate -m 0 $migrate_dir &&
16788                 error "migrate -m 0 $migrate_dir should fail"
16789
16790         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
16791                 error "migrate -c 2 $migrate_dir should fail"
16792
16793         # resume migration should succeed
16794         $LFS migrate -m $MDTIDX $migrate_dir ||
16795                 error "migrate $migrate_dir failed"
16796
16797         echo "Finish migration, then checking.."
16798         for file in $(find $migrate_dir); do
16799                 mdt_index=$($LFS getstripe -m $file)
16800                 [ $mdt_index == $MDTIDX ] ||
16801                         error "$file is not on MDT${MDTIDX}"
16802         done
16803
16804         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16805 }
16806 run_test 230c "check directory accessiblity if migration failed"
16807
16808 test_230d() {
16809         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16810         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16811         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16812                 skip "Need MDS version at least 2.11.52"
16813         # LU-11235
16814         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
16815
16816         local migrate_dir=$DIR/$tdir/migrate_dir
16817         local old_index
16818         local new_index
16819         local old_count
16820         local new_count
16821         local new_hash
16822         local mdt_index
16823         local i
16824         local j
16825
16826         old_index=$((RANDOM % MDSCOUNT))
16827         old_count=$((MDSCOUNT - old_index))
16828         new_index=$((RANDOM % MDSCOUNT))
16829         new_count=$((MDSCOUNT - new_index))
16830         new_hash="all_char"
16831
16832         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
16833         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
16834
16835         test_mkdir $DIR/$tdir
16836         test_mkdir -i $old_index -c $old_count $migrate_dir
16837
16838         for ((i=0; i<100; i++)); do
16839                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
16840                 createmany -o $migrate_dir/dir_${i}/f 100 ||
16841                         error "create files under remote dir failed $i"
16842         done
16843
16844         echo -n "Migrate from MDT$old_index "
16845         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
16846         echo -n "to MDT$new_index"
16847         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
16848         echo
16849
16850         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
16851         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
16852                 error "migrate remote dir error"
16853
16854         echo "Finish migration, then checking.."
16855         for file in $(find $migrate_dir); do
16856                 mdt_index=$($LFS getstripe -m $file)
16857                 if [ $mdt_index -lt $new_index ] ||
16858                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
16859                         error "$file is on MDT$mdt_index"
16860                 fi
16861         done
16862
16863         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16864 }
16865 run_test 230d "check migrate big directory"
16866
16867 test_230e() {
16868         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16869         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16870         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16871                 skip "Need MDS version at least 2.11.52"
16872
16873         local i
16874         local j
16875         local a_fid
16876         local b_fid
16877
16878         mkdir -p $DIR/$tdir
16879         mkdir $DIR/$tdir/migrate_dir
16880         mkdir $DIR/$tdir/other_dir
16881         touch $DIR/$tdir/migrate_dir/a
16882         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
16883         ls $DIR/$tdir/other_dir
16884
16885         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16886                 error "migrate dir fails"
16887
16888         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16889         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16890
16891         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16892         [ $mdt_index == 0 ] || error "a is not on MDT0"
16893
16894         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
16895                 error "migrate dir fails"
16896
16897         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
16898         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
16899
16900         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16901         [ $mdt_index == 1 ] || error "a is not on MDT1"
16902
16903         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
16904         [ $mdt_index == 1 ] || error "b is not on MDT1"
16905
16906         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16907         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
16908
16909         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
16910
16911         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16912 }
16913 run_test 230e "migrate mulitple local link files"
16914
16915 test_230f() {
16916         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16917         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16918         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16919                 skip "Need MDS version at least 2.11.52"
16920
16921         local a_fid
16922         local ln_fid
16923
16924         mkdir -p $DIR/$tdir
16925         mkdir $DIR/$tdir/migrate_dir
16926         $LFS mkdir -i1 $DIR/$tdir/other_dir
16927         touch $DIR/$tdir/migrate_dir/a
16928         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
16929         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
16930         ls $DIR/$tdir/other_dir
16931
16932         # a should be migrated to MDT1, since no other links on MDT0
16933         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16934                 error "#1 migrate dir fails"
16935         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16936         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16937         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16938         [ $mdt_index == 1 ] || error "a is not on MDT1"
16939
16940         # a should stay on MDT1, because it is a mulitple link file
16941         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16942                 error "#2 migrate dir fails"
16943         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16944         [ $mdt_index == 1 ] || error "a is not on MDT1"
16945
16946         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16947                 error "#3 migrate dir fails"
16948
16949         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16950         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
16951         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
16952
16953         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
16954         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
16955
16956         # a should be migrated to MDT0, since no other links on MDT1
16957         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16958                 error "#4 migrate dir fails"
16959         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16960         [ $mdt_index == 0 ] || error "a is not on MDT0"
16961
16962         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16963 }
16964 run_test 230f "migrate mulitple remote link files"
16965
16966 test_230g() {
16967         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16968         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16969         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16970                 skip "Need MDS version at least 2.11.52"
16971
16972         mkdir -p $DIR/$tdir/migrate_dir
16973
16974         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
16975                 error "migrating dir to non-exist MDT succeeds"
16976         true
16977 }
16978 run_test 230g "migrate dir to non-exist MDT"
16979
16980 test_230h() {
16981         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16982         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16983         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16984                 skip "Need MDS version at least 2.11.52"
16985
16986         local mdt_index
16987
16988         mkdir -p $DIR/$tdir/migrate_dir
16989
16990         $LFS migrate -m1 $DIR &&
16991                 error "migrating mountpoint1 should fail"
16992
16993         $LFS migrate -m1 $DIR/$tdir/.. &&
16994                 error "migrating mountpoint2 should fail"
16995
16996         # same as mv
16997         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
16998                 error "migrating $tdir/migrate_dir/.. should fail"
16999
17000         true
17001 }
17002 run_test 230h "migrate .. and root"
17003
17004 test_230i() {
17005         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17006         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17007         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17008                 skip "Need MDS version at least 2.11.52"
17009
17010         mkdir -p $DIR/$tdir/migrate_dir
17011
17012         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
17013                 error "migration fails with a tailing slash"
17014
17015         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
17016                 error "migration fails with two tailing slashes"
17017 }
17018 run_test 230i "lfs migrate -m tolerates trailing slashes"
17019
17020 test_230j() {
17021         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17022         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17023                 skip "Need MDS version at least 2.11.52"
17024
17025         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
17026         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
17027                 error "create $tfile failed"
17028         cat /etc/passwd > $DIR/$tdir/$tfile
17029
17030         $LFS migrate -m 1 $DIR/$tdir
17031
17032         cmp /etc/passwd $DIR/$tdir/$tfile ||
17033                 error "DoM file mismatch after migration"
17034 }
17035 run_test 230j "DoM file data not changed after dir migration"
17036
17037 test_230k() {
17038         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
17039         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17040                 skip "Need MDS version at least 2.11.56"
17041
17042         local total=20
17043         local files_on_starting_mdt=0
17044
17045         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
17046         $LFS getdirstripe $DIR/$tdir
17047         for i in $(seq $total); do
17048                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
17049                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
17050                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17051         done
17052
17053         echo "$files_on_starting_mdt files on MDT0"
17054
17055         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
17056         $LFS getdirstripe $DIR/$tdir
17057
17058         files_on_starting_mdt=0
17059         for i in $(seq $total); do
17060                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
17061                         error "file $tfile.$i mismatch after migration"
17062                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
17063                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17064         done
17065
17066         echo "$files_on_starting_mdt files on MDT1 after migration"
17067         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
17068
17069         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
17070         $LFS getdirstripe $DIR/$tdir
17071
17072         files_on_starting_mdt=0
17073         for i in $(seq $total); do
17074                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
17075                         error "file $tfile.$i mismatch after 2nd migration"
17076                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
17077                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17078         done
17079
17080         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
17081         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
17082
17083         true
17084 }
17085 run_test 230k "file data not changed after dir migration"
17086
17087 test_230l() {
17088         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17089         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17090                 skip "Need MDS version at least 2.11.56"
17091
17092         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
17093         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
17094                 error "create files under remote dir failed $i"
17095         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
17096 }
17097 run_test 230l "readdir between MDTs won't crash"
17098
17099 test_230m() {
17100         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17101         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17102                 skip "Need MDS version at least 2.11.56"
17103
17104         local MDTIDX=1
17105         local mig_dir=$DIR/$tdir/migrate_dir
17106         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
17107         local shortstr="b"
17108         local val
17109
17110         echo "Creating files and dirs with xattrs"
17111         test_mkdir $DIR/$tdir
17112         test_mkdir -i0 -c1 $mig_dir
17113         mkdir $mig_dir/dir
17114         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
17115                 error "cannot set xattr attr1 on dir"
17116         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
17117                 error "cannot set xattr attr2 on dir"
17118         touch $mig_dir/dir/f0
17119         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
17120                 error "cannot set xattr attr1 on file"
17121         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
17122                 error "cannot set xattr attr2 on file"
17123         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17124         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17125         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
17126         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17127         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
17128         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17129         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
17130         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17131         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
17132
17133         echo "Migrating to MDT1"
17134         $LFS migrate -m $MDTIDX $mig_dir ||
17135                 error "fails on migrating dir to MDT1"
17136
17137         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17138         echo "Checking xattrs"
17139         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17140         [ "$val" = $longstr ] ||
17141                 error "expecting xattr1 $longstr on dir, found $val"
17142         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17143         [ "$val" = $shortstr ] ||
17144                 error "expecting xattr2 $shortstr on dir, found $val"
17145         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17146         [ "$val" = $longstr ] ||
17147                 error "expecting xattr1 $longstr on file, found $val"
17148         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17149         [ "$val" = $shortstr ] ||
17150                 error "expecting xattr2 $shortstr on file, found $val"
17151 }
17152 run_test 230m "xattrs not changed after dir migration"
17153
17154 test_231a()
17155 {
17156         # For simplicity this test assumes that max_pages_per_rpc
17157         # is the same across all OSCs
17158         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
17159         local bulk_size=$((max_pages * PAGE_SIZE))
17160         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
17161                                        head -n 1)
17162
17163         mkdir -p $DIR/$tdir
17164         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
17165                 error "failed to set stripe with -S ${brw_size}M option"
17166
17167         # clear the OSC stats
17168         $LCTL set_param osc.*.stats=0 &>/dev/null
17169         stop_writeback
17170
17171         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
17172         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
17173                 oflag=direct &>/dev/null || error "dd failed"
17174
17175         sync; sleep 1; sync # just to be safe
17176         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
17177         if [ x$nrpcs != "x1" ]; then
17178                 $LCTL get_param osc.*.stats
17179                 error "found $nrpcs ost_write RPCs, not 1 as expected"
17180         fi
17181
17182         start_writeback
17183         # Drop the OSC cache, otherwise we will read from it
17184         cancel_lru_locks osc
17185
17186         # clear the OSC stats
17187         $LCTL set_param osc.*.stats=0 &>/dev/null
17188
17189         # Client reads $bulk_size.
17190         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
17191                 iflag=direct &>/dev/null || error "dd failed"
17192
17193         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
17194         if [ x$nrpcs != "x1" ]; then
17195                 $LCTL get_param osc.*.stats
17196                 error "found $nrpcs ost_read RPCs, not 1 as expected"
17197         fi
17198 }
17199 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
17200
17201 test_231b() {
17202         mkdir -p $DIR/$tdir
17203         local i
17204         for i in {0..1023}; do
17205                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
17206                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
17207                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
17208         done
17209         sync
17210 }
17211 run_test 231b "must not assert on fully utilized OST request buffer"
17212
17213 test_232a() {
17214         mkdir -p $DIR/$tdir
17215         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17216
17217         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17218         do_facet ost1 $LCTL set_param fail_loc=0x31c
17219
17220         # ignore dd failure
17221         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
17222
17223         do_facet ost1 $LCTL set_param fail_loc=0
17224         umount_client $MOUNT || error "umount failed"
17225         mount_client $MOUNT || error "mount failed"
17226         stop ost1 || error "cannot stop ost1"
17227         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17228 }
17229 run_test 232a "failed lock should not block umount"
17230
17231 test_232b() {
17232         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
17233                 skip "Need MDS version at least 2.10.58"
17234
17235         mkdir -p $DIR/$tdir
17236         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17237         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
17238         sync
17239         cancel_lru_locks osc
17240
17241         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17242         do_facet ost1 $LCTL set_param fail_loc=0x31c
17243
17244         # ignore failure
17245         $LFS data_version $DIR/$tdir/$tfile || true
17246
17247         do_facet ost1 $LCTL set_param fail_loc=0
17248         umount_client $MOUNT || error "umount failed"
17249         mount_client $MOUNT || error "mount failed"
17250         stop ost1 || error "cannot stop ost1"
17251         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17252 }
17253 run_test 232b "failed data version lock should not block umount"
17254
17255 test_233a() {
17256         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
17257                 skip "Need MDS version at least 2.3.64"
17258         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17259
17260         local fid=$($LFS path2fid $MOUNT)
17261
17262         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17263                 error "cannot access $MOUNT using its FID '$fid'"
17264 }
17265 run_test 233a "checking that OBF of the FS root succeeds"
17266
17267 test_233b() {
17268         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
17269                 skip "Need MDS version at least 2.5.90"
17270         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17271
17272         local fid=$($LFS path2fid $MOUNT/.lustre)
17273
17274         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17275                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
17276
17277         fid=$($LFS path2fid $MOUNT/.lustre/fid)
17278         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17279                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
17280 }
17281 run_test 233b "checking that OBF of the FS .lustre succeeds"
17282
17283 test_234() {
17284         local p="$TMP/sanityN-$TESTNAME.parameters"
17285         save_lustre_params client "llite.*.xattr_cache" > $p
17286         lctl set_param llite.*.xattr_cache 1 ||
17287                 skip_env "xattr cache is not supported"
17288
17289         mkdir -p $DIR/$tdir || error "mkdir failed"
17290         touch $DIR/$tdir/$tfile || error "touch failed"
17291         # OBD_FAIL_LLITE_XATTR_ENOMEM
17292         $LCTL set_param fail_loc=0x1405
17293         getfattr -n user.attr $DIR/$tdir/$tfile &&
17294                 error "getfattr should have failed with ENOMEM"
17295         $LCTL set_param fail_loc=0x0
17296         rm -rf $DIR/$tdir
17297
17298         restore_lustre_params < $p
17299         rm -f $p
17300 }
17301 run_test 234 "xattr cache should not crash on ENOMEM"
17302
17303 test_235() {
17304         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
17305                 skip "Need MDS version at least 2.4.52"
17306
17307         flock_deadlock $DIR/$tfile
17308         local RC=$?
17309         case $RC in
17310                 0)
17311                 ;;
17312                 124) error "process hangs on a deadlock"
17313                 ;;
17314                 *) error "error executing flock_deadlock $DIR/$tfile"
17315                 ;;
17316         esac
17317 }
17318 run_test 235 "LU-1715: flock deadlock detection does not work properly"
17319
17320 #LU-2935
17321 test_236() {
17322         check_swap_layouts_support
17323
17324         local ref1=/etc/passwd
17325         local ref2=/etc/group
17326         local file1=$DIR/$tdir/f1
17327         local file2=$DIR/$tdir/f2
17328
17329         test_mkdir -c1 $DIR/$tdir
17330         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
17331         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
17332         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
17333         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
17334         local fd=$(free_fd)
17335         local cmd="exec $fd<>$file2"
17336         eval $cmd
17337         rm $file2
17338         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
17339                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
17340         cmd="exec $fd>&-"
17341         eval $cmd
17342         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
17343
17344         #cleanup
17345         rm -rf $DIR/$tdir
17346 }
17347 run_test 236 "Layout swap on open unlinked file"
17348
17349 # LU-4659 linkea consistency
17350 test_238() {
17351         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
17352                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
17353                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
17354                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
17355
17356         touch $DIR/$tfile
17357         ln $DIR/$tfile $DIR/$tfile.lnk
17358         touch $DIR/$tfile.new
17359         mv $DIR/$tfile.new $DIR/$tfile
17360         local fid1=$($LFS path2fid $DIR/$tfile)
17361         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
17362         local path1=$($LFS fid2path $FSNAME "$fid1")
17363         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
17364         local path2=$($LFS fid2path $FSNAME "$fid2")
17365         [ $tfile.lnk == $path2 ] ||
17366                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
17367         rm -f $DIR/$tfile*
17368 }
17369 run_test 238 "Verify linkea consistency"
17370
17371 test_239A() { # was test_239
17372         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
17373                 skip "Need MDS version at least 2.5.60"
17374
17375         local list=$(comma_list $(mdts_nodes))
17376
17377         mkdir -p $DIR/$tdir
17378         createmany -o $DIR/$tdir/f- 5000
17379         unlinkmany $DIR/$tdir/f- 5000
17380         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
17381                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
17382         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
17383                         osp.*MDT*.sync_in_flight" | calc_sum)
17384         [ "$changes" -eq 0 ] || error "$changes not synced"
17385 }
17386 run_test 239A "osp_sync test"
17387
17388 test_239a() { #LU-5297
17389         remote_mds_nodsh && skip "remote MDS with nodsh"
17390
17391         touch $DIR/$tfile
17392         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
17393         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
17394         chgrp $RUNAS_GID $DIR/$tfile
17395         wait_delete_completed
17396 }
17397 run_test 239a "process invalid osp sync record correctly"
17398
17399 test_239b() { #LU-5297
17400         remote_mds_nodsh && skip "remote MDS with nodsh"
17401
17402         touch $DIR/$tfile1
17403         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
17404         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
17405         chgrp $RUNAS_GID $DIR/$tfile1
17406         wait_delete_completed
17407         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17408         touch $DIR/$tfile2
17409         chgrp $RUNAS_GID $DIR/$tfile2
17410         wait_delete_completed
17411 }
17412 run_test 239b "process osp sync record with ENOMEM error correctly"
17413
17414 test_240() {
17415         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17416         remote_mds_nodsh && skip "remote MDS with nodsh"
17417
17418         mkdir -p $DIR/$tdir
17419
17420         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
17421                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
17422         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
17423                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
17424
17425         umount_client $MOUNT || error "umount failed"
17426         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
17427         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
17428         mount_client $MOUNT || error "failed to mount client"
17429
17430         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
17431         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
17432 }
17433 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
17434
17435 test_241_bio() {
17436         local count=$1
17437         local bsize=$2
17438
17439         for LOOP in $(seq $count); do
17440                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
17441                 cancel_lru_locks $OSC || true
17442         done
17443 }
17444
17445 test_241_dio() {
17446         local count=$1
17447         local bsize=$2
17448
17449         for LOOP in $(seq $1); do
17450                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
17451                         2>/dev/null
17452         done
17453 }
17454
17455 test_241a() { # was test_241
17456         local bsize=$PAGE_SIZE
17457
17458         (( bsize < 40960 )) && bsize=40960
17459         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17460         ls -la $DIR/$tfile
17461         cancel_lru_locks $OSC
17462         test_241_bio 1000 $bsize &
17463         PID=$!
17464         test_241_dio 1000 $bsize
17465         wait $PID
17466 }
17467 run_test 241a "bio vs dio"
17468
17469 test_241b() {
17470         local bsize=$PAGE_SIZE
17471
17472         (( bsize < 40960 )) && bsize=40960
17473         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17474         ls -la $DIR/$tfile
17475         test_241_dio 1000 $bsize &
17476         PID=$!
17477         test_241_dio 1000 $bsize
17478         wait $PID
17479 }
17480 run_test 241b "dio vs dio"
17481
17482 test_242() {
17483         remote_mds_nodsh && skip "remote MDS with nodsh"
17484
17485         mkdir -p $DIR/$tdir
17486         touch $DIR/$tdir/$tfile
17487
17488         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
17489         do_facet mds1 lctl set_param fail_loc=0x105
17490         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
17491
17492         do_facet mds1 lctl set_param fail_loc=0
17493         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
17494 }
17495 run_test 242 "mdt_readpage failure should not cause directory unreadable"
17496
17497 test_243()
17498 {
17499         test_mkdir $DIR/$tdir
17500         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
17501 }
17502 run_test 243 "various group lock tests"
17503
17504 test_244a()
17505 {
17506         test_mkdir $DIR/$tdir
17507         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
17508         sendfile_grouplock $DIR/$tdir/$tfile || \
17509                 error "sendfile+grouplock failed"
17510         rm -rf $DIR/$tdir
17511 }
17512 run_test 244a "sendfile with group lock tests"
17513
17514 test_244b()
17515 {
17516         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
17517
17518         local threads=50
17519         local size=$((1024*1024))
17520
17521         test_mkdir $DIR/$tdir
17522         for i in $(seq 1 $threads); do
17523                 local file=$DIR/$tdir/file_$((i / 10))
17524                 $MULTIOP $file OG1234w$size_$((i % 3))w$size_$((i % 4))g1234c &
17525                 local pids[$i]=$!
17526         done
17527         for i in $(seq 1 $threads); do
17528                 wait ${pids[$i]}
17529         done
17530 }
17531 run_test 244b "multi-threaded write with group lock"
17532
17533 test_245() {
17534         local flagname="multi_mod_rpcs"
17535         local connect_data_name="max_mod_rpcs"
17536         local out
17537
17538         # check if multiple modify RPCs flag is set
17539         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
17540                 grep "connect_flags:")
17541         echo "$out"
17542
17543         echo "$out" | grep -qw $flagname
17544         if [ $? -ne 0 ]; then
17545                 echo "connect flag $flagname is not set"
17546                 return
17547         fi
17548
17549         # check if multiple modify RPCs data is set
17550         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
17551         echo "$out"
17552
17553         echo "$out" | grep -qw $connect_data_name ||
17554                 error "import should have connect data $connect_data_name"
17555 }
17556 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
17557
17558 cleanup_247() {
17559         local submount=$1
17560
17561         trap 0
17562         umount_client $submount
17563         rmdir $submount
17564 }
17565
17566 test_247a() {
17567         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17568                 grep -q subtree ||
17569                 skip_env "Fileset feature is not supported"
17570
17571         local submount=${MOUNT}_$tdir
17572
17573         mkdir $MOUNT/$tdir
17574         mkdir -p $submount || error "mkdir $submount failed"
17575         FILESET="$FILESET/$tdir" mount_client $submount ||
17576                 error "mount $submount failed"
17577         trap "cleanup_247 $submount" EXIT
17578         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
17579         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
17580                 error "read $MOUNT/$tdir/$tfile failed"
17581         cleanup_247 $submount
17582 }
17583 run_test 247a "mount subdir as fileset"
17584
17585 test_247b() {
17586         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17587                 skip_env "Fileset feature is not supported"
17588
17589         local submount=${MOUNT}_$tdir
17590
17591         rm -rf $MOUNT/$tdir
17592         mkdir -p $submount || error "mkdir $submount failed"
17593         SKIP_FILESET=1
17594         FILESET="$FILESET/$tdir" mount_client $submount &&
17595                 error "mount $submount should fail"
17596         rmdir $submount
17597 }
17598 run_test 247b "mount subdir that dose not exist"
17599
17600 test_247c() {
17601         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17602                 skip_env "Fileset feature is not supported"
17603
17604         local submount=${MOUNT}_$tdir
17605
17606         mkdir -p $MOUNT/$tdir/dir1
17607         mkdir -p $submount || error "mkdir $submount failed"
17608         trap "cleanup_247 $submount" EXIT
17609         FILESET="$FILESET/$tdir" mount_client $submount ||
17610                 error "mount $submount failed"
17611         local fid=$($LFS path2fid $MOUNT/)
17612         $LFS fid2path $submount $fid && error "fid2path should fail"
17613         cleanup_247 $submount
17614 }
17615 run_test 247c "running fid2path outside root"
17616
17617 test_247d() {
17618         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17619                 skip "Fileset feature is not supported"
17620
17621         local submount=${MOUNT}_$tdir
17622
17623         mkdir -p $MOUNT/$tdir/dir1
17624         mkdir -p $submount || error "mkdir $submount failed"
17625         FILESET="$FILESET/$tdir" mount_client $submount ||
17626                 error "mount $submount failed"
17627         trap "cleanup_247 $submount" EXIT
17628         local fid=$($LFS path2fid $submount/dir1)
17629         $LFS fid2path $submount $fid || error "fid2path should succeed"
17630         cleanup_247 $submount
17631 }
17632 run_test 247d "running fid2path inside root"
17633
17634 # LU-8037
17635 test_247e() {
17636         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17637                 grep -q subtree ||
17638                 skip "Fileset feature is not supported"
17639
17640         local submount=${MOUNT}_$tdir
17641
17642         mkdir $MOUNT/$tdir
17643         mkdir -p $submount || error "mkdir $submount failed"
17644         FILESET="$FILESET/.." mount_client $submount &&
17645                 error "mount $submount should fail"
17646         rmdir $submount
17647 }
17648 run_test 247e "mount .. as fileset"
17649
17650 test_248a() {
17651         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
17652         [ -z "$fast_read_sav" ] && skip "no fast read support"
17653
17654         # create a large file for fast read verification
17655         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
17656
17657         # make sure the file is created correctly
17658         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
17659                 { rm -f $DIR/$tfile; skip "file creation error"; }
17660
17661         echo "Test 1: verify that fast read is 4 times faster on cache read"
17662
17663         # small read with fast read enabled
17664         $LCTL set_param -n llite.*.fast_read=1
17665         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17666                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17667                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17668         # small read with fast read disabled
17669         $LCTL set_param -n llite.*.fast_read=0
17670         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17671                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17672                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17673
17674         # verify that fast read is 4 times faster for cache read
17675         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
17676                 error_not_in_vm "fast read was not 4 times faster: " \
17677                            "$t_fast vs $t_slow"
17678
17679         echo "Test 2: verify the performance between big and small read"
17680         $LCTL set_param -n llite.*.fast_read=1
17681
17682         # 1k non-cache read
17683         cancel_lru_locks osc
17684         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17685                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17686                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17687
17688         # 1M non-cache read
17689         cancel_lru_locks osc
17690         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17691                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17692                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17693
17694         # verify that big IO is not 4 times faster than small IO
17695         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
17696                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
17697
17698         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
17699         rm -f $DIR/$tfile
17700 }
17701 run_test 248a "fast read verification"
17702
17703 test_248b() {
17704         # Default short_io_bytes=16384, try both smaller and larger sizes.
17705         # Lustre O_DIRECT read and write needs to be a multiple of PAGE_SIZE.
17706         # 6017024 = 2^12*13*113 = 47008*128 = 11752*512 = 4096*1469 = 53248*113
17707         echo "bs=53248 count=113 normal buffered write"
17708         dd if=/dev/urandom of=$TMP/$tfile.0 bs=53248 count=113 ||
17709                 error "dd of initial data file failed"
17710         stack_trap "rm -f $DIR/$tfile.[0-3] $TMP/$tfile.[0-3]" EXIT
17711
17712         echo "bs=47008 count=128 oflag=dsync normal write $tfile.0"
17713         dd if=$TMP/$tfile.0 of=$DIR/$tfile.0 bs=47008 count=128 oflag=dsync ||
17714                 error "dd with sync normal writes failed"
17715         cmp $TMP/$tfile.0 $DIR/$tfile.0 || error "compare $DIR/$tfile.0 failed"
17716
17717         echo "bs=11752 count=512 oflag=dsync small write $tfile.1"
17718         dd if=$TMP/$tfile.0 of=$DIR/$tfile.1 bs=11752 count=512 oflag=dsync ||
17719                 error "dd with sync small writes failed"
17720         cmp $TMP/$tfile.0 $DIR/$tfile.1 || error "compare $DIR/$tfile.1 failed"
17721
17722         cancel_lru_locks osc
17723
17724         # calculate the small O_DIRECT size and count for the client PAGE_SIZE
17725         local num=$((13 * 113 / (PAGE_SIZE / 4096)))
17726         echo "bs=$PAGE_SIZE count=$num iflag=direct small read $tfile.1"
17727         dd if=$DIR/$tfile.1 of=$TMP/$tfile.1 bs=$PAGE_SIZE count=$num \
17728                 iflag=direct || error "dd with O_DIRECT small read failed"
17729         # adjust bytes checked to handle larger PAGE_SIZE for ARM/PPC
17730         cmp --bytes=$((PAGE_SIZE * num)) $TMP/$tfile.0 $TMP/$tfile.1 ||
17731                 error "compare $TMP/$tfile.1 failed"
17732
17733         local save=$($LCTL get_param -n osc.*OST000*.short_io_bytes | head -n 1)
17734         stack_trap "$LCTL set_param osc.$FSNAME-*.short_io_bytes=$save" EXIT
17735
17736         # just to see what the maximum tunable value is, and test parsing
17737         echo "test invalid parameter 2MB"
17738         $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=2M &&
17739                 error "too-large short_io_bytes allowed"
17740         echo "test maximum parameter 512KB"
17741         # if we can set a larger short_io_bytes, run test regardless of version
17742         if ! $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=512K; then
17743                 # older clients may not allow setting it this large, that's OK
17744                 [ $CLIENT_VERSION -ge $(version_code 2.13.50) ] ||
17745                         skip "Need at least client version 2.13.50"
17746                 error "medium short_io_bytes failed"
17747         fi
17748         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
17749         size=$($LCTL get_param -n osc.$FSNAME-OST0000*.short_io_bytes)
17750
17751         echo "test large parameter 64KB"
17752         $LCTL set_param osc.$FSNAME-*.short_io_bytes=65536
17753         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
17754
17755         echo "bs=47008 count=128 oflag=dsync large write $tfile.2"
17756         dd if=$TMP/$tfile.0 of=$DIR/$tfile.2 bs=47008 count=128 oflag=dsync ||
17757                 error "dd with sync large writes failed"
17758         cmp $TMP/$tfile.0 $DIR/$tfile.2 || error "compare $DIR/$tfile.2 failed"
17759
17760         # calculate the large O_DIRECT size and count for the client PAGE_SIZE
17761         local size=$(((4096 * 13 + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE))
17762         num=$((113 * 4096 / PAGE_SIZE))
17763         echo "bs=$size count=$num oflag=direct large write $tfile.3"
17764         dd if=$TMP/$tfile.0 of=$DIR/$tfile.3 bs=$size count=$num oflag=direct ||
17765                 error "dd with O_DIRECT large writes failed"
17766         cmp --bytes=$((size * num)) $TMP/$tfile.0 $DIR/$tfile.3 ||
17767                 error "compare $DIR/$tfile.3 failed"
17768
17769         cancel_lru_locks osc
17770
17771         echo "bs=$size count=$num iflag=direct large read $tfile.2"
17772         dd if=$DIR/$tfile.2 of=$TMP/$tfile.2 bs=$size count=$num iflag=direct ||
17773                 error "dd with O_DIRECT large read failed"
17774         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.2 ||
17775                 error "compare $TMP/$tfile.2 failed"
17776
17777         echo "bs=$size count=$num iflag=direct large read $tfile.3"
17778         dd if=$DIR/$tfile.3 of=$TMP/$tfile.3 bs=$size count=$num iflag=direct ||
17779                 error "dd with O_DIRECT large read failed"
17780         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.3 ||
17781                 error "compare $TMP/$tfile.3 failed"
17782 }
17783 run_test 248b "test short_io read and write for both small and large sizes"
17784
17785 test_249() { # LU-7890
17786         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
17787                 skip "Need at least version 2.8.54"
17788
17789         rm -f $DIR/$tfile
17790         $LFS setstripe -c 1 $DIR/$tfile
17791         # Offset 2T == 4k * 512M
17792         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
17793                 error "dd to 2T offset failed"
17794 }
17795 run_test 249 "Write above 2T file size"
17796
17797 test_250() {
17798         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
17799          && skip "no 16TB file size limit on ZFS"
17800
17801         $LFS setstripe -c 1 $DIR/$tfile
17802         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
17803         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
17804         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
17805         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
17806                 conv=notrunc,fsync && error "append succeeded"
17807         return 0
17808 }
17809 run_test 250 "Write above 16T limit"
17810
17811 test_251() {
17812         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
17813
17814         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
17815         #Skip once - writing the first stripe will succeed
17816         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17817         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
17818                 error "short write happened"
17819
17820         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17821         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
17822                 error "short read happened"
17823
17824         rm -f $DIR/$tfile
17825 }
17826 run_test 251 "Handling short read and write correctly"
17827
17828 test_252() {
17829         remote_mds_nodsh && skip "remote MDS with nodsh"
17830         remote_ost_nodsh && skip "remote OST with nodsh"
17831         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
17832                 skip_env "ldiskfs only test"
17833         fi
17834
17835         local tgt
17836         local dev
17837         local out
17838         local uuid
17839         local num
17840         local gen
17841
17842         # check lr_reader on OST0000
17843         tgt=ost1
17844         dev=$(facet_device $tgt)
17845         out=$(do_facet $tgt $LR_READER $dev)
17846         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17847         echo "$out"
17848         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
17849         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
17850                 error "Invalid uuid returned by $LR_READER on target $tgt"
17851         echo -e "uuid returned by $LR_READER is '$uuid'\n"
17852
17853         # check lr_reader -c on MDT0000
17854         tgt=mds1
17855         dev=$(facet_device $tgt)
17856         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
17857                 skip "$LR_READER does not support additional options"
17858         fi
17859         out=$(do_facet $tgt $LR_READER -c $dev)
17860         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17861         echo "$out"
17862         num=$(echo "$out" | grep -c "mdtlov")
17863         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
17864                 error "Invalid number of mdtlov clients returned by $LR_READER"
17865         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
17866
17867         # check lr_reader -cr on MDT0000
17868         out=$(do_facet $tgt $LR_READER -cr $dev)
17869         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17870         echo "$out"
17871         echo "$out" | grep -q "^reply_data:$" ||
17872                 error "$LR_READER should have returned 'reply_data' section"
17873         num=$(echo "$out" | grep -c "client_generation")
17874         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
17875 }
17876 run_test 252 "check lr_reader tool"
17877
17878 test_253() {
17879         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17880         remote_mds_nodsh && skip "remote MDS with nodsh"
17881         remote_mgs_nodsh && skip "remote MGS with nodsh"
17882
17883         local ostidx=0
17884         local rc=0
17885         local ost_name=$(ostname_from_index $ostidx)
17886
17887         # on the mdt's osc
17888         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
17889         do_facet $SINGLEMDS $LCTL get_param -n \
17890                 osp.$mdtosc_proc1.reserved_mb_high ||
17891                 skip  "remote MDS does not support reserved_mb_high"
17892
17893         rm -rf $DIR/$tdir
17894         wait_mds_ost_sync
17895         wait_delete_completed
17896         mkdir $DIR/$tdir
17897
17898         pool_add $TESTNAME || error "Pool creation failed"
17899         pool_add_targets $TESTNAME 0 || error "Pool add targets failed"
17900
17901         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
17902                 error "Setstripe failed"
17903
17904         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M count=10
17905
17906         local wms=$(ost_watermarks_set_enospc $tfile $ostidx |
17907                     grep "watermarks")
17908         stack_trap "ost_watermarks_clear_enospc $tfile $ostidx $wms" EXIT
17909
17910         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17911                         osp.$mdtosc_proc1.prealloc_status)
17912         echo "prealloc_status $oa_status"
17913
17914         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1M count=1 &&
17915                 error "File creation should fail"
17916
17917         #object allocation was stopped, but we still able to append files
17918         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M seek=6 count=5 \
17919                 oflag=append || error "Append failed"
17920
17921         rm -f $DIR/$tdir/$tfile.0
17922
17923         # For this test, we want to delete the files we created to go out of
17924         # space but leave the watermark, so we remain nearly out of space
17925         ost_watermarks_enospc_delete_files $tfile $ostidx
17926
17927         wait_delete_completed
17928
17929         sleep_maxage
17930
17931         for i in $(seq 10 12); do
17932                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$i bs=1M count=1 \
17933                         2>/dev/null || error "File creation failed after rm"
17934         done
17935
17936         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17937                         osp.$mdtosc_proc1.prealloc_status)
17938         echo "prealloc_status $oa_status"
17939
17940         if (( oa_status != 0 )); then
17941                 error "Object allocation still disable after rm"
17942         fi
17943 }
17944 run_test 253 "Check object allocation limit"
17945
17946 test_254() {
17947         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17948         remote_mds_nodsh && skip "remote MDS with nodsh"
17949         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
17950                 skip "MDS does not support changelog_size"
17951
17952         local cl_user
17953         local MDT0=$(facet_svc $SINGLEMDS)
17954
17955         changelog_register || error "changelog_register failed"
17956
17957         changelog_clear 0 || error "changelog_clear failed"
17958
17959         local size1=$(do_facet $SINGLEMDS \
17960                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17961         echo "Changelog size $size1"
17962
17963         rm -rf $DIR/$tdir
17964         $LFS mkdir -i 0 $DIR/$tdir
17965         # change something
17966         mkdir -p $DIR/$tdir/pics/2008/zachy
17967         touch $DIR/$tdir/pics/2008/zachy/timestamp
17968         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
17969         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
17970         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
17971         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
17972         rm $DIR/$tdir/pics/desktop.jpg
17973
17974         local size2=$(do_facet $SINGLEMDS \
17975                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17976         echo "Changelog size after work $size2"
17977
17978         (( $size2 > $size1 )) ||
17979                 error "new Changelog size=$size2 less than old size=$size1"
17980 }
17981 run_test 254 "Check changelog size"
17982
17983 ladvise_no_type()
17984 {
17985         local type=$1
17986         local file=$2
17987
17988         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
17989                 awk -F: '{print $2}' | grep $type > /dev/null
17990         if [ $? -ne 0 ]; then
17991                 return 0
17992         fi
17993         return 1
17994 }
17995
17996 ladvise_no_ioctl()
17997 {
17998         local file=$1
17999
18000         lfs ladvise -a willread $file > /dev/null 2>&1
18001         if [ $? -eq 0 ]; then
18002                 return 1
18003         fi
18004
18005         lfs ladvise -a willread $file 2>&1 |
18006                 grep "Inappropriate ioctl for device" > /dev/null
18007         if [ $? -eq 0 ]; then
18008                 return 0
18009         fi
18010         return 1
18011 }
18012
18013 percent() {
18014         bc <<<"scale=2; ($1 - $2) * 100 / $2"
18015 }
18016
18017 # run a random read IO workload
18018 # usage: random_read_iops <filename> <filesize> <iosize>
18019 random_read_iops() {
18020         local file=$1
18021         local fsize=$2
18022         local iosize=${3:-4096}
18023
18024         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
18025                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
18026 }
18027
18028 drop_file_oss_cache() {
18029         local file="$1"
18030         local nodes="$2"
18031
18032         $LFS ladvise -a dontneed $file 2>/dev/null ||
18033                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
18034 }
18035
18036 ladvise_willread_performance()
18037 {
18038         local repeat=10
18039         local average_origin=0
18040         local average_cache=0
18041         local average_ladvise=0
18042
18043         for ((i = 1; i <= $repeat; i++)); do
18044                 echo "Iter $i/$repeat: reading without willread hint"
18045                 cancel_lru_locks osc
18046                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
18047                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
18048                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
18049                 average_origin=$(bc <<<"$average_origin + $speed_origin")
18050
18051                 cancel_lru_locks osc
18052                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
18053                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
18054                 average_cache=$(bc <<<"$average_cache + $speed_cache")
18055
18056                 cancel_lru_locks osc
18057                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
18058                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
18059                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
18060                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
18061                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
18062         done
18063         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
18064         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
18065         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
18066
18067         speedup_cache=$(percent $average_cache $average_origin)
18068         speedup_ladvise=$(percent $average_ladvise $average_origin)
18069
18070         echo "Average uncached read: $average_origin"
18071         echo "Average speedup with OSS cached read: " \
18072                 "$average_cache = +$speedup_cache%"
18073         echo "Average speedup with ladvise willread: " \
18074                 "$average_ladvise = +$speedup_ladvise%"
18075
18076         local lowest_speedup=20
18077         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
18078                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
18079                         "got $average_cache%. Skipping ladvise willread check."
18080                 return 0
18081         fi
18082
18083         # the test won't work on ZFS until it supports 'ladvise dontneed', but
18084         # it is still good to run until then to exercise 'ladvise willread'
18085         ! $LFS ladvise -a dontneed $DIR/$tfile &&
18086                 [ "$ost1_FSTYPE" = "zfs" ] &&
18087                 echo "osd-zfs does not support dontneed or drop_caches" &&
18088                 return 0
18089
18090         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
18091         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
18092                 error_not_in_vm "Speedup with willread is less than " \
18093                         "$lowest_speedup%, got $average_ladvise%"
18094 }
18095
18096 test_255a() {
18097         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
18098                 skip "lustre < 2.8.54 does not support ladvise "
18099         remote_ost_nodsh && skip "remote OST with nodsh"
18100
18101         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
18102
18103         ladvise_no_type willread $DIR/$tfile &&
18104                 skip "willread ladvise is not supported"
18105
18106         ladvise_no_ioctl $DIR/$tfile &&
18107                 skip "ladvise ioctl is not supported"
18108
18109         local size_mb=100
18110         local size=$((size_mb * 1048576))
18111         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
18112                 error "dd to $DIR/$tfile failed"
18113
18114         lfs ladvise -a willread $DIR/$tfile ||
18115                 error "Ladvise failed with no range argument"
18116
18117         lfs ladvise -a willread -s 0 $DIR/$tfile ||
18118                 error "Ladvise failed with no -l or -e argument"
18119
18120         lfs ladvise -a willread -e 1 $DIR/$tfile ||
18121                 error "Ladvise failed with only -e argument"
18122
18123         lfs ladvise -a willread -l 1 $DIR/$tfile ||
18124                 error "Ladvise failed with only -l argument"
18125
18126         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
18127                 error "End offset should not be smaller than start offset"
18128
18129         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
18130                 error "End offset should not be equal to start offset"
18131
18132         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
18133                 error "Ladvise failed with overflowing -s argument"
18134
18135         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
18136                 error "Ladvise failed with overflowing -e argument"
18137
18138         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
18139                 error "Ladvise failed with overflowing -l argument"
18140
18141         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
18142                 error "Ladvise succeeded with conflicting -l and -e arguments"
18143
18144         echo "Synchronous ladvise should wait"
18145         local delay=4
18146 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
18147         do_nodes $(comma_list $(osts_nodes)) \
18148                 $LCTL set_param fail_val=$delay fail_loc=0x237
18149
18150         local start_ts=$SECONDS
18151         lfs ladvise -a willread $DIR/$tfile ||
18152                 error "Ladvise failed with no range argument"
18153         local end_ts=$SECONDS
18154         local inteval_ts=$((end_ts - start_ts))
18155
18156         if [ $inteval_ts -lt $(($delay - 1)) ]; then
18157                 error "Synchronous advice didn't wait reply"
18158         fi
18159
18160         echo "Asynchronous ladvise shouldn't wait"
18161         local start_ts=$SECONDS
18162         lfs ladvise -a willread -b $DIR/$tfile ||
18163                 error "Ladvise failed with no range argument"
18164         local end_ts=$SECONDS
18165         local inteval_ts=$((end_ts - start_ts))
18166
18167         if [ $inteval_ts -gt $(($delay / 2)) ]; then
18168                 error "Asynchronous advice blocked"
18169         fi
18170
18171         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
18172         ladvise_willread_performance
18173 }
18174 run_test 255a "check 'lfs ladvise -a willread'"
18175
18176 facet_meminfo() {
18177         local facet=$1
18178         local info=$2
18179
18180         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
18181 }
18182
18183 test_255b() {
18184         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
18185                 skip "lustre < 2.8.54 does not support ladvise "
18186         remote_ost_nodsh && skip "remote OST with nodsh"
18187
18188         lfs setstripe -c 1 -i 0 $DIR/$tfile
18189
18190         ladvise_no_type dontneed $DIR/$tfile &&
18191                 skip "dontneed ladvise is not supported"
18192
18193         ladvise_no_ioctl $DIR/$tfile &&
18194                 skip "ladvise ioctl is not supported"
18195
18196         ! $LFS ladvise -a dontneed $DIR/$tfile &&
18197                 [ "$ost1_FSTYPE" = "zfs" ] &&
18198                 skip "zfs-osd does not support 'ladvise dontneed'"
18199
18200         local size_mb=100
18201         local size=$((size_mb * 1048576))
18202         # In order to prevent disturbance of other processes, only check 3/4
18203         # of the memory usage
18204         local kibibytes=$((size_mb * 1024 * 3 / 4))
18205
18206         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
18207                 error "dd to $DIR/$tfile failed"
18208
18209         #force write to complete before dropping OST cache & checking memory
18210         sync
18211
18212         local total=$(facet_meminfo ost1 MemTotal)
18213         echo "Total memory: $total KiB"
18214
18215         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
18216         local before_read=$(facet_meminfo ost1 Cached)
18217         echo "Cache used before read: $before_read KiB"
18218
18219         lfs ladvise -a willread $DIR/$tfile ||
18220                 error "Ladvise willread failed"
18221         local after_read=$(facet_meminfo ost1 Cached)
18222         echo "Cache used after read: $after_read KiB"
18223
18224         lfs ladvise -a dontneed $DIR/$tfile ||
18225                 error "Ladvise dontneed again failed"
18226         local no_read=$(facet_meminfo ost1 Cached)
18227         echo "Cache used after dontneed ladvise: $no_read KiB"
18228
18229         if [ $total -lt $((before_read + kibibytes)) ]; then
18230                 echo "Memory is too small, abort checking"
18231                 return 0
18232         fi
18233
18234         if [ $((before_read + kibibytes)) -gt $after_read ]; then
18235                 error "Ladvise willread should use more memory" \
18236                         "than $kibibytes KiB"
18237         fi
18238
18239         if [ $((no_read + kibibytes)) -gt $after_read ]; then
18240                 error "Ladvise dontneed should release more memory" \
18241                         "than $kibibytes KiB"
18242         fi
18243 }
18244 run_test 255b "check 'lfs ladvise -a dontneed'"
18245
18246 test_255c() {
18247         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
18248                 skip "lustre < 2.10.50 does not support lockahead"
18249
18250         local count
18251         local new_count
18252         local difference
18253         local i
18254         local rc
18255
18256         test_mkdir -p $DIR/$tdir
18257         $LFS setstripe -i 0 -c 1 $DIR/$tdir
18258
18259         #test 10 returns only success/failure
18260         i=10
18261         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18262         rc=$?
18263         if [ $rc -eq 255 ]; then
18264                 error "Ladvise test${i} failed, ${rc}"
18265         fi
18266
18267         #test 11 counts lock enqueue requests, all others count new locks
18268         i=11
18269         count=$(do_facet ost1 \
18270                 $LCTL get_param -n ost.OSS.ost.stats)
18271         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
18272
18273         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18274         rc=$?
18275         if [ $rc -eq 255 ]; then
18276                 error "Ladvise test${i} failed, ${rc}"
18277         fi
18278
18279         new_count=$(do_facet ost1 \
18280                 $LCTL get_param -n ost.OSS.ost.stats)
18281         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
18282                    awk '{ print $2 }')
18283
18284         difference="$((new_count - count))"
18285         if [ $difference -ne $rc ]; then
18286                 error "Ladvise test${i}, bad enqueue count, returned " \
18287                       "${rc}, actual ${difference}"
18288         fi
18289
18290         for i in $(seq 12 21); do
18291                 # If we do not do this, we run the risk of having too many
18292                 # locks and starting lock cancellation while we are checking
18293                 # lock counts.
18294                 cancel_lru_locks osc
18295
18296                 count=$($LCTL get_param -n \
18297                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
18298
18299                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
18300                 rc=$?
18301                 if [ $rc -eq 255 ]; then
18302                         error "Ladvise test ${i} failed, ${rc}"
18303                 fi
18304
18305                 new_count=$($LCTL get_param -n \
18306                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
18307                 difference="$((new_count - count))"
18308
18309                 # Test 15 output is divided by 100 to map down to valid return
18310                 if [ $i -eq 15 ]; then
18311                         rc="$((rc * 100))"
18312                 fi
18313
18314                 if [ $difference -ne $rc ]; then
18315                         error "Ladvise test ${i}, bad lock count, returned " \
18316                               "${rc}, actual ${difference}"
18317                 fi
18318         done
18319
18320         #test 22 returns only success/failure
18321         i=22
18322         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18323         rc=$?
18324         if [ $rc -eq 255 ]; then
18325                 error "Ladvise test${i} failed, ${rc}"
18326         fi
18327 }
18328 run_test 255c "suite of ladvise lockahead tests"
18329
18330 test_256() {
18331         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18332         remote_mds_nodsh && skip "remote MDS with nodsh"
18333         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18334         changelog_users $SINGLEMDS | grep "^cl" &&
18335                 skip "active changelog user"
18336
18337         local cl_user
18338         local cat_sl
18339         local mdt_dev
18340
18341         mdt_dev=$(mdsdevname 1)
18342         echo $mdt_dev
18343
18344         changelog_register || error "changelog_register failed"
18345
18346         rm -rf $DIR/$tdir
18347         mkdir -p $DIR/$tdir
18348
18349         changelog_clear 0 || error "changelog_clear failed"
18350
18351         # change something
18352         touch $DIR/$tdir/{1..10}
18353
18354         # stop the MDT
18355         stop $SINGLEMDS || error "Fail to stop MDT"
18356
18357         # remount the MDT
18358
18359         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
18360
18361         #after mount new plainllog is used
18362         touch $DIR/$tdir/{11..19}
18363         local tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
18364         stack_trap "rm -f $tmpfile"
18365         cat_sl=$(do_facet $SINGLEMDS "sync; \
18366                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18367                  llog_reader $tmpfile | grep -c type=1064553b")
18368         do_facet $SINGLEMDS llog_reader $tmpfile
18369
18370         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
18371
18372         changelog_clear 0 || error "changelog_clear failed"
18373
18374         cat_sl=$(do_facet $SINGLEMDS "sync; \
18375                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18376                  llog_reader $tmpfile | grep -c type=1064553b")
18377
18378         if (( cat_sl == 2 )); then
18379                 error "Empty plain llog was not deleted from changelog catalog"
18380         elif (( cat_sl != 1 )); then
18381                 error "Active plain llog shouldn't be deleted from catalog"
18382         fi
18383 }
18384 run_test 256 "Check llog delete for empty and not full state"
18385
18386 test_257() {
18387         remote_mds_nodsh && skip "remote MDS with nodsh"
18388         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
18389                 skip "Need MDS version at least 2.8.55"
18390
18391         test_mkdir $DIR/$tdir
18392
18393         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
18394                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
18395         stat $DIR/$tdir
18396
18397 #define OBD_FAIL_MDS_XATTR_REP                  0x161
18398         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18399         local facet=mds$((mdtidx + 1))
18400         set_nodes_failloc $(facet_active_host $facet) 0x80000161
18401         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
18402
18403         stop $facet || error "stop MDS failed"
18404         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
18405                 error "start MDS fail"
18406         wait_recovery_complete $facet
18407 }
18408 run_test 257 "xattr locks are not lost"
18409
18410 # Verify we take the i_mutex when security requires it
18411 test_258a() {
18412 #define OBD_FAIL_IMUTEX_SEC 0x141c
18413         $LCTL set_param fail_loc=0x141c
18414         touch $DIR/$tfile
18415         chmod u+s $DIR/$tfile
18416         chmod a+rwx $DIR/$tfile
18417         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18418         RC=$?
18419         if [ $RC -ne 0 ]; then
18420                 error "error, failed to take i_mutex, rc=$?"
18421         fi
18422         rm -f $DIR/$tfile
18423 }
18424 run_test 258a "verify i_mutex security behavior when suid attributes is set"
18425
18426 # Verify we do NOT take the i_mutex in the normal case
18427 test_258b() {
18428 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
18429         $LCTL set_param fail_loc=0x141d
18430         touch $DIR/$tfile
18431         chmod a+rwx $DIR
18432         chmod a+rw $DIR/$tfile
18433         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18434         RC=$?
18435         if [ $RC -ne 0 ]; then
18436                 error "error, took i_mutex unnecessarily, rc=$?"
18437         fi
18438         rm -f $DIR/$tfile
18439
18440 }
18441 run_test 258b "verify i_mutex security behavior"
18442
18443 test_259() {
18444         local file=$DIR/$tfile
18445         local before
18446         local after
18447
18448         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18449
18450         stack_trap "rm -f $file" EXIT
18451
18452         wait_delete_completed
18453         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18454         echo "before: $before"
18455
18456         $LFS setstripe -i 0 -c 1 $file
18457         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
18458         sync_all_data
18459         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18460         echo "after write: $after"
18461
18462 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
18463         do_facet ost1 $LCTL set_param fail_loc=0x2301
18464         $TRUNCATE $file 0
18465         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18466         echo "after truncate: $after"
18467
18468         stop ost1
18469         do_facet ost1 $LCTL set_param fail_loc=0
18470         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
18471         sleep 2
18472         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18473         echo "after restart: $after"
18474         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
18475                 error "missing truncate?"
18476
18477         return 0
18478 }
18479 run_test 259 "crash at delayed truncate"
18480
18481 test_260() {
18482 #define OBD_FAIL_MDC_CLOSE               0x806
18483         $LCTL set_param fail_loc=0x80000806
18484         touch $DIR/$tfile
18485
18486 }
18487 run_test 260 "Check mdc_close fail"
18488
18489 ### Data-on-MDT sanity tests ###
18490 test_270a() {
18491         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18492                 skip "Need MDS version at least 2.10.55 for DoM"
18493
18494         # create DoM file
18495         local dom=$DIR/$tdir/dom_file
18496         local tmp=$DIR/$tdir/tmp_file
18497
18498         mkdir -p $DIR/$tdir
18499
18500         # basic checks for DoM component creation
18501         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
18502                 error "Can set MDT layout to non-first entry"
18503
18504         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
18505                 error "Can define multiple entries as MDT layout"
18506
18507         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
18508
18509         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
18510         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
18511         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
18512
18513         local mdtidx=$($LFS getstripe -m $dom)
18514         local mdtname=MDT$(printf %04x $mdtidx)
18515         local facet=mds$((mdtidx + 1))
18516         local space_check=1
18517
18518         # Skip free space checks with ZFS
18519         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
18520
18521         # write
18522         sync
18523         local size_tmp=$((65536 * 3))
18524         local mdtfree1=$(do_facet $facet \
18525                          lctl get_param -n osd*.*$mdtname.kbytesfree)
18526
18527         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18528         # check also direct IO along write
18529         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
18530         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18531         sync
18532         cmp $tmp $dom || error "file data is different"
18533         [ $(stat -c%s $dom) == $size_tmp ] ||
18534                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18535         if [ $space_check == 1 ]; then
18536                 local mdtfree2=$(do_facet $facet \
18537                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
18538
18539                 # increase in usage from by $size_tmp
18540                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18541                         error "MDT free space wrong after write: " \
18542                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18543         fi
18544
18545         # truncate
18546         local size_dom=10000
18547
18548         $TRUNCATE $dom $size_dom
18549         [ $(stat -c%s $dom) == $size_dom ] ||
18550                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
18551         if [ $space_check == 1 ]; then
18552                 mdtfree1=$(do_facet $facet \
18553                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18554                 # decrease in usage from $size_tmp to new $size_dom
18555                 [ $(($mdtfree1 - $mdtfree2)) -ge \
18556                   $(((size_tmp - size_dom) / 1024)) ] ||
18557                         error "MDT free space is wrong after truncate: " \
18558                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
18559         fi
18560
18561         # append
18562         cat $tmp >> $dom
18563         sync
18564         size_dom=$((size_dom + size_tmp))
18565         [ $(stat -c%s $dom) == $size_dom ] ||
18566                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
18567         if [ $space_check == 1 ]; then
18568                 mdtfree2=$(do_facet $facet \
18569                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18570                 # increase in usage by $size_tmp from previous
18571                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18572                         error "MDT free space is wrong after append: " \
18573                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18574         fi
18575
18576         # delete
18577         rm $dom
18578         if [ $space_check == 1 ]; then
18579                 mdtfree1=$(do_facet $facet \
18580                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18581                 # decrease in usage by $size_dom from previous
18582                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
18583                         error "MDT free space is wrong after removal: " \
18584                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
18585         fi
18586
18587         # combined striping
18588         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
18589                 error "Can't create DoM + OST striping"
18590
18591         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
18592         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18593         # check also direct IO along write
18594         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18595         sync
18596         cmp $tmp $dom || error "file data is different"
18597         [ $(stat -c%s $dom) == $size_tmp ] ||
18598                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18599         rm $dom $tmp
18600
18601         return 0
18602 }
18603 run_test 270a "DoM: basic functionality tests"
18604
18605 test_270b() {
18606         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18607                 skip "Need MDS version at least 2.10.55"
18608
18609         local dom=$DIR/$tdir/dom_file
18610         local max_size=1048576
18611
18612         mkdir -p $DIR/$tdir
18613         $LFS setstripe -E $max_size -L mdt $dom
18614
18615         # truncate over the limit
18616         $TRUNCATE $dom $(($max_size + 1)) &&
18617                 error "successful truncate over the maximum size"
18618         # write over the limit
18619         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
18620                 error "successful write over the maximum size"
18621         # append over the limit
18622         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
18623         echo "12345" >> $dom && error "successful append over the maximum size"
18624         rm $dom
18625
18626         return 0
18627 }
18628 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
18629
18630 test_270c() {
18631         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18632                 skip "Need MDS version at least 2.10.55"
18633
18634         mkdir -p $DIR/$tdir
18635         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18636
18637         # check files inherit DoM EA
18638         touch $DIR/$tdir/first
18639         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
18640                 error "bad pattern"
18641         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
18642                 error "bad stripe count"
18643         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
18644                 error "bad stripe size"
18645
18646         # check directory inherits DoM EA and uses it as default
18647         mkdir $DIR/$tdir/subdir
18648         touch $DIR/$tdir/subdir/second
18649         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
18650                 error "bad pattern in sub-directory"
18651         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
18652                 error "bad stripe count in sub-directory"
18653         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
18654                 error "bad stripe size in sub-directory"
18655         return 0
18656 }
18657 run_test 270c "DoM: DoM EA inheritance tests"
18658
18659 test_270d() {
18660         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18661                 skip "Need MDS version at least 2.10.55"
18662
18663         mkdir -p $DIR/$tdir
18664         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18665
18666         # inherit default DoM striping
18667         mkdir $DIR/$tdir/subdir
18668         touch $DIR/$tdir/subdir/f1
18669
18670         # change default directory striping
18671         $LFS setstripe -c 1 $DIR/$tdir/subdir
18672         touch $DIR/$tdir/subdir/f2
18673         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
18674                 error "wrong default striping in file 2"
18675         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
18676                 error "bad pattern in file 2"
18677         return 0
18678 }
18679 run_test 270d "DoM: change striping from DoM to RAID0"
18680
18681 test_270e() {
18682         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18683                 skip "Need MDS version at least 2.10.55"
18684
18685         mkdir -p $DIR/$tdir/dom
18686         mkdir -p $DIR/$tdir/norm
18687         DOMFILES=20
18688         NORMFILES=10
18689         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
18690         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
18691
18692         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
18693         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
18694
18695         # find DoM files by layout
18696         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
18697         [ $NUM -eq  $DOMFILES ] ||
18698                 error "lfs find -L: found $NUM, expected $DOMFILES"
18699         echo "Test 1: lfs find 20 DOM files by layout: OK"
18700
18701         # there should be 1 dir with default DOM striping
18702         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
18703         [ $NUM -eq  1 ] ||
18704                 error "lfs find -L: found $NUM, expected 1 dir"
18705         echo "Test 2: lfs find 1 DOM dir by layout: OK"
18706
18707         # find DoM files by stripe size
18708         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
18709         [ $NUM -eq  $DOMFILES ] ||
18710                 error "lfs find -S: found $NUM, expected $DOMFILES"
18711         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
18712
18713         # find files by stripe offset except DoM files
18714         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
18715         [ $NUM -eq  $NORMFILES ] ||
18716                 error "lfs find -i: found $NUM, expected $NORMFILES"
18717         echo "Test 5: lfs find no DOM files by stripe index: OK"
18718         return 0
18719 }
18720 run_test 270e "DoM: lfs find with DoM files test"
18721
18722 test_270f() {
18723         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18724                 skip "Need MDS version at least 2.10.55"
18725
18726         local mdtname=${FSNAME}-MDT0000-mdtlov
18727         local dom=$DIR/$tdir/dom_file
18728         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
18729                                                 lod.$mdtname.dom_stripesize)
18730         local dom_limit=131072
18731
18732         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
18733         local dom_current=$(do_facet mds1 $LCTL get_param -n \
18734                                                 lod.$mdtname.dom_stripesize)
18735         [ ${dom_limit} -eq ${dom_current} ] ||
18736                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
18737
18738         $LFS mkdir -i 0 -c 1 $DIR/$tdir
18739         $LFS setstripe -d $DIR/$tdir
18740         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
18741                 error "Can't set directory default striping"
18742
18743         # exceed maximum stripe size
18744         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18745                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
18746         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
18747                 error "Able to create DoM component size more than LOD limit"
18748
18749         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
18750         dom_current=$(do_facet mds1 $LCTL get_param -n \
18751                                                 lod.$mdtname.dom_stripesize)
18752         [ 0 -eq ${dom_current} ] ||
18753                 error "Can't set zero DoM stripe limit"
18754         rm $dom
18755
18756         # attempt to create DoM file on server with disabled DoM should
18757         # remove DoM entry from layout and be succeed
18758         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
18759                 error "Can't create DoM file (DoM is disabled)"
18760         [ $($LFS getstripe -L $dom) == "mdt" ] &&
18761                 error "File has DoM component while DoM is disabled"
18762         rm $dom
18763
18764         # attempt to create DoM file with only DoM stripe should return error
18765         $LFS setstripe -E $dom_limit -L mdt $dom &&
18766                 error "Able to create DoM-only file while DoM is disabled"
18767
18768         # too low values to be aligned with smallest stripe size 64K
18769         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
18770         dom_current=$(do_facet mds1 $LCTL get_param -n \
18771                                                 lod.$mdtname.dom_stripesize)
18772         [ 30000 -eq ${dom_current} ] &&
18773                 error "Can set too small DoM stripe limit"
18774
18775         # 64K is a minimal stripe size in Lustre, expect limit of that size
18776         [ 65536 -eq ${dom_current} ] ||
18777                 error "Limit is not set to 64K but ${dom_current}"
18778
18779         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
18780         dom_current=$(do_facet mds1 $LCTL get_param -n \
18781                                                 lod.$mdtname.dom_stripesize)
18782         echo $dom_current
18783         [ 2147483648 -eq ${dom_current} ] &&
18784                 error "Can set too large DoM stripe limit"
18785
18786         do_facet mds1 $LCTL set_param -n \
18787                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
18788         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18789                 error "Can't create DoM component size after limit change"
18790         do_facet mds1 $LCTL set_param -n \
18791                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
18792         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
18793                 error "Can't create DoM file after limit decrease"
18794         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
18795                 error "Can create big DoM component after limit decrease"
18796         touch ${dom}_def ||
18797                 error "Can't create file with old default layout"
18798
18799         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
18800         return 0
18801 }
18802 run_test 270f "DoM: maximum DoM stripe size checks"
18803
18804 test_271a() {
18805         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18806                 skip "Need MDS version at least 2.10.55"
18807
18808         local dom=$DIR/$tdir/dom
18809
18810         mkdir -p $DIR/$tdir
18811
18812         $LFS setstripe -E 1024K -L mdt $dom
18813
18814         lctl set_param -n mdc.*.stats=clear
18815         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18816         cat $dom > /dev/null
18817         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
18818         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
18819         ls $dom
18820         rm -f $dom
18821 }
18822 run_test 271a "DoM: data is cached for read after write"
18823
18824 test_271b() {
18825         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18826                 skip "Need MDS version at least 2.10.55"
18827
18828         local dom=$DIR/$tdir/dom
18829
18830         mkdir -p $DIR/$tdir
18831
18832         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18833
18834         lctl set_param -n mdc.*.stats=clear
18835         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18836         cancel_lru_locks mdc
18837         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
18838         # second stat to check size is cached on client
18839         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
18840         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18841         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
18842         rm -f $dom
18843 }
18844 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
18845
18846 test_271ba() {
18847         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18848                 skip "Need MDS version at least 2.10.55"
18849
18850         local dom=$DIR/$tdir/dom
18851
18852         mkdir -p $DIR/$tdir
18853
18854         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18855
18856         lctl set_param -n mdc.*.stats=clear
18857         lctl set_param -n osc.*.stats=clear
18858         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
18859         cancel_lru_locks mdc
18860         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18861         # second stat to check size is cached on client
18862         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18863         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18864         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
18865         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
18866         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
18867         rm -f $dom
18868 }
18869 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
18870
18871
18872 get_mdc_stats() {
18873         local mdtidx=$1
18874         local param=$2
18875         local mdt=MDT$(printf %04x $mdtidx)
18876
18877         if [ -z $param ]; then
18878                 lctl get_param -n mdc.*$mdt*.stats
18879         else
18880                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
18881         fi
18882 }
18883
18884 test_271c() {
18885         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18886                 skip "Need MDS version at least 2.10.55"
18887
18888         local dom=$DIR/$tdir/dom
18889
18890         mkdir -p $DIR/$tdir
18891
18892         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18893
18894         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18895         local facet=mds$((mdtidx + 1))
18896
18897         cancel_lru_locks mdc
18898         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
18899         createmany -o $dom 1000
18900         lctl set_param -n mdc.*.stats=clear
18901         smalliomany -w $dom 1000 200
18902         get_mdc_stats $mdtidx
18903         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18904         # Each file has 1 open, 1 IO enqueues, total 2000
18905         # but now we have also +1 getxattr for security.capability, total 3000
18906         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
18907         unlinkmany $dom 1000
18908
18909         cancel_lru_locks mdc
18910         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
18911         createmany -o $dom 1000
18912         lctl set_param -n mdc.*.stats=clear
18913         smalliomany -w $dom 1000 200
18914         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18915         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
18916         # for OPEN and IO lock.
18917         [ $((enq - enq_2)) -ge 1000 ] ||
18918                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
18919         unlinkmany $dom 1000
18920         return 0
18921 }
18922 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
18923
18924 cleanup_271def_tests() {
18925         trap 0
18926         rm -f $1
18927 }
18928
18929 test_271d() {
18930         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
18931                 skip "Need MDS version at least 2.10.57"
18932
18933         local dom=$DIR/$tdir/dom
18934         local tmp=$TMP/$tfile
18935         trap "cleanup_271def_tests $tmp" EXIT
18936
18937         mkdir -p $DIR/$tdir
18938
18939         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18940
18941         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
18942
18943         cancel_lru_locks mdc
18944         dd if=/dev/urandom of=$tmp bs=1000 count=1
18945         dd if=$tmp of=$dom bs=1000 count=1
18946         cancel_lru_locks mdc
18947
18948         cat /etc/hosts >> $tmp
18949         lctl set_param -n mdc.*.stats=clear
18950
18951         # append data to the same file it should update local page
18952         echo "Append to the same page"
18953         cat /etc/hosts >> $dom
18954         local num=$(get_mdc_stats $mdtidx ost_read)
18955         local ra=$(get_mdc_stats $mdtidx req_active)
18956         local rw=$(get_mdc_stats $mdtidx req_waittime)
18957
18958         [ -z $num ] || error "$num READ RPC occured"
18959         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18960         echo "... DONE"
18961
18962         # compare content
18963         cmp $tmp $dom || error "file miscompare"
18964
18965         cancel_lru_locks mdc
18966         lctl set_param -n mdc.*.stats=clear
18967
18968         echo "Open and read file"
18969         cat $dom > /dev/null
18970         local num=$(get_mdc_stats $mdtidx ost_read)
18971         local ra=$(get_mdc_stats $mdtidx req_active)
18972         local rw=$(get_mdc_stats $mdtidx req_waittime)
18973
18974         [ -z $num ] || error "$num READ RPC occured"
18975         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18976         echo "... DONE"
18977
18978         # compare content
18979         cmp $tmp $dom || error "file miscompare"
18980
18981         return 0
18982 }
18983 run_test 271d "DoM: read on open (1K file in reply buffer)"
18984
18985 test_271f() {
18986         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
18987                 skip "Need MDS version at least 2.10.57"
18988
18989         local dom=$DIR/$tdir/dom
18990         local tmp=$TMP/$tfile
18991         trap "cleanup_271def_tests $tmp" EXIT
18992
18993         mkdir -p $DIR/$tdir
18994
18995         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18996
18997         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
18998
18999         cancel_lru_locks mdc
19000         dd if=/dev/urandom of=$tmp bs=265000 count=1
19001         dd if=$tmp of=$dom bs=265000 count=1
19002         cancel_lru_locks mdc
19003         cat /etc/hosts >> $tmp
19004         lctl set_param -n mdc.*.stats=clear
19005
19006         echo "Append to the same page"
19007         cat /etc/hosts >> $dom
19008         local num=$(get_mdc_stats $mdtidx ost_read)
19009         local ra=$(get_mdc_stats $mdtidx req_active)
19010         local rw=$(get_mdc_stats $mdtidx req_waittime)
19011
19012         [ -z $num ] || error "$num READ RPC occured"
19013         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19014         echo "... DONE"
19015
19016         # compare content
19017         cmp $tmp $dom || error "file miscompare"
19018
19019         cancel_lru_locks mdc
19020         lctl set_param -n mdc.*.stats=clear
19021
19022         echo "Open and read file"
19023         cat $dom > /dev/null
19024         local num=$(get_mdc_stats $mdtidx ost_read)
19025         local ra=$(get_mdc_stats $mdtidx req_active)
19026         local rw=$(get_mdc_stats $mdtidx req_waittime)
19027
19028         [ -z $num ] && num=0
19029         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
19030         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19031         echo "... DONE"
19032
19033         # compare content
19034         cmp $tmp $dom || error "file miscompare"
19035
19036         return 0
19037 }
19038 run_test 271f "DoM: read on open (200K file and read tail)"
19039
19040 test_271g() {
19041         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
19042                 skip "Skipping due to old client or server version"
19043
19044         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
19045         # to get layout
19046         $CHECKSTAT -t file $DIR1/$tfile
19047
19048         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
19049         MULTIOP_PID=$!
19050         sleep 1
19051         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
19052         $LCTL set_param fail_loc=0x80000314
19053         rm $DIR1/$tfile || error "Unlink fails"
19054         RC=$?
19055         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
19056         [ $RC -eq 0 ] || error "Failed write to stale object"
19057 }
19058 run_test 271g "Discard DoM data vs client flush race"
19059
19060 test_272a() {
19061         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19062                 skip "Need MDS version at least 2.11.50"
19063
19064         local dom=$DIR/$tdir/dom
19065         mkdir -p $DIR/$tdir
19066
19067         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
19068         dd if=/dev/urandom of=$dom bs=512K count=1 ||
19069                 error "failed to write data into $dom"
19070         local old_md5=$(md5sum $dom)
19071
19072         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
19073                 error "failed to migrate to the same DoM component"
19074
19075         local new_md5=$(md5sum $dom)
19076
19077         [ "$old_md5" == "$new_md5" ] ||
19078                 error "md5sum differ: $old_md5, $new_md5"
19079
19080         [ $($LFS getstripe -c $dom) -eq 2 ] ||
19081                 error "bad final stripe count: $($LFS getstripe -c $dom) != 2"
19082 }
19083 run_test 272a "DoM migration: new layout with the same DOM component"
19084
19085 test_272b() {
19086         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19087                 skip "Need MDS version at least 2.11.50"
19088
19089         local dom=$DIR/$tdir/dom
19090         mkdir -p $DIR/$tdir
19091         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19092
19093         local mdtidx=$($LFS getstripe -m $dom)
19094         local mdtname=MDT$(printf %04x $mdtidx)
19095         local facet=mds$((mdtidx + 1))
19096
19097         local mdtfree1=$(do_facet $facet \
19098                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19099         dd if=/dev/urandom of=$dom bs=2M count=1 ||
19100                 error "failed to write data into $dom"
19101         local old_md5=$(md5sum $dom)
19102         cancel_lru_locks mdc
19103         local mdtfree1=$(do_facet $facet \
19104                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19105
19106         $LFS migrate -c2 $dom ||
19107                 error "failed to migrate to the new composite layout"
19108         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19109                 error "MDT stripe was not removed"
19110
19111         cancel_lru_locks mdc
19112         local new_md5=$(md5sum $dom)
19113         [ "$old_md5" == "$new_md5" ] ||
19114                 error "$old_md5 != $new_md5"
19115
19116         # Skip free space checks with ZFS
19117         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19118                 local mdtfree2=$(do_facet $facet \
19119                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19120                 [ $mdtfree2 -gt $mdtfree1 ] ||
19121                         error "MDT space is not freed after migration"
19122         fi
19123         return 0
19124 }
19125 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
19126
19127 test_272c() {
19128         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19129                 skip "Need MDS version at least 2.11.50"
19130
19131         local dom=$DIR/$tdir/$tfile
19132         mkdir -p $DIR/$tdir
19133         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19134
19135         local mdtidx=$($LFS getstripe -m $dom)
19136         local mdtname=MDT$(printf %04x $mdtidx)
19137         local facet=mds$((mdtidx + 1))
19138
19139         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
19140                 error "failed to write data into $dom"
19141         local old_md5=$(md5sum $dom)
19142         cancel_lru_locks mdc
19143         local mdtfree1=$(do_facet $facet \
19144                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19145
19146         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
19147                 error "failed to migrate to the new composite layout"
19148         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
19149                 error "MDT stripe was not removed"
19150
19151         cancel_lru_locks mdc
19152         local new_md5=$(md5sum $dom)
19153         [ "$old_md5" == "$new_md5" ] ||
19154                 error "$old_md5 != $new_md5"
19155
19156         # Skip free space checks with ZFS
19157         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19158                 local mdtfree2=$(do_facet $facet \
19159                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19160                 [ $mdtfree2 -gt $mdtfree1 ] ||
19161                         error "MDS space is not freed after migration"
19162         fi
19163         return 0
19164 }
19165 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
19166
19167 test_272d() {
19168         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19169                 skip "Need MDS version at least 2.12.55"
19170
19171         local dom=$DIR/$tdir/$tfile
19172         mkdir -p $DIR/$tdir
19173         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19174
19175         local mdtidx=$($LFS getstripe -m $dom)
19176         local mdtname=MDT$(printf %04x $mdtidx)
19177         local facet=mds$((mdtidx + 1))
19178
19179         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
19180                 error "failed to write data into $dom"
19181         local old_md5=$(md5sum $dom)
19182         cancel_lru_locks mdc
19183         local mdtfree1=$(do_facet $facet \
19184                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19185
19186         $LFS mirror extend -N -E 2M -c1 -E -1 -c2 $dom ||
19187                 error "failed mirroring to the new composite layout"
19188         $LFS mirror resync $dom ||
19189                 error "failed mirror resync"
19190         $LFS mirror split --mirror-id 1 -d $dom ||
19191                 error "failed mirror split"
19192
19193         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19194                 error "MDT stripe was not removed"
19195
19196         cancel_lru_locks mdc
19197         local new_md5=$(md5sum $dom)
19198         [ "$old_md5" == "$new_md5" ] ||
19199                 error "$old_md5 != $new_md5"
19200
19201         # Skip free space checks with ZFS
19202         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19203                 local mdtfree2=$(do_facet $facet \
19204                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19205                 [ $mdtfree2 -gt $mdtfree1 ] ||
19206                         error "MDS space is not freed after DOM mirror deletion"
19207         fi
19208         return 0
19209 }
19210 run_test 272d "DoM mirroring: OST-striped mirror to DOM file"
19211
19212 test_272e() {
19213         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19214                 skip "Need MDS version at least 2.12.55"
19215
19216         local dom=$DIR/$tdir/$tfile
19217         mkdir -p $DIR/$tdir
19218         $LFS setstripe -c 2 $dom
19219
19220         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
19221                 error "failed to write data into $dom"
19222         local old_md5=$(md5sum $dom)
19223         cancel_lru_locks mdc
19224
19225         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 $dom ||
19226                 error "failed mirroring to the DOM layout"
19227         $LFS mirror resync $dom ||
19228                 error "failed mirror resync"
19229         $LFS mirror split --mirror-id 1 -d $dom ||
19230                 error "failed mirror split"
19231
19232         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19233                 error "MDT stripe was not removed"
19234
19235         cancel_lru_locks mdc
19236         local new_md5=$(md5sum $dom)
19237         [ "$old_md5" == "$new_md5" ] ||
19238                 error "$old_md5 != $new_md5"
19239
19240         return 0
19241 }
19242 run_test 272e "DoM mirroring: DOM mirror to the OST-striped file"
19243
19244 test_272f() {
19245         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19246                 skip "Need MDS version at least 2.12.55"
19247
19248         local dom=$DIR/$tdir/$tfile
19249         mkdir -p $DIR/$tdir
19250         $LFS setstripe -c 2 $dom
19251
19252         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
19253                 error "failed to write data into $dom"
19254         local old_md5=$(md5sum $dom)
19255         cancel_lru_locks mdc
19256
19257         $LFS migrate -E 1M -L mdt -E eof -c2 -v $dom ||
19258                 error "failed migrating to the DOM file"
19259
19260         cancel_lru_locks mdc
19261         local new_md5=$(md5sum $dom)
19262         [ "$old_md5" != "$new_md5" ] &&
19263                 error "$old_md5 != $new_md5"
19264
19265         return 0
19266 }
19267 run_test 272f "DoM migration: OST-striped file to DOM file"
19268
19269 test_273a() {
19270         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19271                 skip "Need MDS version at least 2.11.50"
19272
19273         # Layout swap cannot be done if either file has DOM component,
19274         # this will never be supported, migration should be used instead
19275
19276         local dom=$DIR/$tdir/$tfile
19277         mkdir -p $DIR/$tdir
19278
19279         $LFS setstripe -c2 ${dom}_plain
19280         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
19281         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
19282                 error "can swap layout with DoM component"
19283         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
19284                 error "can swap layout with DoM component"
19285
19286         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
19287         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
19288                 error "can swap layout with DoM component"
19289         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
19290                 error "can swap layout with DoM component"
19291         return 0
19292 }
19293 run_test 273a "DoM: layout swapping should fail with DOM"
19294
19295 test_275() {
19296         remote_ost_nodsh && skip "remote OST with nodsh"
19297         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
19298                 skip "Need OST version >= 2.10.57"
19299
19300         local file=$DIR/$tfile
19301         local oss
19302
19303         oss=$(comma_list $(osts_nodes))
19304
19305         dd if=/dev/urandom of=$file bs=1M count=2 ||
19306                 error "failed to create a file"
19307         cancel_lru_locks osc
19308
19309         #lock 1
19310         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
19311                 error "failed to read a file"
19312
19313 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
19314         $LCTL set_param fail_loc=0x8000031f
19315
19316         cancel_lru_locks osc &
19317         sleep 1
19318
19319 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
19320         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
19321         #IO takes another lock, but matches the PENDING one
19322         #and places it to the IO RPC
19323         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
19324                 error "failed to read a file with PENDING lock"
19325 }
19326 run_test 275 "Read on a canceled duplicate lock"
19327
19328 test_276() {
19329         remote_ost_nodsh && skip "remote OST with nodsh"
19330         local pid
19331
19332         do_facet ost1 "(while true; do \
19333                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
19334                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
19335         pid=$!
19336
19337         for LOOP in $(seq 20); do
19338                 stop ost1
19339                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
19340         done
19341         kill -9 $pid
19342         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
19343                 rm $TMP/sanity_276_pid"
19344 }
19345 run_test 276 "Race between mount and obd_statfs"
19346
19347 test_277() {
19348         $LCTL set_param ldlm.namespaces.*.lru_size=0
19349         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
19350         local cached_mb=$($LCTL get_param llite.*.max_cached_mb |
19351                         grep ^used_mb | awk '{print $2}')
19352         [ $cached_mb -eq 1 ] || error "expected mb 1 got $cached_mb"
19353         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 \
19354                 oflag=direct conv=notrunc
19355         cached_mb=$($LCTL get_param llite.*.max_cached_mb |
19356                         grep ^used_mb | awk '{print $2}')
19357         [ $cached_mb -eq 0 ] || error "expected mb 0 got $cached_mb"
19358 }
19359 run_test 277 "Direct IO shall drop page cache"
19360
19361 test_278() {
19362         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
19363         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
19364         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] &&
19365                 skip "needs the same host for mdt1 mdt2" && return
19366
19367         local pid1
19368         local pid2
19369
19370 #define OBD_FAIL_OBD_STOP_MDS_RACE     0x60b
19371         do_facet mds2 $LCTL set_param fail_loc=0x8000060c
19372         stop mds2 &
19373         pid2=$!
19374
19375         stop mds1
19376
19377         echo "Starting MDTs"
19378         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
19379         wait $pid2
19380 #For the error assertion will happen. lu_env_get_key(..., &mdt_thread_key)
19381 #will return NULL
19382         do_facet mds2 $LCTL set_param fail_loc=0
19383
19384         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS
19385         wait_recovery_complete mds2
19386 }
19387 run_test 278 "Race starting MDS between MDTs stop/start"
19388
19389 cleanup_test_300() {
19390         trap 0
19391         umask $SAVE_UMASK
19392 }
19393 test_striped_dir() {
19394         local mdt_index=$1
19395         local stripe_count
19396         local stripe_index
19397
19398         mkdir -p $DIR/$tdir
19399
19400         SAVE_UMASK=$(umask)
19401         trap cleanup_test_300 RETURN EXIT
19402
19403         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
19404                                                 $DIR/$tdir/striped_dir ||
19405                 error "set striped dir error"
19406
19407         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
19408         [ "$mode" = "755" ] || error "expect 755 got $mode"
19409
19410         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
19411                 error "getdirstripe failed"
19412         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
19413         if [ "$stripe_count" != "2" ]; then
19414                 error "1:stripe_count is $stripe_count, expect 2"
19415         fi
19416         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
19417         if [ "$stripe_count" != "2" ]; then
19418                 error "2:stripe_count is $stripe_count, expect 2"
19419         fi
19420
19421         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
19422         if [ "$stripe_index" != "$mdt_index" ]; then
19423                 error "stripe_index is $stripe_index, expect $mdt_index"
19424         fi
19425
19426         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19427                 error "nlink error after create striped dir"
19428
19429         mkdir $DIR/$tdir/striped_dir/a
19430         mkdir $DIR/$tdir/striped_dir/b
19431
19432         stat $DIR/$tdir/striped_dir/a ||
19433                 error "create dir under striped dir failed"
19434         stat $DIR/$tdir/striped_dir/b ||
19435                 error "create dir under striped dir failed"
19436
19437         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
19438                 error "nlink error after mkdir"
19439
19440         rmdir $DIR/$tdir/striped_dir/a
19441         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
19442                 error "nlink error after rmdir"
19443
19444         rmdir $DIR/$tdir/striped_dir/b
19445         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19446                 error "nlink error after rmdir"
19447
19448         chattr +i $DIR/$tdir/striped_dir
19449         createmany -o $DIR/$tdir/striped_dir/f 10 &&
19450                 error "immutable flags not working under striped dir!"
19451         chattr -i $DIR/$tdir/striped_dir
19452
19453         rmdir $DIR/$tdir/striped_dir ||
19454                 error "rmdir striped dir error"
19455
19456         cleanup_test_300
19457
19458         true
19459 }
19460
19461 test_300a() {
19462         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19463                 skip "skipped for lustre < 2.7.0"
19464         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19465         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19466
19467         test_striped_dir 0 || error "failed on striped dir on MDT0"
19468         test_striped_dir 1 || error "failed on striped dir on MDT0"
19469 }
19470 run_test 300a "basic striped dir sanity test"
19471
19472 test_300b() {
19473         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19474                 skip "skipped for lustre < 2.7.0"
19475         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19476         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19477
19478         local i
19479         local mtime1
19480         local mtime2
19481         local mtime3
19482
19483         test_mkdir $DIR/$tdir || error "mkdir fail"
19484         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19485                 error "set striped dir error"
19486         for i in {0..9}; do
19487                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
19488                 sleep 1
19489                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
19490                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
19491                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
19492                 sleep 1
19493                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
19494                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
19495                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
19496         done
19497         true
19498 }
19499 run_test 300b "check ctime/mtime for striped dir"
19500
19501 test_300c() {
19502         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19503                 skip "skipped for lustre < 2.7.0"
19504         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19505         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19506
19507         local file_count
19508
19509         mkdir -p $DIR/$tdir
19510         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
19511                 error "set striped dir error"
19512
19513         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
19514                 error "chown striped dir failed"
19515
19516         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
19517                 error "create 5k files failed"
19518
19519         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
19520
19521         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
19522
19523         rm -rf $DIR/$tdir
19524 }
19525 run_test 300c "chown && check ls under striped directory"
19526
19527 test_300d() {
19528         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19529                 skip "skipped for lustre < 2.7.0"
19530         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19531         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19532
19533         local stripe_count
19534         local file
19535
19536         mkdir -p $DIR/$tdir
19537         $LFS setstripe -c 2 $DIR/$tdir
19538
19539         #local striped directory
19540         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19541                 error "set striped dir error"
19542         createmany -o $DIR/$tdir/striped_dir/f 10 ||
19543                 error "create 10 files failed"
19544
19545         #remote striped directory
19546         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
19547                 error "set striped dir error"
19548         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
19549                 error "create 10 files failed"
19550
19551         for file in $(find $DIR/$tdir); do
19552                 stripe_count=$($LFS getstripe -c $file)
19553                 [ $stripe_count -eq 2 ] ||
19554                         error "wrong stripe $stripe_count for $file"
19555         done
19556
19557         rm -rf $DIR/$tdir
19558 }
19559 run_test 300d "check default stripe under striped directory"
19560
19561 test_300e() {
19562         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19563                 skip "Need MDS version at least 2.7.55"
19564         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19565         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19566
19567         local stripe_count
19568         local file
19569
19570         mkdir -p $DIR/$tdir
19571
19572         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19573                 error "set striped dir error"
19574
19575         touch $DIR/$tdir/striped_dir/a
19576         touch $DIR/$tdir/striped_dir/b
19577         touch $DIR/$tdir/striped_dir/c
19578
19579         mkdir $DIR/$tdir/striped_dir/dir_a
19580         mkdir $DIR/$tdir/striped_dir/dir_b
19581         mkdir $DIR/$tdir/striped_dir/dir_c
19582
19583         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
19584                 error "set striped adir under striped dir error"
19585
19586         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
19587                 error "set striped bdir under striped dir error"
19588
19589         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
19590                 error "set striped cdir under striped dir error"
19591
19592         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
19593                 error "rename dir under striped dir fails"
19594
19595         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
19596                 error "rename dir under different stripes fails"
19597
19598         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
19599                 error "rename file under striped dir should succeed"
19600
19601         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
19602                 error "rename dir under striped dir should succeed"
19603
19604         rm -rf $DIR/$tdir
19605 }
19606 run_test 300e "check rename under striped directory"
19607
19608 test_300f() {
19609         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19610         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19611         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19612                 skip "Need MDS version at least 2.7.55"
19613
19614         local stripe_count
19615         local file
19616
19617         rm -rf $DIR/$tdir
19618         mkdir -p $DIR/$tdir
19619
19620         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19621                 error "set striped dir error"
19622
19623         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
19624                 error "set striped dir error"
19625
19626         touch $DIR/$tdir/striped_dir/a
19627         mkdir $DIR/$tdir/striped_dir/dir_a
19628         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
19629                 error "create striped dir under striped dir fails"
19630
19631         touch $DIR/$tdir/striped_dir1/b
19632         mkdir $DIR/$tdir/striped_dir1/dir_b
19633         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
19634                 error "create striped dir under striped dir fails"
19635
19636         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
19637                 error "rename dir under different striped dir should fail"
19638
19639         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
19640                 error "rename striped dir under diff striped dir should fail"
19641
19642         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
19643                 error "rename file under diff striped dirs fails"
19644
19645         rm -rf $DIR/$tdir
19646 }
19647 run_test 300f "check rename cross striped directory"
19648
19649 test_300_check_default_striped_dir()
19650 {
19651         local dirname=$1
19652         local default_count=$2
19653         local default_index=$3
19654         local stripe_count
19655         local stripe_index
19656         local dir_stripe_index
19657         local dir
19658
19659         echo "checking $dirname $default_count $default_index"
19660         $LFS setdirstripe -D -c $default_count -i $default_index \
19661                                 -t all_char $DIR/$tdir/$dirname ||
19662                 error "set default stripe on striped dir error"
19663         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
19664         [ $stripe_count -eq $default_count ] ||
19665                 error "expect $default_count get $stripe_count for $dirname"
19666
19667         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
19668         [ $stripe_index -eq $default_index ] ||
19669                 error "expect $default_index get $stripe_index for $dirname"
19670
19671         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
19672                                                 error "create dirs failed"
19673
19674         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
19675         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
19676         for dir in $(find $DIR/$tdir/$dirname/*); do
19677                 stripe_count=$($LFS getdirstripe -c $dir)
19678                 [ $stripe_count -eq $default_count ] ||
19679                 [ $stripe_count -eq 0 ] || [ $default_count -eq 1 ] ||
19680                 error "stripe count $default_count != $stripe_count for $dir"
19681
19682                 stripe_index=$($LFS getdirstripe -i $dir)
19683                 [ $default_index -eq -1 ] ||
19684                         [ $stripe_index -eq $default_index ] ||
19685                         error "$stripe_index != $default_index for $dir"
19686
19687                 #check default stripe
19688                 stripe_count=$($LFS getdirstripe -D -c $dir)
19689                 [ $stripe_count -eq $default_count ] ||
19690                 error "default count $default_count != $stripe_count for $dir"
19691
19692                 stripe_index=$($LFS getdirstripe -D -i $dir)
19693                 [ $stripe_index -eq $default_index ] ||
19694                 error "default index $default_index != $stripe_index for $dir"
19695         done
19696         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
19697 }
19698
19699 test_300g() {
19700         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19701         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19702                 skip "Need MDS version at least 2.7.55"
19703
19704         local dir
19705         local stripe_count
19706         local stripe_index
19707
19708         mkdir $DIR/$tdir
19709         mkdir $DIR/$tdir/normal_dir
19710
19711         #Checking when client cache stripe index
19712         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
19713         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
19714                 error "create striped_dir failed"
19715
19716         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
19717                 error "create dir0 fails"
19718         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
19719         [ $stripe_index -eq 0 ] ||
19720                 error "dir0 expect index 0 got $stripe_index"
19721
19722         mkdir $DIR/$tdir/striped_dir/dir1 ||
19723                 error "create dir1 fails"
19724         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
19725         [ $stripe_index -eq 1 ] ||
19726                 error "dir1 expect index 1 got $stripe_index"
19727
19728         #check default stripe count/stripe index
19729         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
19730         test_300_check_default_striped_dir normal_dir 1 0
19731         test_300_check_default_striped_dir normal_dir 2 1
19732         test_300_check_default_striped_dir normal_dir 2 -1
19733
19734         #delete default stripe information
19735         echo "delete default stripeEA"
19736         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
19737                 error "set default stripe on striped dir error"
19738
19739         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
19740         for dir in $(find $DIR/$tdir/normal_dir/*); do
19741                 stripe_count=$($LFS getdirstripe -c $dir)
19742                 [ $stripe_count -eq 0 ] ||
19743                         error "expect 1 get $stripe_count for $dir"
19744                 stripe_index=$($LFS getdirstripe -i $dir)
19745                 [ $stripe_index -eq 0 ] ||
19746                         error "expect 0 get $stripe_index for $dir"
19747         done
19748 }
19749 run_test 300g "check default striped directory for normal directory"
19750
19751 test_300h() {
19752         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19753         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19754                 skip "Need MDS version at least 2.7.55"
19755
19756         local dir
19757         local stripe_count
19758
19759         mkdir $DIR/$tdir
19760         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19761                 error "set striped dir error"
19762
19763         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
19764         test_300_check_default_striped_dir striped_dir 1 0
19765         test_300_check_default_striped_dir striped_dir 2 1
19766         test_300_check_default_striped_dir striped_dir 2 -1
19767
19768         #delete default stripe information
19769         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
19770                 error "set default stripe on striped dir error"
19771
19772         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
19773         for dir in $(find $DIR/$tdir/striped_dir/*); do
19774                 stripe_count=$($LFS getdirstripe -c $dir)
19775                 [ $stripe_count -eq 0 ] ||
19776                         error "expect 1 get $stripe_count for $dir"
19777         done
19778 }
19779 run_test 300h "check default striped directory for striped directory"
19780
19781 test_300i() {
19782         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19783         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19784         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19785                 skip "Need MDS version at least 2.7.55"
19786
19787         local stripe_count
19788         local file
19789
19790         mkdir $DIR/$tdir
19791
19792         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19793                 error "set striped dir error"
19794
19795         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
19796                 error "create files under striped dir failed"
19797
19798         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
19799                 error "set striped hashdir error"
19800
19801         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
19802                 error "create dir0 under hash dir failed"
19803         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
19804                 error "create dir1 under hash dir failed"
19805
19806         # unfortunately, we need to umount to clear dir layout cache for now
19807         # once we fully implement dir layout, we can drop this
19808         umount_client $MOUNT || error "umount failed"
19809         mount_client $MOUNT || error "mount failed"
19810
19811         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
19812         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
19813         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
19814
19815         #set the stripe to be unknown hash type
19816         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
19817         $LCTL set_param fail_loc=0x1901
19818         for ((i = 0; i < 10; i++)); do
19819                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
19820                         error "stat f-$i failed"
19821                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
19822         done
19823
19824         touch $DIR/$tdir/striped_dir/f0 &&
19825                 error "create under striped dir with unknown hash should fail"
19826
19827         $LCTL set_param fail_loc=0
19828
19829         umount_client $MOUNT || error "umount failed"
19830         mount_client $MOUNT || error "mount failed"
19831
19832         return 0
19833 }
19834 run_test 300i "client handle unknown hash type striped directory"
19835
19836 test_300j() {
19837         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19838         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19839         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19840                 skip "Need MDS version at least 2.7.55"
19841
19842         local stripe_count
19843         local file
19844
19845         mkdir $DIR/$tdir
19846
19847         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
19848         $LCTL set_param fail_loc=0x1702
19849         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19850                 error "set striped dir error"
19851
19852         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
19853                 error "create files under striped dir failed"
19854
19855         $LCTL set_param fail_loc=0
19856
19857         rm -rf $DIR/$tdir || error "unlink striped dir fails"
19858
19859         return 0
19860 }
19861 run_test 300j "test large update record"
19862
19863 test_300k() {
19864         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19865         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19866         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19867                 skip "Need MDS version at least 2.7.55"
19868
19869         # this test needs a huge transaction
19870         local kb
19871         kb=$(do_facet $SINGLEMDS "$LCTL get_param -n \
19872              osd*.$FSNAME-MDT0000.kbytestotal")
19873         [ $kb -lt $((1024*1024)) ] && skip "MDT0 too small: $kb"
19874
19875         local stripe_count
19876         local file
19877
19878         mkdir $DIR/$tdir
19879
19880         #define OBD_FAIL_LARGE_STRIPE   0x1703
19881         $LCTL set_param fail_loc=0x1703
19882         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
19883                 error "set striped dir error"
19884         $LCTL set_param fail_loc=0
19885
19886         $LFS getdirstripe $DIR/$tdir/striped_dir ||
19887                 error "getstripeddir fails"
19888         rm -rf $DIR/$tdir/striped_dir ||
19889                 error "unlink striped dir fails"
19890
19891         return 0
19892 }
19893 run_test 300k "test large striped directory"
19894
19895 test_300l() {
19896         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19897         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19898         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19899                 skip "Need MDS version at least 2.7.55"
19900
19901         local stripe_index
19902
19903         test_mkdir -p $DIR/$tdir/striped_dir
19904         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
19905                         error "chown $RUNAS_ID failed"
19906         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
19907                 error "set default striped dir failed"
19908
19909         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
19910         $LCTL set_param fail_loc=0x80000158
19911         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
19912
19913         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
19914         [ $stripe_index -eq 1 ] ||
19915                 error "expect 1 get $stripe_index for $dir"
19916 }
19917 run_test 300l "non-root user to create dir under striped dir with stale layout"
19918
19919 test_300m() {
19920         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19921         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
19922         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19923                 skip "Need MDS version at least 2.7.55"
19924
19925         mkdir -p $DIR/$tdir/striped_dir
19926         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
19927                 error "set default stripes dir error"
19928
19929         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
19930
19931         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
19932         [ $stripe_count -eq 0 ] ||
19933                         error "expect 0 get $stripe_count for a"
19934
19935         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
19936                 error "set default stripes dir error"
19937
19938         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
19939
19940         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
19941         [ $stripe_count -eq 0 ] ||
19942                         error "expect 0 get $stripe_count for b"
19943
19944         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
19945                 error "set default stripes dir error"
19946
19947         mkdir $DIR/$tdir/striped_dir/c &&
19948                 error "default stripe_index is invalid, mkdir c should fails"
19949
19950         rm -rf $DIR/$tdir || error "rmdir fails"
19951 }
19952 run_test 300m "setstriped directory on single MDT FS"
19953
19954 cleanup_300n() {
19955         local list=$(comma_list $(mdts_nodes))
19956
19957         trap 0
19958         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19959 }
19960
19961 test_300n() {
19962         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19963         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19964         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19965                 skip "Need MDS version at least 2.7.55"
19966         remote_mds_nodsh && skip "remote MDS with nodsh"
19967
19968         local stripe_index
19969         local list=$(comma_list $(mdts_nodes))
19970
19971         trap cleanup_300n RETURN EXIT
19972         mkdir -p $DIR/$tdir
19973         chmod 777 $DIR/$tdir
19974         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
19975                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
19976                 error "create striped dir succeeds with gid=0"
19977
19978         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
19979         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
19980                 error "create striped dir fails with gid=-1"
19981
19982         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19983         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
19984                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
19985                 error "set default striped dir succeeds with gid=0"
19986
19987
19988         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
19989         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
19990                 error "set default striped dir fails with gid=-1"
19991
19992
19993         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19994         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
19995                                         error "create test_dir fails"
19996         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
19997                                         error "create test_dir1 fails"
19998         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
19999                                         error "create test_dir2 fails"
20000         cleanup_300n
20001 }
20002 run_test 300n "non-root user to create dir under striped dir with default EA"
20003
20004 test_300o() {
20005         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20006         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20007         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20008                 skip "Need MDS version at least 2.7.55"
20009
20010         local numfree1
20011         local numfree2
20012
20013         mkdir -p $DIR/$tdir
20014
20015         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
20016         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
20017         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
20018                 skip "not enough free inodes $numfree1 $numfree2"
20019         fi
20020
20021         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
20022         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
20023         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
20024                 skip "not enough free space $numfree1 $numfree2"
20025         fi
20026
20027         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
20028                 error "setdirstripe fails"
20029
20030         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
20031                 error "create dirs fails"
20032
20033         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
20034         ls $DIR/$tdir/striped_dir > /dev/null ||
20035                 error "ls striped dir fails"
20036         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
20037                 error "unlink big striped dir fails"
20038 }
20039 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
20040
20041 test_300p() {
20042         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20043         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20044         remote_mds_nodsh && skip "remote MDS with nodsh"
20045
20046         mkdir -p $DIR/$tdir
20047
20048         #define OBD_FAIL_OUT_ENOSPC     0x1704
20049         do_facet mds2 lctl set_param fail_loc=0x80001704
20050         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
20051                  && error "create striped directory should fail"
20052
20053         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
20054
20055         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
20056         true
20057 }
20058 run_test 300p "create striped directory without space"
20059
20060 test_300q() {
20061         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20062         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20063
20064         local fd=$(free_fd)
20065         local cmd="exec $fd<$tdir"
20066         cd $DIR
20067         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
20068         eval $cmd
20069         cmd="exec $fd<&-"
20070         trap "eval $cmd" EXIT
20071         cd $tdir || error "cd $tdir fails"
20072         rmdir  ../$tdir || error "rmdir $tdir fails"
20073         mkdir local_dir && error "create dir succeeds"
20074         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
20075         eval $cmd
20076         return 0
20077 }
20078 run_test 300q "create remote directory under orphan directory"
20079
20080 test_300r() {
20081         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20082                 skip "Need MDS version at least 2.7.55" && return
20083         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
20084
20085         mkdir $DIR/$tdir
20086
20087         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
20088                 error "set striped dir error"
20089
20090         $LFS getdirstripe $DIR/$tdir/striped_dir ||
20091                 error "getstripeddir fails"
20092
20093         local stripe_count
20094         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
20095                       awk '/lmv_stripe_count:/ { print $2 }')
20096
20097         [ $MDSCOUNT -ne $stripe_count ] &&
20098                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
20099
20100         rm -rf $DIR/$tdir/striped_dir ||
20101                 error "unlink striped dir fails"
20102 }
20103 run_test 300r "test -1 striped directory"
20104
20105 prepare_remote_file() {
20106         mkdir $DIR/$tdir/src_dir ||
20107                 error "create remote source failed"
20108
20109         cp /etc/hosts $DIR/$tdir/src_dir/a ||
20110                  error "cp to remote source failed"
20111         touch $DIR/$tdir/src_dir/a
20112
20113         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
20114                 error "create remote target dir failed"
20115
20116         touch $DIR/$tdir/tgt_dir/b
20117
20118         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
20119                 error "rename dir cross MDT failed!"
20120
20121         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
20122                 error "src_child still exists after rename"
20123
20124         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
20125                 error "missing file(a) after rename"
20126
20127         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
20128                 error "diff after rename"
20129 }
20130
20131 test_310a() {
20132         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
20133         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20134
20135         local remote_file=$DIR/$tdir/tgt_dir/b
20136
20137         mkdir -p $DIR/$tdir
20138
20139         prepare_remote_file || error "prepare remote file failed"
20140
20141         #open-unlink file
20142         $OPENUNLINK $remote_file $remote_file ||
20143                 error "openunlink $remote_file failed"
20144         $CHECKSTAT -a $remote_file || error "$remote_file exists"
20145 }
20146 run_test 310a "open unlink remote file"
20147
20148 test_310b() {
20149         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
20150         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20151
20152         local remote_file=$DIR/$tdir/tgt_dir/b
20153
20154         mkdir -p $DIR/$tdir
20155
20156         prepare_remote_file || error "prepare remote file failed"
20157
20158         ln $remote_file $DIR/$tfile || error "link failed for remote file"
20159         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
20160         $CHECKSTAT -t file $remote_file || error "check file failed"
20161 }
20162 run_test 310b "unlink remote file with multiple links while open"
20163
20164 test_310c() {
20165         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20166         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
20167
20168         local remote_file=$DIR/$tdir/tgt_dir/b
20169
20170         mkdir -p $DIR/$tdir
20171
20172         prepare_remote_file || error "prepare remote file failed"
20173
20174         ln $remote_file $DIR/$tfile || error "link failed for remote file"
20175         multiop_bg_pause $remote_file O_uc ||
20176                         error "mulitop failed for remote file"
20177         MULTIPID=$!
20178         $MULTIOP $DIR/$tfile Ouc
20179         kill -USR1 $MULTIPID
20180         wait $MULTIPID
20181 }
20182 run_test 310c "open-unlink remote file with multiple links"
20183
20184 #LU-4825
20185 test_311() {
20186         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20187         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
20188         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
20189                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
20190         remote_mds_nodsh && skip "remote MDS with nodsh"
20191
20192         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
20193         local mdts=$(comma_list $(mdts_nodes))
20194
20195         mkdir -p $DIR/$tdir
20196         $LFS setstripe -i 0 -c 1 $DIR/$tdir
20197         createmany -o $DIR/$tdir/$tfile. 1000
20198
20199         # statfs data is not real time, let's just calculate it
20200         old_iused=$((old_iused + 1000))
20201
20202         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
20203                         osp.*OST0000*MDT0000.create_count")
20204         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
20205                                 osp.*OST0000*MDT0000.max_create_count")
20206         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
20207
20208         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
20209         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
20210         [ $index -ne 0 ] || error "$tfile stripe index is 0"
20211
20212         unlinkmany $DIR/$tdir/$tfile. 1000
20213
20214         do_nodes $mdts "$LCTL set_param -n \
20215                         osp.*OST0000*.max_create_count=$max_count"
20216         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
20217                 do_nodes $mdts "$LCTL set_param -n \
20218                                 osp.*OST0000*.create_count=$count"
20219         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
20220                         grep "=0" && error "create_count is zero"
20221
20222         local new_iused
20223         for i in $(seq 120); do
20224                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
20225                 # system may be too busy to destroy all objs in time, use
20226                 # a somewhat small value to not fail autotest
20227                 [ $((old_iused - new_iused)) -gt 400 ] && break
20228                 sleep 1
20229         done
20230
20231         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
20232         [ $((old_iused - new_iused)) -gt 400 ] ||
20233                 error "objs not destroyed after unlink"
20234 }
20235 run_test 311 "disable OSP precreate, and unlink should destroy objs"
20236
20237 zfs_oid_to_objid()
20238 {
20239         local ost=$1
20240         local objid=$2
20241
20242         local vdevdir=$(dirname $(facet_vdevice $ost))
20243         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
20244         local zfs_zapid=$(do_facet $ost $cmd |
20245                           grep -w "/O/0/d$((objid%32))" -C 5 |
20246                           awk '/Object/{getline; print $1}')
20247         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
20248                           awk "/$objid = /"'{printf $3}')
20249
20250         echo $zfs_objid
20251 }
20252
20253 zfs_object_blksz() {
20254         local ost=$1
20255         local objid=$2
20256
20257         local vdevdir=$(dirname $(facet_vdevice $ost))
20258         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
20259         local blksz=$(do_facet $ost $cmd $objid |
20260                       awk '/dblk/{getline; printf $4}')
20261
20262         case "${blksz: -1}" in
20263                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
20264                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
20265                 *) ;;
20266         esac
20267
20268         echo $blksz
20269 }
20270
20271 test_312() { # LU-4856
20272         remote_ost_nodsh && skip "remote OST with nodsh"
20273         [ "$ost1_FSTYPE" = "zfs" ] ||
20274                 skip_env "the test only applies to zfs"
20275
20276         local max_blksz=$(do_facet ost1 \
20277                           $ZFS get -p recordsize $(facet_device ost1) |
20278                           awk '!/VALUE/{print $3}')
20279
20280         # to make life a little bit easier
20281         $LFS mkdir -c 1 -i 0 $DIR/$tdir
20282         $LFS setstripe -c 1 -i 0 $DIR/$tdir
20283
20284         local tf=$DIR/$tdir/$tfile
20285         touch $tf
20286         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20287
20288         # Get ZFS object id
20289         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20290         # block size change by sequential overwrite
20291         local bs
20292
20293         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
20294                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
20295
20296                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
20297                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
20298         done
20299         rm -f $tf
20300
20301         # block size change by sequential append write
20302         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
20303         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20304         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20305         local count
20306
20307         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
20308                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
20309                         oflag=sync conv=notrunc
20310
20311                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
20312                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
20313                         error "blksz error, actual $blksz, " \
20314                                 "expected: 2 * $count * $PAGE_SIZE"
20315         done
20316         rm -f $tf
20317
20318         # random write
20319         touch $tf
20320         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20321         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20322
20323         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
20324         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20325         [ $blksz -eq $PAGE_SIZE ] ||
20326                 error "blksz error: $blksz, expected: $PAGE_SIZE"
20327
20328         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
20329         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20330         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
20331
20332         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
20333         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20334         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
20335 }
20336 run_test 312 "make sure ZFS adjusts its block size by write pattern"
20337
20338 test_313() {
20339         remote_ost_nodsh && skip "remote OST with nodsh"
20340
20341         local file=$DIR/$tfile
20342
20343         rm -f $file
20344         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
20345
20346         # define OBD_FAIL_TGT_RCVD_EIO           0x720
20347         do_facet ost1 "$LCTL set_param fail_loc=0x720"
20348         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
20349                 error "write should failed"
20350         do_facet ost1 "$LCTL set_param fail_loc=0"
20351         rm -f $file
20352 }
20353 run_test 313 "io should fail after last_rcvd update fail"
20354
20355 test_314() {
20356         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
20357
20358         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
20359         do_facet ost1 "$LCTL set_param fail_loc=0x720"
20360         rm -f $DIR/$tfile
20361         wait_delete_completed
20362         do_facet ost1 "$LCTL set_param fail_loc=0"
20363 }
20364 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
20365
20366 test_315() { # LU-618
20367         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
20368
20369         local file=$DIR/$tfile
20370         rm -f $file
20371
20372         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
20373                 error "multiop file write failed"
20374         $MULTIOP $file oO_RDONLY:r4063232_c &
20375         PID=$!
20376
20377         sleep 2
20378
20379         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
20380         kill -USR1 $PID
20381
20382         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
20383         rm -f $file
20384 }
20385 run_test 315 "read should be accounted"
20386
20387 test_316() {
20388         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20389         large_xattr_enabled || skip_env "ea_inode feature disabled"
20390
20391         rm -rf $DIR/$tdir/d
20392         mkdir -p $DIR/$tdir/d
20393         chown nobody $DIR/$tdir/d
20394         touch $DIR/$tdir/d/file
20395
20396         $LFS mv -M1 $DIR/$tdir/d || error "lfs mv failed"
20397 }
20398 run_test 316 "lfs mv"
20399
20400 test_317() {
20401         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
20402                 skip "Need MDS version at least 2.11.53"
20403         if [ "$ost1_FSTYPE" == "zfs" ]; then
20404                 skip "LU-10370: no implementation for ZFS"
20405         fi
20406
20407         local trunc_sz
20408         local grant_blk_size
20409
20410         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
20411                         awk '/grant_block_size:/ { print $2; exit; }')
20412         #
20413         # Create File of size 5M. Truncate it to below size's and verify
20414         # blocks count.
20415         #
20416         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
20417                 error "Create file $DIR/$tfile failed"
20418         stack_trap "rm -f $DIR/$tfile" EXIT
20419
20420         for trunc_sz in 2097152 4097 4000 509 0; do
20421                 $TRUNCATE $DIR/$tfile $trunc_sz ||
20422                         error "truncate $tfile to $trunc_sz failed"
20423                 local sz=$(stat --format=%s $DIR/$tfile)
20424                 local blk=$(stat --format=%b $DIR/$tfile)
20425                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
20426                                      grant_blk_size) * 8))
20427
20428                 if [[ $blk -ne $trunc_blk ]]; then
20429                         $(which stat) $DIR/$tfile
20430                         error "Expected Block $trunc_blk got $blk for $tfile"
20431                 fi
20432
20433                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20434                         error "Expected Size $trunc_sz got $sz for $tfile"
20435         done
20436
20437         #
20438         # sparse file test
20439         # Create file with a hole and write actual two blocks. Block count
20440         # must be 16.
20441         #
20442         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
20443                 conv=fsync || error "Create file : $DIR/$tfile"
20444
20445         # Calculate the final truncate size.
20446         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
20447
20448         #
20449         # truncate to size $trunc_sz bytes. Strip the last block
20450         # The block count must drop to 8
20451         #
20452         $TRUNCATE $DIR/$tfile $trunc_sz ||
20453                 error "truncate $tfile to $trunc_sz failed"
20454
20455         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
20456         sz=$(stat --format=%s $DIR/$tfile)
20457         blk=$(stat --format=%b $DIR/$tfile)
20458
20459         if [[ $blk -ne $trunc_bsz ]]; then
20460                 $(which stat) $DIR/$tfile
20461                 error "Expected Block $trunc_bsz got $blk for $tfile"
20462         fi
20463
20464         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20465                 error "Expected Size $trunc_sz got $sz for $tfile"
20466 }
20467 run_test 317 "Verify blocks get correctly update after truncate"
20468
20469 test_318() {
20470         local old_max_active=$($LCTL get_param -n \
20471                             llite.*.max_read_ahead_async_active 2>/dev/null)
20472
20473         $LCTL set_param llite.*.max_read_ahead_async_active=256
20474         local max_active=$($LCTL get_param -n \
20475                            llite.*.max_read_ahead_async_active 2>/dev/null)
20476         [ $max_active -ne 256 ] && error "expected 256 but got $max_active"
20477
20478         # currently reset to 0 is unsupported, leave it 512 for now.
20479         $LCTL set_param llite.*.max_read_ahead_async_active=0 &&
20480                 error "set max_read_ahead_async_active should fail"
20481
20482         $LCTL set_param llite.*.max_read_ahead_async_active=512
20483         max_active=$($LCTL get_param -n \
20484                      llite.*.max_read_ahead_async_active 2>/dev/null)
20485         [ $max_active -eq 512 ] || error "expected 512 but got $max_active"
20486
20487         # restore @max_active
20488         [ $old_max_active -ne 0 ] && $LCTL set_param \
20489                 llite.*.max_read_ahead_async_active=$old_max_active
20490
20491         local old_threshold=$($LCTL get_param -n \
20492                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
20493         local max_per_file_mb=$($LCTL get_param -n \
20494                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
20495
20496         local invalid=$(($max_per_file_mb + 1))
20497         $LCTL set_param \
20498                 llite.*.read_ahead_async_file_threshold_mb=$invalid\
20499                         && error "set $invalid should fail"
20500
20501         local valid=$(($invalid - 1))
20502         $LCTL set_param \
20503                 llite.*.read_ahead_async_file_threshold_mb=$valid ||
20504                         error "set $valid should succeed"
20505         local threshold=$($LCTL get_param -n \
20506                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
20507         [ $threshold -eq $valid ] || error \
20508                 "expect threshold $valid got $threshold"
20509         $LCTL set_param \
20510                 llite.*.read_ahead_async_file_threshold_mb=$old_threshold
20511 }
20512 run_test 318 "Verify async readahead tunables"
20513
20514 test_319() {
20515         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
20516
20517         local before=$(date +%s)
20518         local evict
20519         local mdir=$DIR/$tdir
20520         local file=$mdir/xxx
20521
20522         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
20523         touch $file
20524
20525 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
20526         $LCTL set_param fail_val=5 fail_loc=0x8000032c
20527         $LFS mv -m1 $file &
20528
20529         sleep 1
20530         dd if=$file of=/dev/null
20531         wait
20532         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
20533           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
20534
20535         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
20536 }
20537 run_test 319 "lost lease lock on migrate error"
20538
20539 test_398a() { # LU-4198
20540         $LFS setstripe -c 1 -i 0 $DIR/$tfile
20541         $LCTL set_param ldlm.namespaces.*.lru_size=clear
20542
20543         # request a new lock on client
20544         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
20545
20546         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
20547         local lock_count=$($LCTL get_param -n \
20548                            ldlm.namespaces.*-OST0000-osc-ffff*.lru_size)
20549         [[ $lock_count -eq 0 ]] || error "lock should be cancelled by direct IO"
20550
20551         $LCTL set_param ldlm.namespaces.*-OST0000-osc-ffff*.lru_size=clear
20552
20553         # no lock cached, should use lockless IO and not enqueue new lock
20554         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
20555         lock_count=$($LCTL get_param -n \
20556                      ldlm.namespaces.*-OST0000-osc-ffff*.lru_size)
20557         [[ $lock_count -eq 0 ]] || error "no lock should be held by direct IO"
20558 }
20559 run_test 398a "direct IO should cancel lock otherwise lockless"
20560
20561 test_398b() { # LU-4198
20562         which fio || skip_env "no fio installed"
20563         $LFS setstripe -c -1 $DIR/$tfile
20564
20565         local size=12
20566         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size
20567
20568         local njobs=4
20569         echo "mix direct rw ${size}M to OST0 by fio with $njobs jobs..."
20570         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
20571                 --numjobs=$njobs --fallocate=none \
20572                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
20573                 --filename=$DIR/$tfile &
20574         bg_pid=$!
20575
20576         echo "mix buffer rw ${size}M to OST0 by fio with $njobs jobs..."
20577         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE \
20578                 --numjobs=$njobs --fallocate=none \
20579                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
20580                 --filename=$DIR/$tfile || true
20581         wait $bg_pid
20582
20583         rm -rf $DIR/$tfile
20584 }
20585 run_test 398b "DIO and buffer IO race"
20586
20587 test_398c() { # LU-4198
20588         which fio || skip_env "no fio installed"
20589
20590         saved_debug=$($LCTL get_param -n debug)
20591         $LCTL set_param debug=0
20592
20593         local size=$(lctl get_param -n osc.$FSNAME-OST0000*.kbytesavail | head -1)
20594         ((size /= 1024)) # by megabytes
20595         ((size /= 2)) # write half of the OST at most
20596         [ $size -gt 40 ] && size=40 #reduce test time anyway
20597
20598         $LFS setstripe -c 1 $DIR/$tfile
20599
20600         # it seems like ldiskfs reserves more space than necessary if the
20601         # writing blocks are not mapped, so it extends the file firstly
20602         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size && sync
20603         cancel_lru_locks osc
20604
20605         # clear and verify rpc_stats later
20606         $LCTL set_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats=clear
20607
20608         local njobs=4
20609         echo "writing ${size}M to OST0 by fio with $njobs jobs..."
20610         fio --name=rand-write --rw=randwrite --bs=$PAGE_SIZE --direct=1 \
20611                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
20612                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
20613                 --filename=$DIR/$tfile
20614         [ $? -eq 0 ] || error "fio write error"
20615
20616         [ $($LCTL get_param -n \
20617          ldlm.namespaces.${FSNAME}-OST0000-osc-ffff*.lock_count) -eq 0 ] ||
20618                 error "Locks were requested while doing AIO"
20619
20620         # get the percentage of 1-page I/O
20621         pct=$($LCTL get_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats |
20622                 grep -A 1 'pages per rpc' | grep -v 'pages per rpc' |
20623                 awk '{print $7}')
20624         [ $pct -le 50 ] || error "$pct% of I/O are 1-page"
20625
20626         echo "mix rw ${size}M to OST0 by fio with $njobs jobs..."
20627         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
20628                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
20629                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
20630                 --filename=$DIR/$tfile
20631         [ $? -eq 0 ] || error "fio mixed read write error"
20632
20633         rm -rf $DIR/$tfile
20634         $LCTL set_param debug="$saved_debug"
20635 }
20636 run_test 398c "run fio to test AIO"
20637
20638 test_fake_rw() {
20639         local read_write=$1
20640         if [ "$read_write" = "write" ]; then
20641                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
20642         elif [ "$read_write" = "read" ]; then
20643                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
20644         else
20645                 error "argument error"
20646         fi
20647
20648         # turn off debug for performance testing
20649         local saved_debug=$($LCTL get_param -n debug)
20650         $LCTL set_param debug=0
20651
20652         $LFS setstripe -c 1 -i 0 $DIR/$tfile
20653
20654         # get ost1 size - $FSNAME-OST0000
20655         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
20656         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
20657         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
20658
20659         if [ "$read_write" = "read" ]; then
20660                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
20661         fi
20662
20663         local start_time=$(date +%s.%N)
20664         $dd_cmd bs=1M count=$blocks oflag=sync ||
20665                 error "real dd $read_write error"
20666         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
20667
20668         if [ "$read_write" = "write" ]; then
20669                 rm -f $DIR/$tfile
20670         fi
20671
20672         # define OBD_FAIL_OST_FAKE_RW           0x238
20673         do_facet ost1 $LCTL set_param fail_loc=0x238
20674
20675         local start_time=$(date +%s.%N)
20676         $dd_cmd bs=1M count=$blocks oflag=sync ||
20677                 error "fake dd $read_write error"
20678         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
20679
20680         if [ "$read_write" = "write" ]; then
20681                 # verify file size
20682                 cancel_lru_locks osc
20683                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
20684                         error "$tfile size not $blocks MB"
20685         fi
20686         do_facet ost1 $LCTL set_param fail_loc=0
20687
20688         echo "fake $read_write $duration_fake vs. normal $read_write" \
20689                 "$duration in seconds"
20690         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
20691                 error_not_in_vm "fake write is slower"
20692
20693         $LCTL set_param -n debug="$saved_debug"
20694         rm -f $DIR/$tfile
20695 }
20696 test_399a() { # LU-7655 for OST fake write
20697         remote_ost_nodsh && skip "remote OST with nodsh"
20698
20699         test_fake_rw write
20700 }
20701 run_test 399a "fake write should not be slower than normal write"
20702
20703 test_399b() { # LU-8726 for OST fake read
20704         remote_ost_nodsh && skip "remote OST with nodsh"
20705         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
20706                 skip_env "ldiskfs only test"
20707         fi
20708
20709         test_fake_rw read
20710 }
20711 run_test 399b "fake read should not be slower than normal read"
20712
20713 test_400a() { # LU-1606, was conf-sanity test_74
20714         if ! which $CC > /dev/null 2>&1; then
20715                 skip_env "$CC is not installed"
20716         fi
20717
20718         local extra_flags=''
20719         local out=$TMP/$tfile
20720         local prefix=/usr/include/lustre
20721         local prog
20722
20723         if ! [[ -d $prefix ]]; then
20724                 # Assume we're running in tree and fixup the include path.
20725                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
20726                 extra_flags+=" -L$LUSTRE/utils/.lib"
20727         fi
20728
20729         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
20730                 $CC -Wall -Werror $extra_flags -o $out $prog -llustreapi ||
20731                         error "client api broken"
20732         done
20733         rm -f $out
20734 }
20735 run_test 400a "Lustre client api program can compile and link"
20736
20737 test_400b() { # LU-1606, LU-5011
20738         local header
20739         local out=$TMP/$tfile
20740         local prefix=/usr/include/linux/lustre
20741
20742         # We use a hard coded prefix so that this test will not fail
20743         # when run in tree. There are headers in lustre/include/lustre/
20744         # that are not packaged (like lustre_idl.h) and have more
20745         # complicated include dependencies (like config.h and lnet/types.h).
20746         # Since this test about correct packaging we just skip them when
20747         # they don't exist (see below) rather than try to fixup cppflags.
20748
20749         if ! which $CC > /dev/null 2>&1; then
20750                 skip_env "$CC is not installed"
20751         fi
20752
20753         for header in $prefix/*.h; do
20754                 if ! [[ -f "$header" ]]; then
20755                         continue
20756                 fi
20757
20758                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
20759                         continue # lustre_ioctl.h is internal header
20760                 fi
20761
20762                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
20763                         error "cannot compile '$header'"
20764         done
20765         rm -f $out
20766 }
20767 run_test 400b "packaged headers can be compiled"
20768
20769 test_401a() { #LU-7437
20770         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
20771         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
20772
20773         #count the number of parameters by "list_param -R"
20774         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
20775         #count the number of parameters by listing proc files
20776         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
20777         echo "proc_dirs='$proc_dirs'"
20778         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
20779         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
20780                       sort -u | wc -l)
20781
20782         [ $params -eq $procs ] ||
20783                 error "found $params parameters vs. $procs proc files"
20784
20785         # test the list_param -D option only returns directories
20786         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
20787         #count the number of parameters by listing proc directories
20788         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
20789                 sort -u | wc -l)
20790
20791         [ $params -eq $procs ] ||
20792                 error "found $params parameters vs. $procs proc files"
20793 }
20794 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
20795
20796 test_401b() {
20797         local save=$($LCTL get_param -n jobid_var)
20798         local tmp=testing
20799
20800         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
20801                 error "no error returned when setting bad parameters"
20802
20803         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
20804         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
20805
20806         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
20807         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
20808         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
20809 }
20810 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
20811
20812 test_401c() {
20813         local jobid_var_old=$($LCTL get_param -n jobid_var)
20814         local jobid_var_new
20815
20816         $LCTL set_param jobid_var= &&
20817                 error "no error returned for 'set_param a='"
20818
20819         jobid_var_new=$($LCTL get_param -n jobid_var)
20820         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
20821                 error "jobid_var was changed by setting without value"
20822
20823         $LCTL set_param jobid_var &&
20824                 error "no error returned for 'set_param a'"
20825
20826         jobid_var_new=$($LCTL get_param -n jobid_var)
20827         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
20828                 error "jobid_var was changed by setting without value"
20829 }
20830 run_test 401c "Verify 'lctl set_param' without value fails in either format."
20831
20832 test_401d() {
20833         local jobid_var_old=$($LCTL get_param -n jobid_var)
20834         local jobid_var_new
20835         local new_value="foo=bar"
20836
20837         $LCTL set_param jobid_var=$new_value ||
20838                 error "'set_param a=b' did not accept a value containing '='"
20839
20840         jobid_var_new=$($LCTL get_param -n jobid_var)
20841         [[ "$jobid_var_new" == "$new_value" ]] ||
20842                 error "'set_param a=b' failed on a value containing '='"
20843
20844         # Reset the jobid_var to test the other format
20845         $LCTL set_param jobid_var=$jobid_var_old
20846         jobid_var_new=$($LCTL get_param -n jobid_var)
20847         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
20848                 error "failed to reset jobid_var"
20849
20850         $LCTL set_param jobid_var $new_value ||
20851                 error "'set_param a b' did not accept a value containing '='"
20852
20853         jobid_var_new=$($LCTL get_param -n jobid_var)
20854         [[ "$jobid_var_new" == "$new_value" ]] ||
20855                 error "'set_param a b' failed on a value containing '='"
20856
20857         $LCTL set_param jobid_var $jobid_var_old
20858         jobid_var_new=$($LCTL get_param -n jobid_var)
20859         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
20860                 error "failed to reset jobid_var"
20861 }
20862 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
20863
20864 test_402() {
20865         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
20866         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
20867                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
20868         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
20869                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
20870                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
20871         remote_mds_nodsh && skip "remote MDS with nodsh"
20872
20873         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
20874 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
20875         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
20876         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
20877                 echo "Touch failed - OK"
20878 }
20879 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
20880
20881 test_403() {
20882         local file1=$DIR/$tfile.1
20883         local file2=$DIR/$tfile.2
20884         local tfile=$TMP/$tfile
20885
20886         rm -f $file1 $file2 $tfile
20887
20888         touch $file1
20889         ln $file1 $file2
20890
20891         # 30 sec OBD_TIMEOUT in ll_getattr()
20892         # right before populating st_nlink
20893         $LCTL set_param fail_loc=0x80001409
20894         stat -c %h $file1 > $tfile &
20895
20896         # create an alias, drop all locks and reclaim the dentry
20897         < $file2
20898         cancel_lru_locks mdc
20899         cancel_lru_locks osc
20900         sysctl -w vm.drop_caches=2
20901
20902         wait
20903
20904         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
20905
20906         rm -f $tfile $file1 $file2
20907 }
20908 run_test 403 "i_nlink should not drop to zero due to aliasing"
20909
20910 test_404() { # LU-6601
20911         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
20912                 skip "Need server version newer than 2.8.52"
20913         remote_mds_nodsh && skip "remote MDS with nodsh"
20914
20915         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
20916                 awk '/osp .*-osc-MDT/ { print $4}')
20917
20918         local osp
20919         for osp in $mosps; do
20920                 echo "Deactivate: " $osp
20921                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
20922                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
20923                         awk -vp=$osp '$4 == p { print $2 }')
20924                 [ $stat = IN ] || {
20925                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
20926                         error "deactivate error"
20927                 }
20928                 echo "Activate: " $osp
20929                 do_facet $SINGLEMDS $LCTL --device %$osp activate
20930                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
20931                         awk -vp=$osp '$4 == p { print $2 }')
20932                 [ $stat = UP ] || {
20933                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
20934                         error "activate error"
20935                 }
20936         done
20937 }
20938 run_test 404 "validate manual {de}activated works properly for OSPs"
20939
20940 test_405() {
20941         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
20942         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
20943                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
20944                         skip "Layout swap lock is not supported"
20945
20946         check_swap_layouts_support
20947
20948         test_mkdir $DIR/$tdir
20949         swap_lock_test -d $DIR/$tdir ||
20950                 error "One layout swap locked test failed"
20951 }
20952 run_test 405 "Various layout swap lock tests"
20953
20954 test_406() {
20955         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20956         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
20957         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
20958         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20959         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
20960                 skip "Need MDS version at least 2.8.50"
20961
20962         local def_stripe_size=$($LFS getstripe -S $MOUNT)
20963         local test_pool=$TESTNAME
20964
20965         pool_add $test_pool || error "pool_add failed"
20966         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
20967                 error "pool_add_targets failed"
20968
20969         save_layout_restore_at_exit $MOUNT
20970
20971         # parent set default stripe count only, child will stripe from both
20972         # parent and fs default
20973         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
20974                 error "setstripe $MOUNT failed"
20975         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
20976         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
20977         for i in $(seq 10); do
20978                 local f=$DIR/$tdir/$tfile.$i
20979                 touch $f || error "touch failed"
20980                 local count=$($LFS getstripe -c $f)
20981                 [ $count -eq $OSTCOUNT ] ||
20982                         error "$f stripe count $count != $OSTCOUNT"
20983                 local offset=$($LFS getstripe -i $f)
20984                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
20985                 local size=$($LFS getstripe -S $f)
20986                 [ $size -eq $((def_stripe_size * 2)) ] ||
20987                         error "$f stripe size $size != $((def_stripe_size * 2))"
20988                 local pool=$($LFS getstripe -p $f)
20989                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
20990         done
20991
20992         # change fs default striping, delete parent default striping, now child
20993         # will stripe from new fs default striping only
20994         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
20995                 error "change $MOUNT default stripe failed"
20996         $LFS setstripe -c 0 $DIR/$tdir ||
20997                 error "delete $tdir default stripe failed"
20998         for i in $(seq 11 20); do
20999                 local f=$DIR/$tdir/$tfile.$i
21000                 touch $f || error "touch $f failed"
21001                 local count=$($LFS getstripe -c $f)
21002                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
21003                 local offset=$($LFS getstripe -i $f)
21004                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
21005                 local size=$($LFS getstripe -S $f)
21006                 [ $size -eq $def_stripe_size ] ||
21007                         error "$f stripe size $size != $def_stripe_size"
21008                 local pool=$($LFS getstripe -p $f)
21009                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
21010         done
21011
21012         unlinkmany $DIR/$tdir/$tfile. 1 20
21013
21014         local f=$DIR/$tdir/$tfile
21015         pool_remove_all_targets $test_pool $f
21016         pool_remove $test_pool $f
21017 }
21018 run_test 406 "DNE support fs default striping"
21019
21020 test_407() {
21021         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21022         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
21023                 skip "Need MDS version at least 2.8.55"
21024         remote_mds_nodsh && skip "remote MDS with nodsh"
21025
21026         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
21027                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
21028         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
21029                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
21030         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
21031
21032         #define OBD_FAIL_DT_TXN_STOP    0x2019
21033         for idx in $(seq $MDSCOUNT); do
21034                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
21035         done
21036         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
21037         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
21038                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
21039         true
21040 }
21041 run_test 407 "transaction fail should cause operation fail"
21042
21043 test_408() {
21044         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
21045
21046         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
21047         lctl set_param fail_loc=0x8000040a
21048         # let ll_prepare_partial_page() fail
21049         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
21050
21051         rm -f $DIR/$tfile
21052
21053         # create at least 100 unused inodes so that
21054         # shrink_icache_memory(0) should not return 0
21055         touch $DIR/$tfile-{0..100}
21056         rm -f $DIR/$tfile-{0..100}
21057         sync
21058
21059         echo 2 > /proc/sys/vm/drop_caches
21060 }
21061 run_test 408 "drop_caches should not hang due to page leaks"
21062
21063 test_409()
21064 {
21065         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21066
21067         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
21068         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
21069         touch $DIR/$tdir/guard || error "(2) Fail to create"
21070
21071         local PREFIX=$(str_repeat 'A' 128)
21072         echo "Create 1K hard links start at $(date)"
21073         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
21074                 error "(3) Fail to hard link"
21075
21076         echo "Links count should be right although linkEA overflow"
21077         stat $DIR/$tdir/guard || error "(4) Fail to stat"
21078         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
21079         [ $linkcount -eq 1001 ] ||
21080                 error "(5) Unexpected hard links count: $linkcount"
21081
21082         echo "List all links start at $(date)"
21083         ls -l $DIR/$tdir/foo > /dev/null ||
21084                 error "(6) Fail to list $DIR/$tdir/foo"
21085
21086         echo "Unlink hard links start at $(date)"
21087         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
21088                 error "(7) Fail to unlink"
21089         echo "Unlink hard links finished at $(date)"
21090 }
21091 run_test 409 "Large amount of cross-MDTs hard links on the same file"
21092
21093 test_410()
21094 {
21095         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
21096                 skip "Need client version at least 2.9.59"
21097
21098         # Create a file, and stat it from the kernel
21099         local testfile=$DIR/$tfile
21100         touch $testfile
21101
21102         local run_id=$RANDOM
21103         local my_ino=$(stat --format "%i" $testfile)
21104
21105         # Try to insert the module. This will always fail as the
21106         # module is designed to not be inserted.
21107         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
21108             &> /dev/null
21109
21110         # Anything but success is a test failure
21111         dmesg | grep -q \
21112             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
21113             error "no inode match"
21114 }
21115 run_test 410 "Test inode number returned from kernel thread"
21116
21117 cleanup_test411_cgroup() {
21118         trap 0
21119         rmdir "$1"
21120 }
21121
21122 test_411() {
21123         local cg_basedir=/sys/fs/cgroup/memory
21124         # LU-9966
21125         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
21126                 skip "no setup for cgroup"
21127
21128         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
21129                 error "test file creation failed"
21130         cancel_lru_locks osc
21131
21132         # Create a very small memory cgroup to force a slab allocation error
21133         local cgdir=$cg_basedir/osc_slab_alloc
21134         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
21135         trap "cleanup_test411_cgroup $cgdir" EXIT
21136         echo 2M > $cgdir/memory.kmem.limit_in_bytes
21137         echo 1M > $cgdir/memory.limit_in_bytes
21138
21139         # Should not LBUG, just be killed by oom-killer
21140         # dd will return 0 even allocation failure in some environment.
21141         # So don't check return value
21142         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
21143         cleanup_test411_cgroup $cgdir
21144
21145         return 0
21146 }
21147 run_test 411 "Slab allocation error with cgroup does not LBUG"
21148
21149 test_412() {
21150         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21151         if [ $MDS1_VERSION -lt $(version_code 2.10.55) ]; then
21152                 skip "Need server version at least 2.10.55"
21153         fi
21154
21155         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
21156                 error "mkdir failed"
21157         $LFS getdirstripe $DIR/$tdir
21158         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
21159         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
21160                 error "expect $((MDSCOUT - 1)) get $stripe_index"
21161         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
21162         [ $stripe_count -eq 2 ] ||
21163                 error "expect 2 get $stripe_count"
21164 }
21165 run_test 412 "mkdir on specific MDTs"
21166
21167 test_qos_mkdir() {
21168         local mkdir_cmd=$1
21169         local stripe_count=$2
21170         local mdts=$(comma_list $(mdts_nodes))
21171
21172         local testdir
21173         local lmv_qos_prio_free
21174         local lmv_qos_threshold_rr
21175         local lmv_qos_maxage
21176         local lod_qos_prio_free
21177         local lod_qos_threshold_rr
21178         local lod_qos_maxage
21179         local count
21180         local i
21181
21182         lmv_qos_prio_free=$($LCTL get_param -n lmv.*.qos_prio_free | head -n1)
21183         lmv_qos_prio_free=${lmv_qos_prio_free%%%}
21184         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
21185                 head -n1)
21186         lmv_qos_threshold_rr=${lmv_qos_threshold_rr%%%}
21187         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
21188         stack_trap "$LCTL set_param \
21189                 lmv.*.qos_prio_free=$lmv_qos_prio_free > /dev/null" EXIT
21190         stack_trap "$LCTL set_param \
21191                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null" EXIT
21192         stack_trap "$LCTL set_param \
21193                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null" EXIT
21194
21195         lod_qos_prio_free=$(do_facet mds1 $LCTL get_param -n \
21196                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_prio_free | head -n1)
21197         lod_qos_prio_free=${lod_qos_prio_free%%%}
21198         lod_qos_threshold_rr=$(do_facet mds1 $LCTL get_param -n \
21199                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_threshold_rr | head -n1)
21200         lod_qos_threshold_rr=${lod_qos_threshold_rr%%%}
21201         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
21202                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
21203         stack_trap "do_nodes $mdts $LCTL set_param \
21204                 lod.*.mdt_qos_prio_free=$lod_qos_prio_free > /dev/null" EXIT
21205         stack_trap "do_nodes $mdts $LCTL set_param \
21206                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null" \
21207                 EXIT
21208         stack_trap "do_nodes $mdts $LCTL set_param \
21209                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null" EXIT
21210
21211         echo
21212         echo "Mkdir (stripe_count $stripe_count) roundrobin:"
21213
21214         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
21215         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=100 > /dev/null
21216
21217         testdir=$DIR/$tdir-s$stripe_count/rr
21218
21219         for i in $(seq $((100 * MDSCOUNT))); do
21220                 eval $mkdir_cmd $testdir/subdir$i ||
21221                         error "$mkdir_cmd subdir$i failed"
21222         done
21223
21224         for i in $(seq $MDSCOUNT); do
21225                 count=$($LFS getdirstripe -i $testdir/* |
21226                                 grep ^$((i - 1))$ | wc -l)
21227                 echo "$count directories created on MDT$((i - 1))"
21228                 [ $count -eq 100 ] || error "subdirs are not evenly distributed"
21229
21230                 if [ $stripe_count -gt 1 ]; then
21231                         count=$($LFS getdirstripe $testdir/* |
21232                                 grep -P "^\s+$((i - 1))\t" | wc -l)
21233                         echo "$count stripes created on MDT$((i - 1))"
21234                         # deviation should < 5% of average
21235                         [ $count -lt $((95 * stripe_count)) ] ||
21236                         [ $count -gt $((105 * stripe_count)) ] &&
21237                                 error "stripes are not evenly distributed"
21238                 fi
21239         done
21240
21241         $LCTL set_param lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null
21242         do_nodes $mdts $LCTL set_param \
21243                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null
21244
21245         echo
21246         echo "Check for uneven MDTs: "
21247
21248         local ffree
21249         local bavail
21250         local max
21251         local min
21252         local max_index
21253         local min_index
21254         local tmp
21255
21256         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
21257         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
21258         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
21259
21260         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
21261         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
21262         max_index=0
21263         min_index=0
21264         for ((i = 1; i < ${#ffree[@]}; i++)); do
21265                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
21266                 if [ $tmp -gt $max ]; then
21267                         max=$tmp
21268                         max_index=$i
21269                 fi
21270                 if [ $tmp -lt $min ]; then
21271                         min=$tmp
21272                         min_index=$i
21273                 fi
21274         done
21275
21276         [ ${ffree[min_index]} -eq 0 ] &&
21277                 skip "no free files in MDT$min_index"
21278         [ ${ffree[min_index]} -gt 100000000 ] &&
21279                 skip "too much free files in MDT$min_index"
21280
21281         # Check if we need to generate uneven MDTs
21282         local threshold=50
21283         local diff=$(((max - min) * 100 / min))
21284         local value="$(generate_string 1024)"
21285
21286         while [ $diff -lt $threshold ]; do
21287                 # generate uneven MDTs, create till $threshold% diff
21288                 echo -n "weight diff=$diff% must be > $threshold% ..."
21289                 count=$((${ffree[min_index]} / 10))
21290                 # 50 sec per 10000 files in vm
21291                 [ $count -gt 40000 ] && [ "$SLOW" = "no" ] &&
21292                         skip "$count files to create"
21293                 echo "Fill MDT$min_index with $count files"
21294                 [ -d $DIR/$tdir-MDT$min_index ] ||
21295                         $LFS mkdir -i $min_index $DIR/$tdir-MDT$min_index ||
21296                         error "mkdir $tdir-MDT$min_index failed"
21297                 for i in $(seq $count); do
21298                         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE \
21299                                 $DIR/$tdir-MDT$min_index/f$j_$i > /dev/null ||
21300                                 error "create f$j_$i failed"
21301                         setfattr -n user.413b -v $value \
21302                                 $DIR/$tdir-MDT$min_index/f$j_$i ||
21303                                 error "setfattr f$j_$i failed"
21304                 done
21305
21306                 ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-*.filesfree))
21307                 bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-*.kbytesavail))
21308                 max=$(((${ffree[max_index]} >> 8) * \
21309                         (${bavail[max_index]} * bsize >> 16)))
21310                 min=$(((${ffree[min_index]} >> 8) * \
21311                         (${bavail[min_index]} * bsize >> 16)))
21312                 diff=$(((max - min) * 100 / min))
21313         done
21314
21315         echo "MDT filesfree available: ${ffree[@]}"
21316         echo "MDT blocks available: ${bavail[@]}"
21317         echo "weight diff=$diff%"
21318
21319         echo
21320         echo "Mkdir (stripe_count $stripe_count) with balanced space usage:"
21321
21322         $LCTL set_param lmv.*.qos_prio_free=100 > /dev/null
21323         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_prio_free=100 > /dev/null
21324         # decrease statfs age, so that it can be updated in time
21325         $LCTL set_param lmv.*.qos_maxage=1 > /dev/null
21326         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_maxage=1 > /dev/null
21327
21328         sleep 1
21329
21330         testdir=$DIR/$tdir-s$stripe_count/qos
21331
21332         for i in $(seq $((100 * MDSCOUNT))); do
21333                 eval $mkdir_cmd $testdir/subdir$i ||
21334                         error "$mkdir_cmd subdir$i failed"
21335         done
21336
21337         for i in $(seq $MDSCOUNT); do
21338                 count=$($LFS getdirstripe -i $testdir/* | grep ^$((i - 1))$ |
21339                         wc -l)
21340                 echo "$count directories created on MDT$((i - 1))"
21341
21342                 if [ $stripe_count -gt 1 ]; then
21343                         count=$($LFS getdirstripe $testdir/* |
21344                                 grep -P "^\s+$((i - 1))\t" | wc -l)
21345                         echo "$count stripes created on MDT$((i - 1))"
21346                 fi
21347         done
21348
21349         max=$($LFS getdirstripe -i $testdir/* | grep ^$max_index$ | wc -l)
21350         min=$($LFS getdirstripe -i $testdir/* | grep ^$min_index$ | wc -l)
21351
21352         # D-value should > 10% of averge
21353         [ $((max - min)) -lt 10 ] &&
21354                 error "subdirs shouldn't be evenly distributed"
21355
21356         # ditto
21357         if [ $stripe_count -gt 1 ]; then
21358                 max=$($LFS getdirstripe $testdir/* |
21359                         grep -P "^\s+$max_index\t" | wc -l)
21360                 min=$($LFS getdirstripe $testdir/* |
21361                         grep -P "^\s+$min_index\t" | wc -l)
21362                 [ $((max - min)) -le $((10 * stripe_count)) ] &&
21363                         error "stripes shouldn't be evenly distributed"|| true
21364         fi
21365 }
21366
21367 test_413a() {
21368         [ $MDSCOUNT -lt 2 ] &&
21369                 skip "We need at least 2 MDTs for this test"
21370
21371         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
21372                 skip "Need server version at least 2.12.52"
21373
21374         local stripe_count
21375
21376         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
21377                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
21378                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
21379                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
21380                 test_qos_mkdir "$LFS mkdir -c $stripe_count" $stripe_count
21381         done
21382 }
21383 run_test 413a "QoS mkdir with 'lfs mkdir -i -1'"
21384
21385 test_413b() {
21386         [ $MDSCOUNT -lt 2 ] &&
21387                 skip "We need at least 2 MDTs for this test"
21388
21389         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
21390                 skip "Need server version at least 2.12.52"
21391
21392         local stripe_count
21393
21394         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
21395                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
21396                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
21397                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
21398                 $LFS setdirstripe -D -c $stripe_count \
21399                         $DIR/$tdir-s$stripe_count/rr ||
21400                         error "setdirstripe failed"
21401                 $LFS setdirstripe -D -c $stripe_count \
21402                         $DIR/$tdir-s$stripe_count/qos ||
21403                         error "setdirstripe failed"
21404                 test_qos_mkdir "mkdir" $stripe_count
21405         done
21406 }
21407 run_test 413b "QoS mkdir under dir whose default LMV starting MDT offset is -1"
21408
21409 test_414() {
21410 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
21411         $LCTL set_param fail_loc=0x80000521
21412         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
21413         rm -f $DIR/$tfile
21414 }
21415 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
21416
21417 test_415() {
21418         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21419         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21420                 skip "Need server version at least 2.11.52"
21421
21422         # LU-11102
21423         local total
21424         local setattr_pid
21425         local start_time
21426         local end_time
21427         local duration
21428
21429         total=500
21430         # this test may be slow on ZFS
21431         [ "$mds1_FSTYPE" == "zfs" ] && total=100
21432
21433         # though this test is designed for striped directory, let's test normal
21434         # directory too since lock is always saved as CoS lock.
21435         test_mkdir $DIR/$tdir || error "mkdir $tdir"
21436         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
21437
21438         (
21439                 while true; do
21440                         touch $DIR/$tdir
21441                 done
21442         ) &
21443         setattr_pid=$!
21444
21445         start_time=$(date +%s)
21446         for i in $(seq $total); do
21447                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
21448                         > /dev/null
21449         done
21450         end_time=$(date +%s)
21451         duration=$((end_time - start_time))
21452
21453         kill -9 $setattr_pid
21454
21455         echo "rename $total files took $duration sec"
21456         [ $duration -lt 100 ] || error "rename took $duration sec"
21457 }
21458 run_test 415 "lock revoke is not missing"
21459
21460 test_416() {
21461         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
21462                 skip "Need server version at least 2.11.55"
21463
21464         # define OBD_FAIL_OSD_TXN_START    0x19a
21465         do_facet mds1 lctl set_param fail_loc=0x19a
21466
21467         lfs mkdir -c $MDSCOUNT $DIR/$tdir
21468
21469         true
21470 }
21471 run_test 416 "transaction start failure won't cause system hung"
21472
21473 cleanup_417() {
21474         trap 0
21475         do_nodes $(comma_list $(mdts_nodes)) \
21476                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
21477         do_nodes $(comma_list $(mdts_nodes)) \
21478                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
21479         do_nodes $(comma_list $(mdts_nodes)) \
21480                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
21481 }
21482
21483 test_417() {
21484         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21485         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
21486                 skip "Need MDS version at least 2.11.56"
21487
21488         trap cleanup_417 RETURN EXIT
21489
21490         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
21491         do_nodes $(comma_list $(mdts_nodes)) \
21492                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
21493         $LFS migrate -m 0 $DIR/$tdir.1 &&
21494                 error "migrate dir $tdir.1 should fail"
21495
21496         do_nodes $(comma_list $(mdts_nodes)) \
21497                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
21498         $LFS mkdir -i 1 $DIR/$tdir.2 &&
21499                 error "create remote dir $tdir.2 should fail"
21500
21501         do_nodes $(comma_list $(mdts_nodes)) \
21502                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
21503         $LFS mkdir -c 2 $DIR/$tdir.3 &&
21504                 error "create striped dir $tdir.3 should fail"
21505         true
21506 }
21507 run_test 417 "disable remote dir, striped dir and dir migration"
21508
21509 # Checks that the outputs of df [-i] and lfs df [-i] match
21510 #
21511 # usage: check_lfs_df <blocks | inodes> <mountpoint>
21512 check_lfs_df() {
21513         local dir=$2
21514         local inodes
21515         local df_out
21516         local lfs_df_out
21517         local count
21518         local passed=false
21519
21520         # blocks or inodes
21521         [ "$1" == "blocks" ] && inodes= || inodes="-i"
21522
21523         for count in {1..100}; do
21524                 cancel_lru_locks
21525                 sync; sleep 0.2
21526
21527                 # read the lines of interest
21528                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
21529                         error "df $inodes $dir | tail -n +2 failed"
21530                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
21531                         error "lfs df $inodes $dir | grep summary: failed"
21532
21533                 # skip first substrings of each output as they are different
21534                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
21535                 # compare the two outputs
21536                 passed=true
21537                 for i in {1..5}; do
21538                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
21539                 done
21540                 $passed && break
21541         done
21542
21543         if ! $passed; then
21544                 df -P $inodes $dir
21545                 echo
21546                 lfs df $inodes $dir
21547                 error "df and lfs df $1 output mismatch: "      \
21548                       "df ${inodes}: ${df_out[*]}, "            \
21549                       "lfs df ${inodes}: ${lfs_df_out[*]}"
21550         fi
21551 }
21552
21553 test_418() {
21554         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21555
21556         local dir=$DIR/$tdir
21557         local numfiles=$((RANDOM % 4096 + 2))
21558         local numblocks=$((RANDOM % 256 + 1))
21559
21560         wait_delete_completed
21561         test_mkdir $dir
21562
21563         # check block output
21564         check_lfs_df blocks $dir
21565         # check inode output
21566         check_lfs_df inodes $dir
21567
21568         # create a single file and retest
21569         echo "Creating a single file and testing"
21570         createmany -o $dir/$tfile- 1 &>/dev/null ||
21571                 error "creating 1 file in $dir failed"
21572         check_lfs_df blocks $dir
21573         check_lfs_df inodes $dir
21574
21575         # create a random number of files
21576         echo "Creating $((numfiles - 1)) files and testing"
21577         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
21578                 error "creating $((numfiles - 1)) files in $dir failed"
21579
21580         # write a random number of blocks to the first test file
21581         echo "Writing $numblocks 4K blocks and testing"
21582         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
21583                 count=$numblocks &>/dev/null ||
21584                 error "dd to $dir/${tfile}-0 failed"
21585
21586         # retest
21587         check_lfs_df blocks $dir
21588         check_lfs_df inodes $dir
21589
21590         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
21591                 error "unlinking $numfiles files in $dir failed"
21592 }
21593 run_test 418 "df and lfs df outputs match"
21594
21595 test_419()
21596 {
21597         local dir=$DIR/$tdir
21598
21599         mkdir -p $dir
21600         touch $dir/file
21601
21602         cancel_lru_locks mdc
21603
21604         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
21605         $LCTL set_param fail_loc=0x1410
21606         cat $dir/file
21607         $LCTL set_param fail_loc=0
21608         rm -rf $dir
21609 }
21610 run_test 419 "Verify open file by name doesn't crash kernel"
21611
21612 test_420()
21613 {
21614         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
21615                 skip "Need MDS version at least 2.12.53"
21616
21617         local SAVE_UMASK=$(umask)
21618         local dir=$DIR/$tdir
21619         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
21620
21621         mkdir -p $dir
21622         umask 0000
21623         mkdir -m03777 $dir/testdir
21624         ls -dn $dir/testdir
21625         # Need to remove trailing '.' when SELinux is enabled
21626         local dirperms=$(ls -dn $dir/testdir |
21627                          awk '{ sub(/\.$/, "", $1); print $1}')
21628         [ $dirperms == "drwxrwsrwt" ] ||
21629                 error "incorrect perms on $dir/testdir"
21630
21631         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
21632                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
21633         ls -n $dir/testdir/testfile
21634         local fileperms=$(ls -n $dir/testdir/testfile |
21635                           awk '{ sub(/\.$/, "", $1); print $1}')
21636         [ $fileperms == "-rwxr-xr-x" ] ||
21637                 error "incorrect perms on $dir/testdir/testfile"
21638
21639         umask $SAVE_UMASK
21640 }
21641 run_test 420 "clear SGID bit on non-directories for non-members"
21642
21643 test_421a() {
21644         local cnt
21645         local fid1
21646         local fid2
21647
21648         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21649                 skip "Need MDS version at least 2.12.54"
21650
21651         test_mkdir $DIR/$tdir
21652         createmany -o $DIR/$tdir/f 3
21653         cnt=$(ls -1 $DIR/$tdir | wc -l)
21654         [ $cnt != 3 ] && error "unexpected #files: $cnt"
21655
21656         fid1=$(lfs path2fid $DIR/$tdir/f1)
21657         fid2=$(lfs path2fid $DIR/$tdir/f2)
21658         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
21659
21660         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
21661         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
21662
21663         cnt=$(ls -1 $DIR/$tdir | wc -l)
21664         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
21665
21666         rm -f $DIR/$tdir/f3 || error "can't remove f3"
21667         createmany -o $DIR/$tdir/f 3
21668         cnt=$(ls -1 $DIR/$tdir | wc -l)
21669         [ $cnt != 3 ] && error "unexpected #files: $cnt"
21670
21671         fid1=$(lfs path2fid $DIR/$tdir/f1)
21672         fid2=$(lfs path2fid $DIR/$tdir/f2)
21673         echo "remove using fsname $FSNAME"
21674         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
21675
21676         cnt=$(ls -1 $DIR/$tdir | wc -l)
21677         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
21678 }
21679 run_test 421a "simple rm by fid"
21680
21681 test_421b() {
21682         local cnt
21683         local FID1
21684         local FID2
21685
21686         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21687                 skip "Need MDS version at least 2.12.54"
21688
21689         test_mkdir $DIR/$tdir
21690         createmany -o $DIR/$tdir/f 3
21691         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
21692         MULTIPID=$!
21693
21694         FID1=$(lfs path2fid $DIR/$tdir/f1)
21695         FID2=$(lfs path2fid $DIR/$tdir/f2)
21696         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
21697
21698         kill -USR1 $MULTIPID
21699         wait
21700
21701         cnt=$(ls $DIR/$tdir | wc -l)
21702         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
21703 }
21704 run_test 421b "rm by fid on open file"
21705
21706 test_421c() {
21707         local cnt
21708         local FIDS
21709
21710         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21711                 skip "Need MDS version at least 2.12.54"
21712
21713         test_mkdir $DIR/$tdir
21714         createmany -o $DIR/$tdir/f 3
21715         touch $DIR/$tdir/$tfile
21716         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
21717         cnt=$(ls -1 $DIR/$tdir | wc -l)
21718         [ $cnt != 184 ] && error "unexpected #files: $cnt"
21719
21720         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
21721         $LFS rmfid $DIR $FID1 || error "rmfid failed"
21722
21723         cnt=$(ls $DIR/$tdir | wc -l)
21724         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
21725 }
21726 run_test 421c "rm by fid against hardlinked files"
21727
21728 test_421d() {
21729         local cnt
21730         local FIDS
21731
21732         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21733                 skip "Need MDS version at least 2.12.54"
21734
21735         test_mkdir $DIR/$tdir
21736         createmany -o $DIR/$tdir/f 4097
21737         cnt=$(ls -1 $DIR/$tdir | wc -l)
21738         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
21739
21740         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
21741         $LFS rmfid $DIR $FIDS || error "rmfid failed"
21742
21743         cnt=$(ls $DIR/$tdir | wc -l)
21744         rm -rf $DIR/$tdir
21745         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21746 }
21747 run_test 421d "rmfid en masse"
21748
21749 test_421e() {
21750         local cnt
21751         local FID
21752
21753         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21754         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21755                 skip "Need MDS version at least 2.12.54"
21756
21757         mkdir -p $DIR/$tdir
21758         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
21759         createmany -o $DIR/$tdir/striped_dir/f 512
21760         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21761         [ $cnt != 512 ] && error "unexpected #files: $cnt"
21762
21763         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
21764                 sed "s/[/][^:]*://g")
21765         $LFS rmfid $DIR $FIDS || error "rmfid failed"
21766
21767         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
21768         rm -rf $DIR/$tdir
21769         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21770 }
21771 run_test 421e "rmfid in DNE"
21772
21773 test_421f() {
21774         local cnt
21775         local FID
21776
21777         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21778                 skip "Need MDS version at least 2.12.54"
21779
21780         test_mkdir $DIR/$tdir
21781         touch $DIR/$tdir/f
21782         cnt=$(ls -1 $DIR/$tdir | wc -l)
21783         [ $cnt != 1 ] && error "unexpected #files: $cnt"
21784
21785         FID=$(lfs path2fid $DIR/$tdir/f)
21786         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
21787         # rmfid should fail
21788         cnt=$(ls -1 $DIR/$tdir | wc -l)
21789         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
21790
21791         chmod a+rw $DIR/$tdir
21792         ls -la $DIR/$tdir
21793         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
21794         # rmfid should fail
21795         cnt=$(ls -1 $DIR/$tdir | wc -l)
21796         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
21797
21798         rm -f $DIR/$tdir/f
21799         $RUNAS touch $DIR/$tdir/f
21800         FID=$(lfs path2fid $DIR/$tdir/f)
21801         echo "rmfid as root"
21802         $LFS rmfid $DIR $FID || error "rmfid as root failed"
21803         cnt=$(ls -1 $DIR/$tdir | wc -l)
21804         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
21805
21806         rm -f $DIR/$tdir/f
21807         $RUNAS touch $DIR/$tdir/f
21808         cnt=$(ls -1 $DIR/$tdir | wc -l)
21809         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
21810         FID=$(lfs path2fid $DIR/$tdir/f)
21811         # rmfid w/o user_fid2path mount option should fail
21812         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
21813         cnt=$(ls -1 $DIR/$tdir | wc -l)
21814         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
21815
21816         umount_client $MOUNT || error "failed to umount client"
21817         mount_client $MOUNT "$MOUNT_OPTS,user_fid2path" ||
21818                 error "failed to mount client'"
21819
21820         $RUNAS $LFS rmfid $DIR $FID || error "rmfid failed"
21821         # rmfid should succeed
21822         cnt=$(ls -1 $DIR/$tdir | wc -l)
21823         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
21824
21825         # rmfid shouldn't allow to remove files due to dir's permission
21826         chmod a+rwx $DIR/$tdir
21827         touch $DIR/$tdir/f
21828         ls -la $DIR/$tdir
21829         FID=$(lfs path2fid $DIR/$tdir/f)
21830         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail"
21831
21832         umount_client $MOUNT || error "failed to umount client"
21833         mount_client $MOUNT "$MOUNT_OPTS" ||
21834                 error "failed to mount client'"
21835
21836 }
21837 run_test 421f "rmfid checks permissions"
21838
21839 test_421g() {
21840         local cnt
21841         local FIDS
21842
21843         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21844         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21845                 skip "Need MDS version at least 2.12.54"
21846
21847         mkdir -p $DIR/$tdir
21848         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
21849         createmany -o $DIR/$tdir/striped_dir/f 512
21850         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21851         [ $cnt != 512 ] && error "unexpected #files: $cnt"
21852
21853         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
21854                 sed "s/[/][^:]*://g")
21855
21856         rm -f $DIR/$tdir/striped_dir/f1*
21857         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21858         removed=$((512 - cnt))
21859
21860         # few files have been just removed, so we expect
21861         # rmfid to fail on their fids
21862         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
21863         [ $removed != $errors ] && error "$errors != $removed"
21864
21865         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
21866         rm -rf $DIR/$tdir
21867         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21868 }
21869 run_test 421g "rmfid to return errors properly"
21870
21871 test_422() {
21872         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d1
21873         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d2
21874         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d3
21875         dd if=/dev/zero of=$DIR/$tdir/d1/file1 bs=1k count=1
21876         dd if=/dev/zero of=$DIR/$tdir/d2/file1 bs=1k count=1
21877
21878         local amc=$(at_max_get client)
21879         local amo=$(at_max_get mds1)
21880         local timeout=`lctl get_param -n timeout`
21881
21882         at_max_set 0 client
21883         at_max_set 0 mds1
21884
21885 #define OBD_FAIL_PTLRPC_PAUSE_REQ        0x50a
21886         do_facet mds1 $LCTL set_param fail_loc=0x8000050a \
21887                         fail_val=$(((2*timeout + 10)*1000))
21888         touch $DIR/$tdir/d3/file &
21889         sleep 2
21890 #define OBD_FAIL_TGT_REPLY_DATA_RACE     0x722
21891         do_facet mds1 $LCTL set_param fail_loc=0x80000722 \
21892                         fail_val=$((2*timeout + 5))
21893         mv $DIR/$tdir/d1/file1 $DIR/$tdir/d1/file2 &
21894         local pid=$!
21895         sleep 1
21896         kill -9 $pid
21897         sleep $((2 * timeout))
21898         echo kill $pid
21899         kill -9 $pid
21900         lctl mark touch
21901         touch $DIR/$tdir/d2/file3
21902         touch $DIR/$tdir/d2/file4
21903         touch $DIR/$tdir/d2/file5
21904
21905         wait
21906         at_max_set $amc client
21907         at_max_set $amo mds1
21908
21909         # LU-12838 - verify the ptlrpc thread watchdog is not always throttled
21910         do_facet mds1 "dmesg | grep 'Dumping the stack trace for debugging'" ||
21911                 error "Watchdog is always throttled"
21912 }
21913 run_test 422 "kill a process with RPC in progress"
21914
21915 prep_801() {
21916         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
21917         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
21918                 skip "Need server version at least 2.9.55"
21919
21920         start_full_debug_logging
21921 }
21922
21923 post_801() {
21924         stop_full_debug_logging
21925 }
21926
21927 barrier_stat() {
21928         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
21929                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
21930                            awk '/The barrier for/ { print $7 }')
21931                 echo $st
21932         else
21933                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
21934                 echo \'$st\'
21935         fi
21936 }
21937
21938 barrier_expired() {
21939         local expired
21940
21941         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
21942                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
21943                           awk '/will be expired/ { print $7 }')
21944         else
21945                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
21946         fi
21947
21948         echo $expired
21949 }
21950
21951 test_801a() {
21952         prep_801
21953
21954         echo "Start barrier_freeze at: $(date)"
21955         #define OBD_FAIL_BARRIER_DELAY          0x2202
21956         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
21957         # Do not reduce barrier time - See LU-11873
21958         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
21959
21960         sleep 2
21961         local b_status=$(barrier_stat)
21962         echo "Got barrier status at: $(date)"
21963         [ "$b_status" = "'freezing_p1'" ] ||
21964                 error "(1) unexpected barrier status $b_status"
21965
21966         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
21967         wait
21968         b_status=$(barrier_stat)
21969         [ "$b_status" = "'frozen'" ] ||
21970                 error "(2) unexpected barrier status $b_status"
21971
21972         local expired=$(barrier_expired)
21973         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
21974         sleep $((expired + 3))
21975
21976         b_status=$(barrier_stat)
21977         [ "$b_status" = "'expired'" ] ||
21978                 error "(3) unexpected barrier status $b_status"
21979
21980         # Do not reduce barrier time - See LU-11873
21981         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
21982                 error "(4) fail to freeze barrier"
21983
21984         b_status=$(barrier_stat)
21985         [ "$b_status" = "'frozen'" ] ||
21986                 error "(5) unexpected barrier status $b_status"
21987
21988         echo "Start barrier_thaw at: $(date)"
21989         #define OBD_FAIL_BARRIER_DELAY          0x2202
21990         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
21991         do_facet mgs $LCTL barrier_thaw $FSNAME &
21992
21993         sleep 2
21994         b_status=$(barrier_stat)
21995         echo "Got barrier status at: $(date)"
21996         [ "$b_status" = "'thawing'" ] ||
21997                 error "(6) unexpected barrier status $b_status"
21998
21999         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
22000         wait
22001         b_status=$(barrier_stat)
22002         [ "$b_status" = "'thawed'" ] ||
22003                 error "(7) unexpected barrier status $b_status"
22004
22005         #define OBD_FAIL_BARRIER_FAILURE        0x2203
22006         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
22007         do_facet mgs $LCTL barrier_freeze $FSNAME
22008
22009         b_status=$(barrier_stat)
22010         [ "$b_status" = "'failed'" ] ||
22011                 error "(8) unexpected barrier status $b_status"
22012
22013         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
22014         do_facet mgs $LCTL barrier_thaw $FSNAME
22015
22016         post_801
22017 }
22018 run_test 801a "write barrier user interfaces and stat machine"
22019
22020 test_801b() {
22021         prep_801
22022
22023         mkdir $DIR/$tdir || error "(1) fail to mkdir"
22024         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
22025         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
22026         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
22027         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
22028
22029         cancel_lru_locks mdc
22030
22031         # 180 seconds should be long enough
22032         do_facet mgs $LCTL barrier_freeze $FSNAME 180
22033
22034         local b_status=$(barrier_stat)
22035         [ "$b_status" = "'frozen'" ] ||
22036                 error "(6) unexpected barrier status $b_status"
22037
22038         mkdir $DIR/$tdir/d0/d10 &
22039         mkdir_pid=$!
22040
22041         touch $DIR/$tdir/d1/f13 &
22042         touch_pid=$!
22043
22044         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
22045         ln_pid=$!
22046
22047         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
22048         mv_pid=$!
22049
22050         rm -f $DIR/$tdir/d4/f12 &
22051         rm_pid=$!
22052
22053         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
22054
22055         # To guarantee taht the 'stat' is not blocked
22056         b_status=$(barrier_stat)
22057         [ "$b_status" = "'frozen'" ] ||
22058                 error "(8) unexpected barrier status $b_status"
22059
22060         # let above commands to run at background
22061         sleep 5
22062
22063         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
22064         ps -p $touch_pid || error "(10) touch should be blocked"
22065         ps -p $ln_pid || error "(11) link should be blocked"
22066         ps -p $mv_pid || error "(12) rename should be blocked"
22067         ps -p $rm_pid || error "(13) unlink should be blocked"
22068
22069         b_status=$(barrier_stat)
22070         [ "$b_status" = "'frozen'" ] ||
22071                 error "(14) unexpected barrier status $b_status"
22072
22073         do_facet mgs $LCTL barrier_thaw $FSNAME
22074         b_status=$(barrier_stat)
22075         [ "$b_status" = "'thawed'" ] ||
22076                 error "(15) unexpected barrier status $b_status"
22077
22078         wait $mkdir_pid || error "(16) mkdir should succeed"
22079         wait $touch_pid || error "(17) touch should succeed"
22080         wait $ln_pid || error "(18) link should succeed"
22081         wait $mv_pid || error "(19) rename should succeed"
22082         wait $rm_pid || error "(20) unlink should succeed"
22083
22084         post_801
22085 }
22086 run_test 801b "modification will be blocked by write barrier"
22087
22088 test_801c() {
22089         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22090
22091         prep_801
22092
22093         stop mds2 || error "(1) Fail to stop mds2"
22094
22095         do_facet mgs $LCTL barrier_freeze $FSNAME 30
22096
22097         local b_status=$(barrier_stat)
22098         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
22099                 do_facet mgs $LCTL barrier_thaw $FSNAME
22100                 error "(2) unexpected barrier status $b_status"
22101         }
22102
22103         do_facet mgs $LCTL barrier_rescan $FSNAME ||
22104                 error "(3) Fail to rescan barrier bitmap"
22105
22106         # Do not reduce barrier time - See LU-11873
22107         do_facet mgs $LCTL barrier_freeze $FSNAME 20
22108
22109         b_status=$(barrier_stat)
22110         [ "$b_status" = "'frozen'" ] ||
22111                 error "(4) unexpected barrier status $b_status"
22112
22113         do_facet mgs $LCTL barrier_thaw $FSNAME
22114         b_status=$(barrier_stat)
22115         [ "$b_status" = "'thawed'" ] ||
22116                 error "(5) unexpected barrier status $b_status"
22117
22118         local devname=$(mdsdevname 2)
22119
22120         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
22121
22122         do_facet mgs $LCTL barrier_rescan $FSNAME ||
22123                 error "(7) Fail to rescan barrier bitmap"
22124
22125         post_801
22126 }
22127 run_test 801c "rescan barrier bitmap"
22128
22129 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
22130 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
22131 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
22132 saved_MOUNT_OPTS=$MOUNT_OPTS
22133
22134 cleanup_802a() {
22135         trap 0
22136
22137         stopall
22138         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
22139         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
22140         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
22141         MOUNT_OPTS=$saved_MOUNT_OPTS
22142         setupall
22143 }
22144
22145 test_802a() {
22146         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
22147         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
22148         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
22149                 skip "Need server version at least 2.9.55"
22150
22151         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
22152
22153         mkdir $DIR/$tdir || error "(1) fail to mkdir"
22154
22155         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
22156                 error "(2) Fail to copy"
22157
22158         trap cleanup_802a EXIT
22159
22160         # sync by force before remount as readonly
22161         sync; sync_all_data; sleep 3; sync_all_data
22162
22163         stopall
22164
22165         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
22166         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
22167         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
22168
22169         echo "Mount the server as read only"
22170         setupall server_only || error "(3) Fail to start servers"
22171
22172         echo "Mount client without ro should fail"
22173         mount_client $MOUNT &&
22174                 error "(4) Mount client without 'ro' should fail"
22175
22176         echo "Mount client with ro should succeed"
22177         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
22178         mount_client $MOUNT ||
22179                 error "(5) Mount client with 'ro' should succeed"
22180
22181         echo "Modify should be refused"
22182         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
22183
22184         echo "Read should be allowed"
22185         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
22186                 error "(7) Read should succeed under ro mode"
22187
22188         cleanup_802a
22189 }
22190 run_test 802a "simulate readonly device"
22191
22192 test_802b() {
22193         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22194         remote_mds_nodsh && skip "remote MDS with nodsh"
22195
22196         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
22197                 skip "readonly option not available"
22198
22199         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
22200
22201         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
22202                 error "(2) Fail to copy"
22203
22204         # write back all cached data before setting MDT to readonly
22205         cancel_lru_locks
22206         sync_all_data
22207
22208         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
22209         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
22210
22211         echo "Modify should be refused"
22212         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
22213
22214         echo "Read should be allowed"
22215         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
22216                 error "(7) Read should succeed under ro mode"
22217
22218         # disable readonly
22219         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
22220 }
22221 run_test 802b "be able to set MDTs to readonly"
22222
22223 test_803() {
22224         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22225         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
22226                 skip "MDS needs to be newer than 2.10.54"
22227
22228         mkdir -p $DIR/$tdir
22229         # Create some objects on all MDTs to trigger related logs objects
22230         for idx in $(seq $MDSCOUNT); do
22231                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
22232                         $DIR/$tdir/dir${idx} ||
22233                         error "Fail to create $DIR/$tdir/dir${idx}"
22234         done
22235
22236         sync; sleep 3
22237         wait_delete_completed # ensure old test cleanups are finished
22238         echo "before create:"
22239         $LFS df -i $MOUNT
22240         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
22241
22242         for i in {1..10}; do
22243                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
22244                         error "Fail to create $DIR/$tdir/foo$i"
22245         done
22246
22247         sync; sleep 3
22248         echo "after create:"
22249         $LFS df -i $MOUNT
22250         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
22251
22252         # allow for an llog to be cleaned up during the test
22253         [ $after_used -ge $((before_used + 10 - 1)) ] ||
22254                 error "before ($before_used) + 10 > after ($after_used)"
22255
22256         for i in {1..10}; do
22257                 rm -rf $DIR/$tdir/foo$i ||
22258                         error "Fail to remove $DIR/$tdir/foo$i"
22259         done
22260
22261         sleep 3 # avoid MDT return cached statfs
22262         wait_delete_completed
22263         echo "after unlink:"
22264         $LFS df -i $MOUNT
22265         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
22266
22267         # allow for an llog to be created during the test
22268         [ $after_used -le $((before_used + 1)) ] ||
22269                 error "after ($after_used) > before ($before_used) + 1"
22270 }
22271 run_test 803 "verify agent object for remote object"
22272
22273 test_804() {
22274         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22275         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
22276                 skip "MDS needs to be newer than 2.10.54"
22277         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
22278
22279         mkdir -p $DIR/$tdir
22280         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
22281                 error "Fail to create $DIR/$tdir/dir0"
22282
22283         local fid=$($LFS path2fid $DIR/$tdir/dir0)
22284         local dev=$(mdsdevname 2)
22285
22286         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22287                 grep ${fid} || error "NOT found agent entry for dir0"
22288
22289         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
22290                 error "Fail to create $DIR/$tdir/dir1"
22291
22292         touch $DIR/$tdir/dir1/foo0 ||
22293                 error "Fail to create $DIR/$tdir/dir1/foo0"
22294         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
22295         local rc=0
22296
22297         for idx in $(seq $MDSCOUNT); do
22298                 dev=$(mdsdevname $idx)
22299                 do_facet mds${idx} \
22300                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22301                         grep ${fid} && rc=$idx
22302         done
22303
22304         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
22305                 error "Fail to rename foo0 to foo1"
22306         if [ $rc -eq 0 ]; then
22307                 for idx in $(seq $MDSCOUNT); do
22308                         dev=$(mdsdevname $idx)
22309                         do_facet mds${idx} \
22310                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22311                         grep ${fid} && rc=$idx
22312                 done
22313         fi
22314
22315         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
22316                 error "Fail to rename foo1 to foo2"
22317         if [ $rc -eq 0 ]; then
22318                 for idx in $(seq $MDSCOUNT); do
22319                         dev=$(mdsdevname $idx)
22320                         do_facet mds${idx} \
22321                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22322                         grep ${fid} && rc=$idx
22323                 done
22324         fi
22325
22326         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
22327
22328         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
22329                 error "Fail to link to $DIR/$tdir/dir1/foo2"
22330         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
22331                 error "Fail to rename foo2 to foo0"
22332         unlink $DIR/$tdir/dir1/foo0 ||
22333                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
22334         rm -rf $DIR/$tdir/dir0 ||
22335                 error "Fail to rm $DIR/$tdir/dir0"
22336
22337         for idx in $(seq $MDSCOUNT); do
22338                 dev=$(mdsdevname $idx)
22339                 rc=0
22340
22341                 stop mds${idx}
22342                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
22343                         rc=$?
22344                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
22345                         error "mount mds$idx failed"
22346                 df $MOUNT > /dev/null 2>&1
22347
22348                 # e2fsck should not return error
22349                 [ $rc -eq 0 ] ||
22350                         error "e2fsck detected error on MDT${idx}: rc=$rc"
22351         done
22352 }
22353 run_test 804 "verify agent entry for remote entry"
22354
22355 cleanup_805() {
22356         do_facet $SINGLEMDS zfs set quota=$old $fsset
22357         unlinkmany $DIR/$tdir/f- 1000000
22358         trap 0
22359 }
22360
22361 test_805() {
22362         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
22363         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
22364         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
22365                 skip "netfree not implemented before 0.7"
22366         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
22367                 skip "Need MDS version at least 2.10.57"
22368
22369         local fsset
22370         local freekb
22371         local usedkb
22372         local old
22373         local quota
22374         local pref="osd-zfs.$FSNAME-MDT0000."
22375
22376         # limit available space on MDS dataset to meet nospace issue
22377         # quickly. then ZFS 0.7.2 can use reserved space if asked
22378         # properly (using netfree flag in osd_declare_destroy()
22379         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
22380         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
22381                 gawk '{print $3}')
22382         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
22383         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
22384         let "usedkb=usedkb-freekb"
22385         let "freekb=freekb/2"
22386         if let "freekb > 5000"; then
22387                 let "freekb=5000"
22388         fi
22389         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
22390         trap cleanup_805 EXIT
22391         mkdir $DIR/$tdir
22392         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
22393         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
22394         rm -rf $DIR/$tdir || error "not able to remove"
22395         do_facet $SINGLEMDS zfs set quota=$old $fsset
22396         trap 0
22397 }
22398 run_test 805 "ZFS can remove from full fs"
22399
22400 # Size-on-MDS test
22401 check_lsom_data()
22402 {
22403         local file=$1
22404         local size=$($LFS getsom -s $file)
22405         local expect=$(stat -c %s $file)
22406
22407         [[ $size == $expect ]] ||
22408                 error "$file expected size: $expect, got: $size"
22409
22410         local blocks=$($LFS getsom -b $file)
22411         expect=$(stat -c %b $file)
22412         [[ $blocks == $expect ]] ||
22413                 error "$file expected blocks: $expect, got: $blocks"
22414 }
22415
22416 check_lsom_size()
22417 {
22418         local size=$($LFS getsom -s $1)
22419         local expect=$2
22420
22421         [[ $size == $expect ]] ||
22422                 error "$file expected size: $expect, got: $size"
22423 }
22424
22425 test_806() {
22426         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
22427                 skip "Need MDS version at least 2.11.52"
22428
22429         local bs=1048576
22430
22431         touch $DIR/$tfile || error "touch $tfile failed"
22432
22433         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
22434         save_lustre_params client "llite.*.xattr_cache" > $save
22435         lctl set_param llite.*.xattr_cache=0
22436         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
22437
22438         # single-threaded write
22439         echo "Test SOM for single-threaded write"
22440         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
22441                 error "write $tfile failed"
22442         check_lsom_size $DIR/$tfile $bs
22443
22444         local num=32
22445         local size=$(($num * $bs))
22446         local offset=0
22447         local i
22448
22449         echo "Test SOM for single client multi-threaded($num) write"
22450         $TRUNCATE $DIR/$tfile 0
22451         for ((i = 0; i < $num; i++)); do
22452                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22453                 local pids[$i]=$!
22454                 offset=$((offset + $bs))
22455         done
22456         for (( i=0; i < $num; i++ )); do
22457                 wait ${pids[$i]}
22458         done
22459         check_lsom_size $DIR/$tfile $size
22460
22461         $TRUNCATE $DIR/$tfile 0
22462         for ((i = 0; i < $num; i++)); do
22463                 offset=$((offset - $bs))
22464                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22465                 local pids[$i]=$!
22466         done
22467         for (( i=0; i < $num; i++ )); do
22468                 wait ${pids[$i]}
22469         done
22470         check_lsom_size $DIR/$tfile $size
22471
22472         # multi-client writes
22473         num=$(get_node_count ${CLIENTS//,/ })
22474         size=$(($num * $bs))
22475         offset=0
22476         i=0
22477
22478         echo "Test SOM for multi-client ($num) writes"
22479         $TRUNCATE $DIR/$tfile 0
22480         for client in ${CLIENTS//,/ }; do
22481                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22482                 local pids[$i]=$!
22483                 i=$((i + 1))
22484                 offset=$((offset + $bs))
22485         done
22486         for (( i=0; i < $num; i++ )); do
22487                 wait ${pids[$i]}
22488         done
22489         check_lsom_size $DIR/$tfile $offset
22490
22491         i=0
22492         $TRUNCATE $DIR/$tfile 0
22493         for client in ${CLIENTS//,/ }; do
22494                 offset=$((offset - $bs))
22495                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22496                 local pids[$i]=$!
22497                 i=$((i + 1))
22498         done
22499         for (( i=0; i < $num; i++ )); do
22500                 wait ${pids[$i]}
22501         done
22502         check_lsom_size $DIR/$tfile $size
22503
22504         # verify truncate
22505         echo "Test SOM for truncate"
22506         $TRUNCATE $DIR/$tfile 1048576
22507         check_lsom_size $DIR/$tfile 1048576
22508         $TRUNCATE $DIR/$tfile 1234
22509         check_lsom_size $DIR/$tfile 1234
22510
22511         # verify SOM blocks count
22512         echo "Verify SOM block count"
22513         $TRUNCATE $DIR/$tfile 0
22514         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
22515                 error "failed to write file $tfile"
22516         check_lsom_data $DIR/$tfile
22517 }
22518 run_test 806 "Verify Lazy Size on MDS"
22519
22520 test_807() {
22521         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
22522         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
22523                 skip "Need MDS version at least 2.11.52"
22524
22525         # Registration step
22526         changelog_register || error "changelog_register failed"
22527         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
22528         changelog_users $SINGLEMDS | grep -q $cl_user ||
22529                 error "User $cl_user not found in changelog_users"
22530
22531         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
22532         save_lustre_params client "llite.*.xattr_cache" > $save
22533         lctl set_param llite.*.xattr_cache=0
22534         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
22535
22536         rm -rf $DIR/$tdir || error "rm $tdir failed"
22537         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
22538         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
22539         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
22540         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
22541                 error "truncate $tdir/trunc failed"
22542
22543         local bs=1048576
22544         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 ||
22545                 error "write $tfile failed"
22546
22547         # multi-client wirtes
22548         local num=$(get_node_count ${CLIENTS//,/ })
22549         local offset=0
22550         local i=0
22551
22552         echo "Test SOM for multi-client ($num) writes"
22553         touch $DIR/$tfile || error "touch $tfile failed"
22554         $TRUNCATE $DIR/$tfile 0
22555         for client in ${CLIENTS//,/ }; do
22556                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22557                 local pids[$i]=$!
22558                 i=$((i + 1))
22559                 offset=$((offset + $bs))
22560         done
22561         for (( i=0; i < $num; i++ )); do
22562                 wait ${pids[$i]}
22563         done
22564
22565         sleep 5
22566         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
22567         check_lsom_data $DIR/$tdir/trunc
22568         check_lsom_data $DIR/$tdir/single_dd
22569         check_lsom_data $DIR/$tfile
22570
22571         rm -rf $DIR/$tdir
22572         # Deregistration step
22573         changelog_deregister || error "changelog_deregister failed"
22574 }
22575 run_test 807 "verify LSOM syncing tool"
22576
22577 check_som_nologged()
22578 {
22579         local lines=$($LFS changelog $FSNAME-MDT0000 |
22580                 grep 'x=trusted.som' | wc -l)
22581         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
22582 }
22583
22584 test_808() {
22585         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
22586                 skip "Need MDS version at least 2.11.55"
22587
22588         # Registration step
22589         changelog_register || error "changelog_register failed"
22590
22591         touch $DIR/$tfile || error "touch $tfile failed"
22592         check_som_nologged
22593
22594         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
22595                 error "write $tfile failed"
22596         check_som_nologged
22597
22598         $TRUNCATE $DIR/$tfile 1234
22599         check_som_nologged
22600
22601         $TRUNCATE $DIR/$tfile 1048576
22602         check_som_nologged
22603
22604         # Deregistration step
22605         changelog_deregister || error "changelog_deregister failed"
22606 }
22607 run_test 808 "Check trusted.som xattr not logged in Changelogs"
22608
22609 check_som_nodata()
22610 {
22611         $LFS getsom $1
22612         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
22613 }
22614
22615 test_809() {
22616         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
22617                 skip "Need MDS version at least 2.11.56"
22618
22619         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
22620                 error "failed to create DoM-only file $DIR/$tfile"
22621         touch $DIR/$tfile || error "touch $tfile failed"
22622         check_som_nodata $DIR/$tfile
22623
22624         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
22625                 error "write $tfile failed"
22626         check_som_nodata $DIR/$tfile
22627
22628         $TRUNCATE $DIR/$tfile 1234
22629         check_som_nodata $DIR/$tfile
22630
22631         $TRUNCATE $DIR/$tfile 4097
22632         check_som_nodata $DIR/$file
22633 }
22634 run_test 809 "Verify no SOM xattr store for DoM-only files"
22635
22636 test_810() {
22637         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22638         $GSS && skip_env "could not run with gss"
22639         [[ $OST1_VERSION -gt $(version_code 2.12.58) ]] ||
22640                 skip "OST < 2.12.58 doesn't align checksum"
22641
22642         set_checksums 1
22643         stack_trap "set_checksums $ORIG_CSUM" EXIT
22644         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
22645
22646         local csum
22647         local before
22648         local after
22649         for csum in $CKSUM_TYPES; do
22650                 #define OBD_FAIL_OSC_NO_GRANT   0x411
22651                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
22652                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
22653                         eval set -- $i
22654                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
22655                         before=$(md5sum $DIR/$tfile)
22656                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
22657                         after=$(md5sum $DIR/$tfile)
22658                         [ "$before" == "$after" ] ||
22659                                 error "$csum: $before != $after bs=$1 seek=$2"
22660                 done
22661         done
22662 }
22663 run_test 810 "partial page writes on ZFS (LU-11663)"
22664
22665 test_811() {
22666         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
22667                 skip "Need MDS version at least 2.11.56"
22668
22669         #define OBD_FAIL_MDS_ORPHAN_DELETE      0x165
22670         do_facet mds1 $LCTL set_param fail_loc=0x165
22671         $MULTIOP $DIR/$tfile Ouc || error "multiop failed"
22672
22673         stop mds1
22674         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
22675
22676         wait_update_facet mds1 "pgrep orph_.*-MDD | wc -l" "0" ||
22677                 error "MDD orphan cleanup thread not quit"
22678 }
22679 run_test 811 "orphan name stub can be cleaned up in startup"
22680
22681 test_812a() {
22682         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
22683                 skip "OST < 2.12.51 doesn't support this fail_loc"
22684         [ "$SHARED_KEY" = true ] &&
22685                 skip "OSC connections never go IDLE with Shared-Keys enabled"
22686
22687         $LFS setstripe -c 1 -i 0 $DIR/$tfile
22688         # ensure ost1 is connected
22689         stat $DIR/$tfile >/dev/null || error "can't stat"
22690         wait_osc_import_state client ost1 FULL
22691         # no locks, no reqs to let the connection idle
22692         cancel_lru_locks osc
22693
22694         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
22695 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
22696         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
22697         wait_osc_import_state client ost1 CONNECTING
22698         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
22699
22700         stat $DIR/$tfile >/dev/null || error "can't stat file"
22701 }
22702 run_test 812a "do not drop reqs generated when imp is going to idle (LU-11951)"
22703
22704 test_812b() { # LU-12378
22705         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
22706                 skip "OST < 2.12.51 doesn't support this fail_loc"
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 || error "setstripe failed"
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
22717         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
22718 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
22719         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
22720         wait_osc_import_state client ost1 CONNECTING
22721         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
22722
22723         $LFS quota -u 0 $DIR/ || error "lfs quota should succeed"
22724         wait_osc_import_state client ost1 IDLE
22725 }
22726 run_test 812b "do not drop no resend request for idle connect"
22727
22728 test_813() {
22729         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
22730         [ -z "$file_heat_sav" ] && skip "no file heat support"
22731
22732         local readsample
22733         local writesample
22734         local readbyte
22735         local writebyte
22736         local readsample1
22737         local writesample1
22738         local readbyte1
22739         local writebyte1
22740
22741         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
22742         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
22743
22744         $LCTL set_param -n llite.*.file_heat=1
22745         echo "Turn on file heat"
22746         echo "Period second: $period_second, Decay percentage: $decay_pct"
22747
22748         echo "QQQQ" > $DIR/$tfile
22749         echo "QQQQ" > $DIR/$tfile
22750         echo "QQQQ" > $DIR/$tfile
22751         cat $DIR/$tfile > /dev/null
22752         cat $DIR/$tfile > /dev/null
22753         cat $DIR/$tfile > /dev/null
22754         cat $DIR/$tfile > /dev/null
22755
22756         local out=$($LFS heat_get $DIR/$tfile)
22757
22758         $LFS heat_get $DIR/$tfile
22759         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22760         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22761         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22762         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22763
22764         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
22765         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
22766         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
22767         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
22768
22769         sleep $((period_second + 3))
22770         echo "Sleep $((period_second + 3)) seconds..."
22771         # The recursion formula to calculate the heat of the file f is as
22772         # follow:
22773         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
22774         # Where Hi is the heat value in the period between time points i*I and
22775         # (i+1)*I; Ci is the access count in the period; the symbol P refers
22776         # to the weight of Ci.
22777         out=$($LFS heat_get $DIR/$tfile)
22778         $LFS heat_get $DIR/$tfile
22779         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22780         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22781         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22782         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22783
22784         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
22785                 error "read sample ($readsample) is wrong"
22786         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
22787                 error "write sample ($writesample) is wrong"
22788         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
22789                 error "read bytes ($readbyte) is wrong"
22790         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
22791                 error "write bytes ($writebyte) is wrong"
22792
22793         echo "QQQQ" > $DIR/$tfile
22794         echo "QQQQ" > $DIR/$tfile
22795         echo "QQQQ" > $DIR/$tfile
22796         cat $DIR/$tfile > /dev/null
22797         cat $DIR/$tfile > /dev/null
22798         cat $DIR/$tfile > /dev/null
22799         cat $DIR/$tfile > /dev/null
22800
22801         sleep $((period_second + 3))
22802         echo "Sleep $((period_second + 3)) seconds..."
22803
22804         out=$($LFS heat_get $DIR/$tfile)
22805         $LFS heat_get $DIR/$tfile
22806         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22807         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22808         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22809         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22810
22811         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
22812                 4 * $decay_pct) / 100") -eq 1 ] ||
22813                 error "read sample ($readsample1) is wrong"
22814         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
22815                 3 * $decay_pct) / 100") -eq 1 ] ||
22816                 error "write sample ($writesample1) is wrong"
22817         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
22818                 20 * $decay_pct) / 100") -eq 1 ] ||
22819                 error "read bytes ($readbyte1) is wrong"
22820         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
22821                 15 * $decay_pct) / 100") -eq 1 ] ||
22822                 error "write bytes ($writebyte1) is wrong"
22823
22824         echo "Turn off file heat for the file $DIR/$tfile"
22825         $LFS heat_set -o $DIR/$tfile
22826
22827         echo "QQQQ" > $DIR/$tfile
22828         echo "QQQQ" > $DIR/$tfile
22829         echo "QQQQ" > $DIR/$tfile
22830         cat $DIR/$tfile > /dev/null
22831         cat $DIR/$tfile > /dev/null
22832         cat $DIR/$tfile > /dev/null
22833         cat $DIR/$tfile > /dev/null
22834
22835         out=$($LFS heat_get $DIR/$tfile)
22836         $LFS heat_get $DIR/$tfile
22837         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22838         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22839         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22840         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22841
22842         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
22843         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
22844         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
22845         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
22846
22847         echo "Trun on file heat for the file $DIR/$tfile"
22848         $LFS heat_set -O $DIR/$tfile
22849
22850         echo "QQQQ" > $DIR/$tfile
22851         echo "QQQQ" > $DIR/$tfile
22852         echo "QQQQ" > $DIR/$tfile
22853         cat $DIR/$tfile > /dev/null
22854         cat $DIR/$tfile > /dev/null
22855         cat $DIR/$tfile > /dev/null
22856         cat $DIR/$tfile > /dev/null
22857
22858         out=$($LFS heat_get $DIR/$tfile)
22859         $LFS heat_get $DIR/$tfile
22860         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22861         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22862         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22863         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22864
22865         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
22866         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
22867         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
22868         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
22869
22870         $LFS heat_set -c $DIR/$tfile
22871         $LCTL set_param -n llite.*.file_heat=0
22872         echo "Turn off file heat support for the Lustre filesystem"
22873
22874         echo "QQQQ" > $DIR/$tfile
22875         echo "QQQQ" > $DIR/$tfile
22876         echo "QQQQ" > $DIR/$tfile
22877         cat $DIR/$tfile > /dev/null
22878         cat $DIR/$tfile > /dev/null
22879         cat $DIR/$tfile > /dev/null
22880         cat $DIR/$tfile > /dev/null
22881
22882         out=$($LFS heat_get $DIR/$tfile)
22883         $LFS heat_get $DIR/$tfile
22884         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22885         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22886         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22887         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22888
22889         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
22890         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
22891         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
22892         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
22893
22894         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
22895         rm -f $DIR/$tfile
22896 }
22897 run_test 813 "File heat verfication"
22898
22899 test_814()
22900 {
22901         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
22902         echo -n y >> $DIR/$tfile
22903         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
22904         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
22905 }
22906 run_test 814 "sparse cp works as expected (LU-12361)"
22907
22908 test_815()
22909 {
22910         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
22911         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
22912 }
22913 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
22914
22915 test_816() {
22916         [ "$SHARED_KEY" = true ] &&
22917                 skip "OSC connections never go IDLE with Shared-Keys enabled"
22918
22919         $LFS setstripe -c 1 -i 0 $DIR/$tfile
22920         # ensure ost1 is connected
22921         stat $DIR/$tfile >/dev/null || error "can't stat"
22922         wait_osc_import_state client ost1 FULL
22923         # no locks, no reqs to let the connection idle
22924         cancel_lru_locks osc
22925         lru_resize_disable osc
22926         local before
22927         local now
22928         before=$($LCTL get_param -n \
22929                  ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
22930
22931         wait_osc_import_state client ost1 IDLE
22932         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
22933         now=$($LCTL get_param -n \
22934               ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
22935         [ $before == $now ] || error "lru_size changed $before != $now"
22936 }
22937 run_test 816 "do not reset lru_resize on idle reconnect"
22938
22939 cleanup_817() {
22940         umount $tmpdir
22941         exportfs -u localhost:$DIR/nfsexp
22942         rm -rf $DIR/nfsexp
22943 }
22944
22945 test_817() {
22946         systemctl restart nfs-server.service || skip "failed to restart nfsd"
22947
22948         mkdir -p $DIR/nfsexp
22949         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
22950                 error "failed to export nfs"
22951
22952         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
22953         stack_trap cleanup_817 EXIT
22954
22955         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
22956                 error "failed to mount nfs to $tmpdir"
22957
22958         cp /bin/true $tmpdir
22959         $DIR/nfsexp/true || error "failed to execute 'true' command"
22960 }
22961 run_test 817 "nfsd won't cache write lock for exec file"
22962
22963 test_818() {
22964         mkdir $DIR/$tdir
22965         $LFS setstripe -c1 -i0 $DIR/$tfile
22966         $LFS setstripe -c1 -i1 $DIR/$tfile
22967         stop $SINGLEMDS
22968         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
22969         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
22970         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
22971                 error "start $SINGLEMDS failed"
22972         rm -rf $DIR/$tdir
22973 }
22974 run_test 818 "unlink with failed llog"
22975
22976 test_819a() {
22977         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22978         cancel_lru_locks osc
22979         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
22980         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
22981         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
22982         rm -f $TDIR/$tfile
22983 }
22984 run_test 819a "too big niobuf in read"
22985
22986 test_819b() {
22987         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
22988         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
22989         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22990         cancel_lru_locks osc
22991         sleep 1
22992         rm -f $TDIR/$tfile
22993 }
22994 run_test 819b "too big niobuf in write"
22995
22996
22997 function test_820_start_ost() {
22998         sleep 5
22999
23000         for num in $(seq $OSTCOUNT); do
23001                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS
23002         done
23003 }
23004
23005 test_820() {
23006         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
23007
23008         mkdir $DIR/$tdir
23009         umount_client $MOUNT || error "umount failed"
23010         for num in $(seq $OSTCOUNT); do
23011                 stop ost$num
23012         done
23013
23014         # mount client with no active OSTs
23015         # so that the client can't initialize max LOV EA size
23016         # from OSC notifications
23017         mount_client $MOUNT || error "mount failed"
23018         # delay OST starting to keep this 0 max EA size for a while
23019         test_820_start_ost &
23020
23021         # create a directory on MDS2
23022         test_mkdir -i 1 -c1 $DIR/$tdir/mds2 ||
23023                 error "Failed to create directory"
23024         # open intent should update default EA size
23025         # see mdc_update_max_ea_from_body()
23026         # notice this is the very first RPC to MDS2
23027         cp /etc/services $DIR/$tdir/mds2 ||
23028                 error "Failed to copy files to mds$n"
23029 }
23030 run_test 820 "update max EA from open intent"
23031
23032 #
23033 # tests that do cleanup/setup should be run at the end
23034 #
23035
23036 test_900() {
23037         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23038         local ls
23039
23040         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
23041         $LCTL set_param fail_loc=0x903
23042
23043         cancel_lru_locks MGC
23044
23045         FAIL_ON_ERROR=true cleanup
23046         FAIL_ON_ERROR=true setup
23047 }
23048 run_test 900 "umount should not race with any mgc requeue thread"
23049
23050 # LUS-6253/LU-11185
23051 test_901() {
23052         local oldc
23053         local newc
23054         local olds
23055         local news
23056         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23057
23058         # some get_param have a bug to handle dot in param name
23059         cancel_lru_locks MGC
23060         oldc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
23061         olds=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
23062         umount_client $MOUNT || error "umount failed"
23063         mount_client $MOUNT || error "mount failed"
23064         cancel_lru_locks MGC
23065         newc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
23066         news=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
23067
23068         [ $oldc -lt $newc ] && error "mgc lock leak ($oldc != $newc)"
23069         [ $olds -lt $news ] && error "mgs lock leak ($olds != $news)"
23070
23071         return 0
23072 }
23073 run_test 901 "don't leak a mgc lock on client umount"
23074
23075 complete $SECONDS
23076 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
23077 check_and_cleanup_lustre
23078 if [ "$I_MOUNTED" != "yes" ]; then
23079         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
23080 fi
23081 exit_status