Whamcloud - gitweb
LU-10401 procs: print new line based on distro
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
7 set -e
8
9 ONLY=${ONLY:-"$*"}
10
11 # Check Grants after these tests
12 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c 64d"
13
14 OSC=${OSC:-"osc"}
15
16 CC=${CC:-cc}
17 CREATETEST=${CREATETEST:-createtest}
18 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
19 OPENFILE=${OPENFILE:-openfile}
20 OPENUNLINK=${OPENUNLINK:-openunlink}
21 READS=${READS:-"reads"}
22 MUNLINK=${MUNLINK:-munlink}
23 SOCKETSERVER=${SOCKETSERVER:-socketserver}
24 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
25 MEMHOG=${MEMHOG:-memhog}
26 DIRECTIO=${DIRECTIO:-directio}
27 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
28 DEF_STRIPE_COUNT=-1
29 CHECK_GRANT=${CHECK_GRANT:-"yes"}
30 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
31 export PARALLEL=${PARALLEL:-"no"}
32
33 TRACE=${TRACE:-""}
34 LUSTRE=${LUSTRE:-$(dirname $0)/..}
35 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
36 . $LUSTRE/tests/test-framework.sh
37 init_test_env $@
38
39 init_logging
40
41 ALWAYS_EXCEPT="$SANITY_EXCEPT "
42 # bug number for skipped test: LU-9693 LU-6493 LU-9693
43 ALWAYS_EXCEPT+="               42a     42b     42c "
44 # bug number:    LU-8411 LU-9054 LU-13314
45 ALWAYS_EXCEPT+=" 407     312    56ob"
46
47 if $SHARED_KEY; then
48         # bug number:    LU-9795 LU-9795 LU-9795 LU-9795
49         ALWAYS_EXCEPT+=" 17n     60a     133g    300f"
50 fi
51
52 selinux_status=$(getenforce)
53 if [ "$selinux_status" != "Disabled" ]; then
54         # bug number:
55         ALWAYS_EXCEPT+=""
56 fi
57
58 # skip the grant tests for ARM until they are fixed
59 if [[ $(uname -m) = aarch64 ]]; then
60         # bug number:    LU-11596
61         ALWAYS_EXCEPT+=" $GRANT_CHECK_LIST"
62         # bug number:    LU-11671 LU-11667
63         ALWAYS_EXCEPT+=" 45       317"
64 fi
65
66 # skip nfs tests on kernels >= 4.14.0 until they are fixed
67 if [ $LINUX_VERSION_CODE -ge $(version_code 4.14.0) ]; then
68         # bug number:   LU-12661
69         ALWAYS_EXCEPT+=" 817"
70 fi
71 # skip cgroup tests on RHEL8.1 kernels until they are fixed
72 if (( $LINUX_VERSION_CODE >= $(version_code 4.18.0) &&
73       $LINUX_VERSION_CODE <  $(version_code 5.4.0) )); then
74         # bug number:   LU-13063
75         ALWAYS_EXCEPT+=" 411"
76 fi
77
78 #                                  5          12     8   12  (min)"
79 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 135 136 300o"
80
81 if [ "$mds1_FSTYPE" = "zfs" ]; then
82         # bug number for skipped test:
83         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  "
84         #                                               13    (min)"
85         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
86 fi
87
88 # Get the SLES distro version
89 #
90 # Returns a version string that should only be used in comparing
91 # strings returned by version_code()
92 sles_version_code()
93 {
94         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
95
96         # All SuSE Linux versions have one decimal. version_code expects two
97         local sles_version=$version.0
98         version_code $sles_version
99 }
100
101 # Check if we are running on Ubuntu or SLES so we can make decisions on
102 # what tests to run
103 if [ -r /etc/SuSE-release ]; then
104         sles_version=$(sles_version_code)
105         [ $sles_version -lt $(version_code 11.4.0) ] &&
106                 # bug number for skipped test: LU-4341
107                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
108         [ $sles_version -lt $(version_code 12.0.0) ] &&
109                 # bug number for skipped test: LU-3703
110                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
111 elif [ -r /etc/os-release ]; then
112         if grep -qi ubuntu /etc/os-release; then
113                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
114                                                 -e 's/^VERSION=//p' \
115                                                 /etc/os-release |
116                                                 awk '{ print $1 }'))
117
118                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
119                         # bug number for skipped test:
120                         #                LU-10334 LU-10366
121                         ALWAYS_EXCEPT+=" 103a     410"
122                 fi
123         fi
124 fi
125
126 build_test_filter
127 FAIL_ON_ERROR=false
128
129 cleanup() {
130         echo -n "cln.."
131         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
132         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
133 }
134 setup() {
135         echo -n "mnt.."
136         load_modules
137         setupall || exit 10
138         echo "done"
139 }
140
141 check_swap_layouts_support()
142 {
143         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
144                 skip "Does not support layout lock."
145 }
146
147 check_and_setup_lustre
148 DIR=${DIR:-$MOUNT}
149 assert_DIR
150
151 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
152
153 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
154 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
155 rm -rf $DIR/[Rdfs][0-9]*
156
157 # $RUNAS_ID may get set incorrectly somewhere else
158 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
159         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
160
161 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
162
163 if [ "${ONLY}" = "MOUNT" ] ; then
164         echo "Lustre is up, please go on"
165         exit
166 fi
167
168 echo "preparing for tests involving mounts"
169 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
170 touch $EXT2_DEV
171 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
172 echo # add a newline after mke2fs.
173
174 umask 077
175
176 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
177 lctl set_param debug=-1 2> /dev/null || true
178 test_0a() {
179         touch $DIR/$tfile
180         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
181         rm $DIR/$tfile
182         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
183 }
184 run_test 0a "touch; rm ====================="
185
186 test_0b() {
187         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
188         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
189 }
190 run_test 0b "chmod 0755 $DIR ============================="
191
192 test_0c() {
193         $LCTL get_param mdc.*.import | grep "state: FULL" ||
194                 error "import not FULL"
195         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
196                 error "bad target"
197 }
198 run_test 0c "check import proc"
199
200 test_0d() { # LU-3397
201         [ $MGS_VERSION -lt $(version_code 2.10.57) ] &&
202                 skip "proc exports not supported before 2.10.57"
203
204         local mgs_exp="mgs.MGS.exports"
205         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
206         local exp_client_nid
207         local exp_client_version
208         local exp_val
209         local imp_val
210         local temp_imp=$DIR/$tfile.import
211         local temp_exp=$DIR/$tfile.export
212
213         # save mgc import file to $temp_imp
214         $LCTL get_param mgc.*.import | tee $temp_imp
215         # Check if client uuid is found in MGS export
216         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
217                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
218                         $client_uuid ] &&
219                         break;
220         done
221         # save mgs export file to $temp_exp
222         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
223
224         # Compare the value of field "connect_flags"
225         imp_val=$(grep "connect_flags" $temp_imp)
226         exp_val=$(grep "connect_flags" $temp_exp)
227         [ "$exp_val" == "$imp_val" ] ||
228                 error "export flags '$exp_val' != import flags '$imp_val'"
229
230         # Compare the value of client version
231         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
232         exp_val=$(version_code $exp_client_version)
233         imp_val=$CLIENT_VERSION
234         [ "$exp_val" == "$imp_val" ] ||
235                 error "export client version '$exp_val' != '$imp_val'"
236 }
237 run_test 0d "check export proc ============================="
238
239 test_1() {
240         test_mkdir $DIR/$tdir
241         test_mkdir $DIR/$tdir/d2
242         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
243         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
244         rmdir $DIR/$tdir/d2
245         rmdir $DIR/$tdir
246         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
247 }
248 run_test 1 "mkdir; remkdir; rmdir"
249
250 test_2() {
251         test_mkdir $DIR/$tdir
252         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
253         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
254         rm -r $DIR/$tdir
255         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
256 }
257 run_test 2 "mkdir; touch; rmdir; check file"
258
259 test_3() {
260         test_mkdir $DIR/$tdir
261         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
262         touch $DIR/$tdir/$tfile
263         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
264         rm -r $DIR/$tdir
265         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
266 }
267 run_test 3 "mkdir; touch; rmdir; check dir"
268
269 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
270 test_4() {
271         test_mkdir -i 1 $DIR/$tdir
272
273         touch $DIR/$tdir/$tfile ||
274                 error "Create file under remote directory failed"
275
276         rmdir $DIR/$tdir &&
277                 error "Expect error removing in-use dir $DIR/$tdir"
278
279         test -d $DIR/$tdir || error "Remote directory disappeared"
280
281         rm -rf $DIR/$tdir || error "remove remote dir error"
282 }
283 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
284
285 test_5() {
286         test_mkdir $DIR/$tdir
287         test_mkdir $DIR/$tdir/d2
288         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
289         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
290         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
291 }
292 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
293
294 test_6a() {
295         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
296         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
297         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
298                 error "$tfile does not have perm 0666 or UID $UID"
299         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
300         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
301                 error "$tfile should be 0666 and owned by UID $UID"
302 }
303 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
304
305 test_6c() {
306         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
307
308         touch $DIR/$tfile
309         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
310         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
311                 error "$tfile should be owned by UID $RUNAS_ID"
312         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
313         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
314                 error "$tfile should be owned by UID $RUNAS_ID"
315 }
316 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
317
318 test_6e() {
319         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
320
321         touch $DIR/$tfile
322         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
323         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
324                 error "$tfile should be owned by GID $UID"
325         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
326         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
327                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
328 }
329 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
330
331 test_6g() {
332         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
333
334         test_mkdir $DIR/$tdir
335         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
336         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
337         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
338         test_mkdir $DIR/$tdir/d/subdir
339         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
340                 error "$tdir/d/subdir should be GID $RUNAS_GID"
341         if [[ $MDSCOUNT -gt 1 ]]; then
342                 # check remote dir sgid inherite
343                 $LFS mkdir -i 0 $DIR/$tdir.local ||
344                         error "mkdir $tdir.local failed"
345                 chmod g+s $DIR/$tdir.local ||
346                         error "chmod $tdir.local failed"
347                 chgrp $RUNAS_GID $DIR/$tdir.local ||
348                         error "chgrp $tdir.local failed"
349                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
350                         error "mkdir $tdir.remote failed"
351                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
352                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
353                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
354                         error "$tdir.remote should be mode 02755"
355         fi
356 }
357 run_test 6g "verify new dir in sgid dir inherits group"
358
359 test_6h() { # bug 7331
360         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
361
362         touch $DIR/$tfile || error "touch failed"
363         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
364         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
365                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
366         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
367                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
368 }
369 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
370
371 test_7a() {
372         test_mkdir $DIR/$tdir
373         $MCREATE $DIR/$tdir/$tfile
374         chmod 0666 $DIR/$tdir/$tfile
375         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
376                 error "$tdir/$tfile should be mode 0666"
377 }
378 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
379
380 test_7b() {
381         if [ ! -d $DIR/$tdir ]; then
382                 test_mkdir $DIR/$tdir
383         fi
384         $MCREATE $DIR/$tdir/$tfile
385         echo -n foo > $DIR/$tdir/$tfile
386         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
387         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
388 }
389 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
390
391 test_8() {
392         test_mkdir $DIR/$tdir
393         touch $DIR/$tdir/$tfile
394         chmod 0666 $DIR/$tdir/$tfile
395         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
396                 error "$tfile mode not 0666"
397 }
398 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
399
400 test_9() {
401         test_mkdir $DIR/$tdir
402         test_mkdir $DIR/$tdir/d2
403         test_mkdir $DIR/$tdir/d2/d3
404         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
405 }
406 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
407
408 test_10() {
409         test_mkdir $DIR/$tdir
410         test_mkdir $DIR/$tdir/d2
411         touch $DIR/$tdir/d2/$tfile
412         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
413                 error "$tdir/d2/$tfile not a file"
414 }
415 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
416
417 test_11() {
418         test_mkdir $DIR/$tdir
419         test_mkdir $DIR/$tdir/d2
420         chmod 0666 $DIR/$tdir/d2
421         chmod 0705 $DIR/$tdir/d2
422         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
423                 error "$tdir/d2 mode not 0705"
424 }
425 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
426
427 test_12() {
428         test_mkdir $DIR/$tdir
429         touch $DIR/$tdir/$tfile
430         chmod 0666 $DIR/$tdir/$tfile
431         chmod 0654 $DIR/$tdir/$tfile
432         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
433                 error "$tdir/d2 mode not 0654"
434 }
435 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
436
437 test_13() {
438         test_mkdir $DIR/$tdir
439         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
440         >  $DIR/$tdir/$tfile
441         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
442                 error "$tdir/$tfile size not 0 after truncate"
443 }
444 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
445
446 test_14() {
447         test_mkdir $DIR/$tdir
448         touch $DIR/$tdir/$tfile
449         rm $DIR/$tdir/$tfile
450         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
451 }
452 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
453
454 test_15() {
455         test_mkdir $DIR/$tdir
456         touch $DIR/$tdir/$tfile
457         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
458         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
459                 error "$tdir/${tfile_2} not a file after rename"
460         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
461 }
462 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
463
464 test_16() {
465         test_mkdir $DIR/$tdir
466         touch $DIR/$tdir/$tfile
467         rm -rf $DIR/$tdir/$tfile
468         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
469 }
470 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
471
472 test_17a() {
473         test_mkdir $DIR/$tdir
474         touch $DIR/$tdir/$tfile
475         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
476         ls -l $DIR/$tdir
477         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
478                 error "$tdir/l-exist not a symlink"
479         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
480                 error "$tdir/l-exist not referencing a file"
481         rm -f $DIR/$tdir/l-exist
482         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
483 }
484 run_test 17a "symlinks: create, remove (real)"
485
486 test_17b() {
487         test_mkdir $DIR/$tdir
488         ln -s no-such-file $DIR/$tdir/l-dangle
489         ls -l $DIR/$tdir
490         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
491                 error "$tdir/l-dangle not referencing no-such-file"
492         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
493                 error "$tdir/l-dangle not referencing non-existent file"
494         rm -f $DIR/$tdir/l-dangle
495         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
496 }
497 run_test 17b "symlinks: create, remove (dangling)"
498
499 test_17c() { # bug 3440 - don't save failed open RPC for replay
500         test_mkdir $DIR/$tdir
501         ln -s foo $DIR/$tdir/$tfile
502         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
503 }
504 run_test 17c "symlinks: open dangling (should return error)"
505
506 test_17d() {
507         test_mkdir $DIR/$tdir
508         ln -s foo $DIR/$tdir/$tfile
509         touch $DIR/$tdir/$tfile || error "creating to new symlink"
510 }
511 run_test 17d "symlinks: create dangling"
512
513 test_17e() {
514         test_mkdir $DIR/$tdir
515         local foo=$DIR/$tdir/$tfile
516         ln -s $foo $foo || error "create symlink failed"
517         ls -l $foo || error "ls -l failed"
518         ls $foo && error "ls not failed" || true
519 }
520 run_test 17e "symlinks: create recursive symlink (should return error)"
521
522 test_17f() {
523         test_mkdir $DIR/$tdir
524         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
525         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
526         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
527         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
528         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
529         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/$tdir/666
530         ls -l  $DIR/$tdir
531 }
532 run_test 17f "symlinks: long and very long symlink name"
533
534 # str_repeat(S, N) generate a string that is string S repeated N times
535 str_repeat() {
536         local s=$1
537         local n=$2
538         local ret=''
539         while [ $((n -= 1)) -ge 0 ]; do
540                 ret=$ret$s
541         done
542         echo $ret
543 }
544
545 # Long symlinks and LU-2241
546 test_17g() {
547         test_mkdir $DIR/$tdir
548         local TESTS="59 60 61 4094 4095"
549
550         # Fix for inode size boundary in 2.1.4
551         [ $MDS1_VERSION -lt $(version_code 2.1.4) ] &&
552                 TESTS="4094 4095"
553
554         # Patch not applied to 2.2 or 2.3 branches
555         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
556         [ $MDS1_VERSION -le $(version_code 2.3.55) ] &&
557                 TESTS="4094 4095"
558
559         for i in $TESTS; do
560                 local SYMNAME=$(str_repeat 'x' $i)
561                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
562                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
563         done
564 }
565 run_test 17g "symlinks: really long symlink name and inode boundaries"
566
567 test_17h() { #bug 17378
568         [ $PARALLEL == "yes" ] && skip "skip parallel run"
569         remote_mds_nodsh && skip "remote MDS with nodsh"
570
571         local mdt_idx
572
573         test_mkdir $DIR/$tdir
574         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
575         $LFS setstripe -c -1 $DIR/$tdir
576         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
577         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
578         touch $DIR/$tdir/$tfile || true
579 }
580 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
581
582 test_17i() { #bug 20018
583         [ $PARALLEL == "yes" ] && skip "skip parallel run"
584         remote_mds_nodsh && skip "remote MDS with nodsh"
585
586         local foo=$DIR/$tdir/$tfile
587         local mdt_idx
588
589         test_mkdir -c1 $DIR/$tdir
590         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
591         ln -s $foo $foo || error "create symlink failed"
592 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
593         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
594         ls -l $foo && error "error not detected"
595         return 0
596 }
597 run_test 17i "don't panic on short symlink (should return error)"
598
599 test_17k() { #bug 22301
600         [ $PARALLEL == "yes" ] && skip "skip parallel run"
601         [[ -z "$(which rsync 2>/dev/null)" ]] &&
602                 skip "no rsync command"
603         rsync --help | grep -q xattr ||
604                 skip_env "$(rsync --version | head -n1) does not support xattrs"
605         test_mkdir $DIR/$tdir
606         test_mkdir $DIR/$tdir.new
607         touch $DIR/$tdir/$tfile
608         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
609         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
610                 error "rsync failed with xattrs enabled"
611 }
612 run_test 17k "symlinks: rsync with xattrs enabled"
613
614 test_17l() { # LU-279
615         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
616                 skip "no getfattr command"
617
618         test_mkdir $DIR/$tdir
619         touch $DIR/$tdir/$tfile
620         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
621         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
622                 # -h to not follow symlinks. -m '' to list all the xattrs.
623                 # grep to remove first line: '# file: $path'.
624                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
625                 do
626                         lgetxattr_size_check $path $xattr ||
627                                 error "lgetxattr_size_check $path $xattr failed"
628                 done
629         done
630 }
631 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
632
633 # LU-1540
634 test_17m() {
635         [ $PARALLEL == "yes" ] && skip "skip parallel run"
636         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
637         remote_mds_nodsh && skip "remote MDS with nodsh"
638         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
639         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
640                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
641
642         local short_sym="0123456789"
643         local wdir=$DIR/$tdir
644         local i
645
646         test_mkdir $wdir
647         long_sym=$short_sym
648         # create a long symlink file
649         for ((i = 0; i < 4; ++i)); do
650                 long_sym=${long_sym}${long_sym}
651         done
652
653         echo "create 512 short and long symlink files under $wdir"
654         for ((i = 0; i < 256; ++i)); do
655                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
656                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
657         done
658
659         echo "erase them"
660         rm -f $wdir/*
661         sync
662         wait_delete_completed
663
664         echo "recreate the 512 symlink files with a shorter string"
665         for ((i = 0; i < 512; ++i)); do
666                 # rewrite the symlink file with a shorter string
667                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
668                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
669         done
670
671         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
672         local devname=$(mdsdevname $mds_index)
673
674         echo "stop and checking mds${mds_index}:"
675         # e2fsck should not return error
676         stop mds${mds_index}
677         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
678         rc=$?
679
680         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
681                 error "start mds${mds_index} failed"
682         df $MOUNT > /dev/null 2>&1
683         [ $rc -eq 0 ] ||
684                 error "e2fsck detected error for short/long symlink: rc=$rc"
685         rm -f $wdir/*
686 }
687 run_test 17m "run e2fsck against MDT which contains short/long symlink"
688
689 check_fs_consistency_17n() {
690         local mdt_index
691         local rc=0
692
693         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
694         # so it only check MDT1/MDT2 instead of all of MDTs.
695         for mdt_index in 1 2; do
696                 local devname=$(mdsdevname $mdt_index)
697                 # e2fsck should not return error
698                 stop mds${mdt_index}
699                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
700                         rc=$((rc + $?))
701
702                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
703                         error "mount mds$mdt_index failed"
704                 df $MOUNT > /dev/null 2>&1
705         done
706         return $rc
707 }
708
709 test_17n() {
710         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
711         [ $PARALLEL == "yes" ] && skip "skip parallel run"
712         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
713         remote_mds_nodsh && skip "remote MDS with nodsh"
714         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
715         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
716                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
717
718         local i
719
720         test_mkdir $DIR/$tdir
721         for ((i=0; i<10; i++)); do
722                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
723                         error "create remote dir error $i"
724                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
725                         error "create files under remote dir failed $i"
726         done
727
728         check_fs_consistency_17n ||
729                 error "e2fsck report error after create files under remote dir"
730
731         for ((i = 0; i < 10; i++)); do
732                 rm -rf $DIR/$tdir/remote_dir_${i} ||
733                         error "destroy remote dir error $i"
734         done
735
736         check_fs_consistency_17n ||
737                 error "e2fsck report error after unlink files under remote dir"
738
739         [ $MDS1_VERSION -lt $(version_code 2.4.50) ] &&
740                 skip "lustre < 2.4.50 does not support migrate mv"
741
742         for ((i = 0; i < 10; i++)); do
743                 mkdir -p $DIR/$tdir/remote_dir_${i}
744                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
745                         error "create files under remote dir failed $i"
746                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
747                         error "migrate remote dir error $i"
748         done
749         check_fs_consistency_17n || error "e2fsck report error after migration"
750
751         for ((i = 0; i < 10; i++)); do
752                 rm -rf $DIR/$tdir/remote_dir_${i} ||
753                         error "destroy remote dir error $i"
754         done
755
756         check_fs_consistency_17n || error "e2fsck report error after unlink"
757 }
758 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
759
760 test_17o() {
761         remote_mds_nodsh && skip "remote MDS with nodsh"
762         [ $MDS1_VERSION -lt $(version_code 2.3.64) ] &&
763                 skip "Need MDS version at least 2.3.64"
764
765         local wdir=$DIR/${tdir}o
766         local mdt_index
767         local rc=0
768
769         test_mkdir $wdir
770         touch $wdir/$tfile
771         mdt_index=$($LFS getstripe -m $wdir/$tfile)
772         mdt_index=$((mdt_index + 1))
773
774         cancel_lru_locks mdc
775         #fail mds will wait the failover finish then set
776         #following fail_loc to avoid interfer the recovery process.
777         fail mds${mdt_index}
778
779         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
780         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
781         ls -l $wdir/$tfile && rc=1
782         do_facet mds${mdt_index} lctl set_param fail_loc=0
783         [[ $rc -eq 0 ]] || error "stat file should fail"
784 }
785 run_test 17o "stat file with incompat LMA feature"
786
787 test_18() {
788         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
789         ls $DIR || error "Failed to ls $DIR: $?"
790 }
791 run_test 18 "touch .../f ; ls ... =============================="
792
793 test_19a() {
794         touch $DIR/$tfile
795         ls -l $DIR
796         rm $DIR/$tfile
797         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
798 }
799 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
800
801 test_19b() {
802         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
803 }
804 run_test 19b "ls -l .../f19 (should return error) =============="
805
806 test_19c() {
807         [ $RUNAS_ID -eq $UID ] &&
808                 skip_env "RUNAS_ID = UID = $UID -- skipping"
809
810         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
811 }
812 run_test 19c "$RUNAS touch .../f19 (should return error) =="
813
814 test_19d() {
815         cat $DIR/f19 && error || true
816 }
817 run_test 19d "cat .../f19 (should return error) =============="
818
819 test_20() {
820         touch $DIR/$tfile
821         rm $DIR/$tfile
822         touch $DIR/$tfile
823         rm $DIR/$tfile
824         touch $DIR/$tfile
825         rm $DIR/$tfile
826         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
827 }
828 run_test 20 "touch .../f ; ls -l ..."
829
830 test_21() {
831         test_mkdir $DIR/$tdir
832         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
833         ln -s dangle $DIR/$tdir/link
834         echo foo >> $DIR/$tdir/link
835         cat $DIR/$tdir/dangle
836         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
837         $CHECKSTAT -f -t file $DIR/$tdir/link ||
838                 error "$tdir/link not linked to a file"
839 }
840 run_test 21 "write to dangling link"
841
842 test_22() {
843         local wdir=$DIR/$tdir
844         test_mkdir $wdir
845         chown $RUNAS_ID:$RUNAS_GID $wdir
846         (cd $wdir || error "cd $wdir failed";
847                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
848                 $RUNAS tar xf -)
849         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
850         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
851         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
852                 error "checkstat -u failed"
853 }
854 run_test 22 "unpack tar archive as non-root user"
855
856 # was test_23
857 test_23a() {
858         test_mkdir $DIR/$tdir
859         local file=$DIR/$tdir/$tfile
860
861         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
862         openfile -f O_CREAT:O_EXCL $file &&
863                 error "$file recreate succeeded" || true
864 }
865 run_test 23a "O_CREAT|O_EXCL in subdir"
866
867 test_23b() { # bug 18988
868         test_mkdir $DIR/$tdir
869         local file=$DIR/$tdir/$tfile
870
871         rm -f $file
872         echo foo > $file || error "write filed"
873         echo bar >> $file || error "append filed"
874         $CHECKSTAT -s 8 $file || error "wrong size"
875         rm $file
876 }
877 run_test 23b "O_APPEND check"
878
879 # LU-9409, size with O_APPEND and tiny writes
880 test_23c() {
881         local file=$DIR/$tfile
882
883         # single dd
884         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
885         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
886         rm -f $file
887
888         # racing tiny writes
889         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
890         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
891         wait
892         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
893         rm -f $file
894
895         #racing tiny & normal writes
896         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
897         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
898         wait
899         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
900         rm -f $file
901
902         #racing tiny & normal writes 2, ugly numbers
903         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
904         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
905         wait
906         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
907         rm -f $file
908 }
909 run_test 23c "O_APPEND size checks for tiny writes"
910
911 # LU-11069 file offset is correct after appending writes
912 test_23d() {
913         local file=$DIR/$tfile
914         local offset
915
916         echo CentaurHauls > $file
917         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
918         if ((offset != 26)); then
919                 error "wrong offset, expected 26, got '$offset'"
920         fi
921 }
922 run_test 23d "file offset is correct after appending writes"
923
924 # rename sanity
925 test_24a() {
926         echo '-- same directory rename'
927         test_mkdir $DIR/$tdir
928         touch $DIR/$tdir/$tfile.1
929         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
930         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
931 }
932 run_test 24a "rename file to non-existent target"
933
934 test_24b() {
935         test_mkdir $DIR/$tdir
936         touch $DIR/$tdir/$tfile.{1,2}
937         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
938         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
939         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
940 }
941 run_test 24b "rename file to existing target"
942
943 test_24c() {
944         test_mkdir $DIR/$tdir
945         test_mkdir $DIR/$tdir/d$testnum.1
946         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
947         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
948         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
949 }
950 run_test 24c "rename directory to non-existent target"
951
952 test_24d() {
953         test_mkdir -c1 $DIR/$tdir
954         test_mkdir -c1 $DIR/$tdir/d$testnum.1
955         test_mkdir -c1 $DIR/$tdir/d$testnum.2
956         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
957         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
958         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
959 }
960 run_test 24d "rename directory to existing target"
961
962 test_24e() {
963         echo '-- cross directory renames --'
964         test_mkdir $DIR/R5a
965         test_mkdir $DIR/R5b
966         touch $DIR/R5a/f
967         mv $DIR/R5a/f $DIR/R5b/g
968         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
969         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
970 }
971 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
972
973 test_24f() {
974         test_mkdir $DIR/R6a
975         test_mkdir $DIR/R6b
976         touch $DIR/R6a/f $DIR/R6b/g
977         mv $DIR/R6a/f $DIR/R6b/g
978         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
979         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
980 }
981 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
982
983 test_24g() {
984         test_mkdir $DIR/R7a
985         test_mkdir $DIR/R7b
986         test_mkdir $DIR/R7a/d
987         mv $DIR/R7a/d $DIR/R7b/e
988         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
989         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
990 }
991 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
992
993 test_24h() {
994         test_mkdir -c1 $DIR/R8a
995         test_mkdir -c1 $DIR/R8b
996         test_mkdir -c1 $DIR/R8a/d
997         test_mkdir -c1 $DIR/R8b/e
998         mrename $DIR/R8a/d $DIR/R8b/e
999         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
1000         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
1001 }
1002 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
1003
1004 test_24i() {
1005         echo "-- rename error cases"
1006         test_mkdir $DIR/R9
1007         test_mkdir $DIR/R9/a
1008         touch $DIR/R9/f
1009         mrename $DIR/R9/f $DIR/R9/a
1010         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1011         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1012         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1013 }
1014 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1015
1016 test_24j() {
1017         test_mkdir $DIR/R10
1018         mrename $DIR/R10/f $DIR/R10/g
1019         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1020         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1021         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1022 }
1023 run_test 24j "source does not exist ============================"
1024
1025 test_24k() {
1026         test_mkdir $DIR/R11a
1027         test_mkdir $DIR/R11a/d
1028         touch $DIR/R11a/f
1029         mv $DIR/R11a/f $DIR/R11a/d
1030         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1031         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1032 }
1033 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1034
1035 # bug 2429 - rename foo foo foo creates invalid file
1036 test_24l() {
1037         f="$DIR/f24l"
1038         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1039 }
1040 run_test 24l "Renaming a file to itself ========================"
1041
1042 test_24m() {
1043         f="$DIR/f24m"
1044         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1045         # on ext3 this does not remove either the source or target files
1046         # though the "expected" operation would be to remove the source
1047         $CHECKSTAT -t file ${f} || error "${f} missing"
1048         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1049 }
1050 run_test 24m "Renaming a file to a hard link to itself ========="
1051
1052 test_24n() {
1053     f="$DIR/f24n"
1054     # this stats the old file after it was renamed, so it should fail
1055     touch ${f}
1056     $CHECKSTAT ${f} || error "${f} missing"
1057     mv ${f} ${f}.rename
1058     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1059     $CHECKSTAT -a ${f} || error "${f} exists"
1060 }
1061 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1062
1063 test_24o() {
1064         test_mkdir $DIR/$tdir
1065         rename_many -s random -v -n 10 $DIR/$tdir
1066 }
1067 run_test 24o "rename of files during htree split"
1068
1069 test_24p() {
1070         test_mkdir $DIR/R12a
1071         test_mkdir $DIR/R12b
1072         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1073         mrename $DIR/R12a $DIR/R12b
1074         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1075         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1076         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1077         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1078 }
1079 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1080
1081 cleanup_multiop_pause() {
1082         trap 0
1083         kill -USR1 $MULTIPID
1084 }
1085
1086 test_24q() {
1087         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1088
1089         test_mkdir $DIR/R13a
1090         test_mkdir $DIR/R13b
1091         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1092         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1093         MULTIPID=$!
1094
1095         trap cleanup_multiop_pause EXIT
1096         mrename $DIR/R13a $DIR/R13b
1097         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1098         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1099         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1100         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1101         cleanup_multiop_pause
1102         wait $MULTIPID || error "multiop close failed"
1103 }
1104 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1105
1106 test_24r() { #bug 3789
1107         test_mkdir $DIR/R14a
1108         test_mkdir $DIR/R14a/b
1109         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1110         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1111         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1112 }
1113 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1114
1115 test_24s() {
1116         test_mkdir $DIR/R15a
1117         test_mkdir $DIR/R15a/b
1118         test_mkdir $DIR/R15a/b/c
1119         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1120         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1121         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1122 }
1123 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1124 test_24t() {
1125         test_mkdir $DIR/R16a
1126         test_mkdir $DIR/R16a/b
1127         test_mkdir $DIR/R16a/b/c
1128         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1129         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1130         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1131 }
1132 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1133
1134 test_24u() { # bug12192
1135         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1136         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1137 }
1138 run_test 24u "create stripe file"
1139
1140 simple_cleanup_common() {
1141         local rc=0
1142         trap 0
1143         [ -z "$DIR" ] || [ -z "$tdir" ] && return 0
1144
1145         local start=$SECONDS
1146         rm -rf $DIR/$tdir
1147         rc=$?
1148         wait_delete_completed
1149         echo "cleanup time $((SECONDS - start))"
1150         return $rc
1151 }
1152
1153 max_pages_per_rpc() {
1154         local mdtname="$(printf "MDT%04x" ${1:-0})"
1155         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1156 }
1157
1158 test_24v() {
1159         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1160
1161         local nrfiles=${COUNT:-100000}
1162         local fname="$DIR/$tdir/$tfile"
1163
1164         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1165         [ "$mds1_FSTYPE" = "zfs" ] && nrfiles=${COUNT:-10000}
1166
1167         test_mkdir "$(dirname $fname)"
1168         # assume MDT0000 has the fewest inodes
1169         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1170         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1171         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1172
1173         trap simple_cleanup_common EXIT
1174
1175         createmany -m "$fname" $nrfiles
1176
1177         cancel_lru_locks mdc
1178         lctl set_param mdc.*.stats clear
1179
1180         # was previously test_24D: LU-6101
1181         # readdir() returns correct number of entries after cursor reload
1182         local num_ls=$(ls $DIR/$tdir | wc -l)
1183         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1184         local num_all=$(ls -a $DIR/$tdir | wc -l)
1185         if [ $num_ls -ne $nrfiles ] || [ $num_uniq -ne $nrfiles ] ||
1186                 [ $num_all -ne $((nrfiles + 2)) ]; then
1187                         error "Expected $nrfiles files, got $num_ls " \
1188                                 "($num_uniq unique $num_all .&..)"
1189         fi
1190         # LU-5 large readdir
1191         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1192         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1193         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1194         # take into account of overhead in lu_dirpage header and end mark in
1195         # each page, plus one in rpc_num calculation.
1196         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1197         local page_entries=$(((PAGE_SIZE - 24) / dirent_size))
1198         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1199         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1200         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1201         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1202         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1203         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1204                 error "large readdir doesn't take effect: " \
1205                       "$mds_readpage should be about $rpc_max"
1206
1207         simple_cleanup_common
1208 }
1209 run_test 24v "list large directory (test hash collision, b=17560)"
1210
1211 test_24w() { # bug21506
1212         SZ1=234852
1213         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1214         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1215         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1216         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1217         [[ "$SZ1" -eq "$SZ2" ]] ||
1218                 error "Error reading at the end of the file $tfile"
1219 }
1220 run_test 24w "Reading a file larger than 4Gb"
1221
1222 test_24x() {
1223         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1224         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1225         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1226                 skip "Need MDS version at least 2.7.56"
1227
1228         local MDTIDX=1
1229         local remote_dir=$DIR/$tdir/remote_dir
1230
1231         test_mkdir $DIR/$tdir
1232         $LFS mkdir -i $MDTIDX $remote_dir ||
1233                 error "create remote directory failed"
1234
1235         test_mkdir $DIR/$tdir/src_dir
1236         touch $DIR/$tdir/src_file
1237         test_mkdir $remote_dir/tgt_dir
1238         touch $remote_dir/tgt_file
1239
1240         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1241                 error "rename dir cross MDT failed!"
1242
1243         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1244                 error "rename file cross MDT failed!"
1245
1246         touch $DIR/$tdir/ln_file
1247         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1248                 error "ln file cross MDT failed"
1249
1250         rm -rf $DIR/$tdir || error "Can not delete directories"
1251 }
1252 run_test 24x "cross MDT rename/link"
1253
1254 test_24y() {
1255         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1256         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1257
1258         local remote_dir=$DIR/$tdir/remote_dir
1259         local mdtidx=1
1260
1261         test_mkdir $DIR/$tdir
1262         $LFS mkdir -i $mdtidx $remote_dir ||
1263                 error "create remote directory failed"
1264
1265         test_mkdir $remote_dir/src_dir
1266         touch $remote_dir/src_file
1267         test_mkdir $remote_dir/tgt_dir
1268         touch $remote_dir/tgt_file
1269
1270         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1271                 error "rename subdir in the same remote dir failed!"
1272
1273         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1274                 error "rename files in the same remote dir failed!"
1275
1276         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1277                 error "link files in the same remote dir failed!"
1278
1279         rm -rf $DIR/$tdir || error "Can not delete directories"
1280 }
1281 run_test 24y "rename/link on the same dir should succeed"
1282
1283 test_24z() {
1284         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1285         [[ $MDS1_VERSION -lt $(version_code 2.12.51) ]] &&
1286                 skip "Need MDS version at least 2.12.51"
1287
1288         local index
1289
1290         for index in 0 1; do
1291                 $LFS mkdir -i $index $DIR/$tdir.$index || error "mkdir failed"
1292                 touch $DIR/$tdir.0/$tfile.$index || error "touch failed"
1293         done
1294
1295         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1 || error "mv $tfile.0 failed"
1296
1297         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.0)
1298         [ $index -eq 0 ] || error "$tfile.0 is on MDT$index"
1299
1300         local mdts=$(comma_list $(mdts_nodes))
1301
1302         do_nodes $mdts $LCTL set_param mdt.*.enable_remote_rename=0
1303         stack_trap "do_nodes $mdts $LCTL \
1304                 set_param mdt.*.enable_remote_rename=1" EXIT
1305
1306         mv $DIR/$tdir.0/$tfile.1 $DIR/$tdir.1 || error "mv $tfile.1 failed"
1307
1308         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.1)
1309         [ $index -eq 1 ] || error "$tfile.1 is on MDT$index"
1310 }
1311 run_test 24z "cross-MDT rename is done as cp"
1312
1313 test_24A() { # LU-3182
1314         local NFILES=5000
1315
1316         rm -rf $DIR/$tdir
1317         test_mkdir $DIR/$tdir
1318         trap simple_cleanup_common EXIT
1319         createmany -m $DIR/$tdir/$tfile $NFILES
1320         local t=$(ls $DIR/$tdir | wc -l)
1321         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1322         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1323         if [ $t -ne $NFILES ] || [ $u -ne $NFILES ] ||
1324            [ $v -ne $((NFILES + 2)) ] ; then
1325                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1326         fi
1327
1328         simple_cleanup_common || error "Can not delete directories"
1329 }
1330 run_test 24A "readdir() returns correct number of entries."
1331
1332 test_24B() { # LU-4805
1333         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1334
1335         local count
1336
1337         test_mkdir $DIR/$tdir
1338         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1339                 error "create striped dir failed"
1340
1341         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1342         [ $count -eq 2 ] || error "Expected 2, got $count"
1343
1344         touch $DIR/$tdir/striped_dir/a
1345
1346         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1347         [ $count -eq 3 ] || error "Expected 3, got $count"
1348
1349         touch $DIR/$tdir/striped_dir/.f
1350
1351         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1352         [ $count -eq 4 ] || error "Expected 4, got $count"
1353
1354         rm -rf $DIR/$tdir || error "Can not delete directories"
1355 }
1356 run_test 24B "readdir for striped dir return correct number of entries"
1357
1358 test_24C() {
1359         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1360
1361         mkdir $DIR/$tdir
1362         mkdir $DIR/$tdir/d0
1363         mkdir $DIR/$tdir/d1
1364
1365         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1366                 error "create striped dir failed"
1367
1368         cd $DIR/$tdir/d0/striped_dir
1369
1370         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1371         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1372         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1373
1374         [ "$d0_ino" = "$parent_ino" ] ||
1375                 error ".. wrong, expect $d0_ino, get $parent_ino"
1376
1377         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1378                 error "mv striped dir failed"
1379
1380         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1381
1382         [ "$d1_ino" = "$parent_ino" ] ||
1383                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1384 }
1385 run_test 24C "check .. in striped dir"
1386
1387 test_24E() {
1388         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1389         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1390
1391         mkdir -p $DIR/$tdir
1392         mkdir $DIR/$tdir/src_dir
1393         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1394                 error "create remote source failed"
1395
1396         touch $DIR/$tdir/src_dir/src_child/a
1397
1398         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1399                 error "create remote target dir failed"
1400
1401         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1402                 error "create remote target child failed"
1403
1404         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1405                 error "rename dir cross MDT failed!"
1406
1407         find $DIR/$tdir
1408
1409         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1410                 error "src_child still exists after rename"
1411
1412         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1413                 error "missing file(a) after rename"
1414
1415         rm -rf $DIR/$tdir || error "Can not delete directories"
1416 }
1417 run_test 24E "cross MDT rename/link"
1418
1419 test_24F () {
1420         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
1421
1422         local repeats=1000
1423         [ "$SLOW" = "no" ] && repeats=100
1424
1425         mkdir -p $DIR/$tdir
1426
1427         echo "$repeats repeats"
1428         for ((i = 0; i < repeats; i++)); do
1429                 $LFS mkdir -i0 -c2 $DIR/$tdir/test || error "mkdir fails"
1430                 touch $DIR/$tdir/test/a || error "touch fails"
1431                 mkdir $DIR/$tdir/test/b || error "mkdir fails"
1432                 rm -rf $DIR/$tdir/test || error "rmdir fails"
1433         done
1434
1435         true
1436 }
1437 run_test 24F "hash order vs readdir (LU-11330)"
1438
1439 test_25a() {
1440         echo '== symlink sanity ============================================='
1441
1442         test_mkdir $DIR/d25
1443         ln -s d25 $DIR/s25
1444         touch $DIR/s25/foo ||
1445                 error "File creation in symlinked directory failed"
1446 }
1447 run_test 25a "create file in symlinked directory ==============="
1448
1449 test_25b() {
1450         [ ! -d $DIR/d25 ] && test_25a
1451         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1452 }
1453 run_test 25b "lookup file in symlinked directory ==============="
1454
1455 test_26a() {
1456         test_mkdir $DIR/d26
1457         test_mkdir $DIR/d26/d26-2
1458         ln -s d26/d26-2 $DIR/s26
1459         touch $DIR/s26/foo || error "File creation failed"
1460 }
1461 run_test 26a "multiple component symlink ======================="
1462
1463 test_26b() {
1464         test_mkdir -p $DIR/$tdir/d26-2
1465         ln -s $tdir/d26-2/foo $DIR/s26-2
1466         touch $DIR/s26-2 || error "File creation failed"
1467 }
1468 run_test 26b "multiple component symlink at end of lookup ======"
1469
1470 test_26c() {
1471         test_mkdir $DIR/d26.2
1472         touch $DIR/d26.2/foo
1473         ln -s d26.2 $DIR/s26.2-1
1474         ln -s s26.2-1 $DIR/s26.2-2
1475         ln -s s26.2-2 $DIR/s26.2-3
1476         chmod 0666 $DIR/s26.2-3/foo
1477 }
1478 run_test 26c "chain of symlinks"
1479
1480 # recursive symlinks (bug 439)
1481 test_26d() {
1482         ln -s d26-3/foo $DIR/d26-3
1483 }
1484 run_test 26d "create multiple component recursive symlink"
1485
1486 test_26e() {
1487         [ ! -h $DIR/d26-3 ] && test_26d
1488         rm $DIR/d26-3
1489 }
1490 run_test 26e "unlink multiple component recursive symlink"
1491
1492 # recursive symlinks (bug 7022)
1493 test_26f() {
1494         test_mkdir $DIR/$tdir
1495         test_mkdir $DIR/$tdir/$tfile
1496         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1497         test_mkdir -p lndir/bar1
1498         test_mkdir $DIR/$tdir/$tfile/$tfile
1499         cd $tfile                || error "cd $tfile failed"
1500         ln -s .. dotdot          || error "ln dotdot failed"
1501         ln -s dotdot/lndir lndir || error "ln lndir failed"
1502         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1503         output=`ls $tfile/$tfile/lndir/bar1`
1504         [ "$output" = bar1 ] && error "unexpected output"
1505         rm -r $tfile             || error "rm $tfile failed"
1506         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1507 }
1508 run_test 26f "rm -r of a directory which has recursive symlink"
1509
1510 test_27a() {
1511         test_mkdir $DIR/$tdir
1512         $LFS getstripe $DIR/$tdir
1513         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1514         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1515         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1516 }
1517 run_test 27a "one stripe file"
1518
1519 test_27b() {
1520         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1521
1522         test_mkdir $DIR/$tdir
1523         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1524         $LFS getstripe -c $DIR/$tdir/$tfile
1525         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1526                 error "two-stripe file doesn't have two stripes"
1527
1528         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1529 }
1530 run_test 27b "create and write to two stripe file"
1531
1532 # 27c family tests specific striping, setstripe -o
1533 test_27ca() {
1534         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1535         test_mkdir -p $DIR/$tdir
1536         local osts="1"
1537
1538         $LFS setstripe -o $osts $DIR/$tdir/$tfile  || error "setstripe failed"
1539         $LFS getstripe -i $DIR/$tdir/$tfile
1540         [ $($LFS getstripe -i $DIR/$tdir/$tfile ) -eq $osts ] ||
1541                 error "stripe not on specified OST"
1542
1543         dd if=/dev/zero of=$DIR/$tdir/$tfile  bs=1M count=4 || error "dd failed"
1544 }
1545 run_test 27ca "one stripe on specified OST"
1546
1547 test_27cb() {
1548         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1549         test_mkdir -p $DIR/$tdir
1550         local osts="1,0"
1551         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1552         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1553         echo "$getstripe"
1554
1555         # Strip getstripe output to a space separated list of OSTs
1556         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1557                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1558         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1559                 error "stripes not on specified OSTs"
1560
1561         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1562 }
1563 run_test 27cb "two stripes on specified OSTs"
1564
1565 test_27cc() {
1566         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1567         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1568                 skip "server does not support overstriping"
1569
1570         test_mkdir -p $DIR/$tdir
1571         local osts="0,0"
1572         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1573         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1574         echo "$getstripe"
1575
1576         # Strip getstripe output to a space separated list of OSTs
1577         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1578                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1579         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1580                 error "stripes not on specified OSTs"
1581
1582         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1583 }
1584 run_test 27cc "two stripes on the same OST"
1585
1586 test_27cd() {
1587         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1588         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1589                 skip "server does not support overstriping"
1590         test_mkdir -p $DIR/$tdir
1591         local osts="0,1,1,0"
1592         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1593         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1594         echo "$getstripe"
1595
1596         # Strip getstripe output to a space separated list of OSTs
1597         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1598                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1599         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1600                 error "stripes not on specified OSTs"
1601
1602         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1603 }
1604 run_test 27cd "four stripes on two OSTs"
1605
1606 test_27ce() {
1607         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
1608                 skip_env "too many osts, skipping"
1609         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1610                 skip "server does not support overstriping"
1611         # We do one more stripe than we have OSTs
1612         [ $OSTCOUNT -ge 159 ] || large_xattr_enabled ||
1613                 skip_env "ea_inode feature disabled"
1614
1615         test_mkdir -p $DIR/$tdir
1616         local osts=""
1617         for i in $(seq 0 $OSTCOUNT);
1618         do
1619                 osts=$osts"0"
1620                 if [ $i -ne $OSTCOUNT ]; then
1621                         osts=$osts","
1622                 fi
1623         done
1624         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1625         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1626         echo "$getstripe"
1627
1628         # Strip getstripe output to a space separated list of OSTs
1629         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1630                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1631         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1632                 error "stripes not on specified OSTs"
1633
1634         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1635 }
1636 run_test 27ce "more stripes than OSTs with -o"
1637
1638 test_27cf() {
1639         local osp_proc="osp.$FSNAME-OST0000-osc-MDT000*.active"
1640         local pid=0
1641
1642         test_mkdir -p $DIR/$tdir || error "failed to mkdir $DIR/$tdir"
1643         do_facet $SINGLEMDS "$LCTL set_param -n $osp_proc=0"
1644         stack_trap "do_facet $SINGLEMDS $LCTL set_param -n $osp_proc=1" EXIT
1645         wait_update_facet $SINGLEMDS "$LCTL get_param -n $osp_proc | grep 1" ||
1646                 error "failed to set $osp_proc=0"
1647
1648         $LFS setstripe -o 0 $DIR/$tdir/$tfile &
1649         pid=$!
1650         sleep 1
1651         do_facet $SINGLEMDS "$LCTL set_param -n $osp_proc=1"
1652         wait_update_facet $SINGLEMDS "$LCTL get_param -n $osp_proc | grep 0" ||
1653                 error "failed to set $osp_proc=1"
1654         wait $pid
1655         [[ $pid -ne 0 ]] ||
1656                 error "should return error due to $osp_proc=0"
1657 }
1658 run_test 27cf "'setstripe -o' on inactive OSTs should return error"
1659
1660 test_27d() {
1661         test_mkdir $DIR/$tdir
1662         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1663                 error "setstripe failed"
1664         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1665         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1666 }
1667 run_test 27d "create file with default settings"
1668
1669 test_27e() {
1670         # LU-5839 adds check for existed layout before setting it
1671         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1672                 skip "Need MDS version at least 2.7.56"
1673
1674         test_mkdir $DIR/$tdir
1675         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1676         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1677         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1678 }
1679 run_test 27e "setstripe existing file (should return error)"
1680
1681 test_27f() {
1682         test_mkdir $DIR/$tdir
1683         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1684                 error "$LFS setstripe $DIR/$tdir/$tfile failed"
1685         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1686                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1687         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1688         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1689 }
1690 run_test 27f "setstripe with bad stripe size (should return error)"
1691
1692 test_27g() {
1693         test_mkdir $DIR/$tdir
1694         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1695         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1696                 error "$DIR/$tdir/$tfile has object"
1697 }
1698 run_test 27g "$LFS getstripe with no objects"
1699
1700 test_27ga() {
1701         test_mkdir $DIR/$tdir
1702         touch $DIR/$tdir/$tfile || error "touch failed"
1703         ln -s bogus $DIR/$tdir/$tfile.2 || error "ln failed"
1704         $LFS getstripe -m $DIR/$tdir/$tfile $DIR/$tdir/$tfile.2
1705         local rc=$?
1706         (( rc == 2 )) || error "getstripe did not return ENOENT"
1707 }
1708 run_test 27ga "$LFS getstripe with missing file (should return error)"
1709
1710 test_27i() {
1711         test_mkdir $DIR/$tdir
1712         touch $DIR/$tdir/$tfile || error "touch failed"
1713         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1714                 error "missing objects"
1715 }
1716 run_test 27i "$LFS getstripe with some objects"
1717
1718 test_27j() {
1719         test_mkdir $DIR/$tdir
1720         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1721                 error "setstripe failed" || true
1722 }
1723 run_test 27j "setstripe with bad stripe offset (should return error)"
1724
1725 test_27k() { # bug 2844
1726         test_mkdir $DIR/$tdir
1727         local file=$DIR/$tdir/$tfile
1728         local ll_max_blksize=$((4 * 1024 * 1024))
1729         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1730         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1731         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1732         dd if=/dev/zero of=$file bs=4k count=1
1733         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1734         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1735 }
1736 run_test 27k "limit i_blksize for broken user apps"
1737
1738 test_27l() {
1739         mcreate $DIR/$tfile || error "creating file"
1740         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1741                 error "setstripe should have failed" || true
1742 }
1743 run_test 27l "check setstripe permissions (should return error)"
1744
1745 test_27m() {
1746         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1747
1748         [ -n "$RCLIENTS" -o -n "$MOUNT_2" ] &&
1749                 skip_env "multiple clients -- skipping"
1750
1751         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1752                    head -n1)
1753         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1754                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1755         fi
1756         trap simple_cleanup_common EXIT
1757         test_mkdir $DIR/$tdir
1758         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1759         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1760                 error "dd should fill OST0"
1761         i=2
1762         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1763                 i=$((i + 1))
1764                 [ $i -gt 256 ] && break
1765         done
1766         i=$((i + 1))
1767         touch $DIR/$tdir/$tfile.$i
1768         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1769             awk '{print $1}'| grep -w "0") ] &&
1770                 error "OST0 was full but new created file still use it"
1771         i=$((i + 1))
1772         touch $DIR/$tdir/$tfile.$i
1773         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1774             awk '{print $1}'| grep -w "0") ] &&
1775                 error "OST0 was full but new created file still use it"
1776         simple_cleanup_common
1777 }
1778 run_test 27m "create file while OST0 was full"
1779
1780 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1781 # if the OST isn't full anymore.
1782 reset_enospc() {
1783         local OSTIDX=${1:-""}
1784
1785         local list=$(comma_list $(osts_nodes))
1786         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1787
1788         do_nodes $list lctl set_param fail_loc=0
1789         sync    # initiate all OST_DESTROYs from MDS to OST
1790         sleep_maxage
1791 }
1792
1793 exhaust_precreations() {
1794         local OSTIDX=$1
1795         local FAILLOC=$2
1796         local FAILIDX=${3:-$OSTIDX}
1797         local ofacet=ost$((OSTIDX + 1))
1798
1799         test_mkdir -p -c1 $DIR/$tdir
1800         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1801         local mfacet=mds$((mdtidx + 1))
1802         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1803
1804         local OST=$(ostname_from_index $OSTIDX)
1805
1806         # on the mdt's osc
1807         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1808         local last_id=$(do_facet $mfacet lctl get_param -n \
1809                         osp.$mdtosc_proc1.prealloc_last_id)
1810         local next_id=$(do_facet $mfacet lctl get_param -n \
1811                         osp.$mdtosc_proc1.prealloc_next_id)
1812
1813         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1814         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1815
1816         test_mkdir -p $DIR/$tdir/${OST}
1817         $LFS setstripe -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1818 #define OBD_FAIL_OST_ENOSPC              0x215
1819         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1820         echo "Creating to objid $last_id on ost $OST..."
1821         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1822         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1823         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1824         sleep_maxage
1825 }
1826
1827 exhaust_all_precreations() {
1828         local i
1829         for (( i=0; i < OSTCOUNT; i++ )) ; do
1830                 exhaust_precreations $i $1 -1
1831         done
1832 }
1833
1834 test_27n() {
1835         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1836         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1837         remote_mds_nodsh && skip "remote MDS with nodsh"
1838         remote_ost_nodsh && skip "remote OST with nodsh"
1839
1840         reset_enospc
1841         rm -f $DIR/$tdir/$tfile
1842         exhaust_precreations 0 0x80000215
1843         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1844         touch $DIR/$tdir/$tfile || error "touch failed"
1845         $LFS getstripe $DIR/$tdir/$tfile
1846         reset_enospc
1847 }
1848 run_test 27n "create file with some full OSTs"
1849
1850 test_27o() {
1851         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1852         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1853         remote_mds_nodsh && skip "remote MDS with nodsh"
1854         remote_ost_nodsh && skip "remote OST with nodsh"
1855
1856         reset_enospc
1857         rm -f $DIR/$tdir/$tfile
1858         exhaust_all_precreations 0x215
1859
1860         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1861
1862         reset_enospc
1863         rm -rf $DIR/$tdir/*
1864 }
1865 run_test 27o "create file with all full OSTs (should error)"
1866
1867 test_27p() {
1868         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1869         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1870         remote_mds_nodsh && skip "remote MDS with nodsh"
1871         remote_ost_nodsh && skip "remote OST with nodsh"
1872
1873         reset_enospc
1874         rm -f $DIR/$tdir/$tfile
1875         test_mkdir $DIR/$tdir
1876
1877         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1878         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1879         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1880
1881         exhaust_precreations 0 0x80000215
1882         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1883         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1884         $LFS getstripe $DIR/$tdir/$tfile
1885
1886         reset_enospc
1887 }
1888 run_test 27p "append to a truncated file with some full OSTs"
1889
1890 test_27q() {
1891         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1892         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1893         remote_mds_nodsh && skip "remote MDS with nodsh"
1894         remote_ost_nodsh && skip "remote OST with nodsh"
1895
1896         reset_enospc
1897         rm -f $DIR/$tdir/$tfile
1898
1899         test_mkdir $DIR/$tdir
1900         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1901         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1902                 error "truncate $DIR/$tdir/$tfile failed"
1903         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1904
1905         exhaust_all_precreations 0x215
1906
1907         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1908         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1909
1910         reset_enospc
1911 }
1912 run_test 27q "append to truncated file with all OSTs full (should error)"
1913
1914 test_27r() {
1915         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1916         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1917         remote_mds_nodsh && skip "remote MDS with nodsh"
1918         remote_ost_nodsh && skip "remote OST with nodsh"
1919
1920         reset_enospc
1921         rm -f $DIR/$tdir/$tfile
1922         exhaust_precreations 0 0x80000215
1923
1924         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1925
1926         reset_enospc
1927 }
1928 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1929
1930 test_27s() { # bug 10725
1931         test_mkdir $DIR/$tdir
1932         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1933         local stripe_count=0
1934         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1935         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1936                 error "stripe width >= 2^32 succeeded" || true
1937
1938 }
1939 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1940
1941 test_27t() { # bug 10864
1942         WDIR=$(pwd)
1943         WLFS=$(which lfs)
1944         cd $DIR
1945         touch $tfile
1946         $WLFS getstripe $tfile
1947         cd $WDIR
1948 }
1949 run_test 27t "check that utils parse path correctly"
1950
1951 test_27u() { # bug 4900
1952         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1953         remote_mds_nodsh && skip "remote MDS with nodsh"
1954
1955         local index
1956         local list=$(comma_list $(mdts_nodes))
1957
1958 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1959         do_nodes $list $LCTL set_param fail_loc=0x139
1960         test_mkdir -p $DIR/$tdir
1961         trap simple_cleanup_common EXIT
1962         createmany -o $DIR/$tdir/t- 1000
1963         do_nodes $list $LCTL set_param fail_loc=0
1964
1965         TLOG=$TMP/$tfile.getstripe
1966         $LFS getstripe $DIR/$tdir > $TLOG
1967         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1968         unlinkmany $DIR/$tdir/t- 1000
1969         trap 0
1970         [[ $OBJS -gt 0 ]] &&
1971                 error "$OBJS objects created on OST-0. See $TLOG" ||
1972                 rm -f $TLOG
1973 }
1974 run_test 27u "skip object creation on OSC w/o objects"
1975
1976 test_27v() { # bug 4900
1977         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1978         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1979         remote_mds_nodsh && skip "remote MDS with nodsh"
1980         remote_ost_nodsh && skip "remote OST with nodsh"
1981
1982         exhaust_all_precreations 0x215
1983         reset_enospc
1984
1985         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1986
1987         touch $DIR/$tdir/$tfile
1988         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1989         # all except ost1
1990         for (( i=1; i < OSTCOUNT; i++ )); do
1991                 do_facet ost$i lctl set_param fail_loc=0x705
1992         done
1993         local START=`date +%s`
1994         createmany -o $DIR/$tdir/$tfile 32
1995
1996         local FINISH=`date +%s`
1997         local TIMEOUT=`lctl get_param -n timeout`
1998         local PROCESS=$((FINISH - START))
1999         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
2000                error "$FINISH - $START >= $TIMEOUT / 2"
2001         sleep $((TIMEOUT / 2 - PROCESS))
2002         reset_enospc
2003 }
2004 run_test 27v "skip object creation on slow OST"
2005
2006 test_27w() { # bug 10997
2007         test_mkdir $DIR/$tdir
2008         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
2009         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
2010                 error "stripe size $size != 65536" || true
2011         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
2012                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
2013 }
2014 run_test 27w "check $LFS setstripe -S and getstrip -d options"
2015
2016 test_27wa() {
2017         [[ $OSTCOUNT -lt 2 ]] &&
2018                 skip_env "skipping multiple stripe count/offset test"
2019
2020         test_mkdir $DIR/$tdir
2021         for i in $(seq 1 $OSTCOUNT); do
2022                 offset=$((i - 1))
2023                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
2024                         error "setstripe -c $i -i $offset failed"
2025                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
2026                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
2027                 [ $count -ne $i ] && error "stripe count $count != $i" || true
2028                 [ $index -ne $offset ] &&
2029                         error "stripe offset $index != $offset" || true
2030         done
2031 }
2032 run_test 27wa "check $LFS setstripe -c -i options"
2033
2034 test_27x() {
2035         remote_ost_nodsh && skip "remote OST with nodsh"
2036         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2037         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2038
2039         OFFSET=$(($OSTCOUNT - 1))
2040         OSTIDX=0
2041         local OST=$(ostname_from_index $OSTIDX)
2042
2043         test_mkdir $DIR/$tdir
2044         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
2045         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
2046         sleep_maxage
2047         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
2048         for i in $(seq 0 $OFFSET); do
2049                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
2050                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
2051                 error "OST0 was degraded but new created file still use it"
2052         done
2053         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
2054 }
2055 run_test 27x "create files while OST0 is degraded"
2056
2057 test_27y() {
2058         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2059         remote_mds_nodsh && skip "remote MDS with nodsh"
2060         remote_ost_nodsh && skip "remote OST with nodsh"
2061         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2062
2063         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
2064         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
2065                 osp.$mdtosc.prealloc_last_id)
2066         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
2067                 osp.$mdtosc.prealloc_next_id)
2068         local fcount=$((last_id - next_id))
2069         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
2070         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
2071
2072         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
2073                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
2074         local OST_DEACTIVE_IDX=-1
2075         local OSC
2076         local OSTIDX
2077         local OST
2078
2079         for OSC in $MDS_OSCS; do
2080                 OST=$(osc_to_ost $OSC)
2081                 OSTIDX=$(index_from_ostuuid $OST)
2082                 if [ $OST_DEACTIVE_IDX == -1 ]; then
2083                         OST_DEACTIVE_IDX=$OSTIDX
2084                 fi
2085                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
2086                         echo $OSC "is Deactivated:"
2087                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
2088                 fi
2089         done
2090
2091         OSTIDX=$(index_from_ostuuid $OST)
2092         test_mkdir $DIR/$tdir
2093         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
2094
2095         for OSC in $MDS_OSCS; do
2096                 OST=$(osc_to_ost $OSC)
2097                 OSTIDX=$(index_from_ostuuid $OST)
2098                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2099                         echo $OST "is degraded:"
2100                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2101                                                 obdfilter.$OST.degraded=1
2102                 fi
2103         done
2104
2105         sleep_maxage
2106         createmany -o $DIR/$tdir/$tfile $fcount
2107
2108         for OSC in $MDS_OSCS; do
2109                 OST=$(osc_to_ost $OSC)
2110                 OSTIDX=$(index_from_ostuuid $OST)
2111                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2112                         echo $OST "is recovered from degraded:"
2113                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2114                                                 obdfilter.$OST.degraded=0
2115                 else
2116                         do_facet $SINGLEMDS lctl --device %$OSC activate
2117                 fi
2118         done
2119
2120         # all osp devices get activated, hence -1 stripe count restored
2121         local stripe_count=0
2122
2123         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
2124         # devices get activated.
2125         sleep_maxage
2126         $LFS setstripe -c -1 $DIR/$tfile
2127         stripe_count=$($LFS getstripe -c $DIR/$tfile)
2128         rm -f $DIR/$tfile
2129         [ $stripe_count -ne $OSTCOUNT ] &&
2130                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
2131         return 0
2132 }
2133 run_test 27y "create files while OST0 is degraded and the rest inactive"
2134
2135 check_seq_oid()
2136 {
2137         log "check file $1"
2138
2139         lmm_count=$($LFS getstripe -c $1)
2140         lmm_seq=$($LFS getstripe -v $1 | awk '/lmm_seq/ { print $2 }')
2141         lmm_oid=$($LFS getstripe -v $1 | awk '/lmm_object_id/ { print $2 }')
2142
2143         local old_ifs="$IFS"
2144         IFS=$'[:]'
2145         fid=($($LFS path2fid $1))
2146         IFS="$old_ifs"
2147
2148         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
2149         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
2150
2151         # compare lmm_seq and lu_fid->f_seq
2152         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
2153         # compare lmm_object_id and lu_fid->oid
2154         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
2155
2156         # check the trusted.fid attribute of the OST objects of the file
2157         local have_obdidx=false
2158         local stripe_nr=0
2159         $LFS getstripe $1 | while read obdidx oid hex seq; do
2160                 # skip lines up to and including "obdidx"
2161                 [ -z "$obdidx" ] && break
2162                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
2163                 $have_obdidx || continue
2164
2165                 local ost=$((obdidx + 1))
2166                 local dev=$(ostdevname $ost)
2167                 local oid_hex
2168
2169                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
2170
2171                 seq=$(echo $seq | sed -e "s/^0x//g")
2172                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
2173                         oid_hex=$(echo $oid)
2174                 else
2175                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
2176                 fi
2177                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
2178
2179                 local ff=""
2180                 #
2181                 # Don't unmount/remount the OSTs if we don't need to do that.
2182                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2183                 # update too, until that use mount/ll_decode_filter_fid/mount.
2184                 # Re-enable when debugfs will understand new filter_fid.
2185                 #
2186                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2187                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2188                                 $dev 2>/dev/null" | grep "parent=")
2189                 fi
2190                 if [ -z "$ff" ]; then
2191                         stop ost$ost
2192                         mount_fstype ost$ost
2193                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2194                                 $(facet_mntpt ost$ost)/$obj_file)
2195                         unmount_fstype ost$ost
2196                         start ost$ost $dev $OST_MOUNT_OPTS
2197                         clients_up
2198                 fi
2199
2200                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2201
2202                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2203
2204                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2205                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2206                 #
2207                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2208                 #       stripe_size=1048576 component_id=1 component_start=0 \
2209                 #       component_end=33554432
2210                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2211                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2212                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2213                 local ff_pstripe
2214                 if grep -q 'stripe=' <<<$ff; then
2215                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2216                 else
2217                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2218                         # into f_ver in this case.  See comment on ff_parent.
2219                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2220                 fi
2221
2222                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2223                 [ $ff_pseq = $lmm_seq ] ||
2224                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2225                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2226                 [ $ff_poid = $lmm_oid ] ||
2227                         error "FF parent OID $ff_poid != $lmm_oid"
2228                 (($ff_pstripe == $stripe_nr)) ||
2229                         error "FF stripe $ff_pstripe != $stripe_nr"
2230
2231                 stripe_nr=$((stripe_nr + 1))
2232                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2233                         continue
2234                 if grep -q 'stripe_count=' <<<$ff; then
2235                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2236                                             -e 's/ .*//' <<<$ff)
2237                         [ $lmm_count = $ff_scnt ] ||
2238                                 error "FF stripe count $lmm_count != $ff_scnt"
2239                 fi
2240         done
2241 }
2242
2243 test_27z() {
2244         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2245         remote_ost_nodsh && skip "remote OST with nodsh"
2246
2247         test_mkdir $DIR/$tdir
2248         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2249                 { error "setstripe -c -1 failed"; return 1; }
2250         # We need to send a write to every object to get parent FID info set.
2251         # This _should_ also work for setattr, but does not currently.
2252         # touch $DIR/$tdir/$tfile-1 ||
2253         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2254                 { error "dd $tfile-1 failed"; return 2; }
2255         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2256                 { error "setstripe -c -1 failed"; return 3; }
2257         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2258                 { error "dd $tfile-2 failed"; return 4; }
2259
2260         # make sure write RPCs have been sent to OSTs
2261         sync; sleep 5; sync
2262
2263         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2264         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2265 }
2266 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2267
2268 test_27A() { # b=19102
2269         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2270
2271         save_layout_restore_at_exit $MOUNT
2272         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2273         wait_update $HOSTNAME "$LFS getstripe -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2274                 error "stripe count $($LFS getstripe -c $MOUNT) != 1"
2275         local default_size=$($LFS getstripe -S $MOUNT)
2276         local default_offset=$($LFS getstripe -i $MOUNT)
2277         local dsize=$(do_facet $SINGLEMDS \
2278                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2279         [ $default_size -eq $dsize ] ||
2280                 error "stripe size $default_size != $dsize"
2281         [ $default_offset -eq -1 ] ||
2282                 error "stripe offset $default_offset != -1"
2283 }
2284 run_test 27A "check filesystem-wide default LOV EA values"
2285
2286 test_27B() { # LU-2523
2287         test_mkdir $DIR/$tdir
2288         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2289         touch $DIR/$tdir/f0
2290         # open f1 with O_LOV_DELAY_CREATE
2291         # rename f0 onto f1
2292         # call setstripe ioctl on open file descriptor for f1
2293         # close
2294         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2295                 $DIR/$tdir/f0
2296
2297         rm -f $DIR/$tdir/f1
2298         # open f1 with O_LOV_DELAY_CREATE
2299         # unlink f1
2300         # call setstripe ioctl on open file descriptor for f1
2301         # close
2302         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2303
2304         # Allow multiop to fail in imitation of NFS's busted semantics.
2305         true
2306 }
2307 run_test 27B "call setstripe on open unlinked file/rename victim"
2308
2309 # 27C family tests full striping and overstriping
2310 test_27Ca() { #LU-2871
2311         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2312
2313         declare -a ost_idx
2314         local index
2315         local found
2316         local i
2317         local j
2318
2319         test_mkdir $DIR/$tdir
2320         cd $DIR/$tdir
2321         for i in $(seq 0 $((OSTCOUNT - 1))); do
2322                 # set stripe across all OSTs starting from OST$i
2323                 $LFS setstripe -i $i -c -1 $tfile$i
2324                 # get striping information
2325                 ost_idx=($($LFS getstripe $tfile$i |
2326                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2327                 echo ${ost_idx[@]}
2328
2329                 # check the layout
2330                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2331                         error "${#ost_idx[@]} != $OSTCOUNT"
2332
2333                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2334                         found=0
2335                         for j in $(echo ${ost_idx[@]}); do
2336                                 if [ $index -eq $j ]; then
2337                                         found=1
2338                                         break
2339                                 fi
2340                         done
2341                         [ $found = 1 ] ||
2342                                 error "Can not find $index in ${ost_idx[@]}"
2343                 done
2344         done
2345 }
2346 run_test 27Ca "check full striping across all OSTs"
2347
2348 test_27Cb() {
2349         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2350                 skip "server does not support overstriping"
2351         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2352                 skip_env "too many osts, skipping"
2353
2354         test_mkdir -p $DIR/$tdir
2355         local setcount=$(($OSTCOUNT * 2))
2356         [ $setcount -ge 160 ] || large_xattr_enabled ||
2357                 skip_env "ea_inode feature disabled"
2358
2359         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2360                 error "setstripe failed"
2361
2362         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2363         [ $count -eq $setcount ] ||
2364                 error "stripe count $count, should be $setcount"
2365
2366         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2367                 error "overstriped should be set in pattern"
2368
2369         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2370                 error "dd failed"
2371 }
2372 run_test 27Cb "more stripes than OSTs with -C"
2373
2374 test_27Cc() {
2375         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2376                 skip "server does not support overstriping"
2377         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2378
2379         test_mkdir -p $DIR/$tdir
2380         local setcount=$(($OSTCOUNT - 1))
2381
2382         [ $setcount -ge 160 ] || large_xattr_enabled ||
2383                 skip_env "ea_inode feature disabled"
2384
2385         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2386                 error "setstripe failed"
2387
2388         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2389         [ $count -eq $setcount ] ||
2390                 error "stripe count $count, should be $setcount"
2391
2392         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" &&
2393                 error "overstriped should not be set in pattern"
2394
2395         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2396                 error "dd failed"
2397 }
2398 run_test 27Cc "fewer stripes than OSTs does not set overstriping"
2399
2400 test_27Cd() {
2401         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2402                 skip "server does not support overstriping"
2403         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2404         large_xattr_enabled || skip_env "ea_inode feature disabled"
2405
2406         test_mkdir -p $DIR/$tdir
2407         local setcount=$LOV_MAX_STRIPE_COUNT
2408
2409         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2410                 error "setstripe failed"
2411
2412         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2413         [ $count -eq $setcount ] ||
2414                 error "stripe count $count, should be $setcount"
2415
2416         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2417                 error "overstriped should be set in pattern"
2418
2419         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2420                 error "dd failed"
2421
2422         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2423 }
2424 run_test 27Cd "test maximum stripe count"
2425
2426 test_27Ce() {
2427         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2428                 skip "server does not support overstriping"
2429         test_mkdir -p $DIR/$tdir
2430
2431         pool_add $TESTNAME || error "Pool creation failed"
2432         pool_add_targets $TESTNAME 0 || error "pool_add_targets failed"
2433
2434         local setcount=8
2435
2436         $LFS setstripe  -C $setcount -p "$TESTNAME" $DIR/$tdir/$tfile ||
2437                 error "setstripe failed"
2438
2439         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2440         [ $count -eq $setcount ] ||
2441                 error "stripe count $count, should be $setcount"
2442
2443         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2444                 error "overstriped should be set in pattern"
2445
2446         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2447                 error "dd failed"
2448
2449         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2450 }
2451 run_test 27Ce "test pool with overstriping"
2452
2453 test_27Cf() {
2454         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2455                 skip "server does not support overstriping"
2456         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2457                 skip_env "too many osts, skipping"
2458
2459         test_mkdir -p $DIR/$tdir
2460
2461         local setcount=$(($OSTCOUNT * 2))
2462         [ $setcount -ge 160 ] || large_xattr_enabled ||
2463                 skip_env "ea_inode feature disabled"
2464
2465         $LFS setstripe  -C $setcount $DIR/$tdir/ ||
2466                 error "setstripe failed"
2467
2468         echo 1 > $DIR/$tdir/$tfile
2469
2470         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2471         [ $count -eq $setcount ] ||
2472                 error "stripe count $count, should be $setcount"
2473
2474         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2475                 error "overstriped should be set in pattern"
2476
2477         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2478                 error "dd failed"
2479
2480         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2481 }
2482 run_test 27Cf "test default inheritance with overstriping"
2483
2484 test_27D() {
2485         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2486         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2487         remote_mds_nodsh && skip "remote MDS with nodsh"
2488
2489         local POOL=${POOL:-testpool}
2490         local first_ost=0
2491         local last_ost=$(($OSTCOUNT - 1))
2492         local ost_step=1
2493         local ost_list=$(seq $first_ost $ost_step $last_ost)
2494         local ost_range="$first_ost $last_ost $ost_step"
2495
2496         test_mkdir $DIR/$tdir
2497         pool_add $POOL || error "pool_add failed"
2498         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2499
2500         local skip27D
2501         [ $MDS1_VERSION -lt $(version_code 2.8.55) ] &&
2502                 skip27D+="-s 29"
2503         [ $MDS1_VERSION -lt $(version_code 2.9.55) ] ||
2504                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2505                         skip27D+=" -s 30,31"
2506         [[ ! $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ||
2507           $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2508                 skip27D+=" -s 32,33"
2509         [[ $MDS_VERSION -lt $(version_code $SEL_VER) ]] &&
2510                 skip27D+=" -s 34"
2511         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2512                 error "llapi_layout_test failed"
2513
2514         destroy_test_pools || error "destroy test pools failed"
2515 }
2516 run_test 27D "validate llapi_layout API"
2517
2518 # Verify that default_easize is increased from its initial value after
2519 # accessing a widely striped file.
2520 test_27E() {
2521         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2522         [ $CLIENT_VERSION -lt $(version_code 2.5.57) ] &&
2523                 skip "client does not have LU-3338 fix"
2524
2525         # 72 bytes is the minimum space required to store striping
2526         # information for a file striped across one OST:
2527         # (sizeof(struct lov_user_md_v3) +
2528         #  sizeof(struct lov_user_ost_data_v1))
2529         local min_easize=72
2530         $LCTL set_param -n llite.*.default_easize $min_easize ||
2531                 error "lctl set_param failed"
2532         local easize=$($LCTL get_param -n llite.*.default_easize)
2533
2534         [ $easize -eq $min_easize ] ||
2535                 error "failed to set default_easize"
2536
2537         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2538                 error "setstripe failed"
2539         # In order to ensure stat() call actually talks to MDS we need to
2540         # do something drastic to this file to shake off all lock, e.g.
2541         # rename it (kills lookup lock forcing cache cleaning)
2542         mv $DIR/$tfile $DIR/${tfile}-1
2543         ls -l $DIR/${tfile}-1
2544         rm $DIR/${tfile}-1
2545
2546         easize=$($LCTL get_param -n llite.*.default_easize)
2547
2548         [ $easize -gt $min_easize ] ||
2549                 error "default_easize not updated"
2550 }
2551 run_test 27E "check that default extended attribute size properly increases"
2552
2553 test_27F() { # LU-5346/LU-7975
2554         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2555         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2556         [[ $MDS1_VERSION -lt $(version_code 2.8.51) ]] &&
2557                 skip "Need MDS version at least 2.8.51"
2558         remote_ost_nodsh && skip "remote OST with nodsh"
2559
2560         test_mkdir $DIR/$tdir
2561         rm -f $DIR/$tdir/f0
2562         $LFS setstripe -c 2 $DIR/$tdir
2563
2564         # stop all OSTs to reproduce situation for LU-7975 ticket
2565         for num in $(seq $OSTCOUNT); do
2566                 stop ost$num
2567         done
2568
2569         # open/create f0 with O_LOV_DELAY_CREATE
2570         # truncate f0 to a non-0 size
2571         # close
2572         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2573
2574         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2575         # open/write it again to force delayed layout creation
2576         cat /etc/hosts > $DIR/$tdir/f0 &
2577         catpid=$!
2578
2579         # restart OSTs
2580         for num in $(seq $OSTCOUNT); do
2581                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2582                         error "ost$num failed to start"
2583         done
2584
2585         wait $catpid || error "cat failed"
2586
2587         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2588         [[ $($LFS getstripe -c $DIR/$tdir/f0) == 2 ]] ||
2589                 error "wrong stripecount"
2590
2591 }
2592 run_test 27F "Client resend delayed layout creation with non-zero size"
2593
2594 test_27G() { #LU-10629
2595         [ $MDS1_VERSION -lt $(version_code 2.11.51) ] &&
2596                 skip "Need MDS version at least 2.11.51"
2597         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2598         remote_mds_nodsh && skip "remote MDS with nodsh"
2599         local POOL=${POOL:-testpool}
2600         local ostrange="0 0 1"
2601
2602         test_mkdir $DIR/$tdir
2603         pool_add $POOL || error "pool_add failed"
2604         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2605         $LFS setstripe -p $POOL $DIR/$tdir
2606
2607         local pool=$($LFS getstripe -p $DIR/$tdir)
2608
2609         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2610
2611         $LFS setstripe -d $DIR/$tdir
2612
2613         pool=$($LFS getstripe -p $DIR/$tdir)
2614
2615         rmdir $DIR/$tdir
2616
2617         [ -z "$pool" ] || error "'$pool' is not empty"
2618 }
2619 run_test 27G "Clear OST pool from stripe"
2620
2621 test_27H() {
2622         [[ $MDS1_VERSION -le $(version_code 2.11.54) ]] &&
2623                 skip "Need MDS version newer than 2.11.54"
2624         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2625         test_mkdir $DIR/$tdir
2626         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2627         touch $DIR/$tdir/$tfile
2628         $LFS getstripe -c $DIR/$tdir/$tfile
2629         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2630                 error "two-stripe file doesn't have two stripes"
2631
2632         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2633         $LFS getstripe -y $DIR/$tdir/$tfile
2634         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2635              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2636                 error "expected l_ost_idx: [02]$ not matched"
2637
2638         # make sure ost list has been cleared
2639         local stripesize=$($LFS getstripe -S $DIR/$tdir)
2640         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2641                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2642         touch $DIR/$tdir/f3
2643         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2644 }
2645 run_test 27H "Set specific OSTs stripe"
2646
2647 test_27I() {
2648         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2649         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2650         [[ $MDS1_VERSION -gt $(version_code 2.12.52) ]] ||
2651                 skip "Need MDS version newer than 2.12.52"
2652         local pool=$TESTNAME
2653         local ostrange="1 1 1"
2654
2655         save_layout_restore_at_exit $MOUNT
2656         $LFS setstripe -c 2 -i 0 $MOUNT
2657         pool_add $pool || error "pool_add failed"
2658         pool_add_targets $pool $ostrange || "pool_add_targets failed"
2659         test_mkdir $DIR/$tdir
2660         $LFS setstripe -p $pool $DIR/$tdir
2661         $MULTIOP $DIR/$tdir/$tfile Oc || error "multiop failed"
2662         $LFS getstripe $DIR/$tdir/$tfile
2663 }
2664 run_test 27I "check that root dir striping does not break parent dir one"
2665
2666 test_27J() {
2667         [[ $MDS1_VERSION -le $(version_code 2.12.51) ]] &&
2668                 skip "Need MDS version newer than 2.12.51"
2669
2670         test_mkdir $DIR/$tdir
2671         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2672         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2673
2674         # create foreign file (raw way)
2675         create_foreign_file -f $DIR/$tdir/$tfile -x "${uuid1}@${uuid2}" \
2676                 -t 1 -F 0xda08 || error "create_foreign_file failed"
2677
2678         # verify foreign file (raw way)
2679         parse_foreign_file -f $DIR/$tdir/$tfile |
2680                 grep "lov_foreign_magic: 0x0BD70BD0" ||
2681                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign magic"
2682         parse_foreign_file -f $DIR/$tdir/$tfile | grep "lov_xattr_size: 89" ||
2683                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2684         parse_foreign_file -f $DIR/$tdir/$tfile |
2685                 grep "lov_foreign_size: 73" ||
2686                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2687         parse_foreign_file -f $DIR/$tdir/$tfile |
2688                 grep "lov_foreign_type: 1" ||
2689                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign type"
2690         parse_foreign_file -f $DIR/$tdir/$tfile |
2691                 grep "lov_foreign_flags: 0x0000DA08" ||
2692                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign flags"
2693         local lov=$(parse_foreign_file -f $DIR/$tdir/$tfile |
2694                 grep "lov_foreign_value: 0x" |
2695                 sed -e 's/lov_foreign_value: 0x//')
2696         local lov2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160)
2697         [[ $lov = ${lov2// /} ]] ||
2698                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign value"
2699
2700         # create foreign file (lfs + API)
2701         $LFS setstripe --foreign=daos --flags 0xda08 \
2702                 -x "${uuid1}@${uuid2}" $DIR/$tdir/${tfile}2 ||
2703                 error "$DIR/$tdir/${tfile}2: create failed"
2704
2705         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2706                 grep "lfm_magic:.*0x0BD70BD0" ||
2707                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign magic"
2708         # lfm_length is LOV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2709         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_length:.*73" ||
2710                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign size"
2711         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_type:.*daos" ||
2712                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign type"
2713         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2714                 grep "lfm_flags:.*0x0000DA08" ||
2715                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign flags"
2716         $LFS getstripe $DIR/$tdir/${tfile}2 |
2717                 grep "lfm_value:.*${uuid1}@${uuid2}" ||
2718                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign value"
2719
2720         # modify striping should fail
2721         $LFS setstripe -c 2 $DIR/$tdir/$tfile &&
2722                 error "$DIR/$tdir/$tfile: setstripe should fail"
2723         $LFS setstripe -c 2 $DIR/$tdir/${tfile}2 &&
2724                 error "$DIR/$tdir/${tfile}2: setstripe should fail"
2725
2726         # R/W should fail
2727         cat $DIR/$tdir/$tfile && error "$DIR/$tdir/$tfile: read should fail"
2728         cat $DIR/$tdir/${tfile}2 &&
2729                 error "$DIR/$tdir/${tfile}2: read should fail"
2730         cat /etc/passwd > $DIR/$tdir/$tfile &&
2731                 error "$DIR/$tdir/$tfile: write should fail"
2732         cat /etc/passwd > $DIR/$tdir/${tfile}2 &&
2733                 error "$DIR/$tdir/${tfile}2: write should fail"
2734
2735         # chmod should work
2736         chmod 222 $DIR/$tdir/$tfile ||
2737                 error "$DIR/$tdir/$tfile: chmod failed"
2738         chmod 222 $DIR/$tdir/${tfile}2 ||
2739                 error "$DIR/$tdir/${tfile}2: chmod failed"
2740
2741         # chown should work
2742         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tfile ||
2743                 error "$DIR/$tdir/$tfile: chown failed"
2744         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tfile}2 ||
2745                 error "$DIR/$tdir/${tfile}2: chown failed"
2746
2747         # rename should work
2748         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}.new ||
2749                 error "$DIR/$tdir/$tfile: rename of foreign file has failed"
2750         mv $DIR/$tdir/${tfile}2 $DIR/$tdir/${tfile}2.new ||
2751                 error "$DIR/$tdir/${tfile}2: rename of foreign file has failed"
2752
2753         #remove foreign file
2754         rm $DIR/$tdir/${tfile}.new ||
2755                 error "$DIR/$tdir/${tfile}.new: remove of foreign file has failed"
2756         rm $DIR/$tdir/${tfile}2.new ||
2757                 error "$DIR/$tdir/${tfile}2.new: remove of foreign file has failed"
2758 }
2759 run_test 27J "basic ops on file with foreign LOV"
2760
2761 test_27K() {
2762         [[ $MDS1_VERSION -le $(version_code 2.12.49) ]] &&
2763                 skip "Need MDS version newer than 2.12.49"
2764
2765         test_mkdir $DIR/$tdir
2766         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2767         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2768
2769         # create foreign dir (raw way)
2770         create_foreign_dir -d $DIR/$tdir/$tdir -x "${uuid1}@${uuid2}" -t 1 ||
2771                 error "create_foreign_dir FAILED"
2772
2773         # verify foreign dir (raw way)
2774         parse_foreign_dir -d $DIR/$tdir/$tdir |
2775                 grep "lmv_foreign_magic:.*0xcd50cd0" ||
2776                 error "$DIR/$tdir/$tfile: invalid LMV EA magic"
2777         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_xattr_size:.*89$" ||
2778                 error "$DIR/$tdir/$tdir: invalid LMV EA size"
2779         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_type: 1$" ||
2780                 error "$DIR/$tdir/$tdir: invalid LMV EA type"
2781         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_flags: 0$" ||
2782                 error "$DIR/$tdir/$tdir: invalid LMV EA flags"
2783         local lmv=$(parse_foreign_dir -d $DIR/$tdir/$tdir |
2784                 grep "lmv_foreign_value: 0x" |
2785                 sed 's/lmv_foreign_value: 0x//')
2786         local lmv2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160 |
2787                 sed 's/ //g')
2788         [[ $lmv == $lmv2 ]] || error "$DIR/$tdir/$tdir: invalid LMV EA value"
2789
2790         # create foreign dir (lfs + API)
2791         $LFS mkdir --foreign=daos --xattr="${uuid1}@${uuid2}" --flags=0xda05 \
2792                 $DIR/$tdir/${tdir}2 ||
2793                 error "$DIR/$tdir/${tdir}2: create failed"
2794
2795         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2796                 grep "lfm_magic:.*0x0CD50CD0" ||
2797                 error "$DIR/$tdir/${tdir}2: invalid LMV EA magic"
2798         # lfm_length is LMV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2799         # - sizeof(lfm_type) - sizeof(lfm_flags)
2800         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_length:.*73" ||
2801                 error "$DIR/$tdir/${tdir}2: invalid LMV EA size"
2802         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_type:.*daos" ||
2803                 error "$DIR/$tdir/${tdir}2: invalid LMV EA type"
2804         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2805                 grep "lfm_flags:.*0x0000DA05" ||
2806                 error "$DIR/$tdir/${tdir}2: invalid LMV EA flags"
2807         $LFS getdirstripe $DIR/$tdir/${tdir}2 |
2808                 grep "lfm_value.*${uuid1}@${uuid2}" ||
2809                 error "$DIR/$tdir/${tdir}2: invalid LMV EA value"
2810
2811         # file create in dir should fail
2812         touch $DIR/$tdir/$tdir/$tfile && "$DIR/$tdir: file create should fail"
2813         touch $DIR/$tdir/${tdir}2/$tfile &&
2814                 "$DIR/${tdir}2: file create should fail"
2815
2816         # chmod should work
2817         chmod 777 $DIR/$tdir/$tdir ||
2818                 error "$DIR/$tdir: chmod failed"
2819         chmod 777 $DIR/$tdir/${tdir}2 ||
2820                 error "$DIR/${tdir}2: chmod failed"
2821
2822         # chown should work
2823         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tdir ||
2824                 error "$DIR/$tdir: chown failed"
2825         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tdir}2 ||
2826                 error "$DIR/${tdir}2: chown failed"
2827
2828         # rename should work
2829         mv $DIR/$tdir/$tdir $DIR/$tdir/${tdir}.new ||
2830                 error "$DIR/$tdir/$tdir: rename of foreign dir has failed"
2831         mv $DIR/$tdir/${tdir}2 $DIR/$tdir/${tdir}2.new ||
2832                 error "$DIR/$tdir/${tdir}2: rename of foreign dir has failed"
2833
2834         #remove foreign dir
2835         rmdir $DIR/$tdir/${tdir}.new ||
2836                 error "$DIR/$tdir/${tdir}.new: remove of foreign dir has failed"
2837         rmdir $DIR/$tdir/${tdir}2.new ||
2838                 error "$DIR/$tdir/${tdir}2.new: remove of foreign dir has failed"
2839 }
2840 run_test 27K "basic ops on dir with foreign LMV"
2841
2842 test_27L() {
2843         remote_mds_nodsh && skip "remote MDS with nodsh"
2844
2845         local POOL=${POOL:-$TESTNAME}
2846
2847         pool_add $POOL || error "pool_add failed"
2848
2849         lfs pool_list $MOUNT | grep -Fx "${FSNAME}.${POOL}" ||
2850                  error "pool_list does not contain ${FSNAME}.${POOL}:" \
2851                        "$(lfs pool_list $MOUNT | grep -F "${POOL}")"
2852 }
2853 run_test 27L "lfs pool_list gives correct pool name"
2854
2855 test_27M() {
2856         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.12.57) ]] &&
2857                 skip "Need MDS version >= than 2.12.57"
2858         remote_mds_nodsh && skip "remote MDS with nodsh"
2859         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2860
2861         test_mkdir $DIR/$tdir
2862
2863         # Set default striping on directory
2864         $LFS setstripe -C 4 $DIR/$tdir
2865
2866         echo 1 > $DIR/$tdir/${tfile}.1
2867         local count=$($LFS getstripe -c $DIR/$tdir/${tfile}.1)
2868         local setcount=4
2869         [ $count -eq $setcount ] ||
2870                 error "(1) stripe count $count, should be $setcount"
2871
2872         # Capture existing append_stripe_count setting for restore
2873         local orig_count=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_stripe_count)
2874         local mdts=$(comma_list $(mdts_nodes))
2875         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=$orig_count" EXIT
2876
2877         local appendcount=$orig_count
2878         echo 1 >> $DIR/$tdir/${tfile}.2_append
2879         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.2_append)
2880         [ $count -eq $appendcount ] ||
2881                 error "(2)stripe count $count, should be $appendcount for append"
2882
2883         # Disable O_APPEND striping, verify it works
2884         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2885
2886         # Should now get the default striping, which is 4
2887         setcount=4
2888         echo 1 >> $DIR/$tdir/${tfile}.3_append
2889         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.3_append)
2890         [ $count -eq $setcount ] ||
2891                 error "(3) stripe count $count, should be $setcount"
2892
2893         # Try changing the stripe count for append files
2894         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=2
2895
2896         # Append striping is now 2 (directory default is still 4)
2897         appendcount=2
2898         echo 1 >> $DIR/$tdir/${tfile}.4_append
2899         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.4_append)
2900         [ $count -eq $appendcount ] ||
2901                 error "(4) stripe count $count, should be $appendcount for append"
2902
2903         # Test append stripe count of -1
2904         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=-1
2905         appendcount=$OSTCOUNT
2906         echo 1 >> $DIR/$tdir/${tfile}.5
2907         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.5)
2908         [ $count -eq $appendcount ] ||
2909                 error "(5) stripe count $count, should be $appendcount for append"
2910
2911         # Set append striping back to default of 1
2912         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=1
2913
2914         # Try a new default striping, PFL + DOM
2915         $LFS setstripe -L mdt -E 1M -E -1 -c 2 $DIR/$tdir
2916
2917         # Create normal DOM file, DOM returns stripe count == 0
2918         setcount=0
2919         touch $DIR/$tdir/${tfile}.6
2920         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.6)
2921         [ $count -eq $setcount ] ||
2922                 error "(6) stripe count $count, should be $setcount"
2923
2924         # Show
2925         appendcount=1
2926         echo 1 >> $DIR/$tdir/${tfile}.7_append
2927         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.7_append)
2928         [ $count -eq $appendcount ] ||
2929                 error "(7) stripe count $count, should be $appendcount for append"
2930
2931         # Clean up DOM layout
2932         $LFS setstripe -d $DIR/$tdir
2933
2934         # Now test that append striping works when layout is from root
2935         $LFS setstripe -c 2 $MOUNT
2936         # Make a special directory for this
2937         mkdir $DIR/${tdir}/${tdir}.2
2938         stack_trap "$LFS setstripe -d $MOUNT" EXIT
2939
2940         # Verify for normal file
2941         setcount=2
2942         echo 1 > $DIR/${tdir}/${tdir}.2/${tfile}.8
2943         count=$($LFS getstripe -c $DIR/$tdir/${tdir}.2/${tfile}.8)
2944         [ $count -eq $setcount ] ||
2945                 error "(8) stripe count $count, should be $setcount"
2946
2947         appendcount=1
2948         echo 1 >> $DIR/${tdir}/${tdir}.2/${tfile}.9_append
2949         count=$($LFS getstripe -c $DIR/${tdir}/${tdir}.2/${tfile}.9_append)
2950         [ $count -eq $appendcount ] ||
2951                 error "(9) stripe count $count, should be $appendcount for append"
2952
2953         # Now test O_APPEND striping with pools
2954         do_nodes $mdts $LCTL set_param mdd.*.append_pool="$TESTNAME"
2955         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'" EXIT
2956
2957         # Create the pool
2958         pool_add $TESTNAME || error "pool creation failed"
2959         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
2960
2961         echo 1 >> $DIR/$tdir/${tfile}.10_append
2962
2963         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.10_append)
2964         [ "$pool" = "$TESTNAME" ] || error "(10) incorrect pool: $pool"
2965
2966         # Check that count is still correct
2967         appendcount=1
2968         echo 1 >> $DIR/$tdir/${tfile}.11_append
2969         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.11_append)
2970         [ $count -eq $appendcount ] ||
2971                 error "(11) stripe count $count, should be $appendcount for append"
2972
2973         # Disable O_APPEND stripe count, verify pool works separately
2974         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2975
2976         echo 1 >> $DIR/$tdir/${tfile}.12_append
2977
2978         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.12_append)
2979         [ "$pool" = "$TESTNAME" ] || error "(12) incorrect pool: $pool"
2980
2981         # Remove pool setting, verify it's not applied
2982         do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'
2983
2984         echo 1 >> $DIR/$tdir/${tfile}.13_append
2985
2986         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.13_append)
2987         [ "$pool" = "" ] || error "(13) pool found: $pool"
2988 }
2989 run_test 27M "test O_APPEND striping"
2990
2991 test_27N() {
2992         combined_mgs_mds && skip "needs separate MGS/MDT"
2993
2994         pool_add $TESTNAME || error "pool_add failed"
2995         do_facet mgs "$LCTL pool_list $FSNAME" |
2996                 grep -Fx "${FSNAME}.${TESTNAME}" ||
2997                 error "lctl pool_list on MGS failed"
2998 }
2999 run_test 27N "lctl pool_list on separate MGS gives correct pool name"
3000
3001 # createtest also checks that device nodes are created and
3002 # then visible correctly (#2091)
3003 test_28() { # bug 2091
3004         test_mkdir $DIR/d28
3005         $CREATETEST $DIR/d28/ct || error "createtest failed"
3006 }
3007 run_test 28 "create/mknod/mkdir with bad file types ============"
3008
3009 test_29() {
3010         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3011
3012         sync; sleep 1; sync # flush out any dirty pages from previous tests
3013         cancel_lru_locks
3014         test_mkdir $DIR/d29
3015         touch $DIR/d29/foo
3016         log 'first d29'
3017         ls -l $DIR/d29
3018
3019         declare -i LOCKCOUNTORIG=0
3020         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3021                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
3022         done
3023         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
3024
3025         declare -i LOCKUNUSEDCOUNTORIG=0
3026         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3027                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
3028         done
3029
3030         log 'second d29'
3031         ls -l $DIR/d29
3032         log 'done'
3033
3034         declare -i LOCKCOUNTCURRENT=0
3035         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3036                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
3037         done
3038
3039         declare -i LOCKUNUSEDCOUNTCURRENT=0
3040         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3041                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
3042         done
3043
3044         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
3045                 $LCTL set_param -n ldlm.dump_namespaces ""
3046                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
3047                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3048                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3049                 return 2
3050         fi
3051         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
3052                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
3053                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3054                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3055                 return 3
3056         fi
3057 }
3058 run_test 29 "IT_GETATTR regression  ============================"
3059
3060 test_30a() { # was test_30
3061         cp $(which ls) $DIR || cp /bin/ls $DIR
3062         $DIR/ls / || error "Can't execute binary from lustre"
3063         rm $DIR/ls
3064 }
3065 run_test 30a "execute binary from Lustre (execve) =============="
3066
3067 test_30b() {
3068         cp `which ls` $DIR || cp /bin/ls $DIR
3069         chmod go+rx $DIR/ls
3070         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
3071         rm $DIR/ls
3072 }
3073 run_test 30b "execute binary from Lustre as non-root ==========="
3074
3075 test_30c() { # b=22376
3076         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3077
3078         cp $(which ls) $DIR || cp /bin/ls $DIR
3079         chmod a-rw $DIR/ls
3080         cancel_lru_locks mdc
3081         cancel_lru_locks osc
3082         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
3083         rm -f $DIR/ls
3084 }
3085 run_test 30c "execute binary from Lustre without read perms ===="
3086
3087 test_30d() {
3088         cp $(which dd) $DIR || error "failed to copy dd to $DIR/dd"
3089
3090         for i in {1..10}; do
3091                 $DIR/dd bs=1M count=128 if=/dev/zero of=$DIR/$tfile &
3092                 local PID=$!
3093                 sleep 1
3094                 $LCTL set_param ldlm.namespaces.*MDT*.lru_size=clear
3095                 wait $PID || error "executing dd from Lustre failed"
3096                 rm -f $DIR/$tfile
3097         done
3098
3099         rm -f $DIR/dd
3100 }
3101 run_test 30d "execute binary from Lustre while clear locks"
3102
3103 test_31a() {
3104         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
3105         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
3106 }
3107 run_test 31a "open-unlink file =================================="
3108
3109 test_31b() {
3110         touch $DIR/f31 || error "touch $DIR/f31 failed"
3111         ln $DIR/f31 $DIR/f31b || error "ln failed"
3112         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
3113         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
3114 }
3115 run_test 31b "unlink file with multiple links while open ======="
3116
3117 test_31c() {
3118         touch $DIR/f31 || error "touch $DIR/f31 failed"
3119         ln $DIR/f31 $DIR/f31c || error "ln failed"
3120         multiop_bg_pause $DIR/f31 O_uc ||
3121                 error "multiop_bg_pause for $DIR/f31 failed"
3122         MULTIPID=$!
3123         $MULTIOP $DIR/f31c Ouc
3124         kill -USR1 $MULTIPID
3125         wait $MULTIPID
3126 }
3127 run_test 31c "open-unlink file with multiple links ============="
3128
3129 test_31d() {
3130         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
3131         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
3132 }
3133 run_test 31d "remove of open directory ========================="
3134
3135 test_31e() { # bug 2904
3136         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
3137 }
3138 run_test 31e "remove of open non-empty directory ==============="
3139
3140 test_31f() { # bug 4554
3141         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3142
3143         set -vx
3144         test_mkdir $DIR/d31f
3145         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3146         cp /etc/hosts $DIR/d31f
3147         ls -l $DIR/d31f
3148         $LFS getstripe $DIR/d31f/hosts
3149         multiop_bg_pause $DIR/d31f D_c || return 1
3150         MULTIPID=$!
3151
3152         rm -rv $DIR/d31f || error "first of $DIR/d31f"
3153         test_mkdir $DIR/d31f
3154         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3155         cp /etc/hosts $DIR/d31f
3156         ls -l $DIR/d31f
3157         $LFS getstripe $DIR/d31f/hosts
3158         multiop_bg_pause $DIR/d31f D_c || return 1
3159         MULTIPID2=$!
3160
3161         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
3162         wait $MULTIPID || error "first opendir $MULTIPID failed"
3163
3164         sleep 6
3165
3166         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
3167         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
3168         set +vx
3169 }
3170 run_test 31f "remove of open directory with open-unlink file ==="
3171
3172 test_31g() {
3173         echo "-- cross directory link --"
3174         test_mkdir -c1 $DIR/${tdir}ga
3175         test_mkdir -c1 $DIR/${tdir}gb
3176         touch $DIR/${tdir}ga/f
3177         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
3178         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
3179         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
3180         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
3181         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
3182 }
3183 run_test 31g "cross directory link==============="
3184
3185 test_31h() {
3186         echo "-- cross directory link --"
3187         test_mkdir -c1 $DIR/${tdir}
3188         test_mkdir -c1 $DIR/${tdir}/dir
3189         touch $DIR/${tdir}/f
3190         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
3191         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
3192         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
3193         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
3194         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
3195 }
3196 run_test 31h "cross directory link under child==============="
3197
3198 test_31i() {
3199         echo "-- cross directory link --"
3200         test_mkdir -c1 $DIR/$tdir
3201         test_mkdir -c1 $DIR/$tdir/dir
3202         touch $DIR/$tdir/dir/f
3203         ln $DIR/$tdir/dir/f $DIR/$tdir/g
3204         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
3205         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
3206         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
3207         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
3208 }
3209 run_test 31i "cross directory link under parent==============="
3210
3211 test_31j() {
3212         test_mkdir -c1 -p $DIR/$tdir
3213         test_mkdir -c1 -p $DIR/$tdir/dir1
3214         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
3215         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
3216         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
3217         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
3218         return 0
3219 }
3220 run_test 31j "link for directory==============="
3221
3222 test_31k() {
3223         test_mkdir -c1 -p $DIR/$tdir
3224         touch $DIR/$tdir/s
3225         touch $DIR/$tdir/exist
3226         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
3227         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
3228         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
3229         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
3230         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
3231         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
3232         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
3233         return 0
3234 }
3235 run_test 31k "link to file: the same, non-existing, dir==============="
3236
3237 test_31m() {
3238         mkdir $DIR/d31m
3239         touch $DIR/d31m/s
3240         mkdir $DIR/d31m2
3241         touch $DIR/d31m2/exist
3242         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
3243         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
3244         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
3245         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
3246         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
3247         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
3248         return 0
3249 }
3250 run_test 31m "link to file: the same, non-existing, dir==============="
3251
3252 test_31n() {
3253         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
3254         nlink=$(stat --format=%h $DIR/$tfile)
3255         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3256         local fd=$(free_fd)
3257         local cmd="exec $fd<$DIR/$tfile"
3258         eval $cmd
3259         cmd="exec $fd<&-"
3260         trap "eval $cmd" EXIT
3261         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3262         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3263         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
3264         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3265         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
3266         eval $cmd
3267 }
3268 run_test 31n "check link count of unlinked file"
3269
3270 link_one() {
3271         local tempfile=$(mktemp $1_XXXXXX)
3272         mlink $tempfile $1 2> /dev/null &&
3273                 echo "$BASHPID: link $tempfile to $1 succeeded"
3274         munlink $tempfile
3275 }
3276
3277 test_31o() { # LU-2901
3278         test_mkdir $DIR/$tdir
3279         for LOOP in $(seq 100); do
3280                 rm -f $DIR/$tdir/$tfile*
3281                 for THREAD in $(seq 8); do
3282                         link_one $DIR/$tdir/$tfile.$LOOP &
3283                 done
3284                 wait
3285                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
3286                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
3287                         error "$LINKS duplicate links to $tfile.$LOOP" &&
3288                         break || true
3289         done
3290 }
3291 run_test 31o "duplicate hard links with same filename"
3292
3293 test_31p() {
3294         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3295
3296         test_mkdir $DIR/$tdir
3297         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3298         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
3299
3300         opendirunlink $DIR/$tdir/striped_dir/test1 ||
3301                 error "open unlink test1 failed"
3302         opendirunlink $DIR/$tdir/striped_dir/test2 ||
3303                 error "open unlink test2 failed"
3304
3305         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
3306                 error "test1 still exists"
3307         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
3308                 error "test2 still exists"
3309 }
3310 run_test 31p "remove of open striped directory"
3311
3312 cleanup_test32_mount() {
3313         local rc=0
3314         trap 0
3315         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
3316         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
3317         losetup -d $loopdev || true
3318         rm -rf $DIR/$tdir
3319         return $rc
3320 }
3321
3322 test_32a() {
3323         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3324
3325         echo "== more mountpoints and symlinks ================="
3326         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3327         trap cleanup_test32_mount EXIT
3328         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3329         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3330                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3331         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
3332                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
3333         cleanup_test32_mount
3334 }
3335 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
3336
3337 test_32b() {
3338         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3339
3340         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3341         trap cleanup_test32_mount EXIT
3342         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3343         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3344                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3345         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
3346                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
3347         cleanup_test32_mount
3348 }
3349 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
3350
3351 test_32c() {
3352         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3353
3354         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3355         trap cleanup_test32_mount EXIT
3356         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3357         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3358                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3359         test_mkdir -p $DIR/$tdir/d2/test_dir
3360         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3361                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
3362         cleanup_test32_mount
3363 }
3364 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
3365
3366 test_32d() {
3367         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3368
3369         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3370         trap cleanup_test32_mount EXIT
3371         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3372         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3373                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3374         test_mkdir -p $DIR/$tdir/d2/test_dir
3375         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3376                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
3377         cleanup_test32_mount
3378 }
3379 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
3380
3381 test_32e() {
3382         rm -fr $DIR/$tdir
3383         test_mkdir -p $DIR/$tdir/tmp
3384         local tmp_dir=$DIR/$tdir/tmp
3385         ln -s $DIR/$tdir $tmp_dir/symlink11
3386         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3387         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
3388         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
3389 }
3390 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
3391
3392 test_32f() {
3393         rm -fr $DIR/$tdir
3394         test_mkdir -p $DIR/$tdir/tmp
3395         local tmp_dir=$DIR/$tdir/tmp
3396         ln -s $DIR/$tdir $tmp_dir/symlink11
3397         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3398         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
3399         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
3400 }
3401 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
3402
3403 test_32g() {
3404         local tmp_dir=$DIR/$tdir/tmp
3405         test_mkdir -p $tmp_dir
3406         test_mkdir $DIR/${tdir}2
3407         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3408         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3409         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
3410         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
3411         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
3412         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
3413 }
3414 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3415
3416 test_32h() {
3417         rm -fr $DIR/$tdir $DIR/${tdir}2
3418         tmp_dir=$DIR/$tdir/tmp
3419         test_mkdir -p $tmp_dir
3420         test_mkdir $DIR/${tdir}2
3421         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3422         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3423         ls $tmp_dir/symlink12 || error "listing symlink12"
3424         ls $DIR/$tdir/symlink02  || error "listing symlink02"
3425 }
3426 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3427
3428 test_32i() {
3429         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3430
3431         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3432         trap cleanup_test32_mount EXIT
3433         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3434         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3435                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3436         touch $DIR/$tdir/test_file
3437         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
3438                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
3439         cleanup_test32_mount
3440 }
3441 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
3442
3443 test_32j() {
3444         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3445
3446         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3447         trap cleanup_test32_mount EXIT
3448         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3449         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3450                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3451         touch $DIR/$tdir/test_file
3452         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
3453                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
3454         cleanup_test32_mount
3455 }
3456 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
3457
3458 test_32k() {
3459         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3460
3461         rm -fr $DIR/$tdir
3462         trap cleanup_test32_mount EXIT
3463         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3464         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3465                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3466         test_mkdir -p $DIR/$tdir/d2
3467         touch $DIR/$tdir/d2/test_file || error "touch failed"
3468         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3469                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
3470         cleanup_test32_mount
3471 }
3472 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
3473
3474 test_32l() {
3475         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3476
3477         rm -fr $DIR/$tdir
3478         trap cleanup_test32_mount EXIT
3479         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3480         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3481                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3482         test_mkdir -p $DIR/$tdir/d2
3483         touch $DIR/$tdir/d2/test_file || error "touch failed"
3484         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3485                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
3486         cleanup_test32_mount
3487 }
3488 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
3489
3490 test_32m() {
3491         rm -fr $DIR/d32m
3492         test_mkdir -p $DIR/d32m/tmp
3493         TMP_DIR=$DIR/d32m/tmp
3494         ln -s $DIR $TMP_DIR/symlink11
3495         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3496         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
3497                 error "symlink11 not a link"
3498         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
3499                 error "symlink01 not a link"
3500 }
3501 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
3502
3503 test_32n() {
3504         rm -fr $DIR/d32n
3505         test_mkdir -p $DIR/d32n/tmp
3506         TMP_DIR=$DIR/d32n/tmp
3507         ln -s $DIR $TMP_DIR/symlink11
3508         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3509         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
3510         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
3511 }
3512 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
3513
3514 test_32o() {
3515         touch $DIR/$tfile
3516         test_mkdir -p $DIR/d32o/tmp
3517         TMP_DIR=$DIR/d32o/tmp
3518         ln -s $DIR/$tfile $TMP_DIR/symlink12
3519         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3520         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
3521                 error "symlink12 not a link"
3522         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
3523         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
3524                 error "$DIR/d32o/tmp/symlink12 not file type"
3525         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
3526                 error "$DIR/d32o/symlink02 not file type"
3527 }
3528 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
3529
3530 test_32p() {
3531         log 32p_1
3532         rm -fr $DIR/d32p
3533         log 32p_2
3534         rm -f $DIR/$tfile
3535         log 32p_3
3536         touch $DIR/$tfile
3537         log 32p_4
3538         test_mkdir -p $DIR/d32p/tmp
3539         log 32p_5
3540         TMP_DIR=$DIR/d32p/tmp
3541         log 32p_6
3542         ln -s $DIR/$tfile $TMP_DIR/symlink12
3543         log 32p_7
3544         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3545         log 32p_8
3546         cat $DIR/d32p/tmp/symlink12 ||
3547                 error "Can't open $DIR/d32p/tmp/symlink12"
3548         log 32p_9
3549         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
3550         log 32p_10
3551 }
3552 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
3553
3554 test_32q() {
3555         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3556
3557         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3558         trap cleanup_test32_mount EXIT
3559         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3560         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3561         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3562                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3563         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
3564         cleanup_test32_mount
3565 }
3566 run_test 32q "stat follows mountpoints in Lustre (should return error)"
3567
3568 test_32r() {
3569         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3570
3571         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3572         trap cleanup_test32_mount EXIT
3573         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3574         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3575         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3576                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3577         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
3578         cleanup_test32_mount
3579 }
3580 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
3581
3582 test_33aa() {
3583         rm -f $DIR/$tfile
3584         touch $DIR/$tfile
3585         chmod 444 $DIR/$tfile
3586         chown $RUNAS_ID $DIR/$tfile
3587         log 33_1
3588         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3589         log 33_2
3590 }
3591 run_test 33aa "write file with mode 444 (should return error)"
3592
3593 test_33a() {
3594         rm -fr $DIR/$tdir
3595         test_mkdir $DIR/$tdir
3596         chown $RUNAS_ID $DIR/$tdir
3597         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
3598                 error "$RUNAS create $tdir/$tfile failed"
3599         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
3600                 error "open RDWR" || true
3601 }
3602 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
3603
3604 test_33b() {
3605         rm -fr $DIR/$tdir
3606         test_mkdir $DIR/$tdir
3607         chown $RUNAS_ID $DIR/$tdir
3608         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
3609 }
3610 run_test 33b "test open file with malformed flags (No panic)"
3611
3612 test_33c() {
3613         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3614         remote_ost_nodsh && skip "remote OST with nodsh"
3615
3616         local ostnum
3617         local ostname
3618         local write_bytes
3619         local all_zeros
3620
3621         all_zeros=:
3622         rm -fr $DIR/$tdir
3623         test_mkdir $DIR/$tdir
3624         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
3625
3626         sync
3627         for ostnum in $(seq $OSTCOUNT); do
3628                 # test-framework's OST numbering is one-based, while Lustre's
3629                 # is zero-based
3630                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3631                 # Parsing llobdstat's output sucks; we could grep the /proc
3632                 # path, but that's likely to not be as portable as using the
3633                 # llobdstat utility.  So we parse lctl output instead.
3634                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3635                         obdfilter/$ostname/stats |
3636                         awk '/^write_bytes/ {print $7}' )
3637                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
3638                 if (( ${write_bytes:-0} > 0 ))
3639                 then
3640                         all_zeros=false
3641                         break;
3642                 fi
3643         done
3644
3645         $all_zeros || return 0
3646
3647         # Write four bytes
3648         echo foo > $DIR/$tdir/bar
3649         # Really write them
3650         sync
3651
3652         # Total up write_bytes after writing.  We'd better find non-zeros.
3653         for ostnum in $(seq $OSTCOUNT); do
3654                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3655                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3656                         obdfilter/$ostname/stats |
3657                         awk '/^write_bytes/ {print $7}' )
3658                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
3659                 if (( ${write_bytes:-0} > 0 ))
3660                 then
3661                         all_zeros=false
3662                         break;
3663                 fi
3664         done
3665
3666         if $all_zeros
3667         then
3668                 for ostnum in $(seq $OSTCOUNT); do
3669                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3670                         echo "Check that write_bytes is present in obdfilter/*/stats:"
3671                         do_facet ost$ostnum lctl get_param -n \
3672                                 obdfilter/$ostname/stats
3673                 done
3674                 error "OST not keeping write_bytes stats (b22312)"
3675         fi
3676 }
3677 run_test 33c "test llobdstat and write_bytes"
3678
3679 test_33d() {
3680         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3681         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3682
3683         local MDTIDX=1
3684         local remote_dir=$DIR/$tdir/remote_dir
3685
3686         test_mkdir $DIR/$tdir
3687         $LFS mkdir -i $MDTIDX $remote_dir ||
3688                 error "create remote directory failed"
3689
3690         touch $remote_dir/$tfile
3691         chmod 444 $remote_dir/$tfile
3692         chown $RUNAS_ID $remote_dir/$tfile
3693
3694         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3695
3696         chown $RUNAS_ID $remote_dir
3697         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
3698                                         error "create" || true
3699         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
3700                                     error "open RDWR" || true
3701         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
3702 }
3703 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
3704
3705 test_33e() {
3706         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3707
3708         mkdir $DIR/$tdir
3709
3710         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3711         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3712         mkdir $DIR/$tdir/local_dir
3713
3714         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3715         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3716         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3717
3718         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3719                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
3720
3721         rmdir $DIR/$tdir/* || error "rmdir failed"
3722
3723         umask 777
3724         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3725         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3726         mkdir $DIR/$tdir/local_dir
3727
3728         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3729         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3730         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3731
3732         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3733                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3734
3735         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3736
3737         umask 000
3738         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3739         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3740         mkdir $DIR/$tdir/local_dir
3741
3742         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3743         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3744         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3745
3746         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3747                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3748 }
3749 run_test 33e "mkdir and striped directory should have same mode"
3750
3751 cleanup_33f() {
3752         trap 0
3753         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3754 }
3755
3756 test_33f() {
3757         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3758         remote_mds_nodsh && skip "remote MDS with nodsh"
3759
3760         mkdir $DIR/$tdir
3761         chmod go+rwx $DIR/$tdir
3762         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3763         trap cleanup_33f EXIT
3764
3765         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3766                 error "cannot create striped directory"
3767
3768         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3769                 error "cannot create files in striped directory"
3770
3771         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3772                 error "cannot remove files in striped directory"
3773
3774         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3775                 error "cannot remove striped directory"
3776
3777         cleanup_33f
3778 }
3779 run_test 33f "nonroot user can create, access, and remove a striped directory"
3780
3781 test_33g() {
3782         mkdir -p $DIR/$tdir/dir2
3783
3784         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3785         echo $err
3786         [[ $err =~ "exists" ]] || error "Not exists error"
3787 }
3788 run_test 33g "nonroot user create already existing root created file"
3789
3790 test_33h() {
3791         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3792         [ $MDS1_VERSION -lt $(version_code 2.13.50) ] &&
3793                 skip "Need MDS version at least 2.13.50"
3794
3795         test_mkdir -c $MDSCOUNT -H crush $DIR/$tdir ||
3796                 error "mkdir $tdir failed"
3797         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
3798
3799         local index=$($LFS getstripe -m $DIR/$tdir/$tfile)
3800         local index2
3801
3802         for fname in $DIR/$tdir/$tfile.bak \
3803                      $DIR/$tdir/$tfile.SAV \
3804                      $DIR/$tdir/$tfile.orig \
3805                      $DIR/$tdir/$tfile~; do
3806                 touch $fname  || error "touch $fname failed"
3807                 index2=$($LFS getstripe -m $fname)
3808                 [ $index -eq $index2 ] ||
3809                         error "$fname MDT index mismatch $index != $index2"
3810         done
3811
3812         local failed=0
3813         for i in {1..50}; do
3814                 for fname in $(mktemp -u $DIR/$tdir/.$tfile.XXXXXX) \
3815                              $(mktemp $DIR/$tdir/$tfile.XXXXXXXX); do
3816                         touch $fname  || error "touch $fname failed"
3817                         index2=$($LFS getstripe -m $fname)
3818                         if [[ $index != $index2 ]]; then
3819                                 failed=$((failed + 1))
3820                                 echo "$fname MDT index mismatch $index != $index2"
3821                         fi
3822                 done
3823         done
3824         echo "$failed MDT index mismatches"
3825         (( failed < 4 )) || error "MDT index mismatch $failed times"
3826
3827 }
3828 run_test 33h "temp file is located on the same MDT as target"
3829
3830 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3831 test_34a() {
3832         rm -f $DIR/f34
3833         $MCREATE $DIR/f34 || error "mcreate failed"
3834         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3835                 error "getstripe failed"
3836         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3837         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3838                 error "getstripe failed"
3839         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3840                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3841 }
3842 run_test 34a "truncate file that has not been opened ==========="
3843
3844 test_34b() {
3845         [ ! -f $DIR/f34 ] && test_34a
3846         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3847                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3848         $OPENFILE -f O_RDONLY $DIR/f34
3849         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3850                 error "getstripe failed"
3851         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3852                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3853 }
3854 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3855
3856 test_34c() {
3857         [ ! -f $DIR/f34 ] && test_34a
3858         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3859                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3860         $OPENFILE -f O_RDWR $DIR/f34
3861         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" &&
3862                 error "$LFS getstripe failed"
3863         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3864                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3865 }
3866 run_test 34c "O_RDWR opening file-with-size works =============="
3867
3868 test_34d() {
3869         [ ! -f $DIR/f34 ] && test_34a
3870         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3871                 error "dd failed"
3872         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3873                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3874         rm $DIR/f34
3875 }
3876 run_test 34d "write to sparse file ============================="
3877
3878 test_34e() {
3879         rm -f $DIR/f34e
3880         $MCREATE $DIR/f34e || error "mcreate failed"
3881         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3882         $CHECKSTAT -s 1000 $DIR/f34e ||
3883                 error "Size of $DIR/f34e not equal to 1000 bytes"
3884         $OPENFILE -f O_RDWR $DIR/f34e
3885         $CHECKSTAT -s 1000 $DIR/f34e ||
3886                 error "Size of $DIR/f34e not equal to 1000 bytes"
3887 }
3888 run_test 34e "create objects, some with size and some without =="
3889
3890 test_34f() { # bug 6242, 6243
3891         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3892
3893         SIZE34F=48000
3894         rm -f $DIR/f34f
3895         $MCREATE $DIR/f34f || error "mcreate failed"
3896         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3897         dd if=$DIR/f34f of=$TMP/f34f
3898         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3899         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3900         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3901         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3902         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3903 }
3904 run_test 34f "read from a file with no objects until EOF ======="
3905
3906 test_34g() {
3907         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3908
3909         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3910                 error "dd failed"
3911         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3912         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3913                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3914         cancel_lru_locks osc
3915         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3916                 error "wrong size after lock cancel"
3917
3918         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3919         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3920                 error "expanding truncate failed"
3921         cancel_lru_locks osc
3922         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3923                 error "wrong expanded size after lock cancel"
3924 }
3925 run_test 34g "truncate long file ==============================="
3926
3927 test_34h() {
3928         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3929
3930         local gid=10
3931         local sz=1000
3932
3933         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3934         sync # Flush the cache so that multiop below does not block on cache
3935              # flush when getting the group lock
3936         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3937         MULTIPID=$!
3938
3939         # Since just timed wait is not good enough, let's do a sync write
3940         # that way we are sure enough time for a roundtrip + processing
3941         # passed + 2 seconds of extra margin.
3942         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
3943         rm $DIR/${tfile}-1
3944         sleep 2
3945
3946         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3947                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3948                 kill -9 $MULTIPID
3949         fi
3950         wait $MULTIPID
3951         local nsz=`stat -c %s $DIR/$tfile`
3952         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3953 }
3954 run_test 34h "ftruncate file under grouplock should not block"
3955
3956 test_35a() {
3957         cp /bin/sh $DIR/f35a
3958         chmod 444 $DIR/f35a
3959         chown $RUNAS_ID $DIR/f35a
3960         $RUNAS $DIR/f35a && error || true
3961         rm $DIR/f35a
3962 }
3963 run_test 35a "exec file with mode 444 (should return and not leak)"
3964
3965 test_36a() {
3966         rm -f $DIR/f36
3967         utime $DIR/f36 || error "utime failed for MDS"
3968 }
3969 run_test 36a "MDS utime check (mknod, utime)"
3970
3971 test_36b() {
3972         echo "" > $DIR/f36
3973         utime $DIR/f36 || error "utime failed for OST"
3974 }
3975 run_test 36b "OST utime check (open, utime)"
3976
3977 test_36c() {
3978         rm -f $DIR/d36/f36
3979         test_mkdir $DIR/d36
3980         chown $RUNAS_ID $DIR/d36
3981         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3982 }
3983 run_test 36c "non-root MDS utime check (mknod, utime)"
3984
3985 test_36d() {
3986         [ ! -d $DIR/d36 ] && test_36c
3987         echo "" > $DIR/d36/f36
3988         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3989 }
3990 run_test 36d "non-root OST utime check (open, utime)"
3991
3992 test_36e() {
3993         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3994
3995         test_mkdir $DIR/$tdir
3996         touch $DIR/$tdir/$tfile
3997         $RUNAS utime $DIR/$tdir/$tfile &&
3998                 error "utime worked, expected failure" || true
3999 }
4000 run_test 36e "utime on non-owned file (should return error)"
4001
4002 subr_36fh() {
4003         local fl="$1"
4004         local LANG_SAVE=$LANG
4005         local LC_LANG_SAVE=$LC_LANG
4006         export LANG=C LC_LANG=C # for date language
4007
4008         DATESTR="Dec 20  2000"
4009         test_mkdir $DIR/$tdir
4010         lctl set_param fail_loc=$fl
4011         date; date +%s
4012         cp /etc/hosts $DIR/$tdir/$tfile
4013         sync & # write RPC generated with "current" inode timestamp, but delayed
4014         sleep 1
4015         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
4016         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
4017         cancel_lru_locks $OSC
4018         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
4019         date; date +%s
4020         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
4021                 echo "BEFORE: $LS_BEFORE" && \
4022                 echo "AFTER : $LS_AFTER" && \
4023                 echo "WANT  : $DATESTR" && \
4024                 error "$DIR/$tdir/$tfile timestamps changed" || true
4025
4026         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
4027 }
4028
4029 test_36f() {
4030         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4031
4032         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
4033         subr_36fh "0x80000214"
4034 }
4035 run_test 36f "utime on file racing with OST BRW write =========="
4036
4037 test_36g() {
4038         remote_ost_nodsh && skip "remote OST with nodsh"
4039         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4040         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
4041                 skip "Need MDS version at least 2.12.51"
4042
4043         local fmd_max_age
4044         local fmd
4045         local facet="ost1"
4046         local tgt="obdfilter"
4047
4048         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
4049
4050         test_mkdir $DIR/$tdir
4051         fmd_max_age=$(do_facet $facet \
4052                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
4053                 head -n 1")
4054
4055         echo "FMD max age: ${fmd_max_age}s"
4056         touch $DIR/$tdir/$tfile
4057         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4058                 gawk '{cnt=cnt+$1}  END{print cnt}')
4059         echo "FMD before: $fmd"
4060         [[ $fmd == 0 ]] &&
4061                 error "FMD wasn't create by touch"
4062         sleep $((fmd_max_age + 12))
4063         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4064                 gawk '{cnt=cnt+$1}  END{print cnt}')
4065         echo "FMD after: $fmd"
4066         [[ $fmd == 0 ]] ||
4067                 error "FMD wasn't expired by ping"
4068 }
4069 run_test 36g "FMD cache expiry ====================="
4070
4071 test_36h() {
4072         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4073
4074         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
4075         subr_36fh "0x80000227"
4076 }
4077 run_test 36h "utime on file racing with OST BRW write =========="
4078
4079 test_36i() {
4080         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4081
4082         test_mkdir $DIR/$tdir
4083         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
4084
4085         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
4086         local new_mtime=$((mtime + 200))
4087
4088         #change Modify time of striped dir
4089         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
4090                         error "change mtime failed"
4091
4092         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
4093
4094         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
4095 }
4096 run_test 36i "change mtime on striped directory"
4097
4098 # test_37 - duplicate with tests 32q 32r
4099
4100 test_38() {
4101         local file=$DIR/$tfile
4102         touch $file
4103         openfile -f O_DIRECTORY $file
4104         local RC=$?
4105         local ENOTDIR=20
4106         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
4107         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
4108 }
4109 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
4110
4111 test_39a() { # was test_39
4112         touch $DIR/$tfile
4113         touch $DIR/${tfile}2
4114 #       ls -l  $DIR/$tfile $DIR/${tfile}2
4115 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
4116 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
4117         sleep 2
4118         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
4119         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
4120                 echo "mtime"
4121                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
4122                 echo "atime"
4123                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
4124                 echo "ctime"
4125                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
4126                 error "O_TRUNC didn't change timestamps"
4127         fi
4128 }
4129 run_test 39a "mtime changed on create"
4130
4131 test_39b() {
4132         test_mkdir -c1 $DIR/$tdir
4133         cp -p /etc/passwd $DIR/$tdir/fopen
4134         cp -p /etc/passwd $DIR/$tdir/flink
4135         cp -p /etc/passwd $DIR/$tdir/funlink
4136         cp -p /etc/passwd $DIR/$tdir/frename
4137         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
4138
4139         sleep 1
4140         echo "aaaaaa" >> $DIR/$tdir/fopen
4141         echo "aaaaaa" >> $DIR/$tdir/flink
4142         echo "aaaaaa" >> $DIR/$tdir/funlink
4143         echo "aaaaaa" >> $DIR/$tdir/frename
4144
4145         local open_new=`stat -c %Y $DIR/$tdir/fopen`
4146         local link_new=`stat -c %Y $DIR/$tdir/flink`
4147         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
4148         local rename_new=`stat -c %Y $DIR/$tdir/frename`
4149
4150         cat $DIR/$tdir/fopen > /dev/null
4151         ln $DIR/$tdir/flink $DIR/$tdir/flink2
4152         rm -f $DIR/$tdir/funlink2
4153         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
4154
4155         for (( i=0; i < 2; i++ )) ; do
4156                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
4157                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
4158                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
4159                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
4160
4161                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
4162                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
4163                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
4164                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
4165
4166                 cancel_lru_locks $OSC
4167                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4168         done
4169 }
4170 run_test 39b "mtime change on open, link, unlink, rename  ======"
4171
4172 # this should be set to past
4173 TEST_39_MTIME=`date -d "1 year ago" +%s`
4174
4175 # bug 11063
4176 test_39c() {
4177         touch $DIR1/$tfile
4178         sleep 2
4179         local mtime0=`stat -c %Y $DIR1/$tfile`
4180
4181         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4182         local mtime1=`stat -c %Y $DIR1/$tfile`
4183         [ "$mtime1" = $TEST_39_MTIME ] || \
4184                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
4185
4186         local d1=`date +%s`
4187         echo hello >> $DIR1/$tfile
4188         local d2=`date +%s`
4189         local mtime2=`stat -c %Y $DIR1/$tfile`
4190         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
4191                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
4192
4193         mv $DIR1/$tfile $DIR1/$tfile-1
4194
4195         for (( i=0; i < 2; i++ )) ; do
4196                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
4197                 [ "$mtime2" = "$mtime3" ] || \
4198                         error "mtime ($mtime2) changed (to $mtime3) on rename"
4199
4200                 cancel_lru_locks $OSC
4201                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4202         done
4203 }
4204 run_test 39c "mtime change on rename ==========================="
4205
4206 # bug 21114
4207 test_39d() {
4208         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4209
4210         touch $DIR1/$tfile
4211         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4212
4213         for (( i=0; i < 2; i++ )) ; do
4214                 local mtime=`stat -c %Y $DIR1/$tfile`
4215                 [ $mtime = $TEST_39_MTIME ] || \
4216                         error "mtime($mtime) is not set to $TEST_39_MTIME"
4217
4218                 cancel_lru_locks $OSC
4219                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4220         done
4221 }
4222 run_test 39d "create, utime, stat =============================="
4223
4224 # bug 21114
4225 test_39e() {
4226         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4227
4228         touch $DIR1/$tfile
4229         local mtime1=`stat -c %Y $DIR1/$tfile`
4230
4231         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4232
4233         for (( i=0; i < 2; i++ )) ; do
4234                 local mtime2=`stat -c %Y $DIR1/$tfile`
4235                 [ $mtime2 = $TEST_39_MTIME ] || \
4236                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4237
4238                 cancel_lru_locks $OSC
4239                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4240         done
4241 }
4242 run_test 39e "create, stat, utime, stat ========================"
4243
4244 # bug 21114
4245 test_39f() {
4246         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4247
4248         touch $DIR1/$tfile
4249         mtime1=`stat -c %Y $DIR1/$tfile`
4250
4251         sleep 2
4252         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4253
4254         for (( i=0; i < 2; i++ )) ; do
4255                 local mtime2=`stat -c %Y $DIR1/$tfile`
4256                 [ $mtime2 = $TEST_39_MTIME ] || \
4257                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4258
4259                 cancel_lru_locks $OSC
4260                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4261         done
4262 }
4263 run_test 39f "create, stat, sleep, utime, stat ================="
4264
4265 # bug 11063
4266 test_39g() {
4267         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4268
4269         echo hello >> $DIR1/$tfile
4270         local mtime1=`stat -c %Y $DIR1/$tfile`
4271
4272         sleep 2
4273         chmod o+r $DIR1/$tfile
4274
4275         for (( i=0; i < 2; i++ )) ; do
4276                 local mtime2=`stat -c %Y $DIR1/$tfile`
4277                 [ "$mtime1" = "$mtime2" ] || \
4278                         error "lost mtime: $mtime2, should be $mtime1"
4279
4280                 cancel_lru_locks $OSC
4281                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4282         done
4283 }
4284 run_test 39g "write, chmod, stat ==============================="
4285
4286 # bug 11063
4287 test_39h() {
4288         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4289
4290         touch $DIR1/$tfile
4291         sleep 1
4292
4293         local d1=`date`
4294         echo hello >> $DIR1/$tfile
4295         local mtime1=`stat -c %Y $DIR1/$tfile`
4296
4297         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4298         local d2=`date`
4299         if [ "$d1" != "$d2" ]; then
4300                 echo "write and touch not within one second"
4301         else
4302                 for (( i=0; i < 2; i++ )) ; do
4303                         local mtime2=`stat -c %Y $DIR1/$tfile`
4304                         [ "$mtime2" = $TEST_39_MTIME ] || \
4305                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
4306
4307                         cancel_lru_locks $OSC
4308                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4309                 done
4310         fi
4311 }
4312 run_test 39h "write, utime within one second, stat ============="
4313
4314 test_39i() {
4315         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4316
4317         touch $DIR1/$tfile
4318         sleep 1
4319
4320         echo hello >> $DIR1/$tfile
4321         local mtime1=`stat -c %Y $DIR1/$tfile`
4322
4323         mv $DIR1/$tfile $DIR1/$tfile-1
4324
4325         for (( i=0; i < 2; i++ )) ; do
4326                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4327
4328                 [ "$mtime1" = "$mtime2" ] || \
4329                         error "lost mtime: $mtime2, should be $mtime1"
4330
4331                 cancel_lru_locks $OSC
4332                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4333         done
4334 }
4335 run_test 39i "write, rename, stat =============================="
4336
4337 test_39j() {
4338         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4339
4340         start_full_debug_logging
4341         touch $DIR1/$tfile
4342         sleep 1
4343
4344         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
4345         lctl set_param fail_loc=0x80000412
4346         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
4347                 error "multiop failed"
4348         local multipid=$!
4349         local mtime1=`stat -c %Y $DIR1/$tfile`
4350
4351         mv $DIR1/$tfile $DIR1/$tfile-1
4352
4353         kill -USR1 $multipid
4354         wait $multipid || error "multiop close failed"
4355
4356         for (( i=0; i < 2; i++ )) ; do
4357                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4358                 [ "$mtime1" = "$mtime2" ] ||
4359                         error "mtime is lost on close: $mtime2, " \
4360                               "should be $mtime1"
4361
4362                 cancel_lru_locks
4363                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4364         done
4365         lctl set_param fail_loc=0
4366         stop_full_debug_logging
4367 }
4368 run_test 39j "write, rename, close, stat ======================="
4369
4370 test_39k() {
4371         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4372
4373         touch $DIR1/$tfile
4374         sleep 1
4375
4376         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
4377         local multipid=$!
4378         local mtime1=`stat -c %Y $DIR1/$tfile`
4379
4380         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4381
4382         kill -USR1 $multipid
4383         wait $multipid || error "multiop close failed"
4384
4385         for (( i=0; i < 2; i++ )) ; do
4386                 local mtime2=`stat -c %Y $DIR1/$tfile`
4387
4388                 [ "$mtime2" = $TEST_39_MTIME ] || \
4389                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
4390
4391                 cancel_lru_locks
4392                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4393         done
4394 }
4395 run_test 39k "write, utime, close, stat ========================"
4396
4397 # this should be set to future
4398 TEST_39_ATIME=`date -d "1 year" +%s`
4399
4400 test_39l() {
4401         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4402         remote_mds_nodsh && skip "remote MDS with nodsh"
4403
4404         local atime_diff=$(do_facet $SINGLEMDS \
4405                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4406         rm -rf $DIR/$tdir
4407         mkdir -p $DIR/$tdir
4408
4409         # test setting directory atime to future
4410         touch -a -d @$TEST_39_ATIME $DIR/$tdir
4411         local atime=$(stat -c %X $DIR/$tdir)
4412         [ "$atime" = $TEST_39_ATIME ] ||
4413                 error "atime is not set to future: $atime, $TEST_39_ATIME"
4414
4415         # test setting directory atime from future to now
4416         local now=$(date +%s)
4417         touch -a -d @$now $DIR/$tdir
4418
4419         atime=$(stat -c %X $DIR/$tdir)
4420         [ "$atime" -eq "$now"  ] ||
4421                 error "atime is not updated from future: $atime, $now"
4422
4423         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
4424         sleep 3
4425
4426         # test setting directory atime when now > dir atime + atime_diff
4427         local d1=$(date +%s)
4428         ls $DIR/$tdir
4429         local d2=$(date +%s)
4430         cancel_lru_locks mdc
4431         atime=$(stat -c %X $DIR/$tdir)
4432         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4433                 error "atime is not updated  : $atime, should be $d2"
4434
4435         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
4436         sleep 3
4437
4438         # test not setting directory atime when now < dir atime + atime_diff
4439         ls $DIR/$tdir
4440         cancel_lru_locks mdc
4441         atime=$(stat -c %X $DIR/$tdir)
4442         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4443                 error "atime is updated to $atime, should remain $d1<atime<$d2"
4444
4445         do_facet $SINGLEMDS \
4446                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4447 }
4448 run_test 39l "directory atime update ==========================="
4449
4450 test_39m() {
4451         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4452
4453         touch $DIR1/$tfile
4454         sleep 2
4455         local far_past_mtime=$(date -d "May 29 1953" +%s)
4456         local far_past_atime=$(date -d "Dec 17 1903" +%s)
4457
4458         touch -m -d @$far_past_mtime $DIR1/$tfile
4459         touch -a -d @$far_past_atime $DIR1/$tfile
4460
4461         for (( i=0; i < 2; i++ )) ; do
4462                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
4463                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
4464                         error "atime or mtime set incorrectly"
4465
4466                 cancel_lru_locks $OSC
4467                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4468         done
4469 }
4470 run_test 39m "test atime and mtime before 1970"
4471
4472 test_39n() { # LU-3832
4473         remote_mds_nodsh && skip "remote MDS with nodsh"
4474
4475         local atime_diff=$(do_facet $SINGLEMDS \
4476                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4477         local atime0
4478         local atime1
4479         local atime2
4480
4481         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
4482
4483         rm -rf $DIR/$tfile
4484         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
4485         atime0=$(stat -c %X $DIR/$tfile)
4486
4487         sleep 5
4488         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4489         atime1=$(stat -c %X $DIR/$tfile)
4490
4491         sleep 5
4492         cancel_lru_locks mdc
4493         cancel_lru_locks osc
4494         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4495         atime2=$(stat -c %X $DIR/$tfile)
4496
4497         do_facet $SINGLEMDS \
4498                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4499
4500         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
4501         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
4502 }
4503 run_test 39n "check that O_NOATIME is honored"
4504
4505 test_39o() {
4506         TESTDIR=$DIR/$tdir/$tfile
4507         [ -e $TESTDIR ] && rm -rf $TESTDIR
4508         mkdir -p $TESTDIR
4509         cd $TESTDIR
4510         links1=2
4511         ls
4512         mkdir a b
4513         ls
4514         links2=$(stat -c %h .)
4515         [ $(($links1 + 2)) != $links2 ] &&
4516                 error "wrong links count $(($links1 + 2)) != $links2"
4517         rmdir b
4518         links3=$(stat -c %h .)
4519         [ $(($links1 + 1)) != $links3 ] &&
4520                 error "wrong links count $links1 != $links3"
4521         return 0
4522 }
4523 run_test 39o "directory cached attributes updated after create"
4524
4525 test_39p() {
4526         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4527
4528         local MDTIDX=1
4529         TESTDIR=$DIR/$tdir/$tdir
4530         [ -e $TESTDIR ] && rm -rf $TESTDIR
4531         test_mkdir -p $TESTDIR
4532         cd $TESTDIR
4533         links1=2
4534         ls
4535         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
4536         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
4537         ls
4538         links2=$(stat -c %h .)
4539         [ $(($links1 + 2)) != $links2 ] &&
4540                 error "wrong links count $(($links1 + 2)) != $links2"
4541         rmdir remote_dir2
4542         links3=$(stat -c %h .)
4543         [ $(($links1 + 1)) != $links3 ] &&
4544                 error "wrong links count $links1 != $links3"
4545         return 0
4546 }
4547 run_test 39p "remote directory cached attributes updated after create ========"
4548
4549 test_39r() {
4550         [ $OST1_VERSION -ge $(version_code 2.13.52) ] ||
4551                 skip "no atime update on old OST"
4552         if [ "$ost1_FSTYPE" != ldiskfs ]; then
4553                 skip_env "ldiskfs only test"
4554         fi
4555
4556         local saved_adiff
4557         saved_adiff=$(do_facet ost1 \
4558                 lctl get_param -n obdfilter.*OST0000.atime_diff)
4559         stack_trap "do_facet ost1 \
4560                 lctl set_param obdfilter.*.atime_diff=$saved_adiff"
4561
4562         do_facet ost1 "lctl set_param obdfilter.*.atime_diff=5"
4563
4564         $LFS setstripe -i 0 $DIR/$tfile
4565         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 ||
4566                 error "can't write initial file"
4567         cancel_lru_locks osc
4568
4569         # exceed atime_diff and access file
4570         sleep 6
4571         dd if=$DIR/$tfile of=/dev/null || error "can't udpate atime"
4572
4573         local atime_cli=$(stat -c %X $DIR/$tfile)
4574         echo "client atime: $atime_cli"
4575         # allow atime update to be written to device
4576         do_facet ost1 "$LCTL set_param -n osd*.*OST*.force_sync 1"
4577         sleep 5
4578
4579         local ostdev=$(ostdevname 1)
4580         local fid=($(lfs getstripe -y $DIR/$tfile |
4581                         awk '/l_fid:/ { print $2 }' | tr ':' ' '))
4582         local objpath="O/0/d$((${fid[1]} % 32))/$((${fid[1]}))"
4583         local cmd="debugfs -c -R \\\"stat $objpath\\\" $ostdev"
4584
4585         echo "OST atime: $(do_facet ost1 "$cmd" |& grep atime)"
4586         local atime_ost=$(do_facet ost1 "$cmd" |&
4587                           awk -F'[: ]' '/atime:/ { print $4 }')
4588         (( atime_cli == atime_ost )) ||
4589                 error "atime on client $atime_cli != ost $atime_ost"
4590 }
4591 run_test 39r "lazy atime update on OST"
4592
4593 test_39q() { # LU-8041
4594         local testdir=$DIR/$tdir
4595         mkdir -p $testdir
4596         multiop_bg_pause $testdir D_c || error "multiop failed"
4597         local multipid=$!
4598         cancel_lru_locks mdc
4599         kill -USR1 $multipid
4600         local atime=$(stat -c %X $testdir)
4601         [ "$atime" -ne 0 ] || error "atime is zero"
4602 }
4603 run_test 39q "close won't zero out atime"
4604
4605 test_40() {
4606         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
4607         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
4608                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
4609         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
4610                 error "$tfile is not 4096 bytes in size"
4611 }
4612 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
4613
4614 test_41() {
4615         # bug 1553
4616         small_write $DIR/f41 18
4617 }
4618 run_test 41 "test small file write + fstat ====================="
4619
4620 count_ost_writes() {
4621         lctl get_param -n ${OSC}.*.stats |
4622                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
4623                         END { printf("%0.0f", writes) }'
4624 }
4625
4626 # decent default
4627 WRITEBACK_SAVE=500
4628 DIRTY_RATIO_SAVE=40
4629 MAX_DIRTY_RATIO=50
4630 BG_DIRTY_RATIO_SAVE=10
4631 MAX_BG_DIRTY_RATIO=25
4632
4633 start_writeback() {
4634         trap 0
4635         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
4636         # dirty_ratio, dirty_background_ratio
4637         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4638                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
4639                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
4640                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
4641         else
4642                 # if file not here, we are a 2.4 kernel
4643                 kill -CONT `pidof kupdated`
4644         fi
4645 }
4646
4647 stop_writeback() {
4648         # setup the trap first, so someone cannot exit the test at the
4649         # exact wrong time and mess up a machine
4650         trap start_writeback EXIT
4651         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
4652         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4653                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
4654                 sysctl -w vm.dirty_writeback_centisecs=0
4655                 sysctl -w vm.dirty_writeback_centisecs=0
4656                 # save and increase /proc/sys/vm/dirty_ratio
4657                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
4658                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
4659                 # save and increase /proc/sys/vm/dirty_background_ratio
4660                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
4661                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
4662         else
4663                 # if file not here, we are a 2.4 kernel
4664                 kill -STOP `pidof kupdated`
4665         fi
4666 }
4667
4668 # ensure that all stripes have some grant before we test client-side cache
4669 setup_test42() {
4670         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
4671                 dd if=/dev/zero of=$i bs=4k count=1
4672                 rm $i
4673         done
4674 }
4675
4676 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
4677 # file truncation, and file removal.
4678 test_42a() {
4679         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4680
4681         setup_test42
4682         cancel_lru_locks $OSC
4683         stop_writeback
4684         sync; sleep 1; sync # just to be safe
4685         BEFOREWRITES=`count_ost_writes`
4686         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
4687         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
4688         AFTERWRITES=`count_ost_writes`
4689         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
4690                 error "$BEFOREWRITES < $AFTERWRITES"
4691         start_writeback
4692 }
4693 run_test 42a "ensure that we don't flush on close"
4694
4695 test_42b() {
4696         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4697
4698         setup_test42
4699         cancel_lru_locks $OSC
4700         stop_writeback
4701         sync
4702         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
4703         BEFOREWRITES=$(count_ost_writes)
4704         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
4705         AFTERWRITES=$(count_ost_writes)
4706         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4707                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
4708         fi
4709         BEFOREWRITES=$(count_ost_writes)
4710         sync || error "sync: $?"
4711         AFTERWRITES=$(count_ost_writes)
4712         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4713                 error "$BEFOREWRITES < $AFTERWRITES on sync"
4714         fi
4715         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
4716         start_writeback
4717         return 0
4718 }
4719 run_test 42b "test destroy of file with cached dirty data ======"
4720
4721 # if these tests just want to test the effect of truncation,
4722 # they have to be very careful.  consider:
4723 # - the first open gets a {0,EOF}PR lock
4724 # - the first write conflicts and gets a {0, count-1}PW
4725 # - the rest of the writes are under {count,EOF}PW
4726 # - the open for truncate tries to match a {0,EOF}PR
4727 #   for the filesize and cancels the PWs.
4728 # any number of fixes (don't get {0,EOF} on open, match
4729 # composite locks, do smarter file size management) fix
4730 # this, but for now we want these tests to verify that
4731 # the cancellation with truncate intent works, so we
4732 # start the file with a full-file pw lock to match against
4733 # until the truncate.
4734 trunc_test() {
4735         test=$1
4736         file=$DIR/$test
4737         offset=$2
4738         cancel_lru_locks $OSC
4739         stop_writeback
4740         # prime the file with 0,EOF PW to match
4741         touch $file
4742         $TRUNCATE $file 0
4743         sync; sync
4744         # now the real test..
4745         dd if=/dev/zero of=$file bs=1024 count=100
4746         BEFOREWRITES=`count_ost_writes`
4747         $TRUNCATE $file $offset
4748         cancel_lru_locks $OSC
4749         AFTERWRITES=`count_ost_writes`
4750         start_writeback
4751 }
4752
4753 test_42c() {
4754         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4755
4756         trunc_test 42c 1024
4757         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
4758                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
4759         rm $file
4760 }
4761 run_test 42c "test partial truncate of file with cached dirty data"
4762
4763 test_42d() {
4764         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4765
4766         trunc_test 42d 0
4767         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
4768                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
4769         rm $file
4770 }
4771 run_test 42d "test complete truncate of file with cached dirty data"
4772
4773 test_42e() { # bug22074
4774         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4775
4776         local TDIR=$DIR/${tdir}e
4777         local pages=16 # hardcoded 16 pages, don't change it.
4778         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
4779         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
4780         local max_dirty_mb
4781         local warmup_files
4782
4783         test_mkdir $DIR/${tdir}e
4784         $LFS setstripe -c 1 $TDIR
4785         createmany -o $TDIR/f $files
4786
4787         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
4788
4789         # we assume that with $OSTCOUNT files, at least one of them will
4790         # be allocated on OST0.
4791         warmup_files=$((OSTCOUNT * max_dirty_mb))
4792         createmany -o $TDIR/w $warmup_files
4793
4794         # write a large amount of data into one file and sync, to get good
4795         # avail_grant number from OST.
4796         for ((i=0; i<$warmup_files; i++)); do
4797                 idx=$($LFS getstripe -i $TDIR/w$i)
4798                 [ $idx -ne 0 ] && continue
4799                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
4800                 break
4801         done
4802         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
4803         sync
4804         $LCTL get_param $proc_osc0/cur_dirty_bytes
4805         $LCTL get_param $proc_osc0/cur_grant_bytes
4806
4807         # create as much dirty pages as we can while not to trigger the actual
4808         # RPCs directly. but depends on the env, VFS may trigger flush during this
4809         # period, hopefully we are good.
4810         for ((i=0; i<$warmup_files; i++)); do
4811                 idx=$($LFS getstripe -i $TDIR/w$i)
4812                 [ $idx -ne 0 ] && continue
4813                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4814         done
4815         $LCTL get_param $proc_osc0/cur_dirty_bytes
4816         $LCTL get_param $proc_osc0/cur_grant_bytes
4817
4818         # perform the real test
4819         $LCTL set_param $proc_osc0/rpc_stats 0
4820         for ((;i<$files; i++)); do
4821                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
4822                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
4823         done
4824         sync
4825         $LCTL get_param $proc_osc0/rpc_stats
4826
4827         local percent=0
4828         local have_ppr=false
4829         $LCTL get_param $proc_osc0/rpc_stats |
4830                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4831                         # skip lines until we are at the RPC histogram data
4832                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4833                         $have_ppr || continue
4834
4835                         # we only want the percent stat for < 16 pages
4836                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4837
4838                         percent=$((percent + WPCT))
4839                         if [[ $percent -gt 15 ]]; then
4840                                 error "less than 16-pages write RPCs" \
4841                                       "$percent% > 15%"
4842                                 break
4843                         fi
4844                 done
4845         rm -rf $TDIR
4846 }
4847 run_test 42e "verify sub-RPC writes are not done synchronously"
4848
4849 test_43A() { # was test_43
4850         test_mkdir $DIR/$tdir
4851         cp -p /bin/ls $DIR/$tdir/$tfile
4852         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4853         pid=$!
4854         # give multiop a chance to open
4855         sleep 1
4856
4857         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4858         kill -USR1 $pid
4859 }
4860 run_test 43A "execution of file opened for write should return -ETXTBSY"
4861
4862 test_43a() {
4863         test_mkdir $DIR/$tdir
4864         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4865         $DIR/$tdir/sleep 60 &
4866         SLEEP_PID=$!
4867         # Make sure exec of $tdir/sleep wins race with truncate
4868         sleep 1
4869         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
4870         kill $SLEEP_PID
4871 }
4872 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4873
4874 test_43b() {
4875         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4876
4877         test_mkdir $DIR/$tdir
4878         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4879         $DIR/$tdir/sleep 60 &
4880         SLEEP_PID=$!
4881         # Make sure exec of $tdir/sleep wins race with truncate
4882         sleep 1
4883         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
4884         kill $SLEEP_PID
4885 }
4886 run_test 43b "truncate of file being executed should return -ETXTBSY"
4887
4888 test_43c() {
4889         local testdir="$DIR/$tdir"
4890         test_mkdir $testdir
4891         cp $SHELL $testdir/
4892         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4893                 ( cd $testdir && md5sum -c )
4894 }
4895 run_test 43c "md5sum of copy into lustre"
4896
4897 test_44A() { # was test_44
4898         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4899
4900         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4901         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4902 }
4903 run_test 44A "zero length read from a sparse stripe"
4904
4905 test_44a() {
4906         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4907                 awk '{ print $2 }')
4908         [ -z "$nstripe" ] && skip "can't get stripe info"
4909         [[ $nstripe -gt $OSTCOUNT ]] &&
4910                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4911
4912         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4913                 awk '{ print $2 }')
4914         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4915                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4916                         awk '{ print $2 }')
4917         fi
4918
4919         OFFSETS="0 $((stride/2)) $((stride-1))"
4920         for offset in $OFFSETS; do
4921                 for i in $(seq 0 $((nstripe-1))); do
4922                         local GLOBALOFFSETS=""
4923                         # size in Bytes
4924                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4925                         local myfn=$DIR/d44a-$size
4926                         echo "--------writing $myfn at $size"
4927                         ll_sparseness_write $myfn $size ||
4928                                 error "ll_sparseness_write"
4929                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4930                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4931                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4932
4933                         for j in $(seq 0 $((nstripe-1))); do
4934                                 # size in Bytes
4935                                 size=$((((j + $nstripe )*$stride + $offset)))
4936                                 ll_sparseness_write $myfn $size ||
4937                                         error "ll_sparseness_write"
4938                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4939                         done
4940                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4941                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4942                         rm -f $myfn
4943                 done
4944         done
4945 }
4946 run_test 44a "test sparse pwrite ==============================="
4947
4948 dirty_osc_total() {
4949         tot=0
4950         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4951                 tot=$(($tot + $d))
4952         done
4953         echo $tot
4954 }
4955 do_dirty_record() {
4956         before=`dirty_osc_total`
4957         echo executing "\"$*\""
4958         eval $*
4959         after=`dirty_osc_total`
4960         echo before $before, after $after
4961 }
4962 test_45() {
4963         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4964
4965         f="$DIR/f45"
4966         # Obtain grants from OST if it supports it
4967         echo blah > ${f}_grant
4968         stop_writeback
4969         sync
4970         do_dirty_record "echo blah > $f"
4971         [[ $before -eq $after ]] && error "write wasn't cached"
4972         do_dirty_record "> $f"
4973         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4974         do_dirty_record "echo blah > $f"
4975         [[ $before -eq $after ]] && error "write wasn't cached"
4976         do_dirty_record "sync"
4977         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4978         do_dirty_record "echo blah > $f"
4979         [[ $before -eq $after ]] && error "write wasn't cached"
4980         do_dirty_record "cancel_lru_locks osc"
4981         [[ $before -gt $after ]] ||
4982                 error "lock cancellation didn't lower dirty count"
4983         start_writeback
4984 }
4985 run_test 45 "osc io page accounting ============================"
4986
4987 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4988 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4989 # objects offset and an assert hit when an rpc was built with 1023's mapped
4990 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4991 test_46() {
4992         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4993
4994         f="$DIR/f46"
4995         stop_writeback
4996         sync
4997         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4998         sync
4999         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
5000         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
5001         sync
5002         start_writeback
5003 }
5004 run_test 46 "dirtying a previously written page ================"
5005
5006 # test_47 is removed "Device nodes check" is moved to test_28
5007
5008 test_48a() { # bug 2399
5009         [ "$mds1_FSTYPE" = "zfs" ] &&
5010         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
5011                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
5012
5013         test_mkdir $DIR/$tdir
5014         cd $DIR/$tdir
5015         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
5016         test_mkdir $DIR/$tdir
5017         touch foo || error "'touch foo' failed after recreating cwd"
5018         test_mkdir bar
5019         touch .foo || error "'touch .foo' failed after recreating cwd"
5020         test_mkdir .bar
5021         ls . > /dev/null || error "'ls .' failed after recreating cwd"
5022         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5023         cd . || error "'cd .' failed after recreating cwd"
5024         mkdir . && error "'mkdir .' worked after recreating cwd"
5025         rmdir . && error "'rmdir .' worked after recreating cwd"
5026         ln -s . baz || error "'ln -s .' failed after recreating cwd"
5027         cd .. || error "'cd ..' failed after recreating cwd"
5028 }
5029 run_test 48a "Access renamed working dir (should return errors)="
5030
5031 test_48b() { # bug 2399
5032         rm -rf $DIR/$tdir
5033         test_mkdir $DIR/$tdir
5034         cd $DIR/$tdir
5035         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
5036         touch foo && error "'touch foo' worked after removing cwd"
5037         mkdir foo && error "'mkdir foo' worked after removing cwd"
5038         touch .foo && error "'touch .foo' worked after removing cwd"
5039         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
5040         ls . > /dev/null && error "'ls .' worked after removing cwd"
5041         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5042         mkdir . && error "'mkdir .' worked after removing cwd"
5043         rmdir . && error "'rmdir .' worked after removing cwd"
5044         ln -s . foo && error "'ln -s .' worked after removing cwd"
5045         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
5046 }
5047 run_test 48b "Access removed working dir (should return errors)="
5048
5049 test_48c() { # bug 2350
5050         #lctl set_param debug=-1
5051         #set -vx
5052         rm -rf $DIR/$tdir
5053         test_mkdir -p $DIR/$tdir/dir
5054         cd $DIR/$tdir/dir
5055         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5056         $TRACE touch foo && error "touch foo worked after removing cwd"
5057         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
5058         touch .foo && error "touch .foo worked after removing cwd"
5059         mkdir .foo && error "mkdir .foo worked after removing cwd"
5060         $TRACE ls . && error "'ls .' worked after removing cwd"
5061         $TRACE ls .. || error "'ls ..' failed after removing cwd"
5062         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
5063         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
5064         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
5065         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
5066 }
5067 run_test 48c "Access removed working subdir (should return errors)"
5068
5069 test_48d() { # bug 2350
5070         #lctl set_param debug=-1
5071         #set -vx
5072         rm -rf $DIR/$tdir
5073         test_mkdir -p $DIR/$tdir/dir
5074         cd $DIR/$tdir/dir
5075         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5076         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5077         $TRACE touch foo && error "'touch foo' worked after removing parent"
5078         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
5079         touch .foo && error "'touch .foo' worked after removing parent"
5080         mkdir .foo && error "mkdir .foo worked after removing parent"
5081         $TRACE ls . && error "'ls .' worked after removing parent"
5082         $TRACE ls .. && error "'ls ..' worked after removing parent"
5083         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
5084         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
5085         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
5086         true
5087 }
5088 run_test 48d "Access removed parent subdir (should return errors)"
5089
5090 test_48e() { # bug 4134
5091         #lctl set_param debug=-1
5092         #set -vx
5093         rm -rf $DIR/$tdir
5094         test_mkdir -p $DIR/$tdir/dir
5095         cd $DIR/$tdir/dir
5096         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5097         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5098         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
5099         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
5100         # On a buggy kernel addition of "touch foo" after cd .. will
5101         # produce kernel oops in lookup_hash_it
5102         touch ../foo && error "'cd ..' worked after recreate parent"
5103         cd $DIR
5104         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
5105 }
5106 run_test 48e "Access to recreated parent subdir (should return errors)"
5107
5108 test_49() { # LU-1030
5109         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5110         remote_ost_nodsh && skip "remote OST with nodsh"
5111
5112         # get ost1 size - $FSNAME-OST0000
5113         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
5114                 awk '{ print $4 }')
5115         # write 800M at maximum
5116         [[ $ost1_size -lt 2 ]] && ost1_size=2
5117         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
5118
5119         $LFS setstripe -c 1 -i 0 $DIR/$tfile
5120         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
5121         local dd_pid=$!
5122
5123         # change max_pages_per_rpc while writing the file
5124         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
5125         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
5126         # loop until dd process exits
5127         while ps ax -opid | grep -wq $dd_pid; do
5128                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
5129                 sleep $((RANDOM % 5 + 1))
5130         done
5131         # restore original max_pages_per_rpc
5132         $LCTL set_param $osc1_mppc=$orig_mppc
5133         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
5134 }
5135 run_test 49 "Change max_pages_per_rpc won't break osc extent"
5136
5137 test_50() {
5138         # bug 1485
5139         test_mkdir $DIR/$tdir
5140         cd $DIR/$tdir
5141         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
5142 }
5143 run_test 50 "special situations: /proc symlinks  ==============="
5144
5145 test_51a() {    # was test_51
5146         # bug 1516 - create an empty entry right after ".." then split dir
5147         test_mkdir -c1 $DIR/$tdir
5148         touch $DIR/$tdir/foo
5149         $MCREATE $DIR/$tdir/bar
5150         rm $DIR/$tdir/foo
5151         createmany -m $DIR/$tdir/longfile 201
5152         FNUM=202
5153         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
5154                 $MCREATE $DIR/$tdir/longfile$FNUM
5155                 FNUM=$(($FNUM + 1))
5156                 echo -n "+"
5157         done
5158         echo
5159         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
5160 }
5161 run_test 51a "special situations: split htree with empty entry =="
5162
5163 cleanup_print_lfs_df () {
5164         trap 0
5165         $LFS df
5166         $LFS df -i
5167 }
5168
5169 test_51b() {
5170         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5171
5172         local dir=$DIR/$tdir
5173         local nrdirs=$((65536 + 100))
5174
5175         # cleanup the directory
5176         rm -fr $dir
5177
5178         test_mkdir -c1 $dir
5179
5180         $LFS df
5181         $LFS df -i
5182         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
5183         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
5184         [[ $numfree -lt $nrdirs ]] &&
5185                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
5186
5187         # need to check free space for the directories as well
5188         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
5189         numfree=$(( blkfree / $(fs_inode_ksize) ))
5190         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
5191
5192         trap cleanup_print_lfs_df EXIT
5193
5194         # create files
5195         createmany -d $dir/d $nrdirs || {
5196                 unlinkmany $dir/d $nrdirs
5197                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
5198         }
5199
5200         # really created :
5201         nrdirs=$(ls -U $dir | wc -l)
5202
5203         # unlink all but 100 subdirectories, then check it still works
5204         local left=100
5205         local delete=$((nrdirs - left))
5206
5207         $LFS df
5208         $LFS df -i
5209
5210         # for ldiskfs the nlink count should be 1, but this is OSD specific
5211         # and so this is listed for informational purposes only
5212         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
5213         unlinkmany -d $dir/d $delete ||
5214                 error "unlink of first $delete subdirs failed"
5215
5216         echo "nlink between: $(stat -c %h $dir)"
5217         local found=$(ls -U $dir | wc -l)
5218         [ $found -ne $left ] &&
5219                 error "can't find subdirs: found only $found, expected $left"
5220
5221         unlinkmany -d $dir/d $delete $left ||
5222                 error "unlink of second $left subdirs failed"
5223         # regardless of whether the backing filesystem tracks nlink accurately
5224         # or not, the nlink count shouldn't be more than "." and ".." here
5225         local after=$(stat -c %h $dir)
5226         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
5227                 echo "nlink after: $after"
5228
5229         cleanup_print_lfs_df
5230 }
5231 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
5232
5233 test_51d() {
5234         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5235         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
5236
5237         test_mkdir $DIR/$tdir
5238         createmany -o $DIR/$tdir/t- 1000
5239         $LFS getstripe $DIR/$tdir > $TMP/$tfile
5240         for N in $(seq 0 $((OSTCOUNT - 1))); do
5241                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
5242                         END { printf("%0.0f", objs) }' $TMP/$tfile)
5243                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
5244                         '($1 == '$N') { objs += 1 } \
5245                         END { printf("%0.0f", objs) }')
5246                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
5247         done
5248         unlinkmany $DIR/$tdir/t- 1000
5249
5250         NLAST=0
5251         for N in $(seq 1 $((OSTCOUNT - 1))); do
5252                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
5253                         error "OST $N has less objects vs OST $NLAST" \
5254                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5255                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
5256                         error "OST $N has less objects vs OST $NLAST" \
5257                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5258
5259                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
5260                         error "OST $N has less #0 objects vs OST $NLAST" \
5261                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5262                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
5263                         error "OST $N has less #0 objects vs OST $NLAST" \
5264                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5265                 NLAST=$N
5266         done
5267         rm -f $TMP/$tfile
5268 }
5269 run_test 51d "check object distribution"
5270
5271 test_51e() {
5272         if [ "$mds1_FSTYPE" != ldiskfs ]; then
5273                 skip_env "ldiskfs only test"
5274         fi
5275
5276         test_mkdir -c1 $DIR/$tdir
5277         test_mkdir -c1 $DIR/$tdir/d0
5278
5279         touch $DIR/$tdir/d0/foo
5280         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
5281                 error "file exceed 65000 nlink limit!"
5282         unlinkmany $DIR/$tdir/d0/f- 65001
5283         return 0
5284 }
5285 run_test 51e "check file nlink limit"
5286
5287 test_51f() {
5288         test_mkdir $DIR/$tdir
5289
5290         local max=100000
5291         local ulimit_old=$(ulimit -n)
5292         local spare=20 # number of spare fd's for scripts/libraries, etc.
5293         local mdt=$($LFS getstripe -m $DIR/$tdir)
5294         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
5295
5296         echo "MDT$mdt numfree=$numfree, max=$max"
5297         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
5298         if [ $((numfree + spare)) -gt $ulimit_old ]; then
5299                 while ! ulimit -n $((numfree + spare)); do
5300                         numfree=$((numfree * 3 / 4))
5301                 done
5302                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
5303         else
5304                 echo "left ulimit at $ulimit_old"
5305         fi
5306
5307         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
5308                 unlinkmany $DIR/$tdir/f $numfree
5309                 error "create+open $numfree files in $DIR/$tdir failed"
5310         }
5311         ulimit -n $ulimit_old
5312
5313         # if createmany exits at 120s there will be fewer than $numfree files
5314         unlinkmany $DIR/$tdir/f $numfree || true
5315 }
5316 run_test 51f "check many open files limit"
5317
5318 test_52a() {
5319         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
5320         test_mkdir $DIR/$tdir
5321         touch $DIR/$tdir/foo
5322         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
5323         echo bar >> $DIR/$tdir/foo || error "append bar failed"
5324         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5325         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5326         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5327                                         error "link worked"
5328         echo foo >> $DIR/$tdir/foo || error "append foo failed"
5329         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5330         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
5331                                                      error "lsattr"
5332         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
5333         cp -r $DIR/$tdir $TMP/
5334         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
5335 }
5336 run_test 52a "append-only flag test (should return errors)"
5337
5338 test_52b() {
5339         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
5340         test_mkdir $DIR/$tdir
5341         touch $DIR/$tdir/foo
5342         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
5343         cat test > $DIR/$tdir/foo && error "cat test worked"
5344         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5345         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5346         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5347                                         error "link worked"
5348         echo foo >> $DIR/$tdir/foo && error "echo worked"
5349         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5350         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
5351         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
5352         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
5353                                                         error "lsattr"
5354         chattr -i $DIR/$tdir/foo || error "chattr failed"
5355
5356         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
5357 }
5358 run_test 52b "immutable flag test (should return errors) ======="
5359
5360 test_53() {
5361         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5362         remote_mds_nodsh && skip "remote MDS with nodsh"
5363         remote_ost_nodsh && skip "remote OST with nodsh"
5364
5365         local param
5366         local param_seq
5367         local ostname
5368         local mds_last
5369         local mds_last_seq
5370         local ost_last
5371         local ost_last_seq
5372         local ost_last_id
5373         local ostnum
5374         local node
5375         local found=false
5376         local support_last_seq=true
5377
5378         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
5379                 support_last_seq=false
5380
5381         # only test MDT0000
5382         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
5383         local value
5384         for value in $(do_facet $SINGLEMDS \
5385                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
5386                 param=$(echo ${value[0]} | cut -d "=" -f1)
5387                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
5388
5389                 if $support_last_seq; then
5390                         param_seq=$(echo $param |
5391                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
5392                         mds_last_seq=$(do_facet $SINGLEMDS \
5393                                        $LCTL get_param -n $param_seq)
5394                 fi
5395                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
5396
5397                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
5398                 node=$(facet_active_host ost$((ostnum+1)))
5399                 param="obdfilter.$ostname.last_id"
5400                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
5401                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
5402                         ost_last_id=$ost_last
5403
5404                         if $support_last_seq; then
5405                                 ost_last_id=$(echo $ost_last |
5406                                               awk -F':' '{print $2}' |
5407                                               sed -e "s/^0x//g")
5408                                 ost_last_seq=$(echo $ost_last |
5409                                                awk -F':' '{print $1}')
5410                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
5411                         fi
5412
5413                         if [[ $ost_last_id != $mds_last ]]; then
5414                                 error "$ost_last_id != $mds_last"
5415                         else
5416                                 found=true
5417                                 break
5418                         fi
5419                 done
5420         done
5421         $found || error "can not match last_seq/last_id for $mdtosc"
5422         return 0
5423 }
5424 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
5425
5426 test_54a() {
5427         perl -MSocket -e ';' || skip "no Socket perl module installed"
5428
5429         $SOCKETSERVER $DIR/socket ||
5430                 error "$SOCKETSERVER $DIR/socket failed: $?"
5431         $SOCKETCLIENT $DIR/socket ||
5432                 error "$SOCKETCLIENT $DIR/socket failed: $?"
5433         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
5434 }
5435 run_test 54a "unix domain socket test =========================="
5436
5437 test_54b() {
5438         f="$DIR/f54b"
5439         mknod $f c 1 3
5440         chmod 0666 $f
5441         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
5442 }
5443 run_test 54b "char device works in lustre ======================"
5444
5445 find_loop_dev() {
5446         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
5447         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
5448         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
5449
5450         for i in $(seq 3 7); do
5451                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
5452                 LOOPDEV=$LOOPBASE$i
5453                 LOOPNUM=$i
5454                 break
5455         done
5456 }
5457
5458 cleanup_54c() {
5459         local rc=0
5460         loopdev="$DIR/loop54c"
5461
5462         trap 0
5463         $UMOUNT $DIR/$tdir || rc=$?
5464         losetup -d $loopdev || true
5465         losetup -d $LOOPDEV || true
5466         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
5467         return $rc
5468 }
5469
5470 test_54c() {
5471         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5472
5473         loopdev="$DIR/loop54c"
5474
5475         find_loop_dev
5476         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
5477         trap cleanup_54c EXIT
5478         mknod $loopdev b 7 $LOOPNUM
5479         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
5480         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
5481         losetup $loopdev $DIR/$tfile ||
5482                 error "can't set up $loopdev for $DIR/$tfile"
5483         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
5484         test_mkdir $DIR/$tdir
5485         mount -t ext2 $loopdev $DIR/$tdir ||
5486                 error "error mounting $loopdev on $DIR/$tdir"
5487         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
5488                 error "dd write"
5489         df $DIR/$tdir
5490         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
5491                 error "dd read"
5492         cleanup_54c
5493 }
5494 run_test 54c "block device works in lustre ====================="
5495
5496 test_54d() {
5497         f="$DIR/f54d"
5498         string="aaaaaa"
5499         mknod $f p
5500         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
5501 }
5502 run_test 54d "fifo device works in lustre ======================"
5503
5504 test_54e() {
5505         f="$DIR/f54e"
5506         string="aaaaaa"
5507         cp -aL /dev/console $f
5508         echo $string > $f || error "echo $string to $f failed"
5509 }
5510 run_test 54e "console/tty device works in lustre ======================"
5511
5512 test_56a() {
5513         local numfiles=3
5514         local dir=$DIR/$tdir
5515
5516         rm -rf $dir
5517         test_mkdir -p $dir/dir
5518         for i in $(seq $numfiles); do
5519                 touch $dir/file$i
5520                 touch $dir/dir/file$i
5521         done
5522
5523         local numcomp=$($LFS getstripe --component-count $dir)
5524
5525         [[ $numcomp == 0 ]] && numcomp=1
5526
5527         # test lfs getstripe with --recursive
5528         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
5529
5530         [[ $filenum -eq $((numfiles * 2)) ]] ||
5531                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
5532         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
5533         [[ $filenum -eq $numfiles ]] ||
5534                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
5535         echo "$LFS getstripe showed obdidx or l_ost_idx"
5536
5537         # test lfs getstripe with file instead of dir
5538         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
5539         [[ $filenum -eq 1 ]] ||
5540                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
5541         echo "$LFS getstripe file1 passed"
5542
5543         #test lfs getstripe with --verbose
5544         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
5545         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5546                 error "$LFS getstripe --verbose $dir: "\
5547                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
5548         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
5549                 error "$LFS getstripe $dir: showed lmm_magic"
5550
5551         #test lfs getstripe with -v prints lmm_fid
5552         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
5553         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5554                 error "$LFS getstripe -v $dir: "\
5555                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
5556         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
5557                 error "$LFS getstripe $dir: showed lmm_fid by default"
5558         echo "$LFS getstripe --verbose passed"
5559
5560         #check for FID information
5561         local fid1=$($LFS getstripe --fid $dir/file1)
5562         local fid2=$($LFS getstripe --verbose $dir/file1 |
5563                      awk '/lmm_fid: / { print $2; exit; }')
5564         local fid3=$($LFS path2fid $dir/file1)
5565
5566         [ "$fid1" != "$fid2" ] &&
5567                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
5568         [ "$fid1" != "$fid3" ] &&
5569                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
5570         echo "$LFS getstripe --fid passed"
5571
5572         #test lfs getstripe with --obd
5573         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
5574                 error "$LFS getstripe --obd wrong_uuid: should return error"
5575
5576         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5577
5578         local ostidx=1
5579         local obduuid=$(ostuuid_from_index $ostidx)
5580         local found=$($LFS getstripe -r --obd $obduuid $dir |
5581                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
5582
5583         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
5584         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
5585                 ((filenum--))
5586         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
5587                 ((filenum--))
5588
5589         [[ $found -eq $filenum ]] ||
5590                 error "$LFS getstripe --obd: found $found expect $filenum"
5591         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
5592                 sed '/^[         ]*'${ostidx}'[  ]/d' |
5593                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
5594                 error "$LFS getstripe --obd: should not show file on other obd"
5595         echo "$LFS getstripe --obd passed"
5596 }
5597 run_test 56a "check $LFS getstripe"
5598
5599 test_56b() {
5600         local dir=$DIR/$tdir
5601         local numdirs=3
5602
5603         test_mkdir $dir
5604         for i in $(seq $numdirs); do
5605                 test_mkdir $dir/dir$i
5606         done
5607
5608         # test lfs getdirstripe default mode is non-recursion, which is
5609         # different from lfs getstripe
5610         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
5611
5612         [[ $dircnt -eq 1 ]] ||
5613                 error "$LFS getdirstripe: found $dircnt, not 1"
5614         dircnt=$($LFS getdirstripe --recursive $dir |
5615                 grep -c lmv_stripe_count)
5616         [[ $dircnt -eq $((numdirs + 1)) ]] ||
5617                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
5618 }
5619 run_test 56b "check $LFS getdirstripe"
5620
5621 test_56c() {
5622         remote_ost_nodsh && skip "remote OST with nodsh"
5623
5624         local ost_idx=0
5625         local ost_name=$(ostname_from_index $ost_idx)
5626         local old_status=$(ost_dev_status $ost_idx)
5627         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
5628
5629         [[ -z "$old_status" ]] ||
5630                 skip_env "OST $ost_name is in $old_status status"
5631
5632         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
5633         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
5634                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
5635         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
5636                 save_lustre_params ost1 osd-*.$ost_name.nonrotational > $p
5637                 do_facet ost1 $LCTL set_param -n osd-*.$ost_name.nonrotational=1
5638         fi
5639
5640         [[ $($LFS df -v $MOUNT |& grep -c "inactive device") -eq 0 ]] ||
5641                 error "$LFS df -v showing inactive devices"
5642         sleep_maxage
5643
5644         local new_status=$(ost_dev_status $ost_idx $MOUNT -v)
5645
5646         [[ "$new_status" =~ "D" ]] ||
5647                 error "$ost_name status is '$new_status', missing 'D'"
5648         if [[ $OST1_VERSION -ge $(version_code 2.12.55) ]]; then
5649                 [[ "$new_status" =~ "N" ]] ||
5650                         error "$ost_name status is '$new_status', missing 'N'"
5651         fi
5652         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
5653                 [[ "$new_status" =~ "f" ]] ||
5654                         error "$ost_name status is '$new_status', missing 'f'"
5655         fi
5656
5657         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
5658         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
5659                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
5660         [[ -z "$p" ]] && restore_lustre_params < $p || true
5661         sleep_maxage
5662
5663         new_status=$(ost_dev_status $ost_idx)
5664         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
5665                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
5666         # can't check 'f' as devices may actually be on flash
5667 }
5668 run_test 56c "check 'lfs df' showing device status"
5669
5670 test_56d() {
5671         local mdts=$($LFS df -v $MOUNT | grep -c MDT)
5672         local osts=$($LFS df -v $MOUNT | grep -c OST)
5673
5674         $LFS df $MOUNT
5675
5676         (( mdts == MDSCOUNT )) ||
5677                 error "lfs df -v showed $mdts MDTs, not $MDSCOUNT"
5678         (( osts == OSTCOUNT )) ||
5679                 error "lfs df -v showed $osts OSTs, not $OSTCOUNT"
5680 }
5681 run_test 56d "'lfs df -v' prints only configured devices"
5682
5683 NUMFILES=3
5684 NUMDIRS=3
5685 setup_56() {
5686         local local_tdir="$1"
5687         local local_numfiles="$2"
5688         local local_numdirs="$3"
5689         local dir_params="$4"
5690         local dir_stripe_params="$5"
5691
5692         if [ ! -d "$local_tdir" ] ; then
5693                 test_mkdir -p $dir_stripe_params $local_tdir
5694                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
5695                 for i in $(seq $local_numfiles) ; do
5696                         touch $local_tdir/file$i
5697                 done
5698                 for i in $(seq $local_numdirs) ; do
5699                         test_mkdir $dir_stripe_params $local_tdir/dir$i
5700                         for j in $(seq $local_numfiles) ; do
5701                                 touch $local_tdir/dir$i/file$j
5702                         done
5703                 done
5704         fi
5705 }
5706
5707 setup_56_special() {
5708         local local_tdir=$1
5709         local local_numfiles=$2
5710         local local_numdirs=$3
5711
5712         setup_56 $local_tdir $local_numfiles $local_numdirs
5713
5714         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
5715                 for i in $(seq $local_numfiles) ; do
5716                         mknod $local_tdir/loop${i}b b 7 $i
5717                         mknod $local_tdir/null${i}c c 1 3
5718                         ln -s $local_tdir/file1 $local_tdir/link${i}
5719                 done
5720                 for i in $(seq $local_numdirs) ; do
5721                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
5722                         mknod $local_tdir/dir$i/null${i}c c 1 3
5723                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
5724                 done
5725         fi
5726 }
5727
5728 test_56g() {
5729         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5730         local expected=$(($NUMDIRS + 2))
5731
5732         setup_56 $dir $NUMFILES $NUMDIRS
5733
5734         # test lfs find with -name
5735         for i in $(seq $NUMFILES) ; do
5736                 local nums=$($LFS find -name "*$i" $dir | wc -l)
5737
5738                 [ $nums -eq $expected ] ||
5739                         error "lfs find -name '*$i' $dir wrong: "\
5740                               "found $nums, expected $expected"
5741         done
5742 }
5743 run_test 56g "check lfs find -name"
5744
5745 test_56h() {
5746         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5747         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
5748
5749         setup_56 $dir $NUMFILES $NUMDIRS
5750
5751         # test lfs find with ! -name
5752         for i in $(seq $NUMFILES) ; do
5753                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
5754
5755                 [ $nums -eq $expected ] ||
5756                         error "lfs find ! -name '*$i' $dir wrong: "\
5757                               "found $nums, expected $expected"
5758         done
5759 }
5760 run_test 56h "check lfs find ! -name"
5761
5762 test_56i() {
5763         local dir=$DIR/$tdir
5764
5765         test_mkdir $dir
5766
5767         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
5768         local out=$($cmd)
5769
5770         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
5771 }
5772 run_test 56i "check 'lfs find -ost UUID' skips directories"
5773
5774 test_56j() {
5775         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5776
5777         setup_56_special $dir $NUMFILES $NUMDIRS
5778
5779         local expected=$((NUMDIRS + 1))
5780         local cmd="$LFS find -type d $dir"
5781         local nums=$($cmd | wc -l)
5782
5783         [ $nums -eq $expected ] ||
5784                 error "'$cmd' wrong: found $nums, expected $expected"
5785 }
5786 run_test 56j "check lfs find -type d"
5787
5788 test_56k() {
5789         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5790
5791         setup_56_special $dir $NUMFILES $NUMDIRS
5792
5793         local expected=$(((NUMDIRS + 1) * NUMFILES))
5794         local cmd="$LFS find -type f $dir"
5795         local nums=$($cmd | wc -l)
5796
5797         [ $nums -eq $expected ] ||
5798                 error "'$cmd' wrong: found $nums, expected $expected"
5799 }
5800 run_test 56k "check lfs find -type f"
5801
5802 test_56l() {
5803         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5804
5805         setup_56_special $dir $NUMFILES $NUMDIRS
5806
5807         local expected=$((NUMDIRS + NUMFILES))
5808         local cmd="$LFS find -type b $dir"
5809         local nums=$($cmd | wc -l)
5810
5811         [ $nums -eq $expected ] ||
5812                 error "'$cmd' wrong: found $nums, expected $expected"
5813 }
5814 run_test 56l "check lfs find -type b"
5815
5816 test_56m() {
5817         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5818
5819         setup_56_special $dir $NUMFILES $NUMDIRS
5820
5821         local expected=$((NUMDIRS + NUMFILES))
5822         local cmd="$LFS find -type c $dir"
5823         local nums=$($cmd | wc -l)
5824         [ $nums -eq $expected ] ||
5825                 error "'$cmd' wrong: found $nums, expected $expected"
5826 }
5827 run_test 56m "check lfs find -type c"
5828
5829 test_56n() {
5830         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5831         setup_56_special $dir $NUMFILES $NUMDIRS
5832
5833         local expected=$((NUMDIRS + NUMFILES))
5834         local cmd="$LFS find -type l $dir"
5835         local nums=$($cmd | wc -l)
5836
5837         [ $nums -eq $expected ] ||
5838                 error "'$cmd' wrong: found $nums, expected $expected"
5839 }
5840 run_test 56n "check lfs find -type l"
5841
5842 test_56o() {
5843         local dir=$DIR/$tdir
5844
5845         setup_56 $dir $NUMFILES $NUMDIRS
5846         utime $dir/file1 > /dev/null || error "utime (1)"
5847         utime $dir/file2 > /dev/null || error "utime (2)"
5848         utime $dir/dir1 > /dev/null || error "utime (3)"
5849         utime $dir/dir2 > /dev/null || error "utime (4)"
5850         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5851         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5852
5853         local expected=4
5854         local nums=$($LFS find -mtime +0 $dir | wc -l)
5855
5856         [ $nums -eq $expected ] ||
5857                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5858
5859         expected=12
5860         cmd="$LFS find -mtime 0 $dir"
5861         nums=$($cmd | wc -l)
5862         [ $nums -eq $expected ] ||
5863                 error "'$cmd' wrong: found $nums, expected $expected"
5864 }
5865 run_test 56o "check lfs find -mtime for old files"
5866
5867 test_56ob() {
5868         local dir=$DIR/$tdir
5869         local expected=1
5870         local count=0
5871
5872         # just to make sure there is something that won't be found
5873         test_mkdir $dir
5874         touch $dir/$tfile.now
5875
5876         for age in year week day hour min; do
5877                 count=$((count + 1))
5878
5879                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
5880                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
5881                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
5882
5883                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
5884                 local nums=$($cmd | wc -l)
5885                 [ $nums -eq $expected ] ||
5886                         error "'$cmd' wrong: found $nums, expected $expected"
5887
5888                 cmd="$LFS find $dir -atime $count${age:0:1}"
5889                 nums=$($cmd | wc -l)
5890                 [ $nums -eq $expected ] ||
5891                         error "'$cmd' wrong: found $nums, expected $expected"
5892         done
5893
5894         sleep 2
5895         cmd="$LFS find $dir -ctime +1s -type f"
5896         nums=$($cmd | wc -l)
5897         (( $nums == $count * 2 + 1)) ||
5898                 error "'$cmd' wrong: found $nums, expected $((expected*2+1))"
5899 }
5900 run_test 56ob "check lfs find -atime -mtime -ctime with units"
5901
5902 test_newerXY_base() {
5903         local x=$1
5904         local y=$2
5905         local dir=$DIR/$tdir
5906         local ref
5907         local negref
5908
5909         if [ $y == "t" ]; then
5910                 if [ $x == "b" ]; then
5911                         ref="\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\""
5912                 else
5913                         ref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5914                 fi
5915         else
5916                 ref=$DIR/$tfile.newer.$x$y
5917                 touch $ref || error "touch $ref failed"
5918         fi
5919         sleep 2
5920         setup_56 $dir $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
5921         sleep 2
5922         if [ $y == "t" ]; then
5923                 if [ $x == "b" ]; then
5924                         negref="\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\""
5925                 else
5926                         negref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5927                 fi
5928         else
5929                 negref=$DIR/$tfile.negnewer.$x$y
5930                 touch $negref || error "touch $negref failed"
5931         fi
5932
5933         local cmd="$LFS find $dir -newer$x$y $ref"
5934         local nums=$(eval $cmd | wc -l)
5935         local expected=$(((NUMFILES + 2) * NUMDIRS + 1))
5936
5937         [ $nums -eq $expected ] ||
5938                 error "'$cmd' wrong: found $nums, expected $expected"
5939
5940         cmd="$LFS find $dir ! -newer$x$y $negref"
5941         nums=$(eval $cmd | wc -l)
5942         [ $nums -eq $expected ] ||
5943                 error "'$cmd' wrong: found $nums, expected $expected"
5944
5945         cmd="$LFS find $dir -newer$x$y $ref ! -newer$x$y $negref"
5946         nums=$(eval $cmd | wc -l)
5947         [ $nums -eq $expected ] ||
5948                 error "'$cmd' wrong: found $nums, expected $expected"
5949
5950         rm -rf $DIR/*
5951 }
5952
5953 test_56oc() {
5954         test_newerXY_base "b" "t"
5955         test_newerXY_base "a" "a"
5956         test_newerXY_base "a" "m"
5957         test_newerXY_base "a" "c"
5958         test_newerXY_base "m" "a"
5959         test_newerXY_base "m" "m"
5960         test_newerXY_base "m" "c"
5961         test_newerXY_base "c" "a"
5962         test_newerXY_base "c" "m"
5963         test_newerXY_base "c" "c"
5964         test_newerXY_base "b" "b"
5965         test_newerXY_base "a" "t"
5966         test_newerXY_base "m" "t"
5967         test_newerXY_base "c" "t"
5968         test_newerXY_base "b" "t"
5969 }
5970 run_test 56oc "check lfs find -newerXY work"
5971
5972 btime_supported() {
5973         local dir=$DIR/$tdir
5974         local rc
5975
5976         mkdir -p $dir
5977         touch $dir/$tfile
5978         $LFS find $dir -btime -1d -type f
5979         rc=$?
5980         rm -rf $dir
5981         return $rc
5982 }
5983
5984 test_56od() {
5985         [ $MDS1_VERSION -lt $(version_code 2.13.53) ] &&
5986                 ! btime_supported && skip "btime unsupported on MDS"
5987
5988         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
5989                 ! btime_supported && skip "btime unsupported on clients"
5990
5991         local dir=$DIR/$tdir
5992         local ref=$DIR/$tfile.ref
5993         local negref=$DIR/$tfile.negref
5994
5995         mkdir $dir || error "mkdir $dir failed"
5996         touch $dir/$tfile.n1 || error "touch $dir/$tfile.n1 failed"
5997         touch $dir/$tfile.n2 || error "touch $dir/$tfile.n2 failed"
5998         mkdir $dir/$tdir.n1 || error "mkdir $dir/$tdir.n1 failed"
5999         mkdir $dir/$tdir.n2 || error "mkdir $dir/$tdir.n2 failed"
6000         touch $ref || error "touch $ref failed"
6001         # sleep 3 seconds at least
6002         sleep 3
6003
6004         local before=$(do_facet mds1 date +%s)
6005         local skew=$(($(date +%s) - before + 1))
6006
6007         if (( skew < 0 && skew > -5 )); then
6008                 sleep $((0 - skew + 1))
6009                 skew=0
6010         fi
6011
6012         # Set the dir stripe params to limit files all on MDT0,
6013         # otherwise we need to calc the max clock skew between
6014         # the client and MDTs.
6015         setup_56 $dir/d.btime $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
6016         sleep 2
6017         touch $negref || error "touch $negref failed"
6018
6019         local cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type f"
6020         local nums=$($cmd | wc -l)
6021         local expected=$(((NUMFILES + 1) * NUMDIRS))
6022
6023         [ $nums -eq $expected ] ||
6024                 error "'$cmd' wrong: found $nums, expected $expected"
6025
6026         cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type d"
6027         nums=$($cmd | wc -l)
6028         expected=$((NUMFILES + 1))
6029         [ $nums -eq $expected ] ||
6030                 error "'$cmd' wrong: found $nums, expected $expected"
6031
6032         [ $skew -lt 0 ] && return
6033
6034         local after=$(do_facet mds1 date +%s)
6035         local age=$((after - before + 1 + skew))
6036
6037         cmd="$LFS find $dir -btime -${age}s -type f"
6038         nums=$($cmd | wc -l)
6039         expected=$(((NUMFILES + 1) * NUMDIRS))
6040
6041         echo "Clock skew between client and server: $skew, age:$age"
6042         [ $nums -eq $expected ] ||
6043                 error "'$cmd' wrong: found $nums, expected $expected"
6044
6045         expected=$(($NUMDIRS + 1))
6046         cmd="$LFS find $dir -btime -${age}s -type d"
6047         nums=$($cmd | wc -l)
6048         [ $nums -eq $expected ] ||
6049                 error "'$cmd' wrong: found $nums, expected $expected"
6050         rm -f $ref $negref || error "Failed to remove $ref $negref"
6051 }
6052 run_test 56od "check lfs find -btime with units"
6053
6054 test_56p() {
6055         [ $RUNAS_ID -eq $UID ] &&
6056                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6057
6058         local dir=$DIR/$tdir
6059
6060         setup_56 $dir $NUMFILES $NUMDIRS
6061         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
6062
6063         local expected=$NUMFILES
6064         local cmd="$LFS find -uid $RUNAS_ID $dir"
6065         local nums=$($cmd | wc -l)
6066
6067         [ $nums -eq $expected ] ||
6068                 error "'$cmd' wrong: found $nums, expected $expected"
6069
6070         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
6071         cmd="$LFS find ! -uid $RUNAS_ID $dir"
6072         nums=$($cmd | wc -l)
6073         [ $nums -eq $expected ] ||
6074                 error "'$cmd' wrong: found $nums, expected $expected"
6075 }
6076 run_test 56p "check lfs find -uid and ! -uid"
6077
6078 test_56q() {
6079         [ $RUNAS_ID -eq $UID ] &&
6080                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6081
6082         local dir=$DIR/$tdir
6083
6084         setup_56 $dir $NUMFILES $NUMDIRS
6085         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
6086
6087         local expected=$NUMFILES
6088         local cmd="$LFS find -gid $RUNAS_GID $dir"
6089         local nums=$($cmd | wc -l)
6090
6091         [ $nums -eq $expected ] ||
6092                 error "'$cmd' wrong: found $nums, expected $expected"
6093
6094         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
6095         cmd="$LFS find ! -gid $RUNAS_GID $dir"
6096         nums=$($cmd | wc -l)
6097         [ $nums -eq $expected ] ||
6098                 error "'$cmd' wrong: found $nums, expected $expected"
6099 }
6100 run_test 56q "check lfs find -gid and ! -gid"
6101
6102 test_56r() {
6103         local dir=$DIR/$tdir
6104
6105         setup_56 $dir $NUMFILES $NUMDIRS
6106
6107         local expected=12
6108         local cmd="$LFS find -size 0 -type f -lazy $dir"
6109         local nums=$($cmd | wc -l)
6110
6111         [ $nums -eq $expected ] ||
6112                 error "'$cmd' wrong: found $nums, expected $expected"
6113         cmd="$LFS find -size 0 -type f $dir"
6114         nums=$($cmd | wc -l)
6115         [ $nums -eq $expected ] ||
6116                 error "'$cmd' wrong: found $nums, expected $expected"
6117
6118         expected=0
6119         cmd="$LFS find ! -size 0 -type f -lazy $dir"
6120         nums=$($cmd | wc -l)
6121         [ $nums -eq $expected ] ||
6122                 error "'$cmd' wrong: found $nums, expected $expected"
6123         cmd="$LFS find ! -size 0 -type f $dir"
6124         nums=$($cmd | wc -l)
6125         [ $nums -eq $expected ] ||
6126                 error "'$cmd' wrong: found $nums, expected $expected"
6127
6128         echo "test" > $dir/$tfile
6129         echo "test2" > $dir/$tfile.2 && sync
6130         expected=1
6131         cmd="$LFS find -size 5 -type f -lazy $dir"
6132         nums=$($cmd | wc -l)
6133         [ $nums -eq $expected ] ||
6134                 error "'$cmd' wrong: found $nums, expected $expected"
6135         cmd="$LFS find -size 5 -type f $dir"
6136         nums=$($cmd | wc -l)
6137         [ $nums -eq $expected ] ||
6138                 error "'$cmd' wrong: found $nums, expected $expected"
6139
6140         expected=1
6141         cmd="$LFS find -size +5 -type f -lazy $dir"
6142         nums=$($cmd | wc -l)
6143         [ $nums -eq $expected ] ||
6144                 error "'$cmd' wrong: found $nums, expected $expected"
6145         cmd="$LFS find -size +5 -type f $dir"
6146         nums=$($cmd | wc -l)
6147         [ $nums -eq $expected ] ||
6148                 error "'$cmd' wrong: found $nums, expected $expected"
6149
6150         expected=2
6151         cmd="$LFS find -size +0 -type f -lazy $dir"
6152         nums=$($cmd | wc -l)
6153         [ $nums -eq $expected ] ||
6154                 error "'$cmd' wrong: found $nums, expected $expected"
6155         cmd="$LFS find -size +0 -type f $dir"
6156         nums=$($cmd | wc -l)
6157         [ $nums -eq $expected ] ||
6158                 error "'$cmd' wrong: found $nums, expected $expected"
6159
6160         expected=2
6161         cmd="$LFS find ! -size -5 -type f -lazy $dir"
6162         nums=$($cmd | wc -l)
6163         [ $nums -eq $expected ] ||
6164                 error "'$cmd' wrong: found $nums, expected $expected"
6165         cmd="$LFS find ! -size -5 -type f $dir"
6166         nums=$($cmd | wc -l)
6167         [ $nums -eq $expected ] ||
6168                 error "'$cmd' wrong: found $nums, expected $expected"
6169
6170         expected=12
6171         cmd="$LFS find -size -5 -type f -lazy $dir"
6172         nums=$($cmd | wc -l)
6173         [ $nums -eq $expected ] ||
6174                 error "'$cmd' wrong: found $nums, expected $expected"
6175         cmd="$LFS find -size -5 -type f $dir"
6176         nums=$($cmd | wc -l)
6177         [ $nums -eq $expected ] ||
6178                 error "'$cmd' wrong: found $nums, expected $expected"
6179 }
6180 run_test 56r "check lfs find -size works"
6181
6182 test_56ra_sub() {
6183         local expected=$1
6184         local glimpses=$2
6185         local cmd="$3"
6186
6187         cancel_lru_locks $OSC
6188
6189         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6190         local nums=$($cmd | wc -l)
6191
6192         [ $nums -eq $expected ] ||
6193                 error "'$cmd' wrong: found $nums, expected $expected"
6194
6195         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6196
6197         if (( rpcs_before + glimpses != rpcs_after )); then
6198                 echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6199                 $LCTL get_param osc.*.stats | grep ldlm_glimpse_enqueue
6200
6201                 if [[ $glimpses == 0 ]]; then
6202                         error "'$cmd' should not send glimpse RPCs to OST"
6203                 else
6204                         error "'$cmd' should send $glimpses glimpse RPCs to OST"
6205                 fi
6206         fi
6207 }
6208
6209 test_56ra() {
6210         [[ $MDS1_VERSION -gt $(version_code 2.12.58) ]] ||
6211                 skip "MDS < 2.12.58 doesn't return LSOM data"
6212         local dir=$DIR/$tdir
6213
6214         [[ $OSC == "mdc" ]] && skip "DoM files" && return
6215
6216         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6217         # open and close all files to ensure LSOM is updated
6218         cancel_lru_locks $OSC
6219         find $dir -type f | xargs cat > /dev/null
6220
6221         #   expect_found  glimpse_rpcs  command_to_run
6222         test_56ra_sub 12  0 "$LFS find -size 0 -type f -lazy $dir"
6223         test_56ra_sub 12 12 "$LFS find -size 0 -type f $dir"
6224         test_56ra_sub  0  0 "$LFS find ! -size 0 -type f -lazy $dir"
6225         test_56ra_sub  0 12 "$LFS find ! -size 0 -type f $dir"
6226
6227         echo "test" > $dir/$tfile
6228         echo "test2" > $dir/$tfile.2 && sync
6229         cancel_lru_locks $OSC
6230         cat $dir/$tfile $dir/$tfile.2 > /dev/null
6231
6232         test_56ra_sub  1  0 "$LFS find -size 5 -type f -lazy $dir"
6233         test_56ra_sub  1 14 "$LFS find -size 5 -type f $dir"
6234         test_56ra_sub  1  0 "$LFS find -size +5 -type f -lazy $dir"
6235         test_56ra_sub  1 14 "$LFS find -size +5 -type f $dir"
6236
6237         test_56ra_sub  2  0 "$LFS find -size +0 -type f -lazy $dir"
6238         test_56ra_sub  2 14 "$LFS find -size +0 -type f $dir"
6239         test_56ra_sub  2  0 "$LFS find ! -size -5 -type f -lazy $dir"
6240         test_56ra_sub  2 14 "$LFS find ! -size -5 -type f $dir"
6241         test_56ra_sub 12  0 "$LFS find -size -5 -type f -lazy $dir"
6242         test_56ra_sub 12 14 "$LFS find -size -5 -type f $dir"
6243 }
6244 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
6245
6246 test_56rb() {
6247         local dir=$DIR/$tdir
6248         local tmp=$TMP/$tfile.log
6249         local mdt_idx;
6250
6251         test_mkdir -p $dir || error "failed to mkdir $dir"
6252         $LFS setstripe -c 1 -i 0 $dir/$tfile ||
6253                 error "failed to setstripe $dir/$tfile"
6254         dd if=/dev/zero of=$dir/$tfile bs=1M count=1
6255
6256         stack_trap "rm -f $tmp" EXIT
6257         $LFS find --size +100K --ost 0 $dir 2>&1 | tee $tmp
6258         [ -z "$(cat $tmp | grep "obd_uuid: ")" ] ||
6259                 error "failed to find --size +100K --ost 0 $dir"
6260         $LFS find --size +100K --mdt $mdt_idx $dir 2>&1 | tee $tmp
6261         [ -z "$(cat $tmp | grep "obd_uuid: ")" ] ||
6262                 error "failed to find --size +100K --mdt $mdt_idx $dir"
6263 }
6264 run_test 56rb "check lfs find --size --ost/--mdt works"
6265
6266 test_56s() { # LU-611 #LU-9369
6267         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
6268
6269         local dir=$DIR/$tdir
6270         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
6271
6272         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6273         for i in $(seq $NUMDIRS); do
6274                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
6275         done
6276
6277         local expected=$NUMDIRS
6278         local cmd="$LFS find -c $OSTCOUNT $dir"
6279         local nums=$($cmd | wc -l)
6280
6281         [ $nums -eq $expected ] || {
6282                 $LFS getstripe -R $dir
6283                 error "'$cmd' wrong: found $nums, expected $expected"
6284         }
6285
6286         expected=$((NUMDIRS + onestripe))
6287         cmd="$LFS find -stripe-count +0 -type f $dir"
6288         nums=$($cmd | wc -l)
6289         [ $nums -eq $expected ] || {
6290                 $LFS getstripe -R $dir
6291                 error "'$cmd' wrong: found $nums, expected $expected"
6292         }
6293
6294         expected=$onestripe
6295         cmd="$LFS find -stripe-count 1 -type f $dir"
6296         nums=$($cmd | wc -l)
6297         [ $nums -eq $expected ] || {
6298                 $LFS getstripe -R $dir
6299                 error "'$cmd' wrong: found $nums, expected $expected"
6300         }
6301
6302         cmd="$LFS find -stripe-count -2 -type f $dir"
6303         nums=$($cmd | wc -l)
6304         [ $nums -eq $expected ] || {
6305                 $LFS getstripe -R $dir
6306                 error "'$cmd' wrong: found $nums, expected $expected"
6307         }
6308
6309         expected=0
6310         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
6311         nums=$($cmd | wc -l)
6312         [ $nums -eq $expected ] || {
6313                 $LFS getstripe -R $dir
6314                 error "'$cmd' wrong: found $nums, expected $expected"
6315         }
6316 }
6317 run_test 56s "check lfs find -stripe-count works"
6318
6319 test_56t() { # LU-611 #LU-9369
6320         local dir=$DIR/$tdir
6321
6322         setup_56 $dir 0 $NUMDIRS
6323         for i in $(seq $NUMDIRS); do
6324                 $LFS setstripe -S 8M $dir/dir$i/$tfile
6325         done
6326
6327         local expected=$NUMDIRS
6328         local cmd="$LFS find -S 8M $dir"
6329         local nums=$($cmd | wc -l)
6330
6331         [ $nums -eq $expected ] || {
6332                 $LFS getstripe -R $dir
6333                 error "'$cmd' wrong: found $nums, expected $expected"
6334         }
6335         rm -rf $dir
6336
6337         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
6338
6339         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
6340
6341         expected=$(((NUMDIRS + 1) * NUMFILES))
6342         cmd="$LFS find -stripe-size 512k -type f $dir"
6343         nums=$($cmd | wc -l)
6344         [ $nums -eq $expected ] ||
6345                 error "'$cmd' wrong: found $nums, expected $expected"
6346
6347         cmd="$LFS find -stripe-size +320k -type f $dir"
6348         nums=$($cmd | wc -l)
6349         [ $nums -eq $expected ] ||
6350                 error "'$cmd' wrong: found $nums, expected $expected"
6351
6352         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
6353         cmd="$LFS find -stripe-size +200k -type f $dir"
6354         nums=$($cmd | wc -l)
6355         [ $nums -eq $expected ] ||
6356                 error "'$cmd' wrong: found $nums, expected $expected"
6357
6358         cmd="$LFS find -stripe-size -640k -type f $dir"
6359         nums=$($cmd | wc -l)
6360         [ $nums -eq $expected ] ||
6361                 error "'$cmd' wrong: found $nums, expected $expected"
6362
6363         expected=4
6364         cmd="$LFS find -stripe-size 256k -type f $dir"
6365         nums=$($cmd | wc -l)
6366         [ $nums -eq $expected ] ||
6367                 error "'$cmd' wrong: found $nums, expected $expected"
6368
6369         cmd="$LFS find -stripe-size -320k -type f $dir"
6370         nums=$($cmd | wc -l)
6371         [ $nums -eq $expected ] ||
6372                 error "'$cmd' wrong: found $nums, expected $expected"
6373
6374         expected=0
6375         cmd="$LFS find -stripe-size 1024k -type f $dir"
6376         nums=$($cmd | wc -l)
6377         [ $nums -eq $expected ] ||
6378                 error "'$cmd' wrong: found $nums, expected $expected"
6379 }
6380 run_test 56t "check lfs find -stripe-size works"
6381
6382 test_56u() { # LU-611
6383         local dir=$DIR/$tdir
6384
6385         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
6386
6387         if [[ $OSTCOUNT -gt 1 ]]; then
6388                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
6389                 onestripe=4
6390         else
6391                 onestripe=0
6392         fi
6393
6394         local expected=$(((NUMDIRS + 1) * NUMFILES))
6395         local cmd="$LFS find -stripe-index 0 -type f $dir"
6396         local nums=$($cmd | wc -l)
6397
6398         [ $nums -eq $expected ] ||
6399                 error "'$cmd' wrong: found $nums, expected $expected"
6400
6401         expected=$onestripe
6402         cmd="$LFS find -stripe-index 1 -type f $dir"
6403         nums=$($cmd | wc -l)
6404         [ $nums -eq $expected ] ||
6405                 error "'$cmd' wrong: found $nums, expected $expected"
6406
6407         cmd="$LFS find ! -stripe-index 0 -type f $dir"
6408         nums=$($cmd | wc -l)
6409         [ $nums -eq $expected ] ||
6410                 error "'$cmd' wrong: found $nums, expected $expected"
6411
6412         expected=0
6413         # This should produce an error and not return any files
6414         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
6415         nums=$($cmd 2>/dev/null | wc -l)
6416         [ $nums -eq $expected ] ||
6417                 error "'$cmd' wrong: found $nums, expected $expected"
6418
6419         if [[ $OSTCOUNT -gt 1 ]]; then
6420                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
6421                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
6422                 nums=$($cmd | wc -l)
6423                 [ $nums -eq $expected ] ||
6424                         error "'$cmd' wrong: found $nums, expected $expected"
6425         fi
6426 }
6427 run_test 56u "check lfs find -stripe-index works"
6428
6429 test_56v() {
6430         local mdt_idx=0
6431         local dir=$DIR/$tdir
6432
6433         setup_56 $dir $NUMFILES $NUMDIRS
6434
6435         UUID=$(mdtuuid_from_index $mdt_idx $dir)
6436         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
6437
6438         for file in $($LFS find -m $UUID $dir); do
6439                 file_midx=$($LFS getstripe -m $file)
6440                 [ $file_midx -eq $mdt_idx ] ||
6441                         error "lfs find -m $UUID != getstripe -m $file_midx"
6442         done
6443 }
6444 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
6445
6446 test_56w() {
6447         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6448         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6449
6450         local dir=$DIR/$tdir
6451
6452         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
6453
6454         local stripe_size=$($LFS getstripe -S -d $dir) ||
6455                 error "$LFS getstripe -S -d $dir failed"
6456         stripe_size=${stripe_size%% *}
6457
6458         local file_size=$((stripe_size * OSTCOUNT))
6459         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
6460         local required_space=$((file_num * file_size))
6461         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
6462                            head -n1)
6463         [[ $free_space -le $((required_space / 1024)) ]] &&
6464                 skip_env "need $required_space, have $free_space kbytes"
6465
6466         local dd_bs=65536
6467         local dd_count=$((file_size / dd_bs))
6468
6469         # write data into the files
6470         local i
6471         local j
6472         local file
6473
6474         for i in $(seq $NUMFILES); do
6475                 file=$dir/file$i
6476                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6477                         error "write data into $file failed"
6478         done
6479         for i in $(seq $NUMDIRS); do
6480                 for j in $(seq $NUMFILES); do
6481                         file=$dir/dir$i/file$j
6482                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6483                                 error "write data into $file failed"
6484                 done
6485         done
6486
6487         # $LFS_MIGRATE will fail if hard link migration is unsupported
6488         if [[ $MDS1_VERSION -gt $(version_code 2.5.55) ]]; then
6489                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
6490                         error "creating links to $dir/dir1/file1 failed"
6491         fi
6492
6493         local expected=-1
6494
6495         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
6496
6497         # lfs_migrate file
6498         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
6499
6500         echo "$cmd"
6501         eval $cmd || error "$cmd failed"
6502
6503         check_stripe_count $dir/file1 $expected
6504
6505         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
6506         then
6507                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
6508                 # OST 1 if it is on OST 0. This file is small enough to
6509                 # be on only one stripe.
6510                 file=$dir/migr_1_ost
6511                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
6512                         error "write data into $file failed"
6513                 local obdidx=$($LFS getstripe -i $file)
6514                 local oldmd5=$(md5sum $file)
6515                 local newobdidx=0
6516
6517                 [[ $obdidx -eq 0 ]] && newobdidx=1
6518                 cmd="$LFS migrate -i $newobdidx $file"
6519                 echo $cmd
6520                 eval $cmd || error "$cmd failed"
6521
6522                 local realobdix=$($LFS getstripe -i $file)
6523                 local newmd5=$(md5sum $file)
6524
6525                 [[ $newobdidx -ne $realobdix ]] &&
6526                         error "new OST is different (was=$obdidx, "\
6527                               "wanted=$newobdidx, got=$realobdix)"
6528                 [[ "$oldmd5" != "$newmd5" ]] &&
6529                         error "md5sum differ: $oldmd5, $newmd5"
6530         fi
6531
6532         # lfs_migrate dir
6533         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
6534         echo "$cmd"
6535         eval $cmd || error "$cmd failed"
6536
6537         for j in $(seq $NUMFILES); do
6538                 check_stripe_count $dir/dir1/file$j $expected
6539         done
6540
6541         # lfs_migrate works with lfs find
6542         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
6543              $LFS_MIGRATE -y -c $expected"
6544         echo "$cmd"
6545         eval $cmd || error "$cmd failed"
6546
6547         for i in $(seq 2 $NUMFILES); do
6548                 check_stripe_count $dir/file$i $expected
6549         done
6550         for i in $(seq 2 $NUMDIRS); do
6551                 for j in $(seq $NUMFILES); do
6552                 check_stripe_count $dir/dir$i/file$j $expected
6553                 done
6554         done
6555 }
6556 run_test 56w "check lfs_migrate -c stripe_count works"
6557
6558 test_56wb() {
6559         local file1=$DIR/$tdir/file1
6560         local create_pool=false
6561         local initial_pool=$($LFS getstripe -p $DIR)
6562         local pool_list=()
6563         local pool=""
6564
6565         echo -n "Creating test dir..."
6566         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6567         echo "done."
6568
6569         echo -n "Creating test file..."
6570         touch $file1 || error "cannot create file"
6571         echo "done."
6572
6573         echo -n "Detecting existing pools..."
6574         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
6575
6576         if [ ${#pool_list[@]} -gt 0 ]; then
6577                 echo "${pool_list[@]}"
6578                 for thispool in "${pool_list[@]}"; do
6579                         if [[ -z "$initial_pool" ||
6580                               "$initial_pool" != "$thispool" ]]; then
6581                                 pool="$thispool"
6582                                 echo "Using existing pool '$pool'"
6583                                 break
6584                         fi
6585                 done
6586         else
6587                 echo "none detected."
6588         fi
6589         if [ -z "$pool" ]; then
6590                 pool=${POOL:-testpool}
6591                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
6592                 echo -n "Creating pool '$pool'..."
6593                 create_pool=true
6594                 pool_add $pool &> /dev/null ||
6595                         error "pool_add failed"
6596                 echo "done."
6597
6598                 echo -n "Adding target to pool..."
6599                 pool_add_targets $pool 0 0 1 &> /dev/null ||
6600                         error "pool_add_targets failed"
6601                 echo "done."
6602         fi
6603
6604         echo -n "Setting pool using -p option..."
6605         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
6606                 error "migrate failed rc = $?"
6607         echo "done."
6608
6609         echo -n "Verifying test file is in pool after migrating..."
6610         [ "$($LFS getstripe -p $file1)" = $pool ] ||
6611                 error "file was not migrated to pool $pool"
6612         echo "done."
6613
6614         echo -n "Removing test file from pool '$pool'..."
6615         # "lfs migrate $file" won't remove the file from the pool
6616         # until some striping information is changed.
6617         $LFS migrate -c 1 $file1 &> /dev/null ||
6618                 error "cannot remove from pool"
6619         [ "$($LFS getstripe -p $file1)" ] &&
6620                 error "pool still set"
6621         echo "done."
6622
6623         echo -n "Setting pool using --pool option..."
6624         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
6625                 error "migrate failed rc = $?"
6626         echo "done."
6627
6628         # Clean up
6629         rm -f $file1
6630         if $create_pool; then
6631                 destroy_test_pools 2> /dev/null ||
6632                         error "destroy test pools failed"
6633         fi
6634 }
6635 run_test 56wb "check lfs_migrate pool support"
6636
6637 test_56wc() {
6638         local file1="$DIR/$tdir/file1"
6639         local parent_ssize
6640         local parent_scount
6641         local cur_ssize
6642         local cur_scount
6643         local orig_ssize
6644
6645         echo -n "Creating test dir..."
6646         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6647         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
6648                 error "cannot set stripe by '-S 1M -c 1'"
6649         echo "done"
6650
6651         echo -n "Setting initial stripe for test file..."
6652         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
6653                 error "cannot set stripe"
6654         cur_ssize=$($LFS getstripe -S "$file1")
6655         [ $cur_ssize -eq 524288 ] || error "setstripe -S $cur_ssize != 524288"
6656         echo "done."
6657
6658         # File currently set to -S 512K -c 1
6659
6660         # Ensure -c and -S options are rejected when -R is set
6661         echo -n "Verifying incompatible options are detected..."
6662         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
6663                 error "incompatible -c and -R options not detected"
6664         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
6665                 error "incompatible -S and -R options not detected"
6666         echo "done."
6667
6668         # Ensure unrecognized options are passed through to 'lfs migrate'
6669         echo -n "Verifying -S option is passed through to lfs migrate..."
6670         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
6671                 error "migration failed"
6672         cur_ssize=$($LFS getstripe -S "$file1")
6673         [ $cur_ssize -eq 1048576 ] || error "migrate -S $cur_ssize != 1048576"
6674         echo "done."
6675
6676         # File currently set to -S 1M -c 1
6677
6678         # Ensure long options are supported
6679         echo -n "Verifying long options supported..."
6680         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
6681                 error "long option without argument not supported"
6682         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
6683                 error "long option with argument not supported"
6684         cur_ssize=$($LFS getstripe -S "$file1")
6685         [ $cur_ssize -eq 524288 ] ||
6686                 error "migrate --stripe-size $cur_ssize != 524288"
6687         echo "done."
6688
6689         # File currently set to -S 512K -c 1
6690
6691         if [ "$OSTCOUNT" -gt 1 ]; then
6692                 echo -n "Verifying explicit stripe count can be set..."
6693                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
6694                         error "migrate failed"
6695                 cur_scount=$($LFS getstripe -c "$file1")
6696                 [ $cur_scount -eq 2 ] || error "migrate -c $cur_scount != 2"
6697                 echo "done."
6698         fi
6699
6700         # File currently set to -S 512K -c 1 or -S 512K -c 2
6701
6702         # Ensure parent striping is used if -R is set, and no stripe
6703         # count or size is specified
6704         echo -n "Setting stripe for parent directory..."
6705         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
6706                 error "cannot set stripe '-S 2M -c 1'"
6707         echo "done."
6708
6709         echo -n "Verifying restripe option uses parent stripe settings..."
6710         parent_ssize=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
6711         parent_scount=$($LFS getstripe -c $DIR/$tdir 2>/dev/null)
6712         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
6713                 error "migrate failed"
6714         cur_ssize=$($LFS getstripe -S "$file1")
6715         [ $cur_ssize -eq $parent_ssize ] ||
6716                 error "migrate -R stripe_size $cur_ssize != $parent_ssize"
6717         cur_scount=$($LFS getstripe -c "$file1")
6718         [ $cur_scount -eq $parent_scount ] ||
6719                 error "migrate -R stripe_count $cur_scount != $parent_scount"
6720         echo "done."
6721
6722         # File currently set to -S 1M -c 1
6723
6724         # Ensure striping is preserved if -R is not set, and no stripe
6725         # count or size is specified
6726         echo -n "Verifying striping size preserved when not specified..."
6727         orig_ssize=$($LFS getstripe -S "$file1" 2>/dev/null)
6728         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
6729                 error "cannot set stripe on parent directory"
6730         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6731                 error "migrate failed"
6732         cur_ssize=$($LFS getstripe -S "$file1")
6733         [ $cur_ssize -eq $orig_ssize ] ||
6734                 error "migrate by default $cur_ssize != $orig_ssize"
6735         echo "done."
6736
6737         # Ensure file name properly detected when final option has no argument
6738         echo -n "Verifying file name properly detected..."
6739         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6740                 error "file name interpreted as option argument"
6741         echo "done."
6742
6743         # Clean up
6744         rm -f "$file1"
6745 }
6746 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
6747
6748 test_56wd() {
6749         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6750
6751         local file1=$DIR/$tdir/file1
6752
6753         echo -n "Creating test dir..."
6754         test_mkdir $DIR/$tdir || error "cannot create dir"
6755         echo "done."
6756
6757         echo -n "Creating test file..."
6758         touch $file1
6759         echo "done."
6760
6761         # Ensure 'lfs migrate' will fail by using a non-existent option,
6762         # and make sure rsync is not called to recover
6763         echo -n "Make sure --no-rsync option works..."
6764         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
6765                 grep -q 'refusing to fall back to rsync' ||
6766                 error "rsync was called with --no-rsync set"
6767         echo "done."
6768
6769         # Ensure rsync is called without trying 'lfs migrate' first
6770         echo -n "Make sure --rsync option works..."
6771         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
6772                 grep -q 'falling back to rsync' &&
6773                 error "lfs migrate was called with --rsync set"
6774         echo "done."
6775
6776         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
6777         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
6778                 grep -q 'at the same time' ||
6779                 error "--rsync and --no-rsync accepted concurrently"
6780         echo "done."
6781
6782         # Clean up
6783         rm -f $file1
6784 }
6785 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
6786
6787 test_56x() {
6788         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6789         check_swap_layouts_support
6790
6791         local dir=$DIR/$tdir
6792         local ref1=/etc/passwd
6793         local file1=$dir/file1
6794
6795         test_mkdir $dir || error "creating dir $dir"
6796         $LFS setstripe -c 2 $file1
6797         cp $ref1 $file1
6798         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
6799         stripe=$($LFS getstripe -c $file1)
6800         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6801         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6802
6803         # clean up
6804         rm -f $file1
6805 }
6806 run_test 56x "lfs migration support"
6807
6808 test_56xa() {
6809         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6810         check_swap_layouts_support
6811
6812         local dir=$DIR/$tdir/$testnum
6813
6814         test_mkdir -p $dir
6815
6816         local ref1=/etc/passwd
6817         local file1=$dir/file1
6818
6819         $LFS setstripe -c 2 $file1
6820         cp $ref1 $file1
6821         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
6822
6823         local stripe=$($LFS getstripe -c $file1)
6824
6825         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6826         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6827
6828         # clean up
6829         rm -f $file1
6830 }
6831 run_test 56xa "lfs migration --block support"
6832
6833 check_migrate_links() {
6834         local dir="$1"
6835         local file1="$dir/file1"
6836         local begin="$2"
6837         local count="$3"
6838         local runas="$4"
6839         local total_count=$(($begin + $count - 1))
6840         local symlink_count=10
6841         local uniq_count=10
6842
6843         if [ ! -f "$file1" ]; then
6844                 echo -n "creating initial file..."
6845                 $LFS setstripe -c 1 -S "512k" "$file1" ||
6846                         error "cannot setstripe initial file"
6847                 echo "done"
6848
6849                 echo -n "creating symlinks..."
6850                 for s in $(seq 1 $symlink_count); do
6851                         ln -s "$file1" "$dir/slink$s" ||
6852                                 error "cannot create symlinks"
6853                 done
6854                 echo "done"
6855
6856                 echo -n "creating nonlinked files..."
6857                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
6858                         error "cannot create nonlinked files"
6859                 echo "done"
6860         fi
6861
6862         # create hard links
6863         if [ ! -f "$dir/file$total_count" ]; then
6864                 echo -n "creating hard links $begin:$total_count..."
6865                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
6866                         /dev/null || error "cannot create hard links"
6867                 echo "done"
6868         fi
6869
6870         echo -n "checking number of hard links listed in xattrs..."
6871         local fid=$($LFS getstripe -F "$file1")
6872         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
6873
6874         echo "${#paths[*]}"
6875         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
6876                         skip "hard link list has unexpected size, skipping test"
6877         fi
6878         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
6879                         error "link names should exceed xattrs size"
6880         fi
6881
6882         echo -n "migrating files..."
6883         local migrate_out=$($runas $LFS_MIGRATE -y -S '1m' $dir)
6884         local rc=$?
6885         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
6886         echo "done"
6887
6888         # make sure all links have been properly migrated
6889         echo -n "verifying files..."
6890         fid=$($LFS getstripe -F "$file1") ||
6891                 error "cannot get fid for file $file1"
6892         for i in $(seq 2 $total_count); do
6893                 local fid2=$($LFS getstripe -F $dir/file$i)
6894
6895                 [ "$fid2" == "$fid" ] ||
6896                         error "migrated hard link has mismatched FID"
6897         done
6898
6899         # make sure hard links were properly detected, and migration was
6900         # performed only once for the entire link set; nonlinked files should
6901         # also be migrated
6902         local actual=$(grep -c 'done' <<< "$migrate_out")
6903         local expected=$(($uniq_count + 1))
6904
6905         [ "$actual" -eq  "$expected" ] ||
6906                 error "hard links individually migrated ($actual != $expected)"
6907
6908         # make sure the correct number of hard links are present
6909         local hardlinks=$(stat -c '%h' "$file1")
6910
6911         [ $hardlinks -eq $total_count ] ||
6912                 error "num hard links $hardlinks != $total_count"
6913         echo "done"
6914
6915         return 0
6916 }
6917
6918 test_56xb() {
6919         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
6920                 skip "Need MDS version at least 2.10.55"
6921
6922         local dir="$DIR/$tdir"
6923
6924         test_mkdir "$dir" || error "cannot create dir $dir"
6925
6926         echo "testing lfs migrate mode when all links fit within xattrs"
6927         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
6928
6929         echo "testing rsync mode when all links fit within xattrs"
6930         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
6931
6932         echo "testing lfs migrate mode when all links do not fit within xattrs"
6933         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
6934
6935         echo "testing rsync mode when all links do not fit within xattrs"
6936         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
6937
6938         chown -R $RUNAS_ID $dir
6939         echo "testing non-root lfs migrate mode when not all links are in xattr"
6940         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100 "$RUNAS"
6941
6942         # clean up
6943         rm -rf $dir
6944 }
6945 run_test 56xb "lfs migration hard link support"
6946
6947 test_56xc() {
6948         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6949
6950         local dir="$DIR/$tdir"
6951
6952         test_mkdir "$dir" || error "cannot create dir $dir"
6953
6954         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
6955         echo -n "Setting initial stripe for 20MB test file..."
6956         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
6957                 error "cannot setstripe 20MB file"
6958         echo "done"
6959         echo -n "Sizing 20MB test file..."
6960         truncate "$dir/20mb" 20971520 || error "cannot create 20MB test file"
6961         echo "done"
6962         echo -n "Verifying small file autostripe count is 1..."
6963         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
6964                 error "cannot migrate 20MB file"
6965         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
6966                 error "cannot get stripe for $dir/20mb"
6967         [ $stripe_count -eq 1 ] ||
6968                 error "unexpected stripe count $stripe_count for 20MB file"
6969         rm -f "$dir/20mb"
6970         echo "done"
6971
6972         # Test 2: File is small enough to fit within the available space on
6973         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
6974         # have at least an additional 1KB for each desired stripe for test 3
6975         echo -n "Setting stripe for 1GB test file..."
6976         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
6977         echo "done"
6978         echo -n "Sizing 1GB test file..."
6979         # File size is 1GB + 3KB
6980         truncate "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
6981         echo "done"
6982
6983         # need at least 512MB per OST for 1GB file to fit in 2 stripes
6984         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
6985         if (( avail > 524288 * OSTCOUNT )); then
6986                 echo -n "Migrating 1GB file..."
6987                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
6988                         error "cannot migrate 1GB file"
6989                 echo "done"
6990                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
6991                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
6992                         error "cannot getstripe for 1GB file"
6993                 [ $stripe_count -eq 2 ] ||
6994                         error "unexpected stripe count $stripe_count != 2"
6995                 echo "done"
6996         fi
6997
6998         # Test 3: File is too large to fit within the available space on
6999         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
7000         if [ $OSTCOUNT -ge 3 ]; then
7001                 # The required available space is calculated as
7002                 # file size (1GB + 3KB) / OST count (3).
7003                 local kb_per_ost=349526
7004
7005                 echo -n "Migrating 1GB file with limit..."
7006                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
7007                         error "cannot migrate 1GB file with limit"
7008                 echo "done"
7009
7010                 stripe_count=$($LFS getstripe -c "$dir/1gb")
7011                 echo -n "Verifying 1GB autostripe count with limited space..."
7012                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
7013                         error "unexpected stripe count $stripe_count (min 3)"
7014                 echo "done"
7015         fi
7016
7017         # clean up
7018         rm -rf $dir
7019 }
7020 run_test 56xc "lfs migration autostripe"
7021
7022 test_56xd() {
7023         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7024
7025         local dir=$DIR/$tdir
7026         local f_mgrt=$dir/$tfile.mgrt
7027         local f_yaml=$dir/$tfile.yaml
7028         local f_copy=$dir/$tfile.copy
7029         local layout_yaml="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7030         local layout_copy="-c 2 -S 2M -i 1"
7031         local yamlfile=$dir/yamlfile
7032         local layout_before;
7033         local layout_after;
7034
7035         test_mkdir "$dir" || error "cannot create dir $dir"
7036         $LFS setstripe $layout_yaml $f_yaml ||
7037                 error "cannot setstripe $f_yaml with layout $layout_yaml"
7038         $LFS getstripe --yaml $f_yaml > $yamlfile
7039         $LFS setstripe $layout_copy $f_copy ||
7040                 error "cannot setstripe $f_copy with layout $layout_copy"
7041         touch $f_mgrt
7042         dd if=/dev/zero of=$f_mgrt bs=1M count=4
7043
7044         # 1. test option --yaml
7045         $LFS_MIGRATE -y --yaml $yamlfile $f_mgrt ||
7046                 error "cannot migrate $f_mgrt with --yaml $yamlfile"
7047         layout_before=$(get_layout_param $f_yaml)
7048         layout_after=$(get_layout_param $f_mgrt)
7049         [ "$layout_after" == "$layout_before" ] ||
7050                 error "lfs_migrate --yaml: $layout_after != $layout_before"
7051
7052         # 2. test option --copy
7053         $LFS_MIGRATE -y --copy $f_copy $f_mgrt ||
7054                 error "cannot migrate $f_mgrt with --copy $f_copy"
7055         layout_before=$(get_layout_param $f_copy)
7056         layout_after=$(get_layout_param $f_mgrt)
7057         [ "$layout_after" == "$layout_before" ] ||
7058                 error "lfs_migrate --copy: $layout_after != $layout_before"
7059 }
7060 run_test 56xd "check lfs_migrate --yaml and --copy support"
7061
7062 test_56xe() {
7063         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7064
7065         local dir=$DIR/$tdir
7066         local f_comp=$dir/$tfile
7067         local layout="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7068         local layout_before=""
7069         local layout_after=""
7070
7071         test_mkdir "$dir" || error "cannot create dir $dir"
7072         $LFS setstripe $layout $f_comp ||
7073                 error "cannot setstripe $f_comp with layout $layout"
7074         layout_before=$(get_layout_param $f_comp)
7075         dd if=/dev/zero of=$f_comp bs=1M count=4
7076
7077         # 1. migrate a comp layout file by lfs_migrate
7078         $LFS_MIGRATE -y $f_comp || error "cannot migrate $f_comp by lfs_migrate"
7079         layout_after=$(get_layout_param $f_comp)
7080         [ "$layout_before" == "$layout_after" ] ||
7081                 error "lfs_migrate: $layout_before != $layout_after"
7082
7083         # 2. migrate a comp layout file by lfs migrate
7084         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7085         layout_after=$(get_layout_param $f_comp)
7086         [ "$layout_before" == "$layout_after" ] ||
7087                 error "lfs migrate: $layout_before != $layout_after"
7088 }
7089 run_test 56xe "migrate a composite layout file"
7090
7091 test_56xf() {
7092         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7093
7094         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
7095                 skip "Need server version at least 2.13.53"
7096
7097         local dir=$DIR/$tdir
7098         local f_comp=$dir/$tfile
7099         local layout="-E 1M -c1 -E -1 -c2"
7100         local fid_before=""
7101         local fid_after=""
7102
7103         test_mkdir "$dir" || error "cannot create dir $dir"
7104         $LFS setstripe $layout $f_comp ||
7105                 error "cannot setstripe $f_comp with layout $layout"
7106         fid_before=$($LFS getstripe --fid $f_comp)
7107         dd if=/dev/zero of=$f_comp bs=1M count=4
7108
7109         # 1. migrate a comp layout file to a comp layout
7110         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7111         fid_after=$($LFS getstripe --fid $f_comp)
7112         [ "$fid_before" == "$fid_after" ] ||
7113                 error "comp-to-comp migrate: $fid_before != $fid_after"
7114
7115         # 2. migrate a comp layout file to a plain layout
7116         $LFS migrate -c2 $f_comp ||
7117                 error "cannot migrate $f_comp by lfs migrate"
7118         fid_after=$($LFS getstripe --fid $f_comp)
7119         [ "$fid_before" == "$fid_after" ] ||
7120                 error "comp-to-plain migrate: $fid_before != $fid_after"
7121
7122         # 3. migrate a plain layout file to a comp layout
7123         $LFS migrate $layout $f_comp ||
7124                 error "cannot migrate $f_comp by lfs migrate"
7125         fid_after=$($LFS getstripe --fid $f_comp)
7126         [ "$fid_before" == "$fid_after" ] ||
7127                 error "plain-to-comp migrate: $fid_before != $fid_after"
7128 }
7129 run_test 56xf "FID is not lost during migration of a composite layout file"
7130
7131 test_56y() {
7132         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
7133                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
7134
7135         local res=""
7136         local dir=$DIR/$tdir
7137         local f1=$dir/file1
7138         local f2=$dir/file2
7139
7140         test_mkdir -p $dir || error "creating dir $dir"
7141         touch $f1 || error "creating std file $f1"
7142         $MULTIOP $f2 H2c || error "creating released file $f2"
7143
7144         # a directory can be raid0, so ask only for files
7145         res=$($LFS find $dir -L raid0 -type f | wc -l)
7146         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
7147
7148         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
7149         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
7150
7151         # only files can be released, so no need to force file search
7152         res=$($LFS find $dir -L released)
7153         [[ $res == $f2 ]] || error "search released: found $res != $f2"
7154
7155         res=$($LFS find $dir -type f \! -L released)
7156         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
7157 }
7158 run_test 56y "lfs find -L raid0|released"
7159
7160 test_56z() { # LU-4824
7161         # This checks to make sure 'lfs find' continues after errors
7162         # There are two classes of errors that should be caught:
7163         # - If multiple paths are provided, all should be searched even if one
7164         #   errors out
7165         # - If errors are encountered during the search, it should not terminate
7166         #   early
7167         local dir=$DIR/$tdir
7168         local i
7169
7170         test_mkdir $dir
7171         for i in d{0..9}; do
7172                 test_mkdir $dir/$i
7173                 touch $dir/$i/$tfile
7174         done
7175         $LFS find $DIR/non_existent_dir $dir &&
7176                 error "$LFS find did not return an error"
7177         # Make a directory unsearchable. This should NOT be the last entry in
7178         # directory order.  Arbitrarily pick the 6th entry
7179         chmod 700 $($LFS find $dir -type d | sed '6!d')
7180
7181         $RUNAS $LFS find $DIR/non_existent $dir
7182         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
7183
7184         # The user should be able to see 10 directories and 9 files
7185         (( count == 19 )) ||
7186                 error "$LFS find found $count != 19 entries after error"
7187 }
7188 run_test 56z "lfs find should continue after an error"
7189
7190 test_56aa() { # LU-5937
7191         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
7192
7193         local dir=$DIR/$tdir
7194
7195         mkdir $dir
7196         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
7197
7198         createmany -o $dir/striped_dir/${tfile}- 1024
7199         local dirs=$($LFS find --size +8k $dir/)
7200
7201         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
7202 }
7203 run_test 56aa "lfs find --size under striped dir"
7204
7205 test_56ab() { # LU-10705
7206         test_mkdir $DIR/$tdir
7207         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
7208         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
7209         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
7210         # Flush writes to ensure valid blocks.  Need to be more thorough for
7211         # ZFS, since blocks are not allocated/returned to client immediately.
7212         sync_all_data
7213         wait_zfs_commit ost1 2
7214         cancel_lru_locks osc
7215         ls -ls $DIR/$tdir
7216
7217         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
7218
7219         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
7220
7221         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
7222         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
7223
7224         rm -f $DIR/$tdir/$tfile.[123]
7225 }
7226 run_test 56ab "lfs find --blocks"
7227
7228 test_56ba() {
7229         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
7230                 skip "Need MDS version at least 2.10.50"
7231
7232         # Create composite files with one component
7233         local dir=$DIR/$tdir
7234
7235         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
7236         # Create composite files with three components
7237         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
7238         # Create non-composite files
7239         createmany -o $dir/${tfile}- 10
7240
7241         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
7242
7243         [[ $nfiles == 10 ]] ||
7244                 error "lfs find -E 1M found $nfiles != 10 files"
7245
7246         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
7247         [[ $nfiles == 25 ]] ||
7248                 error "lfs find ! -E 1M found $nfiles != 25 files"
7249
7250         # All files have a component that starts at 0
7251         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
7252         [[ $nfiles == 35 ]] ||
7253                 error "lfs find --component-start 0 - $nfiles != 35 files"
7254
7255         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
7256         [[ $nfiles == 15 ]] ||
7257                 error "lfs find --component-start 2M - $nfiles != 15 files"
7258
7259         # All files created here have a componenet that does not starts at 2M
7260         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
7261         [[ $nfiles == 35 ]] ||
7262                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
7263
7264         # Find files with a specified number of components
7265         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
7266         [[ $nfiles == 15 ]] ||
7267                 error "lfs find --component-count 3 - $nfiles != 15 files"
7268
7269         # Remember non-composite files have a component count of zero
7270         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
7271         [[ $nfiles == 10 ]] ||
7272                 error "lfs find --component-count 0 - $nfiles != 10 files"
7273
7274         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
7275         [[ $nfiles == 20 ]] ||
7276                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
7277
7278         # All files have a flag called "init"
7279         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
7280         [[ $nfiles == 35 ]] ||
7281                 error "lfs find --component-flags init - $nfiles != 35 files"
7282
7283         # Multi-component files will have a component not initialized
7284         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
7285         [[ $nfiles == 15 ]] ||
7286                 error "lfs find !--component-flags init - $nfiles != 15 files"
7287
7288         rm -rf $dir
7289
7290 }
7291 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
7292
7293 test_56ca() {
7294         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
7295                 skip "Need MDS version at least 2.10.57"
7296
7297         local td=$DIR/$tdir
7298         local tf=$td/$tfile
7299         local dir
7300         local nfiles
7301         local cmd
7302         local i
7303         local j
7304
7305         # create mirrored directories and mirrored files
7306         mkdir $td || error "mkdir $td failed"
7307         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
7308         createmany -o $tf- 10 || error "create $tf- failed"
7309
7310         for i in $(seq 2); do
7311                 dir=$td/dir$i
7312                 mkdir $dir || error "mkdir $dir failed"
7313                 $LFS mirror create -N$((3 + i)) $dir ||
7314                         error "create mirrored dir $dir failed"
7315                 createmany -o $dir/$tfile- 10 ||
7316                         error "create $dir/$tfile- failed"
7317         done
7318
7319         # change the states of some mirrored files
7320         echo foo > $tf-6
7321         for i in $(seq 2); do
7322                 dir=$td/dir$i
7323                 for j in $(seq 4 9); do
7324                         echo foo > $dir/$tfile-$j
7325                 done
7326         done
7327
7328         # find mirrored files with specific mirror count
7329         cmd="$LFS find --mirror-count 3 --type f $td"
7330         nfiles=$($cmd | wc -l)
7331         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
7332
7333         cmd="$LFS find ! --mirror-count 3 --type f $td"
7334         nfiles=$($cmd | wc -l)
7335         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
7336
7337         cmd="$LFS find --mirror-count +2 --type f $td"
7338         nfiles=$($cmd | wc -l)
7339         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7340
7341         cmd="$LFS find --mirror-count -6 --type f $td"
7342         nfiles=$($cmd | wc -l)
7343         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7344
7345         # find mirrored files with specific file state
7346         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
7347         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
7348
7349         cmd="$LFS find --mirror-state=ro --type f $td"
7350         nfiles=$($cmd | wc -l)
7351         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
7352
7353         cmd="$LFS find ! --mirror-state=ro --type f $td"
7354         nfiles=$($cmd | wc -l)
7355         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7356
7357         cmd="$LFS find --mirror-state=wp --type f $td"
7358         nfiles=$($cmd | wc -l)
7359         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7360
7361         cmd="$LFS find ! --mirror-state=sp --type f $td"
7362         nfiles=$($cmd | wc -l)
7363         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7364 }
7365 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
7366
7367 test_57a() {
7368         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7369         # note test will not do anything if MDS is not local
7370         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7371                 skip_env "ldiskfs only test"
7372         fi
7373         remote_mds_nodsh && skip "remote MDS with nodsh"
7374
7375         local MNTDEV="osd*.*MDT*.mntdev"
7376         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
7377         [ -z "$DEV" ] && error "can't access $MNTDEV"
7378         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
7379                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
7380                         error "can't access $DEV"
7381                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
7382                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
7383                 rm $TMP/t57a.dump
7384         done
7385 }
7386 run_test 57a "verify MDS filesystem created with large inodes =="
7387
7388 test_57b() {
7389         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7390         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7391                 skip_env "ldiskfs only test"
7392         fi
7393         remote_mds_nodsh && skip "remote MDS with nodsh"
7394
7395         local dir=$DIR/$tdir
7396         local filecount=100
7397         local file1=$dir/f1
7398         local fileN=$dir/f$filecount
7399
7400         rm -rf $dir || error "removing $dir"
7401         test_mkdir -c1 $dir
7402         local mdtidx=$($LFS getstripe -m $dir)
7403         local mdtname=MDT$(printf %04x $mdtidx)
7404         local facet=mds$((mdtidx + 1))
7405
7406         echo "mcreating $filecount files"
7407         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
7408
7409         # verify that files do not have EAs yet
7410         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
7411                 error "$file1 has an EA"
7412         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
7413                 error "$fileN has an EA"
7414
7415         sync
7416         sleep 1
7417         df $dir  #make sure we get new statfs data
7418         local mdsfree=$(do_facet $facet \
7419                         lctl get_param -n osd*.*$mdtname.kbytesfree)
7420         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7421         local file
7422
7423         echo "opening files to create objects/EAs"
7424         for file in $(seq -f $dir/f%g 1 $filecount); do
7425                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
7426                         error "opening $file"
7427         done
7428
7429         # verify that files have EAs now
7430         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
7431         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
7432
7433         sleep 1  #make sure we get new statfs data
7434         df $dir
7435         local mdsfree2=$(do_facet $facet \
7436                          lctl get_param -n osd*.*$mdtname.kbytesfree)
7437         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7438
7439         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
7440                 if [ "$mdsfree" != "$mdsfree2" ]; then
7441                         error "MDC before $mdcfree != after $mdcfree2"
7442                 else
7443                         echo "MDC before $mdcfree != after $mdcfree2"
7444                         echo "unable to confirm if MDS has large inodes"
7445                 fi
7446         fi
7447         rm -rf $dir
7448 }
7449 run_test 57b "default LOV EAs are stored inside large inodes ==="
7450
7451 test_58() {
7452         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7453         [ -z "$(which wiretest 2>/dev/null)" ] &&
7454                         skip_env "could not find wiretest"
7455
7456         wiretest
7457 }
7458 run_test 58 "verify cross-platform wire constants =============="
7459
7460 test_59() {
7461         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7462
7463         echo "touch 130 files"
7464         createmany -o $DIR/f59- 130
7465         echo "rm 130 files"
7466         unlinkmany $DIR/f59- 130
7467         sync
7468         # wait for commitment of removal
7469         wait_delete_completed
7470 }
7471 run_test 59 "verify cancellation of llog records async ========="
7472
7473 TEST60_HEAD="test_60 run $RANDOM"
7474 test_60a() {
7475         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7476         remote_mgs_nodsh && skip "remote MGS with nodsh"
7477         do_facet mgs "! which run-llog.sh &> /dev/null" &&
7478                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
7479                         skip_env "missing subtest run-llog.sh"
7480
7481         log "$TEST60_HEAD - from kernel mode"
7482         do_facet mgs "$LCTL dk > /dev/null"
7483         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
7484         do_facet mgs $LCTL dk > $TMP/$tfile
7485
7486         # LU-6388: test llog_reader
7487         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
7488         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
7489         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
7490                         skip_env "missing llog_reader"
7491         local fstype=$(facet_fstype mgs)
7492         [ $fstype != ldiskfs -a $fstype != zfs ] &&
7493                 skip_env "Only for ldiskfs or zfs type mgs"
7494
7495         local mntpt=$(facet_mntpt mgs)
7496         local mgsdev=$(mgsdevname 1)
7497         local fid_list
7498         local fid
7499         local rec_list
7500         local rec
7501         local rec_type
7502         local obj_file
7503         local path
7504         local seq
7505         local oid
7506         local pass=true
7507
7508         #get fid and record list
7509         fid_list=($(awk '/9_sub.*record/ { print $NF }' $TMP/$tfile |
7510                 tail -n 4))
7511         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' $TMP/$tfile |
7512                 tail -n 4))
7513         #remount mgs as ldiskfs or zfs type
7514         stop mgs || error "stop mgs failed"
7515         mount_fstype mgs || error "remount mgs failed"
7516         for ((i = 0; i < ${#fid_list[@]}; i++)); do
7517                 fid=${fid_list[i]}
7518                 rec=${rec_list[i]}
7519                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
7520                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
7521                 oid=$((16#$oid))
7522
7523                 case $fstype in
7524                         ldiskfs )
7525                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
7526                         zfs )
7527                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
7528                 esac
7529                 echo "obj_file is $obj_file"
7530                 do_facet mgs $llog_reader $obj_file
7531
7532                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
7533                         awk '{ print $3 }' | sed -e "s/^type=//g")
7534                 if [ $rec_type != $rec ]; then
7535                         echo "FAILED test_60a wrong record type $rec_type," \
7536                               "should be $rec"
7537                         pass=false
7538                         break
7539                 fi
7540
7541                 #check obj path if record type is LLOG_LOGID_MAGIC
7542                 if [ "$rec" == "1064553b" ]; then
7543                         path=$(do_facet mgs $llog_reader $obj_file |
7544                                 grep "path=" | awk '{ print $NF }' |
7545                                 sed -e "s/^path=//g")
7546                         if [ $obj_file != $mntpt/$path ]; then
7547                                 echo "FAILED test_60a wrong obj path" \
7548                                       "$montpt/$path, should be $obj_file"
7549                                 pass=false
7550                                 break
7551                         fi
7552                 fi
7553         done
7554         rm -f $TMP/$tfile
7555         #restart mgs before "error", otherwise it will block the next test
7556         stop mgs || error "stop mgs failed"
7557         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
7558         $pass || error "test failed, see FAILED test_60a messages for specifics"
7559 }
7560 run_test 60a "llog_test run from kernel module and test llog_reader"
7561
7562 test_60b() { # bug 6411
7563         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7564
7565         dmesg > $DIR/$tfile
7566         LLOG_COUNT=$(do_facet mgs dmesg |
7567                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
7568                           /llog_[a-z]*.c:[0-9]/ {
7569                                 if (marker)
7570                                         from_marker++
7571                                 from_begin++
7572                           }
7573                           END {
7574                                 if (marker)
7575                                         print from_marker
7576                                 else
7577                                         print from_begin
7578                           }")
7579
7580         [[ $LLOG_COUNT -gt 120 ]] &&
7581                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
7582 }
7583 run_test 60b "limit repeated messages from CERROR/CWARN"
7584
7585 test_60c() {
7586         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7587
7588         echo "create 5000 files"
7589         createmany -o $DIR/f60c- 5000
7590 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
7591         lctl set_param fail_loc=0x80000137
7592         unlinkmany $DIR/f60c- 5000
7593         lctl set_param fail_loc=0
7594 }
7595 run_test 60c "unlink file when mds full"
7596
7597 test_60d() {
7598         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7599
7600         SAVEPRINTK=$(lctl get_param -n printk)
7601         # verify "lctl mark" is even working"
7602         MESSAGE="test message ID $RANDOM $$"
7603         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7604         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
7605
7606         lctl set_param printk=0 || error "set lnet.printk failed"
7607         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
7608         MESSAGE="new test message ID $RANDOM $$"
7609         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
7610         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7611         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
7612
7613         lctl set_param -n printk="$SAVEPRINTK"
7614 }
7615 run_test 60d "test printk console message masking"
7616
7617 test_60e() {
7618         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7619         remote_mds_nodsh && skip "remote MDS with nodsh"
7620
7621         touch $DIR/$tfile
7622 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
7623         do_facet mds1 lctl set_param fail_loc=0x15b
7624         rm $DIR/$tfile
7625 }
7626 run_test 60e "no space while new llog is being created"
7627
7628 test_60g() {
7629         local pid
7630         local i
7631
7632         test_mkdir -c $MDSCOUNT $DIR/$tdir
7633
7634         (
7635                 local index=0
7636                 while true; do
7637                         $LFS setdirstripe -i $(($index % $MDSCOUNT)) \
7638                                 -c $MDSCOUNT $DIR/$tdir/subdir$index \
7639                                 2>/dev/null
7640                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
7641                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
7642                         index=$((index + 1))
7643                 done
7644         ) &
7645
7646         pid=$!
7647
7648         for i in {0..100}; do
7649                 # define OBD_FAIL_OSD_TXN_START    0x19a
7650                 local index=$((i % MDSCOUNT + 1))
7651
7652                 do_facet mds$index $LCTL set_param fail_loc=0x8000019a \
7653                         > /dev/null
7654                 usleep 100
7655         done
7656
7657         kill -9 $pid
7658
7659         for i in $(seq $MDSCOUNT); do
7660                 do_facet mds$i $LCTL set_param fail_loc=0 > /dev/null
7661         done
7662
7663         mkdir $DIR/$tdir/new || error "mkdir failed"
7664         rmdir $DIR/$tdir/new || error "rmdir failed"
7665
7666         do_facet mds1 $LCTL lfsck_start -M $(facet_svc mds1) -A -C \
7667                 -t namespace
7668         for i in $(seq $MDSCOUNT); do
7669                 wait_update_facet mds$i "$LCTL get_param -n \
7670                         mdd.$(facet_svc mds$i).lfsck_namespace |
7671                         awk '/^status/ { print \\\$2 }'" "completed"
7672         done
7673
7674         ls -R $DIR/$tdir || error "ls failed"
7675         rm -rf $DIR/$tdir || error "rmdir failed"
7676 }
7677 run_test 60g "transaction abort won't cause MDT hung"
7678
7679 test_60h() {
7680         [ $MDS1_VERSION -le $(version_code 2.12.52) ] &&
7681                 skip "Need MDS version at least 2.12.52"
7682         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
7683
7684         local f
7685
7686         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
7687         #define OBD_FAIL_MDS_STRIPE_FID          0x189
7688         for fail_loc in 0x80000188 0x80000189; do
7689                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
7690                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
7691                         error "mkdir $dir-$fail_loc failed"
7692                 for i in {0..10}; do
7693                         # create may fail on missing stripe
7694                         echo $i > $DIR/$tdir-$fail_loc/$i
7695                 done
7696                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7697                         error "getdirstripe $tdir-$fail_loc failed"
7698                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
7699                         error "migrate $tdir-$fail_loc failed"
7700                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7701                         error "getdirstripe $tdir-$fail_loc failed"
7702                 pushd $DIR/$tdir-$fail_loc
7703                 for f in *; do
7704                         echo $f | cmp $f - || error "$f data mismatch"
7705                 done
7706                 popd
7707                 rm -rf $DIR/$tdir-$fail_loc
7708         done
7709 }
7710 run_test 60h "striped directory with missing stripes can be accessed"
7711
7712 test_61a() {
7713         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7714
7715         f="$DIR/f61"
7716         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
7717         cancel_lru_locks osc
7718         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
7719         sync
7720 }
7721 run_test 61a "mmap() writes don't make sync hang ================"
7722
7723 test_61b() {
7724         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
7725 }
7726 run_test 61b "mmap() of unstriped file is successful"
7727
7728 # bug 2330 - insufficient obd_match error checking causes LBUG
7729 test_62() {
7730         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7731
7732         f="$DIR/f62"
7733         echo foo > $f
7734         cancel_lru_locks osc
7735         lctl set_param fail_loc=0x405
7736         cat $f && error "cat succeeded, expect -EIO"
7737         lctl set_param fail_loc=0
7738 }
7739 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
7740 # match every page all of the time.
7741 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
7742
7743 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
7744 # Though this test is irrelevant anymore, it helped to reveal some
7745 # other grant bugs (LU-4482), let's keep it.
7746 test_63a() {   # was test_63
7747         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7748
7749         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
7750
7751         for i in `seq 10` ; do
7752                 dd if=/dev/zero of=$DIR/f63 bs=8k &
7753                 sleep 5
7754                 kill $!
7755                 sleep 1
7756         done
7757
7758         rm -f $DIR/f63 || true
7759 }
7760 run_test 63a "Verify oig_wait interruption does not crash ======="
7761
7762 # bug 2248 - async write errors didn't return to application on sync
7763 # bug 3677 - async write errors left page locked
7764 test_63b() {
7765         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7766
7767         debugsave
7768         lctl set_param debug=-1
7769
7770         # ensure we have a grant to do async writes
7771         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
7772         rm $DIR/$tfile
7773
7774         sync    # sync lest earlier test intercept the fail_loc
7775
7776         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7777         lctl set_param fail_loc=0x80000406
7778         $MULTIOP $DIR/$tfile Owy && \
7779                 error "sync didn't return ENOMEM"
7780         sync; sleep 2; sync     # do a real sync this time to flush page
7781         lctl get_param -n llite.*.dump_page_cache | grep locked && \
7782                 error "locked page left in cache after async error" || true
7783         debugrestore
7784 }
7785 run_test 63b "async write errors should be returned to fsync ==="
7786
7787 test_64a () {
7788         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7789
7790         lfs df $DIR
7791         lctl get_param osc.*[oO][sS][cC][_-]*.cur* | grep "=[1-9]"
7792 }
7793 run_test 64a "verify filter grant calculations (in kernel) ====="
7794
7795 test_64b () {
7796         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7797
7798         sh oos.sh $MOUNT || error "oos.sh failed: $?"
7799 }
7800 run_test 64b "check out-of-space detection on client"
7801
7802 test_64c() {
7803         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
7804 }
7805 run_test 64c "verify grant shrink"
7806
7807 # this does exactly what osc_request.c:osc_announce_cached() does in
7808 # order to calculate max amount of grants to ask from server
7809 want_grant() {
7810         local tgt=$1
7811
7812         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
7813         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
7814
7815         ((rpc_in_flight ++));
7816         nrpages=$((nrpages * rpc_in_flight))
7817
7818         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
7819
7820         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
7821
7822         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
7823         local undirty=$((nrpages * PAGE_SIZE))
7824
7825         local max_extent_pages
7826         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
7827             grep grant_max_extent_size | awk '{print $2}')
7828         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
7829         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
7830         local grant_extent_tax
7831         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7832             grep grant_extent_tax | awk '{print $2}')
7833
7834         undirty=$((undirty + nrextents * grant_extent_tax))
7835
7836         echo $undirty
7837 }
7838
7839 # this is size of unit for grant allocation. It should be equal to
7840 # what tgt_grant.c:tgt_grant_chunk() calculates
7841 grant_chunk() {
7842         local tgt=$1
7843         local max_brw_size
7844         local grant_extent_tax
7845
7846         max_brw_size=$($LCTL get_param osc.${tgt}.import |
7847             grep max_brw_size | awk '{print $2}')
7848
7849         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7850             grep grant_extent_tax | awk '{print $2}')
7851
7852         echo $(((max_brw_size + grant_extent_tax) * 2))
7853 }
7854
7855 test_64d() {
7856         [ $OST1_VERSION -lt $(version_code 2.10.56) ] &&
7857                 skip "OST < 2.10.55 doesn't limit grants enough"
7858
7859         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
7860         local file=$DIR/$tfile
7861
7862         [[ $($LCTL get_param osc.${tgt}.import |
7863              grep "connect_flags:.*grant_param") ]] ||
7864                 skip "no grant_param connect flag"
7865
7866         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
7867
7868         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
7869
7870         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
7871         stack_trap "rm -f $file" EXIT
7872
7873         $LFS setstripe $file -i 0 -c 1
7874         dd if=/dev/zero of=$file bs=1M count=1000 &
7875         ddpid=$!
7876
7877         while true
7878         do
7879                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
7880                 if [[ $cur_grant -gt $max_cur_granted ]]
7881                 then
7882                         kill $ddpid
7883                         error "cur_grant $cur_grant > $max_cur_granted"
7884                 fi
7885                 kill -0 $ddpid
7886                 [[ $? -ne 0 ]] && break;
7887                 sleep 2
7888         done
7889
7890         rm -f $DIR/$tfile
7891         wait_delete_completed
7892         $LCTL set_param debug="$olddebug" 2> /dev/null || true
7893 }
7894 run_test 64d "check grant limit exceed"
7895
7896 # bug 1414 - set/get directories' stripe info
7897 test_65a() {
7898         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7899
7900         test_mkdir $DIR/$tdir
7901         touch $DIR/$tdir/f1
7902         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
7903 }
7904 run_test 65a "directory with no stripe info"
7905
7906 test_65b() {
7907         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7908
7909         test_mkdir $DIR/$tdir
7910         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7911
7912         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7913                                                 error "setstripe"
7914         touch $DIR/$tdir/f2
7915         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
7916 }
7917 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
7918
7919 test_65c() {
7920         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7921         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
7922
7923         test_mkdir $DIR/$tdir
7924         local stripesize=$($LFS getstripe -S $DIR/$tdir)
7925
7926         $LFS setstripe -S $((stripesize * 4)) -i 1 \
7927                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
7928         touch $DIR/$tdir/f3
7929         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
7930 }
7931 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
7932
7933 test_65d() {
7934         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7935
7936         test_mkdir $DIR/$tdir
7937         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
7938         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7939
7940         if [[ $STRIPECOUNT -le 0 ]]; then
7941                 sc=1
7942         elif [[ $STRIPECOUNT -gt $LOV_MAX_STRIPE_COUNT ]]; then
7943                 [[ $OSTCOUNT -gt $LOV_MAX_STRIPE_COUNT ]] &&
7944                         sc=$LOV_MAX_STRIPE_COUNT || sc=$(($OSTCOUNT - 1))
7945         else
7946                 sc=$(($STRIPECOUNT - 1))
7947         fi
7948         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
7949         touch $DIR/$tdir/f4 $DIR/$tdir/f5
7950         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
7951                 error "lverify failed"
7952 }
7953 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
7954
7955 test_65e() {
7956         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7957
7958         test_mkdir $DIR/$tdir
7959
7960         $LFS setstripe $DIR/$tdir || error "setstripe"
7961         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7962                                         error "no stripe info failed"
7963         touch $DIR/$tdir/f6
7964         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
7965 }
7966 run_test 65e "directory setstripe defaults"
7967
7968 test_65f() {
7969         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7970
7971         test_mkdir $DIR/${tdir}f
7972         $RUNAS $LFS setstripe $DIR/${tdir}f &&
7973                 error "setstripe succeeded" || true
7974 }
7975 run_test 65f "dir setstripe permission (should return error) ==="
7976
7977 test_65g() {
7978         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7979
7980         test_mkdir $DIR/$tdir
7981         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7982
7983         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7984                 error "setstripe -S failed"
7985         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
7986         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7987                 error "delete default stripe failed"
7988 }
7989 run_test 65g "directory setstripe -d"
7990
7991 test_65h() {
7992         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7993
7994         test_mkdir $DIR/$tdir
7995         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7996
7997         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7998                 error "setstripe -S failed"
7999         test_mkdir $DIR/$tdir/dd1
8000         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
8001                 error "stripe info inherit failed"
8002 }
8003 run_test 65h "directory stripe info inherit ===================="
8004
8005 test_65i() {
8006         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8007
8008         save_layout_restore_at_exit $MOUNT
8009
8010         # bug6367: set non-default striping on root directory
8011         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
8012
8013         # bug12836: getstripe on -1 default directory striping
8014         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
8015
8016         # bug12836: getstripe -v on -1 default directory striping
8017         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
8018
8019         # bug12836: new find on -1 default directory striping
8020         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
8021 }
8022 run_test 65i "various tests to set root directory striping"
8023
8024 test_65j() { # bug6367
8025         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8026
8027         sync; sleep 1
8028
8029         # if we aren't already remounting for each test, do so for this test
8030         if [ "$I_MOUNTED" = "yes" ]; then
8031                 cleanup || error "failed to unmount"
8032                 setup
8033         fi
8034
8035         save_layout_restore_at_exit $MOUNT
8036
8037         $LFS setstripe -d $MOUNT || error "setstripe failed"
8038 }
8039 run_test 65j "set default striping on root directory (bug 6367)="
8040
8041 cleanup_65k() {
8042         rm -rf $DIR/$tdir
8043         wait_delete_completed
8044         do_facet $SINGLEMDS "lctl set_param -n \
8045                 osp.$ost*MDT0000.max_create_count=$max_count"
8046         do_facet $SINGLEMDS "lctl set_param -n \
8047                 osp.$ost*MDT0000.create_count=$count"
8048         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
8049         echo $INACTIVE_OSC "is Activate"
8050
8051         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
8052 }
8053
8054 test_65k() { # bug11679
8055         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8056         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8057         remote_mds_nodsh && skip "remote MDS with nodsh"
8058
8059         local disable_precreate=true
8060         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
8061                 disable_precreate=false
8062
8063         echo "Check OST status: "
8064         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
8065                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
8066
8067         for OSC in $MDS_OSCS; do
8068                 echo $OSC "is active"
8069                 do_facet $SINGLEMDS lctl --device %$OSC activate
8070         done
8071
8072         for INACTIVE_OSC in $MDS_OSCS; do
8073                 local ost=$(osc_to_ost $INACTIVE_OSC)
8074                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
8075                                lov.*md*.target_obd |
8076                                awk -F: /$ost/'{ print $1 }' | head -n 1)
8077
8078                 mkdir -p $DIR/$tdir
8079                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
8080                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
8081
8082                 echo "Deactivate: " $INACTIVE_OSC
8083                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
8084
8085                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
8086                               osp.$ost*MDT0000.create_count")
8087                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
8088                                   osp.$ost*MDT0000.max_create_count")
8089                 $disable_precreate &&
8090                         do_facet $SINGLEMDS "lctl set_param -n \
8091                                 osp.$ost*MDT0000.max_create_count=0"
8092
8093                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
8094                         [ -f $DIR/$tdir/$idx ] && continue
8095                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
8096                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
8097                                 { cleanup_65k;
8098                                   error "setstripe $idx should succeed"; }
8099                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
8100                 done
8101                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
8102                 rmdir $DIR/$tdir
8103
8104                 do_facet $SINGLEMDS "lctl set_param -n \
8105                         osp.$ost*MDT0000.max_create_count=$max_count"
8106                 do_facet $SINGLEMDS "lctl set_param -n \
8107                         osp.$ost*MDT0000.create_count=$count"
8108                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
8109                 echo $INACTIVE_OSC "is Activate"
8110
8111                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
8112         done
8113 }
8114 run_test 65k "validate manual striping works properly with deactivated OSCs"
8115
8116 test_65l() { # bug 12836
8117         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8118
8119         test_mkdir -p $DIR/$tdir/test_dir
8120         $LFS setstripe -c -1 $DIR/$tdir/test_dir
8121         $LFS find -mtime -1 $DIR/$tdir >/dev/null
8122 }
8123 run_test 65l "lfs find on -1 stripe dir ========================"
8124
8125 test_65m() {
8126         local layout=$(save_layout $MOUNT)
8127         $RUNAS $LFS setstripe -c 2 $MOUNT && {
8128                 restore_layout $MOUNT $layout
8129                 error "setstripe should fail by non-root users"
8130         }
8131         true
8132 }
8133 run_test 65m "normal user can't set filesystem default stripe"
8134
8135 test_65n() {
8136         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
8137         [[ $MDS1_VERSION -ge $(version_code 2.12.50) ]] ||
8138                 skip "Need MDS version at least 2.12.50"
8139         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
8140
8141         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
8142         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
8143         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
8144
8145         local root_layout=$(save_layout $MOUNT)
8146         stack_trap "restore_layout $MOUNT $root_layout" EXIT
8147
8148         # new subdirectory under root directory should not inherit
8149         # the default layout from root
8150         local dir1=$MOUNT/$tdir-1
8151         mkdir $dir1 || error "mkdir $dir1 failed"
8152         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
8153                 error "$dir1 shouldn't have LOV EA"
8154
8155         # delete the default layout on root directory
8156         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
8157
8158         local dir2=$MOUNT/$tdir-2
8159         mkdir $dir2 || error "mkdir $dir2 failed"
8160         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
8161                 error "$dir2 shouldn't have LOV EA"
8162
8163         # set a new striping pattern on root directory
8164         local def_stripe_size=$($LFS getstripe -S $MOUNT)
8165         local new_def_stripe_size=$((def_stripe_size * 2))
8166         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
8167                 error "set stripe size on $MOUNT failed"
8168
8169         # new file created in $dir2 should inherit the new stripe size from
8170         # the filesystem default
8171         local file2=$dir2/$tfile-2
8172         touch $file2 || error "touch $file2 failed"
8173
8174         local file2_stripe_size=$($LFS getstripe -S $file2)
8175         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
8176                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
8177
8178         local dir3=$MOUNT/$tdir-3
8179         mkdir $dir3 || error "mkdir $dir3 failed"
8180         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
8181         # the root layout, which is the actual default layout that will be used
8182         # when new files are created in $dir3.
8183         local dir3_layout=$(get_layout_param $dir3)
8184         local root_dir_layout=$(get_layout_param $MOUNT)
8185         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
8186                 error "$dir3 should show the default layout from $MOUNT"
8187
8188         # set OST pool on root directory
8189         local pool=$TESTNAME
8190         pool_add $pool || error "add $pool failed"
8191         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
8192                 error "add targets to $pool failed"
8193
8194         $LFS setstripe -p $pool $MOUNT ||
8195                 error "set OST pool on $MOUNT failed"
8196
8197         # new file created in $dir3 should inherit the pool from
8198         # the filesystem default
8199         local file3=$dir3/$tfile-3
8200         touch $file3 || error "touch $file3 failed"
8201
8202         local file3_pool=$($LFS getstripe -p $file3)
8203         [[ "$file3_pool" = "$pool" ]] ||
8204                 error "$file3 didn't inherit OST pool $pool"
8205
8206         local dir4=$MOUNT/$tdir-4
8207         mkdir $dir4 || error "mkdir $dir4 failed"
8208         local dir4_layout=$(get_layout_param $dir4)
8209         root_dir_layout=$(get_layout_param $MOUNT)
8210         echo "$LFS getstripe -d $dir4"
8211         $LFS getstripe -d $dir4
8212         echo "$LFS getstripe -d $MOUNT"
8213         $LFS getstripe -d $MOUNT
8214         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
8215                 error "$dir4 should show the default layout from $MOUNT"
8216
8217         # new file created in $dir4 should inherit the pool from
8218         # the filesystem default
8219         local file4=$dir4/$tfile-4
8220         touch $file4 || error "touch $file4 failed"
8221
8222         local file4_pool=$($LFS getstripe -p $file4)
8223         [[ "$file4_pool" = "$pool" ]] ||
8224                 error "$file4 didn't inherit OST pool $pool"
8225
8226         # new subdirectory under non-root directory should inherit
8227         # the default layout from its parent directory
8228         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
8229                 error "set directory layout on $dir4 failed"
8230
8231         local dir5=$dir4/$tdir-5
8232         mkdir $dir5 || error "mkdir $dir5 failed"
8233
8234         dir4_layout=$(get_layout_param $dir4)
8235         local dir5_layout=$(get_layout_param $dir5)
8236         [[ "$dir4_layout" = "$dir5_layout" ]] ||
8237                 error "$dir5 should inherit the default layout from $dir4"
8238
8239         # though subdir under ROOT doesn't inherit default layout, but
8240         # its sub dir/file should be created with default layout.
8241         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
8242         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
8243                 skip "Need MDS version at least 2.12.59"
8244
8245         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
8246         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
8247         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
8248
8249         if [ $default_lmv_hash == "none" ]; then
8250                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
8251         else
8252                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
8253                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
8254         fi
8255
8256         $LFS setdirstripe -D -c 2 $MOUNT ||
8257                 error "setdirstripe -D -c 2 failed"
8258         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
8259         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
8260         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
8261 }
8262 run_test 65n "don't inherit default layout from root for new subdirectories"
8263
8264 # bug 2543 - update blocks count on client
8265 test_66() {
8266         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8267
8268         COUNT=${COUNT:-8}
8269         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
8270         sync; sync_all_data; sync; sync_all_data
8271         cancel_lru_locks osc
8272         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
8273         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
8274 }
8275 run_test 66 "update inode blocks count on client ==============="
8276
8277 meminfo() {
8278         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
8279 }
8280
8281 swap_used() {
8282         swapon -s | awk '($1 == "'$1'") { print $4 }'
8283 }
8284
8285 # bug5265, obdfilter oa2dentry return -ENOENT
8286 # #define OBD_FAIL_SRV_ENOENT 0x217
8287 test_69() {
8288         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8289         remote_ost_nodsh && skip "remote OST with nodsh"
8290
8291         f="$DIR/$tfile"
8292         $LFS setstripe -c 1 -i 0 $f
8293
8294         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
8295
8296         do_facet ost1 lctl set_param fail_loc=0x217
8297         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
8298         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
8299
8300         do_facet ost1 lctl set_param fail_loc=0
8301         $DIRECTIO write $f 0 2 || error "write error"
8302
8303         cancel_lru_locks osc
8304         $DIRECTIO read $f 0 1 || error "read error"
8305
8306         do_facet ost1 lctl set_param fail_loc=0x217
8307         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
8308
8309         do_facet ost1 lctl set_param fail_loc=0
8310         rm -f $f
8311 }
8312 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
8313
8314 test_71() {
8315         test_mkdir $DIR/$tdir
8316         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
8317         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
8318 }
8319 run_test 71 "Running dbench on lustre (don't segment fault) ===="
8320
8321 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
8322         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8323         [ "$RUNAS_ID" = "$UID" ] &&
8324                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8325         # Check that testing environment is properly set up. Skip if not
8326         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
8327                 skip_env "User $RUNAS_ID does not exist - skipping"
8328
8329         touch $DIR/$tfile
8330         chmod 777 $DIR/$tfile
8331         chmod ug+s $DIR/$tfile
8332         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
8333                 error "$RUNAS dd $DIR/$tfile failed"
8334         # See if we are still setuid/sgid
8335         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8336                 error "S/gid is not dropped on write"
8337         # Now test that MDS is updated too
8338         cancel_lru_locks mdc
8339         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8340                 error "S/gid is not dropped on MDS"
8341         rm -f $DIR/$tfile
8342 }
8343 run_test 72a "Test that remove suid works properly (bug5695) ===="
8344
8345 test_72b() { # bug 24226 -- keep mode setting when size is not changing
8346         local perm
8347
8348         [ "$RUNAS_ID" = "$UID" ] &&
8349                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8350         [ "$RUNAS_ID" -eq 0 ] &&
8351                 skip_env "RUNAS_ID = 0 -- skipping"
8352         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8353         # Check that testing environment is properly set up. Skip if not
8354         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
8355                 skip_env "User $RUNAS_ID does not exist - skipping"
8356
8357         touch $DIR/${tfile}-f{g,u}
8358         test_mkdir $DIR/${tfile}-dg
8359         test_mkdir $DIR/${tfile}-du
8360         chmod 770 $DIR/${tfile}-{f,d}{g,u}
8361         chmod g+s $DIR/${tfile}-{f,d}g
8362         chmod u+s $DIR/${tfile}-{f,d}u
8363         for perm in 777 2777 4777; do
8364                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
8365                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
8366                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
8367                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
8368         done
8369         true
8370 }
8371 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
8372
8373 # bug 3462 - multiple simultaneous MDC requests
8374 test_73() {
8375         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8376
8377         test_mkdir $DIR/d73-1
8378         test_mkdir $DIR/d73-2
8379         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
8380         pid1=$!
8381
8382         lctl set_param fail_loc=0x80000129
8383         $MULTIOP $DIR/d73-1/f73-2 Oc &
8384         sleep 1
8385         lctl set_param fail_loc=0
8386
8387         $MULTIOP $DIR/d73-2/f73-3 Oc &
8388         pid3=$!
8389
8390         kill -USR1 $pid1
8391         wait $pid1 || return 1
8392
8393         sleep 25
8394
8395         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
8396         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
8397         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
8398
8399         rm -rf $DIR/d73-*
8400 }
8401 run_test 73 "multiple MDC requests (should not deadlock)"
8402
8403 test_74a() { # bug 6149, 6184
8404         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8405
8406         touch $DIR/f74a
8407         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8408         #
8409         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8410         # will spin in a tight reconnection loop
8411         $LCTL set_param fail_loc=0x8000030e
8412         # get any lock that won't be difficult - lookup works.
8413         ls $DIR/f74a
8414         $LCTL set_param fail_loc=0
8415         rm -f $DIR/f74a
8416         true
8417 }
8418 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
8419
8420 test_74b() { # bug 13310
8421         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8422
8423         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8424         #
8425         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8426         # will spin in a tight reconnection loop
8427         $LCTL set_param fail_loc=0x8000030e
8428         # get a "difficult" lock
8429         touch $DIR/f74b
8430         $LCTL set_param fail_loc=0
8431         rm -f $DIR/f74b
8432         true
8433 }
8434 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
8435
8436 test_74c() {
8437         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8438
8439         #define OBD_FAIL_LDLM_NEW_LOCK
8440         $LCTL set_param fail_loc=0x319
8441         touch $DIR/$tfile && error "touch successful"
8442         $LCTL set_param fail_loc=0
8443         true
8444 }
8445 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
8446
8447 num_inodes() {
8448         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
8449 }
8450
8451 test_76() { # Now for bug 20433, added originally in bug 1443
8452         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8453
8454         cancel_lru_locks osc
8455         local cpus=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
8456         local before=$(num_inodes)
8457         local count=$((512 * cpus))
8458         [ "$SLOW" = "no" ] && count=$((64 * cpus))
8459
8460         echo "before inodes: $before"
8461         for i in $(seq $count); do
8462                 touch $DIR/$tfile
8463                 rm -f $DIR/$tfile
8464         done
8465         cancel_lru_locks osc
8466         local after=$(num_inodes)
8467         echo "after inodes: $after"
8468         while (( after > before + 8 * ${cpus:-1} )); do
8469                 sleep 1
8470                 after=$(num_inodes)
8471                 wait=$((wait + 1))
8472                 (( wait % 5 == 0 )) && echo "wait $wait seconds inodes: $after"
8473                 if (( wait > 30 )); then
8474                         error "inode slab grew from $before to $after"
8475                 fi
8476         done
8477 }
8478 run_test 76 "confirm clients recycle inodes properly ===="
8479
8480
8481 export ORIG_CSUM=""
8482 set_checksums()
8483 {
8484         # Note: in sptlrpc modes which enable its own bulk checksum, the
8485         # original crc32_le bulk checksum will be automatically disabled,
8486         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
8487         # will be checked by sptlrpc code against sptlrpc bulk checksum.
8488         # In this case set_checksums() will not be no-op, because sptlrpc
8489         # bulk checksum will be enabled all through the test.
8490
8491         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
8492         lctl set_param -n osc.*.checksums $1
8493         return 0
8494 }
8495
8496 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8497                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
8498 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8499                              tr -d [] | head -n1)}
8500 set_checksum_type()
8501 {
8502         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
8503         rc=$?
8504         log "set checksum type to $1, rc = $rc"
8505         return $rc
8506 }
8507
8508 get_osc_checksum_type()
8509 {
8510         # arugment 1: OST name, like OST0000
8511         ost=$1
8512         checksum_type=$(lctl get_param -n osc.*${ost}-osc-[^mM]*.checksum_type |
8513                         sed 's/.*\[\(.*\)\].*/\1/g')
8514         rc=$?
8515         [ $rc -ne 0 ] && error "failed to get checksum type of $ost, rc = $rc, output = $checksum_type"
8516         echo $checksum_type
8517 }
8518
8519 F77_TMP=$TMP/f77-temp
8520 F77SZ=8
8521 setup_f77() {
8522         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
8523                 error "error writing to $F77_TMP"
8524 }
8525
8526 test_77a() { # bug 10889
8527         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8528         $GSS && skip_env "could not run with gss"
8529
8530         [ ! -f $F77_TMP ] && setup_f77
8531         set_checksums 1
8532         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
8533         set_checksums 0
8534         rm -f $DIR/$tfile
8535 }
8536 run_test 77a "normal checksum read/write operation"
8537
8538 test_77b() { # bug 10889
8539         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8540         $GSS && skip_env "could not run with gss"
8541
8542         [ ! -f $F77_TMP ] && setup_f77
8543         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8544         $LCTL set_param fail_loc=0x80000409
8545         set_checksums 1
8546
8547         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8548                 error "dd error: $?"
8549         $LCTL set_param fail_loc=0
8550
8551         for algo in $CKSUM_TYPES; do
8552                 cancel_lru_locks osc
8553                 set_checksum_type $algo
8554                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8555                 $LCTL set_param fail_loc=0x80000408
8556                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
8557                 $LCTL set_param fail_loc=0
8558         done
8559         set_checksums 0
8560         set_checksum_type $ORIG_CSUM_TYPE
8561         rm -f $DIR/$tfile
8562 }
8563 run_test 77b "checksum error on client write, read"
8564
8565 cleanup_77c() {
8566         trap 0
8567         set_checksums 0
8568         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
8569         $check_ost &&
8570                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
8571         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
8572         $check_ost && [ -n "$ost_file_prefix" ] &&
8573                 do_facet ost1 rm -f ${ost_file_prefix}\*
8574 }
8575
8576 test_77c() {
8577         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8578         $GSS && skip_env "could not run with gss"
8579         remote_ost_nodsh && skip "remote OST with nodsh"
8580
8581         local bad1
8582         local osc_file_prefix
8583         local osc_file
8584         local check_ost=false
8585         local ost_file_prefix
8586         local ost_file
8587         local orig_cksum
8588         local dump_cksum
8589         local fid
8590
8591         # ensure corruption will occur on first OSS/OST
8592         $LFS setstripe -i 0 $DIR/$tfile
8593
8594         [ ! -f $F77_TMP ] && setup_f77
8595         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8596                 error "dd write error: $?"
8597         fid=$($LFS path2fid $DIR/$tfile)
8598
8599         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
8600         then
8601                 check_ost=true
8602                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
8603                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
8604         else
8605                 echo "OSS do not support bulk pages dump upon error"
8606         fi
8607
8608         osc_file_prefix=$($LCTL get_param -n debug_path)
8609         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
8610
8611         trap cleanup_77c EXIT
8612
8613         set_checksums 1
8614         # enable bulk pages dump upon error on Client
8615         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
8616         # enable bulk pages dump upon error on OSS
8617         $check_ost &&
8618                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
8619
8620         # flush Client cache to allow next read to reach OSS
8621         cancel_lru_locks osc
8622
8623         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
8624         $LCTL set_param fail_loc=0x80000408
8625         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
8626         $LCTL set_param fail_loc=0
8627
8628         rm -f $DIR/$tfile
8629
8630         # check cksum dump on Client
8631         osc_file=$(ls ${osc_file_prefix}*)
8632         [ -n "$osc_file" ] || error "no checksum dump file on Client"
8633         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
8634         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
8635         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
8636         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
8637                      cksum)
8638         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
8639         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8640                 error "dump content does not match on Client"
8641
8642         $check_ost || skip "No need to check cksum dump on OSS"
8643
8644         # check cksum dump on OSS
8645         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
8646         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
8647         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
8648         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
8649         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8650                 error "dump content does not match on OSS"
8651
8652         cleanup_77c
8653 }
8654 run_test 77c "checksum error on client read with debug"
8655
8656 test_77d() { # bug 10889
8657         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8658         $GSS && skip_env "could not run with gss"
8659
8660         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8661         $LCTL set_param fail_loc=0x80000409
8662         set_checksums 1
8663         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8664                 error "direct write: rc=$?"
8665         $LCTL set_param fail_loc=0
8666         set_checksums 0
8667
8668         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8669         $LCTL set_param fail_loc=0x80000408
8670         set_checksums 1
8671         cancel_lru_locks osc
8672         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8673                 error "direct read: rc=$?"
8674         $LCTL set_param fail_loc=0
8675         set_checksums 0
8676 }
8677 run_test 77d "checksum error on OST direct write, read"
8678
8679 test_77f() { # bug 10889
8680         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8681         $GSS && skip_env "could not run with gss"
8682
8683         set_checksums 1
8684         for algo in $CKSUM_TYPES; do
8685                 cancel_lru_locks osc
8686                 set_checksum_type $algo
8687                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8688                 $LCTL set_param fail_loc=0x409
8689                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
8690                         error "direct write succeeded"
8691                 $LCTL set_param fail_loc=0
8692         done
8693         set_checksum_type $ORIG_CSUM_TYPE
8694         set_checksums 0
8695 }
8696 run_test 77f "repeat checksum error on write (expect error)"
8697
8698 test_77g() { # bug 10889
8699         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8700         $GSS && skip_env "could not run with gss"
8701         remote_ost_nodsh && skip "remote OST with nodsh"
8702
8703         [ ! -f $F77_TMP ] && setup_f77
8704
8705         local file=$DIR/$tfile
8706         stack_trap "rm -f $file" EXIT
8707
8708         $LFS setstripe -c 1 -i 0 $file
8709         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
8710         do_facet ost1 lctl set_param fail_loc=0x8000021a
8711         set_checksums 1
8712         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
8713                 error "write error: rc=$?"
8714         do_facet ost1 lctl set_param fail_loc=0
8715         set_checksums 0
8716
8717         cancel_lru_locks osc
8718         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
8719         do_facet ost1 lctl set_param fail_loc=0x8000021b
8720         set_checksums 1
8721         cmp $F77_TMP $file || error "file compare failed"
8722         do_facet ost1 lctl set_param fail_loc=0
8723         set_checksums 0
8724 }
8725 run_test 77g "checksum error on OST write, read"
8726
8727 test_77k() { # LU-10906
8728         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8729         $GSS && skip_env "could not run with gss"
8730
8731         local cksum_param="osc.$FSNAME*.checksums"
8732         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
8733         local checksum
8734         local i
8735
8736         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
8737         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
8738         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
8739                 EXIT
8740
8741         for i in 0 1; do
8742                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
8743                         error "failed to set checksum=$i on MGS"
8744                 wait_update $HOSTNAME "$get_checksum" $i
8745                 #remount
8746                 echo "remount client, checksum should be $i"
8747                 remount_client $MOUNT || error "failed to remount client"
8748                 checksum=$(eval $get_checksum)
8749                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8750         done
8751         # remove persistent param to avoid races with checksum mountopt below
8752         do_facet mgs $LCTL set_param -P -d $cksum_param ||
8753                 error "failed to delete checksum on MGS"
8754
8755         for opt in "checksum" "nochecksum"; do
8756                 #remount with mount option
8757                 echo "remount client with option $opt, checksum should be $i"
8758                 umount_client $MOUNT || error "failed to umount client"
8759                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
8760                         error "failed to mount client with option '$opt'"
8761                 checksum=$(eval $get_checksum)
8762                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8763                 i=$((i - 1))
8764         done
8765
8766         remount_client $MOUNT || error "failed to remount client"
8767 }
8768 run_test 77k "enable/disable checksum correctly"
8769
8770 test_77l() {
8771         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8772         $GSS && skip_env "could not run with gss"
8773
8774         set_checksums 1
8775         stack_trap "set_checksums $ORIG_CSUM" EXIT
8776         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
8777
8778         set_checksum_type invalid && error "unexpected success of invalid checksum type"
8779
8780         $LFS setstripe -c 1 -i 0 $DIR/$tfile
8781         for algo in $CKSUM_TYPES; do
8782                 set_checksum_type $algo || error "fail to set checksum type $algo"
8783                 osc_algo=$(get_osc_checksum_type OST0000)
8784                 [ "$osc_algo" != "$algo" ] && error "checksum type is $osc_algo after setting it to $algo"
8785
8786                 # no locks, no reqs to let the connection idle
8787                 cancel_lru_locks osc
8788                 lru_resize_disable osc
8789                 wait_osc_import_state client ost1 IDLE
8790
8791                 # ensure ost1 is connected
8792                 stat $DIR/$tfile >/dev/null || error "can't stat"
8793                 wait_osc_import_state client ost1 FULL
8794
8795                 osc_algo=$(get_osc_checksum_type OST0000)
8796                 [ "$osc_algo" != "$algo" ] && error "checksum type changed from $algo to $osc_algo after reconnection"
8797         done
8798         return 0
8799 }
8800 run_test 77l "preferred checksum type is remembered after reconnected"
8801
8802 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
8803 rm -f $F77_TMP
8804 unset F77_TMP
8805
8806 cleanup_test_78() {
8807         trap 0
8808         rm -f $DIR/$tfile
8809 }
8810
8811 test_78() { # bug 10901
8812         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8813         remote_ost || skip_env "local OST"
8814
8815         NSEQ=5
8816         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
8817         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
8818         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
8819         echo "MemTotal: $MEMTOTAL"
8820
8821         # reserve 256MB of memory for the kernel and other running processes,
8822         # and then take 1/2 of the remaining memory for the read/write buffers.
8823         if [ $MEMTOTAL -gt 512 ] ;then
8824                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
8825         else
8826                 # for those poor memory-starved high-end clusters...
8827                 MEMTOTAL=$((MEMTOTAL / 2))
8828         fi
8829         echo "Mem to use for directio: $MEMTOTAL"
8830
8831         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
8832         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
8833         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
8834         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
8835                 head -n1)
8836         echo "Smallest OST: $SMALLESTOST"
8837         [[ $SMALLESTOST -lt 10240 ]] &&
8838                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
8839
8840         trap cleanup_test_78 EXIT
8841
8842         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
8843                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
8844
8845         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
8846         echo "File size: $F78SIZE"
8847         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
8848         for i in $(seq 1 $NSEQ); do
8849                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
8850                 echo directIO rdwr round $i of $NSEQ
8851                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
8852         done
8853
8854         cleanup_test_78
8855 }
8856 run_test 78 "handle large O_DIRECT writes correctly ============"
8857
8858 test_79() { # bug 12743
8859         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8860
8861         wait_delete_completed
8862
8863         BKTOTAL=$(calc_osc_kbytes kbytestotal)
8864         BKFREE=$(calc_osc_kbytes kbytesfree)
8865         BKAVAIL=$(calc_osc_kbytes kbytesavail)
8866
8867         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
8868         DFTOTAL=`echo $STRING | cut -d, -f1`
8869         DFUSED=`echo $STRING  | cut -d, -f2`
8870         DFAVAIL=`echo $STRING | cut -d, -f3`
8871         DFFREE=$(($DFTOTAL - $DFUSED))
8872
8873         ALLOWANCE=$((64 * $OSTCOUNT))
8874
8875         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
8876            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
8877                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
8878         fi
8879         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
8880            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
8881                 error "df free($DFFREE) mismatch OST free($BKFREE)"
8882         fi
8883         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
8884            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
8885                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
8886         fi
8887 }
8888 run_test 79 "df report consistency check ======================="
8889
8890 test_80() { # bug 10718
8891         remote_ost_nodsh && skip "remote OST with nodsh"
8892         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8893
8894         # relax strong synchronous semantics for slow backends like ZFS
8895         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
8896                 local soc="obdfilter.*.sync_lock_cancel"
8897                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8898
8899                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
8900                 if [ -z "$save" ]; then
8901                         soc="obdfilter.*.sync_on_lock_cancel"
8902                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8903                 fi
8904
8905                 if [ "$save" != "never" ]; then
8906                         local hosts=$(comma_list $(osts_nodes))
8907
8908                         do_nodes $hosts $LCTL set_param $soc=never
8909                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
8910                 fi
8911         fi
8912
8913         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
8914         sync; sleep 1; sync
8915         local before=$(date +%s)
8916         cancel_lru_locks osc
8917         local after=$(date +%s)
8918         local diff=$((after - before))
8919         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
8920
8921         rm -f $DIR/$tfile
8922 }
8923 run_test 80 "Page eviction is equally fast at high offsets too"
8924
8925 test_81a() { # LU-456
8926         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8927         remote_ost_nodsh && skip "remote OST with nodsh"
8928
8929         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8930         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
8931         do_facet ost1 lctl set_param fail_loc=0x80000228
8932
8933         # write should trigger a retry and success
8934         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8935         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8936         RC=$?
8937         if [ $RC -ne 0 ] ; then
8938                 error "write should success, but failed for $RC"
8939         fi
8940 }
8941 run_test 81a "OST should retry write when get -ENOSPC ==============="
8942
8943 test_81b() { # LU-456
8944         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8945         remote_ost_nodsh && skip "remote OST with nodsh"
8946
8947         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8948         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
8949         do_facet ost1 lctl set_param fail_loc=0x228
8950
8951         # write should retry several times and return -ENOSPC finally
8952         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8953         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8954         RC=$?
8955         ENOSPC=28
8956         if [ $RC -ne $ENOSPC ] ; then
8957                 error "dd should fail for -ENOSPC, but succeed."
8958         fi
8959 }
8960 run_test 81b "OST should return -ENOSPC when retry still fails ======="
8961
8962 test_82() { # LU-1031
8963         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
8964         local gid1=14091995
8965         local gid2=16022000
8966
8967         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
8968         local MULTIPID1=$!
8969         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
8970         local MULTIPID2=$!
8971         kill -USR1 $MULTIPID2
8972         sleep 2
8973         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
8974                 error "First grouplock does not block second one"
8975         else
8976                 echo "Second grouplock blocks first one"
8977         fi
8978         kill -USR1 $MULTIPID1
8979         wait $MULTIPID1
8980         wait $MULTIPID2
8981 }
8982 run_test 82 "Basic grouplock test"
8983
8984 test_99() {
8985         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
8986
8987         test_mkdir $DIR/$tdir.cvsroot
8988         chown $RUNAS_ID $DIR/$tdir.cvsroot
8989
8990         cd $TMP
8991         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
8992
8993         cd /etc/init.d
8994         # some versions of cvs import exit(1) when asked to import links or
8995         # files they can't read.  ignore those files.
8996         local toignore=$(find . -type l -printf '-I %f\n' -o \
8997                          ! -perm /4 -printf '-I %f\n')
8998         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
8999                 $tdir.reposname vtag rtag
9000
9001         cd $DIR
9002         test_mkdir $DIR/$tdir.reposname
9003         chown $RUNAS_ID $DIR/$tdir.reposname
9004         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
9005
9006         cd $DIR/$tdir.reposname
9007         $RUNAS touch foo99
9008         $RUNAS cvs add -m 'addmsg' foo99
9009         $RUNAS cvs update
9010         $RUNAS cvs commit -m 'nomsg' foo99
9011         rm -fr $DIR/$tdir.cvsroot
9012 }
9013 run_test 99 "cvs strange file/directory operations"
9014
9015 test_100() {
9016         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9017         [[ "$NETTYPE" =~ tcp ]] ||
9018                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
9019         remote_ost_nodsh && skip "remote OST with nodsh"
9020         remote_mds_nodsh && skip "remote MDS with nodsh"
9021         remote_servers ||
9022                 skip "useless for local single node setup"
9023
9024         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
9025                 [ "$PROT" != "tcp" ] && continue
9026                 RPORT=$(echo $REMOTE | cut -d: -f2)
9027                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
9028
9029                 rc=0
9030                 LPORT=`echo $LOCAL | cut -d: -f2`
9031                 if [ $LPORT -ge 1024 ]; then
9032                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
9033                         netstat -tna
9034                         error_exit "local: $LPORT > 1024, remote: $RPORT"
9035                 fi
9036         done
9037         [ "$rc" = 0 ] || error_exit "privileged port not found" )
9038 }
9039 run_test 100 "check local port using privileged port ==========="
9040
9041 function get_named_value()
9042 {
9043     local tag
9044
9045     tag=$1
9046     while read ;do
9047         line=$REPLY
9048         case $line in
9049         $tag*)
9050             echo $line | sed "s/^$tag[ ]*//"
9051             break
9052             ;;
9053         esac
9054     done
9055 }
9056
9057 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
9058                    awk '/^max_cached_mb/ { print $2 }')
9059
9060 cleanup_101a() {
9061         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
9062         trap 0
9063 }
9064
9065 test_101a() {
9066         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9067
9068         local s
9069         local discard
9070         local nreads=10000
9071         local cache_limit=32
9072
9073         $LCTL set_param -n osc.*-osc*.rpc_stats 0
9074         trap cleanup_101a EXIT
9075         $LCTL set_param -n llite.*.read_ahead_stats 0
9076         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
9077
9078         #
9079         # randomly read 10000 of 64K chunks from file 3x 32MB in size
9080         #
9081         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
9082         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
9083
9084         discard=0
9085         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
9086                 get_named_value 'read but discarded' | cut -d" " -f1); do
9087                         discard=$(($discard + $s))
9088         done
9089         cleanup_101a
9090
9091         $LCTL get_param osc.*-osc*.rpc_stats
9092         $LCTL get_param llite.*.read_ahead_stats
9093
9094         # Discard is generally zero, but sometimes a few random reads line up
9095         # and trigger larger readahead, which is wasted & leads to discards.
9096         if [[ $(($discard)) -gt $nreads ]]; then
9097                 error "too many ($discard) discarded pages"
9098         fi
9099         rm -f $DIR/$tfile || true
9100 }
9101 run_test 101a "check read-ahead for random reads"
9102
9103 setup_test101bc() {
9104         test_mkdir $DIR/$tdir
9105         local ssize=$1
9106         local FILE_LENGTH=$2
9107         STRIPE_OFFSET=0
9108
9109         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
9110
9111         local list=$(comma_list $(osts_nodes))
9112         set_osd_param $list '' read_cache_enable 0
9113         set_osd_param $list '' writethrough_cache_enable 0
9114
9115         trap cleanup_test101bc EXIT
9116         # prepare the read-ahead file
9117         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
9118
9119         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
9120                                 count=$FILE_SIZE_MB 2> /dev/null
9121
9122 }
9123
9124 cleanup_test101bc() {
9125         trap 0
9126         rm -rf $DIR/$tdir
9127         rm -f $DIR/$tfile
9128
9129         local list=$(comma_list $(osts_nodes))
9130         set_osd_param $list '' read_cache_enable 1
9131         set_osd_param $list '' writethrough_cache_enable 1
9132 }
9133
9134 calc_total() {
9135         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
9136 }
9137
9138 ra_check_101() {
9139         local READ_SIZE=$1
9140         local STRIPE_SIZE=$2
9141         local FILE_LENGTH=$3
9142         local RA_INC=1048576
9143         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
9144         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
9145                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
9146         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
9147                         get_named_value 'read but discarded' |
9148                         cut -d" " -f1 | calc_total)
9149         if [[ $DISCARD -gt $discard_limit ]]; then
9150                 $LCTL get_param llite.*.read_ahead_stats
9151                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
9152         else
9153                 echo "Read-ahead success for size ${READ_SIZE}"
9154         fi
9155 }
9156
9157 test_101b() {
9158         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9159         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9160
9161         local STRIPE_SIZE=1048576
9162         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
9163
9164         if [ $SLOW == "yes" ]; then
9165                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
9166         else
9167                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
9168         fi
9169
9170         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
9171
9172         # prepare the read-ahead file
9173         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
9174         cancel_lru_locks osc
9175         for BIDX in 2 4 8 16 32 64 128 256
9176         do
9177                 local BSIZE=$((BIDX*4096))
9178                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
9179                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
9180                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
9181                 $LCTL set_param -n llite.*.read_ahead_stats 0
9182                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
9183                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
9184                 cancel_lru_locks osc
9185                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
9186         done
9187         cleanup_test101bc
9188         true
9189 }
9190 run_test 101b "check stride-io mode read-ahead ================="
9191
9192 test_101c() {
9193         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9194
9195         local STRIPE_SIZE=1048576
9196         local FILE_LENGTH=$((STRIPE_SIZE*100))
9197         local nreads=10000
9198         local rsize=65536
9199         local osc_rpc_stats
9200
9201         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
9202
9203         cancel_lru_locks osc
9204         $LCTL set_param osc.*.rpc_stats 0
9205         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
9206         $LCTL get_param osc.*.rpc_stats
9207         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
9208                 local stats=$($LCTL get_param -n $osc_rpc_stats)
9209                 local lines=$(echo "$stats" | awk 'END {print NR;}')
9210                 local size
9211
9212                 if [ $lines -le 20 ]; then
9213                         echo "continue debug"
9214                         continue
9215                 fi
9216                 for size in 1 2 4 8; do
9217                         local rpc=$(echo "$stats" |
9218                                     awk '($1 == "'$size':") {print $2; exit; }')
9219                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
9220                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
9221                 done
9222                 echo "$osc_rpc_stats check passed!"
9223         done
9224         cleanup_test101bc
9225         true
9226 }
9227 run_test 101c "check stripe_size aligned read-ahead ================="
9228
9229 test_101d() {
9230         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9231
9232         local file=$DIR/$tfile
9233         local sz_MB=${FILESIZE_101d:-80}
9234         local ra_MB=${READAHEAD_MB:-40}
9235
9236         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
9237         [ $free_MB -lt $sz_MB ] &&
9238                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
9239
9240         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
9241         $LFS setstripe -c -1 $file || error "setstripe failed"
9242
9243         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
9244         echo Cancel LRU locks on lustre client to flush the client cache
9245         cancel_lru_locks osc
9246
9247         echo Disable read-ahead
9248         local old_RA=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
9249         $LCTL set_param -n llite.*.max_read_ahead_mb=0
9250         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_RA" EXIT
9251         $LCTL get_param -n llite.*.max_read_ahead_mb
9252
9253         echo "Reading the test file $file with read-ahead disabled"
9254         local sz_KB=$((sz_MB * 1024 / 4))
9255         # 10485760 bytes transferred in 0.000938 secs (11179579337 bytes/sec)
9256         # 104857600 bytes (105 MB) copied, 0.00876352 s, 12.0 GB/s
9257         local raOFF=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
9258                       sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
9259
9260         echo "Cancel LRU locks on lustre client to flush the client cache"
9261         cancel_lru_locks osc
9262         echo Enable read-ahead with ${ra_MB}MB
9263         $LCTL set_param -n llite.*.max_read_ahead_mb=$ra_MB
9264
9265         echo "Reading the test file $file with read-ahead enabled"
9266         local raON=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
9267                      sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
9268
9269         echo "read-ahead disabled time read $raOFF"
9270         echo "read-ahead enabled time read $raON"
9271
9272         rm -f $file
9273         wait_delete_completed
9274
9275         # use awk for this check instead of bash because it handles decimals
9276         awk "{ exit !($raOFF < 1.0 || $raOFF > $raON) }" <<<"ignore_me" ||
9277                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
9278 }
9279 run_test 101d "file read with and without read-ahead enabled"
9280
9281 test_101e() {
9282         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9283
9284         local file=$DIR/$tfile
9285         local size_KB=500  #KB
9286         local count=100
9287         local bsize=1024
9288
9289         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
9290         local need_KB=$((count * size_KB))
9291         [[ $free_KB -le $need_KB ]] &&
9292                 skip_env "Need free space $need_KB, have $free_KB"
9293
9294         echo "Creating $count ${size_KB}K test files"
9295         for ((i = 0; i < $count; i++)); do
9296                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
9297         done
9298
9299         echo "Cancel LRU locks on lustre client to flush the client cache"
9300         cancel_lru_locks $OSC
9301
9302         echo "Reset readahead stats"
9303         $LCTL set_param -n llite.*.read_ahead_stats 0
9304
9305         for ((i = 0; i < $count; i++)); do
9306                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
9307         done
9308
9309         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9310                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
9311
9312         for ((i = 0; i < $count; i++)); do
9313                 rm -rf $file.$i 2>/dev/null
9314         done
9315
9316         #10000 means 20% reads are missing in readahead
9317         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
9318 }
9319 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
9320
9321 test_101f() {
9322         which iozone || skip_env "no iozone installed"
9323
9324         local old_debug=$($LCTL get_param debug)
9325         old_debug=${old_debug#*=}
9326         $LCTL set_param debug="reada mmap"
9327
9328         # create a test file
9329         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
9330
9331         echo Cancel LRU locks on lustre client to flush the client cache
9332         cancel_lru_locks osc
9333
9334         echo Reset readahead stats
9335         $LCTL set_param -n llite.*.read_ahead_stats 0
9336
9337         echo mmap read the file with small block size
9338         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
9339                 > /dev/null 2>&1
9340
9341         echo checking missing pages
9342         $LCTL get_param llite.*.read_ahead_stats
9343         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9344                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9345
9346         $LCTL set_param debug="$old_debug"
9347         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
9348         rm -f $DIR/$tfile
9349 }
9350 run_test 101f "check mmap read performance"
9351
9352 test_101g_brw_size_test() {
9353         local mb=$1
9354         local pages=$((mb * 1048576 / PAGE_SIZE))
9355         local file=$DIR/$tfile
9356
9357         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
9358                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
9359         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
9360                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
9361                         return 2
9362         done
9363
9364         stack_trap "rm -f $file" EXIT
9365         $LCTL set_param -n osc.*.rpc_stats=0
9366
9367         # 10 RPCs should be enough for the test
9368         local count=10
9369         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
9370                 { error "dd write ${mb} MB blocks failed"; return 3; }
9371         cancel_lru_locks osc
9372         dd of=/dev/null if=$file bs=${mb}M count=$count ||
9373                 { error "dd write ${mb} MB blocks failed"; return 4; }
9374
9375         # calculate number of full-sized read and write RPCs
9376         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
9377                 sed -n '/pages per rpc/,/^$/p' |
9378                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
9379                 END { print reads,writes }'))
9380         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
9381                 return 5
9382         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
9383                 return 6
9384
9385         return 0
9386 }
9387
9388 test_101g() {
9389         remote_ost_nodsh && skip "remote OST with nodsh"
9390
9391         local rpcs
9392         local osts=$(get_facets OST)
9393         local list=$(comma_list $(osts_nodes))
9394         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
9395         local brw_size="obdfilter.*.brw_size"
9396
9397         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9398
9399         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
9400
9401         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
9402                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
9403                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
9404            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
9405                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
9406                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
9407
9408                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
9409                         suffix="M"
9410
9411                 if [[ $orig_mb -lt 16 ]]; then
9412                         save_lustre_params $osts "$brw_size" > $p
9413                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
9414                                 error "set 16MB RPC size failed"
9415
9416                         echo "remount client to enable new RPC size"
9417                         remount_client $MOUNT || error "remount_client failed"
9418                 fi
9419
9420                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
9421                 # should be able to set brw_size=12, but no rpc_stats for that
9422                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
9423         fi
9424
9425         test_101g_brw_size_test 4 || error "4MB RPC test failed"
9426
9427         if [[ $orig_mb -lt 16 ]]; then
9428                 restore_lustre_params < $p
9429                 remount_client $MOUNT || error "remount_client restore failed"
9430         fi
9431
9432         rm -f $p $DIR/$tfile
9433 }
9434 run_test 101g "Big bulk(4/16 MiB) readahead"
9435
9436 test_101h() {
9437         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9438
9439         dd if=/dev/zero of=$DIR/$tfile bs=1M count=70 ||
9440                 error "dd 70M file failed"
9441         echo Cancel LRU locks on lustre client to flush the client cache
9442         cancel_lru_locks osc
9443
9444         echo "Reset readahead stats"
9445         $LCTL set_param -n llite.*.read_ahead_stats 0
9446
9447         echo "Read 10M of data but cross 64M bundary"
9448         dd if=$DIR/$tfile of=/dev/null bs=10M skip=6 count=1
9449         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9450                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9451         [ $miss -eq 1 ] || error "expected miss 1 but got $miss"
9452         rm -f $p $DIR/$tfile
9453 }
9454 run_test 101h "Readahead should cover current read window"
9455
9456 test_101i() {
9457         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 ||
9458                 error "dd 10M file failed"
9459
9460         local max_per_file_mb=$($LCTL get_param -n \
9461                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
9462         cancel_lru_locks osc
9463         stack_trap "$LCTL set_param llite.*.max_read_ahead_per_file_mb=$max_per_file_mb"
9464         $LCTL set_param llite.*.max_read_ahead_per_file_mb=1 ||
9465                 error "set max_read_ahead_per_file_mb to 1 failed"
9466
9467         echo "Reset readahead stats"
9468         $LCTL set_param llite.*.read_ahead_stats=0
9469
9470         dd if=$DIR/$tfile of=/dev/null bs=2M
9471
9472         $LCTL get_param llite.*.read_ahead_stats
9473         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9474                      awk '/misses/ { print $2 }')
9475         [ $miss -eq 5 ] || error "expected misses 5 but got $miss"
9476         rm -f $DIR/$tfile
9477 }
9478 run_test 101i "allow current readahead to exceed reservation"
9479
9480 test_101j() {
9481         $LFS setstripe -i 0 -c 1 $DIR/$tfile ||
9482                 error "setstripe $DIR/$tfile failed"
9483         local file_size=$((1048576 * 16))
9484         local old_ra=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
9485         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_ra" EXIT
9486
9487         echo Disable read-ahead
9488         $LCTL set_param -n llite.*.max_read_ahead_mb=0
9489
9490         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$(($file_size / 1048576))
9491         for blk in $PAGE_SIZE 1048576 $file_size; do
9492                 cancel_lru_locks osc
9493                 echo "Reset readahead stats"
9494                 $LCTL set_param -n llite.*.read_ahead_stats=0
9495                 local count=$(($file_size / $blk))
9496                 dd if=$DIR/$tfile bs=$blk count=$count of=/dev/null
9497                 local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9498                              get_named_value 'failed to fast read' |
9499                              cut -d" " -f1 | calc_total)
9500                 $LCTL get_param -n llite.*.read_ahead_stats
9501                 [ $miss -eq $count ] || error "expected $count got $miss"
9502         done
9503
9504         rm -f $p $DIR/$tfile
9505 }
9506 run_test 101j "A complete read block should be submitted when no RA"
9507
9508 setup_test102() {
9509         test_mkdir $DIR/$tdir
9510         chown $RUNAS_ID $DIR/$tdir
9511         STRIPE_SIZE=65536
9512         STRIPE_OFFSET=1
9513         STRIPE_COUNT=$OSTCOUNT
9514         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
9515
9516         trap cleanup_test102 EXIT
9517         cd $DIR
9518         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
9519         cd $DIR/$tdir
9520         for num in 1 2 3 4; do
9521                 for count in $(seq 1 $STRIPE_COUNT); do
9522                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
9523                                 local size=`expr $STRIPE_SIZE \* $num`
9524                                 local file=file"$num-$idx-$count"
9525                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
9526                         done
9527                 done
9528         done
9529
9530         cd $DIR
9531         $1 tar cf $TMP/f102.tar $tdir --xattrs
9532 }
9533
9534 cleanup_test102() {
9535         trap 0
9536         rm -f $TMP/f102.tar
9537         rm -rf $DIR/d0.sanity/d102
9538 }
9539
9540 test_102a() {
9541         [ "$UID" != 0 ] && skip "must run as root"
9542         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
9543                 skip_env "must have user_xattr"
9544
9545         [ -z "$(which setfattr 2>/dev/null)" ] &&
9546                 skip_env "could not find setfattr"
9547
9548         local testfile=$DIR/$tfile
9549
9550         touch $testfile
9551         echo "set/get xattr..."
9552         setfattr -n trusted.name1 -v value1 $testfile ||
9553                 error "setfattr -n trusted.name1=value1 $testfile failed"
9554         getfattr -n trusted.name1 $testfile 2> /dev/null |
9555           grep "trusted.name1=.value1" ||
9556                 error "$testfile missing trusted.name1=value1"
9557
9558         setfattr -n user.author1 -v author1 $testfile ||
9559                 error "setfattr -n user.author1=author1 $testfile failed"
9560         getfattr -n user.author1 $testfile 2> /dev/null |
9561           grep "user.author1=.author1" ||
9562                 error "$testfile missing trusted.author1=author1"
9563
9564         echo "listxattr..."
9565         setfattr -n trusted.name2 -v value2 $testfile ||
9566                 error "$testfile unable to set trusted.name2"
9567         setfattr -n trusted.name3 -v value3 $testfile ||
9568                 error "$testfile unable to set trusted.name3"
9569         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
9570             grep "trusted.name" | wc -l) -eq 3 ] ||
9571                 error "$testfile missing 3 trusted.name xattrs"
9572
9573         setfattr -n user.author2 -v author2 $testfile ||
9574                 error "$testfile unable to set user.author2"
9575         setfattr -n user.author3 -v author3 $testfile ||
9576                 error "$testfile unable to set user.author3"
9577         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
9578             grep "user.author" | wc -l) -eq 3 ] ||
9579                 error "$testfile missing 3 user.author xattrs"
9580
9581         echo "remove xattr..."
9582         setfattr -x trusted.name1 $testfile ||
9583                 error "$testfile error deleting trusted.name1"
9584         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
9585                 error "$testfile did not delete trusted.name1 xattr"
9586
9587         setfattr -x user.author1 $testfile ||
9588                 error "$testfile error deleting user.author1"
9589         echo "set lustre special xattr ..."
9590         $LFS setstripe -c1 $testfile
9591         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
9592                 awk -F "=" '/trusted.lov/ { print $2 }' )
9593         setfattr -n "trusted.lov" -v $lovea $testfile ||
9594                 error "$testfile doesn't ignore setting trusted.lov again"
9595         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
9596                 error "$testfile allow setting invalid trusted.lov"
9597         rm -f $testfile
9598 }
9599 run_test 102a "user xattr test =================================="
9600
9601 check_102b_layout() {
9602         local layout="$*"
9603         local testfile=$DIR/$tfile
9604
9605         echo "test layout '$layout'"
9606         $LFS setstripe $layout $testfile || error "setstripe failed"
9607         $LFS getstripe -y $testfile
9608
9609         echo "get/set/list trusted.lov xattr ..." # b=10930
9610         local value=$(getfattr -n trusted.lov -e hex $testfile | grep trusted)
9611         [[ "$value" =~ "trusted.lov" ]] ||
9612                 error "can't get trusted.lov from $testfile"
9613         local stripe_count_orig=$($LFS getstripe -c $testfile) ||
9614                 error "getstripe failed"
9615
9616         $MCREATE $testfile.2 || error "mcreate $testfile.2 failed"
9617
9618         value=$(cut -d= -f2 <<<$value)
9619         # LU-13168: truncated xattr should fail if short lov_user_md header
9620         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
9621                 lens="${#value}" || lens="$(seq 4 2 ${#value})"
9622         for len in $lens; do
9623                 echo "setfattr $len $testfile.2"
9624                 setfattr -n trusted.lov -v ${value:0:$len} $testfile.2 &&
9625                         [ $len -lt 66 ] && error "short xattr len=$len worked"
9626         done
9627         local stripe_size=$($LFS getstripe -S $testfile.2)
9628         local stripe_count=$($LFS getstripe -c $testfile.2)
9629         [[ $stripe_size -eq 65536 ]] ||
9630                 error "stripe size $stripe_size != 65536"
9631         [[ $stripe_count -eq $stripe_count_orig ]] ||
9632                 error "stripe count $stripe_count != $stripe_count_orig"
9633         rm $testfile $testfile.2
9634 }
9635
9636 test_102b() {
9637         [ -z "$(which setfattr 2>/dev/null)" ] &&
9638                 skip_env "could not find setfattr"
9639         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9640
9641         # check plain layout
9642         check_102b_layout -S 65536 -i 1 -c $OSTCOUNT
9643
9644         # and also check composite layout
9645         check_102b_layout -E 1M -S 65536 -i 1 -c $OSTCOUNT -Eeof -S4M
9646
9647 }
9648 run_test 102b "getfattr/setfattr for trusted.lov EAs"
9649
9650 test_102c() {
9651         [ -z "$(which setfattr 2>/dev/null)" ] &&
9652                 skip_env "could not find setfattr"
9653         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9654
9655         # b10930: get/set/list lustre.lov xattr
9656         echo "get/set/list lustre.lov xattr ..."
9657         test_mkdir $DIR/$tdir
9658         chown $RUNAS_ID $DIR/$tdir
9659         local testfile=$DIR/$tdir/$tfile
9660         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
9661                 error "setstripe failed"
9662         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
9663                 error "getstripe failed"
9664         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
9665         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
9666
9667         local testfile2=${testfile}2
9668         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
9669                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
9670
9671         $RUNAS $MCREATE $testfile2
9672         $RUNAS setfattr -n lustre.lov -v $value $testfile2
9673         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
9674         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
9675         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
9676         [ $stripe_count -eq $STRIPECOUNT ] ||
9677                 error "stripe count $stripe_count != $STRIPECOUNT"
9678 }
9679 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
9680
9681 compare_stripe_info1() {
9682         local stripe_index_all_zero=true
9683
9684         for num in 1 2 3 4; do
9685                 for count in $(seq 1 $STRIPE_COUNT); do
9686                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
9687                                 local size=$((STRIPE_SIZE * num))
9688                                 local file=file"$num-$offset-$count"
9689                                 stripe_size=$($LFS getstripe -S $PWD/$file)
9690                                 [[ $stripe_size -ne $size ]] &&
9691                                     error "$file: size $stripe_size != $size"
9692                                 stripe_count=$($LFS getstripe -c $PWD/$file)
9693                                 # allow fewer stripes to be created, ORI-601
9694                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
9695                                     error "$file: count $stripe_count != $count"
9696                                 stripe_index=$($LFS getstripe -i $PWD/$file)
9697                                 [[ $stripe_index -ne 0 ]] &&
9698                                         stripe_index_all_zero=false
9699                         done
9700                 done
9701         done
9702         $stripe_index_all_zero &&
9703                 error "all files are being extracted starting from OST index 0"
9704         return 0
9705 }
9706
9707 have_xattrs_include() {
9708         tar --help | grep -q xattrs-include &&
9709                 echo --xattrs-include="lustre.*"
9710 }
9711
9712 test_102d() {
9713         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9714         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9715
9716         XINC=$(have_xattrs_include)
9717         setup_test102
9718         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9719         cd $DIR/$tdir/$tdir
9720         compare_stripe_info1
9721 }
9722 run_test 102d "tar restore stripe info from tarfile,not keep osts"
9723
9724 test_102f() {
9725         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9726         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9727
9728         XINC=$(have_xattrs_include)
9729         setup_test102
9730         test_mkdir $DIR/$tdir.restore
9731         cd $DIR
9732         tar cf - --xattrs $tdir | tar xf - \
9733                 -C $DIR/$tdir.restore --xattrs $XINC
9734         cd $DIR/$tdir.restore/$tdir
9735         compare_stripe_info1
9736 }
9737 run_test 102f "tar copy files, not keep osts"
9738
9739 grow_xattr() {
9740         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
9741                 skip "must have user_xattr"
9742         [ -z "$(which setfattr 2>/dev/null)" ] &&
9743                 skip_env "could not find setfattr"
9744         [ -z "$(which getfattr 2>/dev/null)" ] &&
9745                 skip_env "could not find getfattr"
9746
9747         local xsize=${1:-1024}  # in bytes
9748         local file=$DIR/$tfile
9749         local value="$(generate_string $xsize)"
9750         local xbig=trusted.big
9751         local toobig=$2
9752
9753         touch $file
9754         log "save $xbig on $file"
9755         if [ -z "$toobig" ]
9756         then
9757                 setfattr -n $xbig -v $value $file ||
9758                         error "saving $xbig on $file failed"
9759         else
9760                 setfattr -n $xbig -v $value $file &&
9761                         error "saving $xbig on $file succeeded"
9762                 return 0
9763         fi
9764
9765         local orig=$(get_xattr_value $xbig $file)
9766         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
9767
9768         local xsml=trusted.sml
9769         log "save $xsml on $file"
9770         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
9771
9772         local new=$(get_xattr_value $xbig $file)
9773         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
9774
9775         log "grow $xsml on $file"
9776         setfattr -n $xsml -v "$value" $file ||
9777                 error "growing $xsml on $file failed"
9778
9779         new=$(get_xattr_value $xbig $file)
9780         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
9781         log "$xbig still valid after growing $xsml"
9782
9783         rm -f $file
9784 }
9785
9786 test_102h() { # bug 15777
9787         grow_xattr 1024
9788 }
9789 run_test 102h "grow xattr from inside inode to external block"
9790
9791 test_102ha() {
9792         large_xattr_enabled || skip_env "ea_inode feature disabled"
9793
9794         echo "setting xattr of max xattr size: $(max_xattr_size)"
9795         grow_xattr $(max_xattr_size)
9796
9797         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
9798         echo "This should fail:"
9799         grow_xattr $(($(max_xattr_size) + 10)) 1
9800 }
9801 run_test 102ha "grow xattr from inside inode to external inode"
9802
9803 test_102i() { # bug 17038
9804         [ -z "$(which getfattr 2>/dev/null)" ] &&
9805                 skip "could not find getfattr"
9806
9807         touch $DIR/$tfile
9808         ln -s $DIR/$tfile $DIR/${tfile}link
9809         getfattr -n trusted.lov $DIR/$tfile ||
9810                 error "lgetxattr on $DIR/$tfile failed"
9811         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
9812                 grep -i "no such attr" ||
9813                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
9814         rm -f $DIR/$tfile $DIR/${tfile}link
9815 }
9816 run_test 102i "lgetxattr test on symbolic link ============"
9817
9818 test_102j() {
9819         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9820         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9821
9822         XINC=$(have_xattrs_include)
9823         setup_test102 "$RUNAS"
9824         chown $RUNAS_ID $DIR/$tdir
9825         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9826         cd $DIR/$tdir/$tdir
9827         compare_stripe_info1 "$RUNAS"
9828 }
9829 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
9830
9831 test_102k() {
9832         [ -z "$(which setfattr 2>/dev/null)" ] &&
9833                 skip "could not find setfattr"
9834
9835         touch $DIR/$tfile
9836         # b22187 just check that does not crash for regular file.
9837         setfattr -n trusted.lov $DIR/$tfile
9838         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
9839         local test_kdir=$DIR/$tdir
9840         test_mkdir $test_kdir
9841         local default_size=$($LFS getstripe -S $test_kdir)
9842         local default_count=$($LFS getstripe -c $test_kdir)
9843         local default_offset=$($LFS getstripe -i $test_kdir)
9844         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
9845                 error 'dir setstripe failed'
9846         setfattr -n trusted.lov $test_kdir
9847         local stripe_size=$($LFS getstripe -S $test_kdir)
9848         local stripe_count=$($LFS getstripe -c $test_kdir)
9849         local stripe_offset=$($LFS getstripe -i $test_kdir)
9850         [ $stripe_size -eq $default_size ] ||
9851                 error "stripe size $stripe_size != $default_size"
9852         [ $stripe_count -eq $default_count ] ||
9853                 error "stripe count $stripe_count != $default_count"
9854         [ $stripe_offset -eq $default_offset ] ||
9855                 error "stripe offset $stripe_offset != $default_offset"
9856         rm -rf $DIR/$tfile $test_kdir
9857 }
9858 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
9859
9860 test_102l() {
9861         [ -z "$(which getfattr 2>/dev/null)" ] &&
9862                 skip "could not find getfattr"
9863
9864         # LU-532 trusted. xattr is invisible to non-root
9865         local testfile=$DIR/$tfile
9866
9867         touch $testfile
9868
9869         echo "listxattr as user..."
9870         chown $RUNAS_ID $testfile
9871         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
9872             grep -q "trusted" &&
9873                 error "$testfile trusted xattrs are user visible"
9874
9875         return 0;
9876 }
9877 run_test 102l "listxattr size test =================================="
9878
9879 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
9880         local path=$DIR/$tfile
9881         touch $path
9882
9883         listxattr_size_check $path || error "listattr_size_check $path failed"
9884 }
9885 run_test 102m "Ensure listxattr fails on small bufffer ========"
9886
9887 cleanup_test102
9888
9889 getxattr() { # getxattr path name
9890         # Return the base64 encoding of the value of xattr name on path.
9891         local path=$1
9892         local name=$2
9893
9894         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
9895         # file: $path
9896         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9897         #
9898         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9899
9900         getfattr --absolute-names --encoding=base64 --name=$name $path |
9901                 awk -F= -v name=$name '$1 == name {
9902                         print substr($0, index($0, "=") + 1);
9903         }'
9904 }
9905
9906 test_102n() { # LU-4101 mdt: protect internal xattrs
9907         [ -z "$(which setfattr 2>/dev/null)" ] &&
9908                 skip "could not find setfattr"
9909         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
9910         then
9911                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
9912         fi
9913
9914         local file0=$DIR/$tfile.0
9915         local file1=$DIR/$tfile.1
9916         local xattr0=$TMP/$tfile.0
9917         local xattr1=$TMP/$tfile.1
9918         local namelist="lov lma lmv link fid version som hsm"
9919         local name
9920         local value
9921
9922         rm -rf $file0 $file1 $xattr0 $xattr1
9923         touch $file0 $file1
9924
9925         # Get 'before' xattrs of $file1.
9926         getfattr --absolute-names --dump --match=- $file1 > $xattr0
9927
9928         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
9929                 namelist+=" lfsck_namespace"
9930         for name in $namelist; do
9931                 # Try to copy xattr from $file0 to $file1.
9932                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9933
9934                 setfattr --name=trusted.$name --value="$value" $file1 ||
9935                         error "setxattr 'trusted.$name' failed"
9936
9937                 # Try to set a garbage xattr.
9938                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9939
9940                 if [[ x$name == "xlov" ]]; then
9941                         setfattr --name=trusted.lov --value="$value" $file1 &&
9942                         error "setxattr invalid 'trusted.lov' success"
9943                 else
9944                         setfattr --name=trusted.$name --value="$value" $file1 ||
9945                                 error "setxattr invalid 'trusted.$name' failed"
9946                 fi
9947
9948                 # Try to remove the xattr from $file1. We don't care if this
9949                 # appears to succeed or fail, we just don't want there to be
9950                 # any changes or crashes.
9951                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9952         done
9953
9954         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
9955         then
9956                 name="lfsck_ns"
9957                 # Try to copy xattr from $file0 to $file1.
9958                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9959
9960                 setfattr --name=trusted.$name --value="$value" $file1 ||
9961                         error "setxattr 'trusted.$name' failed"
9962
9963                 # Try to set a garbage xattr.
9964                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9965
9966                 setfattr --name=trusted.$name --value="$value" $file1 ||
9967                         error "setxattr 'trusted.$name' failed"
9968
9969                 # Try to remove the xattr from $file1. We don't care if this
9970                 # appears to succeed or fail, we just don't want there to be
9971                 # any changes or crashes.
9972                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9973         fi
9974
9975         # Get 'after' xattrs of file1.
9976         getfattr --absolute-names --dump --match=- $file1 > $xattr1
9977
9978         if ! diff $xattr0 $xattr1; then
9979                 error "before and after xattrs of '$file1' differ"
9980         fi
9981
9982         rm -rf $file0 $file1 $xattr0 $xattr1
9983
9984         return 0
9985 }
9986 run_test 102n "silently ignore setxattr on internal trusted xattrs"
9987
9988 test_102p() { # LU-4703 setxattr did not check ownership
9989         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
9990                 skip "MDS needs to be at least 2.5.56"
9991
9992         local testfile=$DIR/$tfile
9993
9994         touch $testfile
9995
9996         echo "setfacl as user..."
9997         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
9998         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
9999
10000         echo "setfattr as user..."
10001         setfacl -m "u:$RUNAS_ID:---" $testfile
10002         $RUNAS setfattr -x system.posix_acl_access $testfile
10003         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
10004 }
10005 run_test 102p "check setxattr(2) correctly fails without permission"
10006
10007 test_102q() {
10008         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
10009                 skip "MDS needs to be at least 2.6.92"
10010
10011         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
10012 }
10013 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
10014
10015 test_102r() {
10016         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
10017                 skip "MDS needs to be at least 2.6.93"
10018
10019         touch $DIR/$tfile || error "touch"
10020         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
10021         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
10022         rm $DIR/$tfile || error "rm"
10023
10024         #normal directory
10025         mkdir -p $DIR/$tdir || error "mkdir"
10026         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
10027         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
10028         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
10029                 error "$testfile error deleting user.author1"
10030         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
10031                 grep "user.$(basename $tdir)" &&
10032                 error "$tdir did not delete user.$(basename $tdir)"
10033         rmdir $DIR/$tdir || error "rmdir"
10034
10035         #striped directory
10036         test_mkdir $DIR/$tdir
10037         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
10038         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
10039         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
10040                 error "$testfile error deleting user.author1"
10041         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
10042                 grep "user.$(basename $tdir)" &&
10043                 error "$tdir did not delete user.$(basename $tdir)"
10044         rmdir $DIR/$tdir || error "rm striped dir"
10045 }
10046 run_test 102r "set EAs with empty values"
10047
10048 test_102s() {
10049         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
10050                 skip "MDS needs to be at least 2.11.52"
10051
10052         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10053
10054         save_lustre_params client "llite.*.xattr_cache" > $save
10055
10056         for cache in 0 1; do
10057                 lctl set_param llite.*.xattr_cache=$cache
10058
10059                 rm -f $DIR/$tfile
10060                 touch $DIR/$tfile || error "touch"
10061                 for prefix in lustre security system trusted user; do
10062                         # Note getxattr() may fail with 'Operation not
10063                         # supported' or 'No such attribute' depending
10064                         # on prefix and cache.
10065                         getfattr -n $prefix.n102s $DIR/$tfile &&
10066                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
10067                 done
10068         done
10069
10070         restore_lustre_params < $save
10071 }
10072 run_test 102s "getting nonexistent xattrs should fail"
10073
10074 test_102t() {
10075         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
10076                 skip "MDS needs to be at least 2.11.52"
10077
10078         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10079
10080         save_lustre_params client "llite.*.xattr_cache" > $save
10081
10082         for cache in 0 1; do
10083                 lctl set_param llite.*.xattr_cache=$cache
10084
10085                 for buf_size in 0 256; do
10086                         rm -f $DIR/$tfile
10087                         touch $DIR/$tfile || error "touch"
10088                         setfattr -n user.multiop $DIR/$tfile
10089                         $MULTIOP $DIR/$tfile oa$buf_size ||
10090                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
10091                 done
10092         done
10093
10094         restore_lustre_params < $save
10095 }
10096 run_test 102t "zero length xattr values handled correctly"
10097
10098 run_acl_subtest()
10099 {
10100     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
10101     return $?
10102 }
10103
10104 test_103a() {
10105         [ "$UID" != 0 ] && skip "must run as root"
10106         $GSS && skip_env "could not run under gss"
10107         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
10108                 skip_env "must have acl enabled"
10109         [ -z "$(which setfacl 2>/dev/null)" ] &&
10110                 skip_env "could not find setfacl"
10111         remote_mds_nodsh && skip "remote MDS with nodsh"
10112
10113         gpasswd -a daemon bin                           # LU-5641
10114         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
10115
10116         declare -a identity_old
10117
10118         for num in $(seq $MDSCOUNT); do
10119                 switch_identity $num true || identity_old[$num]=$?
10120         done
10121
10122         SAVE_UMASK=$(umask)
10123         umask 0022
10124         mkdir -p $DIR/$tdir
10125         cd $DIR/$tdir
10126
10127         echo "performing cp ..."
10128         run_acl_subtest cp || error "run_acl_subtest cp failed"
10129         echo "performing getfacl-noacl..."
10130         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
10131         echo "performing misc..."
10132         run_acl_subtest misc || error  "misc test failed"
10133         echo "performing permissions..."
10134         run_acl_subtest permissions || error "permissions failed"
10135         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
10136         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
10137                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
10138                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
10139         then
10140                 echo "performing permissions xattr..."
10141                 run_acl_subtest permissions_xattr ||
10142                         error "permissions_xattr failed"
10143         fi
10144         echo "performing setfacl..."
10145         run_acl_subtest setfacl || error  "setfacl test failed"
10146
10147         # inheritance test got from HP
10148         echo "performing inheritance..."
10149         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
10150         chmod +x make-tree || error "chmod +x failed"
10151         run_acl_subtest inheritance || error "inheritance test failed"
10152         rm -f make-tree
10153
10154         echo "LU-974 ignore umask when acl is enabled..."
10155         run_acl_subtest 974 || error "LU-974 umask test failed"
10156         if [ $MDSCOUNT -ge 2 ]; then
10157                 run_acl_subtest 974_remote ||
10158                         error "LU-974 umask test failed under remote dir"
10159         fi
10160
10161         echo "LU-2561 newly created file is same size as directory..."
10162         if [ "$mds1_FSTYPE" != "zfs" ]; then
10163                 run_acl_subtest 2561 || error "LU-2561 test failed"
10164         else
10165                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
10166         fi
10167
10168         run_acl_subtest 4924 || error "LU-4924 test failed"
10169
10170         cd $SAVE_PWD
10171         umask $SAVE_UMASK
10172
10173         for num in $(seq $MDSCOUNT); do
10174                 if [ "${identity_old[$num]}" = 1 ]; then
10175                         switch_identity $num false || identity_old[$num]=$?
10176                 fi
10177         done
10178 }
10179 run_test 103a "acl test"
10180
10181 test_103b() {
10182         declare -a pids
10183         local U
10184
10185         for U in {0..511}; do
10186                 {
10187                 local O=$(printf "%04o" $U)
10188
10189                 umask $(printf "%04o" $((511 ^ $O)))
10190                 $LFS setstripe -c 1 $DIR/$tfile.s$O
10191                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
10192
10193                 (( $S == ($O & 0666) )) ||
10194                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
10195
10196                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
10197                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
10198                 (( $S == ($O & 0666) )) ||
10199                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
10200
10201                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
10202                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
10203                 (( $S == ($O & 0666) )) ||
10204                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
10205                 rm -f $DIR/$tfile.[smp]$0
10206                 } &
10207                 local pid=$!
10208
10209                 # limit the concurrently running threads to 64. LU-11878
10210                 local idx=$((U % 64))
10211                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
10212                 pids[idx]=$pid
10213         done
10214         wait
10215 }
10216 run_test 103b "umask lfs setstripe"
10217
10218 test_103c() {
10219         mkdir -p $DIR/$tdir
10220         cp -rp $DIR/$tdir $DIR/$tdir.bak
10221
10222         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
10223                 error "$DIR/$tdir shouldn't contain default ACL"
10224         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
10225                 error "$DIR/$tdir.bak shouldn't contain default ACL"
10226         true
10227 }
10228 run_test 103c "'cp -rp' won't set empty acl"
10229
10230 test_104a() {
10231         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10232
10233         touch $DIR/$tfile
10234         lfs df || error "lfs df failed"
10235         lfs df -ih || error "lfs df -ih failed"
10236         lfs df -h $DIR || error "lfs df -h $DIR failed"
10237         lfs df -i $DIR || error "lfs df -i $DIR failed"
10238         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
10239         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
10240
10241         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
10242         lctl --device %$OSC deactivate
10243         lfs df || error "lfs df with deactivated OSC failed"
10244         lctl --device %$OSC activate
10245         # wait the osc back to normal
10246         wait_osc_import_ready client ost
10247
10248         lfs df || error "lfs df with reactivated OSC failed"
10249         rm -f $DIR/$tfile
10250 }
10251 run_test 104a "lfs df [-ih] [path] test ========================="
10252
10253 test_104b() {
10254         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10255         [ $RUNAS_ID -eq $UID ] &&
10256                 skip_env "RUNAS_ID = UID = $UID -- skipping"
10257
10258         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
10259                         grep "Permission denied" | wc -l)))
10260         if [ $denied_cnt -ne 0 ]; then
10261                 error "lfs check servers test failed"
10262         fi
10263 }
10264 run_test 104b "$RUNAS lfs check servers test ===================="
10265
10266 test_105a() {
10267         # doesn't work on 2.4 kernels
10268         touch $DIR/$tfile
10269         if $(flock_is_enabled); then
10270                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
10271         else
10272                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
10273         fi
10274         rm -f $DIR/$tfile
10275 }
10276 run_test 105a "flock when mounted without -o flock test ========"
10277
10278 test_105b() {
10279         touch $DIR/$tfile
10280         if $(flock_is_enabled); then
10281                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
10282         else
10283                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
10284         fi
10285         rm -f $DIR/$tfile
10286 }
10287 run_test 105b "fcntl when mounted without -o flock test ========"
10288
10289 test_105c() {
10290         touch $DIR/$tfile
10291         if $(flock_is_enabled); then
10292                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
10293         else
10294                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
10295         fi
10296         rm -f $DIR/$tfile
10297 }
10298 run_test 105c "lockf when mounted without -o flock test"
10299
10300 test_105d() { # bug 15924
10301         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10302
10303         test_mkdir $DIR/$tdir
10304         flock_is_enabled || skip_env "mount w/o flock enabled"
10305         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
10306         $LCTL set_param fail_loc=0x80000315
10307         flocks_test 2 $DIR/$tdir
10308 }
10309 run_test 105d "flock race (should not freeze) ========"
10310
10311 test_105e() { # bug 22660 && 22040
10312         flock_is_enabled || skip_env "mount w/o flock enabled"
10313
10314         touch $DIR/$tfile
10315         flocks_test 3 $DIR/$tfile
10316 }
10317 run_test 105e "Two conflicting flocks from same process"
10318
10319 test_106() { #bug 10921
10320         test_mkdir $DIR/$tdir
10321         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
10322         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
10323 }
10324 run_test 106 "attempt exec of dir followed by chown of that dir"
10325
10326 test_107() {
10327         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10328
10329         CDIR=`pwd`
10330         local file=core
10331
10332         cd $DIR
10333         rm -f $file
10334
10335         local save_pattern=$(sysctl -n kernel.core_pattern)
10336         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
10337         sysctl -w kernel.core_pattern=$file
10338         sysctl -w kernel.core_uses_pid=0
10339
10340         ulimit -c unlimited
10341         sleep 60 &
10342         SLEEPPID=$!
10343
10344         sleep 1
10345
10346         kill -s 11 $SLEEPPID
10347         wait $SLEEPPID
10348         if [ -e $file ]; then
10349                 size=`stat -c%s $file`
10350                 [ $size -eq 0 ] && error "Fail to create core file $file"
10351         else
10352                 error "Fail to create core file $file"
10353         fi
10354         rm -f $file
10355         sysctl -w kernel.core_pattern=$save_pattern
10356         sysctl -w kernel.core_uses_pid=$save_uses_pid
10357         cd $CDIR
10358 }
10359 run_test 107 "Coredump on SIG"
10360
10361 test_110() {
10362         test_mkdir $DIR/$tdir
10363         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
10364         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
10365                 error "mkdir with 256 char should fail, but did not"
10366         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
10367                 error "create with 255 char failed"
10368         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
10369                 error "create with 256 char should fail, but did not"
10370
10371         ls -l $DIR/$tdir
10372         rm -rf $DIR/$tdir
10373 }
10374 run_test 110 "filename length checking"
10375
10376 #
10377 # Purpose: To verify dynamic thread (OSS) creation.
10378 #
10379 test_115() {
10380         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10381         remote_ost_nodsh && skip "remote OST with nodsh"
10382
10383         # Lustre does not stop service threads once they are started.
10384         # Reset number of running threads to default.
10385         stopall
10386         setupall
10387
10388         local OSTIO_pre
10389         local save_params="$TMP/sanity-$TESTNAME.parameters"
10390
10391         # Get ll_ost_io count before I/O
10392         OSTIO_pre=$(do_facet ost1 \
10393                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
10394         # Exit if lustre is not running (ll_ost_io not running).
10395         [ -z "$OSTIO_pre" ] && error "no OSS threads"
10396
10397         echo "Starting with $OSTIO_pre threads"
10398         local thread_max=$((OSTIO_pre * 2))
10399         local rpc_in_flight=$((thread_max * 2))
10400         # Number of I/O Process proposed to be started.
10401         local nfiles
10402         local facets=$(get_facets OST)
10403
10404         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
10405         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
10406
10407         # Set in_flight to $rpc_in_flight
10408         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
10409                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
10410         nfiles=${rpc_in_flight}
10411         # Set ost thread_max to $thread_max
10412         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
10413
10414         # 5 Minutes should be sufficient for max number of OSS
10415         # threads(thread_max) to be created.
10416         local timeout=300
10417
10418         # Start I/O.
10419         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
10420         test_mkdir $DIR/$tdir
10421         for i in $(seq $nfiles); do
10422                 local file=$DIR/$tdir/${tfile}-$i
10423                 $LFS setstripe -c -1 -i 0 $file
10424                 ($WTL $file $timeout)&
10425         done
10426
10427         # I/O Started - Wait for thread_started to reach thread_max or report
10428         # error if thread_started is more than thread_max.
10429         echo "Waiting for thread_started to reach thread_max"
10430         local thread_started=0
10431         local end_time=$((SECONDS + timeout))
10432
10433         while [ $SECONDS -le $end_time ] ; do
10434                 echo -n "."
10435                 # Get ost i/o thread_started count.
10436                 thread_started=$(do_facet ost1 \
10437                         "$LCTL get_param \
10438                         ost.OSS.ost_io.threads_started | cut -d= -f2")
10439                 # Break out if thread_started is equal/greater than thread_max
10440                 if [[ $thread_started -ge $thread_max ]]; then
10441                         echo ll_ost_io thread_started $thread_started, \
10442                                 equal/greater than thread_max $thread_max
10443                         break
10444                 fi
10445                 sleep 1
10446         done
10447
10448         # Cleanup - We have the numbers, Kill i/o jobs if running.
10449         jobcount=($(jobs -p))
10450         for i in $(seq 0 $((${#jobcount[@]}-1)))
10451         do
10452                 kill -9 ${jobcount[$i]}
10453                 if [ $? -ne 0 ] ; then
10454                         echo Warning: \
10455                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
10456                 fi
10457         done
10458
10459         # Cleanup files left by WTL binary.
10460         for i in $(seq $nfiles); do
10461                 local file=$DIR/$tdir/${tfile}-$i
10462                 rm -rf $file
10463                 if [ $? -ne 0 ] ; then
10464                         echo "Warning: Failed to delete file $file"
10465                 fi
10466         done
10467
10468         restore_lustre_params <$save_params
10469         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
10470
10471         # Error out if no new thread has started or Thread started is greater
10472         # than thread max.
10473         if [[ $thread_started -le $OSTIO_pre ||
10474                         $thread_started -gt $thread_max ]]; then
10475                 error "ll_ost_io: thread_started $thread_started" \
10476                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
10477                       "No new thread started or thread started greater " \
10478                       "than thread_max."
10479         fi
10480 }
10481 run_test 115 "verify dynamic thread creation===================="
10482
10483 free_min_max () {
10484         wait_delete_completed
10485         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
10486         echo "OST kbytes available: ${AVAIL[@]}"
10487         MAXV=${AVAIL[0]}
10488         MAXI=0
10489         MINV=${AVAIL[0]}
10490         MINI=0
10491         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
10492                 #echo OST $i: ${AVAIL[i]}kb
10493                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
10494                         MAXV=${AVAIL[i]}
10495                         MAXI=$i
10496                 fi
10497                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
10498                         MINV=${AVAIL[i]}
10499                         MINI=$i
10500                 fi
10501         done
10502         echo "Min free space: OST $MINI: $MINV"
10503         echo "Max free space: OST $MAXI: $MAXV"
10504 }
10505
10506 test_116a() { # was previously test_116()
10507         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10508         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10509         remote_mds_nodsh && skip "remote MDS with nodsh"
10510
10511         echo -n "Free space priority "
10512         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
10513                 head -n1
10514         declare -a AVAIL
10515         free_min_max
10516
10517         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
10518         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
10519         trap simple_cleanup_common EXIT
10520
10521         # Check if we need to generate uneven OSTs
10522         test_mkdir -p $DIR/$tdir/OST${MINI}
10523         local FILL=$((MINV / 4))
10524         local DIFF=$((MAXV - MINV))
10525         local DIFF2=$((DIFF * 100 / MINV))
10526
10527         local threshold=$(do_facet $SINGLEMDS \
10528                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
10529         threshold=${threshold%%%}
10530         echo -n "Check for uneven OSTs: "
10531         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
10532
10533         if [[ $DIFF2 -gt $threshold ]]; then
10534                 echo "ok"
10535                 echo "Don't need to fill OST$MINI"
10536         else
10537                 # generate uneven OSTs. Write 2% over the QOS threshold value
10538                 echo "no"
10539                 DIFF=$((threshold - DIFF2 + 2))
10540                 DIFF2=$((MINV * DIFF / 100))
10541                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
10542                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
10543                         error "setstripe failed"
10544                 DIFF=$((DIFF2 / 2048))
10545                 i=0
10546                 while [ $i -lt $DIFF ]; do
10547                         i=$((i + 1))
10548                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
10549                                 bs=2M count=1 2>/dev/null
10550                         echo -n .
10551                 done
10552                 echo .
10553                 sync
10554                 sleep_maxage
10555                 free_min_max
10556         fi
10557
10558         DIFF=$((MAXV - MINV))
10559         DIFF2=$((DIFF * 100 / MINV))
10560         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
10561         if [ $DIFF2 -gt $threshold ]; then
10562                 echo "ok"
10563         else
10564                 echo "failed - QOS mode won't be used"
10565                 simple_cleanup_common
10566                 skip "QOS imbalance criteria not met"
10567         fi
10568
10569         MINI1=$MINI
10570         MINV1=$MINV
10571         MAXI1=$MAXI
10572         MAXV1=$MAXV
10573
10574         # now fill using QOS
10575         $LFS setstripe -c 1 $DIR/$tdir
10576         FILL=$((FILL / 200))
10577         if [ $FILL -gt 600 ]; then
10578                 FILL=600
10579         fi
10580         echo "writing $FILL files to QOS-assigned OSTs"
10581         i=0
10582         while [ $i -lt $FILL ]; do
10583                 i=$((i + 1))
10584                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
10585                         count=1 2>/dev/null
10586                 echo -n .
10587         done
10588         echo "wrote $i 200k files"
10589         sync
10590         sleep_maxage
10591
10592         echo "Note: free space may not be updated, so measurements might be off"
10593         free_min_max
10594         DIFF2=$((MAXV - MINV))
10595         echo "free space delta: orig $DIFF final $DIFF2"
10596         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
10597         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
10598         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
10599         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
10600         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
10601         if [[ $DIFF -gt 0 ]]; then
10602                 FILL=$((DIFF2 * 100 / DIFF - 100))
10603                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
10604         fi
10605
10606         # Figure out which files were written where
10607         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10608                awk '/'$MINI1': / {print $2; exit}')
10609         echo $UUID
10610         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10611         echo "$MINC files created on smaller OST $MINI1"
10612         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10613                awk '/'$MAXI1': / {print $2; exit}')
10614         echo $UUID
10615         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10616         echo "$MAXC files created on larger OST $MAXI1"
10617         if [[ $MINC -gt 0 ]]; then
10618                 FILL=$((MAXC * 100 / MINC - 100))
10619                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
10620         fi
10621         [[ $MAXC -gt $MINC ]] ||
10622                 error_ignore LU-9 "stripe QOS didn't balance free space"
10623         simple_cleanup_common
10624 }
10625 run_test 116a "stripe QOS: free space balance ==================="
10626
10627 test_116b() { # LU-2093
10628         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10629         remote_mds_nodsh && skip "remote MDS with nodsh"
10630
10631 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
10632         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
10633                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
10634         [ -z "$old_rr" ] && skip "no QOS"
10635         do_facet $SINGLEMDS lctl set_param \
10636                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
10637         mkdir -p $DIR/$tdir
10638         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
10639         createmany -o $DIR/$tdir/f- 20 || error "can't create"
10640         do_facet $SINGLEMDS lctl set_param fail_loc=0
10641         rm -rf $DIR/$tdir
10642         do_facet $SINGLEMDS lctl set_param \
10643                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
10644 }
10645 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
10646
10647 test_117() # bug 10891
10648 {
10649         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10650
10651         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
10652         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
10653         lctl set_param fail_loc=0x21e
10654         > $DIR/$tfile || error "truncate failed"
10655         lctl set_param fail_loc=0
10656         echo "Truncate succeeded."
10657         rm -f $DIR/$tfile
10658 }
10659 run_test 117 "verify osd extend =========="
10660
10661 NO_SLOW_RESENDCOUNT=4
10662 export OLD_RESENDCOUNT=""
10663 set_resend_count () {
10664         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
10665         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
10666         lctl set_param -n $PROC_RESENDCOUNT $1
10667         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
10668 }
10669
10670 # for reduce test_118* time (b=14842)
10671 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10672
10673 # Reset async IO behavior after error case
10674 reset_async() {
10675         FILE=$DIR/reset_async
10676
10677         # Ensure all OSCs are cleared
10678         $LFS setstripe -c -1 $FILE
10679         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
10680         sync
10681         rm $FILE
10682 }
10683
10684 test_118a() #bug 11710
10685 {
10686         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10687
10688         reset_async
10689
10690         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10691         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10692         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10693
10694         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10695                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10696                 return 1;
10697         fi
10698         rm -f $DIR/$tfile
10699 }
10700 run_test 118a "verify O_SYNC works =========="
10701
10702 test_118b()
10703 {
10704         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10705         remote_ost_nodsh && skip "remote OST with nodsh"
10706
10707         reset_async
10708
10709         #define OBD_FAIL_SRV_ENOENT 0x217
10710         set_nodes_failloc "$(osts_nodes)" 0x217
10711         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10712         RC=$?
10713         set_nodes_failloc "$(osts_nodes)" 0
10714         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10715         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10716                     grep -c writeback)
10717
10718         if [[ $RC -eq 0 ]]; then
10719                 error "Must return error due to dropped pages, rc=$RC"
10720                 return 1;
10721         fi
10722
10723         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10724                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10725                 return 1;
10726         fi
10727
10728         echo "Dirty pages not leaked on ENOENT"
10729
10730         # Due to the above error the OSC will issue all RPCs syncronously
10731         # until a subsequent RPC completes successfully without error.
10732         $MULTIOP $DIR/$tfile Ow4096yc
10733         rm -f $DIR/$tfile
10734
10735         return 0
10736 }
10737 run_test 118b "Reclaim dirty pages on fatal error =========="
10738
10739 test_118c()
10740 {
10741         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10742
10743         # for 118c, restore the original resend count, LU-1940
10744         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
10745                                 set_resend_count $OLD_RESENDCOUNT
10746         remote_ost_nodsh && skip "remote OST with nodsh"
10747
10748         reset_async
10749
10750         #define OBD_FAIL_OST_EROFS               0x216
10751         set_nodes_failloc "$(osts_nodes)" 0x216
10752
10753         # multiop should block due to fsync until pages are written
10754         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10755         MULTIPID=$!
10756         sleep 1
10757
10758         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10759                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10760         fi
10761
10762         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10763                     grep -c writeback)
10764         if [[ $WRITEBACK -eq 0 ]]; then
10765                 error "No page in writeback, writeback=$WRITEBACK"
10766         fi
10767
10768         set_nodes_failloc "$(osts_nodes)" 0
10769         wait $MULTIPID
10770         RC=$?
10771         if [[ $RC -ne 0 ]]; then
10772                 error "Multiop fsync failed, rc=$RC"
10773         fi
10774
10775         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10776         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10777                     grep -c writeback)
10778         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10779                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10780         fi
10781
10782         rm -f $DIR/$tfile
10783         echo "Dirty pages flushed via fsync on EROFS"
10784         return 0
10785 }
10786 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
10787
10788 # continue to use small resend count to reduce test_118* time (b=14842)
10789 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10790
10791 test_118d()
10792 {
10793         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10794         remote_ost_nodsh && skip "remote OST with nodsh"
10795
10796         reset_async
10797
10798         #define OBD_FAIL_OST_BRW_PAUSE_BULK
10799         set_nodes_failloc "$(osts_nodes)" 0x214
10800         # multiop should block due to fsync until pages are written
10801         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10802         MULTIPID=$!
10803         sleep 1
10804
10805         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10806                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10807         fi
10808
10809         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10810                     grep -c writeback)
10811         if [[ $WRITEBACK -eq 0 ]]; then
10812                 error "No page in writeback, writeback=$WRITEBACK"
10813         fi
10814
10815         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
10816         set_nodes_failloc "$(osts_nodes)" 0
10817
10818         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10819         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10820                     grep -c writeback)
10821         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10822                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10823         fi
10824
10825         rm -f $DIR/$tfile
10826         echo "Dirty pages gaurenteed flushed via fsync"
10827         return 0
10828 }
10829 run_test 118d "Fsync validation inject a delay of the bulk =========="
10830
10831 test_118f() {
10832         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10833
10834         reset_async
10835
10836         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
10837         lctl set_param fail_loc=0x8000040a
10838
10839         # Should simulate EINVAL error which is fatal
10840         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10841         RC=$?
10842         if [[ $RC -eq 0 ]]; then
10843                 error "Must return error due to dropped pages, rc=$RC"
10844         fi
10845
10846         lctl set_param fail_loc=0x0
10847
10848         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10849         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10850         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10851                     grep -c writeback)
10852         if [[ $LOCKED -ne 0 ]]; then
10853                 error "Locked pages remain in cache, locked=$LOCKED"
10854         fi
10855
10856         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10857                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10858         fi
10859
10860         rm -f $DIR/$tfile
10861         echo "No pages locked after fsync"
10862
10863         reset_async
10864         return 0
10865 }
10866 run_test 118f "Simulate unrecoverable OSC side error =========="
10867
10868 test_118g() {
10869         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10870
10871         reset_async
10872
10873         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
10874         lctl set_param fail_loc=0x406
10875
10876         # simulate local -ENOMEM
10877         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10878         RC=$?
10879
10880         lctl set_param fail_loc=0
10881         if [[ $RC -eq 0 ]]; then
10882                 error "Must return error due to dropped pages, rc=$RC"
10883         fi
10884
10885         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10886         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10887         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10888                         grep -c writeback)
10889         if [[ $LOCKED -ne 0 ]]; then
10890                 error "Locked pages remain in cache, locked=$LOCKED"
10891         fi
10892
10893         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10894                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10895         fi
10896
10897         rm -f $DIR/$tfile
10898         echo "No pages locked after fsync"
10899
10900         reset_async
10901         return 0
10902 }
10903 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
10904
10905 test_118h() {
10906         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10907         remote_ost_nodsh && skip "remote OST with nodsh"
10908
10909         reset_async
10910
10911         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10912         set_nodes_failloc "$(osts_nodes)" 0x20e
10913         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10914         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10915         RC=$?
10916
10917         set_nodes_failloc "$(osts_nodes)" 0
10918         if [[ $RC -eq 0 ]]; then
10919                 error "Must return error due to dropped pages, rc=$RC"
10920         fi
10921
10922         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10923         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10924         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10925                     grep -c writeback)
10926         if [[ $LOCKED -ne 0 ]]; then
10927                 error "Locked pages remain in cache, locked=$LOCKED"
10928         fi
10929
10930         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10931                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10932         fi
10933
10934         rm -f $DIR/$tfile
10935         echo "No pages locked after fsync"
10936
10937         return 0
10938 }
10939 run_test 118h "Verify timeout in handling recoverables errors  =========="
10940
10941 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10942
10943 test_118i() {
10944         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10945         remote_ost_nodsh && skip "remote OST with nodsh"
10946
10947         reset_async
10948
10949         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10950         set_nodes_failloc "$(osts_nodes)" 0x20e
10951
10952         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10953         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10954         PID=$!
10955         sleep 5
10956         set_nodes_failloc "$(osts_nodes)" 0
10957
10958         wait $PID
10959         RC=$?
10960         if [[ $RC -ne 0 ]]; then
10961                 error "got error, but should be not, rc=$RC"
10962         fi
10963
10964         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10965         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10966         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10967         if [[ $LOCKED -ne 0 ]]; then
10968                 error "Locked pages remain in cache, locked=$LOCKED"
10969         fi
10970
10971         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10972                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10973         fi
10974
10975         rm -f $DIR/$tfile
10976         echo "No pages locked after fsync"
10977
10978         return 0
10979 }
10980 run_test 118i "Fix error before timeout in recoverable error  =========="
10981
10982 [ "$SLOW" = "no" ] && set_resend_count 4
10983
10984 test_118j() {
10985         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10986         remote_ost_nodsh && skip "remote OST with nodsh"
10987
10988         reset_async
10989
10990         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
10991         set_nodes_failloc "$(osts_nodes)" 0x220
10992
10993         # return -EIO from OST
10994         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10995         RC=$?
10996         set_nodes_failloc "$(osts_nodes)" 0x0
10997         if [[ $RC -eq 0 ]]; then
10998                 error "Must return error due to dropped pages, rc=$RC"
10999         fi
11000
11001         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
11002         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11003         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
11004         if [[ $LOCKED -ne 0 ]]; then
11005                 error "Locked pages remain in cache, locked=$LOCKED"
11006         fi
11007
11008         # in recoverable error on OST we want resend and stay until it finished
11009         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11010                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11011         fi
11012
11013         rm -f $DIR/$tfile
11014         echo "No pages locked after fsync"
11015
11016         return 0
11017 }
11018 run_test 118j "Simulate unrecoverable OST side error =========="
11019
11020 test_118k()
11021 {
11022         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11023         remote_ost_nodsh && skip "remote OSTs with nodsh"
11024
11025         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
11026         set_nodes_failloc "$(osts_nodes)" 0x20e
11027         test_mkdir $DIR/$tdir
11028
11029         for ((i=0;i<10;i++)); do
11030                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
11031                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
11032                 SLEEPPID=$!
11033                 sleep 0.500s
11034                 kill $SLEEPPID
11035                 wait $SLEEPPID
11036         done
11037
11038         set_nodes_failloc "$(osts_nodes)" 0
11039         rm -rf $DIR/$tdir
11040 }
11041 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
11042
11043 test_118l() # LU-646
11044 {
11045         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11046
11047         test_mkdir $DIR/$tdir
11048         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
11049         rm -rf $DIR/$tdir
11050 }
11051 run_test 118l "fsync dir"
11052
11053 test_118m() # LU-3066
11054 {
11055         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11056
11057         test_mkdir $DIR/$tdir
11058         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
11059         rm -rf $DIR/$tdir
11060 }
11061 run_test 118m "fdatasync dir ========="
11062
11063 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
11064
11065 test_118n()
11066 {
11067         local begin
11068         local end
11069
11070         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11071         remote_ost_nodsh && skip "remote OSTs with nodsh"
11072
11073         # Sleep to avoid a cached response.
11074         #define OBD_STATFS_CACHE_SECONDS 1
11075         sleep 2
11076
11077         # Inject a 10 second delay in the OST_STATFS handler.
11078         #define OBD_FAIL_OST_STATFS_DELAY 0x242
11079         set_nodes_failloc "$(osts_nodes)" 0x242
11080
11081         begin=$SECONDS
11082         stat --file-system $MOUNT > /dev/null
11083         end=$SECONDS
11084
11085         set_nodes_failloc "$(osts_nodes)" 0
11086
11087         if ((end - begin > 20)); then
11088             error "statfs took $((end - begin)) seconds, expected 10"
11089         fi
11090 }
11091 run_test 118n "statfs() sends OST_STATFS requests in parallel"
11092
11093 test_119a() # bug 11737
11094 {
11095         BSIZE=$((512 * 1024))
11096         directio write $DIR/$tfile 0 1 $BSIZE
11097         # We ask to read two blocks, which is more than a file size.
11098         # directio will indicate an error when requested and actual
11099         # sizes aren't equeal (a normal situation in this case) and
11100         # print actual read amount.
11101         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
11102         if [ "$NOB" != "$BSIZE" ]; then
11103                 error "read $NOB bytes instead of $BSIZE"
11104         fi
11105         rm -f $DIR/$tfile
11106 }
11107 run_test 119a "Short directIO read must return actual read amount"
11108
11109 test_119b() # bug 11737
11110 {
11111         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11112
11113         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
11114         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
11115         sync
11116         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
11117                 error "direct read failed"
11118         rm -f $DIR/$tfile
11119 }
11120 run_test 119b "Sparse directIO read must return actual read amount"
11121
11122 test_119c() # bug 13099
11123 {
11124         BSIZE=1048576
11125         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
11126         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
11127         rm -f $DIR/$tfile
11128 }
11129 run_test 119c "Testing for direct read hitting hole"
11130
11131 test_119d() # bug 15950
11132 {
11133         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11134
11135         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
11136         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
11137         BSIZE=1048576
11138         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
11139         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
11140         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
11141         lctl set_param fail_loc=0x40d
11142         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
11143         pid_dio=$!
11144         sleep 1
11145         cat $DIR/$tfile > /dev/null &
11146         lctl set_param fail_loc=0
11147         pid_reads=$!
11148         wait $pid_dio
11149         log "the DIO writes have completed, now wait for the reads (should not block very long)"
11150         sleep 2
11151         [ -n "`ps h -p $pid_reads -o comm`" ] && \
11152         error "the read rpcs have not completed in 2s"
11153         rm -f $DIR/$tfile
11154         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
11155 }
11156 run_test 119d "The DIO path should try to send a new rpc once one is completed"
11157
11158 test_120a() {
11159         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11160         remote_mds_nodsh && skip "remote MDS with nodsh"
11161         test_mkdir -i0 -c1 $DIR/$tdir
11162         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11163                 skip_env "no early lock cancel on server"
11164
11165         lru_resize_disable mdc
11166         lru_resize_disable osc
11167         cancel_lru_locks mdc
11168         # asynchronous object destroy at MDT could cause bl ast to client
11169         cancel_lru_locks osc
11170
11171         stat $DIR/$tdir > /dev/null
11172         can1=$(do_facet mds1 \
11173                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11174                awk '/ldlm_cancel/ {print $2}')
11175         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11176                awk '/ldlm_bl_callback/ {print $2}')
11177         test_mkdir -i0 -c1 $DIR/$tdir/d1
11178         can2=$(do_facet mds1 \
11179                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11180                awk '/ldlm_cancel/ {print $2}')
11181         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11182                awk '/ldlm_bl_callback/ {print $2}')
11183         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11184         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11185         lru_resize_enable mdc
11186         lru_resize_enable osc
11187 }
11188 run_test 120a "Early Lock Cancel: mkdir test"
11189
11190 test_120b() {
11191         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11192         remote_mds_nodsh && skip "remote MDS with nodsh"
11193         test_mkdir $DIR/$tdir
11194         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11195                 skip_env "no early lock cancel on server"
11196
11197         lru_resize_disable mdc
11198         lru_resize_disable osc
11199         cancel_lru_locks mdc
11200         stat $DIR/$tdir > /dev/null
11201         can1=$(do_facet $SINGLEMDS \
11202                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11203                awk '/ldlm_cancel/ {print $2}')
11204         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11205                awk '/ldlm_bl_callback/ {print $2}')
11206         touch $DIR/$tdir/f1
11207         can2=$(do_facet $SINGLEMDS \
11208                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11209                awk '/ldlm_cancel/ {print $2}')
11210         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11211                awk '/ldlm_bl_callback/ {print $2}')
11212         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11213         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11214         lru_resize_enable mdc
11215         lru_resize_enable osc
11216 }
11217 run_test 120b "Early Lock Cancel: create test"
11218
11219 test_120c() {
11220         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11221         remote_mds_nodsh && skip "remote MDS with nodsh"
11222         test_mkdir -i0 -c1 $DIR/$tdir
11223         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11224                 skip "no early lock cancel on server"
11225
11226         lru_resize_disable mdc
11227         lru_resize_disable osc
11228         test_mkdir -i0 -c1 $DIR/$tdir/d1
11229         test_mkdir -i0 -c1 $DIR/$tdir/d2
11230         touch $DIR/$tdir/d1/f1
11231         cancel_lru_locks mdc
11232         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
11233         can1=$(do_facet mds1 \
11234                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11235                awk '/ldlm_cancel/ {print $2}')
11236         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11237                awk '/ldlm_bl_callback/ {print $2}')
11238         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
11239         can2=$(do_facet mds1 \
11240                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11241                awk '/ldlm_cancel/ {print $2}')
11242         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11243                awk '/ldlm_bl_callback/ {print $2}')
11244         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11245         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11246         lru_resize_enable mdc
11247         lru_resize_enable osc
11248 }
11249 run_test 120c "Early Lock Cancel: link test"
11250
11251 test_120d() {
11252         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11253         remote_mds_nodsh && skip "remote MDS with nodsh"
11254         test_mkdir -i0 -c1 $DIR/$tdir
11255         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11256                 skip_env "no early lock cancel on server"
11257
11258         lru_resize_disable mdc
11259         lru_resize_disable osc
11260         touch $DIR/$tdir
11261         cancel_lru_locks mdc
11262         stat $DIR/$tdir > /dev/null
11263         can1=$(do_facet mds1 \
11264                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11265                awk '/ldlm_cancel/ {print $2}')
11266         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11267                awk '/ldlm_bl_callback/ {print $2}')
11268         chmod a+x $DIR/$tdir
11269         can2=$(do_facet mds1 \
11270                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11271                awk '/ldlm_cancel/ {print $2}')
11272         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11273                awk '/ldlm_bl_callback/ {print $2}')
11274         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11275         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11276         lru_resize_enable mdc
11277         lru_resize_enable osc
11278 }
11279 run_test 120d "Early Lock Cancel: setattr test"
11280
11281 test_120e() {
11282         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11283         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11284                 skip_env "no early lock cancel on server"
11285         remote_mds_nodsh && skip "remote MDS with nodsh"
11286
11287         local dlmtrace_set=false
11288
11289         test_mkdir -i0 -c1 $DIR/$tdir
11290         lru_resize_disable mdc
11291         lru_resize_disable osc
11292         ! $LCTL get_param debug | grep -q dlmtrace &&
11293                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
11294         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
11295         cancel_lru_locks mdc
11296         cancel_lru_locks osc
11297         dd if=$DIR/$tdir/f1 of=/dev/null
11298         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
11299         # XXX client can not do early lock cancel of OST lock
11300         # during unlink (LU-4206), so cancel osc lock now.
11301         sleep 2
11302         cancel_lru_locks osc
11303         can1=$(do_facet mds1 \
11304                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11305                awk '/ldlm_cancel/ {print $2}')
11306         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11307                awk '/ldlm_bl_callback/ {print $2}')
11308         unlink $DIR/$tdir/f1
11309         sleep 5
11310         can2=$(do_facet mds1 \
11311                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11312                awk '/ldlm_cancel/ {print $2}')
11313         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11314                awk '/ldlm_bl_callback/ {print $2}')
11315         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
11316                 $LCTL dk $TMP/cancel.debug.txt
11317         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
11318                 $LCTL dk $TMP/blocking.debug.txt
11319         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
11320         lru_resize_enable mdc
11321         lru_resize_enable osc
11322 }
11323 run_test 120e "Early Lock Cancel: unlink test"
11324
11325 test_120f() {
11326         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11327         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11328                 skip_env "no early lock cancel on server"
11329         remote_mds_nodsh && skip "remote MDS with nodsh"
11330
11331         test_mkdir -i0 -c1 $DIR/$tdir
11332         lru_resize_disable mdc
11333         lru_resize_disable osc
11334         test_mkdir -i0 -c1 $DIR/$tdir/d1
11335         test_mkdir -i0 -c1 $DIR/$tdir/d2
11336         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
11337         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
11338         cancel_lru_locks mdc
11339         cancel_lru_locks osc
11340         dd if=$DIR/$tdir/d1/f1 of=/dev/null
11341         dd if=$DIR/$tdir/d2/f2 of=/dev/null
11342         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
11343         # XXX client can not do early lock cancel of OST lock
11344         # during rename (LU-4206), so cancel osc lock now.
11345         sleep 2
11346         cancel_lru_locks osc
11347         can1=$(do_facet mds1 \
11348                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11349                awk '/ldlm_cancel/ {print $2}')
11350         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11351                awk '/ldlm_bl_callback/ {print $2}')
11352         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
11353         sleep 5
11354         can2=$(do_facet mds1 \
11355                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11356                awk '/ldlm_cancel/ {print $2}')
11357         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11358                awk '/ldlm_bl_callback/ {print $2}')
11359         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11360         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11361         lru_resize_enable mdc
11362         lru_resize_enable osc
11363 }
11364 run_test 120f "Early Lock Cancel: rename test"
11365
11366 test_120g() {
11367         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11368         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11369                 skip_env "no early lock cancel on server"
11370         remote_mds_nodsh && skip "remote MDS with nodsh"
11371
11372         lru_resize_disable mdc
11373         lru_resize_disable osc
11374         count=10000
11375         echo create $count files
11376         test_mkdir $DIR/$tdir
11377         cancel_lru_locks mdc
11378         cancel_lru_locks osc
11379         t0=$(date +%s)
11380
11381         can0=$(do_facet $SINGLEMDS \
11382                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11383                awk '/ldlm_cancel/ {print $2}')
11384         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11385                awk '/ldlm_bl_callback/ {print $2}')
11386         createmany -o $DIR/$tdir/f $count
11387         sync
11388         can1=$(do_facet $SINGLEMDS \
11389                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11390                awk '/ldlm_cancel/ {print $2}')
11391         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11392                awk '/ldlm_bl_callback/ {print $2}')
11393         t1=$(date +%s)
11394         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
11395         echo rm $count files
11396         rm -r $DIR/$tdir
11397         sync
11398         can2=$(do_facet $SINGLEMDS \
11399                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11400                awk '/ldlm_cancel/ {print $2}')
11401         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11402                awk '/ldlm_bl_callback/ {print $2}')
11403         t2=$(date +%s)
11404         echo total: $count removes in $((t2-t1))
11405         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
11406         sleep 2
11407         # wait for commitment of removal
11408         lru_resize_enable mdc
11409         lru_resize_enable osc
11410 }
11411 run_test 120g "Early Lock Cancel: performance test"
11412
11413 test_121() { #bug #10589
11414         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11415
11416         rm -rf $DIR/$tfile
11417         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
11418 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
11419         lctl set_param fail_loc=0x310
11420         cancel_lru_locks osc > /dev/null
11421         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
11422         lctl set_param fail_loc=0
11423         [[ $reads -eq $writes ]] ||
11424                 error "read $reads blocks, must be $writes blocks"
11425 }
11426 run_test 121 "read cancel race ========="
11427
11428 test_123a_base() { # was test 123, statahead(bug 11401)
11429         local lsx="$1"
11430
11431         SLOWOK=0
11432         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
11433                 log "testing UP system. Performance may be lower than expected."
11434                 SLOWOK=1
11435         fi
11436
11437         rm -rf $DIR/$tdir
11438         test_mkdir $DIR/$tdir
11439         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
11440         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
11441         MULT=10
11442         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
11443                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
11444
11445                 max=$(lctl get_param -n llite.*.statahead_max | head -n 1)
11446                 lctl set_param -n llite.*.statahead_max 0
11447                 lctl get_param llite.*.statahead_max
11448                 cancel_lru_locks mdc
11449                 cancel_lru_locks osc
11450                 stime=$(date +%s)
11451                 time $lsx $DIR/$tdir | wc -l
11452                 etime=$(date +%s)
11453                 delta=$((etime - stime))
11454                 log "$lsx $i files without statahead: $delta sec"
11455                 lctl set_param llite.*.statahead_max=$max
11456
11457                 swrong=$(lctl get_param -n llite.*.statahead_stats |
11458                         grep "statahead wrong:" | awk '{print $3}')
11459                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
11460                 cancel_lru_locks mdc
11461                 cancel_lru_locks osc
11462                 stime=$(date +%s)
11463                 time $lsx $DIR/$tdir | wc -l
11464                 etime=$(date +%s)
11465                 delta_sa=$((etime - stime))
11466                 log "$lsx $i files with statahead: $delta_sa sec"
11467                 lctl get_param -n llite.*.statahead_stats
11468                 ewrong=$(lctl get_param -n llite.*.statahead_stats |
11469                         grep "statahead wrong:" | awk '{print $3}')
11470
11471                 [[ $swrong -lt $ewrong ]] &&
11472                         log "statahead was stopped, maybe too many locks held!"
11473                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
11474
11475                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
11476                         max=$(lctl get_param -n llite.*.statahead_max |
11477                                 head -n 1)
11478                         lctl set_param -n llite.*.statahead_max 0
11479                         lctl get_param llite.*.statahead_max
11480                         cancel_lru_locks mdc
11481                         cancel_lru_locks osc
11482                         stime=$(date +%s)
11483                         time $lsx $DIR/$tdir | wc -l
11484                         etime=$(date +%s)
11485                         delta=$((etime - stime))
11486                         log "$lsx $i files again without statahead: $delta sec"
11487                         lctl set_param llite.*.statahead_max=$max
11488                         if [ $((delta_sa * 100 > delta * 105 && delta_sa > delta + 2)) ]; then
11489                                 if [  $SLOWOK -eq 0 ]; then
11490                                         error "$lsx $i files is slower with statahead!"
11491                                 else
11492                                         log "$lsx $i files is slower with statahead!"
11493                                 fi
11494                                 break
11495                         fi
11496                 fi
11497
11498                 [ $delta -gt 20 ] && break
11499                 [ $delta -gt 8 ] && MULT=$((50 / delta))
11500                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
11501         done
11502         log "$lsx done"
11503
11504         stime=$(date +%s)
11505         rm -r $DIR/$tdir
11506         sync
11507         etime=$(date +%s)
11508         delta=$((etime - stime))
11509         log "rm -r $DIR/$tdir/: $delta seconds"
11510         log "rm done"
11511         lctl get_param -n llite.*.statahead_stats
11512 }
11513
11514 test_123aa() {
11515         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11516
11517         test_123a_base "ls -l"
11518 }
11519 run_test 123aa "verify statahead work"
11520
11521 test_123ab() {
11522         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11523
11524         statx_supported || skip_env "Test must be statx() syscall supported"
11525
11526         test_123a_base "$STATX -l"
11527 }
11528 run_test 123ab "verify statahead work by using statx"
11529
11530 test_123ac() {
11531         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11532
11533         statx_supported || skip_env "Test must be statx() syscall supported"
11534
11535         local rpcs_before
11536         local rpcs_after
11537         local agl_before
11538         local agl_after
11539
11540         cancel_lru_locks $OSC
11541         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
11542         agl_before=$($LCTL get_param -n llite.*.statahead_stats |
11543                 awk '/agl.total:/ {print $3}')
11544         test_123a_base "$STATX -c \"%n %i %A %h %u %g %W %X %Z\" -D"
11545         test_123a_base "$STATX --cached=always -D"
11546         agl_after=$($LCTL get_param -n llite.*.statahead_stats |
11547                 awk '/agl.total:/ {print $3}')
11548         [ $agl_before -eq $agl_after ] ||
11549                 error "Should not trigger AGL thread - $agl_before:$agl_after"
11550         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
11551         [ $rpcs_after -eq $rpcs_before ] ||
11552                 error "$STATX should not send glimpse RPCs to $OSC"
11553 }
11554 run_test 123ac "verify statahead work by using statx without glimpse RPCs"
11555
11556 test_123b () { # statahead(bug 15027)
11557         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11558
11559         test_mkdir $DIR/$tdir
11560         createmany -o $DIR/$tdir/$tfile-%d 1000
11561
11562         cancel_lru_locks mdc
11563         cancel_lru_locks osc
11564
11565 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
11566         lctl set_param fail_loc=0x80000803
11567         ls -lR $DIR/$tdir > /dev/null
11568         log "ls done"
11569         lctl set_param fail_loc=0x0
11570         lctl get_param -n llite.*.statahead_stats
11571         rm -r $DIR/$tdir
11572         sync
11573
11574 }
11575 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
11576
11577 test_123c() {
11578         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
11579
11580         test_mkdir -i 0 -c 1 $DIR/$tdir.0
11581         test_mkdir -i 1 -c 1 $DIR/$tdir.1
11582         touch $DIR/$tdir.1/{1..3}
11583         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
11584
11585         remount_client $MOUNT
11586
11587         $MULTIOP $DIR/$tdir.0 Q
11588
11589         # let statahead to complete
11590         ls -l $DIR/$tdir.0 > /dev/null
11591
11592         testid=$(echo $TESTNAME | tr '_' ' ')
11593         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
11594                 error "statahead warning" || true
11595 }
11596 run_test 123c "Can not initialize inode warning on DNE statahead"
11597
11598 test_124a() {
11599         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11600         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11601                 skip_env "no lru resize on server"
11602
11603         local NR=2000
11604
11605         test_mkdir $DIR/$tdir
11606
11607         log "create $NR files at $DIR/$tdir"
11608         createmany -o $DIR/$tdir/f $NR ||
11609                 error "failed to create $NR files in $DIR/$tdir"
11610
11611         cancel_lru_locks mdc
11612         ls -l $DIR/$tdir > /dev/null
11613
11614         local NSDIR=""
11615         local LRU_SIZE=0
11616         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
11617                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
11618                 LRU_SIZE=$($LCTL get_param -n $PARAM)
11619                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
11620                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
11621                         log "NSDIR=$NSDIR"
11622                         log "NS=$(basename $NSDIR)"
11623                         break
11624                 fi
11625         done
11626
11627         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
11628                 skip "Not enough cached locks created!"
11629         fi
11630         log "LRU=$LRU_SIZE"
11631
11632         local SLEEP=30
11633
11634         # We know that lru resize allows one client to hold $LIMIT locks
11635         # for 10h. After that locks begin to be killed by client.
11636         local MAX_HRS=10
11637         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
11638         log "LIMIT=$LIMIT"
11639         if [ $LIMIT -lt $LRU_SIZE ]; then
11640                 skip "Limit is too small $LIMIT"
11641         fi
11642
11643         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
11644         # killing locks. Some time was spent for creating locks. This means
11645         # that up to the moment of sleep finish we must have killed some of
11646         # them (10-100 locks). This depends on how fast ther were created.
11647         # Many of them were touched in almost the same moment and thus will
11648         # be killed in groups.
11649         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
11650
11651         # Use $LRU_SIZE_B here to take into account real number of locks
11652         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
11653         local LRU_SIZE_B=$LRU_SIZE
11654         log "LVF=$LVF"
11655         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
11656         log "OLD_LVF=$OLD_LVF"
11657         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
11658
11659         # Let's make sure that we really have some margin. Client checks
11660         # cached locks every 10 sec.
11661         SLEEP=$((SLEEP+20))
11662         log "Sleep ${SLEEP} sec"
11663         local SEC=0
11664         while ((SEC<$SLEEP)); do
11665                 echo -n "..."
11666                 sleep 5
11667                 SEC=$((SEC+5))
11668                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
11669                 echo -n "$LRU_SIZE"
11670         done
11671         echo ""
11672         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
11673         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
11674
11675         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
11676                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
11677                 unlinkmany $DIR/$tdir/f $NR
11678                 return
11679         }
11680
11681         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
11682         log "unlink $NR files at $DIR/$tdir"
11683         unlinkmany $DIR/$tdir/f $NR
11684 }
11685 run_test 124a "lru resize ======================================="
11686
11687 get_max_pool_limit()
11688 {
11689         local limit=$($LCTL get_param \
11690                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
11691         local max=0
11692         for l in $limit; do
11693                 if [[ $l -gt $max ]]; then
11694                         max=$l
11695                 fi
11696         done
11697         echo $max
11698 }
11699
11700 test_124b() {
11701         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11702         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11703                 skip_env "no lru resize on server"
11704
11705         LIMIT=$(get_max_pool_limit)
11706
11707         NR=$(($(default_lru_size)*20))
11708         if [[ $NR -gt $LIMIT ]]; then
11709                 log "Limit lock number by $LIMIT locks"
11710                 NR=$LIMIT
11711         fi
11712
11713         IFree=$(mdsrate_inodes_available)
11714         if [ $IFree -lt $NR ]; then
11715                 log "Limit lock number by $IFree inodes"
11716                 NR=$IFree
11717         fi
11718
11719         lru_resize_disable mdc
11720         test_mkdir -p $DIR/$tdir/disable_lru_resize
11721
11722         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
11723         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
11724         cancel_lru_locks mdc
11725         stime=`date +%s`
11726         PID=""
11727         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11728         PID="$PID $!"
11729         sleep 2
11730         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11731         PID="$PID $!"
11732         sleep 2
11733         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11734         PID="$PID $!"
11735         wait $PID
11736         etime=`date +%s`
11737         nolruresize_delta=$((etime-stime))
11738         log "ls -la time: $nolruresize_delta seconds"
11739         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11740         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
11741
11742         lru_resize_enable mdc
11743         test_mkdir -p $DIR/$tdir/enable_lru_resize
11744
11745         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
11746         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
11747         cancel_lru_locks mdc
11748         stime=`date +%s`
11749         PID=""
11750         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11751         PID="$PID $!"
11752         sleep 2
11753         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11754         PID="$PID $!"
11755         sleep 2
11756         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11757         PID="$PID $!"
11758         wait $PID
11759         etime=`date +%s`
11760         lruresize_delta=$((etime-stime))
11761         log "ls -la time: $lruresize_delta seconds"
11762         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11763
11764         if [ $lruresize_delta -gt $nolruresize_delta ]; then
11765                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
11766         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
11767                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
11768         else
11769                 log "lru resize performs the same with no lru resize"
11770         fi
11771         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
11772 }
11773 run_test 124b "lru resize (performance test) ======================="
11774
11775 test_124c() {
11776         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11777         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11778                 skip_env "no lru resize on server"
11779
11780         # cache ununsed locks on client
11781         local nr=100
11782         cancel_lru_locks mdc
11783         test_mkdir $DIR/$tdir
11784         createmany -o $DIR/$tdir/f $nr ||
11785                 error "failed to create $nr files in $DIR/$tdir"
11786         ls -l $DIR/$tdir > /dev/null
11787
11788         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11789         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11790         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11791         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11792         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11793
11794         # set lru_max_age to 1 sec
11795         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11796         echo "sleep $((recalc_p * 2)) seconds..."
11797         sleep $((recalc_p * 2))
11798
11799         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11800         # restore lru_max_age
11801         $LCTL set_param -n $nsdir.lru_max_age $max_age
11802         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11803         unlinkmany $DIR/$tdir/f $nr
11804 }
11805 run_test 124c "LRUR cancel very aged locks"
11806
11807 test_124d() {
11808         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11809         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11810                 skip_env "no lru resize on server"
11811
11812         # cache ununsed locks on client
11813         local nr=100
11814
11815         lru_resize_disable mdc
11816         stack_trap "lru_resize_enable mdc" EXIT
11817
11818         cancel_lru_locks mdc
11819
11820         # asynchronous object destroy at MDT could cause bl ast to client
11821         test_mkdir $DIR/$tdir
11822         createmany -o $DIR/$tdir/f $nr ||
11823                 error "failed to create $nr files in $DIR/$tdir"
11824         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
11825
11826         ls -l $DIR/$tdir > /dev/null
11827
11828         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11829         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11830         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11831         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11832
11833         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11834
11835         # set lru_max_age to 1 sec
11836         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11837         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
11838
11839         echo "sleep $((recalc_p * 2)) seconds..."
11840         sleep $((recalc_p * 2))
11841
11842         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11843
11844         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11845 }
11846 run_test 124d "cancel very aged locks if lru-resize diasbaled"
11847
11848 test_125() { # 13358
11849         $LCTL get_param -n llite.*.client_type | grep -q local ||
11850                 skip "must run as local client"
11851         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
11852                 skip_env "must have acl enabled"
11853         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
11854
11855         test_mkdir $DIR/$tdir
11856         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
11857         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
11858         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
11859 }
11860 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
11861
11862 test_126() { # bug 12829/13455
11863         $GSS && skip_env "must run as gss disabled"
11864         $LCTL get_param -n llite.*.client_type | grep -q local ||
11865                 skip "must run as local client"
11866         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
11867
11868         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
11869         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
11870         rm -f $DIR/$tfile
11871         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
11872 }
11873 run_test 126 "check that the fsgid provided by the client is taken into account"
11874
11875 test_127a() { # bug 15521
11876         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11877         local name count samp unit min max sum sumsq
11878
11879         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
11880         echo "stats before reset"
11881         $LCTL get_param osc.*.stats
11882         $LCTL set_param osc.*.stats=0
11883         local fsize=$((2048 * 1024))
11884
11885         dd if=/dev/zero of=$DIR/$tfile bs=$fsize count=1
11886         cancel_lru_locks osc
11887         dd if=$DIR/$tfile of=/dev/null bs=$fsize
11888
11889         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/$tfile.tmp
11890         stack_trap "rm -f $TMP/$tfile.tmp"
11891         while read name count samp unit min max sum sumsq; do
11892                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11893                 [ ! $min ] && error "Missing min value for $name proc entry"
11894                 eval $name=$count || error "Wrong proc format"
11895
11896                 case $name in
11897                 read_bytes|write_bytes)
11898                         [[ "$unit" =~ "bytes" ]] ||
11899                                 error "unit is not 'bytes': $unit"
11900                         (( $min >= 4096 )) || error "min is too small: $min"
11901                         (( $min <= $fsize )) || error "min is too big: $min"
11902                         (( $max >= 4096 )) || error "max is too small: $max"
11903                         (( $max <= $fsize )) || error "max is too big: $max"
11904                         (( $sum == $fsize )) || error "sum is wrong: $sum"
11905                         (( $sumsq >= ($fsize / 4096) * (4096 * 4096) )) ||
11906                                 error "sumsquare is too small: $sumsq"
11907                         (( $sumsq <= $fsize * $fsize )) ||
11908                                 error "sumsquare is too big: $sumsq"
11909                         ;;
11910                 ost_read|ost_write)
11911                         [[ "$unit" =~ "usec" ]] ||
11912                                 error "unit is not 'usec': $unit"
11913                         ;;
11914                 *)      ;;
11915                 esac
11916         done < $DIR/$tfile.tmp
11917
11918         #check that we actually got some stats
11919         [ "$read_bytes" ] || error "Missing read_bytes stats"
11920         [ "$write_bytes" ] || error "Missing write_bytes stats"
11921         [ "$read_bytes" != 0 ] || error "no read done"
11922         [ "$write_bytes" != 0 ] || error "no write done"
11923 }
11924 run_test 127a "verify the client stats are sane"
11925
11926 test_127b() { # bug LU-333
11927         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11928         local name count samp unit min max sum sumsq
11929
11930         echo "stats before reset"
11931         $LCTL get_param llite.*.stats
11932         $LCTL set_param llite.*.stats=0
11933
11934         # perform 2 reads and writes so MAX is different from SUM.
11935         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11936         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11937         cancel_lru_locks osc
11938         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11939         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11940
11941         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
11942         stack_trap "rm -f $TMP/$tfile.tmp"
11943         while read name count samp unit min max sum sumsq; do
11944                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11945                 eval $name=$count || error "Wrong proc format"
11946
11947                 case $name in
11948                 read_bytes|write_bytes)
11949                         [[ "$unit" =~ "bytes" ]] ||
11950                                 error "unit is not 'bytes': $unit"
11951                         (( $count == 2 )) || error "count is not 2: $count"
11952                         (( $min == $PAGE_SIZE )) ||
11953                                 error "min is not $PAGE_SIZE: $min"
11954                         (( $max == $PAGE_SIZE )) ||
11955                                 error "max is not $PAGE_SIZE: $max"
11956                         (( $sum == $PAGE_SIZE * 2 )) ||
11957                                 error "sum is not $((PAGE_SIZE * 2)): $sum"
11958                         ;;
11959                 read|write)
11960                         [[ "$unit" =~ "usec" ]] ||
11961                                 error "unit is not 'usec': $unit"
11962                         ;;
11963                 *)      ;;
11964                 esac
11965         done < $TMP/$tfile.tmp
11966
11967         #check that we actually got some stats
11968         [ "$read_bytes" ] || error "Missing read_bytes stats"
11969         [ "$write_bytes" ] || error "Missing write_bytes stats"
11970         [ "$read_bytes" != 0 ] || error "no read done"
11971         [ "$write_bytes" != 0 ] || error "no write done"
11972 }
11973 run_test 127b "verify the llite client stats are sane"
11974
11975 test_127c() { # LU-12394
11976         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11977         local size
11978         local bsize
11979         local reads
11980         local writes
11981         local count
11982
11983         $LCTL set_param llite.*.extents_stats=1
11984         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
11985
11986         # Use two stripes so there is enough space in default config
11987         $LFS setstripe -c 2 $DIR/$tfile
11988
11989         # Extent stats start at 0-4K and go in power of two buckets
11990         # LL_HIST_START = 12 --> 2^12 = 4K
11991         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
11992         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
11993         # small configs
11994         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
11995                 do
11996                 # Write and read, 2x each, second time at a non-zero offset
11997                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
11998                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
11999                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
12000                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
12001                 rm -f $DIR/$tfile
12002         done
12003
12004         $LCTL get_param llite.*.extents_stats
12005
12006         count=2
12007         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
12008                 do
12009                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
12010                                 grep -m 1 $bsize)
12011                 reads=$(echo $bucket | awk '{print $5}')
12012                 writes=$(echo $bucket | awk '{print $9}')
12013                 [ "$reads" -eq $count ] ||
12014                         error "$reads reads in < $bsize bucket, expect $count"
12015                 [ "$writes" -eq $count ] ||
12016                         error "$writes writes in < $bsize bucket, expect $count"
12017         done
12018
12019         # Test mmap write and read
12020         $LCTL set_param llite.*.extents_stats=c
12021         size=512
12022         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
12023         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
12024         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
12025
12026         $LCTL get_param llite.*.extents_stats
12027
12028         count=$(((size*1024) / PAGE_SIZE))
12029
12030         bsize=$((2 * PAGE_SIZE / 1024))K
12031
12032         bucket=$($LCTL get_param -n llite.*.extents_stats |
12033                         grep -m 1 $bsize)
12034         reads=$(echo $bucket | awk '{print $5}')
12035         writes=$(echo $bucket | awk '{print $9}')
12036         # mmap writes fault in the page first, creating an additonal read
12037         [ "$reads" -eq $((2 * count)) ] ||
12038                 error "$reads reads in < $bsize bucket, expect $count"
12039         [ "$writes" -eq $count ] ||
12040                 error "$writes writes in < $bsize bucket, expect $count"
12041 }
12042 run_test 127c "test llite extent stats with regular & mmap i/o"
12043
12044 test_128() { # bug 15212
12045         touch $DIR/$tfile
12046         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
12047                 find $DIR/$tfile
12048                 find $DIR/$tfile
12049         EOF
12050
12051         result=$(grep error $TMP/$tfile.log)
12052         rm -f $DIR/$tfile $TMP/$tfile.log
12053         [ -z "$result" ] ||
12054                 error "consecutive find's under interactive lfs failed"
12055 }
12056 run_test 128 "interactive lfs for 2 consecutive find's"
12057
12058 set_dir_limits () {
12059         local mntdev
12060         local canondev
12061         local node
12062
12063         local ldproc=/proc/fs/ldiskfs
12064         local facets=$(get_facets MDS)
12065
12066         for facet in ${facets//,/ }; do
12067                 canondev=$(ldiskfs_canon \
12068                            *.$(convert_facet2label $facet).mntdev $facet)
12069                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
12070                         ldproc=/sys/fs/ldiskfs
12071                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
12072                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
12073         done
12074 }
12075
12076 check_mds_dmesg() {
12077         local facets=$(get_facets MDS)
12078         for facet in ${facets//,/ }; do
12079                 do_facet $facet "dmesg | tail -3 | grep $1" && return 0
12080         done
12081         return 1
12082 }
12083
12084 test_129() {
12085         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12086         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
12087                 skip "Need MDS version with at least 2.5.56"
12088         if [ "$mds1_FSTYPE" != ldiskfs ]; then
12089                 skip_env "ldiskfs only test"
12090         fi
12091         remote_mds_nodsh && skip "remote MDS with nodsh"
12092
12093         local ENOSPC=28
12094         local has_warning=false
12095
12096         rm -rf $DIR/$tdir
12097         mkdir -p $DIR/$tdir
12098
12099         # block size of mds1
12100         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 8))
12101         set_dir_limits $maxsize $((maxsize * 6 / 8))
12102         stack_trap "set_dir_limits 0 0"
12103         stack_trap "unlinkmany $DIR/$tdir/file_base_ 2000 || true"
12104         local dirsize=$(stat -c%s "$DIR/$tdir")
12105         local nfiles=0
12106         while (( $dirsize <= $maxsize )); do
12107                 $MCREATE $DIR/$tdir/file_base_$nfiles
12108                 rc=$?
12109                 # check two errors:
12110                 # ENOSPC for ext4 max_dir_size, which has been used since
12111                 # kernel v3.6-rc1-8-gdf981d03ee, lustre v2_4_50_0-79-gaed82035c0
12112                 if (( rc == ENOSPC )); then
12113                         set_dir_limits 0 0
12114                         echo "rc=$rc returned as expected after $nfiles files"
12115
12116                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
12117                                 error "create failed w/o dir size limit"
12118
12119                         # messages may be rate limited if test is run repeatedly
12120                         check_mds_dmesg '"is approaching max"' ||
12121                                 echo "warning message should be output"
12122                         check_mds_dmesg '"has reached max"' ||
12123                                 echo "reached message should be output"
12124
12125                         dirsize=$(stat -c%s "$DIR/$tdir")
12126
12127                         [[ $dirsize -ge $maxsize ]] && return 0
12128                         error "dirsize $dirsize < $maxsize after $nfiles files"
12129                 elif (( rc != 0 )); then
12130                         break
12131                 fi
12132                 nfiles=$((nfiles + 1))
12133                 dirsize=$(stat -c%s "$DIR/$tdir")
12134         done
12135
12136         error "rc=$rc, size=$dirsize/$maxsize, mdt=$MDSCOUNT, nfiles=$nfiles"
12137 }
12138 run_test 129 "test directory size limit ========================"
12139
12140 OLDIFS="$IFS"
12141 cleanup_130() {
12142         trap 0
12143         IFS="$OLDIFS"
12144 }
12145
12146 test_130a() {
12147         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12148         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
12149
12150         trap cleanup_130 EXIT RETURN
12151
12152         local fm_file=$DIR/$tfile
12153         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
12154         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
12155                 error "dd failed for $fm_file"
12156
12157         # LU-1795: test filefrag/FIEMAP once, even if unsupported
12158         filefrag -ves $fm_file
12159         RC=$?
12160         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12161                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12162         [ $RC != 0 ] && error "filefrag $fm_file failed"
12163
12164         filefrag_op=$(filefrag -ve -k $fm_file |
12165                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12166         lun=$($LFS getstripe -i $fm_file)
12167
12168         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
12169         IFS=$'\n'
12170         tot_len=0
12171         for line in $filefrag_op
12172         do
12173                 frag_lun=`echo $line | cut -d: -f5`
12174                 ext_len=`echo $line | cut -d: -f4`
12175                 if (( $frag_lun != $lun )); then
12176                         cleanup_130
12177                         error "FIEMAP on 1-stripe file($fm_file) failed"
12178                         return
12179                 fi
12180                 (( tot_len += ext_len ))
12181         done
12182
12183         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
12184                 cleanup_130
12185                 error "FIEMAP on 1-stripe file($fm_file) failed;"
12186                 return
12187         fi
12188
12189         cleanup_130
12190
12191         echo "FIEMAP on single striped file succeeded"
12192 }
12193 run_test 130a "FIEMAP (1-stripe file)"
12194
12195 test_130b() {
12196         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
12197
12198         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12199         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
12200
12201         trap cleanup_130 EXIT RETURN
12202
12203         local fm_file=$DIR/$tfile
12204         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
12205                         error "setstripe on $fm_file"
12206         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12207                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12208
12209         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
12210                 error "dd failed on $fm_file"
12211
12212         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12213         filefrag_op=$(filefrag -ve -k $fm_file |
12214                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12215
12216         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12217                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12218
12219         IFS=$'\n'
12220         tot_len=0
12221         num_luns=1
12222         for line in $filefrag_op
12223         do
12224                 frag_lun=$(echo $line | cut -d: -f5 |
12225                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12226                 ext_len=$(echo $line | cut -d: -f4)
12227                 if (( $frag_lun != $last_lun )); then
12228                         if (( tot_len != 1024 )); then
12229                                 cleanup_130
12230                                 error "FIEMAP on $fm_file failed; returned " \
12231                                 "len $tot_len for OST $last_lun instead of 1024"
12232                                 return
12233                         else
12234                                 (( num_luns += 1 ))
12235                                 tot_len=0
12236                         fi
12237                 fi
12238                 (( tot_len += ext_len ))
12239                 last_lun=$frag_lun
12240         done
12241         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
12242                 cleanup_130
12243                 error "FIEMAP on $fm_file failed; returned wrong number of " \
12244                         "luns or wrong len for OST $last_lun"
12245                 return
12246         fi
12247
12248         cleanup_130
12249
12250         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
12251 }
12252 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
12253
12254 test_130c() {
12255         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12256
12257         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12258         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12259
12260         trap cleanup_130 EXIT RETURN
12261
12262         local fm_file=$DIR/$tfile
12263         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
12264         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12265                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12266
12267         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
12268                         error "dd failed on $fm_file"
12269
12270         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12271         filefrag_op=$(filefrag -ve -k $fm_file |
12272                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12273
12274         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12275                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12276
12277         IFS=$'\n'
12278         tot_len=0
12279         num_luns=1
12280         for line in $filefrag_op
12281         do
12282                 frag_lun=$(echo $line | cut -d: -f5 |
12283                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12284                 ext_len=$(echo $line | cut -d: -f4)
12285                 if (( $frag_lun != $last_lun )); then
12286                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
12287                         if (( logical != 512 )); then
12288                                 cleanup_130
12289                                 error "FIEMAP on $fm_file failed; returned " \
12290                                 "logical start for lun $logical instead of 512"
12291                                 return
12292                         fi
12293                         if (( tot_len != 512 )); then
12294                                 cleanup_130
12295                                 error "FIEMAP on $fm_file failed; returned " \
12296                                 "len $tot_len for OST $last_lun instead of 1024"
12297                                 return
12298                         else
12299                                 (( num_luns += 1 ))
12300                                 tot_len=0
12301                         fi
12302                 fi
12303                 (( tot_len += ext_len ))
12304                 last_lun=$frag_lun
12305         done
12306         if (( num_luns != 2 || tot_len != 512 )); then
12307                 cleanup_130
12308                 error "FIEMAP on $fm_file failed; returned wrong number of " \
12309                         "luns or wrong len for OST $last_lun"
12310                 return
12311         fi
12312
12313         cleanup_130
12314
12315         echo "FIEMAP on 2-stripe file with hole succeeded"
12316 }
12317 run_test 130c "FIEMAP (2-stripe file with hole)"
12318
12319 test_130d() {
12320         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
12321
12322         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12323         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12324
12325         trap cleanup_130 EXIT RETURN
12326
12327         local fm_file=$DIR/$tfile
12328         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
12329                         error "setstripe on $fm_file"
12330         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12331                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12332
12333         local actual_stripe_count=$($LFS getstripe -c $fm_file)
12334         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
12335                 error "dd failed on $fm_file"
12336
12337         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12338         filefrag_op=$(filefrag -ve -k $fm_file |
12339                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12340
12341         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12342                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12343
12344         IFS=$'\n'
12345         tot_len=0
12346         num_luns=1
12347         for line in $filefrag_op
12348         do
12349                 frag_lun=$(echo $line | cut -d: -f5 |
12350                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12351                 ext_len=$(echo $line | cut -d: -f4)
12352                 if (( $frag_lun != $last_lun )); then
12353                         if (( tot_len != 1024 )); then
12354                                 cleanup_130
12355                                 error "FIEMAP on $fm_file failed; returned " \
12356                                 "len $tot_len for OST $last_lun instead of 1024"
12357                                 return
12358                         else
12359                                 (( num_luns += 1 ))
12360                                 tot_len=0
12361                         fi
12362                 fi
12363                 (( tot_len += ext_len ))
12364                 last_lun=$frag_lun
12365         done
12366         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
12367                 cleanup_130
12368                 error "FIEMAP on $fm_file failed; returned wrong number of " \
12369                         "luns or wrong len for OST $last_lun"
12370                 return
12371         fi
12372
12373         cleanup_130
12374
12375         echo "FIEMAP on N-stripe file succeeded"
12376 }
12377 run_test 130d "FIEMAP (N-stripe file)"
12378
12379 test_130e() {
12380         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12381
12382         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12383         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
12384
12385         trap cleanup_130 EXIT RETURN
12386
12387         local fm_file=$DIR/$tfile
12388         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
12389         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12390                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12391
12392         NUM_BLKS=512
12393         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
12394         for ((i = 0; i < $NUM_BLKS; i++))
12395         do
12396                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
12397         done
12398
12399         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12400         filefrag_op=$(filefrag -ve -k $fm_file |
12401                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12402
12403         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12404                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12405
12406         IFS=$'\n'
12407         tot_len=0
12408         num_luns=1
12409         for line in $filefrag_op
12410         do
12411                 frag_lun=$(echo $line | cut -d: -f5 |
12412                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12413                 ext_len=$(echo $line | cut -d: -f4)
12414                 if (( $frag_lun != $last_lun )); then
12415                         if (( tot_len != $EXPECTED_LEN )); then
12416                                 cleanup_130
12417                                 error "FIEMAP on $fm_file failed; returned " \
12418                                 "len $tot_len for OST $last_lun instead " \
12419                                 "of $EXPECTED_LEN"
12420                                 return
12421                         else
12422                                 (( num_luns += 1 ))
12423                                 tot_len=0
12424                         fi
12425                 fi
12426                 (( tot_len += ext_len ))
12427                 last_lun=$frag_lun
12428         done
12429         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
12430                 cleanup_130
12431                 error "FIEMAP on $fm_file failed; returned wrong number " \
12432                         "of luns or wrong len for OST $last_lun"
12433                 return
12434         fi
12435
12436         cleanup_130
12437
12438         echo "FIEMAP with continuation calls succeeded"
12439 }
12440 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
12441
12442 test_130f() {
12443         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12444         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12445
12446         local fm_file=$DIR/$tfile
12447         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
12448                 error "multiop create with lov_delay_create on $fm_file"
12449
12450         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12451         filefrag_extents=$(filefrag -vek $fm_file |
12452                            awk '/extents? found/ { print $2 }')
12453         if [[ "$filefrag_extents" != "0" ]]; then
12454                 error "FIEMAP on $fm_file failed; " \
12455                       "returned $filefrag_extents expected 0"
12456         fi
12457
12458         rm -f $fm_file
12459 }
12460 run_test 130f "FIEMAP (unstriped file)"
12461
12462 # Test for writev/readv
12463 test_131a() {
12464         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
12465                 error "writev test failed"
12466         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
12467                 error "readv failed"
12468         rm -f $DIR/$tfile
12469 }
12470 run_test 131a "test iov's crossing stripe boundary for writev/readv"
12471
12472 test_131b() {
12473         local fsize=$((524288 + 1048576 + 1572864))
12474         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
12475                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12476                         error "append writev test failed"
12477
12478         ((fsize += 1572864 + 1048576))
12479         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
12480                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12481                         error "append writev test failed"
12482         rm -f $DIR/$tfile
12483 }
12484 run_test 131b "test append writev"
12485
12486 test_131c() {
12487         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
12488         error "NOT PASS"
12489 }
12490 run_test 131c "test read/write on file w/o objects"
12491
12492 test_131d() {
12493         rwv -f $DIR/$tfile -w -n 1 1572864
12494         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
12495         if [ "$NOB" != 1572864 ]; then
12496                 error "Short read filed: read $NOB bytes instead of 1572864"
12497         fi
12498         rm -f $DIR/$tfile
12499 }
12500 run_test 131d "test short read"
12501
12502 test_131e() {
12503         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
12504         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
12505         error "read hitting hole failed"
12506         rm -f $DIR/$tfile
12507 }
12508 run_test 131e "test read hitting hole"
12509
12510 check_stats() {
12511         local facet=$1
12512         local op=$2
12513         local want=${3:-0}
12514         local res
12515
12516         case $facet in
12517         mds*) res=$(do_facet $facet \
12518                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
12519                  ;;
12520         ost*) res=$(do_facet $facet \
12521                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
12522                  ;;
12523         *) error "Wrong facet '$facet'" ;;
12524         esac
12525         [ "$res" ] || error "The counter for $op on $facet was not incremented"
12526         # if the argument $3 is zero, it means any stat increment is ok.
12527         if [[ $want -gt 0 ]]; then
12528                 local count=$(echo $res | awk '{ print $2 }')
12529                 [[ $count -ne $want ]] &&
12530                         error "The $op counter on $facet is $count, not $want"
12531         fi
12532 }
12533
12534 test_133a() {
12535         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12536         remote_ost_nodsh && skip "remote OST with nodsh"
12537         remote_mds_nodsh && skip "remote MDS with nodsh"
12538         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12539                 skip_env "MDS doesn't support rename stats"
12540
12541         local testdir=$DIR/${tdir}/stats_testdir
12542
12543         mkdir -p $DIR/${tdir}
12544
12545         # clear stats.
12546         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12547         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12548
12549         # verify mdt stats first.
12550         mkdir ${testdir} || error "mkdir failed"
12551         check_stats $SINGLEMDS "mkdir" 1
12552         touch ${testdir}/${tfile} || error "touch failed"
12553         check_stats $SINGLEMDS "open" 1
12554         check_stats $SINGLEMDS "close" 1
12555         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
12556                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
12557                 check_stats $SINGLEMDS "mknod" 2
12558         }
12559         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
12560         check_stats $SINGLEMDS "unlink" 1
12561         rm -f ${testdir}/${tfile} || error "file remove failed"
12562         check_stats $SINGLEMDS "unlink" 2
12563
12564         # remove working dir and check mdt stats again.
12565         rmdir ${testdir} || error "rmdir failed"
12566         check_stats $SINGLEMDS "rmdir" 1
12567
12568         local testdir1=$DIR/${tdir}/stats_testdir1
12569         mkdir -p ${testdir}
12570         mkdir -p ${testdir1}
12571         touch ${testdir1}/test1
12572         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
12573         check_stats $SINGLEMDS "crossdir_rename" 1
12574
12575         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
12576         check_stats $SINGLEMDS "samedir_rename" 1
12577
12578         rm -rf $DIR/${tdir}
12579 }
12580 run_test 133a "Verifying MDT stats ========================================"
12581
12582 test_133b() {
12583         local res
12584
12585         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12586         remote_ost_nodsh && skip "remote OST with nodsh"
12587         remote_mds_nodsh && skip "remote MDS with nodsh"
12588
12589         local testdir=$DIR/${tdir}/stats_testdir
12590
12591         mkdir -p ${testdir} || error "mkdir failed"
12592         touch ${testdir}/${tfile} || error "touch failed"
12593         cancel_lru_locks mdc
12594
12595         # clear stats.
12596         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12597         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12598
12599         # extra mdt stats verification.
12600         chmod 444 ${testdir}/${tfile} || error "chmod failed"
12601         check_stats $SINGLEMDS "setattr" 1
12602         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12603         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
12604         then            # LU-1740
12605                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
12606                 check_stats $SINGLEMDS "getattr" 1
12607         fi
12608         rm -rf $DIR/${tdir}
12609
12610         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
12611         # so the check below is not reliable
12612         [ $MDSCOUNT -eq 1 ] || return 0
12613
12614         # Sleep to avoid a cached response.
12615         #define OBD_STATFS_CACHE_SECONDS 1
12616         sleep 2
12617         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12618         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12619         $LFS df || error "lfs failed"
12620         check_stats $SINGLEMDS "statfs" 1
12621
12622         # check aggregated statfs (LU-10018)
12623         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
12624                 return 0
12625         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
12626                 return 0
12627         sleep 2
12628         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12629         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12630         df $DIR
12631         check_stats $SINGLEMDS "statfs" 1
12632
12633         # We want to check that the client didn't send OST_STATFS to
12634         # ost1 but the MDT also uses OST_STATFS for precreate. So some
12635         # extra care is needed here.
12636         if remote_mds; then
12637                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
12638                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
12639
12640                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
12641                 [ "$res" ] && error "OST got STATFS"
12642         fi
12643
12644         return 0
12645 }
12646 run_test 133b "Verifying extra MDT stats =================================="
12647
12648 test_133c() {
12649         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12650         remote_ost_nodsh && skip "remote OST with nodsh"
12651         remote_mds_nodsh && skip "remote MDS with nodsh"
12652
12653         local testdir=$DIR/$tdir/stats_testdir
12654
12655         test_mkdir -p $testdir
12656
12657         # verify obdfilter stats.
12658         $LFS setstripe -c 1 -i 0 $testdir/$tfile
12659         sync
12660         cancel_lru_locks osc
12661         wait_delete_completed
12662
12663         # clear stats.
12664         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12665         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12666
12667         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
12668                 error "dd failed"
12669         sync
12670         cancel_lru_locks osc
12671         check_stats ost1 "write" 1
12672
12673         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
12674         check_stats ost1 "read" 1
12675
12676         > $testdir/$tfile || error "truncate failed"
12677         check_stats ost1 "punch" 1
12678
12679         rm -f $testdir/$tfile || error "file remove failed"
12680         wait_delete_completed
12681         check_stats ost1 "destroy" 1
12682
12683         rm -rf $DIR/$tdir
12684 }
12685 run_test 133c "Verifying OST stats ========================================"
12686
12687 order_2() {
12688         local value=$1
12689         local orig=$value
12690         local order=1
12691
12692         while [ $value -ge 2 ]; do
12693                 order=$((order*2))
12694                 value=$((value/2))
12695         done
12696
12697         if [ $orig -gt $order ]; then
12698                 order=$((order*2))
12699         fi
12700         echo $order
12701 }
12702
12703 size_in_KMGT() {
12704     local value=$1
12705     local size=('K' 'M' 'G' 'T');
12706     local i=0
12707     local size_string=$value
12708
12709     while [ $value -ge 1024 ]; do
12710         if [ $i -gt 3 ]; then
12711             #T is the biggest unit we get here, if that is bigger,
12712             #just return XXXT
12713             size_string=${value}T
12714             break
12715         fi
12716         value=$((value >> 10))
12717         if [ $value -lt 1024 ]; then
12718             size_string=${value}${size[$i]}
12719             break
12720         fi
12721         i=$((i + 1))
12722     done
12723
12724     echo $size_string
12725 }
12726
12727 get_rename_size() {
12728         local size=$1
12729         local context=${2:-.}
12730         local sample=$(do_facet $SINGLEMDS $LCTL \
12731                 get_param mdt.$FSNAME-MDT0000.rename_stats |
12732                 grep -A1 $context |
12733                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
12734         echo $sample
12735 }
12736
12737 test_133d() {
12738         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12739         remote_ost_nodsh && skip "remote OST with nodsh"
12740         remote_mds_nodsh && skip "remote MDS with nodsh"
12741         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12742                 skip_env "MDS doesn't support rename stats"
12743
12744         local testdir1=$DIR/${tdir}/stats_testdir1
12745         local testdir2=$DIR/${tdir}/stats_testdir2
12746         mkdir -p $DIR/${tdir}
12747
12748         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12749
12750         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
12751         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
12752
12753         createmany -o $testdir1/test 512 || error "createmany failed"
12754
12755         # check samedir rename size
12756         mv ${testdir1}/test0 ${testdir1}/test_0
12757
12758         local testdir1_size=$(ls -l $DIR/${tdir} |
12759                 awk '/stats_testdir1/ {print $5}')
12760         local testdir2_size=$(ls -l $DIR/${tdir} |
12761                 awk '/stats_testdir2/ {print $5}')
12762
12763         testdir1_size=$(order_2 $testdir1_size)
12764         testdir2_size=$(order_2 $testdir2_size)
12765
12766         testdir1_size=$(size_in_KMGT $testdir1_size)
12767         testdir2_size=$(size_in_KMGT $testdir2_size)
12768
12769         echo "source rename dir size: ${testdir1_size}"
12770         echo "target rename dir size: ${testdir2_size}"
12771
12772         local cmd="do_facet $SINGLEMDS $LCTL "
12773         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
12774
12775         eval $cmd || error "$cmd failed"
12776         local samedir=$($cmd | grep 'same_dir')
12777         local same_sample=$(get_rename_size $testdir1_size)
12778         [ -z "$samedir" ] && error "samedir_rename_size count error"
12779         [[ $same_sample -eq 1 ]] ||
12780                 error "samedir_rename_size error $same_sample"
12781         echo "Check same dir rename stats success"
12782
12783         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12784
12785         # check crossdir rename size
12786         mv ${testdir1}/test_0 ${testdir2}/test_0
12787
12788         testdir1_size=$(ls -l $DIR/${tdir} |
12789                 awk '/stats_testdir1/ {print $5}')
12790         testdir2_size=$(ls -l $DIR/${tdir} |
12791                 awk '/stats_testdir2/ {print $5}')
12792
12793         testdir1_size=$(order_2 $testdir1_size)
12794         testdir2_size=$(order_2 $testdir2_size)
12795
12796         testdir1_size=$(size_in_KMGT $testdir1_size)
12797         testdir2_size=$(size_in_KMGT $testdir2_size)
12798
12799         echo "source rename dir size: ${testdir1_size}"
12800         echo "target rename dir size: ${testdir2_size}"
12801
12802         eval $cmd || error "$cmd failed"
12803         local crossdir=$($cmd | grep 'crossdir')
12804         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
12805         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
12806         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
12807         [[ $src_sample -eq 1 ]] ||
12808                 error "crossdir_rename_size error $src_sample"
12809         [[ $tgt_sample -eq 1 ]] ||
12810                 error "crossdir_rename_size error $tgt_sample"
12811         echo "Check cross dir rename stats success"
12812         rm -rf $DIR/${tdir}
12813 }
12814 run_test 133d "Verifying rename_stats ========================================"
12815
12816 test_133e() {
12817         remote_mds_nodsh && skip "remote MDS with nodsh"
12818         remote_ost_nodsh && skip "remote OST with nodsh"
12819         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12820
12821         local testdir=$DIR/${tdir}/stats_testdir
12822         local ctr f0 f1 bs=32768 count=42 sum
12823
12824         mkdir -p ${testdir} || error "mkdir failed"
12825
12826         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
12827
12828         for ctr in {write,read}_bytes; do
12829                 sync
12830                 cancel_lru_locks osc
12831
12832                 do_facet ost1 $LCTL set_param -n \
12833                         "obdfilter.*.exports.clear=clear"
12834
12835                 if [ $ctr = write_bytes ]; then
12836                         f0=/dev/zero
12837                         f1=${testdir}/${tfile}
12838                 else
12839                         f0=${testdir}/${tfile}
12840                         f1=/dev/null
12841                 fi
12842
12843                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
12844                         error "dd failed"
12845                 sync
12846                 cancel_lru_locks osc
12847
12848                 sum=$(do_facet ost1 $LCTL get_param \
12849                         "obdfilter.*.exports.*.stats" |
12850                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
12851                                 $1 == ctr { sum += $7 }
12852                                 END { printf("%0.0f", sum) }')
12853
12854                 if ((sum != bs * count)); then
12855                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
12856                 fi
12857         done
12858
12859         rm -rf $DIR/${tdir}
12860 }
12861 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
12862
12863 test_133f() {
12864         [[ $(lustre_version_code $facet) -ge $(version_code 2.7.65) ]] ||
12865                 skip "too old lustre for get_param -R ($facet_ver)"
12866
12867         # verifying readability.
12868         $LCTL get_param -R '*' &> /dev/null
12869
12870         # Verifing writability with badarea_io.
12871         $LCTL list_param -FR '*' | grep '=' | tr -d = |
12872                 egrep -v 'force_lbug|changelog_mask' | xargs badarea_io ||
12873                 error "client badarea_io failed"
12874
12875         # remount the FS in case writes/reads /proc break the FS
12876         cleanup || error "failed to unmount"
12877         setup || error "failed to setup"
12878 }
12879 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
12880
12881 test_133g() {
12882         remote_mds_nodsh && skip "remote MDS with nodsh"
12883         remote_ost_nodsh && skip "remote OST with nodsh"
12884
12885         local facet
12886         for facet in mds1 ost1; do
12887                 local facet_ver=$(lustre_version_code $facet)
12888                 if [ $facet_ver -ge $(version_code 2.7.65) ]; then
12889                         do_facet $facet "$LCTL get_param -R '*'" &> /dev/null
12890                 else
12891                         log "$facet: too old lustre for get_param -R"
12892                 fi
12893                 if [ $facet_ver -ge $(version_code 2.5.54) ]; then
12894                         do_facet $facet "$LCTL list_param -FR '*' | grep '=' |
12895                                 tr -d = | egrep -v 'force_lbug|changelog_mask' |
12896                                 xargs badarea_io" ||
12897                                         error "$facet badarea_io failed"
12898                 else
12899                         skip_noexit "$facet: too old lustre for get_param -R"
12900                 fi
12901         done
12902
12903         # remount the FS in case writes/reads /proc break the FS
12904         cleanup || error "failed to unmount"
12905         setup || error "failed to setup"
12906 }
12907 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
12908
12909 test_133h() {
12910         remote_mds_nodsh && skip "remote MDS with nodsh"
12911         remote_ost_nodsh && skip "remote OST with nodsh"
12912         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
12913                 skip "Need MDS version at least 2.9.54"
12914
12915         local facet
12916         for facet in client mds1 ost1; do
12917                 # Get the list of files that are missing the terminating newline
12918                 local plist=$(do_facet $facet
12919                         $LCTL list_param -FR '*' | grep '=' | tr -d =)
12920                 local ent
12921                 for ent in $plist; do
12922                         local missing=$(do_facet $facet $LCTL get_param $ent \|\
12923                                 awk -v FS='\v' -v RS='\v\v' \
12924                                 "'END { if(NR>0 && \\\$NF !~ /.*\\\n\$/) \
12925                                         print FILENAME}'" 2>/dev/null)
12926                         [ -z $missing ] || {
12927                                 do_facet $facet $LCTL get_param $ent | od -An -tx1
12928                                 error "file does not end with newline: $facet-$ent"
12929                         }
12930                 done
12931         done
12932 }
12933 run_test 133h "Proc files should end with newlines"
12934
12935 test_134a() {
12936         remote_mds_nodsh && skip "remote MDS with nodsh"
12937         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12938                 skip "Need MDS version at least 2.7.54"
12939
12940         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12941         cancel_lru_locks mdc
12942
12943         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
12944         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12945         [ $unused -eq 0 ] || error "$unused locks are not cleared"
12946
12947         local nr=1000
12948         createmany -o $DIR/$tdir/f $nr ||
12949                 error "failed to create $nr files in $DIR/$tdir"
12950         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12951
12952         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
12953         do_facet mds1 $LCTL set_param fail_loc=0x327
12954         do_facet mds1 $LCTL set_param fail_val=500
12955         touch $DIR/$tdir/m
12956
12957         echo "sleep 10 seconds ..."
12958         sleep 10
12959         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
12960
12961         do_facet mds1 $LCTL set_param fail_loc=0
12962         do_facet mds1 $LCTL set_param fail_val=0
12963         [ $lck_cnt -lt $unused ] ||
12964                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
12965
12966         rm $DIR/$tdir/m
12967         unlinkmany $DIR/$tdir/f $nr
12968 }
12969 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
12970
12971 test_134b() {
12972         remote_mds_nodsh && skip "remote MDS with nodsh"
12973         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12974                 skip "Need MDS version at least 2.7.54"
12975
12976         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12977         cancel_lru_locks mdc
12978
12979         local low_wm=$(do_facet mds1 $LCTL get_param -n \
12980                         ldlm.lock_reclaim_threshold_mb)
12981         # disable reclaim temporarily
12982         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
12983
12984         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
12985         do_facet mds1 $LCTL set_param fail_loc=0x328
12986         do_facet mds1 $LCTL set_param fail_val=500
12987
12988         $LCTL set_param debug=+trace
12989
12990         local nr=600
12991         createmany -o $DIR/$tdir/f $nr &
12992         local create_pid=$!
12993
12994         echo "Sleep $TIMEOUT seconds ..."
12995         sleep $TIMEOUT
12996         if ! ps -p $create_pid  > /dev/null 2>&1; then
12997                 do_facet mds1 $LCTL set_param fail_loc=0
12998                 do_facet mds1 $LCTL set_param fail_val=0
12999                 do_facet mds1 $LCTL set_param \
13000                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
13001                 error "createmany finished incorrectly!"
13002         fi
13003         do_facet mds1 $LCTL set_param fail_loc=0
13004         do_facet mds1 $LCTL set_param fail_val=0
13005         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
13006         wait $create_pid || return 1
13007
13008         unlinkmany $DIR/$tdir/f $nr
13009 }
13010 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
13011
13012 test_135() {
13013         remote_mds_nodsh && skip "remote MDS with nodsh"
13014         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
13015                 skip "Need MDS version at least 2.13.50"
13016         local fname
13017
13018         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
13019
13020 #define OBD_FAIL_PLAIN_RECORDS 0x1319
13021         #set only one record at plain llog
13022         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1319 fail_val=1
13023
13024         #fill already existed plain llog each 64767
13025         #wrapping whole catalog
13026         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
13027
13028         createmany -o $DIR/$tdir/$tfile_ 64700
13029         for (( i = 0; i < 64700; i = i + 2 ))
13030         do
13031                 rm $DIR/$tdir/$tfile_$i &
13032                 rm $DIR/$tdir/$tfile_$((i + 1)) &
13033                 local pid=$!
13034                 wait $pid
13035         done
13036
13037         #waiting osp synchronization
13038         wait_delete_completed
13039 }
13040 run_test 135 "Race catalog processing"
13041
13042 test_136() {
13043         remote_mds_nodsh && skip "remote MDS with nodsh"
13044         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
13045                 skip "Need MDS version at least 2.13.50"
13046         local fname
13047
13048         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
13049         $LFS setstripe -c 1 -i 0 $DIR/$tdir || error "failed to set striping"
13050         #set only one record at plain llog
13051 #define OBD_FAIL_CATALOG_FULL_CHECK                0x131a
13052         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x131a fail_val=1
13053
13054         #fill already existed 2 plain llogs each 64767
13055         #wrapping whole catalog
13056         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
13057         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 3 / 2))
13058         wait_delete_completed
13059
13060         createmany -o $DIR/$tdir/$tfile_ 10
13061         sleep 25
13062
13063         do_facet $SINGLEMDS $LCTL set_param fail_val=3
13064         for (( i = 0; i < 10; i = i + 3 ))
13065         do
13066                 rm $DIR/$tdir/$tfile_$i &
13067                 rm $DIR/$tdir/$tfile_$((i + 1)) &
13068                 local pid=$!
13069                 wait $pid
13070                 sleep 7
13071                 rm $DIR/$tdir/$tfile_$((i + 2)) &
13072         done
13073
13074         #waiting osp synchronization
13075         wait_delete_completed
13076 }
13077 run_test 136 "Race catalog processing 2"
13078
13079 test_140() { #bug-17379
13080         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13081
13082         test_mkdir $DIR/$tdir
13083         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
13084         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
13085
13086         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
13087         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
13088         local i=0
13089         while i=$((i + 1)); do
13090                 test_mkdir $i
13091                 cd $i || error "Changing to $i"
13092                 ln -s ../stat stat || error "Creating stat symlink"
13093                 # Read the symlink until ELOOP present,
13094                 # not LBUGing the system is considered success,
13095                 # we didn't overrun the stack.
13096                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
13097                 if [ $ret -ne 0 ]; then
13098                         if [ $ret -eq 40 ]; then
13099                                 break  # -ELOOP
13100                         else
13101                                 error "Open stat symlink"
13102                                         return
13103                         fi
13104                 fi
13105         done
13106         i=$((i - 1))
13107         echo "The symlink depth = $i"
13108         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
13109                 error "Invalid symlink depth"
13110
13111         # Test recursive symlink
13112         ln -s symlink_self symlink_self
13113         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
13114         echo "open symlink_self returns $ret"
13115         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
13116 }
13117 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
13118
13119 test_150a() {
13120         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13121
13122         local TF="$TMP/$tfile"
13123
13124         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
13125         cp $TF $DIR/$tfile
13126         cancel_lru_locks $OSC
13127         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
13128         remount_client $MOUNT
13129         df -P $MOUNT
13130         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
13131
13132         $TRUNCATE $TF 6000
13133         $TRUNCATE $DIR/$tfile 6000
13134         cancel_lru_locks $OSC
13135         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
13136
13137         echo "12345" >>$TF
13138         echo "12345" >>$DIR/$tfile
13139         cancel_lru_locks $OSC
13140         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
13141
13142         echo "12345" >>$TF
13143         echo "12345" >>$DIR/$tfile
13144         cancel_lru_locks $OSC
13145         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
13146
13147         rm -f $TF
13148         true
13149 }
13150 run_test 150a "truncate/append tests"
13151
13152 test_150b() {
13153         [ "$ost1_FSTYPE" != ldiskfs ] && skip "non-ldiskfs backend"
13154         [ $OST1_VERSION -lt $(version_code 2.13.50) ] &&
13155                 skip "Need OST version at least 2.13.53"
13156         touch $DIR/$tfile
13157         check_fallocate $DIR/$tfile || error "fallocate failed"
13158 }
13159 run_test 150b "Verify fallocate (prealloc) functionality"
13160
13161 test_150c() {
13162         local bytes
13163         local want
13164
13165         [ "$ost1_FSTYPE" != ldiskfs ] && skip "non-ldiskfs backend"
13166         [ $OST1_VERSION -lt $(version_code 2.13.50) ] &&
13167                 skip "Need OST version at least 2.13.53"
13168
13169         $LFS setstripe -c $OSTCOUNT -S1M $DIR/$tdir || error "setstripe failed"
13170         fallocate -l ${OSTCOUNT}m $DIR/$tdir || error "fallocate failed"
13171         sync; sync_all_data
13172         cancel_lru_locks $OSC
13173         sleep 5
13174         bytes=$(($(stat -c '%b * %B' $DIR/$tdir)))
13175         want=$((OSTCOUNT * 1048576))
13176
13177         # Must allocate all requested space, not more than 5% extra
13178         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
13179                 error "bytes $bytes is not $want"
13180 }
13181 run_test 150c "Verify fallocate Size and Blocks"
13182
13183 test_150d() {
13184         local bytes
13185         local want
13186
13187         [ "$ost1_FSTYPE" != ldiskfs ] && skip "non-ldiskfs backend"
13188         [ $OST1_VERSION -lt $(version_code 2.13.50) ] &&
13189                 skip "Need OST version at least 2.13.53"
13190
13191         $LFS setstripe -c $OSTCOUNT -S1M $DIR/$tdir || error "setstripe failed"
13192         fallocate -o 1G -l ${OSTCOUNT}m $DIR/$tdir || error "fallocate failed"
13193         sync; sync_all_data
13194         cancel_lru_locks $OSC
13195         sleep 5
13196         bytes=$(($(stat -c '%b * %B' $DIR/$tdir)))
13197         want=$((OSTCOUNT * 1048576))
13198
13199         # Must allocate all requested space, not more than 5% extra
13200         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
13201                 error "bytes $bytes is not $want"
13202 }
13203 run_test 150d "Verify fallocate Size and Blocks - Non zero start"
13204
13205 #LU-2902 roc_hit was not able to read all values from lproc
13206 function roc_hit_init() {
13207         local list=$(comma_list $(osts_nodes))
13208         local dir=$DIR/$tdir-check
13209         local file=$dir/$tfile
13210         local BEFORE
13211         local AFTER
13212         local idx
13213
13214         test_mkdir $dir
13215         #use setstripe to do a write to every ost
13216         for i in $(seq 0 $((OSTCOUNT-1))); do
13217                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
13218                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
13219                 idx=$(printf %04x $i)
13220                 BEFORE=$(get_osd_param $list *OST*$idx stats |
13221                         awk '$1 == "cache_access" {sum += $7}
13222                                 END { printf("%0.0f", sum) }')
13223
13224                 cancel_lru_locks osc
13225                 cat $file >/dev/null
13226
13227                 AFTER=$(get_osd_param $list *OST*$idx stats |
13228                         awk '$1 == "cache_access" {sum += $7}
13229                                 END { printf("%0.0f", sum) }')
13230
13231                 echo BEFORE:$BEFORE AFTER:$AFTER
13232                 if ! let "AFTER - BEFORE == 4"; then
13233                         rm -rf $dir
13234                         error "roc_hit is not safe to use"
13235                 fi
13236                 rm $file
13237         done
13238
13239         rm -rf $dir
13240 }
13241
13242 function roc_hit() {
13243         local list=$(comma_list $(osts_nodes))
13244         echo $(get_osd_param $list '' stats |
13245                 awk '$1 == "cache_hit" {sum += $7}
13246                         END { printf("%0.0f", sum) }')
13247 }
13248
13249 function set_cache() {
13250         local on=1
13251
13252         if [ "$2" == "off" ]; then
13253                 on=0;
13254         fi
13255         local list=$(comma_list $(osts_nodes))
13256         set_osd_param $list '' $1_cache_enable $on
13257
13258         cancel_lru_locks osc
13259 }
13260
13261 test_151() {
13262         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13263         remote_ost_nodsh && skip "remote OST with nodsh"
13264
13265         local CPAGES=3
13266         local list=$(comma_list $(osts_nodes))
13267
13268         # check whether obdfilter is cache capable at all
13269         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
13270                 skip "not cache-capable obdfilter"
13271         fi
13272
13273         # check cache is enabled on all obdfilters
13274         if get_osd_param $list '' read_cache_enable | grep 0; then
13275                 skip "oss cache is disabled"
13276         fi
13277
13278         set_osd_param $list '' writethrough_cache_enable 1
13279
13280         # check write cache is enabled on all obdfilters
13281         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
13282                 skip "oss write cache is NOT enabled"
13283         fi
13284
13285         roc_hit_init
13286
13287         #define OBD_FAIL_OBD_NO_LRU  0x609
13288         do_nodes $list $LCTL set_param fail_loc=0x609
13289
13290         # pages should be in the case right after write
13291         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
13292                 error "dd failed"
13293
13294         local BEFORE=$(roc_hit)
13295         cancel_lru_locks osc
13296         cat $DIR/$tfile >/dev/null
13297         local AFTER=$(roc_hit)
13298
13299         do_nodes $list $LCTL set_param fail_loc=0
13300
13301         if ! let "AFTER - BEFORE == CPAGES"; then
13302                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13303         fi
13304
13305         cancel_lru_locks osc
13306         # invalidates OST cache
13307         do_nodes $list "echo 1 > /proc/sys/vm/drop_caches"
13308         set_osd_param $list '' read_cache_enable 0
13309         cat $DIR/$tfile >/dev/null
13310
13311         # now data shouldn't be found in the cache
13312         BEFORE=$(roc_hit)
13313         cancel_lru_locks osc
13314         cat $DIR/$tfile >/dev/null
13315         AFTER=$(roc_hit)
13316         if let "AFTER - BEFORE != 0"; then
13317                 error "IN CACHE: before: $BEFORE, after: $AFTER"
13318         fi
13319
13320         set_osd_param $list '' read_cache_enable 1
13321         rm -f $DIR/$tfile
13322 }
13323 run_test 151 "test cache on oss and controls ==============================="
13324
13325 test_152() {
13326         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13327
13328         local TF="$TMP/$tfile"
13329
13330         # simulate ENOMEM during write
13331 #define OBD_FAIL_OST_NOMEM      0x226
13332         lctl set_param fail_loc=0x80000226
13333         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
13334         cp $TF $DIR/$tfile
13335         sync || error "sync failed"
13336         lctl set_param fail_loc=0
13337
13338         # discard client's cache
13339         cancel_lru_locks osc
13340
13341         # simulate ENOMEM during read
13342         lctl set_param fail_loc=0x80000226
13343         cmp $TF $DIR/$tfile || error "cmp failed"
13344         lctl set_param fail_loc=0
13345
13346         rm -f $TF
13347 }
13348 run_test 152 "test read/write with enomem ============================"
13349
13350 test_153() {
13351         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
13352 }
13353 run_test 153 "test if fdatasync does not crash ======================="
13354
13355 dot_lustre_fid_permission_check() {
13356         local fid=$1
13357         local ffid=$MOUNT/.lustre/fid/$fid
13358         local test_dir=$2
13359
13360         echo "stat fid $fid"
13361         stat $ffid > /dev/null || error "stat $ffid failed."
13362         echo "touch fid $fid"
13363         touch $ffid || error "touch $ffid failed."
13364         echo "write to fid $fid"
13365         cat /etc/hosts > $ffid || error "write $ffid failed."
13366         echo "read fid $fid"
13367         diff /etc/hosts $ffid || error "read $ffid failed."
13368         echo "append write to fid $fid"
13369         cat /etc/hosts >> $ffid || error "append write $ffid failed."
13370         echo "rename fid $fid"
13371         mv $ffid $test_dir/$tfile.1 &&
13372                 error "rename $ffid to $tfile.1 should fail."
13373         touch $test_dir/$tfile.1
13374         mv $test_dir/$tfile.1 $ffid &&
13375                 error "rename $tfile.1 to $ffid should fail."
13376         rm -f $test_dir/$tfile.1
13377         echo "truncate fid $fid"
13378         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
13379         echo "link fid $fid"
13380         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
13381         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
13382                 echo "setfacl fid $fid"
13383                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
13384                 echo "getfacl fid $fid"
13385                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
13386         fi
13387         echo "unlink fid $fid"
13388         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
13389         echo "mknod fid $fid"
13390         mknod $ffid c 1 3 && error "mknod $ffid should fail."
13391
13392         fid=[0xf00000400:0x1:0x0]
13393         ffid=$MOUNT/.lustre/fid/$fid
13394
13395         echo "stat non-exist fid $fid"
13396         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
13397         echo "write to non-exist fid $fid"
13398         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
13399         echo "link new fid $fid"
13400         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
13401
13402         mkdir -p $test_dir/$tdir
13403         touch $test_dir/$tdir/$tfile
13404         fid=$($LFS path2fid $test_dir/$tdir)
13405         rc=$?
13406         [ $rc -ne 0 ] &&
13407                 error "error: could not get fid for $test_dir/$dir/$tfile."
13408
13409         ffid=$MOUNT/.lustre/fid/$fid
13410
13411         echo "ls $fid"
13412         ls $ffid > /dev/null || error "ls $ffid failed."
13413         echo "touch $fid/$tfile.1"
13414         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
13415
13416         echo "touch $MOUNT/.lustre/fid/$tfile"
13417         touch $MOUNT/.lustre/fid/$tfile && \
13418                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
13419
13420         echo "setxattr to $MOUNT/.lustre/fid"
13421         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
13422
13423         echo "listxattr for $MOUNT/.lustre/fid"
13424         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
13425
13426         echo "delxattr from $MOUNT/.lustre/fid"
13427         setfattr -x trusted.name1 $MOUNT/.lustre/fid
13428
13429         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
13430         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
13431                 error "touch invalid fid should fail."
13432
13433         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
13434         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
13435                 error "touch non-normal fid should fail."
13436
13437         echo "rename $tdir to $MOUNT/.lustre/fid"
13438         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
13439                 error "rename to $MOUNT/.lustre/fid should fail."
13440
13441         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
13442         then            # LU-3547
13443                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
13444                 local new_obf_mode=777
13445
13446                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
13447                 chmod $new_obf_mode $DIR/.lustre/fid ||
13448                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
13449
13450                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
13451                 [ $obf_mode -eq $new_obf_mode ] ||
13452                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
13453
13454                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
13455                 chmod $old_obf_mode $DIR/.lustre/fid ||
13456                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
13457         fi
13458
13459         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
13460         fid=$($LFS path2fid $test_dir/$tfile-2)
13461
13462         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
13463         then # LU-5424
13464                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
13465                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
13466                         error "create lov data thru .lustre failed"
13467         fi
13468         echo "cp /etc/passwd $test_dir/$tfile-2"
13469         cp /etc/passwd $test_dir/$tfile-2 ||
13470                 error "copy to $test_dir/$tfile-2 failed."
13471         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
13472         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
13473                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
13474
13475         rm -rf $test_dir/tfile.lnk
13476         rm -rf $test_dir/$tfile-2
13477 }
13478
13479 test_154A() {
13480         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13481                 skip "Need MDS version at least 2.4.1"
13482
13483         local tf=$DIR/$tfile
13484         touch $tf
13485
13486         local fid=$($LFS path2fid $tf)
13487         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
13488
13489         # check that we get the same pathname back
13490         local found=$($LFS fid2path $MOUNT "$fid")
13491         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
13492         [ "$found" == "$tf" ] ||
13493                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
13494 }
13495 run_test 154A "lfs path2fid and fid2path basic checks"
13496
13497 test_154B() {
13498         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13499                 skip "Need MDS version at least 2.4.1"
13500
13501         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
13502         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
13503         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
13504         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
13505
13506         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
13507         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
13508
13509         # check that we get the same pathname
13510         echo "PFID: $PFID, name: $name"
13511         local FOUND=$($LFS fid2path $MOUNT "$PFID")
13512         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
13513         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
13514                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
13515
13516         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
13517 }
13518 run_test 154B "verify the ll_decode_linkea tool"
13519
13520 test_154a() {
13521         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13522         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13523         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
13524                 skip "Need MDS version at least 2.2.51"
13525         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
13526
13527         cp /etc/hosts $DIR/$tfile
13528
13529         fid=$($LFS path2fid $DIR/$tfile)
13530         rc=$?
13531         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
13532
13533         dot_lustre_fid_permission_check "$fid" $DIR ||
13534                 error "dot lustre permission check $fid failed"
13535
13536         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
13537
13538         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
13539
13540         touch $MOUNT/.lustre/file &&
13541                 error "creation is not allowed under .lustre"
13542
13543         mkdir $MOUNT/.lustre/dir &&
13544                 error "mkdir is not allowed under .lustre"
13545
13546         rm -rf $DIR/$tfile
13547 }
13548 run_test 154a "Open-by-FID"
13549
13550 test_154b() {
13551         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13552         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13553         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13554         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
13555                 skip "Need MDS version at least 2.2.51"
13556
13557         local remote_dir=$DIR/$tdir/remote_dir
13558         local MDTIDX=1
13559         local rc=0
13560
13561         mkdir -p $DIR/$tdir
13562         $LFS mkdir -i $MDTIDX $remote_dir ||
13563                 error "create remote directory failed"
13564
13565         cp /etc/hosts $remote_dir/$tfile
13566
13567         fid=$($LFS path2fid $remote_dir/$tfile)
13568         rc=$?
13569         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
13570
13571         dot_lustre_fid_permission_check "$fid" $remote_dir ||
13572                 error "dot lustre permission check $fid failed"
13573         rm -rf $DIR/$tdir
13574 }
13575 run_test 154b "Open-by-FID for remote directory"
13576
13577 test_154c() {
13578         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13579                 skip "Need MDS version at least 2.4.1"
13580
13581         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
13582         local FID1=$($LFS path2fid $DIR/$tfile.1)
13583         local FID2=$($LFS path2fid $DIR/$tfile.2)
13584         local FID3=$($LFS path2fid $DIR/$tfile.3)
13585
13586         local N=1
13587         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
13588                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
13589                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
13590                 local want=FID$N
13591                 [ "$FID" = "${!want}" ] ||
13592                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
13593                 N=$((N + 1))
13594         done
13595
13596         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
13597         do
13598                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
13599                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
13600                 N=$((N + 1))
13601         done
13602 }
13603 run_test 154c "lfs path2fid and fid2path multiple arguments"
13604
13605 test_154d() {
13606         remote_mds_nodsh && skip "remote MDS with nodsh"
13607         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
13608                 skip "Need MDS version at least 2.5.53"
13609
13610         if remote_mds; then
13611                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
13612         else
13613                 nid="0@lo"
13614         fi
13615         local proc_ofile="mdt.*.exports.'$nid'.open_files"
13616         local fd
13617         local cmd
13618
13619         rm -f $DIR/$tfile
13620         touch $DIR/$tfile
13621
13622         local fid=$($LFS path2fid $DIR/$tfile)
13623         # Open the file
13624         fd=$(free_fd)
13625         cmd="exec $fd<$DIR/$tfile"
13626         eval $cmd
13627         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
13628         echo "$fid_list" | grep "$fid"
13629         rc=$?
13630
13631         cmd="exec $fd>/dev/null"
13632         eval $cmd
13633         if [ $rc -ne 0 ]; then
13634                 error "FID $fid not found in open files list $fid_list"
13635         fi
13636 }
13637 run_test 154d "Verify open file fid"
13638
13639 test_154e()
13640 {
13641         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
13642                 skip "Need MDS version at least 2.6.50"
13643
13644         if ls -a $MOUNT | grep -q '^\.lustre$'; then
13645                 error ".lustre returned by readdir"
13646         fi
13647 }
13648 run_test 154e ".lustre is not returned by readdir"
13649
13650 test_154f() {
13651         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13652
13653         # create parent directory on a single MDT to avoid cross-MDT hardlinks
13654         test_mkdir -p -c1 $DIR/$tdir/d
13655         # test dirs inherit from its stripe
13656         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
13657         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
13658         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
13659         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
13660         touch $DIR/f
13661
13662         # get fid of parents
13663         local FID0=$($LFS path2fid $DIR/$tdir/d)
13664         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
13665         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
13666         local FID3=$($LFS path2fid $DIR)
13667
13668         # check that path2fid --parents returns expected <parent_fid>/name
13669         # 1) test for a directory (single parent)
13670         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
13671         [ "$parent" == "$FID0/foo1" ] ||
13672                 error "expected parent: $FID0/foo1, got: $parent"
13673
13674         # 2) test for a file with nlink > 1 (multiple parents)
13675         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
13676         echo "$parent" | grep -F "$FID1/$tfile" ||
13677                 error "$FID1/$tfile not returned in parent list"
13678         echo "$parent" | grep -F "$FID2/link" ||
13679                 error "$FID2/link not returned in parent list"
13680
13681         # 3) get parent by fid
13682         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
13683         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13684         echo "$parent" | grep -F "$FID1/$tfile" ||
13685                 error "$FID1/$tfile not returned in parent list (by fid)"
13686         echo "$parent" | grep -F "$FID2/link" ||
13687                 error "$FID2/link not returned in parent list (by fid)"
13688
13689         # 4) test for entry in root directory
13690         parent=$($LFS path2fid --parents $DIR/f)
13691         echo "$parent" | grep -F "$FID3/f" ||
13692                 error "$FID3/f not returned in parent list"
13693
13694         # 5) test it on root directory
13695         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
13696                 error "$MOUNT should not have parents"
13697
13698         # enable xattr caching and check that linkea is correctly updated
13699         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
13700         save_lustre_params client "llite.*.xattr_cache" > $save
13701         lctl set_param llite.*.xattr_cache 1
13702
13703         # 6.1) linkea update on rename
13704         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
13705
13706         # get parents by fid
13707         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13708         # foo1 should no longer be returned in parent list
13709         echo "$parent" | grep -F "$FID1" &&
13710                 error "$FID1 should no longer be in parent list"
13711         # the new path should appear
13712         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
13713                 error "$FID2/$tfile.moved is not in parent list"
13714
13715         # 6.2) linkea update on unlink
13716         rm -f $DIR/$tdir/d/foo2/link
13717         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13718         # foo2/link should no longer be returned in parent list
13719         echo "$parent" | grep -F "$FID2/link" &&
13720                 error "$FID2/link should no longer be in parent list"
13721         true
13722
13723         rm -f $DIR/f
13724         restore_lustre_params < $save
13725         rm -f $save
13726 }
13727 run_test 154f "get parent fids by reading link ea"
13728
13729 test_154g()
13730 {
13731         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13732         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
13733            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
13734                 skip "Need MDS version at least 2.6.92"
13735
13736         mkdir -p $DIR/$tdir
13737         llapi_fid_test -d $DIR/$tdir
13738 }
13739 run_test 154g "various llapi FID tests"
13740
13741 test_155_small_load() {
13742     local temp=$TMP/$tfile
13743     local file=$DIR/$tfile
13744
13745     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
13746         error "dd of=$temp bs=6096 count=1 failed"
13747     cp $temp $file
13748     cancel_lru_locks $OSC
13749     cmp $temp $file || error "$temp $file differ"
13750
13751     $TRUNCATE $temp 6000
13752     $TRUNCATE $file 6000
13753     cmp $temp $file || error "$temp $file differ (truncate1)"
13754
13755     echo "12345" >>$temp
13756     echo "12345" >>$file
13757     cmp $temp $file || error "$temp $file differ (append1)"
13758
13759     echo "12345" >>$temp
13760     echo "12345" >>$file
13761     cmp $temp $file || error "$temp $file differ (append2)"
13762
13763     rm -f $temp $file
13764     true
13765 }
13766
13767 test_155_big_load() {
13768         remote_ost_nodsh && skip "remote OST with nodsh"
13769
13770         local temp=$TMP/$tfile
13771         local file=$DIR/$tfile
13772
13773         free_min_max
13774         local cache_size=$(do_facet ost$((MAXI+1)) \
13775                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
13776         local large_file_size=$((cache_size * 2))
13777
13778         echo "OSS cache size: $cache_size KB"
13779         echo "Large file size: $large_file_size KB"
13780
13781         [ $MAXV -le $large_file_size ] &&
13782                 skip_env "max available OST size needs > $large_file_size KB"
13783
13784         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
13785
13786         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
13787                 error "dd of=$temp bs=$large_file_size count=1k failed"
13788         cp $temp $file
13789         ls -lh $temp $file
13790         cancel_lru_locks osc
13791         cmp $temp $file || error "$temp $file differ"
13792
13793         rm -f $temp $file
13794         true
13795 }
13796
13797 save_writethrough() {
13798         local facets=$(get_facets OST)
13799
13800         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
13801 }
13802
13803 test_155a() {
13804         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13805
13806         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13807
13808         save_writethrough $p
13809
13810         set_cache read on
13811         set_cache writethrough on
13812         test_155_small_load
13813         restore_lustre_params < $p
13814         rm -f $p
13815 }
13816 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
13817
13818 test_155b() {
13819         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13820
13821         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13822
13823         save_writethrough $p
13824
13825         set_cache read on
13826         set_cache writethrough off
13827         test_155_small_load
13828         restore_lustre_params < $p
13829         rm -f $p
13830 }
13831 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
13832
13833 test_155c() {
13834         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13835
13836         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13837
13838         save_writethrough $p
13839
13840         set_cache read off
13841         set_cache writethrough on
13842         test_155_small_load
13843         restore_lustre_params < $p
13844         rm -f $p
13845 }
13846 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
13847
13848 test_155d() {
13849         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13850
13851         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13852
13853         save_writethrough $p
13854
13855         set_cache read off
13856         set_cache writethrough off
13857         test_155_small_load
13858         restore_lustre_params < $p
13859         rm -f $p
13860 }
13861 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
13862
13863 test_155e() {
13864         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13865
13866         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13867
13868         save_writethrough $p
13869
13870         set_cache read on
13871         set_cache writethrough on
13872         test_155_big_load
13873         restore_lustre_params < $p
13874         rm -f $p
13875 }
13876 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
13877
13878 test_155f() {
13879         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13880
13881         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13882
13883         save_writethrough $p
13884
13885         set_cache read on
13886         set_cache writethrough off
13887         test_155_big_load
13888         restore_lustre_params < $p
13889         rm -f $p
13890 }
13891 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
13892
13893 test_155g() {
13894         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13895
13896         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13897
13898         save_writethrough $p
13899
13900         set_cache read off
13901         set_cache writethrough on
13902         test_155_big_load
13903         restore_lustre_params < $p
13904         rm -f $p
13905 }
13906 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
13907
13908 test_155h() {
13909         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13910
13911         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13912
13913         save_writethrough $p
13914
13915         set_cache read off
13916         set_cache writethrough off
13917         test_155_big_load
13918         restore_lustre_params < $p
13919         rm -f $p
13920 }
13921 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
13922
13923 test_156() {
13924         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13925         remote_ost_nodsh && skip "remote OST with nodsh"
13926         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
13927                 skip "stats not implemented on old servers"
13928         [ "$ost1_FSTYPE" = "zfs" ] &&
13929                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
13930
13931         local CPAGES=3
13932         local BEFORE
13933         local AFTER
13934         local file="$DIR/$tfile"
13935         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13936
13937         save_writethrough $p
13938         roc_hit_init
13939
13940         log "Turn on read and write cache"
13941         set_cache read on
13942         set_cache writethrough on
13943
13944         log "Write data and read it back."
13945         log "Read should be satisfied from the cache."
13946         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13947         BEFORE=$(roc_hit)
13948         cancel_lru_locks osc
13949         cat $file >/dev/null
13950         AFTER=$(roc_hit)
13951         if ! let "AFTER - BEFORE == CPAGES"; then
13952                 error "NOT IN CACHE (2): before: $BEFORE, after: $AFTER"
13953         else
13954                 log "cache hits: before: $BEFORE, after: $AFTER"
13955         fi
13956
13957         log "Read again; it should be satisfied from the cache."
13958         BEFORE=$AFTER
13959         cancel_lru_locks osc
13960         cat $file >/dev/null
13961         AFTER=$(roc_hit)
13962         if ! let "AFTER - BEFORE == CPAGES"; then
13963                 error "NOT IN CACHE (3): before: $BEFORE, after: $AFTER"
13964         else
13965                 log "cache hits:: before: $BEFORE, after: $AFTER"
13966         fi
13967
13968         log "Turn off the read cache and turn on the write cache"
13969         set_cache read off
13970         set_cache writethrough on
13971
13972         log "Read again; it should be satisfied from the cache."
13973         BEFORE=$(roc_hit)
13974         cancel_lru_locks osc
13975         cat $file >/dev/null
13976         AFTER=$(roc_hit)
13977         if ! let "AFTER - BEFORE == CPAGES"; then
13978                 error "NOT IN CACHE (4): before: $BEFORE, after: $AFTER"
13979         else
13980                 log "cache hits:: before: $BEFORE, after: $AFTER"
13981         fi
13982
13983         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
13984                 # > 2.12.56 uses pagecache if cached
13985                 log "Read again; it should not be satisfied from the cache."
13986                 BEFORE=$AFTER
13987                 cancel_lru_locks osc
13988                 cat $file >/dev/null
13989                 AFTER=$(roc_hit)
13990                 if ! let "AFTER - BEFORE == 0"; then
13991                         error "IN CACHE (5): before: $BEFORE, after: $AFTER"
13992                 else
13993                         log "cache hits:: before: $BEFORE, after: $AFTER"
13994                 fi
13995         fi
13996
13997         log "Write data and read it back."
13998         log "Read should be satisfied from the cache."
13999         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
14000         BEFORE=$(roc_hit)
14001         cancel_lru_locks osc
14002         cat $file >/dev/null
14003         AFTER=$(roc_hit)
14004         if ! let "AFTER - BEFORE == CPAGES"; then
14005                 error "NOT IN CACHE (6): before: $BEFORE, after: $AFTER"
14006         else
14007                 log "cache hits:: before: $BEFORE, after: $AFTER"
14008         fi
14009
14010         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
14011                 # > 2.12.56 uses pagecache if cached
14012                 log "Read again; it should not be satisfied from the cache."
14013                 BEFORE=$AFTER
14014                 cancel_lru_locks osc
14015                 cat $file >/dev/null
14016                 AFTER=$(roc_hit)
14017                 if ! let "AFTER - BEFORE == 0"; then
14018                         error "IN CACHE (7): before: $BEFORE, after: $AFTER"
14019                 else
14020                         log "cache hits:: before: $BEFORE, after: $AFTER"
14021                 fi
14022         fi
14023
14024         log "Turn off read and write cache"
14025         set_cache read off
14026         set_cache writethrough off
14027
14028         log "Write data and read it back"
14029         log "It should not be satisfied from the cache."
14030         rm -f $file
14031         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
14032         cancel_lru_locks osc
14033         BEFORE=$(roc_hit)
14034         cat $file >/dev/null
14035         AFTER=$(roc_hit)
14036         if ! let "AFTER - BEFORE == 0"; then
14037                 error_ignore bz20762 "IN CACHE (8):before:$BEFORE,after:$AFTER"
14038         else
14039                 log "cache hits:: before: $BEFORE, after: $AFTER"
14040         fi
14041
14042         log "Turn on the read cache and turn off the write cache"
14043         set_cache read on
14044         set_cache writethrough off
14045
14046         log "Write data and read it back"
14047         log "It should not be satisfied from the cache."
14048         rm -f $file
14049         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
14050         BEFORE=$(roc_hit)
14051         cancel_lru_locks osc
14052         cat $file >/dev/null
14053         AFTER=$(roc_hit)
14054         if ! let "AFTER - BEFORE == 0"; then
14055                 error_ignore bz20762 "IN CACHE (9):before:$BEFORE,after:$AFTER"
14056         else
14057                 log "cache hits:: before: $BEFORE, after: $AFTER"
14058         fi
14059
14060         log "Read again; it should be satisfied from the cache."
14061         BEFORE=$(roc_hit)
14062         cancel_lru_locks osc
14063         cat $file >/dev/null
14064         AFTER=$(roc_hit)
14065         if ! let "AFTER - BEFORE == CPAGES"; then
14066                 error "NOT IN CACHE (1): before: $BEFORE, after: $AFTER"
14067         else
14068                 log "cache hits:: before: $BEFORE, after: $AFTER"
14069         fi
14070
14071         restore_lustre_params < $p
14072         rm -f $p $file
14073 }
14074 run_test 156 "Verification of tunables"
14075
14076 test_160a() {
14077         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14078         remote_mds_nodsh && skip "remote MDS with nodsh"
14079         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
14080                 skip "Need MDS version at least 2.2.0"
14081
14082         changelog_register || error "changelog_register failed"
14083         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14084         changelog_users $SINGLEMDS | grep -q $cl_user ||
14085                 error "User $cl_user not found in changelog_users"
14086
14087         # change something
14088         test_mkdir -p $DIR/$tdir/pics/2008/zachy
14089         changelog_clear 0 || error "changelog_clear failed"
14090         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
14091         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
14092         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
14093         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
14094         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
14095         rm $DIR/$tdir/pics/desktop.jpg
14096
14097         changelog_dump | tail -10
14098
14099         echo "verifying changelog mask"
14100         changelog_chmask "-MKDIR"
14101         changelog_chmask "-CLOSE"
14102
14103         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
14104         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
14105
14106         changelog_chmask "+MKDIR"
14107         changelog_chmask "+CLOSE"
14108
14109         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
14110         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
14111
14112         changelog_dump | tail -10
14113         MKDIRS=$(changelog_dump | grep -c "MKDIR")
14114         CLOSES=$(changelog_dump | grep -c "CLOSE")
14115         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
14116         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
14117
14118         # verify contents
14119         echo "verifying target fid"
14120         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
14121         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
14122         [ "$fidc" == "$fidf" ] ||
14123                 error "changelog '$tfile' fid $fidc != file fid $fidf"
14124         echo "verifying parent fid"
14125         # The FID returned from the Changelog may be the directory shard on
14126         # a different MDT, and not the FID returned by path2fid on the parent.
14127         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
14128         # since this is what will matter when recreating this file in the tree.
14129         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
14130         local pathp=$($LFS fid2path $MOUNT "$fidp")
14131         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
14132                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
14133
14134         echo "getting records for $cl_user"
14135         changelog_users $SINGLEMDS
14136         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
14137         local nclr=3
14138         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
14139                 error "changelog_clear failed"
14140         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
14141         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
14142         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
14143                 error "user index expect $user_rec1 + $nclr != $user_rec2"
14144
14145         local min0_rec=$(changelog_users $SINGLEMDS |
14146                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
14147         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
14148                           awk '{ print $1; exit; }')
14149
14150         changelog_dump | tail -n 5
14151         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
14152         [ $first_rec == $((min0_rec + 1)) ] ||
14153                 error "first index should be $min0_rec + 1 not $first_rec"
14154
14155         # LU-3446 changelog index reset on MDT restart
14156         local cur_rec1=$(changelog_users $SINGLEMDS |
14157                          awk '/^current.index:/ { print $NF }')
14158         changelog_clear 0 ||
14159                 error "clear all changelog records for $cl_user failed"
14160         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
14161         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
14162                 error "Fail to start $SINGLEMDS"
14163         local cur_rec2=$(changelog_users $SINGLEMDS |
14164                          awk '/^current.index:/ { print $NF }')
14165         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
14166         [ $cur_rec1 == $cur_rec2 ] ||
14167                 error "current index should be $cur_rec1 not $cur_rec2"
14168
14169         echo "verifying users from this test are deregistered"
14170         changelog_deregister || error "changelog_deregister failed"
14171         changelog_users $SINGLEMDS | grep -q $cl_user &&
14172                 error "User '$cl_user' still in changelog_users"
14173
14174         # lctl get_param -n mdd.*.changelog_users
14175         # current index: 144
14176         # ID    index (idle seconds)
14177         # cl3   144 (2)
14178         if ! changelog_users $SINGLEMDS | grep "^cl"; then
14179                 # this is the normal case where all users were deregistered
14180                 # make sure no new records are added when no users are present
14181                 local last_rec1=$(changelog_users $SINGLEMDS |
14182                                   awk '/^current.index:/ { print $NF }')
14183                 touch $DIR/$tdir/chloe
14184                 local last_rec2=$(changelog_users $SINGLEMDS |
14185                                   awk '/^current.index:/ { print $NF }')
14186                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
14187                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
14188         else
14189                 # any changelog users must be leftovers from a previous test
14190                 changelog_users $SINGLEMDS
14191                 echo "other changelog users; can't verify off"
14192         fi
14193 }
14194 run_test 160a "changelog sanity"
14195
14196 test_160b() { # LU-3587
14197         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14198         remote_mds_nodsh && skip "remote MDS with nodsh"
14199         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
14200                 skip "Need MDS version at least 2.2.0"
14201
14202         changelog_register || error "changelog_register failed"
14203         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14204         changelog_users $SINGLEMDS | grep -q $cl_user ||
14205                 error "User '$cl_user' not found in changelog_users"
14206
14207         local longname1=$(str_repeat a 255)
14208         local longname2=$(str_repeat b 255)
14209
14210         cd $DIR
14211         echo "creating very long named file"
14212         touch $longname1 || error "create of '$longname1' failed"
14213         echo "renaming very long named file"
14214         mv $longname1 $longname2
14215
14216         changelog_dump | grep RENME | tail -n 5
14217         rm -f $longname2
14218 }
14219 run_test 160b "Verify that very long rename doesn't crash in changelog"
14220
14221 test_160c() {
14222         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14223         remote_mds_nodsh && skip "remote MDS with nodsh"
14224
14225         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
14226                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
14227                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
14228                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
14229
14230         local rc=0
14231
14232         # Registration step
14233         changelog_register || error "changelog_register failed"
14234
14235         rm -rf $DIR/$tdir
14236         mkdir -p $DIR/$tdir
14237         $MCREATE $DIR/$tdir/foo_160c
14238         changelog_chmask "-TRUNC"
14239         $TRUNCATE $DIR/$tdir/foo_160c 200
14240         changelog_chmask "+TRUNC"
14241         $TRUNCATE $DIR/$tdir/foo_160c 199
14242         changelog_dump | tail -n 5
14243         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
14244         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
14245 }
14246 run_test 160c "verify that changelog log catch the truncate event"
14247
14248 test_160d() {
14249         remote_mds_nodsh && skip "remote MDS with nodsh"
14250         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14251         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14252         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
14253                 skip "Need MDS version at least 2.7.60"
14254
14255         # Registration step
14256         changelog_register || error "changelog_register failed"
14257
14258         mkdir -p $DIR/$tdir/migrate_dir
14259         changelog_clear 0 || error "changelog_clear failed"
14260
14261         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
14262         changelog_dump | tail -n 5
14263         local migrates=$(changelog_dump | grep -c "MIGRT")
14264         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
14265 }
14266 run_test 160d "verify that changelog log catch the migrate event"
14267
14268 test_160e() {
14269         remote_mds_nodsh && skip "remote MDS with nodsh"
14270
14271         # Create a user
14272         changelog_register || error "changelog_register failed"
14273
14274         # Delete a future user (expect fail)
14275         local MDT0=$(facet_svc $SINGLEMDS)
14276         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
14277         local rc=$?
14278
14279         if [ $rc -eq 0 ]; then
14280                 error "Deleted non-existant user cl77"
14281         elif [ $rc -ne 2 ]; then
14282                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
14283         fi
14284
14285         # Clear to a bad index (1 billion should be safe)
14286         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
14287         rc=$?
14288
14289         if [ $rc -eq 0 ]; then
14290                 error "Successfully cleared to invalid CL index"
14291         elif [ $rc -ne 22 ]; then
14292                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
14293         fi
14294 }
14295 run_test 160e "changelog negative testing (should return errors)"
14296
14297 test_160f() {
14298         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14299         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14300                 skip "Need MDS version at least 2.10.56"
14301
14302         local mdts=$(comma_list $(mdts_nodes))
14303
14304         # Create a user
14305         changelog_register || error "first changelog_register failed"
14306         changelog_register || error "second changelog_register failed"
14307         local cl_users
14308         declare -A cl_user1
14309         declare -A cl_user2
14310         local user_rec1
14311         local user_rec2
14312         local i
14313
14314         # generate some changelog records to accumulate on each MDT
14315         # use fnv1a because created files should be evenly distributed
14316         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14317                 error "test_mkdir $tdir failed"
14318         log "$(date +%s): creating first files"
14319         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14320                 error "create $DIR/$tdir/$tfile failed"
14321
14322         # check changelogs have been generated
14323         local start=$SECONDS
14324         local idle_time=$((MDSCOUNT * 5 + 5))
14325         local nbcl=$(changelog_dump | wc -l)
14326         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14327
14328         for param in "changelog_max_idle_time=$idle_time" \
14329                      "changelog_gc=1" \
14330                      "changelog_min_gc_interval=2" \
14331                      "changelog_min_free_cat_entries=3"; do
14332                 local MDT0=$(facet_svc $SINGLEMDS)
14333                 local var="${param%=*}"
14334                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14335
14336                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14337                 do_nodes $mdts $LCTL set_param mdd.*.$param
14338         done
14339
14340         # force cl_user2 to be idle (1st part), but also cancel the
14341         # cl_user1 records so that it is not evicted later in the test.
14342         local sleep1=$((idle_time / 2))
14343         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
14344         sleep $sleep1
14345
14346         # simulate changelog catalog almost full
14347         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
14348         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
14349
14350         for i in $(seq $MDSCOUNT); do
14351                 cl_users=(${CL_USERS[mds$i]})
14352                 cl_user1[mds$i]="${cl_users[0]}"
14353                 cl_user2[mds$i]="${cl_users[1]}"
14354
14355                 [ -n "${cl_user1[mds$i]}" ] ||
14356                         error "mds$i: no user registered"
14357                 [ -n "${cl_user2[mds$i]}" ] ||
14358                         error "mds$i: only ${cl_user2[mds$i]} is registered"
14359
14360                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14361                 [ -n "$user_rec1" ] ||
14362                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14363                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14364                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14365                 [ -n "$user_rec2" ] ||
14366                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14367                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14368                      "$user_rec1 + 2 == $user_rec2"
14369                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14370                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14371                               "$user_rec1 + 2, but is $user_rec2"
14372                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14373                 [ -n "$user_rec2" ] ||
14374                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14375                 [ $user_rec1 == $user_rec2 ] ||
14376                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14377                               "$user_rec1, but is $user_rec2"
14378         done
14379
14380         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
14381         local sleep2=$((idle_time - (SECONDS - start) + 1))
14382         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
14383         sleep $sleep2
14384
14385         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
14386         # cl_user1 should be OK because it recently processed records.
14387         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
14388         createmany -m $DIR/$tdir/${tfile}b $((MDSCOUNT * 2)) ||
14389                 error "create $DIR/$tdir/${tfile}b failed"
14390
14391         # ensure gc thread is done
14392         for i in $(mdts_nodes); do
14393                 wait_update $i \
14394                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
14395                         error "$i: GC-thread not done"
14396         done
14397
14398         local first_rec
14399         for i in $(seq $MDSCOUNT); do
14400                 # check cl_user1 still registered
14401                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14402                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14403                 # check cl_user2 unregistered
14404                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14405                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14406
14407                 # check changelogs are present and starting at $user_rec1 + 1
14408                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14409                 [ -n "$user_rec1" ] ||
14410                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14411                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14412                             awk '{ print $1; exit; }')
14413
14414                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14415                 [ $((user_rec1 + 1)) == $first_rec ] ||
14416                         error "mds$i: first index should be $user_rec1 + 1, " \
14417                               "but is $first_rec"
14418         done
14419 }
14420 run_test 160f "changelog garbage collect (timestamped users)"
14421
14422 test_160g() {
14423         remote_mds_nodsh && skip "remote MDS with nodsh"
14424         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14425                 skip "Need MDS version at least 2.10.56"
14426
14427         local mdts=$(comma_list $(mdts_nodes))
14428
14429         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
14430         do_nodes $mdts $LCTL set_param fail_loc=0x1314
14431
14432         # Create a user
14433         changelog_register || error "first changelog_register failed"
14434         changelog_register || error "second changelog_register failed"
14435         local cl_users
14436         declare -A cl_user1
14437         declare -A cl_user2
14438         local user_rec1
14439         local user_rec2
14440         local i
14441
14442         # generate some changelog records to accumulate on each MDT
14443         # use fnv1a because created files should be evenly distributed
14444         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14445                 error "mkdir $tdir failed"
14446         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14447                 error "create $DIR/$tdir/$tfile failed"
14448
14449         # check changelogs have been generated
14450         local nbcl=$(changelog_dump | wc -l)
14451         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14452
14453         # reduce the max_idle_indexes value to make sure we exceed it
14454         max_ndx=$((nbcl / 2 - 1))
14455
14456         for param in "changelog_max_idle_indexes=$max_ndx" \
14457                      "changelog_gc=1" \
14458                      "changelog_min_gc_interval=2" \
14459                      "changelog_min_free_cat_entries=3"; do
14460                 local MDT0=$(facet_svc $SINGLEMDS)
14461                 local var="${param%=*}"
14462                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14463
14464                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14465                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
14466                         error "unable to set mdd.*.$param"
14467         done
14468
14469         # simulate changelog catalog almost full
14470         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
14471         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
14472
14473         for i in $(seq $MDSCOUNT); do
14474                 cl_users=(${CL_USERS[mds$i]})
14475                 cl_user1[mds$i]="${cl_users[0]}"
14476                 cl_user2[mds$i]="${cl_users[1]}"
14477
14478                 [ -n "${cl_user1[mds$i]}" ] ||
14479                         error "mds$i: no user registered"
14480                 [ -n "${cl_user2[mds$i]}" ] ||
14481                         error "mds$i: only ${cl_user1[mds$i]} is registered"
14482
14483                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14484                 [ -n "$user_rec1" ] ||
14485                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14486                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14487                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14488                 [ -n "$user_rec2" ] ||
14489                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14490                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14491                      "$user_rec1 + 2 == $user_rec2"
14492                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14493                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14494                               "$user_rec1 + 2, but is $user_rec2"
14495                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14496                 [ -n "$user_rec2" ] ||
14497                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14498                 [ $user_rec1 == $user_rec2 ] ||
14499                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14500                               "$user_rec1, but is $user_rec2"
14501         done
14502
14503         # ensure we are past the previous changelog_min_gc_interval set above
14504         sleep 2
14505
14506         # generate one more changelog to trigger fail_loc
14507         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14508                 error "create $DIR/$tdir/${tfile}bis failed"
14509
14510         # ensure gc thread is done
14511         for i in $(mdts_nodes); do
14512                 wait_update $i \
14513                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
14514                         error "$i: GC-thread not done"
14515         done
14516
14517         local first_rec
14518         for i in $(seq $MDSCOUNT); do
14519                 # check cl_user1 still registered
14520                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14521                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14522                 # check cl_user2 unregistered
14523                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14524                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14525
14526                 # check changelogs are present and starting at $user_rec1 + 1
14527                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14528                 [ -n "$user_rec1" ] ||
14529                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14530                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14531                             awk '{ print $1; exit; }')
14532
14533                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14534                 [ $((user_rec1 + 1)) == $first_rec ] ||
14535                         error "mds$i: first index should be $user_rec1 + 1, " \
14536                               "but is $first_rec"
14537         done
14538 }
14539 run_test 160g "changelog garbage collect (old users)"
14540
14541 test_160h() {
14542         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14543         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14544                 skip "Need MDS version at least 2.10.56"
14545
14546         local mdts=$(comma_list $(mdts_nodes))
14547
14548         # Create a user
14549         changelog_register || error "first changelog_register failed"
14550         changelog_register || error "second changelog_register failed"
14551         local cl_users
14552         declare -A cl_user1
14553         declare -A cl_user2
14554         local user_rec1
14555         local user_rec2
14556         local i
14557
14558         # generate some changelog records to accumulate on each MDT
14559         # use fnv1a because created files should be evenly distributed
14560         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14561                 error "test_mkdir $tdir failed"
14562         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14563                 error "create $DIR/$tdir/$tfile failed"
14564
14565         # check changelogs have been generated
14566         local nbcl=$(changelog_dump | wc -l)
14567         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14568
14569         for param in "changelog_max_idle_time=10" \
14570                      "changelog_gc=1" \
14571                      "changelog_min_gc_interval=2"; do
14572                 local MDT0=$(facet_svc $SINGLEMDS)
14573                 local var="${param%=*}"
14574                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14575
14576                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14577                 do_nodes $mdts $LCTL set_param mdd.*.$param
14578         done
14579
14580         # force cl_user2 to be idle (1st part)
14581         sleep 9
14582
14583         for i in $(seq $MDSCOUNT); do
14584                 cl_users=(${CL_USERS[mds$i]})
14585                 cl_user1[mds$i]="${cl_users[0]}"
14586                 cl_user2[mds$i]="${cl_users[1]}"
14587
14588                 [ -n "${cl_user1[mds$i]}" ] ||
14589                         error "mds$i: no user registered"
14590                 [ -n "${cl_user2[mds$i]}" ] ||
14591                         error "mds$i: only ${cl_user2[mds$i]} is registered"
14592
14593                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14594                 [ -n "$user_rec1" ] ||
14595                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14596                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14597                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14598                 [ -n "$user_rec2" ] ||
14599                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14600                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14601                      "$user_rec1 + 2 == $user_rec2"
14602                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14603                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14604                               "$user_rec1 + 2, but is $user_rec2"
14605                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14606                 [ -n "$user_rec2" ] ||
14607                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14608                 [ $user_rec1 == $user_rec2 ] ||
14609                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14610                               "$user_rec1, but is $user_rec2"
14611         done
14612
14613         # force cl_user2 to be idle (2nd part) and to reach
14614         # changelog_max_idle_time
14615         sleep 2
14616
14617         # force each GC-thread start and block then
14618         # one per MDT/MDD, set fail_val accordingly
14619         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
14620         do_nodes $mdts $LCTL set_param fail_loc=0x1316
14621
14622         # generate more changelogs to trigger fail_loc
14623         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14624                 error "create $DIR/$tdir/${tfile}bis failed"
14625
14626         # stop MDT to stop GC-thread, should be done in back-ground as it will
14627         # block waiting for the thread to be released and exit
14628         declare -A stop_pids
14629         for i in $(seq $MDSCOUNT); do
14630                 stop mds$i &
14631                 stop_pids[mds$i]=$!
14632         done
14633
14634         for i in $(mdts_nodes); do
14635                 local facet
14636                 local nb=0
14637                 local facets=$(facets_up_on_host $i)
14638
14639                 for facet in ${facets//,/ }; do
14640                         if [[ $facet == mds* ]]; then
14641                                 nb=$((nb + 1))
14642                         fi
14643                 done
14644                 # ensure each MDS's gc threads are still present and all in "R"
14645                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
14646                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
14647                         error "$i: expected $nb GC-thread"
14648                 wait_update $i \
14649                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
14650                         "R" 20 ||
14651                         error "$i: GC-thread not found in R-state"
14652                 # check umounts of each MDT on MDS have reached kthread_stop()
14653                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
14654                         error "$i: expected $nb umount"
14655                 wait_update $i \
14656                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
14657                         error "$i: umount not found in D-state"
14658         done
14659
14660         # release all GC-threads
14661         do_nodes $mdts $LCTL set_param fail_loc=0
14662
14663         # wait for MDT stop to complete
14664         for i in $(seq $MDSCOUNT); do
14665                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
14666         done
14667
14668         # XXX
14669         # may try to check if any orphan changelog records are present
14670         # via ldiskfs/zfs and llog_reader...
14671
14672         # re-start/mount MDTs
14673         for i in $(seq $MDSCOUNT); do
14674                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
14675                         error "Fail to start mds$i"
14676         done
14677
14678         local first_rec
14679         for i in $(seq $MDSCOUNT); do
14680                 # check cl_user1 still registered
14681                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14682                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14683                 # check cl_user2 unregistered
14684                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14685                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14686
14687                 # check changelogs are present and starting at $user_rec1 + 1
14688                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14689                 [ -n "$user_rec1" ] ||
14690                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14691                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14692                             awk '{ print $1; exit; }')
14693
14694                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14695                 [ $((user_rec1 + 1)) == $first_rec ] ||
14696                         error "mds$i: first index should be $user_rec1 + 1, " \
14697                               "but is $first_rec"
14698         done
14699 }
14700 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
14701               "during mount"
14702
14703 test_160i() {
14704
14705         local mdts=$(comma_list $(mdts_nodes))
14706
14707         changelog_register || error "first changelog_register failed"
14708
14709         # generate some changelog records to accumulate on each MDT
14710         # use fnv1a because created files should be evenly distributed
14711         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14712                 error "mkdir $tdir failed"
14713         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14714                 error "create $DIR/$tdir/$tfile failed"
14715
14716         # check changelogs have been generated
14717         local nbcl=$(changelog_dump | wc -l)
14718         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14719
14720         # simulate race between register and unregister
14721         # XXX as fail_loc is set per-MDS, with DNE configs the race
14722         # simulation will only occur for one MDT per MDS and for the
14723         # others the normal race scenario will take place
14724         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
14725         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
14726         do_nodes $mdts $LCTL set_param fail_val=1
14727
14728         # unregister 1st user
14729         changelog_deregister &
14730         local pid1=$!
14731         # wait some time for deregister work to reach race rdv
14732         sleep 2
14733         # register 2nd user
14734         changelog_register || error "2nd user register failed"
14735
14736         wait $pid1 || error "1st user deregister failed"
14737
14738         local i
14739         local last_rec
14740         declare -A LAST_REC
14741         for i in $(seq $MDSCOUNT); do
14742                 if changelog_users mds$i | grep "^cl"; then
14743                         # make sure new records are added with one user present
14744                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
14745                                           awk '/^current.index:/ { print $NF }')
14746                 else
14747                         error "mds$i has no user registered"
14748                 fi
14749         done
14750
14751         # generate more changelog records to accumulate on each MDT
14752         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14753                 error "create $DIR/$tdir/${tfile}bis failed"
14754
14755         for i in $(seq $MDSCOUNT); do
14756                 last_rec=$(changelog_users $SINGLEMDS |
14757                            awk '/^current.index:/ { print $NF }')
14758                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
14759                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
14760                         error "changelogs are off on mds$i"
14761         done
14762 }
14763 run_test 160i "changelog user register/unregister race"
14764
14765 test_160j() {
14766         remote_mds_nodsh && skip "remote MDS with nodsh"
14767         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
14768                 skip "Need MDS version at least 2.12.56"
14769
14770         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
14771         stack_trap "umount $MOUNT2" EXIT
14772
14773         changelog_register || error "first changelog_register failed"
14774         stack_trap "changelog_deregister" EXIT
14775
14776         # generate some changelog
14777         # use fnv1a because created files should be evenly distributed
14778         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14779                 error "mkdir $tdir failed"
14780         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14781                 error "create $DIR/$tdir/${tfile}bis failed"
14782
14783         # open the changelog device
14784         exec 3>/dev/changelog-$FSNAME-MDT0000
14785         stack_trap "exec 3>&-" EXIT
14786         exec 4</dev/changelog-$FSNAME-MDT0000
14787         stack_trap "exec 4<&-" EXIT
14788
14789         # umount the first lustre mount
14790         umount $MOUNT
14791         stack_trap "mount_client $MOUNT" EXIT
14792
14793         # read changelog
14794         cat <&4 >/dev/null || error "read changelog failed"
14795
14796         # clear changelog
14797         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14798         changelog_users $SINGLEMDS | grep -q $cl_user ||
14799                 error "User $cl_user not found in changelog_users"
14800
14801         printf 'clear:'$cl_user':0' >&3
14802 }
14803 run_test 160j "client can be umounted  while its chanangelog is being used"
14804
14805 test_160k() {
14806         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14807         remote_mds_nodsh && skip "remote MDS with nodsh"
14808
14809         mkdir -p $DIR/$tdir/1/1
14810
14811         changelog_register || error "changelog_register failed"
14812         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14813
14814         changelog_users $SINGLEMDS | grep -q $cl_user ||
14815                 error "User '$cl_user' not found in changelog_users"
14816 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
14817         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
14818         rmdir $DIR/$tdir/1/1 & sleep 1
14819         mkdir $DIR/$tdir/2
14820         touch $DIR/$tdir/2/2
14821         rm -rf $DIR/$tdir/2
14822
14823         wait
14824         sleep 4
14825
14826         changelog_dump | grep rmdir || error "rmdir not recorded"
14827
14828         rm -rf $DIR/$tdir
14829         changelog_deregister
14830 }
14831 run_test 160k "Verify that changelog records are not lost"
14832
14833 test_161a() {
14834         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14835
14836         test_mkdir -c1 $DIR/$tdir
14837         cp /etc/hosts $DIR/$tdir/$tfile
14838         test_mkdir -c1 $DIR/$tdir/foo1
14839         test_mkdir -c1 $DIR/$tdir/foo2
14840         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
14841         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
14842         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
14843         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
14844         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
14845         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14846                 $LFS fid2path $DIR $FID
14847                 error "bad link ea"
14848         fi
14849         # middle
14850         rm $DIR/$tdir/foo2/zachary
14851         # last
14852         rm $DIR/$tdir/foo2/thor
14853         # first
14854         rm $DIR/$tdir/$tfile
14855         # rename
14856         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
14857         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
14858                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
14859         rm $DIR/$tdir/foo2/maggie
14860
14861         # overflow the EA
14862         local longname=$tfile.avg_len_is_thirty_two_
14863         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
14864                 error_noexit 'failed to unlink many hardlinks'" EXIT
14865         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
14866                 error "failed to hardlink many files"
14867         links=$($LFS fid2path $DIR $FID | wc -l)
14868         echo -n "${links}/1000 links in link EA"
14869         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
14870 }
14871 run_test 161a "link ea sanity"
14872
14873 test_161b() {
14874         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14875         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
14876
14877         local MDTIDX=1
14878         local remote_dir=$DIR/$tdir/remote_dir
14879
14880         mkdir -p $DIR/$tdir
14881         $LFS mkdir -i $MDTIDX $remote_dir ||
14882                 error "create remote directory failed"
14883
14884         cp /etc/hosts $remote_dir/$tfile
14885         mkdir -p $remote_dir/foo1
14886         mkdir -p $remote_dir/foo2
14887         ln $remote_dir/$tfile $remote_dir/foo1/sofia
14888         ln $remote_dir/$tfile $remote_dir/foo2/zachary
14889         ln $remote_dir/$tfile $remote_dir/foo1/luna
14890         ln $remote_dir/$tfile $remote_dir/foo2/thor
14891
14892         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
14893                      tr -d ']')
14894         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14895                 $LFS fid2path $DIR $FID
14896                 error "bad link ea"
14897         fi
14898         # middle
14899         rm $remote_dir/foo2/zachary
14900         # last
14901         rm $remote_dir/foo2/thor
14902         # first
14903         rm $remote_dir/$tfile
14904         # rename
14905         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
14906         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
14907         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
14908                 $LFS fid2path $DIR $FID
14909                 error "bad link rename"
14910         fi
14911         rm $remote_dir/foo2/maggie
14912
14913         # overflow the EA
14914         local longname=filename_avg_len_is_thirty_two_
14915         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
14916                 error "failed to hardlink many files"
14917         links=$($LFS fid2path $DIR $FID | wc -l)
14918         echo -n "${links}/1000 links in link EA"
14919         [[ ${links} -gt 60 ]] ||
14920                 error "expected at least 60 links in link EA"
14921         unlinkmany $remote_dir/foo2/$longname 1000 ||
14922         error "failed to unlink many hardlinks"
14923 }
14924 run_test 161b "link ea sanity under remote directory"
14925
14926 test_161c() {
14927         remote_mds_nodsh && skip "remote MDS with nodsh"
14928         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14929         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
14930                 skip "Need MDS version at least 2.1.5"
14931
14932         # define CLF_RENAME_LAST 0x0001
14933         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
14934         changelog_register || error "changelog_register failed"
14935
14936         rm -rf $DIR/$tdir
14937         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
14938         touch $DIR/$tdir/foo_161c
14939         touch $DIR/$tdir/bar_161c
14940         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14941         changelog_dump | grep RENME | tail -n 5
14942         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14943         changelog_clear 0 || error "changelog_clear failed"
14944         if [ x$flags != "x0x1" ]; then
14945                 error "flag $flags is not 0x1"
14946         fi
14947
14948         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
14949         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
14950         touch $DIR/$tdir/foo_161c
14951         touch $DIR/$tdir/bar_161c
14952         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14953         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14954         changelog_dump | grep RENME | tail -n 5
14955         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14956         changelog_clear 0 || error "changelog_clear failed"
14957         if [ x$flags != "x0x0" ]; then
14958                 error "flag $flags is not 0x0"
14959         fi
14960         echo "rename overwrite a target having nlink > 1," \
14961                 "changelog record has flags of $flags"
14962
14963         # rename doesn't overwrite a target (changelog flag 0x0)
14964         touch $DIR/$tdir/foo_161c
14965         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
14966         changelog_dump | grep RENME | tail -n 5
14967         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
14968         changelog_clear 0 || error "changelog_clear failed"
14969         if [ x$flags != "x0x0" ]; then
14970                 error "flag $flags is not 0x0"
14971         fi
14972         echo "rename doesn't overwrite a target," \
14973                 "changelog record has flags of $flags"
14974
14975         # define CLF_UNLINK_LAST 0x0001
14976         # unlink a file having nlink = 1 (changelog flag 0x1)
14977         rm -f $DIR/$tdir/foo2_161c
14978         changelog_dump | grep UNLNK | tail -n 5
14979         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14980         changelog_clear 0 || error "changelog_clear failed"
14981         if [ x$flags != "x0x1" ]; then
14982                 error "flag $flags is not 0x1"
14983         fi
14984         echo "unlink a file having nlink = 1," \
14985                 "changelog record has flags of $flags"
14986
14987         # unlink a file having nlink > 1 (changelog flag 0x0)
14988         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14989         rm -f $DIR/$tdir/foobar_161c
14990         changelog_dump | grep UNLNK | tail -n 5
14991         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14992         changelog_clear 0 || error "changelog_clear failed"
14993         if [ x$flags != "x0x0" ]; then
14994                 error "flag $flags is not 0x0"
14995         fi
14996         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
14997 }
14998 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
14999
15000 test_161d() {
15001         remote_mds_nodsh && skip "remote MDS with nodsh"
15002         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
15003
15004         local pid
15005         local fid
15006
15007         changelog_register || error "changelog_register failed"
15008
15009         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
15010         # interfer with $MOUNT/.lustre/fid/ access
15011         mkdir $DIR/$tdir
15012         [[ $? -eq 0 ]] || error "mkdir failed"
15013
15014         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
15015         $LCTL set_param fail_loc=0x8000140c
15016         # 5s pause
15017         $LCTL set_param fail_val=5
15018
15019         # create file
15020         echo foofoo > $DIR/$tdir/$tfile &
15021         pid=$!
15022
15023         # wait for create to be delayed
15024         sleep 2
15025
15026         ps -p $pid
15027         [[ $? -eq 0 ]] || error "create should be blocked"
15028
15029         local tempfile="$(mktemp --tmpdir $tfile.XXXXXX)"
15030         stack_trap "rm -f $tempfile"
15031         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
15032         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
15033         # some delay may occur during ChangeLog publishing and file read just
15034         # above, that could allow file write to happen finally
15035         [[ -s $tempfile ]] && echo "file should be empty"
15036
15037         $LCTL set_param fail_loc=0
15038
15039         wait $pid
15040         [[ $? -eq 0 ]] || error "create failed"
15041 }
15042 run_test 161d "create with concurrent .lustre/fid access"
15043
15044 check_path() {
15045         local expected="$1"
15046         shift
15047         local fid="$2"
15048
15049         local path
15050         path=$($LFS fid2path "$@")
15051         local rc=$?
15052
15053         if [ $rc -ne 0 ]; then
15054                 error "path looked up of '$expected' failed: rc=$rc"
15055         elif [ "$path" != "$expected" ]; then
15056                 error "path looked up '$path' instead of '$expected'"
15057         else
15058                 echo "FID '$fid' resolves to path '$path' as expected"
15059         fi
15060 }
15061
15062 test_162a() { # was test_162
15063         test_mkdir -p -c1 $DIR/$tdir/d2
15064         touch $DIR/$tdir/d2/$tfile
15065         touch $DIR/$tdir/d2/x1
15066         touch $DIR/$tdir/d2/x2
15067         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
15068         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
15069         # regular file
15070         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
15071         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
15072
15073         # softlink
15074         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
15075         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
15076         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
15077
15078         # softlink to wrong file
15079         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
15080         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
15081         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
15082
15083         # hardlink
15084         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
15085         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
15086         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
15087         # fid2path dir/fsname should both work
15088         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
15089         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
15090
15091         # hardlink count: check that there are 2 links
15092         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
15093         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
15094
15095         # hardlink indexing: remove the first link
15096         rm $DIR/$tdir/d2/p/q/r/hlink
15097         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
15098 }
15099 run_test 162a "path lookup sanity"
15100
15101 test_162b() {
15102         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15103         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15104
15105         mkdir $DIR/$tdir
15106         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
15107                                 error "create striped dir failed"
15108
15109         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
15110                                         tail -n 1 | awk '{print $2}')
15111         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
15112
15113         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
15114         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
15115
15116         # regular file
15117         for ((i=0;i<5;i++)); do
15118                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
15119                         error "get fid for f$i failed"
15120                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
15121
15122                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
15123                         error "get fid for d$i failed"
15124                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
15125         done
15126
15127         return 0
15128 }
15129 run_test 162b "striped directory path lookup sanity"
15130
15131 # LU-4239: Verify fid2path works with paths 100 or more directories deep
15132 test_162c() {
15133         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
15134                 skip "Need MDS version at least 2.7.51"
15135
15136         local lpath=$tdir.local
15137         local rpath=$tdir.remote
15138
15139         test_mkdir $DIR/$lpath
15140         test_mkdir $DIR/$rpath
15141
15142         for ((i = 0; i <= 101; i++)); do
15143                 lpath="$lpath/$i"
15144                 mkdir $DIR/$lpath
15145                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
15146                         error "get fid for local directory $DIR/$lpath failed"
15147                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
15148
15149                 rpath="$rpath/$i"
15150                 test_mkdir $DIR/$rpath
15151                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
15152                         error "get fid for remote directory $DIR/$rpath failed"
15153                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
15154         done
15155
15156         return 0
15157 }
15158 run_test 162c "fid2path works with paths 100 or more directories deep"
15159
15160 oalr_event_count() {
15161         local event="${1}"
15162         local trace="${2}"
15163
15164         awk -v name="${FSNAME}-OST0000" \
15165             -v event="${event}" \
15166             '$1 == "TRACE" && $2 == event && $3 == name' \
15167             "${trace}" |
15168         wc -l
15169 }
15170
15171 oalr_expect_event_count() {
15172         local event="${1}"
15173         local trace="${2}"
15174         local expect="${3}"
15175         local count
15176
15177         count=$(oalr_event_count "${event}" "${trace}")
15178         if ((count == expect)); then
15179                 return 0
15180         fi
15181
15182         error_noexit "${event} event count was '${count}', expected ${expect}"
15183         cat "${trace}" >&2
15184         exit 1
15185 }
15186
15187 cleanup_165() {
15188         do_facet ost1 killall --quiet -KILL ofd_access_log_reader || true
15189         stop ost1
15190         start ost1 "$(ostdevname 1)" $OST_MOUNT_OPTS
15191 }
15192
15193 setup_165() {
15194         sync # Flush previous IOs so we can count log entries.
15195         do_facet ost1 $LCTL set_param "obdfilter.${FSNAME}-OST0000.access_log_size=4096"
15196         stack_trap cleanup_165 EXIT
15197 }
15198
15199 test_165a() {
15200         local trace="/tmp/${tfile}.trace"
15201         local rc
15202         local count
15203
15204         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
15205         setup_165
15206         sleep 5
15207
15208         do_facet ost1 ofd_access_log_reader --list
15209         stop ost1
15210
15211         do_facet ost1 killall -TERM ofd_access_log_reader
15212         wait
15213         rc=$?
15214
15215         if ((rc != 0)); then
15216                 error "ofd_access_log_reader exited with rc = '${rc}'"
15217         fi
15218
15219         # Parse trace file for discovery events:
15220         oalr_expect_event_count alr_log_add "${trace}" 1
15221         oalr_expect_event_count alr_log_eof "${trace}" 1
15222         oalr_expect_event_count alr_log_free "${trace}" 1
15223 }
15224 run_test 165a "ofd access log discovery"
15225
15226 test_165b() {
15227         local trace="/tmp/${tfile}.trace"
15228         local file="${DIR}/${tfile}"
15229         local pfid1
15230         local pfid2
15231         local -a entry
15232         local rc
15233         local count
15234         local size
15235         local flags
15236
15237         setup_165
15238
15239         lfs setstripe -c 1 -i 0 "${file}"
15240         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c || error "cannot create '${file}'"
15241         do_facet ost1 ofd_access_log_reader --list
15242
15243         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
15244         sleep 5
15245         do_facet ost1 killall -TERM ofd_access_log_reader
15246         wait
15247         rc=$?
15248
15249         if ((rc != 0)); then
15250                 error "ofd_access_log_reader exited with rc = '${rc}'"
15251         fi
15252
15253         oalr_expect_event_count alr_log_entry "${trace}" 1
15254
15255         pfid1=$($LFS path2fid "${file}")
15256
15257         # 1     2             3   4    5     6   7    8    9     10
15258         # TRACE alr_log_entry OST PFID BEGIN END TIME SIZE COUNT FLAGS
15259         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
15260
15261         echo "entry = '${entry[*]}'" >&2
15262
15263         pfid2=${entry[4]}
15264         if [[ "${pfid1}" != "${pfid2}" ]]; then
15265                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
15266         fi
15267
15268         size=${entry[8]}
15269         if ((size != 1048576)); then
15270                 error "entry '${entry[*]}' has invalid io size '${size}', expected 1048576"
15271         fi
15272
15273         flags=${entry[10]}
15274         if [[ "${flags}" != "w" ]]; then
15275                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'w'"
15276         fi
15277
15278         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
15279         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r524288c || error "cannot read '${file}'"
15280         sleep 5
15281         do_facet ost1 killall -TERM ofd_access_log_reader
15282         wait
15283         rc=$?
15284
15285         if ((rc != 0)); then
15286                 error "ofd_access_log_reader exited with rc = '${rc}'"
15287         fi
15288
15289         oalr_expect_event_count alr_log_entry "${trace}" 1
15290
15291         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
15292         echo "entry = '${entry[*]}'" >&2
15293
15294         pfid2=${entry[4]}
15295         if [[ "${pfid1}" != "${pfid2}" ]]; then
15296                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
15297         fi
15298
15299         size=${entry[8]}
15300         if ((size != 524288)); then
15301                 error "entry '${entry[*]}' has invalid io size '${size}', 524288"
15302         fi
15303
15304         flags=${entry[10]}
15305         if [[ "${flags}" != "r" ]]; then
15306                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'r'"
15307         fi
15308 }
15309 run_test 165b "ofd access log entries are produced and consumed"
15310
15311 test_165c() {
15312         local file="${DIR}/${tdir}/${tfile}"
15313         test_mkdir "${DIR}/${tdir}"
15314
15315         setup_165
15316
15317         lfs setstripe -c 1 -i 0 "${DIR}/${tdir}"
15318
15319         # 4096 / 64 = 64. Create twice as many entries.
15320         for ((i = 0; i < 128; i++)); do
15321                 $MULTIOP "${file}-${i}" oO_CREAT:O_WRONLY:w512c || error "cannot create file"
15322         done
15323
15324         sync
15325         do_facet ost1 ofd_access_log_reader --list
15326         unlinkmany  "${file}-%d" 128
15327 }
15328 run_test 165c "full ofd access logs do not block IOs"
15329
15330 oal_peek_entry_count() {
15331         do_facet ost1 ofd_access_log_reader --list | awk '$1 == "_entry_count:" { print $2; }'
15332 }
15333
15334 oal_expect_entry_count() {
15335         local entry_count=$(oal_peek_entry_count)
15336         local expect="$1"
15337
15338         if ((entry_count == expect)); then
15339                 return 0
15340         fi
15341
15342         error_noexit "bad entry count, got ${entry_count}, expected ${expect}"
15343         do_facet ost1 ofd_access_log_reader --list >&2
15344         exit 1
15345 }
15346
15347 test_165d() {
15348         local trace="/tmp/${tfile}.trace"
15349         local file="${DIR}/${tdir}/${tfile}"
15350         local param="obdfilter.${FSNAME}-OST0000.access_log_mask"
15351         local entry_count
15352         test_mkdir "${DIR}/${tdir}"
15353
15354         setup_165
15355         lfs setstripe -c 1 -i 0 "${file}"
15356
15357         do_facet ost1 lctl set_param "${param}=rw"
15358         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c || error "cannot create '${file}'"
15359         oal_expect_entry_count 1
15360
15361         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c || error "cannot read '${file}'"
15362         oal_expect_entry_count 2
15363
15364         do_facet ost1 lctl set_param "${param}=r"
15365         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c || error "cannot create '${file}'"
15366         oal_expect_entry_count 2
15367
15368         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c || error "cannot read '${file}'"
15369         oal_expect_entry_count 3
15370
15371         do_facet ost1 lctl set_param "${param}=w"
15372         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c || error "cannot create '${file}'"
15373         oal_expect_entry_count 4
15374
15375         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c || error "cannot read '${file}'"
15376         oal_expect_entry_count 4
15377
15378         do_facet ost1 lctl set_param "${param}=0"
15379         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c || error "cannot create '${file}'"
15380         oal_expect_entry_count 4
15381
15382         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c || error "cannot read '${file}'"
15383         oal_expect_entry_count 4
15384 }
15385 run_test 165d "ofd_access_log mask works"
15386
15387 test_169() {
15388         # do directio so as not to populate the page cache
15389         log "creating a 10 Mb file"
15390         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
15391         log "starting reads"
15392         dd if=$DIR/$tfile of=/dev/null bs=4096 &
15393         log "truncating the file"
15394         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
15395         log "killing dd"
15396         kill %+ || true # reads might have finished
15397         echo "wait until dd is finished"
15398         wait
15399         log "removing the temporary file"
15400         rm -rf $DIR/$tfile || error "tmp file removal failed"
15401 }
15402 run_test 169 "parallel read and truncate should not deadlock"
15403
15404 test_170() {
15405         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15406
15407         $LCTL clear     # bug 18514
15408         $LCTL debug_daemon start $TMP/${tfile}_log_good
15409         touch $DIR/$tfile
15410         $LCTL debug_daemon stop
15411         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
15412                 error "sed failed to read log_good"
15413
15414         $LCTL debug_daemon start $TMP/${tfile}_log_good
15415         rm -rf $DIR/$tfile
15416         $LCTL debug_daemon stop
15417
15418         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
15419                error "lctl df log_bad failed"
15420
15421         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
15422         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
15423
15424         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
15425         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
15426
15427         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
15428                 error "bad_line good_line1 good_line2 are empty"
15429
15430         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
15431         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
15432         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
15433
15434         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
15435         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
15436         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
15437
15438         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
15439                 error "bad_line_new good_line_new are empty"
15440
15441         local expected_good=$((good_line1 + good_line2*2))
15442
15443         rm -f $TMP/${tfile}*
15444         # LU-231, short malformed line may not be counted into bad lines
15445         if [ $bad_line -ne $bad_line_new ] &&
15446                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
15447                 error "expected $bad_line bad lines, but got $bad_line_new"
15448                 return 1
15449         fi
15450
15451         if [ $expected_good -ne $good_line_new ]; then
15452                 error "expected $expected_good good lines, but got $good_line_new"
15453                 return 2
15454         fi
15455         true
15456 }
15457 run_test 170 "test lctl df to handle corrupted log ====================="
15458
15459 test_171() { # bug20592
15460         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15461
15462         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
15463         $LCTL set_param fail_loc=0x50e
15464         $LCTL set_param fail_val=3000
15465         multiop_bg_pause $DIR/$tfile O_s || true
15466         local MULTIPID=$!
15467         kill -USR1 $MULTIPID
15468         # cause log dump
15469         sleep 3
15470         wait $MULTIPID
15471         if dmesg | grep "recursive fault"; then
15472                 error "caught a recursive fault"
15473         fi
15474         $LCTL set_param fail_loc=0
15475         true
15476 }
15477 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
15478
15479 # it would be good to share it with obdfilter-survey/iokit-libecho code
15480 setup_obdecho_osc () {
15481         local rc=0
15482         local ost_nid=$1
15483         local obdfilter_name=$2
15484         echo "Creating new osc for $obdfilter_name on $ost_nid"
15485         # make sure we can find loopback nid
15486         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
15487
15488         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
15489                            ${obdfilter_name}_osc_UUID || rc=2; }
15490         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
15491                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
15492         return $rc
15493 }
15494
15495 cleanup_obdecho_osc () {
15496         local obdfilter_name=$1
15497         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
15498         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
15499         return 0
15500 }
15501
15502 obdecho_test() {
15503         local OBD=$1
15504         local node=$2
15505         local pages=${3:-64}
15506         local rc=0
15507         local id
15508
15509         local count=10
15510         local obd_size=$(get_obd_size $node $OBD)
15511         local page_size=$(get_page_size $node)
15512         if [[ -n "$obd_size" ]]; then
15513                 local new_count=$((obd_size / (pages * page_size / 1024)))
15514                 [[ $new_count -ge $count ]] || count=$new_count
15515         fi
15516
15517         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
15518         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
15519                            rc=2; }
15520         if [ $rc -eq 0 ]; then
15521             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
15522             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
15523         fi
15524         echo "New object id is $id"
15525         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
15526                            rc=4; }
15527         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
15528                            "test_brw $count w v $pages $id" || rc=4; }
15529         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
15530                            rc=4; }
15531         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
15532                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
15533         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
15534                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
15535         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
15536         return $rc
15537 }
15538
15539 test_180a() {
15540         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15541
15542         if ! module_loaded obdecho; then
15543                 load_module obdecho/obdecho &&
15544                         stack_trap "rmmod obdecho" EXIT ||
15545                         error "unable to load obdecho on client"
15546         fi
15547
15548         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
15549         local host=$($LCTL get_param -n osc.$osc.import |
15550                      awk '/current_connection:/ { print $2 }' )
15551         local target=$($LCTL get_param -n osc.$osc.import |
15552                        awk '/target:/ { print $2 }' )
15553         target=${target%_UUID}
15554
15555         if [ -n "$target" ]; then
15556                 setup_obdecho_osc $host $target &&
15557                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
15558                         { error "obdecho setup failed with $?"; return; }
15559
15560                 obdecho_test ${target}_osc client ||
15561                         error "obdecho_test failed on ${target}_osc"
15562         else
15563                 $LCTL get_param osc.$osc.import
15564                 error "there is no osc.$osc.import target"
15565         fi
15566 }
15567 run_test 180a "test obdecho on osc"
15568
15569 test_180b() {
15570         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15571         remote_ost_nodsh && skip "remote OST with nodsh"
15572
15573         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
15574                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
15575                 error "failed to load module obdecho"
15576
15577         local target=$(do_facet ost1 $LCTL dl |
15578                        awk '/obdfilter/ { print $4; exit; }')
15579
15580         if [ -n "$target" ]; then
15581                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
15582         else
15583                 do_facet ost1 $LCTL dl
15584                 error "there is no obdfilter target on ost1"
15585         fi
15586 }
15587 run_test 180b "test obdecho directly on obdfilter"
15588
15589 test_180c() { # LU-2598
15590         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15591         remote_ost_nodsh && skip "remote OST with nodsh"
15592         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
15593                 skip "Need MDS version at least 2.4.0"
15594
15595         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
15596                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
15597                 error "failed to load module obdecho"
15598
15599         local target=$(do_facet ost1 $LCTL dl |
15600                        awk '/obdfilter/ { print $4; exit; }')
15601
15602         if [ -n "$target" ]; then
15603                 local pages=16384 # 64MB bulk I/O RPC size
15604
15605                 obdecho_test "$target" ost1 "$pages" ||
15606                         error "obdecho_test with pages=$pages failed with $?"
15607         else
15608                 do_facet ost1 $LCTL dl
15609                 error "there is no obdfilter target on ost1"
15610         fi
15611 }
15612 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
15613
15614 test_181() { # bug 22177
15615         test_mkdir $DIR/$tdir
15616         # create enough files to index the directory
15617         createmany -o $DIR/$tdir/foobar 4000
15618         # print attributes for debug purpose
15619         lsattr -d .
15620         # open dir
15621         multiop_bg_pause $DIR/$tdir D_Sc || return 1
15622         MULTIPID=$!
15623         # remove the files & current working dir
15624         unlinkmany $DIR/$tdir/foobar 4000
15625         rmdir $DIR/$tdir
15626         kill -USR1 $MULTIPID
15627         wait $MULTIPID
15628         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
15629         return 0
15630 }
15631 run_test 181 "Test open-unlinked dir ========================"
15632
15633 test_182() {
15634         local fcount=1000
15635         local tcount=10
15636
15637         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15638
15639         $LCTL set_param mdc.*.rpc_stats=clear
15640
15641         for (( i = 0; i < $tcount; i++ )) ; do
15642                 mkdir $DIR/$tdir/$i
15643         done
15644
15645         for (( i = 0; i < $tcount; i++ )) ; do
15646                 createmany -o $DIR/$tdir/$i/f- $fcount &
15647         done
15648         wait
15649
15650         for (( i = 0; i < $tcount; i++ )) ; do
15651                 unlinkmany $DIR/$tdir/$i/f- $fcount &
15652         done
15653         wait
15654
15655         $LCTL get_param mdc.*.rpc_stats
15656
15657         rm -rf $DIR/$tdir
15658 }
15659 run_test 182 "Test parallel modify metadata operations ================"
15660
15661 test_183() { # LU-2275
15662         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15663         remote_mds_nodsh && skip "remote MDS with nodsh"
15664         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
15665                 skip "Need MDS version at least 2.3.56"
15666
15667         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15668         echo aaa > $DIR/$tdir/$tfile
15669
15670 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
15671         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
15672
15673         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
15674         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
15675
15676         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
15677
15678         # Flush negative dentry cache
15679         touch $DIR/$tdir/$tfile
15680
15681         # We are not checking for any leaked references here, they'll
15682         # become evident next time we do cleanup with module unload.
15683         rm -rf $DIR/$tdir
15684 }
15685 run_test 183 "No crash or request leak in case of strange dispositions ========"
15686
15687 # test suite 184 is for LU-2016, LU-2017
15688 test_184a() {
15689         check_swap_layouts_support
15690
15691         dir0=$DIR/$tdir/$testnum
15692         test_mkdir -p -c1 $dir0
15693         ref1=/etc/passwd
15694         ref2=/etc/group
15695         file1=$dir0/f1
15696         file2=$dir0/f2
15697         $LFS setstripe -c1 $file1
15698         cp $ref1 $file1
15699         $LFS setstripe -c2 $file2
15700         cp $ref2 $file2
15701         gen1=$($LFS getstripe -g $file1)
15702         gen2=$($LFS getstripe -g $file2)
15703
15704         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
15705         gen=$($LFS getstripe -g $file1)
15706         [[ $gen1 != $gen ]] ||
15707                 "Layout generation on $file1 does not change"
15708         gen=$($LFS getstripe -g $file2)
15709         [[ $gen2 != $gen ]] ||
15710                 "Layout generation on $file2 does not change"
15711
15712         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
15713         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
15714
15715         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
15716 }
15717 run_test 184a "Basic layout swap"
15718
15719 test_184b() {
15720         check_swap_layouts_support
15721
15722         dir0=$DIR/$tdir/$testnum
15723         mkdir -p $dir0 || error "creating dir $dir0"
15724         file1=$dir0/f1
15725         file2=$dir0/f2
15726         file3=$dir0/f3
15727         dir1=$dir0/d1
15728         dir2=$dir0/d2
15729         mkdir $dir1 $dir2
15730         $LFS setstripe -c1 $file1
15731         $LFS setstripe -c2 $file2
15732         $LFS setstripe -c1 $file3
15733         chown $RUNAS_ID $file3
15734         gen1=$($LFS getstripe -g $file1)
15735         gen2=$($LFS getstripe -g $file2)
15736
15737         $LFS swap_layouts $dir1 $dir2 &&
15738                 error "swap of directories layouts should fail"
15739         $LFS swap_layouts $dir1 $file1 &&
15740                 error "swap of directory and file layouts should fail"
15741         $RUNAS $LFS swap_layouts $file1 $file2 &&
15742                 error "swap of file we cannot write should fail"
15743         $LFS swap_layouts $file1 $file3 &&
15744                 error "swap of file with different owner should fail"
15745         /bin/true # to clear error code
15746 }
15747 run_test 184b "Forbidden layout swap (will generate errors)"
15748
15749 test_184c() {
15750         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
15751         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
15752         check_swap_layouts_support
15753
15754         local dir0=$DIR/$tdir/$testnum
15755         mkdir -p $dir0 || error "creating dir $dir0"
15756
15757         local ref1=$dir0/ref1
15758         local ref2=$dir0/ref2
15759         local file1=$dir0/file1
15760         local file2=$dir0/file2
15761         # create a file large enough for the concurrent test
15762         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
15763         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
15764         echo "ref file size: ref1($(stat -c %s $ref1))," \
15765              "ref2($(stat -c %s $ref2))"
15766
15767         cp $ref2 $file2
15768         dd if=$ref1 of=$file1 bs=16k &
15769         local DD_PID=$!
15770
15771         # Make sure dd starts to copy file
15772         while [ ! -f $file1 ]; do sleep 0.1; done
15773
15774         $LFS swap_layouts $file1 $file2
15775         local rc=$?
15776         wait $DD_PID
15777         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
15778         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
15779
15780         # how many bytes copied before swapping layout
15781         local copied=$(stat -c %s $file2)
15782         local remaining=$(stat -c %s $ref1)
15783         remaining=$((remaining - copied))
15784         echo "Copied $copied bytes before swapping layout..."
15785
15786         cmp -n $copied $file1 $ref2 | grep differ &&
15787                 error "Content mismatch [0, $copied) of ref2 and file1"
15788         cmp -n $copied $file2 $ref1 ||
15789                 error "Content mismatch [0, $copied) of ref1 and file2"
15790         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
15791                 error "Content mismatch [$copied, EOF) of ref1 and file1"
15792
15793         # clean up
15794         rm -f $ref1 $ref2 $file1 $file2
15795 }
15796 run_test 184c "Concurrent write and layout swap"
15797
15798 test_184d() {
15799         check_swap_layouts_support
15800         [ -z "$(which getfattr 2>/dev/null)" ] &&
15801                 skip_env "no getfattr command"
15802
15803         local file1=$DIR/$tdir/$tfile-1
15804         local file2=$DIR/$tdir/$tfile-2
15805         local file3=$DIR/$tdir/$tfile-3
15806         local lovea1
15807         local lovea2
15808
15809         mkdir -p $DIR/$tdir
15810         touch $file1 || error "create $file1 failed"
15811         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15812                 error "create $file2 failed"
15813         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15814                 error "create $file3 failed"
15815         lovea1=$(get_layout_param $file1)
15816
15817         $LFS swap_layouts $file2 $file3 ||
15818                 error "swap $file2 $file3 layouts failed"
15819         $LFS swap_layouts $file1 $file2 ||
15820                 error "swap $file1 $file2 layouts failed"
15821
15822         lovea2=$(get_layout_param $file2)
15823         echo "$lovea1"
15824         echo "$lovea2"
15825         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
15826
15827         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15828         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
15829 }
15830 run_test 184d "allow stripeless layouts swap"
15831
15832 test_184e() {
15833         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
15834                 skip "Need MDS version at least 2.6.94"
15835         check_swap_layouts_support
15836         [ -z "$(which getfattr 2>/dev/null)" ] &&
15837                 skip_env "no getfattr command"
15838
15839         local file1=$DIR/$tdir/$tfile-1
15840         local file2=$DIR/$tdir/$tfile-2
15841         local file3=$DIR/$tdir/$tfile-3
15842         local lovea
15843
15844         mkdir -p $DIR/$tdir
15845         touch $file1 || error "create $file1 failed"
15846         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15847                 error "create $file2 failed"
15848         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15849                 error "create $file3 failed"
15850
15851         $LFS swap_layouts $file1 $file2 ||
15852                 error "swap $file1 $file2 layouts failed"
15853
15854         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15855         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
15856
15857         echo 123 > $file1 || error "Should be able to write into $file1"
15858
15859         $LFS swap_layouts $file1 $file3 ||
15860                 error "swap $file1 $file3 layouts failed"
15861
15862         echo 123 > $file1 || error "Should be able to write into $file1"
15863
15864         rm -rf $file1 $file2 $file3
15865 }
15866 run_test 184e "Recreate layout after stripeless layout swaps"
15867
15868 test_184f() {
15869         # Create a file with name longer than sizeof(struct stat) ==
15870         # 144 to see if we can get chars from the file name to appear
15871         # in the returned striping. Note that 'f' == 0x66.
15872         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
15873
15874         mkdir -p $DIR/$tdir
15875         mcreate $DIR/$tdir/$file
15876         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
15877                 error "IOC_MDC_GETFILEINFO returned garbage striping"
15878         fi
15879 }
15880 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
15881
15882 test_185() { # LU-2441
15883         # LU-3553 - no volatile file support in old servers
15884         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
15885                 skip "Need MDS version at least 2.3.60"
15886
15887         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15888         touch $DIR/$tdir/spoo
15889         local mtime1=$(stat -c "%Y" $DIR/$tdir)
15890         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
15891                 error "cannot create/write a volatile file"
15892         [ "$FILESET" == "" ] &&
15893         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
15894                 error "FID is still valid after close"
15895
15896         multiop_bg_pause $DIR/$tdir vVw4096_c
15897         local multi_pid=$!
15898
15899         local OLD_IFS=$IFS
15900         IFS=":"
15901         local fidv=($fid)
15902         IFS=$OLD_IFS
15903         # assume that the next FID for this client is sequential, since stdout
15904         # is unfortunately eaten by multiop_bg_pause
15905         local n=$((${fidv[1]} + 1))
15906         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
15907         if [ "$FILESET" == "" ]; then
15908                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
15909                         error "FID is missing before close"
15910         fi
15911         kill -USR1 $multi_pid
15912         # 1 second delay, so if mtime change we will see it
15913         sleep 1
15914         local mtime2=$(stat -c "%Y" $DIR/$tdir)
15915         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
15916 }
15917 run_test 185 "Volatile file support"
15918
15919 function create_check_volatile() {
15920         local idx=$1
15921         local tgt
15922
15923         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
15924         local PID=$!
15925         sleep 1
15926         local FID=$(cat /tmp/${tfile}.fid)
15927         [ "$FID" == "" ] && error "can't get FID for volatile"
15928         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
15929         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
15930         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
15931         kill -USR1 $PID
15932         wait
15933         sleep 1
15934         cancel_lru_locks mdc # flush opencache
15935         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
15936         return 0
15937 }
15938
15939 test_185a(){
15940         # LU-12516 - volatile creation via .lustre
15941         [[ $MDS1_VERSION -ge $(version_code 2.12.55) ]] ||
15942                 skip "Need MDS version at least 2.3.55"
15943
15944         create_check_volatile 0
15945         [ $MDSCOUNT -lt 2 ] && return 0
15946
15947         # DNE case
15948         create_check_volatile 1
15949
15950         return 0
15951 }
15952 run_test 185a "Volatile file creation in .lustre/fid/"
15953
15954 test_187a() {
15955         remote_mds_nodsh && skip "remote MDS with nodsh"
15956         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15957                 skip "Need MDS version at least 2.3.0"
15958
15959         local dir0=$DIR/$tdir/$testnum
15960         mkdir -p $dir0 || error "creating dir $dir0"
15961
15962         local file=$dir0/file1
15963         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
15964         local dv1=$($LFS data_version $file)
15965         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
15966         local dv2=$($LFS data_version $file)
15967         [[ $dv1 != $dv2 ]] ||
15968                 error "data version did not change on write $dv1 == $dv2"
15969
15970         # clean up
15971         rm -f $file1
15972 }
15973 run_test 187a "Test data version change"
15974
15975 test_187b() {
15976         remote_mds_nodsh && skip "remote MDS with nodsh"
15977         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15978                 skip "Need MDS version at least 2.3.0"
15979
15980         local dir0=$DIR/$tdir/$testnum
15981         mkdir -p $dir0 || error "creating dir $dir0"
15982
15983         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
15984         [[ ${DV[0]} != ${DV[1]} ]] ||
15985                 error "data version did not change on write"\
15986                       " ${DV[0]} == ${DV[1]}"
15987
15988         # clean up
15989         rm -f $file1
15990 }
15991 run_test 187b "Test data version change on volatile file"
15992
15993 test_200() {
15994         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15995         remote_mgs_nodsh && skip "remote MGS with nodsh"
15996         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15997
15998         local POOL=${POOL:-cea1}
15999         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
16000         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
16001         # Pool OST targets
16002         local first_ost=0
16003         local last_ost=$(($OSTCOUNT - 1))
16004         local ost_step=2
16005         local ost_list=$(seq $first_ost $ost_step $last_ost)
16006         local ost_range="$first_ost $last_ost $ost_step"
16007         local test_path=$POOL_ROOT/$POOL_DIR_NAME
16008         local file_dir=$POOL_ROOT/file_tst
16009         local subdir=$test_path/subdir
16010         local rc=0
16011
16012         while : ; do
16013                 # former test_200a test_200b
16014                 pool_add $POOL                          || { rc=$? ; break; }
16015                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
16016                 # former test_200c test_200d
16017                 mkdir -p $test_path
16018                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
16019                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
16020                 mkdir -p $subdir
16021                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
16022                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
16023                                                         || { rc=$? ; break; }
16024                 # former test_200e test_200f
16025                 local files=$((OSTCOUNT*3))
16026                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
16027                                                         || { rc=$? ; break; }
16028                 pool_create_files $POOL $file_dir $files "$ost_list" \
16029                                                         || { rc=$? ; break; }
16030                 # former test_200g test_200h
16031                 pool_lfs_df $POOL                       || { rc=$? ; break; }
16032                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
16033
16034                 # former test_201a test_201b test_201c
16035                 pool_remove_first_target $POOL          || { rc=$? ; break; }
16036
16037                 local f=$test_path/$tfile
16038                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
16039                 pool_remove $POOL $f                    || { rc=$? ; break; }
16040                 break
16041         done
16042
16043         destroy_test_pools
16044
16045         return $rc
16046 }
16047 run_test 200 "OST pools"
16048
16049 # usage: default_attr <count | size | offset>
16050 default_attr() {
16051         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
16052 }
16053
16054 # usage: check_default_stripe_attr
16055 check_default_stripe_attr() {
16056         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
16057         case $1 in
16058         --stripe-count|-c)
16059                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
16060         --stripe-size|-S)
16061                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
16062         --stripe-index|-i)
16063                 EXPECTED=-1;;
16064         *)
16065                 error "unknown getstripe attr '$1'"
16066         esac
16067
16068         [ $ACTUAL == $EXPECTED ] ||
16069                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
16070 }
16071
16072 test_204a() {
16073         test_mkdir $DIR/$tdir
16074         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
16075
16076         check_default_stripe_attr --stripe-count
16077         check_default_stripe_attr --stripe-size
16078         check_default_stripe_attr --stripe-index
16079 }
16080 run_test 204a "Print default stripe attributes"
16081
16082 test_204b() {
16083         test_mkdir $DIR/$tdir
16084         $LFS setstripe --stripe-count 1 $DIR/$tdir
16085
16086         check_default_stripe_attr --stripe-size
16087         check_default_stripe_attr --stripe-index
16088 }
16089 run_test 204b "Print default stripe size and offset"
16090
16091 test_204c() {
16092         test_mkdir $DIR/$tdir
16093         $LFS setstripe --stripe-size 65536 $DIR/$tdir
16094
16095         check_default_stripe_attr --stripe-count
16096         check_default_stripe_attr --stripe-index
16097 }
16098 run_test 204c "Print default stripe count and offset"
16099
16100 test_204d() {
16101         test_mkdir $DIR/$tdir
16102         $LFS setstripe --stripe-index 0 $DIR/$tdir
16103
16104         check_default_stripe_attr --stripe-count
16105         check_default_stripe_attr --stripe-size
16106 }
16107 run_test 204d "Print default stripe count and size"
16108
16109 test_204e() {
16110         test_mkdir $DIR/$tdir
16111         $LFS setstripe -d $DIR/$tdir
16112
16113         check_default_stripe_attr --stripe-count --raw
16114         check_default_stripe_attr --stripe-size --raw
16115         check_default_stripe_attr --stripe-index --raw
16116 }
16117 run_test 204e "Print raw stripe attributes"
16118
16119 test_204f() {
16120         test_mkdir $DIR/$tdir
16121         $LFS setstripe --stripe-count 1 $DIR/$tdir
16122
16123         check_default_stripe_attr --stripe-size --raw
16124         check_default_stripe_attr --stripe-index --raw
16125 }
16126 run_test 204f "Print raw stripe size and offset"
16127
16128 test_204g() {
16129         test_mkdir $DIR/$tdir
16130         $LFS setstripe --stripe-size 65536 $DIR/$tdir
16131
16132         check_default_stripe_attr --stripe-count --raw
16133         check_default_stripe_attr --stripe-index --raw
16134 }
16135 run_test 204g "Print raw stripe count and offset"
16136
16137 test_204h() {
16138         test_mkdir $DIR/$tdir
16139         $LFS setstripe --stripe-index 0 $DIR/$tdir
16140
16141         check_default_stripe_attr --stripe-count --raw
16142         check_default_stripe_attr --stripe-size --raw
16143 }
16144 run_test 204h "Print raw stripe count and size"
16145
16146 # Figure out which job scheduler is being used, if any,
16147 # or use a fake one
16148 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
16149         JOBENV=SLURM_JOB_ID
16150 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
16151         JOBENV=LSB_JOBID
16152 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
16153         JOBENV=PBS_JOBID
16154 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
16155         JOBENV=LOADL_STEP_ID
16156 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
16157         JOBENV=JOB_ID
16158 else
16159         $LCTL list_param jobid_name > /dev/null 2>&1
16160         if [ $? -eq 0 ]; then
16161                 JOBENV=nodelocal
16162         else
16163                 JOBENV=FAKE_JOBID
16164         fi
16165 fi
16166 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
16167
16168 verify_jobstats() {
16169         local cmd=($1)
16170         shift
16171         local facets="$@"
16172
16173 # we don't really need to clear the stats for this test to work, since each
16174 # command has a unique jobid, but it makes debugging easier if needed.
16175 #       for facet in $facets; do
16176 #               local dev=$(convert_facet2label $facet)
16177 #               # clear old jobstats
16178 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
16179 #       done
16180
16181         # use a new JobID for each test, or we might see an old one
16182         [ "$JOBENV" = "FAKE_JOBID" ] &&
16183                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
16184
16185         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
16186
16187         [ "$JOBENV" = "nodelocal" ] && {
16188                 FAKE_JOBID=id.$testnum.%e.$RANDOM
16189                 $LCTL set_param jobid_name=$FAKE_JOBID
16190                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
16191         }
16192
16193         log "Test: ${cmd[*]}"
16194         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
16195
16196         if [ $JOBENV = "FAKE_JOBID" ]; then
16197                 FAKE_JOBID=$JOBVAL ${cmd[*]}
16198         else
16199                 ${cmd[*]}
16200         fi
16201
16202         # all files are created on OST0000
16203         for facet in $facets; do
16204                 local stats="*.$(convert_facet2label $facet).job_stats"
16205
16206                 # strip out libtool wrappers for in-tree executables
16207                 if [ $(do_facet $facet lctl get_param $stats |
16208                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
16209                         do_facet $facet lctl get_param $stats
16210                         error "No jobstats for $JOBVAL found on $facet::$stats"
16211                 fi
16212         done
16213 }
16214
16215 jobstats_set() {
16216         local new_jobenv=$1
16217
16218         set_persistent_param_and_check client "jobid_var" \
16219                 "$FSNAME.sys.jobid_var" $new_jobenv
16220 }
16221
16222 test_205a() { # Job stats
16223         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16224         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
16225                 skip "Need MDS version with at least 2.7.1"
16226         remote_mgs_nodsh && skip "remote MGS with nodsh"
16227         remote_mds_nodsh && skip "remote MDS with nodsh"
16228         remote_ost_nodsh && skip "remote OST with nodsh"
16229         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
16230                 skip "Server doesn't support jobstats"
16231         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
16232
16233         local old_jobenv=$($LCTL get_param -n jobid_var)
16234         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
16235
16236         if [[ $PERM_CMD == *"set_param -P"* ]]; then
16237                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
16238         else
16239                 stack_trap "do_facet mgs $PERM_CMD \
16240                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
16241         fi
16242         changelog_register
16243
16244         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
16245                                 mdt.*.job_cleanup_interval | head -n 1)
16246         local new_interval=5
16247         do_facet $SINGLEMDS \
16248                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
16249         stack_trap "do_facet $SINGLEMDS \
16250                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
16251         local start=$SECONDS
16252
16253         local cmd
16254         # mkdir
16255         cmd="mkdir $DIR/$tdir"
16256         verify_jobstats "$cmd" "$SINGLEMDS"
16257         # rmdir
16258         cmd="rmdir $DIR/$tdir"
16259         verify_jobstats "$cmd" "$SINGLEMDS"
16260         # mkdir on secondary MDT
16261         if [ $MDSCOUNT -gt 1 ]; then
16262                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
16263                 verify_jobstats "$cmd" "mds2"
16264         fi
16265         # mknod
16266         cmd="mknod $DIR/$tfile c 1 3"
16267         verify_jobstats "$cmd" "$SINGLEMDS"
16268         # unlink
16269         cmd="rm -f $DIR/$tfile"
16270         verify_jobstats "$cmd" "$SINGLEMDS"
16271         # create all files on OST0000 so verify_jobstats can find OST stats
16272         # open & close
16273         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
16274         verify_jobstats "$cmd" "$SINGLEMDS"
16275         # setattr
16276         cmd="touch $DIR/$tfile"
16277         verify_jobstats "$cmd" "$SINGLEMDS ost1"
16278         # write
16279         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
16280         verify_jobstats "$cmd" "ost1"
16281         # read
16282         cancel_lru_locks osc
16283         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
16284         verify_jobstats "$cmd" "ost1"
16285         # truncate
16286         cmd="$TRUNCATE $DIR/$tfile 0"
16287         verify_jobstats "$cmd" "$SINGLEMDS ost1"
16288         # rename
16289         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
16290         verify_jobstats "$cmd" "$SINGLEMDS"
16291         # jobstats expiry - sleep until old stats should be expired
16292         local left=$((new_interval + 5 - (SECONDS - start)))
16293         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
16294                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
16295                         "0" $left
16296         cmd="mkdir $DIR/$tdir.expire"
16297         verify_jobstats "$cmd" "$SINGLEMDS"
16298         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
16299             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
16300
16301         # Ensure that jobid are present in changelog (if supported by MDS)
16302         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
16303                 changelog_dump | tail -10
16304                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
16305                 [ $jobids -eq 9 ] ||
16306                         error "Wrong changelog jobid count $jobids != 9"
16307
16308                 # LU-5862
16309                 JOBENV="disable"
16310                 jobstats_set $JOBENV
16311                 touch $DIR/$tfile
16312                 changelog_dump | grep $tfile
16313                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
16314                 [ $jobids -eq 0 ] ||
16315                         error "Unexpected jobids when jobid_var=$JOBENV"
16316         fi
16317
16318         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
16319         JOBENV="JOBCOMPLEX"
16320         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
16321
16322         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
16323 }
16324 run_test 205a "Verify job stats"
16325
16326 # LU-13117
16327 test_205b() {
16328         $LCTL set_param jobid_var=USER jobid_name="%e.%u"
16329         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
16330         do_facet $SINGLEMDS $LCTL get_param mdt.*.job_stats |
16331                 grep job_id: | grep foolish &&
16332                         error "Unexpected jobid found"
16333         true
16334 }
16335 run_test 205b "Verify job stats jobid parsing"
16336
16337 # LU-1480, LU-1773 and LU-1657
16338 test_206() {
16339         mkdir -p $DIR/$tdir
16340         $LFS setstripe -c -1 $DIR/$tdir
16341 #define OBD_FAIL_LOV_INIT 0x1403
16342         $LCTL set_param fail_loc=0xa0001403
16343         $LCTL set_param fail_val=1
16344         touch $DIR/$tdir/$tfile || true
16345 }
16346 run_test 206 "fail lov_init_raid0() doesn't lbug"
16347
16348 test_207a() {
16349         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
16350         local fsz=`stat -c %s $DIR/$tfile`
16351         cancel_lru_locks mdc
16352
16353         # do not return layout in getattr intent
16354 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
16355         $LCTL set_param fail_loc=0x170
16356         local sz=`stat -c %s $DIR/$tfile`
16357
16358         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
16359
16360         rm -rf $DIR/$tfile
16361 }
16362 run_test 207a "can refresh layout at glimpse"
16363
16364 test_207b() {
16365         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
16366         local cksum=`md5sum $DIR/$tfile`
16367         local fsz=`stat -c %s $DIR/$tfile`
16368         cancel_lru_locks mdc
16369         cancel_lru_locks osc
16370
16371         # do not return layout in getattr intent
16372 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
16373         $LCTL set_param fail_loc=0x171
16374
16375         # it will refresh layout after the file is opened but before read issues
16376         echo checksum is "$cksum"
16377         echo "$cksum" |md5sum -c --quiet || error "file differs"
16378
16379         rm -rf $DIR/$tfile
16380 }
16381 run_test 207b "can refresh layout at open"
16382
16383 test_208() {
16384         # FIXME: in this test suite, only RD lease is used. This is okay
16385         # for now as only exclusive open is supported. After generic lease
16386         # is done, this test suite should be revised. - Jinshan
16387
16388         remote_mds_nodsh && skip "remote MDS with nodsh"
16389         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
16390                 skip "Need MDS version at least 2.4.52"
16391
16392         echo "==== test 1: verify get lease work"
16393         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
16394
16395         echo "==== test 2: verify lease can be broken by upcoming open"
16396         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
16397         local PID=$!
16398         sleep 1
16399
16400         $MULTIOP $DIR/$tfile oO_RDONLY:c
16401         kill -USR1 $PID && wait $PID || error "break lease error"
16402
16403         echo "==== test 3: verify lease can't be granted if an open already exists"
16404         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
16405         local PID=$!
16406         sleep 1
16407
16408         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
16409         kill -USR1 $PID && wait $PID || error "open file error"
16410
16411         echo "==== test 4: lease can sustain over recovery"
16412         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
16413         PID=$!
16414         sleep 1
16415
16416         fail mds1
16417
16418         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
16419
16420         echo "==== test 5: lease broken can't be regained by replay"
16421         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
16422         PID=$!
16423         sleep 1
16424
16425         # open file to break lease and then recovery
16426         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
16427         fail mds1
16428
16429         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
16430
16431         rm -f $DIR/$tfile
16432 }
16433 run_test 208 "Exclusive open"
16434
16435 test_209() {
16436         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
16437                 skip_env "must have disp_stripe"
16438
16439         touch $DIR/$tfile
16440         sync; sleep 5; sync;
16441
16442         echo 3 > /proc/sys/vm/drop_caches
16443         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
16444
16445         # open/close 500 times
16446         for i in $(seq 500); do
16447                 cat $DIR/$tfile
16448         done
16449
16450         echo 3 > /proc/sys/vm/drop_caches
16451         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
16452
16453         echo "before: $req_before, after: $req_after"
16454         [ $((req_after - req_before)) -ge 300 ] &&
16455                 error "open/close requests are not freed"
16456         return 0
16457 }
16458 run_test 209 "read-only open/close requests should be freed promptly"
16459
16460 test_212() {
16461         size=`date +%s`
16462         size=$((size % 8192 + 1))
16463         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
16464         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
16465         rm -f $DIR/f212 $DIR/f212.xyz
16466 }
16467 run_test 212 "Sendfile test ============================================"
16468
16469 test_213() {
16470         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
16471         cancel_lru_locks osc
16472         lctl set_param fail_loc=0x8000040f
16473         # generate a read lock
16474         cat $DIR/$tfile > /dev/null
16475         # write to the file, it will try to cancel the above read lock.
16476         cat /etc/hosts >> $DIR/$tfile
16477 }
16478 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
16479
16480 test_214() { # for bug 20133
16481         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
16482         for (( i=0; i < 340; i++ )) ; do
16483                 touch $DIR/$tdir/d214c/a$i
16484         done
16485
16486         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
16487         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
16488         ls $DIR/d214c || error "ls $DIR/d214c failed"
16489         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
16490         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
16491 }
16492 run_test 214 "hash-indexed directory test - bug 20133"
16493
16494 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
16495 create_lnet_proc_files() {
16496         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
16497 }
16498
16499 # counterpart of create_lnet_proc_files
16500 remove_lnet_proc_files() {
16501         rm -f $TMP/lnet_$1.sys
16502 }
16503
16504 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
16505 # 3rd arg as regexp for body
16506 check_lnet_proc_stats() {
16507         local l=$(cat "$TMP/lnet_$1" |wc -l)
16508         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
16509
16510         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
16511 }
16512
16513 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
16514 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
16515 # optional and can be regexp for 2nd line (lnet.routes case)
16516 check_lnet_proc_entry() {
16517         local blp=2          # blp stands for 'position of 1st line of body'
16518         [ -z "$5" ] || blp=3 # lnet.routes case
16519
16520         local l=$(cat "$TMP/lnet_$1" |wc -l)
16521         # subtracting one from $blp because the body can be empty
16522         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
16523
16524         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
16525                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
16526
16527         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
16528                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
16529
16530         # bail out if any unexpected line happened
16531         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
16532         [ "$?" != 0 ] || error "$2 misformatted"
16533 }
16534
16535 test_215() { # for bugs 18102, 21079, 21517
16536         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16537
16538         local N='(0|[1-9][0-9]*)'       # non-negative numeric
16539         local P='[1-9][0-9]*'           # positive numeric
16540         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
16541         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
16542         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
16543         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
16544
16545         local L1 # regexp for 1st line
16546         local L2 # regexp for 2nd line (optional)
16547         local BR # regexp for the rest (body)
16548
16549         # lnet.stats should look as 11 space-separated non-negative numerics
16550         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
16551         create_lnet_proc_files "stats"
16552         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
16553         remove_lnet_proc_files "stats"
16554
16555         # lnet.routes should look like this:
16556         # Routing disabled/enabled
16557         # net hops priority state router
16558         # where net is a string like tcp0, hops > 0, priority >= 0,
16559         # state is up/down,
16560         # router is a string like 192.168.1.1@tcp2
16561         L1="^Routing (disabled|enabled)$"
16562         L2="^net +hops +priority +state +router$"
16563         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
16564         create_lnet_proc_files "routes"
16565         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
16566         remove_lnet_proc_files "routes"
16567
16568         # lnet.routers should look like this:
16569         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
16570         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
16571         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
16572         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
16573         L1="^ref +rtr_ref +alive +router$"
16574         BR="^$P +$P +(up|down) +$NID$"
16575         create_lnet_proc_files "routers"
16576         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
16577         remove_lnet_proc_files "routers"
16578
16579         # lnet.peers should look like this:
16580         # nid refs state last max rtr min tx min queue
16581         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
16582         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
16583         # numeric (0 or >0 or <0), queue >= 0.
16584         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
16585         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
16586         create_lnet_proc_files "peers"
16587         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
16588         remove_lnet_proc_files "peers"
16589
16590         # lnet.buffers  should look like this:
16591         # pages count credits min
16592         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
16593         L1="^pages +count +credits +min$"
16594         BR="^ +$N +$N +$I +$I$"
16595         create_lnet_proc_files "buffers"
16596         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
16597         remove_lnet_proc_files "buffers"
16598
16599         # lnet.nis should look like this:
16600         # nid status alive refs peer rtr max tx min
16601         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
16602         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
16603         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
16604         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
16605         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
16606         create_lnet_proc_files "nis"
16607         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
16608         remove_lnet_proc_files "nis"
16609
16610         # can we successfully write to lnet.stats?
16611         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
16612 }
16613 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
16614
16615 test_216() { # bug 20317
16616         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16617         remote_ost_nodsh && skip "remote OST with nodsh"
16618
16619         local node
16620         local facets=$(get_facets OST)
16621         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16622
16623         save_lustre_params client "osc.*.contention_seconds" > $p
16624         save_lustre_params $facets \
16625                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
16626         save_lustre_params $facets \
16627                 "ldlm.namespaces.filter-*.contended_locks" >> $p
16628         save_lustre_params $facets \
16629                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
16630         clear_stats osc.*.osc_stats
16631
16632         # agressive lockless i/o settings
16633         do_nodes $(comma_list $(osts_nodes)) \
16634                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
16635                         ldlm.namespaces.filter-*.contended_locks=0 \
16636                         ldlm.namespaces.filter-*.contention_seconds=60"
16637         lctl set_param -n osc.*.contention_seconds=60
16638
16639         $DIRECTIO write $DIR/$tfile 0 10 4096
16640         $CHECKSTAT -s 40960 $DIR/$tfile
16641
16642         # disable lockless i/o
16643         do_nodes $(comma_list $(osts_nodes)) \
16644                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
16645                         ldlm.namespaces.filter-*.contended_locks=32 \
16646                         ldlm.namespaces.filter-*.contention_seconds=0"
16647         lctl set_param -n osc.*.contention_seconds=0
16648         clear_stats osc.*.osc_stats
16649
16650         dd if=/dev/zero of=$DIR/$tfile count=0
16651         $CHECKSTAT -s 0 $DIR/$tfile
16652
16653         restore_lustre_params <$p
16654         rm -f $p
16655         rm $DIR/$tfile
16656 }
16657 run_test 216 "check lockless direct write updates file size and kms correctly"
16658
16659 test_217() { # bug 22430
16660         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16661
16662         local node
16663         local nid
16664
16665         for node in $(nodes_list); do
16666                 nid=$(host_nids_address $node $NETTYPE)
16667                 if [[ $nid = *-* ]] ; then
16668                         echo "lctl ping $(h2nettype $nid)"
16669                         lctl ping $(h2nettype $nid)
16670                 else
16671                         echo "skipping $node (no hyphen detected)"
16672                 fi
16673         done
16674 }
16675 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
16676
16677 test_218() {
16678        # do directio so as not to populate the page cache
16679        log "creating a 10 Mb file"
16680        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
16681        log "starting reads"
16682        dd if=$DIR/$tfile of=/dev/null bs=4096 &
16683        log "truncating the file"
16684        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
16685        log "killing dd"
16686        kill %+ || true # reads might have finished
16687        echo "wait until dd is finished"
16688        wait
16689        log "removing the temporary file"
16690        rm -rf $DIR/$tfile || error "tmp file removal failed"
16691 }
16692 run_test 218 "parallel read and truncate should not deadlock"
16693
16694 test_219() {
16695         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16696
16697         # write one partial page
16698         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
16699         # set no grant so vvp_io_commit_write will do sync write
16700         $LCTL set_param fail_loc=0x411
16701         # write a full page at the end of file
16702         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
16703
16704         $LCTL set_param fail_loc=0
16705         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
16706         $LCTL set_param fail_loc=0x411
16707         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
16708
16709         # LU-4201
16710         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
16711         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
16712 }
16713 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
16714
16715 test_220() { #LU-325
16716         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16717         remote_ost_nodsh && skip "remote OST with nodsh"
16718         remote_mds_nodsh && skip "remote MDS with nodsh"
16719         remote_mgs_nodsh && skip "remote MGS with nodsh"
16720
16721         local OSTIDX=0
16722
16723         # create on MDT0000 so the last_id and next_id are correct
16724         mkdir $DIR/$tdir
16725         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
16726         OST=${OST%_UUID}
16727
16728         # on the mdt's osc
16729         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
16730         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
16731                         osp.$mdtosc_proc1.prealloc_last_id)
16732         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
16733                         osp.$mdtosc_proc1.prealloc_next_id)
16734
16735         $LFS df -i
16736
16737         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
16738         #define OBD_FAIL_OST_ENOINO              0x229
16739         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
16740         create_pool $FSNAME.$TESTNAME || return 1
16741         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
16742
16743         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
16744
16745         MDSOBJS=$((last_id - next_id))
16746         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
16747
16748         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
16749         echo "OST still has $count kbytes free"
16750
16751         echo "create $MDSOBJS files @next_id..."
16752         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
16753
16754         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
16755                         osp.$mdtosc_proc1.prealloc_last_id)
16756         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
16757                         osp.$mdtosc_proc1.prealloc_next_id)
16758
16759         echo "after creation, last_id=$last_id2, next_id=$next_id2"
16760         $LFS df -i
16761
16762         echo "cleanup..."
16763
16764         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
16765         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
16766
16767         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
16768                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
16769         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
16770                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
16771         echo "unlink $MDSOBJS files @$next_id..."
16772         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
16773 }
16774 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
16775
16776 test_221() {
16777         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16778
16779         dd if=`which date` of=$MOUNT/date oflag=sync
16780         chmod +x $MOUNT/date
16781
16782         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
16783         $LCTL set_param fail_loc=0x80001401
16784
16785         $MOUNT/date > /dev/null
16786         rm -f $MOUNT/date
16787 }
16788 run_test 221 "make sure fault and truncate race to not cause OOM"
16789
16790 test_222a () {
16791         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16792
16793         rm -rf $DIR/$tdir
16794         test_mkdir $DIR/$tdir
16795         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16796         createmany -o $DIR/$tdir/$tfile 10
16797         cancel_lru_locks mdc
16798         cancel_lru_locks osc
16799         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16800         $LCTL set_param fail_loc=0x31a
16801         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
16802         $LCTL set_param fail_loc=0
16803         rm -r $DIR/$tdir
16804 }
16805 run_test 222a "AGL for ls should not trigger CLIO lock failure"
16806
16807 test_222b () {
16808         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16809
16810         rm -rf $DIR/$tdir
16811         test_mkdir $DIR/$tdir
16812         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16813         createmany -o $DIR/$tdir/$tfile 10
16814         cancel_lru_locks mdc
16815         cancel_lru_locks osc
16816         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16817         $LCTL set_param fail_loc=0x31a
16818         rm -r $DIR/$tdir || error "AGL for rmdir failed"
16819         $LCTL set_param fail_loc=0
16820 }
16821 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
16822
16823 test_223 () {
16824         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16825
16826         rm -rf $DIR/$tdir
16827         test_mkdir $DIR/$tdir
16828         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16829         createmany -o $DIR/$tdir/$tfile 10
16830         cancel_lru_locks mdc
16831         cancel_lru_locks osc
16832         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
16833         $LCTL set_param fail_loc=0x31b
16834         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
16835         $LCTL set_param fail_loc=0
16836         rm -r $DIR/$tdir
16837 }
16838 run_test 223 "osc reenqueue if without AGL lock granted ======================="
16839
16840 test_224a() { # LU-1039, MRP-303
16841         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16842
16843         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
16844         $LCTL set_param fail_loc=0x508
16845         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
16846         $LCTL set_param fail_loc=0
16847         df $DIR
16848 }
16849 run_test 224a "Don't panic on bulk IO failure"
16850
16851 test_224b() { # LU-1039, MRP-303
16852         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16853
16854         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
16855         cancel_lru_locks osc
16856         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
16857         $LCTL set_param fail_loc=0x515
16858         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
16859         $LCTL set_param fail_loc=0
16860         df $DIR
16861 }
16862 run_test 224b "Don't panic on bulk IO failure"
16863
16864 test_224c() { # LU-6441
16865         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16866         remote_mds_nodsh && skip "remote MDS with nodsh"
16867
16868         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16869         save_writethrough $p
16870         set_cache writethrough on
16871
16872         local pages_per_rpc=$($LCTL get_param osc.*.max_pages_per_rpc)
16873         local at_max=$($LCTL get_param -n at_max)
16874         local timeout=$($LCTL get_param -n timeout)
16875         local test_at="at_max"
16876         local param_at="$FSNAME.sys.at_max"
16877         local test_timeout="timeout"
16878         local param_timeout="$FSNAME.sys.timeout"
16879
16880         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
16881
16882         set_persistent_param_and_check client "$test_at" "$param_at" 0
16883         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
16884
16885         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
16886         do_facet ost1 "$LCTL set_param fail_loc=0x520"
16887         $LFS setstripe -c 1 -i 0 $DIR/$tfile
16888         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
16889         sync
16890         do_facet ost1 "$LCTL set_param fail_loc=0"
16891
16892         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
16893         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
16894                 $timeout
16895
16896         $LCTL set_param -n $pages_per_rpc
16897         restore_lustre_params < $p
16898         rm -f $p
16899 }
16900 run_test 224c "Don't hang if one of md lost during large bulk RPC"
16901
16902 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
16903 test_225a () {
16904         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16905         if [ -z ${MDSSURVEY} ]; then
16906                 skip_env "mds-survey not found"
16907         fi
16908         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16909                 skip "Need MDS version at least 2.2.51"
16910
16911         local mds=$(facet_host $SINGLEMDS)
16912         local target=$(do_nodes $mds 'lctl dl' |
16913                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16914
16915         local cmd1="file_count=1000 thrhi=4"
16916         local cmd2="dir_count=2 layer=mdd stripe_count=0"
16917         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16918         local cmd="$cmd1 $cmd2 $cmd3"
16919
16920         rm -f ${TMP}/mds_survey*
16921         echo + $cmd
16922         eval $cmd || error "mds-survey with zero-stripe failed"
16923         cat ${TMP}/mds_survey*
16924         rm -f ${TMP}/mds_survey*
16925 }
16926 run_test 225a "Metadata survey sanity with zero-stripe"
16927
16928 test_225b () {
16929         if [ -z ${MDSSURVEY} ]; then
16930                 skip_env "mds-survey not found"
16931         fi
16932         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16933                 skip "Need MDS version at least 2.2.51"
16934         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16935         remote_mds_nodsh && skip "remote MDS with nodsh"
16936         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
16937                 skip_env "Need to mount OST to test"
16938         fi
16939
16940         local mds=$(facet_host $SINGLEMDS)
16941         local target=$(do_nodes $mds 'lctl dl' |
16942                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16943
16944         local cmd1="file_count=1000 thrhi=4"
16945         local cmd2="dir_count=2 layer=mdd stripe_count=1"
16946         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16947         local cmd="$cmd1 $cmd2 $cmd3"
16948
16949         rm -f ${TMP}/mds_survey*
16950         echo + $cmd
16951         eval $cmd || error "mds-survey with stripe_count failed"
16952         cat ${TMP}/mds_survey*
16953         rm -f ${TMP}/mds_survey*
16954 }
16955 run_test 225b "Metadata survey sanity with stripe_count = 1"
16956
16957 mcreate_path2fid () {
16958         local mode=$1
16959         local major=$2
16960         local minor=$3
16961         local name=$4
16962         local desc=$5
16963         local path=$DIR/$tdir/$name
16964         local fid
16965         local rc
16966         local fid_path
16967
16968         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
16969                 error "cannot create $desc"
16970
16971         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
16972         rc=$?
16973         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
16974
16975         fid_path=$($LFS fid2path $MOUNT $fid)
16976         rc=$?
16977         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
16978
16979         [ "$path" == "$fid_path" ] ||
16980                 error "fid2path returned $fid_path, expected $path"
16981
16982         echo "pass with $path and $fid"
16983 }
16984
16985 test_226a () {
16986         rm -rf $DIR/$tdir
16987         mkdir -p $DIR/$tdir
16988
16989         mcreate_path2fid 0010666 0 0 fifo "FIFO"
16990         mcreate_path2fid 0020666 1 3 null "character special file (null)"
16991         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
16992         mcreate_path2fid 0040666 0 0 dir "directory"
16993         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
16994         mcreate_path2fid 0100666 0 0 file "regular file"
16995         mcreate_path2fid 0120666 0 0 link "symbolic link"
16996         mcreate_path2fid 0140666 0 0 sock "socket"
16997 }
16998 run_test 226a "call path2fid and fid2path on files of all type"
16999
17000 test_226b () {
17001         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17002
17003         local MDTIDX=1
17004
17005         rm -rf $DIR/$tdir
17006         mkdir -p $DIR/$tdir
17007         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
17008                 error "create remote directory failed"
17009         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
17010         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
17011                                 "character special file (null)"
17012         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
17013                                 "character special file (no device)"
17014         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
17015         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
17016                                 "block special file (loop)"
17017         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
17018         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
17019         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
17020 }
17021 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
17022
17023 # LU-1299 Executing or running ldd on a truncated executable does not
17024 # cause an out-of-memory condition.
17025 test_227() {
17026         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17027         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
17028
17029         dd if=$(which date) of=$MOUNT/date bs=1k count=1
17030         chmod +x $MOUNT/date
17031
17032         $MOUNT/date > /dev/null
17033         ldd $MOUNT/date > /dev/null
17034         rm -f $MOUNT/date
17035 }
17036 run_test 227 "running truncated executable does not cause OOM"
17037
17038 # LU-1512 try to reuse idle OI blocks
17039 test_228a() {
17040         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17041         remote_mds_nodsh && skip "remote MDS with nodsh"
17042         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
17043
17044         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
17045         local myDIR=$DIR/$tdir
17046
17047         mkdir -p $myDIR
17048         #define OBD_FAIL_SEQ_EXHAUST             0x1002
17049         $LCTL set_param fail_loc=0x80001002
17050         createmany -o $myDIR/t- 10000
17051         $LCTL set_param fail_loc=0
17052         # The guard is current the largest FID holder
17053         touch $myDIR/guard
17054         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
17055                     tr -d '[')
17056         local IDX=$(($SEQ % 64))
17057
17058         do_facet $SINGLEMDS sync
17059         # Make sure journal flushed.
17060         sleep 6
17061         local blk1=$(do_facet $SINGLEMDS \
17062                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17063                      grep Blockcount | awk '{print $4}')
17064
17065         # Remove old files, some OI blocks will become idle.
17066         unlinkmany $myDIR/t- 10000
17067         # Create new files, idle OI blocks should be reused.
17068         createmany -o $myDIR/t- 2000
17069         do_facet $SINGLEMDS sync
17070         # Make sure journal flushed.
17071         sleep 6
17072         local blk2=$(do_facet $SINGLEMDS \
17073                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17074                      grep Blockcount | awk '{print $4}')
17075
17076         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
17077 }
17078 run_test 228a "try to reuse idle OI blocks"
17079
17080 test_228b() {
17081         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17082         remote_mds_nodsh && skip "remote MDS with nodsh"
17083         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
17084
17085         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
17086         local myDIR=$DIR/$tdir
17087
17088         mkdir -p $myDIR
17089         #define OBD_FAIL_SEQ_EXHAUST             0x1002
17090         $LCTL set_param fail_loc=0x80001002
17091         createmany -o $myDIR/t- 10000
17092         $LCTL set_param fail_loc=0
17093         # The guard is current the largest FID holder
17094         touch $myDIR/guard
17095         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
17096                     tr -d '[')
17097         local IDX=$(($SEQ % 64))
17098
17099         do_facet $SINGLEMDS sync
17100         # Make sure journal flushed.
17101         sleep 6
17102         local blk1=$(do_facet $SINGLEMDS \
17103                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17104                      grep Blockcount | awk '{print $4}')
17105
17106         # Remove old files, some OI blocks will become idle.
17107         unlinkmany $myDIR/t- 10000
17108
17109         # stop the MDT
17110         stop $SINGLEMDS || error "Fail to stop MDT."
17111         # remount the MDT
17112         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
17113
17114         df $MOUNT || error "Fail to df."
17115         # Create new files, idle OI blocks should be reused.
17116         createmany -o $myDIR/t- 2000
17117         do_facet $SINGLEMDS sync
17118         # Make sure journal flushed.
17119         sleep 6
17120         local blk2=$(do_facet $SINGLEMDS \
17121                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17122                      grep Blockcount | awk '{print $4}')
17123
17124         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
17125 }
17126 run_test 228b "idle OI blocks can be reused after MDT restart"
17127
17128 #LU-1881
17129 test_228c() {
17130         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17131         remote_mds_nodsh && skip "remote MDS with nodsh"
17132         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
17133
17134         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
17135         local myDIR=$DIR/$tdir
17136
17137         mkdir -p $myDIR
17138         #define OBD_FAIL_SEQ_EXHAUST             0x1002
17139         $LCTL set_param fail_loc=0x80001002
17140         # 20000 files can guarantee there are index nodes in the OI file
17141         createmany -o $myDIR/t- 20000
17142         $LCTL set_param fail_loc=0
17143         # The guard is current the largest FID holder
17144         touch $myDIR/guard
17145         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
17146                     tr -d '[')
17147         local IDX=$(($SEQ % 64))
17148
17149         do_facet $SINGLEMDS sync
17150         # Make sure journal flushed.
17151         sleep 6
17152         local blk1=$(do_facet $SINGLEMDS \
17153                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17154                      grep Blockcount | awk '{print $4}')
17155
17156         # Remove old files, some OI blocks will become idle.
17157         unlinkmany $myDIR/t- 20000
17158         rm -f $myDIR/guard
17159         # The OI file should become empty now
17160
17161         # Create new files, idle OI blocks should be reused.
17162         createmany -o $myDIR/t- 2000
17163         do_facet $SINGLEMDS sync
17164         # Make sure journal flushed.
17165         sleep 6
17166         local blk2=$(do_facet $SINGLEMDS \
17167                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17168                      grep Blockcount | awk '{print $4}')
17169
17170         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
17171 }
17172 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
17173
17174 test_229() { # LU-2482, LU-3448
17175         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17176         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
17177         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
17178                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
17179
17180         rm -f $DIR/$tfile
17181
17182         # Create a file with a released layout and stripe count 2.
17183         $MULTIOP $DIR/$tfile H2c ||
17184                 error "failed to create file with released layout"
17185
17186         $LFS getstripe -v $DIR/$tfile
17187
17188         local pattern=$($LFS getstripe -L $DIR/$tfile)
17189         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
17190
17191         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
17192                 error "getstripe"
17193         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
17194         stat $DIR/$tfile || error "failed to stat released file"
17195
17196         chown $RUNAS_ID $DIR/$tfile ||
17197                 error "chown $RUNAS_ID $DIR/$tfile failed"
17198
17199         chgrp $RUNAS_ID $DIR/$tfile ||
17200                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
17201
17202         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
17203         rm $DIR/$tfile || error "failed to remove released file"
17204 }
17205 run_test 229 "getstripe/stat/rm/attr changes work on released files"
17206
17207 test_230a() {
17208         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17209         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17210         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17211                 skip "Need MDS version at least 2.11.52"
17212
17213         local MDTIDX=1
17214
17215         test_mkdir $DIR/$tdir
17216         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
17217         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
17218         [ $mdt_idx -ne 0 ] &&
17219                 error "create local directory on wrong MDT $mdt_idx"
17220
17221         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
17222                         error "create remote directory failed"
17223         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
17224         [ $mdt_idx -ne $MDTIDX ] &&
17225                 error "create remote directory on wrong MDT $mdt_idx"
17226
17227         createmany -o $DIR/$tdir/test_230/t- 10 ||
17228                 error "create files on remote directory failed"
17229         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
17230         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
17231         rm -r $DIR/$tdir || error "unlink remote directory failed"
17232 }
17233 run_test 230a "Create remote directory and files under the remote directory"
17234
17235 test_230b() {
17236         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17237         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17238         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17239                 skip "Need MDS version at least 2.11.52"
17240
17241         local MDTIDX=1
17242         local mdt_index
17243         local i
17244         local file
17245         local pid
17246         local stripe_count
17247         local migrate_dir=$DIR/$tdir/migrate_dir
17248         local other_dir=$DIR/$tdir/other_dir
17249
17250         test_mkdir $DIR/$tdir
17251         test_mkdir -i0 -c1 $migrate_dir
17252         test_mkdir -i0 -c1 $other_dir
17253         for ((i=0; i<10; i++)); do
17254                 mkdir -p $migrate_dir/dir_${i}
17255                 createmany -o $migrate_dir/dir_${i}/f 10 ||
17256                         error "create files under remote dir failed $i"
17257         done
17258
17259         cp /etc/passwd $migrate_dir/$tfile
17260         cp /etc/passwd $other_dir/$tfile
17261         chattr +SAD $migrate_dir
17262         chattr +SAD $migrate_dir/$tfile
17263
17264         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
17265         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
17266         local old_dir_mode=$(stat -c%f $migrate_dir)
17267         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
17268
17269         mkdir -p $migrate_dir/dir_default_stripe2
17270         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
17271         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
17272
17273         mkdir -p $other_dir
17274         ln $migrate_dir/$tfile $other_dir/luna
17275         ln $migrate_dir/$tfile $migrate_dir/sofia
17276         ln $other_dir/$tfile $migrate_dir/david
17277         ln -s $migrate_dir/$tfile $other_dir/zachary
17278         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
17279         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
17280
17281         local len
17282         local lnktgt
17283
17284         # inline symlink
17285         for len in 58 59 60; do
17286                 lnktgt=$(str_repeat 'l' $len)
17287                 touch $migrate_dir/$lnktgt
17288                 ln -s $lnktgt $migrate_dir/${len}char_ln
17289         done
17290
17291         # PATH_MAX
17292         for len in 4094 4095; do
17293                 lnktgt=$(str_repeat 'l' $len)
17294                 ln -s $lnktgt $migrate_dir/${len}char_ln
17295         done
17296
17297         # NAME_MAX
17298         for len in 254 255; do
17299                 touch $migrate_dir/$(str_repeat 'l' $len)
17300         done
17301
17302         $LFS migrate -m $MDTIDX $migrate_dir ||
17303                 error "fails on migrating remote dir to MDT1"
17304
17305         echo "migratate to MDT1, then checking.."
17306         for ((i = 0; i < 10; i++)); do
17307                 for file in $(find $migrate_dir/dir_${i}); do
17308                         mdt_index=$($LFS getstripe -m $file)
17309                         # broken symlink getstripe will fail
17310                         [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
17311                                 error "$file is not on MDT${MDTIDX}"
17312                 done
17313         done
17314
17315         # the multiple link file should still in MDT0
17316         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
17317         [ $mdt_index == 0 ] ||
17318                 error "$file is not on MDT${MDTIDX}"
17319
17320         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
17321         [ "$old_dir_flag" = "$new_dir_flag" ] ||
17322                 error " expect $old_dir_flag get $new_dir_flag"
17323
17324         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
17325         [ "$old_file_flag" = "$new_file_flag" ] ||
17326                 error " expect $old_file_flag get $new_file_flag"
17327
17328         local new_dir_mode=$(stat -c%f $migrate_dir)
17329         [ "$old_dir_mode" = "$new_dir_mode" ] ||
17330                 error "expect mode $old_dir_mode get $new_dir_mode"
17331
17332         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
17333         [ "$old_file_mode" = "$new_file_mode" ] ||
17334                 error "expect mode $old_file_mode get $new_file_mode"
17335
17336         diff /etc/passwd $migrate_dir/$tfile ||
17337                 error "$tfile different after migration"
17338
17339         diff /etc/passwd $other_dir/luna ||
17340                 error "luna different after migration"
17341
17342         diff /etc/passwd $migrate_dir/sofia ||
17343                 error "sofia different after migration"
17344
17345         diff /etc/passwd $migrate_dir/david ||
17346                 error "david different after migration"
17347
17348         diff /etc/passwd $other_dir/zachary ||
17349                 error "zachary different after migration"
17350
17351         diff /etc/passwd $migrate_dir/${tfile}_ln ||
17352                 error "${tfile}_ln different after migration"
17353
17354         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
17355                 error "${tfile}_ln_other different after migration"
17356
17357         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
17358         [ $stripe_count = 2 ] ||
17359                 error "dir strpe_count $d != 2 after migration."
17360
17361         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
17362         [ $stripe_count = 2 ] ||
17363                 error "file strpe_count $d != 2 after migration."
17364
17365         #migrate back to MDT0
17366         MDTIDX=0
17367
17368         $LFS migrate -m $MDTIDX $migrate_dir ||
17369                 error "fails on migrating remote dir to MDT0"
17370
17371         echo "migrate back to MDT0, checking.."
17372         for file in $(find $migrate_dir); do
17373                 mdt_index=$($LFS getstripe -m $file)
17374                 [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
17375                         error "$file is not on MDT${MDTIDX}"
17376         done
17377
17378         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
17379         [ "$old_dir_flag" = "$new_dir_flag" ] ||
17380                 error " expect $old_dir_flag get $new_dir_flag"
17381
17382         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
17383         [ "$old_file_flag" = "$new_file_flag" ] ||
17384                 error " expect $old_file_flag get $new_file_flag"
17385
17386         local new_dir_mode=$(stat -c%f $migrate_dir)
17387         [ "$old_dir_mode" = "$new_dir_mode" ] ||
17388                 error "expect mode $old_dir_mode get $new_dir_mode"
17389
17390         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
17391         [ "$old_file_mode" = "$new_file_mode" ] ||
17392                 error "expect mode $old_file_mode get $new_file_mode"
17393
17394         diff /etc/passwd ${migrate_dir}/$tfile ||
17395                 error "$tfile different after migration"
17396
17397         diff /etc/passwd ${other_dir}/luna ||
17398                 error "luna different after migration"
17399
17400         diff /etc/passwd ${migrate_dir}/sofia ||
17401                 error "sofia different after migration"
17402
17403         diff /etc/passwd ${other_dir}/zachary ||
17404                 error "zachary different after migration"
17405
17406         diff /etc/passwd $migrate_dir/${tfile}_ln ||
17407                 error "${tfile}_ln different after migration"
17408
17409         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
17410                 error "${tfile}_ln_other different after migration"
17411
17412         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
17413         [ $stripe_count = 2 ] ||
17414                 error "dir strpe_count $d != 2 after migration."
17415
17416         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
17417         [ $stripe_count = 2 ] ||
17418                 error "file strpe_count $d != 2 after migration."
17419
17420         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17421 }
17422 run_test 230b "migrate directory"
17423
17424 test_230c() {
17425         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17426         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17427         remote_mds_nodsh && skip "remote MDS with nodsh"
17428         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17429                 skip "Need MDS version at least 2.11.52"
17430
17431         local MDTIDX=1
17432         local total=3
17433         local mdt_index
17434         local file
17435         local migrate_dir=$DIR/$tdir/migrate_dir
17436
17437         #If migrating directory fails in the middle, all entries of
17438         #the directory is still accessiable.
17439         test_mkdir $DIR/$tdir
17440         test_mkdir -i0 -c1 $migrate_dir
17441         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
17442         stat $migrate_dir
17443         createmany -o $migrate_dir/f $total ||
17444                 error "create files under ${migrate_dir} failed"
17445
17446         # fail after migrating top dir, and this will fail only once, so the
17447         # first sub file migration will fail (currently f3), others succeed.
17448         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
17449         do_facet mds1 lctl set_param fail_loc=0x1801
17450         local t=$(ls $migrate_dir | wc -l)
17451         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
17452                 error "migrate should fail"
17453         local u=$(ls $migrate_dir | wc -l)
17454         [ "$u" == "$t" ] || error "$u != $t during migration"
17455
17456         # add new dir/file should succeed
17457         mkdir $migrate_dir/dir ||
17458                 error "mkdir failed under migrating directory"
17459         touch $migrate_dir/file ||
17460                 error "create file failed under migrating directory"
17461
17462         # add file with existing name should fail
17463         for file in $migrate_dir/f*; do
17464                 stat $file > /dev/null || error "stat $file failed"
17465                 $OPENFILE -f O_CREAT:O_EXCL $file &&
17466                         error "open(O_CREAT|O_EXCL) $file should fail"
17467                 $MULTIOP $file m && error "create $file should fail"
17468                 touch $DIR/$tdir/remote_dir/$tfile ||
17469                         error "touch $tfile failed"
17470                 ln $DIR/$tdir/remote_dir/$tfile $file &&
17471                         error "link $file should fail"
17472                 mdt_index=$($LFS getstripe -m $file)
17473                 if [ $mdt_index == 0 ]; then
17474                         # file failed to migrate is not allowed to rename to
17475                         mv $DIR/$tdir/remote_dir/$tfile $file &&
17476                                 error "rename to $file should fail"
17477                 else
17478                         mv $DIR/$tdir/remote_dir/$tfile $file ||
17479                                 error "rename to $file failed"
17480                 fi
17481                 echo hello >> $file || error "write $file failed"
17482         done
17483
17484         # resume migration with different options should fail
17485         $LFS migrate -m 0 $migrate_dir &&
17486                 error "migrate -m 0 $migrate_dir should fail"
17487
17488         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
17489                 error "migrate -c 2 $migrate_dir should fail"
17490
17491         # resume migration should succeed
17492         $LFS migrate -m $MDTIDX $migrate_dir ||
17493                 error "migrate $migrate_dir failed"
17494
17495         echo "Finish migration, then checking.."
17496         for file in $(find $migrate_dir); do
17497                 mdt_index=$($LFS getstripe -m $file)
17498                 [ $mdt_index == $MDTIDX ] ||
17499                         error "$file is not on MDT${MDTIDX}"
17500         done
17501
17502         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17503 }
17504 run_test 230c "check directory accessiblity if migration failed"
17505
17506 test_230d() {
17507         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17508         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17509         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17510                 skip "Need MDS version at least 2.11.52"
17511         # LU-11235
17512         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
17513
17514         local migrate_dir=$DIR/$tdir/migrate_dir
17515         local old_index
17516         local new_index
17517         local old_count
17518         local new_count
17519         local new_hash
17520         local mdt_index
17521         local i
17522         local j
17523
17524         old_index=$((RANDOM % MDSCOUNT))
17525         old_count=$((MDSCOUNT - old_index))
17526         new_index=$((RANDOM % MDSCOUNT))
17527         new_count=$((MDSCOUNT - new_index))
17528         new_hash=1 # for all_char
17529
17530         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
17531         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
17532
17533         test_mkdir $DIR/$tdir
17534         test_mkdir -i $old_index -c $old_count $migrate_dir
17535
17536         for ((i=0; i<100; i++)); do
17537                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
17538                 createmany -o $migrate_dir/dir_${i}/f 100 ||
17539                         error "create files under remote dir failed $i"
17540         done
17541
17542         echo -n "Migrate from MDT$old_index "
17543         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
17544         echo -n "to MDT$new_index"
17545         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
17546         echo
17547
17548         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
17549         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
17550                 error "migrate remote dir error"
17551
17552         echo "Finish migration, then checking.."
17553         for file in $(find $migrate_dir); do
17554                 mdt_index=$($LFS getstripe -m $file)
17555                 if [ $mdt_index -lt $new_index ] ||
17556                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
17557                         error "$file is on MDT$mdt_index"
17558                 fi
17559         done
17560
17561         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17562 }
17563 run_test 230d "check migrate big directory"
17564
17565 test_230e() {
17566         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17567         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17568         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17569                 skip "Need MDS version at least 2.11.52"
17570
17571         local i
17572         local j
17573         local a_fid
17574         local b_fid
17575
17576         mkdir -p $DIR/$tdir
17577         mkdir $DIR/$tdir/migrate_dir
17578         mkdir $DIR/$tdir/other_dir
17579         touch $DIR/$tdir/migrate_dir/a
17580         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
17581         ls $DIR/$tdir/other_dir
17582
17583         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
17584                 error "migrate dir fails"
17585
17586         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
17587         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
17588
17589         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17590         [ $mdt_index == 0 ] || error "a is not on MDT0"
17591
17592         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
17593                 error "migrate dir fails"
17594
17595         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
17596         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
17597
17598         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17599         [ $mdt_index == 1 ] || error "a is not on MDT1"
17600
17601         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
17602         [ $mdt_index == 1 ] || error "b is not on MDT1"
17603
17604         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
17605         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
17606
17607         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
17608
17609         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17610 }
17611 run_test 230e "migrate mulitple local link files"
17612
17613 test_230f() {
17614         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17615         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17616         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17617                 skip "Need MDS version at least 2.11.52"
17618
17619         local a_fid
17620         local ln_fid
17621
17622         mkdir -p $DIR/$tdir
17623         mkdir $DIR/$tdir/migrate_dir
17624         $LFS mkdir -i1 $DIR/$tdir/other_dir
17625         touch $DIR/$tdir/migrate_dir/a
17626         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
17627         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
17628         ls $DIR/$tdir/other_dir
17629
17630         # a should be migrated to MDT1, since no other links on MDT0
17631         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
17632                 error "#1 migrate dir fails"
17633         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
17634         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
17635         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17636         [ $mdt_index == 1 ] || error "a is not on MDT1"
17637
17638         # a should stay on MDT1, because it is a mulitple link file
17639         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
17640                 error "#2 migrate dir fails"
17641         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17642         [ $mdt_index == 1 ] || error "a is not on MDT1"
17643
17644         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
17645                 error "#3 migrate dir fails"
17646
17647         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
17648         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
17649         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
17650
17651         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
17652         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
17653
17654         # a should be migrated to MDT0, since no other links on MDT1
17655         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
17656                 error "#4 migrate dir fails"
17657         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17658         [ $mdt_index == 0 ] || error "a is not on MDT0"
17659
17660         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17661 }
17662 run_test 230f "migrate mulitple remote link files"
17663
17664 test_230g() {
17665         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17666         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17667         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17668                 skip "Need MDS version at least 2.11.52"
17669
17670         mkdir -p $DIR/$tdir/migrate_dir
17671
17672         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
17673                 error "migrating dir to non-exist MDT succeeds"
17674         true
17675 }
17676 run_test 230g "migrate dir to non-exist MDT"
17677
17678 test_230h() {
17679         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17680         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17681         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17682                 skip "Need MDS version at least 2.11.52"
17683
17684         local mdt_index
17685
17686         mkdir -p $DIR/$tdir/migrate_dir
17687
17688         $LFS migrate -m1 $DIR &&
17689                 error "migrating mountpoint1 should fail"
17690
17691         $LFS migrate -m1 $DIR/$tdir/.. &&
17692                 error "migrating mountpoint2 should fail"
17693
17694         # same as mv
17695         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
17696                 error "migrating $tdir/migrate_dir/.. should fail"
17697
17698         true
17699 }
17700 run_test 230h "migrate .. and root"
17701
17702 test_230i() {
17703         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17704         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17705         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17706                 skip "Need MDS version at least 2.11.52"
17707
17708         mkdir -p $DIR/$tdir/migrate_dir
17709
17710         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
17711                 error "migration fails with a tailing slash"
17712
17713         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
17714                 error "migration fails with two tailing slashes"
17715 }
17716 run_test 230i "lfs migrate -m tolerates trailing slashes"
17717
17718 test_230j() {
17719         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17720         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17721                 skip "Need MDS version at least 2.11.52"
17722
17723         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
17724         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
17725                 error "create $tfile failed"
17726         cat /etc/passwd > $DIR/$tdir/$tfile
17727
17728         $LFS migrate -m 1 $DIR/$tdir
17729
17730         cmp /etc/passwd $DIR/$tdir/$tfile ||
17731                 error "DoM file mismatch after migration"
17732 }
17733 run_test 230j "DoM file data not changed after dir migration"
17734
17735 test_230k() {
17736         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
17737         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17738                 skip "Need MDS version at least 2.11.56"
17739
17740         local total=20
17741         local files_on_starting_mdt=0
17742
17743         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
17744         $LFS getdirstripe $DIR/$tdir
17745         for i in $(seq $total); do
17746                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
17747                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
17748                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17749         done
17750
17751         echo "$files_on_starting_mdt files on MDT0"
17752
17753         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
17754         $LFS getdirstripe $DIR/$tdir
17755
17756         files_on_starting_mdt=0
17757         for i in $(seq $total); do
17758                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
17759                         error "file $tfile.$i mismatch after migration"
17760                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
17761                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17762         done
17763
17764         echo "$files_on_starting_mdt files on MDT1 after migration"
17765         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
17766
17767         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
17768         $LFS getdirstripe $DIR/$tdir
17769
17770         files_on_starting_mdt=0
17771         for i in $(seq $total); do
17772                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
17773                         error "file $tfile.$i mismatch after 2nd migration"
17774                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
17775                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17776         done
17777
17778         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
17779         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
17780
17781         true
17782 }
17783 run_test 230k "file data not changed after dir migration"
17784
17785 test_230l() {
17786         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17787         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17788                 skip "Need MDS version at least 2.11.56"
17789
17790         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
17791         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
17792                 error "create files under remote dir failed $i"
17793         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
17794 }
17795 run_test 230l "readdir between MDTs won't crash"
17796
17797 test_230m() {
17798         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17799         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17800                 skip "Need MDS version at least 2.11.56"
17801
17802         local MDTIDX=1
17803         local mig_dir=$DIR/$tdir/migrate_dir
17804         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
17805         local shortstr="b"
17806         local val
17807
17808         echo "Creating files and dirs with xattrs"
17809         test_mkdir $DIR/$tdir
17810         test_mkdir -i0 -c1 $mig_dir
17811         mkdir $mig_dir/dir
17812         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
17813                 error "cannot set xattr attr1 on dir"
17814         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
17815                 error "cannot set xattr attr2 on dir"
17816         touch $mig_dir/dir/f0
17817         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
17818                 error "cannot set xattr attr1 on file"
17819         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
17820                 error "cannot set xattr attr2 on file"
17821         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17822         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17823         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
17824         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17825         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
17826         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17827         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
17828         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17829         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
17830
17831         echo "Migrating to MDT1"
17832         $LFS migrate -m $MDTIDX $mig_dir ||
17833                 error "fails on migrating dir to MDT1"
17834
17835         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17836         echo "Checking xattrs"
17837         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17838         [ "$val" = $longstr ] ||
17839                 error "expecting xattr1 $longstr on dir, found $val"
17840         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17841         [ "$val" = $shortstr ] ||
17842                 error "expecting xattr2 $shortstr on dir, found $val"
17843         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17844         [ "$val" = $longstr ] ||
17845                 error "expecting xattr1 $longstr on file, found $val"
17846         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17847         [ "$val" = $shortstr ] ||
17848                 error "expecting xattr2 $shortstr on file, found $val"
17849 }
17850 run_test 230m "xattrs not changed after dir migration"
17851
17852 test_230n() {
17853         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17854         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
17855                 skip "Need MDS version at least 2.13.53"
17856
17857         $LFS mkdir -i 0 $DIR/$tdir || error "mkdir $tdir failed"
17858         cat /etc/hosts > $DIR/$tdir/$tfile
17859         $LFS mirror extend -N1 $DIR/$tdir/$tfile || error "Mirroring failed"
17860         $LFS migrate -m 1 $DIR/$tdir || error "Migration failed"
17861
17862         cmp /etc/hosts $DIR/$tdir/$tfile ||
17863                 error "File data mismatch after migration"
17864 }
17865 run_test 230n "Dir migration with mirrored file"
17866
17867 test_231a()
17868 {
17869         # For simplicity this test assumes that max_pages_per_rpc
17870         # is the same across all OSCs
17871         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
17872         local bulk_size=$((max_pages * PAGE_SIZE))
17873         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
17874                                        head -n 1)
17875
17876         mkdir -p $DIR/$tdir
17877         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
17878                 error "failed to set stripe with -S ${brw_size}M option"
17879
17880         # clear the OSC stats
17881         $LCTL set_param osc.*.stats=0 &>/dev/null
17882         stop_writeback
17883
17884         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
17885         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
17886                 oflag=direct &>/dev/null || error "dd failed"
17887
17888         sync; sleep 1; sync # just to be safe
17889         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
17890         if [ x$nrpcs != "x1" ]; then
17891                 $LCTL get_param osc.*.stats
17892                 error "found $nrpcs ost_write RPCs, not 1 as expected"
17893         fi
17894
17895         start_writeback
17896         # Drop the OSC cache, otherwise we will read from it
17897         cancel_lru_locks osc
17898
17899         # clear the OSC stats
17900         $LCTL set_param osc.*.stats=0 &>/dev/null
17901
17902         # Client reads $bulk_size.
17903         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
17904                 iflag=direct &>/dev/null || error "dd failed"
17905
17906         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
17907         if [ x$nrpcs != "x1" ]; then
17908                 $LCTL get_param osc.*.stats
17909                 error "found $nrpcs ost_read RPCs, not 1 as expected"
17910         fi
17911 }
17912 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
17913
17914 test_231b() {
17915         mkdir -p $DIR/$tdir
17916         local i
17917         for i in {0..1023}; do
17918                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
17919                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
17920                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
17921         done
17922         sync
17923 }
17924 run_test 231b "must not assert on fully utilized OST request buffer"
17925
17926 test_232a() {
17927         mkdir -p $DIR/$tdir
17928         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17929
17930         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17931         do_facet ost1 $LCTL set_param fail_loc=0x31c
17932
17933         # ignore dd failure
17934         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
17935
17936         do_facet ost1 $LCTL set_param fail_loc=0
17937         umount_client $MOUNT || error "umount failed"
17938         mount_client $MOUNT || error "mount failed"
17939         stop ost1 || error "cannot stop ost1"
17940         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17941 }
17942 run_test 232a "failed lock should not block umount"
17943
17944 test_232b() {
17945         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
17946                 skip "Need MDS version at least 2.10.58"
17947
17948         mkdir -p $DIR/$tdir
17949         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17950         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
17951         sync
17952         cancel_lru_locks osc
17953
17954         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17955         do_facet ost1 $LCTL set_param fail_loc=0x31c
17956
17957         # ignore failure
17958         $LFS data_version $DIR/$tdir/$tfile || true
17959
17960         do_facet ost1 $LCTL set_param fail_loc=0
17961         umount_client $MOUNT || error "umount failed"
17962         mount_client $MOUNT || error "mount failed"
17963         stop ost1 || error "cannot stop ost1"
17964         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17965 }
17966 run_test 232b "failed data version lock should not block umount"
17967
17968 test_233a() {
17969         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
17970                 skip "Need MDS version at least 2.3.64"
17971         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17972
17973         local fid=$($LFS path2fid $MOUNT)
17974
17975         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17976                 error "cannot access $MOUNT using its FID '$fid'"
17977 }
17978 run_test 233a "checking that OBF of the FS root succeeds"
17979
17980 test_233b() {
17981         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
17982                 skip "Need MDS version at least 2.5.90"
17983         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17984
17985         local fid=$($LFS path2fid $MOUNT/.lustre)
17986
17987         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17988                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
17989
17990         fid=$($LFS path2fid $MOUNT/.lustre/fid)
17991         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17992                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
17993 }
17994 run_test 233b "checking that OBF of the FS .lustre succeeds"
17995
17996 test_234() {
17997         local p="$TMP/sanityN-$TESTNAME.parameters"
17998         save_lustre_params client "llite.*.xattr_cache" > $p
17999         lctl set_param llite.*.xattr_cache 1 ||
18000                 skip_env "xattr cache is not supported"
18001
18002         mkdir -p $DIR/$tdir || error "mkdir failed"
18003         touch $DIR/$tdir/$tfile || error "touch failed"
18004         # OBD_FAIL_LLITE_XATTR_ENOMEM
18005         $LCTL set_param fail_loc=0x1405
18006         getfattr -n user.attr $DIR/$tdir/$tfile &&
18007                 error "getfattr should have failed with ENOMEM"
18008         $LCTL set_param fail_loc=0x0
18009         rm -rf $DIR/$tdir
18010
18011         restore_lustre_params < $p
18012         rm -f $p
18013 }
18014 run_test 234 "xattr cache should not crash on ENOMEM"
18015
18016 test_235() {
18017         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
18018                 skip "Need MDS version at least 2.4.52"
18019
18020         flock_deadlock $DIR/$tfile
18021         local RC=$?
18022         case $RC in
18023                 0)
18024                 ;;
18025                 124) error "process hangs on a deadlock"
18026                 ;;
18027                 *) error "error executing flock_deadlock $DIR/$tfile"
18028                 ;;
18029         esac
18030 }
18031 run_test 235 "LU-1715: flock deadlock detection does not work properly"
18032
18033 #LU-2935
18034 test_236() {
18035         check_swap_layouts_support
18036
18037         local ref1=/etc/passwd
18038         local ref2=/etc/group
18039         local file1=$DIR/$tdir/f1
18040         local file2=$DIR/$tdir/f2
18041
18042         test_mkdir -c1 $DIR/$tdir
18043         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
18044         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
18045         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
18046         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
18047         local fd=$(free_fd)
18048         local cmd="exec $fd<>$file2"
18049         eval $cmd
18050         rm $file2
18051         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
18052                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
18053         cmd="exec $fd>&-"
18054         eval $cmd
18055         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
18056
18057         #cleanup
18058         rm -rf $DIR/$tdir
18059 }
18060 run_test 236 "Layout swap on open unlinked file"
18061
18062 # LU-4659 linkea consistency
18063 test_238() {
18064         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
18065                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
18066                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
18067                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
18068
18069         touch $DIR/$tfile
18070         ln $DIR/$tfile $DIR/$tfile.lnk
18071         touch $DIR/$tfile.new
18072         mv $DIR/$tfile.new $DIR/$tfile
18073         local fid1=$($LFS path2fid $DIR/$tfile)
18074         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
18075         local path1=$($LFS fid2path $FSNAME "$fid1")
18076         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
18077         local path2=$($LFS fid2path $FSNAME "$fid2")
18078         [ $tfile.lnk == $path2 ] ||
18079                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
18080         rm -f $DIR/$tfile*
18081 }
18082 run_test 238 "Verify linkea consistency"
18083
18084 test_239A() { # was test_239
18085         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
18086                 skip "Need MDS version at least 2.5.60"
18087
18088         local list=$(comma_list $(mdts_nodes))
18089
18090         mkdir -p $DIR/$tdir
18091         createmany -o $DIR/$tdir/f- 5000
18092         unlinkmany $DIR/$tdir/f- 5000
18093         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
18094                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
18095         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
18096                         osp.*MDT*.sync_in_flight" | calc_sum)
18097         [ "$changes" -eq 0 ] || error "$changes not synced"
18098 }
18099 run_test 239A "osp_sync test"
18100
18101 test_239a() { #LU-5297
18102         remote_mds_nodsh && skip "remote MDS with nodsh"
18103
18104         touch $DIR/$tfile
18105         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
18106         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
18107         chgrp $RUNAS_GID $DIR/$tfile
18108         wait_delete_completed
18109 }
18110 run_test 239a "process invalid osp sync record correctly"
18111
18112 test_239b() { #LU-5297
18113         remote_mds_nodsh && skip "remote MDS with nodsh"
18114
18115         touch $DIR/$tfile1
18116         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
18117         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
18118         chgrp $RUNAS_GID $DIR/$tfile1
18119         wait_delete_completed
18120         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
18121         touch $DIR/$tfile2
18122         chgrp $RUNAS_GID $DIR/$tfile2
18123         wait_delete_completed
18124 }
18125 run_test 239b "process osp sync record with ENOMEM error correctly"
18126
18127 test_240() {
18128         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18129         remote_mds_nodsh && skip "remote MDS with nodsh"
18130
18131         mkdir -p $DIR/$tdir
18132
18133         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
18134                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
18135         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
18136                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
18137
18138         umount_client $MOUNT || error "umount failed"
18139         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
18140         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
18141         mount_client $MOUNT || error "failed to mount client"
18142
18143         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
18144         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
18145 }
18146 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
18147
18148 test_241_bio() {
18149         local count=$1
18150         local bsize=$2
18151
18152         for LOOP in $(seq $count); do
18153                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
18154                 cancel_lru_locks $OSC || true
18155         done
18156 }
18157
18158 test_241_dio() {
18159         local count=$1
18160         local bsize=$2
18161
18162         for LOOP in $(seq $1); do
18163                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
18164                         2>/dev/null
18165         done
18166 }
18167
18168 test_241a() { # was test_241
18169         local bsize=$PAGE_SIZE
18170
18171         (( bsize < 40960 )) && bsize=40960
18172         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
18173         ls -la $DIR/$tfile
18174         cancel_lru_locks $OSC
18175         test_241_bio 1000 $bsize &
18176         PID=$!
18177         test_241_dio 1000 $bsize
18178         wait $PID
18179 }
18180 run_test 241a "bio vs dio"
18181
18182 test_241b() {
18183         local bsize=$PAGE_SIZE
18184
18185         (( bsize < 40960 )) && bsize=40960
18186         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
18187         ls -la $DIR/$tfile
18188         test_241_dio 1000 $bsize &
18189         PID=$!
18190         test_241_dio 1000 $bsize
18191         wait $PID
18192 }
18193 run_test 241b "dio vs dio"
18194
18195 test_242() {
18196         remote_mds_nodsh && skip "remote MDS with nodsh"
18197
18198         mkdir -p $DIR/$tdir
18199         touch $DIR/$tdir/$tfile
18200
18201         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
18202         do_facet mds1 lctl set_param fail_loc=0x105
18203         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
18204
18205         do_facet mds1 lctl set_param fail_loc=0
18206         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
18207 }
18208 run_test 242 "mdt_readpage failure should not cause directory unreadable"
18209
18210 test_243()
18211 {
18212         test_mkdir $DIR/$tdir
18213         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
18214 }
18215 run_test 243 "various group lock tests"
18216
18217 test_244a()
18218 {
18219         test_mkdir $DIR/$tdir
18220         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
18221         sendfile_grouplock $DIR/$tdir/$tfile || \
18222                 error "sendfile+grouplock failed"
18223         rm -rf $DIR/$tdir
18224 }
18225 run_test 244a "sendfile with group lock tests"
18226
18227 test_244b()
18228 {
18229         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
18230
18231         local threads=50
18232         local size=$((1024*1024))
18233
18234         test_mkdir $DIR/$tdir
18235         for i in $(seq 1 $threads); do
18236                 local file=$DIR/$tdir/file_$((i / 10))
18237                 $MULTIOP $file OG1234w$size_$((i % 3))w$size_$((i % 4))g1234c &
18238                 local pids[$i]=$!
18239         done
18240         for i in $(seq 1 $threads); do
18241                 wait ${pids[$i]}
18242         done
18243 }
18244 run_test 244b "multi-threaded write with group lock"
18245
18246 test_245() {
18247         local flagname="multi_mod_rpcs"
18248         local connect_data_name="max_mod_rpcs"
18249         local out
18250
18251         # check if multiple modify RPCs flag is set
18252         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
18253                 grep "connect_flags:")
18254         echo "$out"
18255
18256         echo "$out" | grep -qw $flagname
18257         if [ $? -ne 0 ]; then
18258                 echo "connect flag $flagname is not set"
18259                 return
18260         fi
18261
18262         # check if multiple modify RPCs data is set
18263         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
18264         echo "$out"
18265
18266         echo "$out" | grep -qw $connect_data_name ||
18267                 error "import should have connect data $connect_data_name"
18268 }
18269 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
18270
18271 cleanup_247() {
18272         local submount=$1
18273
18274         trap 0
18275         umount_client $submount
18276         rmdir $submount
18277 }
18278
18279 test_247a() {
18280         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
18281                 grep -q subtree ||
18282                 skip_env "Fileset feature is not supported"
18283
18284         local submount=${MOUNT}_$tdir
18285
18286         mkdir $MOUNT/$tdir
18287         mkdir -p $submount || error "mkdir $submount failed"
18288         FILESET="$FILESET/$tdir" mount_client $submount ||
18289                 error "mount $submount failed"
18290         trap "cleanup_247 $submount" EXIT
18291         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
18292         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
18293                 error "read $MOUNT/$tdir/$tfile failed"
18294         cleanup_247 $submount
18295 }
18296 run_test 247a "mount subdir as fileset"
18297
18298 test_247b() {
18299         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
18300                 skip_env "Fileset feature is not supported"
18301
18302         local submount=${MOUNT}_$tdir
18303
18304         rm -rf $MOUNT/$tdir
18305         mkdir -p $submount || error "mkdir $submount failed"
18306         SKIP_FILESET=1
18307         FILESET="$FILESET/$tdir" mount_client $submount &&
18308                 error "mount $submount should fail"
18309         rmdir $submount
18310 }
18311 run_test 247b "mount subdir that dose not exist"
18312
18313 test_247c() {
18314         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
18315                 skip_env "Fileset feature is not supported"
18316
18317         local submount=${MOUNT}_$tdir
18318
18319         mkdir -p $MOUNT/$tdir/dir1
18320         mkdir -p $submount || error "mkdir $submount failed"
18321         trap "cleanup_247 $submount" EXIT
18322         FILESET="$FILESET/$tdir" mount_client $submount ||
18323                 error "mount $submount failed"
18324         local fid=$($LFS path2fid $MOUNT/)
18325         $LFS fid2path $submount $fid && error "fid2path should fail"
18326         cleanup_247 $submount
18327 }
18328 run_test 247c "running fid2path outside root"
18329
18330 test_247d() {
18331         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
18332                 skip "Fileset feature is not supported"
18333
18334         local submount=${MOUNT}_$tdir
18335
18336         mkdir -p $MOUNT/$tdir/dir1
18337         mkdir -p $submount || error "mkdir $submount failed"
18338         FILESET="$FILESET/$tdir" mount_client $submount ||
18339                 error "mount $submount failed"
18340         trap "cleanup_247 $submount" EXIT
18341         local fid=$($LFS path2fid $submount/dir1)
18342         $LFS fid2path $submount $fid || error "fid2path should succeed"
18343         cleanup_247 $submount
18344 }
18345 run_test 247d "running fid2path inside root"
18346
18347 # LU-8037
18348 test_247e() {
18349         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
18350                 grep -q subtree ||
18351                 skip "Fileset feature is not supported"
18352
18353         local submount=${MOUNT}_$tdir
18354
18355         mkdir $MOUNT/$tdir
18356         mkdir -p $submount || error "mkdir $submount failed"
18357         FILESET="$FILESET/.." mount_client $submount &&
18358                 error "mount $submount should fail"
18359         rmdir $submount
18360 }
18361 run_test 247e "mount .. as fileset"
18362
18363 test_247f() {
18364         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18365         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
18366                 skip "Need at least version 2.13.52"
18367         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
18368                 grep -q subtree ||
18369                 skip "Fileset feature is not supported"
18370
18371         mkdir $DIR/$tdir || error "mkdir $tdir failed"
18372         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote ||
18373                 error "mkdir remote failed"
18374         mkdir $DIR/$tdir/remote/subdir || error "mkdir remote/subdir failed"
18375         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/striped ||
18376                 error "mkdir striped failed"
18377         mkdir $DIR/$tdir/striped/subdir || error "mkdir striped/subdir failed"
18378
18379         local submount=${MOUNT}_$tdir
18380
18381         mkdir -p $submount || error "mkdir $submount failed"
18382
18383         local dir
18384         local fileset=$FILESET
18385
18386         for dir in $tdir/remote $tdir/remote/subdir \
18387                    $tdir/striped $tdir/striped/subdir $tdir/striped/. ; do
18388                 FILESET="$fileset/$dir" mount_client $submount ||
18389                         error "mount $dir failed"
18390                 umount_client $submount
18391         done
18392 }
18393 run_test 247f "mount striped or remote directory as fileset"
18394
18395 test_248a() {
18396         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
18397         [ -z "$fast_read_sav" ] && skip "no fast read support"
18398
18399         # create a large file for fast read verification
18400         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
18401
18402         # make sure the file is created correctly
18403         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
18404                 { rm -f $DIR/$tfile; skip "file creation error"; }
18405
18406         echo "Test 1: verify that fast read is 4 times faster on cache read"
18407
18408         # small read with fast read enabled
18409         $LCTL set_param -n llite.*.fast_read=1
18410         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
18411                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
18412                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
18413         # small read with fast read disabled
18414         $LCTL set_param -n llite.*.fast_read=0
18415         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
18416                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
18417                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
18418
18419         # verify that fast read is 4 times faster for cache read
18420         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
18421                 error_not_in_vm "fast read was not 4 times faster: " \
18422                            "$t_fast vs $t_slow"
18423
18424         echo "Test 2: verify the performance between big and small read"
18425         $LCTL set_param -n llite.*.fast_read=1
18426
18427         # 1k non-cache read
18428         cancel_lru_locks osc
18429         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
18430                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
18431                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
18432
18433         # 1M non-cache read
18434         cancel_lru_locks osc
18435         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
18436                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
18437                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
18438
18439         # verify that big IO is not 4 times faster than small IO
18440         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
18441                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
18442
18443         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
18444         rm -f $DIR/$tfile
18445 }
18446 run_test 248a "fast read verification"
18447
18448 test_248b() {
18449         # Default short_io_bytes=16384, try both smaller and larger sizes.
18450         # Lustre O_DIRECT read and write needs to be a multiple of PAGE_SIZE.
18451         # 6017024 = 2^12*13*113 = 47008*128 = 11752*512 = 4096*1469 = 53248*113
18452         echo "bs=53248 count=113 normal buffered write"
18453         dd if=/dev/urandom of=$TMP/$tfile.0 bs=53248 count=113 ||
18454                 error "dd of initial data file failed"
18455         stack_trap "rm -f $DIR/$tfile.[0-3] $TMP/$tfile.[0-3]" EXIT
18456
18457         echo "bs=47008 count=128 oflag=dsync normal write $tfile.0"
18458         dd if=$TMP/$tfile.0 of=$DIR/$tfile.0 bs=47008 count=128 oflag=dsync ||
18459                 error "dd with sync normal writes failed"
18460         cmp $TMP/$tfile.0 $DIR/$tfile.0 || error "compare $DIR/$tfile.0 failed"
18461
18462         echo "bs=11752 count=512 oflag=dsync small write $tfile.1"
18463         dd if=$TMP/$tfile.0 of=$DIR/$tfile.1 bs=11752 count=512 oflag=dsync ||
18464                 error "dd with sync small writes failed"
18465         cmp $TMP/$tfile.0 $DIR/$tfile.1 || error "compare $DIR/$tfile.1 failed"
18466
18467         cancel_lru_locks osc
18468
18469         # calculate the small O_DIRECT size and count for the client PAGE_SIZE
18470         local num=$((13 * 113 / (PAGE_SIZE / 4096)))
18471         echo "bs=$PAGE_SIZE count=$num iflag=direct small read $tfile.1"
18472         dd if=$DIR/$tfile.1 of=$TMP/$tfile.1 bs=$PAGE_SIZE count=$num \
18473                 iflag=direct || error "dd with O_DIRECT small read failed"
18474         # adjust bytes checked to handle larger PAGE_SIZE for ARM/PPC
18475         cmp --bytes=$((PAGE_SIZE * num)) $TMP/$tfile.0 $TMP/$tfile.1 ||
18476                 error "compare $TMP/$tfile.1 failed"
18477
18478         local save=$($LCTL get_param -n osc.*OST000*.short_io_bytes | head -n 1)
18479         stack_trap "$LCTL set_param osc.$FSNAME-*.short_io_bytes=$save" EXIT
18480
18481         # just to see what the maximum tunable value is, and test parsing
18482         echo "test invalid parameter 2MB"
18483         $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=2M &&
18484                 error "too-large short_io_bytes allowed"
18485         echo "test maximum parameter 512KB"
18486         # if we can set a larger short_io_bytes, run test regardless of version
18487         if ! $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=512K; then
18488                 # older clients may not allow setting it this large, that's OK
18489                 [ $CLIENT_VERSION -ge $(version_code 2.13.50) ] ||
18490                         skip "Need at least client version 2.13.50"
18491                 error "medium short_io_bytes failed"
18492         fi
18493         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
18494         size=$($LCTL get_param -n osc.$FSNAME-OST0000*.short_io_bytes)
18495
18496         echo "test large parameter 64KB"
18497         $LCTL set_param osc.$FSNAME-*.short_io_bytes=65536
18498         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
18499
18500         echo "bs=47008 count=128 oflag=dsync large write $tfile.2"
18501         dd if=$TMP/$tfile.0 of=$DIR/$tfile.2 bs=47008 count=128 oflag=dsync ||
18502                 error "dd with sync large writes failed"
18503         cmp $TMP/$tfile.0 $DIR/$tfile.2 || error "compare $DIR/$tfile.2 failed"
18504
18505         # calculate the large O_DIRECT size and count for the client PAGE_SIZE
18506         local size=$(((4096 * 13 + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE))
18507         num=$((113 * 4096 / PAGE_SIZE))
18508         echo "bs=$size count=$num oflag=direct large write $tfile.3"
18509         dd if=$TMP/$tfile.0 of=$DIR/$tfile.3 bs=$size count=$num oflag=direct ||
18510                 error "dd with O_DIRECT large writes failed"
18511         cmp --bytes=$((size * num)) $TMP/$tfile.0 $DIR/$tfile.3 ||
18512                 error "compare $DIR/$tfile.3 failed"
18513
18514         cancel_lru_locks osc
18515
18516         echo "bs=$size count=$num iflag=direct large read $tfile.2"
18517         dd if=$DIR/$tfile.2 of=$TMP/$tfile.2 bs=$size count=$num iflag=direct ||
18518                 error "dd with O_DIRECT large read failed"
18519         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.2 ||
18520                 error "compare $TMP/$tfile.2 failed"
18521
18522         echo "bs=$size count=$num iflag=direct large read $tfile.3"
18523         dd if=$DIR/$tfile.3 of=$TMP/$tfile.3 bs=$size count=$num iflag=direct ||
18524                 error "dd with O_DIRECT large read failed"
18525         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.3 ||
18526                 error "compare $TMP/$tfile.3 failed"
18527 }
18528 run_test 248b "test short_io read and write for both small and large sizes"
18529
18530 test_249() { # LU-7890
18531         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
18532                 skip "Need at least version 2.8.54"
18533
18534         rm -f $DIR/$tfile
18535         $LFS setstripe -c 1 $DIR/$tfile
18536         # Offset 2T == 4k * 512M
18537         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
18538                 error "dd to 2T offset failed"
18539 }
18540 run_test 249 "Write above 2T file size"
18541
18542 test_250() {
18543         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
18544          && skip "no 16TB file size limit on ZFS"
18545
18546         $LFS setstripe -c 1 $DIR/$tfile
18547         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
18548         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
18549         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
18550         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
18551                 conv=notrunc,fsync && error "append succeeded"
18552         return 0
18553 }
18554 run_test 250 "Write above 16T limit"
18555
18556 test_251() {
18557         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
18558
18559         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
18560         #Skip once - writing the first stripe will succeed
18561         $LCTL set_param fail_loc=0xa0001407 fail_val=1
18562         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
18563                 error "short write happened"
18564
18565         $LCTL set_param fail_loc=0xa0001407 fail_val=1
18566         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
18567                 error "short read happened"
18568
18569         rm -f $DIR/$tfile
18570 }
18571 run_test 251 "Handling short read and write correctly"
18572
18573 test_252() {
18574         remote_mds_nodsh && skip "remote MDS with nodsh"
18575         remote_ost_nodsh && skip "remote OST with nodsh"
18576         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
18577                 skip_env "ldiskfs only test"
18578         fi
18579
18580         local tgt
18581         local dev
18582         local out
18583         local uuid
18584         local num
18585         local gen
18586
18587         # check lr_reader on OST0000
18588         tgt=ost1
18589         dev=$(facet_device $tgt)
18590         out=$(do_facet $tgt $LR_READER $dev)
18591         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
18592         echo "$out"
18593         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
18594         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
18595                 error "Invalid uuid returned by $LR_READER on target $tgt"
18596         echo -e "uuid returned by $LR_READER is '$uuid'\n"
18597
18598         # check lr_reader -c on MDT0000
18599         tgt=mds1
18600         dev=$(facet_device $tgt)
18601         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
18602                 skip "$LR_READER does not support additional options"
18603         fi
18604         out=$(do_facet $tgt $LR_READER -c $dev)
18605         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
18606         echo "$out"
18607         num=$(echo "$out" | grep -c "mdtlov")
18608         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
18609                 error "Invalid number of mdtlov clients returned by $LR_READER"
18610         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
18611
18612         # check lr_reader -cr on MDT0000
18613         out=$(do_facet $tgt $LR_READER -cr $dev)
18614         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
18615         echo "$out"
18616         echo "$out" | grep -q "^reply_data:$" ||
18617                 error "$LR_READER should have returned 'reply_data' section"
18618         num=$(echo "$out" | grep -c "client_generation")
18619         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
18620 }
18621 run_test 252 "check lr_reader tool"
18622
18623 test_253() {
18624         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18625         remote_mds_nodsh && skip "remote MDS with nodsh"
18626         remote_mgs_nodsh && skip "remote MGS with nodsh"
18627
18628         local ostidx=0
18629         local rc=0
18630         local ost_name=$(ostname_from_index $ostidx)
18631
18632         # on the mdt's osc
18633         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
18634         do_facet $SINGLEMDS $LCTL get_param -n \
18635                 osp.$mdtosc_proc1.reserved_mb_high ||
18636                 skip  "remote MDS does not support reserved_mb_high"
18637
18638         rm -rf $DIR/$tdir
18639         wait_mds_ost_sync
18640         wait_delete_completed
18641         mkdir $DIR/$tdir
18642
18643         pool_add $TESTNAME || error "Pool creation failed"
18644         pool_add_targets $TESTNAME 0 || error "Pool add targets failed"
18645
18646         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
18647                 error "Setstripe failed"
18648
18649         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M count=10
18650
18651         local wms=$(ost_watermarks_set_enospc $tfile $ostidx |
18652                     grep "watermarks")
18653         stack_trap "ost_watermarks_clear_enospc $tfile $ostidx $wms" EXIT
18654
18655         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
18656                         osp.$mdtosc_proc1.prealloc_status)
18657         echo "prealloc_status $oa_status"
18658
18659         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1M count=1 &&
18660                 error "File creation should fail"
18661
18662         #object allocation was stopped, but we still able to append files
18663         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M seek=6 count=5 \
18664                 oflag=append || error "Append failed"
18665
18666         rm -f $DIR/$tdir/$tfile.0
18667
18668         # For this test, we want to delete the files we created to go out of
18669         # space but leave the watermark, so we remain nearly out of space
18670         ost_watermarks_enospc_delete_files $tfile $ostidx
18671
18672         wait_delete_completed
18673
18674         sleep_maxage
18675
18676         for i in $(seq 10 12); do
18677                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$i bs=1M count=1 \
18678                         2>/dev/null || error "File creation failed after rm"
18679         done
18680
18681         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
18682                         osp.$mdtosc_proc1.prealloc_status)
18683         echo "prealloc_status $oa_status"
18684
18685         if (( oa_status != 0 )); then
18686                 error "Object allocation still disable after rm"
18687         fi
18688 }
18689 run_test 253 "Check object allocation limit"
18690
18691 test_254() {
18692         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18693         remote_mds_nodsh && skip "remote MDS with nodsh"
18694         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
18695                 skip "MDS does not support changelog_size"
18696
18697         local cl_user
18698         local MDT0=$(facet_svc $SINGLEMDS)
18699
18700         changelog_register || error "changelog_register failed"
18701
18702         changelog_clear 0 || error "changelog_clear failed"
18703
18704         local size1=$(do_facet $SINGLEMDS \
18705                       $LCTL get_param -n mdd.$MDT0.changelog_size)
18706         echo "Changelog size $size1"
18707
18708         rm -rf $DIR/$tdir
18709         $LFS mkdir -i 0 $DIR/$tdir
18710         # change something
18711         mkdir -p $DIR/$tdir/pics/2008/zachy
18712         touch $DIR/$tdir/pics/2008/zachy/timestamp
18713         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
18714         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
18715         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
18716         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
18717         rm $DIR/$tdir/pics/desktop.jpg
18718
18719         local size2=$(do_facet $SINGLEMDS \
18720                       $LCTL get_param -n mdd.$MDT0.changelog_size)
18721         echo "Changelog size after work $size2"
18722
18723         (( $size2 > $size1 )) ||
18724                 error "new Changelog size=$size2 less than old size=$size1"
18725 }
18726 run_test 254 "Check changelog size"
18727
18728 ladvise_no_type()
18729 {
18730         local type=$1
18731         local file=$2
18732
18733         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
18734                 awk -F: '{print $2}' | grep $type > /dev/null
18735         if [ $? -ne 0 ]; then
18736                 return 0
18737         fi
18738         return 1
18739 }
18740
18741 ladvise_no_ioctl()
18742 {
18743         local file=$1
18744
18745         lfs ladvise -a willread $file > /dev/null 2>&1
18746         if [ $? -eq 0 ]; then
18747                 return 1
18748         fi
18749
18750         lfs ladvise -a willread $file 2>&1 |
18751                 grep "Inappropriate ioctl for device" > /dev/null
18752         if [ $? -eq 0 ]; then
18753                 return 0
18754         fi
18755         return 1
18756 }
18757
18758 percent() {
18759         bc <<<"scale=2; ($1 - $2) * 100 / $2"
18760 }
18761
18762 # run a random read IO workload
18763 # usage: random_read_iops <filename> <filesize> <iosize>
18764 random_read_iops() {
18765         local file=$1
18766         local fsize=$2
18767         local iosize=${3:-4096}
18768
18769         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
18770                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
18771 }
18772
18773 drop_file_oss_cache() {
18774         local file="$1"
18775         local nodes="$2"
18776
18777         $LFS ladvise -a dontneed $file 2>/dev/null ||
18778                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
18779 }
18780
18781 ladvise_willread_performance()
18782 {
18783         local repeat=10
18784         local average_origin=0
18785         local average_cache=0
18786         local average_ladvise=0
18787
18788         for ((i = 1; i <= $repeat; i++)); do
18789                 echo "Iter $i/$repeat: reading without willread hint"
18790                 cancel_lru_locks osc
18791                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
18792                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
18793                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
18794                 average_origin=$(bc <<<"$average_origin + $speed_origin")
18795
18796                 cancel_lru_locks osc
18797                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
18798                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
18799                 average_cache=$(bc <<<"$average_cache + $speed_cache")
18800
18801                 cancel_lru_locks osc
18802                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
18803                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
18804                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
18805                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
18806                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
18807         done
18808         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
18809         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
18810         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
18811
18812         speedup_cache=$(percent $average_cache $average_origin)
18813         speedup_ladvise=$(percent $average_ladvise $average_origin)
18814
18815         echo "Average uncached read: $average_origin"
18816         echo "Average speedup with OSS cached read: " \
18817                 "$average_cache = +$speedup_cache%"
18818         echo "Average speedup with ladvise willread: " \
18819                 "$average_ladvise = +$speedup_ladvise%"
18820
18821         local lowest_speedup=20
18822         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
18823                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
18824                         "got $average_cache%. Skipping ladvise willread check."
18825                 return 0
18826         fi
18827
18828         # the test won't work on ZFS until it supports 'ladvise dontneed', but
18829         # it is still good to run until then to exercise 'ladvise willread'
18830         ! $LFS ladvise -a dontneed $DIR/$tfile &&
18831                 [ "$ost1_FSTYPE" = "zfs" ] &&
18832                 echo "osd-zfs does not support dontneed or drop_caches" &&
18833                 return 0
18834
18835         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
18836         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
18837                 error_not_in_vm "Speedup with willread is less than " \
18838                         "$lowest_speedup%, got $average_ladvise%"
18839 }
18840
18841 test_255a() {
18842         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
18843                 skip "lustre < 2.8.54 does not support ladvise "
18844         remote_ost_nodsh && skip "remote OST with nodsh"
18845
18846         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
18847
18848         ladvise_no_type willread $DIR/$tfile &&
18849                 skip "willread ladvise is not supported"
18850
18851         ladvise_no_ioctl $DIR/$tfile &&
18852                 skip "ladvise ioctl is not supported"
18853
18854         local size_mb=100
18855         local size=$((size_mb * 1048576))
18856         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
18857                 error "dd to $DIR/$tfile failed"
18858
18859         lfs ladvise -a willread $DIR/$tfile ||
18860                 error "Ladvise failed with no range argument"
18861
18862         lfs ladvise -a willread -s 0 $DIR/$tfile ||
18863                 error "Ladvise failed with no -l or -e argument"
18864
18865         lfs ladvise -a willread -e 1 $DIR/$tfile ||
18866                 error "Ladvise failed with only -e argument"
18867
18868         lfs ladvise -a willread -l 1 $DIR/$tfile ||
18869                 error "Ladvise failed with only -l argument"
18870
18871         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
18872                 error "End offset should not be smaller than start offset"
18873
18874         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
18875                 error "End offset should not be equal to start offset"
18876
18877         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
18878                 error "Ladvise failed with overflowing -s argument"
18879
18880         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
18881                 error "Ladvise failed with overflowing -e argument"
18882
18883         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
18884                 error "Ladvise failed with overflowing -l argument"
18885
18886         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
18887                 error "Ladvise succeeded with conflicting -l and -e arguments"
18888
18889         echo "Synchronous ladvise should wait"
18890         local delay=4
18891 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
18892         do_nodes $(comma_list $(osts_nodes)) \
18893                 $LCTL set_param fail_val=$delay fail_loc=0x237
18894
18895         local start_ts=$SECONDS
18896         lfs ladvise -a willread $DIR/$tfile ||
18897                 error "Ladvise failed with no range argument"
18898         local end_ts=$SECONDS
18899         local inteval_ts=$((end_ts - start_ts))
18900
18901         if [ $inteval_ts -lt $(($delay - 1)) ]; then
18902                 error "Synchronous advice didn't wait reply"
18903         fi
18904
18905         echo "Asynchronous ladvise shouldn't wait"
18906         local start_ts=$SECONDS
18907         lfs ladvise -a willread -b $DIR/$tfile ||
18908                 error "Ladvise failed with no range argument"
18909         local end_ts=$SECONDS
18910         local inteval_ts=$((end_ts - start_ts))
18911
18912         if [ $inteval_ts -gt $(($delay / 2)) ]; then
18913                 error "Asynchronous advice blocked"
18914         fi
18915
18916         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
18917         ladvise_willread_performance
18918 }
18919 run_test 255a "check 'lfs ladvise -a willread'"
18920
18921 facet_meminfo() {
18922         local facet=$1
18923         local info=$2
18924
18925         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
18926 }
18927
18928 test_255b() {
18929         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
18930                 skip "lustre < 2.8.54 does not support ladvise "
18931         remote_ost_nodsh && skip "remote OST with nodsh"
18932
18933         lfs setstripe -c 1 -i 0 $DIR/$tfile
18934
18935         ladvise_no_type dontneed $DIR/$tfile &&
18936                 skip "dontneed ladvise is not supported"
18937
18938         ladvise_no_ioctl $DIR/$tfile &&
18939                 skip "ladvise ioctl is not supported"
18940
18941         ! $LFS ladvise -a dontneed $DIR/$tfile &&
18942                 [ "$ost1_FSTYPE" = "zfs" ] &&
18943                 skip "zfs-osd does not support 'ladvise dontneed'"
18944
18945         local size_mb=100
18946         local size=$((size_mb * 1048576))
18947         # In order to prevent disturbance of other processes, only check 3/4
18948         # of the memory usage
18949         local kibibytes=$((size_mb * 1024 * 3 / 4))
18950
18951         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
18952                 error "dd to $DIR/$tfile failed"
18953
18954         #force write to complete before dropping OST cache & checking memory
18955         sync
18956
18957         local total=$(facet_meminfo ost1 MemTotal)
18958         echo "Total memory: $total KiB"
18959
18960         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
18961         local before_read=$(facet_meminfo ost1 Cached)
18962         echo "Cache used before read: $before_read KiB"
18963
18964         lfs ladvise -a willread $DIR/$tfile ||
18965                 error "Ladvise willread failed"
18966         local after_read=$(facet_meminfo ost1 Cached)
18967         echo "Cache used after read: $after_read KiB"
18968
18969         lfs ladvise -a dontneed $DIR/$tfile ||
18970                 error "Ladvise dontneed again failed"
18971         local no_read=$(facet_meminfo ost1 Cached)
18972         echo "Cache used after dontneed ladvise: $no_read KiB"
18973
18974         if [ $total -lt $((before_read + kibibytes)) ]; then
18975                 echo "Memory is too small, abort checking"
18976                 return 0
18977         fi
18978
18979         if [ $((before_read + kibibytes)) -gt $after_read ]; then
18980                 error "Ladvise willread should use more memory" \
18981                         "than $kibibytes KiB"
18982         fi
18983
18984         if [ $((no_read + kibibytes)) -gt $after_read ]; then
18985                 error "Ladvise dontneed should release more memory" \
18986                         "than $kibibytes KiB"
18987         fi
18988 }
18989 run_test 255b "check 'lfs ladvise -a dontneed'"
18990
18991 test_255c() {
18992         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
18993                 skip "lustre < 2.10.50 does not support lockahead"
18994
18995         local count
18996         local new_count
18997         local difference
18998         local i
18999         local rc
19000
19001         test_mkdir -p $DIR/$tdir
19002         $LFS setstripe -i 0 -c 1 $DIR/$tdir
19003
19004         #test 10 returns only success/failure
19005         i=10
19006         lockahead_test -d $DIR/$tdir -t $i -f $tfile
19007         rc=$?
19008         if [ $rc -eq 255 ]; then
19009                 error "Ladvise test${i} failed, ${rc}"
19010         fi
19011
19012         #test 11 counts lock enqueue requests, all others count new locks
19013         i=11
19014         count=$(do_facet ost1 \
19015                 $LCTL get_param -n ost.OSS.ost.stats)
19016         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
19017
19018         lockahead_test -d $DIR/$tdir -t $i -f $tfile
19019         rc=$?
19020         if [ $rc -eq 255 ]; then
19021                 error "Ladvise test${i} failed, ${rc}"
19022         fi
19023
19024         new_count=$(do_facet ost1 \
19025                 $LCTL get_param -n ost.OSS.ost.stats)
19026         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
19027                    awk '{ print $2 }')
19028
19029         difference="$((new_count - count))"
19030         if [ $difference -ne $rc ]; then
19031                 error "Ladvise test${i}, bad enqueue count, returned " \
19032                       "${rc}, actual ${difference}"
19033         fi
19034
19035         for i in $(seq 12 21); do
19036                 # If we do not do this, we run the risk of having too many
19037                 # locks and starting lock cancellation while we are checking
19038                 # lock counts.
19039                 cancel_lru_locks osc
19040
19041                 count=$($LCTL get_param -n \
19042                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
19043
19044                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
19045                 rc=$?
19046                 if [ $rc -eq 255 ]; then
19047                         error "Ladvise test ${i} failed, ${rc}"
19048                 fi
19049
19050                 new_count=$($LCTL get_param -n \
19051                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
19052                 difference="$((new_count - count))"
19053
19054                 # Test 15 output is divided by 100 to map down to valid return
19055                 if [ $i -eq 15 ]; then
19056                         rc="$((rc * 100))"
19057                 fi
19058
19059                 if [ $difference -ne $rc ]; then
19060                         error "Ladvise test ${i}, bad lock count, returned " \
19061                               "${rc}, actual ${difference}"
19062                 fi
19063         done
19064
19065         #test 22 returns only success/failure
19066         i=22
19067         lockahead_test -d $DIR/$tdir -t $i -f $tfile
19068         rc=$?
19069         if [ $rc -eq 255 ]; then
19070                 error "Ladvise test${i} failed, ${rc}"
19071         fi
19072 }
19073 run_test 255c "suite of ladvise lockahead tests"
19074
19075 test_256() {
19076         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19077         remote_mds_nodsh && skip "remote MDS with nodsh"
19078         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
19079         changelog_users $SINGLEMDS | grep "^cl" &&
19080                 skip "active changelog user"
19081
19082         local cl_user
19083         local cat_sl
19084         local mdt_dev
19085
19086         mdt_dev=$(mdsdevname 1)
19087         echo $mdt_dev
19088
19089         changelog_register || error "changelog_register failed"
19090
19091         rm -rf $DIR/$tdir
19092         mkdir -p $DIR/$tdir
19093
19094         changelog_clear 0 || error "changelog_clear failed"
19095
19096         # change something
19097         touch $DIR/$tdir/{1..10}
19098
19099         # stop the MDT
19100         stop $SINGLEMDS || error "Fail to stop MDT"
19101
19102         # remount the MDT
19103
19104         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
19105
19106         #after mount new plainllog is used
19107         touch $DIR/$tdir/{11..19}
19108         local tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
19109         stack_trap "rm -f $tmpfile"
19110         cat_sl=$(do_facet $SINGLEMDS "sync; \
19111                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
19112                  llog_reader $tmpfile | grep -c type=1064553b")
19113         do_facet $SINGLEMDS llog_reader $tmpfile
19114
19115         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
19116
19117         changelog_clear 0 || error "changelog_clear failed"
19118
19119         cat_sl=$(do_facet $SINGLEMDS "sync; \
19120                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
19121                  llog_reader $tmpfile | grep -c type=1064553b")
19122
19123         if (( cat_sl == 2 )); then
19124                 error "Empty plain llog was not deleted from changelog catalog"
19125         elif (( cat_sl != 1 )); then
19126                 error "Active plain llog shouldn't be deleted from catalog"
19127         fi
19128 }
19129 run_test 256 "Check llog delete for empty and not full state"
19130
19131 test_257() {
19132         remote_mds_nodsh && skip "remote MDS with nodsh"
19133         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
19134                 skip "Need MDS version at least 2.8.55"
19135
19136         test_mkdir $DIR/$tdir
19137
19138         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
19139                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
19140         stat $DIR/$tdir
19141
19142 #define OBD_FAIL_MDS_XATTR_REP                  0x161
19143         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
19144         local facet=mds$((mdtidx + 1))
19145         set_nodes_failloc $(facet_active_host $facet) 0x80000161
19146         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
19147
19148         stop $facet || error "stop MDS failed"
19149         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
19150                 error "start MDS fail"
19151         wait_recovery_complete $facet
19152 }
19153 run_test 257 "xattr locks are not lost"
19154
19155 # Verify we take the i_mutex when security requires it
19156 test_258a() {
19157 #define OBD_FAIL_IMUTEX_SEC 0x141c
19158         $LCTL set_param fail_loc=0x141c
19159         touch $DIR/$tfile
19160         chmod u+s $DIR/$tfile
19161         chmod a+rwx $DIR/$tfile
19162         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
19163         RC=$?
19164         if [ $RC -ne 0 ]; then
19165                 error "error, failed to take i_mutex, rc=$?"
19166         fi
19167         rm -f $DIR/$tfile
19168 }
19169 run_test 258a "verify i_mutex security behavior when suid attributes is set"
19170
19171 # Verify we do NOT take the i_mutex in the normal case
19172 test_258b() {
19173 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
19174         $LCTL set_param fail_loc=0x141d
19175         touch $DIR/$tfile
19176         chmod a+rwx $DIR
19177         chmod a+rw $DIR/$tfile
19178         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
19179         RC=$?
19180         if [ $RC -ne 0 ]; then
19181                 error "error, took i_mutex unnecessarily, rc=$?"
19182         fi
19183         rm -f $DIR/$tfile
19184
19185 }
19186 run_test 258b "verify i_mutex security behavior"
19187
19188 test_259() {
19189         local file=$DIR/$tfile
19190         local before
19191         local after
19192
19193         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
19194
19195         stack_trap "rm -f $file" EXIT
19196
19197         wait_delete_completed
19198         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
19199         echo "before: $before"
19200
19201         $LFS setstripe -i 0 -c 1 $file
19202         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
19203         sync_all_data
19204         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
19205         echo "after write: $after"
19206
19207 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
19208         do_facet ost1 $LCTL set_param fail_loc=0x2301
19209         $TRUNCATE $file 0
19210         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
19211         echo "after truncate: $after"
19212
19213         stop ost1
19214         do_facet ost1 $LCTL set_param fail_loc=0
19215         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
19216         sleep 2
19217         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
19218         echo "after restart: $after"
19219         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
19220                 error "missing truncate?"
19221
19222         return 0
19223 }
19224 run_test 259 "crash at delayed truncate"
19225
19226 test_260() {
19227 #define OBD_FAIL_MDC_CLOSE               0x806
19228         $LCTL set_param fail_loc=0x80000806
19229         touch $DIR/$tfile
19230
19231 }
19232 run_test 260 "Check mdc_close fail"
19233
19234 ### Data-on-MDT sanity tests ###
19235 test_270a() {
19236         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19237                 skip "Need MDS version at least 2.10.55 for DoM"
19238
19239         # create DoM file
19240         local dom=$DIR/$tdir/dom_file
19241         local tmp=$DIR/$tdir/tmp_file
19242
19243         mkdir -p $DIR/$tdir
19244
19245         # basic checks for DoM component creation
19246         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
19247                 error "Can set MDT layout to non-first entry"
19248
19249         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
19250                 error "Can define multiple entries as MDT layout"
19251
19252         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
19253
19254         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
19255         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
19256         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
19257
19258         local mdtidx=$($LFS getstripe -m $dom)
19259         local mdtname=MDT$(printf %04x $mdtidx)
19260         local facet=mds$((mdtidx + 1))
19261         local space_check=1
19262
19263         # Skip free space checks with ZFS
19264         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
19265
19266         # write
19267         sync
19268         local size_tmp=$((65536 * 3))
19269         local mdtfree1=$(do_facet $facet \
19270                          lctl get_param -n osd*.*$mdtname.kbytesfree)
19271
19272         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
19273         # check also direct IO along write
19274         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
19275         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
19276         sync
19277         cmp $tmp $dom || error "file data is different"
19278         [ $(stat -c%s $dom) == $size_tmp ] ||
19279                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
19280         if [ $space_check == 1 ]; then
19281                 local mdtfree2=$(do_facet $facet \
19282                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
19283
19284                 # increase in usage from by $size_tmp
19285                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
19286                         error "MDT free space wrong after write: " \
19287                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
19288         fi
19289
19290         # truncate
19291         local size_dom=10000
19292
19293         $TRUNCATE $dom $size_dom
19294         [ $(stat -c%s $dom) == $size_dom ] ||
19295                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
19296         if [ $space_check == 1 ]; then
19297                 mdtfree1=$(do_facet $facet \
19298                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19299                 # decrease in usage from $size_tmp to new $size_dom
19300                 [ $(($mdtfree1 - $mdtfree2)) -ge \
19301                   $(((size_tmp - size_dom) / 1024)) ] ||
19302                         error "MDT free space is wrong after truncate: " \
19303                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
19304         fi
19305
19306         # append
19307         cat $tmp >> $dom
19308         sync
19309         size_dom=$((size_dom + size_tmp))
19310         [ $(stat -c%s $dom) == $size_dom ] ||
19311                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
19312         if [ $space_check == 1 ]; then
19313                 mdtfree2=$(do_facet $facet \
19314                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19315                 # increase in usage by $size_tmp from previous
19316                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
19317                         error "MDT free space is wrong after append: " \
19318                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
19319         fi
19320
19321         # delete
19322         rm $dom
19323         if [ $space_check == 1 ]; then
19324                 mdtfree1=$(do_facet $facet \
19325                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19326                 # decrease in usage by $size_dom from previous
19327                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
19328                         error "MDT free space is wrong after removal: " \
19329                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
19330         fi
19331
19332         # combined striping
19333         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
19334                 error "Can't create DoM + OST striping"
19335
19336         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
19337         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
19338         # check also direct IO along write
19339         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
19340         sync
19341         cmp $tmp $dom || error "file data is different"
19342         [ $(stat -c%s $dom) == $size_tmp ] ||
19343                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
19344         rm $dom $tmp
19345
19346         return 0
19347 }
19348 run_test 270a "DoM: basic functionality tests"
19349
19350 test_270b() {
19351         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19352                 skip "Need MDS version at least 2.10.55"
19353
19354         local dom=$DIR/$tdir/dom_file
19355         local max_size=1048576
19356
19357         mkdir -p $DIR/$tdir
19358         $LFS setstripe -E $max_size -L mdt $dom
19359
19360         # truncate over the limit
19361         $TRUNCATE $dom $(($max_size + 1)) &&
19362                 error "successful truncate over the maximum size"
19363         # write over the limit
19364         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
19365                 error "successful write over the maximum size"
19366         # append over the limit
19367         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
19368         echo "12345" >> $dom && error "successful append over the maximum size"
19369         rm $dom
19370
19371         return 0
19372 }
19373 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
19374
19375 test_270c() {
19376         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19377                 skip "Need MDS version at least 2.10.55"
19378
19379         mkdir -p $DIR/$tdir
19380         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19381
19382         # check files inherit DoM EA
19383         touch $DIR/$tdir/first
19384         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
19385                 error "bad pattern"
19386         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
19387                 error "bad stripe count"
19388         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
19389                 error "bad stripe size"
19390
19391         # check directory inherits DoM EA and uses it as default
19392         mkdir $DIR/$tdir/subdir
19393         touch $DIR/$tdir/subdir/second
19394         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
19395                 error "bad pattern in sub-directory"
19396         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
19397                 error "bad stripe count in sub-directory"
19398         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
19399                 error "bad stripe size in sub-directory"
19400         return 0
19401 }
19402 run_test 270c "DoM: DoM EA inheritance tests"
19403
19404 test_270d() {
19405         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19406                 skip "Need MDS version at least 2.10.55"
19407
19408         mkdir -p $DIR/$tdir
19409         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19410
19411         # inherit default DoM striping
19412         mkdir $DIR/$tdir/subdir
19413         touch $DIR/$tdir/subdir/f1
19414
19415         # change default directory striping
19416         $LFS setstripe -c 1 $DIR/$tdir/subdir
19417         touch $DIR/$tdir/subdir/f2
19418         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
19419                 error "wrong default striping in file 2"
19420         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
19421                 error "bad pattern in file 2"
19422         return 0
19423 }
19424 run_test 270d "DoM: change striping from DoM to RAID0"
19425
19426 test_270e() {
19427         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19428                 skip "Need MDS version at least 2.10.55"
19429
19430         mkdir -p $DIR/$tdir/dom
19431         mkdir -p $DIR/$tdir/norm
19432         DOMFILES=20
19433         NORMFILES=10
19434         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
19435         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
19436
19437         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
19438         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
19439
19440         # find DoM files by layout
19441         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
19442         [ $NUM -eq  $DOMFILES ] ||
19443                 error "lfs find -L: found $NUM, expected $DOMFILES"
19444         echo "Test 1: lfs find 20 DOM files by layout: OK"
19445
19446         # there should be 1 dir with default DOM striping
19447         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
19448         [ $NUM -eq  1 ] ||
19449                 error "lfs find -L: found $NUM, expected 1 dir"
19450         echo "Test 2: lfs find 1 DOM dir by layout: OK"
19451
19452         # find DoM files by stripe size
19453         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
19454         [ $NUM -eq  $DOMFILES ] ||
19455                 error "lfs find -S: found $NUM, expected $DOMFILES"
19456         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
19457
19458         # find files by stripe offset except DoM files
19459         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
19460         [ $NUM -eq  $NORMFILES ] ||
19461                 error "lfs find -i: found $NUM, expected $NORMFILES"
19462         echo "Test 5: lfs find no DOM files by stripe index: OK"
19463         return 0
19464 }
19465 run_test 270e "DoM: lfs find with DoM files test"
19466
19467 test_270f() {
19468         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19469                 skip "Need MDS version at least 2.10.55"
19470
19471         local mdtname=${FSNAME}-MDT0000-mdtlov
19472         local dom=$DIR/$tdir/dom_file
19473         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
19474                                                 lod.$mdtname.dom_stripesize)
19475         local dom_limit=131072
19476
19477         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
19478         local dom_current=$(do_facet mds1 $LCTL get_param -n \
19479                                                 lod.$mdtname.dom_stripesize)
19480         [ ${dom_limit} -eq ${dom_current} ] ||
19481                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
19482
19483         $LFS mkdir -i 0 -c 1 $DIR/$tdir
19484         $LFS setstripe -d $DIR/$tdir
19485         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
19486                 error "Can't set directory default striping"
19487
19488         # exceed maximum stripe size
19489         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
19490                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
19491         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
19492                 error "Able to create DoM component size more than LOD limit"
19493
19494         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
19495         dom_current=$(do_facet mds1 $LCTL get_param -n \
19496                                                 lod.$mdtname.dom_stripesize)
19497         [ 0 -eq ${dom_current} ] ||
19498                 error "Can't set zero DoM stripe limit"
19499         rm $dom
19500
19501         # attempt to create DoM file on server with disabled DoM should
19502         # remove DoM entry from layout and be succeed
19503         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
19504                 error "Can't create DoM file (DoM is disabled)"
19505         [ $($LFS getstripe -L $dom) == "mdt" ] &&
19506                 error "File has DoM component while DoM is disabled"
19507         rm $dom
19508
19509         # attempt to create DoM file with only DoM stripe should return error
19510         $LFS setstripe -E $dom_limit -L mdt $dom &&
19511                 error "Able to create DoM-only file while DoM is disabled"
19512
19513         # too low values to be aligned with smallest stripe size 64K
19514         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
19515         dom_current=$(do_facet mds1 $LCTL get_param -n \
19516                                                 lod.$mdtname.dom_stripesize)
19517         [ 30000 -eq ${dom_current} ] &&
19518                 error "Can set too small DoM stripe limit"
19519
19520         # 64K is a minimal stripe size in Lustre, expect limit of that size
19521         [ 65536 -eq ${dom_current} ] ||
19522                 error "Limit is not set to 64K but ${dom_current}"
19523
19524         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
19525         dom_current=$(do_facet mds1 $LCTL get_param -n \
19526                                                 lod.$mdtname.dom_stripesize)
19527         echo $dom_current
19528         [ 2147483648 -eq ${dom_current} ] &&
19529                 error "Can set too large DoM stripe limit"
19530
19531         do_facet mds1 $LCTL set_param -n \
19532                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
19533         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
19534                 error "Can't create DoM component size after limit change"
19535         do_facet mds1 $LCTL set_param -n \
19536                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
19537         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
19538                 error "Can't create DoM file after limit decrease"
19539         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
19540                 error "Can create big DoM component after limit decrease"
19541         touch ${dom}_def ||
19542                 error "Can't create file with old default layout"
19543
19544         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
19545         return 0
19546 }
19547 run_test 270f "DoM: maximum DoM stripe size checks"
19548
19549 test_270g() {
19550         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
19551                 skip "Need MDS version at least 2.13.52"
19552         local dom=$DIR/$tdir/$tfile
19553
19554         $LFS mkdir -i 0 -c 1 $DIR/$tdir
19555         local lodname=${FSNAME}-MDT0000-mdtlov
19556
19557         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
19558         save_lustre_params mds1 "lod.${lodname}.dom_stripesize_max_kb" > $save
19559         save_lustre_params mds1 "lod.${lodname}.dom_threshold_free_mb" >> $save
19560         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
19561
19562         local dom_limit=1024
19563         local dom_threshold="50%"
19564
19565         $LFS setstripe -d $DIR/$tdir
19566         $LFS setstripe -E ${dom_limit}K -L mdt $DIR/$tdir ||
19567                 error "Can't set directory default striping"
19568
19569         do_facet mds1 $LCTL set_param -n \
19570                                 lod.${lodname}.dom_stripesize_max_kb=$dom_limit
19571         # set 0 threshold and create DOM file to change tunable stripesize
19572         do_facet mds1 $LCTL set_param -n lod.${lodname}.dom_threshold_free_mb=0
19573         $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
19574                 error "Failed to create $dom file"
19575         # now tunable dom_cur_stripesize should reach maximum
19576         local dom_current=$(do_facet mds1 $LCTL get_param -n \
19577                                         lod.${lodname}.dom_stripesize_cur_kb)
19578         [[ $dom_current == $dom_limit ]] ||
19579                 error "Current DOM stripesize is not maximum"
19580         rm $dom
19581
19582         # set threshold for further tests
19583         do_facet mds1 $LCTL set_param -n \
19584                         lod.${lodname}.dom_threshold_free_mb=$dom_threshold
19585         echo "DOM threshold is $dom_threshold free space"
19586         local dom_def
19587         local dom_set
19588         # Spoof bfree to exceed threshold
19589         #define OBD_FAIL_MDS_STATFS_SPOOF   0x168
19590         do_facet mds1 $LCTL set_param -n fail_loc=0x0168
19591         for spfree in 40 20 0 15 30 55; do
19592                 do_facet mds1 $LCTL set_param -n fail_val=$spfree
19593                 $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
19594                         error "Failed to create $dom file"
19595                 dom_def=$(do_facet mds1 $LCTL get_param -n \
19596                                         lod.${lodname}.dom_stripesize_cur_kb)
19597                 echo "Free space: ${spfree}%, default DOM stripe: ${dom_def}K"
19598                 [[ $dom_def != $dom_current ]] ||
19599                         error "Default stripe size was not changed"
19600                 if [[ $spfree > 0 ]] ; then
19601                         dom_set=$($LFS getstripe -S $dom)
19602                         [[ $dom_set == $((dom_def * 1024)) ]] ||
19603                                 error "DOM component size is still old"
19604                 else
19605                         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
19606                                 error "DoM component is set with no free space"
19607                 fi
19608                 rm $dom
19609                 dom_current=$dom_def
19610         done
19611 }
19612 run_test 270g "DoM: default DoM stripe size depends on free space"
19613
19614 test_271a() {
19615         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19616                 skip "Need MDS version at least 2.10.55"
19617
19618         local dom=$DIR/$tdir/dom
19619
19620         mkdir -p $DIR/$tdir
19621
19622         $LFS setstripe -E 1024K -L mdt $dom
19623
19624         lctl set_param -n mdc.*.stats=clear
19625         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
19626         cat $dom > /dev/null
19627         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
19628         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
19629         ls $dom
19630         rm -f $dom
19631 }
19632 run_test 271a "DoM: data is cached for read after write"
19633
19634 test_271b() {
19635         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19636                 skip "Need MDS version at least 2.10.55"
19637
19638         local dom=$DIR/$tdir/dom
19639
19640         mkdir -p $DIR/$tdir
19641
19642         $LFS setstripe -E 1024K -L mdt -E EOF $dom
19643
19644         lctl set_param -n mdc.*.stats=clear
19645         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
19646         cancel_lru_locks mdc
19647         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
19648         # second stat to check size is cached on client
19649         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
19650         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
19651         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
19652         rm -f $dom
19653 }
19654 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
19655
19656 test_271ba() {
19657         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19658                 skip "Need MDS version at least 2.10.55"
19659
19660         local dom=$DIR/$tdir/dom
19661
19662         mkdir -p $DIR/$tdir
19663
19664         $LFS setstripe -E 1024K -L mdt -E EOF $dom
19665
19666         lctl set_param -n mdc.*.stats=clear
19667         lctl set_param -n osc.*.stats=clear
19668         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
19669         cancel_lru_locks mdc
19670         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
19671         # second stat to check size is cached on client
19672         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
19673         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
19674         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
19675         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
19676         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
19677         rm -f $dom
19678 }
19679 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
19680
19681
19682 get_mdc_stats() {
19683         local mdtidx=$1
19684         local param=$2
19685         local mdt=MDT$(printf %04x $mdtidx)
19686
19687         if [ -z $param ]; then
19688                 lctl get_param -n mdc.*$mdt*.stats
19689         else
19690                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
19691         fi
19692 }
19693
19694 test_271c() {
19695         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19696                 skip "Need MDS version at least 2.10.55"
19697
19698         local dom=$DIR/$tdir/dom
19699
19700         mkdir -p $DIR/$tdir
19701
19702         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19703
19704         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
19705         local facet=mds$((mdtidx + 1))
19706
19707         cancel_lru_locks mdc
19708         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
19709         createmany -o $dom 1000
19710         lctl set_param -n mdc.*.stats=clear
19711         smalliomany -w $dom 1000 200
19712         get_mdc_stats $mdtidx
19713         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
19714         # Each file has 1 open, 1 IO enqueues, total 2000
19715         # but now we have also +1 getxattr for security.capability, total 3000
19716         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
19717         unlinkmany $dom 1000
19718
19719         cancel_lru_locks mdc
19720         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
19721         createmany -o $dom 1000
19722         lctl set_param -n mdc.*.stats=clear
19723         smalliomany -w $dom 1000 200
19724         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
19725         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
19726         # for OPEN and IO lock.
19727         [ $((enq - enq_2)) -ge 1000 ] ||
19728                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
19729         unlinkmany $dom 1000
19730         return 0
19731 }
19732 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
19733
19734 cleanup_271def_tests() {
19735         trap 0
19736         rm -f $1
19737 }
19738
19739 test_271d() {
19740         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
19741                 skip "Need MDS version at least 2.10.57"
19742
19743         local dom=$DIR/$tdir/dom
19744         local tmp=$TMP/$tfile
19745         trap "cleanup_271def_tests $tmp" EXIT
19746
19747         mkdir -p $DIR/$tdir
19748
19749         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19750
19751         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
19752
19753         cancel_lru_locks mdc
19754         dd if=/dev/urandom of=$tmp bs=1000 count=1
19755         dd if=$tmp of=$dom bs=1000 count=1
19756         cancel_lru_locks mdc
19757
19758         cat /etc/hosts >> $tmp
19759         lctl set_param -n mdc.*.stats=clear
19760
19761         # append data to the same file it should update local page
19762         echo "Append to the same page"
19763         cat /etc/hosts >> $dom
19764         local num=$(get_mdc_stats $mdtidx ost_read)
19765         local ra=$(get_mdc_stats $mdtidx req_active)
19766         local rw=$(get_mdc_stats $mdtidx req_waittime)
19767
19768         [ -z $num ] || error "$num READ RPC occured"
19769         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19770         echo "... DONE"
19771
19772         # compare content
19773         cmp $tmp $dom || error "file miscompare"
19774
19775         cancel_lru_locks mdc
19776         lctl set_param -n mdc.*.stats=clear
19777
19778         echo "Open and read file"
19779         cat $dom > /dev/null
19780         local num=$(get_mdc_stats $mdtidx ost_read)
19781         local ra=$(get_mdc_stats $mdtidx req_active)
19782         local rw=$(get_mdc_stats $mdtidx req_waittime)
19783
19784         [ -z $num ] || error "$num READ RPC occured"
19785         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19786         echo "... DONE"
19787
19788         # compare content
19789         cmp $tmp $dom || error "file miscompare"
19790
19791         return 0
19792 }
19793 run_test 271d "DoM: read on open (1K file in reply buffer)"
19794
19795 test_271f() {
19796         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
19797                 skip "Need MDS version at least 2.10.57"
19798
19799         local dom=$DIR/$tdir/dom
19800         local tmp=$TMP/$tfile
19801         trap "cleanup_271def_tests $tmp" EXIT
19802
19803         mkdir -p $DIR/$tdir
19804
19805         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19806
19807         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
19808
19809         cancel_lru_locks mdc
19810         dd if=/dev/urandom of=$tmp bs=265000 count=1
19811         dd if=$tmp of=$dom bs=265000 count=1
19812         cancel_lru_locks mdc
19813         cat /etc/hosts >> $tmp
19814         lctl set_param -n mdc.*.stats=clear
19815
19816         echo "Append to the same page"
19817         cat /etc/hosts >> $dom
19818         local num=$(get_mdc_stats $mdtidx ost_read)
19819         local ra=$(get_mdc_stats $mdtidx req_active)
19820         local rw=$(get_mdc_stats $mdtidx req_waittime)
19821
19822         [ -z $num ] || error "$num READ RPC occured"
19823         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19824         echo "... DONE"
19825
19826         # compare content
19827         cmp $tmp $dom || error "file miscompare"
19828
19829         cancel_lru_locks mdc
19830         lctl set_param -n mdc.*.stats=clear
19831
19832         echo "Open and read file"
19833         cat $dom > /dev/null
19834         local num=$(get_mdc_stats $mdtidx ost_read)
19835         local ra=$(get_mdc_stats $mdtidx req_active)
19836         local rw=$(get_mdc_stats $mdtidx req_waittime)
19837
19838         [ -z $num ] && num=0
19839         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
19840         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19841         echo "... DONE"
19842
19843         # compare content
19844         cmp $tmp $dom || error "file miscompare"
19845
19846         return 0
19847 }
19848 run_test 271f "DoM: read on open (200K file and read tail)"
19849
19850 test_271g() {
19851         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
19852                 skip "Skipping due to old client or server version"
19853
19854         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
19855         # to get layout
19856         $CHECKSTAT -t file $DIR1/$tfile
19857
19858         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
19859         MULTIOP_PID=$!
19860         sleep 1
19861         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
19862         $LCTL set_param fail_loc=0x80000314
19863         rm $DIR1/$tfile || error "Unlink fails"
19864         RC=$?
19865         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
19866         [ $RC -eq 0 ] || error "Failed write to stale object"
19867 }
19868 run_test 271g "Discard DoM data vs client flush race"
19869
19870 test_272a() {
19871         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19872                 skip "Need MDS version at least 2.11.50"
19873
19874         local dom=$DIR/$tdir/dom
19875         mkdir -p $DIR/$tdir
19876
19877         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
19878         dd if=/dev/urandom of=$dom bs=512K count=1 ||
19879                 error "failed to write data into $dom"
19880         local old_md5=$(md5sum $dom)
19881
19882         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
19883                 error "failed to migrate to the same DoM component"
19884
19885         local new_md5=$(md5sum $dom)
19886
19887         [ "$old_md5" == "$new_md5" ] ||
19888                 error "md5sum differ: $old_md5, $new_md5"
19889
19890         [ $($LFS getstripe -c $dom) -eq 2 ] ||
19891                 error "bad final stripe count: $($LFS getstripe -c $dom) != 2"
19892 }
19893 run_test 272a "DoM migration: new layout with the same DOM component"
19894
19895 test_272b() {
19896         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19897                 skip "Need MDS version at least 2.11.50"
19898
19899         local dom=$DIR/$tdir/dom
19900         mkdir -p $DIR/$tdir
19901         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19902
19903         local mdtidx=$($LFS getstripe -m $dom)
19904         local mdtname=MDT$(printf %04x $mdtidx)
19905         local facet=mds$((mdtidx + 1))
19906
19907         local mdtfree1=$(do_facet $facet \
19908                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19909         dd if=/dev/urandom of=$dom bs=2M count=1 ||
19910                 error "failed to write data into $dom"
19911         local old_md5=$(md5sum $dom)
19912         cancel_lru_locks mdc
19913         local mdtfree1=$(do_facet $facet \
19914                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19915
19916         $LFS migrate -c2 $dom ||
19917                 error "failed to migrate to the new composite layout"
19918         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19919                 error "MDT stripe was not removed"
19920
19921         cancel_lru_locks mdc
19922         local new_md5=$(md5sum $dom)
19923         [ "$old_md5" == "$new_md5" ] ||
19924                 error "$old_md5 != $new_md5"
19925
19926         # Skip free space checks with ZFS
19927         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19928                 local mdtfree2=$(do_facet $facet \
19929                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19930                 [ $mdtfree2 -gt $mdtfree1 ] ||
19931                         error "MDT space is not freed after migration"
19932         fi
19933         return 0
19934 }
19935 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
19936
19937 test_272c() {
19938         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19939                 skip "Need MDS version at least 2.11.50"
19940
19941         local dom=$DIR/$tdir/$tfile
19942         mkdir -p $DIR/$tdir
19943         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19944
19945         local mdtidx=$($LFS getstripe -m $dom)
19946         local mdtname=MDT$(printf %04x $mdtidx)
19947         local facet=mds$((mdtidx + 1))
19948
19949         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
19950                 error "failed to write data into $dom"
19951         local old_md5=$(md5sum $dom)
19952         cancel_lru_locks mdc
19953         local mdtfree1=$(do_facet $facet \
19954                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19955
19956         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
19957                 error "failed to migrate to the new composite layout"
19958         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
19959                 error "MDT stripe was not removed"
19960
19961         cancel_lru_locks mdc
19962         local new_md5=$(md5sum $dom)
19963         [ "$old_md5" == "$new_md5" ] ||
19964                 error "$old_md5 != $new_md5"
19965
19966         # Skip free space checks with ZFS
19967         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19968                 local mdtfree2=$(do_facet $facet \
19969                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19970                 [ $mdtfree2 -gt $mdtfree1 ] ||
19971                         error "MDS space is not freed after migration"
19972         fi
19973         return 0
19974 }
19975 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
19976
19977 test_272d() {
19978         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19979                 skip "Need MDS version at least 2.12.55"
19980
19981         local dom=$DIR/$tdir/$tfile
19982         mkdir -p $DIR/$tdir
19983         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19984
19985         local mdtidx=$($LFS getstripe -m $dom)
19986         local mdtname=MDT$(printf %04x $mdtidx)
19987         local facet=mds$((mdtidx + 1))
19988
19989         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
19990                 error "failed to write data into $dom"
19991         local old_md5=$(md5sum $dom)
19992         cancel_lru_locks mdc
19993         local mdtfree1=$(do_facet $facet \
19994                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19995
19996         $LFS mirror extend -N -E 2M -c1 -E -1 -c2 $dom ||
19997                 error "failed mirroring to the new composite layout"
19998         $LFS mirror resync $dom ||
19999                 error "failed mirror resync"
20000         $LFS mirror split --mirror-id 1 -d $dom ||
20001                 error "failed mirror split"
20002
20003         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
20004                 error "MDT stripe was not removed"
20005
20006         cancel_lru_locks mdc
20007         local new_md5=$(md5sum $dom)
20008         [ "$old_md5" == "$new_md5" ] ||
20009                 error "$old_md5 != $new_md5"
20010
20011         # Skip free space checks with ZFS
20012         if [ "$(facet_fstype $facet)" != "zfs" ]; then
20013                 local mdtfree2=$(do_facet $facet \
20014                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
20015                 [ $mdtfree2 -gt $mdtfree1 ] ||
20016                         error "MDS space is not freed after DOM mirror deletion"
20017         fi
20018         return 0
20019 }
20020 run_test 272d "DoM mirroring: OST-striped mirror to DOM file"
20021
20022 test_272e() {
20023         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
20024                 skip "Need MDS version at least 2.12.55"
20025
20026         local dom=$DIR/$tdir/$tfile
20027         mkdir -p $DIR/$tdir
20028         $LFS setstripe -c 2 $dom
20029
20030         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
20031                 error "failed to write data into $dom"
20032         local old_md5=$(md5sum $dom)
20033         cancel_lru_locks mdc
20034
20035         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 $dom ||
20036                 error "failed mirroring to the DOM layout"
20037         $LFS mirror resync $dom ||
20038                 error "failed mirror resync"
20039         $LFS mirror split --mirror-id 1 -d $dom ||
20040                 error "failed mirror split"
20041
20042         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
20043                 error "MDT stripe was not removed"
20044
20045         cancel_lru_locks mdc
20046         local new_md5=$(md5sum $dom)
20047         [ "$old_md5" == "$new_md5" ] ||
20048                 error "$old_md5 != $new_md5"
20049
20050         return 0
20051 }
20052 run_test 272e "DoM mirroring: DOM mirror to the OST-striped file"
20053
20054 test_272f() {
20055         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
20056                 skip "Need MDS version at least 2.12.55"
20057
20058         local dom=$DIR/$tdir/$tfile
20059         mkdir -p $DIR/$tdir
20060         $LFS setstripe -c 2 $dom
20061
20062         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
20063                 error "failed to write data into $dom"
20064         local old_md5=$(md5sum $dom)
20065         cancel_lru_locks mdc
20066
20067         $LFS migrate -E 1M -L mdt -E eof -c2 -v $dom ||
20068                 error "failed migrating to the DOM file"
20069
20070         cancel_lru_locks mdc
20071         local new_md5=$(md5sum $dom)
20072         [ "$old_md5" != "$new_md5" ] &&
20073                 error "$old_md5 != $new_md5"
20074
20075         return 0
20076 }
20077 run_test 272f "DoM migration: OST-striped file to DOM file"
20078
20079 test_273a() {
20080         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
20081                 skip "Need MDS version at least 2.11.50"
20082
20083         # Layout swap cannot be done if either file has DOM component,
20084         # this will never be supported, migration should be used instead
20085
20086         local dom=$DIR/$tdir/$tfile
20087         mkdir -p $DIR/$tdir
20088
20089         $LFS setstripe -c2 ${dom}_plain
20090         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
20091         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
20092                 error "can swap layout with DoM component"
20093         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
20094                 error "can swap layout with DoM component"
20095
20096         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
20097         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
20098                 error "can swap layout with DoM component"
20099         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
20100                 error "can swap layout with DoM component"
20101         return 0
20102 }
20103 run_test 273a "DoM: layout swapping should fail with DOM"
20104
20105 test_275() {
20106         remote_ost_nodsh && skip "remote OST with nodsh"
20107         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
20108                 skip "Need OST version >= 2.10.57"
20109
20110         local file=$DIR/$tfile
20111         local oss
20112
20113         oss=$(comma_list $(osts_nodes))
20114
20115         dd if=/dev/urandom of=$file bs=1M count=2 ||
20116                 error "failed to create a file"
20117         cancel_lru_locks osc
20118
20119         #lock 1
20120         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
20121                 error "failed to read a file"
20122
20123 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
20124         $LCTL set_param fail_loc=0x8000031f
20125
20126         cancel_lru_locks osc &
20127         sleep 1
20128
20129 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
20130         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
20131         #IO takes another lock, but matches the PENDING one
20132         #and places it to the IO RPC
20133         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
20134                 error "failed to read a file with PENDING lock"
20135 }
20136 run_test 275 "Read on a canceled duplicate lock"
20137
20138 test_276() {
20139         remote_ost_nodsh && skip "remote OST with nodsh"
20140         local pid
20141
20142         do_facet ost1 "(while true; do \
20143                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
20144                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
20145         pid=$!
20146
20147         for LOOP in $(seq 20); do
20148                 stop ost1
20149                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
20150         done
20151         kill -9 $pid
20152         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
20153                 rm $TMP/sanity_276_pid"
20154 }
20155 run_test 276 "Race between mount and obd_statfs"
20156
20157 test_277() {
20158         $LCTL set_param ldlm.namespaces.*.lru_size=0
20159         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
20160         local cached_mb=$($LCTL get_param llite.*.max_cached_mb |
20161                         grep ^used_mb | awk '{print $2}')
20162         [ $cached_mb -eq 1 ] || error "expected mb 1 got $cached_mb"
20163         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 \
20164                 oflag=direct conv=notrunc
20165         cached_mb=$($LCTL get_param llite.*.max_cached_mb |
20166                         grep ^used_mb | awk '{print $2}')
20167         [ $cached_mb -eq 0 ] || error "expected mb 0 got $cached_mb"
20168 }
20169 run_test 277 "Direct IO shall drop page cache"
20170
20171 test_278() {
20172         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
20173         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
20174         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] &&
20175                 skip "needs the same host for mdt1 mdt2" && return
20176
20177         local pid1
20178         local pid2
20179
20180 #define OBD_FAIL_OBD_STOP_MDS_RACE     0x60b
20181         do_facet mds2 $LCTL set_param fail_loc=0x8000060c
20182         stop mds2 &
20183         pid2=$!
20184
20185         stop mds1
20186
20187         echo "Starting MDTs"
20188         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
20189         wait $pid2
20190 #For the error assertion will happen. lu_env_get_key(..., &mdt_thread_key)
20191 #will return NULL
20192         do_facet mds2 $LCTL set_param fail_loc=0
20193
20194         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS
20195         wait_recovery_complete mds2
20196 }
20197 run_test 278 "Race starting MDS between MDTs stop/start"
20198
20199 test_280() {
20200         [ $MGS_VERSION -lt $(version_code 2.13.52) ] &&
20201                 skip "Need MGS version at least 2.13.52"
20202         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20203         combined_mgs_mds || skip "needs combined MGS/MDT"
20204
20205         umount_client $MOUNT
20206 #define OBD_FAIL_MDS_LLOG_UMOUNT_RACE   0x15e
20207         do_facet mgs $LCTL set_param fail_loc=0x8000015e fail_val=0
20208
20209         mount_client $MOUNT &
20210         sleep 1
20211         stop mgs || error "stop mgs failed"
20212         #for a race mgs would crash
20213         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
20214         mount_client $MOUNT || error "mount client failed"
20215 }
20216 run_test 280 "Race between MGS umount and client llog processing"
20217
20218 cleanup_test_300() {
20219         trap 0
20220         umask $SAVE_UMASK
20221 }
20222 test_striped_dir() {
20223         local mdt_index=$1
20224         local stripe_count
20225         local stripe_index
20226
20227         mkdir -p $DIR/$tdir
20228
20229         SAVE_UMASK=$(umask)
20230         trap cleanup_test_300 RETURN EXIT
20231
20232         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
20233                                                 $DIR/$tdir/striped_dir ||
20234                 error "set striped dir error"
20235
20236         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
20237         [ "$mode" = "755" ] || error "expect 755 got $mode"
20238
20239         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
20240                 error "getdirstripe failed"
20241         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
20242         if [ "$stripe_count" != "2" ]; then
20243                 error "1:stripe_count is $stripe_count, expect 2"
20244         fi
20245         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
20246         if [ "$stripe_count" != "2" ]; then
20247                 error "2:stripe_count is $stripe_count, expect 2"
20248         fi
20249
20250         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
20251         if [ "$stripe_index" != "$mdt_index" ]; then
20252                 error "stripe_index is $stripe_index, expect $mdt_index"
20253         fi
20254
20255         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
20256                 error "nlink error after create striped dir"
20257
20258         mkdir $DIR/$tdir/striped_dir/a
20259         mkdir $DIR/$tdir/striped_dir/b
20260
20261         stat $DIR/$tdir/striped_dir/a ||
20262                 error "create dir under striped dir failed"
20263         stat $DIR/$tdir/striped_dir/b ||
20264                 error "create dir under striped dir failed"
20265
20266         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
20267                 error "nlink error after mkdir"
20268
20269         rmdir $DIR/$tdir/striped_dir/a
20270         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
20271                 error "nlink error after rmdir"
20272
20273         rmdir $DIR/$tdir/striped_dir/b
20274         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
20275                 error "nlink error after rmdir"
20276
20277         chattr +i $DIR/$tdir/striped_dir
20278         createmany -o $DIR/$tdir/striped_dir/f 10 &&
20279                 error "immutable flags not working under striped dir!"
20280         chattr -i $DIR/$tdir/striped_dir
20281
20282         rmdir $DIR/$tdir/striped_dir ||
20283                 error "rmdir striped dir error"
20284
20285         cleanup_test_300
20286
20287         true
20288 }
20289
20290 test_300a() {
20291         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
20292                 skip "skipped for lustre < 2.7.0"
20293         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20294         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20295
20296         test_striped_dir 0 || error "failed on striped dir on MDT0"
20297         test_striped_dir 1 || error "failed on striped dir on MDT0"
20298 }
20299 run_test 300a "basic striped dir sanity test"
20300
20301 test_300b() {
20302         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
20303                 skip "skipped for lustre < 2.7.0"
20304         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20305         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20306
20307         local i
20308         local mtime1
20309         local mtime2
20310         local mtime3
20311
20312         test_mkdir $DIR/$tdir || error "mkdir fail"
20313         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
20314                 error "set striped dir error"
20315         for i in {0..9}; do
20316                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
20317                 sleep 1
20318                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
20319                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
20320                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
20321                 sleep 1
20322                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
20323                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
20324                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
20325         done
20326         true
20327 }
20328 run_test 300b "check ctime/mtime for striped dir"
20329
20330 test_300c() {
20331         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
20332                 skip "skipped for lustre < 2.7.0"
20333         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20334         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20335
20336         local file_count
20337
20338         mkdir -p $DIR/$tdir
20339         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
20340                 error "set striped dir error"
20341
20342         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
20343                 error "chown striped dir failed"
20344
20345         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
20346                 error "create 5k files failed"
20347
20348         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
20349
20350         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
20351
20352         rm -rf $DIR/$tdir
20353 }
20354 run_test 300c "chown && check ls under striped directory"
20355
20356 test_300d() {
20357         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
20358                 skip "skipped for lustre < 2.7.0"
20359         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20360         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20361
20362         local stripe_count
20363         local file
20364
20365         mkdir -p $DIR/$tdir
20366         $LFS setstripe -c 2 $DIR/$tdir
20367
20368         #local striped directory
20369         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
20370                 error "set striped dir error"
20371         #look at the directories for debug purposes
20372         ls -l $DIR/$tdir
20373         $LFS getdirstripe $DIR/$tdir
20374         ls -l $DIR/$tdir/striped_dir
20375         $LFS getdirstripe $DIR/$tdir/striped_dir
20376         createmany -o $DIR/$tdir/striped_dir/f 10 ||
20377                 error "create 10 files failed"
20378
20379         #remote striped directory
20380         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
20381                 error "set striped dir error"
20382         #look at the directories for debug purposes
20383         ls -l $DIR/$tdir
20384         $LFS getdirstripe $DIR/$tdir
20385         ls -l $DIR/$tdir/remote_striped_dir
20386         $LFS getdirstripe $DIR/$tdir/remote_striped_dir
20387         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
20388                 error "create 10 files failed"
20389
20390         for file in $(find $DIR/$tdir); do
20391                 stripe_count=$($LFS getstripe -c $file)
20392                 [ $stripe_count -eq 2 ] ||
20393                         error "wrong stripe $stripe_count for $file"
20394         done
20395
20396         rm -rf $DIR/$tdir
20397 }
20398 run_test 300d "check default stripe under striped directory"
20399
20400 test_300e() {
20401         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20402                 skip "Need MDS version at least 2.7.55"
20403         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20404         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20405
20406         local stripe_count
20407         local file
20408
20409         mkdir -p $DIR/$tdir
20410
20411         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
20412                 error "set striped dir error"
20413
20414         touch $DIR/$tdir/striped_dir/a
20415         touch $DIR/$tdir/striped_dir/b
20416         touch $DIR/$tdir/striped_dir/c
20417
20418         mkdir $DIR/$tdir/striped_dir/dir_a
20419         mkdir $DIR/$tdir/striped_dir/dir_b
20420         mkdir $DIR/$tdir/striped_dir/dir_c
20421
20422         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
20423                 error "set striped adir under striped dir error"
20424
20425         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
20426                 error "set striped bdir under striped dir error"
20427
20428         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
20429                 error "set striped cdir under striped dir error"
20430
20431         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
20432                 error "rename dir under striped dir fails"
20433
20434         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
20435                 error "rename dir under different stripes fails"
20436
20437         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
20438                 error "rename file under striped dir should succeed"
20439
20440         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
20441                 error "rename dir under striped dir should succeed"
20442
20443         rm -rf $DIR/$tdir
20444 }
20445 run_test 300e "check rename under striped directory"
20446
20447 test_300f() {
20448         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20449         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20450         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20451                 skip "Need MDS version at least 2.7.55"
20452
20453         local stripe_count
20454         local file
20455
20456         rm -rf $DIR/$tdir
20457         mkdir -p $DIR/$tdir
20458
20459         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
20460                 error "set striped dir error"
20461
20462         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
20463                 error "set striped dir error"
20464
20465         touch $DIR/$tdir/striped_dir/a
20466         mkdir $DIR/$tdir/striped_dir/dir_a
20467         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
20468                 error "create striped dir under striped dir fails"
20469
20470         touch $DIR/$tdir/striped_dir1/b
20471         mkdir $DIR/$tdir/striped_dir1/dir_b
20472         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
20473                 error "create striped dir under striped dir fails"
20474
20475         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
20476                 error "rename dir under different striped dir should fail"
20477
20478         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
20479                 error "rename striped dir under diff striped dir should fail"
20480
20481         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
20482                 error "rename file under diff striped dirs fails"
20483
20484         rm -rf $DIR/$tdir
20485 }
20486 run_test 300f "check rename cross striped directory"
20487
20488 test_300_check_default_striped_dir()
20489 {
20490         local dirname=$1
20491         local default_count=$2
20492         local default_index=$3
20493         local stripe_count
20494         local stripe_index
20495         local dir_stripe_index
20496         local dir
20497
20498         echo "checking $dirname $default_count $default_index"
20499         $LFS setdirstripe -D -c $default_count -i $default_index \
20500                                 -t all_char $DIR/$tdir/$dirname ||
20501                 error "set default stripe on striped dir error"
20502         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
20503         [ $stripe_count -eq $default_count ] ||
20504                 error "expect $default_count get $stripe_count for $dirname"
20505
20506         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
20507         [ $stripe_index -eq $default_index ] ||
20508                 error "expect $default_index get $stripe_index for $dirname"
20509
20510         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
20511                                                 error "create dirs failed"
20512
20513         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
20514         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
20515         for dir in $(find $DIR/$tdir/$dirname/*); do
20516                 stripe_count=$($LFS getdirstripe -c $dir)
20517                 [ $stripe_count -eq $default_count ] ||
20518                 [ $stripe_count -eq 0 ] || [ $default_count -eq 1 ] ||
20519                 error "stripe count $default_count != $stripe_count for $dir"
20520
20521                 stripe_index=$($LFS getdirstripe -i $dir)
20522                 [ $default_index -eq -1 ] ||
20523                         [ $stripe_index -eq $default_index ] ||
20524                         error "$stripe_index != $default_index for $dir"
20525
20526                 #check default stripe
20527                 stripe_count=$($LFS getdirstripe -D -c $dir)
20528                 [ $stripe_count -eq $default_count ] ||
20529                 error "default count $default_count != $stripe_count for $dir"
20530
20531                 stripe_index=$($LFS getdirstripe -D -i $dir)
20532                 [ $stripe_index -eq $default_index ] ||
20533                 error "default index $default_index != $stripe_index for $dir"
20534         done
20535         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
20536 }
20537
20538 test_300g() {
20539         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20540         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20541                 skip "Need MDS version at least 2.7.55"
20542
20543         local dir
20544         local stripe_count
20545         local stripe_index
20546
20547         mkdir $DIR/$tdir
20548         mkdir $DIR/$tdir/normal_dir
20549
20550         #Checking when client cache stripe index
20551         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
20552         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
20553                 error "create striped_dir failed"
20554
20555         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
20556                 error "create dir0 fails"
20557         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
20558         [ $stripe_index -eq 0 ] ||
20559                 error "dir0 expect index 0 got $stripe_index"
20560
20561         mkdir $DIR/$tdir/striped_dir/dir1 ||
20562                 error "create dir1 fails"
20563         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
20564         [ $stripe_index -eq 1 ] ||
20565                 error "dir1 expect index 1 got $stripe_index"
20566
20567         #check default stripe count/stripe index
20568         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
20569         test_300_check_default_striped_dir normal_dir 1 0
20570         test_300_check_default_striped_dir normal_dir 2 1
20571         test_300_check_default_striped_dir normal_dir 2 -1
20572
20573         #delete default stripe information
20574         echo "delete default stripeEA"
20575         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
20576                 error "set default stripe on striped dir error"
20577
20578         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
20579         for dir in $(find $DIR/$tdir/normal_dir/*); do
20580                 stripe_count=$($LFS getdirstripe -c $dir)
20581                 [ $stripe_count -eq 0 ] ||
20582                         error "expect 1 get $stripe_count for $dir"
20583                 stripe_index=$($LFS getdirstripe -i $dir)
20584                 [ $stripe_index -eq 0 ] ||
20585                         error "expect 0 get $stripe_index for $dir"
20586         done
20587 }
20588 run_test 300g "check default striped directory for normal directory"
20589
20590 test_300h() {
20591         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20592         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20593                 skip "Need MDS version at least 2.7.55"
20594
20595         local dir
20596         local stripe_count
20597
20598         mkdir $DIR/$tdir
20599         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
20600                 error "set striped dir error"
20601
20602         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
20603         test_300_check_default_striped_dir striped_dir 1 0
20604         test_300_check_default_striped_dir striped_dir 2 1
20605         test_300_check_default_striped_dir striped_dir 2 -1
20606
20607         #delete default stripe information
20608         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
20609                 error "set default stripe on striped dir error"
20610
20611         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
20612         for dir in $(find $DIR/$tdir/striped_dir/*); do
20613                 stripe_count=$($LFS getdirstripe -c $dir)
20614                 [ $stripe_count -eq 0 ] ||
20615                         error "expect 1 get $stripe_count for $dir"
20616         done
20617 }
20618 run_test 300h "check default striped directory for striped directory"
20619
20620 test_300i() {
20621         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20622         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20623         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20624                 skip "Need MDS version at least 2.7.55"
20625
20626         local stripe_count
20627         local file
20628
20629         mkdir $DIR/$tdir
20630
20631         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
20632                 error "set striped dir error"
20633
20634         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
20635                 error "create files under striped dir failed"
20636
20637         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
20638                 error "set striped hashdir error"
20639
20640         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
20641                 error "create dir0 under hash dir failed"
20642         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
20643                 error "create dir1 under hash dir failed"
20644
20645         # unfortunately, we need to umount to clear dir layout cache for now
20646         # once we fully implement dir layout, we can drop this
20647         umount_client $MOUNT || error "umount failed"
20648         mount_client $MOUNT || error "mount failed"
20649
20650         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
20651         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
20652         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
20653
20654         #set the stripe to be unknown hash type
20655         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
20656         $LCTL set_param fail_loc=0x1901
20657         for ((i = 0; i < 10; i++)); do
20658                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
20659                         error "stat f-$i failed"
20660                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
20661         done
20662
20663         touch $DIR/$tdir/striped_dir/f0 &&
20664                 error "create under striped dir with unknown hash should fail"
20665
20666         $LCTL set_param fail_loc=0
20667
20668         umount_client $MOUNT || error "umount failed"
20669         mount_client $MOUNT || error "mount failed"
20670
20671         return 0
20672 }
20673 run_test 300i "client handle unknown hash type striped directory"
20674
20675 test_300j() {
20676         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20677         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20678         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20679                 skip "Need MDS version at least 2.7.55"
20680
20681         local stripe_count
20682         local file
20683
20684         mkdir $DIR/$tdir
20685
20686         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
20687         $LCTL set_param fail_loc=0x1702
20688         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
20689                 error "set striped dir error"
20690
20691         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
20692                 error "create files under striped dir failed"
20693
20694         $LCTL set_param fail_loc=0
20695
20696         rm -rf $DIR/$tdir || error "unlink striped dir fails"
20697
20698         return 0
20699 }
20700 run_test 300j "test large update record"
20701
20702 test_300k() {
20703         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20704         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20705         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20706                 skip "Need MDS version at least 2.7.55"
20707
20708         # this test needs a huge transaction
20709         local kb
20710         kb=$(do_facet $SINGLEMDS "$LCTL get_param -n \
20711              osd*.$FSNAME-MDT0000.kbytestotal")
20712         [ $kb -lt $((1024*1024)) ] && skip "MDT0 too small: $kb"
20713
20714         local stripe_count
20715         local file
20716
20717         mkdir $DIR/$tdir
20718
20719         #define OBD_FAIL_LARGE_STRIPE   0x1703
20720         $LCTL set_param fail_loc=0x1703
20721         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
20722                 error "set striped dir error"
20723         $LCTL set_param fail_loc=0
20724
20725         $LFS getdirstripe $DIR/$tdir/striped_dir ||
20726                 error "getstripeddir fails"
20727         rm -rf $DIR/$tdir/striped_dir ||
20728                 error "unlink striped dir fails"
20729
20730         return 0
20731 }
20732 run_test 300k "test large striped directory"
20733
20734 test_300l() {
20735         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20736         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20737         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20738                 skip "Need MDS version at least 2.7.55"
20739
20740         local stripe_index
20741
20742         test_mkdir -p $DIR/$tdir/striped_dir
20743         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
20744                         error "chown $RUNAS_ID failed"
20745         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
20746                 error "set default striped dir failed"
20747
20748         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
20749         $LCTL set_param fail_loc=0x80000158
20750         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
20751
20752         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
20753         [ $stripe_index -eq 1 ] ||
20754                 error "expect 1 get $stripe_index for $dir"
20755 }
20756 run_test 300l "non-root user to create dir under striped dir with stale layout"
20757
20758 test_300m() {
20759         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20760         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
20761         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20762                 skip "Need MDS version at least 2.7.55"
20763
20764         mkdir -p $DIR/$tdir/striped_dir
20765         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
20766                 error "set default stripes dir error"
20767
20768         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
20769
20770         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
20771         [ $stripe_count -eq 0 ] ||
20772                         error "expect 0 get $stripe_count for a"
20773
20774         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
20775                 error "set default stripes dir error"
20776
20777         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
20778
20779         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
20780         [ $stripe_count -eq 0 ] ||
20781                         error "expect 0 get $stripe_count for b"
20782
20783         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
20784                 error "set default stripes dir error"
20785
20786         mkdir $DIR/$tdir/striped_dir/c &&
20787                 error "default stripe_index is invalid, mkdir c should fails"
20788
20789         rm -rf $DIR/$tdir || error "rmdir fails"
20790 }
20791 run_test 300m "setstriped directory on single MDT FS"
20792
20793 cleanup_300n() {
20794         local list=$(comma_list $(mdts_nodes))
20795
20796         trap 0
20797         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
20798 }
20799
20800 test_300n() {
20801         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20802         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20803         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20804                 skip "Need MDS version at least 2.7.55"
20805         remote_mds_nodsh && skip "remote MDS with nodsh"
20806
20807         local stripe_index
20808         local list=$(comma_list $(mdts_nodes))
20809
20810         trap cleanup_300n RETURN EXIT
20811         mkdir -p $DIR/$tdir
20812         chmod 777 $DIR/$tdir
20813         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
20814                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
20815                 error "create striped dir succeeds with gid=0"
20816
20817         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
20818         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
20819                 error "create striped dir fails with gid=-1"
20820
20821         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
20822         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
20823                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
20824                 error "set default striped dir succeeds with gid=0"
20825
20826
20827         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
20828         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
20829                 error "set default striped dir fails with gid=-1"
20830
20831
20832         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
20833         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
20834                                         error "create test_dir fails"
20835         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
20836                                         error "create test_dir1 fails"
20837         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
20838                                         error "create test_dir2 fails"
20839         cleanup_300n
20840 }
20841 run_test 300n "non-root user to create dir under striped dir with default EA"
20842
20843 test_300o() {
20844         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20845         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20846         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20847                 skip "Need MDS version at least 2.7.55"
20848
20849         local numfree1
20850         local numfree2
20851
20852         mkdir -p $DIR/$tdir
20853
20854         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
20855         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
20856         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
20857                 skip "not enough free inodes $numfree1 $numfree2"
20858         fi
20859
20860         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
20861         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
20862         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
20863                 skip "not enough free space $numfree1 $numfree2"
20864         fi
20865
20866         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
20867                 error "setdirstripe fails"
20868
20869         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
20870                 error "create dirs fails"
20871
20872         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
20873         ls $DIR/$tdir/striped_dir > /dev/null ||
20874                 error "ls striped dir fails"
20875         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
20876                 error "unlink big striped dir fails"
20877 }
20878 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
20879
20880 test_300p() {
20881         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20882         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20883         remote_mds_nodsh && skip "remote MDS with nodsh"
20884
20885         mkdir -p $DIR/$tdir
20886
20887         #define OBD_FAIL_OUT_ENOSPC     0x1704
20888         do_facet mds2 lctl set_param fail_loc=0x80001704
20889         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
20890                  && error "create striped directory should fail"
20891
20892         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
20893
20894         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
20895         true
20896 }
20897 run_test 300p "create striped directory without space"
20898
20899 test_300q() {
20900         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20901         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20902
20903         local fd=$(free_fd)
20904         local cmd="exec $fd<$tdir"
20905         cd $DIR
20906         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
20907         eval $cmd
20908         cmd="exec $fd<&-"
20909         trap "eval $cmd" EXIT
20910         cd $tdir || error "cd $tdir fails"
20911         rmdir  ../$tdir || error "rmdir $tdir fails"
20912         mkdir local_dir && error "create dir succeeds"
20913         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
20914         eval $cmd
20915         return 0
20916 }
20917 run_test 300q "create remote directory under orphan directory"
20918
20919 test_300r() {
20920         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20921                 skip "Need MDS version at least 2.7.55" && return
20922         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
20923
20924         mkdir $DIR/$tdir
20925
20926         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
20927                 error "set striped dir error"
20928
20929         $LFS getdirstripe $DIR/$tdir/striped_dir ||
20930                 error "getstripeddir fails"
20931
20932         local stripe_count
20933         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
20934                       awk '/lmv_stripe_count:/ { print $2 }')
20935
20936         [ $MDSCOUNT -ne $stripe_count ] &&
20937                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
20938
20939         rm -rf $DIR/$tdir/striped_dir ||
20940                 error "unlink striped dir fails"
20941 }
20942 run_test 300r "test -1 striped directory"
20943
20944 prepare_remote_file() {
20945         mkdir $DIR/$tdir/src_dir ||
20946                 error "create remote source failed"
20947
20948         cp /etc/hosts $DIR/$tdir/src_dir/a ||
20949                  error "cp to remote source failed"
20950         touch $DIR/$tdir/src_dir/a
20951
20952         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
20953                 error "create remote target dir failed"
20954
20955         touch $DIR/$tdir/tgt_dir/b
20956
20957         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
20958                 error "rename dir cross MDT failed!"
20959
20960         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
20961                 error "src_child still exists after rename"
20962
20963         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
20964                 error "missing file(a) after rename"
20965
20966         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
20967                 error "diff after rename"
20968 }
20969
20970 test_310a() {
20971         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
20972         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20973
20974         local remote_file=$DIR/$tdir/tgt_dir/b
20975
20976         mkdir -p $DIR/$tdir
20977
20978         prepare_remote_file || error "prepare remote file failed"
20979
20980         #open-unlink file
20981         $OPENUNLINK $remote_file $remote_file ||
20982                 error "openunlink $remote_file failed"
20983         $CHECKSTAT -a $remote_file || error "$remote_file exists"
20984 }
20985 run_test 310a "open unlink remote file"
20986
20987 test_310b() {
20988         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
20989         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20990
20991         local remote_file=$DIR/$tdir/tgt_dir/b
20992
20993         mkdir -p $DIR/$tdir
20994
20995         prepare_remote_file || error "prepare remote file failed"
20996
20997         ln $remote_file $DIR/$tfile || error "link failed for remote file"
20998         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
20999         $CHECKSTAT -t file $remote_file || error "check file failed"
21000 }
21001 run_test 310b "unlink remote file with multiple links while open"
21002
21003 test_310c() {
21004         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21005         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
21006
21007         local remote_file=$DIR/$tdir/tgt_dir/b
21008
21009         mkdir -p $DIR/$tdir
21010
21011         prepare_remote_file || error "prepare remote file failed"
21012
21013         ln $remote_file $DIR/$tfile || error "link failed for remote file"
21014         multiop_bg_pause $remote_file O_uc ||
21015                         error "mulitop failed for remote file"
21016         MULTIPID=$!
21017         $MULTIOP $DIR/$tfile Ouc
21018         kill -USR1 $MULTIPID
21019         wait $MULTIPID
21020 }
21021 run_test 310c "open-unlink remote file with multiple links"
21022
21023 #LU-4825
21024 test_311() {
21025         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21026         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
21027         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
21028                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
21029         remote_mds_nodsh && skip "remote MDS with nodsh"
21030
21031         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
21032         local mdts=$(comma_list $(mdts_nodes))
21033
21034         mkdir -p $DIR/$tdir
21035         $LFS setstripe -i 0 -c 1 $DIR/$tdir
21036         createmany -o $DIR/$tdir/$tfile. 1000
21037
21038         # statfs data is not real time, let's just calculate it
21039         old_iused=$((old_iused + 1000))
21040
21041         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
21042                         osp.*OST0000*MDT0000.create_count")
21043         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
21044                                 osp.*OST0000*MDT0000.max_create_count")
21045         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
21046
21047         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
21048         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
21049         [ $index -ne 0 ] || error "$tfile stripe index is 0"
21050
21051         unlinkmany $DIR/$tdir/$tfile. 1000
21052
21053         do_nodes $mdts "$LCTL set_param -n \
21054                         osp.*OST0000*.max_create_count=$max_count"
21055         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
21056                 do_nodes $mdts "$LCTL set_param -n \
21057                                 osp.*OST0000*.create_count=$count"
21058         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
21059                         grep "=0" && error "create_count is zero"
21060
21061         local new_iused
21062         for i in $(seq 120); do
21063                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
21064                 # system may be too busy to destroy all objs in time, use
21065                 # a somewhat small value to not fail autotest
21066                 [ $((old_iused - new_iused)) -gt 400 ] && break
21067                 sleep 1
21068         done
21069
21070         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
21071         [ $((old_iused - new_iused)) -gt 400 ] ||
21072                 error "objs not destroyed after unlink"
21073 }
21074 run_test 311 "disable OSP precreate, and unlink should destroy objs"
21075
21076 zfs_oid_to_objid()
21077 {
21078         local ost=$1
21079         local objid=$2
21080
21081         local vdevdir=$(dirname $(facet_vdevice $ost))
21082         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
21083         local zfs_zapid=$(do_facet $ost $cmd |
21084                           grep -w "/O/0/d$((objid%32))" -C 5 |
21085                           awk '/Object/{getline; print $1}')
21086         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
21087                           awk "/$objid = /"'{printf $3}')
21088
21089         echo $zfs_objid
21090 }
21091
21092 zfs_object_blksz() {
21093         local ost=$1
21094         local objid=$2
21095
21096         local vdevdir=$(dirname $(facet_vdevice $ost))
21097         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
21098         local blksz=$(do_facet $ost $cmd $objid |
21099                       awk '/dblk/{getline; printf $4}')
21100
21101         case "${blksz: -1}" in
21102                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
21103                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
21104                 *) ;;
21105         esac
21106
21107         echo $blksz
21108 }
21109
21110 test_312() { # LU-4856
21111         remote_ost_nodsh && skip "remote OST with nodsh"
21112         [ "$ost1_FSTYPE" = "zfs" ] ||
21113                 skip_env "the test only applies to zfs"
21114
21115         local max_blksz=$(do_facet ost1 \
21116                           $ZFS get -p recordsize $(facet_device ost1) |
21117                           awk '!/VALUE/{print $3}')
21118
21119         # to make life a little bit easier
21120         $LFS mkdir -c 1 -i 0 $DIR/$tdir
21121         $LFS setstripe -c 1 -i 0 $DIR/$tdir
21122
21123         local tf=$DIR/$tdir/$tfile
21124         touch $tf
21125         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
21126
21127         # Get ZFS object id
21128         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
21129         # block size change by sequential overwrite
21130         local bs
21131
21132         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
21133                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
21134
21135                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
21136                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
21137         done
21138         rm -f $tf
21139
21140         # block size change by sequential append write
21141         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
21142         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
21143         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
21144         local count
21145
21146         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
21147                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
21148                         oflag=sync conv=notrunc
21149
21150                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
21151                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
21152                         error "blksz error, actual $blksz, " \
21153                                 "expected: 2 * $count * $PAGE_SIZE"
21154         done
21155         rm -f $tf
21156
21157         # random write
21158         touch $tf
21159         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
21160         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
21161
21162         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
21163         blksz=$(zfs_object_blksz ost1 $zfs_objid)
21164         [ $blksz -eq $PAGE_SIZE ] ||
21165                 error "blksz error: $blksz, expected: $PAGE_SIZE"
21166
21167         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
21168         blksz=$(zfs_object_blksz ost1 $zfs_objid)
21169         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
21170
21171         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
21172         blksz=$(zfs_object_blksz ost1 $zfs_objid)
21173         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
21174 }
21175 run_test 312 "make sure ZFS adjusts its block size by write pattern"
21176
21177 test_313() {
21178         remote_ost_nodsh && skip "remote OST with nodsh"
21179
21180         local file=$DIR/$tfile
21181
21182         rm -f $file
21183         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
21184
21185         # define OBD_FAIL_TGT_RCVD_EIO           0x720
21186         do_facet ost1 "$LCTL set_param fail_loc=0x720"
21187         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
21188                 error "write should failed"
21189         do_facet ost1 "$LCTL set_param fail_loc=0"
21190         rm -f $file
21191 }
21192 run_test 313 "io should fail after last_rcvd update fail"
21193
21194 test_314() {
21195         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
21196
21197         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
21198         do_facet ost1 "$LCTL set_param fail_loc=0x720"
21199         rm -f $DIR/$tfile
21200         wait_delete_completed
21201         do_facet ost1 "$LCTL set_param fail_loc=0"
21202 }
21203 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
21204
21205 test_315() { # LU-618
21206         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
21207
21208         local file=$DIR/$tfile
21209         rm -f $file
21210
21211         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
21212                 error "multiop file write failed"
21213         $MULTIOP $file oO_RDONLY:r4063232_c &
21214         PID=$!
21215
21216         sleep 2
21217
21218         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
21219         kill -USR1 $PID
21220
21221         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
21222         rm -f $file
21223 }
21224 run_test 315 "read should be accounted"
21225
21226 test_316() {
21227         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21228         large_xattr_enabled || skip_env "ea_inode feature disabled"
21229
21230         rm -rf $DIR/$tdir/d
21231         mkdir -p $DIR/$tdir/d
21232         chown nobody $DIR/$tdir/d
21233         touch $DIR/$tdir/d/file
21234
21235         $LFS mv -m1 $DIR/$tdir/d || error "lfs mv failed"
21236 }
21237 run_test 316 "lfs mv"
21238
21239 test_317() {
21240         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
21241                 skip "Need MDS version at least 2.11.53"
21242         if [ "$ost1_FSTYPE" == "zfs" ]; then
21243                 skip "LU-10370: no implementation for ZFS"
21244         fi
21245
21246         local trunc_sz
21247         local grant_blk_size
21248
21249         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
21250                         awk '/grant_block_size:/ { print $2; exit; }')
21251         #
21252         # Create File of size 5M. Truncate it to below size's and verify
21253         # blocks count.
21254         #
21255         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
21256                 error "Create file $DIR/$tfile failed"
21257         stack_trap "rm -f $DIR/$tfile" EXIT
21258
21259         for trunc_sz in 2097152 4097 4000 509 0; do
21260                 $TRUNCATE $DIR/$tfile $trunc_sz ||
21261                         error "truncate $tfile to $trunc_sz failed"
21262                 local sz=$(stat --format=%s $DIR/$tfile)
21263                 local blk=$(stat --format=%b $DIR/$tfile)
21264                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
21265                                      grant_blk_size) * 8))
21266
21267                 if [[ $blk -ne $trunc_blk ]]; then
21268                         $(which stat) $DIR/$tfile
21269                         error "Expected Block $trunc_blk got $blk for $tfile"
21270                 fi
21271
21272                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
21273                         error "Expected Size $trunc_sz got $sz for $tfile"
21274         done
21275
21276         #
21277         # sparse file test
21278         # Create file with a hole and write actual two blocks. Block count
21279         # must be 16.
21280         #
21281         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
21282                 conv=fsync || error "Create file : $DIR/$tfile"
21283
21284         # Calculate the final truncate size.
21285         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
21286
21287         #
21288         # truncate to size $trunc_sz bytes. Strip the last block
21289         # The block count must drop to 8
21290         #
21291         $TRUNCATE $DIR/$tfile $trunc_sz ||
21292                 error "truncate $tfile to $trunc_sz failed"
21293
21294         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
21295         sz=$(stat --format=%s $DIR/$tfile)
21296         blk=$(stat --format=%b $DIR/$tfile)
21297
21298         if [[ $blk -ne $trunc_bsz ]]; then
21299                 $(which stat) $DIR/$tfile
21300                 error "Expected Block $trunc_bsz got $blk for $tfile"
21301         fi
21302
21303         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
21304                 error "Expected Size $trunc_sz got $sz for $tfile"
21305 }
21306 run_test 317 "Verify blocks get correctly update after truncate"
21307
21308 test_318() {
21309         local old_max_active=$($LCTL get_param -n \
21310                             llite.*.max_read_ahead_async_active 2>/dev/null)
21311
21312         $LCTL set_param llite.*.max_read_ahead_async_active=256
21313         local max_active=$($LCTL get_param -n \
21314                            llite.*.max_read_ahead_async_active 2>/dev/null)
21315         [ $max_active -ne 256 ] && error "expected 256 but got $max_active"
21316
21317         $LCTL set_param llite.*.max_read_ahead_async_active=0 ||
21318                 error "set max_read_ahead_async_active should succeed"
21319
21320         $LCTL set_param llite.*.max_read_ahead_async_active=512
21321         max_active=$($LCTL get_param -n \
21322                      llite.*.max_read_ahead_async_active 2>/dev/null)
21323         [ $max_active -eq 512 ] || error "expected 512 but got $max_active"
21324
21325         # restore @max_active
21326         [ $old_max_active -ne 0 ] && $LCTL set_param \
21327                 llite.*.max_read_ahead_async_active=$old_max_active
21328
21329         local old_threshold=$($LCTL get_param -n \
21330                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
21331         local max_per_file_mb=$($LCTL get_param -n \
21332                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
21333
21334         local invalid=$(($max_per_file_mb + 1))
21335         $LCTL set_param \
21336                 llite.*.read_ahead_async_file_threshold_mb=$invalid\
21337                         && error "set $invalid should fail"
21338
21339         local valid=$(($invalid - 1))
21340         $LCTL set_param \
21341                 llite.*.read_ahead_async_file_threshold_mb=$valid ||
21342                         error "set $valid should succeed"
21343         local threshold=$($LCTL get_param -n \
21344                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
21345         [ $threshold -eq $valid ] || error \
21346                 "expect threshold $valid got $threshold"
21347         $LCTL set_param \
21348                 llite.*.read_ahead_async_file_threshold_mb=$old_threshold
21349 }
21350 run_test 318 "Verify async readahead tunables"
21351
21352 test_319() {
21353         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
21354
21355         local before=$(date +%s)
21356         local evict
21357         local mdir=$DIR/$tdir
21358         local file=$mdir/xxx
21359
21360         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
21361         touch $file
21362
21363 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
21364         $LCTL set_param fail_val=5 fail_loc=0x8000032c
21365         $LFS mv -m1 $file &
21366
21367         sleep 1
21368         dd if=$file of=/dev/null
21369         wait
21370         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
21371           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
21372
21373         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
21374 }
21375 run_test 319 "lost lease lock on migrate error"
21376
21377 test_398a() { # LU-4198
21378         $LFS setstripe -c 1 -i 0 $DIR/$tfile
21379         $LCTL set_param ldlm.namespaces.*.lru_size=clear
21380
21381         # request a new lock on client
21382         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
21383
21384         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
21385         local lock_count=$($LCTL get_param -n \
21386                            ldlm.namespaces.*-OST0000-osc-ffff*.lru_size)
21387         [[ $lock_count -eq 0 ]] || error "lock should be cancelled by direct IO"
21388
21389         $LCTL set_param ldlm.namespaces.*-OST0000-osc-ffff*.lru_size=clear
21390
21391         # no lock cached, should use lockless IO and not enqueue new lock
21392         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
21393         lock_count=$($LCTL get_param -n \
21394                      ldlm.namespaces.*-OST0000-osc-ffff*.lru_size)
21395         [[ $lock_count -eq 0 ]] || error "no lock should be held by direct IO"
21396 }
21397 run_test 398a "direct IO should cancel lock otherwise lockless"
21398
21399 test_398b() { # LU-4198
21400         which fio || skip_env "no fio installed"
21401         $LFS setstripe -c -1 $DIR/$tfile
21402
21403         local size=12
21404         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size
21405
21406         local njobs=4
21407         echo "mix direct rw ${size}M to OST0 by fio with $njobs jobs..."
21408         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
21409                 --numjobs=$njobs --fallocate=none \
21410                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
21411                 --filename=$DIR/$tfile &
21412         bg_pid=$!
21413
21414         echo "mix buffer rw ${size}M to OST0 by fio with $njobs jobs..."
21415         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE \
21416                 --numjobs=$njobs --fallocate=none \
21417                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
21418                 --filename=$DIR/$tfile || true
21419         wait $bg_pid
21420
21421         rm -rf $DIR/$tfile
21422 }
21423 run_test 398b "DIO and buffer IO race"
21424
21425 test_398c() { # LU-4198
21426         which fio || skip_env "no fio installed"
21427
21428         saved_debug=$($LCTL get_param -n debug)
21429         $LCTL set_param debug=0
21430
21431         local size=$(lctl get_param -n osc.$FSNAME-OST0000*.kbytesavail | head -1)
21432         ((size /= 1024)) # by megabytes
21433         ((size /= 2)) # write half of the OST at most
21434         [ $size -gt 40 ] && size=40 #reduce test time anyway
21435
21436         $LFS setstripe -c 1 $DIR/$tfile
21437
21438         # it seems like ldiskfs reserves more space than necessary if the
21439         # writing blocks are not mapped, so it extends the file firstly
21440         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size && sync
21441         cancel_lru_locks osc
21442
21443         # clear and verify rpc_stats later
21444         $LCTL set_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats=clear
21445
21446         local njobs=4
21447         echo "writing ${size}M to OST0 by fio with $njobs jobs..."
21448         fio --name=rand-write --rw=randwrite --bs=$PAGE_SIZE --direct=1 \
21449                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
21450                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
21451                 --filename=$DIR/$tfile
21452         [ $? -eq 0 ] || error "fio write error"
21453
21454         [ $($LCTL get_param -n \
21455          ldlm.namespaces.${FSNAME}-OST0000-osc-ffff*.lock_count) -eq 0 ] ||
21456                 error "Locks were requested while doing AIO"
21457
21458         # get the percentage of 1-page I/O
21459         pct=$($LCTL get_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats |
21460                 grep -A 1 'pages per rpc' | grep -v 'pages per rpc' |
21461                 awk '{print $7}')
21462         [ $pct -le 50 ] || error "$pct% of I/O are 1-page"
21463
21464         echo "mix rw ${size}M to OST0 by fio with $njobs jobs..."
21465         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
21466                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
21467                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
21468                 --filename=$DIR/$tfile
21469         [ $? -eq 0 ] || error "fio mixed read write error"
21470
21471         rm -rf $DIR/$tfile
21472         $LCTL set_param debug="$saved_debug"
21473 }
21474 run_test 398c "run fio to test AIO"
21475
21476 test_fake_rw() {
21477         local read_write=$1
21478         if [ "$read_write" = "write" ]; then
21479                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
21480         elif [ "$read_write" = "read" ]; then
21481                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
21482         else
21483                 error "argument error"
21484         fi
21485
21486         # turn off debug for performance testing
21487         local saved_debug=$($LCTL get_param -n debug)
21488         $LCTL set_param debug=0
21489
21490         $LFS setstripe -c 1 -i 0 $DIR/$tfile
21491
21492         # get ost1 size - $FSNAME-OST0000
21493         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
21494         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
21495         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
21496
21497         if [ "$read_write" = "read" ]; then
21498                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
21499         fi
21500
21501         local start_time=$(date +%s.%N)
21502         $dd_cmd bs=1M count=$blocks oflag=sync ||
21503                 error "real dd $read_write error"
21504         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
21505
21506         if [ "$read_write" = "write" ]; then
21507                 rm -f $DIR/$tfile
21508         fi
21509
21510         # define OBD_FAIL_OST_FAKE_RW           0x238
21511         do_facet ost1 $LCTL set_param fail_loc=0x238
21512
21513         local start_time=$(date +%s.%N)
21514         $dd_cmd bs=1M count=$blocks oflag=sync ||
21515                 error "fake dd $read_write error"
21516         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
21517
21518         if [ "$read_write" = "write" ]; then
21519                 # verify file size
21520                 cancel_lru_locks osc
21521                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
21522                         error "$tfile size not $blocks MB"
21523         fi
21524         do_facet ost1 $LCTL set_param fail_loc=0
21525
21526         echo "fake $read_write $duration_fake vs. normal $read_write" \
21527                 "$duration in seconds"
21528         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
21529                 error_not_in_vm "fake write is slower"
21530
21531         $LCTL set_param -n debug="$saved_debug"
21532         rm -f $DIR/$tfile
21533 }
21534 test_399a() { # LU-7655 for OST fake write
21535         remote_ost_nodsh && skip "remote OST with nodsh"
21536
21537         test_fake_rw write
21538 }
21539 run_test 399a "fake write should not be slower than normal write"
21540
21541 test_399b() { # LU-8726 for OST fake read
21542         remote_ost_nodsh && skip "remote OST with nodsh"
21543         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
21544                 skip_env "ldiskfs only test"
21545         fi
21546
21547         test_fake_rw read
21548 }
21549 run_test 399b "fake read should not be slower than normal read"
21550
21551 test_400a() { # LU-1606, was conf-sanity test_74
21552         if ! which $CC > /dev/null 2>&1; then
21553                 skip_env "$CC is not installed"
21554         fi
21555
21556         local extra_flags=''
21557         local out=$TMP/$tfile
21558         local prefix=/usr/include/lustre
21559         local prog
21560
21561         # Oleg removes c files in his test rig so test if any c files exist
21562         [ -z "$(ls -A $LUSTRE_TESTS_API_DIR)" ] && \
21563                 skip_env "Needed c test files are missing"
21564
21565         if ! [[ -d $prefix ]]; then
21566                 # Assume we're running in tree and fixup the include path.
21567                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
21568                 extra_flags+=" -L$LUSTRE/utils/.lib"
21569         fi
21570
21571         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
21572                 $CC -Wall -Werror -std=c99 $extra_flags -o $out $prog -llustreapi ||
21573                         error "client api broken"
21574         done
21575         rm -f $out
21576 }
21577 run_test 400a "Lustre client api program can compile and link"
21578
21579 test_400b() { # LU-1606, LU-5011
21580         local header
21581         local out=$TMP/$tfile
21582         local prefix=/usr/include/linux/lustre
21583
21584         # We use a hard coded prefix so that this test will not fail
21585         # when run in tree. There are headers in lustre/include/lustre/
21586         # that are not packaged (like lustre_idl.h) and have more
21587         # complicated include dependencies (like config.h and lnet/types.h).
21588         # Since this test about correct packaging we just skip them when
21589         # they don't exist (see below) rather than try to fixup cppflags.
21590
21591         if ! which $CC > /dev/null 2>&1; then
21592                 skip_env "$CC is not installed"
21593         fi
21594
21595         for header in $prefix/*.h; do
21596                 if ! [[ -f "$header" ]]; then
21597                         continue
21598                 fi
21599
21600                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
21601                         continue # lustre_ioctl.h is internal header
21602                 fi
21603
21604                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
21605                         error "cannot compile '$header'"
21606         done
21607         rm -f $out
21608 }
21609 run_test 400b "packaged headers can be compiled"
21610
21611 test_401a() { #LU-7437
21612         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
21613         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
21614
21615         #count the number of parameters by "list_param -R"
21616         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
21617         #count the number of parameters by listing proc files
21618         local proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
21619         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
21620         echo "proc_dirs='$proc_dirs'"
21621         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
21622         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
21623                       sort -u | wc -l)
21624
21625         [ $params -eq $procs ] ||
21626                 error "found $params parameters vs. $procs proc files"
21627
21628         # test the list_param -D option only returns directories
21629         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
21630         #count the number of parameters by listing proc directories
21631         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
21632                 sort -u | wc -l)
21633
21634         [ $params -eq $procs ] ||
21635                 error "found $params parameters vs. $procs proc files"
21636 }
21637 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
21638
21639 test_401b() {
21640         local save=$($LCTL get_param -n jobid_var)
21641         local tmp=testing
21642
21643         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
21644                 error "no error returned when setting bad parameters"
21645
21646         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
21647         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
21648
21649         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
21650         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
21651         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
21652 }
21653 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
21654
21655 test_401c() {
21656         local jobid_var_old=$($LCTL get_param -n jobid_var)
21657         local jobid_var_new
21658
21659         $LCTL set_param jobid_var= &&
21660                 error "no error returned for 'set_param a='"
21661
21662         jobid_var_new=$($LCTL get_param -n jobid_var)
21663         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
21664                 error "jobid_var was changed by setting without value"
21665
21666         $LCTL set_param jobid_var &&
21667                 error "no error returned for 'set_param a'"
21668
21669         jobid_var_new=$($LCTL get_param -n jobid_var)
21670         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
21671                 error "jobid_var was changed by setting without value"
21672 }
21673 run_test 401c "Verify 'lctl set_param' without value fails in either format."
21674
21675 test_401d() {
21676         local jobid_var_old=$($LCTL get_param -n jobid_var)
21677         local jobid_var_new
21678         local new_value="foo=bar"
21679
21680         $LCTL set_param jobid_var=$new_value ||
21681                 error "'set_param a=b' did not accept a value containing '='"
21682
21683         jobid_var_new=$($LCTL get_param -n jobid_var)
21684         [[ "$jobid_var_new" == "$new_value" ]] ||
21685                 error "'set_param a=b' failed on a value containing '='"
21686
21687         # Reset the jobid_var to test the other format
21688         $LCTL set_param jobid_var=$jobid_var_old
21689         jobid_var_new=$($LCTL get_param -n jobid_var)
21690         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
21691                 error "failed to reset jobid_var"
21692
21693         $LCTL set_param jobid_var $new_value ||
21694                 error "'set_param a b' did not accept a value containing '='"
21695
21696         jobid_var_new=$($LCTL get_param -n jobid_var)
21697         [[ "$jobid_var_new" == "$new_value" ]] ||
21698                 error "'set_param a b' failed on a value containing '='"
21699
21700         $LCTL set_param jobid_var $jobid_var_old
21701         jobid_var_new=$($LCTL get_param -n jobid_var)
21702         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
21703                 error "failed to reset jobid_var"
21704 }
21705 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
21706
21707 test_402() {
21708         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
21709         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
21710                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
21711         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
21712                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
21713                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
21714         remote_mds_nodsh && skip "remote MDS with nodsh"
21715
21716         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
21717 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
21718         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
21719         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
21720                 echo "Touch failed - OK"
21721 }
21722 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
21723
21724 test_403() {
21725         local file1=$DIR/$tfile.1
21726         local file2=$DIR/$tfile.2
21727         local tfile=$TMP/$tfile
21728
21729         rm -f $file1 $file2 $tfile
21730
21731         touch $file1
21732         ln $file1 $file2
21733
21734         # 30 sec OBD_TIMEOUT in ll_getattr()
21735         # right before populating st_nlink
21736         $LCTL set_param fail_loc=0x80001409
21737         stat -c %h $file1 > $tfile &
21738
21739         # create an alias, drop all locks and reclaim the dentry
21740         < $file2
21741         cancel_lru_locks mdc
21742         cancel_lru_locks osc
21743         sysctl -w vm.drop_caches=2
21744
21745         wait
21746
21747         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
21748
21749         rm -f $tfile $file1 $file2
21750 }
21751 run_test 403 "i_nlink should not drop to zero due to aliasing"
21752
21753 test_404() { # LU-6601
21754         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
21755                 skip "Need server version newer than 2.8.52"
21756         remote_mds_nodsh && skip "remote MDS with nodsh"
21757
21758         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
21759                 awk '/osp .*-osc-MDT/ { print $4}')
21760
21761         local osp
21762         for osp in $mosps; do
21763                 echo "Deactivate: " $osp
21764                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
21765                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
21766                         awk -vp=$osp '$4 == p { print $2 }')
21767                 [ $stat = IN ] || {
21768                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
21769                         error "deactivate error"
21770                 }
21771                 echo "Activate: " $osp
21772                 do_facet $SINGLEMDS $LCTL --device %$osp activate
21773                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
21774                         awk -vp=$osp '$4 == p { print $2 }')
21775                 [ $stat = UP ] || {
21776                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
21777                         error "activate error"
21778                 }
21779         done
21780 }
21781 run_test 404 "validate manual {de}activated works properly for OSPs"
21782
21783 test_405() {
21784         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
21785         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
21786                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
21787                         skip "Layout swap lock is not supported"
21788
21789         check_swap_layouts_support
21790
21791         test_mkdir $DIR/$tdir
21792         swap_lock_test -d $DIR/$tdir ||
21793                 error "One layout swap locked test failed"
21794 }
21795 run_test 405 "Various layout swap lock tests"
21796
21797 test_406() {
21798         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21799         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
21800         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
21801         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21802         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
21803                 skip "Need MDS version at least 2.8.50"
21804
21805         local def_stripe_size=$($LFS getstripe -S $MOUNT)
21806         local test_pool=$TESTNAME
21807
21808         pool_add $test_pool || error "pool_add failed"
21809         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
21810                 error "pool_add_targets failed"
21811
21812         save_layout_restore_at_exit $MOUNT
21813
21814         # parent set default stripe count only, child will stripe from both
21815         # parent and fs default
21816         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
21817                 error "setstripe $MOUNT failed"
21818         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
21819         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
21820         for i in $(seq 10); do
21821                 local f=$DIR/$tdir/$tfile.$i
21822                 touch $f || error "touch failed"
21823                 local count=$($LFS getstripe -c $f)
21824                 [ $count -eq $OSTCOUNT ] ||
21825                         error "$f stripe count $count != $OSTCOUNT"
21826                 local offset=$($LFS getstripe -i $f)
21827                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
21828                 local size=$($LFS getstripe -S $f)
21829                 [ $size -eq $((def_stripe_size * 2)) ] ||
21830                         error "$f stripe size $size != $((def_stripe_size * 2))"
21831                 local pool=$($LFS getstripe -p $f)
21832                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
21833         done
21834
21835         # change fs default striping, delete parent default striping, now child
21836         # will stripe from new fs default striping only
21837         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
21838                 error "change $MOUNT default stripe failed"
21839         $LFS setstripe -c 0 $DIR/$tdir ||
21840                 error "delete $tdir default stripe failed"
21841         for i in $(seq 11 20); do
21842                 local f=$DIR/$tdir/$tfile.$i
21843                 touch $f || error "touch $f failed"
21844                 local count=$($LFS getstripe -c $f)
21845                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
21846                 local offset=$($LFS getstripe -i $f)
21847                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
21848                 local size=$($LFS getstripe -S $f)
21849                 [ $size -eq $def_stripe_size ] ||
21850                         error "$f stripe size $size != $def_stripe_size"
21851                 local pool=$($LFS getstripe -p $f)
21852                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
21853         done
21854
21855         unlinkmany $DIR/$tdir/$tfile. 1 20
21856
21857         local f=$DIR/$tdir/$tfile
21858         pool_remove_all_targets $test_pool $f
21859         pool_remove $test_pool $f
21860 }
21861 run_test 406 "DNE support fs default striping"
21862
21863 test_407() {
21864         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21865         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
21866                 skip "Need MDS version at least 2.8.55"
21867         remote_mds_nodsh && skip "remote MDS with nodsh"
21868
21869         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
21870                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
21871         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
21872                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
21873         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
21874
21875         #define OBD_FAIL_DT_TXN_STOP    0x2019
21876         for idx in $(seq $MDSCOUNT); do
21877                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
21878         done
21879         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
21880         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
21881                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
21882         true
21883 }
21884 run_test 407 "transaction fail should cause operation fail"
21885
21886 test_408() {
21887         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
21888
21889         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
21890         lctl set_param fail_loc=0x8000040a
21891         # let ll_prepare_partial_page() fail
21892         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
21893
21894         rm -f $DIR/$tfile
21895
21896         # create at least 100 unused inodes so that
21897         # shrink_icache_memory(0) should not return 0
21898         touch $DIR/$tfile-{0..100}
21899         rm -f $DIR/$tfile-{0..100}
21900         sync
21901
21902         echo 2 > /proc/sys/vm/drop_caches
21903 }
21904 run_test 408 "drop_caches should not hang due to page leaks"
21905
21906 test_409()
21907 {
21908         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21909
21910         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
21911         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
21912         touch $DIR/$tdir/guard || error "(2) Fail to create"
21913
21914         local PREFIX=$(str_repeat 'A' 128)
21915         echo "Create 1K hard links start at $(date)"
21916         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
21917                 error "(3) Fail to hard link"
21918
21919         echo "Links count should be right although linkEA overflow"
21920         stat $DIR/$tdir/guard || error "(4) Fail to stat"
21921         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
21922         [ $linkcount -eq 1001 ] ||
21923                 error "(5) Unexpected hard links count: $linkcount"
21924
21925         echo "List all links start at $(date)"
21926         ls -l $DIR/$tdir/foo > /dev/null ||
21927                 error "(6) Fail to list $DIR/$tdir/foo"
21928
21929         echo "Unlink hard links start at $(date)"
21930         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
21931                 error "(7) Fail to unlink"
21932         echo "Unlink hard links finished at $(date)"
21933 }
21934 run_test 409 "Large amount of cross-MDTs hard links on the same file"
21935
21936 test_410()
21937 {
21938         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
21939                 skip "Need client version at least 2.9.59"
21940
21941         # Create a file, and stat it from the kernel
21942         local testfile=$DIR/$tfile
21943         touch $testfile
21944
21945         local run_id=$RANDOM
21946         local my_ino=$(stat --format "%i" $testfile)
21947
21948         # Try to insert the module. This will always fail as the
21949         # module is designed to not be inserted.
21950         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
21951             &> /dev/null
21952
21953         # Anything but success is a test failure
21954         dmesg | grep -q \
21955             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
21956             error "no inode match"
21957 }
21958 run_test 410 "Test inode number returned from kernel thread"
21959
21960 cleanup_test411_cgroup() {
21961         trap 0
21962         rmdir "$1"
21963 }
21964
21965 test_411() {
21966         local cg_basedir=/sys/fs/cgroup/memory
21967         # LU-9966
21968         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
21969                 skip "no setup for cgroup"
21970
21971         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
21972                 error "test file creation failed"
21973         cancel_lru_locks osc
21974
21975         # Create a very small memory cgroup to force a slab allocation error
21976         local cgdir=$cg_basedir/osc_slab_alloc
21977         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
21978         trap "cleanup_test411_cgroup $cgdir" EXIT
21979         echo 2M > $cgdir/memory.kmem.limit_in_bytes
21980         echo 1M > $cgdir/memory.limit_in_bytes
21981
21982         # Should not LBUG, just be killed by oom-killer
21983         # dd will return 0 even allocation failure in some environment.
21984         # So don't check return value
21985         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
21986         cleanup_test411_cgroup $cgdir
21987
21988         return 0
21989 }
21990 run_test 411 "Slab allocation error with cgroup does not LBUG"
21991
21992 test_412() {
21993         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21994         if [ $MDS1_VERSION -lt $(version_code 2.10.55) ]; then
21995                 skip "Need server version at least 2.10.55"
21996         fi
21997
21998         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
21999                 error "mkdir failed"
22000         $LFS getdirstripe $DIR/$tdir
22001         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
22002         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
22003                 error "expect $((MDSCOUT - 1)) get $stripe_index"
22004         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
22005         [ $stripe_count -eq 2 ] ||
22006                 error "expect 2 get $stripe_count"
22007 }
22008 run_test 412 "mkdir on specific MDTs"
22009
22010 test_qos_mkdir() {
22011         local mkdir_cmd=$1
22012         local stripe_count=$2
22013         local mdts=$(comma_list $(mdts_nodes))
22014
22015         local testdir
22016         local lmv_qos_prio_free
22017         local lmv_qos_threshold_rr
22018         local lmv_qos_maxage
22019         local lod_qos_prio_free
22020         local lod_qos_threshold_rr
22021         local lod_qos_maxage
22022         local count
22023         local i
22024
22025         lmv_qos_prio_free=$($LCTL get_param -n lmv.*.qos_prio_free | head -n1)
22026         lmv_qos_prio_free=${lmv_qos_prio_free%%%}
22027         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
22028                 head -n1)
22029         lmv_qos_threshold_rr=${lmv_qos_threshold_rr%%%}
22030         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
22031         stack_trap "$LCTL set_param \
22032                 lmv.*.qos_prio_free=$lmv_qos_prio_free > /dev/null" EXIT
22033         stack_trap "$LCTL set_param \
22034                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null" EXIT
22035         stack_trap "$LCTL set_param \
22036                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null" EXIT
22037
22038         lod_qos_prio_free=$(do_facet mds1 $LCTL get_param -n \
22039                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_prio_free | head -n1)
22040         lod_qos_prio_free=${lod_qos_prio_free%%%}
22041         lod_qos_threshold_rr=$(do_facet mds1 $LCTL get_param -n \
22042                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_threshold_rr | head -n1)
22043         lod_qos_threshold_rr=${lod_qos_threshold_rr%%%}
22044         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
22045                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
22046         stack_trap "do_nodes $mdts $LCTL set_param \
22047                 lod.*.mdt_qos_prio_free=$lod_qos_prio_free > /dev/null" EXIT
22048         stack_trap "do_nodes $mdts $LCTL set_param \
22049                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null" \
22050                 EXIT
22051         stack_trap "do_nodes $mdts $LCTL set_param \
22052                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null" EXIT
22053
22054         echo
22055         echo "Mkdir (stripe_count $stripe_count) roundrobin:"
22056
22057         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
22058         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=100 > /dev/null
22059
22060         testdir=$DIR/$tdir-s$stripe_count/rr
22061
22062         for i in $(seq $((100 * MDSCOUNT))); do
22063                 eval $mkdir_cmd $testdir/subdir$i ||
22064                         error "$mkdir_cmd subdir$i failed"
22065         done
22066
22067         for i in $(seq $MDSCOUNT); do
22068                 count=$($LFS getdirstripe -i $testdir/* |
22069                                 grep ^$((i - 1))$ | wc -l)
22070                 echo "$count directories created on MDT$((i - 1))"
22071                 [ $count -eq 100 ] || error "subdirs are not evenly distributed"
22072
22073                 if [ $stripe_count -gt 1 ]; then
22074                         count=$($LFS getdirstripe $testdir/* |
22075                                 grep -P "^\s+$((i - 1))\t" | wc -l)
22076                         echo "$count stripes created on MDT$((i - 1))"
22077                         # deviation should < 5% of average
22078                         [ $count -lt $((95 * stripe_count)) ] ||
22079                         [ $count -gt $((105 * stripe_count)) ] &&
22080                                 error "stripes are not evenly distributed"
22081                 fi
22082         done
22083
22084         $LCTL set_param lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null
22085         do_nodes $mdts $LCTL set_param \
22086                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null
22087
22088         echo
22089         echo "Check for uneven MDTs: "
22090
22091         local ffree
22092         local bavail
22093         local max
22094         local min
22095         local max_index
22096         local min_index
22097         local tmp
22098
22099         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
22100         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
22101         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
22102
22103         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
22104         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
22105         max_index=0
22106         min_index=0
22107         for ((i = 1; i < ${#ffree[@]}; i++)); do
22108                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
22109                 if [ $tmp -gt $max ]; then
22110                         max=$tmp
22111                         max_index=$i
22112                 fi
22113                 if [ $tmp -lt $min ]; then
22114                         min=$tmp
22115                         min_index=$i
22116                 fi
22117         done
22118
22119         [ ${ffree[min_index]} -eq 0 ] &&
22120                 skip "no free files in MDT$min_index"
22121         [ ${ffree[min_index]} -gt 100000000 ] &&
22122                 skip "too much free files in MDT$min_index"
22123
22124         # Check if we need to generate uneven MDTs
22125         local threshold=50
22126         local diff=$(((max - min) * 100 / min))
22127         local value="$(generate_string 1024)"
22128
22129         while [ $diff -lt $threshold ]; do
22130                 # generate uneven MDTs, create till $threshold% diff
22131                 echo -n "weight diff=$diff% must be > $threshold% ..."
22132                 count=$((${ffree[min_index]} / 10))
22133                 # 50 sec per 10000 files in vm
22134                 [ $count -gt 40000 ] && [ "$SLOW" = "no" ] &&
22135                         skip "$count files to create"
22136                 echo "Fill MDT$min_index with $count files"
22137                 [ -d $DIR/$tdir-MDT$min_index ] ||
22138                         $LFS mkdir -i $min_index $DIR/$tdir-MDT$min_index ||
22139                         error "mkdir $tdir-MDT$min_index failed"
22140                 for i in $(seq $count); do
22141                         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE \
22142                                 $DIR/$tdir-MDT$min_index/f$j_$i > /dev/null ||
22143                                 error "create f$j_$i failed"
22144                         setfattr -n user.413b -v $value \
22145                                 $DIR/$tdir-MDT$min_index/f$j_$i ||
22146                                 error "setfattr f$j_$i failed"
22147                 done
22148
22149                 ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-*.filesfree))
22150                 bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-*.kbytesavail))
22151                 max=$(((${ffree[max_index]} >> 8) * \
22152                         (${bavail[max_index]} * bsize >> 16)))
22153                 min=$(((${ffree[min_index]} >> 8) * \
22154                         (${bavail[min_index]} * bsize >> 16)))
22155                 diff=$(((max - min) * 100 / min))
22156         done
22157
22158         echo "MDT filesfree available: ${ffree[@]}"
22159         echo "MDT blocks available: ${bavail[@]}"
22160         echo "weight diff=$diff%"
22161
22162         echo
22163         echo "Mkdir (stripe_count $stripe_count) with balanced space usage:"
22164
22165         $LCTL set_param lmv.*.qos_prio_free=100 > /dev/null
22166         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_prio_free=100 > /dev/null
22167         # decrease statfs age, so that it can be updated in time
22168         $LCTL set_param lmv.*.qos_maxage=1 > /dev/null
22169         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_maxage=1 > /dev/null
22170
22171         sleep 1
22172
22173         testdir=$DIR/$tdir-s$stripe_count/qos
22174
22175         for i in $(seq $((100 * MDSCOUNT))); do
22176                 eval $mkdir_cmd $testdir/subdir$i ||
22177                         error "$mkdir_cmd subdir$i failed"
22178         done
22179
22180         for i in $(seq $MDSCOUNT); do
22181                 count=$($LFS getdirstripe -i $testdir/* | grep ^$((i - 1))$ |
22182                         wc -l)
22183                 echo "$count directories created on MDT$((i - 1))"
22184
22185                 if [ $stripe_count -gt 1 ]; then
22186                         count=$($LFS getdirstripe $testdir/* |
22187                                 grep -P "^\s+$((i - 1))\t" | wc -l)
22188                         echo "$count stripes created on MDT$((i - 1))"
22189                 fi
22190         done
22191
22192         max=$($LFS getdirstripe -i $testdir/* | grep ^$max_index$ | wc -l)
22193         min=$($LFS getdirstripe -i $testdir/* | grep ^$min_index$ | wc -l)
22194
22195         # D-value should > 10% of averge
22196         [ $((max - min)) -lt 10 ] &&
22197                 error "subdirs shouldn't be evenly distributed"
22198
22199         # ditto
22200         if [ $stripe_count -gt 1 ]; then
22201                 max=$($LFS getdirstripe $testdir/* |
22202                         grep -P "^\s+$max_index\t" | wc -l)
22203                 min=$($LFS getdirstripe $testdir/* |
22204                         grep -P "^\s+$min_index\t" | wc -l)
22205                 [ $((max - min)) -le $((10 * stripe_count)) ] &&
22206                         error "stripes shouldn't be evenly distributed"|| true
22207         fi
22208 }
22209
22210 test_413a() {
22211         [ $MDSCOUNT -lt 2 ] &&
22212                 skip "We need at least 2 MDTs for this test"
22213
22214         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
22215                 skip "Need server version at least 2.12.52"
22216
22217         local stripe_count
22218
22219         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
22220                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
22221                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
22222                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
22223                 test_qos_mkdir "$LFS mkdir -c $stripe_count" $stripe_count
22224         done
22225 }
22226 run_test 413a "QoS mkdir with 'lfs mkdir -i -1'"
22227
22228 test_413b() {
22229         [ $MDSCOUNT -lt 2 ] &&
22230                 skip "We need at least 2 MDTs for this test"
22231
22232         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
22233                 skip "Need server version at least 2.12.52"
22234
22235         local stripe_count
22236
22237         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
22238                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
22239                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
22240                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
22241                 $LFS setdirstripe -D -c $stripe_count \
22242                         $DIR/$tdir-s$stripe_count/rr ||
22243                         error "setdirstripe failed"
22244                 $LFS setdirstripe -D -c $stripe_count \
22245                         $DIR/$tdir-s$stripe_count/qos ||
22246                         error "setdirstripe failed"
22247                 test_qos_mkdir "mkdir" $stripe_count
22248         done
22249 }
22250 run_test 413b "QoS mkdir under dir whose default LMV starting MDT offset is -1"
22251
22252 test_414() {
22253 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
22254         $LCTL set_param fail_loc=0x80000521
22255         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
22256         rm -f $DIR/$tfile
22257 }
22258 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
22259
22260 test_415() {
22261         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22262         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
22263                 skip "Need server version at least 2.11.52"
22264
22265         # LU-11102
22266         local total
22267         local setattr_pid
22268         local start_time
22269         local end_time
22270         local duration
22271
22272         total=500
22273         # this test may be slow on ZFS
22274         [ "$mds1_FSTYPE" == "zfs" ] && total=100
22275
22276         # though this test is designed for striped directory, let's test normal
22277         # directory too since lock is always saved as CoS lock.
22278         test_mkdir $DIR/$tdir || error "mkdir $tdir"
22279         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
22280
22281         (
22282                 while true; do
22283                         touch $DIR/$tdir
22284                 done
22285         ) &
22286         setattr_pid=$!
22287
22288         start_time=$(date +%s)
22289         for i in $(seq $total); do
22290                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
22291                         > /dev/null
22292         done
22293         end_time=$(date +%s)
22294         duration=$((end_time - start_time))
22295
22296         kill -9 $setattr_pid
22297
22298         echo "rename $total files took $duration sec"
22299         [ $duration -lt 100 ] || error "rename took $duration sec"
22300 }
22301 run_test 415 "lock revoke is not missing"
22302
22303 test_416() {
22304         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
22305                 skip "Need server version at least 2.11.55"
22306
22307         # define OBD_FAIL_OSD_TXN_START    0x19a
22308         do_facet mds1 lctl set_param fail_loc=0x19a
22309
22310         lfs mkdir -c $MDSCOUNT $DIR/$tdir
22311
22312         true
22313 }
22314 run_test 416 "transaction start failure won't cause system hung"
22315
22316 cleanup_417() {
22317         trap 0
22318         do_nodes $(comma_list $(mdts_nodes)) \
22319                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
22320         do_nodes $(comma_list $(mdts_nodes)) \
22321                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
22322         do_nodes $(comma_list $(mdts_nodes)) \
22323                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
22324 }
22325
22326 test_417() {
22327         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
22328         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
22329                 skip "Need MDS version at least 2.11.56"
22330
22331         trap cleanup_417 RETURN EXIT
22332
22333         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
22334         do_nodes $(comma_list $(mdts_nodes)) \
22335                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
22336         $LFS migrate -m 0 $DIR/$tdir.1 &&
22337                 error "migrate dir $tdir.1 should fail"
22338
22339         do_nodes $(comma_list $(mdts_nodes)) \
22340                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
22341         $LFS mkdir -i 1 $DIR/$tdir.2 &&
22342                 error "create remote dir $tdir.2 should fail"
22343
22344         do_nodes $(comma_list $(mdts_nodes)) \
22345                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
22346         $LFS mkdir -c 2 $DIR/$tdir.3 &&
22347                 error "create striped dir $tdir.3 should fail"
22348         true
22349 }
22350 run_test 417 "disable remote dir, striped dir and dir migration"
22351
22352 # Checks that the outputs of df [-i] and lfs df [-i] match
22353 #
22354 # usage: check_lfs_df <blocks | inodes> <mountpoint>
22355 check_lfs_df() {
22356         local dir=$2
22357         local inodes
22358         local df_out
22359         local lfs_df_out
22360         local count
22361         local passed=false
22362
22363         # blocks or inodes
22364         [ "$1" == "blocks" ] && inodes= || inodes="-i"
22365
22366         for count in {1..100}; do
22367                 cancel_lru_locks
22368                 sync; sleep 0.2
22369
22370                 # read the lines of interest
22371                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
22372                         error "df $inodes $dir | tail -n +2 failed"
22373                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
22374                         error "lfs df $inodes $dir | grep summary: failed"
22375
22376                 # skip first substrings of each output as they are different
22377                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
22378                 # compare the two outputs
22379                 passed=true
22380                 for i in {1..5}; do
22381                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
22382                 done
22383                 $passed && break
22384         done
22385
22386         if ! $passed; then
22387                 df -P $inodes $dir
22388                 echo
22389                 lfs df $inodes $dir
22390                 error "df and lfs df $1 output mismatch: "      \
22391                       "df ${inodes}: ${df_out[*]}, "            \
22392                       "lfs df ${inodes}: ${lfs_df_out[*]}"
22393         fi
22394 }
22395
22396 test_418() {
22397         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22398
22399         local dir=$DIR/$tdir
22400         local numfiles=$((RANDOM % 4096 + 2))
22401         local numblocks=$((RANDOM % 256 + 1))
22402
22403         wait_delete_completed
22404         test_mkdir $dir
22405
22406         # check block output
22407         check_lfs_df blocks $dir
22408         # check inode output
22409         check_lfs_df inodes $dir
22410
22411         # create a single file and retest
22412         echo "Creating a single file and testing"
22413         createmany -o $dir/$tfile- 1 &>/dev/null ||
22414                 error "creating 1 file in $dir failed"
22415         check_lfs_df blocks $dir
22416         check_lfs_df inodes $dir
22417
22418         # create a random number of files
22419         echo "Creating $((numfiles - 1)) files and testing"
22420         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
22421                 error "creating $((numfiles - 1)) files in $dir failed"
22422
22423         # write a random number of blocks to the first test file
22424         echo "Writing $numblocks 4K blocks and testing"
22425         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
22426                 count=$numblocks &>/dev/null ||
22427                 error "dd to $dir/${tfile}-0 failed"
22428
22429         # retest
22430         check_lfs_df blocks $dir
22431         check_lfs_df inodes $dir
22432
22433         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
22434                 error "unlinking $numfiles files in $dir failed"
22435 }
22436 run_test 418 "df and lfs df outputs match"
22437
22438 test_419()
22439 {
22440         local dir=$DIR/$tdir
22441
22442         mkdir -p $dir
22443         touch $dir/file
22444
22445         cancel_lru_locks mdc
22446
22447         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
22448         $LCTL set_param fail_loc=0x1410
22449         cat $dir/file
22450         $LCTL set_param fail_loc=0
22451         rm -rf $dir
22452 }
22453 run_test 419 "Verify open file by name doesn't crash kernel"
22454
22455 test_420()
22456 {
22457         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
22458                 skip "Need MDS version at least 2.12.53"
22459
22460         local SAVE_UMASK=$(umask)
22461         local dir=$DIR/$tdir
22462         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
22463
22464         mkdir -p $dir
22465         umask 0000
22466         mkdir -m03777 $dir/testdir
22467         ls -dn $dir/testdir
22468         # Need to remove trailing '.' when SELinux is enabled
22469         local dirperms=$(ls -dn $dir/testdir |
22470                          awk '{ sub(/\.$/, "", $1); print $1}')
22471         [ $dirperms == "drwxrwsrwt" ] ||
22472                 error "incorrect perms on $dir/testdir"
22473
22474         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
22475                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
22476         ls -n $dir/testdir/testfile
22477         local fileperms=$(ls -n $dir/testdir/testfile |
22478                           awk '{ sub(/\.$/, "", $1); print $1}')
22479         [ $fileperms == "-rwxr-xr-x" ] ||
22480                 error "incorrect perms on $dir/testdir/testfile"
22481
22482         umask $SAVE_UMASK
22483 }
22484 run_test 420 "clear SGID bit on non-directories for non-members"
22485
22486 test_421a() {
22487         local cnt
22488         local fid1
22489         local fid2
22490
22491         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22492                 skip "Need MDS version at least 2.12.54"
22493
22494         test_mkdir $DIR/$tdir
22495         createmany -o $DIR/$tdir/f 3
22496         cnt=$(ls -1 $DIR/$tdir | wc -l)
22497         [ $cnt != 3 ] && error "unexpected #files: $cnt"
22498
22499         fid1=$(lfs path2fid $DIR/$tdir/f1)
22500         fid2=$(lfs path2fid $DIR/$tdir/f2)
22501         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
22502
22503         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
22504         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
22505
22506         cnt=$(ls -1 $DIR/$tdir | wc -l)
22507         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
22508
22509         rm -f $DIR/$tdir/f3 || error "can't remove f3"
22510         createmany -o $DIR/$tdir/f 3
22511         cnt=$(ls -1 $DIR/$tdir | wc -l)
22512         [ $cnt != 3 ] && error "unexpected #files: $cnt"
22513
22514         fid1=$(lfs path2fid $DIR/$tdir/f1)
22515         fid2=$(lfs path2fid $DIR/$tdir/f2)
22516         echo "remove using fsname $FSNAME"
22517         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
22518
22519         cnt=$(ls -1 $DIR/$tdir | wc -l)
22520         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
22521 }
22522 run_test 421a "simple rm by fid"
22523
22524 test_421b() {
22525         local cnt
22526         local FID1
22527         local FID2
22528
22529         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22530                 skip "Need MDS version at least 2.12.54"
22531
22532         test_mkdir $DIR/$tdir
22533         createmany -o $DIR/$tdir/f 3
22534         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
22535         MULTIPID=$!
22536
22537         FID1=$(lfs path2fid $DIR/$tdir/f1)
22538         FID2=$(lfs path2fid $DIR/$tdir/f2)
22539         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
22540
22541         kill -USR1 $MULTIPID
22542         wait
22543
22544         cnt=$(ls $DIR/$tdir | wc -l)
22545         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
22546 }
22547 run_test 421b "rm by fid on open file"
22548
22549 test_421c() {
22550         local cnt
22551         local FIDS
22552
22553         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22554                 skip "Need MDS version at least 2.12.54"
22555
22556         test_mkdir $DIR/$tdir
22557         createmany -o $DIR/$tdir/f 3
22558         touch $DIR/$tdir/$tfile
22559         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
22560         cnt=$(ls -1 $DIR/$tdir | wc -l)
22561         [ $cnt != 184 ] && error "unexpected #files: $cnt"
22562
22563         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
22564         $LFS rmfid $DIR $FID1 || error "rmfid failed"
22565
22566         cnt=$(ls $DIR/$tdir | wc -l)
22567         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
22568 }
22569 run_test 421c "rm by fid against hardlinked files"
22570
22571 test_421d() {
22572         local cnt
22573         local FIDS
22574
22575         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22576                 skip "Need MDS version at least 2.12.54"
22577
22578         test_mkdir $DIR/$tdir
22579         createmany -o $DIR/$tdir/f 4097
22580         cnt=$(ls -1 $DIR/$tdir | wc -l)
22581         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
22582
22583         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
22584         $LFS rmfid $DIR $FIDS || error "rmfid failed"
22585
22586         cnt=$(ls $DIR/$tdir | wc -l)
22587         rm -rf $DIR/$tdir
22588         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
22589 }
22590 run_test 421d "rmfid en masse"
22591
22592 test_421e() {
22593         local cnt
22594         local FID
22595
22596         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
22597         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22598                 skip "Need MDS version at least 2.12.54"
22599
22600         mkdir -p $DIR/$tdir
22601         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
22602         createmany -o $DIR/$tdir/striped_dir/f 512
22603         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
22604         [ $cnt != 512 ] && error "unexpected #files: $cnt"
22605
22606         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
22607                 sed "s/[/][^:]*://g")
22608         $LFS rmfid $DIR $FIDS || error "rmfid failed"
22609
22610         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
22611         rm -rf $DIR/$tdir
22612         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
22613 }
22614 run_test 421e "rmfid in DNE"
22615
22616 test_421f() {
22617         local cnt
22618         local FID
22619
22620         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22621                 skip "Need MDS version at least 2.12.54"
22622
22623         test_mkdir $DIR/$tdir
22624         touch $DIR/$tdir/f
22625         cnt=$(ls -1 $DIR/$tdir | wc -l)
22626         [ $cnt != 1 ] && error "unexpected #files: $cnt"
22627
22628         FID=$(lfs path2fid $DIR/$tdir/f)
22629         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
22630         # rmfid should fail
22631         cnt=$(ls -1 $DIR/$tdir | wc -l)
22632         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
22633
22634         chmod a+rw $DIR/$tdir
22635         ls -la $DIR/$tdir
22636         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
22637         # rmfid should fail
22638         cnt=$(ls -1 $DIR/$tdir | wc -l)
22639         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
22640
22641         rm -f $DIR/$tdir/f
22642         $RUNAS touch $DIR/$tdir/f
22643         FID=$(lfs path2fid $DIR/$tdir/f)
22644         echo "rmfid as root"
22645         $LFS rmfid $DIR $FID || error "rmfid as root failed"
22646         cnt=$(ls -1 $DIR/$tdir | wc -l)
22647         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
22648
22649         rm -f $DIR/$tdir/f
22650         $RUNAS touch $DIR/$tdir/f
22651         cnt=$(ls -1 $DIR/$tdir | wc -l)
22652         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
22653         FID=$(lfs path2fid $DIR/$tdir/f)
22654         # rmfid w/o user_fid2path mount option should fail
22655         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
22656         cnt=$(ls -1 $DIR/$tdir | wc -l)
22657         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
22658
22659         umount_client $MOUNT || error "failed to umount client"
22660         mount_client $MOUNT "$MOUNT_OPTS,user_fid2path" ||
22661                 error "failed to mount client'"
22662
22663         $RUNAS $LFS rmfid $DIR $FID || error "rmfid failed"
22664         # rmfid should succeed
22665         cnt=$(ls -1 $DIR/$tdir | wc -l)
22666         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
22667
22668         # rmfid shouldn't allow to remove files due to dir's permission
22669         chmod a+rwx $DIR/$tdir
22670         touch $DIR/$tdir/f
22671         ls -la $DIR/$tdir
22672         FID=$(lfs path2fid $DIR/$tdir/f)
22673         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail"
22674
22675         umount_client $MOUNT || error "failed to umount client"
22676         mount_client $MOUNT "$MOUNT_OPTS" ||
22677                 error "failed to mount client'"
22678
22679 }
22680 run_test 421f "rmfid checks permissions"
22681
22682 test_421g() {
22683         local cnt
22684         local FIDS
22685
22686         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
22687         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22688                 skip "Need MDS version at least 2.12.54"
22689
22690         mkdir -p $DIR/$tdir
22691         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
22692         createmany -o $DIR/$tdir/striped_dir/f 512
22693         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
22694         [ $cnt != 512 ] && error "unexpected #files: $cnt"
22695
22696         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
22697                 sed "s/[/][^:]*://g")
22698
22699         rm -f $DIR/$tdir/striped_dir/f1*
22700         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
22701         removed=$((512 - cnt))
22702
22703         # few files have been just removed, so we expect
22704         # rmfid to fail on their fids
22705         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
22706         [ $removed != $errors ] && error "$errors != $removed"
22707
22708         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
22709         rm -rf $DIR/$tdir
22710         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
22711 }
22712 run_test 421g "rmfid to return errors properly"
22713
22714 test_422() {
22715         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d1
22716         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d2
22717         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d3
22718         dd if=/dev/zero of=$DIR/$tdir/d1/file1 bs=1k count=1
22719         dd if=/dev/zero of=$DIR/$tdir/d2/file1 bs=1k count=1
22720
22721         local amc=$(at_max_get client)
22722         local amo=$(at_max_get mds1)
22723         local timeout=`lctl get_param -n timeout`
22724
22725         at_max_set 0 client
22726         at_max_set 0 mds1
22727
22728 #define OBD_FAIL_PTLRPC_PAUSE_REQ        0x50a
22729         do_facet mds1 $LCTL set_param fail_loc=0x8000050a \
22730                         fail_val=$(((2*timeout + 10)*1000))
22731         touch $DIR/$tdir/d3/file &
22732         sleep 2
22733 #define OBD_FAIL_TGT_REPLY_DATA_RACE     0x722
22734         do_facet mds1 $LCTL set_param fail_loc=0x80000722 \
22735                         fail_val=$((2*timeout + 5))
22736         mv $DIR/$tdir/d1/file1 $DIR/$tdir/d1/file2 &
22737         local pid=$!
22738         sleep 1
22739         kill -9 $pid
22740         sleep $((2 * timeout))
22741         echo kill $pid
22742         kill -9 $pid
22743         lctl mark touch
22744         touch $DIR/$tdir/d2/file3
22745         touch $DIR/$tdir/d2/file4
22746         touch $DIR/$tdir/d2/file5
22747
22748         wait
22749         at_max_set $amc client
22750         at_max_set $amo mds1
22751
22752         # LU-12838 - verify the ptlrpc thread watchdog is not always throttled
22753         do_facet mds1 "dmesg | grep 'Dumping the stack trace for debugging'" ||
22754                 error "Watchdog is always throttled"
22755 }
22756 run_test 422 "kill a process with RPC in progress"
22757
22758 stat_test() {
22759     df -h $MOUNT &
22760     df -h $MOUNT &
22761     df -h $MOUNT &
22762     df -h $MOUNT &
22763     df -h $MOUNT &
22764     df -h $MOUNT &
22765 }
22766
22767 test_423() {
22768     local _stats
22769     # ensure statfs cache is expired
22770     sleep 2;
22771
22772     _stats=$(stat_test | grep $MOUNT | sort -u | wc -l)
22773     [[ ${_stats} -ne 1 ]] && error "statfs wrong"
22774
22775     return 0
22776 }
22777 run_test 423 "statfs should return a right data"
22778
22779 prep_801() {
22780         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
22781         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
22782                 skip "Need server version at least 2.9.55"
22783
22784         start_full_debug_logging
22785 }
22786
22787 post_801() {
22788         stop_full_debug_logging
22789 }
22790
22791 barrier_stat() {
22792         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
22793                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
22794                            awk '/The barrier for/ { print $7 }')
22795                 echo $st
22796         else
22797                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
22798                 echo \'$st\'
22799         fi
22800 }
22801
22802 barrier_expired() {
22803         local expired
22804
22805         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
22806                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
22807                           awk '/will be expired/ { print $7 }')
22808         else
22809                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
22810         fi
22811
22812         echo $expired
22813 }
22814
22815 test_801a() {
22816         prep_801
22817
22818         echo "Start barrier_freeze at: $(date)"
22819         #define OBD_FAIL_BARRIER_DELAY          0x2202
22820         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
22821         # Do not reduce barrier time - See LU-11873
22822         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
22823
22824         sleep 2
22825         local b_status=$(barrier_stat)
22826         echo "Got barrier status at: $(date)"
22827         [ "$b_status" = "'freezing_p1'" ] ||
22828                 error "(1) unexpected barrier status $b_status"
22829
22830         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
22831         wait
22832         b_status=$(barrier_stat)
22833         [ "$b_status" = "'frozen'" ] ||
22834                 error "(2) unexpected barrier status $b_status"
22835
22836         local expired=$(barrier_expired)
22837         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
22838         sleep $((expired + 3))
22839
22840         b_status=$(barrier_stat)
22841         [ "$b_status" = "'expired'" ] ||
22842                 error "(3) unexpected barrier status $b_status"
22843
22844         # Do not reduce barrier time - See LU-11873
22845         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
22846                 error "(4) fail to freeze barrier"
22847
22848         b_status=$(barrier_stat)
22849         [ "$b_status" = "'frozen'" ] ||
22850                 error "(5) unexpected barrier status $b_status"
22851
22852         echo "Start barrier_thaw at: $(date)"
22853         #define OBD_FAIL_BARRIER_DELAY          0x2202
22854         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
22855         do_facet mgs $LCTL barrier_thaw $FSNAME &
22856
22857         sleep 2
22858         b_status=$(barrier_stat)
22859         echo "Got barrier status at: $(date)"
22860         [ "$b_status" = "'thawing'" ] ||
22861                 error "(6) unexpected barrier status $b_status"
22862
22863         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
22864         wait
22865         b_status=$(barrier_stat)
22866         [ "$b_status" = "'thawed'" ] ||
22867                 error "(7) unexpected barrier status $b_status"
22868
22869         #define OBD_FAIL_BARRIER_FAILURE        0x2203
22870         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
22871         do_facet mgs $LCTL barrier_freeze $FSNAME
22872
22873         b_status=$(barrier_stat)
22874         [ "$b_status" = "'failed'" ] ||
22875                 error "(8) unexpected barrier status $b_status"
22876
22877         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
22878         do_facet mgs $LCTL barrier_thaw $FSNAME
22879
22880         post_801
22881 }
22882 run_test 801a "write barrier user interfaces and stat machine"
22883
22884 test_801b() {
22885         prep_801
22886
22887         mkdir $DIR/$tdir || error "(1) fail to mkdir"
22888         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
22889         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
22890         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
22891         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
22892
22893         cancel_lru_locks mdc
22894
22895         # 180 seconds should be long enough
22896         do_facet mgs $LCTL barrier_freeze $FSNAME 180
22897
22898         local b_status=$(barrier_stat)
22899         [ "$b_status" = "'frozen'" ] ||
22900                 error "(6) unexpected barrier status $b_status"
22901
22902         mkdir $DIR/$tdir/d0/d10 &
22903         mkdir_pid=$!
22904
22905         touch $DIR/$tdir/d1/f13 &
22906         touch_pid=$!
22907
22908         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
22909         ln_pid=$!
22910
22911         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
22912         mv_pid=$!
22913
22914         rm -f $DIR/$tdir/d4/f12 &
22915         rm_pid=$!
22916
22917         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
22918
22919         # To guarantee taht the 'stat' is not blocked
22920         b_status=$(barrier_stat)
22921         [ "$b_status" = "'frozen'" ] ||
22922                 error "(8) unexpected barrier status $b_status"
22923
22924         # let above commands to run at background
22925         sleep 5
22926
22927         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
22928         ps -p $touch_pid || error "(10) touch should be blocked"
22929         ps -p $ln_pid || error "(11) link should be blocked"
22930         ps -p $mv_pid || error "(12) rename should be blocked"
22931         ps -p $rm_pid || error "(13) unlink should be blocked"
22932
22933         b_status=$(barrier_stat)
22934         [ "$b_status" = "'frozen'" ] ||
22935                 error "(14) unexpected barrier status $b_status"
22936
22937         do_facet mgs $LCTL barrier_thaw $FSNAME
22938         b_status=$(barrier_stat)
22939         [ "$b_status" = "'thawed'" ] ||
22940                 error "(15) unexpected barrier status $b_status"
22941
22942         wait $mkdir_pid || error "(16) mkdir should succeed"
22943         wait $touch_pid || error "(17) touch should succeed"
22944         wait $ln_pid || error "(18) link should succeed"
22945         wait $mv_pid || error "(19) rename should succeed"
22946         wait $rm_pid || error "(20) unlink should succeed"
22947
22948         post_801
22949 }
22950 run_test 801b "modification will be blocked by write barrier"
22951
22952 test_801c() {
22953         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22954
22955         prep_801
22956
22957         stop mds2 || error "(1) Fail to stop mds2"
22958
22959         do_facet mgs $LCTL barrier_freeze $FSNAME 30
22960
22961         local b_status=$(barrier_stat)
22962         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
22963                 do_facet mgs $LCTL barrier_thaw $FSNAME
22964                 error "(2) unexpected barrier status $b_status"
22965         }
22966
22967         do_facet mgs $LCTL barrier_rescan $FSNAME ||
22968                 error "(3) Fail to rescan barrier bitmap"
22969
22970         # Do not reduce barrier time - See LU-11873
22971         do_facet mgs $LCTL barrier_freeze $FSNAME 20
22972
22973         b_status=$(barrier_stat)
22974         [ "$b_status" = "'frozen'" ] ||
22975                 error "(4) unexpected barrier status $b_status"
22976
22977         do_facet mgs $LCTL barrier_thaw $FSNAME
22978         b_status=$(barrier_stat)
22979         [ "$b_status" = "'thawed'" ] ||
22980                 error "(5) unexpected barrier status $b_status"
22981
22982         local devname=$(mdsdevname 2)
22983
22984         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
22985
22986         do_facet mgs $LCTL barrier_rescan $FSNAME ||
22987                 error "(7) Fail to rescan barrier bitmap"
22988
22989         post_801
22990 }
22991 run_test 801c "rescan barrier bitmap"
22992
22993 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
22994 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
22995 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
22996 saved_MOUNT_OPTS=$MOUNT_OPTS
22997
22998 cleanup_802a() {
22999         trap 0
23000
23001         stopall
23002         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
23003         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
23004         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
23005         MOUNT_OPTS=$saved_MOUNT_OPTS
23006         setupall
23007 }
23008
23009 test_802a() {
23010         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
23011         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
23012         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
23013                 skip "Need server version at least 2.9.55"
23014
23015         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
23016
23017         mkdir $DIR/$tdir || error "(1) fail to mkdir"
23018
23019         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
23020                 error "(2) Fail to copy"
23021
23022         trap cleanup_802a EXIT
23023
23024         # sync by force before remount as readonly
23025         sync; sync_all_data; sleep 3; sync_all_data
23026
23027         stopall
23028
23029         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
23030         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
23031         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
23032
23033         echo "Mount the server as read only"
23034         setupall server_only || error "(3) Fail to start servers"
23035
23036         echo "Mount client without ro should fail"
23037         mount_client $MOUNT &&
23038                 error "(4) Mount client without 'ro' should fail"
23039
23040         echo "Mount client with ro should succeed"
23041         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
23042         mount_client $MOUNT ||
23043                 error "(5) Mount client with 'ro' should succeed"
23044
23045         echo "Modify should be refused"
23046         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
23047
23048         echo "Read should be allowed"
23049         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
23050                 error "(7) Read should succeed under ro mode"
23051
23052         cleanup_802a
23053 }
23054 run_test 802a "simulate readonly device"
23055
23056 test_802b() {
23057         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23058         remote_mds_nodsh && skip "remote MDS with nodsh"
23059
23060         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
23061                 skip "readonly option not available"
23062
23063         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
23064
23065         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
23066                 error "(2) Fail to copy"
23067
23068         # write back all cached data before setting MDT to readonly
23069         cancel_lru_locks
23070         sync_all_data
23071
23072         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
23073         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
23074
23075         echo "Modify should be refused"
23076         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
23077
23078         echo "Read should be allowed"
23079         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
23080                 error "(7) Read should succeed under ro mode"
23081
23082         # disable readonly
23083         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
23084 }
23085 run_test 802b "be able to set MDTs to readonly"
23086
23087 test_803() {
23088         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
23089         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
23090                 skip "MDS needs to be newer than 2.10.54"
23091
23092         mkdir -p $DIR/$tdir
23093         # Create some objects on all MDTs to trigger related logs objects
23094         for idx in $(seq $MDSCOUNT); do
23095                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
23096                         $DIR/$tdir/dir${idx} ||
23097                         error "Fail to create $DIR/$tdir/dir${idx}"
23098         done
23099
23100         sync; sleep 3
23101         wait_delete_completed # ensure old test cleanups are finished
23102         echo "before create:"
23103         $LFS df -i $MOUNT
23104         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
23105
23106         for i in {1..10}; do
23107                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
23108                         error "Fail to create $DIR/$tdir/foo$i"
23109         done
23110
23111         sync; sleep 3
23112         echo "after create:"
23113         $LFS df -i $MOUNT
23114         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
23115
23116         # allow for an llog to be cleaned up during the test
23117         [ $after_used -ge $((before_used + 10 - 1)) ] ||
23118                 error "before ($before_used) + 10 > after ($after_used)"
23119
23120         for i in {1..10}; do
23121                 rm -rf $DIR/$tdir/foo$i ||
23122                         error "Fail to remove $DIR/$tdir/foo$i"
23123         done
23124
23125         sleep 3 # avoid MDT return cached statfs
23126         wait_delete_completed
23127         echo "after unlink:"
23128         $LFS df -i $MOUNT
23129         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
23130
23131         # allow for an llog to be created during the test
23132         [ $after_used -le $((before_used + 1)) ] ||
23133                 error "after ($after_used) > before ($before_used) + 1"
23134 }
23135 run_test 803 "verify agent object for remote object"
23136
23137 test_804() {
23138         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
23139         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
23140                 skip "MDS needs to be newer than 2.10.54"
23141         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
23142
23143         mkdir -p $DIR/$tdir
23144         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
23145                 error "Fail to create $DIR/$tdir/dir0"
23146
23147         local fid=$($LFS path2fid $DIR/$tdir/dir0)
23148         local dev=$(mdsdevname 2)
23149
23150         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
23151                 grep ${fid} || error "NOT found agent entry for dir0"
23152
23153         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
23154                 error "Fail to create $DIR/$tdir/dir1"
23155
23156         touch $DIR/$tdir/dir1/foo0 ||
23157                 error "Fail to create $DIR/$tdir/dir1/foo0"
23158         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
23159         local rc=0
23160
23161         for idx in $(seq $MDSCOUNT); do
23162                 dev=$(mdsdevname $idx)
23163                 do_facet mds${idx} \
23164                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
23165                         grep ${fid} && rc=$idx
23166         done
23167
23168         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
23169                 error "Fail to rename foo0 to foo1"
23170         if [ $rc -eq 0 ]; then
23171                 for idx in $(seq $MDSCOUNT); do
23172                         dev=$(mdsdevname $idx)
23173                         do_facet mds${idx} \
23174                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
23175                         grep ${fid} && rc=$idx
23176                 done
23177         fi
23178
23179         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
23180                 error "Fail to rename foo1 to foo2"
23181         if [ $rc -eq 0 ]; then
23182                 for idx in $(seq $MDSCOUNT); do
23183                         dev=$(mdsdevname $idx)
23184                         do_facet mds${idx} \
23185                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
23186                         grep ${fid} && rc=$idx
23187                 done
23188         fi
23189
23190         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
23191
23192         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
23193                 error "Fail to link to $DIR/$tdir/dir1/foo2"
23194         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
23195                 error "Fail to rename foo2 to foo0"
23196         unlink $DIR/$tdir/dir1/foo0 ||
23197                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
23198         rm -rf $DIR/$tdir/dir0 ||
23199                 error "Fail to rm $DIR/$tdir/dir0"
23200
23201         for idx in $(seq $MDSCOUNT); do
23202                 dev=$(mdsdevname $idx)
23203                 rc=0
23204
23205                 stop mds${idx}
23206                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
23207                         rc=$?
23208                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
23209                         error "mount mds$idx failed"
23210                 df $MOUNT > /dev/null 2>&1
23211
23212                 # e2fsck should not return error
23213                 [ $rc -eq 0 ] ||
23214                         error "e2fsck detected error on MDT${idx}: rc=$rc"
23215         done
23216 }
23217 run_test 804 "verify agent entry for remote entry"
23218
23219 cleanup_805() {
23220         do_facet $SINGLEMDS zfs set quota=$old $fsset
23221         unlinkmany $DIR/$tdir/f- 1000000
23222         trap 0
23223 }
23224
23225 test_805() {
23226         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
23227         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
23228         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
23229                 skip "netfree not implemented before 0.7"
23230         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
23231                 skip "Need MDS version at least 2.10.57"
23232
23233         local fsset
23234         local freekb
23235         local usedkb
23236         local old
23237         local quota
23238         local pref="osd-zfs.$FSNAME-MDT0000."
23239
23240         # limit available space on MDS dataset to meet nospace issue
23241         # quickly. then ZFS 0.7.2 can use reserved space if asked
23242         # properly (using netfree flag in osd_declare_destroy()
23243         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
23244         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
23245                 gawk '{print $3}')
23246         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
23247         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
23248         let "usedkb=usedkb-freekb"
23249         let "freekb=freekb/2"
23250         if let "freekb > 5000"; then
23251                 let "freekb=5000"
23252         fi
23253         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
23254         trap cleanup_805 EXIT
23255         mkdir $DIR/$tdir
23256         $LFS setstripe -E 1M -c2 -E 4M -c2 -E -1 -c2 $DIR/$tdir ||
23257                 error "Can't set PFL layout"
23258         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
23259         rm -rf $DIR/$tdir || error "not able to remove"
23260         do_facet $SINGLEMDS zfs set quota=$old $fsset
23261         trap 0
23262 }
23263 run_test 805 "ZFS can remove from full fs"
23264
23265 # Size-on-MDS test
23266 check_lsom_data()
23267 {
23268         local file=$1
23269         local size=$($LFS getsom -s $file)
23270         local expect=$(stat -c %s $file)
23271
23272         [[ $size == $expect ]] ||
23273                 error "$file expected size: $expect, got: $size"
23274
23275         local blocks=$($LFS getsom -b $file)
23276         expect=$(stat -c %b $file)
23277         [[ $blocks == $expect ]] ||
23278                 error "$file expected blocks: $expect, got: $blocks"
23279 }
23280
23281 check_lsom_size()
23282 {
23283         local size=$($LFS getsom -s $1)
23284         local expect=$2
23285
23286         [[ $size == $expect ]] ||
23287                 error "$file expected size: $expect, got: $size"
23288 }
23289
23290 test_806() {
23291         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
23292                 skip "Need MDS version at least 2.11.52"
23293
23294         local bs=1048576
23295
23296         touch $DIR/$tfile || error "touch $tfile failed"
23297
23298         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
23299         save_lustre_params client "llite.*.xattr_cache" > $save
23300         lctl set_param llite.*.xattr_cache=0
23301         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
23302
23303         # single-threaded write
23304         echo "Test SOM for single-threaded write"
23305         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
23306                 error "write $tfile failed"
23307         check_lsom_size $DIR/$tfile $bs
23308
23309         local num=32
23310         local size=$(($num * $bs))
23311         local offset=0
23312         local i
23313
23314         echo "Test SOM for single client multi-threaded($num) write"
23315         $TRUNCATE $DIR/$tfile 0
23316         for ((i = 0; i < $num; i++)); do
23317                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
23318                 local pids[$i]=$!
23319                 offset=$((offset + $bs))
23320         done
23321         for (( i=0; i < $num; i++ )); do
23322                 wait ${pids[$i]}
23323         done
23324         check_lsom_size $DIR/$tfile $size
23325
23326         $TRUNCATE $DIR/$tfile 0
23327         for ((i = 0; i < $num; i++)); do
23328                 offset=$((offset - $bs))
23329                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
23330                 local pids[$i]=$!
23331         done
23332         for (( i=0; i < $num; i++ )); do
23333                 wait ${pids[$i]}
23334         done
23335         check_lsom_size $DIR/$tfile $size
23336
23337         # multi-client writes
23338         num=$(get_node_count ${CLIENTS//,/ })
23339         size=$(($num * $bs))
23340         offset=0
23341         i=0
23342
23343         echo "Test SOM for multi-client ($num) writes"
23344         $TRUNCATE $DIR/$tfile 0
23345         for client in ${CLIENTS//,/ }; do
23346                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
23347                 local pids[$i]=$!
23348                 i=$((i + 1))
23349                 offset=$((offset + $bs))
23350         done
23351         for (( i=0; i < $num; i++ )); do
23352                 wait ${pids[$i]}
23353         done
23354         check_lsom_size $DIR/$tfile $offset
23355
23356         i=0
23357         $TRUNCATE $DIR/$tfile 0
23358         for client in ${CLIENTS//,/ }; do
23359                 offset=$((offset - $bs))
23360                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
23361                 local pids[$i]=$!
23362                 i=$((i + 1))
23363         done
23364         for (( i=0; i < $num; i++ )); do
23365                 wait ${pids[$i]}
23366         done
23367         check_lsom_size $DIR/$tfile $size
23368
23369         # verify truncate
23370         echo "Test SOM for truncate"
23371         $TRUNCATE $DIR/$tfile 1048576
23372         check_lsom_size $DIR/$tfile 1048576
23373         $TRUNCATE $DIR/$tfile 1234
23374         check_lsom_size $DIR/$tfile 1234
23375
23376         # verify SOM blocks count
23377         echo "Verify SOM block count"
23378         $TRUNCATE $DIR/$tfile 0
23379         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
23380                 error "failed to write file $tfile"
23381         check_lsom_data $DIR/$tfile
23382 }
23383 run_test 806 "Verify Lazy Size on MDS"
23384
23385 test_807() {
23386         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
23387         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
23388                 skip "Need MDS version at least 2.11.52"
23389
23390         # Registration step
23391         changelog_register || error "changelog_register failed"
23392         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
23393         changelog_users $SINGLEMDS | grep -q $cl_user ||
23394                 error "User $cl_user not found in changelog_users"
23395
23396         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
23397         save_lustre_params client "llite.*.xattr_cache" > $save
23398         lctl set_param llite.*.xattr_cache=0
23399         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
23400
23401         rm -rf $DIR/$tdir || error "rm $tdir failed"
23402         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
23403         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
23404         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
23405         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
23406                 error "truncate $tdir/trunc failed"
23407
23408         local bs=1048576
23409         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 conv=fsync ||
23410                 error "write $tfile failed"
23411
23412         # multi-client wirtes
23413         local num=$(get_node_count ${CLIENTS//,/ })
23414         local offset=0
23415         local i=0
23416
23417         echo "Test SOM for multi-client ($num) writes"
23418         touch $DIR/$tfile || error "touch $tfile failed"
23419         $TRUNCATE $DIR/$tfile 0
23420         for client in ${CLIENTS//,/ }; do
23421                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
23422                 local pids[$i]=$!
23423                 i=$((i + 1))
23424                 offset=$((offset + $bs))
23425         done
23426         for (( i=0; i < $num; i++ )); do
23427                 wait ${pids[$i]}
23428         done
23429
23430         do_rpc_nodes "$CLIENTS" cancel_lru_locks osc
23431         do_nodes "$CLIENTS" "sync ; sleep 5 ; sync"
23432         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
23433         check_lsom_data $DIR/$tdir/trunc
23434         check_lsom_data $DIR/$tdir/single_dd
23435         check_lsom_data $DIR/$tfile
23436
23437         rm -rf $DIR/$tdir
23438         # Deregistration step
23439         changelog_deregister || error "changelog_deregister failed"
23440 }
23441 run_test 807 "verify LSOM syncing tool"
23442
23443 check_som_nologged()
23444 {
23445         local lines=$($LFS changelog $FSNAME-MDT0000 |
23446                 grep 'x=trusted.som' | wc -l)
23447         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
23448 }
23449
23450 test_808() {
23451         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
23452                 skip "Need MDS version at least 2.11.55"
23453
23454         # Registration step
23455         changelog_register || error "changelog_register failed"
23456
23457         touch $DIR/$tfile || error "touch $tfile failed"
23458         check_som_nologged
23459
23460         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
23461                 error "write $tfile failed"
23462         check_som_nologged
23463
23464         $TRUNCATE $DIR/$tfile 1234
23465         check_som_nologged
23466
23467         $TRUNCATE $DIR/$tfile 1048576
23468         check_som_nologged
23469
23470         # Deregistration step
23471         changelog_deregister || error "changelog_deregister failed"
23472 }
23473 run_test 808 "Check trusted.som xattr not logged in Changelogs"
23474
23475 check_som_nodata()
23476 {
23477         $LFS getsom $1
23478         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
23479 }
23480
23481 test_809() {
23482         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
23483                 skip "Need MDS version at least 2.11.56"
23484
23485         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
23486                 error "failed to create DoM-only file $DIR/$tfile"
23487         touch $DIR/$tfile || error "touch $tfile failed"
23488         check_som_nodata $DIR/$tfile
23489
23490         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
23491                 error "write $tfile failed"
23492         check_som_nodata $DIR/$tfile
23493
23494         $TRUNCATE $DIR/$tfile 1234
23495         check_som_nodata $DIR/$tfile
23496
23497         $TRUNCATE $DIR/$tfile 4097
23498         check_som_nodata $DIR/$file
23499 }
23500 run_test 809 "Verify no SOM xattr store for DoM-only files"
23501
23502 test_810() {
23503         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23504         $GSS && skip_env "could not run with gss"
23505         [[ $OST1_VERSION -gt $(version_code 2.12.58) ]] ||
23506                 skip "OST < 2.12.58 doesn't align checksum"
23507
23508         set_checksums 1
23509         stack_trap "set_checksums $ORIG_CSUM" EXIT
23510         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
23511
23512         local csum
23513         local before
23514         local after
23515         for csum in $CKSUM_TYPES; do
23516                 #define OBD_FAIL_OSC_NO_GRANT   0x411
23517                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
23518                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
23519                         eval set -- $i
23520                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
23521                         before=$(md5sum $DIR/$tfile)
23522                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
23523                         after=$(md5sum $DIR/$tfile)
23524                         [ "$before" == "$after" ] ||
23525                                 error "$csum: $before != $after bs=$1 seek=$2"
23526                 done
23527         done
23528 }
23529 run_test 810 "partial page writes on ZFS (LU-11663)"
23530
23531 test_812a() {
23532         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
23533                 skip "OST < 2.12.51 doesn't support this fail_loc"
23534         [ "$SHARED_KEY" = true ] &&
23535                 skip "OSC connections never go IDLE with Shared-Keys enabled"
23536
23537         $LFS setstripe -c 1 -i 0 $DIR/$tfile
23538         # ensure ost1 is connected
23539         stat $DIR/$tfile >/dev/null || error "can't stat"
23540         wait_osc_import_state client ost1 FULL
23541         # no locks, no reqs to let the connection idle
23542         cancel_lru_locks osc
23543
23544         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
23545 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
23546         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
23547         wait_osc_import_state client ost1 CONNECTING
23548         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
23549
23550         stat $DIR/$tfile >/dev/null || error "can't stat file"
23551 }
23552 run_test 812a "do not drop reqs generated when imp is going to idle (LU-11951)"
23553
23554 test_812b() { # LU-12378
23555         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
23556                 skip "OST < 2.12.51 doesn't support this fail_loc"
23557         [ "$SHARED_KEY" = true ] &&
23558                 skip "OSC connections never go IDLE with Shared-Keys enabled"
23559
23560         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "setstripe failed"
23561         # ensure ost1 is connected
23562         stat $DIR/$tfile >/dev/null || error "can't stat"
23563         wait_osc_import_state client ost1 FULL
23564         # no locks, no reqs to let the connection idle
23565         cancel_lru_locks osc
23566
23567         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
23568 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
23569         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
23570         wait_osc_import_state client ost1 CONNECTING
23571         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
23572
23573         $LFS quota -u 0 $DIR/ || error "lfs quota should succeed"
23574         wait_osc_import_state client ost1 IDLE
23575 }
23576 run_test 812b "do not drop no resend request for idle connect"
23577
23578 test_813() {
23579         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
23580         [ -z "$file_heat_sav" ] && skip "no file heat support"
23581
23582         local readsample
23583         local writesample
23584         local readbyte
23585         local writebyte
23586         local readsample1
23587         local writesample1
23588         local readbyte1
23589         local writebyte1
23590
23591         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
23592         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
23593
23594         $LCTL set_param -n llite.*.file_heat=1
23595         echo "Turn on file heat"
23596         echo "Period second: $period_second, Decay percentage: $decay_pct"
23597
23598         echo "QQQQ" > $DIR/$tfile
23599         echo "QQQQ" > $DIR/$tfile
23600         echo "QQQQ" > $DIR/$tfile
23601         cat $DIR/$tfile > /dev/null
23602         cat $DIR/$tfile > /dev/null
23603         cat $DIR/$tfile > /dev/null
23604         cat $DIR/$tfile > /dev/null
23605
23606         local out=$($LFS heat_get $DIR/$tfile)
23607
23608         $LFS heat_get $DIR/$tfile
23609         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23610         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23611         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23612         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23613
23614         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
23615         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
23616         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
23617         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
23618
23619         sleep $((period_second + 3))
23620         echo "Sleep $((period_second + 3)) seconds..."
23621         # The recursion formula to calculate the heat of the file f is as
23622         # follow:
23623         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
23624         # Where Hi is the heat value in the period between time points i*I and
23625         # (i+1)*I; Ci is the access count in the period; the symbol P refers
23626         # to the weight of Ci.
23627         out=$($LFS heat_get $DIR/$tfile)
23628         $LFS heat_get $DIR/$tfile
23629         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23630         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23631         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23632         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23633
23634         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
23635                 error "read sample ($readsample) is wrong"
23636         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
23637                 error "write sample ($writesample) is wrong"
23638         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
23639                 error "read bytes ($readbyte) is wrong"
23640         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
23641                 error "write bytes ($writebyte) is wrong"
23642
23643         echo "QQQQ" > $DIR/$tfile
23644         echo "QQQQ" > $DIR/$tfile
23645         echo "QQQQ" > $DIR/$tfile
23646         cat $DIR/$tfile > /dev/null
23647         cat $DIR/$tfile > /dev/null
23648         cat $DIR/$tfile > /dev/null
23649         cat $DIR/$tfile > /dev/null
23650
23651         sleep $((period_second + 3))
23652         echo "Sleep $((period_second + 3)) seconds..."
23653
23654         out=$($LFS heat_get $DIR/$tfile)
23655         $LFS heat_get $DIR/$tfile
23656         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23657         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23658         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23659         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23660
23661         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
23662                 4 * $decay_pct) / 100") -eq 1 ] ||
23663                 error "read sample ($readsample1) is wrong"
23664         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
23665                 3 * $decay_pct) / 100") -eq 1 ] ||
23666                 error "write sample ($writesample1) is wrong"
23667         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
23668                 20 * $decay_pct) / 100") -eq 1 ] ||
23669                 error "read bytes ($readbyte1) is wrong"
23670         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
23671                 15 * $decay_pct) / 100") -eq 1 ] ||
23672                 error "write bytes ($writebyte1) is wrong"
23673
23674         echo "Turn off file heat for the file $DIR/$tfile"
23675         $LFS heat_set -o $DIR/$tfile
23676
23677         echo "QQQQ" > $DIR/$tfile
23678         echo "QQQQ" > $DIR/$tfile
23679         echo "QQQQ" > $DIR/$tfile
23680         cat $DIR/$tfile > /dev/null
23681         cat $DIR/$tfile > /dev/null
23682         cat $DIR/$tfile > /dev/null
23683         cat $DIR/$tfile > /dev/null
23684
23685         out=$($LFS heat_get $DIR/$tfile)
23686         $LFS heat_get $DIR/$tfile
23687         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23688         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23689         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23690         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23691
23692         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
23693         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
23694         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
23695         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
23696
23697         echo "Trun on file heat for the file $DIR/$tfile"
23698         $LFS heat_set -O $DIR/$tfile
23699
23700         echo "QQQQ" > $DIR/$tfile
23701         echo "QQQQ" > $DIR/$tfile
23702         echo "QQQQ" > $DIR/$tfile
23703         cat $DIR/$tfile > /dev/null
23704         cat $DIR/$tfile > /dev/null
23705         cat $DIR/$tfile > /dev/null
23706         cat $DIR/$tfile > /dev/null
23707
23708         out=$($LFS heat_get $DIR/$tfile)
23709         $LFS heat_get $DIR/$tfile
23710         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23711         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23712         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23713         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23714
23715         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
23716         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
23717         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
23718         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
23719
23720         $LFS heat_set -c $DIR/$tfile
23721         $LCTL set_param -n llite.*.file_heat=0
23722         echo "Turn off file heat support for the Lustre filesystem"
23723
23724         echo "QQQQ" > $DIR/$tfile
23725         echo "QQQQ" > $DIR/$tfile
23726         echo "QQQQ" > $DIR/$tfile
23727         cat $DIR/$tfile > /dev/null
23728         cat $DIR/$tfile > /dev/null
23729         cat $DIR/$tfile > /dev/null
23730         cat $DIR/$tfile > /dev/null
23731
23732         out=$($LFS heat_get $DIR/$tfile)
23733         $LFS heat_get $DIR/$tfile
23734         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23735         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23736         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23737         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23738
23739         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
23740         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
23741         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
23742         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
23743
23744         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
23745         rm -f $DIR/$tfile
23746 }
23747 run_test 813 "File heat verfication"
23748
23749 test_814()
23750 {
23751         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
23752         echo -n y >> $DIR/$tfile
23753         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
23754         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
23755 }
23756 run_test 814 "sparse cp works as expected (LU-12361)"
23757
23758 test_815()
23759 {
23760         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
23761         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
23762 }
23763 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
23764
23765 test_816() {
23766         [ "$SHARED_KEY" = true ] &&
23767                 skip "OSC connections never go IDLE with Shared-Keys enabled"
23768
23769         $LFS setstripe -c 1 -i 0 $DIR/$tfile
23770         # ensure ost1 is connected
23771         stat $DIR/$tfile >/dev/null || error "can't stat"
23772         wait_osc_import_state client ost1 FULL
23773         # no locks, no reqs to let the connection idle
23774         cancel_lru_locks osc
23775         lru_resize_disable osc
23776         local before
23777         local now
23778         before=$($LCTL get_param -n \
23779                  ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
23780
23781         wait_osc_import_state client ost1 IDLE
23782         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
23783         now=$($LCTL get_param -n \
23784               ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
23785         [ $before == $now ] || error "lru_size changed $before != $now"
23786 }
23787 run_test 816 "do not reset lru_resize on idle reconnect"
23788
23789 cleanup_817() {
23790         umount $tmpdir
23791         exportfs -u localhost:$DIR/nfsexp
23792         rm -rf $DIR/nfsexp
23793 }
23794
23795 test_817() {
23796         systemctl restart nfs-server.service || skip "failed to restart nfsd"
23797
23798         mkdir -p $DIR/nfsexp
23799         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
23800                 error "failed to export nfs"
23801
23802         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
23803         stack_trap cleanup_817 EXIT
23804
23805         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
23806                 error "failed to mount nfs to $tmpdir"
23807
23808         cp /bin/true $tmpdir
23809         $DIR/nfsexp/true || error "failed to execute 'true' command"
23810 }
23811 run_test 817 "nfsd won't cache write lock for exec file"
23812
23813 test_818() {
23814         mkdir $DIR/$tdir
23815         $LFS setstripe -c1 -i0 $DIR/$tfile
23816         $LFS setstripe -c1 -i1 $DIR/$tfile
23817         stop $SINGLEMDS
23818         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
23819         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
23820         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
23821                 error "start $SINGLEMDS failed"
23822         rm -rf $DIR/$tdir
23823 }
23824 run_test 818 "unlink with failed llog"
23825
23826 test_819a() {
23827         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23828         cancel_lru_locks osc
23829         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
23830         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
23831         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
23832         rm -f $TDIR/$tfile
23833 }
23834 run_test 819a "too big niobuf in read"
23835
23836 test_819b() {
23837         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
23838         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
23839         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23840         cancel_lru_locks osc
23841         sleep 1
23842         rm -f $TDIR/$tfile
23843 }
23844 run_test 819b "too big niobuf in write"
23845
23846
23847 function test_820_start_ost() {
23848         sleep 5
23849
23850         for num in $(seq $OSTCOUNT); do
23851                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS
23852         done
23853 }
23854
23855 test_820() {
23856         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
23857
23858         mkdir $DIR/$tdir
23859         umount_client $MOUNT || error "umount failed"
23860         for num in $(seq $OSTCOUNT); do
23861                 stop ost$num
23862         done
23863
23864         # mount client with no active OSTs
23865         # so that the client can't initialize max LOV EA size
23866         # from OSC notifications
23867         mount_client $MOUNT || error "mount failed"
23868         # delay OST starting to keep this 0 max EA size for a while
23869         test_820_start_ost &
23870
23871         # create a directory on MDS2
23872         test_mkdir -i 1 -c1 $DIR/$tdir/mds2 ||
23873                 error "Failed to create directory"
23874         # open intent should update default EA size
23875         # see mdc_update_max_ea_from_body()
23876         # notice this is the very first RPC to MDS2
23877         cp /etc/services $DIR/$tdir/mds2 ||
23878                 error "Failed to copy files to mds$n"
23879 }
23880 run_test 820 "update max EA from open intent"
23881
23882 #
23883 # tests that do cleanup/setup should be run at the end
23884 #
23885
23886 test_900() {
23887         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23888         local ls
23889
23890         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
23891         $LCTL set_param fail_loc=0x903
23892
23893         cancel_lru_locks MGC
23894
23895         FAIL_ON_ERROR=true cleanup
23896         FAIL_ON_ERROR=true setup
23897 }
23898 run_test 900 "umount should not race with any mgc requeue thread"
23899
23900 # LUS-6253/LU-11185
23901 test_901() {
23902         local oldc
23903         local newc
23904         local olds
23905         local news
23906         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23907
23908         # some get_param have a bug to handle dot in param name
23909         cancel_lru_locks MGC
23910         oldc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
23911         olds=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
23912         umount_client $MOUNT || error "umount failed"
23913         mount_client $MOUNT || error "mount failed"
23914         cancel_lru_locks MGC
23915         newc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
23916         news=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
23917
23918         [ $oldc -lt $newc ] && error "mgc lock leak ($oldc != $newc)"
23919         [ $olds -lt $news ] && error "mgs lock leak ($olds != $news)"
23920
23921         return 0
23922 }
23923 run_test 901 "don't leak a mgc lock on client umount"
23924
23925 # LU-13377
23926 test_902() {
23927         [ $CLIENT_VERSION -lt $(version_code 2.13.52) ] &&
23928                 skip "client does not have LU-13377 fix"
23929         #define OBD_FAIL_LLITE_SHORT_COMMIT 0x1415
23930         $LCTL set_param fail_loc=0x1415
23931         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23932         cancel_lru_locks osc
23933         rm -f $DIR/$tfile
23934 }
23935 run_test 902 "test short write doesn't hang lustre"
23936
23937 complete $SECONDS
23938 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
23939 check_and_cleanup_lustre
23940 if [ "$I_MOUNTED" != "yes" ]; then
23941         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
23942 fi
23943 exit_status