Whamcloud - gitweb
4d50dcfab8877826132194e9dcfb059c9f72282c
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
7 set -e
8
9 ONLY=${ONLY:-"$*"}
10
11 # Check Grants after these tests
12 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c 64d"
13
14 OSC=${OSC:-"osc"}
15
16 CC=${CC:-cc}
17 CREATETEST=${CREATETEST:-createtest}
18 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
19 OPENFILE=${OPENFILE:-openfile}
20 OPENUNLINK=${OPENUNLINK:-openunlink}
21 READS=${READS:-"reads"}
22 MUNLINK=${MUNLINK:-munlink}
23 SOCKETSERVER=${SOCKETSERVER:-socketserver}
24 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
25 MEMHOG=${MEMHOG:-memhog}
26 DIRECTIO=${DIRECTIO:-directio}
27 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
28 DEF_STRIPE_COUNT=-1
29 CHECK_GRANT=${CHECK_GRANT:-"yes"}
30 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
31 export PARALLEL=${PARALLEL:-"no"}
32
33 TRACE=${TRACE:-""}
34 LUSTRE=${LUSTRE:-$(dirname $0)/..}
35 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
36 . $LUSTRE/tests/test-framework.sh
37 init_test_env $@
38
39 init_logging
40
41 ALWAYS_EXCEPT="$SANITY_EXCEPT "
42 # bug number for skipped test: LU-9693 LU-6493 LU-9693
43 ALWAYS_EXCEPT+="               42a     42b     42c "
44 # bug number:    LU-8411 LU-9054 LU-13314
45 ALWAYS_EXCEPT+=" 407     312    56ob"
46
47 if $SHARED_KEY; then
48         # bug number:    LU-9795 LU-9795 LU-9795 LU-9795
49         ALWAYS_EXCEPT+=" 17n     60a     133g    300f"
50 fi
51
52 selinux_status=$(getenforce)
53 if [ "$selinux_status" != "Disabled" ]; then
54         # bug number:
55         ALWAYS_EXCEPT+=""
56 fi
57
58 # skip the grant tests for ARM until they are fixed
59 if [[ $(uname -m) = aarch64 ]]; then
60         # bug number:    LU-11596
61         ALWAYS_EXCEPT+=" $GRANT_CHECK_LIST"
62         # bug number:    LU-11671 LU-11667
63         ALWAYS_EXCEPT+=" 45       317"
64 fi
65
66 # skip nfs tests on kernels >= 4.14.0 until they are fixed
67 if [ $LINUX_VERSION_CODE -ge $(version_code 4.14.0) ]; then
68         # bug number:   LU-12661
69         ALWAYS_EXCEPT+=" 817"
70 fi
71 # skip cgroup tests on RHEL8.1 kernels until they are fixed
72 if (( $LINUX_VERSION_CODE >= $(version_code 4.18.0) &&
73       $LINUX_VERSION_CODE <  $(version_code 5.4.0) )); then
74         # bug number:   LU-13063
75         ALWAYS_EXCEPT+=" 411"
76 fi
77
78 #                                  5          12     8   12  (min)"
79 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 135 136 300o"
80
81 if [ "$mds1_FSTYPE" = "zfs" ]; then
82         # bug number for skipped test:
83         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  "
84         #                                               13    (min)"
85         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
86 fi
87
88 # Get the SLES distro version
89 #
90 # Returns a version string that should only be used in comparing
91 # strings returned by version_code()
92 sles_version_code()
93 {
94         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
95
96         # All SuSE Linux versions have one decimal. version_code expects two
97         local sles_version=$version.0
98         version_code $sles_version
99 }
100
101 # Check if we are running on Ubuntu or SLES so we can make decisions on
102 # what tests to run
103 if [ -r /etc/SuSE-release ]; then
104         sles_version=$(sles_version_code)
105         [ $sles_version -lt $(version_code 11.4.0) ] &&
106                 # bug number for skipped test: LU-4341
107                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
108         [ $sles_version -lt $(version_code 12.0.0) ] &&
109                 # bug number for skipped test: LU-3703
110                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
111 elif [ -r /etc/os-release ]; then
112         if grep -qi ubuntu /etc/os-release; then
113                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
114                                                 -e 's/^VERSION=//p' \
115                                                 /etc/os-release |
116                                                 awk '{ print $1 }'))
117
118                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
119                         # bug number for skipped test:
120                         #                LU-10334 LU-10366
121                         ALWAYS_EXCEPT+=" 103a     410"
122                 fi
123         fi
124 fi
125
126 build_test_filter
127 FAIL_ON_ERROR=false
128
129 cleanup() {
130         echo -n "cln.."
131         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
132         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
133 }
134 setup() {
135         echo -n "mnt.."
136         load_modules
137         setupall || exit 10
138         echo "done"
139 }
140
141 check_swap_layouts_support()
142 {
143         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
144                 skip "Does not support layout lock."
145 }
146
147 check_and_setup_lustre
148 DIR=${DIR:-$MOUNT}
149 assert_DIR
150
151 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
152
153 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
154 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
155 rm -rf $DIR/[Rdfs][0-9]*
156
157 # $RUNAS_ID may get set incorrectly somewhere else
158 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
159         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
160
161 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
162
163 if [ "${ONLY}" = "MOUNT" ] ; then
164         echo "Lustre is up, please go on"
165         exit
166 fi
167
168 echo "preparing for tests involving mounts"
169 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
170 touch $EXT2_DEV
171 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
172 echo # add a newline after mke2fs.
173
174 umask 077
175
176 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
177 lctl set_param debug=-1 2> /dev/null || true
178 test_0a() {
179         touch $DIR/$tfile
180         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
181         rm $DIR/$tfile
182         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
183 }
184 run_test 0a "touch; rm ====================="
185
186 test_0b() {
187         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
188         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
189 }
190 run_test 0b "chmod 0755 $DIR ============================="
191
192 test_0c() {
193         $LCTL get_param mdc.*.import | grep "state: FULL" ||
194                 error "import not FULL"
195         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
196                 error "bad target"
197 }
198 run_test 0c "check import proc"
199
200 test_0d() { # LU-3397
201         [ $MGS_VERSION -lt $(version_code 2.10.57) ] &&
202                 skip "proc exports not supported before 2.10.57"
203
204         local mgs_exp="mgs.MGS.exports"
205         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
206         local exp_client_nid
207         local exp_client_version
208         local exp_val
209         local imp_val
210         local temp_imp=$DIR/$tfile.import
211         local temp_exp=$DIR/$tfile.export
212
213         # save mgc import file to $temp_imp
214         $LCTL get_param mgc.*.import | tee $temp_imp
215         # Check if client uuid is found in MGS export
216         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
217                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
218                         $client_uuid ] &&
219                         break;
220         done
221         # save mgs export file to $temp_exp
222         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
223
224         # Compare the value of field "connect_flags"
225         imp_val=$(grep "connect_flags" $temp_imp)
226         exp_val=$(grep "connect_flags" $temp_exp)
227         [ "$exp_val" == "$imp_val" ] ||
228                 error "export flags '$exp_val' != import flags '$imp_val'"
229
230         # Compare the value of client version
231         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
232         exp_val=$(version_code $exp_client_version)
233         imp_val=$CLIENT_VERSION
234         [ "$exp_val" == "$imp_val" ] ||
235                 error "export client version '$exp_val' != '$imp_val'"
236 }
237 run_test 0d "check export proc ============================="
238
239 test_1() {
240         test_mkdir $DIR/$tdir
241         test_mkdir $DIR/$tdir/d2
242         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
243         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
244         rmdir $DIR/$tdir/d2
245         rmdir $DIR/$tdir
246         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
247 }
248 run_test 1 "mkdir; remkdir; rmdir"
249
250 test_2() {
251         test_mkdir $DIR/$tdir
252         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
253         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
254         rm -r $DIR/$tdir
255         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
256 }
257 run_test 2 "mkdir; touch; rmdir; check file"
258
259 test_3() {
260         test_mkdir $DIR/$tdir
261         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
262         touch $DIR/$tdir/$tfile
263         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
264         rm -r $DIR/$tdir
265         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
266 }
267 run_test 3 "mkdir; touch; rmdir; check dir"
268
269 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
270 test_4() {
271         test_mkdir -i 1 $DIR/$tdir
272
273         touch $DIR/$tdir/$tfile ||
274                 error "Create file under remote directory failed"
275
276         rmdir $DIR/$tdir &&
277                 error "Expect error removing in-use dir $DIR/$tdir"
278
279         test -d $DIR/$tdir || error "Remote directory disappeared"
280
281         rm -rf $DIR/$tdir || error "remove remote dir error"
282 }
283 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
284
285 test_5() {
286         test_mkdir $DIR/$tdir
287         test_mkdir $DIR/$tdir/d2
288         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
289         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
290         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
291 }
292 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
293
294 test_6a() {
295         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
296         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
297         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
298                 error "$tfile does not have perm 0666 or UID $UID"
299         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
300         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
301                 error "$tfile should be 0666 and owned by UID $UID"
302 }
303 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
304
305 test_6c() {
306         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
307
308         touch $DIR/$tfile
309         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
310         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
311                 error "$tfile should be owned by UID $RUNAS_ID"
312         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
313         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
314                 error "$tfile should be owned by UID $RUNAS_ID"
315 }
316 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
317
318 test_6e() {
319         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
320
321         touch $DIR/$tfile
322         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
323         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
324                 error "$tfile should be owned by GID $UID"
325         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
326         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
327                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
328 }
329 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
330
331 test_6g() {
332         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
333
334         test_mkdir $DIR/$tdir
335         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
336         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
337         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
338         test_mkdir $DIR/$tdir/d/subdir
339         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
340                 error "$tdir/d/subdir should be GID $RUNAS_GID"
341         if [[ $MDSCOUNT -gt 1 ]]; then
342                 # check remote dir sgid inherite
343                 $LFS mkdir -i 0 $DIR/$tdir.local ||
344                         error "mkdir $tdir.local failed"
345                 chmod g+s $DIR/$tdir.local ||
346                         error "chmod $tdir.local failed"
347                 chgrp $RUNAS_GID $DIR/$tdir.local ||
348                         error "chgrp $tdir.local failed"
349                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
350                         error "mkdir $tdir.remote failed"
351                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
352                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
353                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
354                         error "$tdir.remote should be mode 02755"
355         fi
356 }
357 run_test 6g "verify new dir in sgid dir inherits group"
358
359 test_6h() { # bug 7331
360         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
361
362         touch $DIR/$tfile || error "touch failed"
363         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
364         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
365                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
366         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
367                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
368 }
369 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
370
371 test_7a() {
372         test_mkdir $DIR/$tdir
373         $MCREATE $DIR/$tdir/$tfile
374         chmod 0666 $DIR/$tdir/$tfile
375         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
376                 error "$tdir/$tfile should be mode 0666"
377 }
378 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
379
380 test_7b() {
381         if [ ! -d $DIR/$tdir ]; then
382                 test_mkdir $DIR/$tdir
383         fi
384         $MCREATE $DIR/$tdir/$tfile
385         echo -n foo > $DIR/$tdir/$tfile
386         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
387         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
388 }
389 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
390
391 test_8() {
392         test_mkdir $DIR/$tdir
393         touch $DIR/$tdir/$tfile
394         chmod 0666 $DIR/$tdir/$tfile
395         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
396                 error "$tfile mode not 0666"
397 }
398 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
399
400 test_9() {
401         test_mkdir $DIR/$tdir
402         test_mkdir $DIR/$tdir/d2
403         test_mkdir $DIR/$tdir/d2/d3
404         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
405 }
406 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
407
408 test_10() {
409         test_mkdir $DIR/$tdir
410         test_mkdir $DIR/$tdir/d2
411         touch $DIR/$tdir/d2/$tfile
412         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
413                 error "$tdir/d2/$tfile not a file"
414 }
415 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
416
417 test_11() {
418         test_mkdir $DIR/$tdir
419         test_mkdir $DIR/$tdir/d2
420         chmod 0666 $DIR/$tdir/d2
421         chmod 0705 $DIR/$tdir/d2
422         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
423                 error "$tdir/d2 mode not 0705"
424 }
425 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
426
427 test_12() {
428         test_mkdir $DIR/$tdir
429         touch $DIR/$tdir/$tfile
430         chmod 0666 $DIR/$tdir/$tfile
431         chmod 0654 $DIR/$tdir/$tfile
432         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
433                 error "$tdir/d2 mode not 0654"
434 }
435 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
436
437 test_13() {
438         test_mkdir $DIR/$tdir
439         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
440         >  $DIR/$tdir/$tfile
441         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
442                 error "$tdir/$tfile size not 0 after truncate"
443 }
444 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
445
446 test_14() {
447         test_mkdir $DIR/$tdir
448         touch $DIR/$tdir/$tfile
449         rm $DIR/$tdir/$tfile
450         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
451 }
452 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
453
454 test_15() {
455         test_mkdir $DIR/$tdir
456         touch $DIR/$tdir/$tfile
457         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
458         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
459                 error "$tdir/${tfile_2} not a file after rename"
460         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
461 }
462 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
463
464 test_16() {
465         test_mkdir $DIR/$tdir
466         touch $DIR/$tdir/$tfile
467         rm -rf $DIR/$tdir/$tfile
468         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
469 }
470 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
471
472 test_17a() {
473         test_mkdir $DIR/$tdir
474         touch $DIR/$tdir/$tfile
475         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
476         ls -l $DIR/$tdir
477         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
478                 error "$tdir/l-exist not a symlink"
479         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
480                 error "$tdir/l-exist not referencing a file"
481         rm -f $DIR/$tdir/l-exist
482         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
483 }
484 run_test 17a "symlinks: create, remove (real)"
485
486 test_17b() {
487         test_mkdir $DIR/$tdir
488         ln -s no-such-file $DIR/$tdir/l-dangle
489         ls -l $DIR/$tdir
490         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
491                 error "$tdir/l-dangle not referencing no-such-file"
492         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
493                 error "$tdir/l-dangle not referencing non-existent file"
494         rm -f $DIR/$tdir/l-dangle
495         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
496 }
497 run_test 17b "symlinks: create, remove (dangling)"
498
499 test_17c() { # bug 3440 - don't save failed open RPC for replay
500         test_mkdir $DIR/$tdir
501         ln -s foo $DIR/$tdir/$tfile
502         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
503 }
504 run_test 17c "symlinks: open dangling (should return error)"
505
506 test_17d() {
507         test_mkdir $DIR/$tdir
508         ln -s foo $DIR/$tdir/$tfile
509         touch $DIR/$tdir/$tfile || error "creating to new symlink"
510 }
511 run_test 17d "symlinks: create dangling"
512
513 test_17e() {
514         test_mkdir $DIR/$tdir
515         local foo=$DIR/$tdir/$tfile
516         ln -s $foo $foo || error "create symlink failed"
517         ls -l $foo || error "ls -l failed"
518         ls $foo && error "ls not failed" || true
519 }
520 run_test 17e "symlinks: create recursive symlink (should return error)"
521
522 test_17f() {
523         test_mkdir $DIR/$tdir
524         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
525         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
526         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
527         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
528         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
529         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/$tdir/666
530         ls -l  $DIR/$tdir
531 }
532 run_test 17f "symlinks: long and very long symlink name"
533
534 # str_repeat(S, N) generate a string that is string S repeated N times
535 str_repeat() {
536         local s=$1
537         local n=$2
538         local ret=''
539         while [ $((n -= 1)) -ge 0 ]; do
540                 ret=$ret$s
541         done
542         echo $ret
543 }
544
545 # Long symlinks and LU-2241
546 test_17g() {
547         test_mkdir $DIR/$tdir
548         local TESTS="59 60 61 4094 4095"
549
550         # Fix for inode size boundary in 2.1.4
551         [ $MDS1_VERSION -lt $(version_code 2.1.4) ] &&
552                 TESTS="4094 4095"
553
554         # Patch not applied to 2.2 or 2.3 branches
555         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
556         [ $MDS1_VERSION -le $(version_code 2.3.55) ] &&
557                 TESTS="4094 4095"
558
559         for i in $TESTS; do
560                 local SYMNAME=$(str_repeat 'x' $i)
561                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
562                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
563         done
564 }
565 run_test 17g "symlinks: really long symlink name and inode boundaries"
566
567 test_17h() { #bug 17378
568         [ $PARALLEL == "yes" ] && skip "skip parallel run"
569         remote_mds_nodsh && skip "remote MDS with nodsh"
570
571         local mdt_idx
572
573         test_mkdir $DIR/$tdir
574         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
575         $LFS setstripe -c -1 $DIR/$tdir
576         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
577         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
578         touch $DIR/$tdir/$tfile || true
579 }
580 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
581
582 test_17i() { #bug 20018
583         [ $PARALLEL == "yes" ] && skip "skip parallel run"
584         remote_mds_nodsh && skip "remote MDS with nodsh"
585
586         local foo=$DIR/$tdir/$tfile
587         local mdt_idx
588
589         test_mkdir -c1 $DIR/$tdir
590         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
591         ln -s $foo $foo || error "create symlink failed"
592 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
593         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
594         ls -l $foo && error "error not detected"
595         return 0
596 }
597 run_test 17i "don't panic on short symlink (should return error)"
598
599 test_17k() { #bug 22301
600         [ $PARALLEL == "yes" ] && skip "skip parallel run"
601         [[ -z "$(which rsync 2>/dev/null)" ]] &&
602                 skip "no rsync command"
603         rsync --help | grep -q xattr ||
604                 skip_env "$(rsync --version | head -n1) does not support xattrs"
605         test_mkdir $DIR/$tdir
606         test_mkdir $DIR/$tdir.new
607         touch $DIR/$tdir/$tfile
608         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
609         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
610                 error "rsync failed with xattrs enabled"
611 }
612 run_test 17k "symlinks: rsync with xattrs enabled"
613
614 test_17l() { # LU-279
615         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
616                 skip "no getfattr command"
617
618         test_mkdir $DIR/$tdir
619         touch $DIR/$tdir/$tfile
620         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
621         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
622                 # -h to not follow symlinks. -m '' to list all the xattrs.
623                 # grep to remove first line: '# file: $path'.
624                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
625                 do
626                         lgetxattr_size_check $path $xattr ||
627                                 error "lgetxattr_size_check $path $xattr failed"
628                 done
629         done
630 }
631 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
632
633 # LU-1540
634 test_17m() {
635         [ $PARALLEL == "yes" ] && skip "skip parallel run"
636         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
637         remote_mds_nodsh && skip "remote MDS with nodsh"
638         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
639         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
640                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
641
642         local short_sym="0123456789"
643         local wdir=$DIR/$tdir
644         local i
645
646         test_mkdir $wdir
647         long_sym=$short_sym
648         # create a long symlink file
649         for ((i = 0; i < 4; ++i)); do
650                 long_sym=${long_sym}${long_sym}
651         done
652
653         echo "create 512 short and long symlink files under $wdir"
654         for ((i = 0; i < 256; ++i)); do
655                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
656                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
657         done
658
659         echo "erase them"
660         rm -f $wdir/*
661         sync
662         wait_delete_completed
663
664         echo "recreate the 512 symlink files with a shorter string"
665         for ((i = 0; i < 512; ++i)); do
666                 # rewrite the symlink file with a shorter string
667                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
668                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
669         done
670
671         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
672         local devname=$(mdsdevname $mds_index)
673
674         echo "stop and checking mds${mds_index}:"
675         # e2fsck should not return error
676         stop mds${mds_index}
677         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
678         rc=$?
679
680         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
681                 error "start mds${mds_index} failed"
682         df $MOUNT > /dev/null 2>&1
683         [ $rc -eq 0 ] ||
684                 error "e2fsck detected error for short/long symlink: rc=$rc"
685         rm -f $wdir/*
686 }
687 run_test 17m "run e2fsck against MDT which contains short/long symlink"
688
689 check_fs_consistency_17n() {
690         local mdt_index
691         local rc=0
692
693         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
694         # so it only check MDT1/MDT2 instead of all of MDTs.
695         for mdt_index in 1 2; do
696                 local devname=$(mdsdevname $mdt_index)
697                 # e2fsck should not return error
698                 stop mds${mdt_index}
699                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
700                         rc=$((rc + $?))
701
702                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
703                         error "mount mds$mdt_index failed"
704                 df $MOUNT > /dev/null 2>&1
705         done
706         return $rc
707 }
708
709 test_17n() {
710         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
711         [ $PARALLEL == "yes" ] && skip "skip parallel run"
712         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
713         remote_mds_nodsh && skip "remote MDS with nodsh"
714         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
715         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
716                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
717
718         local i
719
720         test_mkdir $DIR/$tdir
721         for ((i=0; i<10; i++)); do
722                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
723                         error "create remote dir error $i"
724                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
725                         error "create files under remote dir failed $i"
726         done
727
728         check_fs_consistency_17n ||
729                 error "e2fsck report error after create files under remote dir"
730
731         for ((i = 0; i < 10; i++)); do
732                 rm -rf $DIR/$tdir/remote_dir_${i} ||
733                         error "destroy remote dir error $i"
734         done
735
736         check_fs_consistency_17n ||
737                 error "e2fsck report error after unlink files under remote dir"
738
739         [ $MDS1_VERSION -lt $(version_code 2.4.50) ] &&
740                 skip "lustre < 2.4.50 does not support migrate mv"
741
742         for ((i = 0; i < 10; i++)); do
743                 mkdir -p $DIR/$tdir/remote_dir_${i}
744                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
745                         error "create files under remote dir failed $i"
746                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
747                         error "migrate remote dir error $i"
748         done
749         check_fs_consistency_17n || error "e2fsck report error after migration"
750
751         for ((i = 0; i < 10; i++)); do
752                 rm -rf $DIR/$tdir/remote_dir_${i} ||
753                         error "destroy remote dir error $i"
754         done
755
756         check_fs_consistency_17n || error "e2fsck report error after unlink"
757 }
758 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
759
760 test_17o() {
761         remote_mds_nodsh && skip "remote MDS with nodsh"
762         [ $MDS1_VERSION -lt $(version_code 2.3.64) ] &&
763                 skip "Need MDS version at least 2.3.64"
764
765         local wdir=$DIR/${tdir}o
766         local mdt_index
767         local rc=0
768
769         test_mkdir $wdir
770         touch $wdir/$tfile
771         mdt_index=$($LFS getstripe -m $wdir/$tfile)
772         mdt_index=$((mdt_index + 1))
773
774         cancel_lru_locks mdc
775         #fail mds will wait the failover finish then set
776         #following fail_loc to avoid interfer the recovery process.
777         fail mds${mdt_index}
778
779         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
780         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
781         ls -l $wdir/$tfile && rc=1
782         do_facet mds${mdt_index} lctl set_param fail_loc=0
783         [[ $rc -eq 0 ]] || error "stat file should fail"
784 }
785 run_test 17o "stat file with incompat LMA feature"
786
787 test_18() {
788         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
789         ls $DIR || error "Failed to ls $DIR: $?"
790 }
791 run_test 18 "touch .../f ; ls ... =============================="
792
793 test_19a() {
794         touch $DIR/$tfile
795         ls -l $DIR
796         rm $DIR/$tfile
797         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
798 }
799 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
800
801 test_19b() {
802         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
803 }
804 run_test 19b "ls -l .../f19 (should return error) =============="
805
806 test_19c() {
807         [ $RUNAS_ID -eq $UID ] &&
808                 skip_env "RUNAS_ID = UID = $UID -- skipping"
809
810         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
811 }
812 run_test 19c "$RUNAS touch .../f19 (should return error) =="
813
814 test_19d() {
815         cat $DIR/f19 && error || true
816 }
817 run_test 19d "cat .../f19 (should return error) =============="
818
819 test_20() {
820         touch $DIR/$tfile
821         rm $DIR/$tfile
822         touch $DIR/$tfile
823         rm $DIR/$tfile
824         touch $DIR/$tfile
825         rm $DIR/$tfile
826         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
827 }
828 run_test 20 "touch .../f ; ls -l ..."
829
830 test_21() {
831         test_mkdir $DIR/$tdir
832         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
833         ln -s dangle $DIR/$tdir/link
834         echo foo >> $DIR/$tdir/link
835         cat $DIR/$tdir/dangle
836         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
837         $CHECKSTAT -f -t file $DIR/$tdir/link ||
838                 error "$tdir/link not linked to a file"
839 }
840 run_test 21 "write to dangling link"
841
842 test_22() {
843         local wdir=$DIR/$tdir
844         test_mkdir $wdir
845         chown $RUNAS_ID:$RUNAS_GID $wdir
846         (cd $wdir || error "cd $wdir failed";
847                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
848                 $RUNAS tar xf -)
849         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
850         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
851         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
852                 error "checkstat -u failed"
853 }
854 run_test 22 "unpack tar archive as non-root user"
855
856 # was test_23
857 test_23a() {
858         test_mkdir $DIR/$tdir
859         local file=$DIR/$tdir/$tfile
860
861         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
862         openfile -f O_CREAT:O_EXCL $file &&
863                 error "$file recreate succeeded" || true
864 }
865 run_test 23a "O_CREAT|O_EXCL in subdir"
866
867 test_23b() { # bug 18988
868         test_mkdir $DIR/$tdir
869         local file=$DIR/$tdir/$tfile
870
871         rm -f $file
872         echo foo > $file || error "write filed"
873         echo bar >> $file || error "append filed"
874         $CHECKSTAT -s 8 $file || error "wrong size"
875         rm $file
876 }
877 run_test 23b "O_APPEND check"
878
879 # LU-9409, size with O_APPEND and tiny writes
880 test_23c() {
881         local file=$DIR/$tfile
882
883         # single dd
884         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
885         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
886         rm -f $file
887
888         # racing tiny writes
889         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
890         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
891         wait
892         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
893         rm -f $file
894
895         #racing tiny & normal writes
896         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
897         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
898         wait
899         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
900         rm -f $file
901
902         #racing tiny & normal writes 2, ugly numbers
903         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
904         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
905         wait
906         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
907         rm -f $file
908 }
909 run_test 23c "O_APPEND size checks for tiny writes"
910
911 # LU-11069 file offset is correct after appending writes
912 test_23d() {
913         local file=$DIR/$tfile
914         local offset
915
916         echo CentaurHauls > $file
917         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
918         if ((offset != 26)); then
919                 error "wrong offset, expected 26, got '$offset'"
920         fi
921 }
922 run_test 23d "file offset is correct after appending writes"
923
924 # rename sanity
925 test_24a() {
926         echo '-- same directory rename'
927         test_mkdir $DIR/$tdir
928         touch $DIR/$tdir/$tfile.1
929         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
930         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
931 }
932 run_test 24a "rename file to non-existent target"
933
934 test_24b() {
935         test_mkdir $DIR/$tdir
936         touch $DIR/$tdir/$tfile.{1,2}
937         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
938         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
939         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
940 }
941 run_test 24b "rename file to existing target"
942
943 test_24c() {
944         test_mkdir $DIR/$tdir
945         test_mkdir $DIR/$tdir/d$testnum.1
946         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
947         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
948         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
949 }
950 run_test 24c "rename directory to non-existent target"
951
952 test_24d() {
953         test_mkdir -c1 $DIR/$tdir
954         test_mkdir -c1 $DIR/$tdir/d$testnum.1
955         test_mkdir -c1 $DIR/$tdir/d$testnum.2
956         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
957         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
958         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
959 }
960 run_test 24d "rename directory to existing target"
961
962 test_24e() {
963         echo '-- cross directory renames --'
964         test_mkdir $DIR/R5a
965         test_mkdir $DIR/R5b
966         touch $DIR/R5a/f
967         mv $DIR/R5a/f $DIR/R5b/g
968         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
969         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
970 }
971 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
972
973 test_24f() {
974         test_mkdir $DIR/R6a
975         test_mkdir $DIR/R6b
976         touch $DIR/R6a/f $DIR/R6b/g
977         mv $DIR/R6a/f $DIR/R6b/g
978         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
979         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
980 }
981 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
982
983 test_24g() {
984         test_mkdir $DIR/R7a
985         test_mkdir $DIR/R7b
986         test_mkdir $DIR/R7a/d
987         mv $DIR/R7a/d $DIR/R7b/e
988         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
989         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
990 }
991 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
992
993 test_24h() {
994         test_mkdir -c1 $DIR/R8a
995         test_mkdir -c1 $DIR/R8b
996         test_mkdir -c1 $DIR/R8a/d
997         test_mkdir -c1 $DIR/R8b/e
998         mrename $DIR/R8a/d $DIR/R8b/e
999         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
1000         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
1001 }
1002 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
1003
1004 test_24i() {
1005         echo "-- rename error cases"
1006         test_mkdir $DIR/R9
1007         test_mkdir $DIR/R9/a
1008         touch $DIR/R9/f
1009         mrename $DIR/R9/f $DIR/R9/a
1010         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1011         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1012         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1013 }
1014 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1015
1016 test_24j() {
1017         test_mkdir $DIR/R10
1018         mrename $DIR/R10/f $DIR/R10/g
1019         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1020         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1021         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1022 }
1023 run_test 24j "source does not exist ============================"
1024
1025 test_24k() {
1026         test_mkdir $DIR/R11a
1027         test_mkdir $DIR/R11a/d
1028         touch $DIR/R11a/f
1029         mv $DIR/R11a/f $DIR/R11a/d
1030         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1031         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1032 }
1033 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1034
1035 # bug 2429 - rename foo foo foo creates invalid file
1036 test_24l() {
1037         f="$DIR/f24l"
1038         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1039 }
1040 run_test 24l "Renaming a file to itself ========================"
1041
1042 test_24m() {
1043         f="$DIR/f24m"
1044         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1045         # on ext3 this does not remove either the source or target files
1046         # though the "expected" operation would be to remove the source
1047         $CHECKSTAT -t file ${f} || error "${f} missing"
1048         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1049 }
1050 run_test 24m "Renaming a file to a hard link to itself ========="
1051
1052 test_24n() {
1053     f="$DIR/f24n"
1054     # this stats the old file after it was renamed, so it should fail
1055     touch ${f}
1056     $CHECKSTAT ${f} || error "${f} missing"
1057     mv ${f} ${f}.rename
1058     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1059     $CHECKSTAT -a ${f} || error "${f} exists"
1060 }
1061 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1062
1063 test_24o() {
1064         test_mkdir $DIR/$tdir
1065         rename_many -s random -v -n 10 $DIR/$tdir
1066 }
1067 run_test 24o "rename of files during htree split"
1068
1069 test_24p() {
1070         test_mkdir $DIR/R12a
1071         test_mkdir $DIR/R12b
1072         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1073         mrename $DIR/R12a $DIR/R12b
1074         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1075         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1076         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1077         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1078 }
1079 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1080
1081 cleanup_multiop_pause() {
1082         trap 0
1083         kill -USR1 $MULTIPID
1084 }
1085
1086 test_24q() {
1087         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1088
1089         test_mkdir $DIR/R13a
1090         test_mkdir $DIR/R13b
1091         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1092         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1093         MULTIPID=$!
1094
1095         trap cleanup_multiop_pause EXIT
1096         mrename $DIR/R13a $DIR/R13b
1097         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1098         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1099         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1100         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1101         cleanup_multiop_pause
1102         wait $MULTIPID || error "multiop close failed"
1103 }
1104 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1105
1106 test_24r() { #bug 3789
1107         test_mkdir $DIR/R14a
1108         test_mkdir $DIR/R14a/b
1109         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1110         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1111         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1112 }
1113 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1114
1115 test_24s() {
1116         test_mkdir $DIR/R15a
1117         test_mkdir $DIR/R15a/b
1118         test_mkdir $DIR/R15a/b/c
1119         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1120         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1121         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1122 }
1123 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1124 test_24t() {
1125         test_mkdir $DIR/R16a
1126         test_mkdir $DIR/R16a/b
1127         test_mkdir $DIR/R16a/b/c
1128         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1129         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1130         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1131 }
1132 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1133
1134 test_24u() { # bug12192
1135         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1136         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1137 }
1138 run_test 24u "create stripe file"
1139
1140 simple_cleanup_common() {
1141         local rc=0
1142         trap 0
1143         [ -z "$DIR" ] || [ -z "$tdir" ] && return 0
1144
1145         local start=$SECONDS
1146         rm -rf $DIR/$tdir
1147         rc=$?
1148         wait_delete_completed
1149         echo "cleanup time $((SECONDS - start))"
1150         return $rc
1151 }
1152
1153 max_pages_per_rpc() {
1154         local mdtname="$(printf "MDT%04x" ${1:-0})"
1155         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1156 }
1157
1158 test_24v() {
1159         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1160
1161         local nrfiles=${COUNT:-100000}
1162         local fname="$DIR/$tdir/$tfile"
1163
1164         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1165         [ "$mds1_FSTYPE" = "zfs" ] && nrfiles=${COUNT:-10000}
1166
1167         test_mkdir "$(dirname $fname)"
1168         # assume MDT0000 has the fewest inodes
1169         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1170         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1171         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1172
1173         trap simple_cleanup_common EXIT
1174
1175         createmany -m "$fname" $nrfiles
1176
1177         cancel_lru_locks mdc
1178         lctl set_param mdc.*.stats clear
1179
1180         # was previously test_24D: LU-6101
1181         # readdir() returns correct number of entries after cursor reload
1182         local num_ls=$(ls $DIR/$tdir | wc -l)
1183         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1184         local num_all=$(ls -a $DIR/$tdir | wc -l)
1185         if [ $num_ls -ne $nrfiles ] || [ $num_uniq -ne $nrfiles ] ||
1186                 [ $num_all -ne $((nrfiles + 2)) ]; then
1187                         error "Expected $nrfiles files, got $num_ls " \
1188                                 "($num_uniq unique $num_all .&..)"
1189         fi
1190         # LU-5 large readdir
1191         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1192         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1193         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1194         # take into account of overhead in lu_dirpage header and end mark in
1195         # each page, plus one in rpc_num calculation.
1196         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1197         local page_entries=$(((PAGE_SIZE - 24) / dirent_size))
1198         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1199         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1200         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1201         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1202         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1203         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1204                 error "large readdir doesn't take effect: " \
1205                       "$mds_readpage should be about $rpc_max"
1206
1207         simple_cleanup_common
1208 }
1209 run_test 24v "list large directory (test hash collision, b=17560)"
1210
1211 test_24w() { # bug21506
1212         SZ1=234852
1213         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1214         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1215         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1216         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1217         [[ "$SZ1" -eq "$SZ2" ]] ||
1218                 error "Error reading at the end of the file $tfile"
1219 }
1220 run_test 24w "Reading a file larger than 4Gb"
1221
1222 test_24x() {
1223         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1224         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1225         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1226                 skip "Need MDS version at least 2.7.56"
1227
1228         local MDTIDX=1
1229         local remote_dir=$DIR/$tdir/remote_dir
1230
1231         test_mkdir $DIR/$tdir
1232         $LFS mkdir -i $MDTIDX $remote_dir ||
1233                 error "create remote directory failed"
1234
1235         test_mkdir $DIR/$tdir/src_dir
1236         touch $DIR/$tdir/src_file
1237         test_mkdir $remote_dir/tgt_dir
1238         touch $remote_dir/tgt_file
1239
1240         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1241                 error "rename dir cross MDT failed!"
1242
1243         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1244                 error "rename file cross MDT failed!"
1245
1246         touch $DIR/$tdir/ln_file
1247         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1248                 error "ln file cross MDT failed"
1249
1250         rm -rf $DIR/$tdir || error "Can not delete directories"
1251 }
1252 run_test 24x "cross MDT rename/link"
1253
1254 test_24y() {
1255         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1256         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1257
1258         local remote_dir=$DIR/$tdir/remote_dir
1259         local mdtidx=1
1260
1261         test_mkdir $DIR/$tdir
1262         $LFS mkdir -i $mdtidx $remote_dir ||
1263                 error "create remote directory failed"
1264
1265         test_mkdir $remote_dir/src_dir
1266         touch $remote_dir/src_file
1267         test_mkdir $remote_dir/tgt_dir
1268         touch $remote_dir/tgt_file
1269
1270         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1271                 error "rename subdir in the same remote dir failed!"
1272
1273         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1274                 error "rename files in the same remote dir failed!"
1275
1276         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1277                 error "link files in the same remote dir failed!"
1278
1279         rm -rf $DIR/$tdir || error "Can not delete directories"
1280 }
1281 run_test 24y "rename/link on the same dir should succeed"
1282
1283 test_24z() {
1284         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1285         [[ $MDS1_VERSION -lt $(version_code 2.12.51) ]] &&
1286                 skip "Need MDS version at least 2.12.51"
1287
1288         local index
1289
1290         for index in 0 1; do
1291                 $LFS mkdir -i $index $DIR/$tdir.$index || error "mkdir failed"
1292                 touch $DIR/$tdir.0/$tfile.$index || error "touch failed"
1293         done
1294
1295         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1 || error "mv $tfile.0 failed"
1296
1297         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.0)
1298         [ $index -eq 0 ] || error "$tfile.0 is on MDT$index"
1299
1300         local mdts=$(comma_list $(mdts_nodes))
1301
1302         do_nodes $mdts $LCTL set_param mdt.*.enable_remote_rename=0
1303         stack_trap "do_nodes $mdts $LCTL \
1304                 set_param mdt.*.enable_remote_rename=1" EXIT
1305
1306         mv $DIR/$tdir.0/$tfile.1 $DIR/$tdir.1 || error "mv $tfile.1 failed"
1307
1308         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.1)
1309         [ $index -eq 1 ] || error "$tfile.1 is on MDT$index"
1310 }
1311 run_test 24z "cross-MDT rename is done as cp"
1312
1313 test_24A() { # LU-3182
1314         local NFILES=5000
1315
1316         rm -rf $DIR/$tdir
1317         test_mkdir $DIR/$tdir
1318         trap simple_cleanup_common EXIT
1319         createmany -m $DIR/$tdir/$tfile $NFILES
1320         local t=$(ls $DIR/$tdir | wc -l)
1321         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1322         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1323         if [ $t -ne $NFILES ] || [ $u -ne $NFILES ] ||
1324            [ $v -ne $((NFILES + 2)) ] ; then
1325                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1326         fi
1327
1328         simple_cleanup_common || error "Can not delete directories"
1329 }
1330 run_test 24A "readdir() returns correct number of entries."
1331
1332 test_24B() { # LU-4805
1333         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1334
1335         local count
1336
1337         test_mkdir $DIR/$tdir
1338         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1339                 error "create striped dir failed"
1340
1341         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1342         [ $count -eq 2 ] || error "Expected 2, got $count"
1343
1344         touch $DIR/$tdir/striped_dir/a
1345
1346         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1347         [ $count -eq 3 ] || error "Expected 3, got $count"
1348
1349         touch $DIR/$tdir/striped_dir/.f
1350
1351         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1352         [ $count -eq 4 ] || error "Expected 4, got $count"
1353
1354         rm -rf $DIR/$tdir || error "Can not delete directories"
1355 }
1356 run_test 24B "readdir for striped dir return correct number of entries"
1357
1358 test_24C() {
1359         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1360
1361         mkdir $DIR/$tdir
1362         mkdir $DIR/$tdir/d0
1363         mkdir $DIR/$tdir/d1
1364
1365         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1366                 error "create striped dir failed"
1367
1368         cd $DIR/$tdir/d0/striped_dir
1369
1370         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1371         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1372         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1373
1374         [ "$d0_ino" = "$parent_ino" ] ||
1375                 error ".. wrong, expect $d0_ino, get $parent_ino"
1376
1377         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1378                 error "mv striped dir failed"
1379
1380         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1381
1382         [ "$d1_ino" = "$parent_ino" ] ||
1383                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1384 }
1385 run_test 24C "check .. in striped dir"
1386
1387 test_24E() {
1388         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1389         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1390
1391         mkdir -p $DIR/$tdir
1392         mkdir $DIR/$tdir/src_dir
1393         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1394                 error "create remote source failed"
1395
1396         touch $DIR/$tdir/src_dir/src_child/a
1397
1398         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1399                 error "create remote target dir failed"
1400
1401         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1402                 error "create remote target child failed"
1403
1404         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1405                 error "rename dir cross MDT failed!"
1406
1407         find $DIR/$tdir
1408
1409         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1410                 error "src_child still exists after rename"
1411
1412         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1413                 error "missing file(a) after rename"
1414
1415         rm -rf $DIR/$tdir || error "Can not delete directories"
1416 }
1417 run_test 24E "cross MDT rename/link"
1418
1419 test_24F () {
1420         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
1421
1422         local repeats=1000
1423         [ "$SLOW" = "no" ] && repeats=100
1424
1425         mkdir -p $DIR/$tdir
1426
1427         echo "$repeats repeats"
1428         for ((i = 0; i < repeats; i++)); do
1429                 $LFS mkdir -i0 -c2 $DIR/$tdir/test || error "mkdir fails"
1430                 touch $DIR/$tdir/test/a || error "touch fails"
1431                 mkdir $DIR/$tdir/test/b || error "mkdir fails"
1432                 rm -rf $DIR/$tdir/test || error "rmdir fails"
1433         done
1434
1435         true
1436 }
1437 run_test 24F "hash order vs readdir (LU-11330)"
1438
1439 test_25a() {
1440         echo '== symlink sanity ============================================='
1441
1442         test_mkdir $DIR/d25
1443         ln -s d25 $DIR/s25
1444         touch $DIR/s25/foo ||
1445                 error "File creation in symlinked directory failed"
1446 }
1447 run_test 25a "create file in symlinked directory ==============="
1448
1449 test_25b() {
1450         [ ! -d $DIR/d25 ] && test_25a
1451         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1452 }
1453 run_test 25b "lookup file in symlinked directory ==============="
1454
1455 test_26a() {
1456         test_mkdir $DIR/d26
1457         test_mkdir $DIR/d26/d26-2
1458         ln -s d26/d26-2 $DIR/s26
1459         touch $DIR/s26/foo || error "File creation failed"
1460 }
1461 run_test 26a "multiple component symlink ======================="
1462
1463 test_26b() {
1464         test_mkdir -p $DIR/$tdir/d26-2
1465         ln -s $tdir/d26-2/foo $DIR/s26-2
1466         touch $DIR/s26-2 || error "File creation failed"
1467 }
1468 run_test 26b "multiple component symlink at end of lookup ======"
1469
1470 test_26c() {
1471         test_mkdir $DIR/d26.2
1472         touch $DIR/d26.2/foo
1473         ln -s d26.2 $DIR/s26.2-1
1474         ln -s s26.2-1 $DIR/s26.2-2
1475         ln -s s26.2-2 $DIR/s26.2-3
1476         chmod 0666 $DIR/s26.2-3/foo
1477 }
1478 run_test 26c "chain of symlinks"
1479
1480 # recursive symlinks (bug 439)
1481 test_26d() {
1482         ln -s d26-3/foo $DIR/d26-3
1483 }
1484 run_test 26d "create multiple component recursive symlink"
1485
1486 test_26e() {
1487         [ ! -h $DIR/d26-3 ] && test_26d
1488         rm $DIR/d26-3
1489 }
1490 run_test 26e "unlink multiple component recursive symlink"
1491
1492 # recursive symlinks (bug 7022)
1493 test_26f() {
1494         test_mkdir $DIR/$tdir
1495         test_mkdir $DIR/$tdir/$tfile
1496         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1497         test_mkdir -p lndir/bar1
1498         test_mkdir $DIR/$tdir/$tfile/$tfile
1499         cd $tfile                || error "cd $tfile failed"
1500         ln -s .. dotdot          || error "ln dotdot failed"
1501         ln -s dotdot/lndir lndir || error "ln lndir failed"
1502         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1503         output=`ls $tfile/$tfile/lndir/bar1`
1504         [ "$output" = bar1 ] && error "unexpected output"
1505         rm -r $tfile             || error "rm $tfile failed"
1506         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1507 }
1508 run_test 26f "rm -r of a directory which has recursive symlink"
1509
1510 test_27a() {
1511         test_mkdir $DIR/$tdir
1512         $LFS getstripe $DIR/$tdir
1513         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1514         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1515         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1516 }
1517 run_test 27a "one stripe file"
1518
1519 test_27b() {
1520         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1521
1522         test_mkdir $DIR/$tdir
1523         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1524         $LFS getstripe -c $DIR/$tdir/$tfile
1525         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1526                 error "two-stripe file doesn't have two stripes"
1527
1528         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1529 }
1530 run_test 27b "create and write to two stripe file"
1531
1532 # 27c family tests specific striping, setstripe -o
1533 test_27ca() {
1534         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1535         test_mkdir -p $DIR/$tdir
1536         local osts="1"
1537
1538         $LFS setstripe -o $osts $DIR/$tdir/$tfile  || error "setstripe failed"
1539         $LFS getstripe -i $DIR/$tdir/$tfile
1540         [ $($LFS getstripe -i $DIR/$tdir/$tfile ) -eq $osts ] ||
1541                 error "stripe not on specified OST"
1542
1543         dd if=/dev/zero of=$DIR/$tdir/$tfile  bs=1M count=4 || error "dd failed"
1544 }
1545 run_test 27ca "one stripe on specified OST"
1546
1547 test_27cb() {
1548         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1549         test_mkdir -p $DIR/$tdir
1550         local osts="1,0"
1551         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1552         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1553         echo "$getstripe"
1554
1555         # Strip getstripe output to a space separated list of OSTs
1556         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1557                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1558         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1559                 error "stripes not on specified OSTs"
1560
1561         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1562 }
1563 run_test 27cb "two stripes on specified OSTs"
1564
1565 test_27cc() {
1566         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1567         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1568                 skip "server does not support overstriping"
1569
1570         test_mkdir -p $DIR/$tdir
1571         local osts="0,0"
1572         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1573         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1574         echo "$getstripe"
1575
1576         # Strip getstripe output to a space separated list of OSTs
1577         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1578                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1579         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1580                 error "stripes not on specified OSTs"
1581
1582         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1583 }
1584 run_test 27cc "two stripes on the same OST"
1585
1586 test_27cd() {
1587         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1588         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1589                 skip "server does not support overstriping"
1590         test_mkdir -p $DIR/$tdir
1591         local osts="0,1,1,0"
1592         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1593         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1594         echo "$getstripe"
1595
1596         # Strip getstripe output to a space separated list of OSTs
1597         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1598                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1599         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1600                 error "stripes not on specified OSTs"
1601
1602         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1603 }
1604 run_test 27cd "four stripes on two OSTs"
1605
1606 test_27ce() {
1607         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
1608                 skip_env "too many osts, skipping"
1609         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1610                 skip "server does not support overstriping"
1611         # We do one more stripe than we have OSTs
1612         [ $OSTCOUNT -ge 159 ] || large_xattr_enabled ||
1613                 skip_env "ea_inode feature disabled"
1614
1615         test_mkdir -p $DIR/$tdir
1616         local osts=""
1617         for i in $(seq 0 $OSTCOUNT);
1618         do
1619                 osts=$osts"0"
1620                 if [ $i -ne $OSTCOUNT ]; then
1621                         osts=$osts","
1622                 fi
1623         done
1624         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1625         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1626         echo "$getstripe"
1627
1628         # Strip getstripe output to a space separated list of OSTs
1629         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1630                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1631         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1632                 error "stripes not on specified OSTs"
1633
1634         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1635 }
1636 run_test 27ce "more stripes than OSTs with -o"
1637
1638 test_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_33h() {
3753         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3754         [ $MDS1_VERSION -lt $(version_code 2.13.50) ] &&
3755                 skip "Need MDS version at least 2.13.50"
3756
3757         test_mkdir -c $MDSCOUNT -H crush $DIR/$tdir ||
3758                 error "mkdir $tdir failed"
3759         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
3760
3761         local index=$($LFS getstripe -m $DIR/$tdir/$tfile)
3762         local index2
3763
3764         for fname in $DIR/$tdir/$tfile.bak \
3765                      $DIR/$tdir/$tfile.SAV \
3766                      $DIR/$tdir/$tfile.orig \
3767                      $DIR/$tdir/$tfile~; do
3768                 touch $fname  || error "touch $fname failed"
3769                 index2=$($LFS getstripe -m $fname)
3770                 [ $index -eq $index2 ] ||
3771                         error "$fname MDT index mismatch $index != $index2"
3772         done
3773
3774         local failed=0
3775         for i in {1..50}; do
3776                 for fname in $(mktemp -u $DIR/$tdir/.$tfile.XXXXXX) \
3777                              $(mktemp $DIR/$tdir/$tfile.XXXXXXXX); do
3778                         touch $fname  || error "touch $fname failed"
3779                         index2=$($LFS getstripe -m $fname)
3780                         if [[ $index != $index2 ]]; then
3781                                 failed=$((failed + 1))
3782                                 echo "$fname MDT index mismatch $index != $index2"
3783                         fi
3784                 done
3785         done
3786         echo "$failed MDT index mismatches"
3787         (( failed < 4 )) || error "MDT index mismatch $failed times"
3788
3789 }
3790 run_test 33h "temp file is located on the same MDT as target"
3791
3792 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3793 test_34a() {
3794         rm -f $DIR/f34
3795         $MCREATE $DIR/f34 || error "mcreate failed"
3796         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3797                 error "getstripe failed"
3798         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3799         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3800                 error "getstripe failed"
3801         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3802                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3803 }
3804 run_test 34a "truncate file that has not been opened ==========="
3805
3806 test_34b() {
3807         [ ! -f $DIR/f34 ] && test_34a
3808         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3809                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3810         $OPENFILE -f O_RDONLY $DIR/f34
3811         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3812                 error "getstripe failed"
3813         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3814                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3815 }
3816 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3817
3818 test_34c() {
3819         [ ! -f $DIR/f34 ] && test_34a
3820         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3821                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3822         $OPENFILE -f O_RDWR $DIR/f34
3823         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" &&
3824                 error "$LFS getstripe failed"
3825         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3826                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3827 }
3828 run_test 34c "O_RDWR opening file-with-size works =============="
3829
3830 test_34d() {
3831         [ ! -f $DIR/f34 ] && test_34a
3832         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3833                 error "dd failed"
3834         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3835                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3836         rm $DIR/f34
3837 }
3838 run_test 34d "write to sparse file ============================="
3839
3840 test_34e() {
3841         rm -f $DIR/f34e
3842         $MCREATE $DIR/f34e || error "mcreate failed"
3843         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3844         $CHECKSTAT -s 1000 $DIR/f34e ||
3845                 error "Size of $DIR/f34e not equal to 1000 bytes"
3846         $OPENFILE -f O_RDWR $DIR/f34e
3847         $CHECKSTAT -s 1000 $DIR/f34e ||
3848                 error "Size of $DIR/f34e not equal to 1000 bytes"
3849 }
3850 run_test 34e "create objects, some with size and some without =="
3851
3852 test_34f() { # bug 6242, 6243
3853         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3854
3855         SIZE34F=48000
3856         rm -f $DIR/f34f
3857         $MCREATE $DIR/f34f || error "mcreate failed"
3858         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3859         dd if=$DIR/f34f of=$TMP/f34f
3860         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3861         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3862         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3863         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3864         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3865 }
3866 run_test 34f "read from a file with no objects until EOF ======="
3867
3868 test_34g() {
3869         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3870
3871         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3872                 error "dd failed"
3873         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3874         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3875                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3876         cancel_lru_locks osc
3877         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3878                 error "wrong size after lock cancel"
3879
3880         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3881         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3882                 error "expanding truncate failed"
3883         cancel_lru_locks osc
3884         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3885                 error "wrong expanded size after lock cancel"
3886 }
3887 run_test 34g "truncate long file ==============================="
3888
3889 test_34h() {
3890         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3891
3892         local gid=10
3893         local sz=1000
3894
3895         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3896         sync # Flush the cache so that multiop below does not block on cache
3897              # flush when getting the group lock
3898         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3899         MULTIPID=$!
3900
3901         # Since just timed wait is not good enough, let's do a sync write
3902         # that way we are sure enough time for a roundtrip + processing
3903         # passed + 2 seconds of extra margin.
3904         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
3905         rm $DIR/${tfile}-1
3906         sleep 2
3907
3908         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3909                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3910                 kill -9 $MULTIPID
3911         fi
3912         wait $MULTIPID
3913         local nsz=`stat -c %s $DIR/$tfile`
3914         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3915 }
3916 run_test 34h "ftruncate file under grouplock should not block"
3917
3918 test_35a() {
3919         cp /bin/sh $DIR/f35a
3920         chmod 444 $DIR/f35a
3921         chown $RUNAS_ID $DIR/f35a
3922         $RUNAS $DIR/f35a && error || true
3923         rm $DIR/f35a
3924 }
3925 run_test 35a "exec file with mode 444 (should return and not leak)"
3926
3927 test_36a() {
3928         rm -f $DIR/f36
3929         utime $DIR/f36 || error "utime failed for MDS"
3930 }
3931 run_test 36a "MDS utime check (mknod, utime)"
3932
3933 test_36b() {
3934         echo "" > $DIR/f36
3935         utime $DIR/f36 || error "utime failed for OST"
3936 }
3937 run_test 36b "OST utime check (open, utime)"
3938
3939 test_36c() {
3940         rm -f $DIR/d36/f36
3941         test_mkdir $DIR/d36
3942         chown $RUNAS_ID $DIR/d36
3943         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3944 }
3945 run_test 36c "non-root MDS utime check (mknod, utime)"
3946
3947 test_36d() {
3948         [ ! -d $DIR/d36 ] && test_36c
3949         echo "" > $DIR/d36/f36
3950         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3951 }
3952 run_test 36d "non-root OST utime check (open, utime)"
3953
3954 test_36e() {
3955         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3956
3957         test_mkdir $DIR/$tdir
3958         touch $DIR/$tdir/$tfile
3959         $RUNAS utime $DIR/$tdir/$tfile &&
3960                 error "utime worked, expected failure" || true
3961 }
3962 run_test 36e "utime on non-owned file (should return error)"
3963
3964 subr_36fh() {
3965         local fl="$1"
3966         local LANG_SAVE=$LANG
3967         local LC_LANG_SAVE=$LC_LANG
3968         export LANG=C LC_LANG=C # for date language
3969
3970         DATESTR="Dec 20  2000"
3971         test_mkdir $DIR/$tdir
3972         lctl set_param fail_loc=$fl
3973         date; date +%s
3974         cp /etc/hosts $DIR/$tdir/$tfile
3975         sync & # write RPC generated with "current" inode timestamp, but delayed
3976         sleep 1
3977         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3978         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3979         cancel_lru_locks $OSC
3980         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3981         date; date +%s
3982         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3983                 echo "BEFORE: $LS_BEFORE" && \
3984                 echo "AFTER : $LS_AFTER" && \
3985                 echo "WANT  : $DATESTR" && \
3986                 error "$DIR/$tdir/$tfile timestamps changed" || true
3987
3988         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3989 }
3990
3991 test_36f() {
3992         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3993
3994         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3995         subr_36fh "0x80000214"
3996 }
3997 run_test 36f "utime on file racing with OST BRW write =========="
3998
3999 test_36g() {
4000         remote_ost_nodsh && skip "remote OST with nodsh"
4001         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4002         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
4003                 skip "Need MDS version at least 2.12.51"
4004
4005         local fmd_max_age
4006         local fmd
4007         local facet="ost1"
4008         local tgt="obdfilter"
4009
4010         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
4011
4012         test_mkdir $DIR/$tdir
4013         fmd_max_age=$(do_facet $facet \
4014                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
4015                 head -n 1")
4016
4017         echo "FMD max age: ${fmd_max_age}s"
4018         touch $DIR/$tdir/$tfile
4019         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4020                 gawk '{cnt=cnt+$1}  END{print cnt}')
4021         echo "FMD before: $fmd"
4022         [[ $fmd == 0 ]] &&
4023                 error "FMD wasn't create by touch"
4024         sleep $((fmd_max_age + 12))
4025         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4026                 gawk '{cnt=cnt+$1}  END{print cnt}')
4027         echo "FMD after: $fmd"
4028         [[ $fmd == 0 ]] ||
4029                 error "FMD wasn't expired by ping"
4030 }
4031 run_test 36g "FMD cache expiry ====================="
4032
4033 test_36h() {
4034         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4035
4036         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
4037         subr_36fh "0x80000227"
4038 }
4039 run_test 36h "utime on file racing with OST BRW write =========="
4040
4041 test_36i() {
4042         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4043
4044         test_mkdir $DIR/$tdir
4045         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
4046
4047         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
4048         local new_mtime=$((mtime + 200))
4049
4050         #change Modify time of striped dir
4051         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
4052                         error "change mtime failed"
4053
4054         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
4055
4056         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
4057 }
4058 run_test 36i "change mtime on striped directory"
4059
4060 # test_37 - duplicate with tests 32q 32r
4061
4062 test_38() {
4063         local file=$DIR/$tfile
4064         touch $file
4065         openfile -f O_DIRECTORY $file
4066         local RC=$?
4067         local ENOTDIR=20
4068         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
4069         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
4070 }
4071 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
4072
4073 test_39a() { # was test_39
4074         touch $DIR/$tfile
4075         touch $DIR/${tfile}2
4076 #       ls -l  $DIR/$tfile $DIR/${tfile}2
4077 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
4078 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
4079         sleep 2
4080         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
4081         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
4082                 echo "mtime"
4083                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
4084                 echo "atime"
4085                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
4086                 echo "ctime"
4087                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
4088                 error "O_TRUNC didn't change timestamps"
4089         fi
4090 }
4091 run_test 39a "mtime changed on create"
4092
4093 test_39b() {
4094         test_mkdir -c1 $DIR/$tdir
4095         cp -p /etc/passwd $DIR/$tdir/fopen
4096         cp -p /etc/passwd $DIR/$tdir/flink
4097         cp -p /etc/passwd $DIR/$tdir/funlink
4098         cp -p /etc/passwd $DIR/$tdir/frename
4099         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
4100
4101         sleep 1
4102         echo "aaaaaa" >> $DIR/$tdir/fopen
4103         echo "aaaaaa" >> $DIR/$tdir/flink
4104         echo "aaaaaa" >> $DIR/$tdir/funlink
4105         echo "aaaaaa" >> $DIR/$tdir/frename
4106
4107         local open_new=`stat -c %Y $DIR/$tdir/fopen`
4108         local link_new=`stat -c %Y $DIR/$tdir/flink`
4109         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
4110         local rename_new=`stat -c %Y $DIR/$tdir/frename`
4111
4112         cat $DIR/$tdir/fopen > /dev/null
4113         ln $DIR/$tdir/flink $DIR/$tdir/flink2
4114         rm -f $DIR/$tdir/funlink2
4115         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
4116
4117         for (( i=0; i < 2; i++ )) ; do
4118                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
4119                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
4120                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
4121                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
4122
4123                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
4124                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
4125                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
4126                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
4127
4128                 cancel_lru_locks $OSC
4129                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4130         done
4131 }
4132 run_test 39b "mtime change on open, link, unlink, rename  ======"
4133
4134 # this should be set to past
4135 TEST_39_MTIME=`date -d "1 year ago" +%s`
4136
4137 # bug 11063
4138 test_39c() {
4139         touch $DIR1/$tfile
4140         sleep 2
4141         local mtime0=`stat -c %Y $DIR1/$tfile`
4142
4143         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4144         local mtime1=`stat -c %Y $DIR1/$tfile`
4145         [ "$mtime1" = $TEST_39_MTIME ] || \
4146                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
4147
4148         local d1=`date +%s`
4149         echo hello >> $DIR1/$tfile
4150         local d2=`date +%s`
4151         local mtime2=`stat -c %Y $DIR1/$tfile`
4152         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
4153                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
4154
4155         mv $DIR1/$tfile $DIR1/$tfile-1
4156
4157         for (( i=0; i < 2; i++ )) ; do
4158                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
4159                 [ "$mtime2" = "$mtime3" ] || \
4160                         error "mtime ($mtime2) changed (to $mtime3) on rename"
4161
4162                 cancel_lru_locks $OSC
4163                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4164         done
4165 }
4166 run_test 39c "mtime change on rename ==========================="
4167
4168 # bug 21114
4169 test_39d() {
4170         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4171
4172         touch $DIR1/$tfile
4173         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4174
4175         for (( i=0; i < 2; i++ )) ; do
4176                 local mtime=`stat -c %Y $DIR1/$tfile`
4177                 [ $mtime = $TEST_39_MTIME ] || \
4178                         error "mtime($mtime) is not set to $TEST_39_MTIME"
4179
4180                 cancel_lru_locks $OSC
4181                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4182         done
4183 }
4184 run_test 39d "create, utime, stat =============================="
4185
4186 # bug 21114
4187 test_39e() {
4188         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4189
4190         touch $DIR1/$tfile
4191         local mtime1=`stat -c %Y $DIR1/$tfile`
4192
4193         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4194
4195         for (( i=0; i < 2; i++ )) ; do
4196                 local mtime2=`stat -c %Y $DIR1/$tfile`
4197                 [ $mtime2 = $TEST_39_MTIME ] || \
4198                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4199
4200                 cancel_lru_locks $OSC
4201                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4202         done
4203 }
4204 run_test 39e "create, stat, utime, stat ========================"
4205
4206 # bug 21114
4207 test_39f() {
4208         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4209
4210         touch $DIR1/$tfile
4211         mtime1=`stat -c %Y $DIR1/$tfile`
4212
4213         sleep 2
4214         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4215
4216         for (( i=0; i < 2; i++ )) ; do
4217                 local mtime2=`stat -c %Y $DIR1/$tfile`
4218                 [ $mtime2 = $TEST_39_MTIME ] || \
4219                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4220
4221                 cancel_lru_locks $OSC
4222                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4223         done
4224 }
4225 run_test 39f "create, stat, sleep, utime, stat ================="
4226
4227 # bug 11063
4228 test_39g() {
4229         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4230
4231         echo hello >> $DIR1/$tfile
4232         local mtime1=`stat -c %Y $DIR1/$tfile`
4233
4234         sleep 2
4235         chmod o+r $DIR1/$tfile
4236
4237         for (( i=0; i < 2; i++ )) ; do
4238                 local mtime2=`stat -c %Y $DIR1/$tfile`
4239                 [ "$mtime1" = "$mtime2" ] || \
4240                         error "lost mtime: $mtime2, should be $mtime1"
4241
4242                 cancel_lru_locks $OSC
4243                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4244         done
4245 }
4246 run_test 39g "write, chmod, stat ==============================="
4247
4248 # bug 11063
4249 test_39h() {
4250         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4251
4252         touch $DIR1/$tfile
4253         sleep 1
4254
4255         local d1=`date`
4256         echo hello >> $DIR1/$tfile
4257         local mtime1=`stat -c %Y $DIR1/$tfile`
4258
4259         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4260         local d2=`date`
4261         if [ "$d1" != "$d2" ]; then
4262                 echo "write and touch not within one second"
4263         else
4264                 for (( i=0; i < 2; i++ )) ; do
4265                         local mtime2=`stat -c %Y $DIR1/$tfile`
4266                         [ "$mtime2" = $TEST_39_MTIME ] || \
4267                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
4268
4269                         cancel_lru_locks $OSC
4270                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4271                 done
4272         fi
4273 }
4274 run_test 39h "write, utime within one second, stat ============="
4275
4276 test_39i() {
4277         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4278
4279         touch $DIR1/$tfile
4280         sleep 1
4281
4282         echo hello >> $DIR1/$tfile
4283         local mtime1=`stat -c %Y $DIR1/$tfile`
4284
4285         mv $DIR1/$tfile $DIR1/$tfile-1
4286
4287         for (( i=0; i < 2; i++ )) ; do
4288                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4289
4290                 [ "$mtime1" = "$mtime2" ] || \
4291                         error "lost mtime: $mtime2, should be $mtime1"
4292
4293                 cancel_lru_locks $OSC
4294                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4295         done
4296 }
4297 run_test 39i "write, rename, stat =============================="
4298
4299 test_39j() {
4300         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4301
4302         start_full_debug_logging
4303         touch $DIR1/$tfile
4304         sleep 1
4305
4306         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
4307         lctl set_param fail_loc=0x80000412
4308         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
4309                 error "multiop failed"
4310         local multipid=$!
4311         local mtime1=`stat -c %Y $DIR1/$tfile`
4312
4313         mv $DIR1/$tfile $DIR1/$tfile-1
4314
4315         kill -USR1 $multipid
4316         wait $multipid || error "multiop close failed"
4317
4318         for (( i=0; i < 2; i++ )) ; do
4319                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4320                 [ "$mtime1" = "$mtime2" ] ||
4321                         error "mtime is lost on close: $mtime2, " \
4322                               "should be $mtime1"
4323
4324                 cancel_lru_locks
4325                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4326         done
4327         lctl set_param fail_loc=0
4328         stop_full_debug_logging
4329 }
4330 run_test 39j "write, rename, close, stat ======================="
4331
4332 test_39k() {
4333         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4334
4335         touch $DIR1/$tfile
4336         sleep 1
4337
4338         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
4339         local multipid=$!
4340         local mtime1=`stat -c %Y $DIR1/$tfile`
4341
4342         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4343
4344         kill -USR1 $multipid
4345         wait $multipid || error "multiop close failed"
4346
4347         for (( i=0; i < 2; i++ )) ; do
4348                 local mtime2=`stat -c %Y $DIR1/$tfile`
4349
4350                 [ "$mtime2" = $TEST_39_MTIME ] || \
4351                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
4352
4353                 cancel_lru_locks
4354                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4355         done
4356 }
4357 run_test 39k "write, utime, close, stat ========================"
4358
4359 # this should be set to future
4360 TEST_39_ATIME=`date -d "1 year" +%s`
4361
4362 test_39l() {
4363         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4364         remote_mds_nodsh && skip "remote MDS with nodsh"
4365
4366         local atime_diff=$(do_facet $SINGLEMDS \
4367                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4368         rm -rf $DIR/$tdir
4369         mkdir -p $DIR/$tdir
4370
4371         # test setting directory atime to future
4372         touch -a -d @$TEST_39_ATIME $DIR/$tdir
4373         local atime=$(stat -c %X $DIR/$tdir)
4374         [ "$atime" = $TEST_39_ATIME ] ||
4375                 error "atime is not set to future: $atime, $TEST_39_ATIME"
4376
4377         # test setting directory atime from future to now
4378         local now=$(date +%s)
4379         touch -a -d @$now $DIR/$tdir
4380
4381         atime=$(stat -c %X $DIR/$tdir)
4382         [ "$atime" -eq "$now"  ] ||
4383                 error "atime is not updated from future: $atime, $now"
4384
4385         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
4386         sleep 3
4387
4388         # test setting directory atime when now > dir atime + atime_diff
4389         local d1=$(date +%s)
4390         ls $DIR/$tdir
4391         local d2=$(date +%s)
4392         cancel_lru_locks mdc
4393         atime=$(stat -c %X $DIR/$tdir)
4394         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4395                 error "atime is not updated  : $atime, should be $d2"
4396
4397         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
4398         sleep 3
4399
4400         # test not setting directory atime when now < dir atime + atime_diff
4401         ls $DIR/$tdir
4402         cancel_lru_locks mdc
4403         atime=$(stat -c %X $DIR/$tdir)
4404         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4405                 error "atime is updated to $atime, should remain $d1<atime<$d2"
4406
4407         do_facet $SINGLEMDS \
4408                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4409 }
4410 run_test 39l "directory atime update ==========================="
4411
4412 test_39m() {
4413         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4414
4415         touch $DIR1/$tfile
4416         sleep 2
4417         local far_past_mtime=$(date -d "May 29 1953" +%s)
4418         local far_past_atime=$(date -d "Dec 17 1903" +%s)
4419
4420         touch -m -d @$far_past_mtime $DIR1/$tfile
4421         touch -a -d @$far_past_atime $DIR1/$tfile
4422
4423         for (( i=0; i < 2; i++ )) ; do
4424                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
4425                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
4426                         error "atime or mtime set incorrectly"
4427
4428                 cancel_lru_locks $OSC
4429                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4430         done
4431 }
4432 run_test 39m "test atime and mtime before 1970"
4433
4434 test_39n() { # LU-3832
4435         remote_mds_nodsh && skip "remote MDS with nodsh"
4436
4437         local atime_diff=$(do_facet $SINGLEMDS \
4438                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4439         local atime0
4440         local atime1
4441         local atime2
4442
4443         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
4444
4445         rm -rf $DIR/$tfile
4446         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
4447         atime0=$(stat -c %X $DIR/$tfile)
4448
4449         sleep 5
4450         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4451         atime1=$(stat -c %X $DIR/$tfile)
4452
4453         sleep 5
4454         cancel_lru_locks mdc
4455         cancel_lru_locks osc
4456         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4457         atime2=$(stat -c %X $DIR/$tfile)
4458
4459         do_facet $SINGLEMDS \
4460                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4461
4462         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
4463         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
4464 }
4465 run_test 39n "check that O_NOATIME is honored"
4466
4467 test_39o() {
4468         TESTDIR=$DIR/$tdir/$tfile
4469         [ -e $TESTDIR ] && rm -rf $TESTDIR
4470         mkdir -p $TESTDIR
4471         cd $TESTDIR
4472         links1=2
4473         ls
4474         mkdir a b
4475         ls
4476         links2=$(stat -c %h .)
4477         [ $(($links1 + 2)) != $links2 ] &&
4478                 error "wrong links count $(($links1 + 2)) != $links2"
4479         rmdir b
4480         links3=$(stat -c %h .)
4481         [ $(($links1 + 1)) != $links3 ] &&
4482                 error "wrong links count $links1 != $links3"
4483         return 0
4484 }
4485 run_test 39o "directory cached attributes updated after create"
4486
4487 test_39p() {
4488         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4489
4490         local MDTIDX=1
4491         TESTDIR=$DIR/$tdir/$tdir
4492         [ -e $TESTDIR ] && rm -rf $TESTDIR
4493         test_mkdir -p $TESTDIR
4494         cd $TESTDIR
4495         links1=2
4496         ls
4497         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
4498         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
4499         ls
4500         links2=$(stat -c %h .)
4501         [ $(($links1 + 2)) != $links2 ] &&
4502                 error "wrong links count $(($links1 + 2)) != $links2"
4503         rmdir remote_dir2
4504         links3=$(stat -c %h .)
4505         [ $(($links1 + 1)) != $links3 ] &&
4506                 error "wrong links count $links1 != $links3"
4507         return 0
4508 }
4509 run_test 39p "remote directory cached attributes updated after create ========"
4510
4511
4512 test_39q() { # LU-8041
4513         local testdir=$DIR/$tdir
4514         mkdir -p $testdir
4515         multiop_bg_pause $testdir D_c || error "multiop failed"
4516         local multipid=$!
4517         cancel_lru_locks mdc
4518         kill -USR1 $multipid
4519         local atime=$(stat -c %X $testdir)
4520         [ "$atime" -ne 0 ] || error "atime is zero"
4521 }
4522 run_test 39q "close won't zero out atime"
4523
4524 test_40() {
4525         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
4526         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
4527                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
4528         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
4529                 error "$tfile is not 4096 bytes in size"
4530 }
4531 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
4532
4533 test_41() {
4534         # bug 1553
4535         small_write $DIR/f41 18
4536 }
4537 run_test 41 "test small file write + fstat ====================="
4538
4539 count_ost_writes() {
4540         lctl get_param -n ${OSC}.*.stats |
4541                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
4542                         END { printf("%0.0f", writes) }'
4543 }
4544
4545 # decent default
4546 WRITEBACK_SAVE=500
4547 DIRTY_RATIO_SAVE=40
4548 MAX_DIRTY_RATIO=50
4549 BG_DIRTY_RATIO_SAVE=10
4550 MAX_BG_DIRTY_RATIO=25
4551
4552 start_writeback() {
4553         trap 0
4554         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
4555         # dirty_ratio, dirty_background_ratio
4556         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4557                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
4558                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
4559                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
4560         else
4561                 # if file not here, we are a 2.4 kernel
4562                 kill -CONT `pidof kupdated`
4563         fi
4564 }
4565
4566 stop_writeback() {
4567         # setup the trap first, so someone cannot exit the test at the
4568         # exact wrong time and mess up a machine
4569         trap start_writeback EXIT
4570         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
4571         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4572                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
4573                 sysctl -w vm.dirty_writeback_centisecs=0
4574                 sysctl -w vm.dirty_writeback_centisecs=0
4575                 # save and increase /proc/sys/vm/dirty_ratio
4576                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
4577                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
4578                 # save and increase /proc/sys/vm/dirty_background_ratio
4579                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
4580                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
4581         else
4582                 # if file not here, we are a 2.4 kernel
4583                 kill -STOP `pidof kupdated`
4584         fi
4585 }
4586
4587 # ensure that all stripes have some grant before we test client-side cache
4588 setup_test42() {
4589         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
4590                 dd if=/dev/zero of=$i bs=4k count=1
4591                 rm $i
4592         done
4593 }
4594
4595 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
4596 # file truncation, and file removal.
4597 test_42a() {
4598         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4599
4600         setup_test42
4601         cancel_lru_locks $OSC
4602         stop_writeback
4603         sync; sleep 1; sync # just to be safe
4604         BEFOREWRITES=`count_ost_writes`
4605         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
4606         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
4607         AFTERWRITES=`count_ost_writes`
4608         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
4609                 error "$BEFOREWRITES < $AFTERWRITES"
4610         start_writeback
4611 }
4612 run_test 42a "ensure that we don't flush on close"
4613
4614 test_42b() {
4615         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4616
4617         setup_test42
4618         cancel_lru_locks $OSC
4619         stop_writeback
4620         sync
4621         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
4622         BEFOREWRITES=$(count_ost_writes)
4623         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
4624         AFTERWRITES=$(count_ost_writes)
4625         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4626                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
4627         fi
4628         BEFOREWRITES=$(count_ost_writes)
4629         sync || error "sync: $?"
4630         AFTERWRITES=$(count_ost_writes)
4631         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4632                 error "$BEFOREWRITES < $AFTERWRITES on sync"
4633         fi
4634         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
4635         start_writeback
4636         return 0
4637 }
4638 run_test 42b "test destroy of file with cached dirty data ======"
4639
4640 # if these tests just want to test the effect of truncation,
4641 # they have to be very careful.  consider:
4642 # - the first open gets a {0,EOF}PR lock
4643 # - the first write conflicts and gets a {0, count-1}PW
4644 # - the rest of the writes are under {count,EOF}PW
4645 # - the open for truncate tries to match a {0,EOF}PR
4646 #   for the filesize and cancels the PWs.
4647 # any number of fixes (don't get {0,EOF} on open, match
4648 # composite locks, do smarter file size management) fix
4649 # this, but for now we want these tests to verify that
4650 # the cancellation with truncate intent works, so we
4651 # start the file with a full-file pw lock to match against
4652 # until the truncate.
4653 trunc_test() {
4654         test=$1
4655         file=$DIR/$test
4656         offset=$2
4657         cancel_lru_locks $OSC
4658         stop_writeback
4659         # prime the file with 0,EOF PW to match
4660         touch $file
4661         $TRUNCATE $file 0
4662         sync; sync
4663         # now the real test..
4664         dd if=/dev/zero of=$file bs=1024 count=100
4665         BEFOREWRITES=`count_ost_writes`
4666         $TRUNCATE $file $offset
4667         cancel_lru_locks $OSC
4668         AFTERWRITES=`count_ost_writes`
4669         start_writeback
4670 }
4671
4672 test_42c() {
4673         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4674
4675         trunc_test 42c 1024
4676         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
4677                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
4678         rm $file
4679 }
4680 run_test 42c "test partial truncate of file with cached dirty data"
4681
4682 test_42d() {
4683         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4684
4685         trunc_test 42d 0
4686         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
4687                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
4688         rm $file
4689 }
4690 run_test 42d "test complete truncate of file with cached dirty data"
4691
4692 test_42e() { # bug22074
4693         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4694
4695         local TDIR=$DIR/${tdir}e
4696         local pages=16 # hardcoded 16 pages, don't change it.
4697         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
4698         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
4699         local max_dirty_mb
4700         local warmup_files
4701
4702         test_mkdir $DIR/${tdir}e
4703         $LFS setstripe -c 1 $TDIR
4704         createmany -o $TDIR/f $files
4705
4706         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
4707
4708         # we assume that with $OSTCOUNT files, at least one of them will
4709         # be allocated on OST0.
4710         warmup_files=$((OSTCOUNT * max_dirty_mb))
4711         createmany -o $TDIR/w $warmup_files
4712
4713         # write a large amount of data into one file and sync, to get good
4714         # avail_grant number from OST.
4715         for ((i=0; i<$warmup_files; i++)); do
4716                 idx=$($LFS getstripe -i $TDIR/w$i)
4717                 [ $idx -ne 0 ] && continue
4718                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
4719                 break
4720         done
4721         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
4722         sync
4723         $LCTL get_param $proc_osc0/cur_dirty_bytes
4724         $LCTL get_param $proc_osc0/cur_grant_bytes
4725
4726         # create as much dirty pages as we can while not to trigger the actual
4727         # RPCs directly. but depends on the env, VFS may trigger flush during this
4728         # period, hopefully we are good.
4729         for ((i=0; i<$warmup_files; i++)); do
4730                 idx=$($LFS getstripe -i $TDIR/w$i)
4731                 [ $idx -ne 0 ] && continue
4732                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4733         done
4734         $LCTL get_param $proc_osc0/cur_dirty_bytes
4735         $LCTL get_param $proc_osc0/cur_grant_bytes
4736
4737         # perform the real test
4738         $LCTL set_param $proc_osc0/rpc_stats 0
4739         for ((;i<$files; i++)); do
4740                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
4741                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
4742         done
4743         sync
4744         $LCTL get_param $proc_osc0/rpc_stats
4745
4746         local percent=0
4747         local have_ppr=false
4748         $LCTL get_param $proc_osc0/rpc_stats |
4749                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4750                         # skip lines until we are at the RPC histogram data
4751                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4752                         $have_ppr || continue
4753
4754                         # we only want the percent stat for < 16 pages
4755                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4756
4757                         percent=$((percent + WPCT))
4758                         if [[ $percent -gt 15 ]]; then
4759                                 error "less than 16-pages write RPCs" \
4760                                       "$percent% > 15%"
4761                                 break
4762                         fi
4763                 done
4764         rm -rf $TDIR
4765 }
4766 run_test 42e "verify sub-RPC writes are not done synchronously"
4767
4768 test_43A() { # was test_43
4769         test_mkdir $DIR/$tdir
4770         cp -p /bin/ls $DIR/$tdir/$tfile
4771         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4772         pid=$!
4773         # give multiop a chance to open
4774         sleep 1
4775
4776         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4777         kill -USR1 $pid
4778 }
4779 run_test 43A "execution of file opened for write should return -ETXTBSY"
4780
4781 test_43a() {
4782         test_mkdir $DIR/$tdir
4783         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4784         $DIR/$tdir/sleep 60 &
4785         SLEEP_PID=$!
4786         # Make sure exec of $tdir/sleep wins race with truncate
4787         sleep 1
4788         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
4789         kill $SLEEP_PID
4790 }
4791 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4792
4793 test_43b() {
4794         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4795
4796         test_mkdir $DIR/$tdir
4797         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4798         $DIR/$tdir/sleep 60 &
4799         SLEEP_PID=$!
4800         # Make sure exec of $tdir/sleep wins race with truncate
4801         sleep 1
4802         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
4803         kill $SLEEP_PID
4804 }
4805 run_test 43b "truncate of file being executed should return -ETXTBSY"
4806
4807 test_43c() {
4808         local testdir="$DIR/$tdir"
4809         test_mkdir $testdir
4810         cp $SHELL $testdir/
4811         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4812                 ( cd $testdir && md5sum -c )
4813 }
4814 run_test 43c "md5sum of copy into lustre"
4815
4816 test_44A() { # was test_44
4817         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4818
4819         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4820         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4821 }
4822 run_test 44A "zero length read from a sparse stripe"
4823
4824 test_44a() {
4825         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4826                 awk '{ print $2 }')
4827         [ -z "$nstripe" ] && skip "can't get stripe info"
4828         [[ $nstripe -gt $OSTCOUNT ]] &&
4829                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4830
4831         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4832                 awk '{ print $2 }')
4833         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4834                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4835                         awk '{ print $2 }')
4836         fi
4837
4838         OFFSETS="0 $((stride/2)) $((stride-1))"
4839         for offset in $OFFSETS; do
4840                 for i in $(seq 0 $((nstripe-1))); do
4841                         local GLOBALOFFSETS=""
4842                         # size in Bytes
4843                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4844                         local myfn=$DIR/d44a-$size
4845                         echo "--------writing $myfn at $size"
4846                         ll_sparseness_write $myfn $size ||
4847                                 error "ll_sparseness_write"
4848                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4849                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4850                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4851
4852                         for j in $(seq 0 $((nstripe-1))); do
4853                                 # size in Bytes
4854                                 size=$((((j + $nstripe )*$stride + $offset)))
4855                                 ll_sparseness_write $myfn $size ||
4856                                         error "ll_sparseness_write"
4857                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4858                         done
4859                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4860                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4861                         rm -f $myfn
4862                 done
4863         done
4864 }
4865 run_test 44a "test sparse pwrite ==============================="
4866
4867 dirty_osc_total() {
4868         tot=0
4869         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4870                 tot=$(($tot + $d))
4871         done
4872         echo $tot
4873 }
4874 do_dirty_record() {
4875         before=`dirty_osc_total`
4876         echo executing "\"$*\""
4877         eval $*
4878         after=`dirty_osc_total`
4879         echo before $before, after $after
4880 }
4881 test_45() {
4882         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4883
4884         f="$DIR/f45"
4885         # Obtain grants from OST if it supports it
4886         echo blah > ${f}_grant
4887         stop_writeback
4888         sync
4889         do_dirty_record "echo blah > $f"
4890         [[ $before -eq $after ]] && error "write wasn't cached"
4891         do_dirty_record "> $f"
4892         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4893         do_dirty_record "echo blah > $f"
4894         [[ $before -eq $after ]] && error "write wasn't cached"
4895         do_dirty_record "sync"
4896         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4897         do_dirty_record "echo blah > $f"
4898         [[ $before -eq $after ]] && error "write wasn't cached"
4899         do_dirty_record "cancel_lru_locks osc"
4900         [[ $before -gt $after ]] ||
4901                 error "lock cancellation didn't lower dirty count"
4902         start_writeback
4903 }
4904 run_test 45 "osc io page accounting ============================"
4905
4906 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4907 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4908 # objects offset and an assert hit when an rpc was built with 1023's mapped
4909 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4910 test_46() {
4911         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4912
4913         f="$DIR/f46"
4914         stop_writeback
4915         sync
4916         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4917         sync
4918         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
4919         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4920         sync
4921         start_writeback
4922 }
4923 run_test 46 "dirtying a previously written page ================"
4924
4925 # test_47 is removed "Device nodes check" is moved to test_28
4926
4927 test_48a() { # bug 2399
4928         [ "$mds1_FSTYPE" = "zfs" ] &&
4929         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
4930                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
4931
4932         test_mkdir $DIR/$tdir
4933         cd $DIR/$tdir
4934         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
4935         test_mkdir $DIR/$tdir
4936         touch foo || error "'touch foo' failed after recreating cwd"
4937         test_mkdir bar
4938         touch .foo || error "'touch .foo' failed after recreating cwd"
4939         test_mkdir .bar
4940         ls . > /dev/null || error "'ls .' failed after recreating cwd"
4941         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4942         cd . || error "'cd .' failed after recreating cwd"
4943         mkdir . && error "'mkdir .' worked after recreating cwd"
4944         rmdir . && error "'rmdir .' worked after recreating cwd"
4945         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4946         cd .. || error "'cd ..' failed after recreating cwd"
4947 }
4948 run_test 48a "Access renamed working dir (should return errors)="
4949
4950 test_48b() { # bug 2399
4951         rm -rf $DIR/$tdir
4952         test_mkdir $DIR/$tdir
4953         cd $DIR/$tdir
4954         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4955         touch foo && error "'touch foo' worked after removing cwd"
4956         mkdir foo && error "'mkdir foo' worked after removing cwd"
4957         touch .foo && error "'touch .foo' worked after removing cwd"
4958         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4959         ls . > /dev/null && error "'ls .' worked after removing cwd"
4960         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4961         mkdir . && error "'mkdir .' worked after removing cwd"
4962         rmdir . && error "'rmdir .' worked after removing cwd"
4963         ln -s . foo && error "'ln -s .' worked after removing cwd"
4964         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4965 }
4966 run_test 48b "Access removed working dir (should return errors)="
4967
4968 test_48c() { # bug 2350
4969         #lctl set_param debug=-1
4970         #set -vx
4971         rm -rf $DIR/$tdir
4972         test_mkdir -p $DIR/$tdir/dir
4973         cd $DIR/$tdir/dir
4974         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4975         $TRACE touch foo && error "touch foo worked after removing cwd"
4976         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4977         touch .foo && error "touch .foo worked after removing cwd"
4978         mkdir .foo && error "mkdir .foo worked after removing cwd"
4979         $TRACE ls . && error "'ls .' worked after removing cwd"
4980         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4981         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4982         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4983         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4984         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4985 }
4986 run_test 48c "Access removed working subdir (should return errors)"
4987
4988 test_48d() { # bug 2350
4989         #lctl set_param debug=-1
4990         #set -vx
4991         rm -rf $DIR/$tdir
4992         test_mkdir -p $DIR/$tdir/dir
4993         cd $DIR/$tdir/dir
4994         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4995         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4996         $TRACE touch foo && error "'touch foo' worked after removing parent"
4997         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4998         touch .foo && error "'touch .foo' worked after removing parent"
4999         mkdir .foo && error "mkdir .foo worked after removing parent"
5000         $TRACE ls . && error "'ls .' worked after removing parent"
5001         $TRACE ls .. && error "'ls ..' worked after removing parent"
5002         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
5003         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
5004         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
5005         true
5006 }
5007 run_test 48d "Access removed parent subdir (should return errors)"
5008
5009 test_48e() { # bug 4134
5010         #lctl set_param debug=-1
5011         #set -vx
5012         rm -rf $DIR/$tdir
5013         test_mkdir -p $DIR/$tdir/dir
5014         cd $DIR/$tdir/dir
5015         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5016         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5017         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
5018         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
5019         # On a buggy kernel addition of "touch foo" after cd .. will
5020         # produce kernel oops in lookup_hash_it
5021         touch ../foo && error "'cd ..' worked after recreate parent"
5022         cd $DIR
5023         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
5024 }
5025 run_test 48e "Access to recreated parent subdir (should return errors)"
5026
5027 test_49() { # LU-1030
5028         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5029         remote_ost_nodsh && skip "remote OST with nodsh"
5030
5031         # get ost1 size - $FSNAME-OST0000
5032         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
5033                 awk '{ print $4 }')
5034         # write 800M at maximum
5035         [[ $ost1_size -lt 2 ]] && ost1_size=2
5036         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
5037
5038         $LFS setstripe -c 1 -i 0 $DIR/$tfile
5039         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
5040         local dd_pid=$!
5041
5042         # change max_pages_per_rpc while writing the file
5043         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
5044         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
5045         # loop until dd process exits
5046         while ps ax -opid | grep -wq $dd_pid; do
5047                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
5048                 sleep $((RANDOM % 5 + 1))
5049         done
5050         # restore original max_pages_per_rpc
5051         $LCTL set_param $osc1_mppc=$orig_mppc
5052         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
5053 }
5054 run_test 49 "Change max_pages_per_rpc won't break osc extent"
5055
5056 test_50() {
5057         # bug 1485
5058         test_mkdir $DIR/$tdir
5059         cd $DIR/$tdir
5060         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
5061 }
5062 run_test 50 "special situations: /proc symlinks  ==============="
5063
5064 test_51a() {    # was test_51
5065         # bug 1516 - create an empty entry right after ".." then split dir
5066         test_mkdir -c1 $DIR/$tdir
5067         touch $DIR/$tdir/foo
5068         $MCREATE $DIR/$tdir/bar
5069         rm $DIR/$tdir/foo
5070         createmany -m $DIR/$tdir/longfile 201
5071         FNUM=202
5072         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
5073                 $MCREATE $DIR/$tdir/longfile$FNUM
5074                 FNUM=$(($FNUM + 1))
5075                 echo -n "+"
5076         done
5077         echo
5078         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
5079 }
5080 run_test 51a "special situations: split htree with empty entry =="
5081
5082 cleanup_print_lfs_df () {
5083         trap 0
5084         $LFS df
5085         $LFS df -i
5086 }
5087
5088 test_51b() {
5089         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5090
5091         local dir=$DIR/$tdir
5092         local nrdirs=$((65536 + 100))
5093
5094         # cleanup the directory
5095         rm -fr $dir
5096
5097         test_mkdir -c1 $dir
5098
5099         $LFS df
5100         $LFS df -i
5101         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
5102         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
5103         [[ $numfree -lt $nrdirs ]] &&
5104                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
5105
5106         # need to check free space for the directories as well
5107         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
5108         numfree=$(( blkfree / $(fs_inode_ksize) ))
5109         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
5110
5111         trap cleanup_print_lfs_df EXIT
5112
5113         # create files
5114         createmany -d $dir/d $nrdirs || {
5115                 unlinkmany $dir/d $nrdirs
5116                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
5117         }
5118
5119         # really created :
5120         nrdirs=$(ls -U $dir | wc -l)
5121
5122         # unlink all but 100 subdirectories, then check it still works
5123         local left=100
5124         local delete=$((nrdirs - left))
5125
5126         $LFS df
5127         $LFS df -i
5128
5129         # for ldiskfs the nlink count should be 1, but this is OSD specific
5130         # and so this is listed for informational purposes only
5131         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
5132         unlinkmany -d $dir/d $delete ||
5133                 error "unlink of first $delete subdirs failed"
5134
5135         echo "nlink between: $(stat -c %h $dir)"
5136         local found=$(ls -U $dir | wc -l)
5137         [ $found -ne $left ] &&
5138                 error "can't find subdirs: found only $found, expected $left"
5139
5140         unlinkmany -d $dir/d $delete $left ||
5141                 error "unlink of second $left subdirs failed"
5142         # regardless of whether the backing filesystem tracks nlink accurately
5143         # or not, the nlink count shouldn't be more than "." and ".." here
5144         local after=$(stat -c %h $dir)
5145         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
5146                 echo "nlink after: $after"
5147
5148         cleanup_print_lfs_df
5149 }
5150 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
5151
5152 test_51d() {
5153         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5154         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
5155
5156         test_mkdir $DIR/$tdir
5157         createmany -o $DIR/$tdir/t- 1000
5158         $LFS getstripe $DIR/$tdir > $TMP/$tfile
5159         for N in $(seq 0 $((OSTCOUNT - 1))); do
5160                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
5161                         END { printf("%0.0f", objs) }' $TMP/$tfile)
5162                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
5163                         '($1 == '$N') { objs += 1 } \
5164                         END { printf("%0.0f", objs) }')
5165                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
5166         done
5167         unlinkmany $DIR/$tdir/t- 1000
5168
5169         NLAST=0
5170         for N in $(seq 1 $((OSTCOUNT - 1))); do
5171                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
5172                         error "OST $N has less objects vs OST $NLAST" \
5173                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5174                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
5175                         error "OST $N has less objects vs OST $NLAST" \
5176                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5177
5178                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
5179                         error "OST $N has less #0 objects vs OST $NLAST" \
5180                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5181                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
5182                         error "OST $N has less #0 objects vs OST $NLAST" \
5183                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5184                 NLAST=$N
5185         done
5186         rm -f $TMP/$tfile
5187 }
5188 run_test 51d "check object distribution"
5189
5190 test_51e() {
5191         if [ "$mds1_FSTYPE" != ldiskfs ]; then
5192                 skip_env "ldiskfs only test"
5193         fi
5194
5195         test_mkdir -c1 $DIR/$tdir
5196         test_mkdir -c1 $DIR/$tdir/d0
5197
5198         touch $DIR/$tdir/d0/foo
5199         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
5200                 error "file exceed 65000 nlink limit!"
5201         unlinkmany $DIR/$tdir/d0/f- 65001
5202         return 0
5203 }
5204 run_test 51e "check file nlink limit"
5205
5206 test_51f() {
5207         test_mkdir $DIR/$tdir
5208
5209         local max=100000
5210         local ulimit_old=$(ulimit -n)
5211         local spare=20 # number of spare fd's for scripts/libraries, etc.
5212         local mdt=$($LFS getstripe -m $DIR/$tdir)
5213         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
5214
5215         echo "MDT$mdt numfree=$numfree, max=$max"
5216         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
5217         if [ $((numfree + spare)) -gt $ulimit_old ]; then
5218                 while ! ulimit -n $((numfree + spare)); do
5219                         numfree=$((numfree * 3 / 4))
5220                 done
5221                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
5222         else
5223                 echo "left ulimit at $ulimit_old"
5224         fi
5225
5226         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
5227                 unlinkmany $DIR/$tdir/f $numfree
5228                 error "create+open $numfree files in $DIR/$tdir failed"
5229         }
5230         ulimit -n $ulimit_old
5231
5232         # if createmany exits at 120s there will be fewer than $numfree files
5233         unlinkmany $DIR/$tdir/f $numfree || true
5234 }
5235 run_test 51f "check many open files limit"
5236
5237 test_52a() {
5238         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
5239         test_mkdir $DIR/$tdir
5240         touch $DIR/$tdir/foo
5241         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
5242         echo bar >> $DIR/$tdir/foo || error "append bar failed"
5243         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5244         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5245         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5246                                         error "link worked"
5247         echo foo >> $DIR/$tdir/foo || error "append foo failed"
5248         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5249         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
5250                                                      error "lsattr"
5251         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
5252         cp -r $DIR/$tdir $TMP/
5253         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
5254 }
5255 run_test 52a "append-only flag test (should return errors)"
5256
5257 test_52b() {
5258         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
5259         test_mkdir $DIR/$tdir
5260         touch $DIR/$tdir/foo
5261         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
5262         cat test > $DIR/$tdir/foo && error "cat test worked"
5263         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5264         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5265         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5266                                         error "link worked"
5267         echo foo >> $DIR/$tdir/foo && error "echo worked"
5268         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5269         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
5270         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
5271         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
5272                                                         error "lsattr"
5273         chattr -i $DIR/$tdir/foo || error "chattr failed"
5274
5275         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
5276 }
5277 run_test 52b "immutable flag test (should return errors) ======="
5278
5279 test_53() {
5280         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5281         remote_mds_nodsh && skip "remote MDS with nodsh"
5282         remote_ost_nodsh && skip "remote OST with nodsh"
5283
5284         local param
5285         local param_seq
5286         local ostname
5287         local mds_last
5288         local mds_last_seq
5289         local ost_last
5290         local ost_last_seq
5291         local ost_last_id
5292         local ostnum
5293         local node
5294         local found=false
5295         local support_last_seq=true
5296
5297         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
5298                 support_last_seq=false
5299
5300         # only test MDT0000
5301         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
5302         local value
5303         for value in $(do_facet $SINGLEMDS \
5304                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
5305                 param=$(echo ${value[0]} | cut -d "=" -f1)
5306                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
5307
5308                 if $support_last_seq; then
5309                         param_seq=$(echo $param |
5310                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
5311                         mds_last_seq=$(do_facet $SINGLEMDS \
5312                                        $LCTL get_param -n $param_seq)
5313                 fi
5314                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
5315
5316                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
5317                 node=$(facet_active_host ost$((ostnum+1)))
5318                 param="obdfilter.$ostname.last_id"
5319                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
5320                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
5321                         ost_last_id=$ost_last
5322
5323                         if $support_last_seq; then
5324                                 ost_last_id=$(echo $ost_last |
5325                                               awk -F':' '{print $2}' |
5326                                               sed -e "s/^0x//g")
5327                                 ost_last_seq=$(echo $ost_last |
5328                                                awk -F':' '{print $1}')
5329                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
5330                         fi
5331
5332                         if [[ $ost_last_id != $mds_last ]]; then
5333                                 error "$ost_last_id != $mds_last"
5334                         else
5335                                 found=true
5336                                 break
5337                         fi
5338                 done
5339         done
5340         $found || error "can not match last_seq/last_id for $mdtosc"
5341         return 0
5342 }
5343 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
5344
5345 test_54a() {
5346         perl -MSocket -e ';' || skip "no Socket perl module installed"
5347
5348         $SOCKETSERVER $DIR/socket ||
5349                 error "$SOCKETSERVER $DIR/socket failed: $?"
5350         $SOCKETCLIENT $DIR/socket ||
5351                 error "$SOCKETCLIENT $DIR/socket failed: $?"
5352         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
5353 }
5354 run_test 54a "unix domain socket test =========================="
5355
5356 test_54b() {
5357         f="$DIR/f54b"
5358         mknod $f c 1 3
5359         chmod 0666 $f
5360         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
5361 }
5362 run_test 54b "char device works in lustre ======================"
5363
5364 find_loop_dev() {
5365         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
5366         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
5367         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
5368
5369         for i in $(seq 3 7); do
5370                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
5371                 LOOPDEV=$LOOPBASE$i
5372                 LOOPNUM=$i
5373                 break
5374         done
5375 }
5376
5377 cleanup_54c() {
5378         local rc=0
5379         loopdev="$DIR/loop54c"
5380
5381         trap 0
5382         $UMOUNT $DIR/$tdir || rc=$?
5383         losetup -d $loopdev || true
5384         losetup -d $LOOPDEV || true
5385         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
5386         return $rc
5387 }
5388
5389 test_54c() {
5390         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5391
5392         loopdev="$DIR/loop54c"
5393
5394         find_loop_dev
5395         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
5396         trap cleanup_54c EXIT
5397         mknod $loopdev b 7 $LOOPNUM
5398         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
5399         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
5400         losetup $loopdev $DIR/$tfile ||
5401                 error "can't set up $loopdev for $DIR/$tfile"
5402         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
5403         test_mkdir $DIR/$tdir
5404         mount -t ext2 $loopdev $DIR/$tdir ||
5405                 error "error mounting $loopdev on $DIR/$tdir"
5406         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
5407                 error "dd write"
5408         df $DIR/$tdir
5409         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
5410                 error "dd read"
5411         cleanup_54c
5412 }
5413 run_test 54c "block device works in lustre ====================="
5414
5415 test_54d() {
5416         f="$DIR/f54d"
5417         string="aaaaaa"
5418         mknod $f p
5419         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
5420 }
5421 run_test 54d "fifo device works in lustre ======================"
5422
5423 test_54e() {
5424         f="$DIR/f54e"
5425         string="aaaaaa"
5426         cp -aL /dev/console $f
5427         echo $string > $f || error "echo $string to $f failed"
5428 }
5429 run_test 54e "console/tty device works in lustre ======================"
5430
5431 test_56a() {
5432         local numfiles=3
5433         local dir=$DIR/$tdir
5434
5435         rm -rf $dir
5436         test_mkdir -p $dir/dir
5437         for i in $(seq $numfiles); do
5438                 touch $dir/file$i
5439                 touch $dir/dir/file$i
5440         done
5441
5442         local numcomp=$($LFS getstripe --component-count $dir)
5443
5444         [[ $numcomp == 0 ]] && numcomp=1
5445
5446         # test lfs getstripe with --recursive
5447         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
5448
5449         [[ $filenum -eq $((numfiles * 2)) ]] ||
5450                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
5451         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
5452         [[ $filenum -eq $numfiles ]] ||
5453                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
5454         echo "$LFS getstripe showed obdidx or l_ost_idx"
5455
5456         # test lfs getstripe with file instead of dir
5457         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
5458         [[ $filenum -eq 1 ]] ||
5459                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
5460         echo "$LFS getstripe file1 passed"
5461
5462         #test lfs getstripe with --verbose
5463         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
5464         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5465                 error "$LFS getstripe --verbose $dir: "\
5466                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
5467         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
5468                 error "$LFS getstripe $dir: showed lmm_magic"
5469
5470         #test lfs getstripe with -v prints lmm_fid
5471         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
5472         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5473                 error "$LFS getstripe -v $dir: "\
5474                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
5475         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
5476                 error "$LFS getstripe $dir: showed lmm_fid by default"
5477         echo "$LFS getstripe --verbose passed"
5478
5479         #check for FID information
5480         local fid1=$($LFS getstripe --fid $dir/file1)
5481         local fid2=$($LFS getstripe --verbose $dir/file1 |
5482                      awk '/lmm_fid: / { print $2; exit; }')
5483         local fid3=$($LFS path2fid $dir/file1)
5484
5485         [ "$fid1" != "$fid2" ] &&
5486                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
5487         [ "$fid1" != "$fid3" ] &&
5488                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
5489         echo "$LFS getstripe --fid passed"
5490
5491         #test lfs getstripe with --obd
5492         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
5493                 error "$LFS getstripe --obd wrong_uuid: should return error"
5494
5495         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5496
5497         local ostidx=1
5498         local obduuid=$(ostuuid_from_index $ostidx)
5499         local found=$($LFS getstripe -r --obd $obduuid $dir |
5500                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
5501
5502         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
5503         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
5504                 ((filenum--))
5505         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
5506                 ((filenum--))
5507
5508         [[ $found -eq $filenum ]] ||
5509                 error "$LFS getstripe --obd: found $found expect $filenum"
5510         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
5511                 sed '/^[         ]*'${ostidx}'[  ]/d' |
5512                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
5513                 error "$LFS getstripe --obd: should not show file on other obd"
5514         echo "$LFS getstripe --obd passed"
5515 }
5516 run_test 56a "check $LFS getstripe"
5517
5518 test_56b() {
5519         local dir=$DIR/$tdir
5520         local numdirs=3
5521
5522         test_mkdir $dir
5523         for i in $(seq $numdirs); do
5524                 test_mkdir $dir/dir$i
5525         done
5526
5527         # test lfs getdirstripe default mode is non-recursion, which is
5528         # different from lfs getstripe
5529         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
5530
5531         [[ $dircnt -eq 1 ]] ||
5532                 error "$LFS getdirstripe: found $dircnt, not 1"
5533         dircnt=$($LFS getdirstripe --recursive $dir |
5534                 grep -c lmv_stripe_count)
5535         [[ $dircnt -eq $((numdirs + 1)) ]] ||
5536                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
5537 }
5538 run_test 56b "check $LFS getdirstripe"
5539
5540 test_56c() {
5541         remote_ost_nodsh && skip "remote OST with nodsh"
5542
5543         local ost_idx=0
5544         local ost_name=$(ostname_from_index $ost_idx)
5545         local old_status=$(ost_dev_status $ost_idx)
5546
5547         [[ -z "$old_status" ]] ||
5548                 skip_env "OST $ost_name is in $old_status status"
5549
5550         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
5551         [[ $OST1_VERSION -ge $(version_code 2.12.55) ]] && do_facet ost1 \
5552                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
5553         sleep_maxage
5554
5555         local new_status=$(ost_dev_status $ost_idx)
5556
5557         [[ "$new_status" =~ "D" ]] ||
5558                 error "$ost_name status is '$new_status', missing 'D'"
5559         if [[ $OST1_VERSION -ge $(version_code 2.12.55) ]]; then
5560                 [[ "$new_status" =~ "N" ]] ||
5561                         error "$ost_name status is '$new_status', missing 'N'"
5562         fi
5563
5564         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
5565         [[ $OST1_VERSION -ge $(version_code 2.12.55) ]] && do_facet ost1 \
5566                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
5567         sleep_maxage
5568
5569         new_status=$(ost_dev_status $ost_idx)
5570         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
5571                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
5572 }
5573 run_test 56c "check 'lfs df' showing device status"
5574
5575 NUMFILES=3
5576 NUMDIRS=3
5577 setup_56() {
5578         local local_tdir="$1"
5579         local local_numfiles="$2"
5580         local local_numdirs="$3"
5581         local dir_params="$4"
5582         local dir_stripe_params="$5"
5583
5584         if [ ! -d "$local_tdir" ] ; then
5585                 test_mkdir -p $dir_stripe_params $local_tdir
5586                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
5587                 for i in $(seq $local_numfiles) ; do
5588                         touch $local_tdir/file$i
5589                 done
5590                 for i in $(seq $local_numdirs) ; do
5591                         test_mkdir $dir_stripe_params $local_tdir/dir$i
5592                         for j in $(seq $local_numfiles) ; do
5593                                 touch $local_tdir/dir$i/file$j
5594                         done
5595                 done
5596         fi
5597 }
5598
5599 setup_56_special() {
5600         local local_tdir=$1
5601         local local_numfiles=$2
5602         local local_numdirs=$3
5603
5604         setup_56 $local_tdir $local_numfiles $local_numdirs
5605
5606         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
5607                 for i in $(seq $local_numfiles) ; do
5608                         mknod $local_tdir/loop${i}b b 7 $i
5609                         mknod $local_tdir/null${i}c c 1 3
5610                         ln -s $local_tdir/file1 $local_tdir/link${i}
5611                 done
5612                 for i in $(seq $local_numdirs) ; do
5613                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
5614                         mknod $local_tdir/dir$i/null${i}c c 1 3
5615                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
5616                 done
5617         fi
5618 }
5619
5620 test_56g() {
5621         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5622         local expected=$(($NUMDIRS + 2))
5623
5624         setup_56 $dir $NUMFILES $NUMDIRS
5625
5626         # test lfs find with -name
5627         for i in $(seq $NUMFILES) ; do
5628                 local nums=$($LFS find -name "*$i" $dir | wc -l)
5629
5630                 [ $nums -eq $expected ] ||
5631                         error "lfs find -name '*$i' $dir wrong: "\
5632                               "found $nums, expected $expected"
5633         done
5634 }
5635 run_test 56g "check lfs find -name"
5636
5637 test_56h() {
5638         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5639         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
5640
5641         setup_56 $dir $NUMFILES $NUMDIRS
5642
5643         # test lfs find with ! -name
5644         for i in $(seq $NUMFILES) ; do
5645                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
5646
5647                 [ $nums -eq $expected ] ||
5648                         error "lfs find ! -name '*$i' $dir wrong: "\
5649                               "found $nums, expected $expected"
5650         done
5651 }
5652 run_test 56h "check lfs find ! -name"
5653
5654 test_56i() {
5655         local dir=$DIR/$tdir
5656
5657         test_mkdir $dir
5658
5659         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
5660         local out=$($cmd)
5661
5662         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
5663 }
5664 run_test 56i "check 'lfs find -ost UUID' skips directories"
5665
5666 test_56j() {
5667         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5668
5669         setup_56_special $dir $NUMFILES $NUMDIRS
5670
5671         local expected=$((NUMDIRS + 1))
5672         local cmd="$LFS find -type d $dir"
5673         local nums=$($cmd | wc -l)
5674
5675         [ $nums -eq $expected ] ||
5676                 error "'$cmd' wrong: found $nums, expected $expected"
5677 }
5678 run_test 56j "check lfs find -type d"
5679
5680 test_56k() {
5681         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5682
5683         setup_56_special $dir $NUMFILES $NUMDIRS
5684
5685         local expected=$(((NUMDIRS + 1) * NUMFILES))
5686         local cmd="$LFS find -type f $dir"
5687         local nums=$($cmd | wc -l)
5688
5689         [ $nums -eq $expected ] ||
5690                 error "'$cmd' wrong: found $nums, expected $expected"
5691 }
5692 run_test 56k "check lfs find -type f"
5693
5694 test_56l() {
5695         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5696
5697         setup_56_special $dir $NUMFILES $NUMDIRS
5698
5699         local expected=$((NUMDIRS + NUMFILES))
5700         local cmd="$LFS find -type b $dir"
5701         local nums=$($cmd | wc -l)
5702
5703         [ $nums -eq $expected ] ||
5704                 error "'$cmd' wrong: found $nums, expected $expected"
5705 }
5706 run_test 56l "check lfs find -type b"
5707
5708 test_56m() {
5709         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5710
5711         setup_56_special $dir $NUMFILES $NUMDIRS
5712
5713         local expected=$((NUMDIRS + NUMFILES))
5714         local cmd="$LFS find -type c $dir"
5715         local nums=$($cmd | wc -l)
5716         [ $nums -eq $expected ] ||
5717                 error "'$cmd' wrong: found $nums, expected $expected"
5718 }
5719 run_test 56m "check lfs find -type c"
5720
5721 test_56n() {
5722         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5723         setup_56_special $dir $NUMFILES $NUMDIRS
5724
5725         local expected=$((NUMDIRS + NUMFILES))
5726         local cmd="$LFS find -type l $dir"
5727         local nums=$($cmd | wc -l)
5728
5729         [ $nums -eq $expected ] ||
5730                 error "'$cmd' wrong: found $nums, expected $expected"
5731 }
5732 run_test 56n "check lfs find -type l"
5733
5734 test_56o() {
5735         local dir=$DIR/$tdir
5736
5737         setup_56 $dir $NUMFILES $NUMDIRS
5738         utime $dir/file1 > /dev/null || error "utime (1)"
5739         utime $dir/file2 > /dev/null || error "utime (2)"
5740         utime $dir/dir1 > /dev/null || error "utime (3)"
5741         utime $dir/dir2 > /dev/null || error "utime (4)"
5742         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5743         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5744
5745         local expected=4
5746         local nums=$($LFS find -mtime +0 $dir | wc -l)
5747
5748         [ $nums -eq $expected ] ||
5749                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5750
5751         expected=12
5752         cmd="$LFS find -mtime 0 $dir"
5753         nums=$($cmd | wc -l)
5754         [ $nums -eq $expected ] ||
5755                 error "'$cmd' wrong: found $nums, expected $expected"
5756 }
5757 run_test 56o "check lfs find -mtime for old files"
5758
5759 test_56ob() {
5760         local dir=$DIR/$tdir
5761         local expected=1
5762         local count=0
5763
5764         # just to make sure there is something that won't be found
5765         test_mkdir $dir
5766         touch $dir/$tfile.now
5767
5768         for age in year week day hour min; do
5769                 count=$((count + 1))
5770
5771                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
5772                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
5773                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
5774
5775                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
5776                 local nums=$($cmd | wc -l)
5777                 [ $nums -eq $expected ] ||
5778                         error "'$cmd' wrong: found $nums, expected $expected"
5779
5780                 cmd="$LFS find $dir -atime $count${age:0:1}"
5781                 nums=$($cmd | wc -l)
5782                 [ $nums -eq $expected ] ||
5783                         error "'$cmd' wrong: found $nums, expected $expected"
5784         done
5785
5786         sleep 2
5787         cmd="$LFS find $dir -ctime +1s -type f"
5788         nums=$($cmd | wc -l)
5789         (( $nums == $count * 2 + 1)) ||
5790                 error "'$cmd' wrong: found $nums, expected $((expected*2+1))"
5791 }
5792 run_test 56ob "check lfs find -atime -mtime -ctime with units"
5793
5794 test_newerXY_base() {
5795         local x=$1
5796         local y=$2
5797         local dir=$DIR/$tdir
5798         local ref
5799         local negref
5800
5801         if [ $y == "t" ]; then
5802                 ref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5803         else
5804                 ref=$DIR/$tfile.newer
5805                 touch $ref || error "touch $ref failed"
5806         fi
5807         sleep 2
5808         setup_56 $dir $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
5809         sleep 2
5810         if [ $y == "t" ]; then
5811                 negref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5812         else
5813                 negref=$DIR/$tfile.newerneg
5814                 touch $negref || error "touch $negref failed"
5815         fi
5816
5817         local cmd="$LFS find $dir -newer$x$y $ref"
5818         local nums=$(eval $cmd | wc -l)
5819         local expected=$(((NUMFILES + 2) * NUMDIRS + 1))
5820
5821         [ $nums -eq $expected ] ||
5822                 error "'$cmd' wrong: found $nums, expected $expected"
5823
5824         cmd="$LFS find $dir ! -newer$x$y $negref"
5825         nums=$(eval $cmd | wc -l)
5826         [ $nums -eq $expected ] ||
5827                 error "'$cmd' wrong: found $nums, expected $expected"
5828
5829         cmd="$LFS find $dir -newer$x$y $ref ! -newer$x$y $negref"
5830         nums=$(eval $cmd | wc -l)
5831         [ $nums -eq $expected ] ||
5832                 error "'$cmd' wrong: found $nums, expected $expected"
5833
5834         rm -rf $DIR/*
5835 }
5836
5837 test_56oc() {
5838         test_newerXY_base "a" "a"
5839         test_newerXY_base "a" "m"
5840         test_newerXY_base "a" "c"
5841         test_newerXY_base "m" "a"
5842         test_newerXY_base "m" "m"
5843         test_newerXY_base "m" "c"
5844         test_newerXY_base "c" "a"
5845         test_newerXY_base "c" "m"
5846         test_newerXY_base "c" "c"
5847         test_newerXY_base "a" "t"
5848         test_newerXY_base "m" "t"
5849         test_newerXY_base "c" "t"
5850 }
5851 run_test 56oc "check lfs find -newerXY work"
5852
5853 test_56p() {
5854         [ $RUNAS_ID -eq $UID ] &&
5855                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5856
5857         local dir=$DIR/$tdir
5858
5859         setup_56 $dir $NUMFILES $NUMDIRS
5860         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
5861
5862         local expected=$NUMFILES
5863         local cmd="$LFS find -uid $RUNAS_ID $dir"
5864         local nums=$($cmd | wc -l)
5865
5866         [ $nums -eq $expected ] ||
5867                 error "'$cmd' wrong: found $nums, expected $expected"
5868
5869         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
5870         cmd="$LFS find ! -uid $RUNAS_ID $dir"
5871         nums=$($cmd | wc -l)
5872         [ $nums -eq $expected ] ||
5873                 error "'$cmd' wrong: found $nums, expected $expected"
5874 }
5875 run_test 56p "check lfs find -uid and ! -uid"
5876
5877 test_56q() {
5878         [ $RUNAS_ID -eq $UID ] &&
5879                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5880
5881         local dir=$DIR/$tdir
5882
5883         setup_56 $dir $NUMFILES $NUMDIRS
5884         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
5885
5886         local expected=$NUMFILES
5887         local cmd="$LFS find -gid $RUNAS_GID $dir"
5888         local nums=$($cmd | wc -l)
5889
5890         [ $nums -eq $expected ] ||
5891                 error "'$cmd' wrong: found $nums, expected $expected"
5892
5893         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
5894         cmd="$LFS find ! -gid $RUNAS_GID $dir"
5895         nums=$($cmd | wc -l)
5896         [ $nums -eq $expected ] ||
5897                 error "'$cmd' wrong: found $nums, expected $expected"
5898 }
5899 run_test 56q "check lfs find -gid and ! -gid"
5900
5901 test_56r() {
5902         local dir=$DIR/$tdir
5903
5904         setup_56 $dir $NUMFILES $NUMDIRS
5905
5906         local expected=12
5907         local cmd="$LFS find -size 0 -type f -lazy $dir"
5908         local nums=$($cmd | wc -l)
5909
5910         [ $nums -eq $expected ] ||
5911                 error "'$cmd' wrong: found $nums, expected $expected"
5912         cmd="$LFS find -size 0 -type f $dir"
5913         nums=$($cmd | wc -l)
5914         [ $nums -eq $expected ] ||
5915                 error "'$cmd' wrong: found $nums, expected $expected"
5916
5917         expected=0
5918         cmd="$LFS find ! -size 0 -type f -lazy $dir"
5919         nums=$($cmd | wc -l)
5920         [ $nums -eq $expected ] ||
5921                 error "'$cmd' wrong: found $nums, expected $expected"
5922         cmd="$LFS find ! -size 0 -type f $dir"
5923         nums=$($cmd | wc -l)
5924         [ $nums -eq $expected ] ||
5925                 error "'$cmd' wrong: found $nums, expected $expected"
5926
5927         echo "test" > $dir/$tfile
5928         echo "test2" > $dir/$tfile.2 && sync
5929         expected=1
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         expected=1
5940         cmd="$LFS find -size +5 -type f -lazy $dir"
5941         nums=$($cmd | wc -l)
5942         [ $nums -eq $expected ] ||
5943                 error "'$cmd' wrong: found $nums, expected $expected"
5944         cmd="$LFS find -size +5 -type f $dir"
5945         nums=$($cmd | wc -l)
5946         [ $nums -eq $expected ] ||
5947                 error "'$cmd' wrong: found $nums, expected $expected"
5948
5949         expected=2
5950         cmd="$LFS find -size +0 -type f -lazy $dir"
5951         nums=$($cmd | wc -l)
5952         [ $nums -eq $expected ] ||
5953                 error "'$cmd' wrong: found $nums, expected $expected"
5954         cmd="$LFS find -size +0 -type f $dir"
5955         nums=$($cmd | wc -l)
5956         [ $nums -eq $expected ] ||
5957                 error "'$cmd' wrong: found $nums, expected $expected"
5958
5959         expected=2
5960         cmd="$LFS find ! -size -5 -type f -lazy $dir"
5961         nums=$($cmd | wc -l)
5962         [ $nums -eq $expected ] ||
5963                 error "'$cmd' wrong: found $nums, expected $expected"
5964         cmd="$LFS find ! -size -5 -type f $dir"
5965         nums=$($cmd | wc -l)
5966         [ $nums -eq $expected ] ||
5967                 error "'$cmd' wrong: found $nums, expected $expected"
5968
5969         expected=12
5970         cmd="$LFS find -size -5 -type f -lazy $dir"
5971         nums=$($cmd | wc -l)
5972         [ $nums -eq $expected ] ||
5973                 error "'$cmd' wrong: found $nums, expected $expected"
5974         cmd="$LFS find -size -5 -type f $dir"
5975         nums=$($cmd | wc -l)
5976         [ $nums -eq $expected ] ||
5977                 error "'$cmd' wrong: found $nums, expected $expected"
5978 }
5979 run_test 56r "check lfs find -size works"
5980
5981 test_56ra() {
5982         [[ $MDS1_VERSION -gt $(version_code 2.12.58) ]] ||
5983                 skip "MDS < 2.12.58 doesn't return LSOM data"
5984         local dir=$DIR/$tdir
5985
5986         [[ $OSC == "mdc" ]] && skip "DoM files" && return
5987
5988         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
5989
5990         cancel_lru_locks $OSC
5991
5992         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5993         local expected=12
5994         local cmd="$LFS find -size 0 -type f -lazy $dir"
5995         local nums=$($cmd | wc -l)
5996
5997         [ $nums -eq $expected ] ||
5998                 error "'$cmd' wrong: found $nums, expected $expected"
5999
6000         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6001         [ $rpcs_before -eq $rpcs_after ] ||
6002                 error "'$cmd' should not send glimpse RPCs to OST"
6003         cmd="$LFS find -size 0 -type f $dir"
6004         nums=$($cmd | wc -l)
6005         [ $nums -eq $expected ] ||
6006                 error "'$cmd' wrong: found $nums, expected $expected"
6007         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6008         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6009         $LCTL get_param osc.*.stats
6010         [ $rpcs_after -eq $((rpcs_before + 12)) ] ||
6011                 error "'$cmd' should send 12 glimpse RPCs to OST"
6012
6013         cancel_lru_locks $OSC
6014         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6015         expected=0
6016         cmd="$LFS find ! -size 0 -type f -lazy $dir"
6017         nums=$($cmd | wc -l)
6018         [ $nums -eq $expected ] ||
6019                 error "'$cmd' wrong: found $nums, expected $expected"
6020         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6021         $LCTL get_param mdc.*.stats
6022         [ $rpcs_before -eq $rpcs_after ] ||
6023                 error "'$cmd' should not send glimpse RPCs to OST"
6024         cmd="$LFS find ! -size 0 -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 + 12)) ] ||
6031                 error "'$cmd' should send 12 glimpse RPCs to OST"
6032
6033         echo "test" > $dir/$tfile
6034         echo "test2" > $dir/$tfile.2 && sync
6035         cancel_lru_locks $OSC
6036         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6037         expected=1
6038         cmd="$LFS find -size 5 -type f -lazy $dir"
6039         nums=$($cmd | wc -l)
6040         [ $nums -eq $expected ] ||
6041                 error "'$cmd' wrong: found $nums, expected $expected"
6042         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6043         [ $rpcs_before -eq $rpcs_after ] ||
6044                 error "'$cmd' should not send glimpse RPCs to OST"
6045         cmd="$LFS find -size 5 -type f $dir"
6046         nums=$($cmd | wc -l)
6047         [ $nums -eq $expected ] ||
6048                 error "'$cmd' wrong: found $nums, expected $expected"
6049         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6050         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6051         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6052                 error "'$cmd' should send 14 glimpse RPCs to OST"
6053
6054         cancel_lru_locks $OSC
6055         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6056         expected=1
6057         cmd="$LFS find -size +5 -type f -lazy $dir"
6058         nums=$($cmd | wc -l)
6059         [ $nums -eq $expected ] ||
6060                 error "'$cmd' wrong: found $nums, expected $expected"
6061         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6062         [ $rpcs_before -eq $rpcs_after ] ||
6063                 error "'$cmd' should not send glimpse RPCs to OST"
6064         cmd="$LFS find -size +5 -type f $dir"
6065         nums=$($cmd | wc -l)
6066         [ $nums -eq $expected ] ||
6067                 error "'$cmd' wrong: found $nums, expected $expected"
6068         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6069         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6070         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6071                 error "'$cmd' should send 14 glimpse RPCs to OST"
6072
6073         cancel_lru_locks $OSC
6074         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6075         expected=2
6076         cmd="$LFS find -size +0 -type f -lazy $dir"
6077         nums=$($cmd | wc -l)
6078         [ $nums -eq $expected ] ||
6079                 error "'$cmd' wrong: found $nums, expected $expected"
6080         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6081         [ $rpcs_before -eq $rpcs_after ] ||
6082                 error "'$cmd' should not send glimpse RPCs to OST"
6083         cmd="$LFS find -size +0 -type f $dir"
6084         nums=$($cmd | wc -l)
6085         [ $nums -eq $expected ] ||
6086                 error "'$cmd' wrong: found $nums, expected $expected"
6087         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6088         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6089         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6090                 error "'$cmd' should send 14 glimpse RPCs to OST"
6091
6092         cancel_lru_locks $OSC
6093         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6094         expected=2
6095         cmd="$LFS find ! -size -5 -type f -lazy $dir"
6096         nums=$($cmd | wc -l)
6097         [ $nums -eq $expected ] ||
6098                 error "'$cmd' wrong: found $nums, expected $expected"
6099         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6100         [ $rpcs_before -eq $rpcs_after ] ||
6101                 error "'$cmd' should not send glimpse RPCs to OST"
6102         cmd="$LFS find ! -size -5 -type f $dir"
6103         nums=$($cmd | wc -l)
6104         [ $nums -eq $expected ] ||
6105                 error "'$cmd' wrong: found $nums, expected $expected"
6106         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6107         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6108         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6109                 error "'$cmd' should send 14 glimpse RPCs to OST"
6110
6111         cancel_lru_locks $OSC
6112         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6113         expected=12
6114         cmd="$LFS find -size -5 -type f -lazy $dir"
6115         nums=$($cmd | wc -l)
6116         [ $nums -eq $expected ] ||
6117                 error "'$cmd' wrong: found $nums, expected $expected"
6118         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6119         [ $rpcs_before -eq $rpcs_after ] ||
6120                 error "'$cmd' should not send glimpse RPCs to OST"
6121         cmd="$LFS find -size -5 -type f $dir"
6122         nums=$($cmd | wc -l)
6123         [ $nums -eq $expected ] ||
6124                 error "'$cmd' wrong: found $nums, expected $expected"
6125         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6126         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6127         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6128                 error "'$cmd' should send 14 glimpse RPCs to OST"
6129 }
6130 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
6131
6132 test_56s() { # LU-611 #LU-9369
6133         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
6134
6135         local dir=$DIR/$tdir
6136         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
6137
6138         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6139         for i in $(seq $NUMDIRS); do
6140                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
6141         done
6142
6143         local expected=$NUMDIRS
6144         local cmd="$LFS find -c $OSTCOUNT $dir"
6145         local nums=$($cmd | wc -l)
6146
6147         [ $nums -eq $expected ] || {
6148                 $LFS getstripe -R $dir
6149                 error "'$cmd' wrong: found $nums, expected $expected"
6150         }
6151
6152         expected=$((NUMDIRS + onestripe))
6153         cmd="$LFS find -stripe-count +0 -type f $dir"
6154         nums=$($cmd | wc -l)
6155         [ $nums -eq $expected ] || {
6156                 $LFS getstripe -R $dir
6157                 error "'$cmd' wrong: found $nums, expected $expected"
6158         }
6159
6160         expected=$onestripe
6161         cmd="$LFS find -stripe-count 1 -type f $dir"
6162         nums=$($cmd | wc -l)
6163         [ $nums -eq $expected ] || {
6164                 $LFS getstripe -R $dir
6165                 error "'$cmd' wrong: found $nums, expected $expected"
6166         }
6167
6168         cmd="$LFS find -stripe-count -2 -type f $dir"
6169         nums=$($cmd | wc -l)
6170         [ $nums -eq $expected ] || {
6171                 $LFS getstripe -R $dir
6172                 error "'$cmd' wrong: found $nums, expected $expected"
6173         }
6174
6175         expected=0
6176         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
6177         nums=$($cmd | wc -l)
6178         [ $nums -eq $expected ] || {
6179                 $LFS getstripe -R $dir
6180                 error "'$cmd' wrong: found $nums, expected $expected"
6181         }
6182 }
6183 run_test 56s "check lfs find -stripe-count works"
6184
6185 test_56t() { # LU-611 #LU-9369
6186         local dir=$DIR/$tdir
6187
6188         setup_56 $dir 0 $NUMDIRS
6189         for i in $(seq $NUMDIRS); do
6190                 $LFS setstripe -S 8M $dir/dir$i/$tfile
6191         done
6192
6193         local expected=$NUMDIRS
6194         local cmd="$LFS find -S 8M $dir"
6195         local nums=$($cmd | wc -l)
6196
6197         [ $nums -eq $expected ] || {
6198                 $LFS getstripe -R $dir
6199                 error "'$cmd' wrong: found $nums, expected $expected"
6200         }
6201         rm -rf $dir
6202
6203         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
6204
6205         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
6206
6207         expected=$(((NUMDIRS + 1) * NUMFILES))
6208         cmd="$LFS find -stripe-size 512k -type f $dir"
6209         nums=$($cmd | wc -l)
6210         [ $nums -eq $expected ] ||
6211                 error "'$cmd' wrong: found $nums, expected $expected"
6212
6213         cmd="$LFS find -stripe-size +320k -type f $dir"
6214         nums=$($cmd | wc -l)
6215         [ $nums -eq $expected ] ||
6216                 error "'$cmd' wrong: found $nums, expected $expected"
6217
6218         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
6219         cmd="$LFS find -stripe-size +200k -type f $dir"
6220         nums=$($cmd | wc -l)
6221         [ $nums -eq $expected ] ||
6222                 error "'$cmd' wrong: found $nums, expected $expected"
6223
6224         cmd="$LFS find -stripe-size -640k -type f $dir"
6225         nums=$($cmd | wc -l)
6226         [ $nums -eq $expected ] ||
6227                 error "'$cmd' wrong: found $nums, expected $expected"
6228
6229         expected=4
6230         cmd="$LFS find -stripe-size 256k -type f $dir"
6231         nums=$($cmd | wc -l)
6232         [ $nums -eq $expected ] ||
6233                 error "'$cmd' wrong: found $nums, expected $expected"
6234
6235         cmd="$LFS find -stripe-size -320k -type f $dir"
6236         nums=$($cmd | wc -l)
6237         [ $nums -eq $expected ] ||
6238                 error "'$cmd' wrong: found $nums, expected $expected"
6239
6240         expected=0
6241         cmd="$LFS find -stripe-size 1024k -type f $dir"
6242         nums=$($cmd | wc -l)
6243         [ $nums -eq $expected ] ||
6244                 error "'$cmd' wrong: found $nums, expected $expected"
6245 }
6246 run_test 56t "check lfs find -stripe-size works"
6247
6248 test_56u() { # LU-611
6249         local dir=$DIR/$tdir
6250
6251         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
6252
6253         if [[ $OSTCOUNT -gt 1 ]]; then
6254                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
6255                 onestripe=4
6256         else
6257                 onestripe=0
6258         fi
6259
6260         local expected=$(((NUMDIRS + 1) * NUMFILES))
6261         local cmd="$LFS find -stripe-index 0 -type f $dir"
6262         local nums=$($cmd | wc -l)
6263
6264         [ $nums -eq $expected ] ||
6265                 error "'$cmd' wrong: found $nums, expected $expected"
6266
6267         expected=$onestripe
6268         cmd="$LFS find -stripe-index 1 -type f $dir"
6269         nums=$($cmd | wc -l)
6270         [ $nums -eq $expected ] ||
6271                 error "'$cmd' wrong: found $nums, expected $expected"
6272
6273         cmd="$LFS find ! -stripe-index 0 -type f $dir"
6274         nums=$($cmd | wc -l)
6275         [ $nums -eq $expected ] ||
6276                 error "'$cmd' wrong: found $nums, expected $expected"
6277
6278         expected=0
6279         # This should produce an error and not return any files
6280         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
6281         nums=$($cmd 2>/dev/null | wc -l)
6282         [ $nums -eq $expected ] ||
6283                 error "'$cmd' wrong: found $nums, expected $expected"
6284
6285         if [[ $OSTCOUNT -gt 1 ]]; then
6286                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
6287                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
6288                 nums=$($cmd | wc -l)
6289                 [ $nums -eq $expected ] ||
6290                         error "'$cmd' wrong: found $nums, expected $expected"
6291         fi
6292 }
6293 run_test 56u "check lfs find -stripe-index works"
6294
6295 test_56v() {
6296         local mdt_idx=0
6297         local dir=$DIR/$tdir
6298
6299         setup_56 $dir $NUMFILES $NUMDIRS
6300
6301         UUID=$(mdtuuid_from_index $mdt_idx $dir)
6302         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
6303
6304         for file in $($LFS find -m $UUID $dir); do
6305                 file_midx=$($LFS getstripe -m $file)
6306                 [ $file_midx -eq $mdt_idx ] ||
6307                         error "lfs find -m $UUID != getstripe -m $file_midx"
6308         done
6309 }
6310 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
6311
6312 test_56w() {
6313         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6314         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6315
6316         local dir=$DIR/$tdir
6317
6318         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
6319
6320         local stripe_size=$($LFS getstripe -S -d $dir) ||
6321                 error "$LFS getstripe -S -d $dir failed"
6322         stripe_size=${stripe_size%% *}
6323
6324         local file_size=$((stripe_size * OSTCOUNT))
6325         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
6326         local required_space=$((file_num * file_size))
6327         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
6328                            head -n1)
6329         [[ $free_space -le $((required_space / 1024)) ]] &&
6330                 skip_env "need $required_space, have $free_space kbytes"
6331
6332         local dd_bs=65536
6333         local dd_count=$((file_size / dd_bs))
6334
6335         # write data into the files
6336         local i
6337         local j
6338         local file
6339
6340         for i in $(seq $NUMFILES); do
6341                 file=$dir/file$i
6342                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6343                         error "write data into $file failed"
6344         done
6345         for i in $(seq $NUMDIRS); do
6346                 for j in $(seq $NUMFILES); do
6347                         file=$dir/dir$i/file$j
6348                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6349                                 error "write data into $file failed"
6350                 done
6351         done
6352
6353         # $LFS_MIGRATE will fail if hard link migration is unsupported
6354         if [[ $MDS1_VERSION -gt $(version_code 2.5.55) ]]; then
6355                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
6356                         error "creating links to $dir/dir1/file1 failed"
6357         fi
6358
6359         local expected=-1
6360
6361         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
6362
6363         # lfs_migrate file
6364         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
6365
6366         echo "$cmd"
6367         eval $cmd || error "$cmd failed"
6368
6369         check_stripe_count $dir/file1 $expected
6370
6371         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
6372         then
6373                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
6374                 # OST 1 if it is on OST 0. This file is small enough to
6375                 # be on only one stripe.
6376                 file=$dir/migr_1_ost
6377                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
6378                         error "write data into $file failed"
6379                 local obdidx=$($LFS getstripe -i $file)
6380                 local oldmd5=$(md5sum $file)
6381                 local newobdidx=0
6382
6383                 [[ $obdidx -eq 0 ]] && newobdidx=1
6384                 cmd="$LFS migrate -i $newobdidx $file"
6385                 echo $cmd
6386                 eval $cmd || error "$cmd failed"
6387
6388                 local realobdix=$($LFS getstripe -i $file)
6389                 local newmd5=$(md5sum $file)
6390
6391                 [[ $newobdidx -ne $realobdix ]] &&
6392                         error "new OST is different (was=$obdidx, "\
6393                               "wanted=$newobdidx, got=$realobdix)"
6394                 [[ "$oldmd5" != "$newmd5" ]] &&
6395                         error "md5sum differ: $oldmd5, $newmd5"
6396         fi
6397
6398         # lfs_migrate dir
6399         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
6400         echo "$cmd"
6401         eval $cmd || error "$cmd failed"
6402
6403         for j in $(seq $NUMFILES); do
6404                 check_stripe_count $dir/dir1/file$j $expected
6405         done
6406
6407         # lfs_migrate works with lfs find
6408         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
6409              $LFS_MIGRATE -y -c $expected"
6410         echo "$cmd"
6411         eval $cmd || error "$cmd failed"
6412
6413         for i in $(seq 2 $NUMFILES); do
6414                 check_stripe_count $dir/file$i $expected
6415         done
6416         for i in $(seq 2 $NUMDIRS); do
6417                 for j in $(seq $NUMFILES); do
6418                 check_stripe_count $dir/dir$i/file$j $expected
6419                 done
6420         done
6421 }
6422 run_test 56w "check lfs_migrate -c stripe_count works"
6423
6424 test_56wb() {
6425         local file1=$DIR/$tdir/file1
6426         local create_pool=false
6427         local initial_pool=$($LFS getstripe -p $DIR)
6428         local pool_list=()
6429         local pool=""
6430
6431         echo -n "Creating test dir..."
6432         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6433         echo "done."
6434
6435         echo -n "Creating test file..."
6436         touch $file1 || error "cannot create file"
6437         echo "done."
6438
6439         echo -n "Detecting existing pools..."
6440         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
6441
6442         if [ ${#pool_list[@]} -gt 0 ]; then
6443                 echo "${pool_list[@]}"
6444                 for thispool in "${pool_list[@]}"; do
6445                         if [[ -z "$initial_pool" ||
6446                               "$initial_pool" != "$thispool" ]]; then
6447                                 pool="$thispool"
6448                                 echo "Using existing pool '$pool'"
6449                                 break
6450                         fi
6451                 done
6452         else
6453                 echo "none detected."
6454         fi
6455         if [ -z "$pool" ]; then
6456                 pool=${POOL:-testpool}
6457                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
6458                 echo -n "Creating pool '$pool'..."
6459                 create_pool=true
6460                 pool_add $pool &> /dev/null ||
6461                         error "pool_add failed"
6462                 echo "done."
6463
6464                 echo -n "Adding target to pool..."
6465                 pool_add_targets $pool 0 0 1 &> /dev/null ||
6466                         error "pool_add_targets failed"
6467                 echo "done."
6468         fi
6469
6470         echo -n "Setting pool using -p option..."
6471         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
6472                 error "migrate failed rc = $?"
6473         echo "done."
6474
6475         echo -n "Verifying test file is in pool after migrating..."
6476         [ "$($LFS getstripe -p $file1)" = $pool ] ||
6477                 error "file was not migrated to pool $pool"
6478         echo "done."
6479
6480         echo -n "Removing test file from pool '$pool'..."
6481         # "lfs migrate $file" won't remove the file from the pool
6482         # until some striping information is changed.
6483         $LFS migrate -c 1 $file1 &> /dev/null ||
6484                 error "cannot remove from pool"
6485         [ "$($LFS getstripe -p $file1)" ] &&
6486                 error "pool still set"
6487         echo "done."
6488
6489         echo -n "Setting pool using --pool option..."
6490         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
6491                 error "migrate failed rc = $?"
6492         echo "done."
6493
6494         # Clean up
6495         rm -f $file1
6496         if $create_pool; then
6497                 destroy_test_pools 2> /dev/null ||
6498                         error "destroy test pools failed"
6499         fi
6500 }
6501 run_test 56wb "check lfs_migrate pool support"
6502
6503 test_56wc() {
6504         local file1="$DIR/$tdir/file1"
6505         local parent_ssize
6506         local parent_scount
6507         local cur_ssize
6508         local cur_scount
6509         local orig_ssize
6510
6511         echo -n "Creating test dir..."
6512         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6513         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
6514                 error "cannot set stripe by '-S 1M -c 1'"
6515         echo "done"
6516
6517         echo -n "Setting initial stripe for test file..."
6518         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
6519                 error "cannot set stripe"
6520         cur_ssize=$($LFS getstripe -S "$file1")
6521         [ $cur_ssize -eq 524288 ] || error "setstripe -S $cur_ssize != 524288"
6522         echo "done."
6523
6524         # File currently set to -S 512K -c 1
6525
6526         # Ensure -c and -S options are rejected when -R is set
6527         echo -n "Verifying incompatible options are detected..."
6528         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
6529                 error "incompatible -c and -R options not detected"
6530         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
6531                 error "incompatible -S and -R options not detected"
6532         echo "done."
6533
6534         # Ensure unrecognized options are passed through to 'lfs migrate'
6535         echo -n "Verifying -S option is passed through to lfs migrate..."
6536         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
6537                 error "migration failed"
6538         cur_ssize=$($LFS getstripe -S "$file1")
6539         [ $cur_ssize -eq 1048576 ] || error "migrate -S $cur_ssize != 1048576"
6540         echo "done."
6541
6542         # File currently set to -S 1M -c 1
6543
6544         # Ensure long options are supported
6545         echo -n "Verifying long options supported..."
6546         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
6547                 error "long option without argument not supported"
6548         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
6549                 error "long option with argument not supported"
6550         cur_ssize=$($LFS getstripe -S "$file1")
6551         [ $cur_ssize -eq 524288 ] ||
6552                 error "migrate --stripe-size $cur_ssize != 524288"
6553         echo "done."
6554
6555         # File currently set to -S 512K -c 1
6556
6557         if [ "$OSTCOUNT" -gt 1 ]; then
6558                 echo -n "Verifying explicit stripe count can be set..."
6559                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
6560                         error "migrate failed"
6561                 cur_scount=$($LFS getstripe -c "$file1")
6562                 [ $cur_scount -eq 2 ] || error "migrate -c $cur_scount != 2"
6563                 echo "done."
6564         fi
6565
6566         # File currently set to -S 512K -c 1 or -S 512K -c 2
6567
6568         # Ensure parent striping is used if -R is set, and no stripe
6569         # count or size is specified
6570         echo -n "Setting stripe for parent directory..."
6571         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
6572                 error "cannot set stripe '-S 2M -c 1'"
6573         echo "done."
6574
6575         echo -n "Verifying restripe option uses parent stripe settings..."
6576         parent_ssize=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
6577         parent_scount=$($LFS getstripe -c $DIR/$tdir 2>/dev/null)
6578         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
6579                 error "migrate failed"
6580         cur_ssize=$($LFS getstripe -S "$file1")
6581         [ $cur_ssize -eq $parent_ssize ] ||
6582                 error "migrate -R stripe_size $cur_ssize != $parent_ssize"
6583         cur_scount=$($LFS getstripe -c "$file1")
6584         [ $cur_scount -eq $parent_scount ] ||
6585                 error "migrate -R stripe_count $cur_scount != $parent_scount"
6586         echo "done."
6587
6588         # File currently set to -S 1M -c 1
6589
6590         # Ensure striping is preserved if -R is not set, and no stripe
6591         # count or size is specified
6592         echo -n "Verifying striping size preserved when not specified..."
6593         orig_ssize=$($LFS getstripe -S "$file1" 2>/dev/null)
6594         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
6595                 error "cannot set stripe on parent directory"
6596         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6597                 error "migrate failed"
6598         cur_ssize=$($LFS getstripe -S "$file1")
6599         [ $cur_ssize -eq $orig_ssize ] ||
6600                 error "migrate by default $cur_ssize != $orig_ssize"
6601         echo "done."
6602
6603         # Ensure file name properly detected when final option has no argument
6604         echo -n "Verifying file name properly detected..."
6605         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6606                 error "file name interpreted as option argument"
6607         echo "done."
6608
6609         # Clean up
6610         rm -f "$file1"
6611 }
6612 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
6613
6614 test_56wd() {
6615         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6616
6617         local file1=$DIR/$tdir/file1
6618
6619         echo -n "Creating test dir..."
6620         test_mkdir $DIR/$tdir || error "cannot create dir"
6621         echo "done."
6622
6623         echo -n "Creating test file..."
6624         touch $file1
6625         echo "done."
6626
6627         # Ensure 'lfs migrate' will fail by using a non-existent option,
6628         # and make sure rsync is not called to recover
6629         echo -n "Make sure --no-rsync option works..."
6630         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
6631                 grep -q 'refusing to fall back to rsync' ||
6632                 error "rsync was called with --no-rsync set"
6633         echo "done."
6634
6635         # Ensure rsync is called without trying 'lfs migrate' first
6636         echo -n "Make sure --rsync option works..."
6637         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
6638                 grep -q 'falling back to rsync' &&
6639                 error "lfs migrate was called with --rsync set"
6640         echo "done."
6641
6642         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
6643         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
6644                 grep -q 'at the same time' ||
6645                 error "--rsync and --no-rsync accepted concurrently"
6646         echo "done."
6647
6648         # Clean up
6649         rm -f $file1
6650 }
6651 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
6652
6653 test_56x() {
6654         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6655         check_swap_layouts_support
6656
6657         local dir=$DIR/$tdir
6658         local ref1=/etc/passwd
6659         local file1=$dir/file1
6660
6661         test_mkdir $dir || error "creating dir $dir"
6662         $LFS setstripe -c 2 $file1
6663         cp $ref1 $file1
6664         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
6665         stripe=$($LFS getstripe -c $file1)
6666         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6667         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6668
6669         # clean up
6670         rm -f $file1
6671 }
6672 run_test 56x "lfs migration support"
6673
6674 test_56xa() {
6675         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6676         check_swap_layouts_support
6677
6678         local dir=$DIR/$tdir/$testnum
6679
6680         test_mkdir -p $dir
6681
6682         local ref1=/etc/passwd
6683         local file1=$dir/file1
6684
6685         $LFS setstripe -c 2 $file1
6686         cp $ref1 $file1
6687         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
6688
6689         local stripe=$($LFS getstripe -c $file1)
6690
6691         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6692         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6693
6694         # clean up
6695         rm -f $file1
6696 }
6697 run_test 56xa "lfs migration --block support"
6698
6699 check_migrate_links() {
6700         local dir="$1"
6701         local file1="$dir/file1"
6702         local begin="$2"
6703         local count="$3"
6704         local runas="$4"
6705         local total_count=$(($begin + $count - 1))
6706         local symlink_count=10
6707         local uniq_count=10
6708
6709         if [ ! -f "$file1" ]; then
6710                 echo -n "creating initial file..."
6711                 $LFS setstripe -c 1 -S "512k" "$file1" ||
6712                         error "cannot setstripe initial file"
6713                 echo "done"
6714
6715                 echo -n "creating symlinks..."
6716                 for s in $(seq 1 $symlink_count); do
6717                         ln -s "$file1" "$dir/slink$s" ||
6718                                 error "cannot create symlinks"
6719                 done
6720                 echo "done"
6721
6722                 echo -n "creating nonlinked files..."
6723                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
6724                         error "cannot create nonlinked files"
6725                 echo "done"
6726         fi
6727
6728         # create hard links
6729         if [ ! -f "$dir/file$total_count" ]; then
6730                 echo -n "creating hard links $begin:$total_count..."
6731                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
6732                         /dev/null || error "cannot create hard links"
6733                 echo "done"
6734         fi
6735
6736         echo -n "checking number of hard links listed in xattrs..."
6737         local fid=$($LFS getstripe -F "$file1")
6738         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
6739
6740         echo "${#paths[*]}"
6741         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
6742                         skip "hard link list has unexpected size, skipping test"
6743         fi
6744         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
6745                         error "link names should exceed xattrs size"
6746         fi
6747
6748         echo -n "migrating files..."
6749         local migrate_out=$($runas $LFS_MIGRATE -y -S '1m' $dir)
6750         local rc=$?
6751         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
6752         echo "done"
6753
6754         # make sure all links have been properly migrated
6755         echo -n "verifying files..."
6756         fid=$($LFS getstripe -F "$file1") ||
6757                 error "cannot get fid for file $file1"
6758         for i in $(seq 2 $total_count); do
6759                 local fid2=$($LFS getstripe -F $dir/file$i)
6760
6761                 [ "$fid2" == "$fid" ] ||
6762                         error "migrated hard link has mismatched FID"
6763         done
6764
6765         # make sure hard links were properly detected, and migration was
6766         # performed only once for the entire link set; nonlinked files should
6767         # also be migrated
6768         local actual=$(grep -c 'done' <<< "$migrate_out")
6769         local expected=$(($uniq_count + 1))
6770
6771         [ "$actual" -eq  "$expected" ] ||
6772                 error "hard links individually migrated ($actual != $expected)"
6773
6774         # make sure the correct number of hard links are present
6775         local hardlinks=$(stat -c '%h' "$file1")
6776
6777         [ $hardlinks -eq $total_count ] ||
6778                 error "num hard links $hardlinks != $total_count"
6779         echo "done"
6780
6781         return 0
6782 }
6783
6784 test_56xb() {
6785         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
6786                 skip "Need MDS version at least 2.10.55"
6787
6788         local dir="$DIR/$tdir"
6789
6790         test_mkdir "$dir" || error "cannot create dir $dir"
6791
6792         echo "testing lfs migrate mode when all links fit within xattrs"
6793         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
6794
6795         echo "testing rsync mode when all links fit within xattrs"
6796         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
6797
6798         echo "testing lfs migrate mode when all links do not fit within xattrs"
6799         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
6800
6801         echo "testing rsync mode when all links do not fit within xattrs"
6802         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
6803
6804         chown -R $RUNAS_ID $dir
6805         echo "testing non-root lfs migrate mode when not all links are in xattr"
6806         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100 "$RUNAS"
6807
6808         # clean up
6809         rm -rf $dir
6810 }
6811 run_test 56xb "lfs migration hard link support"
6812
6813 test_56xc() {
6814         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6815
6816         local dir="$DIR/$tdir"
6817
6818         test_mkdir "$dir" || error "cannot create dir $dir"
6819
6820         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
6821         echo -n "Setting initial stripe for 20MB test file..."
6822         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
6823                 error "cannot setstripe 20MB file"
6824         echo "done"
6825         echo -n "Sizing 20MB test file..."
6826         truncate "$dir/20mb" 20971520 || error "cannot create 20MB test file"
6827         echo "done"
6828         echo -n "Verifying small file autostripe count is 1..."
6829         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
6830                 error "cannot migrate 20MB file"
6831         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
6832                 error "cannot get stripe for $dir/20mb"
6833         [ $stripe_count -eq 1 ] ||
6834                 error "unexpected stripe count $stripe_count for 20MB file"
6835         rm -f "$dir/20mb"
6836         echo "done"
6837
6838         # Test 2: File is small enough to fit within the available space on
6839         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
6840         # have at least an additional 1KB for each desired stripe for test 3
6841         echo -n "Setting stripe for 1GB test file..."
6842         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
6843         echo "done"
6844         echo -n "Sizing 1GB test file..."
6845         # File size is 1GB + 3KB
6846         truncate "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
6847         echo "done"
6848
6849         # need at least 512MB per OST for 1GB file to fit in 2 stripes
6850         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
6851         if (( avail > 524288 * OSTCOUNT )); then
6852                 echo -n "Migrating 1GB file..."
6853                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
6854                         error "cannot migrate 1GB file"
6855                 echo "done"
6856                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
6857                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
6858                         error "cannot getstripe for 1GB file"
6859                 [ $stripe_count -eq 2 ] ||
6860                         error "unexpected stripe count $stripe_count != 2"
6861                 echo "done"
6862         fi
6863
6864         # Test 3: File is too large to fit within the available space on
6865         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
6866         if [ $OSTCOUNT -ge 3 ]; then
6867                 # The required available space is calculated as
6868                 # file size (1GB + 3KB) / OST count (3).
6869                 local kb_per_ost=349526
6870
6871                 echo -n "Migrating 1GB file with limit..."
6872                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
6873                         error "cannot migrate 1GB file with limit"
6874                 echo "done"
6875
6876                 stripe_count=$($LFS getstripe -c "$dir/1gb")
6877                 echo -n "Verifying 1GB autostripe count with limited space..."
6878                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
6879                         error "unexpected stripe count $stripe_count (min 3)"
6880                 echo "done"
6881         fi
6882
6883         # clean up
6884         rm -rf $dir
6885 }
6886 run_test 56xc "lfs migration autostripe"
6887
6888 test_56xd() {
6889         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6890
6891         local dir=$DIR/$tdir
6892         local f_mgrt=$dir/$tfile.mgrt
6893         local f_yaml=$dir/$tfile.yaml
6894         local f_copy=$dir/$tfile.copy
6895         local layout_yaml="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
6896         local layout_copy="-c 2 -S 2M -i 1"
6897         local yamlfile=$dir/yamlfile
6898         local layout_before;
6899         local layout_after;
6900
6901         test_mkdir "$dir" || error "cannot create dir $dir"
6902         $LFS setstripe $layout_yaml $f_yaml ||
6903                 error "cannot setstripe $f_yaml with layout $layout_yaml"
6904         $LFS getstripe --yaml $f_yaml > $yamlfile
6905         $LFS setstripe $layout_copy $f_copy ||
6906                 error "cannot setstripe $f_copy with layout $layout_copy"
6907         touch $f_mgrt
6908         dd if=/dev/zero of=$f_mgrt bs=1M count=4
6909
6910         # 1. test option --yaml
6911         $LFS_MIGRATE -y --yaml $yamlfile $f_mgrt ||
6912                 error "cannot migrate $f_mgrt with --yaml $yamlfile"
6913         layout_before=$(get_layout_param $f_yaml)
6914         layout_after=$(get_layout_param $f_mgrt)
6915         [ "$layout_after" == "$layout_before" ] ||
6916                 error "lfs_migrate --yaml: $layout_after != $layout_before"
6917
6918         # 2. test option --copy
6919         $LFS_MIGRATE -y --copy $f_copy $f_mgrt ||
6920                 error "cannot migrate $f_mgrt with --copy $f_copy"
6921         layout_before=$(get_layout_param $f_copy)
6922         layout_after=$(get_layout_param $f_mgrt)
6923         [ "$layout_after" == "$layout_before" ] ||
6924                 error "lfs_migrate --copy: $layout_after != $layout_before"
6925 }
6926 run_test 56xd "check lfs_migrate --yaml and --copy support"
6927
6928 test_56xe() {
6929         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6930
6931         local dir=$DIR/$tdir
6932         local f_comp=$dir/$tfile
6933         local layout="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
6934         local layout_before=""
6935         local layout_after=""
6936
6937         test_mkdir "$dir" || error "cannot create dir $dir"
6938         $LFS setstripe $layout $f_comp ||
6939                 error "cannot setstripe $f_comp with layout $layout"
6940         layout_before=$(get_layout_param $f_comp)
6941         dd if=/dev/zero of=$f_comp bs=1M count=4
6942
6943         # 1. migrate a comp layout file by lfs_migrate
6944         $LFS_MIGRATE -y $f_comp || error "cannot migrate $f_comp by lfs_migrate"
6945         layout_after=$(get_layout_param $f_comp)
6946         [ "$layout_before" == "$layout_after" ] ||
6947                 error "lfs_migrate: $layout_before != $layout_after"
6948
6949         # 2. migrate a comp layout file by lfs migrate
6950         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
6951         layout_after=$(get_layout_param $f_comp)
6952         [ "$layout_before" == "$layout_after" ] ||
6953                 error "lfs migrate: $layout_before != $layout_after"
6954 }
6955 run_test 56xe "migrate a composite layout file"
6956
6957 test_56y() {
6958         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
6959                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
6960
6961         local res=""
6962         local dir=$DIR/$tdir
6963         local f1=$dir/file1
6964         local f2=$dir/file2
6965
6966         test_mkdir -p $dir || error "creating dir $dir"
6967         touch $f1 || error "creating std file $f1"
6968         $MULTIOP $f2 H2c || error "creating released file $f2"
6969
6970         # a directory can be raid0, so ask only for files
6971         res=$($LFS find $dir -L raid0 -type f | wc -l)
6972         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
6973
6974         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
6975         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
6976
6977         # only files can be released, so no need to force file search
6978         res=$($LFS find $dir -L released)
6979         [[ $res == $f2 ]] || error "search released: found $res != $f2"
6980
6981         res=$($LFS find $dir -type f \! -L released)
6982         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
6983 }
6984 run_test 56y "lfs find -L raid0|released"
6985
6986 test_56z() { # LU-4824
6987         # This checks to make sure 'lfs find' continues after errors
6988         # There are two classes of errors that should be caught:
6989         # - If multiple paths are provided, all should be searched even if one
6990         #   errors out
6991         # - If errors are encountered during the search, it should not terminate
6992         #   early
6993         local dir=$DIR/$tdir
6994         local i
6995
6996         test_mkdir $dir
6997         for i in d{0..9}; do
6998                 test_mkdir $dir/$i
6999                 touch $dir/$i/$tfile
7000         done
7001         $LFS find $DIR/non_existent_dir $dir &&
7002                 error "$LFS find did not return an error"
7003         # Make a directory unsearchable. This should NOT be the last entry in
7004         # directory order.  Arbitrarily pick the 6th entry
7005         chmod 700 $($LFS find $dir -type d | sed '6!d')
7006
7007         $RUNAS $LFS find $DIR/non_existent $dir
7008         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
7009
7010         # The user should be able to see 10 directories and 9 files
7011         (( count == 19 )) ||
7012                 error "$LFS find found $count != 19 entries after error"
7013 }
7014 run_test 56z "lfs find should continue after an error"
7015
7016 test_56aa() { # LU-5937
7017         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
7018
7019         local dir=$DIR/$tdir
7020
7021         mkdir $dir
7022         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
7023
7024         createmany -o $dir/striped_dir/${tfile}- 1024
7025         local dirs=$($LFS find --size +8k $dir/)
7026
7027         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
7028 }
7029 run_test 56aa "lfs find --size under striped dir"
7030
7031 test_56ab() { # LU-10705
7032         test_mkdir $DIR/$tdir
7033         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
7034         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
7035         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
7036         # Flush writes to ensure valid blocks.  Need to be more thorough for
7037         # ZFS, since blocks are not allocated/returned to client immediately.
7038         sync_all_data
7039         wait_zfs_commit ost1 2
7040         cancel_lru_locks osc
7041         ls -ls $DIR/$tdir
7042
7043         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
7044
7045         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
7046
7047         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
7048         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
7049
7050         rm -f $DIR/$tdir/$tfile.[123]
7051 }
7052 run_test 56ab "lfs find --blocks"
7053
7054 test_56ba() {
7055         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
7056                 skip "Need MDS version at least 2.10.50"
7057
7058         # Create composite files with one component
7059         local dir=$DIR/$tdir
7060
7061         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
7062         # Create composite files with three components
7063         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
7064         # Create non-composite files
7065         createmany -o $dir/${tfile}- 10
7066
7067         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
7068
7069         [[ $nfiles == 10 ]] ||
7070                 error "lfs find -E 1M found $nfiles != 10 files"
7071
7072         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
7073         [[ $nfiles == 25 ]] ||
7074                 error "lfs find ! -E 1M found $nfiles != 25 files"
7075
7076         # All files have a component that starts at 0
7077         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
7078         [[ $nfiles == 35 ]] ||
7079                 error "lfs find --component-start 0 - $nfiles != 35 files"
7080
7081         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
7082         [[ $nfiles == 15 ]] ||
7083                 error "lfs find --component-start 2M - $nfiles != 15 files"
7084
7085         # All files created here have a componenet that does not starts at 2M
7086         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
7087         [[ $nfiles == 35 ]] ||
7088                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
7089
7090         # Find files with a specified number of components
7091         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
7092         [[ $nfiles == 15 ]] ||
7093                 error "lfs find --component-count 3 - $nfiles != 15 files"
7094
7095         # Remember non-composite files have a component count of zero
7096         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
7097         [[ $nfiles == 10 ]] ||
7098                 error "lfs find --component-count 0 - $nfiles != 10 files"
7099
7100         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
7101         [[ $nfiles == 20 ]] ||
7102                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
7103
7104         # All files have a flag called "init"
7105         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
7106         [[ $nfiles == 35 ]] ||
7107                 error "lfs find --component-flags init - $nfiles != 35 files"
7108
7109         # Multi-component files will have a component not initialized
7110         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
7111         [[ $nfiles == 15 ]] ||
7112                 error "lfs find !--component-flags init - $nfiles != 15 files"
7113
7114         rm -rf $dir
7115
7116 }
7117 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
7118
7119 test_56ca() {
7120         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
7121                 skip "Need MDS version at least 2.10.57"
7122
7123         local td=$DIR/$tdir
7124         local tf=$td/$tfile
7125         local dir
7126         local nfiles
7127         local cmd
7128         local i
7129         local j
7130
7131         # create mirrored directories and mirrored files
7132         mkdir $td || error "mkdir $td failed"
7133         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
7134         createmany -o $tf- 10 || error "create $tf- failed"
7135
7136         for i in $(seq 2); do
7137                 dir=$td/dir$i
7138                 mkdir $dir || error "mkdir $dir failed"
7139                 $LFS mirror create -N$((3 + i)) $dir ||
7140                         error "create mirrored dir $dir failed"
7141                 createmany -o $dir/$tfile- 10 ||
7142                         error "create $dir/$tfile- failed"
7143         done
7144
7145         # change the states of some mirrored files
7146         echo foo > $tf-6
7147         for i in $(seq 2); do
7148                 dir=$td/dir$i
7149                 for j in $(seq 4 9); do
7150                         echo foo > $dir/$tfile-$j
7151                 done
7152         done
7153
7154         # find mirrored files with specific mirror count
7155         cmd="$LFS find --mirror-count 3 --type f $td"
7156         nfiles=$($cmd | wc -l)
7157         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
7158
7159         cmd="$LFS find ! --mirror-count 3 --type f $td"
7160         nfiles=$($cmd | wc -l)
7161         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
7162
7163         cmd="$LFS find --mirror-count +2 --type f $td"
7164         nfiles=$($cmd | wc -l)
7165         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7166
7167         cmd="$LFS find --mirror-count -6 --type f $td"
7168         nfiles=$($cmd | wc -l)
7169         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7170
7171         # find mirrored files with specific file state
7172         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
7173         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
7174
7175         cmd="$LFS find --mirror-state=ro --type f $td"
7176         nfiles=$($cmd | wc -l)
7177         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
7178
7179         cmd="$LFS find ! --mirror-state=ro --type f $td"
7180         nfiles=$($cmd | wc -l)
7181         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7182
7183         cmd="$LFS find --mirror-state=wp --type f $td"
7184         nfiles=$($cmd | wc -l)
7185         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7186
7187         cmd="$LFS find ! --mirror-state=sp --type f $td"
7188         nfiles=$($cmd | wc -l)
7189         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7190 }
7191 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
7192
7193 test_57a() {
7194         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7195         # note test will not do anything if MDS is not local
7196         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7197                 skip_env "ldiskfs only test"
7198         fi
7199         remote_mds_nodsh && skip "remote MDS with nodsh"
7200
7201         local MNTDEV="osd*.*MDT*.mntdev"
7202         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
7203         [ -z "$DEV" ] && error "can't access $MNTDEV"
7204         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
7205                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
7206                         error "can't access $DEV"
7207                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
7208                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
7209                 rm $TMP/t57a.dump
7210         done
7211 }
7212 run_test 57a "verify MDS filesystem created with large inodes =="
7213
7214 test_57b() {
7215         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7216         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7217                 skip_env "ldiskfs only test"
7218         fi
7219         remote_mds_nodsh && skip "remote MDS with nodsh"
7220
7221         local dir=$DIR/$tdir
7222         local filecount=100
7223         local file1=$dir/f1
7224         local fileN=$dir/f$filecount
7225
7226         rm -rf $dir || error "removing $dir"
7227         test_mkdir -c1 $dir
7228         local mdtidx=$($LFS getstripe -m $dir)
7229         local mdtname=MDT$(printf %04x $mdtidx)
7230         local facet=mds$((mdtidx + 1))
7231
7232         echo "mcreating $filecount files"
7233         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
7234
7235         # verify that files do not have EAs yet
7236         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
7237                 error "$file1 has an EA"
7238         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
7239                 error "$fileN has an EA"
7240
7241         sync
7242         sleep 1
7243         df $dir  #make sure we get new statfs data
7244         local mdsfree=$(do_facet $facet \
7245                         lctl get_param -n osd*.*$mdtname.kbytesfree)
7246         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7247         local file
7248
7249         echo "opening files to create objects/EAs"
7250         for file in $(seq -f $dir/f%g 1 $filecount); do
7251                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
7252                         error "opening $file"
7253         done
7254
7255         # verify that files have EAs now
7256         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
7257         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
7258
7259         sleep 1  #make sure we get new statfs data
7260         df $dir
7261         local mdsfree2=$(do_facet $facet \
7262                          lctl get_param -n osd*.*$mdtname.kbytesfree)
7263         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7264
7265         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
7266                 if [ "$mdsfree" != "$mdsfree2" ]; then
7267                         error "MDC before $mdcfree != after $mdcfree2"
7268                 else
7269                         echo "MDC before $mdcfree != after $mdcfree2"
7270                         echo "unable to confirm if MDS has large inodes"
7271                 fi
7272         fi
7273         rm -rf $dir
7274 }
7275 run_test 57b "default LOV EAs are stored inside large inodes ==="
7276
7277 test_58() {
7278         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7279         [ -z "$(which wiretest 2>/dev/null)" ] &&
7280                         skip_env "could not find wiretest"
7281
7282         wiretest
7283 }
7284 run_test 58 "verify cross-platform wire constants =============="
7285
7286 test_59() {
7287         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7288
7289         echo "touch 130 files"
7290         createmany -o $DIR/f59- 130
7291         echo "rm 130 files"
7292         unlinkmany $DIR/f59- 130
7293         sync
7294         # wait for commitment of removal
7295         wait_delete_completed
7296 }
7297 run_test 59 "verify cancellation of llog records async ========="
7298
7299 TEST60_HEAD="test_60 run $RANDOM"
7300 test_60a() {
7301         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7302         remote_mgs_nodsh && skip "remote MGS with nodsh"
7303         do_facet mgs "! which run-llog.sh &> /dev/null" &&
7304                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
7305                         skip_env "missing subtest run-llog.sh"
7306
7307         log "$TEST60_HEAD - from kernel mode"
7308         do_facet mgs "$LCTL dk > /dev/null"
7309         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
7310         do_facet mgs $LCTL dk > $TMP/$tfile
7311
7312         # LU-6388: test llog_reader
7313         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
7314         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
7315         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
7316                         skip_env "missing llog_reader"
7317         local fstype=$(facet_fstype mgs)
7318         [ $fstype != ldiskfs -a $fstype != zfs ] &&
7319                 skip_env "Only for ldiskfs or zfs type mgs"
7320
7321         local mntpt=$(facet_mntpt mgs)
7322         local mgsdev=$(mgsdevname 1)
7323         local fid_list
7324         local fid
7325         local rec_list
7326         local rec
7327         local rec_type
7328         local obj_file
7329         local path
7330         local seq
7331         local oid
7332         local pass=true
7333
7334         #get fid and record list
7335         fid_list=($(awk '/9_sub.*record/ { print $NF }' $TMP/$tfile |
7336                 tail -n 4))
7337         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' $TMP/$tfile |
7338                 tail -n 4))
7339         #remount mgs as ldiskfs or zfs type
7340         stop mgs || error "stop mgs failed"
7341         mount_fstype mgs || error "remount mgs failed"
7342         for ((i = 0; i < ${#fid_list[@]}; i++)); do
7343                 fid=${fid_list[i]}
7344                 rec=${rec_list[i]}
7345                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
7346                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
7347                 oid=$((16#$oid))
7348
7349                 case $fstype in
7350                         ldiskfs )
7351                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
7352                         zfs )
7353                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
7354                 esac
7355                 echo "obj_file is $obj_file"
7356                 do_facet mgs $llog_reader $obj_file
7357
7358                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
7359                         awk '{ print $3 }' | sed -e "s/^type=//g")
7360                 if [ $rec_type != $rec ]; then
7361                         echo "FAILED test_60a wrong record type $rec_type," \
7362                               "should be $rec"
7363                         pass=false
7364                         break
7365                 fi
7366
7367                 #check obj path if record type is LLOG_LOGID_MAGIC
7368                 if [ "$rec" == "1064553b" ]; then
7369                         path=$(do_facet mgs $llog_reader $obj_file |
7370                                 grep "path=" | awk '{ print $NF }' |
7371                                 sed -e "s/^path=//g")
7372                         if [ $obj_file != $mntpt/$path ]; then
7373                                 echo "FAILED test_60a wrong obj path" \
7374                                       "$montpt/$path, should be $obj_file"
7375                                 pass=false
7376                                 break
7377                         fi
7378                 fi
7379         done
7380         rm -f $TMP/$tfile
7381         #restart mgs before "error", otherwise it will block the next test
7382         stop mgs || error "stop mgs failed"
7383         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
7384         $pass || error "test failed, see FAILED test_60a messages for specifics"
7385 }
7386 run_test 60a "llog_test run from kernel module and test llog_reader"
7387
7388 test_60b() { # bug 6411
7389         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7390
7391         dmesg > $DIR/$tfile
7392         LLOG_COUNT=$(do_facet mgs dmesg |
7393                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
7394                           /llog_[a-z]*.c:[0-9]/ {
7395                                 if (marker)
7396                                         from_marker++
7397                                 from_begin++
7398                           }
7399                           END {
7400                                 if (marker)
7401                                         print from_marker
7402                                 else
7403                                         print from_begin
7404                           }")
7405
7406         [[ $LLOG_COUNT -gt 120 ]] &&
7407                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
7408 }
7409 run_test 60b "limit repeated messages from CERROR/CWARN"
7410
7411 test_60c() {
7412         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7413
7414         echo "create 5000 files"
7415         createmany -o $DIR/f60c- 5000
7416 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
7417         lctl set_param fail_loc=0x80000137
7418         unlinkmany $DIR/f60c- 5000
7419         lctl set_param fail_loc=0
7420 }
7421 run_test 60c "unlink file when mds full"
7422
7423 test_60d() {
7424         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7425
7426         SAVEPRINTK=$(lctl get_param -n printk)
7427         # verify "lctl mark" is even working"
7428         MESSAGE="test message ID $RANDOM $$"
7429         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7430         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
7431
7432         lctl set_param printk=0 || error "set lnet.printk failed"
7433         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
7434         MESSAGE="new test message ID $RANDOM $$"
7435         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
7436         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7437         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
7438
7439         lctl set_param -n printk="$SAVEPRINTK"
7440 }
7441 run_test 60d "test printk console message masking"
7442
7443 test_60e() {
7444         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7445         remote_mds_nodsh && skip "remote MDS with nodsh"
7446
7447         touch $DIR/$tfile
7448 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
7449         do_facet mds1 lctl set_param fail_loc=0x15b
7450         rm $DIR/$tfile
7451 }
7452 run_test 60e "no space while new llog is being created"
7453
7454 test_60g() {
7455         local pid
7456         local i
7457
7458         test_mkdir -c $MDSCOUNT $DIR/$tdir
7459
7460         (
7461                 local index=0
7462                 while true; do
7463                         $LFS setdirstripe -i $(($index % $MDSCOUNT)) \
7464                                 -c $MDSCOUNT $DIR/$tdir/subdir$index \
7465                                 2>/dev/null
7466                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
7467                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
7468                         index=$((index + 1))
7469                 done
7470         ) &
7471
7472         pid=$!
7473
7474         for i in {0..100}; do
7475                 # define OBD_FAIL_OSD_TXN_START    0x19a
7476                 local index=$((i % MDSCOUNT + 1))
7477
7478                 do_facet mds$index $LCTL set_param fail_loc=0x8000019a \
7479                         > /dev/null
7480                 usleep 100
7481         done
7482
7483         kill -9 $pid
7484
7485         for i in $(seq $MDSCOUNT); do
7486                 do_facet mds$i $LCTL set_param fail_loc=0 > /dev/null
7487         done
7488
7489         mkdir $DIR/$tdir/new || error "mkdir failed"
7490         rmdir $DIR/$tdir/new || error "rmdir failed"
7491
7492         do_facet mds1 $LCTL lfsck_start -M $(facet_svc mds1) -A -C \
7493                 -t namespace
7494         for i in $(seq $MDSCOUNT); do
7495                 wait_update_facet mds$i "$LCTL get_param -n \
7496                         mdd.$(facet_svc mds$i).lfsck_namespace |
7497                         awk '/^status/ { print \\\$2 }'" "completed"
7498         done
7499
7500         ls -R $DIR/$tdir || error "ls failed"
7501         rm -rf $DIR/$tdir || error "rmdir failed"
7502 }
7503 run_test 60g "transaction abort won't cause MDT hung"
7504
7505 test_60h() {
7506         [ $MDS1_VERSION -le $(version_code 2.12.52) ] &&
7507                 skip "Need MDS version at least 2.12.52"
7508         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
7509
7510         local f
7511
7512         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
7513         #define OBD_FAIL_MDS_STRIPE_FID          0x189
7514         for fail_loc in 0x80000188 0x80000189; do
7515                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
7516                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
7517                         error "mkdir $dir-$fail_loc failed"
7518                 for i in {0..10}; do
7519                         # create may fail on missing stripe
7520                         echo $i > $DIR/$tdir-$fail_loc/$i
7521                 done
7522                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7523                         error "getdirstripe $tdir-$fail_loc failed"
7524                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
7525                         error "migrate $tdir-$fail_loc failed"
7526                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7527                         error "getdirstripe $tdir-$fail_loc failed"
7528                 pushd $DIR/$tdir-$fail_loc
7529                 for f in *; do
7530                         echo $f | cmp $f - || error "$f data mismatch"
7531                 done
7532                 popd
7533                 rm -rf $DIR/$tdir-$fail_loc
7534         done
7535 }
7536 run_test 60h "striped directory with missing stripes can be accessed"
7537
7538 test_61a() {
7539         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7540
7541         f="$DIR/f61"
7542         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
7543         cancel_lru_locks osc
7544         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
7545         sync
7546 }
7547 run_test 61a "mmap() writes don't make sync hang ================"
7548
7549 test_61b() {
7550         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
7551 }
7552 run_test 61b "mmap() of unstriped file is successful"
7553
7554 # bug 2330 - insufficient obd_match error checking causes LBUG
7555 test_62() {
7556         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7557
7558         f="$DIR/f62"
7559         echo foo > $f
7560         cancel_lru_locks osc
7561         lctl set_param fail_loc=0x405
7562         cat $f && error "cat succeeded, expect -EIO"
7563         lctl set_param fail_loc=0
7564 }
7565 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
7566 # match every page all of the time.
7567 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
7568
7569 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
7570 # Though this test is irrelevant anymore, it helped to reveal some
7571 # other grant bugs (LU-4482), let's keep it.
7572 test_63a() {   # was test_63
7573         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7574
7575         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
7576
7577         for i in `seq 10` ; do
7578                 dd if=/dev/zero of=$DIR/f63 bs=8k &
7579                 sleep 5
7580                 kill $!
7581                 sleep 1
7582         done
7583
7584         rm -f $DIR/f63 || true
7585 }
7586 run_test 63a "Verify oig_wait interruption does not crash ======="
7587
7588 # bug 2248 - async write errors didn't return to application on sync
7589 # bug 3677 - async write errors left page locked
7590 test_63b() {
7591         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7592
7593         debugsave
7594         lctl set_param debug=-1
7595
7596         # ensure we have a grant to do async writes
7597         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
7598         rm $DIR/$tfile
7599
7600         sync    # sync lest earlier test intercept the fail_loc
7601
7602         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7603         lctl set_param fail_loc=0x80000406
7604         $MULTIOP $DIR/$tfile Owy && \
7605                 error "sync didn't return ENOMEM"
7606         sync; sleep 2; sync     # do a real sync this time to flush page
7607         lctl get_param -n llite.*.dump_page_cache | grep locked && \
7608                 error "locked page left in cache after async error" || true
7609         debugrestore
7610 }
7611 run_test 63b "async write errors should be returned to fsync ==="
7612
7613 test_64a () {
7614         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7615
7616         lfs df $DIR
7617         lctl get_param osc.*[oO][sS][cC][_-]*.cur* | grep "=[1-9]"
7618 }
7619 run_test 64a "verify filter grant calculations (in kernel) ====="
7620
7621 test_64b () {
7622         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7623
7624         sh oos.sh $MOUNT || error "oos.sh failed: $?"
7625 }
7626 run_test 64b "check out-of-space detection on client"
7627
7628 test_64c() {
7629         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
7630 }
7631 run_test 64c "verify grant shrink"
7632
7633 # this does exactly what osc_request.c:osc_announce_cached() does in
7634 # order to calculate max amount of grants to ask from server
7635 want_grant() {
7636         local tgt=$1
7637
7638         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
7639         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
7640
7641         ((rpc_in_flight ++));
7642         nrpages=$((nrpages * rpc_in_flight))
7643
7644         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
7645
7646         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
7647
7648         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
7649         local undirty=$((nrpages * PAGE_SIZE))
7650
7651         local max_extent_pages
7652         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
7653             grep grant_max_extent_size | awk '{print $2}')
7654         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
7655         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
7656         local grant_extent_tax
7657         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7658             grep grant_extent_tax | awk '{print $2}')
7659
7660         undirty=$((undirty + nrextents * grant_extent_tax))
7661
7662         echo $undirty
7663 }
7664
7665 # this is size of unit for grant allocation. It should be equal to
7666 # what tgt_grant.c:tgt_grant_chunk() calculates
7667 grant_chunk() {
7668         local tgt=$1
7669         local max_brw_size
7670         local grant_extent_tax
7671
7672         max_brw_size=$($LCTL get_param osc.${tgt}.import |
7673             grep max_brw_size | awk '{print $2}')
7674
7675         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7676             grep grant_extent_tax | awk '{print $2}')
7677
7678         echo $(((max_brw_size + grant_extent_tax) * 2))
7679 }
7680
7681 test_64d() {
7682         [ $OST1_VERSION -lt $(version_code 2.10.56) ] &&
7683                 skip "OST < 2.10.55 doesn't limit grants enough"
7684
7685         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
7686         local file=$DIR/$tfile
7687
7688         [[ $($LCTL get_param osc.${tgt}.import |
7689              grep "connect_flags:.*grant_param") ]] ||
7690                 skip "no grant_param connect flag"
7691
7692         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
7693
7694         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
7695
7696         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
7697         stack_trap "rm -f $file" EXIT
7698
7699         $LFS setstripe $file -i 0 -c 1
7700         dd if=/dev/zero of=$file bs=1M count=1000 &
7701         ddpid=$!
7702
7703         while true
7704         do
7705                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
7706                 if [[ $cur_grant -gt $max_cur_granted ]]
7707                 then
7708                         kill $ddpid
7709                         error "cur_grant $cur_grant > $max_cur_granted"
7710                 fi
7711                 kill -0 $ddpid
7712                 [[ $? -ne 0 ]] && break;
7713                 sleep 2
7714         done
7715
7716         rm -f $DIR/$tfile
7717         wait_delete_completed
7718         $LCTL set_param debug="$olddebug" 2> /dev/null || true
7719 }
7720 run_test 64d "check grant limit exceed"
7721
7722 # bug 1414 - set/get directories' stripe info
7723 test_65a() {
7724         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7725
7726         test_mkdir $DIR/$tdir
7727         touch $DIR/$tdir/f1
7728         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
7729 }
7730 run_test 65a "directory with no stripe info"
7731
7732 test_65b() {
7733         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7734
7735         test_mkdir $DIR/$tdir
7736         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7737
7738         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7739                                                 error "setstripe"
7740         touch $DIR/$tdir/f2
7741         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
7742 }
7743 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
7744
7745 test_65c() {
7746         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7747         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
7748
7749         test_mkdir $DIR/$tdir
7750         local stripesize=$($LFS getstripe -S $DIR/$tdir)
7751
7752         $LFS setstripe -S $((stripesize * 4)) -i 1 \
7753                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
7754         touch $DIR/$tdir/f3
7755         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
7756 }
7757 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
7758
7759 test_65d() {
7760         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7761
7762         test_mkdir $DIR/$tdir
7763         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
7764         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7765
7766         if [[ $STRIPECOUNT -le 0 ]]; then
7767                 sc=1
7768         elif [[ $STRIPECOUNT -gt $LOV_MAX_STRIPE_COUNT ]]; then
7769                 [[ $OSTCOUNT -gt $LOV_MAX_STRIPE_COUNT ]] &&
7770                         sc=$LOV_MAX_STRIPE_COUNT || sc=$(($OSTCOUNT - 1))
7771         else
7772                 sc=$(($STRIPECOUNT - 1))
7773         fi
7774         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
7775         touch $DIR/$tdir/f4 $DIR/$tdir/f5
7776         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
7777                 error "lverify failed"
7778 }
7779 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
7780
7781 test_65e() {
7782         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7783
7784         test_mkdir $DIR/$tdir
7785
7786         $LFS setstripe $DIR/$tdir || error "setstripe"
7787         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7788                                         error "no stripe info failed"
7789         touch $DIR/$tdir/f6
7790         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
7791 }
7792 run_test 65e "directory setstripe defaults"
7793
7794 test_65f() {
7795         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7796
7797         test_mkdir $DIR/${tdir}f
7798         $RUNAS $LFS setstripe $DIR/${tdir}f &&
7799                 error "setstripe succeeded" || true
7800 }
7801 run_test 65f "dir setstripe permission (should return error) ==="
7802
7803 test_65g() {
7804         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7805
7806         test_mkdir $DIR/$tdir
7807         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7808
7809         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7810                 error "setstripe -S failed"
7811         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
7812         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7813                 error "delete default stripe failed"
7814 }
7815 run_test 65g "directory setstripe -d"
7816
7817 test_65h() {
7818         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7819
7820         test_mkdir $DIR/$tdir
7821         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7822
7823         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7824                 error "setstripe -S failed"
7825         test_mkdir $DIR/$tdir/dd1
7826         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
7827                 error "stripe info inherit failed"
7828 }
7829 run_test 65h "directory stripe info inherit ===================="
7830
7831 test_65i() {
7832         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7833
7834         save_layout_restore_at_exit $MOUNT
7835
7836         # bug6367: set non-default striping on root directory
7837         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
7838
7839         # bug12836: getstripe on -1 default directory striping
7840         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
7841
7842         # bug12836: getstripe -v on -1 default directory striping
7843         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
7844
7845         # bug12836: new find on -1 default directory striping
7846         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
7847 }
7848 run_test 65i "various tests to set root directory striping"
7849
7850 test_65j() { # bug6367
7851         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7852
7853         sync; sleep 1
7854
7855         # if we aren't already remounting for each test, do so for this test
7856         if [ "$I_MOUNTED" = "yes" ]; then
7857                 cleanup || error "failed to unmount"
7858                 setup
7859         fi
7860
7861         save_layout_restore_at_exit $MOUNT
7862
7863         $LFS setstripe -d $MOUNT || error "setstripe failed"
7864 }
7865 run_test 65j "set default striping on root directory (bug 6367)="
7866
7867 cleanup_65k() {
7868         rm -rf $DIR/$tdir
7869         wait_delete_completed
7870         do_facet $SINGLEMDS "lctl set_param -n \
7871                 osp.$ost*MDT0000.max_create_count=$max_count"
7872         do_facet $SINGLEMDS "lctl set_param -n \
7873                 osp.$ost*MDT0000.create_count=$count"
7874         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7875         echo $INACTIVE_OSC "is Activate"
7876
7877         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7878 }
7879
7880 test_65k() { # bug11679
7881         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7882         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7883         remote_mds_nodsh && skip "remote MDS with nodsh"
7884
7885         local disable_precreate=true
7886         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
7887                 disable_precreate=false
7888
7889         echo "Check OST status: "
7890         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
7891                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
7892
7893         for OSC in $MDS_OSCS; do
7894                 echo $OSC "is active"
7895                 do_facet $SINGLEMDS lctl --device %$OSC activate
7896         done
7897
7898         for INACTIVE_OSC in $MDS_OSCS; do
7899                 local ost=$(osc_to_ost $INACTIVE_OSC)
7900                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
7901                                lov.*md*.target_obd |
7902                                awk -F: /$ost/'{ print $1 }' | head -n 1)
7903
7904                 mkdir -p $DIR/$tdir
7905                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
7906                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
7907
7908                 echo "Deactivate: " $INACTIVE_OSC
7909                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
7910
7911                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
7912                               osp.$ost*MDT0000.create_count")
7913                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
7914                                   osp.$ost*MDT0000.max_create_count")
7915                 $disable_precreate &&
7916                         do_facet $SINGLEMDS "lctl set_param -n \
7917                                 osp.$ost*MDT0000.max_create_count=0"
7918
7919                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
7920                         [ -f $DIR/$tdir/$idx ] && continue
7921                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
7922                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
7923                                 { cleanup_65k;
7924                                   error "setstripe $idx should succeed"; }
7925                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
7926                 done
7927                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
7928                 rmdir $DIR/$tdir
7929
7930                 do_facet $SINGLEMDS "lctl set_param -n \
7931                         osp.$ost*MDT0000.max_create_count=$max_count"
7932                 do_facet $SINGLEMDS "lctl set_param -n \
7933                         osp.$ost*MDT0000.create_count=$count"
7934                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7935                 echo $INACTIVE_OSC "is Activate"
7936
7937                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7938         done
7939 }
7940 run_test 65k "validate manual striping works properly with deactivated OSCs"
7941
7942 test_65l() { # bug 12836
7943         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7944
7945         test_mkdir -p $DIR/$tdir/test_dir
7946         $LFS setstripe -c -1 $DIR/$tdir/test_dir
7947         $LFS find -mtime -1 $DIR/$tdir >/dev/null
7948 }
7949 run_test 65l "lfs find on -1 stripe dir ========================"
7950
7951 test_65m() {
7952         local layout=$(save_layout $MOUNT)
7953         $RUNAS $LFS setstripe -c 2 $MOUNT && {
7954                 restore_layout $MOUNT $layout
7955                 error "setstripe should fail by non-root users"
7956         }
7957         true
7958 }
7959 run_test 65m "normal user can't set filesystem default stripe"
7960
7961 test_65n() {
7962         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
7963         [[ $MDS1_VERSION -ge $(version_code 2.12.50) ]] ||
7964                 skip "Need MDS version at least 2.12.50"
7965         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
7966
7967         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7968         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
7969         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
7970
7971         local root_layout=$(save_layout $MOUNT)
7972         stack_trap "restore_layout $MOUNT $root_layout" EXIT
7973
7974         # new subdirectory under root directory should not inherit
7975         # the default layout from root
7976         local dir1=$MOUNT/$tdir-1
7977         mkdir $dir1 || error "mkdir $dir1 failed"
7978         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
7979                 error "$dir1 shouldn't have LOV EA"
7980
7981         # delete the default layout on root directory
7982         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
7983
7984         local dir2=$MOUNT/$tdir-2
7985         mkdir $dir2 || error "mkdir $dir2 failed"
7986         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
7987                 error "$dir2 shouldn't have LOV EA"
7988
7989         # set a new striping pattern on root directory
7990         local def_stripe_size=$($LFS getstripe -S $MOUNT)
7991         local new_def_stripe_size=$((def_stripe_size * 2))
7992         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
7993                 error "set stripe size on $MOUNT failed"
7994
7995         # new file created in $dir2 should inherit the new stripe size from
7996         # the filesystem default
7997         local file2=$dir2/$tfile-2
7998         touch $file2 || error "touch $file2 failed"
7999
8000         local file2_stripe_size=$($LFS getstripe -S $file2)
8001         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
8002                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
8003
8004         local dir3=$MOUNT/$tdir-3
8005         mkdir $dir3 || error "mkdir $dir3 failed"
8006         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
8007         # the root layout, which is the actual default layout that will be used
8008         # when new files are created in $dir3.
8009         local dir3_layout=$(get_layout_param $dir3)
8010         local root_dir_layout=$(get_layout_param $MOUNT)
8011         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
8012                 error "$dir3 should show the default layout from $MOUNT"
8013
8014         # set OST pool on root directory
8015         local pool=$TESTNAME
8016         pool_add $pool || error "add $pool failed"
8017         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
8018                 error "add targets to $pool failed"
8019
8020         $LFS setstripe -p $pool $MOUNT ||
8021                 error "set OST pool on $MOUNT failed"
8022
8023         # new file created in $dir3 should inherit the pool from
8024         # the filesystem default
8025         local file3=$dir3/$tfile-3
8026         touch $file3 || error "touch $file3 failed"
8027
8028         local file3_pool=$($LFS getstripe -p $file3)
8029         [[ "$file3_pool" = "$pool" ]] ||
8030                 error "$file3 didn't inherit OST pool $pool"
8031
8032         local dir4=$MOUNT/$tdir-4
8033         mkdir $dir4 || error "mkdir $dir4 failed"
8034         local dir4_layout=$(get_layout_param $dir4)
8035         root_dir_layout=$(get_layout_param $MOUNT)
8036         echo "$LFS getstripe -d $dir4"
8037         $LFS getstripe -d $dir4
8038         echo "$LFS getstripe -d $MOUNT"
8039         $LFS getstripe -d $MOUNT
8040         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
8041                 error "$dir4 should show the default layout from $MOUNT"
8042
8043         # new file created in $dir4 should inherit the pool from
8044         # the filesystem default
8045         local file4=$dir4/$tfile-4
8046         touch $file4 || error "touch $file4 failed"
8047
8048         local file4_pool=$($LFS getstripe -p $file4)
8049         [[ "$file4_pool" = "$pool" ]] ||
8050                 error "$file4 didn't inherit OST pool $pool"
8051
8052         # new subdirectory under non-root directory should inherit
8053         # the default layout from its parent directory
8054         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
8055                 error "set directory layout on $dir4 failed"
8056
8057         local dir5=$dir4/$tdir-5
8058         mkdir $dir5 || error "mkdir $dir5 failed"
8059
8060         dir4_layout=$(get_layout_param $dir4)
8061         local dir5_layout=$(get_layout_param $dir5)
8062         [[ "$dir4_layout" = "$dir5_layout" ]] ||
8063                 error "$dir5 should inherit the default layout from $dir4"
8064
8065         # though subdir under ROOT doesn't inherit default layout, but
8066         # its sub dir/file should be created with default layout.
8067         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
8068         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
8069                 skip "Need MDS version at least 2.12.59"
8070
8071         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
8072         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
8073         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
8074
8075         if [ $default_lmv_hash == "none" ]; then
8076                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
8077         else
8078                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
8079                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
8080         fi
8081
8082         $LFS setdirstripe -D -c 2 $MOUNT ||
8083                 error "setdirstripe -D -c 2 failed"
8084         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
8085         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
8086         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
8087 }
8088 run_test 65n "don't inherit default layout from root for new subdirectories"
8089
8090 # bug 2543 - update blocks count on client
8091 test_66() {
8092         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8093
8094         COUNT=${COUNT:-8}
8095         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
8096         sync; sync_all_data; sync; sync_all_data
8097         cancel_lru_locks osc
8098         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
8099         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
8100 }
8101 run_test 66 "update inode blocks count on client ==============="
8102
8103 meminfo() {
8104         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
8105 }
8106
8107 swap_used() {
8108         swapon -s | awk '($1 == "'$1'") { print $4 }'
8109 }
8110
8111 # bug5265, obdfilter oa2dentry return -ENOENT
8112 # #define OBD_FAIL_SRV_ENOENT 0x217
8113 test_69() {
8114         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8115         remote_ost_nodsh && skip "remote OST with nodsh"
8116
8117         f="$DIR/$tfile"
8118         $LFS setstripe -c 1 -i 0 $f
8119
8120         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
8121
8122         do_facet ost1 lctl set_param fail_loc=0x217
8123         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
8124         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
8125
8126         do_facet ost1 lctl set_param fail_loc=0
8127         $DIRECTIO write $f 0 2 || error "write error"
8128
8129         cancel_lru_locks osc
8130         $DIRECTIO read $f 0 1 || error "read error"
8131
8132         do_facet ost1 lctl set_param fail_loc=0x217
8133         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
8134
8135         do_facet ost1 lctl set_param fail_loc=0
8136         rm -f $f
8137 }
8138 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
8139
8140 test_71() {
8141         test_mkdir $DIR/$tdir
8142         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
8143         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
8144 }
8145 run_test 71 "Running dbench on lustre (don't segment fault) ===="
8146
8147 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
8148         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8149         [ "$RUNAS_ID" = "$UID" ] &&
8150                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8151         # Check that testing environment is properly set up. Skip if not
8152         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
8153                 skip_env "User $RUNAS_ID does not exist - skipping"
8154
8155         touch $DIR/$tfile
8156         chmod 777 $DIR/$tfile
8157         chmod ug+s $DIR/$tfile
8158         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
8159                 error "$RUNAS dd $DIR/$tfile failed"
8160         # See if we are still setuid/sgid
8161         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8162                 error "S/gid is not dropped on write"
8163         # Now test that MDS is updated too
8164         cancel_lru_locks mdc
8165         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8166                 error "S/gid is not dropped on MDS"
8167         rm -f $DIR/$tfile
8168 }
8169 run_test 72a "Test that remove suid works properly (bug5695) ===="
8170
8171 test_72b() { # bug 24226 -- keep mode setting when size is not changing
8172         local perm
8173
8174         [ "$RUNAS_ID" = "$UID" ] &&
8175                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8176         [ "$RUNAS_ID" -eq 0 ] &&
8177                 skip_env "RUNAS_ID = 0 -- skipping"
8178         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8179         # Check that testing environment is properly set up. Skip if not
8180         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
8181                 skip_env "User $RUNAS_ID does not exist - skipping"
8182
8183         touch $DIR/${tfile}-f{g,u}
8184         test_mkdir $DIR/${tfile}-dg
8185         test_mkdir $DIR/${tfile}-du
8186         chmod 770 $DIR/${tfile}-{f,d}{g,u}
8187         chmod g+s $DIR/${tfile}-{f,d}g
8188         chmod u+s $DIR/${tfile}-{f,d}u
8189         for perm in 777 2777 4777; do
8190                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
8191                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
8192                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
8193                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
8194         done
8195         true
8196 }
8197 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
8198
8199 # bug 3462 - multiple simultaneous MDC requests
8200 test_73() {
8201         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8202
8203         test_mkdir $DIR/d73-1
8204         test_mkdir $DIR/d73-2
8205         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
8206         pid1=$!
8207
8208         lctl set_param fail_loc=0x80000129
8209         $MULTIOP $DIR/d73-1/f73-2 Oc &
8210         sleep 1
8211         lctl set_param fail_loc=0
8212
8213         $MULTIOP $DIR/d73-2/f73-3 Oc &
8214         pid3=$!
8215
8216         kill -USR1 $pid1
8217         wait $pid1 || return 1
8218
8219         sleep 25
8220
8221         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
8222         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
8223         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
8224
8225         rm -rf $DIR/d73-*
8226 }
8227 run_test 73 "multiple MDC requests (should not deadlock)"
8228
8229 test_74a() { # bug 6149, 6184
8230         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8231
8232         touch $DIR/f74a
8233         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8234         #
8235         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8236         # will spin in a tight reconnection loop
8237         $LCTL set_param fail_loc=0x8000030e
8238         # get any lock that won't be difficult - lookup works.
8239         ls $DIR/f74a
8240         $LCTL set_param fail_loc=0
8241         rm -f $DIR/f74a
8242         true
8243 }
8244 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
8245
8246 test_74b() { # bug 13310
8247         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8248
8249         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8250         #
8251         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8252         # will spin in a tight reconnection loop
8253         $LCTL set_param fail_loc=0x8000030e
8254         # get a "difficult" lock
8255         touch $DIR/f74b
8256         $LCTL set_param fail_loc=0
8257         rm -f $DIR/f74b
8258         true
8259 }
8260 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
8261
8262 test_74c() {
8263         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8264
8265         #define OBD_FAIL_LDLM_NEW_LOCK
8266         $LCTL set_param fail_loc=0x319
8267         touch $DIR/$tfile && error "touch successful"
8268         $LCTL set_param fail_loc=0
8269         true
8270 }
8271 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
8272
8273 num_inodes() {
8274         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
8275 }
8276
8277 test_76() { # Now for bug 20433, added originally in bug 1443
8278         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8279
8280         cancel_lru_locks osc
8281         local cpus=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
8282         local before=$(num_inodes)
8283         local count=$((512 * cpus))
8284         [ "$SLOW" = "no" ] && count=$((64 * cpus))
8285
8286         echo "before inodes: $before"
8287         for i in $(seq $count); do
8288                 touch $DIR/$tfile
8289                 rm -f $DIR/$tfile
8290         done
8291         cancel_lru_locks osc
8292         local after=$(num_inodes)
8293         echo "after inodes: $after"
8294         while (( after > before + 8 * ${cpus:-1} )); do
8295                 sleep 1
8296                 after=$(num_inodes)
8297                 wait=$((wait + 1))
8298                 (( wait % 5 == 0 )) && echo "wait $wait seconds inodes: $after"
8299                 if (( wait > 30 )); then
8300                         error "inode slab grew from $before to $after"
8301                 fi
8302         done
8303 }
8304 run_test 76 "confirm clients recycle inodes properly ===="
8305
8306
8307 export ORIG_CSUM=""
8308 set_checksums()
8309 {
8310         # Note: in sptlrpc modes which enable its own bulk checksum, the
8311         # original crc32_le bulk checksum will be automatically disabled,
8312         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
8313         # will be checked by sptlrpc code against sptlrpc bulk checksum.
8314         # In this case set_checksums() will not be no-op, because sptlrpc
8315         # bulk checksum will be enabled all through the test.
8316
8317         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
8318         lctl set_param -n osc.*.checksums $1
8319         return 0
8320 }
8321
8322 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8323                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
8324 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8325                              tr -d [] | head -n1)}
8326 set_checksum_type()
8327 {
8328         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
8329         rc=$?
8330         log "set checksum type to $1, rc = $rc"
8331         return $rc
8332 }
8333
8334 get_osc_checksum_type()
8335 {
8336         # arugment 1: OST name, like OST0000
8337         ost=$1
8338         checksum_type=$(lctl get_param -n osc.*${ost}-osc-[^mM]*.checksum_type |
8339                         sed 's/.*\[\(.*\)\].*/\1/g')
8340         rc=$?
8341         [ $rc -ne 0 ] && error "failed to get checksum type of $ost, rc = $rc, output = $checksum_type"
8342         echo $checksum_type
8343 }
8344
8345 F77_TMP=$TMP/f77-temp
8346 F77SZ=8
8347 setup_f77() {
8348         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
8349                 error "error writing to $F77_TMP"
8350 }
8351
8352 test_77a() { # bug 10889
8353         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8354         $GSS && skip_env "could not run with gss"
8355
8356         [ ! -f $F77_TMP ] && setup_f77
8357         set_checksums 1
8358         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
8359         set_checksums 0
8360         rm -f $DIR/$tfile
8361 }
8362 run_test 77a "normal checksum read/write operation"
8363
8364 test_77b() { # bug 10889
8365         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8366         $GSS && skip_env "could not run with gss"
8367
8368         [ ! -f $F77_TMP ] && setup_f77
8369         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8370         $LCTL set_param fail_loc=0x80000409
8371         set_checksums 1
8372
8373         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8374                 error "dd error: $?"
8375         $LCTL set_param fail_loc=0
8376
8377         for algo in $CKSUM_TYPES; do
8378                 cancel_lru_locks osc
8379                 set_checksum_type $algo
8380                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8381                 $LCTL set_param fail_loc=0x80000408
8382                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
8383                 $LCTL set_param fail_loc=0
8384         done
8385         set_checksums 0
8386         set_checksum_type $ORIG_CSUM_TYPE
8387         rm -f $DIR/$tfile
8388 }
8389 run_test 77b "checksum error on client write, read"
8390
8391 cleanup_77c() {
8392         trap 0
8393         set_checksums 0
8394         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
8395         $check_ost &&
8396                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
8397         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
8398         $check_ost && [ -n "$ost_file_prefix" ] &&
8399                 do_facet ost1 rm -f ${ost_file_prefix}\*
8400 }
8401
8402 test_77c() {
8403         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8404         $GSS && skip_env "could not run with gss"
8405         remote_ost_nodsh && skip "remote OST with nodsh"
8406
8407         local bad1
8408         local osc_file_prefix
8409         local osc_file
8410         local check_ost=false
8411         local ost_file_prefix
8412         local ost_file
8413         local orig_cksum
8414         local dump_cksum
8415         local fid
8416
8417         # ensure corruption will occur on first OSS/OST
8418         $LFS setstripe -i 0 $DIR/$tfile
8419
8420         [ ! -f $F77_TMP ] && setup_f77
8421         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8422                 error "dd write error: $?"
8423         fid=$($LFS path2fid $DIR/$tfile)
8424
8425         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
8426         then
8427                 check_ost=true
8428                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
8429                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
8430         else
8431                 echo "OSS do not support bulk pages dump upon error"
8432         fi
8433
8434         osc_file_prefix=$($LCTL get_param -n debug_path)
8435         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
8436
8437         trap cleanup_77c EXIT
8438
8439         set_checksums 1
8440         # enable bulk pages dump upon error on Client
8441         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
8442         # enable bulk pages dump upon error on OSS
8443         $check_ost &&
8444                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
8445
8446         # flush Client cache to allow next read to reach OSS
8447         cancel_lru_locks osc
8448
8449         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
8450         $LCTL set_param fail_loc=0x80000408
8451         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
8452         $LCTL set_param fail_loc=0
8453
8454         rm -f $DIR/$tfile
8455
8456         # check cksum dump on Client
8457         osc_file=$(ls ${osc_file_prefix}*)
8458         [ -n "$osc_file" ] || error "no checksum dump file on Client"
8459         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
8460         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
8461         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
8462         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
8463                      cksum)
8464         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
8465         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8466                 error "dump content does not match on Client"
8467
8468         $check_ost || skip "No need to check cksum dump on OSS"
8469
8470         # check cksum dump on OSS
8471         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
8472         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
8473         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
8474         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
8475         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8476                 error "dump content does not match on OSS"
8477
8478         cleanup_77c
8479 }
8480 run_test 77c "checksum error on client read with debug"
8481
8482 test_77d() { # bug 10889
8483         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8484         $GSS && skip_env "could not run with gss"
8485
8486         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8487         $LCTL set_param fail_loc=0x80000409
8488         set_checksums 1
8489         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8490                 error "direct write: rc=$?"
8491         $LCTL set_param fail_loc=0
8492         set_checksums 0
8493
8494         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8495         $LCTL set_param fail_loc=0x80000408
8496         set_checksums 1
8497         cancel_lru_locks osc
8498         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8499                 error "direct read: rc=$?"
8500         $LCTL set_param fail_loc=0
8501         set_checksums 0
8502 }
8503 run_test 77d "checksum error on OST direct write, read"
8504
8505 test_77f() { # bug 10889
8506         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8507         $GSS && skip_env "could not run with gss"
8508
8509         set_checksums 1
8510         for algo in $CKSUM_TYPES; do
8511                 cancel_lru_locks osc
8512                 set_checksum_type $algo
8513                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8514                 $LCTL set_param fail_loc=0x409
8515                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
8516                         error "direct write succeeded"
8517                 $LCTL set_param fail_loc=0
8518         done
8519         set_checksum_type $ORIG_CSUM_TYPE
8520         set_checksums 0
8521 }
8522 run_test 77f "repeat checksum error on write (expect error)"
8523
8524 test_77g() { # bug 10889
8525         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8526         $GSS && skip_env "could not run with gss"
8527         remote_ost_nodsh && skip "remote OST with nodsh"
8528
8529         [ ! -f $F77_TMP ] && setup_f77
8530
8531         local file=$DIR/$tfile
8532         stack_trap "rm -f $file" EXIT
8533
8534         $LFS setstripe -c 1 -i 0 $file
8535         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
8536         do_facet ost1 lctl set_param fail_loc=0x8000021a
8537         set_checksums 1
8538         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
8539                 error "write error: rc=$?"
8540         do_facet ost1 lctl set_param fail_loc=0
8541         set_checksums 0
8542
8543         cancel_lru_locks osc
8544         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
8545         do_facet ost1 lctl set_param fail_loc=0x8000021b
8546         set_checksums 1
8547         cmp $F77_TMP $file || error "file compare failed"
8548         do_facet ost1 lctl set_param fail_loc=0
8549         set_checksums 0
8550 }
8551 run_test 77g "checksum error on OST write, read"
8552
8553 test_77k() { # LU-10906
8554         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8555         $GSS && skip_env "could not run with gss"
8556
8557         local cksum_param="osc.$FSNAME*.checksums"
8558         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
8559         local checksum
8560         local i
8561
8562         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
8563         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
8564         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
8565                 EXIT
8566
8567         for i in 0 1; do
8568                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
8569                         error "failed to set checksum=$i on MGS"
8570                 wait_update $HOSTNAME "$get_checksum" $i
8571                 #remount
8572                 echo "remount client, checksum should be $i"
8573                 remount_client $MOUNT || error "failed to remount client"
8574                 checksum=$(eval $get_checksum)
8575                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8576         done
8577         # remove persistent param to avoid races with checksum mountopt below
8578         do_facet mgs $LCTL set_param -P -d $cksum_param ||
8579                 error "failed to delete checksum on MGS"
8580
8581         for opt in "checksum" "nochecksum"; do
8582                 #remount with mount option
8583                 echo "remount client with option $opt, checksum should be $i"
8584                 umount_client $MOUNT || error "failed to umount client"
8585                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
8586                         error "failed to mount client with option '$opt'"
8587                 checksum=$(eval $get_checksum)
8588                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8589                 i=$((i - 1))
8590         done
8591
8592         remount_client $MOUNT || error "failed to remount client"
8593 }
8594 run_test 77k "enable/disable checksum correctly"
8595
8596 test_77l() {
8597         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8598         $GSS && skip_env "could not run with gss"
8599
8600         set_checksums 1
8601         stack_trap "set_checksums $ORIG_CSUM" EXIT
8602         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
8603
8604         set_checksum_type invalid && error "unexpected success of invalid checksum type"
8605
8606         $LFS setstripe -c 1 -i 0 $DIR/$tfile
8607         for algo in $CKSUM_TYPES; do
8608                 set_checksum_type $algo || error "fail to set checksum type $algo"
8609                 osc_algo=$(get_osc_checksum_type OST0000)
8610                 [ "$osc_algo" != "$algo" ] && error "checksum type is $osc_algo after setting it to $algo"
8611
8612                 # no locks, no reqs to let the connection idle
8613                 cancel_lru_locks osc
8614                 lru_resize_disable osc
8615                 wait_osc_import_state client ost1 IDLE
8616
8617                 # ensure ost1 is connected
8618                 stat $DIR/$tfile >/dev/null || error "can't stat"
8619                 wait_osc_import_state client ost1 FULL
8620
8621                 osc_algo=$(get_osc_checksum_type OST0000)
8622                 [ "$osc_algo" != "$algo" ] && error "checksum type changed from $algo to $osc_algo after reconnection"
8623         done
8624         return 0
8625 }
8626 run_test 77l "preferred checksum type is remembered after reconnected"
8627
8628 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
8629 rm -f $F77_TMP
8630 unset F77_TMP
8631
8632 cleanup_test_78() {
8633         trap 0
8634         rm -f $DIR/$tfile
8635 }
8636
8637 test_78() { # bug 10901
8638         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8639         remote_ost || skip_env "local OST"
8640
8641         NSEQ=5
8642         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
8643         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
8644         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
8645         echo "MemTotal: $MEMTOTAL"
8646
8647         # reserve 256MB of memory for the kernel and other running processes,
8648         # and then take 1/2 of the remaining memory for the read/write buffers.
8649         if [ $MEMTOTAL -gt 512 ] ;then
8650                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
8651         else
8652                 # for those poor memory-starved high-end clusters...
8653                 MEMTOTAL=$((MEMTOTAL / 2))
8654         fi
8655         echo "Mem to use for directio: $MEMTOTAL"
8656
8657         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
8658         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
8659         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
8660         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
8661                 head -n1)
8662         echo "Smallest OST: $SMALLESTOST"
8663         [[ $SMALLESTOST -lt 10240 ]] &&
8664                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
8665
8666         trap cleanup_test_78 EXIT
8667
8668         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
8669                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
8670
8671         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
8672         echo "File size: $F78SIZE"
8673         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
8674         for i in $(seq 1 $NSEQ); do
8675                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
8676                 echo directIO rdwr round $i of $NSEQ
8677                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
8678         done
8679
8680         cleanup_test_78
8681 }
8682 run_test 78 "handle large O_DIRECT writes correctly ============"
8683
8684 test_79() { # bug 12743
8685         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8686
8687         wait_delete_completed
8688
8689         BKTOTAL=$(calc_osc_kbytes kbytestotal)
8690         BKFREE=$(calc_osc_kbytes kbytesfree)
8691         BKAVAIL=$(calc_osc_kbytes kbytesavail)
8692
8693         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
8694         DFTOTAL=`echo $STRING | cut -d, -f1`
8695         DFUSED=`echo $STRING  | cut -d, -f2`
8696         DFAVAIL=`echo $STRING | cut -d, -f3`
8697         DFFREE=$(($DFTOTAL - $DFUSED))
8698
8699         ALLOWANCE=$((64 * $OSTCOUNT))
8700
8701         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
8702            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
8703                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
8704         fi
8705         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
8706            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
8707                 error "df free($DFFREE) mismatch OST free($BKFREE)"
8708         fi
8709         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
8710            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
8711                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
8712         fi
8713 }
8714 run_test 79 "df report consistency check ======================="
8715
8716 test_80() { # bug 10718
8717         remote_ost_nodsh && skip "remote OST with nodsh"
8718         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8719
8720         # relax strong synchronous semantics for slow backends like ZFS
8721         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
8722                 local soc="obdfilter.*.sync_lock_cancel"
8723                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8724
8725                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
8726                 if [ -z "$save" ]; then
8727                         soc="obdfilter.*.sync_on_lock_cancel"
8728                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8729                 fi
8730
8731                 if [ "$save" != "never" ]; then
8732                         local hosts=$(comma_list $(osts_nodes))
8733
8734                         do_nodes $hosts $LCTL set_param $soc=never
8735                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
8736                 fi
8737         fi
8738
8739         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
8740         sync; sleep 1; sync
8741         local before=$(date +%s)
8742         cancel_lru_locks osc
8743         local after=$(date +%s)
8744         local diff=$((after - before))
8745         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
8746
8747         rm -f $DIR/$tfile
8748 }
8749 run_test 80 "Page eviction is equally fast at high offsets too"
8750
8751 test_81a() { # LU-456
8752         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8753         remote_ost_nodsh && skip "remote OST with nodsh"
8754
8755         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8756         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
8757         do_facet ost1 lctl set_param fail_loc=0x80000228
8758
8759         # write should trigger a retry and success
8760         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8761         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8762         RC=$?
8763         if [ $RC -ne 0 ] ; then
8764                 error "write should success, but failed for $RC"
8765         fi
8766 }
8767 run_test 81a "OST should retry write when get -ENOSPC ==============="
8768
8769 test_81b() { # LU-456
8770         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8771         remote_ost_nodsh && skip "remote OST with nodsh"
8772
8773         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8774         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
8775         do_facet ost1 lctl set_param fail_loc=0x228
8776
8777         # write should retry several times and return -ENOSPC finally
8778         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8779         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8780         RC=$?
8781         ENOSPC=28
8782         if [ $RC -ne $ENOSPC ] ; then
8783                 error "dd should fail for -ENOSPC, but succeed."
8784         fi
8785 }
8786 run_test 81b "OST should return -ENOSPC when retry still fails ======="
8787
8788 test_82() { # LU-1031
8789         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
8790         local gid1=14091995
8791         local gid2=16022000
8792
8793         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
8794         local MULTIPID1=$!
8795         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
8796         local MULTIPID2=$!
8797         kill -USR1 $MULTIPID2
8798         sleep 2
8799         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
8800                 error "First grouplock does not block second one"
8801         else
8802                 echo "Second grouplock blocks first one"
8803         fi
8804         kill -USR1 $MULTIPID1
8805         wait $MULTIPID1
8806         wait $MULTIPID2
8807 }
8808 run_test 82 "Basic grouplock test"
8809
8810 test_99() {
8811         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
8812
8813         test_mkdir $DIR/$tdir.cvsroot
8814         chown $RUNAS_ID $DIR/$tdir.cvsroot
8815
8816         cd $TMP
8817         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
8818
8819         cd /etc/init.d
8820         # some versions of cvs import exit(1) when asked to import links or
8821         # files they can't read.  ignore those files.
8822         local toignore=$(find . -type l -printf '-I %f\n' -o \
8823                          ! -perm /4 -printf '-I %f\n')
8824         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
8825                 $tdir.reposname vtag rtag
8826
8827         cd $DIR
8828         test_mkdir $DIR/$tdir.reposname
8829         chown $RUNAS_ID $DIR/$tdir.reposname
8830         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
8831
8832         cd $DIR/$tdir.reposname
8833         $RUNAS touch foo99
8834         $RUNAS cvs add -m 'addmsg' foo99
8835         $RUNAS cvs update
8836         $RUNAS cvs commit -m 'nomsg' foo99
8837         rm -fr $DIR/$tdir.cvsroot
8838 }
8839 run_test 99 "cvs strange file/directory operations"
8840
8841 test_100() {
8842         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8843         [[ "$NETTYPE" =~ tcp ]] ||
8844                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
8845         remote_ost_nodsh && skip "remote OST with nodsh"
8846         remote_mds_nodsh && skip "remote MDS with nodsh"
8847         remote_servers ||
8848                 skip "useless for local single node setup"
8849
8850         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
8851                 [ "$PROT" != "tcp" ] && continue
8852                 RPORT=$(echo $REMOTE | cut -d: -f2)
8853                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
8854
8855                 rc=0
8856                 LPORT=`echo $LOCAL | cut -d: -f2`
8857                 if [ $LPORT -ge 1024 ]; then
8858                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
8859                         netstat -tna
8860                         error_exit "local: $LPORT > 1024, remote: $RPORT"
8861                 fi
8862         done
8863         [ "$rc" = 0 ] || error_exit "privileged port not found" )
8864 }
8865 run_test 100 "check local port using privileged port ==========="
8866
8867 function get_named_value()
8868 {
8869     local tag
8870
8871     tag=$1
8872     while read ;do
8873         line=$REPLY
8874         case $line in
8875         $tag*)
8876             echo $line | sed "s/^$tag[ ]*//"
8877             break
8878             ;;
8879         esac
8880     done
8881 }
8882
8883 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
8884                    awk '/^max_cached_mb/ { print $2 }')
8885
8886 cleanup_101a() {
8887         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
8888         trap 0
8889 }
8890
8891 test_101a() {
8892         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8893
8894         local s
8895         local discard
8896         local nreads=10000
8897         local cache_limit=32
8898
8899         $LCTL set_param -n osc.*-osc*.rpc_stats 0
8900         trap cleanup_101a EXIT
8901         $LCTL set_param -n llite.*.read_ahead_stats 0
8902         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
8903
8904         #
8905         # randomly read 10000 of 64K chunks from file 3x 32MB in size
8906         #
8907         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
8908         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
8909
8910         discard=0
8911         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
8912                 get_named_value 'read but discarded' | cut -d" " -f1); do
8913                         discard=$(($discard + $s))
8914         done
8915         cleanup_101a
8916
8917         $LCTL get_param osc.*-osc*.rpc_stats
8918         $LCTL get_param llite.*.read_ahead_stats
8919
8920         # Discard is generally zero, but sometimes a few random reads line up
8921         # and trigger larger readahead, which is wasted & leads to discards.
8922         if [[ $(($discard)) -gt $nreads ]]; then
8923                 error "too many ($discard) discarded pages"
8924         fi
8925         rm -f $DIR/$tfile || true
8926 }
8927 run_test 101a "check read-ahead for random reads"
8928
8929 setup_test101bc() {
8930         test_mkdir $DIR/$tdir
8931         local ssize=$1
8932         local FILE_LENGTH=$2
8933         STRIPE_OFFSET=0
8934
8935         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
8936
8937         local list=$(comma_list $(osts_nodes))
8938         set_osd_param $list '' read_cache_enable 0
8939         set_osd_param $list '' writethrough_cache_enable 0
8940
8941         trap cleanup_test101bc EXIT
8942         # prepare the read-ahead file
8943         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
8944
8945         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
8946                                 count=$FILE_SIZE_MB 2> /dev/null
8947
8948 }
8949
8950 cleanup_test101bc() {
8951         trap 0
8952         rm -rf $DIR/$tdir
8953         rm -f $DIR/$tfile
8954
8955         local list=$(comma_list $(osts_nodes))
8956         set_osd_param $list '' read_cache_enable 1
8957         set_osd_param $list '' writethrough_cache_enable 1
8958 }
8959
8960 calc_total() {
8961         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
8962 }
8963
8964 ra_check_101() {
8965         local READ_SIZE=$1
8966         local STRIPE_SIZE=$2
8967         local FILE_LENGTH=$3
8968         local RA_INC=1048576
8969         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
8970         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
8971                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
8972         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
8973                         get_named_value 'read but discarded' |
8974                         cut -d" " -f1 | calc_total)
8975         if [[ $DISCARD -gt $discard_limit ]]; then
8976                 $LCTL get_param llite.*.read_ahead_stats
8977                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
8978         else
8979                 echo "Read-ahead success for size ${READ_SIZE}"
8980         fi
8981 }
8982
8983 test_101b() {
8984         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8985         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8986
8987         local STRIPE_SIZE=1048576
8988         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
8989
8990         if [ $SLOW == "yes" ]; then
8991                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
8992         else
8993                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
8994         fi
8995
8996         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
8997
8998         # prepare the read-ahead file
8999         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
9000         cancel_lru_locks osc
9001         for BIDX in 2 4 8 16 32 64 128 256
9002         do
9003                 local BSIZE=$((BIDX*4096))
9004                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
9005                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
9006                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
9007                 $LCTL set_param -n llite.*.read_ahead_stats 0
9008                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
9009                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
9010                 cancel_lru_locks osc
9011                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
9012         done
9013         cleanup_test101bc
9014         true
9015 }
9016 run_test 101b "check stride-io mode read-ahead ================="
9017
9018 test_101c() {
9019         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9020
9021         local STRIPE_SIZE=1048576
9022         local FILE_LENGTH=$((STRIPE_SIZE*100))
9023         local nreads=10000
9024         local rsize=65536
9025         local osc_rpc_stats
9026
9027         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
9028
9029         cancel_lru_locks osc
9030         $LCTL set_param osc.*.rpc_stats 0
9031         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
9032         $LCTL get_param osc.*.rpc_stats
9033         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
9034                 local stats=$($LCTL get_param -n $osc_rpc_stats)
9035                 local lines=$(echo "$stats" | awk 'END {print NR;}')
9036                 local size
9037
9038                 if [ $lines -le 20 ]; then
9039                         echo "continue debug"
9040                         continue
9041                 fi
9042                 for size in 1 2 4 8; do
9043                         local rpc=$(echo "$stats" |
9044                                     awk '($1 == "'$size':") {print $2; exit; }')
9045                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
9046                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
9047                 done
9048                 echo "$osc_rpc_stats check passed!"
9049         done
9050         cleanup_test101bc
9051         true
9052 }
9053 run_test 101c "check stripe_size aligned read-ahead ================="
9054
9055 test_101d() {
9056         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9057
9058         local file=$DIR/$tfile
9059         local sz_MB=${FILESIZE_101d:-500}
9060         local ra_MB=${READAHEAD_MB:-40}
9061
9062         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
9063         [ $free_MB -lt $sz_MB ] &&
9064                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
9065
9066         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
9067         $LFS setstripe -c -1 $file || error "setstripe failed"
9068
9069         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
9070         echo Cancel LRU locks on lustre client to flush the client cache
9071         cancel_lru_locks osc
9072
9073         echo Disable read-ahead
9074         local old_RA=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
9075         $LCTL set_param -n llite.*.max_read_ahead_mb=0
9076         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_RA" EXIT
9077         $LCTL get_param -n llite.*.max_read_ahead_mb
9078
9079         echo Reading the test file $file with read-ahead disabled
9080         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
9081
9082         echo Cancel LRU locks on lustre client to flush the client cache
9083         cancel_lru_locks osc
9084         echo Enable read-ahead with ${ra_MB}MB
9085         $LCTL set_param -n llite.*.max_read_ahead_mb=$ra_MB
9086
9087         echo Reading the test file $file with read-ahead enabled
9088         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
9089
9090         echo "read-ahead disabled time read $raOFF"
9091         echo "read-ahead enabled  time read $raON"
9092
9093         rm -f $file
9094         wait_delete_completed
9095
9096         [ $raOFF -le 1 ] || [ $raON -lt $raOFF ] ||
9097                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
9098 }
9099 run_test 101d "file read with and without read-ahead enabled"
9100
9101 test_101e() {
9102         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9103
9104         local file=$DIR/$tfile
9105         local size_KB=500  #KB
9106         local count=100
9107         local bsize=1024
9108
9109         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
9110         local need_KB=$((count * size_KB))
9111         [[ $free_KB -le $need_KB ]] &&
9112                 skip_env "Need free space $need_KB, have $free_KB"
9113
9114         echo "Creating $count ${size_KB}K test files"
9115         for ((i = 0; i < $count; i++)); do
9116                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
9117         done
9118
9119         echo "Cancel LRU locks on lustre client to flush the client cache"
9120         cancel_lru_locks $OSC
9121
9122         echo "Reset readahead stats"
9123         $LCTL set_param -n llite.*.read_ahead_stats 0
9124
9125         for ((i = 0; i < $count; i++)); do
9126                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
9127         done
9128
9129         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9130                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
9131
9132         for ((i = 0; i < $count; i++)); do
9133                 rm -rf $file.$i 2>/dev/null
9134         done
9135
9136         #10000 means 20% reads are missing in readahead
9137         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
9138 }
9139 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
9140
9141 test_101f() {
9142         which iozone || skip_env "no iozone installed"
9143
9144         local old_debug=$($LCTL get_param debug)
9145         old_debug=${old_debug#*=}
9146         $LCTL set_param debug="reada mmap"
9147
9148         # create a test file
9149         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
9150
9151         echo Cancel LRU locks on lustre client to flush the client cache
9152         cancel_lru_locks osc
9153
9154         echo Reset readahead stats
9155         $LCTL set_param -n llite.*.read_ahead_stats 0
9156
9157         echo mmap read the file with small block size
9158         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
9159                 > /dev/null 2>&1
9160
9161         echo checking missing pages
9162         $LCTL get_param llite.*.read_ahead_stats
9163         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9164                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9165
9166         $LCTL set_param debug="$old_debug"
9167         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
9168         rm -f $DIR/$tfile
9169 }
9170 run_test 101f "check mmap read performance"
9171
9172 test_101g_brw_size_test() {
9173         local mb=$1
9174         local pages=$((mb * 1048576 / PAGE_SIZE))
9175         local file=$DIR/$tfile
9176
9177         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
9178                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
9179         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
9180                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
9181                         return 2
9182         done
9183
9184         stack_trap "rm -f $file" EXIT
9185         $LCTL set_param -n osc.*.rpc_stats=0
9186
9187         # 10 RPCs should be enough for the test
9188         local count=10
9189         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
9190                 { error "dd write ${mb} MB blocks failed"; return 3; }
9191         cancel_lru_locks osc
9192         dd of=/dev/null if=$file bs=${mb}M count=$count ||
9193                 { error "dd write ${mb} MB blocks failed"; return 4; }
9194
9195         # calculate number of full-sized read and write RPCs
9196         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
9197                 sed -n '/pages per rpc/,/^$/p' |
9198                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
9199                 END { print reads,writes }'))
9200         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
9201                 return 5
9202         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
9203                 return 6
9204
9205         return 0
9206 }
9207
9208 test_101g() {
9209         remote_ost_nodsh && skip "remote OST with nodsh"
9210
9211         local rpcs
9212         local osts=$(get_facets OST)
9213         local list=$(comma_list $(osts_nodes))
9214         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
9215         local brw_size="obdfilter.*.brw_size"
9216
9217         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9218
9219         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
9220
9221         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
9222                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
9223                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
9224            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
9225                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
9226                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
9227
9228                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
9229                         suffix="M"
9230
9231                 if [[ $orig_mb -lt 16 ]]; then
9232                         save_lustre_params $osts "$brw_size" > $p
9233                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
9234                                 error "set 16MB RPC size failed"
9235
9236                         echo "remount client to enable new RPC size"
9237                         remount_client $MOUNT || error "remount_client failed"
9238                 fi
9239
9240                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
9241                 # should be able to set brw_size=12, but no rpc_stats for that
9242                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
9243         fi
9244
9245         test_101g_brw_size_test 4 || error "4MB RPC test failed"
9246
9247         if [[ $orig_mb -lt 16 ]]; then
9248                 restore_lustre_params < $p
9249                 remount_client $MOUNT || error "remount_client restore failed"
9250         fi
9251
9252         rm -f $p $DIR/$tfile
9253 }
9254 run_test 101g "Big bulk(4/16 MiB) readahead"
9255
9256 test_101h() {
9257         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9258
9259         dd if=/dev/zero of=$DIR/$tfile bs=1M count=70 ||
9260                 error "dd 70M file failed"
9261         echo Cancel LRU locks on lustre client to flush the client cache
9262         cancel_lru_locks osc
9263
9264         echo "Reset readahead stats"
9265         $LCTL set_param -n llite.*.read_ahead_stats 0
9266
9267         echo "Read 10M of data but cross 64M bundary"
9268         dd if=$DIR/$tfile of=/dev/null bs=10M skip=6 count=1
9269         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9270                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9271         [ $miss -eq 1 ] || error "expected miss 1 but got $miss"
9272         rm -f $p $DIR/$tfile
9273 }
9274 run_test 101h "Readahead should cover current read window"
9275
9276 test_101i() {
9277         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 ||
9278                 error "dd 10M file failed"
9279
9280         local max_per_file_mb=$($LCTL get_param -n \
9281                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
9282         cancel_lru_locks osc
9283         stack_trap "$LCTL set_param llite.*.max_read_ahead_per_file_mb=$max_per_file_mb"
9284         $LCTL set_param llite.*.max_read_ahead_per_file_mb=1 ||
9285                 error "set max_read_ahead_per_file_mb to 1 failed"
9286
9287         echo "Reset readahead stats"
9288         $LCTL set_param llite.*.read_ahead_stats=0
9289
9290         dd if=$DIR/$tfile of=/dev/null bs=2M
9291
9292         $LCTL get_param llite.*.read_ahead_stats
9293         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9294                      awk '/misses/ { print $2 }')
9295         [ $miss -eq 5 ] || error "expected misses 5 but got $miss"
9296         rm -f $DIR/$tfile
9297 }
9298 run_test 101i "allow current readahead to exceed reservation"
9299
9300 setup_test102() {
9301         test_mkdir $DIR/$tdir
9302         chown $RUNAS_ID $DIR/$tdir
9303         STRIPE_SIZE=65536
9304         STRIPE_OFFSET=1
9305         STRIPE_COUNT=$OSTCOUNT
9306         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
9307
9308         trap cleanup_test102 EXIT
9309         cd $DIR
9310         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
9311         cd $DIR/$tdir
9312         for num in 1 2 3 4; do
9313                 for count in $(seq 1 $STRIPE_COUNT); do
9314                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
9315                                 local size=`expr $STRIPE_SIZE \* $num`
9316                                 local file=file"$num-$idx-$count"
9317                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
9318                         done
9319                 done
9320         done
9321
9322         cd $DIR
9323         $1 tar cf $TMP/f102.tar $tdir --xattrs
9324 }
9325
9326 cleanup_test102() {
9327         trap 0
9328         rm -f $TMP/f102.tar
9329         rm -rf $DIR/d0.sanity/d102
9330 }
9331
9332 test_102a() {
9333         [ "$UID" != 0 ] && skip "must run as root"
9334         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
9335                 skip_env "must have user_xattr"
9336
9337         [ -z "$(which setfattr 2>/dev/null)" ] &&
9338                 skip_env "could not find setfattr"
9339
9340         local testfile=$DIR/$tfile
9341
9342         touch $testfile
9343         echo "set/get xattr..."
9344         setfattr -n trusted.name1 -v value1 $testfile ||
9345                 error "setfattr -n trusted.name1=value1 $testfile failed"
9346         getfattr -n trusted.name1 $testfile 2> /dev/null |
9347           grep "trusted.name1=.value1" ||
9348                 error "$testfile missing trusted.name1=value1"
9349
9350         setfattr -n user.author1 -v author1 $testfile ||
9351                 error "setfattr -n user.author1=author1 $testfile failed"
9352         getfattr -n user.author1 $testfile 2> /dev/null |
9353           grep "user.author1=.author1" ||
9354                 error "$testfile missing trusted.author1=author1"
9355
9356         echo "listxattr..."
9357         setfattr -n trusted.name2 -v value2 $testfile ||
9358                 error "$testfile unable to set trusted.name2"
9359         setfattr -n trusted.name3 -v value3 $testfile ||
9360                 error "$testfile unable to set trusted.name3"
9361         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
9362             grep "trusted.name" | wc -l) -eq 3 ] ||
9363                 error "$testfile missing 3 trusted.name xattrs"
9364
9365         setfattr -n user.author2 -v author2 $testfile ||
9366                 error "$testfile unable to set user.author2"
9367         setfattr -n user.author3 -v author3 $testfile ||
9368                 error "$testfile unable to set user.author3"
9369         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
9370             grep "user.author" | wc -l) -eq 3 ] ||
9371                 error "$testfile missing 3 user.author xattrs"
9372
9373         echo "remove xattr..."
9374         setfattr -x trusted.name1 $testfile ||
9375                 error "$testfile error deleting trusted.name1"
9376         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
9377                 error "$testfile did not delete trusted.name1 xattr"
9378
9379         setfattr -x user.author1 $testfile ||
9380                 error "$testfile error deleting user.author1"
9381         echo "set lustre special xattr ..."
9382         $LFS setstripe -c1 $testfile
9383         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
9384                 awk -F "=" '/trusted.lov/ { print $2 }' )
9385         setfattr -n "trusted.lov" -v $lovea $testfile ||
9386                 error "$testfile doesn't ignore setting trusted.lov again"
9387         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
9388                 error "$testfile allow setting invalid trusted.lov"
9389         rm -f $testfile
9390 }
9391 run_test 102a "user xattr test =================================="
9392
9393 test_102b() {
9394         [ -z "$(which setfattr 2>/dev/null)" ] &&
9395                 skip_env "could not find setfattr"
9396         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9397
9398         # b10930: get/set/list trusted.lov xattr
9399         echo "get/set/list trusted.lov xattr ..."
9400         local testfile=$DIR/$tfile
9401         $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
9402                 error "setstripe failed"
9403         local STRIPECOUNT=$($LFS getstripe -c $testfile) ||
9404                 error "getstripe failed"
9405         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
9406                 error "can't get trusted.lov from $testfile"
9407
9408         local testfile2=${testfile}2
9409         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
9410                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
9411
9412         $MCREATE $testfile2
9413         setfattr -n trusted.lov -v $value $testfile2
9414         local stripe_size=$($LFS getstripe -S $testfile2)
9415         local stripe_count=$($LFS getstripe -c $testfile2)
9416         [[ $stripe_size -eq 65536 ]] ||
9417                 error "stripe size $stripe_size != 65536"
9418         [[ $stripe_count -eq $STRIPECOUNT ]] ||
9419                 error "stripe count $stripe_count != $STRIPECOUNT"
9420         rm -f $DIR/$tfile
9421 }
9422 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
9423
9424 test_102c() {
9425         [ -z "$(which setfattr 2>/dev/null)" ] &&
9426                 skip_env "could not find setfattr"
9427         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9428
9429         # b10930: get/set/list lustre.lov xattr
9430         echo "get/set/list lustre.lov xattr ..."
9431         test_mkdir $DIR/$tdir
9432         chown $RUNAS_ID $DIR/$tdir
9433         local testfile=$DIR/$tdir/$tfile
9434         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
9435                 error "setstripe failed"
9436         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
9437                 error "getstripe failed"
9438         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
9439         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
9440
9441         local testfile2=${testfile}2
9442         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
9443                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
9444
9445         $RUNAS $MCREATE $testfile2
9446         $RUNAS setfattr -n lustre.lov -v $value $testfile2
9447         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
9448         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
9449         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
9450         [ $stripe_count -eq $STRIPECOUNT ] ||
9451                 error "stripe count $stripe_count != $STRIPECOUNT"
9452 }
9453 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
9454
9455 compare_stripe_info1() {
9456         local stripe_index_all_zero=true
9457
9458         for num in 1 2 3 4; do
9459                 for count in $(seq 1 $STRIPE_COUNT); do
9460                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
9461                                 local size=$((STRIPE_SIZE * num))
9462                                 local file=file"$num-$offset-$count"
9463                                 stripe_size=$($LFS getstripe -S $PWD/$file)
9464                                 [[ $stripe_size -ne $size ]] &&
9465                                     error "$file: size $stripe_size != $size"
9466                                 stripe_count=$($LFS getstripe -c $PWD/$file)
9467                                 # allow fewer stripes to be created, ORI-601
9468                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
9469                                     error "$file: count $stripe_count != $count"
9470                                 stripe_index=$($LFS getstripe -i $PWD/$file)
9471                                 [[ $stripe_index -ne 0 ]] &&
9472                                         stripe_index_all_zero=false
9473                         done
9474                 done
9475         done
9476         $stripe_index_all_zero &&
9477                 error "all files are being extracted starting from OST index 0"
9478         return 0
9479 }
9480
9481 have_xattrs_include() {
9482         tar --help | grep -q xattrs-include &&
9483                 echo --xattrs-include="lustre.*"
9484 }
9485
9486 test_102d() {
9487         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9488         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9489
9490         XINC=$(have_xattrs_include)
9491         setup_test102
9492         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9493         cd $DIR/$tdir/$tdir
9494         compare_stripe_info1
9495 }
9496 run_test 102d "tar restore stripe info from tarfile,not keep osts"
9497
9498 test_102f() {
9499         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9500         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9501
9502         XINC=$(have_xattrs_include)
9503         setup_test102
9504         test_mkdir $DIR/$tdir.restore
9505         cd $DIR
9506         tar cf - --xattrs $tdir | tar xf - \
9507                 -C $DIR/$tdir.restore --xattrs $XINC
9508         cd $DIR/$tdir.restore/$tdir
9509         compare_stripe_info1
9510 }
9511 run_test 102f "tar copy files, not keep osts"
9512
9513 grow_xattr() {
9514         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
9515                 skip "must have user_xattr"
9516         [ -z "$(which setfattr 2>/dev/null)" ] &&
9517                 skip_env "could not find setfattr"
9518         [ -z "$(which getfattr 2>/dev/null)" ] &&
9519                 skip_env "could not find getfattr"
9520
9521         local xsize=${1:-1024}  # in bytes
9522         local file=$DIR/$tfile
9523         local value="$(generate_string $xsize)"
9524         local xbig=trusted.big
9525         local toobig=$2
9526
9527         touch $file
9528         log "save $xbig on $file"
9529         if [ -z "$toobig" ]
9530         then
9531                 setfattr -n $xbig -v $value $file ||
9532                         error "saving $xbig on $file failed"
9533         else
9534                 setfattr -n $xbig -v $value $file &&
9535                         error "saving $xbig on $file succeeded"
9536                 return 0
9537         fi
9538
9539         local orig=$(get_xattr_value $xbig $file)
9540         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
9541
9542         local xsml=trusted.sml
9543         log "save $xsml on $file"
9544         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
9545
9546         local new=$(get_xattr_value $xbig $file)
9547         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
9548
9549         log "grow $xsml on $file"
9550         setfattr -n $xsml -v "$value" $file ||
9551                 error "growing $xsml on $file failed"
9552
9553         new=$(get_xattr_value $xbig $file)
9554         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
9555         log "$xbig still valid after growing $xsml"
9556
9557         rm -f $file
9558 }
9559
9560 test_102h() { # bug 15777
9561         grow_xattr 1024
9562 }
9563 run_test 102h "grow xattr from inside inode to external block"
9564
9565 test_102ha() {
9566         large_xattr_enabled || skip_env "ea_inode feature disabled"
9567
9568         echo "setting xattr of max xattr size: $(max_xattr_size)"
9569         grow_xattr $(max_xattr_size)
9570
9571         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
9572         echo "This should fail:"
9573         grow_xattr $(($(max_xattr_size) + 10)) 1
9574 }
9575 run_test 102ha "grow xattr from inside inode to external inode"
9576
9577 test_102i() { # bug 17038
9578         [ -z "$(which getfattr 2>/dev/null)" ] &&
9579                 skip "could not find getfattr"
9580
9581         touch $DIR/$tfile
9582         ln -s $DIR/$tfile $DIR/${tfile}link
9583         getfattr -n trusted.lov $DIR/$tfile ||
9584                 error "lgetxattr on $DIR/$tfile failed"
9585         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
9586                 grep -i "no such attr" ||
9587                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
9588         rm -f $DIR/$tfile $DIR/${tfile}link
9589 }
9590 run_test 102i "lgetxattr test on symbolic link ============"
9591
9592 test_102j() {
9593         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9594         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9595
9596         XINC=$(have_xattrs_include)
9597         setup_test102 "$RUNAS"
9598         chown $RUNAS_ID $DIR/$tdir
9599         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9600         cd $DIR/$tdir/$tdir
9601         compare_stripe_info1 "$RUNAS"
9602 }
9603 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
9604
9605 test_102k() {
9606         [ -z "$(which setfattr 2>/dev/null)" ] &&
9607                 skip "could not find setfattr"
9608
9609         touch $DIR/$tfile
9610         # b22187 just check that does not crash for regular file.
9611         setfattr -n trusted.lov $DIR/$tfile
9612         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
9613         local test_kdir=$DIR/$tdir
9614         test_mkdir $test_kdir
9615         local default_size=$($LFS getstripe -S $test_kdir)
9616         local default_count=$($LFS getstripe -c $test_kdir)
9617         local default_offset=$($LFS getstripe -i $test_kdir)
9618         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
9619                 error 'dir setstripe failed'
9620         setfattr -n trusted.lov $test_kdir
9621         local stripe_size=$($LFS getstripe -S $test_kdir)
9622         local stripe_count=$($LFS getstripe -c $test_kdir)
9623         local stripe_offset=$($LFS getstripe -i $test_kdir)
9624         [ $stripe_size -eq $default_size ] ||
9625                 error "stripe size $stripe_size != $default_size"
9626         [ $stripe_count -eq $default_count ] ||
9627                 error "stripe count $stripe_count != $default_count"
9628         [ $stripe_offset -eq $default_offset ] ||
9629                 error "stripe offset $stripe_offset != $default_offset"
9630         rm -rf $DIR/$tfile $test_kdir
9631 }
9632 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
9633
9634 test_102l() {
9635         [ -z "$(which getfattr 2>/dev/null)" ] &&
9636                 skip "could not find getfattr"
9637
9638         # LU-532 trusted. xattr is invisible to non-root
9639         local testfile=$DIR/$tfile
9640
9641         touch $testfile
9642
9643         echo "listxattr as user..."
9644         chown $RUNAS_ID $testfile
9645         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
9646             grep -q "trusted" &&
9647                 error "$testfile trusted xattrs are user visible"
9648
9649         return 0;
9650 }
9651 run_test 102l "listxattr size test =================================="
9652
9653 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
9654         local path=$DIR/$tfile
9655         touch $path
9656
9657         listxattr_size_check $path || error "listattr_size_check $path failed"
9658 }
9659 run_test 102m "Ensure listxattr fails on small bufffer ========"
9660
9661 cleanup_test102
9662
9663 getxattr() { # getxattr path name
9664         # Return the base64 encoding of the value of xattr name on path.
9665         local path=$1
9666         local name=$2
9667
9668         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
9669         # file: $path
9670         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9671         #
9672         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9673
9674         getfattr --absolute-names --encoding=base64 --name=$name $path |
9675                 awk -F= -v name=$name '$1 == name {
9676                         print substr($0, index($0, "=") + 1);
9677         }'
9678 }
9679
9680 test_102n() { # LU-4101 mdt: protect internal xattrs
9681         [ -z "$(which setfattr 2>/dev/null)" ] &&
9682                 skip "could not find setfattr"
9683         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
9684         then
9685                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
9686         fi
9687
9688         local file0=$DIR/$tfile.0
9689         local file1=$DIR/$tfile.1
9690         local xattr0=$TMP/$tfile.0
9691         local xattr1=$TMP/$tfile.1
9692         local namelist="lov lma lmv link fid version som hsm"
9693         local name
9694         local value
9695
9696         rm -rf $file0 $file1 $xattr0 $xattr1
9697         touch $file0 $file1
9698
9699         # Get 'before' xattrs of $file1.
9700         getfattr --absolute-names --dump --match=- $file1 > $xattr0
9701
9702         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
9703                 namelist+=" lfsck_namespace"
9704         for name in $namelist; do
9705                 # Try to copy xattr from $file0 to $file1.
9706                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9707
9708                 setfattr --name=trusted.$name --value="$value" $file1 ||
9709                         error "setxattr 'trusted.$name' failed"
9710
9711                 # Try to set a garbage xattr.
9712                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9713
9714                 if [[ x$name == "xlov" ]]; then
9715                         setfattr --name=trusted.lov --value="$value" $file1 &&
9716                         error "setxattr invalid 'trusted.lov' success"
9717                 else
9718                         setfattr --name=trusted.$name --value="$value" $file1 ||
9719                                 error "setxattr invalid 'trusted.$name' failed"
9720                 fi
9721
9722                 # Try to remove the xattr from $file1. We don't care if this
9723                 # appears to succeed or fail, we just don't want there to be
9724                 # any changes or crashes.
9725                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9726         done
9727
9728         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
9729         then
9730                 name="lfsck_ns"
9731                 # Try to copy xattr from $file0 to $file1.
9732                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9733
9734                 setfattr --name=trusted.$name --value="$value" $file1 ||
9735                         error "setxattr 'trusted.$name' failed"
9736
9737                 # Try to set a garbage xattr.
9738                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9739
9740                 setfattr --name=trusted.$name --value="$value" $file1 ||
9741                         error "setxattr 'trusted.$name' failed"
9742
9743                 # Try to remove the xattr from $file1. We don't care if this
9744                 # appears to succeed or fail, we just don't want there to be
9745                 # any changes or crashes.
9746                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9747         fi
9748
9749         # Get 'after' xattrs of file1.
9750         getfattr --absolute-names --dump --match=- $file1 > $xattr1
9751
9752         if ! diff $xattr0 $xattr1; then
9753                 error "before and after xattrs of '$file1' differ"
9754         fi
9755
9756         rm -rf $file0 $file1 $xattr0 $xattr1
9757
9758         return 0
9759 }
9760 run_test 102n "silently ignore setxattr on internal trusted xattrs"
9761
9762 test_102p() { # LU-4703 setxattr did not check ownership
9763         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
9764                 skip "MDS needs to be at least 2.5.56"
9765
9766         local testfile=$DIR/$tfile
9767
9768         touch $testfile
9769
9770         echo "setfacl as user..."
9771         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
9772         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
9773
9774         echo "setfattr as user..."
9775         setfacl -m "u:$RUNAS_ID:---" $testfile
9776         $RUNAS setfattr -x system.posix_acl_access $testfile
9777         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
9778 }
9779 run_test 102p "check setxattr(2) correctly fails without permission"
9780
9781 test_102q() {
9782         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
9783                 skip "MDS needs to be at least 2.6.92"
9784
9785         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
9786 }
9787 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
9788
9789 test_102r() {
9790         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
9791                 skip "MDS needs to be at least 2.6.93"
9792
9793         touch $DIR/$tfile || error "touch"
9794         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
9795         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
9796         rm $DIR/$tfile || error "rm"
9797
9798         #normal directory
9799         mkdir -p $DIR/$tdir || error "mkdir"
9800         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9801         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9802         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9803                 error "$testfile error deleting user.author1"
9804         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9805                 grep "user.$(basename $tdir)" &&
9806                 error "$tdir did not delete user.$(basename $tdir)"
9807         rmdir $DIR/$tdir || error "rmdir"
9808
9809         #striped directory
9810         test_mkdir $DIR/$tdir
9811         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9812         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9813         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9814                 error "$testfile error deleting user.author1"
9815         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9816                 grep "user.$(basename $tdir)" &&
9817                 error "$tdir did not delete user.$(basename $tdir)"
9818         rmdir $DIR/$tdir || error "rm striped dir"
9819 }
9820 run_test 102r "set EAs with empty values"
9821
9822 test_102s() {
9823         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9824                 skip "MDS needs to be at least 2.11.52"
9825
9826         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9827
9828         save_lustre_params client "llite.*.xattr_cache" > $save
9829
9830         for cache in 0 1; do
9831                 lctl set_param llite.*.xattr_cache=$cache
9832
9833                 rm -f $DIR/$tfile
9834                 touch $DIR/$tfile || error "touch"
9835                 for prefix in lustre security system trusted user; do
9836                         # Note getxattr() may fail with 'Operation not
9837                         # supported' or 'No such attribute' depending
9838                         # on prefix and cache.
9839                         getfattr -n $prefix.n102s $DIR/$tfile &&
9840                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
9841                 done
9842         done
9843
9844         restore_lustre_params < $save
9845 }
9846 run_test 102s "getting nonexistent xattrs should fail"
9847
9848 test_102t() {
9849         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9850                 skip "MDS needs to be at least 2.11.52"
9851
9852         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9853
9854         save_lustre_params client "llite.*.xattr_cache" > $save
9855
9856         for cache in 0 1; do
9857                 lctl set_param llite.*.xattr_cache=$cache
9858
9859                 for buf_size in 0 256; do
9860                         rm -f $DIR/$tfile
9861                         touch $DIR/$tfile || error "touch"
9862                         setfattr -n user.multiop $DIR/$tfile
9863                         $MULTIOP $DIR/$tfile oa$buf_size ||
9864                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
9865                 done
9866         done
9867
9868         restore_lustre_params < $save
9869 }
9870 run_test 102t "zero length xattr values handled correctly"
9871
9872 run_acl_subtest()
9873 {
9874     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
9875     return $?
9876 }
9877
9878 test_103a() {
9879         [ "$UID" != 0 ] && skip "must run as root"
9880         $GSS && skip_env "could not run under gss"
9881         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
9882                 skip_env "must have acl enabled"
9883         [ -z "$(which setfacl 2>/dev/null)" ] &&
9884                 skip_env "could not find setfacl"
9885         remote_mds_nodsh && skip "remote MDS with nodsh"
9886
9887         gpasswd -a daemon bin                           # LU-5641
9888         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
9889
9890         declare -a identity_old
9891
9892         for num in $(seq $MDSCOUNT); do
9893                 switch_identity $num true || identity_old[$num]=$?
9894         done
9895
9896         SAVE_UMASK=$(umask)
9897         umask 0022
9898         mkdir -p $DIR/$tdir
9899         cd $DIR/$tdir
9900
9901         echo "performing cp ..."
9902         run_acl_subtest cp || error "run_acl_subtest cp failed"
9903         echo "performing getfacl-noacl..."
9904         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
9905         echo "performing misc..."
9906         run_acl_subtest misc || error  "misc test failed"
9907         echo "performing permissions..."
9908         run_acl_subtest permissions || error "permissions failed"
9909         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
9910         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
9911                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
9912                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
9913         then
9914                 echo "performing permissions xattr..."
9915                 run_acl_subtest permissions_xattr ||
9916                         error "permissions_xattr failed"
9917         fi
9918         echo "performing setfacl..."
9919         run_acl_subtest setfacl || error  "setfacl test failed"
9920
9921         # inheritance test got from HP
9922         echo "performing inheritance..."
9923         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
9924         chmod +x make-tree || error "chmod +x failed"
9925         run_acl_subtest inheritance || error "inheritance test failed"
9926         rm -f make-tree
9927
9928         echo "LU-974 ignore umask when acl is enabled..."
9929         run_acl_subtest 974 || error "LU-974 umask test failed"
9930         if [ $MDSCOUNT -ge 2 ]; then
9931                 run_acl_subtest 974_remote ||
9932                         error "LU-974 umask test failed under remote dir"
9933         fi
9934
9935         echo "LU-2561 newly created file is same size as directory..."
9936         if [ "$mds1_FSTYPE" != "zfs" ]; then
9937                 run_acl_subtest 2561 || error "LU-2561 test failed"
9938         else
9939                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
9940         fi
9941
9942         run_acl_subtest 4924 || error "LU-4924 test failed"
9943
9944         cd $SAVE_PWD
9945         umask $SAVE_UMASK
9946
9947         for num in $(seq $MDSCOUNT); do
9948                 if [ "${identity_old[$num]}" = 1 ]; then
9949                         switch_identity $num false || identity_old[$num]=$?
9950                 fi
9951         done
9952 }
9953 run_test 103a "acl test"
9954
9955 test_103b() {
9956         declare -a pids
9957         local U
9958
9959         for U in {0..511}; do
9960                 {
9961                 local O=$(printf "%04o" $U)
9962
9963                 umask $(printf "%04o" $((511 ^ $O)))
9964                 $LFS setstripe -c 1 $DIR/$tfile.s$O
9965                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
9966
9967                 (( $S == ($O & 0666) )) ||
9968                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
9969
9970                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
9971                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
9972                 (( $S == ($O & 0666) )) ||
9973                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
9974
9975                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
9976                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
9977                 (( $S == ($O & 0666) )) ||
9978                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
9979                 rm -f $DIR/$tfile.[smp]$0
9980                 } &
9981                 local pid=$!
9982
9983                 # limit the concurrently running threads to 64. LU-11878
9984                 local idx=$((U % 64))
9985                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
9986                 pids[idx]=$pid
9987         done
9988         wait
9989 }
9990 run_test 103b "umask lfs setstripe"
9991
9992 test_103c() {
9993         mkdir -p $DIR/$tdir
9994         cp -rp $DIR/$tdir $DIR/$tdir.bak
9995
9996         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
9997                 error "$DIR/$tdir shouldn't contain default ACL"
9998         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
9999                 error "$DIR/$tdir.bak shouldn't contain default ACL"
10000         true
10001 }
10002 run_test 103c "'cp -rp' won't set empty acl"
10003
10004 test_104a() {
10005         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10006
10007         touch $DIR/$tfile
10008         lfs df || error "lfs df failed"
10009         lfs df -ih || error "lfs df -ih failed"
10010         lfs df -h $DIR || error "lfs df -h $DIR failed"
10011         lfs df -i $DIR || error "lfs df -i $DIR failed"
10012         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
10013         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
10014
10015         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
10016         lctl --device %$OSC deactivate
10017         lfs df || error "lfs df with deactivated OSC failed"
10018         lctl --device %$OSC activate
10019         # wait the osc back to normal
10020         wait_osc_import_ready client ost
10021
10022         lfs df || error "lfs df with reactivated OSC failed"
10023         rm -f $DIR/$tfile
10024 }
10025 run_test 104a "lfs df [-ih] [path] test ========================="
10026
10027 test_104b() {
10028         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10029         [ $RUNAS_ID -eq $UID ] &&
10030                 skip_env "RUNAS_ID = UID = $UID -- skipping"
10031
10032         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
10033                         grep "Permission denied" | wc -l)))
10034         if [ $denied_cnt -ne 0 ]; then
10035                 error "lfs check servers test failed"
10036         fi
10037 }
10038 run_test 104b "$RUNAS lfs check servers test ===================="
10039
10040 test_105a() {
10041         # doesn't work on 2.4 kernels
10042         touch $DIR/$tfile
10043         if $(flock_is_enabled); then
10044                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
10045         else
10046                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
10047         fi
10048         rm -f $DIR/$tfile
10049 }
10050 run_test 105a "flock when mounted without -o flock test ========"
10051
10052 test_105b() {
10053         touch $DIR/$tfile
10054         if $(flock_is_enabled); then
10055                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
10056         else
10057                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
10058         fi
10059         rm -f $DIR/$tfile
10060 }
10061 run_test 105b "fcntl when mounted without -o flock test ========"
10062
10063 test_105c() {
10064         touch $DIR/$tfile
10065         if $(flock_is_enabled); then
10066                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
10067         else
10068                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
10069         fi
10070         rm -f $DIR/$tfile
10071 }
10072 run_test 105c "lockf when mounted without -o flock test"
10073
10074 test_105d() { # bug 15924
10075         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10076
10077         test_mkdir $DIR/$tdir
10078         flock_is_enabled || skip_env "mount w/o flock enabled"
10079         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
10080         $LCTL set_param fail_loc=0x80000315
10081         flocks_test 2 $DIR/$tdir
10082 }
10083 run_test 105d "flock race (should not freeze) ========"
10084
10085 test_105e() { # bug 22660 && 22040
10086         flock_is_enabled || skip_env "mount w/o flock enabled"
10087
10088         touch $DIR/$tfile
10089         flocks_test 3 $DIR/$tfile
10090 }
10091 run_test 105e "Two conflicting flocks from same process"
10092
10093 test_106() { #bug 10921
10094         test_mkdir $DIR/$tdir
10095         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
10096         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
10097 }
10098 run_test 106 "attempt exec of dir followed by chown of that dir"
10099
10100 test_107() {
10101         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10102
10103         CDIR=`pwd`
10104         local file=core
10105
10106         cd $DIR
10107         rm -f $file
10108
10109         local save_pattern=$(sysctl -n kernel.core_pattern)
10110         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
10111         sysctl -w kernel.core_pattern=$file
10112         sysctl -w kernel.core_uses_pid=0
10113
10114         ulimit -c unlimited
10115         sleep 60 &
10116         SLEEPPID=$!
10117
10118         sleep 1
10119
10120         kill -s 11 $SLEEPPID
10121         wait $SLEEPPID
10122         if [ -e $file ]; then
10123                 size=`stat -c%s $file`
10124                 [ $size -eq 0 ] && error "Fail to create core file $file"
10125         else
10126                 error "Fail to create core file $file"
10127         fi
10128         rm -f $file
10129         sysctl -w kernel.core_pattern=$save_pattern
10130         sysctl -w kernel.core_uses_pid=$save_uses_pid
10131         cd $CDIR
10132 }
10133 run_test 107 "Coredump on SIG"
10134
10135 test_110() {
10136         test_mkdir $DIR/$tdir
10137         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
10138         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
10139                 error "mkdir with 256 char should fail, but did not"
10140         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
10141                 error "create with 255 char failed"
10142         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
10143                 error "create with 256 char should fail, but did not"
10144
10145         ls -l $DIR/$tdir
10146         rm -rf $DIR/$tdir
10147 }
10148 run_test 110 "filename length checking"
10149
10150 #
10151 # Purpose: To verify dynamic thread (OSS) creation.
10152 #
10153 test_115() {
10154         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10155         remote_ost_nodsh && skip "remote OST with nodsh"
10156
10157         # Lustre does not stop service threads once they are started.
10158         # Reset number of running threads to default.
10159         stopall
10160         setupall
10161
10162         local OSTIO_pre
10163         local save_params="$TMP/sanity-$TESTNAME.parameters"
10164
10165         # Get ll_ost_io count before I/O
10166         OSTIO_pre=$(do_facet ost1 \
10167                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
10168         # Exit if lustre is not running (ll_ost_io not running).
10169         [ -z "$OSTIO_pre" ] && error "no OSS threads"
10170
10171         echo "Starting with $OSTIO_pre threads"
10172         local thread_max=$((OSTIO_pre * 2))
10173         local rpc_in_flight=$((thread_max * 2))
10174         # Number of I/O Process proposed to be started.
10175         local nfiles
10176         local facets=$(get_facets OST)
10177
10178         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
10179         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
10180
10181         # Set in_flight to $rpc_in_flight
10182         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
10183                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
10184         nfiles=${rpc_in_flight}
10185         # Set ost thread_max to $thread_max
10186         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
10187
10188         # 5 Minutes should be sufficient for max number of OSS
10189         # threads(thread_max) to be created.
10190         local timeout=300
10191
10192         # Start I/O.
10193         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
10194         test_mkdir $DIR/$tdir
10195         for i in $(seq $nfiles); do
10196                 local file=$DIR/$tdir/${tfile}-$i
10197                 $LFS setstripe -c -1 -i 0 $file
10198                 ($WTL $file $timeout)&
10199         done
10200
10201         # I/O Started - Wait for thread_started to reach thread_max or report
10202         # error if thread_started is more than thread_max.
10203         echo "Waiting for thread_started to reach thread_max"
10204         local thread_started=0
10205         local end_time=$((SECONDS + timeout))
10206
10207         while [ $SECONDS -le $end_time ] ; do
10208                 echo -n "."
10209                 # Get ost i/o thread_started count.
10210                 thread_started=$(do_facet ost1 \
10211                         "$LCTL get_param \
10212                         ost.OSS.ost_io.threads_started | cut -d= -f2")
10213                 # Break out if thread_started is equal/greater than thread_max
10214                 if [[ $thread_started -ge $thread_max ]]; then
10215                         echo ll_ost_io thread_started $thread_started, \
10216                                 equal/greater than thread_max $thread_max
10217                         break
10218                 fi
10219                 sleep 1
10220         done
10221
10222         # Cleanup - We have the numbers, Kill i/o jobs if running.
10223         jobcount=($(jobs -p))
10224         for i in $(seq 0 $((${#jobcount[@]}-1)))
10225         do
10226                 kill -9 ${jobcount[$i]}
10227                 if [ $? -ne 0 ] ; then
10228                         echo Warning: \
10229                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
10230                 fi
10231         done
10232
10233         # Cleanup files left by WTL binary.
10234         for i in $(seq $nfiles); do
10235                 local file=$DIR/$tdir/${tfile}-$i
10236                 rm -rf $file
10237                 if [ $? -ne 0 ] ; then
10238                         echo "Warning: Failed to delete file $file"
10239                 fi
10240         done
10241
10242         restore_lustre_params <$save_params
10243         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
10244
10245         # Error out if no new thread has started or Thread started is greater
10246         # than thread max.
10247         if [[ $thread_started -le $OSTIO_pre ||
10248                         $thread_started -gt $thread_max ]]; then
10249                 error "ll_ost_io: thread_started $thread_started" \
10250                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
10251                       "No new thread started or thread started greater " \
10252                       "than thread_max."
10253         fi
10254 }
10255 run_test 115 "verify dynamic thread creation===================="
10256
10257 free_min_max () {
10258         wait_delete_completed
10259         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
10260         echo "OST kbytes available: ${AVAIL[@]}"
10261         MAXV=${AVAIL[0]}
10262         MAXI=0
10263         MINV=${AVAIL[0]}
10264         MINI=0
10265         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
10266                 #echo OST $i: ${AVAIL[i]}kb
10267                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
10268                         MAXV=${AVAIL[i]}
10269                         MAXI=$i
10270                 fi
10271                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
10272                         MINV=${AVAIL[i]}
10273                         MINI=$i
10274                 fi
10275         done
10276         echo "Min free space: OST $MINI: $MINV"
10277         echo "Max free space: OST $MAXI: $MAXV"
10278 }
10279
10280 test_116a() { # was previously test_116()
10281         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10282         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10283         remote_mds_nodsh && skip "remote MDS with nodsh"
10284
10285         echo -n "Free space priority "
10286         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
10287                 head -n1
10288         declare -a AVAIL
10289         free_min_max
10290
10291         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
10292         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
10293         trap simple_cleanup_common EXIT
10294
10295         # Check if we need to generate uneven OSTs
10296         test_mkdir -p $DIR/$tdir/OST${MINI}
10297         local FILL=$((MINV / 4))
10298         local DIFF=$((MAXV - MINV))
10299         local DIFF2=$((DIFF * 100 / MINV))
10300
10301         local threshold=$(do_facet $SINGLEMDS \
10302                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
10303         threshold=${threshold%%%}
10304         echo -n "Check for uneven OSTs: "
10305         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
10306
10307         if [[ $DIFF2 -gt $threshold ]]; then
10308                 echo "ok"
10309                 echo "Don't need to fill OST$MINI"
10310         else
10311                 # generate uneven OSTs. Write 2% over the QOS threshold value
10312                 echo "no"
10313                 DIFF=$((threshold - DIFF2 + 2))
10314                 DIFF2=$((MINV * DIFF / 100))
10315                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
10316                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
10317                         error "setstripe failed"
10318                 DIFF=$((DIFF2 / 2048))
10319                 i=0
10320                 while [ $i -lt $DIFF ]; do
10321                         i=$((i + 1))
10322                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
10323                                 bs=2M count=1 2>/dev/null
10324                         echo -n .
10325                 done
10326                 echo .
10327                 sync
10328                 sleep_maxage
10329                 free_min_max
10330         fi
10331
10332         DIFF=$((MAXV - MINV))
10333         DIFF2=$((DIFF * 100 / MINV))
10334         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
10335         if [ $DIFF2 -gt $threshold ]; then
10336                 echo "ok"
10337         else
10338                 echo "failed - QOS mode won't be used"
10339                 simple_cleanup_common
10340                 skip "QOS imbalance criteria not met"
10341         fi
10342
10343         MINI1=$MINI
10344         MINV1=$MINV
10345         MAXI1=$MAXI
10346         MAXV1=$MAXV
10347
10348         # now fill using QOS
10349         $LFS setstripe -c 1 $DIR/$tdir
10350         FILL=$((FILL / 200))
10351         if [ $FILL -gt 600 ]; then
10352                 FILL=600
10353         fi
10354         echo "writing $FILL files to QOS-assigned OSTs"
10355         i=0
10356         while [ $i -lt $FILL ]; do
10357                 i=$((i + 1))
10358                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
10359                         count=1 2>/dev/null
10360                 echo -n .
10361         done
10362         echo "wrote $i 200k files"
10363         sync
10364         sleep_maxage
10365
10366         echo "Note: free space may not be updated, so measurements might be off"
10367         free_min_max
10368         DIFF2=$((MAXV - MINV))
10369         echo "free space delta: orig $DIFF final $DIFF2"
10370         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
10371         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
10372         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
10373         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
10374         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
10375         if [[ $DIFF -gt 0 ]]; then
10376                 FILL=$((DIFF2 * 100 / DIFF - 100))
10377                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
10378         fi
10379
10380         # Figure out which files were written where
10381         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10382                awk '/'$MINI1': / {print $2; exit}')
10383         echo $UUID
10384         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10385         echo "$MINC files created on smaller OST $MINI1"
10386         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10387                awk '/'$MAXI1': / {print $2; exit}')
10388         echo $UUID
10389         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10390         echo "$MAXC files created on larger OST $MAXI1"
10391         if [[ $MINC -gt 0 ]]; then
10392                 FILL=$((MAXC * 100 / MINC - 100))
10393                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
10394         fi
10395         [[ $MAXC -gt $MINC ]] ||
10396                 error_ignore LU-9 "stripe QOS didn't balance free space"
10397         simple_cleanup_common
10398 }
10399 run_test 116a "stripe QOS: free space balance ==================="
10400
10401 test_116b() { # LU-2093
10402         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10403         remote_mds_nodsh && skip "remote MDS with nodsh"
10404
10405 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
10406         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
10407                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
10408         [ -z "$old_rr" ] && skip "no QOS"
10409         do_facet $SINGLEMDS lctl set_param \
10410                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
10411         mkdir -p $DIR/$tdir
10412         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
10413         createmany -o $DIR/$tdir/f- 20 || error "can't create"
10414         do_facet $SINGLEMDS lctl set_param fail_loc=0
10415         rm -rf $DIR/$tdir
10416         do_facet $SINGLEMDS lctl set_param \
10417                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
10418 }
10419 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
10420
10421 test_117() # bug 10891
10422 {
10423         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10424
10425         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
10426         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
10427         lctl set_param fail_loc=0x21e
10428         > $DIR/$tfile || error "truncate failed"
10429         lctl set_param fail_loc=0
10430         echo "Truncate succeeded."
10431         rm -f $DIR/$tfile
10432 }
10433 run_test 117 "verify osd extend =========="
10434
10435 NO_SLOW_RESENDCOUNT=4
10436 export OLD_RESENDCOUNT=""
10437 set_resend_count () {
10438         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
10439         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
10440         lctl set_param -n $PROC_RESENDCOUNT $1
10441         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
10442 }
10443
10444 # for reduce test_118* time (b=14842)
10445 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10446
10447 # Reset async IO behavior after error case
10448 reset_async() {
10449         FILE=$DIR/reset_async
10450
10451         # Ensure all OSCs are cleared
10452         $LFS setstripe -c -1 $FILE
10453         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
10454         sync
10455         rm $FILE
10456 }
10457
10458 test_118a() #bug 11710
10459 {
10460         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10461
10462         reset_async
10463
10464         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10465         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10466         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10467
10468         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10469                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10470                 return 1;
10471         fi
10472         rm -f $DIR/$tfile
10473 }
10474 run_test 118a "verify O_SYNC works =========="
10475
10476 test_118b()
10477 {
10478         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10479         remote_ost_nodsh && skip "remote OST with nodsh"
10480
10481         reset_async
10482
10483         #define OBD_FAIL_SRV_ENOENT 0x217
10484         set_nodes_failloc "$(osts_nodes)" 0x217
10485         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10486         RC=$?
10487         set_nodes_failloc "$(osts_nodes)" 0
10488         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10489         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10490                     grep -c writeback)
10491
10492         if [[ $RC -eq 0 ]]; then
10493                 error "Must return error due to dropped pages, rc=$RC"
10494                 return 1;
10495         fi
10496
10497         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10498                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10499                 return 1;
10500         fi
10501
10502         echo "Dirty pages not leaked on ENOENT"
10503
10504         # Due to the above error the OSC will issue all RPCs syncronously
10505         # until a subsequent RPC completes successfully without error.
10506         $MULTIOP $DIR/$tfile Ow4096yc
10507         rm -f $DIR/$tfile
10508
10509         return 0
10510 }
10511 run_test 118b "Reclaim dirty pages on fatal error =========="
10512
10513 test_118c()
10514 {
10515         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10516
10517         # for 118c, restore the original resend count, LU-1940
10518         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
10519                                 set_resend_count $OLD_RESENDCOUNT
10520         remote_ost_nodsh && skip "remote OST with nodsh"
10521
10522         reset_async
10523
10524         #define OBD_FAIL_OST_EROFS               0x216
10525         set_nodes_failloc "$(osts_nodes)" 0x216
10526
10527         # multiop should block due to fsync until pages are written
10528         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10529         MULTIPID=$!
10530         sleep 1
10531
10532         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10533                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10534         fi
10535
10536         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10537                     grep -c writeback)
10538         if [[ $WRITEBACK -eq 0 ]]; then
10539                 error "No page in writeback, writeback=$WRITEBACK"
10540         fi
10541
10542         set_nodes_failloc "$(osts_nodes)" 0
10543         wait $MULTIPID
10544         RC=$?
10545         if [[ $RC -ne 0 ]]; then
10546                 error "Multiop fsync failed, rc=$RC"
10547         fi
10548
10549         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10550         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10551                     grep -c writeback)
10552         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10553                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10554         fi
10555
10556         rm -f $DIR/$tfile
10557         echo "Dirty pages flushed via fsync on EROFS"
10558         return 0
10559 }
10560 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
10561
10562 # continue to use small resend count to reduce test_118* time (b=14842)
10563 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10564
10565 test_118d()
10566 {
10567         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10568         remote_ost_nodsh && skip "remote OST with nodsh"
10569
10570         reset_async
10571
10572         #define OBD_FAIL_OST_BRW_PAUSE_BULK
10573         set_nodes_failloc "$(osts_nodes)" 0x214
10574         # multiop should block due to fsync until pages are written
10575         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10576         MULTIPID=$!
10577         sleep 1
10578
10579         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10580                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10581         fi
10582
10583         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10584                     grep -c writeback)
10585         if [[ $WRITEBACK -eq 0 ]]; then
10586                 error "No page in writeback, writeback=$WRITEBACK"
10587         fi
10588
10589         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
10590         set_nodes_failloc "$(osts_nodes)" 0
10591
10592         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10593         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10594                     grep -c writeback)
10595         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10596                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10597         fi
10598
10599         rm -f $DIR/$tfile
10600         echo "Dirty pages gaurenteed flushed via fsync"
10601         return 0
10602 }
10603 run_test 118d "Fsync validation inject a delay of the bulk =========="
10604
10605 test_118f() {
10606         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10607
10608         reset_async
10609
10610         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
10611         lctl set_param fail_loc=0x8000040a
10612
10613         # Should simulate EINVAL error which is fatal
10614         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10615         RC=$?
10616         if [[ $RC -eq 0 ]]; then
10617                 error "Must return error due to dropped pages, rc=$RC"
10618         fi
10619
10620         lctl set_param fail_loc=0x0
10621
10622         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10623         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10624         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10625                     grep -c writeback)
10626         if [[ $LOCKED -ne 0 ]]; then
10627                 error "Locked pages remain in cache, locked=$LOCKED"
10628         fi
10629
10630         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10631                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10632         fi
10633
10634         rm -f $DIR/$tfile
10635         echo "No pages locked after fsync"
10636
10637         reset_async
10638         return 0
10639 }
10640 run_test 118f "Simulate unrecoverable OSC side error =========="
10641
10642 test_118g() {
10643         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10644
10645         reset_async
10646
10647         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
10648         lctl set_param fail_loc=0x406
10649
10650         # simulate local -ENOMEM
10651         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10652         RC=$?
10653
10654         lctl set_param fail_loc=0
10655         if [[ $RC -eq 0 ]]; then
10656                 error "Must return error due to dropped pages, rc=$RC"
10657         fi
10658
10659         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10660         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10661         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10662                         grep -c writeback)
10663         if [[ $LOCKED -ne 0 ]]; then
10664                 error "Locked pages remain in cache, locked=$LOCKED"
10665         fi
10666
10667         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10668                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10669         fi
10670
10671         rm -f $DIR/$tfile
10672         echo "No pages locked after fsync"
10673
10674         reset_async
10675         return 0
10676 }
10677 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
10678
10679 test_118h() {
10680         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10681         remote_ost_nodsh && skip "remote OST with nodsh"
10682
10683         reset_async
10684
10685         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10686         set_nodes_failloc "$(osts_nodes)" 0x20e
10687         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10688         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10689         RC=$?
10690
10691         set_nodes_failloc "$(osts_nodes)" 0
10692         if [[ $RC -eq 0 ]]; then
10693                 error "Must return error due to dropped pages, rc=$RC"
10694         fi
10695
10696         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10697         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10698         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10699                     grep -c writeback)
10700         if [[ $LOCKED -ne 0 ]]; then
10701                 error "Locked pages remain in cache, locked=$LOCKED"
10702         fi
10703
10704         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10705                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10706         fi
10707
10708         rm -f $DIR/$tfile
10709         echo "No pages locked after fsync"
10710
10711         return 0
10712 }
10713 run_test 118h "Verify timeout in handling recoverables errors  =========="
10714
10715 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10716
10717 test_118i() {
10718         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10719         remote_ost_nodsh && skip "remote OST with nodsh"
10720
10721         reset_async
10722
10723         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10724         set_nodes_failloc "$(osts_nodes)" 0x20e
10725
10726         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10727         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10728         PID=$!
10729         sleep 5
10730         set_nodes_failloc "$(osts_nodes)" 0
10731
10732         wait $PID
10733         RC=$?
10734         if [[ $RC -ne 0 ]]; then
10735                 error "got error, but should be not, rc=$RC"
10736         fi
10737
10738         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10739         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10740         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10741         if [[ $LOCKED -ne 0 ]]; then
10742                 error "Locked pages remain in cache, locked=$LOCKED"
10743         fi
10744
10745         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10746                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10747         fi
10748
10749         rm -f $DIR/$tfile
10750         echo "No pages locked after fsync"
10751
10752         return 0
10753 }
10754 run_test 118i "Fix error before timeout in recoverable error  =========="
10755
10756 [ "$SLOW" = "no" ] && set_resend_count 4
10757
10758 test_118j() {
10759         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10760         remote_ost_nodsh && skip "remote OST with nodsh"
10761
10762         reset_async
10763
10764         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
10765         set_nodes_failloc "$(osts_nodes)" 0x220
10766
10767         # return -EIO from OST
10768         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10769         RC=$?
10770         set_nodes_failloc "$(osts_nodes)" 0x0
10771         if [[ $RC -eq 0 ]]; then
10772                 error "Must return error due to dropped pages, rc=$RC"
10773         fi
10774
10775         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10776         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10777         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10778         if [[ $LOCKED -ne 0 ]]; then
10779                 error "Locked pages remain in cache, locked=$LOCKED"
10780         fi
10781
10782         # in recoverable error on OST we want resend and stay until it finished
10783         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10784                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10785         fi
10786
10787         rm -f $DIR/$tfile
10788         echo "No pages locked after fsync"
10789
10790         return 0
10791 }
10792 run_test 118j "Simulate unrecoverable OST side error =========="
10793
10794 test_118k()
10795 {
10796         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10797         remote_ost_nodsh && skip "remote OSTs with nodsh"
10798
10799         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10800         set_nodes_failloc "$(osts_nodes)" 0x20e
10801         test_mkdir $DIR/$tdir
10802
10803         for ((i=0;i<10;i++)); do
10804                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
10805                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
10806                 SLEEPPID=$!
10807                 sleep 0.500s
10808                 kill $SLEEPPID
10809                 wait $SLEEPPID
10810         done
10811
10812         set_nodes_failloc "$(osts_nodes)" 0
10813         rm -rf $DIR/$tdir
10814 }
10815 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
10816
10817 test_118l() # LU-646
10818 {
10819         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10820
10821         test_mkdir $DIR/$tdir
10822         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
10823         rm -rf $DIR/$tdir
10824 }
10825 run_test 118l "fsync dir"
10826
10827 test_118m() # LU-3066
10828 {
10829         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10830
10831         test_mkdir $DIR/$tdir
10832         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
10833         rm -rf $DIR/$tdir
10834 }
10835 run_test 118m "fdatasync dir ========="
10836
10837 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10838
10839 test_118n()
10840 {
10841         local begin
10842         local end
10843
10844         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10845         remote_ost_nodsh && skip "remote OSTs with nodsh"
10846
10847         # Sleep to avoid a cached response.
10848         #define OBD_STATFS_CACHE_SECONDS 1
10849         sleep 2
10850
10851         # Inject a 10 second delay in the OST_STATFS handler.
10852         #define OBD_FAIL_OST_STATFS_DELAY 0x242
10853         set_nodes_failloc "$(osts_nodes)" 0x242
10854
10855         begin=$SECONDS
10856         stat --file-system $MOUNT > /dev/null
10857         end=$SECONDS
10858
10859         set_nodes_failloc "$(osts_nodes)" 0
10860
10861         if ((end - begin > 20)); then
10862             error "statfs took $((end - begin)) seconds, expected 10"
10863         fi
10864 }
10865 run_test 118n "statfs() sends OST_STATFS requests in parallel"
10866
10867 test_119a() # bug 11737
10868 {
10869         BSIZE=$((512 * 1024))
10870         directio write $DIR/$tfile 0 1 $BSIZE
10871         # We ask to read two blocks, which is more than a file size.
10872         # directio will indicate an error when requested and actual
10873         # sizes aren't equeal (a normal situation in this case) and
10874         # print actual read amount.
10875         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
10876         if [ "$NOB" != "$BSIZE" ]; then
10877                 error "read $NOB bytes instead of $BSIZE"
10878         fi
10879         rm -f $DIR/$tfile
10880 }
10881 run_test 119a "Short directIO read must return actual read amount"
10882
10883 test_119b() # bug 11737
10884 {
10885         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10886
10887         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
10888         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
10889         sync
10890         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
10891                 error "direct read failed"
10892         rm -f $DIR/$tfile
10893 }
10894 run_test 119b "Sparse directIO read must return actual read amount"
10895
10896 test_119c() # bug 13099
10897 {
10898         BSIZE=1048576
10899         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
10900         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
10901         rm -f $DIR/$tfile
10902 }
10903 run_test 119c "Testing for direct read hitting hole"
10904
10905 test_119d() # bug 15950
10906 {
10907         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10908
10909         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
10910         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
10911         BSIZE=1048576
10912         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
10913         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
10914         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
10915         lctl set_param fail_loc=0x40d
10916         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
10917         pid_dio=$!
10918         sleep 1
10919         cat $DIR/$tfile > /dev/null &
10920         lctl set_param fail_loc=0
10921         pid_reads=$!
10922         wait $pid_dio
10923         log "the DIO writes have completed, now wait for the reads (should not block very long)"
10924         sleep 2
10925         [ -n "`ps h -p $pid_reads -o comm`" ] && \
10926         error "the read rpcs have not completed in 2s"
10927         rm -f $DIR/$tfile
10928         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
10929 }
10930 run_test 119d "The DIO path should try to send a new rpc once one is completed"
10931
10932 test_120a() {
10933         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10934         remote_mds_nodsh && skip "remote MDS with nodsh"
10935         test_mkdir -i0 -c1 $DIR/$tdir
10936         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10937                 skip_env "no early lock cancel on server"
10938
10939         lru_resize_disable mdc
10940         lru_resize_disable osc
10941         cancel_lru_locks mdc
10942         # asynchronous object destroy at MDT could cause bl ast to client
10943         cancel_lru_locks osc
10944
10945         stat $DIR/$tdir > /dev/null
10946         can1=$(do_facet mds1 \
10947                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10948                awk '/ldlm_cancel/ {print $2}')
10949         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10950                awk '/ldlm_bl_callback/ {print $2}')
10951         test_mkdir -i0 -c1 $DIR/$tdir/d1
10952         can2=$(do_facet mds1 \
10953                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10954                awk '/ldlm_cancel/ {print $2}')
10955         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10956                awk '/ldlm_bl_callback/ {print $2}')
10957         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10958         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10959         lru_resize_enable mdc
10960         lru_resize_enable osc
10961 }
10962 run_test 120a "Early Lock Cancel: mkdir test"
10963
10964 test_120b() {
10965         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10966         remote_mds_nodsh && skip "remote MDS with nodsh"
10967         test_mkdir $DIR/$tdir
10968         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10969                 skip_env "no early lock cancel on server"
10970
10971         lru_resize_disable mdc
10972         lru_resize_disable osc
10973         cancel_lru_locks mdc
10974         stat $DIR/$tdir > /dev/null
10975         can1=$(do_facet $SINGLEMDS \
10976                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10977                awk '/ldlm_cancel/ {print $2}')
10978         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10979                awk '/ldlm_bl_callback/ {print $2}')
10980         touch $DIR/$tdir/f1
10981         can2=$(do_facet $SINGLEMDS \
10982                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10983                awk '/ldlm_cancel/ {print $2}')
10984         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10985                awk '/ldlm_bl_callback/ {print $2}')
10986         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10987         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10988         lru_resize_enable mdc
10989         lru_resize_enable osc
10990 }
10991 run_test 120b "Early Lock Cancel: create test"
10992
10993 test_120c() {
10994         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10995         remote_mds_nodsh && skip "remote MDS with nodsh"
10996         test_mkdir -i0 -c1 $DIR/$tdir
10997         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10998                 skip "no early lock cancel on server"
10999
11000         lru_resize_disable mdc
11001         lru_resize_disable osc
11002         test_mkdir -i0 -c1 $DIR/$tdir/d1
11003         test_mkdir -i0 -c1 $DIR/$tdir/d2
11004         touch $DIR/$tdir/d1/f1
11005         cancel_lru_locks mdc
11006         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
11007         can1=$(do_facet mds1 \
11008                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11009                awk '/ldlm_cancel/ {print $2}')
11010         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11011                awk '/ldlm_bl_callback/ {print $2}')
11012         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
11013         can2=$(do_facet mds1 \
11014                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11015                awk '/ldlm_cancel/ {print $2}')
11016         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11017                awk '/ldlm_bl_callback/ {print $2}')
11018         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11019         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11020         lru_resize_enable mdc
11021         lru_resize_enable osc
11022 }
11023 run_test 120c "Early Lock Cancel: link test"
11024
11025 test_120d() {
11026         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11027         remote_mds_nodsh && skip "remote MDS with nodsh"
11028         test_mkdir -i0 -c1 $DIR/$tdir
11029         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11030                 skip_env "no early lock cancel on server"
11031
11032         lru_resize_disable mdc
11033         lru_resize_disable osc
11034         touch $DIR/$tdir
11035         cancel_lru_locks mdc
11036         stat $DIR/$tdir > /dev/null
11037         can1=$(do_facet mds1 \
11038                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11039                awk '/ldlm_cancel/ {print $2}')
11040         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11041                awk '/ldlm_bl_callback/ {print $2}')
11042         chmod a+x $DIR/$tdir
11043         can2=$(do_facet mds1 \
11044                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11045                awk '/ldlm_cancel/ {print $2}')
11046         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11047                awk '/ldlm_bl_callback/ {print $2}')
11048         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11049         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11050         lru_resize_enable mdc
11051         lru_resize_enable osc
11052 }
11053 run_test 120d "Early Lock Cancel: setattr test"
11054
11055 test_120e() {
11056         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11057         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11058                 skip_env "no early lock cancel on server"
11059         remote_mds_nodsh && skip "remote MDS with nodsh"
11060
11061         local dlmtrace_set=false
11062
11063         test_mkdir -i0 -c1 $DIR/$tdir
11064         lru_resize_disable mdc
11065         lru_resize_disable osc
11066         ! $LCTL get_param debug | grep -q dlmtrace &&
11067                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
11068         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
11069         cancel_lru_locks mdc
11070         cancel_lru_locks osc
11071         dd if=$DIR/$tdir/f1 of=/dev/null
11072         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
11073         # XXX client can not do early lock cancel of OST lock
11074         # during unlink (LU-4206), so cancel osc lock now.
11075         sleep 2
11076         cancel_lru_locks osc
11077         can1=$(do_facet mds1 \
11078                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11079                awk '/ldlm_cancel/ {print $2}')
11080         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11081                awk '/ldlm_bl_callback/ {print $2}')
11082         unlink $DIR/$tdir/f1
11083         sleep 5
11084         can2=$(do_facet mds1 \
11085                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11086                awk '/ldlm_cancel/ {print $2}')
11087         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11088                awk '/ldlm_bl_callback/ {print $2}')
11089         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
11090                 $LCTL dk $TMP/cancel.debug.txt
11091         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
11092                 $LCTL dk $TMP/blocking.debug.txt
11093         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
11094         lru_resize_enable mdc
11095         lru_resize_enable osc
11096 }
11097 run_test 120e "Early Lock Cancel: unlink test"
11098
11099 test_120f() {
11100         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11101         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11102                 skip_env "no early lock cancel on server"
11103         remote_mds_nodsh && skip "remote MDS with nodsh"
11104
11105         test_mkdir -i0 -c1 $DIR/$tdir
11106         lru_resize_disable mdc
11107         lru_resize_disable osc
11108         test_mkdir -i0 -c1 $DIR/$tdir/d1
11109         test_mkdir -i0 -c1 $DIR/$tdir/d2
11110         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
11111         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
11112         cancel_lru_locks mdc
11113         cancel_lru_locks osc
11114         dd if=$DIR/$tdir/d1/f1 of=/dev/null
11115         dd if=$DIR/$tdir/d2/f2 of=/dev/null
11116         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
11117         # XXX client can not do early lock cancel of OST lock
11118         # during rename (LU-4206), so cancel osc lock now.
11119         sleep 2
11120         cancel_lru_locks osc
11121         can1=$(do_facet mds1 \
11122                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11123                awk '/ldlm_cancel/ {print $2}')
11124         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11125                awk '/ldlm_bl_callback/ {print $2}')
11126         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
11127         sleep 5
11128         can2=$(do_facet mds1 \
11129                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11130                awk '/ldlm_cancel/ {print $2}')
11131         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11132                awk '/ldlm_bl_callback/ {print $2}')
11133         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11134         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11135         lru_resize_enable mdc
11136         lru_resize_enable osc
11137 }
11138 run_test 120f "Early Lock Cancel: rename test"
11139
11140 test_120g() {
11141         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11142         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11143                 skip_env "no early lock cancel on server"
11144         remote_mds_nodsh && skip "remote MDS with nodsh"
11145
11146         lru_resize_disable mdc
11147         lru_resize_disable osc
11148         count=10000
11149         echo create $count files
11150         test_mkdir $DIR/$tdir
11151         cancel_lru_locks mdc
11152         cancel_lru_locks osc
11153         t0=$(date +%s)
11154
11155         can0=$(do_facet $SINGLEMDS \
11156                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11157                awk '/ldlm_cancel/ {print $2}')
11158         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11159                awk '/ldlm_bl_callback/ {print $2}')
11160         createmany -o $DIR/$tdir/f $count
11161         sync
11162         can1=$(do_facet $SINGLEMDS \
11163                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11164                awk '/ldlm_cancel/ {print $2}')
11165         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11166                awk '/ldlm_bl_callback/ {print $2}')
11167         t1=$(date +%s)
11168         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
11169         echo rm $count files
11170         rm -r $DIR/$tdir
11171         sync
11172         can2=$(do_facet $SINGLEMDS \
11173                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11174                awk '/ldlm_cancel/ {print $2}')
11175         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11176                awk '/ldlm_bl_callback/ {print $2}')
11177         t2=$(date +%s)
11178         echo total: $count removes in $((t2-t1))
11179         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
11180         sleep 2
11181         # wait for commitment of removal
11182         lru_resize_enable mdc
11183         lru_resize_enable osc
11184 }
11185 run_test 120g "Early Lock Cancel: performance test"
11186
11187 test_121() { #bug #10589
11188         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11189
11190         rm -rf $DIR/$tfile
11191         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
11192 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
11193         lctl set_param fail_loc=0x310
11194         cancel_lru_locks osc > /dev/null
11195         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
11196         lctl set_param fail_loc=0
11197         [[ $reads -eq $writes ]] ||
11198                 error "read $reads blocks, must be $writes blocks"
11199 }
11200 run_test 121 "read cancel race ========="
11201
11202 test_123a() { # was test 123, statahead(bug 11401)
11203         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11204
11205         SLOWOK=0
11206         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
11207                 log "testing UP system. Performance may be lower than expected."
11208                 SLOWOK=1
11209         fi
11210
11211         rm -rf $DIR/$tdir
11212         test_mkdir $DIR/$tdir
11213         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
11214         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
11215         MULT=10
11216         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
11217                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
11218
11219                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
11220                 lctl set_param -n llite.*.statahead_max 0
11221                 lctl get_param llite.*.statahead_max
11222                 cancel_lru_locks mdc
11223                 cancel_lru_locks osc
11224                 stime=`date +%s`
11225                 time ls -l $DIR/$tdir | wc -l
11226                 etime=`date +%s`
11227                 delta=$((etime - stime))
11228                 log "ls $i files without statahead: $delta sec"
11229                 lctl set_param llite.*.statahead_max=$max
11230
11231                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
11232                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
11233                 cancel_lru_locks mdc
11234                 cancel_lru_locks osc
11235                 stime=`date +%s`
11236                 time ls -l $DIR/$tdir | wc -l
11237                 etime=`date +%s`
11238                 delta_sa=$((etime - stime))
11239                 log "ls $i files with statahead: $delta_sa sec"
11240                 lctl get_param -n llite.*.statahead_stats
11241                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
11242
11243                 [[ $swrong -lt $ewrong ]] &&
11244                         log "statahead was stopped, maybe too many locks held!"
11245                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
11246
11247                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
11248                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
11249                     lctl set_param -n llite.*.statahead_max 0
11250                     lctl get_param llite.*.statahead_max
11251                     cancel_lru_locks mdc
11252                     cancel_lru_locks osc
11253                     stime=`date +%s`
11254                     time ls -l $DIR/$tdir | wc -l
11255                     etime=`date +%s`
11256                     delta=$((etime - stime))
11257                     log "ls $i files again without statahead: $delta sec"
11258                     lctl set_param llite.*.statahead_max=$max
11259                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
11260                         if [  $SLOWOK -eq 0 ]; then
11261                                 error "ls $i files is slower with statahead!"
11262                         else
11263                                 log "ls $i files is slower with statahead!"
11264                         fi
11265                         break
11266                     fi
11267                 fi
11268
11269                 [ $delta -gt 20 ] && break
11270                 [ $delta -gt 8 ] && MULT=$((50 / delta))
11271                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
11272         done
11273         log "ls done"
11274
11275         stime=`date +%s`
11276         rm -r $DIR/$tdir
11277         sync
11278         etime=`date +%s`
11279         delta=$((etime - stime))
11280         log "rm -r $DIR/$tdir/: $delta seconds"
11281         log "rm done"
11282         lctl get_param -n llite.*.statahead_stats
11283 }
11284 run_test 123a "verify statahead work"
11285
11286 test_123b () { # statahead(bug 15027)
11287         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11288
11289         test_mkdir $DIR/$tdir
11290         createmany -o $DIR/$tdir/$tfile-%d 1000
11291
11292         cancel_lru_locks mdc
11293         cancel_lru_locks osc
11294
11295 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
11296         lctl set_param fail_loc=0x80000803
11297         ls -lR $DIR/$tdir > /dev/null
11298         log "ls done"
11299         lctl set_param fail_loc=0x0
11300         lctl get_param -n llite.*.statahead_stats
11301         rm -r $DIR/$tdir
11302         sync
11303
11304 }
11305 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
11306
11307 test_123c() {
11308         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
11309
11310         test_mkdir -i 0 -c 1 $DIR/$tdir.0
11311         test_mkdir -i 1 -c 1 $DIR/$tdir.1
11312         touch $DIR/$tdir.1/{1..3}
11313         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
11314
11315         remount_client $MOUNT
11316
11317         $MULTIOP $DIR/$tdir.0 Q
11318
11319         # let statahead to complete
11320         ls -l $DIR/$tdir.0 > /dev/null
11321
11322         testid=$(echo $TESTNAME | tr '_' ' ')
11323         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
11324                 error "statahead warning" || true
11325 }
11326 run_test 123c "Can not initialize inode warning on DNE statahead"
11327
11328 test_124a() {
11329         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11330         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11331                 skip_env "no lru resize on server"
11332
11333         local NR=2000
11334
11335         test_mkdir $DIR/$tdir
11336
11337         log "create $NR files at $DIR/$tdir"
11338         createmany -o $DIR/$tdir/f $NR ||
11339                 error "failed to create $NR files in $DIR/$tdir"
11340
11341         cancel_lru_locks mdc
11342         ls -l $DIR/$tdir > /dev/null
11343
11344         local NSDIR=""
11345         local LRU_SIZE=0
11346         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
11347                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
11348                 LRU_SIZE=$($LCTL get_param -n $PARAM)
11349                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
11350                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
11351                         log "NSDIR=$NSDIR"
11352                         log "NS=$(basename $NSDIR)"
11353                         break
11354                 fi
11355         done
11356
11357         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
11358                 skip "Not enough cached locks created!"
11359         fi
11360         log "LRU=$LRU_SIZE"
11361
11362         local SLEEP=30
11363
11364         # We know that lru resize allows one client to hold $LIMIT locks
11365         # for 10h. After that locks begin to be killed by client.
11366         local MAX_HRS=10
11367         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
11368         log "LIMIT=$LIMIT"
11369         if [ $LIMIT -lt $LRU_SIZE ]; then
11370                 skip "Limit is too small $LIMIT"
11371         fi
11372
11373         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
11374         # killing locks. Some time was spent for creating locks. This means
11375         # that up to the moment of sleep finish we must have killed some of
11376         # them (10-100 locks). This depends on how fast ther were created.
11377         # Many of them were touched in almost the same moment and thus will
11378         # be killed in groups.
11379         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
11380
11381         # Use $LRU_SIZE_B here to take into account real number of locks
11382         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
11383         local LRU_SIZE_B=$LRU_SIZE
11384         log "LVF=$LVF"
11385         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
11386         log "OLD_LVF=$OLD_LVF"
11387         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
11388
11389         # Let's make sure that we really have some margin. Client checks
11390         # cached locks every 10 sec.
11391         SLEEP=$((SLEEP+20))
11392         log "Sleep ${SLEEP} sec"
11393         local SEC=0
11394         while ((SEC<$SLEEP)); do
11395                 echo -n "..."
11396                 sleep 5
11397                 SEC=$((SEC+5))
11398                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
11399                 echo -n "$LRU_SIZE"
11400         done
11401         echo ""
11402         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
11403         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
11404
11405         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
11406                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
11407                 unlinkmany $DIR/$tdir/f $NR
11408                 return
11409         }
11410
11411         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
11412         log "unlink $NR files at $DIR/$tdir"
11413         unlinkmany $DIR/$tdir/f $NR
11414 }
11415 run_test 124a "lru resize ======================================="
11416
11417 get_max_pool_limit()
11418 {
11419         local limit=$($LCTL get_param \
11420                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
11421         local max=0
11422         for l in $limit; do
11423                 if [[ $l -gt $max ]]; then
11424                         max=$l
11425                 fi
11426         done
11427         echo $max
11428 }
11429
11430 test_124b() {
11431         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11432         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11433                 skip_env "no lru resize on server"
11434
11435         LIMIT=$(get_max_pool_limit)
11436
11437         NR=$(($(default_lru_size)*20))
11438         if [[ $NR -gt $LIMIT ]]; then
11439                 log "Limit lock number by $LIMIT locks"
11440                 NR=$LIMIT
11441         fi
11442
11443         IFree=$(mdsrate_inodes_available)
11444         if [ $IFree -lt $NR ]; then
11445                 log "Limit lock number by $IFree inodes"
11446                 NR=$IFree
11447         fi
11448
11449         lru_resize_disable mdc
11450         test_mkdir -p $DIR/$tdir/disable_lru_resize
11451
11452         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
11453         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
11454         cancel_lru_locks mdc
11455         stime=`date +%s`
11456         PID=""
11457         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11458         PID="$PID $!"
11459         sleep 2
11460         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11461         PID="$PID $!"
11462         sleep 2
11463         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11464         PID="$PID $!"
11465         wait $PID
11466         etime=`date +%s`
11467         nolruresize_delta=$((etime-stime))
11468         log "ls -la time: $nolruresize_delta seconds"
11469         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11470         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
11471
11472         lru_resize_enable mdc
11473         test_mkdir -p $DIR/$tdir/enable_lru_resize
11474
11475         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
11476         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
11477         cancel_lru_locks mdc
11478         stime=`date +%s`
11479         PID=""
11480         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11481         PID="$PID $!"
11482         sleep 2
11483         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11484         PID="$PID $!"
11485         sleep 2
11486         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11487         PID="$PID $!"
11488         wait $PID
11489         etime=`date +%s`
11490         lruresize_delta=$((etime-stime))
11491         log "ls -la time: $lruresize_delta seconds"
11492         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11493
11494         if [ $lruresize_delta -gt $nolruresize_delta ]; then
11495                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
11496         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
11497                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
11498         else
11499                 log "lru resize performs the same with no lru resize"
11500         fi
11501         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
11502 }
11503 run_test 124b "lru resize (performance test) ======================="
11504
11505 test_124c() {
11506         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11507         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11508                 skip_env "no lru resize on server"
11509
11510         # cache ununsed locks on client
11511         local nr=100
11512         cancel_lru_locks mdc
11513         test_mkdir $DIR/$tdir
11514         createmany -o $DIR/$tdir/f $nr ||
11515                 error "failed to create $nr files in $DIR/$tdir"
11516         ls -l $DIR/$tdir > /dev/null
11517
11518         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11519         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11520         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11521         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11522         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11523
11524         # set lru_max_age to 1 sec
11525         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11526         echo "sleep $((recalc_p * 2)) seconds..."
11527         sleep $((recalc_p * 2))
11528
11529         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11530         # restore lru_max_age
11531         $LCTL set_param -n $nsdir.lru_max_age $max_age
11532         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11533         unlinkmany $DIR/$tdir/f $nr
11534 }
11535 run_test 124c "LRUR cancel very aged locks"
11536
11537 test_124d() {
11538         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11539         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11540                 skip_env "no lru resize on server"
11541
11542         # cache ununsed locks on client
11543         local nr=100
11544
11545         lru_resize_disable mdc
11546         stack_trap "lru_resize_enable mdc" EXIT
11547
11548         cancel_lru_locks mdc
11549
11550         # asynchronous object destroy at MDT could cause bl ast to client
11551         test_mkdir $DIR/$tdir
11552         createmany -o $DIR/$tdir/f $nr ||
11553                 error "failed to create $nr files in $DIR/$tdir"
11554         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
11555
11556         ls -l $DIR/$tdir > /dev/null
11557
11558         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11559         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11560         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11561         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11562
11563         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11564
11565         # set lru_max_age to 1 sec
11566         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11567         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
11568
11569         echo "sleep $((recalc_p * 2)) seconds..."
11570         sleep $((recalc_p * 2))
11571
11572         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11573
11574         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11575 }
11576 run_test 124d "cancel very aged locks if lru-resize diasbaled"
11577
11578 test_125() { # 13358
11579         $LCTL get_param -n llite.*.client_type | grep -q local ||
11580                 skip "must run as local client"
11581         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
11582                 skip_env "must have acl enabled"
11583         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
11584
11585         test_mkdir $DIR/$tdir
11586         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
11587         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
11588         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
11589 }
11590 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
11591
11592 test_126() { # bug 12829/13455
11593         $GSS && skip_env "must run as gss disabled"
11594         $LCTL get_param -n llite.*.client_type | grep -q local ||
11595                 skip "must run as local client"
11596         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
11597
11598         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
11599         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
11600         rm -f $DIR/$tfile
11601         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
11602 }
11603 run_test 126 "check that the fsgid provided by the client is taken into account"
11604
11605 test_127a() { # bug 15521
11606         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11607         local name count samp unit min max sum sumsq
11608
11609         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
11610         echo "stats before reset"
11611         $LCTL get_param osc.*.stats
11612         $LCTL set_param osc.*.stats=0
11613         local fsize=$((2048 * 1024))
11614
11615         dd if=/dev/zero of=$DIR/$tfile bs=$fsize count=1
11616         cancel_lru_locks osc
11617         dd if=$DIR/$tfile of=/dev/null bs=$fsize
11618
11619         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/$tfile.tmp
11620         stack_trap "rm -f $TMP/$tfile.tmp"
11621         while read name count samp unit min max sum sumsq; do
11622                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11623                 [ ! $min ] && error "Missing min value for $name proc entry"
11624                 eval $name=$count || error "Wrong proc format"
11625
11626                 case $name in
11627                 read_bytes|write_bytes)
11628                         [[ "$unit" =~ "bytes" ]] ||
11629                                 error "unit is not 'bytes': $unit"
11630                         (( $min >= 4096 )) || error "min is too small: $min"
11631                         (( $min <= $fsize )) || error "min is too big: $min"
11632                         (( $max >= 4096 )) || error "max is too small: $max"
11633                         (( $max <= $fsize )) || error "max is too big: $max"
11634                         (( $sum == $fsize )) || error "sum is wrong: $sum"
11635                         (( $sumsq >= ($fsize / 4096) * (4096 * 4096) )) ||
11636                                 error "sumsquare is too small: $sumsq"
11637                         (( $sumsq <= $fsize * $fsize )) ||
11638                                 error "sumsquare is too big: $sumsq"
11639                         ;;
11640                 ost_read|ost_write)
11641                         [[ "$unit" =~ "usec" ]] ||
11642                                 error "unit is not 'usec': $unit"
11643                         ;;
11644                 *)      ;;
11645                 esac
11646         done < $DIR/$tfile.tmp
11647
11648         #check that we actually got some stats
11649         [ "$read_bytes" ] || error "Missing read_bytes stats"
11650         [ "$write_bytes" ] || error "Missing write_bytes stats"
11651         [ "$read_bytes" != 0 ] || error "no read done"
11652         [ "$write_bytes" != 0 ] || error "no write done"
11653 }
11654 run_test 127a "verify the client stats are sane"
11655
11656 test_127b() { # bug LU-333
11657         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11658         local name count samp unit min max sum sumsq
11659
11660         echo "stats before reset"
11661         $LCTL get_param llite.*.stats
11662         $LCTL set_param llite.*.stats=0
11663
11664         # perform 2 reads and writes so MAX is different from SUM.
11665         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11666         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11667         cancel_lru_locks osc
11668         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11669         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11670
11671         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
11672         stack_trap "rm -f $TMP/$tfile.tmp"
11673         while read name count samp unit min max sum sumsq; do
11674                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11675                 eval $name=$count || error "Wrong proc format"
11676
11677                 case $name in
11678                 read_bytes|write_bytes)
11679                         [[ "$unit" =~ "bytes" ]] ||
11680                                 error "unit is not 'bytes': $unit"
11681                         (( $count == 2 )) || error "count is not 2: $count"
11682                         (( $min == $PAGE_SIZE )) ||
11683                                 error "min is not $PAGE_SIZE: $min"
11684                         (( $max == $PAGE_SIZE )) ||
11685                                 error "max is not $PAGE_SIZE: $max"
11686                         (( $sum == $PAGE_SIZE * 2 )) ||
11687                                 error "sum is not $((PAGE_SIZE * 2)): $sum"
11688                         ;;
11689                 read|write)
11690                         [[ "$unit" =~ "usec" ]] ||
11691                                 error "unit is not 'usec': $unit"
11692                         ;;
11693                 *)      ;;
11694                 esac
11695         done < $TMP/$tfile.tmp
11696
11697         #check that we actually got some stats
11698         [ "$read_bytes" ] || error "Missing read_bytes stats"
11699         [ "$write_bytes" ] || error "Missing write_bytes stats"
11700         [ "$read_bytes" != 0 ] || error "no read done"
11701         [ "$write_bytes" != 0 ] || error "no write done"
11702 }
11703 run_test 127b "verify the llite client stats are sane"
11704
11705 test_127c() { # LU-12394
11706         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11707         local size
11708         local bsize
11709         local reads
11710         local writes
11711         local count
11712
11713         $LCTL set_param llite.*.extents_stats=1
11714         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
11715
11716         # Use two stripes so there is enough space in default config
11717         $LFS setstripe -c 2 $DIR/$tfile
11718
11719         # Extent stats start at 0-4K and go in power of two buckets
11720         # LL_HIST_START = 12 --> 2^12 = 4K
11721         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
11722         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
11723         # small configs
11724         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
11725                 do
11726                 # Write and read, 2x each, second time at a non-zero offset
11727                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
11728                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
11729                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
11730                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
11731                 rm -f $DIR/$tfile
11732         done
11733
11734         $LCTL get_param llite.*.extents_stats
11735
11736         count=2
11737         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
11738                 do
11739                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
11740                                 grep -m 1 $bsize)
11741                 reads=$(echo $bucket | awk '{print $5}')
11742                 writes=$(echo $bucket | awk '{print $9}')
11743                 [ "$reads" -eq $count ] ||
11744                         error "$reads reads in < $bsize bucket, expect $count"
11745                 [ "$writes" -eq $count ] ||
11746                         error "$writes writes in < $bsize bucket, expect $count"
11747         done
11748
11749         # Test mmap write and read
11750         $LCTL set_param llite.*.extents_stats=c
11751         size=512
11752         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
11753         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
11754         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
11755
11756         $LCTL get_param llite.*.extents_stats
11757
11758         count=$(((size*1024) / PAGE_SIZE))
11759
11760         bsize=$((2 * PAGE_SIZE / 1024))K
11761
11762         bucket=$($LCTL get_param -n llite.*.extents_stats |
11763                         grep -m 1 $bsize)
11764         reads=$(echo $bucket | awk '{print $5}')
11765         writes=$(echo $bucket | awk '{print $9}')
11766         # mmap writes fault in the page first, creating an additonal read
11767         [ "$reads" -eq $((2 * count)) ] ||
11768                 error "$reads reads in < $bsize bucket, expect $count"
11769         [ "$writes" -eq $count ] ||
11770                 error "$writes writes in < $bsize bucket, expect $count"
11771 }
11772 run_test 127c "test llite extent stats with regular & mmap i/o"
11773
11774 test_128() { # bug 15212
11775         touch $DIR/$tfile
11776         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
11777                 find $DIR/$tfile
11778                 find $DIR/$tfile
11779         EOF
11780
11781         result=$(grep error $TMP/$tfile.log)
11782         rm -f $DIR/$tfile $TMP/$tfile.log
11783         [ -z "$result" ] ||
11784                 error "consecutive find's under interactive lfs failed"
11785 }
11786 run_test 128 "interactive lfs for 2 consecutive find's"
11787
11788 set_dir_limits () {
11789         local mntdev
11790         local canondev
11791         local node
11792
11793         local ldproc=/proc/fs/ldiskfs
11794         local facets=$(get_facets MDS)
11795
11796         for facet in ${facets//,/ }; do
11797                 canondev=$(ldiskfs_canon \
11798                            *.$(convert_facet2label $facet).mntdev $facet)
11799                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
11800                         ldproc=/sys/fs/ldiskfs
11801                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
11802                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
11803         done
11804 }
11805
11806 check_mds_dmesg() {
11807         local facets=$(get_facets MDS)
11808         for facet in ${facets//,/ }; do
11809                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
11810         done
11811         return 1
11812 }
11813
11814 test_129() {
11815         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11816         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
11817                 skip "Need MDS version with at least 2.5.56"
11818         if [ "$mds1_FSTYPE" != ldiskfs ]; then
11819                 skip_env "ldiskfs only test"
11820         fi
11821         remote_mds_nodsh && skip "remote MDS with nodsh"
11822
11823         local ENOSPC=28
11824         local EFBIG=27
11825         local has_warning=false
11826
11827         rm -rf $DIR/$tdir
11828         mkdir -p $DIR/$tdir
11829
11830         # block size of mds1
11831         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
11832         set_dir_limits $maxsize $maxsize
11833         local dirsize=$(stat -c%s "$DIR/$tdir")
11834         local nfiles=0
11835         while [[ $dirsize -le $maxsize ]]; do
11836                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
11837                 rc=$?
11838                 if ! $has_warning; then
11839                         check_mds_dmesg '"is approaching"' && has_warning=true
11840                 fi
11841                 # check two errors:
11842                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
11843                 # EFBIG for previous versions included in ldiskfs series
11844                 if [ $rc -eq $EFBIG ] || [ $rc -eq $ENOSPC ]; then
11845                         set_dir_limits 0 0
11846                         echo "return code $rc received as expected"
11847
11848                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
11849                                 error_exit "create failed w/o dir size limit"
11850
11851                         check_mds_dmesg '"has reached"' ||
11852                                 error_exit "reached message should be output"
11853
11854                         [ $has_warning = "false" ] &&
11855                                 error_exit "warning message should be output"
11856
11857                         dirsize=$(stat -c%s "$DIR/$tdir")
11858
11859                         [[ $dirsize -ge $maxsize ]] && return 0
11860                         error_exit "current dir size $dirsize, " \
11861                                    "previous limit $maxsize"
11862                 elif [ $rc -ne 0 ]; then
11863                         set_dir_limits 0 0
11864                         error_exit "return $rc received instead of expected " \
11865                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
11866                 fi
11867                 nfiles=$((nfiles + 1))
11868                 dirsize=$(stat -c%s "$DIR/$tdir")
11869         done
11870
11871         set_dir_limits 0 0
11872         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
11873 }
11874 run_test 129 "test directory size limit ========================"
11875
11876 OLDIFS="$IFS"
11877 cleanup_130() {
11878         trap 0
11879         IFS="$OLDIFS"
11880 }
11881
11882 test_130a() {
11883         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11884         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11885
11886         trap cleanup_130 EXIT RETURN
11887
11888         local fm_file=$DIR/$tfile
11889         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
11890         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
11891                 error "dd failed for $fm_file"
11892
11893         # LU-1795: test filefrag/FIEMAP once, even if unsupported
11894         filefrag -ves $fm_file
11895         RC=$?
11896         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11897                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11898         [ $RC != 0 ] && error "filefrag $fm_file failed"
11899
11900         filefrag_op=$(filefrag -ve -k $fm_file |
11901                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11902         lun=$($LFS getstripe -i $fm_file)
11903
11904         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
11905         IFS=$'\n'
11906         tot_len=0
11907         for line in $filefrag_op
11908         do
11909                 frag_lun=`echo $line | cut -d: -f5`
11910                 ext_len=`echo $line | cut -d: -f4`
11911                 if (( $frag_lun != $lun )); then
11912                         cleanup_130
11913                         error "FIEMAP on 1-stripe file($fm_file) failed"
11914                         return
11915                 fi
11916                 (( tot_len += ext_len ))
11917         done
11918
11919         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
11920                 cleanup_130
11921                 error "FIEMAP on 1-stripe file($fm_file) failed;"
11922                 return
11923         fi
11924
11925         cleanup_130
11926
11927         echo "FIEMAP on single striped file succeeded"
11928 }
11929 run_test 130a "FIEMAP (1-stripe file)"
11930
11931 test_130b() {
11932         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11933
11934         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11935         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11936
11937         trap cleanup_130 EXIT RETURN
11938
11939         local fm_file=$DIR/$tfile
11940         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
11941                         error "setstripe on $fm_file"
11942         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11943                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11944
11945         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
11946                 error "dd failed on $fm_file"
11947
11948         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11949         filefrag_op=$(filefrag -ve -k $fm_file |
11950                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11951
11952         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11953                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11954
11955         IFS=$'\n'
11956         tot_len=0
11957         num_luns=1
11958         for line in $filefrag_op
11959         do
11960                 frag_lun=$(echo $line | cut -d: -f5 |
11961                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11962                 ext_len=$(echo $line | cut -d: -f4)
11963                 if (( $frag_lun != $last_lun )); then
11964                         if (( tot_len != 1024 )); then
11965                                 cleanup_130
11966                                 error "FIEMAP on $fm_file failed; returned " \
11967                                 "len $tot_len for OST $last_lun instead of 1024"
11968                                 return
11969                         else
11970                                 (( num_luns += 1 ))
11971                                 tot_len=0
11972                         fi
11973                 fi
11974                 (( tot_len += ext_len ))
11975                 last_lun=$frag_lun
11976         done
11977         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
11978                 cleanup_130
11979                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11980                         "luns or wrong len for OST $last_lun"
11981                 return
11982         fi
11983
11984         cleanup_130
11985
11986         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
11987 }
11988 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
11989
11990 test_130c() {
11991         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11992
11993         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11994         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11995
11996         trap cleanup_130 EXIT RETURN
11997
11998         local fm_file=$DIR/$tfile
11999         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
12000         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12001                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12002
12003         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
12004                         error "dd failed on $fm_file"
12005
12006         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12007         filefrag_op=$(filefrag -ve -k $fm_file |
12008                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12009
12010         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12011                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12012
12013         IFS=$'\n'
12014         tot_len=0
12015         num_luns=1
12016         for line in $filefrag_op
12017         do
12018                 frag_lun=$(echo $line | cut -d: -f5 |
12019                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12020                 ext_len=$(echo $line | cut -d: -f4)
12021                 if (( $frag_lun != $last_lun )); then
12022                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
12023                         if (( logical != 512 )); then
12024                                 cleanup_130
12025                                 error "FIEMAP on $fm_file failed; returned " \
12026                                 "logical start for lun $logical instead of 512"
12027                                 return
12028                         fi
12029                         if (( tot_len != 512 )); then
12030                                 cleanup_130
12031                                 error "FIEMAP on $fm_file failed; returned " \
12032                                 "len $tot_len for OST $last_lun instead of 1024"
12033                                 return
12034                         else
12035                                 (( num_luns += 1 ))
12036                                 tot_len=0
12037                         fi
12038                 fi
12039                 (( tot_len += ext_len ))
12040                 last_lun=$frag_lun
12041         done
12042         if (( num_luns != 2 || tot_len != 512 )); then
12043                 cleanup_130
12044                 error "FIEMAP on $fm_file failed; returned wrong number of " \
12045                         "luns or wrong len for OST $last_lun"
12046                 return
12047         fi
12048
12049         cleanup_130
12050
12051         echo "FIEMAP on 2-stripe file with hole succeeded"
12052 }
12053 run_test 130c "FIEMAP (2-stripe file with hole)"
12054
12055 test_130d() {
12056         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
12057
12058         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12059         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12060
12061         trap cleanup_130 EXIT RETURN
12062
12063         local fm_file=$DIR/$tfile
12064         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
12065                         error "setstripe on $fm_file"
12066         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12067                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12068
12069         local actual_stripe_count=$($LFS getstripe -c $fm_file)
12070         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
12071                 error "dd failed on $fm_file"
12072
12073         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12074         filefrag_op=$(filefrag -ve -k $fm_file |
12075                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12076
12077         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12078                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12079
12080         IFS=$'\n'
12081         tot_len=0
12082         num_luns=1
12083         for line in $filefrag_op
12084         do
12085                 frag_lun=$(echo $line | cut -d: -f5 |
12086                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12087                 ext_len=$(echo $line | cut -d: -f4)
12088                 if (( $frag_lun != $last_lun )); then
12089                         if (( tot_len != 1024 )); then
12090                                 cleanup_130
12091                                 error "FIEMAP on $fm_file failed; returned " \
12092                                 "len $tot_len for OST $last_lun instead of 1024"
12093                                 return
12094                         else
12095                                 (( num_luns += 1 ))
12096                                 tot_len=0
12097                         fi
12098                 fi
12099                 (( tot_len += ext_len ))
12100                 last_lun=$frag_lun
12101         done
12102         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
12103                 cleanup_130
12104                 error "FIEMAP on $fm_file failed; returned wrong number of " \
12105                         "luns or wrong len for OST $last_lun"
12106                 return
12107         fi
12108
12109         cleanup_130
12110
12111         echo "FIEMAP on N-stripe file succeeded"
12112 }
12113 run_test 130d "FIEMAP (N-stripe file)"
12114
12115 test_130e() {
12116         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12117
12118         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12119         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
12120
12121         trap cleanup_130 EXIT RETURN
12122
12123         local fm_file=$DIR/$tfile
12124         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
12125         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12126                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12127
12128         NUM_BLKS=512
12129         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
12130         for ((i = 0; i < $NUM_BLKS; i++))
12131         do
12132                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
12133         done
12134
12135         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12136         filefrag_op=$(filefrag -ve -k $fm_file |
12137                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12138
12139         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12140                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12141
12142         IFS=$'\n'
12143         tot_len=0
12144         num_luns=1
12145         for line in $filefrag_op
12146         do
12147                 frag_lun=$(echo $line | cut -d: -f5 |
12148                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12149                 ext_len=$(echo $line | cut -d: -f4)
12150                 if (( $frag_lun != $last_lun )); then
12151                         if (( tot_len != $EXPECTED_LEN )); then
12152                                 cleanup_130
12153                                 error "FIEMAP on $fm_file failed; returned " \
12154                                 "len $tot_len for OST $last_lun instead " \
12155                                 "of $EXPECTED_LEN"
12156                                 return
12157                         else
12158                                 (( num_luns += 1 ))
12159                                 tot_len=0
12160                         fi
12161                 fi
12162                 (( tot_len += ext_len ))
12163                 last_lun=$frag_lun
12164         done
12165         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
12166                 cleanup_130
12167                 error "FIEMAP on $fm_file failed; returned wrong number " \
12168                         "of luns or wrong len for OST $last_lun"
12169                 return
12170         fi
12171
12172         cleanup_130
12173
12174         echo "FIEMAP with continuation calls succeeded"
12175 }
12176 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
12177
12178 test_130f() {
12179         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12180         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12181
12182         local fm_file=$DIR/$tfile
12183         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
12184                 error "multiop create with lov_delay_create on $fm_file"
12185
12186         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12187         filefrag_extents=$(filefrag -vek $fm_file |
12188                            awk '/extents? found/ { print $2 }')
12189         if [[ "$filefrag_extents" != "0" ]]; then
12190                 error "FIEMAP on $fm_file failed; " \
12191                       "returned $filefrag_extents expected 0"
12192         fi
12193
12194         rm -f $fm_file
12195 }
12196 run_test 130f "FIEMAP (unstriped file)"
12197
12198 # Test for writev/readv
12199 test_131a() {
12200         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
12201                 error "writev test failed"
12202         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
12203                 error "readv failed"
12204         rm -f $DIR/$tfile
12205 }
12206 run_test 131a "test iov's crossing stripe boundary for writev/readv"
12207
12208 test_131b() {
12209         local fsize=$((524288 + 1048576 + 1572864))
12210         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
12211                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12212                         error "append writev test failed"
12213
12214         ((fsize += 1572864 + 1048576))
12215         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
12216                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12217                         error "append writev test failed"
12218         rm -f $DIR/$tfile
12219 }
12220 run_test 131b "test append writev"
12221
12222 test_131c() {
12223         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
12224         error "NOT PASS"
12225 }
12226 run_test 131c "test read/write on file w/o objects"
12227
12228 test_131d() {
12229         rwv -f $DIR/$tfile -w -n 1 1572864
12230         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
12231         if [ "$NOB" != 1572864 ]; then
12232                 error "Short read filed: read $NOB bytes instead of 1572864"
12233         fi
12234         rm -f $DIR/$tfile
12235 }
12236 run_test 131d "test short read"
12237
12238 test_131e() {
12239         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
12240         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
12241         error "read hitting hole failed"
12242         rm -f $DIR/$tfile
12243 }
12244 run_test 131e "test read hitting hole"
12245
12246 check_stats() {
12247         local facet=$1
12248         local op=$2
12249         local want=${3:-0}
12250         local res
12251
12252         case $facet in
12253         mds*) res=$(do_facet $facet \
12254                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
12255                  ;;
12256         ost*) res=$(do_facet $facet \
12257                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
12258                  ;;
12259         *) error "Wrong facet '$facet'" ;;
12260         esac
12261         [ "$res" ] || error "The counter for $op on $facet was not incremented"
12262         # if the argument $3 is zero, it means any stat increment is ok.
12263         if [[ $want -gt 0 ]]; then
12264                 local count=$(echo $res | awk '{ print $2 }')
12265                 [[ $count -ne $want ]] &&
12266                         error "The $op counter on $facet is $count, not $want"
12267         fi
12268 }
12269
12270 test_133a() {
12271         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12272         remote_ost_nodsh && skip "remote OST with nodsh"
12273         remote_mds_nodsh && skip "remote MDS with nodsh"
12274         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12275                 skip_env "MDS doesn't support rename stats"
12276
12277         local testdir=$DIR/${tdir}/stats_testdir
12278
12279         mkdir -p $DIR/${tdir}
12280
12281         # clear stats.
12282         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12283         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12284
12285         # verify mdt stats first.
12286         mkdir ${testdir} || error "mkdir failed"
12287         check_stats $SINGLEMDS "mkdir" 1
12288         touch ${testdir}/${tfile} || error "touch failed"
12289         check_stats $SINGLEMDS "open" 1
12290         check_stats $SINGLEMDS "close" 1
12291         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
12292                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
12293                 check_stats $SINGLEMDS "mknod" 2
12294         }
12295         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
12296         check_stats $SINGLEMDS "unlink" 1
12297         rm -f ${testdir}/${tfile} || error "file remove failed"
12298         check_stats $SINGLEMDS "unlink" 2
12299
12300         # remove working dir and check mdt stats again.
12301         rmdir ${testdir} || error "rmdir failed"
12302         check_stats $SINGLEMDS "rmdir" 1
12303
12304         local testdir1=$DIR/${tdir}/stats_testdir1
12305         mkdir -p ${testdir}
12306         mkdir -p ${testdir1}
12307         touch ${testdir1}/test1
12308         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
12309         check_stats $SINGLEMDS "crossdir_rename" 1
12310
12311         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
12312         check_stats $SINGLEMDS "samedir_rename" 1
12313
12314         rm -rf $DIR/${tdir}
12315 }
12316 run_test 133a "Verifying MDT stats ========================================"
12317
12318 test_133b() {
12319         local res
12320
12321         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12322         remote_ost_nodsh && skip "remote OST with nodsh"
12323         remote_mds_nodsh && skip "remote MDS with nodsh"
12324
12325         local testdir=$DIR/${tdir}/stats_testdir
12326
12327         mkdir -p ${testdir} || error "mkdir failed"
12328         touch ${testdir}/${tfile} || error "touch failed"
12329         cancel_lru_locks mdc
12330
12331         # clear stats.
12332         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12333         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12334
12335         # extra mdt stats verification.
12336         chmod 444 ${testdir}/${tfile} || error "chmod failed"
12337         check_stats $SINGLEMDS "setattr" 1
12338         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12339         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
12340         then            # LU-1740
12341                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
12342                 check_stats $SINGLEMDS "getattr" 1
12343         fi
12344         rm -rf $DIR/${tdir}
12345
12346         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
12347         # so the check below is not reliable
12348         [ $MDSCOUNT -eq 1 ] || return 0
12349
12350         # Sleep to avoid a cached response.
12351         #define OBD_STATFS_CACHE_SECONDS 1
12352         sleep 2
12353         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12354         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12355         $LFS df || error "lfs failed"
12356         check_stats $SINGLEMDS "statfs" 1
12357
12358         # check aggregated statfs (LU-10018)
12359         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
12360                 return 0
12361         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
12362                 return 0
12363         sleep 2
12364         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12365         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12366         df $DIR
12367         check_stats $SINGLEMDS "statfs" 1
12368
12369         # We want to check that the client didn't send OST_STATFS to
12370         # ost1 but the MDT also uses OST_STATFS for precreate. So some
12371         # extra care is needed here.
12372         if remote_mds; then
12373                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
12374                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
12375
12376                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
12377                 [ "$res" ] && error "OST got STATFS"
12378         fi
12379
12380         return 0
12381 }
12382 run_test 133b "Verifying extra MDT stats =================================="
12383
12384 test_133c() {
12385         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12386         remote_ost_nodsh && skip "remote OST with nodsh"
12387         remote_mds_nodsh && skip "remote MDS with nodsh"
12388
12389         local testdir=$DIR/$tdir/stats_testdir
12390
12391         test_mkdir -p $testdir
12392
12393         # verify obdfilter stats.
12394         $LFS setstripe -c 1 -i 0 $testdir/$tfile
12395         sync
12396         cancel_lru_locks osc
12397         wait_delete_completed
12398
12399         # clear stats.
12400         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12401         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12402
12403         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
12404                 error "dd failed"
12405         sync
12406         cancel_lru_locks osc
12407         check_stats ost1 "write" 1
12408
12409         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
12410         check_stats ost1 "read" 1
12411
12412         > $testdir/$tfile || error "truncate failed"
12413         check_stats ost1 "punch" 1
12414
12415         rm -f $testdir/$tfile || error "file remove failed"
12416         wait_delete_completed
12417         check_stats ost1 "destroy" 1
12418
12419         rm -rf $DIR/$tdir
12420 }
12421 run_test 133c "Verifying OST stats ========================================"
12422
12423 order_2() {
12424         local value=$1
12425         local orig=$value
12426         local order=1
12427
12428         while [ $value -ge 2 ]; do
12429                 order=$((order*2))
12430                 value=$((value/2))
12431         done
12432
12433         if [ $orig -gt $order ]; then
12434                 order=$((order*2))
12435         fi
12436         echo $order
12437 }
12438
12439 size_in_KMGT() {
12440     local value=$1
12441     local size=('K' 'M' 'G' 'T');
12442     local i=0
12443     local size_string=$value
12444
12445     while [ $value -ge 1024 ]; do
12446         if [ $i -gt 3 ]; then
12447             #T is the biggest unit we get here, if that is bigger,
12448             #just return XXXT
12449             size_string=${value}T
12450             break
12451         fi
12452         value=$((value >> 10))
12453         if [ $value -lt 1024 ]; then
12454             size_string=${value}${size[$i]}
12455             break
12456         fi
12457         i=$((i + 1))
12458     done
12459
12460     echo $size_string
12461 }
12462
12463 get_rename_size() {
12464         local size=$1
12465         local context=${2:-.}
12466         local sample=$(do_facet $SINGLEMDS $LCTL \
12467                 get_param mdt.$FSNAME-MDT0000.rename_stats |
12468                 grep -A1 $context |
12469                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
12470         echo $sample
12471 }
12472
12473 test_133d() {
12474         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12475         remote_ost_nodsh && skip "remote OST with nodsh"
12476         remote_mds_nodsh && skip "remote MDS with nodsh"
12477         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12478                 skip_env "MDS doesn't support rename stats"
12479
12480         local testdir1=$DIR/${tdir}/stats_testdir1
12481         local testdir2=$DIR/${tdir}/stats_testdir2
12482         mkdir -p $DIR/${tdir}
12483
12484         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12485
12486         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
12487         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
12488
12489         createmany -o $testdir1/test 512 || error "createmany failed"
12490
12491         # check samedir rename size
12492         mv ${testdir1}/test0 ${testdir1}/test_0
12493
12494         local testdir1_size=$(ls -l $DIR/${tdir} |
12495                 awk '/stats_testdir1/ {print $5}')
12496         local testdir2_size=$(ls -l $DIR/${tdir} |
12497                 awk '/stats_testdir2/ {print $5}')
12498
12499         testdir1_size=$(order_2 $testdir1_size)
12500         testdir2_size=$(order_2 $testdir2_size)
12501
12502         testdir1_size=$(size_in_KMGT $testdir1_size)
12503         testdir2_size=$(size_in_KMGT $testdir2_size)
12504
12505         echo "source rename dir size: ${testdir1_size}"
12506         echo "target rename dir size: ${testdir2_size}"
12507
12508         local cmd="do_facet $SINGLEMDS $LCTL "
12509         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
12510
12511         eval $cmd || error "$cmd failed"
12512         local samedir=$($cmd | grep 'same_dir')
12513         local same_sample=$(get_rename_size $testdir1_size)
12514         [ -z "$samedir" ] && error "samedir_rename_size count error"
12515         [[ $same_sample -eq 1 ]] ||
12516                 error "samedir_rename_size error $same_sample"
12517         echo "Check same dir rename stats success"
12518
12519         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12520
12521         # check crossdir rename size
12522         mv ${testdir1}/test_0 ${testdir2}/test_0
12523
12524         testdir1_size=$(ls -l $DIR/${tdir} |
12525                 awk '/stats_testdir1/ {print $5}')
12526         testdir2_size=$(ls -l $DIR/${tdir} |
12527                 awk '/stats_testdir2/ {print $5}')
12528
12529         testdir1_size=$(order_2 $testdir1_size)
12530         testdir2_size=$(order_2 $testdir2_size)
12531
12532         testdir1_size=$(size_in_KMGT $testdir1_size)
12533         testdir2_size=$(size_in_KMGT $testdir2_size)
12534
12535         echo "source rename dir size: ${testdir1_size}"
12536         echo "target rename dir size: ${testdir2_size}"
12537
12538         eval $cmd || error "$cmd failed"
12539         local crossdir=$($cmd | grep 'crossdir')
12540         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
12541         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
12542         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
12543         [[ $src_sample -eq 1 ]] ||
12544                 error "crossdir_rename_size error $src_sample"
12545         [[ $tgt_sample -eq 1 ]] ||
12546                 error "crossdir_rename_size error $tgt_sample"
12547         echo "Check cross dir rename stats success"
12548         rm -rf $DIR/${tdir}
12549 }
12550 run_test 133d "Verifying rename_stats ========================================"
12551
12552 test_133e() {
12553         remote_mds_nodsh && skip "remote MDS with nodsh"
12554         remote_ost_nodsh && skip "remote OST with nodsh"
12555         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12556
12557         local testdir=$DIR/${tdir}/stats_testdir
12558         local ctr f0 f1 bs=32768 count=42 sum
12559
12560         mkdir -p ${testdir} || error "mkdir failed"
12561
12562         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
12563
12564         for ctr in {write,read}_bytes; do
12565                 sync
12566                 cancel_lru_locks osc
12567
12568                 do_facet ost1 $LCTL set_param -n \
12569                         "obdfilter.*.exports.clear=clear"
12570
12571                 if [ $ctr = write_bytes ]; then
12572                         f0=/dev/zero
12573                         f1=${testdir}/${tfile}
12574                 else
12575                         f0=${testdir}/${tfile}
12576                         f1=/dev/null
12577                 fi
12578
12579                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
12580                         error "dd failed"
12581                 sync
12582                 cancel_lru_locks osc
12583
12584                 sum=$(do_facet ost1 $LCTL get_param \
12585                         "obdfilter.*.exports.*.stats" |
12586                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
12587                                 $1 == ctr { sum += $7 }
12588                                 END { printf("%0.0f", sum) }')
12589
12590                 if ((sum != bs * count)); then
12591                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
12592                 fi
12593         done
12594
12595         rm -rf $DIR/${tdir}
12596 }
12597 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
12598
12599 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
12600
12601 # Some versions of find (4.5.11, 4.5.14) included in CentOS 7.3-7.5 do
12602 # not honor the -ignore_readdir_race option correctly. So we call
12603 # error_ignore() rather than error() in these cases. See LU-11152.
12604 error_133() {
12605         if (find --version; do_facet mds1 find --version) |
12606                 grep -q '\b4\.5\.1[1-4]\b'; then
12607                 error_ignore LU-11152 "$@"
12608         else
12609                 error "$@"
12610         fi
12611 }
12612
12613 test_133f() {
12614         # First without trusting modes.
12615         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
12616         echo "proc_dirs='$proc_dirs'"
12617         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
12618         find $proc_dirs -exec cat '{}' \; &> /dev/null
12619
12620         # Second verifying readability.
12621         $LCTL get_param -R '*' &> /dev/null
12622
12623         # Verifing writability with badarea_io.
12624         find $proc_dirs \
12625                 -ignore_readdir_race \
12626                 -type f \
12627                 -not -name force_lbug \
12628                 -not -name changelog_mask \
12629                 -exec badarea_io '{}' \; ||
12630                         error_133 "find $proc_dirs failed"
12631 }
12632 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
12633
12634 test_133g() {
12635         remote_mds_nodsh && skip "remote MDS with nodsh"
12636         remote_ost_nodsh && skip "remote OST with nodsh"
12637
12638         local facet
12639         for facet in mds1 ost1; do
12640                 local facet_ver=$(lustre_version_code $facet)
12641                 if [ $facet_ver -ge $(version_code 2.7.65) ]; then
12642                         do_facet $facet "$LCTL get_param -R '*'" &> /dev/null
12643                 else
12644                         log "$facet: too old lustre for get_param -R"
12645                 fi
12646                 if [ $facet_ver -ge $(version_code 2.5.54) ]; then
12647                         do_facet $facet "$LCTL list_param -R '*' | grep '=' |
12648                                 tr -d= | egrep -v 'force_lbug|changelog_mask' |
12649                                 xargs badarea_io" ||
12650                                         error_133 "$facet badarea_io failed"
12651                 else
12652                         skip_noexit "$facet: too old lustre for get_param -R"
12653                 fi
12654         done
12655
12656         # remount the FS in case writes/reads /proc break the FS
12657         cleanup || error "failed to unmount"
12658         setup || error "failed to setup"
12659         true
12660 }
12661 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
12662
12663 test_133h() {
12664         remote_mds_nodsh && skip "remote MDS with nodsh"
12665         remote_ost_nodsh && skip "remote OST with nodsh"
12666         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
12667                 skip "Need MDS version at least 2.9.54"
12668
12669         local facet
12670
12671         for facet in client mds1 ost1; do
12672                 local facet_proc_dirs=$(do_facet $facet \
12673                                         \\\ls -d $proc_regexp 2> /dev/null)
12674                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12675                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12676                 # Get the list of files that are missing the terminating newline
12677                 local missing=($(do_facet $facet \
12678                         find ${facet_proc_dirs} -type f \|              \
12679                                 while read F\; do                       \
12680                                         awk -v FS='\v' -v RS='\v\v'     \
12681                                         "'END { if(NR>0 &&              \
12682                                         \\\$NF !~ /.*\\\n\$/)           \
12683                                                 print FILENAME}'"       \
12684                                         '\$F'\;                         \
12685                                 done 2>/dev/null))
12686                 [ ${#missing[*]} -eq 0 ] ||
12687                         error "files do not end with newline: ${missing[*]}"
12688         done
12689 }
12690 run_test 133h "Proc files should end with newlines"
12691
12692 test_134a() {
12693         remote_mds_nodsh && skip "remote MDS with nodsh"
12694         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12695                 skip "Need MDS version at least 2.7.54"
12696
12697         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12698         cancel_lru_locks mdc
12699
12700         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
12701         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12702         [ $unused -eq 0 ] || error "$unused locks are not cleared"
12703
12704         local nr=1000
12705         createmany -o $DIR/$tdir/f $nr ||
12706                 error "failed to create $nr files in $DIR/$tdir"
12707         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12708
12709         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
12710         do_facet mds1 $LCTL set_param fail_loc=0x327
12711         do_facet mds1 $LCTL set_param fail_val=500
12712         touch $DIR/$tdir/m
12713
12714         echo "sleep 10 seconds ..."
12715         sleep 10
12716         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
12717
12718         do_facet mds1 $LCTL set_param fail_loc=0
12719         do_facet mds1 $LCTL set_param fail_val=0
12720         [ $lck_cnt -lt $unused ] ||
12721                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
12722
12723         rm $DIR/$tdir/m
12724         unlinkmany $DIR/$tdir/f $nr
12725 }
12726 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
12727
12728 test_134b() {
12729         remote_mds_nodsh && skip "remote MDS with nodsh"
12730         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12731                 skip "Need MDS version at least 2.7.54"
12732
12733         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12734         cancel_lru_locks mdc
12735
12736         local low_wm=$(do_facet mds1 $LCTL get_param -n \
12737                         ldlm.lock_reclaim_threshold_mb)
12738         # disable reclaim temporarily
12739         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
12740
12741         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
12742         do_facet mds1 $LCTL set_param fail_loc=0x328
12743         do_facet mds1 $LCTL set_param fail_val=500
12744
12745         $LCTL set_param debug=+trace
12746
12747         local nr=600
12748         createmany -o $DIR/$tdir/f $nr &
12749         local create_pid=$!
12750
12751         echo "Sleep $TIMEOUT seconds ..."
12752         sleep $TIMEOUT
12753         if ! ps -p $create_pid  > /dev/null 2>&1; then
12754                 do_facet mds1 $LCTL set_param fail_loc=0
12755                 do_facet mds1 $LCTL set_param fail_val=0
12756                 do_facet mds1 $LCTL set_param \
12757                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
12758                 error "createmany finished incorrectly!"
12759         fi
12760         do_facet mds1 $LCTL set_param fail_loc=0
12761         do_facet mds1 $LCTL set_param fail_val=0
12762         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
12763         wait $create_pid || return 1
12764
12765         unlinkmany $DIR/$tdir/f $nr
12766 }
12767 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
12768
12769 test_135() {
12770         remote_mds_nodsh && skip "remote MDS with nodsh"
12771         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
12772                 skip "Need MDS version at least 2.13.50"
12773         local fname
12774
12775         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12776
12777 #define OBD_FAIL_PLAIN_RECORDS 0x1319
12778         #set only one record at plain llog
12779         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1319 fail_val=1
12780
12781         #fill already existed plain llog each 64767
12782         #wrapping whole catalog
12783         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
12784
12785         createmany -o $DIR/$tdir/$tfile_ 64700
12786         for (( i = 0; i < 64700; i = i + 2 ))
12787         do
12788                 rm $DIR/$tdir/$tfile_$i &
12789                 rm $DIR/$tdir/$tfile_$((i + 1)) &
12790                 local pid=$!
12791                 wait $pid
12792         done
12793
12794         #waiting osp synchronization
12795         wait_delete_completed
12796 }
12797 run_test 135 "Race catalog processing"
12798
12799 test_136() {
12800         remote_mds_nodsh && skip "remote MDS with nodsh"
12801         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
12802                 skip "Need MDS version at least 2.13.50"
12803         local fname
12804
12805         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12806         $LFS setstripe -c 1 -i 0 $DIR/$tdir || error "failed to set striping"
12807         #set only one record at plain llog
12808 #define OBD_FAIL_CATALOG_FULL_CHECK                0x131a
12809         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x131a fail_val=1
12810
12811         #fill already existed 2 plain llogs each 64767
12812         #wrapping whole catalog
12813         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
12814         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 3 / 2))
12815         wait_delete_completed
12816
12817         createmany -o $DIR/$tdir/$tfile_ 10
12818         sleep 25
12819
12820         do_facet $SINGLEMDS $LCTL set_param fail_val=3
12821         for (( i = 0; i < 10; i = i + 3 ))
12822         do
12823                 rm $DIR/$tdir/$tfile_$i &
12824                 rm $DIR/$tdir/$tfile_$((i + 1)) &
12825                 local pid=$!
12826                 wait $pid
12827                 sleep 7
12828                 rm $DIR/$tdir/$tfile_$((i + 2)) &
12829         done
12830
12831         #waiting osp synchronization
12832         wait_delete_completed
12833 }
12834 run_test 136 "Race catalog processing 2"
12835
12836 test_140() { #bug-17379
12837         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12838
12839         test_mkdir $DIR/$tdir
12840         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
12841         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
12842
12843         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
12844         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
12845         local i=0
12846         while i=$((i + 1)); do
12847                 test_mkdir $i
12848                 cd $i || error "Changing to $i"
12849                 ln -s ../stat stat || error "Creating stat symlink"
12850                 # Read the symlink until ELOOP present,
12851                 # not LBUGing the system is considered success,
12852                 # we didn't overrun the stack.
12853                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
12854                 if [ $ret -ne 0 ]; then
12855                         if [ $ret -eq 40 ]; then
12856                                 break  # -ELOOP
12857                         else
12858                                 error "Open stat symlink"
12859                                         return
12860                         fi
12861                 fi
12862         done
12863         i=$((i - 1))
12864         echo "The symlink depth = $i"
12865         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
12866                 error "Invalid symlink depth"
12867
12868         # Test recursive symlink
12869         ln -s symlink_self symlink_self
12870         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
12871         echo "open symlink_self returns $ret"
12872         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
12873 }
12874 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
12875
12876 test_150() {
12877         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12878
12879         local TF="$TMP/$tfile"
12880
12881         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12882         cp $TF $DIR/$tfile
12883         cancel_lru_locks $OSC
12884         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
12885         remount_client $MOUNT
12886         df -P $MOUNT
12887         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
12888
12889         $TRUNCATE $TF 6000
12890         $TRUNCATE $DIR/$tfile 6000
12891         cancel_lru_locks $OSC
12892         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
12893
12894         echo "12345" >>$TF
12895         echo "12345" >>$DIR/$tfile
12896         cancel_lru_locks $OSC
12897         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
12898
12899         echo "12345" >>$TF
12900         echo "12345" >>$DIR/$tfile
12901         cancel_lru_locks $OSC
12902         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
12903
12904         rm -f $TF
12905         true
12906 }
12907 run_test 150 "truncate/append tests"
12908
12909 #LU-2902 roc_hit was not able to read all values from lproc
12910 function roc_hit_init() {
12911         local list=$(comma_list $(osts_nodes))
12912         local dir=$DIR/$tdir-check
12913         local file=$dir/$tfile
12914         local BEFORE
12915         local AFTER
12916         local idx
12917
12918         test_mkdir $dir
12919         #use setstripe to do a write to every ost
12920         for i in $(seq 0 $((OSTCOUNT-1))); do
12921                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
12922                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
12923                 idx=$(printf %04x $i)
12924                 BEFORE=$(get_osd_param $list *OST*$idx stats |
12925                         awk '$1 == "cache_access" {sum += $7}
12926                                 END { printf("%0.0f", sum) }')
12927
12928                 cancel_lru_locks osc
12929                 cat $file >/dev/null
12930
12931                 AFTER=$(get_osd_param $list *OST*$idx stats |
12932                         awk '$1 == "cache_access" {sum += $7}
12933                                 END { printf("%0.0f", sum) }')
12934
12935                 echo BEFORE:$BEFORE AFTER:$AFTER
12936                 if ! let "AFTER - BEFORE == 4"; then
12937                         rm -rf $dir
12938                         error "roc_hit is not safe to use"
12939                 fi
12940                 rm $file
12941         done
12942
12943         rm -rf $dir
12944 }
12945
12946 function roc_hit() {
12947         local list=$(comma_list $(osts_nodes))
12948         echo $(get_osd_param $list '' stats |
12949                 awk '$1 == "cache_hit" {sum += $7}
12950                         END { printf("%0.0f", sum) }')
12951 }
12952
12953 function set_cache() {
12954         local on=1
12955
12956         if [ "$2" == "off" ]; then
12957                 on=0;
12958         fi
12959         local list=$(comma_list $(osts_nodes))
12960         set_osd_param $list '' $1_cache_enable $on
12961
12962         cancel_lru_locks osc
12963 }
12964
12965 test_151() {
12966         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12967         remote_ost_nodsh && skip "remote OST with nodsh"
12968
12969         local CPAGES=3
12970         local list=$(comma_list $(osts_nodes))
12971
12972         # check whether obdfilter is cache capable at all
12973         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
12974                 skip "not cache-capable obdfilter"
12975         fi
12976
12977         # check cache is enabled on all obdfilters
12978         if get_osd_param $list '' read_cache_enable | grep 0; then
12979                 skip "oss cache is disabled"
12980         fi
12981
12982         set_osd_param $list '' writethrough_cache_enable 1
12983
12984         # check write cache is enabled on all obdfilters
12985         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
12986                 skip "oss write cache is NOT enabled"
12987         fi
12988
12989         roc_hit_init
12990
12991         #define OBD_FAIL_OBD_NO_LRU  0x609
12992         do_nodes $list $LCTL set_param fail_loc=0x609
12993
12994         # pages should be in the case right after write
12995         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
12996                 error "dd failed"
12997
12998         local BEFORE=$(roc_hit)
12999         cancel_lru_locks osc
13000         cat $DIR/$tfile >/dev/null
13001         local AFTER=$(roc_hit)
13002
13003         do_nodes $list $LCTL set_param fail_loc=0
13004
13005         if ! let "AFTER - BEFORE == CPAGES"; then
13006                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13007         fi
13008
13009         cancel_lru_locks osc
13010         # invalidates OST cache
13011         do_nodes $list "echo 1 > /proc/sys/vm/drop_caches"
13012         set_osd_param $list '' read_cache_enable 0
13013         cat $DIR/$tfile >/dev/null
13014
13015         # now data shouldn't be found in the cache
13016         BEFORE=$(roc_hit)
13017         cancel_lru_locks osc
13018         cat $DIR/$tfile >/dev/null
13019         AFTER=$(roc_hit)
13020         if let "AFTER - BEFORE != 0"; then
13021                 error "IN CACHE: before: $BEFORE, after: $AFTER"
13022         fi
13023
13024         set_osd_param $list '' read_cache_enable 1
13025         rm -f $DIR/$tfile
13026 }
13027 run_test 151 "test cache on oss and controls ==============================="
13028
13029 test_152() {
13030         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13031
13032         local TF="$TMP/$tfile"
13033
13034         # simulate ENOMEM during write
13035 #define OBD_FAIL_OST_NOMEM      0x226
13036         lctl set_param fail_loc=0x80000226
13037         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
13038         cp $TF $DIR/$tfile
13039         sync || error "sync failed"
13040         lctl set_param fail_loc=0
13041
13042         # discard client's cache
13043         cancel_lru_locks osc
13044
13045         # simulate ENOMEM during read
13046         lctl set_param fail_loc=0x80000226
13047         cmp $TF $DIR/$tfile || error "cmp failed"
13048         lctl set_param fail_loc=0
13049
13050         rm -f $TF
13051 }
13052 run_test 152 "test read/write with enomem ============================"
13053
13054 test_153() {
13055         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
13056 }
13057 run_test 153 "test if fdatasync does not crash ======================="
13058
13059 dot_lustre_fid_permission_check() {
13060         local fid=$1
13061         local ffid=$MOUNT/.lustre/fid/$fid
13062         local test_dir=$2
13063
13064         echo "stat fid $fid"
13065         stat $ffid > /dev/null || error "stat $ffid failed."
13066         echo "touch fid $fid"
13067         touch $ffid || error "touch $ffid failed."
13068         echo "write to fid $fid"
13069         cat /etc/hosts > $ffid || error "write $ffid failed."
13070         echo "read fid $fid"
13071         diff /etc/hosts $ffid || error "read $ffid failed."
13072         echo "append write to fid $fid"
13073         cat /etc/hosts >> $ffid || error "append write $ffid failed."
13074         echo "rename fid $fid"
13075         mv $ffid $test_dir/$tfile.1 &&
13076                 error "rename $ffid to $tfile.1 should fail."
13077         touch $test_dir/$tfile.1
13078         mv $test_dir/$tfile.1 $ffid &&
13079                 error "rename $tfile.1 to $ffid should fail."
13080         rm -f $test_dir/$tfile.1
13081         echo "truncate fid $fid"
13082         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
13083         echo "link fid $fid"
13084         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
13085         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
13086                 echo "setfacl fid $fid"
13087                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
13088                 echo "getfacl fid $fid"
13089                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
13090         fi
13091         echo "unlink fid $fid"
13092         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
13093         echo "mknod fid $fid"
13094         mknod $ffid c 1 3 && error "mknod $ffid should fail."
13095
13096         fid=[0xf00000400:0x1:0x0]
13097         ffid=$MOUNT/.lustre/fid/$fid
13098
13099         echo "stat non-exist fid $fid"
13100         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
13101         echo "write to non-exist fid $fid"
13102         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
13103         echo "link new fid $fid"
13104         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
13105
13106         mkdir -p $test_dir/$tdir
13107         touch $test_dir/$tdir/$tfile
13108         fid=$($LFS path2fid $test_dir/$tdir)
13109         rc=$?
13110         [ $rc -ne 0 ] &&
13111                 error "error: could not get fid for $test_dir/$dir/$tfile."
13112
13113         ffid=$MOUNT/.lustre/fid/$fid
13114
13115         echo "ls $fid"
13116         ls $ffid > /dev/null || error "ls $ffid failed."
13117         echo "touch $fid/$tfile.1"
13118         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
13119
13120         echo "touch $MOUNT/.lustre/fid/$tfile"
13121         touch $MOUNT/.lustre/fid/$tfile && \
13122                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
13123
13124         echo "setxattr to $MOUNT/.lustre/fid"
13125         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
13126
13127         echo "listxattr for $MOUNT/.lustre/fid"
13128         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
13129
13130         echo "delxattr from $MOUNT/.lustre/fid"
13131         setfattr -x trusted.name1 $MOUNT/.lustre/fid
13132
13133         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
13134         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
13135                 error "touch invalid fid should fail."
13136
13137         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
13138         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
13139                 error "touch non-normal fid should fail."
13140
13141         echo "rename $tdir to $MOUNT/.lustre/fid"
13142         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
13143                 error "rename to $MOUNT/.lustre/fid should fail."
13144
13145         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
13146         then            # LU-3547
13147                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
13148                 local new_obf_mode=777
13149
13150                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
13151                 chmod $new_obf_mode $DIR/.lustre/fid ||
13152                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
13153
13154                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
13155                 [ $obf_mode -eq $new_obf_mode ] ||
13156                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
13157
13158                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
13159                 chmod $old_obf_mode $DIR/.lustre/fid ||
13160                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
13161         fi
13162
13163         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
13164         fid=$($LFS path2fid $test_dir/$tfile-2)
13165
13166         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
13167         then # LU-5424
13168                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
13169                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
13170                         error "create lov data thru .lustre failed"
13171         fi
13172         echo "cp /etc/passwd $test_dir/$tfile-2"
13173         cp /etc/passwd $test_dir/$tfile-2 ||
13174                 error "copy to $test_dir/$tfile-2 failed."
13175         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
13176         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
13177                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
13178
13179         rm -rf $test_dir/tfile.lnk
13180         rm -rf $test_dir/$tfile-2
13181 }
13182
13183 test_154A() {
13184         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13185                 skip "Need MDS version at least 2.4.1"
13186
13187         local tf=$DIR/$tfile
13188         touch $tf
13189
13190         local fid=$($LFS path2fid $tf)
13191         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
13192
13193         # check that we get the same pathname back
13194         local found=$($LFS fid2path $MOUNT "$fid")
13195         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
13196         [ "$found" == "$tf" ] ||
13197                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
13198 }
13199 run_test 154A "lfs path2fid and fid2path basic checks"
13200
13201 test_154B() {
13202         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13203                 skip "Need MDS version at least 2.4.1"
13204
13205         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
13206         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
13207         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
13208         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
13209
13210         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
13211         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
13212
13213         # check that we get the same pathname
13214         echo "PFID: $PFID, name: $name"
13215         local FOUND=$($LFS fid2path $MOUNT "$PFID")
13216         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
13217         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
13218                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
13219
13220         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
13221 }
13222 run_test 154B "verify the ll_decode_linkea tool"
13223
13224 test_154a() {
13225         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13226         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13227         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
13228                 skip "Need MDS version at least 2.2.51"
13229         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
13230
13231         cp /etc/hosts $DIR/$tfile
13232
13233         fid=$($LFS path2fid $DIR/$tfile)
13234         rc=$?
13235         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
13236
13237         dot_lustre_fid_permission_check "$fid" $DIR ||
13238                 error "dot lustre permission check $fid failed"
13239
13240         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
13241
13242         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
13243
13244         touch $MOUNT/.lustre/file &&
13245                 error "creation is not allowed under .lustre"
13246
13247         mkdir $MOUNT/.lustre/dir &&
13248                 error "mkdir is not allowed under .lustre"
13249
13250         rm -rf $DIR/$tfile
13251 }
13252 run_test 154a "Open-by-FID"
13253
13254 test_154b() {
13255         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13256         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13257         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13258         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
13259                 skip "Need MDS version at least 2.2.51"
13260
13261         local remote_dir=$DIR/$tdir/remote_dir
13262         local MDTIDX=1
13263         local rc=0
13264
13265         mkdir -p $DIR/$tdir
13266         $LFS mkdir -i $MDTIDX $remote_dir ||
13267                 error "create remote directory failed"
13268
13269         cp /etc/hosts $remote_dir/$tfile
13270
13271         fid=$($LFS path2fid $remote_dir/$tfile)
13272         rc=$?
13273         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
13274
13275         dot_lustre_fid_permission_check "$fid" $remote_dir ||
13276                 error "dot lustre permission check $fid failed"
13277         rm -rf $DIR/$tdir
13278 }
13279 run_test 154b "Open-by-FID for remote directory"
13280
13281 test_154c() {
13282         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13283                 skip "Need MDS version at least 2.4.1"
13284
13285         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
13286         local FID1=$($LFS path2fid $DIR/$tfile.1)
13287         local FID2=$($LFS path2fid $DIR/$tfile.2)
13288         local FID3=$($LFS path2fid $DIR/$tfile.3)
13289
13290         local N=1
13291         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
13292                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
13293                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
13294                 local want=FID$N
13295                 [ "$FID" = "${!want}" ] ||
13296                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
13297                 N=$((N + 1))
13298         done
13299
13300         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
13301         do
13302                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
13303                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
13304                 N=$((N + 1))
13305         done
13306 }
13307 run_test 154c "lfs path2fid and fid2path multiple arguments"
13308
13309 test_154d() {
13310         remote_mds_nodsh && skip "remote MDS with nodsh"
13311         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
13312                 skip "Need MDS version at least 2.5.53"
13313
13314         if remote_mds; then
13315                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
13316         else
13317                 nid="0@lo"
13318         fi
13319         local proc_ofile="mdt.*.exports.'$nid'.open_files"
13320         local fd
13321         local cmd
13322
13323         rm -f $DIR/$tfile
13324         touch $DIR/$tfile
13325
13326         local fid=$($LFS path2fid $DIR/$tfile)
13327         # Open the file
13328         fd=$(free_fd)
13329         cmd="exec $fd<$DIR/$tfile"
13330         eval $cmd
13331         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
13332         echo "$fid_list" | grep "$fid"
13333         rc=$?
13334
13335         cmd="exec $fd>/dev/null"
13336         eval $cmd
13337         if [ $rc -ne 0 ]; then
13338                 error "FID $fid not found in open files list $fid_list"
13339         fi
13340 }
13341 run_test 154d "Verify open file fid"
13342
13343 test_154e()
13344 {
13345         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
13346                 skip "Need MDS version at least 2.6.50"
13347
13348         if ls -a $MOUNT | grep -q '^\.lustre$'; then
13349                 error ".lustre returned by readdir"
13350         fi
13351 }
13352 run_test 154e ".lustre is not returned by readdir"
13353
13354 test_154f() {
13355         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13356
13357         # create parent directory on a single MDT to avoid cross-MDT hardlinks
13358         test_mkdir -p -c1 $DIR/$tdir/d
13359         # test dirs inherit from its stripe
13360         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
13361         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
13362         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
13363         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
13364         touch $DIR/f
13365
13366         # get fid of parents
13367         local FID0=$($LFS path2fid $DIR/$tdir/d)
13368         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
13369         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
13370         local FID3=$($LFS path2fid $DIR)
13371
13372         # check that path2fid --parents returns expected <parent_fid>/name
13373         # 1) test for a directory (single parent)
13374         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
13375         [ "$parent" == "$FID0/foo1" ] ||
13376                 error "expected parent: $FID0/foo1, got: $parent"
13377
13378         # 2) test for a file with nlink > 1 (multiple parents)
13379         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
13380         echo "$parent" | grep -F "$FID1/$tfile" ||
13381                 error "$FID1/$tfile not returned in parent list"
13382         echo "$parent" | grep -F "$FID2/link" ||
13383                 error "$FID2/link not returned in parent list"
13384
13385         # 3) get parent by fid
13386         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
13387         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13388         echo "$parent" | grep -F "$FID1/$tfile" ||
13389                 error "$FID1/$tfile not returned in parent list (by fid)"
13390         echo "$parent" | grep -F "$FID2/link" ||
13391                 error "$FID2/link not returned in parent list (by fid)"
13392
13393         # 4) test for entry in root directory
13394         parent=$($LFS path2fid --parents $DIR/f)
13395         echo "$parent" | grep -F "$FID3/f" ||
13396                 error "$FID3/f not returned in parent list"
13397
13398         # 5) test it on root directory
13399         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
13400                 error "$MOUNT should not have parents"
13401
13402         # enable xattr caching and check that linkea is correctly updated
13403         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
13404         save_lustre_params client "llite.*.xattr_cache" > $save
13405         lctl set_param llite.*.xattr_cache 1
13406
13407         # 6.1) linkea update on rename
13408         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
13409
13410         # get parents by fid
13411         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13412         # foo1 should no longer be returned in parent list
13413         echo "$parent" | grep -F "$FID1" &&
13414                 error "$FID1 should no longer be in parent list"
13415         # the new path should appear
13416         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
13417                 error "$FID2/$tfile.moved is not in parent list"
13418
13419         # 6.2) linkea update on unlink
13420         rm -f $DIR/$tdir/d/foo2/link
13421         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13422         # foo2/link should no longer be returned in parent list
13423         echo "$parent" | grep -F "$FID2/link" &&
13424                 error "$FID2/link should no longer be in parent list"
13425         true
13426
13427         rm -f $DIR/f
13428         restore_lustre_params < $save
13429         rm -f $save
13430 }
13431 run_test 154f "get parent fids by reading link ea"
13432
13433 test_154g()
13434 {
13435         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13436         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
13437            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
13438                 skip "Need MDS version at least 2.6.92"
13439
13440         mkdir -p $DIR/$tdir
13441         llapi_fid_test -d $DIR/$tdir
13442 }
13443 run_test 154g "various llapi FID tests"
13444
13445 test_155_small_load() {
13446     local temp=$TMP/$tfile
13447     local file=$DIR/$tfile
13448
13449     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
13450         error "dd of=$temp bs=6096 count=1 failed"
13451     cp $temp $file
13452     cancel_lru_locks $OSC
13453     cmp $temp $file || error "$temp $file differ"
13454
13455     $TRUNCATE $temp 6000
13456     $TRUNCATE $file 6000
13457     cmp $temp $file || error "$temp $file differ (truncate1)"
13458
13459     echo "12345" >>$temp
13460     echo "12345" >>$file
13461     cmp $temp $file || error "$temp $file differ (append1)"
13462
13463     echo "12345" >>$temp
13464     echo "12345" >>$file
13465     cmp $temp $file || error "$temp $file differ (append2)"
13466
13467     rm -f $temp $file
13468     true
13469 }
13470
13471 test_155_big_load() {
13472         remote_ost_nodsh && skip "remote OST with nodsh"
13473
13474         local temp=$TMP/$tfile
13475         local file=$DIR/$tfile
13476
13477         free_min_max
13478         local cache_size=$(do_facet ost$((MAXI+1)) \
13479                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
13480         local large_file_size=$((cache_size * 2))
13481
13482         echo "OSS cache size: $cache_size KB"
13483         echo "Large file size: $large_file_size KB"
13484
13485         [ $MAXV -le $large_file_size ] &&
13486                 skip_env "max available OST size needs > $large_file_size KB"
13487
13488         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
13489
13490         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
13491                 error "dd of=$temp bs=$large_file_size count=1k failed"
13492         cp $temp $file
13493         ls -lh $temp $file
13494         cancel_lru_locks osc
13495         cmp $temp $file || error "$temp $file differ"
13496
13497         rm -f $temp $file
13498         true
13499 }
13500
13501 save_writethrough() {
13502         local facets=$(get_facets OST)
13503
13504         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
13505 }
13506
13507 test_155a() {
13508         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13509
13510         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13511
13512         save_writethrough $p
13513
13514         set_cache read on
13515         set_cache writethrough on
13516         test_155_small_load
13517         restore_lustre_params < $p
13518         rm -f $p
13519 }
13520 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
13521
13522 test_155b() {
13523         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13524
13525         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13526
13527         save_writethrough $p
13528
13529         set_cache read on
13530         set_cache writethrough off
13531         test_155_small_load
13532         restore_lustre_params < $p
13533         rm -f $p
13534 }
13535 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
13536
13537 test_155c() {
13538         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13539
13540         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13541
13542         save_writethrough $p
13543
13544         set_cache read off
13545         set_cache writethrough on
13546         test_155_small_load
13547         restore_lustre_params < $p
13548         rm -f $p
13549 }
13550 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
13551
13552 test_155d() {
13553         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13554
13555         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13556
13557         save_writethrough $p
13558
13559         set_cache read off
13560         set_cache writethrough off
13561         test_155_small_load
13562         restore_lustre_params < $p
13563         rm -f $p
13564 }
13565 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
13566
13567 test_155e() {
13568         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13569
13570         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13571
13572         save_writethrough $p
13573
13574         set_cache read on
13575         set_cache writethrough on
13576         test_155_big_load
13577         restore_lustre_params < $p
13578         rm -f $p
13579 }
13580 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
13581
13582 test_155f() {
13583         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13584
13585         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13586
13587         save_writethrough $p
13588
13589         set_cache read on
13590         set_cache writethrough off
13591         test_155_big_load
13592         restore_lustre_params < $p
13593         rm -f $p
13594 }
13595 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
13596
13597 test_155g() {
13598         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13599
13600         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13601
13602         save_writethrough $p
13603
13604         set_cache read off
13605         set_cache writethrough on
13606         test_155_big_load
13607         restore_lustre_params < $p
13608         rm -f $p
13609 }
13610 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
13611
13612 test_155h() {
13613         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13614
13615         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13616
13617         save_writethrough $p
13618
13619         set_cache read off
13620         set_cache writethrough off
13621         test_155_big_load
13622         restore_lustre_params < $p
13623         rm -f $p
13624 }
13625 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
13626
13627 test_156() {
13628         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13629         remote_ost_nodsh && skip "remote OST with nodsh"
13630         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
13631                 skip "stats not implemented on old servers"
13632         [ "$ost1_FSTYPE" = "zfs" ] &&
13633                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
13634
13635         local CPAGES=3
13636         local BEFORE
13637         local AFTER
13638         local file="$DIR/$tfile"
13639         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13640
13641         save_writethrough $p
13642         roc_hit_init
13643
13644         log "Turn on read and write cache"
13645         set_cache read on
13646         set_cache writethrough on
13647
13648         log "Write data and read it back."
13649         log "Read should be satisfied from the cache."
13650         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13651         BEFORE=$(roc_hit)
13652         cancel_lru_locks osc
13653         cat $file >/dev/null
13654         AFTER=$(roc_hit)
13655         if ! let "AFTER - BEFORE == CPAGES"; then
13656                 error "NOT IN CACHE (2): before: $BEFORE, after: $AFTER"
13657         else
13658                 log "cache hits: before: $BEFORE, after: $AFTER"
13659         fi
13660
13661         log "Read again; it should be satisfied from the cache."
13662         BEFORE=$AFTER
13663         cancel_lru_locks osc
13664         cat $file >/dev/null
13665         AFTER=$(roc_hit)
13666         if ! let "AFTER - BEFORE == CPAGES"; then
13667                 error "NOT IN CACHE (3): before: $BEFORE, after: $AFTER"
13668         else
13669                 log "cache hits:: before: $BEFORE, after: $AFTER"
13670         fi
13671
13672         log "Turn off the read cache and turn on the write cache"
13673         set_cache read off
13674         set_cache writethrough on
13675
13676         log "Read again; it should be satisfied from the cache."
13677         BEFORE=$(roc_hit)
13678         cancel_lru_locks osc
13679         cat $file >/dev/null
13680         AFTER=$(roc_hit)
13681         if ! let "AFTER - BEFORE == CPAGES"; then
13682                 error "NOT IN CACHE (4): before: $BEFORE, after: $AFTER"
13683         else
13684                 log "cache hits:: before: $BEFORE, after: $AFTER"
13685         fi
13686
13687         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
13688                 # > 2.12.56 uses pagecache if cached
13689                 log "Read again; it should not be satisfied from the cache."
13690                 BEFORE=$AFTER
13691                 cancel_lru_locks osc
13692                 cat $file >/dev/null
13693                 AFTER=$(roc_hit)
13694                 if ! let "AFTER - BEFORE == 0"; then
13695                         error "IN CACHE (5): before: $BEFORE, after: $AFTER"
13696                 else
13697                         log "cache hits:: before: $BEFORE, after: $AFTER"
13698                 fi
13699         fi
13700
13701         log "Write data and read it back."
13702         log "Read should be satisfied from the cache."
13703         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13704         BEFORE=$(roc_hit)
13705         cancel_lru_locks osc
13706         cat $file >/dev/null
13707         AFTER=$(roc_hit)
13708         if ! let "AFTER - BEFORE == CPAGES"; then
13709                 error "NOT IN CACHE (6): before: $BEFORE, after: $AFTER"
13710         else
13711                 log "cache hits:: before: $BEFORE, after: $AFTER"
13712         fi
13713
13714         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
13715                 # > 2.12.56 uses pagecache if cached
13716                 log "Read again; it should not be satisfied from the cache."
13717                 BEFORE=$AFTER
13718                 cancel_lru_locks osc
13719                 cat $file >/dev/null
13720                 AFTER=$(roc_hit)
13721                 if ! let "AFTER - BEFORE == 0"; then
13722                         error "IN CACHE (7): before: $BEFORE, after: $AFTER"
13723                 else
13724                         log "cache hits:: before: $BEFORE, after: $AFTER"
13725                 fi
13726         fi
13727
13728         log "Turn off read and write cache"
13729         set_cache read off
13730         set_cache writethrough off
13731
13732         log "Write data and read it back"
13733         log "It should not be satisfied from the cache."
13734         rm -f $file
13735         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13736         cancel_lru_locks osc
13737         BEFORE=$(roc_hit)
13738         cat $file >/dev/null
13739         AFTER=$(roc_hit)
13740         if ! let "AFTER - BEFORE == 0"; then
13741                 error_ignore bz20762 "IN CACHE (8):before:$BEFORE,after:$AFTER"
13742         else
13743                 log "cache hits:: before: $BEFORE, after: $AFTER"
13744         fi
13745
13746         log "Turn on the read cache and turn off the write cache"
13747         set_cache read on
13748         set_cache writethrough off
13749
13750         log "Write data and read it back"
13751         log "It should not be satisfied from the cache."
13752         rm -f $file
13753         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13754         BEFORE=$(roc_hit)
13755         cancel_lru_locks osc
13756         cat $file >/dev/null
13757         AFTER=$(roc_hit)
13758         if ! let "AFTER - BEFORE == 0"; then
13759                 error_ignore bz20762 "IN CACHE (9):before:$BEFORE,after:$AFTER"
13760         else
13761                 log "cache hits:: before: $BEFORE, after: $AFTER"
13762         fi
13763
13764         log "Read again; it should be satisfied from the cache."
13765         BEFORE=$(roc_hit)
13766         cancel_lru_locks osc
13767         cat $file >/dev/null
13768         AFTER=$(roc_hit)
13769         if ! let "AFTER - BEFORE == CPAGES"; then
13770                 error "NOT IN CACHE (1): before: $BEFORE, after: $AFTER"
13771         else
13772                 log "cache hits:: before: $BEFORE, after: $AFTER"
13773         fi
13774
13775         restore_lustre_params < $p
13776         rm -f $p $file
13777 }
13778 run_test 156 "Verification of tunables"
13779
13780 test_160a() {
13781         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13782         remote_mds_nodsh && skip "remote MDS with nodsh"
13783         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13784                 skip "Need MDS version at least 2.2.0"
13785
13786         changelog_register || error "changelog_register failed"
13787         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13788         changelog_users $SINGLEMDS | grep -q $cl_user ||
13789                 error "User $cl_user not found in changelog_users"
13790
13791         # change something
13792         test_mkdir -p $DIR/$tdir/pics/2008/zachy
13793         changelog_clear 0 || error "changelog_clear failed"
13794         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
13795         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
13796         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
13797         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
13798         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
13799         rm $DIR/$tdir/pics/desktop.jpg
13800
13801         changelog_dump | tail -10
13802
13803         echo "verifying changelog mask"
13804         changelog_chmask "-MKDIR"
13805         changelog_chmask "-CLOSE"
13806
13807         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
13808         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
13809
13810         changelog_chmask "+MKDIR"
13811         changelog_chmask "+CLOSE"
13812
13813         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
13814         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
13815
13816         changelog_dump | tail -10
13817         MKDIRS=$(changelog_dump | grep -c "MKDIR")
13818         CLOSES=$(changelog_dump | grep -c "CLOSE")
13819         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
13820         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
13821
13822         # verify contents
13823         echo "verifying target fid"
13824         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
13825         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
13826         [ "$fidc" == "$fidf" ] ||
13827                 error "changelog '$tfile' fid $fidc != file fid $fidf"
13828         echo "verifying parent fid"
13829         # The FID returned from the Changelog may be the directory shard on
13830         # a different MDT, and not the FID returned by path2fid on the parent.
13831         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
13832         # since this is what will matter when recreating this file in the tree.
13833         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
13834         local pathp=$($LFS fid2path $MOUNT "$fidp")
13835         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
13836                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
13837
13838         echo "getting records for $cl_user"
13839         changelog_users $SINGLEMDS
13840         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
13841         local nclr=3
13842         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
13843                 error "changelog_clear failed"
13844         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
13845         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
13846         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
13847                 error "user index expect $user_rec1 + $nclr != $user_rec2"
13848
13849         local min0_rec=$(changelog_users $SINGLEMDS |
13850                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
13851         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
13852                           awk '{ print $1; exit; }')
13853
13854         changelog_dump | tail -n 5
13855         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
13856         [ $first_rec == $((min0_rec + 1)) ] ||
13857                 error "first index should be $min0_rec + 1 not $first_rec"
13858
13859         # LU-3446 changelog index reset on MDT restart
13860         local cur_rec1=$(changelog_users $SINGLEMDS |
13861                          awk '/^current.index:/ { print $NF }')
13862         changelog_clear 0 ||
13863                 error "clear all changelog records for $cl_user failed"
13864         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
13865         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
13866                 error "Fail to start $SINGLEMDS"
13867         local cur_rec2=$(changelog_users $SINGLEMDS |
13868                          awk '/^current.index:/ { print $NF }')
13869         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
13870         [ $cur_rec1 == $cur_rec2 ] ||
13871                 error "current index should be $cur_rec1 not $cur_rec2"
13872
13873         echo "verifying users from this test are deregistered"
13874         changelog_deregister || error "changelog_deregister failed"
13875         changelog_users $SINGLEMDS | grep -q $cl_user &&
13876                 error "User '$cl_user' still in changelog_users"
13877
13878         # lctl get_param -n mdd.*.changelog_users
13879         # current index: 144
13880         # ID    index (idle seconds)
13881         # cl3   144 (2)
13882         if ! changelog_users $SINGLEMDS | grep "^cl"; then
13883                 # this is the normal case where all users were deregistered
13884                 # make sure no new records are added when no users are present
13885                 local last_rec1=$(changelog_users $SINGLEMDS |
13886                                   awk '/^current.index:/ { print $NF }')
13887                 touch $DIR/$tdir/chloe
13888                 local last_rec2=$(changelog_users $SINGLEMDS |
13889                                   awk '/^current.index:/ { print $NF }')
13890                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
13891                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
13892         else
13893                 # any changelog users must be leftovers from a previous test
13894                 changelog_users $SINGLEMDS
13895                 echo "other changelog users; can't verify off"
13896         fi
13897 }
13898 run_test 160a "changelog sanity"
13899
13900 test_160b() { # LU-3587
13901         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13902         remote_mds_nodsh && skip "remote MDS with nodsh"
13903         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13904                 skip "Need MDS version at least 2.2.0"
13905
13906         changelog_register || error "changelog_register failed"
13907         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13908         changelog_users $SINGLEMDS | grep -q $cl_user ||
13909                 error "User '$cl_user' not found in changelog_users"
13910
13911         local longname1=$(str_repeat a 255)
13912         local longname2=$(str_repeat b 255)
13913
13914         cd $DIR
13915         echo "creating very long named file"
13916         touch $longname1 || error "create of '$longname1' failed"
13917         echo "renaming very long named file"
13918         mv $longname1 $longname2
13919
13920         changelog_dump | grep RENME | tail -n 5
13921         rm -f $longname2
13922 }
13923 run_test 160b "Verify that very long rename doesn't crash in changelog"
13924
13925 test_160c() {
13926         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13927         remote_mds_nodsh && skip "remote MDS with nodsh"
13928
13929         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
13930                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
13931                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
13932                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
13933
13934         local rc=0
13935
13936         # Registration step
13937         changelog_register || error "changelog_register failed"
13938
13939         rm -rf $DIR/$tdir
13940         mkdir -p $DIR/$tdir
13941         $MCREATE $DIR/$tdir/foo_160c
13942         changelog_chmask "-TRUNC"
13943         $TRUNCATE $DIR/$tdir/foo_160c 200
13944         changelog_chmask "+TRUNC"
13945         $TRUNCATE $DIR/$tdir/foo_160c 199
13946         changelog_dump | tail -n 5
13947         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
13948         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
13949 }
13950 run_test 160c "verify that changelog log catch the truncate event"
13951
13952 test_160d() {
13953         remote_mds_nodsh && skip "remote MDS with nodsh"
13954         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13955         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13956         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
13957                 skip "Need MDS version at least 2.7.60"
13958
13959         # Registration step
13960         changelog_register || error "changelog_register failed"
13961
13962         mkdir -p $DIR/$tdir/migrate_dir
13963         changelog_clear 0 || error "changelog_clear failed"
13964
13965         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
13966         changelog_dump | tail -n 5
13967         local migrates=$(changelog_dump | grep -c "MIGRT")
13968         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
13969 }
13970 run_test 160d "verify that changelog log catch the migrate event"
13971
13972 test_160e() {
13973         remote_mds_nodsh && skip "remote MDS with nodsh"
13974
13975         # Create a user
13976         changelog_register || error "changelog_register failed"
13977
13978         # Delete a future user (expect fail)
13979         local MDT0=$(facet_svc $SINGLEMDS)
13980         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
13981         local rc=$?
13982
13983         if [ $rc -eq 0 ]; then
13984                 error "Deleted non-existant user cl77"
13985         elif [ $rc -ne 2 ]; then
13986                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
13987         fi
13988
13989         # Clear to a bad index (1 billion should be safe)
13990         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
13991         rc=$?
13992
13993         if [ $rc -eq 0 ]; then
13994                 error "Successfully cleared to invalid CL index"
13995         elif [ $rc -ne 22 ]; then
13996                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
13997         fi
13998 }
13999 run_test 160e "changelog negative testing (should return errors)"
14000
14001 test_160f() {
14002         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14003         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14004                 skip "Need MDS version at least 2.10.56"
14005
14006         local mdts=$(comma_list $(mdts_nodes))
14007
14008         # Create a user
14009         changelog_register || error "first changelog_register failed"
14010         changelog_register || error "second changelog_register failed"
14011         local cl_users
14012         declare -A cl_user1
14013         declare -A cl_user2
14014         local user_rec1
14015         local user_rec2
14016         local i
14017
14018         # generate some changelog records to accumulate on each MDT
14019         # use fnv1a because created files should be evenly distributed
14020         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14021                 error "test_mkdir $tdir failed"
14022         log "$(date +%s): creating first files"
14023         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14024                 error "create $DIR/$tdir/$tfile failed"
14025
14026         # check changelogs have been generated
14027         local start=$SECONDS
14028         local idle_time=$((MDSCOUNT * 5 + 5))
14029         local nbcl=$(changelog_dump | wc -l)
14030         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14031
14032         for param in "changelog_max_idle_time=$idle_time" \
14033                      "changelog_gc=1" \
14034                      "changelog_min_gc_interval=2" \
14035                      "changelog_min_free_cat_entries=3"; do
14036                 local MDT0=$(facet_svc $SINGLEMDS)
14037                 local var="${param%=*}"
14038                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14039
14040                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14041                 do_nodes $mdts $LCTL set_param mdd.*.$param
14042         done
14043
14044         # force cl_user2 to be idle (1st part), but also cancel the
14045         # cl_user1 records so that it is not evicted later in the test.
14046         local sleep1=$((idle_time / 2))
14047         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
14048         sleep $sleep1
14049
14050         # simulate changelog catalog almost full
14051         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
14052         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
14053
14054         for i in $(seq $MDSCOUNT); do
14055                 cl_users=(${CL_USERS[mds$i]})
14056                 cl_user1[mds$i]="${cl_users[0]}"
14057                 cl_user2[mds$i]="${cl_users[1]}"
14058
14059                 [ -n "${cl_user1[mds$i]}" ] ||
14060                         error "mds$i: no user registered"
14061                 [ -n "${cl_user2[mds$i]}" ] ||
14062                         error "mds$i: only ${cl_user2[mds$i]} is registered"
14063
14064                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14065                 [ -n "$user_rec1" ] ||
14066                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14067                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14068                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14069                 [ -n "$user_rec2" ] ||
14070                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14071                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14072                      "$user_rec1 + 2 == $user_rec2"
14073                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14074                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14075                               "$user_rec1 + 2, but is $user_rec2"
14076                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14077                 [ -n "$user_rec2" ] ||
14078                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14079                 [ $user_rec1 == $user_rec2 ] ||
14080                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14081                               "$user_rec1, but is $user_rec2"
14082         done
14083
14084         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
14085         local sleep2=$((idle_time - (SECONDS - start) + 1))
14086         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
14087         sleep $sleep2
14088
14089         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
14090         # cl_user1 should be OK because it recently processed records.
14091         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
14092         createmany -m $DIR/$tdir/${tfile}b $((MDSCOUNT * 2)) ||
14093                 error "create $DIR/$tdir/${tfile}b failed"
14094
14095         # ensure gc thread is done
14096         for i in $(mdts_nodes); do
14097                 wait_update $i \
14098                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
14099                         error "$i: GC-thread not done"
14100         done
14101
14102         local first_rec
14103         for i in $(seq $MDSCOUNT); do
14104                 # check cl_user1 still registered
14105                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14106                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14107                 # check cl_user2 unregistered
14108                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14109                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14110
14111                 # check changelogs are present and starting at $user_rec1 + 1
14112                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14113                 [ -n "$user_rec1" ] ||
14114                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14115                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14116                             awk '{ print $1; exit; }')
14117
14118                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14119                 [ $((user_rec1 + 1)) == $first_rec ] ||
14120                         error "mds$i: first index should be $user_rec1 + 1, " \
14121                               "but is $first_rec"
14122         done
14123 }
14124 run_test 160f "changelog garbage collect (timestamped users)"
14125
14126 test_160g() {
14127         remote_mds_nodsh && skip "remote MDS with nodsh"
14128         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14129                 skip "Need MDS version at least 2.10.56"
14130
14131         local mdts=$(comma_list $(mdts_nodes))
14132
14133         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
14134         do_nodes $mdts $LCTL set_param fail_loc=0x1314
14135
14136         # Create a user
14137         changelog_register || error "first changelog_register failed"
14138         changelog_register || error "second changelog_register failed"
14139         local cl_users
14140         declare -A cl_user1
14141         declare -A cl_user2
14142         local user_rec1
14143         local user_rec2
14144         local i
14145
14146         # generate some changelog records to accumulate on each MDT
14147         # use fnv1a because created files should be evenly distributed
14148         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14149                 error "mkdir $tdir failed"
14150         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14151                 error "create $DIR/$tdir/$tfile failed"
14152
14153         # check changelogs have been generated
14154         local nbcl=$(changelog_dump | wc -l)
14155         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14156
14157         # reduce the max_idle_indexes value to make sure we exceed it
14158         max_ndx=$((nbcl / 2 - 1))
14159
14160         for param in "changelog_max_idle_indexes=$max_ndx" \
14161                      "changelog_gc=1" \
14162                      "changelog_min_gc_interval=2" \
14163                      "changelog_min_free_cat_entries=3"; do
14164                 local MDT0=$(facet_svc $SINGLEMDS)
14165                 local var="${param%=*}"
14166                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14167
14168                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14169                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
14170                         error "unable to set mdd.*.$param"
14171         done
14172
14173         # simulate changelog catalog almost full
14174         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
14175         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
14176
14177         for i in $(seq $MDSCOUNT); do
14178                 cl_users=(${CL_USERS[mds$i]})
14179                 cl_user1[mds$i]="${cl_users[0]}"
14180                 cl_user2[mds$i]="${cl_users[1]}"
14181
14182                 [ -n "${cl_user1[mds$i]}" ] ||
14183                         error "mds$i: no user registered"
14184                 [ -n "${cl_user2[mds$i]}" ] ||
14185                         error "mds$i: only ${cl_user1[mds$i]} is registered"
14186
14187                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14188                 [ -n "$user_rec1" ] ||
14189                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14190                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14191                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14192                 [ -n "$user_rec2" ] ||
14193                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14194                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14195                      "$user_rec1 + 2 == $user_rec2"
14196                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14197                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14198                               "$user_rec1 + 2, but is $user_rec2"
14199                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14200                 [ -n "$user_rec2" ] ||
14201                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14202                 [ $user_rec1 == $user_rec2 ] ||
14203                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14204                               "$user_rec1, but is $user_rec2"
14205         done
14206
14207         # ensure we are past the previous changelog_min_gc_interval set above
14208         sleep 2
14209
14210         # generate one more changelog to trigger fail_loc
14211         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14212                 error "create $DIR/$tdir/${tfile}bis failed"
14213
14214         # ensure gc thread is done
14215         for i in $(mdts_nodes); do
14216                 wait_update $i \
14217                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
14218                         error "$i: GC-thread not done"
14219         done
14220
14221         local first_rec
14222         for i in $(seq $MDSCOUNT); do
14223                 # check cl_user1 still registered
14224                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14225                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14226                 # check cl_user2 unregistered
14227                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14228                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14229
14230                 # check changelogs are present and starting at $user_rec1 + 1
14231                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14232                 [ -n "$user_rec1" ] ||
14233                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14234                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14235                             awk '{ print $1; exit; }')
14236
14237                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14238                 [ $((user_rec1 + 1)) == $first_rec ] ||
14239                         error "mds$i: first index should be $user_rec1 + 1, " \
14240                               "but is $first_rec"
14241         done
14242 }
14243 run_test 160g "changelog garbage collect (old users)"
14244
14245 test_160h() {
14246         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14247         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14248                 skip "Need MDS version at least 2.10.56"
14249
14250         local mdts=$(comma_list $(mdts_nodes))
14251
14252         # Create a user
14253         changelog_register || error "first changelog_register failed"
14254         changelog_register || error "second changelog_register failed"
14255         local cl_users
14256         declare -A cl_user1
14257         declare -A cl_user2
14258         local user_rec1
14259         local user_rec2
14260         local i
14261
14262         # generate some changelog records to accumulate on each MDT
14263         # use fnv1a because created files should be evenly distributed
14264         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14265                 error "test_mkdir $tdir failed"
14266         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14267                 error "create $DIR/$tdir/$tfile failed"
14268
14269         # check changelogs have been generated
14270         local nbcl=$(changelog_dump | wc -l)
14271         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14272
14273         for param in "changelog_max_idle_time=10" \
14274                      "changelog_gc=1" \
14275                      "changelog_min_gc_interval=2"; do
14276                 local MDT0=$(facet_svc $SINGLEMDS)
14277                 local var="${param%=*}"
14278                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14279
14280                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14281                 do_nodes $mdts $LCTL set_param mdd.*.$param
14282         done
14283
14284         # force cl_user2 to be idle (1st part)
14285         sleep 9
14286
14287         for i in $(seq $MDSCOUNT); do
14288                 cl_users=(${CL_USERS[mds$i]})
14289                 cl_user1[mds$i]="${cl_users[0]}"
14290                 cl_user2[mds$i]="${cl_users[1]}"
14291
14292                 [ -n "${cl_user1[mds$i]}" ] ||
14293                         error "mds$i: no user registered"
14294                 [ -n "${cl_user2[mds$i]}" ] ||
14295                         error "mds$i: only ${cl_user2[mds$i]} is registered"
14296
14297                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14298                 [ -n "$user_rec1" ] ||
14299                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14300                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14301                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14302                 [ -n "$user_rec2" ] ||
14303                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14304                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14305                      "$user_rec1 + 2 == $user_rec2"
14306                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14307                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14308                               "$user_rec1 + 2, but is $user_rec2"
14309                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14310                 [ -n "$user_rec2" ] ||
14311                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14312                 [ $user_rec1 == $user_rec2 ] ||
14313                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14314                               "$user_rec1, but is $user_rec2"
14315         done
14316
14317         # force cl_user2 to be idle (2nd part) and to reach
14318         # changelog_max_idle_time
14319         sleep 2
14320
14321         # force each GC-thread start and block then
14322         # one per MDT/MDD, set fail_val accordingly
14323         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
14324         do_nodes $mdts $LCTL set_param fail_loc=0x1316
14325
14326         # generate more changelogs to trigger fail_loc
14327         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14328                 error "create $DIR/$tdir/${tfile}bis failed"
14329
14330         # stop MDT to stop GC-thread, should be done in back-ground as it will
14331         # block waiting for the thread to be released and exit
14332         declare -A stop_pids
14333         for i in $(seq $MDSCOUNT); do
14334                 stop mds$i &
14335                 stop_pids[mds$i]=$!
14336         done
14337
14338         for i in $(mdts_nodes); do
14339                 local facet
14340                 local nb=0
14341                 local facets=$(facets_up_on_host $i)
14342
14343                 for facet in ${facets//,/ }; do
14344                         if [[ $facet == mds* ]]; then
14345                                 nb=$((nb + 1))
14346                         fi
14347                 done
14348                 # ensure each MDS's gc threads are still present and all in "R"
14349                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
14350                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
14351                         error "$i: expected $nb GC-thread"
14352                 wait_update $i \
14353                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
14354                         "R" 20 ||
14355                         error "$i: GC-thread not found in R-state"
14356                 # check umounts of each MDT on MDS have reached kthread_stop()
14357                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
14358                         error "$i: expected $nb umount"
14359                 wait_update $i \
14360                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
14361                         error "$i: umount not found in D-state"
14362         done
14363
14364         # release all GC-threads
14365         do_nodes $mdts $LCTL set_param fail_loc=0
14366
14367         # wait for MDT stop to complete
14368         for i in $(seq $MDSCOUNT); do
14369                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
14370         done
14371
14372         # XXX
14373         # may try to check if any orphan changelog records are present
14374         # via ldiskfs/zfs and llog_reader...
14375
14376         # re-start/mount MDTs
14377         for i in $(seq $MDSCOUNT); do
14378                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
14379                         error "Fail to start mds$i"
14380         done
14381
14382         local first_rec
14383         for i in $(seq $MDSCOUNT); do
14384                 # check cl_user1 still registered
14385                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14386                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14387                 # check cl_user2 unregistered
14388                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14389                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14390
14391                 # check changelogs are present and starting at $user_rec1 + 1
14392                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14393                 [ -n "$user_rec1" ] ||
14394                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14395                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14396                             awk '{ print $1; exit; }')
14397
14398                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14399                 [ $((user_rec1 + 1)) == $first_rec ] ||
14400                         error "mds$i: first index should be $user_rec1 + 1, " \
14401                               "but is $first_rec"
14402         done
14403 }
14404 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
14405               "during mount"
14406
14407 test_160i() {
14408
14409         local mdts=$(comma_list $(mdts_nodes))
14410
14411         changelog_register || error "first changelog_register failed"
14412
14413         # generate some changelog records to accumulate on each MDT
14414         # use fnv1a because created files should be evenly distributed
14415         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14416                 error "mkdir $tdir failed"
14417         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14418                 error "create $DIR/$tdir/$tfile failed"
14419
14420         # check changelogs have been generated
14421         local nbcl=$(changelog_dump | wc -l)
14422         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14423
14424         # simulate race between register and unregister
14425         # XXX as fail_loc is set per-MDS, with DNE configs the race
14426         # simulation will only occur for one MDT per MDS and for the
14427         # others the normal race scenario will take place
14428         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
14429         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
14430         do_nodes $mdts $LCTL set_param fail_val=1
14431
14432         # unregister 1st user
14433         changelog_deregister &
14434         local pid1=$!
14435         # wait some time for deregister work to reach race rdv
14436         sleep 2
14437         # register 2nd user
14438         changelog_register || error "2nd user register failed"
14439
14440         wait $pid1 || error "1st user deregister failed"
14441
14442         local i
14443         local last_rec
14444         declare -A LAST_REC
14445         for i in $(seq $MDSCOUNT); do
14446                 if changelog_users mds$i | grep "^cl"; then
14447                         # make sure new records are added with one user present
14448                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
14449                                           awk '/^current.index:/ { print $NF }')
14450                 else
14451                         error "mds$i has no user registered"
14452                 fi
14453         done
14454
14455         # generate more changelog records to accumulate on each MDT
14456         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14457                 error "create $DIR/$tdir/${tfile}bis failed"
14458
14459         for i in $(seq $MDSCOUNT); do
14460                 last_rec=$(changelog_users $SINGLEMDS |
14461                            awk '/^current.index:/ { print $NF }')
14462                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
14463                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
14464                         error "changelogs are off on mds$i"
14465         done
14466 }
14467 run_test 160i "changelog user register/unregister race"
14468
14469 test_160j() {
14470         remote_mds_nodsh && skip "remote MDS with nodsh"
14471         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
14472                 skip "Need MDS version at least 2.12.56"
14473
14474         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
14475         stack_trap "umount $MOUNT2" EXIT
14476
14477         changelog_register || error "first changelog_register failed"
14478         stack_trap "changelog_deregister" EXIT
14479
14480         # generate some changelog
14481         # use fnv1a because created files should be evenly distributed
14482         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14483                 error "mkdir $tdir failed"
14484         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14485                 error "create $DIR/$tdir/${tfile}bis failed"
14486
14487         # open the changelog device
14488         exec 3>/dev/changelog-$FSNAME-MDT0000
14489         stack_trap "exec 3>&-" EXIT
14490         exec 4</dev/changelog-$FSNAME-MDT0000
14491         stack_trap "exec 4<&-" EXIT
14492
14493         # umount the first lustre mount
14494         umount $MOUNT
14495         stack_trap "mount_client $MOUNT" EXIT
14496
14497         # read changelog
14498         cat <&4 >/dev/null || error "read changelog failed"
14499
14500         # clear changelog
14501         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14502         changelog_users $SINGLEMDS | grep -q $cl_user ||
14503                 error "User $cl_user not found in changelog_users"
14504
14505         printf 'clear:'$cl_user':0' >&3
14506 }
14507 run_test 160j "client can be umounted  while its chanangelog is being used"
14508
14509 test_160k() {
14510         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14511         remote_mds_nodsh && skip "remote MDS with nodsh"
14512
14513         mkdir -p $DIR/$tdir/1/1
14514
14515         changelog_register || error "changelog_register failed"
14516         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14517
14518         changelog_users $SINGLEMDS | grep -q $cl_user ||
14519                 error "User '$cl_user' not found in changelog_users"
14520 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
14521         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
14522         rmdir $DIR/$tdir/1/1 & sleep 1
14523         mkdir $DIR/$tdir/2
14524         touch $DIR/$tdir/2/2
14525         rm -rf $DIR/$tdir/2
14526
14527         wait
14528         sleep 4
14529
14530         changelog_dump | grep rmdir || error "rmdir not recorded"
14531
14532         rm -rf $DIR/$tdir
14533         changelog_deregister
14534 }
14535 run_test 160k "Verify that changelog records are not lost"
14536
14537 test_161a() {
14538         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14539
14540         test_mkdir -c1 $DIR/$tdir
14541         cp /etc/hosts $DIR/$tdir/$tfile
14542         test_mkdir -c1 $DIR/$tdir/foo1
14543         test_mkdir -c1 $DIR/$tdir/foo2
14544         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
14545         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
14546         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
14547         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
14548         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
14549         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14550                 $LFS fid2path $DIR $FID
14551                 error "bad link ea"
14552         fi
14553         # middle
14554         rm $DIR/$tdir/foo2/zachary
14555         # last
14556         rm $DIR/$tdir/foo2/thor
14557         # first
14558         rm $DIR/$tdir/$tfile
14559         # rename
14560         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
14561         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
14562                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
14563         rm $DIR/$tdir/foo2/maggie
14564
14565         # overflow the EA
14566         local longname=$tfile.avg_len_is_thirty_two_
14567         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
14568                 error_noexit 'failed to unlink many hardlinks'" EXIT
14569         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
14570                 error "failed to hardlink many files"
14571         links=$($LFS fid2path $DIR $FID | wc -l)
14572         echo -n "${links}/1000 links in link EA"
14573         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
14574 }
14575 run_test 161a "link ea sanity"
14576
14577 test_161b() {
14578         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14579         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
14580
14581         local MDTIDX=1
14582         local remote_dir=$DIR/$tdir/remote_dir
14583
14584         mkdir -p $DIR/$tdir
14585         $LFS mkdir -i $MDTIDX $remote_dir ||
14586                 error "create remote directory failed"
14587
14588         cp /etc/hosts $remote_dir/$tfile
14589         mkdir -p $remote_dir/foo1
14590         mkdir -p $remote_dir/foo2
14591         ln $remote_dir/$tfile $remote_dir/foo1/sofia
14592         ln $remote_dir/$tfile $remote_dir/foo2/zachary
14593         ln $remote_dir/$tfile $remote_dir/foo1/luna
14594         ln $remote_dir/$tfile $remote_dir/foo2/thor
14595
14596         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
14597                      tr -d ']')
14598         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14599                 $LFS fid2path $DIR $FID
14600                 error "bad link ea"
14601         fi
14602         # middle
14603         rm $remote_dir/foo2/zachary
14604         # last
14605         rm $remote_dir/foo2/thor
14606         # first
14607         rm $remote_dir/$tfile
14608         # rename
14609         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
14610         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
14611         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
14612                 $LFS fid2path $DIR $FID
14613                 error "bad link rename"
14614         fi
14615         rm $remote_dir/foo2/maggie
14616
14617         # overflow the EA
14618         local longname=filename_avg_len_is_thirty_two_
14619         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
14620                 error "failed to hardlink many files"
14621         links=$($LFS fid2path $DIR $FID | wc -l)
14622         echo -n "${links}/1000 links in link EA"
14623         [[ ${links} -gt 60 ]] ||
14624                 error "expected at least 60 links in link EA"
14625         unlinkmany $remote_dir/foo2/$longname 1000 ||
14626         error "failed to unlink many hardlinks"
14627 }
14628 run_test 161b "link ea sanity under remote directory"
14629
14630 test_161c() {
14631         remote_mds_nodsh && skip "remote MDS with nodsh"
14632         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14633         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
14634                 skip "Need MDS version at least 2.1.5"
14635
14636         # define CLF_RENAME_LAST 0x0001
14637         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
14638         changelog_register || error "changelog_register failed"
14639
14640         rm -rf $DIR/$tdir
14641         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
14642         touch $DIR/$tdir/foo_161c
14643         touch $DIR/$tdir/bar_161c
14644         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14645         changelog_dump | grep RENME | tail -n 5
14646         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14647         changelog_clear 0 || error "changelog_clear failed"
14648         if [ x$flags != "x0x1" ]; then
14649                 error "flag $flags is not 0x1"
14650         fi
14651
14652         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
14653         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
14654         touch $DIR/$tdir/foo_161c
14655         touch $DIR/$tdir/bar_161c
14656         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14657         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14658         changelog_dump | grep RENME | tail -n 5
14659         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14660         changelog_clear 0 || error "changelog_clear failed"
14661         if [ x$flags != "x0x0" ]; then
14662                 error "flag $flags is not 0x0"
14663         fi
14664         echo "rename overwrite a target having nlink > 1," \
14665                 "changelog record has flags of $flags"
14666
14667         # rename doesn't overwrite a target (changelog flag 0x0)
14668         touch $DIR/$tdir/foo_161c
14669         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
14670         changelog_dump | grep RENME | tail -n 5
14671         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
14672         changelog_clear 0 || error "changelog_clear failed"
14673         if [ x$flags != "x0x0" ]; then
14674                 error "flag $flags is not 0x0"
14675         fi
14676         echo "rename doesn't overwrite a target," \
14677                 "changelog record has flags of $flags"
14678
14679         # define CLF_UNLINK_LAST 0x0001
14680         # unlink a file having nlink = 1 (changelog flag 0x1)
14681         rm -f $DIR/$tdir/foo2_161c
14682         changelog_dump | grep UNLNK | tail -n 5
14683         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14684         changelog_clear 0 || error "changelog_clear failed"
14685         if [ x$flags != "x0x1" ]; then
14686                 error "flag $flags is not 0x1"
14687         fi
14688         echo "unlink a file having nlink = 1," \
14689                 "changelog record has flags of $flags"
14690
14691         # unlink a file having nlink > 1 (changelog flag 0x0)
14692         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14693         rm -f $DIR/$tdir/foobar_161c
14694         changelog_dump | grep UNLNK | tail -n 5
14695         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14696         changelog_clear 0 || error "changelog_clear failed"
14697         if [ x$flags != "x0x0" ]; then
14698                 error "flag $flags is not 0x0"
14699         fi
14700         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
14701 }
14702 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
14703
14704 test_161d() {
14705         remote_mds_nodsh && skip "remote MDS with nodsh"
14706         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
14707
14708         local pid
14709         local fid
14710
14711         changelog_register || error "changelog_register failed"
14712
14713         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
14714         # interfer with $MOUNT/.lustre/fid/ access
14715         mkdir $DIR/$tdir
14716         [[ $? -eq 0 ]] || error "mkdir failed"
14717
14718         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
14719         $LCTL set_param fail_loc=0x8000140c
14720         # 5s pause
14721         $LCTL set_param fail_val=5
14722
14723         # create file
14724         echo foofoo > $DIR/$tdir/$tfile &
14725         pid=$!
14726
14727         # wait for create to be delayed
14728         sleep 2
14729
14730         ps -p $pid
14731         [[ $? -eq 0 ]] || error "create should be blocked"
14732
14733         local tempfile="$(mktemp --tmpdir $tfile.XXXXXX)"
14734         stack_trap "rm -f $tempfile"
14735         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
14736         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
14737         # some delay may occur during ChangeLog publishing and file read just
14738         # above, that could allow file write to happen finally
14739         [[ -s $tempfile ]] && echo "file should be empty"
14740
14741         $LCTL set_param fail_loc=0
14742
14743         wait $pid
14744         [[ $? -eq 0 ]] || error "create failed"
14745 }
14746 run_test 161d "create with concurrent .lustre/fid access"
14747
14748 check_path() {
14749         local expected="$1"
14750         shift
14751         local fid="$2"
14752
14753         local path
14754         path=$($LFS fid2path "$@")
14755         local rc=$?
14756
14757         if [ $rc -ne 0 ]; then
14758                 error "path looked up of '$expected' failed: rc=$rc"
14759         elif [ "$path" != "$expected" ]; then
14760                 error "path looked up '$path' instead of '$expected'"
14761         else
14762                 echo "FID '$fid' resolves to path '$path' as expected"
14763         fi
14764 }
14765
14766 test_162a() { # was test_162
14767         test_mkdir -p -c1 $DIR/$tdir/d2
14768         touch $DIR/$tdir/d2/$tfile
14769         touch $DIR/$tdir/d2/x1
14770         touch $DIR/$tdir/d2/x2
14771         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
14772         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
14773         # regular file
14774         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
14775         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
14776
14777         # softlink
14778         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
14779         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
14780         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
14781
14782         # softlink to wrong file
14783         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
14784         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
14785         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
14786
14787         # hardlink
14788         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
14789         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
14790         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
14791         # fid2path dir/fsname should both work
14792         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
14793         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
14794
14795         # hardlink count: check that there are 2 links
14796         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
14797         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
14798
14799         # hardlink indexing: remove the first link
14800         rm $DIR/$tdir/d2/p/q/r/hlink
14801         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
14802 }
14803 run_test 162a "path lookup sanity"
14804
14805 test_162b() {
14806         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14807         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14808
14809         mkdir $DIR/$tdir
14810         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
14811                                 error "create striped dir failed"
14812
14813         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
14814                                         tail -n 1 | awk '{print $2}')
14815         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
14816
14817         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
14818         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
14819
14820         # regular file
14821         for ((i=0;i<5;i++)); do
14822                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
14823                         error "get fid for f$i failed"
14824                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
14825
14826                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
14827                         error "get fid for d$i failed"
14828                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
14829         done
14830
14831         return 0
14832 }
14833 run_test 162b "striped directory path lookup sanity"
14834
14835 # LU-4239: Verify fid2path works with paths 100 or more directories deep
14836 test_162c() {
14837         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
14838                 skip "Need MDS version at least 2.7.51"
14839
14840         local lpath=$tdir.local
14841         local rpath=$tdir.remote
14842
14843         test_mkdir $DIR/$lpath
14844         test_mkdir $DIR/$rpath
14845
14846         for ((i = 0; i <= 101; i++)); do
14847                 lpath="$lpath/$i"
14848                 mkdir $DIR/$lpath
14849                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
14850                         error "get fid for local directory $DIR/$lpath failed"
14851                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
14852
14853                 rpath="$rpath/$i"
14854                 test_mkdir $DIR/$rpath
14855                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
14856                         error "get fid for remote directory $DIR/$rpath failed"
14857                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
14858         done
14859
14860         return 0
14861 }
14862 run_test 162c "fid2path works with paths 100 or more directories deep"
14863
14864 test_169() {
14865         # do directio so as not to populate the page cache
14866         log "creating a 10 Mb file"
14867         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
14868         log "starting reads"
14869         dd if=$DIR/$tfile of=/dev/null bs=4096 &
14870         log "truncating the file"
14871         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
14872         log "killing dd"
14873         kill %+ || true # reads might have finished
14874         echo "wait until dd is finished"
14875         wait
14876         log "removing the temporary file"
14877         rm -rf $DIR/$tfile || error "tmp file removal failed"
14878 }
14879 run_test 169 "parallel read and truncate should not deadlock"
14880
14881 test_170() {
14882         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14883
14884         $LCTL clear     # bug 18514
14885         $LCTL debug_daemon start $TMP/${tfile}_log_good
14886         touch $DIR/$tfile
14887         $LCTL debug_daemon stop
14888         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
14889                 error "sed failed to read log_good"
14890
14891         $LCTL debug_daemon start $TMP/${tfile}_log_good
14892         rm -rf $DIR/$tfile
14893         $LCTL debug_daemon stop
14894
14895         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
14896                error "lctl df log_bad failed"
14897
14898         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14899         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14900
14901         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
14902         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
14903
14904         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
14905                 error "bad_line good_line1 good_line2 are empty"
14906
14907         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14908         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
14909         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14910
14911         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
14912         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14913         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14914
14915         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
14916                 error "bad_line_new good_line_new are empty"
14917
14918         local expected_good=$((good_line1 + good_line2*2))
14919
14920         rm -f $TMP/${tfile}*
14921         # LU-231, short malformed line may not be counted into bad lines
14922         if [ $bad_line -ne $bad_line_new ] &&
14923                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
14924                 error "expected $bad_line bad lines, but got $bad_line_new"
14925                 return 1
14926         fi
14927
14928         if [ $expected_good -ne $good_line_new ]; then
14929                 error "expected $expected_good good lines, but got $good_line_new"
14930                 return 2
14931         fi
14932         true
14933 }
14934 run_test 170 "test lctl df to handle corrupted log ====================="
14935
14936 test_171() { # bug20592
14937         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14938
14939         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
14940         $LCTL set_param fail_loc=0x50e
14941         $LCTL set_param fail_val=3000
14942         multiop_bg_pause $DIR/$tfile O_s || true
14943         local MULTIPID=$!
14944         kill -USR1 $MULTIPID
14945         # cause log dump
14946         sleep 3
14947         wait $MULTIPID
14948         if dmesg | grep "recursive fault"; then
14949                 error "caught a recursive fault"
14950         fi
14951         $LCTL set_param fail_loc=0
14952         true
14953 }
14954 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
14955
14956 # it would be good to share it with obdfilter-survey/iokit-libecho code
14957 setup_obdecho_osc () {
14958         local rc=0
14959         local ost_nid=$1
14960         local obdfilter_name=$2
14961         echo "Creating new osc for $obdfilter_name on $ost_nid"
14962         # make sure we can find loopback nid
14963         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
14964
14965         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
14966                            ${obdfilter_name}_osc_UUID || rc=2; }
14967         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
14968                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
14969         return $rc
14970 }
14971
14972 cleanup_obdecho_osc () {
14973         local obdfilter_name=$1
14974         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
14975         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
14976         return 0
14977 }
14978
14979 obdecho_test() {
14980         local OBD=$1
14981         local node=$2
14982         local pages=${3:-64}
14983         local rc=0
14984         local id
14985
14986         local count=10
14987         local obd_size=$(get_obd_size $node $OBD)
14988         local page_size=$(get_page_size $node)
14989         if [[ -n "$obd_size" ]]; then
14990                 local new_count=$((obd_size / (pages * page_size / 1024)))
14991                 [[ $new_count -ge $count ]] || count=$new_count
14992         fi
14993
14994         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
14995         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
14996                            rc=2; }
14997         if [ $rc -eq 0 ]; then
14998             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
14999             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
15000         fi
15001         echo "New object id is $id"
15002         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
15003                            rc=4; }
15004         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
15005                            "test_brw $count w v $pages $id" || rc=4; }
15006         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
15007                            rc=4; }
15008         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
15009                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
15010         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
15011                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
15012         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
15013         return $rc
15014 }
15015
15016 test_180a() {
15017         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15018
15019         if ! module_loaded obdecho; then
15020                 load_module obdecho/obdecho &&
15021                         stack_trap "rmmod obdecho" EXIT ||
15022                         error "unable to load obdecho on client"
15023         fi
15024
15025         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
15026         local host=$($LCTL get_param -n osc.$osc.import |
15027                      awk '/current_connection:/ { print $2 }' )
15028         local target=$($LCTL get_param -n osc.$osc.import |
15029                        awk '/target:/ { print $2 }' )
15030         target=${target%_UUID}
15031
15032         if [ -n "$target" ]; then
15033                 setup_obdecho_osc $host $target &&
15034                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
15035                         { error "obdecho setup failed with $?"; return; }
15036
15037                 obdecho_test ${target}_osc client ||
15038                         error "obdecho_test failed on ${target}_osc"
15039         else
15040                 $LCTL get_param osc.$osc.import
15041                 error "there is no osc.$osc.import target"
15042         fi
15043 }
15044 run_test 180a "test obdecho on osc"
15045
15046 test_180b() {
15047         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15048         remote_ost_nodsh && skip "remote OST with nodsh"
15049
15050         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
15051                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
15052                 error "failed to load module obdecho"
15053
15054         local target=$(do_facet ost1 $LCTL dl |
15055                        awk '/obdfilter/ { print $4; exit; }')
15056
15057         if [ -n "$target" ]; then
15058                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
15059         else
15060                 do_facet ost1 $LCTL dl
15061                 error "there is no obdfilter target on ost1"
15062         fi
15063 }
15064 run_test 180b "test obdecho directly on obdfilter"
15065
15066 test_180c() { # LU-2598
15067         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15068         remote_ost_nodsh && skip "remote OST with nodsh"
15069         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
15070                 skip "Need MDS version at least 2.4.0"
15071
15072         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
15073                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
15074                 error "failed to load module obdecho"
15075
15076         local target=$(do_facet ost1 $LCTL dl |
15077                        awk '/obdfilter/ { print $4; exit; }')
15078
15079         if [ -n "$target" ]; then
15080                 local pages=16384 # 64MB bulk I/O RPC size
15081
15082                 obdecho_test "$target" ost1 "$pages" ||
15083                         error "obdecho_test with pages=$pages failed with $?"
15084         else
15085                 do_facet ost1 $LCTL dl
15086                 error "there is no obdfilter target on ost1"
15087         fi
15088 }
15089 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
15090
15091 test_181() { # bug 22177
15092         test_mkdir $DIR/$tdir
15093         # create enough files to index the directory
15094         createmany -o $DIR/$tdir/foobar 4000
15095         # print attributes for debug purpose
15096         lsattr -d .
15097         # open dir
15098         multiop_bg_pause $DIR/$tdir D_Sc || return 1
15099         MULTIPID=$!
15100         # remove the files & current working dir
15101         unlinkmany $DIR/$tdir/foobar 4000
15102         rmdir $DIR/$tdir
15103         kill -USR1 $MULTIPID
15104         wait $MULTIPID
15105         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
15106         return 0
15107 }
15108 run_test 181 "Test open-unlinked dir ========================"
15109
15110 test_182() {
15111         local fcount=1000
15112         local tcount=10
15113
15114         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15115
15116         $LCTL set_param mdc.*.rpc_stats=clear
15117
15118         for (( i = 0; i < $tcount; i++ )) ; do
15119                 mkdir $DIR/$tdir/$i
15120         done
15121
15122         for (( i = 0; i < $tcount; i++ )) ; do
15123                 createmany -o $DIR/$tdir/$i/f- $fcount &
15124         done
15125         wait
15126
15127         for (( i = 0; i < $tcount; i++ )) ; do
15128                 unlinkmany $DIR/$tdir/$i/f- $fcount &
15129         done
15130         wait
15131
15132         $LCTL get_param mdc.*.rpc_stats
15133
15134         rm -rf $DIR/$tdir
15135 }
15136 run_test 182 "Test parallel modify metadata operations ================"
15137
15138 test_183() { # LU-2275
15139         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15140         remote_mds_nodsh && skip "remote MDS with nodsh"
15141         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
15142                 skip "Need MDS version at least 2.3.56"
15143
15144         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15145         echo aaa > $DIR/$tdir/$tfile
15146
15147 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
15148         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
15149
15150         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
15151         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
15152
15153         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
15154
15155         # Flush negative dentry cache
15156         touch $DIR/$tdir/$tfile
15157
15158         # We are not checking for any leaked references here, they'll
15159         # become evident next time we do cleanup with module unload.
15160         rm -rf $DIR/$tdir
15161 }
15162 run_test 183 "No crash or request leak in case of strange dispositions ========"
15163
15164 # test suite 184 is for LU-2016, LU-2017
15165 test_184a() {
15166         check_swap_layouts_support
15167
15168         dir0=$DIR/$tdir/$testnum
15169         test_mkdir -p -c1 $dir0
15170         ref1=/etc/passwd
15171         ref2=/etc/group
15172         file1=$dir0/f1
15173         file2=$dir0/f2
15174         $LFS setstripe -c1 $file1
15175         cp $ref1 $file1
15176         $LFS setstripe -c2 $file2
15177         cp $ref2 $file2
15178         gen1=$($LFS getstripe -g $file1)
15179         gen2=$($LFS getstripe -g $file2)
15180
15181         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
15182         gen=$($LFS getstripe -g $file1)
15183         [[ $gen1 != $gen ]] ||
15184                 "Layout generation on $file1 does not change"
15185         gen=$($LFS getstripe -g $file2)
15186         [[ $gen2 != $gen ]] ||
15187                 "Layout generation on $file2 does not change"
15188
15189         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
15190         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
15191
15192         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
15193 }
15194 run_test 184a "Basic layout swap"
15195
15196 test_184b() {
15197         check_swap_layouts_support
15198
15199         dir0=$DIR/$tdir/$testnum
15200         mkdir -p $dir0 || error "creating dir $dir0"
15201         file1=$dir0/f1
15202         file2=$dir0/f2
15203         file3=$dir0/f3
15204         dir1=$dir0/d1
15205         dir2=$dir0/d2
15206         mkdir $dir1 $dir2
15207         $LFS setstripe -c1 $file1
15208         $LFS setstripe -c2 $file2
15209         $LFS setstripe -c1 $file3
15210         chown $RUNAS_ID $file3
15211         gen1=$($LFS getstripe -g $file1)
15212         gen2=$($LFS getstripe -g $file2)
15213
15214         $LFS swap_layouts $dir1 $dir2 &&
15215                 error "swap of directories layouts should fail"
15216         $LFS swap_layouts $dir1 $file1 &&
15217                 error "swap of directory and file layouts should fail"
15218         $RUNAS $LFS swap_layouts $file1 $file2 &&
15219                 error "swap of file we cannot write should fail"
15220         $LFS swap_layouts $file1 $file3 &&
15221                 error "swap of file with different owner should fail"
15222         /bin/true # to clear error code
15223 }
15224 run_test 184b "Forbidden layout swap (will generate errors)"
15225
15226 test_184c() {
15227         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
15228         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
15229         check_swap_layouts_support
15230
15231         local dir0=$DIR/$tdir/$testnum
15232         mkdir -p $dir0 || error "creating dir $dir0"
15233
15234         local ref1=$dir0/ref1
15235         local ref2=$dir0/ref2
15236         local file1=$dir0/file1
15237         local file2=$dir0/file2
15238         # create a file large enough for the concurrent test
15239         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
15240         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
15241         echo "ref file size: ref1($(stat -c %s $ref1))," \
15242              "ref2($(stat -c %s $ref2))"
15243
15244         cp $ref2 $file2
15245         dd if=$ref1 of=$file1 bs=16k &
15246         local DD_PID=$!
15247
15248         # Make sure dd starts to copy file
15249         while [ ! -f $file1 ]; do sleep 0.1; done
15250
15251         $LFS swap_layouts $file1 $file2
15252         local rc=$?
15253         wait $DD_PID
15254         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
15255         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
15256
15257         # how many bytes copied before swapping layout
15258         local copied=$(stat -c %s $file2)
15259         local remaining=$(stat -c %s $ref1)
15260         remaining=$((remaining - copied))
15261         echo "Copied $copied bytes before swapping layout..."
15262
15263         cmp -n $copied $file1 $ref2 | grep differ &&
15264                 error "Content mismatch [0, $copied) of ref2 and file1"
15265         cmp -n $copied $file2 $ref1 ||
15266                 error "Content mismatch [0, $copied) of ref1 and file2"
15267         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
15268                 error "Content mismatch [$copied, EOF) of ref1 and file1"
15269
15270         # clean up
15271         rm -f $ref1 $ref2 $file1 $file2
15272 }
15273 run_test 184c "Concurrent write and layout swap"
15274
15275 test_184d() {
15276         check_swap_layouts_support
15277         [ -z "$(which getfattr 2>/dev/null)" ] &&
15278                 skip_env "no getfattr command"
15279
15280         local file1=$DIR/$tdir/$tfile-1
15281         local file2=$DIR/$tdir/$tfile-2
15282         local file3=$DIR/$tdir/$tfile-3
15283         local lovea1
15284         local lovea2
15285
15286         mkdir -p $DIR/$tdir
15287         touch $file1 || error "create $file1 failed"
15288         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15289                 error "create $file2 failed"
15290         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15291                 error "create $file3 failed"
15292         lovea1=$(get_layout_param $file1)
15293
15294         $LFS swap_layouts $file2 $file3 ||
15295                 error "swap $file2 $file3 layouts failed"
15296         $LFS swap_layouts $file1 $file2 ||
15297                 error "swap $file1 $file2 layouts failed"
15298
15299         lovea2=$(get_layout_param $file2)
15300         echo "$lovea1"
15301         echo "$lovea2"
15302         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
15303
15304         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15305         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
15306 }
15307 run_test 184d "allow stripeless layouts swap"
15308
15309 test_184e() {
15310         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
15311                 skip "Need MDS version at least 2.6.94"
15312         check_swap_layouts_support
15313         [ -z "$(which getfattr 2>/dev/null)" ] &&
15314                 skip_env "no getfattr command"
15315
15316         local file1=$DIR/$tdir/$tfile-1
15317         local file2=$DIR/$tdir/$tfile-2
15318         local file3=$DIR/$tdir/$tfile-3
15319         local lovea
15320
15321         mkdir -p $DIR/$tdir
15322         touch $file1 || error "create $file1 failed"
15323         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15324                 error "create $file2 failed"
15325         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15326                 error "create $file3 failed"
15327
15328         $LFS swap_layouts $file1 $file2 ||
15329                 error "swap $file1 $file2 layouts failed"
15330
15331         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15332         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
15333
15334         echo 123 > $file1 || error "Should be able to write into $file1"
15335
15336         $LFS swap_layouts $file1 $file3 ||
15337                 error "swap $file1 $file3 layouts failed"
15338
15339         echo 123 > $file1 || error "Should be able to write into $file1"
15340
15341         rm -rf $file1 $file2 $file3
15342 }
15343 run_test 184e "Recreate layout after stripeless layout swaps"
15344
15345 test_184f() {
15346         # Create a file with name longer than sizeof(struct stat) ==
15347         # 144 to see if we can get chars from the file name to appear
15348         # in the returned striping. Note that 'f' == 0x66.
15349         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
15350
15351         mkdir -p $DIR/$tdir
15352         mcreate $DIR/$tdir/$file
15353         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
15354                 error "IOC_MDC_GETFILEINFO returned garbage striping"
15355         fi
15356 }
15357 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
15358
15359 test_185() { # LU-2441
15360         # LU-3553 - no volatile file support in old servers
15361         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
15362                 skip "Need MDS version at least 2.3.60"
15363
15364         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15365         touch $DIR/$tdir/spoo
15366         local mtime1=$(stat -c "%Y" $DIR/$tdir)
15367         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
15368                 error "cannot create/write a volatile file"
15369         [ "$FILESET" == "" ] &&
15370         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
15371                 error "FID is still valid after close"
15372
15373         multiop_bg_pause $DIR/$tdir vVw4096_c
15374         local multi_pid=$!
15375
15376         local OLD_IFS=$IFS
15377         IFS=":"
15378         local fidv=($fid)
15379         IFS=$OLD_IFS
15380         # assume that the next FID for this client is sequential, since stdout
15381         # is unfortunately eaten by multiop_bg_pause
15382         local n=$((${fidv[1]} + 1))
15383         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
15384         if [ "$FILESET" == "" ]; then
15385                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
15386                         error "FID is missing before close"
15387         fi
15388         kill -USR1 $multi_pid
15389         # 1 second delay, so if mtime change we will see it
15390         sleep 1
15391         local mtime2=$(stat -c "%Y" $DIR/$tdir)
15392         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
15393 }
15394 run_test 185 "Volatile file support"
15395
15396 function create_check_volatile() {
15397         local idx=$1
15398         local tgt
15399
15400         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
15401         local PID=$!
15402         sleep 1
15403         local FID=$(cat /tmp/${tfile}.fid)
15404         [ "$FID" == "" ] && error "can't get FID for volatile"
15405         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
15406         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
15407         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
15408         kill -USR1 $PID
15409         wait
15410         sleep 1
15411         cancel_lru_locks mdc # flush opencache
15412         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
15413         return 0
15414 }
15415
15416 test_185a(){
15417         # LU-12516 - volatile creation via .lustre
15418         [[ $MDS1_VERSION -ge $(version_code 2.12.55) ]] ||
15419                 skip "Need MDS version at least 2.3.55"
15420
15421         create_check_volatile 0
15422         [ $MDSCOUNT -lt 2 ] && return 0
15423
15424         # DNE case
15425         create_check_volatile 1
15426
15427         return 0
15428 }
15429 run_test 185a "Volatile file creation in .lustre/fid/"
15430
15431 test_187a() {
15432         remote_mds_nodsh && skip "remote MDS with nodsh"
15433         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15434                 skip "Need MDS version at least 2.3.0"
15435
15436         local dir0=$DIR/$tdir/$testnum
15437         mkdir -p $dir0 || error "creating dir $dir0"
15438
15439         local file=$dir0/file1
15440         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
15441         local dv1=$($LFS data_version $file)
15442         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
15443         local dv2=$($LFS data_version $file)
15444         [[ $dv1 != $dv2 ]] ||
15445                 error "data version did not change on write $dv1 == $dv2"
15446
15447         # clean up
15448         rm -f $file1
15449 }
15450 run_test 187a "Test data version change"
15451
15452 test_187b() {
15453         remote_mds_nodsh && skip "remote MDS with nodsh"
15454         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15455                 skip "Need MDS version at least 2.3.0"
15456
15457         local dir0=$DIR/$tdir/$testnum
15458         mkdir -p $dir0 || error "creating dir $dir0"
15459
15460         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
15461         [[ ${DV[0]} != ${DV[1]} ]] ||
15462                 error "data version did not change on write"\
15463                       " ${DV[0]} == ${DV[1]}"
15464
15465         # clean up
15466         rm -f $file1
15467 }
15468 run_test 187b "Test data version change on volatile file"
15469
15470 test_200() {
15471         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15472         remote_mgs_nodsh && skip "remote MGS with nodsh"
15473         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15474
15475         local POOL=${POOL:-cea1}
15476         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
15477         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
15478         # Pool OST targets
15479         local first_ost=0
15480         local last_ost=$(($OSTCOUNT - 1))
15481         local ost_step=2
15482         local ost_list=$(seq $first_ost $ost_step $last_ost)
15483         local ost_range="$first_ost $last_ost $ost_step"
15484         local test_path=$POOL_ROOT/$POOL_DIR_NAME
15485         local file_dir=$POOL_ROOT/file_tst
15486         local subdir=$test_path/subdir
15487         local rc=0
15488
15489         while : ; do
15490                 # former test_200a test_200b
15491                 pool_add $POOL                          || { rc=$? ; break; }
15492                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
15493                 # former test_200c test_200d
15494                 mkdir -p $test_path
15495                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
15496                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
15497                 mkdir -p $subdir
15498                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
15499                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
15500                                                         || { rc=$? ; break; }
15501                 # former test_200e test_200f
15502                 local files=$((OSTCOUNT*3))
15503                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
15504                                                         || { rc=$? ; break; }
15505                 pool_create_files $POOL $file_dir $files "$ost_list" \
15506                                                         || { rc=$? ; break; }
15507                 # former test_200g test_200h
15508                 pool_lfs_df $POOL                       || { rc=$? ; break; }
15509                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
15510
15511                 # former test_201a test_201b test_201c
15512                 pool_remove_first_target $POOL          || { rc=$? ; break; }
15513
15514                 local f=$test_path/$tfile
15515                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
15516                 pool_remove $POOL $f                    || { rc=$? ; break; }
15517                 break
15518         done
15519
15520         destroy_test_pools
15521
15522         return $rc
15523 }
15524 run_test 200 "OST pools"
15525
15526 # usage: default_attr <count | size | offset>
15527 default_attr() {
15528         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
15529 }
15530
15531 # usage: check_default_stripe_attr
15532 check_default_stripe_attr() {
15533         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
15534         case $1 in
15535         --stripe-count|-c)
15536                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
15537         --stripe-size|-S)
15538                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
15539         --stripe-index|-i)
15540                 EXPECTED=-1;;
15541         *)
15542                 error "unknown getstripe attr '$1'"
15543         esac
15544
15545         [ $ACTUAL == $EXPECTED ] ||
15546                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
15547 }
15548
15549 test_204a() {
15550         test_mkdir $DIR/$tdir
15551         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
15552
15553         check_default_stripe_attr --stripe-count
15554         check_default_stripe_attr --stripe-size
15555         check_default_stripe_attr --stripe-index
15556 }
15557 run_test 204a "Print default stripe attributes"
15558
15559 test_204b() {
15560         test_mkdir $DIR/$tdir
15561         $LFS setstripe --stripe-count 1 $DIR/$tdir
15562
15563         check_default_stripe_attr --stripe-size
15564         check_default_stripe_attr --stripe-index
15565 }
15566 run_test 204b "Print default stripe size and offset"
15567
15568 test_204c() {
15569         test_mkdir $DIR/$tdir
15570         $LFS setstripe --stripe-size 65536 $DIR/$tdir
15571
15572         check_default_stripe_attr --stripe-count
15573         check_default_stripe_attr --stripe-index
15574 }
15575 run_test 204c "Print default stripe count and offset"
15576
15577 test_204d() {
15578         test_mkdir $DIR/$tdir
15579         $LFS setstripe --stripe-index 0 $DIR/$tdir
15580
15581         check_default_stripe_attr --stripe-count
15582         check_default_stripe_attr --stripe-size
15583 }
15584 run_test 204d "Print default stripe count and size"
15585
15586 test_204e() {
15587         test_mkdir $DIR/$tdir
15588         $LFS setstripe -d $DIR/$tdir
15589
15590         check_default_stripe_attr --stripe-count --raw
15591         check_default_stripe_attr --stripe-size --raw
15592         check_default_stripe_attr --stripe-index --raw
15593 }
15594 run_test 204e "Print raw stripe attributes"
15595
15596 test_204f() {
15597         test_mkdir $DIR/$tdir
15598         $LFS setstripe --stripe-count 1 $DIR/$tdir
15599
15600         check_default_stripe_attr --stripe-size --raw
15601         check_default_stripe_attr --stripe-index --raw
15602 }
15603 run_test 204f "Print raw stripe size and offset"
15604
15605 test_204g() {
15606         test_mkdir $DIR/$tdir
15607         $LFS setstripe --stripe-size 65536 $DIR/$tdir
15608
15609         check_default_stripe_attr --stripe-count --raw
15610         check_default_stripe_attr --stripe-index --raw
15611 }
15612 run_test 204g "Print raw stripe count and offset"
15613
15614 test_204h() {
15615         test_mkdir $DIR/$tdir
15616         $LFS setstripe --stripe-index 0 $DIR/$tdir
15617
15618         check_default_stripe_attr --stripe-count --raw
15619         check_default_stripe_attr --stripe-size --raw
15620 }
15621 run_test 204h "Print raw stripe count and size"
15622
15623 # Figure out which job scheduler is being used, if any,
15624 # or use a fake one
15625 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
15626         JOBENV=SLURM_JOB_ID
15627 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
15628         JOBENV=LSB_JOBID
15629 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
15630         JOBENV=PBS_JOBID
15631 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
15632         JOBENV=LOADL_STEP_ID
15633 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
15634         JOBENV=JOB_ID
15635 else
15636         $LCTL list_param jobid_name > /dev/null 2>&1
15637         if [ $? -eq 0 ]; then
15638                 JOBENV=nodelocal
15639         else
15640                 JOBENV=FAKE_JOBID
15641         fi
15642 fi
15643 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
15644
15645 verify_jobstats() {
15646         local cmd=($1)
15647         shift
15648         local facets="$@"
15649
15650 # we don't really need to clear the stats for this test to work, since each
15651 # command has a unique jobid, but it makes debugging easier if needed.
15652 #       for facet in $facets; do
15653 #               local dev=$(convert_facet2label $facet)
15654 #               # clear old jobstats
15655 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
15656 #       done
15657
15658         # use a new JobID for each test, or we might see an old one
15659         [ "$JOBENV" = "FAKE_JOBID" ] &&
15660                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
15661
15662         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
15663
15664         [ "$JOBENV" = "nodelocal" ] && {
15665                 FAKE_JOBID=id.$testnum.%e.$RANDOM
15666                 $LCTL set_param jobid_name=$FAKE_JOBID
15667                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
15668         }
15669
15670         log "Test: ${cmd[*]}"
15671         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
15672
15673         if [ $JOBENV = "FAKE_JOBID" ]; then
15674                 FAKE_JOBID=$JOBVAL ${cmd[*]}
15675         else
15676                 ${cmd[*]}
15677         fi
15678
15679         # all files are created on OST0000
15680         for facet in $facets; do
15681                 local stats="*.$(convert_facet2label $facet).job_stats"
15682
15683                 # strip out libtool wrappers for in-tree executables
15684                 if [ $(do_facet $facet lctl get_param $stats |
15685                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
15686                         do_facet $facet lctl get_param $stats
15687                         error "No jobstats for $JOBVAL found on $facet::$stats"
15688                 fi
15689         done
15690 }
15691
15692 jobstats_set() {
15693         local new_jobenv=$1
15694
15695         set_persistent_param_and_check client "jobid_var" \
15696                 "$FSNAME.sys.jobid_var" $new_jobenv
15697 }
15698
15699 test_205a() { # Job stats
15700         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15701         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
15702                 skip "Need MDS version with at least 2.7.1"
15703         remote_mgs_nodsh && skip "remote MGS with nodsh"
15704         remote_mds_nodsh && skip "remote MDS with nodsh"
15705         remote_ost_nodsh && skip "remote OST with nodsh"
15706         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
15707                 skip "Server doesn't support jobstats"
15708         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
15709
15710         local old_jobenv=$($LCTL get_param -n jobid_var)
15711         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
15712
15713         if [[ $PERM_CMD == *"set_param -P"* ]]; then
15714                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
15715         else
15716                 stack_trap "do_facet mgs $PERM_CMD \
15717                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
15718         fi
15719         changelog_register
15720
15721         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
15722                                 mdt.*.job_cleanup_interval | head -n 1)
15723         local new_interval=5
15724         do_facet $SINGLEMDS \
15725                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
15726         stack_trap "do_facet $SINGLEMDS \
15727                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
15728         local start=$SECONDS
15729
15730         local cmd
15731         # mkdir
15732         cmd="mkdir $DIR/$tdir"
15733         verify_jobstats "$cmd" "$SINGLEMDS"
15734         # rmdir
15735         cmd="rmdir $DIR/$tdir"
15736         verify_jobstats "$cmd" "$SINGLEMDS"
15737         # mkdir on secondary MDT
15738         if [ $MDSCOUNT -gt 1 ]; then
15739                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
15740                 verify_jobstats "$cmd" "mds2"
15741         fi
15742         # mknod
15743         cmd="mknod $DIR/$tfile c 1 3"
15744         verify_jobstats "$cmd" "$SINGLEMDS"
15745         # unlink
15746         cmd="rm -f $DIR/$tfile"
15747         verify_jobstats "$cmd" "$SINGLEMDS"
15748         # create all files on OST0000 so verify_jobstats can find OST stats
15749         # open & close
15750         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
15751         verify_jobstats "$cmd" "$SINGLEMDS"
15752         # setattr
15753         cmd="touch $DIR/$tfile"
15754         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15755         # write
15756         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
15757         verify_jobstats "$cmd" "ost1"
15758         # read
15759         cancel_lru_locks osc
15760         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
15761         verify_jobstats "$cmd" "ost1"
15762         # truncate
15763         cmd="$TRUNCATE $DIR/$tfile 0"
15764         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15765         # rename
15766         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
15767         verify_jobstats "$cmd" "$SINGLEMDS"
15768         # jobstats expiry - sleep until old stats should be expired
15769         local left=$((new_interval + 5 - (SECONDS - start)))
15770         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
15771                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
15772                         "0" $left
15773         cmd="mkdir $DIR/$tdir.expire"
15774         verify_jobstats "$cmd" "$SINGLEMDS"
15775         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
15776             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
15777
15778         # Ensure that jobid are present in changelog (if supported by MDS)
15779         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
15780                 changelog_dump | tail -10
15781                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
15782                 [ $jobids -eq 9 ] ||
15783                         error "Wrong changelog jobid count $jobids != 9"
15784
15785                 # LU-5862
15786                 JOBENV="disable"
15787                 jobstats_set $JOBENV
15788                 touch $DIR/$tfile
15789                 changelog_dump | grep $tfile
15790                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
15791                 [ $jobids -eq 0 ] ||
15792                         error "Unexpected jobids when jobid_var=$JOBENV"
15793         fi
15794
15795         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
15796         JOBENV="JOBCOMPLEX"
15797         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
15798
15799         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
15800 }
15801 run_test 205a "Verify job stats"
15802
15803 # LU-13117
15804 test_205b() {
15805         $LCTL set_param jobid_var=USER jobid_name="%e.%u"
15806         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
15807         do_facet $SINGLEMDS $LCTL get_param mdt.*.job_stats |
15808                 grep job_id: | grep foolish &&
15809                         error "Unexpected jobid found"
15810         true
15811 }
15812 run_test 205b "Verify job stats jobid parsing"
15813
15814 # LU-1480, LU-1773 and LU-1657
15815 test_206() {
15816         mkdir -p $DIR/$tdir
15817         $LFS setstripe -c -1 $DIR/$tdir
15818 #define OBD_FAIL_LOV_INIT 0x1403
15819         $LCTL set_param fail_loc=0xa0001403
15820         $LCTL set_param fail_val=1
15821         touch $DIR/$tdir/$tfile || true
15822 }
15823 run_test 206 "fail lov_init_raid0() doesn't lbug"
15824
15825 test_207a() {
15826         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15827         local fsz=`stat -c %s $DIR/$tfile`
15828         cancel_lru_locks mdc
15829
15830         # do not return layout in getattr intent
15831 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
15832         $LCTL set_param fail_loc=0x170
15833         local sz=`stat -c %s $DIR/$tfile`
15834
15835         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
15836
15837         rm -rf $DIR/$tfile
15838 }
15839 run_test 207a "can refresh layout at glimpse"
15840
15841 test_207b() {
15842         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15843         local cksum=`md5sum $DIR/$tfile`
15844         local fsz=`stat -c %s $DIR/$tfile`
15845         cancel_lru_locks mdc
15846         cancel_lru_locks osc
15847
15848         # do not return layout in getattr intent
15849 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
15850         $LCTL set_param fail_loc=0x171
15851
15852         # it will refresh layout after the file is opened but before read issues
15853         echo checksum is "$cksum"
15854         echo "$cksum" |md5sum -c --quiet || error "file differs"
15855
15856         rm -rf $DIR/$tfile
15857 }
15858 run_test 207b "can refresh layout at open"
15859
15860 test_208() {
15861         # FIXME: in this test suite, only RD lease is used. This is okay
15862         # for now as only exclusive open is supported. After generic lease
15863         # is done, this test suite should be revised. - Jinshan
15864
15865         remote_mds_nodsh && skip "remote MDS with nodsh"
15866         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
15867                 skip "Need MDS version at least 2.4.52"
15868
15869         echo "==== test 1: verify get lease work"
15870         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
15871
15872         echo "==== test 2: verify lease can be broken by upcoming open"
15873         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15874         local PID=$!
15875         sleep 1
15876
15877         $MULTIOP $DIR/$tfile oO_RDONLY:c
15878         kill -USR1 $PID && wait $PID || error "break lease error"
15879
15880         echo "==== test 3: verify lease can't be granted if an open already exists"
15881         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
15882         local PID=$!
15883         sleep 1
15884
15885         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
15886         kill -USR1 $PID && wait $PID || error "open file error"
15887
15888         echo "==== test 4: lease can sustain over recovery"
15889         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
15890         PID=$!
15891         sleep 1
15892
15893         fail mds1
15894
15895         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
15896
15897         echo "==== test 5: lease broken can't be regained by replay"
15898         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15899         PID=$!
15900         sleep 1
15901
15902         # open file to break lease and then recovery
15903         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
15904         fail mds1
15905
15906         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
15907
15908         rm -f $DIR/$tfile
15909 }
15910 run_test 208 "Exclusive open"
15911
15912 test_209() {
15913         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
15914                 skip_env "must have disp_stripe"
15915
15916         touch $DIR/$tfile
15917         sync; sleep 5; sync;
15918
15919         echo 3 > /proc/sys/vm/drop_caches
15920         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15921
15922         # open/close 500 times
15923         for i in $(seq 500); do
15924                 cat $DIR/$tfile
15925         done
15926
15927         echo 3 > /proc/sys/vm/drop_caches
15928         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15929
15930         echo "before: $req_before, after: $req_after"
15931         [ $((req_after - req_before)) -ge 300 ] &&
15932                 error "open/close requests are not freed"
15933         return 0
15934 }
15935 run_test 209 "read-only open/close requests should be freed promptly"
15936
15937 test_212() {
15938         size=`date +%s`
15939         size=$((size % 8192 + 1))
15940         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
15941         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
15942         rm -f $DIR/f212 $DIR/f212.xyz
15943 }
15944 run_test 212 "Sendfile test ============================================"
15945
15946 test_213() {
15947         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
15948         cancel_lru_locks osc
15949         lctl set_param fail_loc=0x8000040f
15950         # generate a read lock
15951         cat $DIR/$tfile > /dev/null
15952         # write to the file, it will try to cancel the above read lock.
15953         cat /etc/hosts >> $DIR/$tfile
15954 }
15955 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
15956
15957 test_214() { # for bug 20133
15958         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
15959         for (( i=0; i < 340; i++ )) ; do
15960                 touch $DIR/$tdir/d214c/a$i
15961         done
15962
15963         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
15964         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
15965         ls $DIR/d214c || error "ls $DIR/d214c failed"
15966         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
15967         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
15968 }
15969 run_test 214 "hash-indexed directory test - bug 20133"
15970
15971 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
15972 create_lnet_proc_files() {
15973         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
15974 }
15975
15976 # counterpart of create_lnet_proc_files
15977 remove_lnet_proc_files() {
15978         rm -f $TMP/lnet_$1.sys
15979 }
15980
15981 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15982 # 3rd arg as regexp for body
15983 check_lnet_proc_stats() {
15984         local l=$(cat "$TMP/lnet_$1" |wc -l)
15985         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
15986
15987         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
15988 }
15989
15990 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15991 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
15992 # optional and can be regexp for 2nd line (lnet.routes case)
15993 check_lnet_proc_entry() {
15994         local blp=2          # blp stands for 'position of 1st line of body'
15995         [ -z "$5" ] || blp=3 # lnet.routes case
15996
15997         local l=$(cat "$TMP/lnet_$1" |wc -l)
15998         # subtracting one from $blp because the body can be empty
15999         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
16000
16001         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
16002                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
16003
16004         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
16005                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
16006
16007         # bail out if any unexpected line happened
16008         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
16009         [ "$?" != 0 ] || error "$2 misformatted"
16010 }
16011
16012 test_215() { # for bugs 18102, 21079, 21517
16013         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16014
16015         local N='(0|[1-9][0-9]*)'       # non-negative numeric
16016         local P='[1-9][0-9]*'           # positive numeric
16017         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
16018         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
16019         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
16020         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
16021
16022         local L1 # regexp for 1st line
16023         local L2 # regexp for 2nd line (optional)
16024         local BR # regexp for the rest (body)
16025
16026         # lnet.stats should look as 11 space-separated non-negative numerics
16027         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
16028         create_lnet_proc_files "stats"
16029         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
16030         remove_lnet_proc_files "stats"
16031
16032         # lnet.routes should look like this:
16033         # Routing disabled/enabled
16034         # net hops priority state router
16035         # where net is a string like tcp0, hops > 0, priority >= 0,
16036         # state is up/down,
16037         # router is a string like 192.168.1.1@tcp2
16038         L1="^Routing (disabled|enabled)$"
16039         L2="^net +hops +priority +state +router$"
16040         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
16041         create_lnet_proc_files "routes"
16042         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
16043         remove_lnet_proc_files "routes"
16044
16045         # lnet.routers should look like this:
16046         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
16047         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
16048         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
16049         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
16050         L1="^ref +rtr_ref +alive +router$"
16051         BR="^$P +$P +(up|down) +$NID$"
16052         create_lnet_proc_files "routers"
16053         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
16054         remove_lnet_proc_files "routers"
16055
16056         # lnet.peers should look like this:
16057         # nid refs state last max rtr min tx min queue
16058         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
16059         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
16060         # numeric (0 or >0 or <0), queue >= 0.
16061         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
16062         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
16063         create_lnet_proc_files "peers"
16064         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
16065         remove_lnet_proc_files "peers"
16066
16067         # lnet.buffers  should look like this:
16068         # pages count credits min
16069         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
16070         L1="^pages +count +credits +min$"
16071         BR="^ +$N +$N +$I +$I$"
16072         create_lnet_proc_files "buffers"
16073         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
16074         remove_lnet_proc_files "buffers"
16075
16076         # lnet.nis should look like this:
16077         # nid status alive refs peer rtr max tx min
16078         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
16079         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
16080         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
16081         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
16082         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
16083         create_lnet_proc_files "nis"
16084         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
16085         remove_lnet_proc_files "nis"
16086
16087         # can we successfully write to lnet.stats?
16088         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
16089 }
16090 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
16091
16092 test_216() { # bug 20317
16093         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16094         remote_ost_nodsh && skip "remote OST with nodsh"
16095
16096         local node
16097         local facets=$(get_facets OST)
16098         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16099
16100         save_lustre_params client "osc.*.contention_seconds" > $p
16101         save_lustre_params $facets \
16102                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
16103         save_lustre_params $facets \
16104                 "ldlm.namespaces.filter-*.contended_locks" >> $p
16105         save_lustre_params $facets \
16106                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
16107         clear_stats osc.*.osc_stats
16108
16109         # agressive lockless i/o settings
16110         do_nodes $(comma_list $(osts_nodes)) \
16111                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
16112                         ldlm.namespaces.filter-*.contended_locks=0 \
16113                         ldlm.namespaces.filter-*.contention_seconds=60"
16114         lctl set_param -n osc.*.contention_seconds=60
16115
16116         $DIRECTIO write $DIR/$tfile 0 10 4096
16117         $CHECKSTAT -s 40960 $DIR/$tfile
16118
16119         # disable lockless i/o
16120         do_nodes $(comma_list $(osts_nodes)) \
16121                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
16122                         ldlm.namespaces.filter-*.contended_locks=32 \
16123                         ldlm.namespaces.filter-*.contention_seconds=0"
16124         lctl set_param -n osc.*.contention_seconds=0
16125         clear_stats osc.*.osc_stats
16126
16127         dd if=/dev/zero of=$DIR/$tfile count=0
16128         $CHECKSTAT -s 0 $DIR/$tfile
16129
16130         restore_lustre_params <$p
16131         rm -f $p
16132         rm $DIR/$tfile
16133 }
16134 run_test 216 "check lockless direct write updates file size and kms correctly"
16135
16136 test_217() { # bug 22430
16137         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16138
16139         local node
16140         local nid
16141
16142         for node in $(nodes_list); do
16143                 nid=$(host_nids_address $node $NETTYPE)
16144                 if [[ $nid = *-* ]] ; then
16145                         echo "lctl ping $(h2nettype $nid)"
16146                         lctl ping $(h2nettype $nid)
16147                 else
16148                         echo "skipping $node (no hyphen detected)"
16149                 fi
16150         done
16151 }
16152 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
16153
16154 test_218() {
16155        # do directio so as not to populate the page cache
16156        log "creating a 10 Mb file"
16157        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
16158        log "starting reads"
16159        dd if=$DIR/$tfile of=/dev/null bs=4096 &
16160        log "truncating the file"
16161        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
16162        log "killing dd"
16163        kill %+ || true # reads might have finished
16164        echo "wait until dd is finished"
16165        wait
16166        log "removing the temporary file"
16167        rm -rf $DIR/$tfile || error "tmp file removal failed"
16168 }
16169 run_test 218 "parallel read and truncate should not deadlock"
16170
16171 test_219() {
16172         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16173
16174         # write one partial page
16175         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
16176         # set no grant so vvp_io_commit_write will do sync write
16177         $LCTL set_param fail_loc=0x411
16178         # write a full page at the end of file
16179         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
16180
16181         $LCTL set_param fail_loc=0
16182         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
16183         $LCTL set_param fail_loc=0x411
16184         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
16185
16186         # LU-4201
16187         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
16188         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
16189 }
16190 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
16191
16192 test_220() { #LU-325
16193         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16194         remote_ost_nodsh && skip "remote OST with nodsh"
16195         remote_mds_nodsh && skip "remote MDS with nodsh"
16196         remote_mgs_nodsh && skip "remote MGS with nodsh"
16197
16198         local OSTIDX=0
16199
16200         # create on MDT0000 so the last_id and next_id are correct
16201         mkdir $DIR/$tdir
16202         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
16203         OST=${OST%_UUID}
16204
16205         # on the mdt's osc
16206         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
16207         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
16208                         osp.$mdtosc_proc1.prealloc_last_id)
16209         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
16210                         osp.$mdtosc_proc1.prealloc_next_id)
16211
16212         $LFS df -i
16213
16214         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
16215         #define OBD_FAIL_OST_ENOINO              0x229
16216         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
16217         create_pool $FSNAME.$TESTNAME || return 1
16218         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
16219
16220         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
16221
16222         MDSOBJS=$((last_id - next_id))
16223         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
16224
16225         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
16226         echo "OST still has $count kbytes free"
16227
16228         echo "create $MDSOBJS files @next_id..."
16229         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
16230
16231         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
16232                         osp.$mdtosc_proc1.prealloc_last_id)
16233         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
16234                         osp.$mdtosc_proc1.prealloc_next_id)
16235
16236         echo "after creation, last_id=$last_id2, next_id=$next_id2"
16237         $LFS df -i
16238
16239         echo "cleanup..."
16240
16241         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
16242         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
16243
16244         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
16245                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
16246         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
16247                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
16248         echo "unlink $MDSOBJS files @$next_id..."
16249         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
16250 }
16251 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
16252
16253 test_221() {
16254         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16255
16256         dd if=`which date` of=$MOUNT/date oflag=sync
16257         chmod +x $MOUNT/date
16258
16259         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
16260         $LCTL set_param fail_loc=0x80001401
16261
16262         $MOUNT/date > /dev/null
16263         rm -f $MOUNT/date
16264 }
16265 run_test 221 "make sure fault and truncate race to not cause OOM"
16266
16267 test_222a () {
16268         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16269
16270         rm -rf $DIR/$tdir
16271         test_mkdir $DIR/$tdir
16272         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16273         createmany -o $DIR/$tdir/$tfile 10
16274         cancel_lru_locks mdc
16275         cancel_lru_locks osc
16276         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16277         $LCTL set_param fail_loc=0x31a
16278         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
16279         $LCTL set_param fail_loc=0
16280         rm -r $DIR/$tdir
16281 }
16282 run_test 222a "AGL for ls should not trigger CLIO lock failure"
16283
16284 test_222b () {
16285         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16286
16287         rm -rf $DIR/$tdir
16288         test_mkdir $DIR/$tdir
16289         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16290         createmany -o $DIR/$tdir/$tfile 10
16291         cancel_lru_locks mdc
16292         cancel_lru_locks osc
16293         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16294         $LCTL set_param fail_loc=0x31a
16295         rm -r $DIR/$tdir || error "AGL for rmdir failed"
16296         $LCTL set_param fail_loc=0
16297 }
16298 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
16299
16300 test_223 () {
16301         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16302
16303         rm -rf $DIR/$tdir
16304         test_mkdir $DIR/$tdir
16305         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16306         createmany -o $DIR/$tdir/$tfile 10
16307         cancel_lru_locks mdc
16308         cancel_lru_locks osc
16309         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
16310         $LCTL set_param fail_loc=0x31b
16311         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
16312         $LCTL set_param fail_loc=0
16313         rm -r $DIR/$tdir
16314 }
16315 run_test 223 "osc reenqueue if without AGL lock granted ======================="
16316
16317 test_224a() { # LU-1039, MRP-303
16318         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16319
16320         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
16321         $LCTL set_param fail_loc=0x508
16322         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
16323         $LCTL set_param fail_loc=0
16324         df $DIR
16325 }
16326 run_test 224a "Don't panic on bulk IO failure"
16327
16328 test_224b() { # LU-1039, MRP-303
16329         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16330
16331         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
16332         cancel_lru_locks osc
16333         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
16334         $LCTL set_param fail_loc=0x515
16335         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
16336         $LCTL set_param fail_loc=0
16337         df $DIR
16338 }
16339 run_test 224b "Don't panic on bulk IO failure"
16340
16341 test_224c() { # LU-6441
16342         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16343         remote_mds_nodsh && skip "remote MDS with nodsh"
16344
16345         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16346         save_writethrough $p
16347         set_cache writethrough on
16348
16349         local pages_per_rpc=$($LCTL get_param osc.*.max_pages_per_rpc)
16350         local at_max=$($LCTL get_param -n at_max)
16351         local timeout=$($LCTL get_param -n timeout)
16352         local test_at="at_max"
16353         local param_at="$FSNAME.sys.at_max"
16354         local test_timeout="timeout"
16355         local param_timeout="$FSNAME.sys.timeout"
16356
16357         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
16358
16359         set_persistent_param_and_check client "$test_at" "$param_at" 0
16360         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
16361
16362         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
16363         do_facet ost1 "$LCTL set_param fail_loc=0x520"
16364         $LFS setstripe -c 1 -i 0 $DIR/$tfile
16365         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
16366         sync
16367         do_facet ost1 "$LCTL set_param fail_loc=0"
16368
16369         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
16370         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
16371                 $timeout
16372
16373         $LCTL set_param -n $pages_per_rpc
16374         restore_lustre_params < $p
16375         rm -f $p
16376 }
16377 run_test 224c "Don't hang if one of md lost during large bulk RPC"
16378
16379 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
16380 test_225a () {
16381         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16382         if [ -z ${MDSSURVEY} ]; then
16383                 skip_env "mds-survey not found"
16384         fi
16385         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16386                 skip "Need MDS version at least 2.2.51"
16387
16388         local mds=$(facet_host $SINGLEMDS)
16389         local target=$(do_nodes $mds 'lctl dl' |
16390                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16391
16392         local cmd1="file_count=1000 thrhi=4"
16393         local cmd2="dir_count=2 layer=mdd stripe_count=0"
16394         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16395         local cmd="$cmd1 $cmd2 $cmd3"
16396
16397         rm -f ${TMP}/mds_survey*
16398         echo + $cmd
16399         eval $cmd || error "mds-survey with zero-stripe failed"
16400         cat ${TMP}/mds_survey*
16401         rm -f ${TMP}/mds_survey*
16402 }
16403 run_test 225a "Metadata survey sanity with zero-stripe"
16404
16405 test_225b () {
16406         if [ -z ${MDSSURVEY} ]; then
16407                 skip_env "mds-survey not found"
16408         fi
16409         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16410                 skip "Need MDS version at least 2.2.51"
16411         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16412         remote_mds_nodsh && skip "remote MDS with nodsh"
16413         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
16414                 skip_env "Need to mount OST to test"
16415         fi
16416
16417         local mds=$(facet_host $SINGLEMDS)
16418         local target=$(do_nodes $mds 'lctl dl' |
16419                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16420
16421         local cmd1="file_count=1000 thrhi=4"
16422         local cmd2="dir_count=2 layer=mdd stripe_count=1"
16423         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16424         local cmd="$cmd1 $cmd2 $cmd3"
16425
16426         rm -f ${TMP}/mds_survey*
16427         echo + $cmd
16428         eval $cmd || error "mds-survey with stripe_count failed"
16429         cat ${TMP}/mds_survey*
16430         rm -f ${TMP}/mds_survey*
16431 }
16432 run_test 225b "Metadata survey sanity with stripe_count = 1"
16433
16434 mcreate_path2fid () {
16435         local mode=$1
16436         local major=$2
16437         local minor=$3
16438         local name=$4
16439         local desc=$5
16440         local path=$DIR/$tdir/$name
16441         local fid
16442         local rc
16443         local fid_path
16444
16445         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
16446                 error "cannot create $desc"
16447
16448         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
16449         rc=$?
16450         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
16451
16452         fid_path=$($LFS fid2path $MOUNT $fid)
16453         rc=$?
16454         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
16455
16456         [ "$path" == "$fid_path" ] ||
16457                 error "fid2path returned $fid_path, expected $path"
16458
16459         echo "pass with $path and $fid"
16460 }
16461
16462 test_226a () {
16463         rm -rf $DIR/$tdir
16464         mkdir -p $DIR/$tdir
16465
16466         mcreate_path2fid 0010666 0 0 fifo "FIFO"
16467         mcreate_path2fid 0020666 1 3 null "character special file (null)"
16468         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
16469         mcreate_path2fid 0040666 0 0 dir "directory"
16470         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
16471         mcreate_path2fid 0100666 0 0 file "regular file"
16472         mcreate_path2fid 0120666 0 0 link "symbolic link"
16473         mcreate_path2fid 0140666 0 0 sock "socket"
16474 }
16475 run_test 226a "call path2fid and fid2path on files of all type"
16476
16477 test_226b () {
16478         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16479
16480         local MDTIDX=1
16481
16482         rm -rf $DIR/$tdir
16483         mkdir -p $DIR/$tdir
16484         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
16485                 error "create remote directory failed"
16486         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
16487         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
16488                                 "character special file (null)"
16489         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
16490                                 "character special file (no device)"
16491         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
16492         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
16493                                 "block special file (loop)"
16494         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
16495         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
16496         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
16497 }
16498 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
16499
16500 # LU-1299 Executing or running ldd on a truncated executable does not
16501 # cause an out-of-memory condition.
16502 test_227() {
16503         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16504         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
16505
16506         dd if=$(which date) of=$MOUNT/date bs=1k count=1
16507         chmod +x $MOUNT/date
16508
16509         $MOUNT/date > /dev/null
16510         ldd $MOUNT/date > /dev/null
16511         rm -f $MOUNT/date
16512 }
16513 run_test 227 "running truncated executable does not cause OOM"
16514
16515 # LU-1512 try to reuse idle OI blocks
16516 test_228a() {
16517         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16518         remote_mds_nodsh && skip "remote MDS with nodsh"
16519         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16520
16521         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16522         local myDIR=$DIR/$tdir
16523
16524         mkdir -p $myDIR
16525         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16526         $LCTL set_param fail_loc=0x80001002
16527         createmany -o $myDIR/t- 10000
16528         $LCTL set_param fail_loc=0
16529         # The guard is current the largest FID holder
16530         touch $myDIR/guard
16531         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16532                     tr -d '[')
16533         local IDX=$(($SEQ % 64))
16534
16535         do_facet $SINGLEMDS sync
16536         # Make sure journal flushed.
16537         sleep 6
16538         local blk1=$(do_facet $SINGLEMDS \
16539                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16540                      grep Blockcount | awk '{print $4}')
16541
16542         # Remove old files, some OI blocks will become idle.
16543         unlinkmany $myDIR/t- 10000
16544         # Create new files, idle OI blocks should be reused.
16545         createmany -o $myDIR/t- 2000
16546         do_facet $SINGLEMDS sync
16547         # Make sure journal flushed.
16548         sleep 6
16549         local blk2=$(do_facet $SINGLEMDS \
16550                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16551                      grep Blockcount | awk '{print $4}')
16552
16553         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16554 }
16555 run_test 228a "try to reuse idle OI blocks"
16556
16557 test_228b() {
16558         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16559         remote_mds_nodsh && skip "remote MDS with nodsh"
16560         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16561
16562         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16563         local myDIR=$DIR/$tdir
16564
16565         mkdir -p $myDIR
16566         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16567         $LCTL set_param fail_loc=0x80001002
16568         createmany -o $myDIR/t- 10000
16569         $LCTL set_param fail_loc=0
16570         # The guard is current the largest FID holder
16571         touch $myDIR/guard
16572         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16573                     tr -d '[')
16574         local IDX=$(($SEQ % 64))
16575
16576         do_facet $SINGLEMDS sync
16577         # Make sure journal flushed.
16578         sleep 6
16579         local blk1=$(do_facet $SINGLEMDS \
16580                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16581                      grep Blockcount | awk '{print $4}')
16582
16583         # Remove old files, some OI blocks will become idle.
16584         unlinkmany $myDIR/t- 10000
16585
16586         # stop the MDT
16587         stop $SINGLEMDS || error "Fail to stop MDT."
16588         # remount the MDT
16589         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
16590
16591         df $MOUNT || error "Fail to df."
16592         # Create new files, idle OI blocks should be reused.
16593         createmany -o $myDIR/t- 2000
16594         do_facet $SINGLEMDS sync
16595         # Make sure journal flushed.
16596         sleep 6
16597         local blk2=$(do_facet $SINGLEMDS \
16598                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16599                      grep Blockcount | awk '{print $4}')
16600
16601         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16602 }
16603 run_test 228b "idle OI blocks can be reused after MDT restart"
16604
16605 #LU-1881
16606 test_228c() {
16607         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16608         remote_mds_nodsh && skip "remote MDS with nodsh"
16609         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16610
16611         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16612         local myDIR=$DIR/$tdir
16613
16614         mkdir -p $myDIR
16615         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16616         $LCTL set_param fail_loc=0x80001002
16617         # 20000 files can guarantee there are index nodes in the OI file
16618         createmany -o $myDIR/t- 20000
16619         $LCTL set_param fail_loc=0
16620         # The guard is current the largest FID holder
16621         touch $myDIR/guard
16622         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16623                     tr -d '[')
16624         local IDX=$(($SEQ % 64))
16625
16626         do_facet $SINGLEMDS sync
16627         # Make sure journal flushed.
16628         sleep 6
16629         local blk1=$(do_facet $SINGLEMDS \
16630                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16631                      grep Blockcount | awk '{print $4}')
16632
16633         # Remove old files, some OI blocks will become idle.
16634         unlinkmany $myDIR/t- 20000
16635         rm -f $myDIR/guard
16636         # The OI file should become empty now
16637
16638         # Create new files, idle OI blocks should be reused.
16639         createmany -o $myDIR/t- 2000
16640         do_facet $SINGLEMDS sync
16641         # Make sure journal flushed.
16642         sleep 6
16643         local blk2=$(do_facet $SINGLEMDS \
16644                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16645                      grep Blockcount | awk '{print $4}')
16646
16647         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16648 }
16649 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
16650
16651 test_229() { # LU-2482, LU-3448
16652         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16653         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
16654         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
16655                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
16656
16657         rm -f $DIR/$tfile
16658
16659         # Create a file with a released layout and stripe count 2.
16660         $MULTIOP $DIR/$tfile H2c ||
16661                 error "failed to create file with released layout"
16662
16663         $LFS getstripe -v $DIR/$tfile
16664
16665         local pattern=$($LFS getstripe -L $DIR/$tfile)
16666         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
16667
16668         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
16669                 error "getstripe"
16670         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
16671         stat $DIR/$tfile || error "failed to stat released file"
16672
16673         chown $RUNAS_ID $DIR/$tfile ||
16674                 error "chown $RUNAS_ID $DIR/$tfile failed"
16675
16676         chgrp $RUNAS_ID $DIR/$tfile ||
16677                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
16678
16679         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
16680         rm $DIR/$tfile || error "failed to remove released file"
16681 }
16682 run_test 229 "getstripe/stat/rm/attr changes work on released files"
16683
16684 test_230a() {
16685         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16686         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16687         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16688                 skip "Need MDS version at least 2.11.52"
16689
16690         local MDTIDX=1
16691
16692         test_mkdir $DIR/$tdir
16693         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
16694         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
16695         [ $mdt_idx -ne 0 ] &&
16696                 error "create local directory on wrong MDT $mdt_idx"
16697
16698         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
16699                         error "create remote directory failed"
16700         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
16701         [ $mdt_idx -ne $MDTIDX ] &&
16702                 error "create remote directory on wrong MDT $mdt_idx"
16703
16704         createmany -o $DIR/$tdir/test_230/t- 10 ||
16705                 error "create files on remote directory failed"
16706         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
16707         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
16708         rm -r $DIR/$tdir || error "unlink remote directory failed"
16709 }
16710 run_test 230a "Create remote directory and files under the remote directory"
16711
16712 test_230b() {
16713         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16714         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16715         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16716                 skip "Need MDS version at least 2.11.52"
16717
16718         local MDTIDX=1
16719         local mdt_index
16720         local i
16721         local file
16722         local pid
16723         local stripe_count
16724         local migrate_dir=$DIR/$tdir/migrate_dir
16725         local other_dir=$DIR/$tdir/other_dir
16726
16727         test_mkdir $DIR/$tdir
16728         test_mkdir -i0 -c1 $migrate_dir
16729         test_mkdir -i0 -c1 $other_dir
16730         for ((i=0; i<10; i++)); do
16731                 mkdir -p $migrate_dir/dir_${i}
16732                 createmany -o $migrate_dir/dir_${i}/f 10 ||
16733                         error "create files under remote dir failed $i"
16734         done
16735
16736         cp /etc/passwd $migrate_dir/$tfile
16737         cp /etc/passwd $other_dir/$tfile
16738         chattr +SAD $migrate_dir
16739         chattr +SAD $migrate_dir/$tfile
16740
16741         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16742         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16743         local old_dir_mode=$(stat -c%f $migrate_dir)
16744         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
16745
16746         mkdir -p $migrate_dir/dir_default_stripe2
16747         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
16748         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
16749
16750         mkdir -p $other_dir
16751         ln $migrate_dir/$tfile $other_dir/luna
16752         ln $migrate_dir/$tfile $migrate_dir/sofia
16753         ln $other_dir/$tfile $migrate_dir/david
16754         ln -s $migrate_dir/$tfile $other_dir/zachary
16755         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
16756         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
16757
16758         local len
16759         local lnktgt
16760
16761         # inline symlink
16762         for len in 58 59 60; do
16763                 lnktgt=$(str_repeat 'l' $len)
16764                 touch $migrate_dir/$lnktgt
16765                 ln -s $lnktgt $migrate_dir/${len}char_ln
16766         done
16767
16768         # PATH_MAX
16769         for len in 4094 4095; do
16770                 lnktgt=$(str_repeat 'l' $len)
16771                 ln -s $lnktgt $migrate_dir/${len}char_ln
16772         done
16773
16774         # NAME_MAX
16775         for len in 254 255; do
16776                 touch $migrate_dir/$(str_repeat 'l' $len)
16777         done
16778
16779         $LFS migrate -m $MDTIDX $migrate_dir ||
16780                 error "fails on migrating remote dir to MDT1"
16781
16782         echo "migratate to MDT1, then checking.."
16783         for ((i = 0; i < 10; i++)); do
16784                 for file in $(find $migrate_dir/dir_${i}); do
16785                         mdt_index=$($LFS getstripe -m $file)
16786                         # broken symlink getstripe will fail
16787                         [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
16788                                 error "$file is not on MDT${MDTIDX}"
16789                 done
16790         done
16791
16792         # the multiple link file should still in MDT0
16793         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
16794         [ $mdt_index == 0 ] ||
16795                 error "$file is not on MDT${MDTIDX}"
16796
16797         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16798         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16799                 error " expect $old_dir_flag get $new_dir_flag"
16800
16801         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16802         [ "$old_file_flag" = "$new_file_flag" ] ||
16803                 error " expect $old_file_flag get $new_file_flag"
16804
16805         local new_dir_mode=$(stat -c%f $migrate_dir)
16806         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16807                 error "expect mode $old_dir_mode get $new_dir_mode"
16808
16809         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16810         [ "$old_file_mode" = "$new_file_mode" ] ||
16811                 error "expect mode $old_file_mode get $new_file_mode"
16812
16813         diff /etc/passwd $migrate_dir/$tfile ||
16814                 error "$tfile different after migration"
16815
16816         diff /etc/passwd $other_dir/luna ||
16817                 error "luna different after migration"
16818
16819         diff /etc/passwd $migrate_dir/sofia ||
16820                 error "sofia different after migration"
16821
16822         diff /etc/passwd $migrate_dir/david ||
16823                 error "david different after migration"
16824
16825         diff /etc/passwd $other_dir/zachary ||
16826                 error "zachary different after migration"
16827
16828         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16829                 error "${tfile}_ln different after migration"
16830
16831         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16832                 error "${tfile}_ln_other different after migration"
16833
16834         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
16835         [ $stripe_count = 2 ] ||
16836                 error "dir strpe_count $d != 2 after migration."
16837
16838         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
16839         [ $stripe_count = 2 ] ||
16840                 error "file strpe_count $d != 2 after migration."
16841
16842         #migrate back to MDT0
16843         MDTIDX=0
16844
16845         $LFS migrate -m $MDTIDX $migrate_dir ||
16846                 error "fails on migrating remote dir to MDT0"
16847
16848         echo "migrate back to MDT0, checking.."
16849         for file in $(find $migrate_dir); do
16850                 mdt_index=$($LFS getstripe -m $file)
16851                 [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
16852                         error "$file is not on MDT${MDTIDX}"
16853         done
16854
16855         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16856         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16857                 error " expect $old_dir_flag get $new_dir_flag"
16858
16859         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16860         [ "$old_file_flag" = "$new_file_flag" ] ||
16861                 error " expect $old_file_flag get $new_file_flag"
16862
16863         local new_dir_mode=$(stat -c%f $migrate_dir)
16864         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16865                 error "expect mode $old_dir_mode get $new_dir_mode"
16866
16867         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16868         [ "$old_file_mode" = "$new_file_mode" ] ||
16869                 error "expect mode $old_file_mode get $new_file_mode"
16870
16871         diff /etc/passwd ${migrate_dir}/$tfile ||
16872                 error "$tfile different after migration"
16873
16874         diff /etc/passwd ${other_dir}/luna ||
16875                 error "luna different after migration"
16876
16877         diff /etc/passwd ${migrate_dir}/sofia ||
16878                 error "sofia different after migration"
16879
16880         diff /etc/passwd ${other_dir}/zachary ||
16881                 error "zachary different after migration"
16882
16883         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16884                 error "${tfile}_ln different after migration"
16885
16886         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16887                 error "${tfile}_ln_other different after migration"
16888
16889         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
16890         [ $stripe_count = 2 ] ||
16891                 error "dir strpe_count $d != 2 after migration."
16892
16893         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
16894         [ $stripe_count = 2 ] ||
16895                 error "file strpe_count $d != 2 after migration."
16896
16897         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16898 }
16899 run_test 230b "migrate directory"
16900
16901 test_230c() {
16902         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16903         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16904         remote_mds_nodsh && skip "remote MDS with nodsh"
16905         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16906                 skip "Need MDS version at least 2.11.52"
16907
16908         local MDTIDX=1
16909         local total=3
16910         local mdt_index
16911         local file
16912         local migrate_dir=$DIR/$tdir/migrate_dir
16913
16914         #If migrating directory fails in the middle, all entries of
16915         #the directory is still accessiable.
16916         test_mkdir $DIR/$tdir
16917         test_mkdir -i0 -c1 $migrate_dir
16918         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
16919         stat $migrate_dir
16920         createmany -o $migrate_dir/f $total ||
16921                 error "create files under ${migrate_dir} failed"
16922
16923         # fail after migrating top dir, and this will fail only once, so the
16924         # first sub file migration will fail (currently f3), others succeed.
16925         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
16926         do_facet mds1 lctl set_param fail_loc=0x1801
16927         local t=$(ls $migrate_dir | wc -l)
16928         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
16929                 error "migrate should fail"
16930         local u=$(ls $migrate_dir | wc -l)
16931         [ "$u" == "$t" ] || error "$u != $t during migration"
16932
16933         # add new dir/file should succeed
16934         mkdir $migrate_dir/dir ||
16935                 error "mkdir failed under migrating directory"
16936         touch $migrate_dir/file ||
16937                 error "create file failed under migrating directory"
16938
16939         # add file with existing name should fail
16940         for file in $migrate_dir/f*; do
16941                 stat $file > /dev/null || error "stat $file failed"
16942                 $OPENFILE -f O_CREAT:O_EXCL $file &&
16943                         error "open(O_CREAT|O_EXCL) $file should fail"
16944                 $MULTIOP $file m && error "create $file should fail"
16945                 touch $DIR/$tdir/remote_dir/$tfile ||
16946                         error "touch $tfile failed"
16947                 ln $DIR/$tdir/remote_dir/$tfile $file &&
16948                         error "link $file should fail"
16949                 mdt_index=$($LFS getstripe -m $file)
16950                 if [ $mdt_index == 0 ]; then
16951                         # file failed to migrate is not allowed to rename to
16952                         mv $DIR/$tdir/remote_dir/$tfile $file &&
16953                                 error "rename to $file should fail"
16954                 else
16955                         mv $DIR/$tdir/remote_dir/$tfile $file ||
16956                                 error "rename to $file failed"
16957                 fi
16958                 echo hello >> $file || error "write $file failed"
16959         done
16960
16961         # resume migration with different options should fail
16962         $LFS migrate -m 0 $migrate_dir &&
16963                 error "migrate -m 0 $migrate_dir should fail"
16964
16965         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
16966                 error "migrate -c 2 $migrate_dir should fail"
16967
16968         # resume migration should succeed
16969         $LFS migrate -m $MDTIDX $migrate_dir ||
16970                 error "migrate $migrate_dir failed"
16971
16972         echo "Finish migration, then checking.."
16973         for file in $(find $migrate_dir); do
16974                 mdt_index=$($LFS getstripe -m $file)
16975                 [ $mdt_index == $MDTIDX ] ||
16976                         error "$file is not on MDT${MDTIDX}"
16977         done
16978
16979         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16980 }
16981 run_test 230c "check directory accessiblity if migration failed"
16982
16983 test_230d() {
16984         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16985         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16986         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16987                 skip "Need MDS version at least 2.11.52"
16988         # LU-11235
16989         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
16990
16991         local migrate_dir=$DIR/$tdir/migrate_dir
16992         local old_index
16993         local new_index
16994         local old_count
16995         local new_count
16996         local new_hash
16997         local mdt_index
16998         local i
16999         local j
17000
17001         old_index=$((RANDOM % MDSCOUNT))
17002         old_count=$((MDSCOUNT - old_index))
17003         new_index=$((RANDOM % MDSCOUNT))
17004         new_count=$((MDSCOUNT - new_index))
17005         new_hash="all_char"
17006
17007         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
17008         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
17009
17010         test_mkdir $DIR/$tdir
17011         test_mkdir -i $old_index -c $old_count $migrate_dir
17012
17013         for ((i=0; i<100; i++)); do
17014                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
17015                 createmany -o $migrate_dir/dir_${i}/f 100 ||
17016                         error "create files under remote dir failed $i"
17017         done
17018
17019         echo -n "Migrate from MDT$old_index "
17020         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
17021         echo -n "to MDT$new_index"
17022         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
17023         echo
17024
17025         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
17026         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
17027                 error "migrate remote dir error"
17028
17029         echo "Finish migration, then checking.."
17030         for file in $(find $migrate_dir); do
17031                 mdt_index=$($LFS getstripe -m $file)
17032                 if [ $mdt_index -lt $new_index ] ||
17033                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
17034                         error "$file is on MDT$mdt_index"
17035                 fi
17036         done
17037
17038         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17039 }
17040 run_test 230d "check migrate big directory"
17041
17042 test_230e() {
17043         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17044         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17045         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17046                 skip "Need MDS version at least 2.11.52"
17047
17048         local i
17049         local j
17050         local a_fid
17051         local b_fid
17052
17053         mkdir -p $DIR/$tdir
17054         mkdir $DIR/$tdir/migrate_dir
17055         mkdir $DIR/$tdir/other_dir
17056         touch $DIR/$tdir/migrate_dir/a
17057         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
17058         ls $DIR/$tdir/other_dir
17059
17060         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
17061                 error "migrate dir fails"
17062
17063         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
17064         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
17065
17066         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17067         [ $mdt_index == 0 ] || error "a is not on MDT0"
17068
17069         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
17070                 error "migrate dir fails"
17071
17072         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
17073         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
17074
17075         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17076         [ $mdt_index == 1 ] || error "a is not on MDT1"
17077
17078         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
17079         [ $mdt_index == 1 ] || error "b is not on MDT1"
17080
17081         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
17082         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
17083
17084         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
17085
17086         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17087 }
17088 run_test 230e "migrate mulitple local link files"
17089
17090 test_230f() {
17091         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17092         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17093         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17094                 skip "Need MDS version at least 2.11.52"
17095
17096         local a_fid
17097         local ln_fid
17098
17099         mkdir -p $DIR/$tdir
17100         mkdir $DIR/$tdir/migrate_dir
17101         $LFS mkdir -i1 $DIR/$tdir/other_dir
17102         touch $DIR/$tdir/migrate_dir/a
17103         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
17104         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
17105         ls $DIR/$tdir/other_dir
17106
17107         # a should be migrated to MDT1, since no other links on MDT0
17108         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
17109                 error "#1 migrate dir fails"
17110         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
17111         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
17112         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17113         [ $mdt_index == 1 ] || error "a is not on MDT1"
17114
17115         # a should stay on MDT1, because it is a mulitple link file
17116         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
17117                 error "#2 migrate dir fails"
17118         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17119         [ $mdt_index == 1 ] || error "a is not on MDT1"
17120
17121         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
17122                 error "#3 migrate dir fails"
17123
17124         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
17125         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
17126         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
17127
17128         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
17129         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
17130
17131         # a should be migrated to MDT0, since no other links on MDT1
17132         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
17133                 error "#4 migrate dir fails"
17134         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17135         [ $mdt_index == 0 ] || error "a is not on MDT0"
17136
17137         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17138 }
17139 run_test 230f "migrate mulitple remote link files"
17140
17141 test_230g() {
17142         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17143         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17144         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17145                 skip "Need MDS version at least 2.11.52"
17146
17147         mkdir -p $DIR/$tdir/migrate_dir
17148
17149         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
17150                 error "migrating dir to non-exist MDT succeeds"
17151         true
17152 }
17153 run_test 230g "migrate dir to non-exist MDT"
17154
17155 test_230h() {
17156         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17157         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17158         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17159                 skip "Need MDS version at least 2.11.52"
17160
17161         local mdt_index
17162
17163         mkdir -p $DIR/$tdir/migrate_dir
17164
17165         $LFS migrate -m1 $DIR &&
17166                 error "migrating mountpoint1 should fail"
17167
17168         $LFS migrate -m1 $DIR/$tdir/.. &&
17169                 error "migrating mountpoint2 should fail"
17170
17171         # same as mv
17172         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
17173                 error "migrating $tdir/migrate_dir/.. should fail"
17174
17175         true
17176 }
17177 run_test 230h "migrate .. and root"
17178
17179 test_230i() {
17180         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17181         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17182         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17183                 skip "Need MDS version at least 2.11.52"
17184
17185         mkdir -p $DIR/$tdir/migrate_dir
17186
17187         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
17188                 error "migration fails with a tailing slash"
17189
17190         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
17191                 error "migration fails with two tailing slashes"
17192 }
17193 run_test 230i "lfs migrate -m tolerates trailing slashes"
17194
17195 test_230j() {
17196         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17197         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17198                 skip "Need MDS version at least 2.11.52"
17199
17200         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
17201         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
17202                 error "create $tfile failed"
17203         cat /etc/passwd > $DIR/$tdir/$tfile
17204
17205         $LFS migrate -m 1 $DIR/$tdir
17206
17207         cmp /etc/passwd $DIR/$tdir/$tfile ||
17208                 error "DoM file mismatch after migration"
17209 }
17210 run_test 230j "DoM file data not changed after dir migration"
17211
17212 test_230k() {
17213         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
17214         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17215                 skip "Need MDS version at least 2.11.56"
17216
17217         local total=20
17218         local files_on_starting_mdt=0
17219
17220         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
17221         $LFS getdirstripe $DIR/$tdir
17222         for i in $(seq $total); do
17223                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
17224                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
17225                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17226         done
17227
17228         echo "$files_on_starting_mdt files on MDT0"
17229
17230         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
17231         $LFS getdirstripe $DIR/$tdir
17232
17233         files_on_starting_mdt=0
17234         for i in $(seq $total); do
17235                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
17236                         error "file $tfile.$i mismatch after migration"
17237                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
17238                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17239         done
17240
17241         echo "$files_on_starting_mdt files on MDT1 after migration"
17242         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
17243
17244         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
17245         $LFS getdirstripe $DIR/$tdir
17246
17247         files_on_starting_mdt=0
17248         for i in $(seq $total); do
17249                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
17250                         error "file $tfile.$i mismatch after 2nd migration"
17251                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
17252                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17253         done
17254
17255         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
17256         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
17257
17258         true
17259 }
17260 run_test 230k "file data not changed after dir migration"
17261
17262 test_230l() {
17263         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17264         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17265                 skip "Need MDS version at least 2.11.56"
17266
17267         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
17268         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
17269                 error "create files under remote dir failed $i"
17270         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
17271 }
17272 run_test 230l "readdir between MDTs won't crash"
17273
17274 test_230m() {
17275         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17276         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17277                 skip "Need MDS version at least 2.11.56"
17278
17279         local MDTIDX=1
17280         local mig_dir=$DIR/$tdir/migrate_dir
17281         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
17282         local shortstr="b"
17283         local val
17284
17285         echo "Creating files and dirs with xattrs"
17286         test_mkdir $DIR/$tdir
17287         test_mkdir -i0 -c1 $mig_dir
17288         mkdir $mig_dir/dir
17289         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
17290                 error "cannot set xattr attr1 on dir"
17291         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
17292                 error "cannot set xattr attr2 on dir"
17293         touch $mig_dir/dir/f0
17294         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
17295                 error "cannot set xattr attr1 on file"
17296         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
17297                 error "cannot set xattr attr2 on file"
17298         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17299         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17300         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
17301         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17302         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
17303         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17304         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
17305         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17306         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
17307
17308         echo "Migrating to MDT1"
17309         $LFS migrate -m $MDTIDX $mig_dir ||
17310                 error "fails on migrating dir to MDT1"
17311
17312         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17313         echo "Checking xattrs"
17314         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17315         [ "$val" = $longstr ] ||
17316                 error "expecting xattr1 $longstr on dir, found $val"
17317         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17318         [ "$val" = $shortstr ] ||
17319                 error "expecting xattr2 $shortstr on dir, found $val"
17320         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17321         [ "$val" = $longstr ] ||
17322                 error "expecting xattr1 $longstr on file, found $val"
17323         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17324         [ "$val" = $shortstr ] ||
17325                 error "expecting xattr2 $shortstr on file, found $val"
17326 }
17327 run_test 230m "xattrs not changed after dir migration"
17328
17329 test_231a()
17330 {
17331         # For simplicity this test assumes that max_pages_per_rpc
17332         # is the same across all OSCs
17333         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
17334         local bulk_size=$((max_pages * PAGE_SIZE))
17335         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
17336                                        head -n 1)
17337
17338         mkdir -p $DIR/$tdir
17339         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
17340                 error "failed to set stripe with -S ${brw_size}M option"
17341
17342         # clear the OSC stats
17343         $LCTL set_param osc.*.stats=0 &>/dev/null
17344         stop_writeback
17345
17346         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
17347         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
17348                 oflag=direct &>/dev/null || error "dd failed"
17349
17350         sync; sleep 1; sync # just to be safe
17351         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
17352         if [ x$nrpcs != "x1" ]; then
17353                 $LCTL get_param osc.*.stats
17354                 error "found $nrpcs ost_write RPCs, not 1 as expected"
17355         fi
17356
17357         start_writeback
17358         # Drop the OSC cache, otherwise we will read from it
17359         cancel_lru_locks osc
17360
17361         # clear the OSC stats
17362         $LCTL set_param osc.*.stats=0 &>/dev/null
17363
17364         # Client reads $bulk_size.
17365         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
17366                 iflag=direct &>/dev/null || error "dd failed"
17367
17368         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
17369         if [ x$nrpcs != "x1" ]; then
17370                 $LCTL get_param osc.*.stats
17371                 error "found $nrpcs ost_read RPCs, not 1 as expected"
17372         fi
17373 }
17374 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
17375
17376 test_231b() {
17377         mkdir -p $DIR/$tdir
17378         local i
17379         for i in {0..1023}; do
17380                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
17381                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
17382                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
17383         done
17384         sync
17385 }
17386 run_test 231b "must not assert on fully utilized OST request buffer"
17387
17388 test_232a() {
17389         mkdir -p $DIR/$tdir
17390         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17391
17392         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17393         do_facet ost1 $LCTL set_param fail_loc=0x31c
17394
17395         # ignore dd failure
17396         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
17397
17398         do_facet ost1 $LCTL set_param fail_loc=0
17399         umount_client $MOUNT || error "umount failed"
17400         mount_client $MOUNT || error "mount failed"
17401         stop ost1 || error "cannot stop ost1"
17402         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17403 }
17404 run_test 232a "failed lock should not block umount"
17405
17406 test_232b() {
17407         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
17408                 skip "Need MDS version at least 2.10.58"
17409
17410         mkdir -p $DIR/$tdir
17411         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17412         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
17413         sync
17414         cancel_lru_locks osc
17415
17416         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17417         do_facet ost1 $LCTL set_param fail_loc=0x31c
17418
17419         # ignore failure
17420         $LFS data_version $DIR/$tdir/$tfile || true
17421
17422         do_facet ost1 $LCTL set_param fail_loc=0
17423         umount_client $MOUNT || error "umount failed"
17424         mount_client $MOUNT || error "mount failed"
17425         stop ost1 || error "cannot stop ost1"
17426         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17427 }
17428 run_test 232b "failed data version lock should not block umount"
17429
17430 test_233a() {
17431         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
17432                 skip "Need MDS version at least 2.3.64"
17433         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17434
17435         local fid=$($LFS path2fid $MOUNT)
17436
17437         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17438                 error "cannot access $MOUNT using its FID '$fid'"
17439 }
17440 run_test 233a "checking that OBF of the FS root succeeds"
17441
17442 test_233b() {
17443         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
17444                 skip "Need MDS version at least 2.5.90"
17445         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17446
17447         local fid=$($LFS path2fid $MOUNT/.lustre)
17448
17449         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17450                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
17451
17452         fid=$($LFS path2fid $MOUNT/.lustre/fid)
17453         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17454                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
17455 }
17456 run_test 233b "checking that OBF of the FS .lustre succeeds"
17457
17458 test_234() {
17459         local p="$TMP/sanityN-$TESTNAME.parameters"
17460         save_lustre_params client "llite.*.xattr_cache" > $p
17461         lctl set_param llite.*.xattr_cache 1 ||
17462                 skip_env "xattr cache is not supported"
17463
17464         mkdir -p $DIR/$tdir || error "mkdir failed"
17465         touch $DIR/$tdir/$tfile || error "touch failed"
17466         # OBD_FAIL_LLITE_XATTR_ENOMEM
17467         $LCTL set_param fail_loc=0x1405
17468         getfattr -n user.attr $DIR/$tdir/$tfile &&
17469                 error "getfattr should have failed with ENOMEM"
17470         $LCTL set_param fail_loc=0x0
17471         rm -rf $DIR/$tdir
17472
17473         restore_lustre_params < $p
17474         rm -f $p
17475 }
17476 run_test 234 "xattr cache should not crash on ENOMEM"
17477
17478 test_235() {
17479         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
17480                 skip "Need MDS version at least 2.4.52"
17481
17482         flock_deadlock $DIR/$tfile
17483         local RC=$?
17484         case $RC in
17485                 0)
17486                 ;;
17487                 124) error "process hangs on a deadlock"
17488                 ;;
17489                 *) error "error executing flock_deadlock $DIR/$tfile"
17490                 ;;
17491         esac
17492 }
17493 run_test 235 "LU-1715: flock deadlock detection does not work properly"
17494
17495 #LU-2935
17496 test_236() {
17497         check_swap_layouts_support
17498
17499         local ref1=/etc/passwd
17500         local ref2=/etc/group
17501         local file1=$DIR/$tdir/f1
17502         local file2=$DIR/$tdir/f2
17503
17504         test_mkdir -c1 $DIR/$tdir
17505         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
17506         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
17507         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
17508         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
17509         local fd=$(free_fd)
17510         local cmd="exec $fd<>$file2"
17511         eval $cmd
17512         rm $file2
17513         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
17514                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
17515         cmd="exec $fd>&-"
17516         eval $cmd
17517         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
17518
17519         #cleanup
17520         rm -rf $DIR/$tdir
17521 }
17522 run_test 236 "Layout swap on open unlinked file"
17523
17524 # LU-4659 linkea consistency
17525 test_238() {
17526         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
17527                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
17528                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
17529                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
17530
17531         touch $DIR/$tfile
17532         ln $DIR/$tfile $DIR/$tfile.lnk
17533         touch $DIR/$tfile.new
17534         mv $DIR/$tfile.new $DIR/$tfile
17535         local fid1=$($LFS path2fid $DIR/$tfile)
17536         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
17537         local path1=$($LFS fid2path $FSNAME "$fid1")
17538         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
17539         local path2=$($LFS fid2path $FSNAME "$fid2")
17540         [ $tfile.lnk == $path2 ] ||
17541                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
17542         rm -f $DIR/$tfile*
17543 }
17544 run_test 238 "Verify linkea consistency"
17545
17546 test_239A() { # was test_239
17547         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
17548                 skip "Need MDS version at least 2.5.60"
17549
17550         local list=$(comma_list $(mdts_nodes))
17551
17552         mkdir -p $DIR/$tdir
17553         createmany -o $DIR/$tdir/f- 5000
17554         unlinkmany $DIR/$tdir/f- 5000
17555         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
17556                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
17557         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
17558                         osp.*MDT*.sync_in_flight" | calc_sum)
17559         [ "$changes" -eq 0 ] || error "$changes not synced"
17560 }
17561 run_test 239A "osp_sync test"
17562
17563 test_239a() { #LU-5297
17564         remote_mds_nodsh && skip "remote MDS with nodsh"
17565
17566         touch $DIR/$tfile
17567         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
17568         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
17569         chgrp $RUNAS_GID $DIR/$tfile
17570         wait_delete_completed
17571 }
17572 run_test 239a "process invalid osp sync record correctly"
17573
17574 test_239b() { #LU-5297
17575         remote_mds_nodsh && skip "remote MDS with nodsh"
17576
17577         touch $DIR/$tfile1
17578         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
17579         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
17580         chgrp $RUNAS_GID $DIR/$tfile1
17581         wait_delete_completed
17582         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17583         touch $DIR/$tfile2
17584         chgrp $RUNAS_GID $DIR/$tfile2
17585         wait_delete_completed
17586 }
17587 run_test 239b "process osp sync record with ENOMEM error correctly"
17588
17589 test_240() {
17590         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17591         remote_mds_nodsh && skip "remote MDS with nodsh"
17592
17593         mkdir -p $DIR/$tdir
17594
17595         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
17596                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
17597         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
17598                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
17599
17600         umount_client $MOUNT || error "umount failed"
17601         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
17602         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
17603         mount_client $MOUNT || error "failed to mount client"
17604
17605         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
17606         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
17607 }
17608 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
17609
17610 test_241_bio() {
17611         local count=$1
17612         local bsize=$2
17613
17614         for LOOP in $(seq $count); do
17615                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
17616                 cancel_lru_locks $OSC || true
17617         done
17618 }
17619
17620 test_241_dio() {
17621         local count=$1
17622         local bsize=$2
17623
17624         for LOOP in $(seq $1); do
17625                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
17626                         2>/dev/null
17627         done
17628 }
17629
17630 test_241a() { # was test_241
17631         local bsize=$PAGE_SIZE
17632
17633         (( bsize < 40960 )) && bsize=40960
17634         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17635         ls -la $DIR/$tfile
17636         cancel_lru_locks $OSC
17637         test_241_bio 1000 $bsize &
17638         PID=$!
17639         test_241_dio 1000 $bsize
17640         wait $PID
17641 }
17642 run_test 241a "bio vs dio"
17643
17644 test_241b() {
17645         local bsize=$PAGE_SIZE
17646
17647         (( bsize < 40960 )) && bsize=40960
17648         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17649         ls -la $DIR/$tfile
17650         test_241_dio 1000 $bsize &
17651         PID=$!
17652         test_241_dio 1000 $bsize
17653         wait $PID
17654 }
17655 run_test 241b "dio vs dio"
17656
17657 test_242() {
17658         remote_mds_nodsh && skip "remote MDS with nodsh"
17659
17660         mkdir -p $DIR/$tdir
17661         touch $DIR/$tdir/$tfile
17662
17663         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
17664         do_facet mds1 lctl set_param fail_loc=0x105
17665         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
17666
17667         do_facet mds1 lctl set_param fail_loc=0
17668         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
17669 }
17670 run_test 242 "mdt_readpage failure should not cause directory unreadable"
17671
17672 test_243()
17673 {
17674         test_mkdir $DIR/$tdir
17675         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
17676 }
17677 run_test 243 "various group lock tests"
17678
17679 test_244a()
17680 {
17681         test_mkdir $DIR/$tdir
17682         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
17683         sendfile_grouplock $DIR/$tdir/$tfile || \
17684                 error "sendfile+grouplock failed"
17685         rm -rf $DIR/$tdir
17686 }
17687 run_test 244a "sendfile with group lock tests"
17688
17689 test_244b()
17690 {
17691         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
17692
17693         local threads=50
17694         local size=$((1024*1024))
17695
17696         test_mkdir $DIR/$tdir
17697         for i in $(seq 1 $threads); do
17698                 local file=$DIR/$tdir/file_$((i / 10))
17699                 $MULTIOP $file OG1234w$size_$((i % 3))w$size_$((i % 4))g1234c &
17700                 local pids[$i]=$!
17701         done
17702         for i in $(seq 1 $threads); do
17703                 wait ${pids[$i]}
17704         done
17705 }
17706 run_test 244b "multi-threaded write with group lock"
17707
17708 test_245() {
17709         local flagname="multi_mod_rpcs"
17710         local connect_data_name="max_mod_rpcs"
17711         local out
17712
17713         # check if multiple modify RPCs flag is set
17714         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
17715                 grep "connect_flags:")
17716         echo "$out"
17717
17718         echo "$out" | grep -qw $flagname
17719         if [ $? -ne 0 ]; then
17720                 echo "connect flag $flagname is not set"
17721                 return
17722         fi
17723
17724         # check if multiple modify RPCs data is set
17725         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
17726         echo "$out"
17727
17728         echo "$out" | grep -qw $connect_data_name ||
17729                 error "import should have connect data $connect_data_name"
17730 }
17731 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
17732
17733 cleanup_247() {
17734         local submount=$1
17735
17736         trap 0
17737         umount_client $submount
17738         rmdir $submount
17739 }
17740
17741 test_247a() {
17742         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17743                 grep -q subtree ||
17744                 skip_env "Fileset feature is not supported"
17745
17746         local submount=${MOUNT}_$tdir
17747
17748         mkdir $MOUNT/$tdir
17749         mkdir -p $submount || error "mkdir $submount failed"
17750         FILESET="$FILESET/$tdir" mount_client $submount ||
17751                 error "mount $submount failed"
17752         trap "cleanup_247 $submount" EXIT
17753         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
17754         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
17755                 error "read $MOUNT/$tdir/$tfile failed"
17756         cleanup_247 $submount
17757 }
17758 run_test 247a "mount subdir as fileset"
17759
17760 test_247b() {
17761         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17762                 skip_env "Fileset feature is not supported"
17763
17764         local submount=${MOUNT}_$tdir
17765
17766         rm -rf $MOUNT/$tdir
17767         mkdir -p $submount || error "mkdir $submount failed"
17768         SKIP_FILESET=1
17769         FILESET="$FILESET/$tdir" mount_client $submount &&
17770                 error "mount $submount should fail"
17771         rmdir $submount
17772 }
17773 run_test 247b "mount subdir that dose not exist"
17774
17775 test_247c() {
17776         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17777                 skip_env "Fileset feature is not supported"
17778
17779         local submount=${MOUNT}_$tdir
17780
17781         mkdir -p $MOUNT/$tdir/dir1
17782         mkdir -p $submount || error "mkdir $submount failed"
17783         trap "cleanup_247 $submount" EXIT
17784         FILESET="$FILESET/$tdir" mount_client $submount ||
17785                 error "mount $submount failed"
17786         local fid=$($LFS path2fid $MOUNT/)
17787         $LFS fid2path $submount $fid && error "fid2path should fail"
17788         cleanup_247 $submount
17789 }
17790 run_test 247c "running fid2path outside root"
17791
17792 test_247d() {
17793         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17794                 skip "Fileset feature is not supported"
17795
17796         local submount=${MOUNT}_$tdir
17797
17798         mkdir -p $MOUNT/$tdir/dir1
17799         mkdir -p $submount || error "mkdir $submount failed"
17800         FILESET="$FILESET/$tdir" mount_client $submount ||
17801                 error "mount $submount failed"
17802         trap "cleanup_247 $submount" EXIT
17803         local fid=$($LFS path2fid $submount/dir1)
17804         $LFS fid2path $submount $fid || error "fid2path should succeed"
17805         cleanup_247 $submount
17806 }
17807 run_test 247d "running fid2path inside root"
17808
17809 # LU-8037
17810 test_247e() {
17811         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17812                 grep -q subtree ||
17813                 skip "Fileset feature is not supported"
17814
17815         local submount=${MOUNT}_$tdir
17816
17817         mkdir $MOUNT/$tdir
17818         mkdir -p $submount || error "mkdir $submount failed"
17819         FILESET="$FILESET/.." mount_client $submount &&
17820                 error "mount $submount should fail"
17821         rmdir $submount
17822 }
17823 run_test 247e "mount .. as fileset"
17824
17825 test_248a() {
17826         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
17827         [ -z "$fast_read_sav" ] && skip "no fast read support"
17828
17829         # create a large file for fast read verification
17830         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
17831
17832         # make sure the file is created correctly
17833         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
17834                 { rm -f $DIR/$tfile; skip "file creation error"; }
17835
17836         echo "Test 1: verify that fast read is 4 times faster on cache read"
17837
17838         # small read with fast read enabled
17839         $LCTL set_param -n llite.*.fast_read=1
17840         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17841                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17842                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17843         # small read with fast read disabled
17844         $LCTL set_param -n llite.*.fast_read=0
17845         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17846                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17847                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17848
17849         # verify that fast read is 4 times faster for cache read
17850         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
17851                 error_not_in_vm "fast read was not 4 times faster: " \
17852                            "$t_fast vs $t_slow"
17853
17854         echo "Test 2: verify the performance between big and small read"
17855         $LCTL set_param -n llite.*.fast_read=1
17856
17857         # 1k non-cache read
17858         cancel_lru_locks osc
17859         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17860                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17861                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17862
17863         # 1M non-cache read
17864         cancel_lru_locks osc
17865         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17866                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17867                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17868
17869         # verify that big IO is not 4 times faster than small IO
17870         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
17871                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
17872
17873         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
17874         rm -f $DIR/$tfile
17875 }
17876 run_test 248a "fast read verification"
17877
17878 test_248b() {
17879         # Default short_io_bytes=16384, try both smaller and larger sizes.
17880         # Lustre O_DIRECT read and write needs to be a multiple of PAGE_SIZE.
17881         # 6017024 = 2^12*13*113 = 47008*128 = 11752*512 = 4096*1469 = 53248*113
17882         echo "bs=53248 count=113 normal buffered write"
17883         dd if=/dev/urandom of=$TMP/$tfile.0 bs=53248 count=113 ||
17884                 error "dd of initial data file failed"
17885         stack_trap "rm -f $DIR/$tfile.[0-3] $TMP/$tfile.[0-3]" EXIT
17886
17887         echo "bs=47008 count=128 oflag=dsync normal write $tfile.0"
17888         dd if=$TMP/$tfile.0 of=$DIR/$tfile.0 bs=47008 count=128 oflag=dsync ||
17889                 error "dd with sync normal writes failed"
17890         cmp $TMP/$tfile.0 $DIR/$tfile.0 || error "compare $DIR/$tfile.0 failed"
17891
17892         echo "bs=11752 count=512 oflag=dsync small write $tfile.1"
17893         dd if=$TMP/$tfile.0 of=$DIR/$tfile.1 bs=11752 count=512 oflag=dsync ||
17894                 error "dd with sync small writes failed"
17895         cmp $TMP/$tfile.0 $DIR/$tfile.1 || error "compare $DIR/$tfile.1 failed"
17896
17897         cancel_lru_locks osc
17898
17899         # calculate the small O_DIRECT size and count for the client PAGE_SIZE
17900         local num=$((13 * 113 / (PAGE_SIZE / 4096)))
17901         echo "bs=$PAGE_SIZE count=$num iflag=direct small read $tfile.1"
17902         dd if=$DIR/$tfile.1 of=$TMP/$tfile.1 bs=$PAGE_SIZE count=$num \
17903                 iflag=direct || error "dd with O_DIRECT small read failed"
17904         # adjust bytes checked to handle larger PAGE_SIZE for ARM/PPC
17905         cmp --bytes=$((PAGE_SIZE * num)) $TMP/$tfile.0 $TMP/$tfile.1 ||
17906                 error "compare $TMP/$tfile.1 failed"
17907
17908         local save=$($LCTL get_param -n osc.*OST000*.short_io_bytes | head -n 1)
17909         stack_trap "$LCTL set_param osc.$FSNAME-*.short_io_bytes=$save" EXIT
17910
17911         # just to see what the maximum tunable value is, and test parsing
17912         echo "test invalid parameter 2MB"
17913         $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=2M &&
17914                 error "too-large short_io_bytes allowed"
17915         echo "test maximum parameter 512KB"
17916         # if we can set a larger short_io_bytes, run test regardless of version
17917         if ! $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=512K; then
17918                 # older clients may not allow setting it this large, that's OK
17919                 [ $CLIENT_VERSION -ge $(version_code 2.13.50) ] ||
17920                         skip "Need at least client version 2.13.50"
17921                 error "medium short_io_bytes failed"
17922         fi
17923         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
17924         size=$($LCTL get_param -n osc.$FSNAME-OST0000*.short_io_bytes)
17925
17926         echo "test large parameter 64KB"
17927         $LCTL set_param osc.$FSNAME-*.short_io_bytes=65536
17928         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
17929
17930         echo "bs=47008 count=128 oflag=dsync large write $tfile.2"
17931         dd if=$TMP/$tfile.0 of=$DIR/$tfile.2 bs=47008 count=128 oflag=dsync ||
17932                 error "dd with sync large writes failed"
17933         cmp $TMP/$tfile.0 $DIR/$tfile.2 || error "compare $DIR/$tfile.2 failed"
17934
17935         # calculate the large O_DIRECT size and count for the client PAGE_SIZE
17936         local size=$(((4096 * 13 + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE))
17937         num=$((113 * 4096 / PAGE_SIZE))
17938         echo "bs=$size count=$num oflag=direct large write $tfile.3"
17939         dd if=$TMP/$tfile.0 of=$DIR/$tfile.3 bs=$size count=$num oflag=direct ||
17940                 error "dd with O_DIRECT large writes failed"
17941         cmp --bytes=$((size * num)) $TMP/$tfile.0 $DIR/$tfile.3 ||
17942                 error "compare $DIR/$tfile.3 failed"
17943
17944         cancel_lru_locks osc
17945
17946         echo "bs=$size count=$num iflag=direct large read $tfile.2"
17947         dd if=$DIR/$tfile.2 of=$TMP/$tfile.2 bs=$size count=$num iflag=direct ||
17948                 error "dd with O_DIRECT large read failed"
17949         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.2 ||
17950                 error "compare $TMP/$tfile.2 failed"
17951
17952         echo "bs=$size count=$num iflag=direct large read $tfile.3"
17953         dd if=$DIR/$tfile.3 of=$TMP/$tfile.3 bs=$size count=$num iflag=direct ||
17954                 error "dd with O_DIRECT large read failed"
17955         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.3 ||
17956                 error "compare $TMP/$tfile.3 failed"
17957 }
17958 run_test 248b "test short_io read and write for both small and large sizes"
17959
17960 test_249() { # LU-7890
17961         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
17962                 skip "Need at least version 2.8.54"
17963
17964         rm -f $DIR/$tfile
17965         $LFS setstripe -c 1 $DIR/$tfile
17966         # Offset 2T == 4k * 512M
17967         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
17968                 error "dd to 2T offset failed"
17969 }
17970 run_test 249 "Write above 2T file size"
17971
17972 test_250() {
17973         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
17974          && skip "no 16TB file size limit on ZFS"
17975
17976         $LFS setstripe -c 1 $DIR/$tfile
17977         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
17978         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
17979         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
17980         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
17981                 conv=notrunc,fsync && error "append succeeded"
17982         return 0
17983 }
17984 run_test 250 "Write above 16T limit"
17985
17986 test_251() {
17987         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
17988
17989         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
17990         #Skip once - writing the first stripe will succeed
17991         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17992         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
17993                 error "short write happened"
17994
17995         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17996         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
17997                 error "short read happened"
17998
17999         rm -f $DIR/$tfile
18000 }
18001 run_test 251 "Handling short read and write correctly"
18002
18003 test_252() {
18004         remote_mds_nodsh && skip "remote MDS with nodsh"
18005         remote_ost_nodsh && skip "remote OST with nodsh"
18006         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
18007                 skip_env "ldiskfs only test"
18008         fi
18009
18010         local tgt
18011         local dev
18012         local out
18013         local uuid
18014         local num
18015         local gen
18016
18017         # check lr_reader on OST0000
18018         tgt=ost1
18019         dev=$(facet_device $tgt)
18020         out=$(do_facet $tgt $LR_READER $dev)
18021         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
18022         echo "$out"
18023         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
18024         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
18025                 error "Invalid uuid returned by $LR_READER on target $tgt"
18026         echo -e "uuid returned by $LR_READER is '$uuid'\n"
18027
18028         # check lr_reader -c on MDT0000
18029         tgt=mds1
18030         dev=$(facet_device $tgt)
18031         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
18032                 skip "$LR_READER does not support additional options"
18033         fi
18034         out=$(do_facet $tgt $LR_READER -c $dev)
18035         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
18036         echo "$out"
18037         num=$(echo "$out" | grep -c "mdtlov")
18038         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
18039                 error "Invalid number of mdtlov clients returned by $LR_READER"
18040         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
18041
18042         # check lr_reader -cr on MDT0000
18043         out=$(do_facet $tgt $LR_READER -cr $dev)
18044         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
18045         echo "$out"
18046         echo "$out" | grep -q "^reply_data:$" ||
18047                 error "$LR_READER should have returned 'reply_data' section"
18048         num=$(echo "$out" | grep -c "client_generation")
18049         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
18050 }
18051 run_test 252 "check lr_reader tool"
18052
18053 test_253() {
18054         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18055         remote_mds_nodsh && skip "remote MDS with nodsh"
18056         remote_mgs_nodsh && skip "remote MGS with nodsh"
18057
18058         local ostidx=0
18059         local rc=0
18060         local ost_name=$(ostname_from_index $ostidx)
18061
18062         # on the mdt's osc
18063         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
18064         do_facet $SINGLEMDS $LCTL get_param -n \
18065                 osp.$mdtosc_proc1.reserved_mb_high ||
18066                 skip  "remote MDS does not support reserved_mb_high"
18067
18068         rm -rf $DIR/$tdir
18069         wait_mds_ost_sync
18070         wait_delete_completed
18071         mkdir $DIR/$tdir
18072
18073         pool_add $TESTNAME || error "Pool creation failed"
18074         pool_add_targets $TESTNAME 0 || error "Pool add targets failed"
18075
18076         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
18077                 error "Setstripe failed"
18078
18079         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M count=10
18080
18081         local wms=$(ost_watermarks_set_enospc $tfile $ostidx |
18082                     grep "watermarks")
18083         stack_trap "ost_watermarks_clear_enospc $tfile $ostidx $wms" EXIT
18084
18085         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
18086                         osp.$mdtosc_proc1.prealloc_status)
18087         echo "prealloc_status $oa_status"
18088
18089         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1M count=1 &&
18090                 error "File creation should fail"
18091
18092         #object allocation was stopped, but we still able to append files
18093         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M seek=6 count=5 \
18094                 oflag=append || error "Append failed"
18095
18096         rm -f $DIR/$tdir/$tfile.0
18097
18098         # For this test, we want to delete the files we created to go out of
18099         # space but leave the watermark, so we remain nearly out of space
18100         ost_watermarks_enospc_delete_files $tfile $ostidx
18101
18102         wait_delete_completed
18103
18104         sleep_maxage
18105
18106         for i in $(seq 10 12); do
18107                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$i bs=1M count=1 \
18108                         2>/dev/null || error "File creation failed after rm"
18109         done
18110
18111         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
18112                         osp.$mdtosc_proc1.prealloc_status)
18113         echo "prealloc_status $oa_status"
18114
18115         if (( oa_status != 0 )); then
18116                 error "Object allocation still disable after rm"
18117         fi
18118 }
18119 run_test 253 "Check object allocation limit"
18120
18121 test_254() {
18122         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18123         remote_mds_nodsh && skip "remote MDS with nodsh"
18124         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
18125                 skip "MDS does not support changelog_size"
18126
18127         local cl_user
18128         local MDT0=$(facet_svc $SINGLEMDS)
18129
18130         changelog_register || error "changelog_register failed"
18131
18132         changelog_clear 0 || error "changelog_clear failed"
18133
18134         local size1=$(do_facet $SINGLEMDS \
18135                       $LCTL get_param -n mdd.$MDT0.changelog_size)
18136         echo "Changelog size $size1"
18137
18138         rm -rf $DIR/$tdir
18139         $LFS mkdir -i 0 $DIR/$tdir
18140         # change something
18141         mkdir -p $DIR/$tdir/pics/2008/zachy
18142         touch $DIR/$tdir/pics/2008/zachy/timestamp
18143         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
18144         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
18145         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
18146         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
18147         rm $DIR/$tdir/pics/desktop.jpg
18148
18149         local size2=$(do_facet $SINGLEMDS \
18150                       $LCTL get_param -n mdd.$MDT0.changelog_size)
18151         echo "Changelog size after work $size2"
18152
18153         (( $size2 > $size1 )) ||
18154                 error "new Changelog size=$size2 less than old size=$size1"
18155 }
18156 run_test 254 "Check changelog size"
18157
18158 ladvise_no_type()
18159 {
18160         local type=$1
18161         local file=$2
18162
18163         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
18164                 awk -F: '{print $2}' | grep $type > /dev/null
18165         if [ $? -ne 0 ]; then
18166                 return 0
18167         fi
18168         return 1
18169 }
18170
18171 ladvise_no_ioctl()
18172 {
18173         local file=$1
18174
18175         lfs ladvise -a willread $file > /dev/null 2>&1
18176         if [ $? -eq 0 ]; then
18177                 return 1
18178         fi
18179
18180         lfs ladvise -a willread $file 2>&1 |
18181                 grep "Inappropriate ioctl for device" > /dev/null
18182         if [ $? -eq 0 ]; then
18183                 return 0
18184         fi
18185         return 1
18186 }
18187
18188 percent() {
18189         bc <<<"scale=2; ($1 - $2) * 100 / $2"
18190 }
18191
18192 # run a random read IO workload
18193 # usage: random_read_iops <filename> <filesize> <iosize>
18194 random_read_iops() {
18195         local file=$1
18196         local fsize=$2
18197         local iosize=${3:-4096}
18198
18199         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
18200                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
18201 }
18202
18203 drop_file_oss_cache() {
18204         local file="$1"
18205         local nodes="$2"
18206
18207         $LFS ladvise -a dontneed $file 2>/dev/null ||
18208                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
18209 }
18210
18211 ladvise_willread_performance()
18212 {
18213         local repeat=10
18214         local average_origin=0
18215         local average_cache=0
18216         local average_ladvise=0
18217
18218         for ((i = 1; i <= $repeat; i++)); do
18219                 echo "Iter $i/$repeat: reading without willread hint"
18220                 cancel_lru_locks osc
18221                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
18222                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
18223                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
18224                 average_origin=$(bc <<<"$average_origin + $speed_origin")
18225
18226                 cancel_lru_locks osc
18227                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
18228                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
18229                 average_cache=$(bc <<<"$average_cache + $speed_cache")
18230
18231                 cancel_lru_locks osc
18232                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
18233                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
18234                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
18235                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
18236                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
18237         done
18238         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
18239         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
18240         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
18241
18242         speedup_cache=$(percent $average_cache $average_origin)
18243         speedup_ladvise=$(percent $average_ladvise $average_origin)
18244
18245         echo "Average uncached read: $average_origin"
18246         echo "Average speedup with OSS cached read: " \
18247                 "$average_cache = +$speedup_cache%"
18248         echo "Average speedup with ladvise willread: " \
18249                 "$average_ladvise = +$speedup_ladvise%"
18250
18251         local lowest_speedup=20
18252         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
18253                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
18254                         "got $average_cache%. Skipping ladvise willread check."
18255                 return 0
18256         fi
18257
18258         # the test won't work on ZFS until it supports 'ladvise dontneed', but
18259         # it is still good to run until then to exercise 'ladvise willread'
18260         ! $LFS ladvise -a dontneed $DIR/$tfile &&
18261                 [ "$ost1_FSTYPE" = "zfs" ] &&
18262                 echo "osd-zfs does not support dontneed or drop_caches" &&
18263                 return 0
18264
18265         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
18266         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
18267                 error_not_in_vm "Speedup with willread is less than " \
18268                         "$lowest_speedup%, got $average_ladvise%"
18269 }
18270
18271 test_255a() {
18272         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
18273                 skip "lustre < 2.8.54 does not support ladvise "
18274         remote_ost_nodsh && skip "remote OST with nodsh"
18275
18276         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
18277
18278         ladvise_no_type willread $DIR/$tfile &&
18279                 skip "willread ladvise is not supported"
18280
18281         ladvise_no_ioctl $DIR/$tfile &&
18282                 skip "ladvise ioctl is not supported"
18283
18284         local size_mb=100
18285         local size=$((size_mb * 1048576))
18286         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
18287                 error "dd to $DIR/$tfile failed"
18288
18289         lfs ladvise -a willread $DIR/$tfile ||
18290                 error "Ladvise failed with no range argument"
18291
18292         lfs ladvise -a willread -s 0 $DIR/$tfile ||
18293                 error "Ladvise failed with no -l or -e argument"
18294
18295         lfs ladvise -a willread -e 1 $DIR/$tfile ||
18296                 error "Ladvise failed with only -e argument"
18297
18298         lfs ladvise -a willread -l 1 $DIR/$tfile ||
18299                 error "Ladvise failed with only -l argument"
18300
18301         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
18302                 error "End offset should not be smaller than start offset"
18303
18304         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
18305                 error "End offset should not be equal to start offset"
18306
18307         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
18308                 error "Ladvise failed with overflowing -s argument"
18309
18310         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
18311                 error "Ladvise failed with overflowing -e argument"
18312
18313         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
18314                 error "Ladvise failed with overflowing -l argument"
18315
18316         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
18317                 error "Ladvise succeeded with conflicting -l and -e arguments"
18318
18319         echo "Synchronous ladvise should wait"
18320         local delay=4
18321 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
18322         do_nodes $(comma_list $(osts_nodes)) \
18323                 $LCTL set_param fail_val=$delay fail_loc=0x237
18324
18325         local start_ts=$SECONDS
18326         lfs ladvise -a willread $DIR/$tfile ||
18327                 error "Ladvise failed with no range argument"
18328         local end_ts=$SECONDS
18329         local inteval_ts=$((end_ts - start_ts))
18330
18331         if [ $inteval_ts -lt $(($delay - 1)) ]; then
18332                 error "Synchronous advice didn't wait reply"
18333         fi
18334
18335         echo "Asynchronous ladvise shouldn't wait"
18336         local start_ts=$SECONDS
18337         lfs ladvise -a willread -b $DIR/$tfile ||
18338                 error "Ladvise failed with no range argument"
18339         local end_ts=$SECONDS
18340         local inteval_ts=$((end_ts - start_ts))
18341
18342         if [ $inteval_ts -gt $(($delay / 2)) ]; then
18343                 error "Asynchronous advice blocked"
18344         fi
18345
18346         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
18347         ladvise_willread_performance
18348 }
18349 run_test 255a "check 'lfs ladvise -a willread'"
18350
18351 facet_meminfo() {
18352         local facet=$1
18353         local info=$2
18354
18355         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
18356 }
18357
18358 test_255b() {
18359         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
18360                 skip "lustre < 2.8.54 does not support ladvise "
18361         remote_ost_nodsh && skip "remote OST with nodsh"
18362
18363         lfs setstripe -c 1 -i 0 $DIR/$tfile
18364
18365         ladvise_no_type dontneed $DIR/$tfile &&
18366                 skip "dontneed ladvise is not supported"
18367
18368         ladvise_no_ioctl $DIR/$tfile &&
18369                 skip "ladvise ioctl is not supported"
18370
18371         ! $LFS ladvise -a dontneed $DIR/$tfile &&
18372                 [ "$ost1_FSTYPE" = "zfs" ] &&
18373                 skip "zfs-osd does not support 'ladvise dontneed'"
18374
18375         local size_mb=100
18376         local size=$((size_mb * 1048576))
18377         # In order to prevent disturbance of other processes, only check 3/4
18378         # of the memory usage
18379         local kibibytes=$((size_mb * 1024 * 3 / 4))
18380
18381         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
18382                 error "dd to $DIR/$tfile failed"
18383
18384         #force write to complete before dropping OST cache & checking memory
18385         sync
18386
18387         local total=$(facet_meminfo ost1 MemTotal)
18388         echo "Total memory: $total KiB"
18389
18390         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
18391         local before_read=$(facet_meminfo ost1 Cached)
18392         echo "Cache used before read: $before_read KiB"
18393
18394         lfs ladvise -a willread $DIR/$tfile ||
18395                 error "Ladvise willread failed"
18396         local after_read=$(facet_meminfo ost1 Cached)
18397         echo "Cache used after read: $after_read KiB"
18398
18399         lfs ladvise -a dontneed $DIR/$tfile ||
18400                 error "Ladvise dontneed again failed"
18401         local no_read=$(facet_meminfo ost1 Cached)
18402         echo "Cache used after dontneed ladvise: $no_read KiB"
18403
18404         if [ $total -lt $((before_read + kibibytes)) ]; then
18405                 echo "Memory is too small, abort checking"
18406                 return 0
18407         fi
18408
18409         if [ $((before_read + kibibytes)) -gt $after_read ]; then
18410                 error "Ladvise willread should use more memory" \
18411                         "than $kibibytes KiB"
18412         fi
18413
18414         if [ $((no_read + kibibytes)) -gt $after_read ]; then
18415                 error "Ladvise dontneed should release more memory" \
18416                         "than $kibibytes KiB"
18417         fi
18418 }
18419 run_test 255b "check 'lfs ladvise -a dontneed'"
18420
18421 test_255c() {
18422         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
18423                 skip "lustre < 2.10.50 does not support lockahead"
18424
18425         local count
18426         local new_count
18427         local difference
18428         local i
18429         local rc
18430
18431         test_mkdir -p $DIR/$tdir
18432         $LFS setstripe -i 0 -c 1 $DIR/$tdir
18433
18434         #test 10 returns only success/failure
18435         i=10
18436         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18437         rc=$?
18438         if [ $rc -eq 255 ]; then
18439                 error "Ladvise test${i} failed, ${rc}"
18440         fi
18441
18442         #test 11 counts lock enqueue requests, all others count new locks
18443         i=11
18444         count=$(do_facet ost1 \
18445                 $LCTL get_param -n ost.OSS.ost.stats)
18446         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
18447
18448         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18449         rc=$?
18450         if [ $rc -eq 255 ]; then
18451                 error "Ladvise test${i} failed, ${rc}"
18452         fi
18453
18454         new_count=$(do_facet ost1 \
18455                 $LCTL get_param -n ost.OSS.ost.stats)
18456         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
18457                    awk '{ print $2 }')
18458
18459         difference="$((new_count - count))"
18460         if [ $difference -ne $rc ]; then
18461                 error "Ladvise test${i}, bad enqueue count, returned " \
18462                       "${rc}, actual ${difference}"
18463         fi
18464
18465         for i in $(seq 12 21); do
18466                 # If we do not do this, we run the risk of having too many
18467                 # locks and starting lock cancellation while we are checking
18468                 # lock counts.
18469                 cancel_lru_locks osc
18470
18471                 count=$($LCTL get_param -n \
18472                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
18473
18474                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
18475                 rc=$?
18476                 if [ $rc -eq 255 ]; then
18477                         error "Ladvise test ${i} failed, ${rc}"
18478                 fi
18479
18480                 new_count=$($LCTL get_param -n \
18481                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
18482                 difference="$((new_count - count))"
18483
18484                 # Test 15 output is divided by 100 to map down to valid return
18485                 if [ $i -eq 15 ]; then
18486                         rc="$((rc * 100))"
18487                 fi
18488
18489                 if [ $difference -ne $rc ]; then
18490                         error "Ladvise test ${i}, bad lock count, returned " \
18491                               "${rc}, actual ${difference}"
18492                 fi
18493         done
18494
18495         #test 22 returns only success/failure
18496         i=22
18497         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18498         rc=$?
18499         if [ $rc -eq 255 ]; then
18500                 error "Ladvise test${i} failed, ${rc}"
18501         fi
18502 }
18503 run_test 255c "suite of ladvise lockahead tests"
18504
18505 test_256() {
18506         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18507         remote_mds_nodsh && skip "remote MDS with nodsh"
18508         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18509         changelog_users $SINGLEMDS | grep "^cl" &&
18510                 skip "active changelog user"
18511
18512         local cl_user
18513         local cat_sl
18514         local mdt_dev
18515
18516         mdt_dev=$(mdsdevname 1)
18517         echo $mdt_dev
18518
18519         changelog_register || error "changelog_register failed"
18520
18521         rm -rf $DIR/$tdir
18522         mkdir -p $DIR/$tdir
18523
18524         changelog_clear 0 || error "changelog_clear failed"
18525
18526         # change something
18527         touch $DIR/$tdir/{1..10}
18528
18529         # stop the MDT
18530         stop $SINGLEMDS || error "Fail to stop MDT"
18531
18532         # remount the MDT
18533
18534         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
18535
18536         #after mount new plainllog is used
18537         touch $DIR/$tdir/{11..19}
18538         local tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
18539         stack_trap "rm -f $tmpfile"
18540         cat_sl=$(do_facet $SINGLEMDS "sync; \
18541                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18542                  llog_reader $tmpfile | grep -c type=1064553b")
18543         do_facet $SINGLEMDS llog_reader $tmpfile
18544
18545         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
18546
18547         changelog_clear 0 || error "changelog_clear failed"
18548
18549         cat_sl=$(do_facet $SINGLEMDS "sync; \
18550                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18551                  llog_reader $tmpfile | grep -c type=1064553b")
18552
18553         if (( cat_sl == 2 )); then
18554                 error "Empty plain llog was not deleted from changelog catalog"
18555         elif (( cat_sl != 1 )); then
18556                 error "Active plain llog shouldn't be deleted from catalog"
18557         fi
18558 }
18559 run_test 256 "Check llog delete for empty and not full state"
18560
18561 test_257() {
18562         remote_mds_nodsh && skip "remote MDS with nodsh"
18563         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
18564                 skip "Need MDS version at least 2.8.55"
18565
18566         test_mkdir $DIR/$tdir
18567
18568         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
18569                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
18570         stat $DIR/$tdir
18571
18572 #define OBD_FAIL_MDS_XATTR_REP                  0x161
18573         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18574         local facet=mds$((mdtidx + 1))
18575         set_nodes_failloc $(facet_active_host $facet) 0x80000161
18576         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
18577
18578         stop $facet || error "stop MDS failed"
18579         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
18580                 error "start MDS fail"
18581         wait_recovery_complete $facet
18582 }
18583 run_test 257 "xattr locks are not lost"
18584
18585 # Verify we take the i_mutex when security requires it
18586 test_258a() {
18587 #define OBD_FAIL_IMUTEX_SEC 0x141c
18588         $LCTL set_param fail_loc=0x141c
18589         touch $DIR/$tfile
18590         chmod u+s $DIR/$tfile
18591         chmod a+rwx $DIR/$tfile
18592         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18593         RC=$?
18594         if [ $RC -ne 0 ]; then
18595                 error "error, failed to take i_mutex, rc=$?"
18596         fi
18597         rm -f $DIR/$tfile
18598 }
18599 run_test 258a "verify i_mutex security behavior when suid attributes is set"
18600
18601 # Verify we do NOT take the i_mutex in the normal case
18602 test_258b() {
18603 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
18604         $LCTL set_param fail_loc=0x141d
18605         touch $DIR/$tfile
18606         chmod a+rwx $DIR
18607         chmod a+rw $DIR/$tfile
18608         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18609         RC=$?
18610         if [ $RC -ne 0 ]; then
18611                 error "error, took i_mutex unnecessarily, rc=$?"
18612         fi
18613         rm -f $DIR/$tfile
18614
18615 }
18616 run_test 258b "verify i_mutex security behavior"
18617
18618 test_259() {
18619         local file=$DIR/$tfile
18620         local before
18621         local after
18622
18623         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18624
18625         stack_trap "rm -f $file" EXIT
18626
18627         wait_delete_completed
18628         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18629         echo "before: $before"
18630
18631         $LFS setstripe -i 0 -c 1 $file
18632         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
18633         sync_all_data
18634         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18635         echo "after write: $after"
18636
18637 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
18638         do_facet ost1 $LCTL set_param fail_loc=0x2301
18639         $TRUNCATE $file 0
18640         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18641         echo "after truncate: $after"
18642
18643         stop ost1
18644         do_facet ost1 $LCTL set_param fail_loc=0
18645         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
18646         sleep 2
18647         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18648         echo "after restart: $after"
18649         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
18650                 error "missing truncate?"
18651
18652         return 0
18653 }
18654 run_test 259 "crash at delayed truncate"
18655
18656 test_260() {
18657 #define OBD_FAIL_MDC_CLOSE               0x806
18658         $LCTL set_param fail_loc=0x80000806
18659         touch $DIR/$tfile
18660
18661 }
18662 run_test 260 "Check mdc_close fail"
18663
18664 ### Data-on-MDT sanity tests ###
18665 test_270a() {
18666         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18667                 skip "Need MDS version at least 2.10.55 for DoM"
18668
18669         # create DoM file
18670         local dom=$DIR/$tdir/dom_file
18671         local tmp=$DIR/$tdir/tmp_file
18672
18673         mkdir -p $DIR/$tdir
18674
18675         # basic checks for DoM component creation
18676         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
18677                 error "Can set MDT layout to non-first entry"
18678
18679         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
18680                 error "Can define multiple entries as MDT layout"
18681
18682         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
18683
18684         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
18685         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
18686         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
18687
18688         local mdtidx=$($LFS getstripe -m $dom)
18689         local mdtname=MDT$(printf %04x $mdtidx)
18690         local facet=mds$((mdtidx + 1))
18691         local space_check=1
18692
18693         # Skip free space checks with ZFS
18694         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
18695
18696         # write
18697         sync
18698         local size_tmp=$((65536 * 3))
18699         local mdtfree1=$(do_facet $facet \
18700                          lctl get_param -n osd*.*$mdtname.kbytesfree)
18701
18702         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18703         # check also direct IO along write
18704         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
18705         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18706         sync
18707         cmp $tmp $dom || error "file data is different"
18708         [ $(stat -c%s $dom) == $size_tmp ] ||
18709                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18710         if [ $space_check == 1 ]; then
18711                 local mdtfree2=$(do_facet $facet \
18712                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
18713
18714                 # increase in usage from by $size_tmp
18715                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18716                         error "MDT free space wrong after write: " \
18717                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18718         fi
18719
18720         # truncate
18721         local size_dom=10000
18722
18723         $TRUNCATE $dom $size_dom
18724         [ $(stat -c%s $dom) == $size_dom ] ||
18725                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
18726         if [ $space_check == 1 ]; then
18727                 mdtfree1=$(do_facet $facet \
18728                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18729                 # decrease in usage from $size_tmp to new $size_dom
18730                 [ $(($mdtfree1 - $mdtfree2)) -ge \
18731                   $(((size_tmp - size_dom) / 1024)) ] ||
18732                         error "MDT free space is wrong after truncate: " \
18733                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
18734         fi
18735
18736         # append
18737         cat $tmp >> $dom
18738         sync
18739         size_dom=$((size_dom + size_tmp))
18740         [ $(stat -c%s $dom) == $size_dom ] ||
18741                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
18742         if [ $space_check == 1 ]; then
18743                 mdtfree2=$(do_facet $facet \
18744                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18745                 # increase in usage by $size_tmp from previous
18746                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18747                         error "MDT free space is wrong after append: " \
18748                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18749         fi
18750
18751         # delete
18752         rm $dom
18753         if [ $space_check == 1 ]; then
18754                 mdtfree1=$(do_facet $facet \
18755                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18756                 # decrease in usage by $size_dom from previous
18757                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
18758                         error "MDT free space is wrong after removal: " \
18759                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
18760         fi
18761
18762         # combined striping
18763         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
18764                 error "Can't create DoM + OST striping"
18765
18766         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
18767         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18768         # check also direct IO along write
18769         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18770         sync
18771         cmp $tmp $dom || error "file data is different"
18772         [ $(stat -c%s $dom) == $size_tmp ] ||
18773                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18774         rm $dom $tmp
18775
18776         return 0
18777 }
18778 run_test 270a "DoM: basic functionality tests"
18779
18780 test_270b() {
18781         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18782                 skip "Need MDS version at least 2.10.55"
18783
18784         local dom=$DIR/$tdir/dom_file
18785         local max_size=1048576
18786
18787         mkdir -p $DIR/$tdir
18788         $LFS setstripe -E $max_size -L mdt $dom
18789
18790         # truncate over the limit
18791         $TRUNCATE $dom $(($max_size + 1)) &&
18792                 error "successful truncate over the maximum size"
18793         # write over the limit
18794         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
18795                 error "successful write over the maximum size"
18796         # append over the limit
18797         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
18798         echo "12345" >> $dom && error "successful append over the maximum size"
18799         rm $dom
18800
18801         return 0
18802 }
18803 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
18804
18805 test_270c() {
18806         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18807                 skip "Need MDS version at least 2.10.55"
18808
18809         mkdir -p $DIR/$tdir
18810         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18811
18812         # check files inherit DoM EA
18813         touch $DIR/$tdir/first
18814         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
18815                 error "bad pattern"
18816         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
18817                 error "bad stripe count"
18818         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
18819                 error "bad stripe size"
18820
18821         # check directory inherits DoM EA and uses it as default
18822         mkdir $DIR/$tdir/subdir
18823         touch $DIR/$tdir/subdir/second
18824         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
18825                 error "bad pattern in sub-directory"
18826         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
18827                 error "bad stripe count in sub-directory"
18828         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
18829                 error "bad stripe size in sub-directory"
18830         return 0
18831 }
18832 run_test 270c "DoM: DoM EA inheritance tests"
18833
18834 test_270d() {
18835         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18836                 skip "Need MDS version at least 2.10.55"
18837
18838         mkdir -p $DIR/$tdir
18839         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18840
18841         # inherit default DoM striping
18842         mkdir $DIR/$tdir/subdir
18843         touch $DIR/$tdir/subdir/f1
18844
18845         # change default directory striping
18846         $LFS setstripe -c 1 $DIR/$tdir/subdir
18847         touch $DIR/$tdir/subdir/f2
18848         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
18849                 error "wrong default striping in file 2"
18850         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
18851                 error "bad pattern in file 2"
18852         return 0
18853 }
18854 run_test 270d "DoM: change striping from DoM to RAID0"
18855
18856 test_270e() {
18857         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18858                 skip "Need MDS version at least 2.10.55"
18859
18860         mkdir -p $DIR/$tdir/dom
18861         mkdir -p $DIR/$tdir/norm
18862         DOMFILES=20
18863         NORMFILES=10
18864         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
18865         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
18866
18867         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
18868         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
18869
18870         # find DoM files by layout
18871         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
18872         [ $NUM -eq  $DOMFILES ] ||
18873                 error "lfs find -L: found $NUM, expected $DOMFILES"
18874         echo "Test 1: lfs find 20 DOM files by layout: OK"
18875
18876         # there should be 1 dir with default DOM striping
18877         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
18878         [ $NUM -eq  1 ] ||
18879                 error "lfs find -L: found $NUM, expected 1 dir"
18880         echo "Test 2: lfs find 1 DOM dir by layout: OK"
18881
18882         # find DoM files by stripe size
18883         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
18884         [ $NUM -eq  $DOMFILES ] ||
18885                 error "lfs find -S: found $NUM, expected $DOMFILES"
18886         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
18887
18888         # find files by stripe offset except DoM files
18889         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
18890         [ $NUM -eq  $NORMFILES ] ||
18891                 error "lfs find -i: found $NUM, expected $NORMFILES"
18892         echo "Test 5: lfs find no DOM files by stripe index: OK"
18893         return 0
18894 }
18895 run_test 270e "DoM: lfs find with DoM files test"
18896
18897 test_270f() {
18898         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18899                 skip "Need MDS version at least 2.10.55"
18900
18901         local mdtname=${FSNAME}-MDT0000-mdtlov
18902         local dom=$DIR/$tdir/dom_file
18903         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
18904                                                 lod.$mdtname.dom_stripesize)
18905         local dom_limit=131072
18906
18907         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
18908         local dom_current=$(do_facet mds1 $LCTL get_param -n \
18909                                                 lod.$mdtname.dom_stripesize)
18910         [ ${dom_limit} -eq ${dom_current} ] ||
18911                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
18912
18913         $LFS mkdir -i 0 -c 1 $DIR/$tdir
18914         $LFS setstripe -d $DIR/$tdir
18915         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
18916                 error "Can't set directory default striping"
18917
18918         # exceed maximum stripe size
18919         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18920                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
18921         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
18922                 error "Able to create DoM component size more than LOD limit"
18923
18924         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
18925         dom_current=$(do_facet mds1 $LCTL get_param -n \
18926                                                 lod.$mdtname.dom_stripesize)
18927         [ 0 -eq ${dom_current} ] ||
18928                 error "Can't set zero DoM stripe limit"
18929         rm $dom
18930
18931         # attempt to create DoM file on server with disabled DoM should
18932         # remove DoM entry from layout and be succeed
18933         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
18934                 error "Can't create DoM file (DoM is disabled)"
18935         [ $($LFS getstripe -L $dom) == "mdt" ] &&
18936                 error "File has DoM component while DoM is disabled"
18937         rm $dom
18938
18939         # attempt to create DoM file with only DoM stripe should return error
18940         $LFS setstripe -E $dom_limit -L mdt $dom &&
18941                 error "Able to create DoM-only file while DoM is disabled"
18942
18943         # too low values to be aligned with smallest stripe size 64K
18944         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
18945         dom_current=$(do_facet mds1 $LCTL get_param -n \
18946                                                 lod.$mdtname.dom_stripesize)
18947         [ 30000 -eq ${dom_current} ] &&
18948                 error "Can set too small DoM stripe limit"
18949
18950         # 64K is a minimal stripe size in Lustre, expect limit of that size
18951         [ 65536 -eq ${dom_current} ] ||
18952                 error "Limit is not set to 64K but ${dom_current}"
18953
18954         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
18955         dom_current=$(do_facet mds1 $LCTL get_param -n \
18956                                                 lod.$mdtname.dom_stripesize)
18957         echo $dom_current
18958         [ 2147483648 -eq ${dom_current} ] &&
18959                 error "Can set too large DoM stripe limit"
18960
18961         do_facet mds1 $LCTL set_param -n \
18962                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
18963         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18964                 error "Can't create DoM component size after limit change"
18965         do_facet mds1 $LCTL set_param -n \
18966                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
18967         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
18968                 error "Can't create DoM file after limit decrease"
18969         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
18970                 error "Can create big DoM component after limit decrease"
18971         touch ${dom}_def ||
18972                 error "Can't create file with old default layout"
18973
18974         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
18975         return 0
18976 }
18977 run_test 270f "DoM: maximum DoM stripe size checks"
18978
18979 test_271a() {
18980         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18981                 skip "Need MDS version at least 2.10.55"
18982
18983         local dom=$DIR/$tdir/dom
18984
18985         mkdir -p $DIR/$tdir
18986
18987         $LFS setstripe -E 1024K -L mdt $dom
18988
18989         lctl set_param -n mdc.*.stats=clear
18990         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18991         cat $dom > /dev/null
18992         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
18993         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
18994         ls $dom
18995         rm -f $dom
18996 }
18997 run_test 271a "DoM: data is cached for read after write"
18998
18999 test_271b() {
19000         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19001                 skip "Need MDS version at least 2.10.55"
19002
19003         local dom=$DIR/$tdir/dom
19004
19005         mkdir -p $DIR/$tdir
19006
19007         $LFS setstripe -E 1024K -L mdt -E EOF $dom
19008
19009         lctl set_param -n mdc.*.stats=clear
19010         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
19011         cancel_lru_locks mdc
19012         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
19013         # second stat to check size is cached on client
19014         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
19015         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
19016         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
19017         rm -f $dom
19018 }
19019 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
19020
19021 test_271ba() {
19022         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19023                 skip "Need MDS version at least 2.10.55"
19024
19025         local dom=$DIR/$tdir/dom
19026
19027         mkdir -p $DIR/$tdir
19028
19029         $LFS setstripe -E 1024K -L mdt -E EOF $dom
19030
19031         lctl set_param -n mdc.*.stats=clear
19032         lctl set_param -n osc.*.stats=clear
19033         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
19034         cancel_lru_locks mdc
19035         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
19036         # second stat to check size is cached on client
19037         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
19038         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
19039         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
19040         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
19041         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
19042         rm -f $dom
19043 }
19044 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
19045
19046
19047 get_mdc_stats() {
19048         local mdtidx=$1
19049         local param=$2
19050         local mdt=MDT$(printf %04x $mdtidx)
19051
19052         if [ -z $param ]; then
19053                 lctl get_param -n mdc.*$mdt*.stats
19054         else
19055                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
19056         fi
19057 }
19058
19059 test_271c() {
19060         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19061                 skip "Need MDS version at least 2.10.55"
19062
19063         local dom=$DIR/$tdir/dom
19064
19065         mkdir -p $DIR/$tdir
19066
19067         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19068
19069         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
19070         local facet=mds$((mdtidx + 1))
19071
19072         cancel_lru_locks mdc
19073         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
19074         createmany -o $dom 1000
19075         lctl set_param -n mdc.*.stats=clear
19076         smalliomany -w $dom 1000 200
19077         get_mdc_stats $mdtidx
19078         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
19079         # Each file has 1 open, 1 IO enqueues, total 2000
19080         # but now we have also +1 getxattr for security.capability, total 3000
19081         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
19082         unlinkmany $dom 1000
19083
19084         cancel_lru_locks mdc
19085         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
19086         createmany -o $dom 1000
19087         lctl set_param -n mdc.*.stats=clear
19088         smalliomany -w $dom 1000 200
19089         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
19090         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
19091         # for OPEN and IO lock.
19092         [ $((enq - enq_2)) -ge 1000 ] ||
19093                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
19094         unlinkmany $dom 1000
19095         return 0
19096 }
19097 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
19098
19099 cleanup_271def_tests() {
19100         trap 0
19101         rm -f $1
19102 }
19103
19104 test_271d() {
19105         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
19106                 skip "Need MDS version at least 2.10.57"
19107
19108         local dom=$DIR/$tdir/dom
19109         local tmp=$TMP/$tfile
19110         trap "cleanup_271def_tests $tmp" EXIT
19111
19112         mkdir -p $DIR/$tdir
19113
19114         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19115
19116         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
19117
19118         cancel_lru_locks mdc
19119         dd if=/dev/urandom of=$tmp bs=1000 count=1
19120         dd if=$tmp of=$dom bs=1000 count=1
19121         cancel_lru_locks mdc
19122
19123         cat /etc/hosts >> $tmp
19124         lctl set_param -n mdc.*.stats=clear
19125
19126         # append data to the same file it should update local page
19127         echo "Append to the same page"
19128         cat /etc/hosts >> $dom
19129         local num=$(get_mdc_stats $mdtidx ost_read)
19130         local ra=$(get_mdc_stats $mdtidx req_active)
19131         local rw=$(get_mdc_stats $mdtidx req_waittime)
19132
19133         [ -z $num ] || error "$num READ RPC occured"
19134         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19135         echo "... DONE"
19136
19137         # compare content
19138         cmp $tmp $dom || error "file miscompare"
19139
19140         cancel_lru_locks mdc
19141         lctl set_param -n mdc.*.stats=clear
19142
19143         echo "Open and read file"
19144         cat $dom > /dev/null
19145         local num=$(get_mdc_stats $mdtidx ost_read)
19146         local ra=$(get_mdc_stats $mdtidx req_active)
19147         local rw=$(get_mdc_stats $mdtidx req_waittime)
19148
19149         [ -z $num ] || error "$num READ RPC occured"
19150         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19151         echo "... DONE"
19152
19153         # compare content
19154         cmp $tmp $dom || error "file miscompare"
19155
19156         return 0
19157 }
19158 run_test 271d "DoM: read on open (1K file in reply buffer)"
19159
19160 test_271f() {
19161         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
19162                 skip "Need MDS version at least 2.10.57"
19163
19164         local dom=$DIR/$tdir/dom
19165         local tmp=$TMP/$tfile
19166         trap "cleanup_271def_tests $tmp" EXIT
19167
19168         mkdir -p $DIR/$tdir
19169
19170         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19171
19172         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
19173
19174         cancel_lru_locks mdc
19175         dd if=/dev/urandom of=$tmp bs=265000 count=1
19176         dd if=$tmp of=$dom bs=265000 count=1
19177         cancel_lru_locks mdc
19178         cat /etc/hosts >> $tmp
19179         lctl set_param -n mdc.*.stats=clear
19180
19181         echo "Append to the same page"
19182         cat /etc/hosts >> $dom
19183         local num=$(get_mdc_stats $mdtidx ost_read)
19184         local ra=$(get_mdc_stats $mdtidx req_active)
19185         local rw=$(get_mdc_stats $mdtidx req_waittime)
19186
19187         [ -z $num ] || error "$num READ RPC occured"
19188         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19189         echo "... DONE"
19190
19191         # compare content
19192         cmp $tmp $dom || error "file miscompare"
19193
19194         cancel_lru_locks mdc
19195         lctl set_param -n mdc.*.stats=clear
19196
19197         echo "Open and read file"
19198         cat $dom > /dev/null
19199         local num=$(get_mdc_stats $mdtidx ost_read)
19200         local ra=$(get_mdc_stats $mdtidx req_active)
19201         local rw=$(get_mdc_stats $mdtidx req_waittime)
19202
19203         [ -z $num ] && num=0
19204         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
19205         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19206         echo "... DONE"
19207
19208         # compare content
19209         cmp $tmp $dom || error "file miscompare"
19210
19211         return 0
19212 }
19213 run_test 271f "DoM: read on open (200K file and read tail)"
19214
19215 test_271g() {
19216         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
19217                 skip "Skipping due to old client or server version"
19218
19219         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
19220         # to get layout
19221         $CHECKSTAT -t file $DIR1/$tfile
19222
19223         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
19224         MULTIOP_PID=$!
19225         sleep 1
19226         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
19227         $LCTL set_param fail_loc=0x80000314
19228         rm $DIR1/$tfile || error "Unlink fails"
19229         RC=$?
19230         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
19231         [ $RC -eq 0 ] || error "Failed write to stale object"
19232 }
19233 run_test 271g "Discard DoM data vs client flush race"
19234
19235 test_272a() {
19236         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19237                 skip "Need MDS version at least 2.11.50"
19238
19239         local dom=$DIR/$tdir/dom
19240         mkdir -p $DIR/$tdir
19241
19242         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
19243         dd if=/dev/urandom of=$dom bs=512K count=1 ||
19244                 error "failed to write data into $dom"
19245         local old_md5=$(md5sum $dom)
19246
19247         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
19248                 error "failed to migrate to the same DoM component"
19249
19250         local new_md5=$(md5sum $dom)
19251
19252         [ "$old_md5" == "$new_md5" ] ||
19253                 error "md5sum differ: $old_md5, $new_md5"
19254
19255         [ $($LFS getstripe -c $dom) -eq 2 ] ||
19256                 error "bad final stripe count: $($LFS getstripe -c $dom) != 2"
19257 }
19258 run_test 272a "DoM migration: new layout with the same DOM component"
19259
19260 test_272b() {
19261         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19262                 skip "Need MDS version at least 2.11.50"
19263
19264         local dom=$DIR/$tdir/dom
19265         mkdir -p $DIR/$tdir
19266         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19267
19268         local mdtidx=$($LFS getstripe -m $dom)
19269         local mdtname=MDT$(printf %04x $mdtidx)
19270         local facet=mds$((mdtidx + 1))
19271
19272         local mdtfree1=$(do_facet $facet \
19273                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19274         dd if=/dev/urandom of=$dom bs=2M count=1 ||
19275                 error "failed to write data into $dom"
19276         local old_md5=$(md5sum $dom)
19277         cancel_lru_locks mdc
19278         local mdtfree1=$(do_facet $facet \
19279                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19280
19281         $LFS migrate -c2 $dom ||
19282                 error "failed to migrate to the new composite layout"
19283         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19284                 error "MDT stripe was not removed"
19285
19286         cancel_lru_locks mdc
19287         local new_md5=$(md5sum $dom)
19288         [ "$old_md5" == "$new_md5" ] ||
19289                 error "$old_md5 != $new_md5"
19290
19291         # Skip free space checks with ZFS
19292         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19293                 local mdtfree2=$(do_facet $facet \
19294                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19295                 [ $mdtfree2 -gt $mdtfree1 ] ||
19296                         error "MDT space is not freed after migration"
19297         fi
19298         return 0
19299 }
19300 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
19301
19302 test_272c() {
19303         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19304                 skip "Need MDS version at least 2.11.50"
19305
19306         local dom=$DIR/$tdir/$tfile
19307         mkdir -p $DIR/$tdir
19308         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19309
19310         local mdtidx=$($LFS getstripe -m $dom)
19311         local mdtname=MDT$(printf %04x $mdtidx)
19312         local facet=mds$((mdtidx + 1))
19313
19314         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
19315                 error "failed to write data into $dom"
19316         local old_md5=$(md5sum $dom)
19317         cancel_lru_locks mdc
19318         local mdtfree1=$(do_facet $facet \
19319                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19320
19321         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
19322                 error "failed to migrate to the new composite layout"
19323         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
19324                 error "MDT stripe was not removed"
19325
19326         cancel_lru_locks mdc
19327         local new_md5=$(md5sum $dom)
19328         [ "$old_md5" == "$new_md5" ] ||
19329                 error "$old_md5 != $new_md5"
19330
19331         # Skip free space checks with ZFS
19332         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19333                 local mdtfree2=$(do_facet $facet \
19334                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19335                 [ $mdtfree2 -gt $mdtfree1 ] ||
19336                         error "MDS space is not freed after migration"
19337         fi
19338         return 0
19339 }
19340 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
19341
19342 test_272d() {
19343         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19344                 skip "Need MDS version at least 2.12.55"
19345
19346         local dom=$DIR/$tdir/$tfile
19347         mkdir -p $DIR/$tdir
19348         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19349
19350         local mdtidx=$($LFS getstripe -m $dom)
19351         local mdtname=MDT$(printf %04x $mdtidx)
19352         local facet=mds$((mdtidx + 1))
19353
19354         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
19355                 error "failed to write data into $dom"
19356         local old_md5=$(md5sum $dom)
19357         cancel_lru_locks mdc
19358         local mdtfree1=$(do_facet $facet \
19359                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19360
19361         $LFS mirror extend -N -E 2M -c1 -E -1 -c2 $dom ||
19362                 error "failed mirroring to the new composite layout"
19363         $LFS mirror resync $dom ||
19364                 error "failed mirror resync"
19365         $LFS mirror split --mirror-id 1 -d $dom ||
19366                 error "failed mirror split"
19367
19368         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19369                 error "MDT stripe was not removed"
19370
19371         cancel_lru_locks mdc
19372         local new_md5=$(md5sum $dom)
19373         [ "$old_md5" == "$new_md5" ] ||
19374                 error "$old_md5 != $new_md5"
19375
19376         # Skip free space checks with ZFS
19377         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19378                 local mdtfree2=$(do_facet $facet \
19379                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19380                 [ $mdtfree2 -gt $mdtfree1 ] ||
19381                         error "MDS space is not freed after DOM mirror deletion"
19382         fi
19383         return 0
19384 }
19385 run_test 272d "DoM mirroring: OST-striped mirror to DOM file"
19386
19387 test_272e() {
19388         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19389                 skip "Need MDS version at least 2.12.55"
19390
19391         local dom=$DIR/$tdir/$tfile
19392         mkdir -p $DIR/$tdir
19393         $LFS setstripe -c 2 $dom
19394
19395         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
19396                 error "failed to write data into $dom"
19397         local old_md5=$(md5sum $dom)
19398         cancel_lru_locks mdc
19399
19400         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 $dom ||
19401                 error "failed mirroring to the DOM layout"
19402         $LFS mirror resync $dom ||
19403                 error "failed mirror resync"
19404         $LFS mirror split --mirror-id 1 -d $dom ||
19405                 error "failed mirror split"
19406
19407         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19408                 error "MDT stripe was not removed"
19409
19410         cancel_lru_locks mdc
19411         local new_md5=$(md5sum $dom)
19412         [ "$old_md5" == "$new_md5" ] ||
19413                 error "$old_md5 != $new_md5"
19414
19415         return 0
19416 }
19417 run_test 272e "DoM mirroring: DOM mirror to the OST-striped file"
19418
19419 test_272f() {
19420         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19421                 skip "Need MDS version at least 2.12.55"
19422
19423         local dom=$DIR/$tdir/$tfile
19424         mkdir -p $DIR/$tdir
19425         $LFS setstripe -c 2 $dom
19426
19427         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
19428                 error "failed to write data into $dom"
19429         local old_md5=$(md5sum $dom)
19430         cancel_lru_locks mdc
19431
19432         $LFS migrate -E 1M -L mdt -E eof -c2 -v $dom ||
19433                 error "failed migrating to the DOM file"
19434
19435         cancel_lru_locks mdc
19436         local new_md5=$(md5sum $dom)
19437         [ "$old_md5" != "$new_md5" ] &&
19438                 error "$old_md5 != $new_md5"
19439
19440         return 0
19441 }
19442 run_test 272f "DoM migration: OST-striped file to DOM file"
19443
19444 test_273a() {
19445         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19446                 skip "Need MDS version at least 2.11.50"
19447
19448         # Layout swap cannot be done if either file has DOM component,
19449         # this will never be supported, migration should be used instead
19450
19451         local dom=$DIR/$tdir/$tfile
19452         mkdir -p $DIR/$tdir
19453
19454         $LFS setstripe -c2 ${dom}_plain
19455         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
19456         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
19457                 error "can swap layout with DoM component"
19458         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
19459                 error "can swap layout with DoM component"
19460
19461         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
19462         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
19463                 error "can swap layout with DoM component"
19464         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
19465                 error "can swap layout with DoM component"
19466         return 0
19467 }
19468 run_test 273a "DoM: layout swapping should fail with DOM"
19469
19470 test_275() {
19471         remote_ost_nodsh && skip "remote OST with nodsh"
19472         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
19473                 skip "Need OST version >= 2.10.57"
19474
19475         local file=$DIR/$tfile
19476         local oss
19477
19478         oss=$(comma_list $(osts_nodes))
19479
19480         dd if=/dev/urandom of=$file bs=1M count=2 ||
19481                 error "failed to create a file"
19482         cancel_lru_locks osc
19483
19484         #lock 1
19485         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
19486                 error "failed to read a file"
19487
19488 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
19489         $LCTL set_param fail_loc=0x8000031f
19490
19491         cancel_lru_locks osc &
19492         sleep 1
19493
19494 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
19495         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
19496         #IO takes another lock, but matches the PENDING one
19497         #and places it to the IO RPC
19498         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
19499                 error "failed to read a file with PENDING lock"
19500 }
19501 run_test 275 "Read on a canceled duplicate lock"
19502
19503 test_276() {
19504         remote_ost_nodsh && skip "remote OST with nodsh"
19505         local pid
19506
19507         do_facet ost1 "(while true; do \
19508                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
19509                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
19510         pid=$!
19511
19512         for LOOP in $(seq 20); do
19513                 stop ost1
19514                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
19515         done
19516         kill -9 $pid
19517         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
19518                 rm $TMP/sanity_276_pid"
19519 }
19520 run_test 276 "Race between mount and obd_statfs"
19521
19522 test_277() {
19523         $LCTL set_param ldlm.namespaces.*.lru_size=0
19524         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
19525         local cached_mb=$($LCTL get_param llite.*.max_cached_mb |
19526                         grep ^used_mb | awk '{print $2}')
19527         [ $cached_mb -eq 1 ] || error "expected mb 1 got $cached_mb"
19528         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 \
19529                 oflag=direct conv=notrunc
19530         cached_mb=$($LCTL get_param llite.*.max_cached_mb |
19531                         grep ^used_mb | awk '{print $2}')
19532         [ $cached_mb -eq 0 ] || error "expected mb 0 got $cached_mb"
19533 }
19534 run_test 277 "Direct IO shall drop page cache"
19535
19536 test_278() {
19537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
19538         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
19539         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] &&
19540                 skip "needs the same host for mdt1 mdt2" && return
19541
19542         local pid1
19543         local pid2
19544
19545 #define OBD_FAIL_OBD_STOP_MDS_RACE     0x60b
19546         do_facet mds2 $LCTL set_param fail_loc=0x8000060c
19547         stop mds2 &
19548         pid2=$!
19549
19550         stop mds1
19551
19552         echo "Starting MDTs"
19553         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
19554         wait $pid2
19555 #For the error assertion will happen. lu_env_get_key(..., &mdt_thread_key)
19556 #will return NULL
19557         do_facet mds2 $LCTL set_param fail_loc=0
19558
19559         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS
19560         wait_recovery_complete mds2
19561 }
19562 run_test 278 "Race starting MDS between MDTs stop/start"
19563
19564 test_280() {
19565         [ $MGS_VERSION -lt $(version_code 2.13.52) ] &&
19566                 skip "Need MGS version at least 2.13.52"
19567         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19568         combined_mgs_mds || skip "needs combined MGS/MDT"
19569
19570         umount_client $MOUNT
19571 #define OBD_FAIL_MDS_LLOG_UMOUNT_RACE   0x15e
19572         do_facet mgs $LCTL set_param fail_loc=0x8000015e fail_val=0
19573
19574         mount_client $MOUNT &
19575         sleep 1
19576         stop mgs || error "stop mgs failed"
19577         #for a race mgs would crash
19578         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
19579         mount_client $MOUNT || error "mount client failed"
19580 }
19581 run_test 280 "Race between MGS umount and client llog processing"
19582
19583 cleanup_test_300() {
19584         trap 0
19585         umask $SAVE_UMASK
19586 }
19587 test_striped_dir() {
19588         local mdt_index=$1
19589         local stripe_count
19590         local stripe_index
19591
19592         mkdir -p $DIR/$tdir
19593
19594         SAVE_UMASK=$(umask)
19595         trap cleanup_test_300 RETURN EXIT
19596
19597         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
19598                                                 $DIR/$tdir/striped_dir ||
19599                 error "set striped dir error"
19600
19601         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
19602         [ "$mode" = "755" ] || error "expect 755 got $mode"
19603
19604         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
19605                 error "getdirstripe failed"
19606         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
19607         if [ "$stripe_count" != "2" ]; then
19608                 error "1:stripe_count is $stripe_count, expect 2"
19609         fi
19610         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
19611         if [ "$stripe_count" != "2" ]; then
19612                 error "2:stripe_count is $stripe_count, expect 2"
19613         fi
19614
19615         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
19616         if [ "$stripe_index" != "$mdt_index" ]; then
19617                 error "stripe_index is $stripe_index, expect $mdt_index"
19618         fi
19619
19620         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19621                 error "nlink error after create striped dir"
19622
19623         mkdir $DIR/$tdir/striped_dir/a
19624         mkdir $DIR/$tdir/striped_dir/b
19625
19626         stat $DIR/$tdir/striped_dir/a ||
19627                 error "create dir under striped dir failed"
19628         stat $DIR/$tdir/striped_dir/b ||
19629                 error "create dir under striped dir failed"
19630
19631         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
19632                 error "nlink error after mkdir"
19633
19634         rmdir $DIR/$tdir/striped_dir/a
19635         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
19636                 error "nlink error after rmdir"
19637
19638         rmdir $DIR/$tdir/striped_dir/b
19639         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19640                 error "nlink error after rmdir"
19641
19642         chattr +i $DIR/$tdir/striped_dir
19643         createmany -o $DIR/$tdir/striped_dir/f 10 &&
19644                 error "immutable flags not working under striped dir!"
19645         chattr -i $DIR/$tdir/striped_dir
19646
19647         rmdir $DIR/$tdir/striped_dir ||
19648                 error "rmdir striped dir error"
19649
19650         cleanup_test_300
19651
19652         true
19653 }
19654
19655 test_300a() {
19656         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19657                 skip "skipped for lustre < 2.7.0"
19658         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19659         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19660
19661         test_striped_dir 0 || error "failed on striped dir on MDT0"
19662         test_striped_dir 1 || error "failed on striped dir on MDT0"
19663 }
19664 run_test 300a "basic striped dir sanity test"
19665
19666 test_300b() {
19667         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19668                 skip "skipped for lustre < 2.7.0"
19669         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19670         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19671
19672         local i
19673         local mtime1
19674         local mtime2
19675         local mtime3
19676
19677         test_mkdir $DIR/$tdir || error "mkdir fail"
19678         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19679                 error "set striped dir error"
19680         for i in {0..9}; do
19681                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
19682                 sleep 1
19683                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
19684                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
19685                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
19686                 sleep 1
19687                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
19688                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
19689                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
19690         done
19691         true
19692 }
19693 run_test 300b "check ctime/mtime for striped dir"
19694
19695 test_300c() {
19696         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19697                 skip "skipped for lustre < 2.7.0"
19698         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19699         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19700
19701         local file_count
19702
19703         mkdir -p $DIR/$tdir
19704         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
19705                 error "set striped dir error"
19706
19707         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
19708                 error "chown striped dir failed"
19709
19710         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
19711                 error "create 5k files failed"
19712
19713         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
19714
19715         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
19716
19717         rm -rf $DIR/$tdir
19718 }
19719 run_test 300c "chown && check ls under striped directory"
19720
19721 test_300d() {
19722         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19723                 skip "skipped for lustre < 2.7.0"
19724         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19725         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19726
19727         local stripe_count
19728         local file
19729
19730         mkdir -p $DIR/$tdir
19731         $LFS setstripe -c 2 $DIR/$tdir
19732
19733         #local striped directory
19734         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19735                 error "set striped dir error"
19736         #look at the directories for debug purposes
19737         ls -l $DIR/$tdir
19738         $LFS getdirstripe $DIR/$tdir
19739         ls -l $DIR/$tdir/striped_dir
19740         $LFS getdirstripe $DIR/$tdir/striped_dir
19741         createmany -o $DIR/$tdir/striped_dir/f 10 ||
19742                 error "create 10 files failed"
19743
19744         #remote striped directory
19745         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
19746                 error "set striped dir error"
19747         #look at the directories for debug purposes
19748         ls -l $DIR/$tdir
19749         $LFS getdirstripe $DIR/$tdir
19750         ls -l $DIR/$tdir/remote_striped_dir
19751         $LFS getdirstripe $DIR/$tdir/remote_striped_dir
19752         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
19753                 error "create 10 files failed"
19754
19755         for file in $(find $DIR/$tdir); do
19756                 stripe_count=$($LFS getstripe -c $file)
19757                 [ $stripe_count -eq 2 ] ||
19758                         error "wrong stripe $stripe_count for $file"
19759         done
19760
19761         rm -rf $DIR/$tdir
19762 }
19763 run_test 300d "check default stripe under striped directory"
19764
19765 test_300e() {
19766         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19767                 skip "Need MDS version at least 2.7.55"
19768         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19769         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19770
19771         local stripe_count
19772         local file
19773
19774         mkdir -p $DIR/$tdir
19775
19776         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19777                 error "set striped dir error"
19778
19779         touch $DIR/$tdir/striped_dir/a
19780         touch $DIR/$tdir/striped_dir/b
19781         touch $DIR/$tdir/striped_dir/c
19782
19783         mkdir $DIR/$tdir/striped_dir/dir_a
19784         mkdir $DIR/$tdir/striped_dir/dir_b
19785         mkdir $DIR/$tdir/striped_dir/dir_c
19786
19787         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
19788                 error "set striped adir under striped dir error"
19789
19790         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
19791                 error "set striped bdir under striped dir error"
19792
19793         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
19794                 error "set striped cdir under striped dir error"
19795
19796         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
19797                 error "rename dir under striped dir fails"
19798
19799         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
19800                 error "rename dir under different stripes fails"
19801
19802         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
19803                 error "rename file under striped dir should succeed"
19804
19805         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
19806                 error "rename dir under striped dir should succeed"
19807
19808         rm -rf $DIR/$tdir
19809 }
19810 run_test 300e "check rename under striped directory"
19811
19812 test_300f() {
19813         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19814         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19815         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19816                 skip "Need MDS version at least 2.7.55"
19817
19818         local stripe_count
19819         local file
19820
19821         rm -rf $DIR/$tdir
19822         mkdir -p $DIR/$tdir
19823
19824         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19825                 error "set striped dir error"
19826
19827         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
19828                 error "set striped dir error"
19829
19830         touch $DIR/$tdir/striped_dir/a
19831         mkdir $DIR/$tdir/striped_dir/dir_a
19832         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
19833                 error "create striped dir under striped dir fails"
19834
19835         touch $DIR/$tdir/striped_dir1/b
19836         mkdir $DIR/$tdir/striped_dir1/dir_b
19837         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
19838                 error "create striped dir under striped dir fails"
19839
19840         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
19841                 error "rename dir under different striped dir should fail"
19842
19843         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
19844                 error "rename striped dir under diff striped dir should fail"
19845
19846         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
19847                 error "rename file under diff striped dirs fails"
19848
19849         rm -rf $DIR/$tdir
19850 }
19851 run_test 300f "check rename cross striped directory"
19852
19853 test_300_check_default_striped_dir()
19854 {
19855         local dirname=$1
19856         local default_count=$2
19857         local default_index=$3
19858         local stripe_count
19859         local stripe_index
19860         local dir_stripe_index
19861         local dir
19862
19863         echo "checking $dirname $default_count $default_index"
19864         $LFS setdirstripe -D -c $default_count -i $default_index \
19865                                 -t all_char $DIR/$tdir/$dirname ||
19866                 error "set default stripe on striped dir error"
19867         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
19868         [ $stripe_count -eq $default_count ] ||
19869                 error "expect $default_count get $stripe_count for $dirname"
19870
19871         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
19872         [ $stripe_index -eq $default_index ] ||
19873                 error "expect $default_index get $stripe_index for $dirname"
19874
19875         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
19876                                                 error "create dirs failed"
19877
19878         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
19879         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
19880         for dir in $(find $DIR/$tdir/$dirname/*); do
19881                 stripe_count=$($LFS getdirstripe -c $dir)
19882                 [ $stripe_count -eq $default_count ] ||
19883                 [ $stripe_count -eq 0 ] || [ $default_count -eq 1 ] ||
19884                 error "stripe count $default_count != $stripe_count for $dir"
19885
19886                 stripe_index=$($LFS getdirstripe -i $dir)
19887                 [ $default_index -eq -1 ] ||
19888                         [ $stripe_index -eq $default_index ] ||
19889                         error "$stripe_index != $default_index for $dir"
19890
19891                 #check default stripe
19892                 stripe_count=$($LFS getdirstripe -D -c $dir)
19893                 [ $stripe_count -eq $default_count ] ||
19894                 error "default count $default_count != $stripe_count for $dir"
19895
19896                 stripe_index=$($LFS getdirstripe -D -i $dir)
19897                 [ $stripe_index -eq $default_index ] ||
19898                 error "default index $default_index != $stripe_index for $dir"
19899         done
19900         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
19901 }
19902
19903 test_300g() {
19904         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19905         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19906                 skip "Need MDS version at least 2.7.55"
19907
19908         local dir
19909         local stripe_count
19910         local stripe_index
19911
19912         mkdir $DIR/$tdir
19913         mkdir $DIR/$tdir/normal_dir
19914
19915         #Checking when client cache stripe index
19916         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
19917         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
19918                 error "create striped_dir failed"
19919
19920         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
19921                 error "create dir0 fails"
19922         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
19923         [ $stripe_index -eq 0 ] ||
19924                 error "dir0 expect index 0 got $stripe_index"
19925
19926         mkdir $DIR/$tdir/striped_dir/dir1 ||
19927                 error "create dir1 fails"
19928         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
19929         [ $stripe_index -eq 1 ] ||
19930                 error "dir1 expect index 1 got $stripe_index"
19931
19932         #check default stripe count/stripe index
19933         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
19934         test_300_check_default_striped_dir normal_dir 1 0
19935         test_300_check_default_striped_dir normal_dir 2 1
19936         test_300_check_default_striped_dir normal_dir 2 -1
19937
19938         #delete default stripe information
19939         echo "delete default stripeEA"
19940         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
19941                 error "set default stripe on striped dir error"
19942
19943         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
19944         for dir in $(find $DIR/$tdir/normal_dir/*); do
19945                 stripe_count=$($LFS getdirstripe -c $dir)
19946                 [ $stripe_count -eq 0 ] ||
19947                         error "expect 1 get $stripe_count for $dir"
19948                 stripe_index=$($LFS getdirstripe -i $dir)
19949                 [ $stripe_index -eq 0 ] ||
19950                         error "expect 0 get $stripe_index for $dir"
19951         done
19952 }
19953 run_test 300g "check default striped directory for normal directory"
19954
19955 test_300h() {
19956         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19957         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19958                 skip "Need MDS version at least 2.7.55"
19959
19960         local dir
19961         local stripe_count
19962
19963         mkdir $DIR/$tdir
19964         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19965                 error "set striped dir error"
19966
19967         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
19968         test_300_check_default_striped_dir striped_dir 1 0
19969         test_300_check_default_striped_dir striped_dir 2 1
19970         test_300_check_default_striped_dir striped_dir 2 -1
19971
19972         #delete default stripe information
19973         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
19974                 error "set default stripe on striped dir error"
19975
19976         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
19977         for dir in $(find $DIR/$tdir/striped_dir/*); do
19978                 stripe_count=$($LFS getdirstripe -c $dir)
19979                 [ $stripe_count -eq 0 ] ||
19980                         error "expect 1 get $stripe_count for $dir"
19981         done
19982 }
19983 run_test 300h "check default striped directory for striped directory"
19984
19985 test_300i() {
19986         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19987         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19988         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19989                 skip "Need MDS version at least 2.7.55"
19990
19991         local stripe_count
19992         local file
19993
19994         mkdir $DIR/$tdir
19995
19996         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19997                 error "set striped dir error"
19998
19999         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
20000                 error "create files under striped dir failed"
20001
20002         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
20003                 error "set striped hashdir error"
20004
20005         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
20006                 error "create dir0 under hash dir failed"
20007         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
20008                 error "create dir1 under hash dir failed"
20009
20010         # unfortunately, we need to umount to clear dir layout cache for now
20011         # once we fully implement dir layout, we can drop this
20012         umount_client $MOUNT || error "umount failed"
20013         mount_client $MOUNT || error "mount failed"
20014
20015         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
20016         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
20017         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
20018
20019         #set the stripe to be unknown hash type
20020         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
20021         $LCTL set_param fail_loc=0x1901
20022         for ((i = 0; i < 10; i++)); do
20023                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
20024                         error "stat f-$i failed"
20025                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
20026         done
20027
20028         touch $DIR/$tdir/striped_dir/f0 &&
20029                 error "create under striped dir with unknown hash should fail"
20030
20031         $LCTL set_param fail_loc=0
20032
20033         umount_client $MOUNT || error "umount failed"
20034         mount_client $MOUNT || error "mount failed"
20035
20036         return 0
20037 }
20038 run_test 300i "client handle unknown hash type striped directory"
20039
20040 test_300j() {
20041         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20042         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20043         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20044                 skip "Need MDS version at least 2.7.55"
20045
20046         local stripe_count
20047         local file
20048
20049         mkdir $DIR/$tdir
20050
20051         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
20052         $LCTL set_param fail_loc=0x1702
20053         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
20054                 error "set striped dir error"
20055
20056         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
20057                 error "create files under striped dir failed"
20058
20059         $LCTL set_param fail_loc=0
20060
20061         rm -rf $DIR/$tdir || error "unlink striped dir fails"
20062
20063         return 0
20064 }
20065 run_test 300j "test large update record"
20066
20067 test_300k() {
20068         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20069         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20070         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20071                 skip "Need MDS version at least 2.7.55"
20072
20073         # this test needs a huge transaction
20074         local kb
20075         kb=$(do_facet $SINGLEMDS "$LCTL get_param -n \
20076              osd*.$FSNAME-MDT0000.kbytestotal")
20077         [ $kb -lt $((1024*1024)) ] && skip "MDT0 too small: $kb"
20078
20079         local stripe_count
20080         local file
20081
20082         mkdir $DIR/$tdir
20083
20084         #define OBD_FAIL_LARGE_STRIPE   0x1703
20085         $LCTL set_param fail_loc=0x1703
20086         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
20087                 error "set striped dir error"
20088         $LCTL set_param fail_loc=0
20089
20090         $LFS getdirstripe $DIR/$tdir/striped_dir ||
20091                 error "getstripeddir fails"
20092         rm -rf $DIR/$tdir/striped_dir ||
20093                 error "unlink striped dir fails"
20094
20095         return 0
20096 }
20097 run_test 300k "test large striped directory"
20098
20099 test_300l() {
20100         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20101         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20102         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20103                 skip "Need MDS version at least 2.7.55"
20104
20105         local stripe_index
20106
20107         test_mkdir -p $DIR/$tdir/striped_dir
20108         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
20109                         error "chown $RUNAS_ID failed"
20110         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
20111                 error "set default striped dir failed"
20112
20113         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
20114         $LCTL set_param fail_loc=0x80000158
20115         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
20116
20117         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
20118         [ $stripe_index -eq 1 ] ||
20119                 error "expect 1 get $stripe_index for $dir"
20120 }
20121 run_test 300l "non-root user to create dir under striped dir with stale layout"
20122
20123 test_300m() {
20124         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20125         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
20126         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20127                 skip "Need MDS version at least 2.7.55"
20128
20129         mkdir -p $DIR/$tdir/striped_dir
20130         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
20131                 error "set default stripes dir error"
20132
20133         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
20134
20135         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
20136         [ $stripe_count -eq 0 ] ||
20137                         error "expect 0 get $stripe_count for a"
20138
20139         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
20140                 error "set default stripes dir error"
20141
20142         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
20143
20144         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
20145         [ $stripe_count -eq 0 ] ||
20146                         error "expect 0 get $stripe_count for b"
20147
20148         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
20149                 error "set default stripes dir error"
20150
20151         mkdir $DIR/$tdir/striped_dir/c &&
20152                 error "default stripe_index is invalid, mkdir c should fails"
20153
20154         rm -rf $DIR/$tdir || error "rmdir fails"
20155 }
20156 run_test 300m "setstriped directory on single MDT FS"
20157
20158 cleanup_300n() {
20159         local list=$(comma_list $(mdts_nodes))
20160
20161         trap 0
20162         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
20163 }
20164
20165 test_300n() {
20166         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20167         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20168         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20169                 skip "Need MDS version at least 2.7.55"
20170         remote_mds_nodsh && skip "remote MDS with nodsh"
20171
20172         local stripe_index
20173         local list=$(comma_list $(mdts_nodes))
20174
20175         trap cleanup_300n RETURN EXIT
20176         mkdir -p $DIR/$tdir
20177         chmod 777 $DIR/$tdir
20178         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
20179                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
20180                 error "create striped dir succeeds with gid=0"
20181
20182         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
20183         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
20184                 error "create striped dir fails with gid=-1"
20185
20186         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
20187         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
20188                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
20189                 error "set default striped dir succeeds with gid=0"
20190
20191
20192         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
20193         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
20194                 error "set default striped dir fails with gid=-1"
20195
20196
20197         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
20198         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
20199                                         error "create test_dir fails"
20200         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
20201                                         error "create test_dir1 fails"
20202         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
20203                                         error "create test_dir2 fails"
20204         cleanup_300n
20205 }
20206 run_test 300n "non-root user to create dir under striped dir with default EA"
20207
20208 test_300o() {
20209         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20210         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20211         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20212                 skip "Need MDS version at least 2.7.55"
20213
20214         local numfree1
20215         local numfree2
20216
20217         mkdir -p $DIR/$tdir
20218
20219         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
20220         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
20221         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
20222                 skip "not enough free inodes $numfree1 $numfree2"
20223         fi
20224
20225         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
20226         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
20227         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
20228                 skip "not enough free space $numfree1 $numfree2"
20229         fi
20230
20231         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
20232                 error "setdirstripe fails"
20233
20234         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
20235                 error "create dirs fails"
20236
20237         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
20238         ls $DIR/$tdir/striped_dir > /dev/null ||
20239                 error "ls striped dir fails"
20240         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
20241                 error "unlink big striped dir fails"
20242 }
20243 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
20244
20245 test_300p() {
20246         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20247         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20248         remote_mds_nodsh && skip "remote MDS with nodsh"
20249
20250         mkdir -p $DIR/$tdir
20251
20252         #define OBD_FAIL_OUT_ENOSPC     0x1704
20253         do_facet mds2 lctl set_param fail_loc=0x80001704
20254         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
20255                  && error "create striped directory should fail"
20256
20257         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
20258
20259         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
20260         true
20261 }
20262 run_test 300p "create striped directory without space"
20263
20264 test_300q() {
20265         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20266         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20267
20268         local fd=$(free_fd)
20269         local cmd="exec $fd<$tdir"
20270         cd $DIR
20271         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
20272         eval $cmd
20273         cmd="exec $fd<&-"
20274         trap "eval $cmd" EXIT
20275         cd $tdir || error "cd $tdir fails"
20276         rmdir  ../$tdir || error "rmdir $tdir fails"
20277         mkdir local_dir && error "create dir succeeds"
20278         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
20279         eval $cmd
20280         return 0
20281 }
20282 run_test 300q "create remote directory under orphan directory"
20283
20284 test_300r() {
20285         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20286                 skip "Need MDS version at least 2.7.55" && return
20287         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
20288
20289         mkdir $DIR/$tdir
20290
20291         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
20292                 error "set striped dir error"
20293
20294         $LFS getdirstripe $DIR/$tdir/striped_dir ||
20295                 error "getstripeddir fails"
20296
20297         local stripe_count
20298         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
20299                       awk '/lmv_stripe_count:/ { print $2 }')
20300
20301         [ $MDSCOUNT -ne $stripe_count ] &&
20302                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
20303
20304         rm -rf $DIR/$tdir/striped_dir ||
20305                 error "unlink striped dir fails"
20306 }
20307 run_test 300r "test -1 striped directory"
20308
20309 prepare_remote_file() {
20310         mkdir $DIR/$tdir/src_dir ||
20311                 error "create remote source failed"
20312
20313         cp /etc/hosts $DIR/$tdir/src_dir/a ||
20314                  error "cp to remote source failed"
20315         touch $DIR/$tdir/src_dir/a
20316
20317         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
20318                 error "create remote target dir failed"
20319
20320         touch $DIR/$tdir/tgt_dir/b
20321
20322         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
20323                 error "rename dir cross MDT failed!"
20324
20325         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
20326                 error "src_child still exists after rename"
20327
20328         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
20329                 error "missing file(a) after rename"
20330
20331         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
20332                 error "diff after rename"
20333 }
20334
20335 test_310a() {
20336         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
20337         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20338
20339         local remote_file=$DIR/$tdir/tgt_dir/b
20340
20341         mkdir -p $DIR/$tdir
20342
20343         prepare_remote_file || error "prepare remote file failed"
20344
20345         #open-unlink file
20346         $OPENUNLINK $remote_file $remote_file ||
20347                 error "openunlink $remote_file failed"
20348         $CHECKSTAT -a $remote_file || error "$remote_file exists"
20349 }
20350 run_test 310a "open unlink remote file"
20351
20352 test_310b() {
20353         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
20354         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20355
20356         local remote_file=$DIR/$tdir/tgt_dir/b
20357
20358         mkdir -p $DIR/$tdir
20359
20360         prepare_remote_file || error "prepare remote file failed"
20361
20362         ln $remote_file $DIR/$tfile || error "link failed for remote file"
20363         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
20364         $CHECKSTAT -t file $remote_file || error "check file failed"
20365 }
20366 run_test 310b "unlink remote file with multiple links while open"
20367
20368 test_310c() {
20369         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20370         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
20371
20372         local remote_file=$DIR/$tdir/tgt_dir/b
20373
20374         mkdir -p $DIR/$tdir
20375
20376         prepare_remote_file || error "prepare remote file failed"
20377
20378         ln $remote_file $DIR/$tfile || error "link failed for remote file"
20379         multiop_bg_pause $remote_file O_uc ||
20380                         error "mulitop failed for remote file"
20381         MULTIPID=$!
20382         $MULTIOP $DIR/$tfile Ouc
20383         kill -USR1 $MULTIPID
20384         wait $MULTIPID
20385 }
20386 run_test 310c "open-unlink remote file with multiple links"
20387
20388 #LU-4825
20389 test_311() {
20390         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20391         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
20392         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
20393                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
20394         remote_mds_nodsh && skip "remote MDS with nodsh"
20395
20396         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
20397         local mdts=$(comma_list $(mdts_nodes))
20398
20399         mkdir -p $DIR/$tdir
20400         $LFS setstripe -i 0 -c 1 $DIR/$tdir
20401         createmany -o $DIR/$tdir/$tfile. 1000
20402
20403         # statfs data is not real time, let's just calculate it
20404         old_iused=$((old_iused + 1000))
20405
20406         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
20407                         osp.*OST0000*MDT0000.create_count")
20408         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
20409                                 osp.*OST0000*MDT0000.max_create_count")
20410         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
20411
20412         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
20413         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
20414         [ $index -ne 0 ] || error "$tfile stripe index is 0"
20415
20416         unlinkmany $DIR/$tdir/$tfile. 1000
20417
20418         do_nodes $mdts "$LCTL set_param -n \
20419                         osp.*OST0000*.max_create_count=$max_count"
20420         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
20421                 do_nodes $mdts "$LCTL set_param -n \
20422                                 osp.*OST0000*.create_count=$count"
20423         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
20424                         grep "=0" && error "create_count is zero"
20425
20426         local new_iused
20427         for i in $(seq 120); do
20428                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
20429                 # system may be too busy to destroy all objs in time, use
20430                 # a somewhat small value to not fail autotest
20431                 [ $((old_iused - new_iused)) -gt 400 ] && break
20432                 sleep 1
20433         done
20434
20435         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
20436         [ $((old_iused - new_iused)) -gt 400 ] ||
20437                 error "objs not destroyed after unlink"
20438 }
20439 run_test 311 "disable OSP precreate, and unlink should destroy objs"
20440
20441 zfs_oid_to_objid()
20442 {
20443         local ost=$1
20444         local objid=$2
20445
20446         local vdevdir=$(dirname $(facet_vdevice $ost))
20447         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
20448         local zfs_zapid=$(do_facet $ost $cmd |
20449                           grep -w "/O/0/d$((objid%32))" -C 5 |
20450                           awk '/Object/{getline; print $1}')
20451         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
20452                           awk "/$objid = /"'{printf $3}')
20453
20454         echo $zfs_objid
20455 }
20456
20457 zfs_object_blksz() {
20458         local ost=$1
20459         local objid=$2
20460
20461         local vdevdir=$(dirname $(facet_vdevice $ost))
20462         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
20463         local blksz=$(do_facet $ost $cmd $objid |
20464                       awk '/dblk/{getline; printf $4}')
20465
20466         case "${blksz: -1}" in
20467                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
20468                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
20469                 *) ;;
20470         esac
20471
20472         echo $blksz
20473 }
20474
20475 test_312() { # LU-4856
20476         remote_ost_nodsh && skip "remote OST with nodsh"
20477         [ "$ost1_FSTYPE" = "zfs" ] ||
20478                 skip_env "the test only applies to zfs"
20479
20480         local max_blksz=$(do_facet ost1 \
20481                           $ZFS get -p recordsize $(facet_device ost1) |
20482                           awk '!/VALUE/{print $3}')
20483
20484         # to make life a little bit easier
20485         $LFS mkdir -c 1 -i 0 $DIR/$tdir
20486         $LFS setstripe -c 1 -i 0 $DIR/$tdir
20487
20488         local tf=$DIR/$tdir/$tfile
20489         touch $tf
20490         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20491
20492         # Get ZFS object id
20493         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20494         # block size change by sequential overwrite
20495         local bs
20496
20497         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
20498                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
20499
20500                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
20501                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
20502         done
20503         rm -f $tf
20504
20505         # block size change by sequential append write
20506         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
20507         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20508         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20509         local count
20510
20511         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
20512                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
20513                         oflag=sync conv=notrunc
20514
20515                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
20516                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
20517                         error "blksz error, actual $blksz, " \
20518                                 "expected: 2 * $count * $PAGE_SIZE"
20519         done
20520         rm -f $tf
20521
20522         # random write
20523         touch $tf
20524         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20525         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20526
20527         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
20528         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20529         [ $blksz -eq $PAGE_SIZE ] ||
20530                 error "blksz error: $blksz, expected: $PAGE_SIZE"
20531
20532         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
20533         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20534         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
20535
20536         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
20537         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20538         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
20539 }
20540 run_test 312 "make sure ZFS adjusts its block size by write pattern"
20541
20542 test_313() {
20543         remote_ost_nodsh && skip "remote OST with nodsh"
20544
20545         local file=$DIR/$tfile
20546
20547         rm -f $file
20548         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
20549
20550         # define OBD_FAIL_TGT_RCVD_EIO           0x720
20551         do_facet ost1 "$LCTL set_param fail_loc=0x720"
20552         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
20553                 error "write should failed"
20554         do_facet ost1 "$LCTL set_param fail_loc=0"
20555         rm -f $file
20556 }
20557 run_test 313 "io should fail after last_rcvd update fail"
20558
20559 test_314() {
20560         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
20561
20562         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
20563         do_facet ost1 "$LCTL set_param fail_loc=0x720"
20564         rm -f $DIR/$tfile
20565         wait_delete_completed
20566         do_facet ost1 "$LCTL set_param fail_loc=0"
20567 }
20568 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
20569
20570 test_315() { # LU-618
20571         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
20572
20573         local file=$DIR/$tfile
20574         rm -f $file
20575
20576         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
20577                 error "multiop file write failed"
20578         $MULTIOP $file oO_RDONLY:r4063232_c &
20579         PID=$!
20580
20581         sleep 2
20582
20583         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
20584         kill -USR1 $PID
20585
20586         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
20587         rm -f $file
20588 }
20589 run_test 315 "read should be accounted"
20590
20591 test_316() {
20592         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20593         large_xattr_enabled || skip_env "ea_inode feature disabled"
20594
20595         rm -rf $DIR/$tdir/d
20596         mkdir -p $DIR/$tdir/d
20597         chown nobody $DIR/$tdir/d
20598         touch $DIR/$tdir/d/file
20599
20600         $LFS mv -m1 $DIR/$tdir/d || error "lfs mv failed"
20601 }
20602 run_test 316 "lfs mv"
20603
20604 test_317() {
20605         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
20606                 skip "Need MDS version at least 2.11.53"
20607         if [ "$ost1_FSTYPE" == "zfs" ]; then
20608                 skip "LU-10370: no implementation for ZFS"
20609         fi
20610
20611         local trunc_sz
20612         local grant_blk_size
20613
20614         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
20615                         awk '/grant_block_size:/ { print $2; exit; }')
20616         #
20617         # Create File of size 5M. Truncate it to below size's and verify
20618         # blocks count.
20619         #
20620         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
20621                 error "Create file $DIR/$tfile failed"
20622         stack_trap "rm -f $DIR/$tfile" EXIT
20623
20624         for trunc_sz in 2097152 4097 4000 509 0; do
20625                 $TRUNCATE $DIR/$tfile $trunc_sz ||
20626                         error "truncate $tfile to $trunc_sz failed"
20627                 local sz=$(stat --format=%s $DIR/$tfile)
20628                 local blk=$(stat --format=%b $DIR/$tfile)
20629                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
20630                                      grant_blk_size) * 8))
20631
20632                 if [[ $blk -ne $trunc_blk ]]; then
20633                         $(which stat) $DIR/$tfile
20634                         error "Expected Block $trunc_blk got $blk for $tfile"
20635                 fi
20636
20637                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20638                         error "Expected Size $trunc_sz got $sz for $tfile"
20639         done
20640
20641         #
20642         # sparse file test
20643         # Create file with a hole and write actual two blocks. Block count
20644         # must be 16.
20645         #
20646         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
20647                 conv=fsync || error "Create file : $DIR/$tfile"
20648
20649         # Calculate the final truncate size.
20650         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
20651
20652         #
20653         # truncate to size $trunc_sz bytes. Strip the last block
20654         # The block count must drop to 8
20655         #
20656         $TRUNCATE $DIR/$tfile $trunc_sz ||
20657                 error "truncate $tfile to $trunc_sz failed"
20658
20659         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
20660         sz=$(stat --format=%s $DIR/$tfile)
20661         blk=$(stat --format=%b $DIR/$tfile)
20662
20663         if [[ $blk -ne $trunc_bsz ]]; then
20664                 $(which stat) $DIR/$tfile
20665                 error "Expected Block $trunc_bsz got $blk for $tfile"
20666         fi
20667
20668         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20669                 error "Expected Size $trunc_sz got $sz for $tfile"
20670 }
20671 run_test 317 "Verify blocks get correctly update after truncate"
20672
20673 test_318() {
20674         local old_max_active=$($LCTL get_param -n \
20675                             llite.*.max_read_ahead_async_active 2>/dev/null)
20676
20677         $LCTL set_param llite.*.max_read_ahead_async_active=256
20678         local max_active=$($LCTL get_param -n \
20679                            llite.*.max_read_ahead_async_active 2>/dev/null)
20680         [ $max_active -ne 256 ] && error "expected 256 but got $max_active"
20681
20682         $LCTL set_param llite.*.max_read_ahead_async_active=0 ||
20683                 error "set max_read_ahead_async_active should succeed"
20684
20685         $LCTL set_param llite.*.max_read_ahead_async_active=512
20686         max_active=$($LCTL get_param -n \
20687                      llite.*.max_read_ahead_async_active 2>/dev/null)
20688         [ $max_active -eq 512 ] || error "expected 512 but got $max_active"
20689
20690         # restore @max_active
20691         [ $old_max_active -ne 0 ] && $LCTL set_param \
20692                 llite.*.max_read_ahead_async_active=$old_max_active
20693
20694         local old_threshold=$($LCTL get_param -n \
20695                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
20696         local max_per_file_mb=$($LCTL get_param -n \
20697                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
20698
20699         local invalid=$(($max_per_file_mb + 1))
20700         $LCTL set_param \
20701                 llite.*.read_ahead_async_file_threshold_mb=$invalid\
20702                         && error "set $invalid should fail"
20703
20704         local valid=$(($invalid - 1))
20705         $LCTL set_param \
20706                 llite.*.read_ahead_async_file_threshold_mb=$valid ||
20707                         error "set $valid should succeed"
20708         local threshold=$($LCTL get_param -n \
20709                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
20710         [ $threshold -eq $valid ] || error \
20711                 "expect threshold $valid got $threshold"
20712         $LCTL set_param \
20713                 llite.*.read_ahead_async_file_threshold_mb=$old_threshold
20714 }
20715 run_test 318 "Verify async readahead tunables"
20716
20717 test_319() {
20718         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
20719
20720         local before=$(date +%s)
20721         local evict
20722         local mdir=$DIR/$tdir
20723         local file=$mdir/xxx
20724
20725         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
20726         touch $file
20727
20728 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
20729         $LCTL set_param fail_val=5 fail_loc=0x8000032c
20730         $LFS mv -m1 $file &
20731
20732         sleep 1
20733         dd if=$file of=/dev/null
20734         wait
20735         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
20736           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
20737
20738         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
20739 }
20740 run_test 319 "lost lease lock on migrate error"
20741
20742 test_398a() { # LU-4198
20743         $LFS setstripe -c 1 -i 0 $DIR/$tfile
20744         $LCTL set_param ldlm.namespaces.*.lru_size=clear
20745
20746         # request a new lock on client
20747         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
20748
20749         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
20750         local lock_count=$($LCTL get_param -n \
20751                            ldlm.namespaces.*-OST0000-osc-ffff*.lru_size)
20752         [[ $lock_count -eq 0 ]] || error "lock should be cancelled by direct IO"
20753
20754         $LCTL set_param ldlm.namespaces.*-OST0000-osc-ffff*.lru_size=clear
20755
20756         # no lock cached, should use lockless IO and not enqueue new lock
20757         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
20758         lock_count=$($LCTL get_param -n \
20759                      ldlm.namespaces.*-OST0000-osc-ffff*.lru_size)
20760         [[ $lock_count -eq 0 ]] || error "no lock should be held by direct IO"
20761 }
20762 run_test 398a "direct IO should cancel lock otherwise lockless"
20763
20764 test_398b() { # LU-4198
20765         which fio || skip_env "no fio installed"
20766         $LFS setstripe -c -1 $DIR/$tfile
20767
20768         local size=12
20769         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size
20770
20771         local njobs=4
20772         echo "mix direct rw ${size}M to OST0 by fio with $njobs jobs..."
20773         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
20774                 --numjobs=$njobs --fallocate=none \
20775                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
20776                 --filename=$DIR/$tfile &
20777         bg_pid=$!
20778
20779         echo "mix buffer rw ${size}M to OST0 by fio with $njobs jobs..."
20780         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE \
20781                 --numjobs=$njobs --fallocate=none \
20782                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
20783                 --filename=$DIR/$tfile || true
20784         wait $bg_pid
20785
20786         rm -rf $DIR/$tfile
20787 }
20788 run_test 398b "DIO and buffer IO race"
20789
20790 test_398c() { # LU-4198
20791         which fio || skip_env "no fio installed"
20792
20793         saved_debug=$($LCTL get_param -n debug)
20794         $LCTL set_param debug=0
20795
20796         local size=$(lctl get_param -n osc.$FSNAME-OST0000*.kbytesavail | head -1)
20797         ((size /= 1024)) # by megabytes
20798         ((size /= 2)) # write half of the OST at most
20799         [ $size -gt 40 ] && size=40 #reduce test time anyway
20800
20801         $LFS setstripe -c 1 $DIR/$tfile
20802
20803         # it seems like ldiskfs reserves more space than necessary if the
20804         # writing blocks are not mapped, so it extends the file firstly
20805         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size && sync
20806         cancel_lru_locks osc
20807
20808         # clear and verify rpc_stats later
20809         $LCTL set_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats=clear
20810
20811         local njobs=4
20812         echo "writing ${size}M to OST0 by fio with $njobs jobs..."
20813         fio --name=rand-write --rw=randwrite --bs=$PAGE_SIZE --direct=1 \
20814                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
20815                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
20816                 --filename=$DIR/$tfile
20817         [ $? -eq 0 ] || error "fio write error"
20818
20819         [ $($LCTL get_param -n \
20820          ldlm.namespaces.${FSNAME}-OST0000-osc-ffff*.lock_count) -eq 0 ] ||
20821                 error "Locks were requested while doing AIO"
20822
20823         # get the percentage of 1-page I/O
20824         pct=$($LCTL get_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats |
20825                 grep -A 1 'pages per rpc' | grep -v 'pages per rpc' |
20826                 awk '{print $7}')
20827         [ $pct -le 50 ] || error "$pct% of I/O are 1-page"
20828
20829         echo "mix rw ${size}M to OST0 by fio with $njobs jobs..."
20830         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
20831                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
20832                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
20833                 --filename=$DIR/$tfile
20834         [ $? -eq 0 ] || error "fio mixed read write error"
20835
20836         rm -rf $DIR/$tfile
20837         $LCTL set_param debug="$saved_debug"
20838 }
20839 run_test 398c "run fio to test AIO"
20840
20841 test_fake_rw() {
20842         local read_write=$1
20843         if [ "$read_write" = "write" ]; then
20844                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
20845         elif [ "$read_write" = "read" ]; then
20846                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
20847         else
20848                 error "argument error"
20849         fi
20850
20851         # turn off debug for performance testing
20852         local saved_debug=$($LCTL get_param -n debug)
20853         $LCTL set_param debug=0
20854
20855         $LFS setstripe -c 1 -i 0 $DIR/$tfile
20856
20857         # get ost1 size - $FSNAME-OST0000
20858         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
20859         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
20860         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
20861
20862         if [ "$read_write" = "read" ]; then
20863                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
20864         fi
20865
20866         local start_time=$(date +%s.%N)
20867         $dd_cmd bs=1M count=$blocks oflag=sync ||
20868                 error "real dd $read_write error"
20869         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
20870
20871         if [ "$read_write" = "write" ]; then
20872                 rm -f $DIR/$tfile
20873         fi
20874
20875         # define OBD_FAIL_OST_FAKE_RW           0x238
20876         do_facet ost1 $LCTL set_param fail_loc=0x238
20877
20878         local start_time=$(date +%s.%N)
20879         $dd_cmd bs=1M count=$blocks oflag=sync ||
20880                 error "fake dd $read_write error"
20881         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
20882
20883         if [ "$read_write" = "write" ]; then
20884                 # verify file size
20885                 cancel_lru_locks osc
20886                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
20887                         error "$tfile size not $blocks MB"
20888         fi
20889         do_facet ost1 $LCTL set_param fail_loc=0
20890
20891         echo "fake $read_write $duration_fake vs. normal $read_write" \
20892                 "$duration in seconds"
20893         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
20894                 error_not_in_vm "fake write is slower"
20895
20896         $LCTL set_param -n debug="$saved_debug"
20897         rm -f $DIR/$tfile
20898 }
20899 test_399a() { # LU-7655 for OST fake write
20900         remote_ost_nodsh && skip "remote OST with nodsh"
20901
20902         test_fake_rw write
20903 }
20904 run_test 399a "fake write should not be slower than normal write"
20905
20906 test_399b() { # LU-8726 for OST fake read
20907         remote_ost_nodsh && skip "remote OST with nodsh"
20908         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
20909                 skip_env "ldiskfs only test"
20910         fi
20911
20912         test_fake_rw read
20913 }
20914 run_test 399b "fake read should not be slower than normal read"
20915
20916 test_400a() { # LU-1606, was conf-sanity test_74
20917         if ! which $CC > /dev/null 2>&1; then
20918                 skip_env "$CC is not installed"
20919         fi
20920
20921         local extra_flags=''
20922         local out=$TMP/$tfile
20923         local prefix=/usr/include/lustre
20924         local prog
20925
20926         # Oleg removes c files in his test rig so test if any c files exist
20927         [ -z "$(ls -A $LUSTRE_TESTS_API_DIR)" ] && \
20928                 skip_env "Needed c test files are missing"
20929
20930         if ! [[ -d $prefix ]]; then
20931                 # Assume we're running in tree and fixup the include path.
20932                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
20933                 extra_flags+=" -L$LUSTRE/utils/.lib"
20934         fi
20935
20936         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
20937                 $CC -Wall -Werror -std=c99 $extra_flags -o $out $prog -llustreapi ||
20938                         error "client api broken"
20939         done
20940         rm -f $out
20941 }
20942 run_test 400a "Lustre client api program can compile and link"
20943
20944 test_400b() { # LU-1606, LU-5011
20945         local header
20946         local out=$TMP/$tfile
20947         local prefix=/usr/include/linux/lustre
20948
20949         # We use a hard coded prefix so that this test will not fail
20950         # when run in tree. There are headers in lustre/include/lustre/
20951         # that are not packaged (like lustre_idl.h) and have more
20952         # complicated include dependencies (like config.h and lnet/types.h).
20953         # Since this test about correct packaging we just skip them when
20954         # they don't exist (see below) rather than try to fixup cppflags.
20955
20956         if ! which $CC > /dev/null 2>&1; then
20957                 skip_env "$CC is not installed"
20958         fi
20959
20960         for header in $prefix/*.h; do
20961                 if ! [[ -f "$header" ]]; then
20962                         continue
20963                 fi
20964
20965                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
20966                         continue # lustre_ioctl.h is internal header
20967                 fi
20968
20969                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
20970                         error "cannot compile '$header'"
20971         done
20972         rm -f $out
20973 }
20974 run_test 400b "packaged headers can be compiled"
20975
20976 test_401a() { #LU-7437
20977         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
20978         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
20979
20980         #count the number of parameters by "list_param -R"
20981         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
20982         #count the number of parameters by listing proc files
20983         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
20984         echo "proc_dirs='$proc_dirs'"
20985         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
20986         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
20987                       sort -u | wc -l)
20988
20989         [ $params -eq $procs ] ||
20990                 error "found $params parameters vs. $procs proc files"
20991
20992         # test the list_param -D option only returns directories
20993         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
20994         #count the number of parameters by listing proc directories
20995         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
20996                 sort -u | wc -l)
20997
20998         [ $params -eq $procs ] ||
20999                 error "found $params parameters vs. $procs proc files"
21000 }
21001 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
21002
21003 test_401b() {
21004         local save=$($LCTL get_param -n jobid_var)
21005         local tmp=testing
21006
21007         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
21008                 error "no error returned when setting bad parameters"
21009
21010         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
21011         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
21012
21013         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
21014         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
21015         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
21016 }
21017 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
21018
21019 test_401c() {
21020         local jobid_var_old=$($LCTL get_param -n jobid_var)
21021         local jobid_var_new
21022
21023         $LCTL set_param jobid_var= &&
21024                 error "no error returned for 'set_param a='"
21025
21026         jobid_var_new=$($LCTL get_param -n jobid_var)
21027         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
21028                 error "jobid_var was changed by setting without value"
21029
21030         $LCTL set_param jobid_var &&
21031                 error "no error returned for 'set_param a'"
21032
21033         jobid_var_new=$($LCTL get_param -n jobid_var)
21034         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
21035                 error "jobid_var was changed by setting without value"
21036 }
21037 run_test 401c "Verify 'lctl set_param' without value fails in either format."
21038
21039 test_401d() {
21040         local jobid_var_old=$($LCTL get_param -n jobid_var)
21041         local jobid_var_new
21042         local new_value="foo=bar"
21043
21044         $LCTL set_param jobid_var=$new_value ||
21045                 error "'set_param a=b' did not accept a value containing '='"
21046
21047         jobid_var_new=$($LCTL get_param -n jobid_var)
21048         [[ "$jobid_var_new" == "$new_value" ]] ||
21049                 error "'set_param a=b' failed on a value containing '='"
21050
21051         # Reset the jobid_var to test the other format
21052         $LCTL set_param jobid_var=$jobid_var_old
21053         jobid_var_new=$($LCTL get_param -n jobid_var)
21054         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
21055                 error "failed to reset jobid_var"
21056
21057         $LCTL set_param jobid_var $new_value ||
21058                 error "'set_param a b' did not accept a value containing '='"
21059
21060         jobid_var_new=$($LCTL get_param -n jobid_var)
21061         [[ "$jobid_var_new" == "$new_value" ]] ||
21062                 error "'set_param a b' failed on a value containing '='"
21063
21064         $LCTL set_param jobid_var $jobid_var_old
21065         jobid_var_new=$($LCTL get_param -n jobid_var)
21066         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
21067                 error "failed to reset jobid_var"
21068 }
21069 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
21070
21071 test_402() {
21072         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
21073         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
21074                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
21075         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
21076                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
21077                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
21078         remote_mds_nodsh && skip "remote MDS with nodsh"
21079
21080         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
21081 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
21082         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
21083         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
21084                 echo "Touch failed - OK"
21085 }
21086 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
21087
21088 test_403() {
21089         local file1=$DIR/$tfile.1
21090         local file2=$DIR/$tfile.2
21091         local tfile=$TMP/$tfile
21092
21093         rm -f $file1 $file2 $tfile
21094
21095         touch $file1
21096         ln $file1 $file2
21097
21098         # 30 sec OBD_TIMEOUT in ll_getattr()
21099         # right before populating st_nlink
21100         $LCTL set_param fail_loc=0x80001409
21101         stat -c %h $file1 > $tfile &
21102
21103         # create an alias, drop all locks and reclaim the dentry
21104         < $file2
21105         cancel_lru_locks mdc
21106         cancel_lru_locks osc
21107         sysctl -w vm.drop_caches=2
21108
21109         wait
21110
21111         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
21112
21113         rm -f $tfile $file1 $file2
21114 }
21115 run_test 403 "i_nlink should not drop to zero due to aliasing"
21116
21117 test_404() { # LU-6601
21118         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
21119                 skip "Need server version newer than 2.8.52"
21120         remote_mds_nodsh && skip "remote MDS with nodsh"
21121
21122         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
21123                 awk '/osp .*-osc-MDT/ { print $4}')
21124
21125         local osp
21126         for osp in $mosps; do
21127                 echo "Deactivate: " $osp
21128                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
21129                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
21130                         awk -vp=$osp '$4 == p { print $2 }')
21131                 [ $stat = IN ] || {
21132                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
21133                         error "deactivate error"
21134                 }
21135                 echo "Activate: " $osp
21136                 do_facet $SINGLEMDS $LCTL --device %$osp activate
21137                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
21138                         awk -vp=$osp '$4 == p { print $2 }')
21139                 [ $stat = UP ] || {
21140                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
21141                         error "activate error"
21142                 }
21143         done
21144 }
21145 run_test 404 "validate manual {de}activated works properly for OSPs"
21146
21147 test_405() {
21148         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
21149         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
21150                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
21151                         skip "Layout swap lock is not supported"
21152
21153         check_swap_layouts_support
21154
21155         test_mkdir $DIR/$tdir
21156         swap_lock_test -d $DIR/$tdir ||
21157                 error "One layout swap locked test failed"
21158 }
21159 run_test 405 "Various layout swap lock tests"
21160
21161 test_406() {
21162         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21163         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
21164         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
21165         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21166         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
21167                 skip "Need MDS version at least 2.8.50"
21168
21169         local def_stripe_size=$($LFS getstripe -S $MOUNT)
21170         local test_pool=$TESTNAME
21171
21172         pool_add $test_pool || error "pool_add failed"
21173         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
21174                 error "pool_add_targets failed"
21175
21176         save_layout_restore_at_exit $MOUNT
21177
21178         # parent set default stripe count only, child will stripe from both
21179         # parent and fs default
21180         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
21181                 error "setstripe $MOUNT failed"
21182         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
21183         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
21184         for i in $(seq 10); do
21185                 local f=$DIR/$tdir/$tfile.$i
21186                 touch $f || error "touch failed"
21187                 local count=$($LFS getstripe -c $f)
21188                 [ $count -eq $OSTCOUNT ] ||
21189                         error "$f stripe count $count != $OSTCOUNT"
21190                 local offset=$($LFS getstripe -i $f)
21191                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
21192                 local size=$($LFS getstripe -S $f)
21193                 [ $size -eq $((def_stripe_size * 2)) ] ||
21194                         error "$f stripe size $size != $((def_stripe_size * 2))"
21195                 local pool=$($LFS getstripe -p $f)
21196                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
21197         done
21198
21199         # change fs default striping, delete parent default striping, now child
21200         # will stripe from new fs default striping only
21201         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
21202                 error "change $MOUNT default stripe failed"
21203         $LFS setstripe -c 0 $DIR/$tdir ||
21204                 error "delete $tdir default stripe failed"
21205         for i in $(seq 11 20); do
21206                 local f=$DIR/$tdir/$tfile.$i
21207                 touch $f || error "touch $f failed"
21208                 local count=$($LFS getstripe -c $f)
21209                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
21210                 local offset=$($LFS getstripe -i $f)
21211                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
21212                 local size=$($LFS getstripe -S $f)
21213                 [ $size -eq $def_stripe_size ] ||
21214                         error "$f stripe size $size != $def_stripe_size"
21215                 local pool=$($LFS getstripe -p $f)
21216                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
21217         done
21218
21219         unlinkmany $DIR/$tdir/$tfile. 1 20
21220
21221         local f=$DIR/$tdir/$tfile
21222         pool_remove_all_targets $test_pool $f
21223         pool_remove $test_pool $f
21224 }
21225 run_test 406 "DNE support fs default striping"
21226
21227 test_407() {
21228         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21229         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
21230                 skip "Need MDS version at least 2.8.55"
21231         remote_mds_nodsh && skip "remote MDS with nodsh"
21232
21233         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
21234                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
21235         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
21236                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
21237         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
21238
21239         #define OBD_FAIL_DT_TXN_STOP    0x2019
21240         for idx in $(seq $MDSCOUNT); do
21241                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
21242         done
21243         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
21244         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
21245                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
21246         true
21247 }
21248 run_test 407 "transaction fail should cause operation fail"
21249
21250 test_408() {
21251         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
21252
21253         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
21254         lctl set_param fail_loc=0x8000040a
21255         # let ll_prepare_partial_page() fail
21256         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
21257
21258         rm -f $DIR/$tfile
21259
21260         # create at least 100 unused inodes so that
21261         # shrink_icache_memory(0) should not return 0
21262         touch $DIR/$tfile-{0..100}
21263         rm -f $DIR/$tfile-{0..100}
21264         sync
21265
21266         echo 2 > /proc/sys/vm/drop_caches
21267 }
21268 run_test 408 "drop_caches should not hang due to page leaks"
21269
21270 test_409()
21271 {
21272         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21273
21274         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
21275         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
21276         touch $DIR/$tdir/guard || error "(2) Fail to create"
21277
21278         local PREFIX=$(str_repeat 'A' 128)
21279         echo "Create 1K hard links start at $(date)"
21280         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
21281                 error "(3) Fail to hard link"
21282
21283         echo "Links count should be right although linkEA overflow"
21284         stat $DIR/$tdir/guard || error "(4) Fail to stat"
21285         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
21286         [ $linkcount -eq 1001 ] ||
21287                 error "(5) Unexpected hard links count: $linkcount"
21288
21289         echo "List all links start at $(date)"
21290         ls -l $DIR/$tdir/foo > /dev/null ||
21291                 error "(6) Fail to list $DIR/$tdir/foo"
21292
21293         echo "Unlink hard links start at $(date)"
21294         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
21295                 error "(7) Fail to unlink"
21296         echo "Unlink hard links finished at $(date)"
21297 }
21298 run_test 409 "Large amount of cross-MDTs hard links on the same file"
21299
21300 test_410()
21301 {
21302         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
21303                 skip "Need client version at least 2.9.59"
21304
21305         # Create a file, and stat it from the kernel
21306         local testfile=$DIR/$tfile
21307         touch $testfile
21308
21309         local run_id=$RANDOM
21310         local my_ino=$(stat --format "%i" $testfile)
21311
21312         # Try to insert the module. This will always fail as the
21313         # module is designed to not be inserted.
21314         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
21315             &> /dev/null
21316
21317         # Anything but success is a test failure
21318         dmesg | grep -q \
21319             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
21320             error "no inode match"
21321 }
21322 run_test 410 "Test inode number returned from kernel thread"
21323
21324 cleanup_test411_cgroup() {
21325         trap 0
21326         rmdir "$1"
21327 }
21328
21329 test_411() {
21330         local cg_basedir=/sys/fs/cgroup/memory
21331         # LU-9966
21332         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
21333                 skip "no setup for cgroup"
21334
21335         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
21336                 error "test file creation failed"
21337         cancel_lru_locks osc
21338
21339         # Create a very small memory cgroup to force a slab allocation error
21340         local cgdir=$cg_basedir/osc_slab_alloc
21341         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
21342         trap "cleanup_test411_cgroup $cgdir" EXIT
21343         echo 2M > $cgdir/memory.kmem.limit_in_bytes
21344         echo 1M > $cgdir/memory.limit_in_bytes
21345
21346         # Should not LBUG, just be killed by oom-killer
21347         # dd will return 0 even allocation failure in some environment.
21348         # So don't check return value
21349         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
21350         cleanup_test411_cgroup $cgdir
21351
21352         return 0
21353 }
21354 run_test 411 "Slab allocation error with cgroup does not LBUG"
21355
21356 test_412() {
21357         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21358         if [ $MDS1_VERSION -lt $(version_code 2.10.55) ]; then
21359                 skip "Need server version at least 2.10.55"
21360         fi
21361
21362         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
21363                 error "mkdir failed"
21364         $LFS getdirstripe $DIR/$tdir
21365         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
21366         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
21367                 error "expect $((MDSCOUT - 1)) get $stripe_index"
21368         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
21369         [ $stripe_count -eq 2 ] ||
21370                 error "expect 2 get $stripe_count"
21371 }
21372 run_test 412 "mkdir on specific MDTs"
21373
21374 test_qos_mkdir() {
21375         local mkdir_cmd=$1
21376         local stripe_count=$2
21377         local mdts=$(comma_list $(mdts_nodes))
21378
21379         local testdir
21380         local lmv_qos_prio_free
21381         local lmv_qos_threshold_rr
21382         local lmv_qos_maxage
21383         local lod_qos_prio_free
21384         local lod_qos_threshold_rr
21385         local lod_qos_maxage
21386         local count
21387         local i
21388
21389         lmv_qos_prio_free=$($LCTL get_param -n lmv.*.qos_prio_free | head -n1)
21390         lmv_qos_prio_free=${lmv_qos_prio_free%%%}
21391         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
21392                 head -n1)
21393         lmv_qos_threshold_rr=${lmv_qos_threshold_rr%%%}
21394         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
21395         stack_trap "$LCTL set_param \
21396                 lmv.*.qos_prio_free=$lmv_qos_prio_free > /dev/null" EXIT
21397         stack_trap "$LCTL set_param \
21398                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null" EXIT
21399         stack_trap "$LCTL set_param \
21400                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null" EXIT
21401
21402         lod_qos_prio_free=$(do_facet mds1 $LCTL get_param -n \
21403                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_prio_free | head -n1)
21404         lod_qos_prio_free=${lod_qos_prio_free%%%}
21405         lod_qos_threshold_rr=$(do_facet mds1 $LCTL get_param -n \
21406                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_threshold_rr | head -n1)
21407         lod_qos_threshold_rr=${lod_qos_threshold_rr%%%}
21408         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
21409                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
21410         stack_trap "do_nodes $mdts $LCTL set_param \
21411                 lod.*.mdt_qos_prio_free=$lod_qos_prio_free > /dev/null" EXIT
21412         stack_trap "do_nodes $mdts $LCTL set_param \
21413                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null" \
21414                 EXIT
21415         stack_trap "do_nodes $mdts $LCTL set_param \
21416                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null" EXIT
21417
21418         echo
21419         echo "Mkdir (stripe_count $stripe_count) roundrobin:"
21420
21421         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
21422         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=100 > /dev/null
21423
21424         testdir=$DIR/$tdir-s$stripe_count/rr
21425
21426         for i in $(seq $((100 * MDSCOUNT))); do
21427                 eval $mkdir_cmd $testdir/subdir$i ||
21428                         error "$mkdir_cmd subdir$i failed"
21429         done
21430
21431         for i in $(seq $MDSCOUNT); do
21432                 count=$($LFS getdirstripe -i $testdir/* |
21433                                 grep ^$((i - 1))$ | wc -l)
21434                 echo "$count directories created on MDT$((i - 1))"
21435                 [ $count -eq 100 ] || error "subdirs are not evenly distributed"
21436
21437                 if [ $stripe_count -gt 1 ]; then
21438                         count=$($LFS getdirstripe $testdir/* |
21439                                 grep -P "^\s+$((i - 1))\t" | wc -l)
21440                         echo "$count stripes created on MDT$((i - 1))"
21441                         # deviation should < 5% of average
21442                         [ $count -lt $((95 * stripe_count)) ] ||
21443                         [ $count -gt $((105 * stripe_count)) ] &&
21444                                 error "stripes are not evenly distributed"
21445                 fi
21446         done
21447
21448         $LCTL set_param lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null
21449         do_nodes $mdts $LCTL set_param \
21450                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null
21451
21452         echo
21453         echo "Check for uneven MDTs: "
21454
21455         local ffree
21456         local bavail
21457         local max
21458         local min
21459         local max_index
21460         local min_index
21461         local tmp
21462
21463         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
21464         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
21465         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
21466
21467         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
21468         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
21469         max_index=0
21470         min_index=0
21471         for ((i = 1; i < ${#ffree[@]}; i++)); do
21472                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
21473                 if [ $tmp -gt $max ]; then
21474                         max=$tmp
21475                         max_index=$i
21476                 fi
21477                 if [ $tmp -lt $min ]; then
21478                         min=$tmp
21479                         min_index=$i
21480                 fi
21481         done
21482
21483         [ ${ffree[min_index]} -eq 0 ] &&
21484                 skip "no free files in MDT$min_index"
21485         [ ${ffree[min_index]} -gt 100000000 ] &&
21486                 skip "too much free files in MDT$min_index"
21487
21488         # Check if we need to generate uneven MDTs
21489         local threshold=50
21490         local diff=$(((max - min) * 100 / min))
21491         local value="$(generate_string 1024)"
21492
21493         while [ $diff -lt $threshold ]; do
21494                 # generate uneven MDTs, create till $threshold% diff
21495                 echo -n "weight diff=$diff% must be > $threshold% ..."
21496                 count=$((${ffree[min_index]} / 10))
21497                 # 50 sec per 10000 files in vm
21498                 [ $count -gt 40000 ] && [ "$SLOW" = "no" ] &&
21499                         skip "$count files to create"
21500                 echo "Fill MDT$min_index with $count files"
21501                 [ -d $DIR/$tdir-MDT$min_index ] ||
21502                         $LFS mkdir -i $min_index $DIR/$tdir-MDT$min_index ||
21503                         error "mkdir $tdir-MDT$min_index failed"
21504                 for i in $(seq $count); do
21505                         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE \
21506                                 $DIR/$tdir-MDT$min_index/f$j_$i > /dev/null ||
21507                                 error "create f$j_$i failed"
21508                         setfattr -n user.413b -v $value \
21509                                 $DIR/$tdir-MDT$min_index/f$j_$i ||
21510                                 error "setfattr f$j_$i failed"
21511                 done
21512
21513                 ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-*.filesfree))
21514                 bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-*.kbytesavail))
21515                 max=$(((${ffree[max_index]} >> 8) * \
21516                         (${bavail[max_index]} * bsize >> 16)))
21517                 min=$(((${ffree[min_index]} >> 8) * \
21518                         (${bavail[min_index]} * bsize >> 16)))
21519                 diff=$(((max - min) * 100 / min))
21520         done
21521
21522         echo "MDT filesfree available: ${ffree[@]}"
21523         echo "MDT blocks available: ${bavail[@]}"
21524         echo "weight diff=$diff%"
21525
21526         echo
21527         echo "Mkdir (stripe_count $stripe_count) with balanced space usage:"
21528
21529         $LCTL set_param lmv.*.qos_prio_free=100 > /dev/null
21530         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_prio_free=100 > /dev/null
21531         # decrease statfs age, so that it can be updated in time
21532         $LCTL set_param lmv.*.qos_maxage=1 > /dev/null
21533         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_maxage=1 > /dev/null
21534
21535         sleep 1
21536
21537         testdir=$DIR/$tdir-s$stripe_count/qos
21538
21539         for i in $(seq $((100 * MDSCOUNT))); do
21540                 eval $mkdir_cmd $testdir/subdir$i ||
21541                         error "$mkdir_cmd subdir$i failed"
21542         done
21543
21544         for i in $(seq $MDSCOUNT); do
21545                 count=$($LFS getdirstripe -i $testdir/* | grep ^$((i - 1))$ |
21546                         wc -l)
21547                 echo "$count directories created on MDT$((i - 1))"
21548
21549                 if [ $stripe_count -gt 1 ]; then
21550                         count=$($LFS getdirstripe $testdir/* |
21551                                 grep -P "^\s+$((i - 1))\t" | wc -l)
21552                         echo "$count stripes created on MDT$((i - 1))"
21553                 fi
21554         done
21555
21556         max=$($LFS getdirstripe -i $testdir/* | grep ^$max_index$ | wc -l)
21557         min=$($LFS getdirstripe -i $testdir/* | grep ^$min_index$ | wc -l)
21558
21559         # D-value should > 10% of averge
21560         [ $((max - min)) -lt 10 ] &&
21561                 error "subdirs shouldn't be evenly distributed"
21562
21563         # ditto
21564         if [ $stripe_count -gt 1 ]; then
21565                 max=$($LFS getdirstripe $testdir/* |
21566                         grep -P "^\s+$max_index\t" | wc -l)
21567                 min=$($LFS getdirstripe $testdir/* |
21568                         grep -P "^\s+$min_index\t" | wc -l)
21569                 [ $((max - min)) -le $((10 * stripe_count)) ] &&
21570                         error "stripes shouldn't be evenly distributed"|| true
21571         fi
21572 }
21573
21574 test_413a() {
21575         [ $MDSCOUNT -lt 2 ] &&
21576                 skip "We need at least 2 MDTs for this test"
21577
21578         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
21579                 skip "Need server version at least 2.12.52"
21580
21581         local stripe_count
21582
21583         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
21584                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
21585                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
21586                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
21587                 test_qos_mkdir "$LFS mkdir -c $stripe_count" $stripe_count
21588         done
21589 }
21590 run_test 413a "QoS mkdir with 'lfs mkdir -i -1'"
21591
21592 test_413b() {
21593         [ $MDSCOUNT -lt 2 ] &&
21594                 skip "We need at least 2 MDTs for this test"
21595
21596         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
21597                 skip "Need server version at least 2.12.52"
21598
21599         local stripe_count
21600
21601         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
21602                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
21603                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
21604                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
21605                 $LFS setdirstripe -D -c $stripe_count \
21606                         $DIR/$tdir-s$stripe_count/rr ||
21607                         error "setdirstripe failed"
21608                 $LFS setdirstripe -D -c $stripe_count \
21609                         $DIR/$tdir-s$stripe_count/qos ||
21610                         error "setdirstripe failed"
21611                 test_qos_mkdir "mkdir" $stripe_count
21612         done
21613 }
21614 run_test 413b "QoS mkdir under dir whose default LMV starting MDT offset is -1"
21615
21616 test_414() {
21617 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
21618         $LCTL set_param fail_loc=0x80000521
21619         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
21620         rm -f $DIR/$tfile
21621 }
21622 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
21623
21624 test_415() {
21625         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21626         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21627                 skip "Need server version at least 2.11.52"
21628
21629         # LU-11102
21630         local total
21631         local setattr_pid
21632         local start_time
21633         local end_time
21634         local duration
21635
21636         total=500
21637         # this test may be slow on ZFS
21638         [ "$mds1_FSTYPE" == "zfs" ] && total=100
21639
21640         # though this test is designed for striped directory, let's test normal
21641         # directory too since lock is always saved as CoS lock.
21642         test_mkdir $DIR/$tdir || error "mkdir $tdir"
21643         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
21644
21645         (
21646                 while true; do
21647                         touch $DIR/$tdir
21648                 done
21649         ) &
21650         setattr_pid=$!
21651
21652         start_time=$(date +%s)
21653         for i in $(seq $total); do
21654                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
21655                         > /dev/null
21656         done
21657         end_time=$(date +%s)
21658         duration=$((end_time - start_time))
21659
21660         kill -9 $setattr_pid
21661
21662         echo "rename $total files took $duration sec"
21663         [ $duration -lt 100 ] || error "rename took $duration sec"
21664 }
21665 run_test 415 "lock revoke is not missing"
21666
21667 test_416() {
21668         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
21669                 skip "Need server version at least 2.11.55"
21670
21671         # define OBD_FAIL_OSD_TXN_START    0x19a
21672         do_facet mds1 lctl set_param fail_loc=0x19a
21673
21674         lfs mkdir -c $MDSCOUNT $DIR/$tdir
21675
21676         true
21677 }
21678 run_test 416 "transaction start failure won't cause system hung"
21679
21680 cleanup_417() {
21681         trap 0
21682         do_nodes $(comma_list $(mdts_nodes)) \
21683                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
21684         do_nodes $(comma_list $(mdts_nodes)) \
21685                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
21686         do_nodes $(comma_list $(mdts_nodes)) \
21687                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
21688 }
21689
21690 test_417() {
21691         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21692         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
21693                 skip "Need MDS version at least 2.11.56"
21694
21695         trap cleanup_417 RETURN EXIT
21696
21697         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
21698         do_nodes $(comma_list $(mdts_nodes)) \
21699                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
21700         $LFS migrate -m 0 $DIR/$tdir.1 &&
21701                 error "migrate dir $tdir.1 should fail"
21702
21703         do_nodes $(comma_list $(mdts_nodes)) \
21704                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
21705         $LFS mkdir -i 1 $DIR/$tdir.2 &&
21706                 error "create remote dir $tdir.2 should fail"
21707
21708         do_nodes $(comma_list $(mdts_nodes)) \
21709                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
21710         $LFS mkdir -c 2 $DIR/$tdir.3 &&
21711                 error "create striped dir $tdir.3 should fail"
21712         true
21713 }
21714 run_test 417 "disable remote dir, striped dir and dir migration"
21715
21716 # Checks that the outputs of df [-i] and lfs df [-i] match
21717 #
21718 # usage: check_lfs_df <blocks | inodes> <mountpoint>
21719 check_lfs_df() {
21720         local dir=$2
21721         local inodes
21722         local df_out
21723         local lfs_df_out
21724         local count
21725         local passed=false
21726
21727         # blocks or inodes
21728         [ "$1" == "blocks" ] && inodes= || inodes="-i"
21729
21730         for count in {1..100}; do
21731                 cancel_lru_locks
21732                 sync; sleep 0.2
21733
21734                 # read the lines of interest
21735                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
21736                         error "df $inodes $dir | tail -n +2 failed"
21737                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
21738                         error "lfs df $inodes $dir | grep summary: failed"
21739
21740                 # skip first substrings of each output as they are different
21741                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
21742                 # compare the two outputs
21743                 passed=true
21744                 for i in {1..5}; do
21745                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
21746                 done
21747                 $passed && break
21748         done
21749
21750         if ! $passed; then
21751                 df -P $inodes $dir
21752                 echo
21753                 lfs df $inodes $dir
21754                 error "df and lfs df $1 output mismatch: "      \
21755                       "df ${inodes}: ${df_out[*]}, "            \
21756                       "lfs df ${inodes}: ${lfs_df_out[*]}"
21757         fi
21758 }
21759
21760 test_418() {
21761         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21762
21763         local dir=$DIR/$tdir
21764         local numfiles=$((RANDOM % 4096 + 2))
21765         local numblocks=$((RANDOM % 256 + 1))
21766
21767         wait_delete_completed
21768         test_mkdir $dir
21769
21770         # check block output
21771         check_lfs_df blocks $dir
21772         # check inode output
21773         check_lfs_df inodes $dir
21774
21775         # create a single file and retest
21776         echo "Creating a single file and testing"
21777         createmany -o $dir/$tfile- 1 &>/dev/null ||
21778                 error "creating 1 file in $dir failed"
21779         check_lfs_df blocks $dir
21780         check_lfs_df inodes $dir
21781
21782         # create a random number of files
21783         echo "Creating $((numfiles - 1)) files and testing"
21784         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
21785                 error "creating $((numfiles - 1)) files in $dir failed"
21786
21787         # write a random number of blocks to the first test file
21788         echo "Writing $numblocks 4K blocks and testing"
21789         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
21790                 count=$numblocks &>/dev/null ||
21791                 error "dd to $dir/${tfile}-0 failed"
21792
21793         # retest
21794         check_lfs_df blocks $dir
21795         check_lfs_df inodes $dir
21796
21797         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
21798                 error "unlinking $numfiles files in $dir failed"
21799 }
21800 run_test 418 "df and lfs df outputs match"
21801
21802 test_419()
21803 {
21804         local dir=$DIR/$tdir
21805
21806         mkdir -p $dir
21807         touch $dir/file
21808
21809         cancel_lru_locks mdc
21810
21811         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
21812         $LCTL set_param fail_loc=0x1410
21813         cat $dir/file
21814         $LCTL set_param fail_loc=0
21815         rm -rf $dir
21816 }
21817 run_test 419 "Verify open file by name doesn't crash kernel"
21818
21819 test_420()
21820 {
21821         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
21822                 skip "Need MDS version at least 2.12.53"
21823
21824         local SAVE_UMASK=$(umask)
21825         local dir=$DIR/$tdir
21826         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
21827
21828         mkdir -p $dir
21829         umask 0000
21830         mkdir -m03777 $dir/testdir
21831         ls -dn $dir/testdir
21832         # Need to remove trailing '.' when SELinux is enabled
21833         local dirperms=$(ls -dn $dir/testdir |
21834                          awk '{ sub(/\.$/, "", $1); print $1}')
21835         [ $dirperms == "drwxrwsrwt" ] ||
21836                 error "incorrect perms on $dir/testdir"
21837
21838         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
21839                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
21840         ls -n $dir/testdir/testfile
21841         local fileperms=$(ls -n $dir/testdir/testfile |
21842                           awk '{ sub(/\.$/, "", $1); print $1}')
21843         [ $fileperms == "-rwxr-xr-x" ] ||
21844                 error "incorrect perms on $dir/testdir/testfile"
21845
21846         umask $SAVE_UMASK
21847 }
21848 run_test 420 "clear SGID bit on non-directories for non-members"
21849
21850 test_421a() {
21851         local cnt
21852         local fid1
21853         local fid2
21854
21855         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21856                 skip "Need MDS version at least 2.12.54"
21857
21858         test_mkdir $DIR/$tdir
21859         createmany -o $DIR/$tdir/f 3
21860         cnt=$(ls -1 $DIR/$tdir | wc -l)
21861         [ $cnt != 3 ] && error "unexpected #files: $cnt"
21862
21863         fid1=$(lfs path2fid $DIR/$tdir/f1)
21864         fid2=$(lfs path2fid $DIR/$tdir/f2)
21865         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
21866
21867         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
21868         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
21869
21870         cnt=$(ls -1 $DIR/$tdir | wc -l)
21871         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
21872
21873         rm -f $DIR/$tdir/f3 || error "can't remove f3"
21874         createmany -o $DIR/$tdir/f 3
21875         cnt=$(ls -1 $DIR/$tdir | wc -l)
21876         [ $cnt != 3 ] && error "unexpected #files: $cnt"
21877
21878         fid1=$(lfs path2fid $DIR/$tdir/f1)
21879         fid2=$(lfs path2fid $DIR/$tdir/f2)
21880         echo "remove using fsname $FSNAME"
21881         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
21882
21883         cnt=$(ls -1 $DIR/$tdir | wc -l)
21884         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
21885 }
21886 run_test 421a "simple rm by fid"
21887
21888 test_421b() {
21889         local cnt
21890         local FID1
21891         local FID2
21892
21893         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21894                 skip "Need MDS version at least 2.12.54"
21895
21896         test_mkdir $DIR/$tdir
21897         createmany -o $DIR/$tdir/f 3
21898         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
21899         MULTIPID=$!
21900
21901         FID1=$(lfs path2fid $DIR/$tdir/f1)
21902         FID2=$(lfs path2fid $DIR/$tdir/f2)
21903         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
21904
21905         kill -USR1 $MULTIPID
21906         wait
21907
21908         cnt=$(ls $DIR/$tdir | wc -l)
21909         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
21910 }
21911 run_test 421b "rm by fid on open file"
21912
21913 test_421c() {
21914         local cnt
21915         local FIDS
21916
21917         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21918                 skip "Need MDS version at least 2.12.54"
21919
21920         test_mkdir $DIR/$tdir
21921         createmany -o $DIR/$tdir/f 3
21922         touch $DIR/$tdir/$tfile
21923         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
21924         cnt=$(ls -1 $DIR/$tdir | wc -l)
21925         [ $cnt != 184 ] && error "unexpected #files: $cnt"
21926
21927         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
21928         $LFS rmfid $DIR $FID1 || error "rmfid failed"
21929
21930         cnt=$(ls $DIR/$tdir | wc -l)
21931         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
21932 }
21933 run_test 421c "rm by fid against hardlinked files"
21934
21935 test_421d() {
21936         local cnt
21937         local FIDS
21938
21939         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21940                 skip "Need MDS version at least 2.12.54"
21941
21942         test_mkdir $DIR/$tdir
21943         createmany -o $DIR/$tdir/f 4097
21944         cnt=$(ls -1 $DIR/$tdir | wc -l)
21945         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
21946
21947         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
21948         $LFS rmfid $DIR $FIDS || error "rmfid failed"
21949
21950         cnt=$(ls $DIR/$tdir | wc -l)
21951         rm -rf $DIR/$tdir
21952         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21953 }
21954 run_test 421d "rmfid en masse"
21955
21956 test_421e() {
21957         local cnt
21958         local FID
21959
21960         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21961         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21962                 skip "Need MDS version at least 2.12.54"
21963
21964         mkdir -p $DIR/$tdir
21965         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
21966         createmany -o $DIR/$tdir/striped_dir/f 512
21967         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21968         [ $cnt != 512 ] && error "unexpected #files: $cnt"
21969
21970         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
21971                 sed "s/[/][^:]*://g")
21972         $LFS rmfid $DIR $FIDS || error "rmfid failed"
21973
21974         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
21975         rm -rf $DIR/$tdir
21976         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21977 }
21978 run_test 421e "rmfid in DNE"
21979
21980 test_421f() {
21981         local cnt
21982         local FID
21983
21984         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21985                 skip "Need MDS version at least 2.12.54"
21986
21987         test_mkdir $DIR/$tdir
21988         touch $DIR/$tdir/f
21989         cnt=$(ls -1 $DIR/$tdir | wc -l)
21990         [ $cnt != 1 ] && error "unexpected #files: $cnt"
21991
21992         FID=$(lfs path2fid $DIR/$tdir/f)
21993         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
21994         # rmfid should fail
21995         cnt=$(ls -1 $DIR/$tdir | wc -l)
21996         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
21997
21998         chmod a+rw $DIR/$tdir
21999         ls -la $DIR/$tdir
22000         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
22001         # rmfid should fail
22002         cnt=$(ls -1 $DIR/$tdir | wc -l)
22003         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
22004
22005         rm -f $DIR/$tdir/f
22006         $RUNAS touch $DIR/$tdir/f
22007         FID=$(lfs path2fid $DIR/$tdir/f)
22008         echo "rmfid as root"
22009         $LFS rmfid $DIR $FID || error "rmfid as root failed"
22010         cnt=$(ls -1 $DIR/$tdir | wc -l)
22011         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
22012
22013         rm -f $DIR/$tdir/f
22014         $RUNAS touch $DIR/$tdir/f
22015         cnt=$(ls -1 $DIR/$tdir | wc -l)
22016         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
22017         FID=$(lfs path2fid $DIR/$tdir/f)
22018         # rmfid w/o user_fid2path mount option should fail
22019         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
22020         cnt=$(ls -1 $DIR/$tdir | wc -l)
22021         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
22022
22023         umount_client $MOUNT || error "failed to umount client"
22024         mount_client $MOUNT "$MOUNT_OPTS,user_fid2path" ||
22025                 error "failed to mount client'"
22026
22027         $RUNAS $LFS rmfid $DIR $FID || error "rmfid failed"
22028         # rmfid should succeed
22029         cnt=$(ls -1 $DIR/$tdir | wc -l)
22030         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
22031
22032         # rmfid shouldn't allow to remove files due to dir's permission
22033         chmod a+rwx $DIR/$tdir
22034         touch $DIR/$tdir/f
22035         ls -la $DIR/$tdir
22036         FID=$(lfs path2fid $DIR/$tdir/f)
22037         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail"
22038
22039         umount_client $MOUNT || error "failed to umount client"
22040         mount_client $MOUNT "$MOUNT_OPTS" ||
22041                 error "failed to mount client'"
22042
22043 }
22044 run_test 421f "rmfid checks permissions"
22045
22046 test_421g() {
22047         local cnt
22048         local FIDS
22049
22050         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
22051         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22052                 skip "Need MDS version at least 2.12.54"
22053
22054         mkdir -p $DIR/$tdir
22055         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
22056         createmany -o $DIR/$tdir/striped_dir/f 512
22057         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
22058         [ $cnt != 512 ] && error "unexpected #files: $cnt"
22059
22060         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
22061                 sed "s/[/][^:]*://g")
22062
22063         rm -f $DIR/$tdir/striped_dir/f1*
22064         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
22065         removed=$((512 - cnt))
22066
22067         # few files have been just removed, so we expect
22068         # rmfid to fail on their fids
22069         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
22070         [ $removed != $errors ] && error "$errors != $removed"
22071
22072         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
22073         rm -rf $DIR/$tdir
22074         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
22075 }
22076 run_test 421g "rmfid to return errors properly"
22077
22078 test_422() {
22079         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d1
22080         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d2
22081         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d3
22082         dd if=/dev/zero of=$DIR/$tdir/d1/file1 bs=1k count=1
22083         dd if=/dev/zero of=$DIR/$tdir/d2/file1 bs=1k count=1
22084
22085         local amc=$(at_max_get client)
22086         local amo=$(at_max_get mds1)
22087         local timeout=`lctl get_param -n timeout`
22088
22089         at_max_set 0 client
22090         at_max_set 0 mds1
22091
22092 #define OBD_FAIL_PTLRPC_PAUSE_REQ        0x50a
22093         do_facet mds1 $LCTL set_param fail_loc=0x8000050a \
22094                         fail_val=$(((2*timeout + 10)*1000))
22095         touch $DIR/$tdir/d3/file &
22096         sleep 2
22097 #define OBD_FAIL_TGT_REPLY_DATA_RACE     0x722
22098         do_facet mds1 $LCTL set_param fail_loc=0x80000722 \
22099                         fail_val=$((2*timeout + 5))
22100         mv $DIR/$tdir/d1/file1 $DIR/$tdir/d1/file2 &
22101         local pid=$!
22102         sleep 1
22103         kill -9 $pid
22104         sleep $((2 * timeout))
22105         echo kill $pid
22106         kill -9 $pid
22107         lctl mark touch
22108         touch $DIR/$tdir/d2/file3
22109         touch $DIR/$tdir/d2/file4
22110         touch $DIR/$tdir/d2/file5
22111
22112         wait
22113         at_max_set $amc client
22114         at_max_set $amo mds1
22115
22116         # LU-12838 - verify the ptlrpc thread watchdog is not always throttled
22117         do_facet mds1 "dmesg | grep 'Dumping the stack trace for debugging'" ||
22118                 error "Watchdog is always throttled"
22119 }
22120 run_test 422 "kill a process with RPC in progress"
22121
22122 stat_test() {
22123     df -h $MOUNT &
22124     df -h $MOUNT &
22125     df -h $MOUNT &
22126     df -h $MOUNT &
22127     df -h $MOUNT &
22128     df -h $MOUNT &
22129 }
22130
22131 test_423() {
22132     local _stats
22133     # ensure statfs cache is expired
22134     sleep 2;
22135
22136     _stats=$(stat_test | grep $MOUNT | sort -u | wc -l)
22137     [[ ${_stats} -ne 1 ]] && error "statfs wrong"
22138
22139     return 0
22140 }
22141 run_test 423 "statfs should return a right data"
22142
22143 prep_801() {
22144         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
22145         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
22146                 skip "Need server version at least 2.9.55"
22147
22148         start_full_debug_logging
22149 }
22150
22151 post_801() {
22152         stop_full_debug_logging
22153 }
22154
22155 barrier_stat() {
22156         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
22157                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
22158                            awk '/The barrier for/ { print $7 }')
22159                 echo $st
22160         else
22161                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
22162                 echo \'$st\'
22163         fi
22164 }
22165
22166 barrier_expired() {
22167         local expired
22168
22169         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
22170                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
22171                           awk '/will be expired/ { print $7 }')
22172         else
22173                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
22174         fi
22175
22176         echo $expired
22177 }
22178
22179 test_801a() {
22180         prep_801
22181
22182         echo "Start barrier_freeze at: $(date)"
22183         #define OBD_FAIL_BARRIER_DELAY          0x2202
22184         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
22185         # Do not reduce barrier time - See LU-11873
22186         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
22187
22188         sleep 2
22189         local b_status=$(barrier_stat)
22190         echo "Got barrier status at: $(date)"
22191         [ "$b_status" = "'freezing_p1'" ] ||
22192                 error "(1) unexpected barrier status $b_status"
22193
22194         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
22195         wait
22196         b_status=$(barrier_stat)
22197         [ "$b_status" = "'frozen'" ] ||
22198                 error "(2) unexpected barrier status $b_status"
22199
22200         local expired=$(barrier_expired)
22201         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
22202         sleep $((expired + 3))
22203
22204         b_status=$(barrier_stat)
22205         [ "$b_status" = "'expired'" ] ||
22206                 error "(3) unexpected barrier status $b_status"
22207
22208         # Do not reduce barrier time - See LU-11873
22209         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
22210                 error "(4) fail to freeze barrier"
22211
22212         b_status=$(barrier_stat)
22213         [ "$b_status" = "'frozen'" ] ||
22214                 error "(5) unexpected barrier status $b_status"
22215
22216         echo "Start barrier_thaw at: $(date)"
22217         #define OBD_FAIL_BARRIER_DELAY          0x2202
22218         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
22219         do_facet mgs $LCTL barrier_thaw $FSNAME &
22220
22221         sleep 2
22222         b_status=$(barrier_stat)
22223         echo "Got barrier status at: $(date)"
22224         [ "$b_status" = "'thawing'" ] ||
22225                 error "(6) unexpected barrier status $b_status"
22226
22227         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
22228         wait
22229         b_status=$(barrier_stat)
22230         [ "$b_status" = "'thawed'" ] ||
22231                 error "(7) unexpected barrier status $b_status"
22232
22233         #define OBD_FAIL_BARRIER_FAILURE        0x2203
22234         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
22235         do_facet mgs $LCTL barrier_freeze $FSNAME
22236
22237         b_status=$(barrier_stat)
22238         [ "$b_status" = "'failed'" ] ||
22239                 error "(8) unexpected barrier status $b_status"
22240
22241         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
22242         do_facet mgs $LCTL barrier_thaw $FSNAME
22243
22244         post_801
22245 }
22246 run_test 801a "write barrier user interfaces and stat machine"
22247
22248 test_801b() {
22249         prep_801
22250
22251         mkdir $DIR/$tdir || error "(1) fail to mkdir"
22252         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
22253         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
22254         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
22255         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
22256
22257         cancel_lru_locks mdc
22258
22259         # 180 seconds should be long enough
22260         do_facet mgs $LCTL barrier_freeze $FSNAME 180
22261
22262         local b_status=$(barrier_stat)
22263         [ "$b_status" = "'frozen'" ] ||
22264                 error "(6) unexpected barrier status $b_status"
22265
22266         mkdir $DIR/$tdir/d0/d10 &
22267         mkdir_pid=$!
22268
22269         touch $DIR/$tdir/d1/f13 &
22270         touch_pid=$!
22271
22272         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
22273         ln_pid=$!
22274
22275         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
22276         mv_pid=$!
22277
22278         rm -f $DIR/$tdir/d4/f12 &
22279         rm_pid=$!
22280
22281         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
22282
22283         # To guarantee taht the 'stat' is not blocked
22284         b_status=$(barrier_stat)
22285         [ "$b_status" = "'frozen'" ] ||
22286                 error "(8) unexpected barrier status $b_status"
22287
22288         # let above commands to run at background
22289         sleep 5
22290
22291         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
22292         ps -p $touch_pid || error "(10) touch should be blocked"
22293         ps -p $ln_pid || error "(11) link should be blocked"
22294         ps -p $mv_pid || error "(12) rename should be blocked"
22295         ps -p $rm_pid || error "(13) unlink should be blocked"
22296
22297         b_status=$(barrier_stat)
22298         [ "$b_status" = "'frozen'" ] ||
22299                 error "(14) unexpected barrier status $b_status"
22300
22301         do_facet mgs $LCTL barrier_thaw $FSNAME
22302         b_status=$(barrier_stat)
22303         [ "$b_status" = "'thawed'" ] ||
22304                 error "(15) unexpected barrier status $b_status"
22305
22306         wait $mkdir_pid || error "(16) mkdir should succeed"
22307         wait $touch_pid || error "(17) touch should succeed"
22308         wait $ln_pid || error "(18) link should succeed"
22309         wait $mv_pid || error "(19) rename should succeed"
22310         wait $rm_pid || error "(20) unlink should succeed"
22311
22312         post_801
22313 }
22314 run_test 801b "modification will be blocked by write barrier"
22315
22316 test_801c() {
22317         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22318
22319         prep_801
22320
22321         stop mds2 || error "(1) Fail to stop mds2"
22322
22323         do_facet mgs $LCTL barrier_freeze $FSNAME 30
22324
22325         local b_status=$(barrier_stat)
22326         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
22327                 do_facet mgs $LCTL barrier_thaw $FSNAME
22328                 error "(2) unexpected barrier status $b_status"
22329         }
22330
22331         do_facet mgs $LCTL barrier_rescan $FSNAME ||
22332                 error "(3) Fail to rescan barrier bitmap"
22333
22334         # Do not reduce barrier time - See LU-11873
22335         do_facet mgs $LCTL barrier_freeze $FSNAME 20
22336
22337         b_status=$(barrier_stat)
22338         [ "$b_status" = "'frozen'" ] ||
22339                 error "(4) unexpected barrier status $b_status"
22340
22341         do_facet mgs $LCTL barrier_thaw $FSNAME
22342         b_status=$(barrier_stat)
22343         [ "$b_status" = "'thawed'" ] ||
22344                 error "(5) unexpected barrier status $b_status"
22345
22346         local devname=$(mdsdevname 2)
22347
22348         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
22349
22350         do_facet mgs $LCTL barrier_rescan $FSNAME ||
22351                 error "(7) Fail to rescan barrier bitmap"
22352
22353         post_801
22354 }
22355 run_test 801c "rescan barrier bitmap"
22356
22357 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
22358 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
22359 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
22360 saved_MOUNT_OPTS=$MOUNT_OPTS
22361
22362 cleanup_802a() {
22363         trap 0
22364
22365         stopall
22366         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
22367         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
22368         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
22369         MOUNT_OPTS=$saved_MOUNT_OPTS
22370         setupall
22371 }
22372
22373 test_802a() {
22374         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
22375         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
22376         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
22377                 skip "Need server version at least 2.9.55"
22378
22379         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
22380
22381         mkdir $DIR/$tdir || error "(1) fail to mkdir"
22382
22383         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
22384                 error "(2) Fail to copy"
22385
22386         trap cleanup_802a EXIT
22387
22388         # sync by force before remount as readonly
22389         sync; sync_all_data; sleep 3; sync_all_data
22390
22391         stopall
22392
22393         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
22394         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
22395         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
22396
22397         echo "Mount the server as read only"
22398         setupall server_only || error "(3) Fail to start servers"
22399
22400         echo "Mount client without ro should fail"
22401         mount_client $MOUNT &&
22402                 error "(4) Mount client without 'ro' should fail"
22403
22404         echo "Mount client with ro should succeed"
22405         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
22406         mount_client $MOUNT ||
22407                 error "(5) Mount client with 'ro' should succeed"
22408
22409         echo "Modify should be refused"
22410         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
22411
22412         echo "Read should be allowed"
22413         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
22414                 error "(7) Read should succeed under ro mode"
22415
22416         cleanup_802a
22417 }
22418 run_test 802a "simulate readonly device"
22419
22420 test_802b() {
22421         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22422         remote_mds_nodsh && skip "remote MDS with nodsh"
22423
22424         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
22425                 skip "readonly option not available"
22426
22427         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
22428
22429         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
22430                 error "(2) Fail to copy"
22431
22432         # write back all cached data before setting MDT to readonly
22433         cancel_lru_locks
22434         sync_all_data
22435
22436         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
22437         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
22438
22439         echo "Modify should be refused"
22440         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
22441
22442         echo "Read should be allowed"
22443         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
22444                 error "(7) Read should succeed under ro mode"
22445
22446         # disable readonly
22447         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
22448 }
22449 run_test 802b "be able to set MDTs to readonly"
22450
22451 test_803() {
22452         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22453         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
22454                 skip "MDS needs to be newer than 2.10.54"
22455
22456         mkdir -p $DIR/$tdir
22457         # Create some objects on all MDTs to trigger related logs objects
22458         for idx in $(seq $MDSCOUNT); do
22459                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
22460                         $DIR/$tdir/dir${idx} ||
22461                         error "Fail to create $DIR/$tdir/dir${idx}"
22462         done
22463
22464         sync; sleep 3
22465         wait_delete_completed # ensure old test cleanups are finished
22466         echo "before create:"
22467         $LFS df -i $MOUNT
22468         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
22469
22470         for i in {1..10}; do
22471                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
22472                         error "Fail to create $DIR/$tdir/foo$i"
22473         done
22474
22475         sync; sleep 3
22476         echo "after create:"
22477         $LFS df -i $MOUNT
22478         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
22479
22480         # allow for an llog to be cleaned up during the test
22481         [ $after_used -ge $((before_used + 10 - 1)) ] ||
22482                 error "before ($before_used) + 10 > after ($after_used)"
22483
22484         for i in {1..10}; do
22485                 rm -rf $DIR/$tdir/foo$i ||
22486                         error "Fail to remove $DIR/$tdir/foo$i"
22487         done
22488
22489         sleep 3 # avoid MDT return cached statfs
22490         wait_delete_completed
22491         echo "after unlink:"
22492         $LFS df -i $MOUNT
22493         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
22494
22495         # allow for an llog to be created during the test
22496         [ $after_used -le $((before_used + 1)) ] ||
22497                 error "after ($after_used) > before ($before_used) + 1"
22498 }
22499 run_test 803 "verify agent object for remote object"
22500
22501 test_804() {
22502         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22503         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
22504                 skip "MDS needs to be newer than 2.10.54"
22505         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
22506
22507         mkdir -p $DIR/$tdir
22508         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
22509                 error "Fail to create $DIR/$tdir/dir0"
22510
22511         local fid=$($LFS path2fid $DIR/$tdir/dir0)
22512         local dev=$(mdsdevname 2)
22513
22514         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22515                 grep ${fid} || error "NOT found agent entry for dir0"
22516
22517         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
22518                 error "Fail to create $DIR/$tdir/dir1"
22519
22520         touch $DIR/$tdir/dir1/foo0 ||
22521                 error "Fail to create $DIR/$tdir/dir1/foo0"
22522         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
22523         local rc=0
22524
22525         for idx in $(seq $MDSCOUNT); do
22526                 dev=$(mdsdevname $idx)
22527                 do_facet mds${idx} \
22528                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22529                         grep ${fid} && rc=$idx
22530         done
22531
22532         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
22533                 error "Fail to rename foo0 to foo1"
22534         if [ $rc -eq 0 ]; then
22535                 for idx in $(seq $MDSCOUNT); do
22536                         dev=$(mdsdevname $idx)
22537                         do_facet mds${idx} \
22538                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22539                         grep ${fid} && rc=$idx
22540                 done
22541         fi
22542
22543         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
22544                 error "Fail to rename foo1 to foo2"
22545         if [ $rc -eq 0 ]; then
22546                 for idx in $(seq $MDSCOUNT); do
22547                         dev=$(mdsdevname $idx)
22548                         do_facet mds${idx} \
22549                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22550                         grep ${fid} && rc=$idx
22551                 done
22552         fi
22553
22554         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
22555
22556         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
22557                 error "Fail to link to $DIR/$tdir/dir1/foo2"
22558         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
22559                 error "Fail to rename foo2 to foo0"
22560         unlink $DIR/$tdir/dir1/foo0 ||
22561                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
22562         rm -rf $DIR/$tdir/dir0 ||
22563                 error "Fail to rm $DIR/$tdir/dir0"
22564
22565         for idx in $(seq $MDSCOUNT); do
22566                 dev=$(mdsdevname $idx)
22567                 rc=0
22568
22569                 stop mds${idx}
22570                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
22571                         rc=$?
22572                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
22573                         error "mount mds$idx failed"
22574                 df $MOUNT > /dev/null 2>&1
22575
22576                 # e2fsck should not return error
22577                 [ $rc -eq 0 ] ||
22578                         error "e2fsck detected error on MDT${idx}: rc=$rc"
22579         done
22580 }
22581 run_test 804 "verify agent entry for remote entry"
22582
22583 cleanup_805() {
22584         do_facet $SINGLEMDS zfs set quota=$old $fsset
22585         unlinkmany $DIR/$tdir/f- 1000000
22586         trap 0
22587 }
22588
22589 test_805() {
22590         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
22591         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
22592         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
22593                 skip "netfree not implemented before 0.7"
22594         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
22595                 skip "Need MDS version at least 2.10.57"
22596
22597         local fsset
22598         local freekb
22599         local usedkb
22600         local old
22601         local quota
22602         local pref="osd-zfs.$FSNAME-MDT0000."
22603
22604         # limit available space on MDS dataset to meet nospace issue
22605         # quickly. then ZFS 0.7.2 can use reserved space if asked
22606         # properly (using netfree flag in osd_declare_destroy()
22607         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
22608         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
22609                 gawk '{print $3}')
22610         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
22611         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
22612         let "usedkb=usedkb-freekb"
22613         let "freekb=freekb/2"
22614         if let "freekb > 5000"; then
22615                 let "freekb=5000"
22616         fi
22617         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
22618         trap cleanup_805 EXIT
22619         mkdir $DIR/$tdir
22620         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
22621         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
22622         rm -rf $DIR/$tdir || error "not able to remove"
22623         do_facet $SINGLEMDS zfs set quota=$old $fsset
22624         trap 0
22625 }
22626 run_test 805 "ZFS can remove from full fs"
22627
22628 # Size-on-MDS test
22629 check_lsom_data()
22630 {
22631         local file=$1
22632         local size=$($LFS getsom -s $file)
22633         local expect=$(stat -c %s $file)
22634
22635         [[ $size == $expect ]] ||
22636                 error "$file expected size: $expect, got: $size"
22637
22638         local blocks=$($LFS getsom -b $file)
22639         expect=$(stat -c %b $file)
22640         [[ $blocks == $expect ]] ||
22641                 error "$file expected blocks: $expect, got: $blocks"
22642 }
22643
22644 check_lsom_size()
22645 {
22646         local size=$($LFS getsom -s $1)
22647         local expect=$2
22648
22649         [[ $size == $expect ]] ||
22650                 error "$file expected size: $expect, got: $size"
22651 }
22652
22653 test_806() {
22654         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
22655                 skip "Need MDS version at least 2.11.52"
22656
22657         local bs=1048576
22658
22659         touch $DIR/$tfile || error "touch $tfile failed"
22660
22661         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
22662         save_lustre_params client "llite.*.xattr_cache" > $save
22663         lctl set_param llite.*.xattr_cache=0
22664         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
22665
22666         # single-threaded write
22667         echo "Test SOM for single-threaded write"
22668         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
22669                 error "write $tfile failed"
22670         check_lsom_size $DIR/$tfile $bs
22671
22672         local num=32
22673         local size=$(($num * $bs))
22674         local offset=0
22675         local i
22676
22677         echo "Test SOM for single client multi-threaded($num) write"
22678         $TRUNCATE $DIR/$tfile 0
22679         for ((i = 0; i < $num; i++)); do
22680                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22681                 local pids[$i]=$!
22682                 offset=$((offset + $bs))
22683         done
22684         for (( i=0; i < $num; i++ )); do
22685                 wait ${pids[$i]}
22686         done
22687         check_lsom_size $DIR/$tfile $size
22688
22689         $TRUNCATE $DIR/$tfile 0
22690         for ((i = 0; i < $num; i++)); do
22691                 offset=$((offset - $bs))
22692                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22693                 local pids[$i]=$!
22694         done
22695         for (( i=0; i < $num; i++ )); do
22696                 wait ${pids[$i]}
22697         done
22698         check_lsom_size $DIR/$tfile $size
22699
22700         # multi-client writes
22701         num=$(get_node_count ${CLIENTS//,/ })
22702         size=$(($num * $bs))
22703         offset=0
22704         i=0
22705
22706         echo "Test SOM for multi-client ($num) writes"
22707         $TRUNCATE $DIR/$tfile 0
22708         for client in ${CLIENTS//,/ }; do
22709                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22710                 local pids[$i]=$!
22711                 i=$((i + 1))
22712                 offset=$((offset + $bs))
22713         done
22714         for (( i=0; i < $num; i++ )); do
22715                 wait ${pids[$i]}
22716         done
22717         check_lsom_size $DIR/$tfile $offset
22718
22719         i=0
22720         $TRUNCATE $DIR/$tfile 0
22721         for client in ${CLIENTS//,/ }; do
22722                 offset=$((offset - $bs))
22723                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22724                 local pids[$i]=$!
22725                 i=$((i + 1))
22726         done
22727         for (( i=0; i < $num; i++ )); do
22728                 wait ${pids[$i]}
22729         done
22730         check_lsom_size $DIR/$tfile $size
22731
22732         # verify truncate
22733         echo "Test SOM for truncate"
22734         $TRUNCATE $DIR/$tfile 1048576
22735         check_lsom_size $DIR/$tfile 1048576
22736         $TRUNCATE $DIR/$tfile 1234
22737         check_lsom_size $DIR/$tfile 1234
22738
22739         # verify SOM blocks count
22740         echo "Verify SOM block count"
22741         $TRUNCATE $DIR/$tfile 0
22742         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
22743                 error "failed to write file $tfile"
22744         check_lsom_data $DIR/$tfile
22745 }
22746 run_test 806 "Verify Lazy Size on MDS"
22747
22748 test_807() {
22749         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
22750         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
22751                 skip "Need MDS version at least 2.11.52"
22752
22753         # Registration step
22754         changelog_register || error "changelog_register failed"
22755         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
22756         changelog_users $SINGLEMDS | grep -q $cl_user ||
22757                 error "User $cl_user not found in changelog_users"
22758
22759         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
22760         save_lustre_params client "llite.*.xattr_cache" > $save
22761         lctl set_param llite.*.xattr_cache=0
22762         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
22763
22764         rm -rf $DIR/$tdir || error "rm $tdir failed"
22765         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
22766         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
22767         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
22768         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
22769                 error "truncate $tdir/trunc failed"
22770
22771         local bs=1048576
22772         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 ||
22773                 error "write $tfile failed"
22774
22775         # multi-client wirtes
22776         local num=$(get_node_count ${CLIENTS//,/ })
22777         local offset=0
22778         local i=0
22779
22780         echo "Test SOM for multi-client ($num) writes"
22781         touch $DIR/$tfile || error "touch $tfile failed"
22782         $TRUNCATE $DIR/$tfile 0
22783         for client in ${CLIENTS//,/ }; do
22784                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22785                 local pids[$i]=$!
22786                 i=$((i + 1))
22787                 offset=$((offset + $bs))
22788         done
22789         for (( i=0; i < $num; i++ )); do
22790                 wait ${pids[$i]}
22791         done
22792
22793         sleep 5
22794         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
22795         check_lsom_data $DIR/$tdir/trunc
22796         check_lsom_data $DIR/$tdir/single_dd
22797         check_lsom_data $DIR/$tfile
22798
22799         rm -rf $DIR/$tdir
22800         # Deregistration step
22801         changelog_deregister || error "changelog_deregister failed"
22802 }
22803 run_test 807 "verify LSOM syncing tool"
22804
22805 check_som_nologged()
22806 {
22807         local lines=$($LFS changelog $FSNAME-MDT0000 |
22808                 grep 'x=trusted.som' | wc -l)
22809         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
22810 }
22811
22812 test_808() {
22813         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
22814                 skip "Need MDS version at least 2.11.55"
22815
22816         # Registration step
22817         changelog_register || error "changelog_register failed"
22818
22819         touch $DIR/$tfile || error "touch $tfile failed"
22820         check_som_nologged
22821
22822         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
22823                 error "write $tfile failed"
22824         check_som_nologged
22825
22826         $TRUNCATE $DIR/$tfile 1234
22827         check_som_nologged
22828
22829         $TRUNCATE $DIR/$tfile 1048576
22830         check_som_nologged
22831
22832         # Deregistration step
22833         changelog_deregister || error "changelog_deregister failed"
22834 }
22835 run_test 808 "Check trusted.som xattr not logged in Changelogs"
22836
22837 check_som_nodata()
22838 {
22839         $LFS getsom $1
22840         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
22841 }
22842
22843 test_809() {
22844         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
22845                 skip "Need MDS version at least 2.11.56"
22846
22847         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
22848                 error "failed to create DoM-only file $DIR/$tfile"
22849         touch $DIR/$tfile || error "touch $tfile failed"
22850         check_som_nodata $DIR/$tfile
22851
22852         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
22853                 error "write $tfile failed"
22854         check_som_nodata $DIR/$tfile
22855
22856         $TRUNCATE $DIR/$tfile 1234
22857         check_som_nodata $DIR/$tfile
22858
22859         $TRUNCATE $DIR/$tfile 4097
22860         check_som_nodata $DIR/$file
22861 }
22862 run_test 809 "Verify no SOM xattr store for DoM-only files"
22863
22864 test_810() {
22865         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22866         $GSS && skip_env "could not run with gss"
22867         [[ $OST1_VERSION -gt $(version_code 2.12.58) ]] ||
22868                 skip "OST < 2.12.58 doesn't align checksum"
22869
22870         set_checksums 1
22871         stack_trap "set_checksums $ORIG_CSUM" EXIT
22872         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
22873
22874         local csum
22875         local before
22876         local after
22877         for csum in $CKSUM_TYPES; do
22878                 #define OBD_FAIL_OSC_NO_GRANT   0x411
22879                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
22880                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
22881                         eval set -- $i
22882                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
22883                         before=$(md5sum $DIR/$tfile)
22884                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
22885                         after=$(md5sum $DIR/$tfile)
22886                         [ "$before" == "$after" ] ||
22887                                 error "$csum: $before != $after bs=$1 seek=$2"
22888                 done
22889         done
22890 }
22891 run_test 810 "partial page writes on ZFS (LU-11663)"
22892
22893 test_811() {
22894         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
22895                 skip "Need MDS version at least 2.11.56"
22896
22897         #define OBD_FAIL_MDS_ORPHAN_DELETE      0x165
22898         do_facet mds1 $LCTL set_param fail_loc=0x165
22899         $MULTIOP $DIR/$tfile Ouc || error "multiop failed"
22900
22901         stop mds1
22902         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
22903
22904         wait_update_facet mds1 "pgrep orph_.*-MDD | wc -l" "0" ||
22905                 error "MDD orphan cleanup thread not quit"
22906 }
22907 run_test 811 "orphan name stub can be cleaned up in startup"
22908
22909 test_812a() {
22910         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
22911                 skip "OST < 2.12.51 doesn't support this fail_loc"
22912         [ "$SHARED_KEY" = true ] &&
22913                 skip "OSC connections never go IDLE with Shared-Keys enabled"
22914
22915         $LFS setstripe -c 1 -i 0 $DIR/$tfile
22916         # ensure ost1 is connected
22917         stat $DIR/$tfile >/dev/null || error "can't stat"
22918         wait_osc_import_state client ost1 FULL
22919         # no locks, no reqs to let the connection idle
22920         cancel_lru_locks osc
22921
22922         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
22923 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
22924         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
22925         wait_osc_import_state client ost1 CONNECTING
22926         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
22927
22928         stat $DIR/$tfile >/dev/null || error "can't stat file"
22929 }
22930 run_test 812a "do not drop reqs generated when imp is going to idle (LU-11951)"
22931
22932 test_812b() { # LU-12378
22933         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
22934                 skip "OST < 2.12.51 doesn't support this fail_loc"
22935         [ "$SHARED_KEY" = true ] &&
22936                 skip "OSC connections never go IDLE with Shared-Keys enabled"
22937
22938         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "setstripe failed"
22939         # ensure ost1 is connected
22940         stat $DIR/$tfile >/dev/null || error "can't stat"
22941         wait_osc_import_state client ost1 FULL
22942         # no locks, no reqs to let the connection idle
22943         cancel_lru_locks osc
22944
22945         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
22946 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
22947         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
22948         wait_osc_import_state client ost1 CONNECTING
22949         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
22950
22951         $LFS quota -u 0 $DIR/ || error "lfs quota should succeed"
22952         wait_osc_import_state client ost1 IDLE
22953 }
22954 run_test 812b "do not drop no resend request for idle connect"
22955
22956 test_813() {
22957         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
22958         [ -z "$file_heat_sav" ] && skip "no file heat support"
22959
22960         local readsample
22961         local writesample
22962         local readbyte
22963         local writebyte
22964         local readsample1
22965         local writesample1
22966         local readbyte1
22967         local writebyte1
22968
22969         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
22970         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
22971
22972         $LCTL set_param -n llite.*.file_heat=1
22973         echo "Turn on file heat"
22974         echo "Period second: $period_second, Decay percentage: $decay_pct"
22975
22976         echo "QQQQ" > $DIR/$tfile
22977         echo "QQQQ" > $DIR/$tfile
22978         echo "QQQQ" > $DIR/$tfile
22979         cat $DIR/$tfile > /dev/null
22980         cat $DIR/$tfile > /dev/null
22981         cat $DIR/$tfile > /dev/null
22982         cat $DIR/$tfile > /dev/null
22983
22984         local out=$($LFS heat_get $DIR/$tfile)
22985
22986         $LFS heat_get $DIR/$tfile
22987         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
22988         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
22989         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
22990         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
22991
22992         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
22993         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
22994         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
22995         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
22996
22997         sleep $((period_second + 3))
22998         echo "Sleep $((period_second + 3)) seconds..."
22999         # The recursion formula to calculate the heat of the file f is as
23000         # follow:
23001         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
23002         # Where Hi is the heat value in the period between time points i*I and
23003         # (i+1)*I; Ci is the access count in the period; the symbol P refers
23004         # to the weight of Ci.
23005         out=$($LFS heat_get $DIR/$tfile)
23006         $LFS heat_get $DIR/$tfile
23007         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23008         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23009         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23010         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23011
23012         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
23013                 error "read sample ($readsample) is wrong"
23014         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
23015                 error "write sample ($writesample) is wrong"
23016         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
23017                 error "read bytes ($readbyte) is wrong"
23018         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
23019                 error "write bytes ($writebyte) is wrong"
23020
23021         echo "QQQQ" > $DIR/$tfile
23022         echo "QQQQ" > $DIR/$tfile
23023         echo "QQQQ" > $DIR/$tfile
23024         cat $DIR/$tfile > /dev/null
23025         cat $DIR/$tfile > /dev/null
23026         cat $DIR/$tfile > /dev/null
23027         cat $DIR/$tfile > /dev/null
23028
23029         sleep $((period_second + 3))
23030         echo "Sleep $((period_second + 3)) seconds..."
23031
23032         out=$($LFS heat_get $DIR/$tfile)
23033         $LFS heat_get $DIR/$tfile
23034         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23035         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23036         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23037         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23038
23039         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
23040                 4 * $decay_pct) / 100") -eq 1 ] ||
23041                 error "read sample ($readsample1) is wrong"
23042         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
23043                 3 * $decay_pct) / 100") -eq 1 ] ||
23044                 error "write sample ($writesample1) is wrong"
23045         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
23046                 20 * $decay_pct) / 100") -eq 1 ] ||
23047                 error "read bytes ($readbyte1) is wrong"
23048         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
23049                 15 * $decay_pct) / 100") -eq 1 ] ||
23050                 error "write bytes ($writebyte1) is wrong"
23051
23052         echo "Turn off file heat for the file $DIR/$tfile"
23053         $LFS heat_set -o $DIR/$tfile
23054
23055         echo "QQQQ" > $DIR/$tfile
23056         echo "QQQQ" > $DIR/$tfile
23057         echo "QQQQ" > $DIR/$tfile
23058         cat $DIR/$tfile > /dev/null
23059         cat $DIR/$tfile > /dev/null
23060         cat $DIR/$tfile > /dev/null
23061         cat $DIR/$tfile > /dev/null
23062
23063         out=$($LFS heat_get $DIR/$tfile)
23064         $LFS heat_get $DIR/$tfile
23065         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23066         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23067         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23068         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23069
23070         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
23071         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
23072         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
23073         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
23074
23075         echo "Trun on file heat for the file $DIR/$tfile"
23076         $LFS heat_set -O $DIR/$tfile
23077
23078         echo "QQQQ" > $DIR/$tfile
23079         echo "QQQQ" > $DIR/$tfile
23080         echo "QQQQ" > $DIR/$tfile
23081         cat $DIR/$tfile > /dev/null
23082         cat $DIR/$tfile > /dev/null
23083         cat $DIR/$tfile > /dev/null
23084         cat $DIR/$tfile > /dev/null
23085
23086         out=$($LFS heat_get $DIR/$tfile)
23087         $LFS heat_get $DIR/$tfile
23088         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23089         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23090         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23091         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23092
23093         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
23094         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
23095         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
23096         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
23097
23098         $LFS heat_set -c $DIR/$tfile
23099         $LCTL set_param -n llite.*.file_heat=0
23100         echo "Turn off file heat support for the Lustre filesystem"
23101
23102         echo "QQQQ" > $DIR/$tfile
23103         echo "QQQQ" > $DIR/$tfile
23104         echo "QQQQ" > $DIR/$tfile
23105         cat $DIR/$tfile > /dev/null
23106         cat $DIR/$tfile > /dev/null
23107         cat $DIR/$tfile > /dev/null
23108         cat $DIR/$tfile > /dev/null
23109
23110         out=$($LFS heat_get $DIR/$tfile)
23111         $LFS heat_get $DIR/$tfile
23112         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23113         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23114         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23115         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23116
23117         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
23118         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
23119         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
23120         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
23121
23122         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
23123         rm -f $DIR/$tfile
23124 }
23125 run_test 813 "File heat verfication"
23126
23127 test_814()
23128 {
23129         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
23130         echo -n y >> $DIR/$tfile
23131         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
23132         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
23133 }
23134 run_test 814 "sparse cp works as expected (LU-12361)"
23135
23136 test_815()
23137 {
23138         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
23139         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
23140 }
23141 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
23142
23143 test_816() {
23144         [ "$SHARED_KEY" = true ] &&
23145                 skip "OSC connections never go IDLE with Shared-Keys enabled"
23146
23147         $LFS setstripe -c 1 -i 0 $DIR/$tfile
23148         # ensure ost1 is connected
23149         stat $DIR/$tfile >/dev/null || error "can't stat"
23150         wait_osc_import_state client ost1 FULL
23151         # no locks, no reqs to let the connection idle
23152         cancel_lru_locks osc
23153         lru_resize_disable osc
23154         local before
23155         local now
23156         before=$($LCTL get_param -n \
23157                  ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
23158
23159         wait_osc_import_state client ost1 IDLE
23160         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
23161         now=$($LCTL get_param -n \
23162               ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
23163         [ $before == $now ] || error "lru_size changed $before != $now"
23164 }
23165 run_test 816 "do not reset lru_resize on idle reconnect"
23166
23167 cleanup_817() {
23168         umount $tmpdir
23169         exportfs -u localhost:$DIR/nfsexp
23170         rm -rf $DIR/nfsexp
23171 }
23172
23173 test_817() {
23174         systemctl restart nfs-server.service || skip "failed to restart nfsd"
23175
23176         mkdir -p $DIR/nfsexp
23177         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
23178                 error "failed to export nfs"
23179
23180         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
23181         stack_trap cleanup_817 EXIT
23182
23183         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
23184                 error "failed to mount nfs to $tmpdir"
23185
23186         cp /bin/true $tmpdir
23187         $DIR/nfsexp/true || error "failed to execute 'true' command"
23188 }
23189 run_test 817 "nfsd won't cache write lock for exec file"
23190
23191 test_818() {
23192         mkdir $DIR/$tdir
23193         $LFS setstripe -c1 -i0 $DIR/$tfile
23194         $LFS setstripe -c1 -i1 $DIR/$tfile
23195         stop $SINGLEMDS
23196         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
23197         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
23198         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
23199                 error "start $SINGLEMDS failed"
23200         rm -rf $DIR/$tdir
23201 }
23202 run_test 818 "unlink with failed llog"
23203
23204 test_819a() {
23205         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23206         cancel_lru_locks osc
23207         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
23208         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
23209         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
23210         rm -f $TDIR/$tfile
23211 }
23212 run_test 819a "too big niobuf in read"
23213
23214 test_819b() {
23215         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
23216         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
23217         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23218         cancel_lru_locks osc
23219         sleep 1
23220         rm -f $TDIR/$tfile
23221 }
23222 run_test 819b "too big niobuf in write"
23223
23224
23225 function test_820_start_ost() {
23226         sleep 5
23227
23228         for num in $(seq $OSTCOUNT); do
23229                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS
23230         done
23231 }
23232
23233 test_820() {
23234         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
23235
23236         mkdir $DIR/$tdir
23237         umount_client $MOUNT || error "umount failed"
23238         for num in $(seq $OSTCOUNT); do
23239                 stop ost$num
23240         done
23241
23242         # mount client with no active OSTs
23243         # so that the client can't initialize max LOV EA size
23244         # from OSC notifications
23245         mount_client $MOUNT || error "mount failed"
23246         # delay OST starting to keep this 0 max EA size for a while
23247         test_820_start_ost &
23248
23249         # create a directory on MDS2
23250         test_mkdir -i 1 -c1 $DIR/$tdir/mds2 ||
23251                 error "Failed to create directory"
23252         # open intent should update default EA size
23253         # see mdc_update_max_ea_from_body()
23254         # notice this is the very first RPC to MDS2
23255         cp /etc/services $DIR/$tdir/mds2 ||
23256                 error "Failed to copy files to mds$n"
23257 }
23258 run_test 820 "update max EA from open intent"
23259
23260 #
23261 # tests that do cleanup/setup should be run at the end
23262 #
23263
23264 test_900() {
23265         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23266         local ls
23267
23268         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
23269         $LCTL set_param fail_loc=0x903
23270
23271         cancel_lru_locks MGC
23272
23273         FAIL_ON_ERROR=true cleanup
23274         FAIL_ON_ERROR=true setup
23275 }
23276 run_test 900 "umount should not race with any mgc requeue thread"
23277
23278 # LUS-6253/LU-11185
23279 test_901() {
23280         local oldc
23281         local newc
23282         local olds
23283         local news
23284         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23285
23286         # some get_param have a bug to handle dot in param name
23287         cancel_lru_locks MGC
23288         oldc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
23289         olds=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
23290         umount_client $MOUNT || error "umount failed"
23291         mount_client $MOUNT || error "mount failed"
23292         cancel_lru_locks MGC
23293         newc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
23294         news=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
23295
23296         [ $oldc -lt $newc ] && error "mgc lock leak ($oldc != $newc)"
23297         [ $olds -lt $news ] && error "mgs lock leak ($olds != $news)"
23298
23299         return 0
23300 }
23301 run_test 901 "don't leak a mgc lock on client umount"
23302
23303 # LU-13377
23304 test_902() {
23305         [ $CLIENT_VERSION -lt $(version_code 2.13.52) ] &&
23306                 skip "client does not have LU-13377 fix"
23307         #define OBD_FAIL_LLITE_SHORT_COMMIT 0x1415
23308         $LCTL set_param fail_loc=0x1415
23309         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23310         cancel_lru_locks osc
23311         rm -f $DIR/$tfile
23312 }
23313 run_test 902 "test short write doesn't hang lustre"
23314
23315 complete $SECONDS
23316 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
23317 check_and_cleanup_lustre
23318 if [ "$I_MOUNTED" != "yes" ]; then
23319         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
23320 fi
23321 exit_status