Whamcloud - gitweb
LU-13301 lfs: return error when setstripe -o on inactive OSTs
[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_31a() {
3088         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
3089         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
3090 }
3091 run_test 31a "open-unlink file =================================="
3092
3093 test_31b() {
3094         touch $DIR/f31 || error "touch $DIR/f31 failed"
3095         ln $DIR/f31 $DIR/f31b || error "ln failed"
3096         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
3097         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
3098 }
3099 run_test 31b "unlink file with multiple links while open ======="
3100
3101 test_31c() {
3102         touch $DIR/f31 || error "touch $DIR/f31 failed"
3103         ln $DIR/f31 $DIR/f31c || error "ln failed"
3104         multiop_bg_pause $DIR/f31 O_uc ||
3105                 error "multiop_bg_pause for $DIR/f31 failed"
3106         MULTIPID=$!
3107         $MULTIOP $DIR/f31c Ouc
3108         kill -USR1 $MULTIPID
3109         wait $MULTIPID
3110 }
3111 run_test 31c "open-unlink file with multiple links ============="
3112
3113 test_31d() {
3114         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
3115         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
3116 }
3117 run_test 31d "remove of open directory ========================="
3118
3119 test_31e() { # bug 2904
3120         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
3121 }
3122 run_test 31e "remove of open non-empty directory ==============="
3123
3124 test_31f() { # bug 4554
3125         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3126
3127         set -vx
3128         test_mkdir $DIR/d31f
3129         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3130         cp /etc/hosts $DIR/d31f
3131         ls -l $DIR/d31f
3132         $LFS getstripe $DIR/d31f/hosts
3133         multiop_bg_pause $DIR/d31f D_c || return 1
3134         MULTIPID=$!
3135
3136         rm -rv $DIR/d31f || error "first of $DIR/d31f"
3137         test_mkdir $DIR/d31f
3138         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3139         cp /etc/hosts $DIR/d31f
3140         ls -l $DIR/d31f
3141         $LFS getstripe $DIR/d31f/hosts
3142         multiop_bg_pause $DIR/d31f D_c || return 1
3143         MULTIPID2=$!
3144
3145         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
3146         wait $MULTIPID || error "first opendir $MULTIPID failed"
3147
3148         sleep 6
3149
3150         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
3151         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
3152         set +vx
3153 }
3154 run_test 31f "remove of open directory with open-unlink file ==="
3155
3156 test_31g() {
3157         echo "-- cross directory link --"
3158         test_mkdir -c1 $DIR/${tdir}ga
3159         test_mkdir -c1 $DIR/${tdir}gb
3160         touch $DIR/${tdir}ga/f
3161         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
3162         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
3163         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
3164         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
3165         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
3166 }
3167 run_test 31g "cross directory link==============="
3168
3169 test_31h() {
3170         echo "-- cross directory link --"
3171         test_mkdir -c1 $DIR/${tdir}
3172         test_mkdir -c1 $DIR/${tdir}/dir
3173         touch $DIR/${tdir}/f
3174         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
3175         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
3176         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
3177         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
3178         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
3179 }
3180 run_test 31h "cross directory link under child==============="
3181
3182 test_31i() {
3183         echo "-- cross directory link --"
3184         test_mkdir -c1 $DIR/$tdir
3185         test_mkdir -c1 $DIR/$tdir/dir
3186         touch $DIR/$tdir/dir/f
3187         ln $DIR/$tdir/dir/f $DIR/$tdir/g
3188         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
3189         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
3190         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
3191         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
3192 }
3193 run_test 31i "cross directory link under parent==============="
3194
3195 test_31j() {
3196         test_mkdir -c1 -p $DIR/$tdir
3197         test_mkdir -c1 -p $DIR/$tdir/dir1
3198         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
3199         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
3200         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
3201         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
3202         return 0
3203 }
3204 run_test 31j "link for directory==============="
3205
3206 test_31k() {
3207         test_mkdir -c1 -p $DIR/$tdir
3208         touch $DIR/$tdir/s
3209         touch $DIR/$tdir/exist
3210         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
3211         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
3212         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
3213         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
3214         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
3215         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
3216         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
3217         return 0
3218 }
3219 run_test 31k "link to file: the same, non-existing, dir==============="
3220
3221 test_31m() {
3222         mkdir $DIR/d31m
3223         touch $DIR/d31m/s
3224         mkdir $DIR/d31m2
3225         touch $DIR/d31m2/exist
3226         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
3227         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
3228         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
3229         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
3230         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
3231         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
3232         return 0
3233 }
3234 run_test 31m "link to file: the same, non-existing, dir==============="
3235
3236 test_31n() {
3237         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
3238         nlink=$(stat --format=%h $DIR/$tfile)
3239         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3240         local fd=$(free_fd)
3241         local cmd="exec $fd<$DIR/$tfile"
3242         eval $cmd
3243         cmd="exec $fd<&-"
3244         trap "eval $cmd" EXIT
3245         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3246         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3247         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
3248         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3249         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
3250         eval $cmd
3251 }
3252 run_test 31n "check link count of unlinked file"
3253
3254 link_one() {
3255         local tempfile=$(mktemp $1_XXXXXX)
3256         mlink $tempfile $1 2> /dev/null &&
3257                 echo "$BASHPID: link $tempfile to $1 succeeded"
3258         munlink $tempfile
3259 }
3260
3261 test_31o() { # LU-2901
3262         test_mkdir $DIR/$tdir
3263         for LOOP in $(seq 100); do
3264                 rm -f $DIR/$tdir/$tfile*
3265                 for THREAD in $(seq 8); do
3266                         link_one $DIR/$tdir/$tfile.$LOOP &
3267                 done
3268                 wait
3269                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
3270                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
3271                         error "$LINKS duplicate links to $tfile.$LOOP" &&
3272                         break || true
3273         done
3274 }
3275 run_test 31o "duplicate hard links with same filename"
3276
3277 test_31p() {
3278         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3279
3280         test_mkdir $DIR/$tdir
3281         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3282         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
3283
3284         opendirunlink $DIR/$tdir/striped_dir/test1 ||
3285                 error "open unlink test1 failed"
3286         opendirunlink $DIR/$tdir/striped_dir/test2 ||
3287                 error "open unlink test2 failed"
3288
3289         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
3290                 error "test1 still exists"
3291         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
3292                 error "test2 still exists"
3293 }
3294 run_test 31p "remove of open striped directory"
3295
3296 cleanup_test32_mount() {
3297         local rc=0
3298         trap 0
3299         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
3300         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
3301         losetup -d $loopdev || true
3302         rm -rf $DIR/$tdir
3303         return $rc
3304 }
3305
3306 test_32a() {
3307         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3308
3309         echo "== more mountpoints and symlinks ================="
3310         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3311         trap cleanup_test32_mount EXIT
3312         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3313         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3314                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3315         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
3316                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
3317         cleanup_test32_mount
3318 }
3319 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
3320
3321 test_32b() {
3322         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3323
3324         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3325         trap cleanup_test32_mount EXIT
3326         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3327         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3328                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3329         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
3330                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
3331         cleanup_test32_mount
3332 }
3333 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
3334
3335 test_32c() {
3336         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3337
3338         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3339         trap cleanup_test32_mount EXIT
3340         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3341         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3342                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3343         test_mkdir -p $DIR/$tdir/d2/test_dir
3344         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3345                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
3346         cleanup_test32_mount
3347 }
3348 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
3349
3350 test_32d() {
3351         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3352
3353         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3354         trap cleanup_test32_mount EXIT
3355         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3356         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3357                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3358         test_mkdir -p $DIR/$tdir/d2/test_dir
3359         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3360                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
3361         cleanup_test32_mount
3362 }
3363 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
3364
3365 test_32e() {
3366         rm -fr $DIR/$tdir
3367         test_mkdir -p $DIR/$tdir/tmp
3368         local tmp_dir=$DIR/$tdir/tmp
3369         ln -s $DIR/$tdir $tmp_dir/symlink11
3370         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3371         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
3372         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
3373 }
3374 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
3375
3376 test_32f() {
3377         rm -fr $DIR/$tdir
3378         test_mkdir -p $DIR/$tdir/tmp
3379         local tmp_dir=$DIR/$tdir/tmp
3380         ln -s $DIR/$tdir $tmp_dir/symlink11
3381         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3382         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
3383         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
3384 }
3385 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
3386
3387 test_32g() {
3388         local tmp_dir=$DIR/$tdir/tmp
3389         test_mkdir -p $tmp_dir
3390         test_mkdir $DIR/${tdir}2
3391         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3392         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3393         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
3394         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
3395         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
3396         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
3397 }
3398 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3399
3400 test_32h() {
3401         rm -fr $DIR/$tdir $DIR/${tdir}2
3402         tmp_dir=$DIR/$tdir/tmp
3403         test_mkdir -p $tmp_dir
3404         test_mkdir $DIR/${tdir}2
3405         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3406         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3407         ls $tmp_dir/symlink12 || error "listing symlink12"
3408         ls $DIR/$tdir/symlink02  || error "listing symlink02"
3409 }
3410 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3411
3412 test_32i() {
3413         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3414
3415         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3416         trap cleanup_test32_mount EXIT
3417         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3418         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3419                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3420         touch $DIR/$tdir/test_file
3421         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
3422                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
3423         cleanup_test32_mount
3424 }
3425 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
3426
3427 test_32j() {
3428         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3429
3430         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3431         trap cleanup_test32_mount EXIT
3432         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3433         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3434                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3435         touch $DIR/$tdir/test_file
3436         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
3437                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
3438         cleanup_test32_mount
3439 }
3440 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
3441
3442 test_32k() {
3443         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3444
3445         rm -fr $DIR/$tdir
3446         trap cleanup_test32_mount EXIT
3447         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3448         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3449                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3450         test_mkdir -p $DIR/$tdir/d2
3451         touch $DIR/$tdir/d2/test_file || error "touch failed"
3452         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3453                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
3454         cleanup_test32_mount
3455 }
3456 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
3457
3458 test_32l() {
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         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3469                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
3470         cleanup_test32_mount
3471 }
3472 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
3473
3474 test_32m() {
3475         rm -fr $DIR/d32m
3476         test_mkdir -p $DIR/d32m/tmp
3477         TMP_DIR=$DIR/d32m/tmp
3478         ln -s $DIR $TMP_DIR/symlink11
3479         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3480         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
3481                 error "symlink11 not a link"
3482         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
3483                 error "symlink01 not a link"
3484 }
3485 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
3486
3487 test_32n() {
3488         rm -fr $DIR/d32n
3489         test_mkdir -p $DIR/d32n/tmp
3490         TMP_DIR=$DIR/d32n/tmp
3491         ln -s $DIR $TMP_DIR/symlink11
3492         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3493         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
3494         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
3495 }
3496 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
3497
3498 test_32o() {
3499         touch $DIR/$tfile
3500         test_mkdir -p $DIR/d32o/tmp
3501         TMP_DIR=$DIR/d32o/tmp
3502         ln -s $DIR/$tfile $TMP_DIR/symlink12
3503         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3504         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
3505                 error "symlink12 not a link"
3506         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
3507         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
3508                 error "$DIR/d32o/tmp/symlink12 not file type"
3509         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
3510                 error "$DIR/d32o/symlink02 not file type"
3511 }
3512 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
3513
3514 test_32p() {
3515         log 32p_1
3516         rm -fr $DIR/d32p
3517         log 32p_2
3518         rm -f $DIR/$tfile
3519         log 32p_3
3520         touch $DIR/$tfile
3521         log 32p_4
3522         test_mkdir -p $DIR/d32p/tmp
3523         log 32p_5
3524         TMP_DIR=$DIR/d32p/tmp
3525         log 32p_6
3526         ln -s $DIR/$tfile $TMP_DIR/symlink12
3527         log 32p_7
3528         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3529         log 32p_8
3530         cat $DIR/d32p/tmp/symlink12 ||
3531                 error "Can't open $DIR/d32p/tmp/symlink12"
3532         log 32p_9
3533         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
3534         log 32p_10
3535 }
3536 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
3537
3538 test_32q() {
3539         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3540
3541         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3542         trap cleanup_test32_mount EXIT
3543         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3544         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3545         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3546                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3547         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
3548         cleanup_test32_mount
3549 }
3550 run_test 32q "stat follows mountpoints in Lustre (should return error)"
3551
3552 test_32r() {
3553         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3554
3555         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3556         trap cleanup_test32_mount EXIT
3557         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3558         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3559         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3560                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3561         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
3562         cleanup_test32_mount
3563 }
3564 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
3565
3566 test_33aa() {
3567         rm -f $DIR/$tfile
3568         touch $DIR/$tfile
3569         chmod 444 $DIR/$tfile
3570         chown $RUNAS_ID $DIR/$tfile
3571         log 33_1
3572         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3573         log 33_2
3574 }
3575 run_test 33aa "write file with mode 444 (should return error)"
3576
3577 test_33a() {
3578         rm -fr $DIR/$tdir
3579         test_mkdir $DIR/$tdir
3580         chown $RUNAS_ID $DIR/$tdir
3581         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
3582                 error "$RUNAS create $tdir/$tfile failed"
3583         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
3584                 error "open RDWR" || true
3585 }
3586 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
3587
3588 test_33b() {
3589         rm -fr $DIR/$tdir
3590         test_mkdir $DIR/$tdir
3591         chown $RUNAS_ID $DIR/$tdir
3592         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
3593 }
3594 run_test 33b "test open file with malformed flags (No panic)"
3595
3596 test_33c() {
3597         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3598         remote_ost_nodsh && skip "remote OST with nodsh"
3599
3600         local ostnum
3601         local ostname
3602         local write_bytes
3603         local all_zeros
3604
3605         all_zeros=:
3606         rm -fr $DIR/$tdir
3607         test_mkdir $DIR/$tdir
3608         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
3609
3610         sync
3611         for ostnum in $(seq $OSTCOUNT); do
3612                 # test-framework's OST numbering is one-based, while Lustre's
3613                 # is zero-based
3614                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3615                 # Parsing llobdstat's output sucks; we could grep the /proc
3616                 # path, but that's likely to not be as portable as using the
3617                 # llobdstat utility.  So we parse lctl output instead.
3618                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3619                         obdfilter/$ostname/stats |
3620                         awk '/^write_bytes/ {print $7}' )
3621                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
3622                 if (( ${write_bytes:-0} > 0 ))
3623                 then
3624                         all_zeros=false
3625                         break;
3626                 fi
3627         done
3628
3629         $all_zeros || return 0
3630
3631         # Write four bytes
3632         echo foo > $DIR/$tdir/bar
3633         # Really write them
3634         sync
3635
3636         # Total up write_bytes after writing.  We'd better find non-zeros.
3637         for ostnum in $(seq $OSTCOUNT); do
3638                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3639                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3640                         obdfilter/$ostname/stats |
3641                         awk '/^write_bytes/ {print $7}' )
3642                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
3643                 if (( ${write_bytes:-0} > 0 ))
3644                 then
3645                         all_zeros=false
3646                         break;
3647                 fi
3648         done
3649
3650         if $all_zeros
3651         then
3652                 for ostnum in $(seq $OSTCOUNT); do
3653                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3654                         echo "Check that write_bytes is present in obdfilter/*/stats:"
3655                         do_facet ost$ostnum lctl get_param -n \
3656                                 obdfilter/$ostname/stats
3657                 done
3658                 error "OST not keeping write_bytes stats (b22312)"
3659         fi
3660 }
3661 run_test 33c "test llobdstat and write_bytes"
3662
3663 test_33d() {
3664         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3665         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3666
3667         local MDTIDX=1
3668         local remote_dir=$DIR/$tdir/remote_dir
3669
3670         test_mkdir $DIR/$tdir
3671         $LFS mkdir -i $MDTIDX $remote_dir ||
3672                 error "create remote directory failed"
3673
3674         touch $remote_dir/$tfile
3675         chmod 444 $remote_dir/$tfile
3676         chown $RUNAS_ID $remote_dir/$tfile
3677
3678         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3679
3680         chown $RUNAS_ID $remote_dir
3681         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
3682                                         error "create" || true
3683         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
3684                                     error "open RDWR" || true
3685         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
3686 }
3687 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
3688
3689 test_33e() {
3690         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3691
3692         mkdir $DIR/$tdir
3693
3694         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3695         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3696         mkdir $DIR/$tdir/local_dir
3697
3698         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3699         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3700         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3701
3702         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3703                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
3704
3705         rmdir $DIR/$tdir/* || error "rmdir failed"
3706
3707         umask 777
3708         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3709         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3710         mkdir $DIR/$tdir/local_dir
3711
3712         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3713         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3714         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3715
3716         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3717                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3718
3719         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3720
3721         umask 000
3722         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3723         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3724         mkdir $DIR/$tdir/local_dir
3725
3726         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3727         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3728         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3729
3730         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3731                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3732 }
3733 run_test 33e "mkdir and striped directory should have same mode"
3734
3735 cleanup_33f() {
3736         trap 0
3737         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3738 }
3739
3740 test_33f() {
3741         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3742         remote_mds_nodsh && skip "remote MDS with nodsh"
3743
3744         mkdir $DIR/$tdir
3745         chmod go+rwx $DIR/$tdir
3746         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3747         trap cleanup_33f EXIT
3748
3749         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3750                 error "cannot create striped directory"
3751
3752         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3753                 error "cannot create files in striped directory"
3754
3755         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3756                 error "cannot remove files in striped directory"
3757
3758         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3759                 error "cannot remove striped directory"
3760
3761         cleanup_33f
3762 }
3763 run_test 33f "nonroot user can create, access, and remove a striped directory"
3764
3765 test_33g() {
3766         mkdir -p $DIR/$tdir/dir2
3767
3768         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3769         echo $err
3770         [[ $err =~ "exists" ]] || error "Not exists error"
3771 }
3772 run_test 33g "nonroot user create already existing root created file"
3773
3774 test_33h() {
3775         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3776         [ $MDS1_VERSION -lt $(version_code 2.13.50) ] &&
3777                 skip "Need MDS version at least 2.13.50"
3778
3779         test_mkdir -c $MDSCOUNT -H crush $DIR/$tdir ||
3780                 error "mkdir $tdir failed"
3781         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
3782
3783         local index=$($LFS getstripe -m $DIR/$tdir/$tfile)
3784         local index2
3785
3786         for fname in $DIR/$tdir/$tfile.bak \
3787                      $DIR/$tdir/$tfile.SAV \
3788                      $DIR/$tdir/$tfile.orig \
3789                      $DIR/$tdir/$tfile~; do
3790                 touch $fname  || error "touch $fname failed"
3791                 index2=$($LFS getstripe -m $fname)
3792                 [ $index -eq $index2 ] ||
3793                         error "$fname MDT index mismatch $index != $index2"
3794         done
3795
3796         local failed=0
3797         for i in {1..50}; do
3798                 for fname in $(mktemp -u $DIR/$tdir/.$tfile.XXXXXX) \
3799                              $(mktemp $DIR/$tdir/$tfile.XXXXXXXX); do
3800                         touch $fname  || error "touch $fname failed"
3801                         index2=$($LFS getstripe -m $fname)
3802                         if [[ $index != $index2 ]]; then
3803                                 failed=$((failed + 1))
3804                                 echo "$fname MDT index mismatch $index != $index2"
3805                         fi
3806                 done
3807         done
3808         echo "$failed MDT index mismatches"
3809         (( failed < 4 )) || error "MDT index mismatch $failed times"
3810
3811 }
3812 run_test 33h "temp file is located on the same MDT as target"
3813
3814 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3815 test_34a() {
3816         rm -f $DIR/f34
3817         $MCREATE $DIR/f34 || error "mcreate failed"
3818         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3819                 error "getstripe failed"
3820         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3821         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3822                 error "getstripe failed"
3823         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3824                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3825 }
3826 run_test 34a "truncate file that has not been opened ==========="
3827
3828 test_34b() {
3829         [ ! -f $DIR/f34 ] && test_34a
3830         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3831                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3832         $OPENFILE -f O_RDONLY $DIR/f34
3833         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3834                 error "getstripe failed"
3835         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3836                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3837 }
3838 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3839
3840 test_34c() {
3841         [ ! -f $DIR/f34 ] && test_34a
3842         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3843                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3844         $OPENFILE -f O_RDWR $DIR/f34
3845         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" &&
3846                 error "$LFS getstripe failed"
3847         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3848                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3849 }
3850 run_test 34c "O_RDWR opening file-with-size works =============="
3851
3852 test_34d() {
3853         [ ! -f $DIR/f34 ] && test_34a
3854         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3855                 error "dd failed"
3856         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3857                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3858         rm $DIR/f34
3859 }
3860 run_test 34d "write to sparse file ============================="
3861
3862 test_34e() {
3863         rm -f $DIR/f34e
3864         $MCREATE $DIR/f34e || error "mcreate failed"
3865         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3866         $CHECKSTAT -s 1000 $DIR/f34e ||
3867                 error "Size of $DIR/f34e not equal to 1000 bytes"
3868         $OPENFILE -f O_RDWR $DIR/f34e
3869         $CHECKSTAT -s 1000 $DIR/f34e ||
3870                 error "Size of $DIR/f34e not equal to 1000 bytes"
3871 }
3872 run_test 34e "create objects, some with size and some without =="
3873
3874 test_34f() { # bug 6242, 6243
3875         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3876
3877         SIZE34F=48000
3878         rm -f $DIR/f34f
3879         $MCREATE $DIR/f34f || error "mcreate failed"
3880         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3881         dd if=$DIR/f34f of=$TMP/f34f
3882         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3883         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3884         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3885         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3886         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3887 }
3888 run_test 34f "read from a file with no objects until EOF ======="
3889
3890 test_34g() {
3891         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3892
3893         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3894                 error "dd failed"
3895         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3896         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3897                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3898         cancel_lru_locks osc
3899         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3900                 error "wrong size after lock cancel"
3901
3902         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3903         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3904                 error "expanding truncate failed"
3905         cancel_lru_locks osc
3906         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3907                 error "wrong expanded size after lock cancel"
3908 }
3909 run_test 34g "truncate long file ==============================="
3910
3911 test_34h() {
3912         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3913
3914         local gid=10
3915         local sz=1000
3916
3917         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3918         sync # Flush the cache so that multiop below does not block on cache
3919              # flush when getting the group lock
3920         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3921         MULTIPID=$!
3922
3923         # Since just timed wait is not good enough, let's do a sync write
3924         # that way we are sure enough time for a roundtrip + processing
3925         # passed + 2 seconds of extra margin.
3926         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
3927         rm $DIR/${tfile}-1
3928         sleep 2
3929
3930         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3931                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3932                 kill -9 $MULTIPID
3933         fi
3934         wait $MULTIPID
3935         local nsz=`stat -c %s $DIR/$tfile`
3936         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3937 }
3938 run_test 34h "ftruncate file under grouplock should not block"
3939
3940 test_35a() {
3941         cp /bin/sh $DIR/f35a
3942         chmod 444 $DIR/f35a
3943         chown $RUNAS_ID $DIR/f35a
3944         $RUNAS $DIR/f35a && error || true
3945         rm $DIR/f35a
3946 }
3947 run_test 35a "exec file with mode 444 (should return and not leak)"
3948
3949 test_36a() {
3950         rm -f $DIR/f36
3951         utime $DIR/f36 || error "utime failed for MDS"
3952 }
3953 run_test 36a "MDS utime check (mknod, utime)"
3954
3955 test_36b() {
3956         echo "" > $DIR/f36
3957         utime $DIR/f36 || error "utime failed for OST"
3958 }
3959 run_test 36b "OST utime check (open, utime)"
3960
3961 test_36c() {
3962         rm -f $DIR/d36/f36
3963         test_mkdir $DIR/d36
3964         chown $RUNAS_ID $DIR/d36
3965         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3966 }
3967 run_test 36c "non-root MDS utime check (mknod, utime)"
3968
3969 test_36d() {
3970         [ ! -d $DIR/d36 ] && test_36c
3971         echo "" > $DIR/d36/f36
3972         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3973 }
3974 run_test 36d "non-root OST utime check (open, utime)"
3975
3976 test_36e() {
3977         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3978
3979         test_mkdir $DIR/$tdir
3980         touch $DIR/$tdir/$tfile
3981         $RUNAS utime $DIR/$tdir/$tfile &&
3982                 error "utime worked, expected failure" || true
3983 }
3984 run_test 36e "utime on non-owned file (should return error)"
3985
3986 subr_36fh() {
3987         local fl="$1"
3988         local LANG_SAVE=$LANG
3989         local LC_LANG_SAVE=$LC_LANG
3990         export LANG=C LC_LANG=C # for date language
3991
3992         DATESTR="Dec 20  2000"
3993         test_mkdir $DIR/$tdir
3994         lctl set_param fail_loc=$fl
3995         date; date +%s
3996         cp /etc/hosts $DIR/$tdir/$tfile
3997         sync & # write RPC generated with "current" inode timestamp, but delayed
3998         sleep 1
3999         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
4000         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
4001         cancel_lru_locks $OSC
4002         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
4003         date; date +%s
4004         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
4005                 echo "BEFORE: $LS_BEFORE" && \
4006                 echo "AFTER : $LS_AFTER" && \
4007                 echo "WANT  : $DATESTR" && \
4008                 error "$DIR/$tdir/$tfile timestamps changed" || true
4009
4010         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
4011 }
4012
4013 test_36f() {
4014         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4015
4016         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
4017         subr_36fh "0x80000214"
4018 }
4019 run_test 36f "utime on file racing with OST BRW write =========="
4020
4021 test_36g() {
4022         remote_ost_nodsh && skip "remote OST with nodsh"
4023         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4024         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
4025                 skip "Need MDS version at least 2.12.51"
4026
4027         local fmd_max_age
4028         local fmd
4029         local facet="ost1"
4030         local tgt="obdfilter"
4031
4032         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
4033
4034         test_mkdir $DIR/$tdir
4035         fmd_max_age=$(do_facet $facet \
4036                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
4037                 head -n 1")
4038
4039         echo "FMD max age: ${fmd_max_age}s"
4040         touch $DIR/$tdir/$tfile
4041         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4042                 gawk '{cnt=cnt+$1}  END{print cnt}')
4043         echo "FMD before: $fmd"
4044         [[ $fmd == 0 ]] &&
4045                 error "FMD wasn't create by touch"
4046         sleep $((fmd_max_age + 12))
4047         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4048                 gawk '{cnt=cnt+$1}  END{print cnt}')
4049         echo "FMD after: $fmd"
4050         [[ $fmd == 0 ]] ||
4051                 error "FMD wasn't expired by ping"
4052 }
4053 run_test 36g "FMD cache expiry ====================="
4054
4055 test_36h() {
4056         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4057
4058         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
4059         subr_36fh "0x80000227"
4060 }
4061 run_test 36h "utime on file racing with OST BRW write =========="
4062
4063 test_36i() {
4064         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4065
4066         test_mkdir $DIR/$tdir
4067         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
4068
4069         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
4070         local new_mtime=$((mtime + 200))
4071
4072         #change Modify time of striped dir
4073         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
4074                         error "change mtime failed"
4075
4076         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
4077
4078         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
4079 }
4080 run_test 36i "change mtime on striped directory"
4081
4082 # test_37 - duplicate with tests 32q 32r
4083
4084 test_38() {
4085         local file=$DIR/$tfile
4086         touch $file
4087         openfile -f O_DIRECTORY $file
4088         local RC=$?
4089         local ENOTDIR=20
4090         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
4091         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
4092 }
4093 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
4094
4095 test_39a() { # was test_39
4096         touch $DIR/$tfile
4097         touch $DIR/${tfile}2
4098 #       ls -l  $DIR/$tfile $DIR/${tfile}2
4099 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
4100 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
4101         sleep 2
4102         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
4103         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
4104                 echo "mtime"
4105                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
4106                 echo "atime"
4107                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
4108                 echo "ctime"
4109                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
4110                 error "O_TRUNC didn't change timestamps"
4111         fi
4112 }
4113 run_test 39a "mtime changed on create"
4114
4115 test_39b() {
4116         test_mkdir -c1 $DIR/$tdir
4117         cp -p /etc/passwd $DIR/$tdir/fopen
4118         cp -p /etc/passwd $DIR/$tdir/flink
4119         cp -p /etc/passwd $DIR/$tdir/funlink
4120         cp -p /etc/passwd $DIR/$tdir/frename
4121         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
4122
4123         sleep 1
4124         echo "aaaaaa" >> $DIR/$tdir/fopen
4125         echo "aaaaaa" >> $DIR/$tdir/flink
4126         echo "aaaaaa" >> $DIR/$tdir/funlink
4127         echo "aaaaaa" >> $DIR/$tdir/frename
4128
4129         local open_new=`stat -c %Y $DIR/$tdir/fopen`
4130         local link_new=`stat -c %Y $DIR/$tdir/flink`
4131         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
4132         local rename_new=`stat -c %Y $DIR/$tdir/frename`
4133
4134         cat $DIR/$tdir/fopen > /dev/null
4135         ln $DIR/$tdir/flink $DIR/$tdir/flink2
4136         rm -f $DIR/$tdir/funlink2
4137         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
4138
4139         for (( i=0; i < 2; i++ )) ; do
4140                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
4141                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
4142                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
4143                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
4144
4145                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
4146                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
4147                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
4148                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
4149
4150                 cancel_lru_locks $OSC
4151                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4152         done
4153 }
4154 run_test 39b "mtime change on open, link, unlink, rename  ======"
4155
4156 # this should be set to past
4157 TEST_39_MTIME=`date -d "1 year ago" +%s`
4158
4159 # bug 11063
4160 test_39c() {
4161         touch $DIR1/$tfile
4162         sleep 2
4163         local mtime0=`stat -c %Y $DIR1/$tfile`
4164
4165         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4166         local mtime1=`stat -c %Y $DIR1/$tfile`
4167         [ "$mtime1" = $TEST_39_MTIME ] || \
4168                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
4169
4170         local d1=`date +%s`
4171         echo hello >> $DIR1/$tfile
4172         local d2=`date +%s`
4173         local mtime2=`stat -c %Y $DIR1/$tfile`
4174         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
4175                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
4176
4177         mv $DIR1/$tfile $DIR1/$tfile-1
4178
4179         for (( i=0; i < 2; i++ )) ; do
4180                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
4181                 [ "$mtime2" = "$mtime3" ] || \
4182                         error "mtime ($mtime2) changed (to $mtime3) on rename"
4183
4184                 cancel_lru_locks $OSC
4185                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4186         done
4187 }
4188 run_test 39c "mtime change on rename ==========================="
4189
4190 # bug 21114
4191 test_39d() {
4192         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4193
4194         touch $DIR1/$tfile
4195         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4196
4197         for (( i=0; i < 2; i++ )) ; do
4198                 local mtime=`stat -c %Y $DIR1/$tfile`
4199                 [ $mtime = $TEST_39_MTIME ] || \
4200                         error "mtime($mtime) is not set to $TEST_39_MTIME"
4201
4202                 cancel_lru_locks $OSC
4203                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4204         done
4205 }
4206 run_test 39d "create, utime, stat =============================="
4207
4208 # bug 21114
4209 test_39e() {
4210         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4211
4212         touch $DIR1/$tfile
4213         local mtime1=`stat -c %Y $DIR1/$tfile`
4214
4215         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4216
4217         for (( i=0; i < 2; i++ )) ; do
4218                 local mtime2=`stat -c %Y $DIR1/$tfile`
4219                 [ $mtime2 = $TEST_39_MTIME ] || \
4220                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4221
4222                 cancel_lru_locks $OSC
4223                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4224         done
4225 }
4226 run_test 39e "create, stat, utime, stat ========================"
4227
4228 # bug 21114
4229 test_39f() {
4230         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4231
4232         touch $DIR1/$tfile
4233         mtime1=`stat -c %Y $DIR1/$tfile`
4234
4235         sleep 2
4236         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4237
4238         for (( i=0; i < 2; i++ )) ; do
4239                 local mtime2=`stat -c %Y $DIR1/$tfile`
4240                 [ $mtime2 = $TEST_39_MTIME ] || \
4241                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4242
4243                 cancel_lru_locks $OSC
4244                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4245         done
4246 }
4247 run_test 39f "create, stat, sleep, utime, stat ================="
4248
4249 # bug 11063
4250 test_39g() {
4251         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4252
4253         echo hello >> $DIR1/$tfile
4254         local mtime1=`stat -c %Y $DIR1/$tfile`
4255
4256         sleep 2
4257         chmod o+r $DIR1/$tfile
4258
4259         for (( i=0; i < 2; i++ )) ; do
4260                 local mtime2=`stat -c %Y $DIR1/$tfile`
4261                 [ "$mtime1" = "$mtime2" ] || \
4262                         error "lost mtime: $mtime2, should be $mtime1"
4263
4264                 cancel_lru_locks $OSC
4265                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4266         done
4267 }
4268 run_test 39g "write, chmod, stat ==============================="
4269
4270 # bug 11063
4271 test_39h() {
4272         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4273
4274         touch $DIR1/$tfile
4275         sleep 1
4276
4277         local d1=`date`
4278         echo hello >> $DIR1/$tfile
4279         local mtime1=`stat -c %Y $DIR1/$tfile`
4280
4281         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4282         local d2=`date`
4283         if [ "$d1" != "$d2" ]; then
4284                 echo "write and touch not within one second"
4285         else
4286                 for (( i=0; i < 2; i++ )) ; do
4287                         local mtime2=`stat -c %Y $DIR1/$tfile`
4288                         [ "$mtime2" = $TEST_39_MTIME ] || \
4289                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
4290
4291                         cancel_lru_locks $OSC
4292                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4293                 done
4294         fi
4295 }
4296 run_test 39h "write, utime within one second, stat ============="
4297
4298 test_39i() {
4299         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4300
4301         touch $DIR1/$tfile
4302         sleep 1
4303
4304         echo hello >> $DIR1/$tfile
4305         local mtime1=`stat -c %Y $DIR1/$tfile`
4306
4307         mv $DIR1/$tfile $DIR1/$tfile-1
4308
4309         for (( i=0; i < 2; i++ )) ; do
4310                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4311
4312                 [ "$mtime1" = "$mtime2" ] || \
4313                         error "lost mtime: $mtime2, should be $mtime1"
4314
4315                 cancel_lru_locks $OSC
4316                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4317         done
4318 }
4319 run_test 39i "write, rename, stat =============================="
4320
4321 test_39j() {
4322         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4323
4324         start_full_debug_logging
4325         touch $DIR1/$tfile
4326         sleep 1
4327
4328         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
4329         lctl set_param fail_loc=0x80000412
4330         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
4331                 error "multiop failed"
4332         local multipid=$!
4333         local mtime1=`stat -c %Y $DIR1/$tfile`
4334
4335         mv $DIR1/$tfile $DIR1/$tfile-1
4336
4337         kill -USR1 $multipid
4338         wait $multipid || error "multiop close failed"
4339
4340         for (( i=0; i < 2; i++ )) ; do
4341                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4342                 [ "$mtime1" = "$mtime2" ] ||
4343                         error "mtime is lost on close: $mtime2, " \
4344                               "should be $mtime1"
4345
4346                 cancel_lru_locks
4347                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4348         done
4349         lctl set_param fail_loc=0
4350         stop_full_debug_logging
4351 }
4352 run_test 39j "write, rename, close, stat ======================="
4353
4354 test_39k() {
4355         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4356
4357         touch $DIR1/$tfile
4358         sleep 1
4359
4360         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
4361         local multipid=$!
4362         local mtime1=`stat -c %Y $DIR1/$tfile`
4363
4364         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4365
4366         kill -USR1 $multipid
4367         wait $multipid || error "multiop close failed"
4368
4369         for (( i=0; i < 2; i++ )) ; do
4370                 local mtime2=`stat -c %Y $DIR1/$tfile`
4371
4372                 [ "$mtime2" = $TEST_39_MTIME ] || \
4373                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
4374
4375                 cancel_lru_locks
4376                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4377         done
4378 }
4379 run_test 39k "write, utime, close, stat ========================"
4380
4381 # this should be set to future
4382 TEST_39_ATIME=`date -d "1 year" +%s`
4383
4384 test_39l() {
4385         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4386         remote_mds_nodsh && skip "remote MDS with nodsh"
4387
4388         local atime_diff=$(do_facet $SINGLEMDS \
4389                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4390         rm -rf $DIR/$tdir
4391         mkdir -p $DIR/$tdir
4392
4393         # test setting directory atime to future
4394         touch -a -d @$TEST_39_ATIME $DIR/$tdir
4395         local atime=$(stat -c %X $DIR/$tdir)
4396         [ "$atime" = $TEST_39_ATIME ] ||
4397                 error "atime is not set to future: $atime, $TEST_39_ATIME"
4398
4399         # test setting directory atime from future to now
4400         local now=$(date +%s)
4401         touch -a -d @$now $DIR/$tdir
4402
4403         atime=$(stat -c %X $DIR/$tdir)
4404         [ "$atime" -eq "$now"  ] ||
4405                 error "atime is not updated from future: $atime, $now"
4406
4407         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
4408         sleep 3
4409
4410         # test setting directory atime when now > dir atime + atime_diff
4411         local d1=$(date +%s)
4412         ls $DIR/$tdir
4413         local d2=$(date +%s)
4414         cancel_lru_locks mdc
4415         atime=$(stat -c %X $DIR/$tdir)
4416         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4417                 error "atime is not updated  : $atime, should be $d2"
4418
4419         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
4420         sleep 3
4421
4422         # test not setting directory atime when now < dir atime + atime_diff
4423         ls $DIR/$tdir
4424         cancel_lru_locks mdc
4425         atime=$(stat -c %X $DIR/$tdir)
4426         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4427                 error "atime is updated to $atime, should remain $d1<atime<$d2"
4428
4429         do_facet $SINGLEMDS \
4430                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4431 }
4432 run_test 39l "directory atime update ==========================="
4433
4434 test_39m() {
4435         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4436
4437         touch $DIR1/$tfile
4438         sleep 2
4439         local far_past_mtime=$(date -d "May 29 1953" +%s)
4440         local far_past_atime=$(date -d "Dec 17 1903" +%s)
4441
4442         touch -m -d @$far_past_mtime $DIR1/$tfile
4443         touch -a -d @$far_past_atime $DIR1/$tfile
4444
4445         for (( i=0; i < 2; i++ )) ; do
4446                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
4447                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
4448                         error "atime or mtime set incorrectly"
4449
4450                 cancel_lru_locks $OSC
4451                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4452         done
4453 }
4454 run_test 39m "test atime and mtime before 1970"
4455
4456 test_39n() { # LU-3832
4457         remote_mds_nodsh && skip "remote MDS with nodsh"
4458
4459         local atime_diff=$(do_facet $SINGLEMDS \
4460                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4461         local atime0
4462         local atime1
4463         local atime2
4464
4465         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
4466
4467         rm -rf $DIR/$tfile
4468         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
4469         atime0=$(stat -c %X $DIR/$tfile)
4470
4471         sleep 5
4472         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4473         atime1=$(stat -c %X $DIR/$tfile)
4474
4475         sleep 5
4476         cancel_lru_locks mdc
4477         cancel_lru_locks osc
4478         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4479         atime2=$(stat -c %X $DIR/$tfile)
4480
4481         do_facet $SINGLEMDS \
4482                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4483
4484         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
4485         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
4486 }
4487 run_test 39n "check that O_NOATIME is honored"
4488
4489 test_39o() {
4490         TESTDIR=$DIR/$tdir/$tfile
4491         [ -e $TESTDIR ] && rm -rf $TESTDIR
4492         mkdir -p $TESTDIR
4493         cd $TESTDIR
4494         links1=2
4495         ls
4496         mkdir a b
4497         ls
4498         links2=$(stat -c %h .)
4499         [ $(($links1 + 2)) != $links2 ] &&
4500                 error "wrong links count $(($links1 + 2)) != $links2"
4501         rmdir b
4502         links3=$(stat -c %h .)
4503         [ $(($links1 + 1)) != $links3 ] &&
4504                 error "wrong links count $links1 != $links3"
4505         return 0
4506 }
4507 run_test 39o "directory cached attributes updated after create"
4508
4509 test_39p() {
4510         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4511
4512         local MDTIDX=1
4513         TESTDIR=$DIR/$tdir/$tdir
4514         [ -e $TESTDIR ] && rm -rf $TESTDIR
4515         test_mkdir -p $TESTDIR
4516         cd $TESTDIR
4517         links1=2
4518         ls
4519         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
4520         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
4521         ls
4522         links2=$(stat -c %h .)
4523         [ $(($links1 + 2)) != $links2 ] &&
4524                 error "wrong links count $(($links1 + 2)) != $links2"
4525         rmdir remote_dir2
4526         links3=$(stat -c %h .)
4527         [ $(($links1 + 1)) != $links3 ] &&
4528                 error "wrong links count $links1 != $links3"
4529         return 0
4530 }
4531 run_test 39p "remote directory cached attributes updated after create ========"
4532
4533 test_39r() {
4534         [ $OST1_VERSION -ge $(version_code 2.13.52) ] ||
4535                 skip "no atime update on old OST"
4536         if [ "$ost1_FSTYPE" != ldiskfs ]; then
4537                 skip_env "ldiskfs only test"
4538         fi
4539
4540         local saved_adiff
4541         saved_adiff=$(do_facet ost1 \
4542                 lctl get_param -n obdfilter.*OST0000.atime_diff)
4543         stack_trap "do_facet ost1 \
4544                 lctl set_param obdfilter.*.atime_diff=$saved_adiff"
4545
4546         do_facet ost1 "lctl set_param obdfilter.*.atime_diff=5"
4547
4548         $LFS setstripe -i 0 $DIR/$tfile
4549         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 ||
4550                 error "can't write initial file"
4551         cancel_lru_locks osc
4552
4553         # exceed atime_diff and access file
4554         sleep 6
4555         dd if=$DIR/$tfile of=/dev/null || error "can't udpate atime"
4556
4557         local atime_cli=$(stat -c %X $DIR/$tfile)
4558         echo "client atime: $atime_cli"
4559         # allow atime update to be written to device
4560         do_facet ost1 "$LCTL set_param -n osd*.*OST*.force_sync 1"
4561         sleep 5
4562
4563         local ostdev=$(ostdevname 1)
4564         local fid=($(lfs getstripe -y $DIR/$tfile |
4565                         awk '/l_fid:/ { print $2 }' | tr ':' ' '))
4566         local objpath="O/0/d$((${fid[1]} % 32))/$((${fid[1]}))"
4567         local cmd="debugfs -c -R \\\"stat $objpath\\\" $ostdev"
4568
4569         echo "OST atime: $(do_facet ost1 "$cmd" |& grep atime)"
4570         local atime_ost=$(do_facet ost1 "$cmd" |&
4571                           awk -F'[: ]' '/atime:/ { print $4 }')
4572         (( atime_cli == atime_ost )) ||
4573                 error "atime on client $atime_cli != ost $atime_ost"
4574 }
4575 run_test 39r "lazy atime update on OST"
4576
4577 test_39q() { # LU-8041
4578         local testdir=$DIR/$tdir
4579         mkdir -p $testdir
4580         multiop_bg_pause $testdir D_c || error "multiop failed"
4581         local multipid=$!
4582         cancel_lru_locks mdc
4583         kill -USR1 $multipid
4584         local atime=$(stat -c %X $testdir)
4585         [ "$atime" -ne 0 ] || error "atime is zero"
4586 }
4587 run_test 39q "close won't zero out atime"
4588
4589 test_40() {
4590         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
4591         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
4592                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
4593         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
4594                 error "$tfile is not 4096 bytes in size"
4595 }
4596 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
4597
4598 test_41() {
4599         # bug 1553
4600         small_write $DIR/f41 18
4601 }
4602 run_test 41 "test small file write + fstat ====================="
4603
4604 count_ost_writes() {
4605         lctl get_param -n ${OSC}.*.stats |
4606                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
4607                         END { printf("%0.0f", writes) }'
4608 }
4609
4610 # decent default
4611 WRITEBACK_SAVE=500
4612 DIRTY_RATIO_SAVE=40
4613 MAX_DIRTY_RATIO=50
4614 BG_DIRTY_RATIO_SAVE=10
4615 MAX_BG_DIRTY_RATIO=25
4616
4617 start_writeback() {
4618         trap 0
4619         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
4620         # dirty_ratio, dirty_background_ratio
4621         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4622                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
4623                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
4624                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
4625         else
4626                 # if file not here, we are a 2.4 kernel
4627                 kill -CONT `pidof kupdated`
4628         fi
4629 }
4630
4631 stop_writeback() {
4632         # setup the trap first, so someone cannot exit the test at the
4633         # exact wrong time and mess up a machine
4634         trap start_writeback EXIT
4635         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
4636         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4637                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
4638                 sysctl -w vm.dirty_writeback_centisecs=0
4639                 sysctl -w vm.dirty_writeback_centisecs=0
4640                 # save and increase /proc/sys/vm/dirty_ratio
4641                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
4642                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
4643                 # save and increase /proc/sys/vm/dirty_background_ratio
4644                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
4645                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
4646         else
4647                 # if file not here, we are a 2.4 kernel
4648                 kill -STOP `pidof kupdated`
4649         fi
4650 }
4651
4652 # ensure that all stripes have some grant before we test client-side cache
4653 setup_test42() {
4654         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
4655                 dd if=/dev/zero of=$i bs=4k count=1
4656                 rm $i
4657         done
4658 }
4659
4660 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
4661 # file truncation, and file removal.
4662 test_42a() {
4663         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4664
4665         setup_test42
4666         cancel_lru_locks $OSC
4667         stop_writeback
4668         sync; sleep 1; sync # just to be safe
4669         BEFOREWRITES=`count_ost_writes`
4670         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
4671         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
4672         AFTERWRITES=`count_ost_writes`
4673         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
4674                 error "$BEFOREWRITES < $AFTERWRITES"
4675         start_writeback
4676 }
4677 run_test 42a "ensure that we don't flush on close"
4678
4679 test_42b() {
4680         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4681
4682         setup_test42
4683         cancel_lru_locks $OSC
4684         stop_writeback
4685         sync
4686         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
4687         BEFOREWRITES=$(count_ost_writes)
4688         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
4689         AFTERWRITES=$(count_ost_writes)
4690         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4691                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
4692         fi
4693         BEFOREWRITES=$(count_ost_writes)
4694         sync || error "sync: $?"
4695         AFTERWRITES=$(count_ost_writes)
4696         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4697                 error "$BEFOREWRITES < $AFTERWRITES on sync"
4698         fi
4699         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
4700         start_writeback
4701         return 0
4702 }
4703 run_test 42b "test destroy of file with cached dirty data ======"
4704
4705 # if these tests just want to test the effect of truncation,
4706 # they have to be very careful.  consider:
4707 # - the first open gets a {0,EOF}PR lock
4708 # - the first write conflicts and gets a {0, count-1}PW
4709 # - the rest of the writes are under {count,EOF}PW
4710 # - the open for truncate tries to match a {0,EOF}PR
4711 #   for the filesize and cancels the PWs.
4712 # any number of fixes (don't get {0,EOF} on open, match
4713 # composite locks, do smarter file size management) fix
4714 # this, but for now we want these tests to verify that
4715 # the cancellation with truncate intent works, so we
4716 # start the file with a full-file pw lock to match against
4717 # until the truncate.
4718 trunc_test() {
4719         test=$1
4720         file=$DIR/$test
4721         offset=$2
4722         cancel_lru_locks $OSC
4723         stop_writeback
4724         # prime the file with 0,EOF PW to match
4725         touch $file
4726         $TRUNCATE $file 0
4727         sync; sync
4728         # now the real test..
4729         dd if=/dev/zero of=$file bs=1024 count=100
4730         BEFOREWRITES=`count_ost_writes`
4731         $TRUNCATE $file $offset
4732         cancel_lru_locks $OSC
4733         AFTERWRITES=`count_ost_writes`
4734         start_writeback
4735 }
4736
4737 test_42c() {
4738         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4739
4740         trunc_test 42c 1024
4741         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
4742                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
4743         rm $file
4744 }
4745 run_test 42c "test partial truncate of file with cached dirty data"
4746
4747 test_42d() {
4748         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4749
4750         trunc_test 42d 0
4751         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
4752                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
4753         rm $file
4754 }
4755 run_test 42d "test complete truncate of file with cached dirty data"
4756
4757 test_42e() { # bug22074
4758         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4759
4760         local TDIR=$DIR/${tdir}e
4761         local pages=16 # hardcoded 16 pages, don't change it.
4762         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
4763         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
4764         local max_dirty_mb
4765         local warmup_files
4766
4767         test_mkdir $DIR/${tdir}e
4768         $LFS setstripe -c 1 $TDIR
4769         createmany -o $TDIR/f $files
4770
4771         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
4772
4773         # we assume that with $OSTCOUNT files, at least one of them will
4774         # be allocated on OST0.
4775         warmup_files=$((OSTCOUNT * max_dirty_mb))
4776         createmany -o $TDIR/w $warmup_files
4777
4778         # write a large amount of data into one file and sync, to get good
4779         # avail_grant number from OST.
4780         for ((i=0; i<$warmup_files; i++)); do
4781                 idx=$($LFS getstripe -i $TDIR/w$i)
4782                 [ $idx -ne 0 ] && continue
4783                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
4784                 break
4785         done
4786         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
4787         sync
4788         $LCTL get_param $proc_osc0/cur_dirty_bytes
4789         $LCTL get_param $proc_osc0/cur_grant_bytes
4790
4791         # create as much dirty pages as we can while not to trigger the actual
4792         # RPCs directly. but depends on the env, VFS may trigger flush during this
4793         # period, hopefully we are good.
4794         for ((i=0; i<$warmup_files; i++)); do
4795                 idx=$($LFS getstripe -i $TDIR/w$i)
4796                 [ $idx -ne 0 ] && continue
4797                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4798         done
4799         $LCTL get_param $proc_osc0/cur_dirty_bytes
4800         $LCTL get_param $proc_osc0/cur_grant_bytes
4801
4802         # perform the real test
4803         $LCTL set_param $proc_osc0/rpc_stats 0
4804         for ((;i<$files; i++)); do
4805                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
4806                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
4807         done
4808         sync
4809         $LCTL get_param $proc_osc0/rpc_stats
4810
4811         local percent=0
4812         local have_ppr=false
4813         $LCTL get_param $proc_osc0/rpc_stats |
4814                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4815                         # skip lines until we are at the RPC histogram data
4816                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4817                         $have_ppr || continue
4818
4819                         # we only want the percent stat for < 16 pages
4820                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4821
4822                         percent=$((percent + WPCT))
4823                         if [[ $percent -gt 15 ]]; then
4824                                 error "less than 16-pages write RPCs" \
4825                                       "$percent% > 15%"
4826                                 break
4827                         fi
4828                 done
4829         rm -rf $TDIR
4830 }
4831 run_test 42e "verify sub-RPC writes are not done synchronously"
4832
4833 test_43A() { # was test_43
4834         test_mkdir $DIR/$tdir
4835         cp -p /bin/ls $DIR/$tdir/$tfile
4836         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4837         pid=$!
4838         # give multiop a chance to open
4839         sleep 1
4840
4841         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4842         kill -USR1 $pid
4843 }
4844 run_test 43A "execution of file opened for write should return -ETXTBSY"
4845
4846 test_43a() {
4847         test_mkdir $DIR/$tdir
4848         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4849         $DIR/$tdir/sleep 60 &
4850         SLEEP_PID=$!
4851         # Make sure exec of $tdir/sleep wins race with truncate
4852         sleep 1
4853         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
4854         kill $SLEEP_PID
4855 }
4856 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4857
4858 test_43b() {
4859         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4860
4861         test_mkdir $DIR/$tdir
4862         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4863         $DIR/$tdir/sleep 60 &
4864         SLEEP_PID=$!
4865         # Make sure exec of $tdir/sleep wins race with truncate
4866         sleep 1
4867         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
4868         kill $SLEEP_PID
4869 }
4870 run_test 43b "truncate of file being executed should return -ETXTBSY"
4871
4872 test_43c() {
4873         local testdir="$DIR/$tdir"
4874         test_mkdir $testdir
4875         cp $SHELL $testdir/
4876         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4877                 ( cd $testdir && md5sum -c )
4878 }
4879 run_test 43c "md5sum of copy into lustre"
4880
4881 test_44A() { # was test_44
4882         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4883
4884         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4885         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4886 }
4887 run_test 44A "zero length read from a sparse stripe"
4888
4889 test_44a() {
4890         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4891                 awk '{ print $2 }')
4892         [ -z "$nstripe" ] && skip "can't get stripe info"
4893         [[ $nstripe -gt $OSTCOUNT ]] &&
4894                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4895
4896         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4897                 awk '{ print $2 }')
4898         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4899                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4900                         awk '{ print $2 }')
4901         fi
4902
4903         OFFSETS="0 $((stride/2)) $((stride-1))"
4904         for offset in $OFFSETS; do
4905                 for i in $(seq 0 $((nstripe-1))); do
4906                         local GLOBALOFFSETS=""
4907                         # size in Bytes
4908                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4909                         local myfn=$DIR/d44a-$size
4910                         echo "--------writing $myfn at $size"
4911                         ll_sparseness_write $myfn $size ||
4912                                 error "ll_sparseness_write"
4913                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4914                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4915                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4916
4917                         for j in $(seq 0 $((nstripe-1))); do
4918                                 # size in Bytes
4919                                 size=$((((j + $nstripe )*$stride + $offset)))
4920                                 ll_sparseness_write $myfn $size ||
4921                                         error "ll_sparseness_write"
4922                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4923                         done
4924                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4925                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4926                         rm -f $myfn
4927                 done
4928         done
4929 }
4930 run_test 44a "test sparse pwrite ==============================="
4931
4932 dirty_osc_total() {
4933         tot=0
4934         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4935                 tot=$(($tot + $d))
4936         done
4937         echo $tot
4938 }
4939 do_dirty_record() {
4940         before=`dirty_osc_total`
4941         echo executing "\"$*\""
4942         eval $*
4943         after=`dirty_osc_total`
4944         echo before $before, after $after
4945 }
4946 test_45() {
4947         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4948
4949         f="$DIR/f45"
4950         # Obtain grants from OST if it supports it
4951         echo blah > ${f}_grant
4952         stop_writeback
4953         sync
4954         do_dirty_record "echo blah > $f"
4955         [[ $before -eq $after ]] && error "write wasn't cached"
4956         do_dirty_record "> $f"
4957         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4958         do_dirty_record "echo blah > $f"
4959         [[ $before -eq $after ]] && error "write wasn't cached"
4960         do_dirty_record "sync"
4961         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4962         do_dirty_record "echo blah > $f"
4963         [[ $before -eq $after ]] && error "write wasn't cached"
4964         do_dirty_record "cancel_lru_locks osc"
4965         [[ $before -gt $after ]] ||
4966                 error "lock cancellation didn't lower dirty count"
4967         start_writeback
4968 }
4969 run_test 45 "osc io page accounting ============================"
4970
4971 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4972 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4973 # objects offset and an assert hit when an rpc was built with 1023's mapped
4974 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4975 test_46() {
4976         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4977
4978         f="$DIR/f46"
4979         stop_writeback
4980         sync
4981         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4982         sync
4983         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
4984         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4985         sync
4986         start_writeback
4987 }
4988 run_test 46 "dirtying a previously written page ================"
4989
4990 # test_47 is removed "Device nodes check" is moved to test_28
4991
4992 test_48a() { # bug 2399
4993         [ "$mds1_FSTYPE" = "zfs" ] &&
4994         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
4995                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
4996
4997         test_mkdir $DIR/$tdir
4998         cd $DIR/$tdir
4999         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
5000         test_mkdir $DIR/$tdir
5001         touch foo || error "'touch foo' failed after recreating cwd"
5002         test_mkdir bar
5003         touch .foo || error "'touch .foo' failed after recreating cwd"
5004         test_mkdir .bar
5005         ls . > /dev/null || error "'ls .' failed after recreating cwd"
5006         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5007         cd . || error "'cd .' failed after recreating cwd"
5008         mkdir . && error "'mkdir .' worked after recreating cwd"
5009         rmdir . && error "'rmdir .' worked after recreating cwd"
5010         ln -s . baz || error "'ln -s .' failed after recreating cwd"
5011         cd .. || error "'cd ..' failed after recreating cwd"
5012 }
5013 run_test 48a "Access renamed working dir (should return errors)="
5014
5015 test_48b() { # bug 2399
5016         rm -rf $DIR/$tdir
5017         test_mkdir $DIR/$tdir
5018         cd $DIR/$tdir
5019         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
5020         touch foo && error "'touch foo' worked after removing cwd"
5021         mkdir foo && error "'mkdir foo' worked after removing cwd"
5022         touch .foo && error "'touch .foo' worked after removing cwd"
5023         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
5024         ls . > /dev/null && error "'ls .' worked after removing cwd"
5025         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5026         mkdir . && error "'mkdir .' worked after removing cwd"
5027         rmdir . && error "'rmdir .' worked after removing cwd"
5028         ln -s . foo && error "'ln -s .' worked after removing cwd"
5029         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
5030 }
5031 run_test 48b "Access removed working dir (should return errors)="
5032
5033 test_48c() { # bug 2350
5034         #lctl set_param debug=-1
5035         #set -vx
5036         rm -rf $DIR/$tdir
5037         test_mkdir -p $DIR/$tdir/dir
5038         cd $DIR/$tdir/dir
5039         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5040         $TRACE touch foo && error "touch foo worked after removing cwd"
5041         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
5042         touch .foo && error "touch .foo worked after removing cwd"
5043         mkdir .foo && error "mkdir .foo worked after removing cwd"
5044         $TRACE ls . && error "'ls .' worked after removing cwd"
5045         $TRACE ls .. || error "'ls ..' failed after removing cwd"
5046         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
5047         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
5048         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
5049         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
5050 }
5051 run_test 48c "Access removed working subdir (should return errors)"
5052
5053 test_48d() { # bug 2350
5054         #lctl set_param debug=-1
5055         #set -vx
5056         rm -rf $DIR/$tdir
5057         test_mkdir -p $DIR/$tdir/dir
5058         cd $DIR/$tdir/dir
5059         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5060         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5061         $TRACE touch foo && error "'touch foo' worked after removing parent"
5062         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
5063         touch .foo && error "'touch .foo' worked after removing parent"
5064         mkdir .foo && error "mkdir .foo worked after removing parent"
5065         $TRACE ls . && error "'ls .' worked after removing parent"
5066         $TRACE ls .. && error "'ls ..' worked after removing parent"
5067         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
5068         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
5069         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
5070         true
5071 }
5072 run_test 48d "Access removed parent subdir (should return errors)"
5073
5074 test_48e() { # bug 4134
5075         #lctl set_param debug=-1
5076         #set -vx
5077         rm -rf $DIR/$tdir
5078         test_mkdir -p $DIR/$tdir/dir
5079         cd $DIR/$tdir/dir
5080         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5081         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5082         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
5083         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
5084         # On a buggy kernel addition of "touch foo" after cd .. will
5085         # produce kernel oops in lookup_hash_it
5086         touch ../foo && error "'cd ..' worked after recreate parent"
5087         cd $DIR
5088         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
5089 }
5090 run_test 48e "Access to recreated parent subdir (should return errors)"
5091
5092 test_49() { # LU-1030
5093         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5094         remote_ost_nodsh && skip "remote OST with nodsh"
5095
5096         # get ost1 size - $FSNAME-OST0000
5097         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
5098                 awk '{ print $4 }')
5099         # write 800M at maximum
5100         [[ $ost1_size -lt 2 ]] && ost1_size=2
5101         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
5102
5103         $LFS setstripe -c 1 -i 0 $DIR/$tfile
5104         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
5105         local dd_pid=$!
5106
5107         # change max_pages_per_rpc while writing the file
5108         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
5109         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
5110         # loop until dd process exits
5111         while ps ax -opid | grep -wq $dd_pid; do
5112                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
5113                 sleep $((RANDOM % 5 + 1))
5114         done
5115         # restore original max_pages_per_rpc
5116         $LCTL set_param $osc1_mppc=$orig_mppc
5117         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
5118 }
5119 run_test 49 "Change max_pages_per_rpc won't break osc extent"
5120
5121 test_50() {
5122         # bug 1485
5123         test_mkdir $DIR/$tdir
5124         cd $DIR/$tdir
5125         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
5126 }
5127 run_test 50 "special situations: /proc symlinks  ==============="
5128
5129 test_51a() {    # was test_51
5130         # bug 1516 - create an empty entry right after ".." then split dir
5131         test_mkdir -c1 $DIR/$tdir
5132         touch $DIR/$tdir/foo
5133         $MCREATE $DIR/$tdir/bar
5134         rm $DIR/$tdir/foo
5135         createmany -m $DIR/$tdir/longfile 201
5136         FNUM=202
5137         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
5138                 $MCREATE $DIR/$tdir/longfile$FNUM
5139                 FNUM=$(($FNUM + 1))
5140                 echo -n "+"
5141         done
5142         echo
5143         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
5144 }
5145 run_test 51a "special situations: split htree with empty entry =="
5146
5147 cleanup_print_lfs_df () {
5148         trap 0
5149         $LFS df
5150         $LFS df -i
5151 }
5152
5153 test_51b() {
5154         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5155
5156         local dir=$DIR/$tdir
5157         local nrdirs=$((65536 + 100))
5158
5159         # cleanup the directory
5160         rm -fr $dir
5161
5162         test_mkdir -c1 $dir
5163
5164         $LFS df
5165         $LFS df -i
5166         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
5167         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
5168         [[ $numfree -lt $nrdirs ]] &&
5169                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
5170
5171         # need to check free space for the directories as well
5172         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
5173         numfree=$(( blkfree / $(fs_inode_ksize) ))
5174         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
5175
5176         trap cleanup_print_lfs_df EXIT
5177
5178         # create files
5179         createmany -d $dir/d $nrdirs || {
5180                 unlinkmany $dir/d $nrdirs
5181                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
5182         }
5183
5184         # really created :
5185         nrdirs=$(ls -U $dir | wc -l)
5186
5187         # unlink all but 100 subdirectories, then check it still works
5188         local left=100
5189         local delete=$((nrdirs - left))
5190
5191         $LFS df
5192         $LFS df -i
5193
5194         # for ldiskfs the nlink count should be 1, but this is OSD specific
5195         # and so this is listed for informational purposes only
5196         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
5197         unlinkmany -d $dir/d $delete ||
5198                 error "unlink of first $delete subdirs failed"
5199
5200         echo "nlink between: $(stat -c %h $dir)"
5201         local found=$(ls -U $dir | wc -l)
5202         [ $found -ne $left ] &&
5203                 error "can't find subdirs: found only $found, expected $left"
5204
5205         unlinkmany -d $dir/d $delete $left ||
5206                 error "unlink of second $left subdirs failed"
5207         # regardless of whether the backing filesystem tracks nlink accurately
5208         # or not, the nlink count shouldn't be more than "." and ".." here
5209         local after=$(stat -c %h $dir)
5210         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
5211                 echo "nlink after: $after"
5212
5213         cleanup_print_lfs_df
5214 }
5215 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
5216
5217 test_51d() {
5218         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5219         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
5220
5221         test_mkdir $DIR/$tdir
5222         createmany -o $DIR/$tdir/t- 1000
5223         $LFS getstripe $DIR/$tdir > $TMP/$tfile
5224         for N in $(seq 0 $((OSTCOUNT - 1))); do
5225                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
5226                         END { printf("%0.0f", objs) }' $TMP/$tfile)
5227                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
5228                         '($1 == '$N') { objs += 1 } \
5229                         END { printf("%0.0f", objs) }')
5230                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
5231         done
5232         unlinkmany $DIR/$tdir/t- 1000
5233
5234         NLAST=0
5235         for N in $(seq 1 $((OSTCOUNT - 1))); do
5236                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
5237                         error "OST $N has less objects vs OST $NLAST" \
5238                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5239                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
5240                         error "OST $N has less objects vs OST $NLAST" \
5241                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5242
5243                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
5244                         error "OST $N has less #0 objects vs OST $NLAST" \
5245                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5246                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
5247                         error "OST $N has less #0 objects vs OST $NLAST" \
5248                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5249                 NLAST=$N
5250         done
5251         rm -f $TMP/$tfile
5252 }
5253 run_test 51d "check object distribution"
5254
5255 test_51e() {
5256         if [ "$mds1_FSTYPE" != ldiskfs ]; then
5257                 skip_env "ldiskfs only test"
5258         fi
5259
5260         test_mkdir -c1 $DIR/$tdir
5261         test_mkdir -c1 $DIR/$tdir/d0
5262
5263         touch $DIR/$tdir/d0/foo
5264         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
5265                 error "file exceed 65000 nlink limit!"
5266         unlinkmany $DIR/$tdir/d0/f- 65001
5267         return 0
5268 }
5269 run_test 51e "check file nlink limit"
5270
5271 test_51f() {
5272         test_mkdir $DIR/$tdir
5273
5274         local max=100000
5275         local ulimit_old=$(ulimit -n)
5276         local spare=20 # number of spare fd's for scripts/libraries, etc.
5277         local mdt=$($LFS getstripe -m $DIR/$tdir)
5278         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
5279
5280         echo "MDT$mdt numfree=$numfree, max=$max"
5281         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
5282         if [ $((numfree + spare)) -gt $ulimit_old ]; then
5283                 while ! ulimit -n $((numfree + spare)); do
5284                         numfree=$((numfree * 3 / 4))
5285                 done
5286                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
5287         else
5288                 echo "left ulimit at $ulimit_old"
5289         fi
5290
5291         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
5292                 unlinkmany $DIR/$tdir/f $numfree
5293                 error "create+open $numfree files in $DIR/$tdir failed"
5294         }
5295         ulimit -n $ulimit_old
5296
5297         # if createmany exits at 120s there will be fewer than $numfree files
5298         unlinkmany $DIR/$tdir/f $numfree || true
5299 }
5300 run_test 51f "check many open files limit"
5301
5302 test_52a() {
5303         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
5304         test_mkdir $DIR/$tdir
5305         touch $DIR/$tdir/foo
5306         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
5307         echo bar >> $DIR/$tdir/foo || error "append bar failed"
5308         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5309         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5310         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5311                                         error "link worked"
5312         echo foo >> $DIR/$tdir/foo || error "append foo failed"
5313         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5314         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
5315                                                      error "lsattr"
5316         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
5317         cp -r $DIR/$tdir $TMP/
5318         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
5319 }
5320 run_test 52a "append-only flag test (should return errors)"
5321
5322 test_52b() {
5323         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
5324         test_mkdir $DIR/$tdir
5325         touch $DIR/$tdir/foo
5326         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
5327         cat test > $DIR/$tdir/foo && error "cat test worked"
5328         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5329         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5330         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5331                                         error "link worked"
5332         echo foo >> $DIR/$tdir/foo && error "echo worked"
5333         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5334         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
5335         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
5336         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
5337                                                         error "lsattr"
5338         chattr -i $DIR/$tdir/foo || error "chattr failed"
5339
5340         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
5341 }
5342 run_test 52b "immutable flag test (should return errors) ======="
5343
5344 test_53() {
5345         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5346         remote_mds_nodsh && skip "remote MDS with nodsh"
5347         remote_ost_nodsh && skip "remote OST with nodsh"
5348
5349         local param
5350         local param_seq
5351         local ostname
5352         local mds_last
5353         local mds_last_seq
5354         local ost_last
5355         local ost_last_seq
5356         local ost_last_id
5357         local ostnum
5358         local node
5359         local found=false
5360         local support_last_seq=true
5361
5362         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
5363                 support_last_seq=false
5364
5365         # only test MDT0000
5366         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
5367         local value
5368         for value in $(do_facet $SINGLEMDS \
5369                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
5370                 param=$(echo ${value[0]} | cut -d "=" -f1)
5371                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
5372
5373                 if $support_last_seq; then
5374                         param_seq=$(echo $param |
5375                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
5376                         mds_last_seq=$(do_facet $SINGLEMDS \
5377                                        $LCTL get_param -n $param_seq)
5378                 fi
5379                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
5380
5381                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
5382                 node=$(facet_active_host ost$((ostnum+1)))
5383                 param="obdfilter.$ostname.last_id"
5384                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
5385                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
5386                         ost_last_id=$ost_last
5387
5388                         if $support_last_seq; then
5389                                 ost_last_id=$(echo $ost_last |
5390                                               awk -F':' '{print $2}' |
5391                                               sed -e "s/^0x//g")
5392                                 ost_last_seq=$(echo $ost_last |
5393                                                awk -F':' '{print $1}')
5394                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
5395                         fi
5396
5397                         if [[ $ost_last_id != $mds_last ]]; then
5398                                 error "$ost_last_id != $mds_last"
5399                         else
5400                                 found=true
5401                                 break
5402                         fi
5403                 done
5404         done
5405         $found || error "can not match last_seq/last_id for $mdtosc"
5406         return 0
5407 }
5408 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
5409
5410 test_54a() {
5411         perl -MSocket -e ';' || skip "no Socket perl module installed"
5412
5413         $SOCKETSERVER $DIR/socket ||
5414                 error "$SOCKETSERVER $DIR/socket failed: $?"
5415         $SOCKETCLIENT $DIR/socket ||
5416                 error "$SOCKETCLIENT $DIR/socket failed: $?"
5417         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
5418 }
5419 run_test 54a "unix domain socket test =========================="
5420
5421 test_54b() {
5422         f="$DIR/f54b"
5423         mknod $f c 1 3
5424         chmod 0666 $f
5425         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
5426 }
5427 run_test 54b "char device works in lustre ======================"
5428
5429 find_loop_dev() {
5430         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
5431         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
5432         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
5433
5434         for i in $(seq 3 7); do
5435                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
5436                 LOOPDEV=$LOOPBASE$i
5437                 LOOPNUM=$i
5438                 break
5439         done
5440 }
5441
5442 cleanup_54c() {
5443         local rc=0
5444         loopdev="$DIR/loop54c"
5445
5446         trap 0
5447         $UMOUNT $DIR/$tdir || rc=$?
5448         losetup -d $loopdev || true
5449         losetup -d $LOOPDEV || true
5450         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
5451         return $rc
5452 }
5453
5454 test_54c() {
5455         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5456
5457         loopdev="$DIR/loop54c"
5458
5459         find_loop_dev
5460         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
5461         trap cleanup_54c EXIT
5462         mknod $loopdev b 7 $LOOPNUM
5463         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
5464         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
5465         losetup $loopdev $DIR/$tfile ||
5466                 error "can't set up $loopdev for $DIR/$tfile"
5467         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
5468         test_mkdir $DIR/$tdir
5469         mount -t ext2 $loopdev $DIR/$tdir ||
5470                 error "error mounting $loopdev on $DIR/$tdir"
5471         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
5472                 error "dd write"
5473         df $DIR/$tdir
5474         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
5475                 error "dd read"
5476         cleanup_54c
5477 }
5478 run_test 54c "block device works in lustre ====================="
5479
5480 test_54d() {
5481         f="$DIR/f54d"
5482         string="aaaaaa"
5483         mknod $f p
5484         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
5485 }
5486 run_test 54d "fifo device works in lustre ======================"
5487
5488 test_54e() {
5489         f="$DIR/f54e"
5490         string="aaaaaa"
5491         cp -aL /dev/console $f
5492         echo $string > $f || error "echo $string to $f failed"
5493 }
5494 run_test 54e "console/tty device works in lustre ======================"
5495
5496 test_56a() {
5497         local numfiles=3
5498         local dir=$DIR/$tdir
5499
5500         rm -rf $dir
5501         test_mkdir -p $dir/dir
5502         for i in $(seq $numfiles); do
5503                 touch $dir/file$i
5504                 touch $dir/dir/file$i
5505         done
5506
5507         local numcomp=$($LFS getstripe --component-count $dir)
5508
5509         [[ $numcomp == 0 ]] && numcomp=1
5510
5511         # test lfs getstripe with --recursive
5512         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
5513
5514         [[ $filenum -eq $((numfiles * 2)) ]] ||
5515                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
5516         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
5517         [[ $filenum -eq $numfiles ]] ||
5518                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
5519         echo "$LFS getstripe showed obdidx or l_ost_idx"
5520
5521         # test lfs getstripe with file instead of dir
5522         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
5523         [[ $filenum -eq 1 ]] ||
5524                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
5525         echo "$LFS getstripe file1 passed"
5526
5527         #test lfs getstripe with --verbose
5528         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
5529         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5530                 error "$LFS getstripe --verbose $dir: "\
5531                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
5532         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
5533                 error "$LFS getstripe $dir: showed lmm_magic"
5534
5535         #test lfs getstripe with -v prints lmm_fid
5536         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
5537         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5538                 error "$LFS getstripe -v $dir: "\
5539                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
5540         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
5541                 error "$LFS getstripe $dir: showed lmm_fid by default"
5542         echo "$LFS getstripe --verbose passed"
5543
5544         #check for FID information
5545         local fid1=$($LFS getstripe --fid $dir/file1)
5546         local fid2=$($LFS getstripe --verbose $dir/file1 |
5547                      awk '/lmm_fid: / { print $2; exit; }')
5548         local fid3=$($LFS path2fid $dir/file1)
5549
5550         [ "$fid1" != "$fid2" ] &&
5551                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
5552         [ "$fid1" != "$fid3" ] &&
5553                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
5554         echo "$LFS getstripe --fid passed"
5555
5556         #test lfs getstripe with --obd
5557         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
5558                 error "$LFS getstripe --obd wrong_uuid: should return error"
5559
5560         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5561
5562         local ostidx=1
5563         local obduuid=$(ostuuid_from_index $ostidx)
5564         local found=$($LFS getstripe -r --obd $obduuid $dir |
5565                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
5566
5567         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
5568         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
5569                 ((filenum--))
5570         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
5571                 ((filenum--))
5572
5573         [[ $found -eq $filenum ]] ||
5574                 error "$LFS getstripe --obd: found $found expect $filenum"
5575         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
5576                 sed '/^[         ]*'${ostidx}'[  ]/d' |
5577                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
5578                 error "$LFS getstripe --obd: should not show file on other obd"
5579         echo "$LFS getstripe --obd passed"
5580 }
5581 run_test 56a "check $LFS getstripe"
5582
5583 test_56b() {
5584         local dir=$DIR/$tdir
5585         local numdirs=3
5586
5587         test_mkdir $dir
5588         for i in $(seq $numdirs); do
5589                 test_mkdir $dir/dir$i
5590         done
5591
5592         # test lfs getdirstripe default mode is non-recursion, which is
5593         # different from lfs getstripe
5594         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
5595
5596         [[ $dircnt -eq 1 ]] ||
5597                 error "$LFS getdirstripe: found $dircnt, not 1"
5598         dircnt=$($LFS getdirstripe --recursive $dir |
5599                 grep -c lmv_stripe_count)
5600         [[ $dircnt -eq $((numdirs + 1)) ]] ||
5601                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
5602 }
5603 run_test 56b "check $LFS getdirstripe"
5604
5605 test_56c() {
5606         remote_ost_nodsh && skip "remote OST with nodsh"
5607
5608         local ost_idx=0
5609         local ost_name=$(ostname_from_index $ost_idx)
5610         local old_status=$(ost_dev_status $ost_idx)
5611
5612         [[ -z "$old_status" ]] ||
5613                 skip_env "OST $ost_name is in $old_status status"
5614
5615         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
5616         [[ $OST1_VERSION -ge $(version_code 2.12.55) ]] && do_facet ost1 \
5617                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
5618         sleep_maxage
5619
5620         local new_status=$(ost_dev_status $ost_idx)
5621
5622         [[ "$new_status" =~ "D" ]] ||
5623                 error "$ost_name status is '$new_status', missing 'D'"
5624         if [[ $OST1_VERSION -ge $(version_code 2.12.55) ]]; then
5625                 [[ "$new_status" =~ "N" ]] ||
5626                         error "$ost_name status is '$new_status', missing 'N'"
5627         fi
5628
5629         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
5630         [[ $OST1_VERSION -ge $(version_code 2.12.55) ]] && do_facet ost1 \
5631                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
5632         sleep_maxage
5633
5634         new_status=$(ost_dev_status $ost_idx)
5635         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
5636                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
5637 }
5638 run_test 56c "check 'lfs df' showing device status"
5639
5640 NUMFILES=3
5641 NUMDIRS=3
5642 setup_56() {
5643         local local_tdir="$1"
5644         local local_numfiles="$2"
5645         local local_numdirs="$3"
5646         local dir_params="$4"
5647         local dir_stripe_params="$5"
5648
5649         if [ ! -d "$local_tdir" ] ; then
5650                 test_mkdir -p $dir_stripe_params $local_tdir
5651                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
5652                 for i in $(seq $local_numfiles) ; do
5653                         touch $local_tdir/file$i
5654                 done
5655                 for i in $(seq $local_numdirs) ; do
5656                         test_mkdir $dir_stripe_params $local_tdir/dir$i
5657                         for j in $(seq $local_numfiles) ; do
5658                                 touch $local_tdir/dir$i/file$j
5659                         done
5660                 done
5661         fi
5662 }
5663
5664 setup_56_special() {
5665         local local_tdir=$1
5666         local local_numfiles=$2
5667         local local_numdirs=$3
5668
5669         setup_56 $local_tdir $local_numfiles $local_numdirs
5670
5671         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
5672                 for i in $(seq $local_numfiles) ; do
5673                         mknod $local_tdir/loop${i}b b 7 $i
5674                         mknod $local_tdir/null${i}c c 1 3
5675                         ln -s $local_tdir/file1 $local_tdir/link${i}
5676                 done
5677                 for i in $(seq $local_numdirs) ; do
5678                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
5679                         mknod $local_tdir/dir$i/null${i}c c 1 3
5680                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
5681                 done
5682         fi
5683 }
5684
5685 test_56g() {
5686         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5687         local expected=$(($NUMDIRS + 2))
5688
5689         setup_56 $dir $NUMFILES $NUMDIRS
5690
5691         # test lfs find with -name
5692         for i in $(seq $NUMFILES) ; do
5693                 local nums=$($LFS find -name "*$i" $dir | wc -l)
5694
5695                 [ $nums -eq $expected ] ||
5696                         error "lfs find -name '*$i' $dir wrong: "\
5697                               "found $nums, expected $expected"
5698         done
5699 }
5700 run_test 56g "check lfs find -name"
5701
5702 test_56h() {
5703         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5704         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
5705
5706         setup_56 $dir $NUMFILES $NUMDIRS
5707
5708         # test lfs find with ! -name
5709         for i in $(seq $NUMFILES) ; do
5710                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
5711
5712                 [ $nums -eq $expected ] ||
5713                         error "lfs find ! -name '*$i' $dir wrong: "\
5714                               "found $nums, expected $expected"
5715         done
5716 }
5717 run_test 56h "check lfs find ! -name"
5718
5719 test_56i() {
5720         local dir=$DIR/$tdir
5721
5722         test_mkdir $dir
5723
5724         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
5725         local out=$($cmd)
5726
5727         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
5728 }
5729 run_test 56i "check 'lfs find -ost UUID' skips directories"
5730
5731 test_56j() {
5732         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5733
5734         setup_56_special $dir $NUMFILES $NUMDIRS
5735
5736         local expected=$((NUMDIRS + 1))
5737         local cmd="$LFS find -type d $dir"
5738         local nums=$($cmd | wc -l)
5739
5740         [ $nums -eq $expected ] ||
5741                 error "'$cmd' wrong: found $nums, expected $expected"
5742 }
5743 run_test 56j "check lfs find -type d"
5744
5745 test_56k() {
5746         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5747
5748         setup_56_special $dir $NUMFILES $NUMDIRS
5749
5750         local expected=$(((NUMDIRS + 1) * NUMFILES))
5751         local cmd="$LFS find -type f $dir"
5752         local nums=$($cmd | wc -l)
5753
5754         [ $nums -eq $expected ] ||
5755                 error "'$cmd' wrong: found $nums, expected $expected"
5756 }
5757 run_test 56k "check lfs find -type f"
5758
5759 test_56l() {
5760         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5761
5762         setup_56_special $dir $NUMFILES $NUMDIRS
5763
5764         local expected=$((NUMDIRS + NUMFILES))
5765         local cmd="$LFS find -type b $dir"
5766         local nums=$($cmd | wc -l)
5767
5768         [ $nums -eq $expected ] ||
5769                 error "'$cmd' wrong: found $nums, expected $expected"
5770 }
5771 run_test 56l "check lfs find -type b"
5772
5773 test_56m() {
5774         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5775
5776         setup_56_special $dir $NUMFILES $NUMDIRS
5777
5778         local expected=$((NUMDIRS + NUMFILES))
5779         local cmd="$LFS find -type c $dir"
5780         local nums=$($cmd | wc -l)
5781         [ $nums -eq $expected ] ||
5782                 error "'$cmd' wrong: found $nums, expected $expected"
5783 }
5784 run_test 56m "check lfs find -type c"
5785
5786 test_56n() {
5787         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5788         setup_56_special $dir $NUMFILES $NUMDIRS
5789
5790         local expected=$((NUMDIRS + NUMFILES))
5791         local cmd="$LFS find -type l $dir"
5792         local nums=$($cmd | wc -l)
5793
5794         [ $nums -eq $expected ] ||
5795                 error "'$cmd' wrong: found $nums, expected $expected"
5796 }
5797 run_test 56n "check lfs find -type l"
5798
5799 test_56o() {
5800         local dir=$DIR/$tdir
5801
5802         setup_56 $dir $NUMFILES $NUMDIRS
5803         utime $dir/file1 > /dev/null || error "utime (1)"
5804         utime $dir/file2 > /dev/null || error "utime (2)"
5805         utime $dir/dir1 > /dev/null || error "utime (3)"
5806         utime $dir/dir2 > /dev/null || error "utime (4)"
5807         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5808         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5809
5810         local expected=4
5811         local nums=$($LFS find -mtime +0 $dir | wc -l)
5812
5813         [ $nums -eq $expected ] ||
5814                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5815
5816         expected=12
5817         cmd="$LFS find -mtime 0 $dir"
5818         nums=$($cmd | wc -l)
5819         [ $nums -eq $expected ] ||
5820                 error "'$cmd' wrong: found $nums, expected $expected"
5821 }
5822 run_test 56o "check lfs find -mtime for old files"
5823
5824 test_56ob() {
5825         local dir=$DIR/$tdir
5826         local expected=1
5827         local count=0
5828
5829         # just to make sure there is something that won't be found
5830         test_mkdir $dir
5831         touch $dir/$tfile.now
5832
5833         for age in year week day hour min; do
5834                 count=$((count + 1))
5835
5836                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
5837                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
5838                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
5839
5840                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
5841                 local nums=$($cmd | wc -l)
5842                 [ $nums -eq $expected ] ||
5843                         error "'$cmd' wrong: found $nums, expected $expected"
5844
5845                 cmd="$LFS find $dir -atime $count${age:0:1}"
5846                 nums=$($cmd | wc -l)
5847                 [ $nums -eq $expected ] ||
5848                         error "'$cmd' wrong: found $nums, expected $expected"
5849         done
5850
5851         sleep 2
5852         cmd="$LFS find $dir -ctime +1s -type f"
5853         nums=$($cmd | wc -l)
5854         (( $nums == $count * 2 + 1)) ||
5855                 error "'$cmd' wrong: found $nums, expected $((expected*2+1))"
5856 }
5857 run_test 56ob "check lfs find -atime -mtime -ctime with units"
5858
5859 test_newerXY_base() {
5860         local x=$1
5861         local y=$2
5862         local dir=$DIR/$tdir
5863         local ref
5864         local negref
5865
5866         if [ $y == "t" ]; then
5867                 ref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5868         else
5869                 ref=$DIR/$tfile.newer
5870                 touch $ref || error "touch $ref failed"
5871         fi
5872         sleep 2
5873         setup_56 $dir $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
5874         sleep 2
5875         if [ $y == "t" ]; then
5876                 negref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5877         else
5878                 negref=$DIR/$tfile.newerneg
5879                 touch $negref || error "touch $negref failed"
5880         fi
5881
5882         local cmd="$LFS find $dir -newer$x$y $ref"
5883         local nums=$(eval $cmd | wc -l)
5884         local expected=$(((NUMFILES + 2) * NUMDIRS + 1))
5885
5886         [ $nums -eq $expected ] ||
5887                 error "'$cmd' wrong: found $nums, expected $expected"
5888
5889         cmd="$LFS find $dir ! -newer$x$y $negref"
5890         nums=$(eval $cmd | wc -l)
5891         [ $nums -eq $expected ] ||
5892                 error "'$cmd' wrong: found $nums, expected $expected"
5893
5894         cmd="$LFS find $dir -newer$x$y $ref ! -newer$x$y $negref"
5895         nums=$(eval $cmd | wc -l)
5896         [ $nums -eq $expected ] ||
5897                 error "'$cmd' wrong: found $nums, expected $expected"
5898
5899         rm -rf $DIR/*
5900 }
5901
5902 test_56oc() {
5903         test_newerXY_base "a" "a"
5904         test_newerXY_base "a" "m"
5905         test_newerXY_base "a" "c"
5906         test_newerXY_base "m" "a"
5907         test_newerXY_base "m" "m"
5908         test_newerXY_base "m" "c"
5909         test_newerXY_base "c" "a"
5910         test_newerXY_base "c" "m"
5911         test_newerXY_base "c" "c"
5912         test_newerXY_base "a" "t"
5913         test_newerXY_base "m" "t"
5914         test_newerXY_base "c" "t"
5915 }
5916 run_test 56oc "check lfs find -newerXY work"
5917
5918 test_56p() {
5919         [ $RUNAS_ID -eq $UID ] &&
5920                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5921
5922         local dir=$DIR/$tdir
5923
5924         setup_56 $dir $NUMFILES $NUMDIRS
5925         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
5926
5927         local expected=$NUMFILES
5928         local cmd="$LFS find -uid $RUNAS_ID $dir"
5929         local nums=$($cmd | wc -l)
5930
5931         [ $nums -eq $expected ] ||
5932                 error "'$cmd' wrong: found $nums, expected $expected"
5933
5934         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
5935         cmd="$LFS find ! -uid $RUNAS_ID $dir"
5936         nums=$($cmd | wc -l)
5937         [ $nums -eq $expected ] ||
5938                 error "'$cmd' wrong: found $nums, expected $expected"
5939 }
5940 run_test 56p "check lfs find -uid and ! -uid"
5941
5942 test_56q() {
5943         [ $RUNAS_ID -eq $UID ] &&
5944                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5945
5946         local dir=$DIR/$tdir
5947
5948         setup_56 $dir $NUMFILES $NUMDIRS
5949         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
5950
5951         local expected=$NUMFILES
5952         local cmd="$LFS find -gid $RUNAS_GID $dir"
5953         local nums=$($cmd | wc -l)
5954
5955         [ $nums -eq $expected ] ||
5956                 error "'$cmd' wrong: found $nums, expected $expected"
5957
5958         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
5959         cmd="$LFS find ! -gid $RUNAS_GID $dir"
5960         nums=$($cmd | wc -l)
5961         [ $nums -eq $expected ] ||
5962                 error "'$cmd' wrong: found $nums, expected $expected"
5963 }
5964 run_test 56q "check lfs find -gid and ! -gid"
5965
5966 test_56r() {
5967         local dir=$DIR/$tdir
5968
5969         setup_56 $dir $NUMFILES $NUMDIRS
5970
5971         local expected=12
5972         local cmd="$LFS find -size 0 -type f -lazy $dir"
5973         local nums=$($cmd | wc -l)
5974
5975         [ $nums -eq $expected ] ||
5976                 error "'$cmd' wrong: found $nums, expected $expected"
5977         cmd="$LFS find -size 0 -type f $dir"
5978         nums=$($cmd | wc -l)
5979         [ $nums -eq $expected ] ||
5980                 error "'$cmd' wrong: found $nums, expected $expected"
5981
5982         expected=0
5983         cmd="$LFS find ! -size 0 -type f -lazy $dir"
5984         nums=$($cmd | wc -l)
5985         [ $nums -eq $expected ] ||
5986                 error "'$cmd' wrong: found $nums, expected $expected"
5987         cmd="$LFS find ! -size 0 -type f $dir"
5988         nums=$($cmd | wc -l)
5989         [ $nums -eq $expected ] ||
5990                 error "'$cmd' wrong: found $nums, expected $expected"
5991
5992         echo "test" > $dir/$tfile
5993         echo "test2" > $dir/$tfile.2 && sync
5994         expected=1
5995         cmd="$LFS find -size 5 -type f -lazy $dir"
5996         nums=$($cmd | wc -l)
5997         [ $nums -eq $expected ] ||
5998                 error "'$cmd' wrong: found $nums, expected $expected"
5999         cmd="$LFS find -size 5 -type f $dir"
6000         nums=$($cmd | wc -l)
6001         [ $nums -eq $expected ] ||
6002                 error "'$cmd' wrong: found $nums, expected $expected"
6003
6004         expected=1
6005         cmd="$LFS find -size +5 -type f -lazy $dir"
6006         nums=$($cmd | wc -l)
6007         [ $nums -eq $expected ] ||
6008                 error "'$cmd' wrong: found $nums, expected $expected"
6009         cmd="$LFS find -size +5 -type f $dir"
6010         nums=$($cmd | wc -l)
6011         [ $nums -eq $expected ] ||
6012                 error "'$cmd' wrong: found $nums, expected $expected"
6013
6014         expected=2
6015         cmd="$LFS find -size +0 -type f -lazy $dir"
6016         nums=$($cmd | wc -l)
6017         [ $nums -eq $expected ] ||
6018                 error "'$cmd' wrong: found $nums, expected $expected"
6019         cmd="$LFS find -size +0 -type f $dir"
6020         nums=$($cmd | wc -l)
6021         [ $nums -eq $expected ] ||
6022                 error "'$cmd' wrong: found $nums, expected $expected"
6023
6024         expected=2
6025         cmd="$LFS find ! -size -5 -type f -lazy $dir"
6026         nums=$($cmd | wc -l)
6027         [ $nums -eq $expected ] ||
6028                 error "'$cmd' wrong: found $nums, expected $expected"
6029         cmd="$LFS find ! -size -5 -type f $dir"
6030         nums=$($cmd | wc -l)
6031         [ $nums -eq $expected ] ||
6032                 error "'$cmd' wrong: found $nums, expected $expected"
6033
6034         expected=12
6035         cmd="$LFS find -size -5 -type f -lazy $dir"
6036         nums=$($cmd | wc -l)
6037         [ $nums -eq $expected ] ||
6038                 error "'$cmd' wrong: found $nums, expected $expected"
6039         cmd="$LFS find -size -5 -type f $dir"
6040         nums=$($cmd | wc -l)
6041         [ $nums -eq $expected ] ||
6042                 error "'$cmd' wrong: found $nums, expected $expected"
6043 }
6044 run_test 56r "check lfs find -size works"
6045
6046 test_56ra() {
6047         [[ $MDS1_VERSION -gt $(version_code 2.12.58) ]] ||
6048                 skip "MDS < 2.12.58 doesn't return LSOM data"
6049         local dir=$DIR/$tdir
6050
6051         [[ $OSC == "mdc" ]] && skip "DoM files" && return
6052
6053         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6054
6055         cancel_lru_locks $OSC
6056
6057         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6058         local expected=12
6059         local cmd="$LFS find -size 0 -type f -lazy $dir"
6060         local nums=$($cmd | wc -l)
6061
6062         [ $nums -eq $expected ] ||
6063                 error "'$cmd' wrong: found $nums, expected $expected"
6064
6065         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6066         [ $rpcs_before -eq $rpcs_after ] ||
6067                 error "'$cmd' should not send glimpse RPCs to OST"
6068         cmd="$LFS find -size 0 -type f $dir"
6069         nums=$($cmd | wc -l)
6070         [ $nums -eq $expected ] ||
6071                 error "'$cmd' wrong: found $nums, expected $expected"
6072         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6073         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6074         $LCTL get_param osc.*.stats
6075         [ $rpcs_after -eq $((rpcs_before + 12)) ] ||
6076                 error "'$cmd' should send 12 glimpse RPCs to OST"
6077
6078         cancel_lru_locks $OSC
6079         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6080         expected=0
6081         cmd="$LFS find ! -size 0 -type f -lazy $dir"
6082         nums=$($cmd | wc -l)
6083         [ $nums -eq $expected ] ||
6084                 error "'$cmd' wrong: found $nums, expected $expected"
6085         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6086         $LCTL get_param mdc.*.stats
6087         [ $rpcs_before -eq $rpcs_after ] ||
6088                 error "'$cmd' should not send glimpse RPCs to OST"
6089         cmd="$LFS find ! -size 0 -type f $dir"
6090         nums=$($cmd | wc -l)
6091         [ $nums -eq $expected ] ||
6092                 error "'$cmd' wrong: found $nums, expected $expected"
6093         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6094         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6095         [ $rpcs_after -eq $((rpcs_before + 12)) ] ||
6096                 error "'$cmd' should send 12 glimpse RPCs to OST"
6097
6098         echo "test" > $dir/$tfile
6099         echo "test2" > $dir/$tfile.2 && sync
6100         cancel_lru_locks $OSC
6101         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6102         expected=1
6103         cmd="$LFS find -size 5 -type f -lazy $dir"
6104         nums=$($cmd | wc -l)
6105         [ $nums -eq $expected ] ||
6106                 error "'$cmd' wrong: found $nums, expected $expected"
6107         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6108         [ $rpcs_before -eq $rpcs_after ] ||
6109                 error "'$cmd' should not send glimpse RPCs to OST"
6110         cmd="$LFS find -size 5 -type f $dir"
6111         nums=$($cmd | wc -l)
6112         [ $nums -eq $expected ] ||
6113                 error "'$cmd' wrong: found $nums, expected $expected"
6114         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6115         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6116         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6117                 error "'$cmd' should send 14 glimpse RPCs to OST"
6118
6119         cancel_lru_locks $OSC
6120         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6121         expected=1
6122         cmd="$LFS find -size +5 -type f -lazy $dir"
6123         nums=$($cmd | wc -l)
6124         [ $nums -eq $expected ] ||
6125                 error "'$cmd' wrong: found $nums, expected $expected"
6126         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6127         [ $rpcs_before -eq $rpcs_after ] ||
6128                 error "'$cmd' should not send glimpse RPCs to OST"
6129         cmd="$LFS find -size +5 -type f $dir"
6130         nums=$($cmd | wc -l)
6131         [ $nums -eq $expected ] ||
6132                 error "'$cmd' wrong: found $nums, expected $expected"
6133         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6134         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6135         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6136                 error "'$cmd' should send 14 glimpse RPCs to OST"
6137
6138         cancel_lru_locks $OSC
6139         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6140         expected=2
6141         cmd="$LFS find -size +0 -type f -lazy $dir"
6142         nums=$($cmd | wc -l)
6143         [ $nums -eq $expected ] ||
6144                 error "'$cmd' wrong: found $nums, expected $expected"
6145         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6146         [ $rpcs_before -eq $rpcs_after ] ||
6147                 error "'$cmd' should not send glimpse RPCs to OST"
6148         cmd="$LFS find -size +0 -type f $dir"
6149         nums=$($cmd | wc -l)
6150         [ $nums -eq $expected ] ||
6151                 error "'$cmd' wrong: found $nums, expected $expected"
6152         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6153         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6154         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6155                 error "'$cmd' should send 14 glimpse RPCs to OST"
6156
6157         cancel_lru_locks $OSC
6158         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6159         expected=2
6160         cmd="$LFS find ! -size -5 -type f -lazy $dir"
6161         nums=$($cmd | wc -l)
6162         [ $nums -eq $expected ] ||
6163                 error "'$cmd' wrong: found $nums, expected $expected"
6164         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6165         [ $rpcs_before -eq $rpcs_after ] ||
6166                 error "'$cmd' should not send glimpse RPCs to OST"
6167         cmd="$LFS find ! -size -5 -type f $dir"
6168         nums=$($cmd | wc -l)
6169         [ $nums -eq $expected ] ||
6170                 error "'$cmd' wrong: found $nums, expected $expected"
6171         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6172         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6173         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6174                 error "'$cmd' should send 14 glimpse RPCs to OST"
6175
6176         cancel_lru_locks $OSC
6177         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6178         expected=12
6179         cmd="$LFS find -size -5 -type f -lazy $dir"
6180         nums=$($cmd | wc -l)
6181         [ $nums -eq $expected ] ||
6182                 error "'$cmd' wrong: found $nums, expected $expected"
6183         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6184         [ $rpcs_before -eq $rpcs_after ] ||
6185                 error "'$cmd' should not send glimpse RPCs to OST"
6186         cmd="$LFS find -size -5 -type f $dir"
6187         nums=$($cmd | wc -l)
6188         [ $nums -eq $expected ] ||
6189                 error "'$cmd' wrong: found $nums, expected $expected"
6190         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6191         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6192         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6193                 error "'$cmd' should send 14 glimpse RPCs to OST"
6194 }
6195 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
6196
6197 test_56s() { # LU-611 #LU-9369
6198         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
6199
6200         local dir=$DIR/$tdir
6201         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
6202
6203         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6204         for i in $(seq $NUMDIRS); do
6205                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
6206         done
6207
6208         local expected=$NUMDIRS
6209         local cmd="$LFS find -c $OSTCOUNT $dir"
6210         local nums=$($cmd | wc -l)
6211
6212         [ $nums -eq $expected ] || {
6213                 $LFS getstripe -R $dir
6214                 error "'$cmd' wrong: found $nums, expected $expected"
6215         }
6216
6217         expected=$((NUMDIRS + onestripe))
6218         cmd="$LFS find -stripe-count +0 -type f $dir"
6219         nums=$($cmd | wc -l)
6220         [ $nums -eq $expected ] || {
6221                 $LFS getstripe -R $dir
6222                 error "'$cmd' wrong: found $nums, expected $expected"
6223         }
6224
6225         expected=$onestripe
6226         cmd="$LFS find -stripe-count 1 -type f $dir"
6227         nums=$($cmd | wc -l)
6228         [ $nums -eq $expected ] || {
6229                 $LFS getstripe -R $dir
6230                 error "'$cmd' wrong: found $nums, expected $expected"
6231         }
6232
6233         cmd="$LFS find -stripe-count -2 -type f $dir"
6234         nums=$($cmd | wc -l)
6235         [ $nums -eq $expected ] || {
6236                 $LFS getstripe -R $dir
6237                 error "'$cmd' wrong: found $nums, expected $expected"
6238         }
6239
6240         expected=0
6241         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
6242         nums=$($cmd | wc -l)
6243         [ $nums -eq $expected ] || {
6244                 $LFS getstripe -R $dir
6245                 error "'$cmd' wrong: found $nums, expected $expected"
6246         }
6247 }
6248 run_test 56s "check lfs find -stripe-count works"
6249
6250 test_56t() { # LU-611 #LU-9369
6251         local dir=$DIR/$tdir
6252
6253         setup_56 $dir 0 $NUMDIRS
6254         for i in $(seq $NUMDIRS); do
6255                 $LFS setstripe -S 8M $dir/dir$i/$tfile
6256         done
6257
6258         local expected=$NUMDIRS
6259         local cmd="$LFS find -S 8M $dir"
6260         local nums=$($cmd | wc -l)
6261
6262         [ $nums -eq $expected ] || {
6263                 $LFS getstripe -R $dir
6264                 error "'$cmd' wrong: found $nums, expected $expected"
6265         }
6266         rm -rf $dir
6267
6268         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
6269
6270         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
6271
6272         expected=$(((NUMDIRS + 1) * NUMFILES))
6273         cmd="$LFS find -stripe-size 512k -type f $dir"
6274         nums=$($cmd | wc -l)
6275         [ $nums -eq $expected ] ||
6276                 error "'$cmd' wrong: found $nums, expected $expected"
6277
6278         cmd="$LFS find -stripe-size +320k -type f $dir"
6279         nums=$($cmd | wc -l)
6280         [ $nums -eq $expected ] ||
6281                 error "'$cmd' wrong: found $nums, expected $expected"
6282
6283         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
6284         cmd="$LFS find -stripe-size +200k -type f $dir"
6285         nums=$($cmd | wc -l)
6286         [ $nums -eq $expected ] ||
6287                 error "'$cmd' wrong: found $nums, expected $expected"
6288
6289         cmd="$LFS find -stripe-size -640k -type f $dir"
6290         nums=$($cmd | wc -l)
6291         [ $nums -eq $expected ] ||
6292                 error "'$cmd' wrong: found $nums, expected $expected"
6293
6294         expected=4
6295         cmd="$LFS find -stripe-size 256k -type f $dir"
6296         nums=$($cmd | wc -l)
6297         [ $nums -eq $expected ] ||
6298                 error "'$cmd' wrong: found $nums, expected $expected"
6299
6300         cmd="$LFS find -stripe-size -320k -type f $dir"
6301         nums=$($cmd | wc -l)
6302         [ $nums -eq $expected ] ||
6303                 error "'$cmd' wrong: found $nums, expected $expected"
6304
6305         expected=0
6306         cmd="$LFS find -stripe-size 1024k -type f $dir"
6307         nums=$($cmd | wc -l)
6308         [ $nums -eq $expected ] ||
6309                 error "'$cmd' wrong: found $nums, expected $expected"
6310 }
6311 run_test 56t "check lfs find -stripe-size works"
6312
6313 test_56u() { # LU-611
6314         local dir=$DIR/$tdir
6315
6316         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
6317
6318         if [[ $OSTCOUNT -gt 1 ]]; then
6319                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
6320                 onestripe=4
6321         else
6322                 onestripe=0
6323         fi
6324
6325         local expected=$(((NUMDIRS + 1) * NUMFILES))
6326         local cmd="$LFS find -stripe-index 0 -type f $dir"
6327         local nums=$($cmd | wc -l)
6328
6329         [ $nums -eq $expected ] ||
6330                 error "'$cmd' wrong: found $nums, expected $expected"
6331
6332         expected=$onestripe
6333         cmd="$LFS find -stripe-index 1 -type f $dir"
6334         nums=$($cmd | wc -l)
6335         [ $nums -eq $expected ] ||
6336                 error "'$cmd' wrong: found $nums, expected $expected"
6337
6338         cmd="$LFS find ! -stripe-index 0 -type f $dir"
6339         nums=$($cmd | wc -l)
6340         [ $nums -eq $expected ] ||
6341                 error "'$cmd' wrong: found $nums, expected $expected"
6342
6343         expected=0
6344         # This should produce an error and not return any files
6345         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
6346         nums=$($cmd 2>/dev/null | wc -l)
6347         [ $nums -eq $expected ] ||
6348                 error "'$cmd' wrong: found $nums, expected $expected"
6349
6350         if [[ $OSTCOUNT -gt 1 ]]; then
6351                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
6352                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
6353                 nums=$($cmd | wc -l)
6354                 [ $nums -eq $expected ] ||
6355                         error "'$cmd' wrong: found $nums, expected $expected"
6356         fi
6357 }
6358 run_test 56u "check lfs find -stripe-index works"
6359
6360 test_56v() {
6361         local mdt_idx=0
6362         local dir=$DIR/$tdir
6363
6364         setup_56 $dir $NUMFILES $NUMDIRS
6365
6366         UUID=$(mdtuuid_from_index $mdt_idx $dir)
6367         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
6368
6369         for file in $($LFS find -m $UUID $dir); do
6370                 file_midx=$($LFS getstripe -m $file)
6371                 [ $file_midx -eq $mdt_idx ] ||
6372                         error "lfs find -m $UUID != getstripe -m $file_midx"
6373         done
6374 }
6375 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
6376
6377 test_56w() {
6378         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6379         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6380
6381         local dir=$DIR/$tdir
6382
6383         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
6384
6385         local stripe_size=$($LFS getstripe -S -d $dir) ||
6386                 error "$LFS getstripe -S -d $dir failed"
6387         stripe_size=${stripe_size%% *}
6388
6389         local file_size=$((stripe_size * OSTCOUNT))
6390         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
6391         local required_space=$((file_num * file_size))
6392         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
6393                            head -n1)
6394         [[ $free_space -le $((required_space / 1024)) ]] &&
6395                 skip_env "need $required_space, have $free_space kbytes"
6396
6397         local dd_bs=65536
6398         local dd_count=$((file_size / dd_bs))
6399
6400         # write data into the files
6401         local i
6402         local j
6403         local file
6404
6405         for i in $(seq $NUMFILES); do
6406                 file=$dir/file$i
6407                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6408                         error "write data into $file failed"
6409         done
6410         for i in $(seq $NUMDIRS); do
6411                 for j in $(seq $NUMFILES); do
6412                         file=$dir/dir$i/file$j
6413                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6414                                 error "write data into $file failed"
6415                 done
6416         done
6417
6418         # $LFS_MIGRATE will fail if hard link migration is unsupported
6419         if [[ $MDS1_VERSION -gt $(version_code 2.5.55) ]]; then
6420                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
6421                         error "creating links to $dir/dir1/file1 failed"
6422         fi
6423
6424         local expected=-1
6425
6426         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
6427
6428         # lfs_migrate file
6429         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
6430
6431         echo "$cmd"
6432         eval $cmd || error "$cmd failed"
6433
6434         check_stripe_count $dir/file1 $expected
6435
6436         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
6437         then
6438                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
6439                 # OST 1 if it is on OST 0. This file is small enough to
6440                 # be on only one stripe.
6441                 file=$dir/migr_1_ost
6442                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
6443                         error "write data into $file failed"
6444                 local obdidx=$($LFS getstripe -i $file)
6445                 local oldmd5=$(md5sum $file)
6446                 local newobdidx=0
6447
6448                 [[ $obdidx -eq 0 ]] && newobdidx=1
6449                 cmd="$LFS migrate -i $newobdidx $file"
6450                 echo $cmd
6451                 eval $cmd || error "$cmd failed"
6452
6453                 local realobdix=$($LFS getstripe -i $file)
6454                 local newmd5=$(md5sum $file)
6455
6456                 [[ $newobdidx -ne $realobdix ]] &&
6457                         error "new OST is different (was=$obdidx, "\
6458                               "wanted=$newobdidx, got=$realobdix)"
6459                 [[ "$oldmd5" != "$newmd5" ]] &&
6460                         error "md5sum differ: $oldmd5, $newmd5"
6461         fi
6462
6463         # lfs_migrate dir
6464         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
6465         echo "$cmd"
6466         eval $cmd || error "$cmd failed"
6467
6468         for j in $(seq $NUMFILES); do
6469                 check_stripe_count $dir/dir1/file$j $expected
6470         done
6471
6472         # lfs_migrate works with lfs find
6473         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
6474              $LFS_MIGRATE -y -c $expected"
6475         echo "$cmd"
6476         eval $cmd || error "$cmd failed"
6477
6478         for i in $(seq 2 $NUMFILES); do
6479                 check_stripe_count $dir/file$i $expected
6480         done
6481         for i in $(seq 2 $NUMDIRS); do
6482                 for j in $(seq $NUMFILES); do
6483                 check_stripe_count $dir/dir$i/file$j $expected
6484                 done
6485         done
6486 }
6487 run_test 56w "check lfs_migrate -c stripe_count works"
6488
6489 test_56wb() {
6490         local file1=$DIR/$tdir/file1
6491         local create_pool=false
6492         local initial_pool=$($LFS getstripe -p $DIR)
6493         local pool_list=()
6494         local pool=""
6495
6496         echo -n "Creating test dir..."
6497         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6498         echo "done."
6499
6500         echo -n "Creating test file..."
6501         touch $file1 || error "cannot create file"
6502         echo "done."
6503
6504         echo -n "Detecting existing pools..."
6505         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
6506
6507         if [ ${#pool_list[@]} -gt 0 ]; then
6508                 echo "${pool_list[@]}"
6509                 for thispool in "${pool_list[@]}"; do
6510                         if [[ -z "$initial_pool" ||
6511                               "$initial_pool" != "$thispool" ]]; then
6512                                 pool="$thispool"
6513                                 echo "Using existing pool '$pool'"
6514                                 break
6515                         fi
6516                 done
6517         else
6518                 echo "none detected."
6519         fi
6520         if [ -z "$pool" ]; then
6521                 pool=${POOL:-testpool}
6522                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
6523                 echo -n "Creating pool '$pool'..."
6524                 create_pool=true
6525                 pool_add $pool &> /dev/null ||
6526                         error "pool_add failed"
6527                 echo "done."
6528
6529                 echo -n "Adding target to pool..."
6530                 pool_add_targets $pool 0 0 1 &> /dev/null ||
6531                         error "pool_add_targets failed"
6532                 echo "done."
6533         fi
6534
6535         echo -n "Setting pool using -p option..."
6536         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
6537                 error "migrate failed rc = $?"
6538         echo "done."
6539
6540         echo -n "Verifying test file is in pool after migrating..."
6541         [ "$($LFS getstripe -p $file1)" = $pool ] ||
6542                 error "file was not migrated to pool $pool"
6543         echo "done."
6544
6545         echo -n "Removing test file from pool '$pool'..."
6546         # "lfs migrate $file" won't remove the file from the pool
6547         # until some striping information is changed.
6548         $LFS migrate -c 1 $file1 &> /dev/null ||
6549                 error "cannot remove from pool"
6550         [ "$($LFS getstripe -p $file1)" ] &&
6551                 error "pool still set"
6552         echo "done."
6553
6554         echo -n "Setting pool using --pool option..."
6555         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
6556                 error "migrate failed rc = $?"
6557         echo "done."
6558
6559         # Clean up
6560         rm -f $file1
6561         if $create_pool; then
6562                 destroy_test_pools 2> /dev/null ||
6563                         error "destroy test pools failed"
6564         fi
6565 }
6566 run_test 56wb "check lfs_migrate pool support"
6567
6568 test_56wc() {
6569         local file1="$DIR/$tdir/file1"
6570         local parent_ssize
6571         local parent_scount
6572         local cur_ssize
6573         local cur_scount
6574         local orig_ssize
6575
6576         echo -n "Creating test dir..."
6577         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6578         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
6579                 error "cannot set stripe by '-S 1M -c 1'"
6580         echo "done"
6581
6582         echo -n "Setting initial stripe for test file..."
6583         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
6584                 error "cannot set stripe"
6585         cur_ssize=$($LFS getstripe -S "$file1")
6586         [ $cur_ssize -eq 524288 ] || error "setstripe -S $cur_ssize != 524288"
6587         echo "done."
6588
6589         # File currently set to -S 512K -c 1
6590
6591         # Ensure -c and -S options are rejected when -R is set
6592         echo -n "Verifying incompatible options are detected..."
6593         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
6594                 error "incompatible -c and -R options not detected"
6595         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
6596                 error "incompatible -S and -R options not detected"
6597         echo "done."
6598
6599         # Ensure unrecognized options are passed through to 'lfs migrate'
6600         echo -n "Verifying -S option is passed through to lfs migrate..."
6601         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
6602                 error "migration failed"
6603         cur_ssize=$($LFS getstripe -S "$file1")
6604         [ $cur_ssize -eq 1048576 ] || error "migrate -S $cur_ssize != 1048576"
6605         echo "done."
6606
6607         # File currently set to -S 1M -c 1
6608
6609         # Ensure long options are supported
6610         echo -n "Verifying long options supported..."
6611         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
6612                 error "long option without argument not supported"
6613         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
6614                 error "long option with argument not supported"
6615         cur_ssize=$($LFS getstripe -S "$file1")
6616         [ $cur_ssize -eq 524288 ] ||
6617                 error "migrate --stripe-size $cur_ssize != 524288"
6618         echo "done."
6619
6620         # File currently set to -S 512K -c 1
6621
6622         if [ "$OSTCOUNT" -gt 1 ]; then
6623                 echo -n "Verifying explicit stripe count can be set..."
6624                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
6625                         error "migrate failed"
6626                 cur_scount=$($LFS getstripe -c "$file1")
6627                 [ $cur_scount -eq 2 ] || error "migrate -c $cur_scount != 2"
6628                 echo "done."
6629         fi
6630
6631         # File currently set to -S 512K -c 1 or -S 512K -c 2
6632
6633         # Ensure parent striping is used if -R is set, and no stripe
6634         # count or size is specified
6635         echo -n "Setting stripe for parent directory..."
6636         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
6637                 error "cannot set stripe '-S 2M -c 1'"
6638         echo "done."
6639
6640         echo -n "Verifying restripe option uses parent stripe settings..."
6641         parent_ssize=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
6642         parent_scount=$($LFS getstripe -c $DIR/$tdir 2>/dev/null)
6643         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
6644                 error "migrate failed"
6645         cur_ssize=$($LFS getstripe -S "$file1")
6646         [ $cur_ssize -eq $parent_ssize ] ||
6647                 error "migrate -R stripe_size $cur_ssize != $parent_ssize"
6648         cur_scount=$($LFS getstripe -c "$file1")
6649         [ $cur_scount -eq $parent_scount ] ||
6650                 error "migrate -R stripe_count $cur_scount != $parent_scount"
6651         echo "done."
6652
6653         # File currently set to -S 1M -c 1
6654
6655         # Ensure striping is preserved if -R is not set, and no stripe
6656         # count or size is specified
6657         echo -n "Verifying striping size preserved when not specified..."
6658         orig_ssize=$($LFS getstripe -S "$file1" 2>/dev/null)
6659         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
6660                 error "cannot set stripe on parent directory"
6661         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6662                 error "migrate failed"
6663         cur_ssize=$($LFS getstripe -S "$file1")
6664         [ $cur_ssize -eq $orig_ssize ] ||
6665                 error "migrate by default $cur_ssize != $orig_ssize"
6666         echo "done."
6667
6668         # Ensure file name properly detected when final option has no argument
6669         echo -n "Verifying file name properly detected..."
6670         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6671                 error "file name interpreted as option argument"
6672         echo "done."
6673
6674         # Clean up
6675         rm -f "$file1"
6676 }
6677 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
6678
6679 test_56wd() {
6680         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6681
6682         local file1=$DIR/$tdir/file1
6683
6684         echo -n "Creating test dir..."
6685         test_mkdir $DIR/$tdir || error "cannot create dir"
6686         echo "done."
6687
6688         echo -n "Creating test file..."
6689         touch $file1
6690         echo "done."
6691
6692         # Ensure 'lfs migrate' will fail by using a non-existent option,
6693         # and make sure rsync is not called to recover
6694         echo -n "Make sure --no-rsync option works..."
6695         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
6696                 grep -q 'refusing to fall back to rsync' ||
6697                 error "rsync was called with --no-rsync set"
6698         echo "done."
6699
6700         # Ensure rsync is called without trying 'lfs migrate' first
6701         echo -n "Make sure --rsync option works..."
6702         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
6703                 grep -q 'falling back to rsync' &&
6704                 error "lfs migrate was called with --rsync set"
6705         echo "done."
6706
6707         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
6708         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
6709                 grep -q 'at the same time' ||
6710                 error "--rsync and --no-rsync accepted concurrently"
6711         echo "done."
6712
6713         # Clean up
6714         rm -f $file1
6715 }
6716 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
6717
6718 test_56x() {
6719         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6720         check_swap_layouts_support
6721
6722         local dir=$DIR/$tdir
6723         local ref1=/etc/passwd
6724         local file1=$dir/file1
6725
6726         test_mkdir $dir || error "creating dir $dir"
6727         $LFS setstripe -c 2 $file1
6728         cp $ref1 $file1
6729         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
6730         stripe=$($LFS getstripe -c $file1)
6731         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6732         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6733
6734         # clean up
6735         rm -f $file1
6736 }
6737 run_test 56x "lfs migration support"
6738
6739 test_56xa() {
6740         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6741         check_swap_layouts_support
6742
6743         local dir=$DIR/$tdir/$testnum
6744
6745         test_mkdir -p $dir
6746
6747         local ref1=/etc/passwd
6748         local file1=$dir/file1
6749
6750         $LFS setstripe -c 2 $file1
6751         cp $ref1 $file1
6752         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
6753
6754         local stripe=$($LFS getstripe -c $file1)
6755
6756         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6757         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6758
6759         # clean up
6760         rm -f $file1
6761 }
6762 run_test 56xa "lfs migration --block support"
6763
6764 check_migrate_links() {
6765         local dir="$1"
6766         local file1="$dir/file1"
6767         local begin="$2"
6768         local count="$3"
6769         local runas="$4"
6770         local total_count=$(($begin + $count - 1))
6771         local symlink_count=10
6772         local uniq_count=10
6773
6774         if [ ! -f "$file1" ]; then
6775                 echo -n "creating initial file..."
6776                 $LFS setstripe -c 1 -S "512k" "$file1" ||
6777                         error "cannot setstripe initial file"
6778                 echo "done"
6779
6780                 echo -n "creating symlinks..."
6781                 for s in $(seq 1 $symlink_count); do
6782                         ln -s "$file1" "$dir/slink$s" ||
6783                                 error "cannot create symlinks"
6784                 done
6785                 echo "done"
6786
6787                 echo -n "creating nonlinked files..."
6788                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
6789                         error "cannot create nonlinked files"
6790                 echo "done"
6791         fi
6792
6793         # create hard links
6794         if [ ! -f "$dir/file$total_count" ]; then
6795                 echo -n "creating hard links $begin:$total_count..."
6796                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
6797                         /dev/null || error "cannot create hard links"
6798                 echo "done"
6799         fi
6800
6801         echo -n "checking number of hard links listed in xattrs..."
6802         local fid=$($LFS getstripe -F "$file1")
6803         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
6804
6805         echo "${#paths[*]}"
6806         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
6807                         skip "hard link list has unexpected size, skipping test"
6808         fi
6809         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
6810                         error "link names should exceed xattrs size"
6811         fi
6812
6813         echo -n "migrating files..."
6814         local migrate_out=$($runas $LFS_MIGRATE -y -S '1m' $dir)
6815         local rc=$?
6816         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
6817         echo "done"
6818
6819         # make sure all links have been properly migrated
6820         echo -n "verifying files..."
6821         fid=$($LFS getstripe -F "$file1") ||
6822                 error "cannot get fid for file $file1"
6823         for i in $(seq 2 $total_count); do
6824                 local fid2=$($LFS getstripe -F $dir/file$i)
6825
6826                 [ "$fid2" == "$fid" ] ||
6827                         error "migrated hard link has mismatched FID"
6828         done
6829
6830         # make sure hard links were properly detected, and migration was
6831         # performed only once for the entire link set; nonlinked files should
6832         # also be migrated
6833         local actual=$(grep -c 'done' <<< "$migrate_out")
6834         local expected=$(($uniq_count + 1))
6835
6836         [ "$actual" -eq  "$expected" ] ||
6837                 error "hard links individually migrated ($actual != $expected)"
6838
6839         # make sure the correct number of hard links are present
6840         local hardlinks=$(stat -c '%h' "$file1")
6841
6842         [ $hardlinks -eq $total_count ] ||
6843                 error "num hard links $hardlinks != $total_count"
6844         echo "done"
6845
6846         return 0
6847 }
6848
6849 test_56xb() {
6850         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
6851                 skip "Need MDS version at least 2.10.55"
6852
6853         local dir="$DIR/$tdir"
6854
6855         test_mkdir "$dir" || error "cannot create dir $dir"
6856
6857         echo "testing lfs migrate mode when all links fit within xattrs"
6858         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
6859
6860         echo "testing rsync mode when all links fit within xattrs"
6861         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
6862
6863         echo "testing lfs migrate mode when all links do not fit within xattrs"
6864         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
6865
6866         echo "testing rsync mode when all links do not fit within xattrs"
6867         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
6868
6869         chown -R $RUNAS_ID $dir
6870         echo "testing non-root lfs migrate mode when not all links are in xattr"
6871         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100 "$RUNAS"
6872
6873         # clean up
6874         rm -rf $dir
6875 }
6876 run_test 56xb "lfs migration hard link support"
6877
6878 test_56xc() {
6879         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6880
6881         local dir="$DIR/$tdir"
6882
6883         test_mkdir "$dir" || error "cannot create dir $dir"
6884
6885         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
6886         echo -n "Setting initial stripe for 20MB test file..."
6887         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
6888                 error "cannot setstripe 20MB file"
6889         echo "done"
6890         echo -n "Sizing 20MB test file..."
6891         truncate "$dir/20mb" 20971520 || error "cannot create 20MB test file"
6892         echo "done"
6893         echo -n "Verifying small file autostripe count is 1..."
6894         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
6895                 error "cannot migrate 20MB file"
6896         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
6897                 error "cannot get stripe for $dir/20mb"
6898         [ $stripe_count -eq 1 ] ||
6899                 error "unexpected stripe count $stripe_count for 20MB file"
6900         rm -f "$dir/20mb"
6901         echo "done"
6902
6903         # Test 2: File is small enough to fit within the available space on
6904         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
6905         # have at least an additional 1KB for each desired stripe for test 3
6906         echo -n "Setting stripe for 1GB test file..."
6907         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
6908         echo "done"
6909         echo -n "Sizing 1GB test file..."
6910         # File size is 1GB + 3KB
6911         truncate "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
6912         echo "done"
6913
6914         # need at least 512MB per OST for 1GB file to fit in 2 stripes
6915         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
6916         if (( avail > 524288 * OSTCOUNT )); then
6917                 echo -n "Migrating 1GB file..."
6918                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
6919                         error "cannot migrate 1GB file"
6920                 echo "done"
6921                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
6922                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
6923                         error "cannot getstripe for 1GB file"
6924                 [ $stripe_count -eq 2 ] ||
6925                         error "unexpected stripe count $stripe_count != 2"
6926                 echo "done"
6927         fi
6928
6929         # Test 3: File is too large to fit within the available space on
6930         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
6931         if [ $OSTCOUNT -ge 3 ]; then
6932                 # The required available space is calculated as
6933                 # file size (1GB + 3KB) / OST count (3).
6934                 local kb_per_ost=349526
6935
6936                 echo -n "Migrating 1GB file with limit..."
6937                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
6938                         error "cannot migrate 1GB file with limit"
6939                 echo "done"
6940
6941                 stripe_count=$($LFS getstripe -c "$dir/1gb")
6942                 echo -n "Verifying 1GB autostripe count with limited space..."
6943                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
6944                         error "unexpected stripe count $stripe_count (min 3)"
6945                 echo "done"
6946         fi
6947
6948         # clean up
6949         rm -rf $dir
6950 }
6951 run_test 56xc "lfs migration autostripe"
6952
6953 test_56xd() {
6954         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6955
6956         local dir=$DIR/$tdir
6957         local f_mgrt=$dir/$tfile.mgrt
6958         local f_yaml=$dir/$tfile.yaml
6959         local f_copy=$dir/$tfile.copy
6960         local layout_yaml="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
6961         local layout_copy="-c 2 -S 2M -i 1"
6962         local yamlfile=$dir/yamlfile
6963         local layout_before;
6964         local layout_after;
6965
6966         test_mkdir "$dir" || error "cannot create dir $dir"
6967         $LFS setstripe $layout_yaml $f_yaml ||
6968                 error "cannot setstripe $f_yaml with layout $layout_yaml"
6969         $LFS getstripe --yaml $f_yaml > $yamlfile
6970         $LFS setstripe $layout_copy $f_copy ||
6971                 error "cannot setstripe $f_copy with layout $layout_copy"
6972         touch $f_mgrt
6973         dd if=/dev/zero of=$f_mgrt bs=1M count=4
6974
6975         # 1. test option --yaml
6976         $LFS_MIGRATE -y --yaml $yamlfile $f_mgrt ||
6977                 error "cannot migrate $f_mgrt with --yaml $yamlfile"
6978         layout_before=$(get_layout_param $f_yaml)
6979         layout_after=$(get_layout_param $f_mgrt)
6980         [ "$layout_after" == "$layout_before" ] ||
6981                 error "lfs_migrate --yaml: $layout_after != $layout_before"
6982
6983         # 2. test option --copy
6984         $LFS_MIGRATE -y --copy $f_copy $f_mgrt ||
6985                 error "cannot migrate $f_mgrt with --copy $f_copy"
6986         layout_before=$(get_layout_param $f_copy)
6987         layout_after=$(get_layout_param $f_mgrt)
6988         [ "$layout_after" == "$layout_before" ] ||
6989                 error "lfs_migrate --copy: $layout_after != $layout_before"
6990 }
6991 run_test 56xd "check lfs_migrate --yaml and --copy support"
6992
6993 test_56xe() {
6994         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6995
6996         local dir=$DIR/$tdir
6997         local f_comp=$dir/$tfile
6998         local layout="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
6999         local layout_before=""
7000         local layout_after=""
7001
7002         test_mkdir "$dir" || error "cannot create dir $dir"
7003         $LFS setstripe $layout $f_comp ||
7004                 error "cannot setstripe $f_comp with layout $layout"
7005         layout_before=$(get_layout_param $f_comp)
7006         dd if=/dev/zero of=$f_comp bs=1M count=4
7007
7008         # 1. migrate a comp layout file by lfs_migrate
7009         $LFS_MIGRATE -y $f_comp || error "cannot migrate $f_comp by lfs_migrate"
7010         layout_after=$(get_layout_param $f_comp)
7011         [ "$layout_before" == "$layout_after" ] ||
7012                 error "lfs_migrate: $layout_before != $layout_after"
7013
7014         # 2. migrate a comp layout file by lfs migrate
7015         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7016         layout_after=$(get_layout_param $f_comp)
7017         [ "$layout_before" == "$layout_after" ] ||
7018                 error "lfs migrate: $layout_before != $layout_after"
7019 }
7020 run_test 56xe "migrate a composite layout file"
7021
7022 test_56y() {
7023         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
7024                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
7025
7026         local res=""
7027         local dir=$DIR/$tdir
7028         local f1=$dir/file1
7029         local f2=$dir/file2
7030
7031         test_mkdir -p $dir || error "creating dir $dir"
7032         touch $f1 || error "creating std file $f1"
7033         $MULTIOP $f2 H2c || error "creating released file $f2"
7034
7035         # a directory can be raid0, so ask only for files
7036         res=$($LFS find $dir -L raid0 -type f | wc -l)
7037         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
7038
7039         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
7040         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
7041
7042         # only files can be released, so no need to force file search
7043         res=$($LFS find $dir -L released)
7044         [[ $res == $f2 ]] || error "search released: found $res != $f2"
7045
7046         res=$($LFS find $dir -type f \! -L released)
7047         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
7048 }
7049 run_test 56y "lfs find -L raid0|released"
7050
7051 test_56z() { # LU-4824
7052         # This checks to make sure 'lfs find' continues after errors
7053         # There are two classes of errors that should be caught:
7054         # - If multiple paths are provided, all should be searched even if one
7055         #   errors out
7056         # - If errors are encountered during the search, it should not terminate
7057         #   early
7058         local dir=$DIR/$tdir
7059         local i
7060
7061         test_mkdir $dir
7062         for i in d{0..9}; do
7063                 test_mkdir $dir/$i
7064                 touch $dir/$i/$tfile
7065         done
7066         $LFS find $DIR/non_existent_dir $dir &&
7067                 error "$LFS find did not return an error"
7068         # Make a directory unsearchable. This should NOT be the last entry in
7069         # directory order.  Arbitrarily pick the 6th entry
7070         chmod 700 $($LFS find $dir -type d | sed '6!d')
7071
7072         $RUNAS $LFS find $DIR/non_existent $dir
7073         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
7074
7075         # The user should be able to see 10 directories and 9 files
7076         (( count == 19 )) ||
7077                 error "$LFS find found $count != 19 entries after error"
7078 }
7079 run_test 56z "lfs find should continue after an error"
7080
7081 test_56aa() { # LU-5937
7082         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
7083
7084         local dir=$DIR/$tdir
7085
7086         mkdir $dir
7087         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
7088
7089         createmany -o $dir/striped_dir/${tfile}- 1024
7090         local dirs=$($LFS find --size +8k $dir/)
7091
7092         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
7093 }
7094 run_test 56aa "lfs find --size under striped dir"
7095
7096 test_56ab() { # LU-10705
7097         test_mkdir $DIR/$tdir
7098         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
7099         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
7100         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
7101         # Flush writes to ensure valid blocks.  Need to be more thorough for
7102         # ZFS, since blocks are not allocated/returned to client immediately.
7103         sync_all_data
7104         wait_zfs_commit ost1 2
7105         cancel_lru_locks osc
7106         ls -ls $DIR/$tdir
7107
7108         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
7109
7110         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
7111
7112         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
7113         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
7114
7115         rm -f $DIR/$tdir/$tfile.[123]
7116 }
7117 run_test 56ab "lfs find --blocks"
7118
7119 test_56ba() {
7120         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
7121                 skip "Need MDS version at least 2.10.50"
7122
7123         # Create composite files with one component
7124         local dir=$DIR/$tdir
7125
7126         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
7127         # Create composite files with three components
7128         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
7129         # Create non-composite files
7130         createmany -o $dir/${tfile}- 10
7131
7132         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
7133
7134         [[ $nfiles == 10 ]] ||
7135                 error "lfs find -E 1M found $nfiles != 10 files"
7136
7137         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
7138         [[ $nfiles == 25 ]] ||
7139                 error "lfs find ! -E 1M found $nfiles != 25 files"
7140
7141         # All files have a component that starts at 0
7142         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
7143         [[ $nfiles == 35 ]] ||
7144                 error "lfs find --component-start 0 - $nfiles != 35 files"
7145
7146         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
7147         [[ $nfiles == 15 ]] ||
7148                 error "lfs find --component-start 2M - $nfiles != 15 files"
7149
7150         # All files created here have a componenet that does not starts at 2M
7151         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
7152         [[ $nfiles == 35 ]] ||
7153                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
7154
7155         # Find files with a specified number of components
7156         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
7157         [[ $nfiles == 15 ]] ||
7158                 error "lfs find --component-count 3 - $nfiles != 15 files"
7159
7160         # Remember non-composite files have a component count of zero
7161         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
7162         [[ $nfiles == 10 ]] ||
7163                 error "lfs find --component-count 0 - $nfiles != 10 files"
7164
7165         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
7166         [[ $nfiles == 20 ]] ||
7167                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
7168
7169         # All files have a flag called "init"
7170         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
7171         [[ $nfiles == 35 ]] ||
7172                 error "lfs find --component-flags init - $nfiles != 35 files"
7173
7174         # Multi-component files will have a component not initialized
7175         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
7176         [[ $nfiles == 15 ]] ||
7177                 error "lfs find !--component-flags init - $nfiles != 15 files"
7178
7179         rm -rf $dir
7180
7181 }
7182 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
7183
7184 test_56ca() {
7185         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
7186                 skip "Need MDS version at least 2.10.57"
7187
7188         local td=$DIR/$tdir
7189         local tf=$td/$tfile
7190         local dir
7191         local nfiles
7192         local cmd
7193         local i
7194         local j
7195
7196         # create mirrored directories and mirrored files
7197         mkdir $td || error "mkdir $td failed"
7198         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
7199         createmany -o $tf- 10 || error "create $tf- failed"
7200
7201         for i in $(seq 2); do
7202                 dir=$td/dir$i
7203                 mkdir $dir || error "mkdir $dir failed"
7204                 $LFS mirror create -N$((3 + i)) $dir ||
7205                         error "create mirrored dir $dir failed"
7206                 createmany -o $dir/$tfile- 10 ||
7207                         error "create $dir/$tfile- failed"
7208         done
7209
7210         # change the states of some mirrored files
7211         echo foo > $tf-6
7212         for i in $(seq 2); do
7213                 dir=$td/dir$i
7214                 for j in $(seq 4 9); do
7215                         echo foo > $dir/$tfile-$j
7216                 done
7217         done
7218
7219         # find mirrored files with specific mirror count
7220         cmd="$LFS find --mirror-count 3 --type f $td"
7221         nfiles=$($cmd | wc -l)
7222         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
7223
7224         cmd="$LFS find ! --mirror-count 3 --type f $td"
7225         nfiles=$($cmd | wc -l)
7226         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
7227
7228         cmd="$LFS find --mirror-count +2 --type f $td"
7229         nfiles=$($cmd | wc -l)
7230         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7231
7232         cmd="$LFS find --mirror-count -6 --type f $td"
7233         nfiles=$($cmd | wc -l)
7234         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7235
7236         # find mirrored files with specific file state
7237         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
7238         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
7239
7240         cmd="$LFS find --mirror-state=ro --type f $td"
7241         nfiles=$($cmd | wc -l)
7242         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
7243
7244         cmd="$LFS find ! --mirror-state=ro --type f $td"
7245         nfiles=$($cmd | wc -l)
7246         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7247
7248         cmd="$LFS find --mirror-state=wp --type f $td"
7249         nfiles=$($cmd | wc -l)
7250         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7251
7252         cmd="$LFS find ! --mirror-state=sp --type f $td"
7253         nfiles=$($cmd | wc -l)
7254         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7255 }
7256 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
7257
7258 test_57a() {
7259         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7260         # note test will not do anything if MDS is not local
7261         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7262                 skip_env "ldiskfs only test"
7263         fi
7264         remote_mds_nodsh && skip "remote MDS with nodsh"
7265
7266         local MNTDEV="osd*.*MDT*.mntdev"
7267         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
7268         [ -z "$DEV" ] && error "can't access $MNTDEV"
7269         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
7270                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
7271                         error "can't access $DEV"
7272                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
7273                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
7274                 rm $TMP/t57a.dump
7275         done
7276 }
7277 run_test 57a "verify MDS filesystem created with large inodes =="
7278
7279 test_57b() {
7280         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7281         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7282                 skip_env "ldiskfs only test"
7283         fi
7284         remote_mds_nodsh && skip "remote MDS with nodsh"
7285
7286         local dir=$DIR/$tdir
7287         local filecount=100
7288         local file1=$dir/f1
7289         local fileN=$dir/f$filecount
7290
7291         rm -rf $dir || error "removing $dir"
7292         test_mkdir -c1 $dir
7293         local mdtidx=$($LFS getstripe -m $dir)
7294         local mdtname=MDT$(printf %04x $mdtidx)
7295         local facet=mds$((mdtidx + 1))
7296
7297         echo "mcreating $filecount files"
7298         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
7299
7300         # verify that files do not have EAs yet
7301         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
7302                 error "$file1 has an EA"
7303         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
7304                 error "$fileN has an EA"
7305
7306         sync
7307         sleep 1
7308         df $dir  #make sure we get new statfs data
7309         local mdsfree=$(do_facet $facet \
7310                         lctl get_param -n osd*.*$mdtname.kbytesfree)
7311         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7312         local file
7313
7314         echo "opening files to create objects/EAs"
7315         for file in $(seq -f $dir/f%g 1 $filecount); do
7316                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
7317                         error "opening $file"
7318         done
7319
7320         # verify that files have EAs now
7321         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
7322         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
7323
7324         sleep 1  #make sure we get new statfs data
7325         df $dir
7326         local mdsfree2=$(do_facet $facet \
7327                          lctl get_param -n osd*.*$mdtname.kbytesfree)
7328         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7329
7330         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
7331                 if [ "$mdsfree" != "$mdsfree2" ]; then
7332                         error "MDC before $mdcfree != after $mdcfree2"
7333                 else
7334                         echo "MDC before $mdcfree != after $mdcfree2"
7335                         echo "unable to confirm if MDS has large inodes"
7336                 fi
7337         fi
7338         rm -rf $dir
7339 }
7340 run_test 57b "default LOV EAs are stored inside large inodes ==="
7341
7342 test_58() {
7343         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7344         [ -z "$(which wiretest 2>/dev/null)" ] &&
7345                         skip_env "could not find wiretest"
7346
7347         wiretest
7348 }
7349 run_test 58 "verify cross-platform wire constants =============="
7350
7351 test_59() {
7352         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7353
7354         echo "touch 130 files"
7355         createmany -o $DIR/f59- 130
7356         echo "rm 130 files"
7357         unlinkmany $DIR/f59- 130
7358         sync
7359         # wait for commitment of removal
7360         wait_delete_completed
7361 }
7362 run_test 59 "verify cancellation of llog records async ========="
7363
7364 TEST60_HEAD="test_60 run $RANDOM"
7365 test_60a() {
7366         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7367         remote_mgs_nodsh && skip "remote MGS with nodsh"
7368         do_facet mgs "! which run-llog.sh &> /dev/null" &&
7369                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
7370                         skip_env "missing subtest run-llog.sh"
7371
7372         log "$TEST60_HEAD - from kernel mode"
7373         do_facet mgs "$LCTL dk > /dev/null"
7374         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
7375         do_facet mgs $LCTL dk > $TMP/$tfile
7376
7377         # LU-6388: test llog_reader
7378         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
7379         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
7380         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
7381                         skip_env "missing llog_reader"
7382         local fstype=$(facet_fstype mgs)
7383         [ $fstype != ldiskfs -a $fstype != zfs ] &&
7384                 skip_env "Only for ldiskfs or zfs type mgs"
7385
7386         local mntpt=$(facet_mntpt mgs)
7387         local mgsdev=$(mgsdevname 1)
7388         local fid_list
7389         local fid
7390         local rec_list
7391         local rec
7392         local rec_type
7393         local obj_file
7394         local path
7395         local seq
7396         local oid
7397         local pass=true
7398
7399         #get fid and record list
7400         fid_list=($(awk '/9_sub.*record/ { print $NF }' $TMP/$tfile |
7401                 tail -n 4))
7402         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' $TMP/$tfile |
7403                 tail -n 4))
7404         #remount mgs as ldiskfs or zfs type
7405         stop mgs || error "stop mgs failed"
7406         mount_fstype mgs || error "remount mgs failed"
7407         for ((i = 0; i < ${#fid_list[@]}; i++)); do
7408                 fid=${fid_list[i]}
7409                 rec=${rec_list[i]}
7410                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
7411                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
7412                 oid=$((16#$oid))
7413
7414                 case $fstype in
7415                         ldiskfs )
7416                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
7417                         zfs )
7418                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
7419                 esac
7420                 echo "obj_file is $obj_file"
7421                 do_facet mgs $llog_reader $obj_file
7422
7423                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
7424                         awk '{ print $3 }' | sed -e "s/^type=//g")
7425                 if [ $rec_type != $rec ]; then
7426                         echo "FAILED test_60a wrong record type $rec_type," \
7427                               "should be $rec"
7428                         pass=false
7429                         break
7430                 fi
7431
7432                 #check obj path if record type is LLOG_LOGID_MAGIC
7433                 if [ "$rec" == "1064553b" ]; then
7434                         path=$(do_facet mgs $llog_reader $obj_file |
7435                                 grep "path=" | awk '{ print $NF }' |
7436                                 sed -e "s/^path=//g")
7437                         if [ $obj_file != $mntpt/$path ]; then
7438                                 echo "FAILED test_60a wrong obj path" \
7439                                       "$montpt/$path, should be $obj_file"
7440                                 pass=false
7441                                 break
7442                         fi
7443                 fi
7444         done
7445         rm -f $TMP/$tfile
7446         #restart mgs before "error", otherwise it will block the next test
7447         stop mgs || error "stop mgs failed"
7448         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
7449         $pass || error "test failed, see FAILED test_60a messages for specifics"
7450 }
7451 run_test 60a "llog_test run from kernel module and test llog_reader"
7452
7453 test_60b() { # bug 6411
7454         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7455
7456         dmesg > $DIR/$tfile
7457         LLOG_COUNT=$(do_facet mgs dmesg |
7458                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
7459                           /llog_[a-z]*.c:[0-9]/ {
7460                                 if (marker)
7461                                         from_marker++
7462                                 from_begin++
7463                           }
7464                           END {
7465                                 if (marker)
7466                                         print from_marker
7467                                 else
7468                                         print from_begin
7469                           }")
7470
7471         [[ $LLOG_COUNT -gt 120 ]] &&
7472                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
7473 }
7474 run_test 60b "limit repeated messages from CERROR/CWARN"
7475
7476 test_60c() {
7477         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7478
7479         echo "create 5000 files"
7480         createmany -o $DIR/f60c- 5000
7481 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
7482         lctl set_param fail_loc=0x80000137
7483         unlinkmany $DIR/f60c- 5000
7484         lctl set_param fail_loc=0
7485 }
7486 run_test 60c "unlink file when mds full"
7487
7488 test_60d() {
7489         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7490
7491         SAVEPRINTK=$(lctl get_param -n printk)
7492         # verify "lctl mark" is even working"
7493         MESSAGE="test message ID $RANDOM $$"
7494         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7495         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
7496
7497         lctl set_param printk=0 || error "set lnet.printk failed"
7498         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
7499         MESSAGE="new test message ID $RANDOM $$"
7500         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
7501         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7502         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
7503
7504         lctl set_param -n printk="$SAVEPRINTK"
7505 }
7506 run_test 60d "test printk console message masking"
7507
7508 test_60e() {
7509         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7510         remote_mds_nodsh && skip "remote MDS with nodsh"
7511
7512         touch $DIR/$tfile
7513 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
7514         do_facet mds1 lctl set_param fail_loc=0x15b
7515         rm $DIR/$tfile
7516 }
7517 run_test 60e "no space while new llog is being created"
7518
7519 test_60g() {
7520         local pid
7521         local i
7522
7523         test_mkdir -c $MDSCOUNT $DIR/$tdir
7524
7525         (
7526                 local index=0
7527                 while true; do
7528                         $LFS setdirstripe -i $(($index % $MDSCOUNT)) \
7529                                 -c $MDSCOUNT $DIR/$tdir/subdir$index \
7530                                 2>/dev/null
7531                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
7532                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
7533                         index=$((index + 1))
7534                 done
7535         ) &
7536
7537         pid=$!
7538
7539         for i in {0..100}; do
7540                 # define OBD_FAIL_OSD_TXN_START    0x19a
7541                 local index=$((i % MDSCOUNT + 1))
7542
7543                 do_facet mds$index $LCTL set_param fail_loc=0x8000019a \
7544                         > /dev/null
7545                 usleep 100
7546         done
7547
7548         kill -9 $pid
7549
7550         for i in $(seq $MDSCOUNT); do
7551                 do_facet mds$i $LCTL set_param fail_loc=0 > /dev/null
7552         done
7553
7554         mkdir $DIR/$tdir/new || error "mkdir failed"
7555         rmdir $DIR/$tdir/new || error "rmdir failed"
7556
7557         do_facet mds1 $LCTL lfsck_start -M $(facet_svc mds1) -A -C \
7558                 -t namespace
7559         for i in $(seq $MDSCOUNT); do
7560                 wait_update_facet mds$i "$LCTL get_param -n \
7561                         mdd.$(facet_svc mds$i).lfsck_namespace |
7562                         awk '/^status/ { print \\\$2 }'" "completed"
7563         done
7564
7565         ls -R $DIR/$tdir || error "ls failed"
7566         rm -rf $DIR/$tdir || error "rmdir failed"
7567 }
7568 run_test 60g "transaction abort won't cause MDT hung"
7569
7570 test_60h() {
7571         [ $MDS1_VERSION -le $(version_code 2.12.52) ] &&
7572                 skip "Need MDS version at least 2.12.52"
7573         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
7574
7575         local f
7576
7577         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
7578         #define OBD_FAIL_MDS_STRIPE_FID          0x189
7579         for fail_loc in 0x80000188 0x80000189; do
7580                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
7581                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
7582                         error "mkdir $dir-$fail_loc failed"
7583                 for i in {0..10}; do
7584                         # create may fail on missing stripe
7585                         echo $i > $DIR/$tdir-$fail_loc/$i
7586                 done
7587                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7588                         error "getdirstripe $tdir-$fail_loc failed"
7589                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
7590                         error "migrate $tdir-$fail_loc failed"
7591                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7592                         error "getdirstripe $tdir-$fail_loc failed"
7593                 pushd $DIR/$tdir-$fail_loc
7594                 for f in *; do
7595                         echo $f | cmp $f - || error "$f data mismatch"
7596                 done
7597                 popd
7598                 rm -rf $DIR/$tdir-$fail_loc
7599         done
7600 }
7601 run_test 60h "striped directory with missing stripes can be accessed"
7602
7603 test_61a() {
7604         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7605
7606         f="$DIR/f61"
7607         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
7608         cancel_lru_locks osc
7609         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
7610         sync
7611 }
7612 run_test 61a "mmap() writes don't make sync hang ================"
7613
7614 test_61b() {
7615         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
7616 }
7617 run_test 61b "mmap() of unstriped file is successful"
7618
7619 # bug 2330 - insufficient obd_match error checking causes LBUG
7620 test_62() {
7621         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7622
7623         f="$DIR/f62"
7624         echo foo > $f
7625         cancel_lru_locks osc
7626         lctl set_param fail_loc=0x405
7627         cat $f && error "cat succeeded, expect -EIO"
7628         lctl set_param fail_loc=0
7629 }
7630 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
7631 # match every page all of the time.
7632 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
7633
7634 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
7635 # Though this test is irrelevant anymore, it helped to reveal some
7636 # other grant bugs (LU-4482), let's keep it.
7637 test_63a() {   # was test_63
7638         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7639
7640         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
7641
7642         for i in `seq 10` ; do
7643                 dd if=/dev/zero of=$DIR/f63 bs=8k &
7644                 sleep 5
7645                 kill $!
7646                 sleep 1
7647         done
7648
7649         rm -f $DIR/f63 || true
7650 }
7651 run_test 63a "Verify oig_wait interruption does not crash ======="
7652
7653 # bug 2248 - async write errors didn't return to application on sync
7654 # bug 3677 - async write errors left page locked
7655 test_63b() {
7656         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7657
7658         debugsave
7659         lctl set_param debug=-1
7660
7661         # ensure we have a grant to do async writes
7662         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
7663         rm $DIR/$tfile
7664
7665         sync    # sync lest earlier test intercept the fail_loc
7666
7667         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7668         lctl set_param fail_loc=0x80000406
7669         $MULTIOP $DIR/$tfile Owy && \
7670                 error "sync didn't return ENOMEM"
7671         sync; sleep 2; sync     # do a real sync this time to flush page
7672         lctl get_param -n llite.*.dump_page_cache | grep locked && \
7673                 error "locked page left in cache after async error" || true
7674         debugrestore
7675 }
7676 run_test 63b "async write errors should be returned to fsync ==="
7677
7678 test_64a () {
7679         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7680
7681         lfs df $DIR
7682         lctl get_param osc.*[oO][sS][cC][_-]*.cur* | grep "=[1-9]"
7683 }
7684 run_test 64a "verify filter grant calculations (in kernel) ====="
7685
7686 test_64b () {
7687         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7688
7689         sh oos.sh $MOUNT || error "oos.sh failed: $?"
7690 }
7691 run_test 64b "check out-of-space detection on client"
7692
7693 test_64c() {
7694         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
7695 }
7696 run_test 64c "verify grant shrink"
7697
7698 # this does exactly what osc_request.c:osc_announce_cached() does in
7699 # order to calculate max amount of grants to ask from server
7700 want_grant() {
7701         local tgt=$1
7702
7703         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
7704         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
7705
7706         ((rpc_in_flight ++));
7707         nrpages=$((nrpages * rpc_in_flight))
7708
7709         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
7710
7711         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
7712
7713         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
7714         local undirty=$((nrpages * PAGE_SIZE))
7715
7716         local max_extent_pages
7717         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
7718             grep grant_max_extent_size | awk '{print $2}')
7719         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
7720         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
7721         local grant_extent_tax
7722         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7723             grep grant_extent_tax | awk '{print $2}')
7724
7725         undirty=$((undirty + nrextents * grant_extent_tax))
7726
7727         echo $undirty
7728 }
7729
7730 # this is size of unit for grant allocation. It should be equal to
7731 # what tgt_grant.c:tgt_grant_chunk() calculates
7732 grant_chunk() {
7733         local tgt=$1
7734         local max_brw_size
7735         local grant_extent_tax
7736
7737         max_brw_size=$($LCTL get_param osc.${tgt}.import |
7738             grep max_brw_size | awk '{print $2}')
7739
7740         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7741             grep grant_extent_tax | awk '{print $2}')
7742
7743         echo $(((max_brw_size + grant_extent_tax) * 2))
7744 }
7745
7746 test_64d() {
7747         [ $OST1_VERSION -lt $(version_code 2.10.56) ] &&
7748                 skip "OST < 2.10.55 doesn't limit grants enough"
7749
7750         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
7751         local file=$DIR/$tfile
7752
7753         [[ $($LCTL get_param osc.${tgt}.import |
7754              grep "connect_flags:.*grant_param") ]] ||
7755                 skip "no grant_param connect flag"
7756
7757         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
7758
7759         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
7760
7761         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
7762         stack_trap "rm -f $file" EXIT
7763
7764         $LFS setstripe $file -i 0 -c 1
7765         dd if=/dev/zero of=$file bs=1M count=1000 &
7766         ddpid=$!
7767
7768         while true
7769         do
7770                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
7771                 if [[ $cur_grant -gt $max_cur_granted ]]
7772                 then
7773                         kill $ddpid
7774                         error "cur_grant $cur_grant > $max_cur_granted"
7775                 fi
7776                 kill -0 $ddpid
7777                 [[ $? -ne 0 ]] && break;
7778                 sleep 2
7779         done
7780
7781         rm -f $DIR/$tfile
7782         wait_delete_completed
7783         $LCTL set_param debug="$olddebug" 2> /dev/null || true
7784 }
7785 run_test 64d "check grant limit exceed"
7786
7787 # bug 1414 - set/get directories' stripe info
7788 test_65a() {
7789         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7790
7791         test_mkdir $DIR/$tdir
7792         touch $DIR/$tdir/f1
7793         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
7794 }
7795 run_test 65a "directory with no stripe info"
7796
7797 test_65b() {
7798         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7799
7800         test_mkdir $DIR/$tdir
7801         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7802
7803         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7804                                                 error "setstripe"
7805         touch $DIR/$tdir/f2
7806         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
7807 }
7808 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
7809
7810 test_65c() {
7811         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7812         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
7813
7814         test_mkdir $DIR/$tdir
7815         local stripesize=$($LFS getstripe -S $DIR/$tdir)
7816
7817         $LFS setstripe -S $((stripesize * 4)) -i 1 \
7818                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
7819         touch $DIR/$tdir/f3
7820         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
7821 }
7822 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
7823
7824 test_65d() {
7825         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7826
7827         test_mkdir $DIR/$tdir
7828         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
7829         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7830
7831         if [[ $STRIPECOUNT -le 0 ]]; then
7832                 sc=1
7833         elif [[ $STRIPECOUNT -gt $LOV_MAX_STRIPE_COUNT ]]; then
7834                 [[ $OSTCOUNT -gt $LOV_MAX_STRIPE_COUNT ]] &&
7835                         sc=$LOV_MAX_STRIPE_COUNT || sc=$(($OSTCOUNT - 1))
7836         else
7837                 sc=$(($STRIPECOUNT - 1))
7838         fi
7839         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
7840         touch $DIR/$tdir/f4 $DIR/$tdir/f5
7841         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
7842                 error "lverify failed"
7843 }
7844 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
7845
7846 test_65e() {
7847         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7848
7849         test_mkdir $DIR/$tdir
7850
7851         $LFS setstripe $DIR/$tdir || error "setstripe"
7852         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7853                                         error "no stripe info failed"
7854         touch $DIR/$tdir/f6
7855         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
7856 }
7857 run_test 65e "directory setstripe defaults"
7858
7859 test_65f() {
7860         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7861
7862         test_mkdir $DIR/${tdir}f
7863         $RUNAS $LFS setstripe $DIR/${tdir}f &&
7864                 error "setstripe succeeded" || true
7865 }
7866 run_test 65f "dir setstripe permission (should return error) ==="
7867
7868 test_65g() {
7869         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7870
7871         test_mkdir $DIR/$tdir
7872         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7873
7874         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7875                 error "setstripe -S failed"
7876         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
7877         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7878                 error "delete default stripe failed"
7879 }
7880 run_test 65g "directory setstripe -d"
7881
7882 test_65h() {
7883         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7884
7885         test_mkdir $DIR/$tdir
7886         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7887
7888         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7889                 error "setstripe -S failed"
7890         test_mkdir $DIR/$tdir/dd1
7891         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
7892                 error "stripe info inherit failed"
7893 }
7894 run_test 65h "directory stripe info inherit ===================="
7895
7896 test_65i() {
7897         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7898
7899         save_layout_restore_at_exit $MOUNT
7900
7901         # bug6367: set non-default striping on root directory
7902         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
7903
7904         # bug12836: getstripe on -1 default directory striping
7905         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
7906
7907         # bug12836: getstripe -v on -1 default directory striping
7908         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
7909
7910         # bug12836: new find on -1 default directory striping
7911         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
7912 }
7913 run_test 65i "various tests to set root directory striping"
7914
7915 test_65j() { # bug6367
7916         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7917
7918         sync; sleep 1
7919
7920         # if we aren't already remounting for each test, do so for this test
7921         if [ "$I_MOUNTED" = "yes" ]; then
7922                 cleanup || error "failed to unmount"
7923                 setup
7924         fi
7925
7926         save_layout_restore_at_exit $MOUNT
7927
7928         $LFS setstripe -d $MOUNT || error "setstripe failed"
7929 }
7930 run_test 65j "set default striping on root directory (bug 6367)="
7931
7932 cleanup_65k() {
7933         rm -rf $DIR/$tdir
7934         wait_delete_completed
7935         do_facet $SINGLEMDS "lctl set_param -n \
7936                 osp.$ost*MDT0000.max_create_count=$max_count"
7937         do_facet $SINGLEMDS "lctl set_param -n \
7938                 osp.$ost*MDT0000.create_count=$count"
7939         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7940         echo $INACTIVE_OSC "is Activate"
7941
7942         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7943 }
7944
7945 test_65k() { # bug11679
7946         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7947         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7948         remote_mds_nodsh && skip "remote MDS with nodsh"
7949
7950         local disable_precreate=true
7951         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
7952                 disable_precreate=false
7953
7954         echo "Check OST status: "
7955         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
7956                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
7957
7958         for OSC in $MDS_OSCS; do
7959                 echo $OSC "is active"
7960                 do_facet $SINGLEMDS lctl --device %$OSC activate
7961         done
7962
7963         for INACTIVE_OSC in $MDS_OSCS; do
7964                 local ost=$(osc_to_ost $INACTIVE_OSC)
7965                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
7966                                lov.*md*.target_obd |
7967                                awk -F: /$ost/'{ print $1 }' | head -n 1)
7968
7969                 mkdir -p $DIR/$tdir
7970                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
7971                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
7972
7973                 echo "Deactivate: " $INACTIVE_OSC
7974                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
7975
7976                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
7977                               osp.$ost*MDT0000.create_count")
7978                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
7979                                   osp.$ost*MDT0000.max_create_count")
7980                 $disable_precreate &&
7981                         do_facet $SINGLEMDS "lctl set_param -n \
7982                                 osp.$ost*MDT0000.max_create_count=0"
7983
7984                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
7985                         [ -f $DIR/$tdir/$idx ] && continue
7986                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
7987                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
7988                                 { cleanup_65k;
7989                                   error "setstripe $idx should succeed"; }
7990                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
7991                 done
7992                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
7993                 rmdir $DIR/$tdir
7994
7995                 do_facet $SINGLEMDS "lctl set_param -n \
7996                         osp.$ost*MDT0000.max_create_count=$max_count"
7997                 do_facet $SINGLEMDS "lctl set_param -n \
7998                         osp.$ost*MDT0000.create_count=$count"
7999                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
8000                 echo $INACTIVE_OSC "is Activate"
8001
8002                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
8003         done
8004 }
8005 run_test 65k "validate manual striping works properly with deactivated OSCs"
8006
8007 test_65l() { # bug 12836
8008         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8009
8010         test_mkdir -p $DIR/$tdir/test_dir
8011         $LFS setstripe -c -1 $DIR/$tdir/test_dir
8012         $LFS find -mtime -1 $DIR/$tdir >/dev/null
8013 }
8014 run_test 65l "lfs find on -1 stripe dir ========================"
8015
8016 test_65m() {
8017         local layout=$(save_layout $MOUNT)
8018         $RUNAS $LFS setstripe -c 2 $MOUNT && {
8019                 restore_layout $MOUNT $layout
8020                 error "setstripe should fail by non-root users"
8021         }
8022         true
8023 }
8024 run_test 65m "normal user can't set filesystem default stripe"
8025
8026 test_65n() {
8027         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
8028         [[ $MDS1_VERSION -ge $(version_code 2.12.50) ]] ||
8029                 skip "Need MDS version at least 2.12.50"
8030         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
8031
8032         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
8033         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
8034         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
8035
8036         local root_layout=$(save_layout $MOUNT)
8037         stack_trap "restore_layout $MOUNT $root_layout" EXIT
8038
8039         # new subdirectory under root directory should not inherit
8040         # the default layout from root
8041         local dir1=$MOUNT/$tdir-1
8042         mkdir $dir1 || error "mkdir $dir1 failed"
8043         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
8044                 error "$dir1 shouldn't have LOV EA"
8045
8046         # delete the default layout on root directory
8047         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
8048
8049         local dir2=$MOUNT/$tdir-2
8050         mkdir $dir2 || error "mkdir $dir2 failed"
8051         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
8052                 error "$dir2 shouldn't have LOV EA"
8053
8054         # set a new striping pattern on root directory
8055         local def_stripe_size=$($LFS getstripe -S $MOUNT)
8056         local new_def_stripe_size=$((def_stripe_size * 2))
8057         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
8058                 error "set stripe size on $MOUNT failed"
8059
8060         # new file created in $dir2 should inherit the new stripe size from
8061         # the filesystem default
8062         local file2=$dir2/$tfile-2
8063         touch $file2 || error "touch $file2 failed"
8064
8065         local file2_stripe_size=$($LFS getstripe -S $file2)
8066         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
8067                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
8068
8069         local dir3=$MOUNT/$tdir-3
8070         mkdir $dir3 || error "mkdir $dir3 failed"
8071         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
8072         # the root layout, which is the actual default layout that will be used
8073         # when new files are created in $dir3.
8074         local dir3_layout=$(get_layout_param $dir3)
8075         local root_dir_layout=$(get_layout_param $MOUNT)
8076         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
8077                 error "$dir3 should show the default layout from $MOUNT"
8078
8079         # set OST pool on root directory
8080         local pool=$TESTNAME
8081         pool_add $pool || error "add $pool failed"
8082         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
8083                 error "add targets to $pool failed"
8084
8085         $LFS setstripe -p $pool $MOUNT ||
8086                 error "set OST pool on $MOUNT failed"
8087
8088         # new file created in $dir3 should inherit the pool from
8089         # the filesystem default
8090         local file3=$dir3/$tfile-3
8091         touch $file3 || error "touch $file3 failed"
8092
8093         local file3_pool=$($LFS getstripe -p $file3)
8094         [[ "$file3_pool" = "$pool" ]] ||
8095                 error "$file3 didn't inherit OST pool $pool"
8096
8097         local dir4=$MOUNT/$tdir-4
8098         mkdir $dir4 || error "mkdir $dir4 failed"
8099         local dir4_layout=$(get_layout_param $dir4)
8100         root_dir_layout=$(get_layout_param $MOUNT)
8101         echo "$LFS getstripe -d $dir4"
8102         $LFS getstripe -d $dir4
8103         echo "$LFS getstripe -d $MOUNT"
8104         $LFS getstripe -d $MOUNT
8105         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
8106                 error "$dir4 should show the default layout from $MOUNT"
8107
8108         # new file created in $dir4 should inherit the pool from
8109         # the filesystem default
8110         local file4=$dir4/$tfile-4
8111         touch $file4 || error "touch $file4 failed"
8112
8113         local file4_pool=$($LFS getstripe -p $file4)
8114         [[ "$file4_pool" = "$pool" ]] ||
8115                 error "$file4 didn't inherit OST pool $pool"
8116
8117         # new subdirectory under non-root directory should inherit
8118         # the default layout from its parent directory
8119         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
8120                 error "set directory layout on $dir4 failed"
8121
8122         local dir5=$dir4/$tdir-5
8123         mkdir $dir5 || error "mkdir $dir5 failed"
8124
8125         dir4_layout=$(get_layout_param $dir4)
8126         local dir5_layout=$(get_layout_param $dir5)
8127         [[ "$dir4_layout" = "$dir5_layout" ]] ||
8128                 error "$dir5 should inherit the default layout from $dir4"
8129
8130         # though subdir under ROOT doesn't inherit default layout, but
8131         # its sub dir/file should be created with default layout.
8132         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
8133         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
8134                 skip "Need MDS version at least 2.12.59"
8135
8136         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
8137         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
8138         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
8139
8140         if [ $default_lmv_hash == "none" ]; then
8141                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
8142         else
8143                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
8144                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
8145         fi
8146
8147         $LFS setdirstripe -D -c 2 $MOUNT ||
8148                 error "setdirstripe -D -c 2 failed"
8149         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
8150         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
8151         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
8152 }
8153 run_test 65n "don't inherit default layout from root for new subdirectories"
8154
8155 # bug 2543 - update blocks count on client
8156 test_66() {
8157         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8158
8159         COUNT=${COUNT:-8}
8160         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
8161         sync; sync_all_data; sync; sync_all_data
8162         cancel_lru_locks osc
8163         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
8164         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
8165 }
8166 run_test 66 "update inode blocks count on client ==============="
8167
8168 meminfo() {
8169         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
8170 }
8171
8172 swap_used() {
8173         swapon -s | awk '($1 == "'$1'") { print $4 }'
8174 }
8175
8176 # bug5265, obdfilter oa2dentry return -ENOENT
8177 # #define OBD_FAIL_SRV_ENOENT 0x217
8178 test_69() {
8179         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8180         remote_ost_nodsh && skip "remote OST with nodsh"
8181
8182         f="$DIR/$tfile"
8183         $LFS setstripe -c 1 -i 0 $f
8184
8185         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
8186
8187         do_facet ost1 lctl set_param fail_loc=0x217
8188         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
8189         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
8190
8191         do_facet ost1 lctl set_param fail_loc=0
8192         $DIRECTIO write $f 0 2 || error "write error"
8193
8194         cancel_lru_locks osc
8195         $DIRECTIO read $f 0 1 || error "read error"
8196
8197         do_facet ost1 lctl set_param fail_loc=0x217
8198         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
8199
8200         do_facet ost1 lctl set_param fail_loc=0
8201         rm -f $f
8202 }
8203 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
8204
8205 test_71() {
8206         test_mkdir $DIR/$tdir
8207         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
8208         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
8209 }
8210 run_test 71 "Running dbench on lustre (don't segment fault) ===="
8211
8212 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
8213         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8214         [ "$RUNAS_ID" = "$UID" ] &&
8215                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8216         # Check that testing environment is properly set up. Skip if not
8217         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
8218                 skip_env "User $RUNAS_ID does not exist - skipping"
8219
8220         touch $DIR/$tfile
8221         chmod 777 $DIR/$tfile
8222         chmod ug+s $DIR/$tfile
8223         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
8224                 error "$RUNAS dd $DIR/$tfile failed"
8225         # See if we are still setuid/sgid
8226         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8227                 error "S/gid is not dropped on write"
8228         # Now test that MDS is updated too
8229         cancel_lru_locks mdc
8230         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8231                 error "S/gid is not dropped on MDS"
8232         rm -f $DIR/$tfile
8233 }
8234 run_test 72a "Test that remove suid works properly (bug5695) ===="
8235
8236 test_72b() { # bug 24226 -- keep mode setting when size is not changing
8237         local perm
8238
8239         [ "$RUNAS_ID" = "$UID" ] &&
8240                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8241         [ "$RUNAS_ID" -eq 0 ] &&
8242                 skip_env "RUNAS_ID = 0 -- skipping"
8243         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8244         # Check that testing environment is properly set up. Skip if not
8245         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
8246                 skip_env "User $RUNAS_ID does not exist - skipping"
8247
8248         touch $DIR/${tfile}-f{g,u}
8249         test_mkdir $DIR/${tfile}-dg
8250         test_mkdir $DIR/${tfile}-du
8251         chmod 770 $DIR/${tfile}-{f,d}{g,u}
8252         chmod g+s $DIR/${tfile}-{f,d}g
8253         chmod u+s $DIR/${tfile}-{f,d}u
8254         for perm in 777 2777 4777; do
8255                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
8256                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
8257                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
8258                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
8259         done
8260         true
8261 }
8262 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
8263
8264 # bug 3462 - multiple simultaneous MDC requests
8265 test_73() {
8266         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8267
8268         test_mkdir $DIR/d73-1
8269         test_mkdir $DIR/d73-2
8270         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
8271         pid1=$!
8272
8273         lctl set_param fail_loc=0x80000129
8274         $MULTIOP $DIR/d73-1/f73-2 Oc &
8275         sleep 1
8276         lctl set_param fail_loc=0
8277
8278         $MULTIOP $DIR/d73-2/f73-3 Oc &
8279         pid3=$!
8280
8281         kill -USR1 $pid1
8282         wait $pid1 || return 1
8283
8284         sleep 25
8285
8286         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
8287         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
8288         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
8289
8290         rm -rf $DIR/d73-*
8291 }
8292 run_test 73 "multiple MDC requests (should not deadlock)"
8293
8294 test_74a() { # bug 6149, 6184
8295         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8296
8297         touch $DIR/f74a
8298         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8299         #
8300         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8301         # will spin in a tight reconnection loop
8302         $LCTL set_param fail_loc=0x8000030e
8303         # get any lock that won't be difficult - lookup works.
8304         ls $DIR/f74a
8305         $LCTL set_param fail_loc=0
8306         rm -f $DIR/f74a
8307         true
8308 }
8309 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
8310
8311 test_74b() { # bug 13310
8312         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8313
8314         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8315         #
8316         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8317         # will spin in a tight reconnection loop
8318         $LCTL set_param fail_loc=0x8000030e
8319         # get a "difficult" lock
8320         touch $DIR/f74b
8321         $LCTL set_param fail_loc=0
8322         rm -f $DIR/f74b
8323         true
8324 }
8325 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
8326
8327 test_74c() {
8328         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8329
8330         #define OBD_FAIL_LDLM_NEW_LOCK
8331         $LCTL set_param fail_loc=0x319
8332         touch $DIR/$tfile && error "touch successful"
8333         $LCTL set_param fail_loc=0
8334         true
8335 }
8336 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
8337
8338 num_inodes() {
8339         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
8340 }
8341
8342 test_76() { # Now for bug 20433, added originally in bug 1443
8343         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8344
8345         cancel_lru_locks osc
8346         local cpus=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
8347         local before=$(num_inodes)
8348         local count=$((512 * cpus))
8349         [ "$SLOW" = "no" ] && count=$((64 * cpus))
8350
8351         echo "before inodes: $before"
8352         for i in $(seq $count); do
8353                 touch $DIR/$tfile
8354                 rm -f $DIR/$tfile
8355         done
8356         cancel_lru_locks osc
8357         local after=$(num_inodes)
8358         echo "after inodes: $after"
8359         while (( after > before + 8 * ${cpus:-1} )); do
8360                 sleep 1
8361                 after=$(num_inodes)
8362                 wait=$((wait + 1))
8363                 (( wait % 5 == 0 )) && echo "wait $wait seconds inodes: $after"
8364                 if (( wait > 30 )); then
8365                         error "inode slab grew from $before to $after"
8366                 fi
8367         done
8368 }
8369 run_test 76 "confirm clients recycle inodes properly ===="
8370
8371
8372 export ORIG_CSUM=""
8373 set_checksums()
8374 {
8375         # Note: in sptlrpc modes which enable its own bulk checksum, the
8376         # original crc32_le bulk checksum will be automatically disabled,
8377         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
8378         # will be checked by sptlrpc code against sptlrpc bulk checksum.
8379         # In this case set_checksums() will not be no-op, because sptlrpc
8380         # bulk checksum will be enabled all through the test.
8381
8382         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
8383         lctl set_param -n osc.*.checksums $1
8384         return 0
8385 }
8386
8387 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8388                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
8389 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8390                              tr -d [] | head -n1)}
8391 set_checksum_type()
8392 {
8393         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
8394         rc=$?
8395         log "set checksum type to $1, rc = $rc"
8396         return $rc
8397 }
8398
8399 get_osc_checksum_type()
8400 {
8401         # arugment 1: OST name, like OST0000
8402         ost=$1
8403         checksum_type=$(lctl get_param -n osc.*${ost}-osc-[^mM]*.checksum_type |
8404                         sed 's/.*\[\(.*\)\].*/\1/g')
8405         rc=$?
8406         [ $rc -ne 0 ] && error "failed to get checksum type of $ost, rc = $rc, output = $checksum_type"
8407         echo $checksum_type
8408 }
8409
8410 F77_TMP=$TMP/f77-temp
8411 F77SZ=8
8412 setup_f77() {
8413         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
8414                 error "error writing to $F77_TMP"
8415 }
8416
8417 test_77a() { # bug 10889
8418         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8419         $GSS && skip_env "could not run with gss"
8420
8421         [ ! -f $F77_TMP ] && setup_f77
8422         set_checksums 1
8423         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
8424         set_checksums 0
8425         rm -f $DIR/$tfile
8426 }
8427 run_test 77a "normal checksum read/write operation"
8428
8429 test_77b() { # bug 10889
8430         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8431         $GSS && skip_env "could not run with gss"
8432
8433         [ ! -f $F77_TMP ] && setup_f77
8434         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8435         $LCTL set_param fail_loc=0x80000409
8436         set_checksums 1
8437
8438         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8439                 error "dd error: $?"
8440         $LCTL set_param fail_loc=0
8441
8442         for algo in $CKSUM_TYPES; do
8443                 cancel_lru_locks osc
8444                 set_checksum_type $algo
8445                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8446                 $LCTL set_param fail_loc=0x80000408
8447                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
8448                 $LCTL set_param fail_loc=0
8449         done
8450         set_checksums 0
8451         set_checksum_type $ORIG_CSUM_TYPE
8452         rm -f $DIR/$tfile
8453 }
8454 run_test 77b "checksum error on client write, read"
8455
8456 cleanup_77c() {
8457         trap 0
8458         set_checksums 0
8459         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
8460         $check_ost &&
8461                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
8462         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
8463         $check_ost && [ -n "$ost_file_prefix" ] &&
8464                 do_facet ost1 rm -f ${ost_file_prefix}\*
8465 }
8466
8467 test_77c() {
8468         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8469         $GSS && skip_env "could not run with gss"
8470         remote_ost_nodsh && skip "remote OST with nodsh"
8471
8472         local bad1
8473         local osc_file_prefix
8474         local osc_file
8475         local check_ost=false
8476         local ost_file_prefix
8477         local ost_file
8478         local orig_cksum
8479         local dump_cksum
8480         local fid
8481
8482         # ensure corruption will occur on first OSS/OST
8483         $LFS setstripe -i 0 $DIR/$tfile
8484
8485         [ ! -f $F77_TMP ] && setup_f77
8486         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8487                 error "dd write error: $?"
8488         fid=$($LFS path2fid $DIR/$tfile)
8489
8490         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
8491         then
8492                 check_ost=true
8493                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
8494                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
8495         else
8496                 echo "OSS do not support bulk pages dump upon error"
8497         fi
8498
8499         osc_file_prefix=$($LCTL get_param -n debug_path)
8500         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
8501
8502         trap cleanup_77c EXIT
8503
8504         set_checksums 1
8505         # enable bulk pages dump upon error on Client
8506         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
8507         # enable bulk pages dump upon error on OSS
8508         $check_ost &&
8509                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
8510
8511         # flush Client cache to allow next read to reach OSS
8512         cancel_lru_locks osc
8513
8514         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
8515         $LCTL set_param fail_loc=0x80000408
8516         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
8517         $LCTL set_param fail_loc=0
8518
8519         rm -f $DIR/$tfile
8520
8521         # check cksum dump on Client
8522         osc_file=$(ls ${osc_file_prefix}*)
8523         [ -n "$osc_file" ] || error "no checksum dump file on Client"
8524         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
8525         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
8526         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
8527         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
8528                      cksum)
8529         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
8530         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8531                 error "dump content does not match on Client"
8532
8533         $check_ost || skip "No need to check cksum dump on OSS"
8534
8535         # check cksum dump on OSS
8536         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
8537         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
8538         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
8539         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
8540         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8541                 error "dump content does not match on OSS"
8542
8543         cleanup_77c
8544 }
8545 run_test 77c "checksum error on client read with debug"
8546
8547 test_77d() { # bug 10889
8548         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8549         $GSS && skip_env "could not run with gss"
8550
8551         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8552         $LCTL set_param fail_loc=0x80000409
8553         set_checksums 1
8554         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8555                 error "direct write: rc=$?"
8556         $LCTL set_param fail_loc=0
8557         set_checksums 0
8558
8559         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8560         $LCTL set_param fail_loc=0x80000408
8561         set_checksums 1
8562         cancel_lru_locks osc
8563         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8564                 error "direct read: rc=$?"
8565         $LCTL set_param fail_loc=0
8566         set_checksums 0
8567 }
8568 run_test 77d "checksum error on OST direct write, read"
8569
8570 test_77f() { # bug 10889
8571         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8572         $GSS && skip_env "could not run with gss"
8573
8574         set_checksums 1
8575         for algo in $CKSUM_TYPES; do
8576                 cancel_lru_locks osc
8577                 set_checksum_type $algo
8578                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8579                 $LCTL set_param fail_loc=0x409
8580                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
8581                         error "direct write succeeded"
8582                 $LCTL set_param fail_loc=0
8583         done
8584         set_checksum_type $ORIG_CSUM_TYPE
8585         set_checksums 0
8586 }
8587 run_test 77f "repeat checksum error on write (expect error)"
8588
8589 test_77g() { # bug 10889
8590         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8591         $GSS && skip_env "could not run with gss"
8592         remote_ost_nodsh && skip "remote OST with nodsh"
8593
8594         [ ! -f $F77_TMP ] && setup_f77
8595
8596         local file=$DIR/$tfile
8597         stack_trap "rm -f $file" EXIT
8598
8599         $LFS setstripe -c 1 -i 0 $file
8600         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
8601         do_facet ost1 lctl set_param fail_loc=0x8000021a
8602         set_checksums 1
8603         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
8604                 error "write error: rc=$?"
8605         do_facet ost1 lctl set_param fail_loc=0
8606         set_checksums 0
8607
8608         cancel_lru_locks osc
8609         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
8610         do_facet ost1 lctl set_param fail_loc=0x8000021b
8611         set_checksums 1
8612         cmp $F77_TMP $file || error "file compare failed"
8613         do_facet ost1 lctl set_param fail_loc=0
8614         set_checksums 0
8615 }
8616 run_test 77g "checksum error on OST write, read"
8617
8618 test_77k() { # LU-10906
8619         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8620         $GSS && skip_env "could not run with gss"
8621
8622         local cksum_param="osc.$FSNAME*.checksums"
8623         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
8624         local checksum
8625         local i
8626
8627         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
8628         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
8629         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
8630                 EXIT
8631
8632         for i in 0 1; do
8633                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
8634                         error "failed to set checksum=$i on MGS"
8635                 wait_update $HOSTNAME "$get_checksum" $i
8636                 #remount
8637                 echo "remount client, checksum should be $i"
8638                 remount_client $MOUNT || error "failed to remount client"
8639                 checksum=$(eval $get_checksum)
8640                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8641         done
8642         # remove persistent param to avoid races with checksum mountopt below
8643         do_facet mgs $LCTL set_param -P -d $cksum_param ||
8644                 error "failed to delete checksum on MGS"
8645
8646         for opt in "checksum" "nochecksum"; do
8647                 #remount with mount option
8648                 echo "remount client with option $opt, checksum should be $i"
8649                 umount_client $MOUNT || error "failed to umount client"
8650                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
8651                         error "failed to mount client with option '$opt'"
8652                 checksum=$(eval $get_checksum)
8653                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8654                 i=$((i - 1))
8655         done
8656
8657         remount_client $MOUNT || error "failed to remount client"
8658 }
8659 run_test 77k "enable/disable checksum correctly"
8660
8661 test_77l() {
8662         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8663         $GSS && skip_env "could not run with gss"
8664
8665         set_checksums 1
8666         stack_trap "set_checksums $ORIG_CSUM" EXIT
8667         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
8668
8669         set_checksum_type invalid && error "unexpected success of invalid checksum type"
8670
8671         $LFS setstripe -c 1 -i 0 $DIR/$tfile
8672         for algo in $CKSUM_TYPES; do
8673                 set_checksum_type $algo || error "fail to set checksum type $algo"
8674                 osc_algo=$(get_osc_checksum_type OST0000)
8675                 [ "$osc_algo" != "$algo" ] && error "checksum type is $osc_algo after setting it to $algo"
8676
8677                 # no locks, no reqs to let the connection idle
8678                 cancel_lru_locks osc
8679                 lru_resize_disable osc
8680                 wait_osc_import_state client ost1 IDLE
8681
8682                 # ensure ost1 is connected
8683                 stat $DIR/$tfile >/dev/null || error "can't stat"
8684                 wait_osc_import_state client ost1 FULL
8685
8686                 osc_algo=$(get_osc_checksum_type OST0000)
8687                 [ "$osc_algo" != "$algo" ] && error "checksum type changed from $algo to $osc_algo after reconnection"
8688         done
8689         return 0
8690 }
8691 run_test 77l "preferred checksum type is remembered after reconnected"
8692
8693 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
8694 rm -f $F77_TMP
8695 unset F77_TMP
8696
8697 cleanup_test_78() {
8698         trap 0
8699         rm -f $DIR/$tfile
8700 }
8701
8702 test_78() { # bug 10901
8703         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8704         remote_ost || skip_env "local OST"
8705
8706         NSEQ=5
8707         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
8708         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
8709         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
8710         echo "MemTotal: $MEMTOTAL"
8711
8712         # reserve 256MB of memory for the kernel and other running processes,
8713         # and then take 1/2 of the remaining memory for the read/write buffers.
8714         if [ $MEMTOTAL -gt 512 ] ;then
8715                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
8716         else
8717                 # for those poor memory-starved high-end clusters...
8718                 MEMTOTAL=$((MEMTOTAL / 2))
8719         fi
8720         echo "Mem to use for directio: $MEMTOTAL"
8721
8722         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
8723         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
8724         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
8725         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
8726                 head -n1)
8727         echo "Smallest OST: $SMALLESTOST"
8728         [[ $SMALLESTOST -lt 10240 ]] &&
8729                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
8730
8731         trap cleanup_test_78 EXIT
8732
8733         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
8734                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
8735
8736         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
8737         echo "File size: $F78SIZE"
8738         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
8739         for i in $(seq 1 $NSEQ); do
8740                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
8741                 echo directIO rdwr round $i of $NSEQ
8742                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
8743         done
8744
8745         cleanup_test_78
8746 }
8747 run_test 78 "handle large O_DIRECT writes correctly ============"
8748
8749 test_79() { # bug 12743
8750         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8751
8752         wait_delete_completed
8753
8754         BKTOTAL=$(calc_osc_kbytes kbytestotal)
8755         BKFREE=$(calc_osc_kbytes kbytesfree)
8756         BKAVAIL=$(calc_osc_kbytes kbytesavail)
8757
8758         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
8759         DFTOTAL=`echo $STRING | cut -d, -f1`
8760         DFUSED=`echo $STRING  | cut -d, -f2`
8761         DFAVAIL=`echo $STRING | cut -d, -f3`
8762         DFFREE=$(($DFTOTAL - $DFUSED))
8763
8764         ALLOWANCE=$((64 * $OSTCOUNT))
8765
8766         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
8767            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
8768                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
8769         fi
8770         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
8771            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
8772                 error "df free($DFFREE) mismatch OST free($BKFREE)"
8773         fi
8774         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
8775            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
8776                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
8777         fi
8778 }
8779 run_test 79 "df report consistency check ======================="
8780
8781 test_80() { # bug 10718
8782         remote_ost_nodsh && skip "remote OST with nodsh"
8783         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8784
8785         # relax strong synchronous semantics for slow backends like ZFS
8786         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
8787                 local soc="obdfilter.*.sync_lock_cancel"
8788                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8789
8790                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
8791                 if [ -z "$save" ]; then
8792                         soc="obdfilter.*.sync_on_lock_cancel"
8793                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8794                 fi
8795
8796                 if [ "$save" != "never" ]; then
8797                         local hosts=$(comma_list $(osts_nodes))
8798
8799                         do_nodes $hosts $LCTL set_param $soc=never
8800                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
8801                 fi
8802         fi
8803
8804         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
8805         sync; sleep 1; sync
8806         local before=$(date +%s)
8807         cancel_lru_locks osc
8808         local after=$(date +%s)
8809         local diff=$((after - before))
8810         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
8811
8812         rm -f $DIR/$tfile
8813 }
8814 run_test 80 "Page eviction is equally fast at high offsets too"
8815
8816 test_81a() { # LU-456
8817         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8818         remote_ost_nodsh && skip "remote OST with nodsh"
8819
8820         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8821         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
8822         do_facet ost1 lctl set_param fail_loc=0x80000228
8823
8824         # write should trigger a retry and success
8825         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8826         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8827         RC=$?
8828         if [ $RC -ne 0 ] ; then
8829                 error "write should success, but failed for $RC"
8830         fi
8831 }
8832 run_test 81a "OST should retry write when get -ENOSPC ==============="
8833
8834 test_81b() { # LU-456
8835         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8836         remote_ost_nodsh && skip "remote OST with nodsh"
8837
8838         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8839         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
8840         do_facet ost1 lctl set_param fail_loc=0x228
8841
8842         # write should retry several times and return -ENOSPC finally
8843         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8844         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8845         RC=$?
8846         ENOSPC=28
8847         if [ $RC -ne $ENOSPC ] ; then
8848                 error "dd should fail for -ENOSPC, but succeed."
8849         fi
8850 }
8851 run_test 81b "OST should return -ENOSPC when retry still fails ======="
8852
8853 test_82() { # LU-1031
8854         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
8855         local gid1=14091995
8856         local gid2=16022000
8857
8858         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
8859         local MULTIPID1=$!
8860         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
8861         local MULTIPID2=$!
8862         kill -USR1 $MULTIPID2
8863         sleep 2
8864         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
8865                 error "First grouplock does not block second one"
8866         else
8867                 echo "Second grouplock blocks first one"
8868         fi
8869         kill -USR1 $MULTIPID1
8870         wait $MULTIPID1
8871         wait $MULTIPID2
8872 }
8873 run_test 82 "Basic grouplock test"
8874
8875 test_99() {
8876         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
8877
8878         test_mkdir $DIR/$tdir.cvsroot
8879         chown $RUNAS_ID $DIR/$tdir.cvsroot
8880
8881         cd $TMP
8882         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
8883
8884         cd /etc/init.d
8885         # some versions of cvs import exit(1) when asked to import links or
8886         # files they can't read.  ignore those files.
8887         local toignore=$(find . -type l -printf '-I %f\n' -o \
8888                          ! -perm /4 -printf '-I %f\n')
8889         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
8890                 $tdir.reposname vtag rtag
8891
8892         cd $DIR
8893         test_mkdir $DIR/$tdir.reposname
8894         chown $RUNAS_ID $DIR/$tdir.reposname
8895         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
8896
8897         cd $DIR/$tdir.reposname
8898         $RUNAS touch foo99
8899         $RUNAS cvs add -m 'addmsg' foo99
8900         $RUNAS cvs update
8901         $RUNAS cvs commit -m 'nomsg' foo99
8902         rm -fr $DIR/$tdir.cvsroot
8903 }
8904 run_test 99 "cvs strange file/directory operations"
8905
8906 test_100() {
8907         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8908         [[ "$NETTYPE" =~ tcp ]] ||
8909                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
8910         remote_ost_nodsh && skip "remote OST with nodsh"
8911         remote_mds_nodsh && skip "remote MDS with nodsh"
8912         remote_servers ||
8913                 skip "useless for local single node setup"
8914
8915         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
8916                 [ "$PROT" != "tcp" ] && continue
8917                 RPORT=$(echo $REMOTE | cut -d: -f2)
8918                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
8919
8920                 rc=0
8921                 LPORT=`echo $LOCAL | cut -d: -f2`
8922                 if [ $LPORT -ge 1024 ]; then
8923                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
8924                         netstat -tna
8925                         error_exit "local: $LPORT > 1024, remote: $RPORT"
8926                 fi
8927         done
8928         [ "$rc" = 0 ] || error_exit "privileged port not found" )
8929 }
8930 run_test 100 "check local port using privileged port ==========="
8931
8932 function get_named_value()
8933 {
8934     local tag
8935
8936     tag=$1
8937     while read ;do
8938         line=$REPLY
8939         case $line in
8940         $tag*)
8941             echo $line | sed "s/^$tag[ ]*//"
8942             break
8943             ;;
8944         esac
8945     done
8946 }
8947
8948 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
8949                    awk '/^max_cached_mb/ { print $2 }')
8950
8951 cleanup_101a() {
8952         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
8953         trap 0
8954 }
8955
8956 test_101a() {
8957         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8958
8959         local s
8960         local discard
8961         local nreads=10000
8962         local cache_limit=32
8963
8964         $LCTL set_param -n osc.*-osc*.rpc_stats 0
8965         trap cleanup_101a EXIT
8966         $LCTL set_param -n llite.*.read_ahead_stats 0
8967         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
8968
8969         #
8970         # randomly read 10000 of 64K chunks from file 3x 32MB in size
8971         #
8972         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
8973         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
8974
8975         discard=0
8976         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
8977                 get_named_value 'read but discarded' | cut -d" " -f1); do
8978                         discard=$(($discard + $s))
8979         done
8980         cleanup_101a
8981
8982         $LCTL get_param osc.*-osc*.rpc_stats
8983         $LCTL get_param llite.*.read_ahead_stats
8984
8985         # Discard is generally zero, but sometimes a few random reads line up
8986         # and trigger larger readahead, which is wasted & leads to discards.
8987         if [[ $(($discard)) -gt $nreads ]]; then
8988                 error "too many ($discard) discarded pages"
8989         fi
8990         rm -f $DIR/$tfile || true
8991 }
8992 run_test 101a "check read-ahead for random reads"
8993
8994 setup_test101bc() {
8995         test_mkdir $DIR/$tdir
8996         local ssize=$1
8997         local FILE_LENGTH=$2
8998         STRIPE_OFFSET=0
8999
9000         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
9001
9002         local list=$(comma_list $(osts_nodes))
9003         set_osd_param $list '' read_cache_enable 0
9004         set_osd_param $list '' writethrough_cache_enable 0
9005
9006         trap cleanup_test101bc EXIT
9007         # prepare the read-ahead file
9008         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
9009
9010         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
9011                                 count=$FILE_SIZE_MB 2> /dev/null
9012
9013 }
9014
9015 cleanup_test101bc() {
9016         trap 0
9017         rm -rf $DIR/$tdir
9018         rm -f $DIR/$tfile
9019
9020         local list=$(comma_list $(osts_nodes))
9021         set_osd_param $list '' read_cache_enable 1
9022         set_osd_param $list '' writethrough_cache_enable 1
9023 }
9024
9025 calc_total() {
9026         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
9027 }
9028
9029 ra_check_101() {
9030         local READ_SIZE=$1
9031         local STRIPE_SIZE=$2
9032         local FILE_LENGTH=$3
9033         local RA_INC=1048576
9034         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
9035         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
9036                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
9037         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
9038                         get_named_value 'read but discarded' |
9039                         cut -d" " -f1 | calc_total)
9040         if [[ $DISCARD -gt $discard_limit ]]; then
9041                 $LCTL get_param llite.*.read_ahead_stats
9042                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
9043         else
9044                 echo "Read-ahead success for size ${READ_SIZE}"
9045         fi
9046 }
9047
9048 test_101b() {
9049         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9050         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9051
9052         local STRIPE_SIZE=1048576
9053         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
9054
9055         if [ $SLOW == "yes" ]; then
9056                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
9057         else
9058                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
9059         fi
9060
9061         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
9062
9063         # prepare the read-ahead file
9064         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
9065         cancel_lru_locks osc
9066         for BIDX in 2 4 8 16 32 64 128 256
9067         do
9068                 local BSIZE=$((BIDX*4096))
9069                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
9070                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
9071                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
9072                 $LCTL set_param -n llite.*.read_ahead_stats 0
9073                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
9074                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
9075                 cancel_lru_locks osc
9076                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
9077         done
9078         cleanup_test101bc
9079         true
9080 }
9081 run_test 101b "check stride-io mode read-ahead ================="
9082
9083 test_101c() {
9084         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9085
9086         local STRIPE_SIZE=1048576
9087         local FILE_LENGTH=$((STRIPE_SIZE*100))
9088         local nreads=10000
9089         local rsize=65536
9090         local osc_rpc_stats
9091
9092         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
9093
9094         cancel_lru_locks osc
9095         $LCTL set_param osc.*.rpc_stats 0
9096         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
9097         $LCTL get_param osc.*.rpc_stats
9098         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
9099                 local stats=$($LCTL get_param -n $osc_rpc_stats)
9100                 local lines=$(echo "$stats" | awk 'END {print NR;}')
9101                 local size
9102
9103                 if [ $lines -le 20 ]; then
9104                         echo "continue debug"
9105                         continue
9106                 fi
9107                 for size in 1 2 4 8; do
9108                         local rpc=$(echo "$stats" |
9109                                     awk '($1 == "'$size':") {print $2; exit; }')
9110                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
9111                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
9112                 done
9113                 echo "$osc_rpc_stats check passed!"
9114         done
9115         cleanup_test101bc
9116         true
9117 }
9118 run_test 101c "check stripe_size aligned read-ahead ================="
9119
9120 test_101d() {
9121         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9122
9123         local file=$DIR/$tfile
9124         local sz_MB=${FILESIZE_101d:-80}
9125         local ra_MB=${READAHEAD_MB:-40}
9126
9127         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
9128         [ $free_MB -lt $sz_MB ] &&
9129                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
9130
9131         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
9132         $LFS setstripe -c -1 $file || error "setstripe failed"
9133
9134         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
9135         echo Cancel LRU locks on lustre client to flush the client cache
9136         cancel_lru_locks osc
9137
9138         echo Disable read-ahead
9139         local old_RA=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
9140         $LCTL set_param -n llite.*.max_read_ahead_mb=0
9141         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_RA" EXIT
9142         $LCTL get_param -n llite.*.max_read_ahead_mb
9143
9144         echo Reading the test file $file with read-ahead disabled
9145         local sz_KB=$((sz_MB * 1024 / 4))
9146         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=4k count=$sz_KB")
9147
9148         echo Cancel LRU locks on lustre client to flush the client cache
9149         cancel_lru_locks osc
9150         echo Enable read-ahead with ${ra_MB}MB
9151         $LCTL set_param -n llite.*.max_read_ahead_mb=$ra_MB
9152
9153         echo Reading the test file $file with read-ahead enabled
9154         local raON=$(do_and_time "dd if=$file of=/dev/null bs=4k count=$sz_KB")
9155
9156         echo "read-ahead disabled time read $raOFF"
9157         echo "read-ahead enabled time read $raON"
9158
9159         rm -f $file
9160         wait_delete_completed
9161
9162         [ $raOFF -le 1 ] || [ $raON -lt $raOFF ] ||
9163                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
9164 }
9165 run_test 101d "file read with and without read-ahead enabled"
9166
9167 test_101e() {
9168         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9169
9170         local file=$DIR/$tfile
9171         local size_KB=500  #KB
9172         local count=100
9173         local bsize=1024
9174
9175         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
9176         local need_KB=$((count * size_KB))
9177         [[ $free_KB -le $need_KB ]] &&
9178                 skip_env "Need free space $need_KB, have $free_KB"
9179
9180         echo "Creating $count ${size_KB}K test files"
9181         for ((i = 0; i < $count; i++)); do
9182                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
9183         done
9184
9185         echo "Cancel LRU locks on lustre client to flush the client cache"
9186         cancel_lru_locks $OSC
9187
9188         echo "Reset readahead stats"
9189         $LCTL set_param -n llite.*.read_ahead_stats 0
9190
9191         for ((i = 0; i < $count; i++)); do
9192                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
9193         done
9194
9195         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9196                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
9197
9198         for ((i = 0; i < $count; i++)); do
9199                 rm -rf $file.$i 2>/dev/null
9200         done
9201
9202         #10000 means 20% reads are missing in readahead
9203         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
9204 }
9205 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
9206
9207 test_101f() {
9208         which iozone || skip_env "no iozone installed"
9209
9210         local old_debug=$($LCTL get_param debug)
9211         old_debug=${old_debug#*=}
9212         $LCTL set_param debug="reada mmap"
9213
9214         # create a test file
9215         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
9216
9217         echo Cancel LRU locks on lustre client to flush the client cache
9218         cancel_lru_locks osc
9219
9220         echo Reset readahead stats
9221         $LCTL set_param -n llite.*.read_ahead_stats 0
9222
9223         echo mmap read the file with small block size
9224         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
9225                 > /dev/null 2>&1
9226
9227         echo checking missing pages
9228         $LCTL get_param llite.*.read_ahead_stats
9229         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9230                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9231
9232         $LCTL set_param debug="$old_debug"
9233         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
9234         rm -f $DIR/$tfile
9235 }
9236 run_test 101f "check mmap read performance"
9237
9238 test_101g_brw_size_test() {
9239         local mb=$1
9240         local pages=$((mb * 1048576 / PAGE_SIZE))
9241         local file=$DIR/$tfile
9242
9243         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
9244                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
9245         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
9246                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
9247                         return 2
9248         done
9249
9250         stack_trap "rm -f $file" EXIT
9251         $LCTL set_param -n osc.*.rpc_stats=0
9252
9253         # 10 RPCs should be enough for the test
9254         local count=10
9255         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
9256                 { error "dd write ${mb} MB blocks failed"; return 3; }
9257         cancel_lru_locks osc
9258         dd of=/dev/null if=$file bs=${mb}M count=$count ||
9259                 { error "dd write ${mb} MB blocks failed"; return 4; }
9260
9261         # calculate number of full-sized read and write RPCs
9262         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
9263                 sed -n '/pages per rpc/,/^$/p' |
9264                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
9265                 END { print reads,writes }'))
9266         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
9267                 return 5
9268         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
9269                 return 6
9270
9271         return 0
9272 }
9273
9274 test_101g() {
9275         remote_ost_nodsh && skip "remote OST with nodsh"
9276
9277         local rpcs
9278         local osts=$(get_facets OST)
9279         local list=$(comma_list $(osts_nodes))
9280         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
9281         local brw_size="obdfilter.*.brw_size"
9282
9283         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9284
9285         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
9286
9287         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
9288                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
9289                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
9290            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
9291                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
9292                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
9293
9294                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
9295                         suffix="M"
9296
9297                 if [[ $orig_mb -lt 16 ]]; then
9298                         save_lustre_params $osts "$brw_size" > $p
9299                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
9300                                 error "set 16MB RPC size failed"
9301
9302                         echo "remount client to enable new RPC size"
9303                         remount_client $MOUNT || error "remount_client failed"
9304                 fi
9305
9306                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
9307                 # should be able to set brw_size=12, but no rpc_stats for that
9308                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
9309         fi
9310
9311         test_101g_brw_size_test 4 || error "4MB RPC test failed"
9312
9313         if [[ $orig_mb -lt 16 ]]; then
9314                 restore_lustre_params < $p
9315                 remount_client $MOUNT || error "remount_client restore failed"
9316         fi
9317
9318         rm -f $p $DIR/$tfile
9319 }
9320 run_test 101g "Big bulk(4/16 MiB) readahead"
9321
9322 test_101h() {
9323         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9324
9325         dd if=/dev/zero of=$DIR/$tfile bs=1M count=70 ||
9326                 error "dd 70M file failed"
9327         echo Cancel LRU locks on lustre client to flush the client cache
9328         cancel_lru_locks osc
9329
9330         echo "Reset readahead stats"
9331         $LCTL set_param -n llite.*.read_ahead_stats 0
9332
9333         echo "Read 10M of data but cross 64M bundary"
9334         dd if=$DIR/$tfile of=/dev/null bs=10M skip=6 count=1
9335         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9336                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9337         [ $miss -eq 1 ] || error "expected miss 1 but got $miss"
9338         rm -f $p $DIR/$tfile
9339 }
9340 run_test 101h "Readahead should cover current read window"
9341
9342 test_101i() {
9343         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 ||
9344                 error "dd 10M file failed"
9345
9346         local max_per_file_mb=$($LCTL get_param -n \
9347                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
9348         cancel_lru_locks osc
9349         stack_trap "$LCTL set_param llite.*.max_read_ahead_per_file_mb=$max_per_file_mb"
9350         $LCTL set_param llite.*.max_read_ahead_per_file_mb=1 ||
9351                 error "set max_read_ahead_per_file_mb to 1 failed"
9352
9353         echo "Reset readahead stats"
9354         $LCTL set_param llite.*.read_ahead_stats=0
9355
9356         dd if=$DIR/$tfile of=/dev/null bs=2M
9357
9358         $LCTL get_param llite.*.read_ahead_stats
9359         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9360                      awk '/misses/ { print $2 }')
9361         [ $miss -eq 5 ] || error "expected misses 5 but got $miss"
9362         rm -f $DIR/$tfile
9363 }
9364 run_test 101i "allow current readahead to exceed reservation"
9365
9366 test_101j() {
9367         $LFS setstripe -i 0 -c 1 $DIR/$tfile ||
9368                 error "setstripe $DIR/$tfile failed"
9369         local file_size=$((1048576 * 16))
9370         local old_ra=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
9371         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_ra" EXIT
9372
9373         echo Disable read-ahead
9374         $LCTL set_param -n llite.*.max_read_ahead_mb=0
9375
9376         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$(($file_size / 1048576))
9377         for blk in $PAGE_SIZE 1048576 $file_size; do
9378                 cancel_lru_locks osc
9379                 echo "Reset readahead stats"
9380                 $LCTL set_param -n llite.*.read_ahead_stats=0
9381                 local count=$(($file_size / $blk))
9382                 dd if=$DIR/$tfile bs=$blk count=$count of=/dev/null
9383                 local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9384                              get_named_value 'failed to fast read' |
9385                              cut -d" " -f1 | calc_total)
9386                 $LCTL get_param -n llite.*.read_ahead_stats
9387                 [ $miss -eq $count ] || error "expected $count got $miss"
9388         done
9389
9390         rm -f $p $DIR/$tfile
9391 }
9392 run_test 101j "A complete read block should be submitted when no RA"
9393
9394 setup_test102() {
9395         test_mkdir $DIR/$tdir
9396         chown $RUNAS_ID $DIR/$tdir
9397         STRIPE_SIZE=65536
9398         STRIPE_OFFSET=1
9399         STRIPE_COUNT=$OSTCOUNT
9400         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
9401
9402         trap cleanup_test102 EXIT
9403         cd $DIR
9404         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
9405         cd $DIR/$tdir
9406         for num in 1 2 3 4; do
9407                 for count in $(seq 1 $STRIPE_COUNT); do
9408                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
9409                                 local size=`expr $STRIPE_SIZE \* $num`
9410                                 local file=file"$num-$idx-$count"
9411                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
9412                         done
9413                 done
9414         done
9415
9416         cd $DIR
9417         $1 tar cf $TMP/f102.tar $tdir --xattrs
9418 }
9419
9420 cleanup_test102() {
9421         trap 0
9422         rm -f $TMP/f102.tar
9423         rm -rf $DIR/d0.sanity/d102
9424 }
9425
9426 test_102a() {
9427         [ "$UID" != 0 ] && skip "must run as root"
9428         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
9429                 skip_env "must have user_xattr"
9430
9431         [ -z "$(which setfattr 2>/dev/null)" ] &&
9432                 skip_env "could not find setfattr"
9433
9434         local testfile=$DIR/$tfile
9435
9436         touch $testfile
9437         echo "set/get xattr..."
9438         setfattr -n trusted.name1 -v value1 $testfile ||
9439                 error "setfattr -n trusted.name1=value1 $testfile failed"
9440         getfattr -n trusted.name1 $testfile 2> /dev/null |
9441           grep "trusted.name1=.value1" ||
9442                 error "$testfile missing trusted.name1=value1"
9443
9444         setfattr -n user.author1 -v author1 $testfile ||
9445                 error "setfattr -n user.author1=author1 $testfile failed"
9446         getfattr -n user.author1 $testfile 2> /dev/null |
9447           grep "user.author1=.author1" ||
9448                 error "$testfile missing trusted.author1=author1"
9449
9450         echo "listxattr..."
9451         setfattr -n trusted.name2 -v value2 $testfile ||
9452                 error "$testfile unable to set trusted.name2"
9453         setfattr -n trusted.name3 -v value3 $testfile ||
9454                 error "$testfile unable to set trusted.name3"
9455         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
9456             grep "trusted.name" | wc -l) -eq 3 ] ||
9457                 error "$testfile missing 3 trusted.name xattrs"
9458
9459         setfattr -n user.author2 -v author2 $testfile ||
9460                 error "$testfile unable to set user.author2"
9461         setfattr -n user.author3 -v author3 $testfile ||
9462                 error "$testfile unable to set user.author3"
9463         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
9464             grep "user.author" | wc -l) -eq 3 ] ||
9465                 error "$testfile missing 3 user.author xattrs"
9466
9467         echo "remove xattr..."
9468         setfattr -x trusted.name1 $testfile ||
9469                 error "$testfile error deleting trusted.name1"
9470         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
9471                 error "$testfile did not delete trusted.name1 xattr"
9472
9473         setfattr -x user.author1 $testfile ||
9474                 error "$testfile error deleting user.author1"
9475         echo "set lustre special xattr ..."
9476         $LFS setstripe -c1 $testfile
9477         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
9478                 awk -F "=" '/trusted.lov/ { print $2 }' )
9479         setfattr -n "trusted.lov" -v $lovea $testfile ||
9480                 error "$testfile doesn't ignore setting trusted.lov again"
9481         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
9482                 error "$testfile allow setting invalid trusted.lov"
9483         rm -f $testfile
9484 }
9485 run_test 102a "user xattr test =================================="
9486
9487 test_102b() {
9488         [ -z "$(which setfattr 2>/dev/null)" ] &&
9489                 skip_env "could not find setfattr"
9490         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9491
9492         # b10930: get/set/list trusted.lov xattr
9493         echo "get/set/list trusted.lov xattr ..."
9494         local testfile=$DIR/$tfile
9495         $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
9496                 error "setstripe failed"
9497         local STRIPECOUNT=$($LFS getstripe -c $testfile) ||
9498                 error "getstripe failed"
9499         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
9500                 error "can't get trusted.lov from $testfile"
9501
9502         local testfile2=${testfile}2
9503         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
9504                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
9505
9506         $MCREATE $testfile2
9507         setfattr -n trusted.lov -v $value $testfile2
9508         local stripe_size=$($LFS getstripe -S $testfile2)
9509         local stripe_count=$($LFS getstripe -c $testfile2)
9510         [[ $stripe_size -eq 65536 ]] ||
9511                 error "stripe size $stripe_size != 65536"
9512         [[ $stripe_count -eq $STRIPECOUNT ]] ||
9513                 error "stripe count $stripe_count != $STRIPECOUNT"
9514         rm -f $DIR/$tfile
9515 }
9516 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
9517
9518 test_102c() {
9519         [ -z "$(which setfattr 2>/dev/null)" ] &&
9520                 skip_env "could not find setfattr"
9521         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9522
9523         # b10930: get/set/list lustre.lov xattr
9524         echo "get/set/list lustre.lov xattr ..."
9525         test_mkdir $DIR/$tdir
9526         chown $RUNAS_ID $DIR/$tdir
9527         local testfile=$DIR/$tdir/$tfile
9528         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
9529                 error "setstripe failed"
9530         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
9531                 error "getstripe failed"
9532         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
9533         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
9534
9535         local testfile2=${testfile}2
9536         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
9537                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
9538
9539         $RUNAS $MCREATE $testfile2
9540         $RUNAS setfattr -n lustre.lov -v $value $testfile2
9541         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
9542         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
9543         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
9544         [ $stripe_count -eq $STRIPECOUNT ] ||
9545                 error "stripe count $stripe_count != $STRIPECOUNT"
9546 }
9547 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
9548
9549 compare_stripe_info1() {
9550         local stripe_index_all_zero=true
9551
9552         for num in 1 2 3 4; do
9553                 for count in $(seq 1 $STRIPE_COUNT); do
9554                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
9555                                 local size=$((STRIPE_SIZE * num))
9556                                 local file=file"$num-$offset-$count"
9557                                 stripe_size=$($LFS getstripe -S $PWD/$file)
9558                                 [[ $stripe_size -ne $size ]] &&
9559                                     error "$file: size $stripe_size != $size"
9560                                 stripe_count=$($LFS getstripe -c $PWD/$file)
9561                                 # allow fewer stripes to be created, ORI-601
9562                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
9563                                     error "$file: count $stripe_count != $count"
9564                                 stripe_index=$($LFS getstripe -i $PWD/$file)
9565                                 [[ $stripe_index -ne 0 ]] &&
9566                                         stripe_index_all_zero=false
9567                         done
9568                 done
9569         done
9570         $stripe_index_all_zero &&
9571                 error "all files are being extracted starting from OST index 0"
9572         return 0
9573 }
9574
9575 have_xattrs_include() {
9576         tar --help | grep -q xattrs-include &&
9577                 echo --xattrs-include="lustre.*"
9578 }
9579
9580 test_102d() {
9581         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9582         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9583
9584         XINC=$(have_xattrs_include)
9585         setup_test102
9586         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9587         cd $DIR/$tdir/$tdir
9588         compare_stripe_info1
9589 }
9590 run_test 102d "tar restore stripe info from tarfile,not keep osts"
9591
9592 test_102f() {
9593         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9594         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9595
9596         XINC=$(have_xattrs_include)
9597         setup_test102
9598         test_mkdir $DIR/$tdir.restore
9599         cd $DIR
9600         tar cf - --xattrs $tdir | tar xf - \
9601                 -C $DIR/$tdir.restore --xattrs $XINC
9602         cd $DIR/$tdir.restore/$tdir
9603         compare_stripe_info1
9604 }
9605 run_test 102f "tar copy files, not keep osts"
9606
9607 grow_xattr() {
9608         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
9609                 skip "must have user_xattr"
9610         [ -z "$(which setfattr 2>/dev/null)" ] &&
9611                 skip_env "could not find setfattr"
9612         [ -z "$(which getfattr 2>/dev/null)" ] &&
9613                 skip_env "could not find getfattr"
9614
9615         local xsize=${1:-1024}  # in bytes
9616         local file=$DIR/$tfile
9617         local value="$(generate_string $xsize)"
9618         local xbig=trusted.big
9619         local toobig=$2
9620
9621         touch $file
9622         log "save $xbig on $file"
9623         if [ -z "$toobig" ]
9624         then
9625                 setfattr -n $xbig -v $value $file ||
9626                         error "saving $xbig on $file failed"
9627         else
9628                 setfattr -n $xbig -v $value $file &&
9629                         error "saving $xbig on $file succeeded"
9630                 return 0
9631         fi
9632
9633         local orig=$(get_xattr_value $xbig $file)
9634         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
9635
9636         local xsml=trusted.sml
9637         log "save $xsml on $file"
9638         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
9639
9640         local new=$(get_xattr_value $xbig $file)
9641         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
9642
9643         log "grow $xsml on $file"
9644         setfattr -n $xsml -v "$value" $file ||
9645                 error "growing $xsml on $file failed"
9646
9647         new=$(get_xattr_value $xbig $file)
9648         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
9649         log "$xbig still valid after growing $xsml"
9650
9651         rm -f $file
9652 }
9653
9654 test_102h() { # bug 15777
9655         grow_xattr 1024
9656 }
9657 run_test 102h "grow xattr from inside inode to external block"
9658
9659 test_102ha() {
9660         large_xattr_enabled || skip_env "ea_inode feature disabled"
9661
9662         echo "setting xattr of max xattr size: $(max_xattr_size)"
9663         grow_xattr $(max_xattr_size)
9664
9665         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
9666         echo "This should fail:"
9667         grow_xattr $(($(max_xattr_size) + 10)) 1
9668 }
9669 run_test 102ha "grow xattr from inside inode to external inode"
9670
9671 test_102i() { # bug 17038
9672         [ -z "$(which getfattr 2>/dev/null)" ] &&
9673                 skip "could not find getfattr"
9674
9675         touch $DIR/$tfile
9676         ln -s $DIR/$tfile $DIR/${tfile}link
9677         getfattr -n trusted.lov $DIR/$tfile ||
9678                 error "lgetxattr on $DIR/$tfile failed"
9679         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
9680                 grep -i "no such attr" ||
9681                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
9682         rm -f $DIR/$tfile $DIR/${tfile}link
9683 }
9684 run_test 102i "lgetxattr test on symbolic link ============"
9685
9686 test_102j() {
9687         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9688         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9689
9690         XINC=$(have_xattrs_include)
9691         setup_test102 "$RUNAS"
9692         chown $RUNAS_ID $DIR/$tdir
9693         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9694         cd $DIR/$tdir/$tdir
9695         compare_stripe_info1 "$RUNAS"
9696 }
9697 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
9698
9699 test_102k() {
9700         [ -z "$(which setfattr 2>/dev/null)" ] &&
9701                 skip "could not find setfattr"
9702
9703         touch $DIR/$tfile
9704         # b22187 just check that does not crash for regular file.
9705         setfattr -n trusted.lov $DIR/$tfile
9706         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
9707         local test_kdir=$DIR/$tdir
9708         test_mkdir $test_kdir
9709         local default_size=$($LFS getstripe -S $test_kdir)
9710         local default_count=$($LFS getstripe -c $test_kdir)
9711         local default_offset=$($LFS getstripe -i $test_kdir)
9712         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
9713                 error 'dir setstripe failed'
9714         setfattr -n trusted.lov $test_kdir
9715         local stripe_size=$($LFS getstripe -S $test_kdir)
9716         local stripe_count=$($LFS getstripe -c $test_kdir)
9717         local stripe_offset=$($LFS getstripe -i $test_kdir)
9718         [ $stripe_size -eq $default_size ] ||
9719                 error "stripe size $stripe_size != $default_size"
9720         [ $stripe_count -eq $default_count ] ||
9721                 error "stripe count $stripe_count != $default_count"
9722         [ $stripe_offset -eq $default_offset ] ||
9723                 error "stripe offset $stripe_offset != $default_offset"
9724         rm -rf $DIR/$tfile $test_kdir
9725 }
9726 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
9727
9728 test_102l() {
9729         [ -z "$(which getfattr 2>/dev/null)" ] &&
9730                 skip "could not find getfattr"
9731
9732         # LU-532 trusted. xattr is invisible to non-root
9733         local testfile=$DIR/$tfile
9734
9735         touch $testfile
9736
9737         echo "listxattr as user..."
9738         chown $RUNAS_ID $testfile
9739         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
9740             grep -q "trusted" &&
9741                 error "$testfile trusted xattrs are user visible"
9742
9743         return 0;
9744 }
9745 run_test 102l "listxattr size test =================================="
9746
9747 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
9748         local path=$DIR/$tfile
9749         touch $path
9750
9751         listxattr_size_check $path || error "listattr_size_check $path failed"
9752 }
9753 run_test 102m "Ensure listxattr fails on small bufffer ========"
9754
9755 cleanup_test102
9756
9757 getxattr() { # getxattr path name
9758         # Return the base64 encoding of the value of xattr name on path.
9759         local path=$1
9760         local name=$2
9761
9762         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
9763         # file: $path
9764         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9765         #
9766         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9767
9768         getfattr --absolute-names --encoding=base64 --name=$name $path |
9769                 awk -F= -v name=$name '$1 == name {
9770                         print substr($0, index($0, "=") + 1);
9771         }'
9772 }
9773
9774 test_102n() { # LU-4101 mdt: protect internal xattrs
9775         [ -z "$(which setfattr 2>/dev/null)" ] &&
9776                 skip "could not find setfattr"
9777         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
9778         then
9779                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
9780         fi
9781
9782         local file0=$DIR/$tfile.0
9783         local file1=$DIR/$tfile.1
9784         local xattr0=$TMP/$tfile.0
9785         local xattr1=$TMP/$tfile.1
9786         local namelist="lov lma lmv link fid version som hsm"
9787         local name
9788         local value
9789
9790         rm -rf $file0 $file1 $xattr0 $xattr1
9791         touch $file0 $file1
9792
9793         # Get 'before' xattrs of $file1.
9794         getfattr --absolute-names --dump --match=- $file1 > $xattr0
9795
9796         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
9797                 namelist+=" lfsck_namespace"
9798         for name in $namelist; do
9799                 # Try to copy xattr from $file0 to $file1.
9800                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9801
9802                 setfattr --name=trusted.$name --value="$value" $file1 ||
9803                         error "setxattr 'trusted.$name' failed"
9804
9805                 # Try to set a garbage xattr.
9806                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9807
9808                 if [[ x$name == "xlov" ]]; then
9809                         setfattr --name=trusted.lov --value="$value" $file1 &&
9810                         error "setxattr invalid 'trusted.lov' success"
9811                 else
9812                         setfattr --name=trusted.$name --value="$value" $file1 ||
9813                                 error "setxattr invalid 'trusted.$name' failed"
9814                 fi
9815
9816                 # Try to remove the xattr from $file1. We don't care if this
9817                 # appears to succeed or fail, we just don't want there to be
9818                 # any changes or crashes.
9819                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9820         done
9821
9822         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
9823         then
9824                 name="lfsck_ns"
9825                 # Try to copy xattr from $file0 to $file1.
9826                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9827
9828                 setfattr --name=trusted.$name --value="$value" $file1 ||
9829                         error "setxattr 'trusted.$name' failed"
9830
9831                 # Try to set a garbage xattr.
9832                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9833
9834                 setfattr --name=trusted.$name --value="$value" $file1 ||
9835                         error "setxattr 'trusted.$name' failed"
9836
9837                 # Try to remove the xattr from $file1. We don't care if this
9838                 # appears to succeed or fail, we just don't want there to be
9839                 # any changes or crashes.
9840                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9841         fi
9842
9843         # Get 'after' xattrs of file1.
9844         getfattr --absolute-names --dump --match=- $file1 > $xattr1
9845
9846         if ! diff $xattr0 $xattr1; then
9847                 error "before and after xattrs of '$file1' differ"
9848         fi
9849
9850         rm -rf $file0 $file1 $xattr0 $xattr1
9851
9852         return 0
9853 }
9854 run_test 102n "silently ignore setxattr on internal trusted xattrs"
9855
9856 test_102p() { # LU-4703 setxattr did not check ownership
9857         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
9858                 skip "MDS needs to be at least 2.5.56"
9859
9860         local testfile=$DIR/$tfile
9861
9862         touch $testfile
9863
9864         echo "setfacl as user..."
9865         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
9866         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
9867
9868         echo "setfattr as user..."
9869         setfacl -m "u:$RUNAS_ID:---" $testfile
9870         $RUNAS setfattr -x system.posix_acl_access $testfile
9871         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
9872 }
9873 run_test 102p "check setxattr(2) correctly fails without permission"
9874
9875 test_102q() {
9876         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
9877                 skip "MDS needs to be at least 2.6.92"
9878
9879         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
9880 }
9881 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
9882
9883 test_102r() {
9884         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
9885                 skip "MDS needs to be at least 2.6.93"
9886
9887         touch $DIR/$tfile || error "touch"
9888         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
9889         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
9890         rm $DIR/$tfile || error "rm"
9891
9892         #normal directory
9893         mkdir -p $DIR/$tdir || error "mkdir"
9894         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9895         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9896         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9897                 error "$testfile error deleting user.author1"
9898         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9899                 grep "user.$(basename $tdir)" &&
9900                 error "$tdir did not delete user.$(basename $tdir)"
9901         rmdir $DIR/$tdir || error "rmdir"
9902
9903         #striped directory
9904         test_mkdir $DIR/$tdir
9905         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9906         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9907         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9908                 error "$testfile error deleting user.author1"
9909         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9910                 grep "user.$(basename $tdir)" &&
9911                 error "$tdir did not delete user.$(basename $tdir)"
9912         rmdir $DIR/$tdir || error "rm striped dir"
9913 }
9914 run_test 102r "set EAs with empty values"
9915
9916 test_102s() {
9917         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9918                 skip "MDS needs to be at least 2.11.52"
9919
9920         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9921
9922         save_lustre_params client "llite.*.xattr_cache" > $save
9923
9924         for cache in 0 1; do
9925                 lctl set_param llite.*.xattr_cache=$cache
9926
9927                 rm -f $DIR/$tfile
9928                 touch $DIR/$tfile || error "touch"
9929                 for prefix in lustre security system trusted user; do
9930                         # Note getxattr() may fail with 'Operation not
9931                         # supported' or 'No such attribute' depending
9932                         # on prefix and cache.
9933                         getfattr -n $prefix.n102s $DIR/$tfile &&
9934                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
9935                 done
9936         done
9937
9938         restore_lustre_params < $save
9939 }
9940 run_test 102s "getting nonexistent xattrs should fail"
9941
9942 test_102t() {
9943         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9944                 skip "MDS needs to be at least 2.11.52"
9945
9946         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9947
9948         save_lustre_params client "llite.*.xattr_cache" > $save
9949
9950         for cache in 0 1; do
9951                 lctl set_param llite.*.xattr_cache=$cache
9952
9953                 for buf_size in 0 256; do
9954                         rm -f $DIR/$tfile
9955                         touch $DIR/$tfile || error "touch"
9956                         setfattr -n user.multiop $DIR/$tfile
9957                         $MULTIOP $DIR/$tfile oa$buf_size ||
9958                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
9959                 done
9960         done
9961
9962         restore_lustre_params < $save
9963 }
9964 run_test 102t "zero length xattr values handled correctly"
9965
9966 run_acl_subtest()
9967 {
9968     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
9969     return $?
9970 }
9971
9972 test_103a() {
9973         [ "$UID" != 0 ] && skip "must run as root"
9974         $GSS && skip_env "could not run under gss"
9975         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
9976                 skip_env "must have acl enabled"
9977         [ -z "$(which setfacl 2>/dev/null)" ] &&
9978                 skip_env "could not find setfacl"
9979         remote_mds_nodsh && skip "remote MDS with nodsh"
9980
9981         gpasswd -a daemon bin                           # LU-5641
9982         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
9983
9984         declare -a identity_old
9985
9986         for num in $(seq $MDSCOUNT); do
9987                 switch_identity $num true || identity_old[$num]=$?
9988         done
9989
9990         SAVE_UMASK=$(umask)
9991         umask 0022
9992         mkdir -p $DIR/$tdir
9993         cd $DIR/$tdir
9994
9995         echo "performing cp ..."
9996         run_acl_subtest cp || error "run_acl_subtest cp failed"
9997         echo "performing getfacl-noacl..."
9998         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
9999         echo "performing misc..."
10000         run_acl_subtest misc || error  "misc test failed"
10001         echo "performing permissions..."
10002         run_acl_subtest permissions || error "permissions failed"
10003         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
10004         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
10005                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
10006                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
10007         then
10008                 echo "performing permissions xattr..."
10009                 run_acl_subtest permissions_xattr ||
10010                         error "permissions_xattr failed"
10011         fi
10012         echo "performing setfacl..."
10013         run_acl_subtest setfacl || error  "setfacl test failed"
10014
10015         # inheritance test got from HP
10016         echo "performing inheritance..."
10017         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
10018         chmod +x make-tree || error "chmod +x failed"
10019         run_acl_subtest inheritance || error "inheritance test failed"
10020         rm -f make-tree
10021
10022         echo "LU-974 ignore umask when acl is enabled..."
10023         run_acl_subtest 974 || error "LU-974 umask test failed"
10024         if [ $MDSCOUNT -ge 2 ]; then
10025                 run_acl_subtest 974_remote ||
10026                         error "LU-974 umask test failed under remote dir"
10027         fi
10028
10029         echo "LU-2561 newly created file is same size as directory..."
10030         if [ "$mds1_FSTYPE" != "zfs" ]; then
10031                 run_acl_subtest 2561 || error "LU-2561 test failed"
10032         else
10033                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
10034         fi
10035
10036         run_acl_subtest 4924 || error "LU-4924 test failed"
10037
10038         cd $SAVE_PWD
10039         umask $SAVE_UMASK
10040
10041         for num in $(seq $MDSCOUNT); do
10042                 if [ "${identity_old[$num]}" = 1 ]; then
10043                         switch_identity $num false || identity_old[$num]=$?
10044                 fi
10045         done
10046 }
10047 run_test 103a "acl test"
10048
10049 test_103b() {
10050         declare -a pids
10051         local U
10052
10053         for U in {0..511}; do
10054                 {
10055                 local O=$(printf "%04o" $U)
10056
10057                 umask $(printf "%04o" $((511 ^ $O)))
10058                 $LFS setstripe -c 1 $DIR/$tfile.s$O
10059                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
10060
10061                 (( $S == ($O & 0666) )) ||
10062                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
10063
10064                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
10065                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
10066                 (( $S == ($O & 0666) )) ||
10067                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
10068
10069                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
10070                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
10071                 (( $S == ($O & 0666) )) ||
10072                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
10073                 rm -f $DIR/$tfile.[smp]$0
10074                 } &
10075                 local pid=$!
10076
10077                 # limit the concurrently running threads to 64. LU-11878
10078                 local idx=$((U % 64))
10079                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
10080                 pids[idx]=$pid
10081         done
10082         wait
10083 }
10084 run_test 103b "umask lfs setstripe"
10085
10086 test_103c() {
10087         mkdir -p $DIR/$tdir
10088         cp -rp $DIR/$tdir $DIR/$tdir.bak
10089
10090         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
10091                 error "$DIR/$tdir shouldn't contain default ACL"
10092         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
10093                 error "$DIR/$tdir.bak shouldn't contain default ACL"
10094         true
10095 }
10096 run_test 103c "'cp -rp' won't set empty acl"
10097
10098 test_104a() {
10099         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10100
10101         touch $DIR/$tfile
10102         lfs df || error "lfs df failed"
10103         lfs df -ih || error "lfs df -ih failed"
10104         lfs df -h $DIR || error "lfs df -h $DIR failed"
10105         lfs df -i $DIR || error "lfs df -i $DIR failed"
10106         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
10107         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
10108
10109         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
10110         lctl --device %$OSC deactivate
10111         lfs df || error "lfs df with deactivated OSC failed"
10112         lctl --device %$OSC activate
10113         # wait the osc back to normal
10114         wait_osc_import_ready client ost
10115
10116         lfs df || error "lfs df with reactivated OSC failed"
10117         rm -f $DIR/$tfile
10118 }
10119 run_test 104a "lfs df [-ih] [path] test ========================="
10120
10121 test_104b() {
10122         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10123         [ $RUNAS_ID -eq $UID ] &&
10124                 skip_env "RUNAS_ID = UID = $UID -- skipping"
10125
10126         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
10127                         grep "Permission denied" | wc -l)))
10128         if [ $denied_cnt -ne 0 ]; then
10129                 error "lfs check servers test failed"
10130         fi
10131 }
10132 run_test 104b "$RUNAS lfs check servers test ===================="
10133
10134 test_105a() {
10135         # doesn't work on 2.4 kernels
10136         touch $DIR/$tfile
10137         if $(flock_is_enabled); then
10138                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
10139         else
10140                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
10141         fi
10142         rm -f $DIR/$tfile
10143 }
10144 run_test 105a "flock when mounted without -o flock test ========"
10145
10146 test_105b() {
10147         touch $DIR/$tfile
10148         if $(flock_is_enabled); then
10149                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
10150         else
10151                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
10152         fi
10153         rm -f $DIR/$tfile
10154 }
10155 run_test 105b "fcntl when mounted without -o flock test ========"
10156
10157 test_105c() {
10158         touch $DIR/$tfile
10159         if $(flock_is_enabled); then
10160                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
10161         else
10162                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
10163         fi
10164         rm -f $DIR/$tfile
10165 }
10166 run_test 105c "lockf when mounted without -o flock test"
10167
10168 test_105d() { # bug 15924
10169         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10170
10171         test_mkdir $DIR/$tdir
10172         flock_is_enabled || skip_env "mount w/o flock enabled"
10173         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
10174         $LCTL set_param fail_loc=0x80000315
10175         flocks_test 2 $DIR/$tdir
10176 }
10177 run_test 105d "flock race (should not freeze) ========"
10178
10179 test_105e() { # bug 22660 && 22040
10180         flock_is_enabled || skip_env "mount w/o flock enabled"
10181
10182         touch $DIR/$tfile
10183         flocks_test 3 $DIR/$tfile
10184 }
10185 run_test 105e "Two conflicting flocks from same process"
10186
10187 test_106() { #bug 10921
10188         test_mkdir $DIR/$tdir
10189         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
10190         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
10191 }
10192 run_test 106 "attempt exec of dir followed by chown of that dir"
10193
10194 test_107() {
10195         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10196
10197         CDIR=`pwd`
10198         local file=core
10199
10200         cd $DIR
10201         rm -f $file
10202
10203         local save_pattern=$(sysctl -n kernel.core_pattern)
10204         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
10205         sysctl -w kernel.core_pattern=$file
10206         sysctl -w kernel.core_uses_pid=0
10207
10208         ulimit -c unlimited
10209         sleep 60 &
10210         SLEEPPID=$!
10211
10212         sleep 1
10213
10214         kill -s 11 $SLEEPPID
10215         wait $SLEEPPID
10216         if [ -e $file ]; then
10217                 size=`stat -c%s $file`
10218                 [ $size -eq 0 ] && error "Fail to create core file $file"
10219         else
10220                 error "Fail to create core file $file"
10221         fi
10222         rm -f $file
10223         sysctl -w kernel.core_pattern=$save_pattern
10224         sysctl -w kernel.core_uses_pid=$save_uses_pid
10225         cd $CDIR
10226 }
10227 run_test 107 "Coredump on SIG"
10228
10229 test_110() {
10230         test_mkdir $DIR/$tdir
10231         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
10232         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
10233                 error "mkdir with 256 char should fail, but did not"
10234         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
10235                 error "create with 255 char failed"
10236         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
10237                 error "create with 256 char should fail, but did not"
10238
10239         ls -l $DIR/$tdir
10240         rm -rf $DIR/$tdir
10241 }
10242 run_test 110 "filename length checking"
10243
10244 #
10245 # Purpose: To verify dynamic thread (OSS) creation.
10246 #
10247 test_115() {
10248         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10249         remote_ost_nodsh && skip "remote OST with nodsh"
10250
10251         # Lustre does not stop service threads once they are started.
10252         # Reset number of running threads to default.
10253         stopall
10254         setupall
10255
10256         local OSTIO_pre
10257         local save_params="$TMP/sanity-$TESTNAME.parameters"
10258
10259         # Get ll_ost_io count before I/O
10260         OSTIO_pre=$(do_facet ost1 \
10261                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
10262         # Exit if lustre is not running (ll_ost_io not running).
10263         [ -z "$OSTIO_pre" ] && error "no OSS threads"
10264
10265         echo "Starting with $OSTIO_pre threads"
10266         local thread_max=$((OSTIO_pre * 2))
10267         local rpc_in_flight=$((thread_max * 2))
10268         # Number of I/O Process proposed to be started.
10269         local nfiles
10270         local facets=$(get_facets OST)
10271
10272         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
10273         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
10274
10275         # Set in_flight to $rpc_in_flight
10276         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
10277                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
10278         nfiles=${rpc_in_flight}
10279         # Set ost thread_max to $thread_max
10280         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
10281
10282         # 5 Minutes should be sufficient for max number of OSS
10283         # threads(thread_max) to be created.
10284         local timeout=300
10285
10286         # Start I/O.
10287         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
10288         test_mkdir $DIR/$tdir
10289         for i in $(seq $nfiles); do
10290                 local file=$DIR/$tdir/${tfile}-$i
10291                 $LFS setstripe -c -1 -i 0 $file
10292                 ($WTL $file $timeout)&
10293         done
10294
10295         # I/O Started - Wait for thread_started to reach thread_max or report
10296         # error if thread_started is more than thread_max.
10297         echo "Waiting for thread_started to reach thread_max"
10298         local thread_started=0
10299         local end_time=$((SECONDS + timeout))
10300
10301         while [ $SECONDS -le $end_time ] ; do
10302                 echo -n "."
10303                 # Get ost i/o thread_started count.
10304                 thread_started=$(do_facet ost1 \
10305                         "$LCTL get_param \
10306                         ost.OSS.ost_io.threads_started | cut -d= -f2")
10307                 # Break out if thread_started is equal/greater than thread_max
10308                 if [[ $thread_started -ge $thread_max ]]; then
10309                         echo ll_ost_io thread_started $thread_started, \
10310                                 equal/greater than thread_max $thread_max
10311                         break
10312                 fi
10313                 sleep 1
10314         done
10315
10316         # Cleanup - We have the numbers, Kill i/o jobs if running.
10317         jobcount=($(jobs -p))
10318         for i in $(seq 0 $((${#jobcount[@]}-1)))
10319         do
10320                 kill -9 ${jobcount[$i]}
10321                 if [ $? -ne 0 ] ; then
10322                         echo Warning: \
10323                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
10324                 fi
10325         done
10326
10327         # Cleanup files left by WTL binary.
10328         for i in $(seq $nfiles); do
10329                 local file=$DIR/$tdir/${tfile}-$i
10330                 rm -rf $file
10331                 if [ $? -ne 0 ] ; then
10332                         echo "Warning: Failed to delete file $file"
10333                 fi
10334         done
10335
10336         restore_lustre_params <$save_params
10337         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
10338
10339         # Error out if no new thread has started or Thread started is greater
10340         # than thread max.
10341         if [[ $thread_started -le $OSTIO_pre ||
10342                         $thread_started -gt $thread_max ]]; then
10343                 error "ll_ost_io: thread_started $thread_started" \
10344                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
10345                       "No new thread started or thread started greater " \
10346                       "than thread_max."
10347         fi
10348 }
10349 run_test 115 "verify dynamic thread creation===================="
10350
10351 free_min_max () {
10352         wait_delete_completed
10353         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
10354         echo "OST kbytes available: ${AVAIL[@]}"
10355         MAXV=${AVAIL[0]}
10356         MAXI=0
10357         MINV=${AVAIL[0]}
10358         MINI=0
10359         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
10360                 #echo OST $i: ${AVAIL[i]}kb
10361                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
10362                         MAXV=${AVAIL[i]}
10363                         MAXI=$i
10364                 fi
10365                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
10366                         MINV=${AVAIL[i]}
10367                         MINI=$i
10368                 fi
10369         done
10370         echo "Min free space: OST $MINI: $MINV"
10371         echo "Max free space: OST $MAXI: $MAXV"
10372 }
10373
10374 test_116a() { # was previously test_116()
10375         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10376         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10377         remote_mds_nodsh && skip "remote MDS with nodsh"
10378
10379         echo -n "Free space priority "
10380         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
10381                 head -n1
10382         declare -a AVAIL
10383         free_min_max
10384
10385         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
10386         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
10387         trap simple_cleanup_common EXIT
10388
10389         # Check if we need to generate uneven OSTs
10390         test_mkdir -p $DIR/$tdir/OST${MINI}
10391         local FILL=$((MINV / 4))
10392         local DIFF=$((MAXV - MINV))
10393         local DIFF2=$((DIFF * 100 / MINV))
10394
10395         local threshold=$(do_facet $SINGLEMDS \
10396                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
10397         threshold=${threshold%%%}
10398         echo -n "Check for uneven OSTs: "
10399         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
10400
10401         if [[ $DIFF2 -gt $threshold ]]; then
10402                 echo "ok"
10403                 echo "Don't need to fill OST$MINI"
10404         else
10405                 # generate uneven OSTs. Write 2% over the QOS threshold value
10406                 echo "no"
10407                 DIFF=$((threshold - DIFF2 + 2))
10408                 DIFF2=$((MINV * DIFF / 100))
10409                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
10410                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
10411                         error "setstripe failed"
10412                 DIFF=$((DIFF2 / 2048))
10413                 i=0
10414                 while [ $i -lt $DIFF ]; do
10415                         i=$((i + 1))
10416                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
10417                                 bs=2M count=1 2>/dev/null
10418                         echo -n .
10419                 done
10420                 echo .
10421                 sync
10422                 sleep_maxage
10423                 free_min_max
10424         fi
10425
10426         DIFF=$((MAXV - MINV))
10427         DIFF2=$((DIFF * 100 / MINV))
10428         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
10429         if [ $DIFF2 -gt $threshold ]; then
10430                 echo "ok"
10431         else
10432                 echo "failed - QOS mode won't be used"
10433                 simple_cleanup_common
10434                 skip "QOS imbalance criteria not met"
10435         fi
10436
10437         MINI1=$MINI
10438         MINV1=$MINV
10439         MAXI1=$MAXI
10440         MAXV1=$MAXV
10441
10442         # now fill using QOS
10443         $LFS setstripe -c 1 $DIR/$tdir
10444         FILL=$((FILL / 200))
10445         if [ $FILL -gt 600 ]; then
10446                 FILL=600
10447         fi
10448         echo "writing $FILL files to QOS-assigned OSTs"
10449         i=0
10450         while [ $i -lt $FILL ]; do
10451                 i=$((i + 1))
10452                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
10453                         count=1 2>/dev/null
10454                 echo -n .
10455         done
10456         echo "wrote $i 200k files"
10457         sync
10458         sleep_maxage
10459
10460         echo "Note: free space may not be updated, so measurements might be off"
10461         free_min_max
10462         DIFF2=$((MAXV - MINV))
10463         echo "free space delta: orig $DIFF final $DIFF2"
10464         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
10465         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
10466         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
10467         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
10468         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
10469         if [[ $DIFF -gt 0 ]]; then
10470                 FILL=$((DIFF2 * 100 / DIFF - 100))
10471                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
10472         fi
10473
10474         # Figure out which files were written where
10475         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10476                awk '/'$MINI1': / {print $2; exit}')
10477         echo $UUID
10478         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10479         echo "$MINC files created on smaller OST $MINI1"
10480         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10481                awk '/'$MAXI1': / {print $2; exit}')
10482         echo $UUID
10483         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10484         echo "$MAXC files created on larger OST $MAXI1"
10485         if [[ $MINC -gt 0 ]]; then
10486                 FILL=$((MAXC * 100 / MINC - 100))
10487                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
10488         fi
10489         [[ $MAXC -gt $MINC ]] ||
10490                 error_ignore LU-9 "stripe QOS didn't balance free space"
10491         simple_cleanup_common
10492 }
10493 run_test 116a "stripe QOS: free space balance ==================="
10494
10495 test_116b() { # LU-2093
10496         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10497         remote_mds_nodsh && skip "remote MDS with nodsh"
10498
10499 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
10500         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
10501                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
10502         [ -z "$old_rr" ] && skip "no QOS"
10503         do_facet $SINGLEMDS lctl set_param \
10504                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
10505         mkdir -p $DIR/$tdir
10506         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
10507         createmany -o $DIR/$tdir/f- 20 || error "can't create"
10508         do_facet $SINGLEMDS lctl set_param fail_loc=0
10509         rm -rf $DIR/$tdir
10510         do_facet $SINGLEMDS lctl set_param \
10511                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
10512 }
10513 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
10514
10515 test_117() # bug 10891
10516 {
10517         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10518
10519         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
10520         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
10521         lctl set_param fail_loc=0x21e
10522         > $DIR/$tfile || error "truncate failed"
10523         lctl set_param fail_loc=0
10524         echo "Truncate succeeded."
10525         rm -f $DIR/$tfile
10526 }
10527 run_test 117 "verify osd extend =========="
10528
10529 NO_SLOW_RESENDCOUNT=4
10530 export OLD_RESENDCOUNT=""
10531 set_resend_count () {
10532         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
10533         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
10534         lctl set_param -n $PROC_RESENDCOUNT $1
10535         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
10536 }
10537
10538 # for reduce test_118* time (b=14842)
10539 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10540
10541 # Reset async IO behavior after error case
10542 reset_async() {
10543         FILE=$DIR/reset_async
10544
10545         # Ensure all OSCs are cleared
10546         $LFS setstripe -c -1 $FILE
10547         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
10548         sync
10549         rm $FILE
10550 }
10551
10552 test_118a() #bug 11710
10553 {
10554         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10555
10556         reset_async
10557
10558         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10559         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10560         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10561
10562         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10563                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10564                 return 1;
10565         fi
10566         rm -f $DIR/$tfile
10567 }
10568 run_test 118a "verify O_SYNC works =========="
10569
10570 test_118b()
10571 {
10572         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10573         remote_ost_nodsh && skip "remote OST with nodsh"
10574
10575         reset_async
10576
10577         #define OBD_FAIL_SRV_ENOENT 0x217
10578         set_nodes_failloc "$(osts_nodes)" 0x217
10579         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10580         RC=$?
10581         set_nodes_failloc "$(osts_nodes)" 0
10582         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10583         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10584                     grep -c writeback)
10585
10586         if [[ $RC -eq 0 ]]; then
10587                 error "Must return error due to dropped pages, rc=$RC"
10588                 return 1;
10589         fi
10590
10591         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10592                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10593                 return 1;
10594         fi
10595
10596         echo "Dirty pages not leaked on ENOENT"
10597
10598         # Due to the above error the OSC will issue all RPCs syncronously
10599         # until a subsequent RPC completes successfully without error.
10600         $MULTIOP $DIR/$tfile Ow4096yc
10601         rm -f $DIR/$tfile
10602
10603         return 0
10604 }
10605 run_test 118b "Reclaim dirty pages on fatal error =========="
10606
10607 test_118c()
10608 {
10609         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10610
10611         # for 118c, restore the original resend count, LU-1940
10612         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
10613                                 set_resend_count $OLD_RESENDCOUNT
10614         remote_ost_nodsh && skip "remote OST with nodsh"
10615
10616         reset_async
10617
10618         #define OBD_FAIL_OST_EROFS               0x216
10619         set_nodes_failloc "$(osts_nodes)" 0x216
10620
10621         # multiop should block due to fsync until pages are written
10622         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10623         MULTIPID=$!
10624         sleep 1
10625
10626         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10627                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10628         fi
10629
10630         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10631                     grep -c writeback)
10632         if [[ $WRITEBACK -eq 0 ]]; then
10633                 error "No page in writeback, writeback=$WRITEBACK"
10634         fi
10635
10636         set_nodes_failloc "$(osts_nodes)" 0
10637         wait $MULTIPID
10638         RC=$?
10639         if [[ $RC -ne 0 ]]; then
10640                 error "Multiop fsync failed, rc=$RC"
10641         fi
10642
10643         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10644         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10645                     grep -c writeback)
10646         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10647                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10648         fi
10649
10650         rm -f $DIR/$tfile
10651         echo "Dirty pages flushed via fsync on EROFS"
10652         return 0
10653 }
10654 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
10655
10656 # continue to use small resend count to reduce test_118* time (b=14842)
10657 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10658
10659 test_118d()
10660 {
10661         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10662         remote_ost_nodsh && skip "remote OST with nodsh"
10663
10664         reset_async
10665
10666         #define OBD_FAIL_OST_BRW_PAUSE_BULK
10667         set_nodes_failloc "$(osts_nodes)" 0x214
10668         # multiop should block due to fsync until pages are written
10669         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10670         MULTIPID=$!
10671         sleep 1
10672
10673         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10674                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10675         fi
10676
10677         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10678                     grep -c writeback)
10679         if [[ $WRITEBACK -eq 0 ]]; then
10680                 error "No page in writeback, writeback=$WRITEBACK"
10681         fi
10682
10683         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
10684         set_nodes_failloc "$(osts_nodes)" 0
10685
10686         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10687         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10688                     grep -c writeback)
10689         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10690                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10691         fi
10692
10693         rm -f $DIR/$tfile
10694         echo "Dirty pages gaurenteed flushed via fsync"
10695         return 0
10696 }
10697 run_test 118d "Fsync validation inject a delay of the bulk =========="
10698
10699 test_118f() {
10700         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10701
10702         reset_async
10703
10704         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
10705         lctl set_param fail_loc=0x8000040a
10706
10707         # Should simulate EINVAL error which is fatal
10708         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10709         RC=$?
10710         if [[ $RC -eq 0 ]]; then
10711                 error "Must return error due to dropped pages, rc=$RC"
10712         fi
10713
10714         lctl set_param fail_loc=0x0
10715
10716         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10717         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10718         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10719                     grep -c writeback)
10720         if [[ $LOCKED -ne 0 ]]; then
10721                 error "Locked pages remain in cache, locked=$LOCKED"
10722         fi
10723
10724         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10725                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10726         fi
10727
10728         rm -f $DIR/$tfile
10729         echo "No pages locked after fsync"
10730
10731         reset_async
10732         return 0
10733 }
10734 run_test 118f "Simulate unrecoverable OSC side error =========="
10735
10736 test_118g() {
10737         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10738
10739         reset_async
10740
10741         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
10742         lctl set_param fail_loc=0x406
10743
10744         # simulate local -ENOMEM
10745         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10746         RC=$?
10747
10748         lctl set_param fail_loc=0
10749         if [[ $RC -eq 0 ]]; then
10750                 error "Must return error due to dropped pages, rc=$RC"
10751         fi
10752
10753         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10754         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10755         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10756                         grep -c writeback)
10757         if [[ $LOCKED -ne 0 ]]; then
10758                 error "Locked pages remain in cache, locked=$LOCKED"
10759         fi
10760
10761         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10762                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10763         fi
10764
10765         rm -f $DIR/$tfile
10766         echo "No pages locked after fsync"
10767
10768         reset_async
10769         return 0
10770 }
10771 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
10772
10773 test_118h() {
10774         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10775         remote_ost_nodsh && skip "remote OST with nodsh"
10776
10777         reset_async
10778
10779         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10780         set_nodes_failloc "$(osts_nodes)" 0x20e
10781         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10782         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10783         RC=$?
10784
10785         set_nodes_failloc "$(osts_nodes)" 0
10786         if [[ $RC -eq 0 ]]; then
10787                 error "Must return error due to dropped pages, rc=$RC"
10788         fi
10789
10790         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10791         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10792         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10793                     grep -c writeback)
10794         if [[ $LOCKED -ne 0 ]]; then
10795                 error "Locked pages remain in cache, locked=$LOCKED"
10796         fi
10797
10798         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10799                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10800         fi
10801
10802         rm -f $DIR/$tfile
10803         echo "No pages locked after fsync"
10804
10805         return 0
10806 }
10807 run_test 118h "Verify timeout in handling recoverables errors  =========="
10808
10809 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10810
10811 test_118i() {
10812         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10813         remote_ost_nodsh && skip "remote OST with nodsh"
10814
10815         reset_async
10816
10817         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10818         set_nodes_failloc "$(osts_nodes)" 0x20e
10819
10820         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10821         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10822         PID=$!
10823         sleep 5
10824         set_nodes_failloc "$(osts_nodes)" 0
10825
10826         wait $PID
10827         RC=$?
10828         if [[ $RC -ne 0 ]]; then
10829                 error "got error, but should be not, rc=$RC"
10830         fi
10831
10832         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10833         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10834         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10835         if [[ $LOCKED -ne 0 ]]; then
10836                 error "Locked pages remain in cache, locked=$LOCKED"
10837         fi
10838
10839         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10840                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10841         fi
10842
10843         rm -f $DIR/$tfile
10844         echo "No pages locked after fsync"
10845
10846         return 0
10847 }
10848 run_test 118i "Fix error before timeout in recoverable error  =========="
10849
10850 [ "$SLOW" = "no" ] && set_resend_count 4
10851
10852 test_118j() {
10853         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10854         remote_ost_nodsh && skip "remote OST with nodsh"
10855
10856         reset_async
10857
10858         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
10859         set_nodes_failloc "$(osts_nodes)" 0x220
10860
10861         # return -EIO from OST
10862         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10863         RC=$?
10864         set_nodes_failloc "$(osts_nodes)" 0x0
10865         if [[ $RC -eq 0 ]]; then
10866                 error "Must return error due to dropped pages, rc=$RC"
10867         fi
10868
10869         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10870         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10871         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10872         if [[ $LOCKED -ne 0 ]]; then
10873                 error "Locked pages remain in cache, locked=$LOCKED"
10874         fi
10875
10876         # in recoverable error on OST we want resend and stay until it finished
10877         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10878                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10879         fi
10880
10881         rm -f $DIR/$tfile
10882         echo "No pages locked after fsync"
10883
10884         return 0
10885 }
10886 run_test 118j "Simulate unrecoverable OST side error =========="
10887
10888 test_118k()
10889 {
10890         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10891         remote_ost_nodsh && skip "remote OSTs with nodsh"
10892
10893         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10894         set_nodes_failloc "$(osts_nodes)" 0x20e
10895         test_mkdir $DIR/$tdir
10896
10897         for ((i=0;i<10;i++)); do
10898                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
10899                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
10900                 SLEEPPID=$!
10901                 sleep 0.500s
10902                 kill $SLEEPPID
10903                 wait $SLEEPPID
10904         done
10905
10906         set_nodes_failloc "$(osts_nodes)" 0
10907         rm -rf $DIR/$tdir
10908 }
10909 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
10910
10911 test_118l() # LU-646
10912 {
10913         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10914
10915         test_mkdir $DIR/$tdir
10916         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
10917         rm -rf $DIR/$tdir
10918 }
10919 run_test 118l "fsync dir"
10920
10921 test_118m() # LU-3066
10922 {
10923         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10924
10925         test_mkdir $DIR/$tdir
10926         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
10927         rm -rf $DIR/$tdir
10928 }
10929 run_test 118m "fdatasync dir ========="
10930
10931 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10932
10933 test_118n()
10934 {
10935         local begin
10936         local end
10937
10938         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10939         remote_ost_nodsh && skip "remote OSTs with nodsh"
10940
10941         # Sleep to avoid a cached response.
10942         #define OBD_STATFS_CACHE_SECONDS 1
10943         sleep 2
10944
10945         # Inject a 10 second delay in the OST_STATFS handler.
10946         #define OBD_FAIL_OST_STATFS_DELAY 0x242
10947         set_nodes_failloc "$(osts_nodes)" 0x242
10948
10949         begin=$SECONDS
10950         stat --file-system $MOUNT > /dev/null
10951         end=$SECONDS
10952
10953         set_nodes_failloc "$(osts_nodes)" 0
10954
10955         if ((end - begin > 20)); then
10956             error "statfs took $((end - begin)) seconds, expected 10"
10957         fi
10958 }
10959 run_test 118n "statfs() sends OST_STATFS requests in parallel"
10960
10961 test_119a() # bug 11737
10962 {
10963         BSIZE=$((512 * 1024))
10964         directio write $DIR/$tfile 0 1 $BSIZE
10965         # We ask to read two blocks, which is more than a file size.
10966         # directio will indicate an error when requested and actual
10967         # sizes aren't equeal (a normal situation in this case) and
10968         # print actual read amount.
10969         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
10970         if [ "$NOB" != "$BSIZE" ]; then
10971                 error "read $NOB bytes instead of $BSIZE"
10972         fi
10973         rm -f $DIR/$tfile
10974 }
10975 run_test 119a "Short directIO read must return actual read amount"
10976
10977 test_119b() # bug 11737
10978 {
10979         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10980
10981         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
10982         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
10983         sync
10984         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
10985                 error "direct read failed"
10986         rm -f $DIR/$tfile
10987 }
10988 run_test 119b "Sparse directIO read must return actual read amount"
10989
10990 test_119c() # bug 13099
10991 {
10992         BSIZE=1048576
10993         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
10994         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
10995         rm -f $DIR/$tfile
10996 }
10997 run_test 119c "Testing for direct read hitting hole"
10998
10999 test_119d() # bug 15950
11000 {
11001         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11002
11003         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
11004         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
11005         BSIZE=1048576
11006         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
11007         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
11008         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
11009         lctl set_param fail_loc=0x40d
11010         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
11011         pid_dio=$!
11012         sleep 1
11013         cat $DIR/$tfile > /dev/null &
11014         lctl set_param fail_loc=0
11015         pid_reads=$!
11016         wait $pid_dio
11017         log "the DIO writes have completed, now wait for the reads (should not block very long)"
11018         sleep 2
11019         [ -n "`ps h -p $pid_reads -o comm`" ] && \
11020         error "the read rpcs have not completed in 2s"
11021         rm -f $DIR/$tfile
11022         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
11023 }
11024 run_test 119d "The DIO path should try to send a new rpc once one is completed"
11025
11026 test_120a() {
11027         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11028         remote_mds_nodsh && skip "remote MDS with nodsh"
11029         test_mkdir -i0 -c1 $DIR/$tdir
11030         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11031                 skip_env "no early lock cancel on server"
11032
11033         lru_resize_disable mdc
11034         lru_resize_disable osc
11035         cancel_lru_locks mdc
11036         # asynchronous object destroy at MDT could cause bl ast to client
11037         cancel_lru_locks osc
11038
11039         stat $DIR/$tdir > /dev/null
11040         can1=$(do_facet mds1 \
11041                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11042                awk '/ldlm_cancel/ {print $2}')
11043         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11044                awk '/ldlm_bl_callback/ {print $2}')
11045         test_mkdir -i0 -c1 $DIR/$tdir/d1
11046         can2=$(do_facet mds1 \
11047                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11048                awk '/ldlm_cancel/ {print $2}')
11049         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11050                awk '/ldlm_bl_callback/ {print $2}')
11051         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11052         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11053         lru_resize_enable mdc
11054         lru_resize_enable osc
11055 }
11056 run_test 120a "Early Lock Cancel: mkdir test"
11057
11058 test_120b() {
11059         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11060         remote_mds_nodsh && skip "remote MDS with nodsh"
11061         test_mkdir $DIR/$tdir
11062         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11063                 skip_env "no early lock cancel on server"
11064
11065         lru_resize_disable mdc
11066         lru_resize_disable osc
11067         cancel_lru_locks mdc
11068         stat $DIR/$tdir > /dev/null
11069         can1=$(do_facet $SINGLEMDS \
11070                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11071                awk '/ldlm_cancel/ {print $2}')
11072         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11073                awk '/ldlm_bl_callback/ {print $2}')
11074         touch $DIR/$tdir/f1
11075         can2=$(do_facet $SINGLEMDS \
11076                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11077                awk '/ldlm_cancel/ {print $2}')
11078         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11079                awk '/ldlm_bl_callback/ {print $2}')
11080         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11081         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11082         lru_resize_enable mdc
11083         lru_resize_enable osc
11084 }
11085 run_test 120b "Early Lock Cancel: create test"
11086
11087 test_120c() {
11088         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11089         remote_mds_nodsh && skip "remote MDS with nodsh"
11090         test_mkdir -i0 -c1 $DIR/$tdir
11091         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11092                 skip "no early lock cancel on server"
11093
11094         lru_resize_disable mdc
11095         lru_resize_disable osc
11096         test_mkdir -i0 -c1 $DIR/$tdir/d1
11097         test_mkdir -i0 -c1 $DIR/$tdir/d2
11098         touch $DIR/$tdir/d1/f1
11099         cancel_lru_locks mdc
11100         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
11101         can1=$(do_facet mds1 \
11102                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11103                awk '/ldlm_cancel/ {print $2}')
11104         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11105                awk '/ldlm_bl_callback/ {print $2}')
11106         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
11107         can2=$(do_facet mds1 \
11108                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11109                awk '/ldlm_cancel/ {print $2}')
11110         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11111                awk '/ldlm_bl_callback/ {print $2}')
11112         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11113         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11114         lru_resize_enable mdc
11115         lru_resize_enable osc
11116 }
11117 run_test 120c "Early Lock Cancel: link test"
11118
11119 test_120d() {
11120         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11121         remote_mds_nodsh && skip "remote MDS with nodsh"
11122         test_mkdir -i0 -c1 $DIR/$tdir
11123         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11124                 skip_env "no early lock cancel on server"
11125
11126         lru_resize_disable mdc
11127         lru_resize_disable osc
11128         touch $DIR/$tdir
11129         cancel_lru_locks mdc
11130         stat $DIR/$tdir > /dev/null
11131         can1=$(do_facet mds1 \
11132                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11133                awk '/ldlm_cancel/ {print $2}')
11134         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11135                awk '/ldlm_bl_callback/ {print $2}')
11136         chmod a+x $DIR/$tdir
11137         can2=$(do_facet mds1 \
11138                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11139                awk '/ldlm_cancel/ {print $2}')
11140         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11141                awk '/ldlm_bl_callback/ {print $2}')
11142         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11143         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11144         lru_resize_enable mdc
11145         lru_resize_enable osc
11146 }
11147 run_test 120d "Early Lock Cancel: setattr test"
11148
11149 test_120e() {
11150         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11151         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11152                 skip_env "no early lock cancel on server"
11153         remote_mds_nodsh && skip "remote MDS with nodsh"
11154
11155         local dlmtrace_set=false
11156
11157         test_mkdir -i0 -c1 $DIR/$tdir
11158         lru_resize_disable mdc
11159         lru_resize_disable osc
11160         ! $LCTL get_param debug | grep -q dlmtrace &&
11161                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
11162         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
11163         cancel_lru_locks mdc
11164         cancel_lru_locks osc
11165         dd if=$DIR/$tdir/f1 of=/dev/null
11166         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
11167         # XXX client can not do early lock cancel of OST lock
11168         # during unlink (LU-4206), so cancel osc lock now.
11169         sleep 2
11170         cancel_lru_locks osc
11171         can1=$(do_facet mds1 \
11172                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11173                awk '/ldlm_cancel/ {print $2}')
11174         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11175                awk '/ldlm_bl_callback/ {print $2}')
11176         unlink $DIR/$tdir/f1
11177         sleep 5
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 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
11184                 $LCTL dk $TMP/cancel.debug.txt
11185         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
11186                 $LCTL dk $TMP/blocking.debug.txt
11187         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
11188         lru_resize_enable mdc
11189         lru_resize_enable osc
11190 }
11191 run_test 120e "Early Lock Cancel: unlink test"
11192
11193 test_120f() {
11194         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11195         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11196                 skip_env "no early lock cancel on server"
11197         remote_mds_nodsh && skip "remote MDS with nodsh"
11198
11199         test_mkdir -i0 -c1 $DIR/$tdir
11200         lru_resize_disable mdc
11201         lru_resize_disable osc
11202         test_mkdir -i0 -c1 $DIR/$tdir/d1
11203         test_mkdir -i0 -c1 $DIR/$tdir/d2
11204         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
11205         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
11206         cancel_lru_locks mdc
11207         cancel_lru_locks osc
11208         dd if=$DIR/$tdir/d1/f1 of=/dev/null
11209         dd if=$DIR/$tdir/d2/f2 of=/dev/null
11210         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
11211         # XXX client can not do early lock cancel of OST lock
11212         # during rename (LU-4206), so cancel osc lock now.
11213         sleep 2
11214         cancel_lru_locks osc
11215         can1=$(do_facet mds1 \
11216                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11217                awk '/ldlm_cancel/ {print $2}')
11218         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11219                awk '/ldlm_bl_callback/ {print $2}')
11220         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
11221         sleep 5
11222         can2=$(do_facet mds1 \
11223                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11224                awk '/ldlm_cancel/ {print $2}')
11225         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11226                awk '/ldlm_bl_callback/ {print $2}')
11227         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11228         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11229         lru_resize_enable mdc
11230         lru_resize_enable osc
11231 }
11232 run_test 120f "Early Lock Cancel: rename test"
11233
11234 test_120g() {
11235         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11236         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11237                 skip_env "no early lock cancel on server"
11238         remote_mds_nodsh && skip "remote MDS with nodsh"
11239
11240         lru_resize_disable mdc
11241         lru_resize_disable osc
11242         count=10000
11243         echo create $count files
11244         test_mkdir $DIR/$tdir
11245         cancel_lru_locks mdc
11246         cancel_lru_locks osc
11247         t0=$(date +%s)
11248
11249         can0=$(do_facet $SINGLEMDS \
11250                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11251                awk '/ldlm_cancel/ {print $2}')
11252         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11253                awk '/ldlm_bl_callback/ {print $2}')
11254         createmany -o $DIR/$tdir/f $count
11255         sync
11256         can1=$(do_facet $SINGLEMDS \
11257                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11258                awk '/ldlm_cancel/ {print $2}')
11259         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11260                awk '/ldlm_bl_callback/ {print $2}')
11261         t1=$(date +%s)
11262         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
11263         echo rm $count files
11264         rm -r $DIR/$tdir
11265         sync
11266         can2=$(do_facet $SINGLEMDS \
11267                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11268                awk '/ldlm_cancel/ {print $2}')
11269         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11270                awk '/ldlm_bl_callback/ {print $2}')
11271         t2=$(date +%s)
11272         echo total: $count removes in $((t2-t1))
11273         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
11274         sleep 2
11275         # wait for commitment of removal
11276         lru_resize_enable mdc
11277         lru_resize_enable osc
11278 }
11279 run_test 120g "Early Lock Cancel: performance test"
11280
11281 test_121() { #bug #10589
11282         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11283
11284         rm -rf $DIR/$tfile
11285         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
11286 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
11287         lctl set_param fail_loc=0x310
11288         cancel_lru_locks osc > /dev/null
11289         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
11290         lctl set_param fail_loc=0
11291         [[ $reads -eq $writes ]] ||
11292                 error "read $reads blocks, must be $writes blocks"
11293 }
11294 run_test 121 "read cancel race ========="
11295
11296 test_123a() { # was test 123, statahead(bug 11401)
11297         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11298
11299         SLOWOK=0
11300         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
11301                 log "testing UP system. Performance may be lower than expected."
11302                 SLOWOK=1
11303         fi
11304
11305         rm -rf $DIR/$tdir
11306         test_mkdir $DIR/$tdir
11307         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
11308         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
11309         MULT=10
11310         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
11311                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
11312
11313                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
11314                 lctl set_param -n llite.*.statahead_max 0
11315                 lctl get_param llite.*.statahead_max
11316                 cancel_lru_locks mdc
11317                 cancel_lru_locks osc
11318                 stime=`date +%s`
11319                 time ls -l $DIR/$tdir | wc -l
11320                 etime=`date +%s`
11321                 delta=$((etime - stime))
11322                 log "ls $i files without statahead: $delta sec"
11323                 lctl set_param llite.*.statahead_max=$max
11324
11325                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
11326                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
11327                 cancel_lru_locks mdc
11328                 cancel_lru_locks osc
11329                 stime=`date +%s`
11330                 time ls -l $DIR/$tdir | wc -l
11331                 etime=`date +%s`
11332                 delta_sa=$((etime - stime))
11333                 log "ls $i files with statahead: $delta_sa sec"
11334                 lctl get_param -n llite.*.statahead_stats
11335                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
11336
11337                 [[ $swrong -lt $ewrong ]] &&
11338                         log "statahead was stopped, maybe too many locks held!"
11339                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
11340
11341                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
11342                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
11343                     lctl set_param -n llite.*.statahead_max 0
11344                     lctl get_param llite.*.statahead_max
11345                     cancel_lru_locks mdc
11346                     cancel_lru_locks osc
11347                     stime=`date +%s`
11348                     time ls -l $DIR/$tdir | wc -l
11349                     etime=`date +%s`
11350                     delta=$((etime - stime))
11351                     log "ls $i files again without statahead: $delta sec"
11352                     lctl set_param llite.*.statahead_max=$max
11353                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
11354                         if [  $SLOWOK -eq 0 ]; then
11355                                 error "ls $i files is slower with statahead!"
11356                         else
11357                                 log "ls $i files is slower with statahead!"
11358                         fi
11359                         break
11360                     fi
11361                 fi
11362
11363                 [ $delta -gt 20 ] && break
11364                 [ $delta -gt 8 ] && MULT=$((50 / delta))
11365                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
11366         done
11367         log "ls done"
11368
11369         stime=`date +%s`
11370         rm -r $DIR/$tdir
11371         sync
11372         etime=`date +%s`
11373         delta=$((etime - stime))
11374         log "rm -r $DIR/$tdir/: $delta seconds"
11375         log "rm done"
11376         lctl get_param -n llite.*.statahead_stats
11377 }
11378 run_test 123a "verify statahead work"
11379
11380 test_123b () { # statahead(bug 15027)
11381         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11382
11383         test_mkdir $DIR/$tdir
11384         createmany -o $DIR/$tdir/$tfile-%d 1000
11385
11386         cancel_lru_locks mdc
11387         cancel_lru_locks osc
11388
11389 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
11390         lctl set_param fail_loc=0x80000803
11391         ls -lR $DIR/$tdir > /dev/null
11392         log "ls done"
11393         lctl set_param fail_loc=0x0
11394         lctl get_param -n llite.*.statahead_stats
11395         rm -r $DIR/$tdir
11396         sync
11397
11398 }
11399 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
11400
11401 test_123c() {
11402         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
11403
11404         test_mkdir -i 0 -c 1 $DIR/$tdir.0
11405         test_mkdir -i 1 -c 1 $DIR/$tdir.1
11406         touch $DIR/$tdir.1/{1..3}
11407         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
11408
11409         remount_client $MOUNT
11410
11411         $MULTIOP $DIR/$tdir.0 Q
11412
11413         # let statahead to complete
11414         ls -l $DIR/$tdir.0 > /dev/null
11415
11416         testid=$(echo $TESTNAME | tr '_' ' ')
11417         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
11418                 error "statahead warning" || true
11419 }
11420 run_test 123c "Can not initialize inode warning on DNE statahead"
11421
11422 test_124a() {
11423         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11424         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11425                 skip_env "no lru resize on server"
11426
11427         local NR=2000
11428
11429         test_mkdir $DIR/$tdir
11430
11431         log "create $NR files at $DIR/$tdir"
11432         createmany -o $DIR/$tdir/f $NR ||
11433                 error "failed to create $NR files in $DIR/$tdir"
11434
11435         cancel_lru_locks mdc
11436         ls -l $DIR/$tdir > /dev/null
11437
11438         local NSDIR=""
11439         local LRU_SIZE=0
11440         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
11441                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
11442                 LRU_SIZE=$($LCTL get_param -n $PARAM)
11443                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
11444                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
11445                         log "NSDIR=$NSDIR"
11446                         log "NS=$(basename $NSDIR)"
11447                         break
11448                 fi
11449         done
11450
11451         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
11452                 skip "Not enough cached locks created!"
11453         fi
11454         log "LRU=$LRU_SIZE"
11455
11456         local SLEEP=30
11457
11458         # We know that lru resize allows one client to hold $LIMIT locks
11459         # for 10h. After that locks begin to be killed by client.
11460         local MAX_HRS=10
11461         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
11462         log "LIMIT=$LIMIT"
11463         if [ $LIMIT -lt $LRU_SIZE ]; then
11464                 skip "Limit is too small $LIMIT"
11465         fi
11466
11467         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
11468         # killing locks. Some time was spent for creating locks. This means
11469         # that up to the moment of sleep finish we must have killed some of
11470         # them (10-100 locks). This depends on how fast ther were created.
11471         # Many of them were touched in almost the same moment and thus will
11472         # be killed in groups.
11473         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
11474
11475         # Use $LRU_SIZE_B here to take into account real number of locks
11476         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
11477         local LRU_SIZE_B=$LRU_SIZE
11478         log "LVF=$LVF"
11479         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
11480         log "OLD_LVF=$OLD_LVF"
11481         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
11482
11483         # Let's make sure that we really have some margin. Client checks
11484         # cached locks every 10 sec.
11485         SLEEP=$((SLEEP+20))
11486         log "Sleep ${SLEEP} sec"
11487         local SEC=0
11488         while ((SEC<$SLEEP)); do
11489                 echo -n "..."
11490                 sleep 5
11491                 SEC=$((SEC+5))
11492                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
11493                 echo -n "$LRU_SIZE"
11494         done
11495         echo ""
11496         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
11497         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
11498
11499         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
11500                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
11501                 unlinkmany $DIR/$tdir/f $NR
11502                 return
11503         }
11504
11505         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
11506         log "unlink $NR files at $DIR/$tdir"
11507         unlinkmany $DIR/$tdir/f $NR
11508 }
11509 run_test 124a "lru resize ======================================="
11510
11511 get_max_pool_limit()
11512 {
11513         local limit=$($LCTL get_param \
11514                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
11515         local max=0
11516         for l in $limit; do
11517                 if [[ $l -gt $max ]]; then
11518                         max=$l
11519                 fi
11520         done
11521         echo $max
11522 }
11523
11524 test_124b() {
11525         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11526         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11527                 skip_env "no lru resize on server"
11528
11529         LIMIT=$(get_max_pool_limit)
11530
11531         NR=$(($(default_lru_size)*20))
11532         if [[ $NR -gt $LIMIT ]]; then
11533                 log "Limit lock number by $LIMIT locks"
11534                 NR=$LIMIT
11535         fi
11536
11537         IFree=$(mdsrate_inodes_available)
11538         if [ $IFree -lt $NR ]; then
11539                 log "Limit lock number by $IFree inodes"
11540                 NR=$IFree
11541         fi
11542
11543         lru_resize_disable mdc
11544         test_mkdir -p $DIR/$tdir/disable_lru_resize
11545
11546         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
11547         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
11548         cancel_lru_locks mdc
11549         stime=`date +%s`
11550         PID=""
11551         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11552         PID="$PID $!"
11553         sleep 2
11554         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11555         PID="$PID $!"
11556         sleep 2
11557         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11558         PID="$PID $!"
11559         wait $PID
11560         etime=`date +%s`
11561         nolruresize_delta=$((etime-stime))
11562         log "ls -la time: $nolruresize_delta seconds"
11563         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11564         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
11565
11566         lru_resize_enable mdc
11567         test_mkdir -p $DIR/$tdir/enable_lru_resize
11568
11569         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
11570         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
11571         cancel_lru_locks mdc
11572         stime=`date +%s`
11573         PID=""
11574         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11575         PID="$PID $!"
11576         sleep 2
11577         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11578         PID="$PID $!"
11579         sleep 2
11580         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11581         PID="$PID $!"
11582         wait $PID
11583         etime=`date +%s`
11584         lruresize_delta=$((etime-stime))
11585         log "ls -la time: $lruresize_delta seconds"
11586         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11587
11588         if [ $lruresize_delta -gt $nolruresize_delta ]; then
11589                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
11590         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
11591                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
11592         else
11593                 log "lru resize performs the same with no lru resize"
11594         fi
11595         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
11596 }
11597 run_test 124b "lru resize (performance test) ======================="
11598
11599 test_124c() {
11600         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11601         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11602                 skip_env "no lru resize on server"
11603
11604         # cache ununsed locks on client
11605         local nr=100
11606         cancel_lru_locks mdc
11607         test_mkdir $DIR/$tdir
11608         createmany -o $DIR/$tdir/f $nr ||
11609                 error "failed to create $nr files in $DIR/$tdir"
11610         ls -l $DIR/$tdir > /dev/null
11611
11612         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11613         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11614         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11615         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11616         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11617
11618         # set lru_max_age to 1 sec
11619         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11620         echo "sleep $((recalc_p * 2)) seconds..."
11621         sleep $((recalc_p * 2))
11622
11623         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11624         # restore lru_max_age
11625         $LCTL set_param -n $nsdir.lru_max_age $max_age
11626         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11627         unlinkmany $DIR/$tdir/f $nr
11628 }
11629 run_test 124c "LRUR cancel very aged locks"
11630
11631 test_124d() {
11632         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11633         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11634                 skip_env "no lru resize on server"
11635
11636         # cache ununsed locks on client
11637         local nr=100
11638
11639         lru_resize_disable mdc
11640         stack_trap "lru_resize_enable mdc" EXIT
11641
11642         cancel_lru_locks mdc
11643
11644         # asynchronous object destroy at MDT could cause bl ast to client
11645         test_mkdir $DIR/$tdir
11646         createmany -o $DIR/$tdir/f $nr ||
11647                 error "failed to create $nr files in $DIR/$tdir"
11648         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
11649
11650         ls -l $DIR/$tdir > /dev/null
11651
11652         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11653         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11654         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11655         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11656
11657         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11658
11659         # set lru_max_age to 1 sec
11660         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11661         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
11662
11663         echo "sleep $((recalc_p * 2)) seconds..."
11664         sleep $((recalc_p * 2))
11665
11666         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11667
11668         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11669 }
11670 run_test 124d "cancel very aged locks if lru-resize diasbaled"
11671
11672 test_125() { # 13358
11673         $LCTL get_param -n llite.*.client_type | grep -q local ||
11674                 skip "must run as local client"
11675         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
11676                 skip_env "must have acl enabled"
11677         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
11678
11679         test_mkdir $DIR/$tdir
11680         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
11681         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
11682         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
11683 }
11684 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
11685
11686 test_126() { # bug 12829/13455
11687         $GSS && skip_env "must run as gss disabled"
11688         $LCTL get_param -n llite.*.client_type | grep -q local ||
11689                 skip "must run as local client"
11690         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
11691
11692         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
11693         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
11694         rm -f $DIR/$tfile
11695         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
11696 }
11697 run_test 126 "check that the fsgid provided by the client is taken into account"
11698
11699 test_127a() { # bug 15521
11700         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11701         local name count samp unit min max sum sumsq
11702
11703         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
11704         echo "stats before reset"
11705         $LCTL get_param osc.*.stats
11706         $LCTL set_param osc.*.stats=0
11707         local fsize=$((2048 * 1024))
11708
11709         dd if=/dev/zero of=$DIR/$tfile bs=$fsize count=1
11710         cancel_lru_locks osc
11711         dd if=$DIR/$tfile of=/dev/null bs=$fsize
11712
11713         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/$tfile.tmp
11714         stack_trap "rm -f $TMP/$tfile.tmp"
11715         while read name count samp unit min max sum sumsq; do
11716                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11717                 [ ! $min ] && error "Missing min value for $name proc entry"
11718                 eval $name=$count || error "Wrong proc format"
11719
11720                 case $name in
11721                 read_bytes|write_bytes)
11722                         [[ "$unit" =~ "bytes" ]] ||
11723                                 error "unit is not 'bytes': $unit"
11724                         (( $min >= 4096 )) || error "min is too small: $min"
11725                         (( $min <= $fsize )) || error "min is too big: $min"
11726                         (( $max >= 4096 )) || error "max is too small: $max"
11727                         (( $max <= $fsize )) || error "max is too big: $max"
11728                         (( $sum == $fsize )) || error "sum is wrong: $sum"
11729                         (( $sumsq >= ($fsize / 4096) * (4096 * 4096) )) ||
11730                                 error "sumsquare is too small: $sumsq"
11731                         (( $sumsq <= $fsize * $fsize )) ||
11732                                 error "sumsquare is too big: $sumsq"
11733                         ;;
11734                 ost_read|ost_write)
11735                         [[ "$unit" =~ "usec" ]] ||
11736                                 error "unit is not 'usec': $unit"
11737                         ;;
11738                 *)      ;;
11739                 esac
11740         done < $DIR/$tfile.tmp
11741
11742         #check that we actually got some stats
11743         [ "$read_bytes" ] || error "Missing read_bytes stats"
11744         [ "$write_bytes" ] || error "Missing write_bytes stats"
11745         [ "$read_bytes" != 0 ] || error "no read done"
11746         [ "$write_bytes" != 0 ] || error "no write done"
11747 }
11748 run_test 127a "verify the client stats are sane"
11749
11750 test_127b() { # bug LU-333
11751         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11752         local name count samp unit min max sum sumsq
11753
11754         echo "stats before reset"
11755         $LCTL get_param llite.*.stats
11756         $LCTL set_param llite.*.stats=0
11757
11758         # perform 2 reads and writes so MAX is different from SUM.
11759         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11760         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11761         cancel_lru_locks osc
11762         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11763         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11764
11765         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
11766         stack_trap "rm -f $TMP/$tfile.tmp"
11767         while read name count samp unit min max sum sumsq; do
11768                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11769                 eval $name=$count || error "Wrong proc format"
11770
11771                 case $name in
11772                 read_bytes|write_bytes)
11773                         [[ "$unit" =~ "bytes" ]] ||
11774                                 error "unit is not 'bytes': $unit"
11775                         (( $count == 2 )) || error "count is not 2: $count"
11776                         (( $min == $PAGE_SIZE )) ||
11777                                 error "min is not $PAGE_SIZE: $min"
11778                         (( $max == $PAGE_SIZE )) ||
11779                                 error "max is not $PAGE_SIZE: $max"
11780                         (( $sum == $PAGE_SIZE * 2 )) ||
11781                                 error "sum is not $((PAGE_SIZE * 2)): $sum"
11782                         ;;
11783                 read|write)
11784                         [[ "$unit" =~ "usec" ]] ||
11785                                 error "unit is not 'usec': $unit"
11786                         ;;
11787                 *)      ;;
11788                 esac
11789         done < $TMP/$tfile.tmp
11790
11791         #check that we actually got some stats
11792         [ "$read_bytes" ] || error "Missing read_bytes stats"
11793         [ "$write_bytes" ] || error "Missing write_bytes stats"
11794         [ "$read_bytes" != 0 ] || error "no read done"
11795         [ "$write_bytes" != 0 ] || error "no write done"
11796 }
11797 run_test 127b "verify the llite client stats are sane"
11798
11799 test_127c() { # LU-12394
11800         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11801         local size
11802         local bsize
11803         local reads
11804         local writes
11805         local count
11806
11807         $LCTL set_param llite.*.extents_stats=1
11808         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
11809
11810         # Use two stripes so there is enough space in default config
11811         $LFS setstripe -c 2 $DIR/$tfile
11812
11813         # Extent stats start at 0-4K and go in power of two buckets
11814         # LL_HIST_START = 12 --> 2^12 = 4K
11815         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
11816         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
11817         # small configs
11818         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
11819                 do
11820                 # Write and read, 2x each, second time at a non-zero offset
11821                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
11822                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
11823                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
11824                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
11825                 rm -f $DIR/$tfile
11826         done
11827
11828         $LCTL get_param llite.*.extents_stats
11829
11830         count=2
11831         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
11832                 do
11833                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
11834                                 grep -m 1 $bsize)
11835                 reads=$(echo $bucket | awk '{print $5}')
11836                 writes=$(echo $bucket | awk '{print $9}')
11837                 [ "$reads" -eq $count ] ||
11838                         error "$reads reads in < $bsize bucket, expect $count"
11839                 [ "$writes" -eq $count ] ||
11840                         error "$writes writes in < $bsize bucket, expect $count"
11841         done
11842
11843         # Test mmap write and read
11844         $LCTL set_param llite.*.extents_stats=c
11845         size=512
11846         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
11847         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
11848         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
11849
11850         $LCTL get_param llite.*.extents_stats
11851
11852         count=$(((size*1024) / PAGE_SIZE))
11853
11854         bsize=$((2 * PAGE_SIZE / 1024))K
11855
11856         bucket=$($LCTL get_param -n llite.*.extents_stats |
11857                         grep -m 1 $bsize)
11858         reads=$(echo $bucket | awk '{print $5}')
11859         writes=$(echo $bucket | awk '{print $9}')
11860         # mmap writes fault in the page first, creating an additonal read
11861         [ "$reads" -eq $((2 * count)) ] ||
11862                 error "$reads reads in < $bsize bucket, expect $count"
11863         [ "$writes" -eq $count ] ||
11864                 error "$writes writes in < $bsize bucket, expect $count"
11865 }
11866 run_test 127c "test llite extent stats with regular & mmap i/o"
11867
11868 test_128() { # bug 15212
11869         touch $DIR/$tfile
11870         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
11871                 find $DIR/$tfile
11872                 find $DIR/$tfile
11873         EOF
11874
11875         result=$(grep error $TMP/$tfile.log)
11876         rm -f $DIR/$tfile $TMP/$tfile.log
11877         [ -z "$result" ] ||
11878                 error "consecutive find's under interactive lfs failed"
11879 }
11880 run_test 128 "interactive lfs for 2 consecutive find's"
11881
11882 set_dir_limits () {
11883         local mntdev
11884         local canondev
11885         local node
11886
11887         local ldproc=/proc/fs/ldiskfs
11888         local facets=$(get_facets MDS)
11889
11890         for facet in ${facets//,/ }; do
11891                 canondev=$(ldiskfs_canon \
11892                            *.$(convert_facet2label $facet).mntdev $facet)
11893                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
11894                         ldproc=/sys/fs/ldiskfs
11895                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
11896                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
11897         done
11898 }
11899
11900 check_mds_dmesg() {
11901         local facets=$(get_facets MDS)
11902         for facet in ${facets//,/ }; do
11903                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
11904         done
11905         return 1
11906 }
11907
11908 test_129() {
11909         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11910         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
11911                 skip "Need MDS version with at least 2.5.56"
11912         if [ "$mds1_FSTYPE" != ldiskfs ]; then
11913                 skip_env "ldiskfs only test"
11914         fi
11915         remote_mds_nodsh && skip "remote MDS with nodsh"
11916
11917         local ENOSPC=28
11918         local EFBIG=27
11919         local has_warning=false
11920
11921         rm -rf $DIR/$tdir
11922         mkdir -p $DIR/$tdir
11923
11924         # block size of mds1
11925         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
11926         set_dir_limits $maxsize $maxsize
11927         local dirsize=$(stat -c%s "$DIR/$tdir")
11928         local nfiles=0
11929         while [[ $dirsize -le $maxsize ]]; do
11930                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
11931                 rc=$?
11932                 if ! $has_warning; then
11933                         check_mds_dmesg '"is approaching"' && has_warning=true
11934                 fi
11935                 # check two errors:
11936                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
11937                 # EFBIG for previous versions included in ldiskfs series
11938                 if [ $rc -eq $EFBIG ] || [ $rc -eq $ENOSPC ]; then
11939                         set_dir_limits 0 0
11940                         echo "return code $rc received as expected"
11941
11942                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
11943                                 error_exit "create failed w/o dir size limit"
11944
11945                         check_mds_dmesg '"has reached"' ||
11946                                 error_exit "reached message should be output"
11947
11948                         [ $has_warning = "false" ] &&
11949                                 error_exit "warning message should be output"
11950
11951                         dirsize=$(stat -c%s "$DIR/$tdir")
11952
11953                         [[ $dirsize -ge $maxsize ]] && return 0
11954                         error_exit "current dir size $dirsize, " \
11955                                    "previous limit $maxsize"
11956                 elif [ $rc -ne 0 ]; then
11957                         set_dir_limits 0 0
11958                         error_exit "return $rc received instead of expected " \
11959                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
11960                 fi
11961                 nfiles=$((nfiles + 1))
11962                 dirsize=$(stat -c%s "$DIR/$tdir")
11963         done
11964
11965         set_dir_limits 0 0
11966         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
11967 }
11968 run_test 129 "test directory size limit ========================"
11969
11970 OLDIFS="$IFS"
11971 cleanup_130() {
11972         trap 0
11973         IFS="$OLDIFS"
11974 }
11975
11976 test_130a() {
11977         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11978         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11979
11980         trap cleanup_130 EXIT RETURN
11981
11982         local fm_file=$DIR/$tfile
11983         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
11984         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
11985                 error "dd failed for $fm_file"
11986
11987         # LU-1795: test filefrag/FIEMAP once, even if unsupported
11988         filefrag -ves $fm_file
11989         RC=$?
11990         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11991                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11992         [ $RC != 0 ] && error "filefrag $fm_file failed"
11993
11994         filefrag_op=$(filefrag -ve -k $fm_file |
11995                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11996         lun=$($LFS getstripe -i $fm_file)
11997
11998         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
11999         IFS=$'\n'
12000         tot_len=0
12001         for line in $filefrag_op
12002         do
12003                 frag_lun=`echo $line | cut -d: -f5`
12004                 ext_len=`echo $line | cut -d: -f4`
12005                 if (( $frag_lun != $lun )); then
12006                         cleanup_130
12007                         error "FIEMAP on 1-stripe file($fm_file) failed"
12008                         return
12009                 fi
12010                 (( tot_len += ext_len ))
12011         done
12012
12013         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
12014                 cleanup_130
12015                 error "FIEMAP on 1-stripe file($fm_file) failed;"
12016                 return
12017         fi
12018
12019         cleanup_130
12020
12021         echo "FIEMAP on single striped file succeeded"
12022 }
12023 run_test 130a "FIEMAP (1-stripe file)"
12024
12025 test_130b() {
12026         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
12027
12028         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12029         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
12030
12031         trap cleanup_130 EXIT RETURN
12032
12033         local fm_file=$DIR/$tfile
12034         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
12035                         error "setstripe on $fm_file"
12036         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12037                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12038
12039         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
12040                 error "dd failed on $fm_file"
12041
12042         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12043         filefrag_op=$(filefrag -ve -k $fm_file |
12044                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12045
12046         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12047                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12048
12049         IFS=$'\n'
12050         tot_len=0
12051         num_luns=1
12052         for line in $filefrag_op
12053         do
12054                 frag_lun=$(echo $line | cut -d: -f5 |
12055                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12056                 ext_len=$(echo $line | cut -d: -f4)
12057                 if (( $frag_lun != $last_lun )); then
12058                         if (( tot_len != 1024 )); then
12059                                 cleanup_130
12060                                 error "FIEMAP on $fm_file failed; returned " \
12061                                 "len $tot_len for OST $last_lun instead of 1024"
12062                                 return
12063                         else
12064                                 (( num_luns += 1 ))
12065                                 tot_len=0
12066                         fi
12067                 fi
12068                 (( tot_len += ext_len ))
12069                 last_lun=$frag_lun
12070         done
12071         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
12072                 cleanup_130
12073                 error "FIEMAP on $fm_file failed; returned wrong number of " \
12074                         "luns or wrong len for OST $last_lun"
12075                 return
12076         fi
12077
12078         cleanup_130
12079
12080         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
12081 }
12082 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
12083
12084 test_130c() {
12085         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12086
12087         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12088         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12089
12090         trap cleanup_130 EXIT RETURN
12091
12092         local fm_file=$DIR/$tfile
12093         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
12094         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12095                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12096
12097         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
12098                         error "dd failed on $fm_file"
12099
12100         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12101         filefrag_op=$(filefrag -ve -k $fm_file |
12102                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12103
12104         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12105                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12106
12107         IFS=$'\n'
12108         tot_len=0
12109         num_luns=1
12110         for line in $filefrag_op
12111         do
12112                 frag_lun=$(echo $line | cut -d: -f5 |
12113                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12114                 ext_len=$(echo $line | cut -d: -f4)
12115                 if (( $frag_lun != $last_lun )); then
12116                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
12117                         if (( logical != 512 )); then
12118                                 cleanup_130
12119                                 error "FIEMAP on $fm_file failed; returned " \
12120                                 "logical start for lun $logical instead of 512"
12121                                 return
12122                         fi
12123                         if (( tot_len != 512 )); then
12124                                 cleanup_130
12125                                 error "FIEMAP on $fm_file failed; returned " \
12126                                 "len $tot_len for OST $last_lun instead of 1024"
12127                                 return
12128                         else
12129                                 (( num_luns += 1 ))
12130                                 tot_len=0
12131                         fi
12132                 fi
12133                 (( tot_len += ext_len ))
12134                 last_lun=$frag_lun
12135         done
12136         if (( num_luns != 2 || tot_len != 512 )); then
12137                 cleanup_130
12138                 error "FIEMAP on $fm_file failed; returned wrong number of " \
12139                         "luns or wrong len for OST $last_lun"
12140                 return
12141         fi
12142
12143         cleanup_130
12144
12145         echo "FIEMAP on 2-stripe file with hole succeeded"
12146 }
12147 run_test 130c "FIEMAP (2-stripe file with hole)"
12148
12149 test_130d() {
12150         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
12151
12152         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12153         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12154
12155         trap cleanup_130 EXIT RETURN
12156
12157         local fm_file=$DIR/$tfile
12158         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
12159                         error "setstripe on $fm_file"
12160         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12161                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12162
12163         local actual_stripe_count=$($LFS getstripe -c $fm_file)
12164         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
12165                 error "dd failed on $fm_file"
12166
12167         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12168         filefrag_op=$(filefrag -ve -k $fm_file |
12169                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12170
12171         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12172                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12173
12174         IFS=$'\n'
12175         tot_len=0
12176         num_luns=1
12177         for line in $filefrag_op
12178         do
12179                 frag_lun=$(echo $line | cut -d: -f5 |
12180                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12181                 ext_len=$(echo $line | cut -d: -f4)
12182                 if (( $frag_lun != $last_lun )); then
12183                         if (( tot_len != 1024 )); then
12184                                 cleanup_130
12185                                 error "FIEMAP on $fm_file failed; returned " \
12186                                 "len $tot_len for OST $last_lun instead of 1024"
12187                                 return
12188                         else
12189                                 (( num_luns += 1 ))
12190                                 tot_len=0
12191                         fi
12192                 fi
12193                 (( tot_len += ext_len ))
12194                 last_lun=$frag_lun
12195         done
12196         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
12197                 cleanup_130
12198                 error "FIEMAP on $fm_file failed; returned wrong number of " \
12199                         "luns or wrong len for OST $last_lun"
12200                 return
12201         fi
12202
12203         cleanup_130
12204
12205         echo "FIEMAP on N-stripe file succeeded"
12206 }
12207 run_test 130d "FIEMAP (N-stripe file)"
12208
12209 test_130e() {
12210         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12211
12212         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12213         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
12214
12215         trap cleanup_130 EXIT RETURN
12216
12217         local fm_file=$DIR/$tfile
12218         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
12219         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12220                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12221
12222         NUM_BLKS=512
12223         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
12224         for ((i = 0; i < $NUM_BLKS; i++))
12225         do
12226                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
12227         done
12228
12229         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12230         filefrag_op=$(filefrag -ve -k $fm_file |
12231                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12232
12233         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12234                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12235
12236         IFS=$'\n'
12237         tot_len=0
12238         num_luns=1
12239         for line in $filefrag_op
12240         do
12241                 frag_lun=$(echo $line | cut -d: -f5 |
12242                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12243                 ext_len=$(echo $line | cut -d: -f4)
12244                 if (( $frag_lun != $last_lun )); then
12245                         if (( tot_len != $EXPECTED_LEN )); then
12246                                 cleanup_130
12247                                 error "FIEMAP on $fm_file failed; returned " \
12248                                 "len $tot_len for OST $last_lun instead " \
12249                                 "of $EXPECTED_LEN"
12250                                 return
12251                         else
12252                                 (( num_luns += 1 ))
12253                                 tot_len=0
12254                         fi
12255                 fi
12256                 (( tot_len += ext_len ))
12257                 last_lun=$frag_lun
12258         done
12259         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
12260                 cleanup_130
12261                 error "FIEMAP on $fm_file failed; returned wrong number " \
12262                         "of luns or wrong len for OST $last_lun"
12263                 return
12264         fi
12265
12266         cleanup_130
12267
12268         echo "FIEMAP with continuation calls succeeded"
12269 }
12270 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
12271
12272 test_130f() {
12273         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12274         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12275
12276         local fm_file=$DIR/$tfile
12277         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
12278                 error "multiop create with lov_delay_create on $fm_file"
12279
12280         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12281         filefrag_extents=$(filefrag -vek $fm_file |
12282                            awk '/extents? found/ { print $2 }')
12283         if [[ "$filefrag_extents" != "0" ]]; then
12284                 error "FIEMAP on $fm_file failed; " \
12285                       "returned $filefrag_extents expected 0"
12286         fi
12287
12288         rm -f $fm_file
12289 }
12290 run_test 130f "FIEMAP (unstriped file)"
12291
12292 # Test for writev/readv
12293 test_131a() {
12294         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
12295                 error "writev test failed"
12296         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
12297                 error "readv failed"
12298         rm -f $DIR/$tfile
12299 }
12300 run_test 131a "test iov's crossing stripe boundary for writev/readv"
12301
12302 test_131b() {
12303         local fsize=$((524288 + 1048576 + 1572864))
12304         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
12305                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12306                         error "append writev test failed"
12307
12308         ((fsize += 1572864 + 1048576))
12309         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
12310                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12311                         error "append writev test failed"
12312         rm -f $DIR/$tfile
12313 }
12314 run_test 131b "test append writev"
12315
12316 test_131c() {
12317         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
12318         error "NOT PASS"
12319 }
12320 run_test 131c "test read/write on file w/o objects"
12321
12322 test_131d() {
12323         rwv -f $DIR/$tfile -w -n 1 1572864
12324         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
12325         if [ "$NOB" != 1572864 ]; then
12326                 error "Short read filed: read $NOB bytes instead of 1572864"
12327         fi
12328         rm -f $DIR/$tfile
12329 }
12330 run_test 131d "test short read"
12331
12332 test_131e() {
12333         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
12334         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
12335         error "read hitting hole failed"
12336         rm -f $DIR/$tfile
12337 }
12338 run_test 131e "test read hitting hole"
12339
12340 check_stats() {
12341         local facet=$1
12342         local op=$2
12343         local want=${3:-0}
12344         local res
12345
12346         case $facet in
12347         mds*) res=$(do_facet $facet \
12348                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
12349                  ;;
12350         ost*) res=$(do_facet $facet \
12351                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
12352                  ;;
12353         *) error "Wrong facet '$facet'" ;;
12354         esac
12355         [ "$res" ] || error "The counter for $op on $facet was not incremented"
12356         # if the argument $3 is zero, it means any stat increment is ok.
12357         if [[ $want -gt 0 ]]; then
12358                 local count=$(echo $res | awk '{ print $2 }')
12359                 [[ $count -ne $want ]] &&
12360                         error "The $op counter on $facet is $count, not $want"
12361         fi
12362 }
12363
12364 test_133a() {
12365         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12366         remote_ost_nodsh && skip "remote OST with nodsh"
12367         remote_mds_nodsh && skip "remote MDS with nodsh"
12368         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12369                 skip_env "MDS doesn't support rename stats"
12370
12371         local testdir=$DIR/${tdir}/stats_testdir
12372
12373         mkdir -p $DIR/${tdir}
12374
12375         # clear stats.
12376         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12377         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12378
12379         # verify mdt stats first.
12380         mkdir ${testdir} || error "mkdir failed"
12381         check_stats $SINGLEMDS "mkdir" 1
12382         touch ${testdir}/${tfile} || error "touch failed"
12383         check_stats $SINGLEMDS "open" 1
12384         check_stats $SINGLEMDS "close" 1
12385         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
12386                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
12387                 check_stats $SINGLEMDS "mknod" 2
12388         }
12389         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
12390         check_stats $SINGLEMDS "unlink" 1
12391         rm -f ${testdir}/${tfile} || error "file remove failed"
12392         check_stats $SINGLEMDS "unlink" 2
12393
12394         # remove working dir and check mdt stats again.
12395         rmdir ${testdir} || error "rmdir failed"
12396         check_stats $SINGLEMDS "rmdir" 1
12397
12398         local testdir1=$DIR/${tdir}/stats_testdir1
12399         mkdir -p ${testdir}
12400         mkdir -p ${testdir1}
12401         touch ${testdir1}/test1
12402         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
12403         check_stats $SINGLEMDS "crossdir_rename" 1
12404
12405         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
12406         check_stats $SINGLEMDS "samedir_rename" 1
12407
12408         rm -rf $DIR/${tdir}
12409 }
12410 run_test 133a "Verifying MDT stats ========================================"
12411
12412 test_133b() {
12413         local res
12414
12415         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12416         remote_ost_nodsh && skip "remote OST with nodsh"
12417         remote_mds_nodsh && skip "remote MDS with nodsh"
12418
12419         local testdir=$DIR/${tdir}/stats_testdir
12420
12421         mkdir -p ${testdir} || error "mkdir failed"
12422         touch ${testdir}/${tfile} || error "touch failed"
12423         cancel_lru_locks mdc
12424
12425         # clear stats.
12426         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12427         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12428
12429         # extra mdt stats verification.
12430         chmod 444 ${testdir}/${tfile} || error "chmod failed"
12431         check_stats $SINGLEMDS "setattr" 1
12432         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12433         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
12434         then            # LU-1740
12435                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
12436                 check_stats $SINGLEMDS "getattr" 1
12437         fi
12438         rm -rf $DIR/${tdir}
12439
12440         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
12441         # so the check below is not reliable
12442         [ $MDSCOUNT -eq 1 ] || return 0
12443
12444         # Sleep to avoid a cached response.
12445         #define OBD_STATFS_CACHE_SECONDS 1
12446         sleep 2
12447         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12448         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12449         $LFS df || error "lfs failed"
12450         check_stats $SINGLEMDS "statfs" 1
12451
12452         # check aggregated statfs (LU-10018)
12453         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
12454                 return 0
12455         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
12456                 return 0
12457         sleep 2
12458         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12459         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12460         df $DIR
12461         check_stats $SINGLEMDS "statfs" 1
12462
12463         # We want to check that the client didn't send OST_STATFS to
12464         # ost1 but the MDT also uses OST_STATFS for precreate. So some
12465         # extra care is needed here.
12466         if remote_mds; then
12467                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
12468                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
12469
12470                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
12471                 [ "$res" ] && error "OST got STATFS"
12472         fi
12473
12474         return 0
12475 }
12476 run_test 133b "Verifying extra MDT stats =================================="
12477
12478 test_133c() {
12479         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12480         remote_ost_nodsh && skip "remote OST with nodsh"
12481         remote_mds_nodsh && skip "remote MDS with nodsh"
12482
12483         local testdir=$DIR/$tdir/stats_testdir
12484
12485         test_mkdir -p $testdir
12486
12487         # verify obdfilter stats.
12488         $LFS setstripe -c 1 -i 0 $testdir/$tfile
12489         sync
12490         cancel_lru_locks osc
12491         wait_delete_completed
12492
12493         # clear stats.
12494         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12495         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12496
12497         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
12498                 error "dd failed"
12499         sync
12500         cancel_lru_locks osc
12501         check_stats ost1 "write" 1
12502
12503         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
12504         check_stats ost1 "read" 1
12505
12506         > $testdir/$tfile || error "truncate failed"
12507         check_stats ost1 "punch" 1
12508
12509         rm -f $testdir/$tfile || error "file remove failed"
12510         wait_delete_completed
12511         check_stats ost1 "destroy" 1
12512
12513         rm -rf $DIR/$tdir
12514 }
12515 run_test 133c "Verifying OST stats ========================================"
12516
12517 order_2() {
12518         local value=$1
12519         local orig=$value
12520         local order=1
12521
12522         while [ $value -ge 2 ]; do
12523                 order=$((order*2))
12524                 value=$((value/2))
12525         done
12526
12527         if [ $orig -gt $order ]; then
12528                 order=$((order*2))
12529         fi
12530         echo $order
12531 }
12532
12533 size_in_KMGT() {
12534     local value=$1
12535     local size=('K' 'M' 'G' 'T');
12536     local i=0
12537     local size_string=$value
12538
12539     while [ $value -ge 1024 ]; do
12540         if [ $i -gt 3 ]; then
12541             #T is the biggest unit we get here, if that is bigger,
12542             #just return XXXT
12543             size_string=${value}T
12544             break
12545         fi
12546         value=$((value >> 10))
12547         if [ $value -lt 1024 ]; then
12548             size_string=${value}${size[$i]}
12549             break
12550         fi
12551         i=$((i + 1))
12552     done
12553
12554     echo $size_string
12555 }
12556
12557 get_rename_size() {
12558         local size=$1
12559         local context=${2:-.}
12560         local sample=$(do_facet $SINGLEMDS $LCTL \
12561                 get_param mdt.$FSNAME-MDT0000.rename_stats |
12562                 grep -A1 $context |
12563                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
12564         echo $sample
12565 }
12566
12567 test_133d() {
12568         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12569         remote_ost_nodsh && skip "remote OST with nodsh"
12570         remote_mds_nodsh && skip "remote MDS with nodsh"
12571         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12572                 skip_env "MDS doesn't support rename stats"
12573
12574         local testdir1=$DIR/${tdir}/stats_testdir1
12575         local testdir2=$DIR/${tdir}/stats_testdir2
12576         mkdir -p $DIR/${tdir}
12577
12578         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12579
12580         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
12581         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
12582
12583         createmany -o $testdir1/test 512 || error "createmany failed"
12584
12585         # check samedir rename size
12586         mv ${testdir1}/test0 ${testdir1}/test_0
12587
12588         local testdir1_size=$(ls -l $DIR/${tdir} |
12589                 awk '/stats_testdir1/ {print $5}')
12590         local testdir2_size=$(ls -l $DIR/${tdir} |
12591                 awk '/stats_testdir2/ {print $5}')
12592
12593         testdir1_size=$(order_2 $testdir1_size)
12594         testdir2_size=$(order_2 $testdir2_size)
12595
12596         testdir1_size=$(size_in_KMGT $testdir1_size)
12597         testdir2_size=$(size_in_KMGT $testdir2_size)
12598
12599         echo "source rename dir size: ${testdir1_size}"
12600         echo "target rename dir size: ${testdir2_size}"
12601
12602         local cmd="do_facet $SINGLEMDS $LCTL "
12603         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
12604
12605         eval $cmd || error "$cmd failed"
12606         local samedir=$($cmd | grep 'same_dir')
12607         local same_sample=$(get_rename_size $testdir1_size)
12608         [ -z "$samedir" ] && error "samedir_rename_size count error"
12609         [[ $same_sample -eq 1 ]] ||
12610                 error "samedir_rename_size error $same_sample"
12611         echo "Check same dir rename stats success"
12612
12613         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12614
12615         # check crossdir rename size
12616         mv ${testdir1}/test_0 ${testdir2}/test_0
12617
12618         testdir1_size=$(ls -l $DIR/${tdir} |
12619                 awk '/stats_testdir1/ {print $5}')
12620         testdir2_size=$(ls -l $DIR/${tdir} |
12621                 awk '/stats_testdir2/ {print $5}')
12622
12623         testdir1_size=$(order_2 $testdir1_size)
12624         testdir2_size=$(order_2 $testdir2_size)
12625
12626         testdir1_size=$(size_in_KMGT $testdir1_size)
12627         testdir2_size=$(size_in_KMGT $testdir2_size)
12628
12629         echo "source rename dir size: ${testdir1_size}"
12630         echo "target rename dir size: ${testdir2_size}"
12631
12632         eval $cmd || error "$cmd failed"
12633         local crossdir=$($cmd | grep 'crossdir')
12634         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
12635         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
12636         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
12637         [[ $src_sample -eq 1 ]] ||
12638                 error "crossdir_rename_size error $src_sample"
12639         [[ $tgt_sample -eq 1 ]] ||
12640                 error "crossdir_rename_size error $tgt_sample"
12641         echo "Check cross dir rename stats success"
12642         rm -rf $DIR/${tdir}
12643 }
12644 run_test 133d "Verifying rename_stats ========================================"
12645
12646 test_133e() {
12647         remote_mds_nodsh && skip "remote MDS with nodsh"
12648         remote_ost_nodsh && skip "remote OST with nodsh"
12649         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12650
12651         local testdir=$DIR/${tdir}/stats_testdir
12652         local ctr f0 f1 bs=32768 count=42 sum
12653
12654         mkdir -p ${testdir} || error "mkdir failed"
12655
12656         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
12657
12658         for ctr in {write,read}_bytes; do
12659                 sync
12660                 cancel_lru_locks osc
12661
12662                 do_facet ost1 $LCTL set_param -n \
12663                         "obdfilter.*.exports.clear=clear"
12664
12665                 if [ $ctr = write_bytes ]; then
12666                         f0=/dev/zero
12667                         f1=${testdir}/${tfile}
12668                 else
12669                         f0=${testdir}/${tfile}
12670                         f1=/dev/null
12671                 fi
12672
12673                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
12674                         error "dd failed"
12675                 sync
12676                 cancel_lru_locks osc
12677
12678                 sum=$(do_facet ost1 $LCTL get_param \
12679                         "obdfilter.*.exports.*.stats" |
12680                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
12681                                 $1 == ctr { sum += $7 }
12682                                 END { printf("%0.0f", sum) }')
12683
12684                 if ((sum != bs * count)); then
12685                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
12686                 fi
12687         done
12688
12689         rm -rf $DIR/${tdir}
12690 }
12691 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
12692
12693 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
12694
12695 # Some versions of find (4.5.11, 4.5.14) included in CentOS 7.3-7.5 do
12696 # not honor the -ignore_readdir_race option correctly. So we call
12697 # error_ignore() rather than error() in these cases. See LU-11152.
12698 error_133() {
12699         if (find --version; do_facet mds1 find --version) |
12700                 grep -q '\b4\.5\.1[1-4]\b'; then
12701                 error_ignore LU-11152 "$@"
12702         else
12703                 error "$@"
12704         fi
12705 }
12706
12707 test_133f() {
12708         # First without trusting modes.
12709         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
12710         echo "proc_dirs='$proc_dirs'"
12711         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
12712         find $proc_dirs -exec cat '{}' \; &> /dev/null
12713
12714         # Second verifying readability.
12715         $LCTL get_param -R '*' &> /dev/null
12716
12717         # Verifing writability with badarea_io.
12718         find $proc_dirs \
12719                 -ignore_readdir_race \
12720                 -type f \
12721                 -not -name force_lbug \
12722                 -not -name changelog_mask \
12723                 -exec badarea_io '{}' \; ||
12724                         error_133 "find $proc_dirs failed"
12725 }
12726 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
12727
12728 test_133g() {
12729         remote_mds_nodsh && skip "remote MDS with nodsh"
12730         remote_ost_nodsh && skip "remote OST with nodsh"
12731
12732         local facet
12733         for facet in mds1 ost1; do
12734                 local facet_ver=$(lustre_version_code $facet)
12735                 if [ $facet_ver -ge $(version_code 2.7.65) ]; then
12736                         do_facet $facet "$LCTL get_param -R '*'" &> /dev/null
12737                 else
12738                         log "$facet: too old lustre for get_param -R"
12739                 fi
12740                 if [ $facet_ver -ge $(version_code 2.5.54) ]; then
12741                         do_facet $facet "$LCTL list_param -R '*' | grep '=' |
12742                                 tr -d= | egrep -v 'force_lbug|changelog_mask' |
12743                                 xargs badarea_io" ||
12744                                         error_133 "$facet badarea_io failed"
12745                 else
12746                         skip_noexit "$facet: too old lustre for get_param -R"
12747                 fi
12748         done
12749
12750         # remount the FS in case writes/reads /proc break the FS
12751         cleanup || error "failed to unmount"
12752         setup || error "failed to setup"
12753         true
12754 }
12755 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
12756
12757 test_133h() {
12758         remote_mds_nodsh && skip "remote MDS with nodsh"
12759         remote_ost_nodsh && skip "remote OST with nodsh"
12760         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
12761                 skip "Need MDS version at least 2.9.54"
12762
12763         local facet
12764
12765         for facet in client mds1 ost1; do
12766                 local facet_proc_dirs=$(do_facet $facet \
12767                                         \\\ls -d $proc_regexp 2> /dev/null)
12768                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12769                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12770                 # Get the list of files that are missing the terminating newline
12771                 local missing=($(do_facet $facet \
12772                         find ${facet_proc_dirs} -type f \|              \
12773                                 while read F\; do                       \
12774                                         awk -v FS='\v' -v RS='\v\v'     \
12775                                         "'END { if(NR>0 &&              \
12776                                         \\\$NF !~ /.*\\\n\$/)           \
12777                                                 print FILENAME}'"       \
12778                                         '\$F'\;                         \
12779                                 done 2>/dev/null))
12780                 [ ${#missing[*]} -eq 0 ] ||
12781                         error "files do not end with newline: ${missing[*]}"
12782         done
12783 }
12784 run_test 133h "Proc files should end with newlines"
12785
12786 test_134a() {
12787         remote_mds_nodsh && skip "remote MDS with nodsh"
12788         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12789                 skip "Need MDS version at least 2.7.54"
12790
12791         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12792         cancel_lru_locks mdc
12793
12794         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
12795         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12796         [ $unused -eq 0 ] || error "$unused locks are not cleared"
12797
12798         local nr=1000
12799         createmany -o $DIR/$tdir/f $nr ||
12800                 error "failed to create $nr files in $DIR/$tdir"
12801         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12802
12803         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
12804         do_facet mds1 $LCTL set_param fail_loc=0x327
12805         do_facet mds1 $LCTL set_param fail_val=500
12806         touch $DIR/$tdir/m
12807
12808         echo "sleep 10 seconds ..."
12809         sleep 10
12810         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
12811
12812         do_facet mds1 $LCTL set_param fail_loc=0
12813         do_facet mds1 $LCTL set_param fail_val=0
12814         [ $lck_cnt -lt $unused ] ||
12815                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
12816
12817         rm $DIR/$tdir/m
12818         unlinkmany $DIR/$tdir/f $nr
12819 }
12820 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
12821
12822 test_134b() {
12823         remote_mds_nodsh && skip "remote MDS with nodsh"
12824         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12825                 skip "Need MDS version at least 2.7.54"
12826
12827         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12828         cancel_lru_locks mdc
12829
12830         local low_wm=$(do_facet mds1 $LCTL get_param -n \
12831                         ldlm.lock_reclaim_threshold_mb)
12832         # disable reclaim temporarily
12833         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
12834
12835         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
12836         do_facet mds1 $LCTL set_param fail_loc=0x328
12837         do_facet mds1 $LCTL set_param fail_val=500
12838
12839         $LCTL set_param debug=+trace
12840
12841         local nr=600
12842         createmany -o $DIR/$tdir/f $nr &
12843         local create_pid=$!
12844
12845         echo "Sleep $TIMEOUT seconds ..."
12846         sleep $TIMEOUT
12847         if ! ps -p $create_pid  > /dev/null 2>&1; then
12848                 do_facet mds1 $LCTL set_param fail_loc=0
12849                 do_facet mds1 $LCTL set_param fail_val=0
12850                 do_facet mds1 $LCTL set_param \
12851                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
12852                 error "createmany finished incorrectly!"
12853         fi
12854         do_facet mds1 $LCTL set_param fail_loc=0
12855         do_facet mds1 $LCTL set_param fail_val=0
12856         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
12857         wait $create_pid || return 1
12858
12859         unlinkmany $DIR/$tdir/f $nr
12860 }
12861 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
12862
12863 test_135() {
12864         remote_mds_nodsh && skip "remote MDS with nodsh"
12865         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
12866                 skip "Need MDS version at least 2.13.50"
12867         local fname
12868
12869         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12870
12871 #define OBD_FAIL_PLAIN_RECORDS 0x1319
12872         #set only one record at plain llog
12873         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1319 fail_val=1
12874
12875         #fill already existed plain llog each 64767
12876         #wrapping whole catalog
12877         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
12878
12879         createmany -o $DIR/$tdir/$tfile_ 64700
12880         for (( i = 0; i < 64700; i = i + 2 ))
12881         do
12882                 rm $DIR/$tdir/$tfile_$i &
12883                 rm $DIR/$tdir/$tfile_$((i + 1)) &
12884                 local pid=$!
12885                 wait $pid
12886         done
12887
12888         #waiting osp synchronization
12889         wait_delete_completed
12890 }
12891 run_test 135 "Race catalog processing"
12892
12893 test_136() {
12894         remote_mds_nodsh && skip "remote MDS with nodsh"
12895         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
12896                 skip "Need MDS version at least 2.13.50"
12897         local fname
12898
12899         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12900         $LFS setstripe -c 1 -i 0 $DIR/$tdir || error "failed to set striping"
12901         #set only one record at plain llog
12902 #define OBD_FAIL_CATALOG_FULL_CHECK                0x131a
12903         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x131a fail_val=1
12904
12905         #fill already existed 2 plain llogs each 64767
12906         #wrapping whole catalog
12907         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
12908         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 3 / 2))
12909         wait_delete_completed
12910
12911         createmany -o $DIR/$tdir/$tfile_ 10
12912         sleep 25
12913
12914         do_facet $SINGLEMDS $LCTL set_param fail_val=3
12915         for (( i = 0; i < 10; i = i + 3 ))
12916         do
12917                 rm $DIR/$tdir/$tfile_$i &
12918                 rm $DIR/$tdir/$tfile_$((i + 1)) &
12919                 local pid=$!
12920                 wait $pid
12921                 sleep 7
12922                 rm $DIR/$tdir/$tfile_$((i + 2)) &
12923         done
12924
12925         #waiting osp synchronization
12926         wait_delete_completed
12927 }
12928 run_test 136 "Race catalog processing 2"
12929
12930 test_140() { #bug-17379
12931         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12932
12933         test_mkdir $DIR/$tdir
12934         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
12935         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
12936
12937         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
12938         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
12939         local i=0
12940         while i=$((i + 1)); do
12941                 test_mkdir $i
12942                 cd $i || error "Changing to $i"
12943                 ln -s ../stat stat || error "Creating stat symlink"
12944                 # Read the symlink until ELOOP present,
12945                 # not LBUGing the system is considered success,
12946                 # we didn't overrun the stack.
12947                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
12948                 if [ $ret -ne 0 ]; then
12949                         if [ $ret -eq 40 ]; then
12950                                 break  # -ELOOP
12951                         else
12952                                 error "Open stat symlink"
12953                                         return
12954                         fi
12955                 fi
12956         done
12957         i=$((i - 1))
12958         echo "The symlink depth = $i"
12959         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
12960                 error "Invalid symlink depth"
12961
12962         # Test recursive symlink
12963         ln -s symlink_self symlink_self
12964         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
12965         echo "open symlink_self returns $ret"
12966         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
12967 }
12968 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
12969
12970 test_150() {
12971         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12972
12973         local TF="$TMP/$tfile"
12974
12975         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12976         cp $TF $DIR/$tfile
12977         cancel_lru_locks $OSC
12978         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
12979         remount_client $MOUNT
12980         df -P $MOUNT
12981         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
12982
12983         $TRUNCATE $TF 6000
12984         $TRUNCATE $DIR/$tfile 6000
12985         cancel_lru_locks $OSC
12986         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
12987
12988         echo "12345" >>$TF
12989         echo "12345" >>$DIR/$tfile
12990         cancel_lru_locks $OSC
12991         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
12992
12993         echo "12345" >>$TF
12994         echo "12345" >>$DIR/$tfile
12995         cancel_lru_locks $OSC
12996         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
12997
12998         rm -f $TF
12999         true
13000 }
13001 run_test 150 "truncate/append tests"
13002
13003 #LU-2902 roc_hit was not able to read all values from lproc
13004 function roc_hit_init() {
13005         local list=$(comma_list $(osts_nodes))
13006         local dir=$DIR/$tdir-check
13007         local file=$dir/$tfile
13008         local BEFORE
13009         local AFTER
13010         local idx
13011
13012         test_mkdir $dir
13013         #use setstripe to do a write to every ost
13014         for i in $(seq 0 $((OSTCOUNT-1))); do
13015                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
13016                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
13017                 idx=$(printf %04x $i)
13018                 BEFORE=$(get_osd_param $list *OST*$idx stats |
13019                         awk '$1 == "cache_access" {sum += $7}
13020                                 END { printf("%0.0f", sum) }')
13021
13022                 cancel_lru_locks osc
13023                 cat $file >/dev/null
13024
13025                 AFTER=$(get_osd_param $list *OST*$idx stats |
13026                         awk '$1 == "cache_access" {sum += $7}
13027                                 END { printf("%0.0f", sum) }')
13028
13029                 echo BEFORE:$BEFORE AFTER:$AFTER
13030                 if ! let "AFTER - BEFORE == 4"; then
13031                         rm -rf $dir
13032                         error "roc_hit is not safe to use"
13033                 fi
13034                 rm $file
13035         done
13036
13037         rm -rf $dir
13038 }
13039
13040 function roc_hit() {
13041         local list=$(comma_list $(osts_nodes))
13042         echo $(get_osd_param $list '' stats |
13043                 awk '$1 == "cache_hit" {sum += $7}
13044                         END { printf("%0.0f", sum) }')
13045 }
13046
13047 function set_cache() {
13048         local on=1
13049
13050         if [ "$2" == "off" ]; then
13051                 on=0;
13052         fi
13053         local list=$(comma_list $(osts_nodes))
13054         set_osd_param $list '' $1_cache_enable $on
13055
13056         cancel_lru_locks osc
13057 }
13058
13059 test_151() {
13060         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13061         remote_ost_nodsh && skip "remote OST with nodsh"
13062
13063         local CPAGES=3
13064         local list=$(comma_list $(osts_nodes))
13065
13066         # check whether obdfilter is cache capable at all
13067         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
13068                 skip "not cache-capable obdfilter"
13069         fi
13070
13071         # check cache is enabled on all obdfilters
13072         if get_osd_param $list '' read_cache_enable | grep 0; then
13073                 skip "oss cache is disabled"
13074         fi
13075
13076         set_osd_param $list '' writethrough_cache_enable 1
13077
13078         # check write cache is enabled on all obdfilters
13079         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
13080                 skip "oss write cache is NOT enabled"
13081         fi
13082
13083         roc_hit_init
13084
13085         #define OBD_FAIL_OBD_NO_LRU  0x609
13086         do_nodes $list $LCTL set_param fail_loc=0x609
13087
13088         # pages should be in the case right after write
13089         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
13090                 error "dd failed"
13091
13092         local BEFORE=$(roc_hit)
13093         cancel_lru_locks osc
13094         cat $DIR/$tfile >/dev/null
13095         local AFTER=$(roc_hit)
13096
13097         do_nodes $list $LCTL set_param fail_loc=0
13098
13099         if ! let "AFTER - BEFORE == CPAGES"; then
13100                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13101         fi
13102
13103         cancel_lru_locks osc
13104         # invalidates OST cache
13105         do_nodes $list "echo 1 > /proc/sys/vm/drop_caches"
13106         set_osd_param $list '' read_cache_enable 0
13107         cat $DIR/$tfile >/dev/null
13108
13109         # now data shouldn't be found in the cache
13110         BEFORE=$(roc_hit)
13111         cancel_lru_locks osc
13112         cat $DIR/$tfile >/dev/null
13113         AFTER=$(roc_hit)
13114         if let "AFTER - BEFORE != 0"; then
13115                 error "IN CACHE: before: $BEFORE, after: $AFTER"
13116         fi
13117
13118         set_osd_param $list '' read_cache_enable 1
13119         rm -f $DIR/$tfile
13120 }
13121 run_test 151 "test cache on oss and controls ==============================="
13122
13123 test_152() {
13124         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13125
13126         local TF="$TMP/$tfile"
13127
13128         # simulate ENOMEM during write
13129 #define OBD_FAIL_OST_NOMEM      0x226
13130         lctl set_param fail_loc=0x80000226
13131         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
13132         cp $TF $DIR/$tfile
13133         sync || error "sync failed"
13134         lctl set_param fail_loc=0
13135
13136         # discard client's cache
13137         cancel_lru_locks osc
13138
13139         # simulate ENOMEM during read
13140         lctl set_param fail_loc=0x80000226
13141         cmp $TF $DIR/$tfile || error "cmp failed"
13142         lctl set_param fail_loc=0
13143
13144         rm -f $TF
13145 }
13146 run_test 152 "test read/write with enomem ============================"
13147
13148 test_153() {
13149         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
13150 }
13151 run_test 153 "test if fdatasync does not crash ======================="
13152
13153 dot_lustre_fid_permission_check() {
13154         local fid=$1
13155         local ffid=$MOUNT/.lustre/fid/$fid
13156         local test_dir=$2
13157
13158         echo "stat fid $fid"
13159         stat $ffid > /dev/null || error "stat $ffid failed."
13160         echo "touch fid $fid"
13161         touch $ffid || error "touch $ffid failed."
13162         echo "write to fid $fid"
13163         cat /etc/hosts > $ffid || error "write $ffid failed."
13164         echo "read fid $fid"
13165         diff /etc/hosts $ffid || error "read $ffid failed."
13166         echo "append write to fid $fid"
13167         cat /etc/hosts >> $ffid || error "append write $ffid failed."
13168         echo "rename fid $fid"
13169         mv $ffid $test_dir/$tfile.1 &&
13170                 error "rename $ffid to $tfile.1 should fail."
13171         touch $test_dir/$tfile.1
13172         mv $test_dir/$tfile.1 $ffid &&
13173                 error "rename $tfile.1 to $ffid should fail."
13174         rm -f $test_dir/$tfile.1
13175         echo "truncate fid $fid"
13176         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
13177         echo "link fid $fid"
13178         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
13179         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
13180                 echo "setfacl fid $fid"
13181                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
13182                 echo "getfacl fid $fid"
13183                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
13184         fi
13185         echo "unlink fid $fid"
13186         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
13187         echo "mknod fid $fid"
13188         mknod $ffid c 1 3 && error "mknod $ffid should fail."
13189
13190         fid=[0xf00000400:0x1:0x0]
13191         ffid=$MOUNT/.lustre/fid/$fid
13192
13193         echo "stat non-exist fid $fid"
13194         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
13195         echo "write to non-exist fid $fid"
13196         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
13197         echo "link new fid $fid"
13198         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
13199
13200         mkdir -p $test_dir/$tdir
13201         touch $test_dir/$tdir/$tfile
13202         fid=$($LFS path2fid $test_dir/$tdir)
13203         rc=$?
13204         [ $rc -ne 0 ] &&
13205                 error "error: could not get fid for $test_dir/$dir/$tfile."
13206
13207         ffid=$MOUNT/.lustre/fid/$fid
13208
13209         echo "ls $fid"
13210         ls $ffid > /dev/null || error "ls $ffid failed."
13211         echo "touch $fid/$tfile.1"
13212         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
13213
13214         echo "touch $MOUNT/.lustre/fid/$tfile"
13215         touch $MOUNT/.lustre/fid/$tfile && \
13216                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
13217
13218         echo "setxattr to $MOUNT/.lustre/fid"
13219         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
13220
13221         echo "listxattr for $MOUNT/.lustre/fid"
13222         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
13223
13224         echo "delxattr from $MOUNT/.lustre/fid"
13225         setfattr -x trusted.name1 $MOUNT/.lustre/fid
13226
13227         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
13228         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
13229                 error "touch invalid fid should fail."
13230
13231         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
13232         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
13233                 error "touch non-normal fid should fail."
13234
13235         echo "rename $tdir to $MOUNT/.lustre/fid"
13236         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
13237                 error "rename to $MOUNT/.lustre/fid should fail."
13238
13239         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
13240         then            # LU-3547
13241                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
13242                 local new_obf_mode=777
13243
13244                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
13245                 chmod $new_obf_mode $DIR/.lustre/fid ||
13246                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
13247
13248                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
13249                 [ $obf_mode -eq $new_obf_mode ] ||
13250                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
13251
13252                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
13253                 chmod $old_obf_mode $DIR/.lustre/fid ||
13254                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
13255         fi
13256
13257         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
13258         fid=$($LFS path2fid $test_dir/$tfile-2)
13259
13260         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
13261         then # LU-5424
13262                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
13263                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
13264                         error "create lov data thru .lustre failed"
13265         fi
13266         echo "cp /etc/passwd $test_dir/$tfile-2"
13267         cp /etc/passwd $test_dir/$tfile-2 ||
13268                 error "copy to $test_dir/$tfile-2 failed."
13269         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
13270         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
13271                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
13272
13273         rm -rf $test_dir/tfile.lnk
13274         rm -rf $test_dir/$tfile-2
13275 }
13276
13277 test_154A() {
13278         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13279                 skip "Need MDS version at least 2.4.1"
13280
13281         local tf=$DIR/$tfile
13282         touch $tf
13283
13284         local fid=$($LFS path2fid $tf)
13285         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
13286
13287         # check that we get the same pathname back
13288         local found=$($LFS fid2path $MOUNT "$fid")
13289         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
13290         [ "$found" == "$tf" ] ||
13291                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
13292 }
13293 run_test 154A "lfs path2fid and fid2path basic checks"
13294
13295 test_154B() {
13296         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13297                 skip "Need MDS version at least 2.4.1"
13298
13299         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
13300         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
13301         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
13302         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
13303
13304         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
13305         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
13306
13307         # check that we get the same pathname
13308         echo "PFID: $PFID, name: $name"
13309         local FOUND=$($LFS fid2path $MOUNT "$PFID")
13310         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
13311         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
13312                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
13313
13314         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
13315 }
13316 run_test 154B "verify the ll_decode_linkea tool"
13317
13318 test_154a() {
13319         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13320         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13321         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
13322                 skip "Need MDS version at least 2.2.51"
13323         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
13324
13325         cp /etc/hosts $DIR/$tfile
13326
13327         fid=$($LFS path2fid $DIR/$tfile)
13328         rc=$?
13329         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
13330
13331         dot_lustre_fid_permission_check "$fid" $DIR ||
13332                 error "dot lustre permission check $fid failed"
13333
13334         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
13335
13336         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
13337
13338         touch $MOUNT/.lustre/file &&
13339                 error "creation is not allowed under .lustre"
13340
13341         mkdir $MOUNT/.lustre/dir &&
13342                 error "mkdir is not allowed under .lustre"
13343
13344         rm -rf $DIR/$tfile
13345 }
13346 run_test 154a "Open-by-FID"
13347
13348 test_154b() {
13349         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13350         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13351         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13352         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
13353                 skip "Need MDS version at least 2.2.51"
13354
13355         local remote_dir=$DIR/$tdir/remote_dir
13356         local MDTIDX=1
13357         local rc=0
13358
13359         mkdir -p $DIR/$tdir
13360         $LFS mkdir -i $MDTIDX $remote_dir ||
13361                 error "create remote directory failed"
13362
13363         cp /etc/hosts $remote_dir/$tfile
13364
13365         fid=$($LFS path2fid $remote_dir/$tfile)
13366         rc=$?
13367         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
13368
13369         dot_lustre_fid_permission_check "$fid" $remote_dir ||
13370                 error "dot lustre permission check $fid failed"
13371         rm -rf $DIR/$tdir
13372 }
13373 run_test 154b "Open-by-FID for remote directory"
13374
13375 test_154c() {
13376         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13377                 skip "Need MDS version at least 2.4.1"
13378
13379         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
13380         local FID1=$($LFS path2fid $DIR/$tfile.1)
13381         local FID2=$($LFS path2fid $DIR/$tfile.2)
13382         local FID3=$($LFS path2fid $DIR/$tfile.3)
13383
13384         local N=1
13385         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
13386                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
13387                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
13388                 local want=FID$N
13389                 [ "$FID" = "${!want}" ] ||
13390                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
13391                 N=$((N + 1))
13392         done
13393
13394         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
13395         do
13396                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
13397                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
13398                 N=$((N + 1))
13399         done
13400 }
13401 run_test 154c "lfs path2fid and fid2path multiple arguments"
13402
13403 test_154d() {
13404         remote_mds_nodsh && skip "remote MDS with nodsh"
13405         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
13406                 skip "Need MDS version at least 2.5.53"
13407
13408         if remote_mds; then
13409                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
13410         else
13411                 nid="0@lo"
13412         fi
13413         local proc_ofile="mdt.*.exports.'$nid'.open_files"
13414         local fd
13415         local cmd
13416
13417         rm -f $DIR/$tfile
13418         touch $DIR/$tfile
13419
13420         local fid=$($LFS path2fid $DIR/$tfile)
13421         # Open the file
13422         fd=$(free_fd)
13423         cmd="exec $fd<$DIR/$tfile"
13424         eval $cmd
13425         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
13426         echo "$fid_list" | grep "$fid"
13427         rc=$?
13428
13429         cmd="exec $fd>/dev/null"
13430         eval $cmd
13431         if [ $rc -ne 0 ]; then
13432                 error "FID $fid not found in open files list $fid_list"
13433         fi
13434 }
13435 run_test 154d "Verify open file fid"
13436
13437 test_154e()
13438 {
13439         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
13440                 skip "Need MDS version at least 2.6.50"
13441
13442         if ls -a $MOUNT | grep -q '^\.lustre$'; then
13443                 error ".lustre returned by readdir"
13444         fi
13445 }
13446 run_test 154e ".lustre is not returned by readdir"
13447
13448 test_154f() {
13449         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13450
13451         # create parent directory on a single MDT to avoid cross-MDT hardlinks
13452         test_mkdir -p -c1 $DIR/$tdir/d
13453         # test dirs inherit from its stripe
13454         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
13455         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
13456         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
13457         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
13458         touch $DIR/f
13459
13460         # get fid of parents
13461         local FID0=$($LFS path2fid $DIR/$tdir/d)
13462         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
13463         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
13464         local FID3=$($LFS path2fid $DIR)
13465
13466         # check that path2fid --parents returns expected <parent_fid>/name
13467         # 1) test for a directory (single parent)
13468         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
13469         [ "$parent" == "$FID0/foo1" ] ||
13470                 error "expected parent: $FID0/foo1, got: $parent"
13471
13472         # 2) test for a file with nlink > 1 (multiple parents)
13473         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
13474         echo "$parent" | grep -F "$FID1/$tfile" ||
13475                 error "$FID1/$tfile not returned in parent list"
13476         echo "$parent" | grep -F "$FID2/link" ||
13477                 error "$FID2/link not returned in parent list"
13478
13479         # 3) get parent by fid
13480         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
13481         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13482         echo "$parent" | grep -F "$FID1/$tfile" ||
13483                 error "$FID1/$tfile not returned in parent list (by fid)"
13484         echo "$parent" | grep -F "$FID2/link" ||
13485                 error "$FID2/link not returned in parent list (by fid)"
13486
13487         # 4) test for entry in root directory
13488         parent=$($LFS path2fid --parents $DIR/f)
13489         echo "$parent" | grep -F "$FID3/f" ||
13490                 error "$FID3/f not returned in parent list"
13491
13492         # 5) test it on root directory
13493         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
13494                 error "$MOUNT should not have parents"
13495
13496         # enable xattr caching and check that linkea is correctly updated
13497         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
13498         save_lustre_params client "llite.*.xattr_cache" > $save
13499         lctl set_param llite.*.xattr_cache 1
13500
13501         # 6.1) linkea update on rename
13502         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
13503
13504         # get parents by fid
13505         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13506         # foo1 should no longer be returned in parent list
13507         echo "$parent" | grep -F "$FID1" &&
13508                 error "$FID1 should no longer be in parent list"
13509         # the new path should appear
13510         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
13511                 error "$FID2/$tfile.moved is not in parent list"
13512
13513         # 6.2) linkea update on unlink
13514         rm -f $DIR/$tdir/d/foo2/link
13515         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13516         # foo2/link should no longer be returned in parent list
13517         echo "$parent" | grep -F "$FID2/link" &&
13518                 error "$FID2/link should no longer be in parent list"
13519         true
13520
13521         rm -f $DIR/f
13522         restore_lustre_params < $save
13523         rm -f $save
13524 }
13525 run_test 154f "get parent fids by reading link ea"
13526
13527 test_154g()
13528 {
13529         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13530         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
13531            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
13532                 skip "Need MDS version at least 2.6.92"
13533
13534         mkdir -p $DIR/$tdir
13535         llapi_fid_test -d $DIR/$tdir
13536 }
13537 run_test 154g "various llapi FID tests"
13538
13539 test_155_small_load() {
13540     local temp=$TMP/$tfile
13541     local file=$DIR/$tfile
13542
13543     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
13544         error "dd of=$temp bs=6096 count=1 failed"
13545     cp $temp $file
13546     cancel_lru_locks $OSC
13547     cmp $temp $file || error "$temp $file differ"
13548
13549     $TRUNCATE $temp 6000
13550     $TRUNCATE $file 6000
13551     cmp $temp $file || error "$temp $file differ (truncate1)"
13552
13553     echo "12345" >>$temp
13554     echo "12345" >>$file
13555     cmp $temp $file || error "$temp $file differ (append1)"
13556
13557     echo "12345" >>$temp
13558     echo "12345" >>$file
13559     cmp $temp $file || error "$temp $file differ (append2)"
13560
13561     rm -f $temp $file
13562     true
13563 }
13564
13565 test_155_big_load() {
13566         remote_ost_nodsh && skip "remote OST with nodsh"
13567
13568         local temp=$TMP/$tfile
13569         local file=$DIR/$tfile
13570
13571         free_min_max
13572         local cache_size=$(do_facet ost$((MAXI+1)) \
13573                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
13574         local large_file_size=$((cache_size * 2))
13575
13576         echo "OSS cache size: $cache_size KB"
13577         echo "Large file size: $large_file_size KB"
13578
13579         [ $MAXV -le $large_file_size ] &&
13580                 skip_env "max available OST size needs > $large_file_size KB"
13581
13582         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
13583
13584         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
13585                 error "dd of=$temp bs=$large_file_size count=1k failed"
13586         cp $temp $file
13587         ls -lh $temp $file
13588         cancel_lru_locks osc
13589         cmp $temp $file || error "$temp $file differ"
13590
13591         rm -f $temp $file
13592         true
13593 }
13594
13595 save_writethrough() {
13596         local facets=$(get_facets OST)
13597
13598         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
13599 }
13600
13601 test_155a() {
13602         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13603
13604         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13605
13606         save_writethrough $p
13607
13608         set_cache read on
13609         set_cache writethrough on
13610         test_155_small_load
13611         restore_lustre_params < $p
13612         rm -f $p
13613 }
13614 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
13615
13616 test_155b() {
13617         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13618
13619         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13620
13621         save_writethrough $p
13622
13623         set_cache read on
13624         set_cache writethrough off
13625         test_155_small_load
13626         restore_lustre_params < $p
13627         rm -f $p
13628 }
13629 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
13630
13631 test_155c() {
13632         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13633
13634         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13635
13636         save_writethrough $p
13637
13638         set_cache read off
13639         set_cache writethrough on
13640         test_155_small_load
13641         restore_lustre_params < $p
13642         rm -f $p
13643 }
13644 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
13645
13646 test_155d() {
13647         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13648
13649         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13650
13651         save_writethrough $p
13652
13653         set_cache read off
13654         set_cache writethrough off
13655         test_155_small_load
13656         restore_lustre_params < $p
13657         rm -f $p
13658 }
13659 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
13660
13661 test_155e() {
13662         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13663
13664         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13665
13666         save_writethrough $p
13667
13668         set_cache read on
13669         set_cache writethrough on
13670         test_155_big_load
13671         restore_lustre_params < $p
13672         rm -f $p
13673 }
13674 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
13675
13676 test_155f() {
13677         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13678
13679         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13680
13681         save_writethrough $p
13682
13683         set_cache read on
13684         set_cache writethrough off
13685         test_155_big_load
13686         restore_lustre_params < $p
13687         rm -f $p
13688 }
13689 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
13690
13691 test_155g() {
13692         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13693
13694         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13695
13696         save_writethrough $p
13697
13698         set_cache read off
13699         set_cache writethrough on
13700         test_155_big_load
13701         restore_lustre_params < $p
13702         rm -f $p
13703 }
13704 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
13705
13706 test_155h() {
13707         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13708
13709         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13710
13711         save_writethrough $p
13712
13713         set_cache read off
13714         set_cache writethrough off
13715         test_155_big_load
13716         restore_lustre_params < $p
13717         rm -f $p
13718 }
13719 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
13720
13721 test_156() {
13722         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13723         remote_ost_nodsh && skip "remote OST with nodsh"
13724         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
13725                 skip "stats not implemented on old servers"
13726         [ "$ost1_FSTYPE" = "zfs" ] &&
13727                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
13728
13729         local CPAGES=3
13730         local BEFORE
13731         local AFTER
13732         local file="$DIR/$tfile"
13733         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13734
13735         save_writethrough $p
13736         roc_hit_init
13737
13738         log "Turn on read and write cache"
13739         set_cache read on
13740         set_cache writethrough on
13741
13742         log "Write data and read it back."
13743         log "Read should be satisfied from the cache."
13744         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13745         BEFORE=$(roc_hit)
13746         cancel_lru_locks osc
13747         cat $file >/dev/null
13748         AFTER=$(roc_hit)
13749         if ! let "AFTER - BEFORE == CPAGES"; then
13750                 error "NOT IN CACHE (2): before: $BEFORE, after: $AFTER"
13751         else
13752                 log "cache hits: before: $BEFORE, after: $AFTER"
13753         fi
13754
13755         log "Read again; it should be satisfied from the cache."
13756         BEFORE=$AFTER
13757         cancel_lru_locks osc
13758         cat $file >/dev/null
13759         AFTER=$(roc_hit)
13760         if ! let "AFTER - BEFORE == CPAGES"; then
13761                 error "NOT IN CACHE (3): before: $BEFORE, after: $AFTER"
13762         else
13763                 log "cache hits:: before: $BEFORE, after: $AFTER"
13764         fi
13765
13766         log "Turn off the read cache and turn on the write cache"
13767         set_cache read off
13768         set_cache writethrough on
13769
13770         log "Read again; it should be satisfied from the cache."
13771         BEFORE=$(roc_hit)
13772         cancel_lru_locks osc
13773         cat $file >/dev/null
13774         AFTER=$(roc_hit)
13775         if ! let "AFTER - BEFORE == CPAGES"; then
13776                 error "NOT IN CACHE (4): before: $BEFORE, after: $AFTER"
13777         else
13778                 log "cache hits:: before: $BEFORE, after: $AFTER"
13779         fi
13780
13781         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
13782                 # > 2.12.56 uses pagecache if cached
13783                 log "Read again; it should not be satisfied from the cache."
13784                 BEFORE=$AFTER
13785                 cancel_lru_locks osc
13786                 cat $file >/dev/null
13787                 AFTER=$(roc_hit)
13788                 if ! let "AFTER - BEFORE == 0"; then
13789                         error "IN CACHE (5): before: $BEFORE, after: $AFTER"
13790                 else
13791                         log "cache hits:: before: $BEFORE, after: $AFTER"
13792                 fi
13793         fi
13794
13795         log "Write data and read it back."
13796         log "Read should be satisfied from the cache."
13797         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13798         BEFORE=$(roc_hit)
13799         cancel_lru_locks osc
13800         cat $file >/dev/null
13801         AFTER=$(roc_hit)
13802         if ! let "AFTER - BEFORE == CPAGES"; then
13803                 error "NOT IN CACHE (6): before: $BEFORE, after: $AFTER"
13804         else
13805                 log "cache hits:: before: $BEFORE, after: $AFTER"
13806         fi
13807
13808         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
13809                 # > 2.12.56 uses pagecache if cached
13810                 log "Read again; it should not be satisfied from the cache."
13811                 BEFORE=$AFTER
13812                 cancel_lru_locks osc
13813                 cat $file >/dev/null
13814                 AFTER=$(roc_hit)
13815                 if ! let "AFTER - BEFORE == 0"; then
13816                         error "IN CACHE (7): before: $BEFORE, after: $AFTER"
13817                 else
13818                         log "cache hits:: before: $BEFORE, after: $AFTER"
13819                 fi
13820         fi
13821
13822         log "Turn off read and write cache"
13823         set_cache read off
13824         set_cache writethrough off
13825
13826         log "Write data and read it back"
13827         log "It should not be satisfied from the cache."
13828         rm -f $file
13829         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13830         cancel_lru_locks osc
13831         BEFORE=$(roc_hit)
13832         cat $file >/dev/null
13833         AFTER=$(roc_hit)
13834         if ! let "AFTER - BEFORE == 0"; then
13835                 error_ignore bz20762 "IN CACHE (8):before:$BEFORE,after:$AFTER"
13836         else
13837                 log "cache hits:: before: $BEFORE, after: $AFTER"
13838         fi
13839
13840         log "Turn on the read cache and turn off the write cache"
13841         set_cache read on
13842         set_cache writethrough off
13843
13844         log "Write data and read it back"
13845         log "It should not be satisfied from the cache."
13846         rm -f $file
13847         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13848         BEFORE=$(roc_hit)
13849         cancel_lru_locks osc
13850         cat $file >/dev/null
13851         AFTER=$(roc_hit)
13852         if ! let "AFTER - BEFORE == 0"; then
13853                 error_ignore bz20762 "IN CACHE (9):before:$BEFORE,after:$AFTER"
13854         else
13855                 log "cache hits:: before: $BEFORE, after: $AFTER"
13856         fi
13857
13858         log "Read again; it should be satisfied from the cache."
13859         BEFORE=$(roc_hit)
13860         cancel_lru_locks osc
13861         cat $file >/dev/null
13862         AFTER=$(roc_hit)
13863         if ! let "AFTER - BEFORE == CPAGES"; then
13864                 error "NOT IN CACHE (1): before: $BEFORE, after: $AFTER"
13865         else
13866                 log "cache hits:: before: $BEFORE, after: $AFTER"
13867         fi
13868
13869         restore_lustre_params < $p
13870         rm -f $p $file
13871 }
13872 run_test 156 "Verification of tunables"
13873
13874 test_160a() {
13875         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13876         remote_mds_nodsh && skip "remote MDS with nodsh"
13877         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13878                 skip "Need MDS version at least 2.2.0"
13879
13880         changelog_register || error "changelog_register failed"
13881         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13882         changelog_users $SINGLEMDS | grep -q $cl_user ||
13883                 error "User $cl_user not found in changelog_users"
13884
13885         # change something
13886         test_mkdir -p $DIR/$tdir/pics/2008/zachy
13887         changelog_clear 0 || error "changelog_clear failed"
13888         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
13889         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
13890         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
13891         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
13892         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
13893         rm $DIR/$tdir/pics/desktop.jpg
13894
13895         changelog_dump | tail -10
13896
13897         echo "verifying changelog mask"
13898         changelog_chmask "-MKDIR"
13899         changelog_chmask "-CLOSE"
13900
13901         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
13902         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
13903
13904         changelog_chmask "+MKDIR"
13905         changelog_chmask "+CLOSE"
13906
13907         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
13908         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
13909
13910         changelog_dump | tail -10
13911         MKDIRS=$(changelog_dump | grep -c "MKDIR")
13912         CLOSES=$(changelog_dump | grep -c "CLOSE")
13913         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
13914         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
13915
13916         # verify contents
13917         echo "verifying target fid"
13918         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
13919         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
13920         [ "$fidc" == "$fidf" ] ||
13921                 error "changelog '$tfile' fid $fidc != file fid $fidf"
13922         echo "verifying parent fid"
13923         # The FID returned from the Changelog may be the directory shard on
13924         # a different MDT, and not the FID returned by path2fid on the parent.
13925         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
13926         # since this is what will matter when recreating this file in the tree.
13927         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
13928         local pathp=$($LFS fid2path $MOUNT "$fidp")
13929         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
13930                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
13931
13932         echo "getting records for $cl_user"
13933         changelog_users $SINGLEMDS
13934         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
13935         local nclr=3
13936         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
13937                 error "changelog_clear failed"
13938         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
13939         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
13940         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
13941                 error "user index expect $user_rec1 + $nclr != $user_rec2"
13942
13943         local min0_rec=$(changelog_users $SINGLEMDS |
13944                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
13945         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
13946                           awk '{ print $1; exit; }')
13947
13948         changelog_dump | tail -n 5
13949         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
13950         [ $first_rec == $((min0_rec + 1)) ] ||
13951                 error "first index should be $min0_rec + 1 not $first_rec"
13952
13953         # LU-3446 changelog index reset on MDT restart
13954         local cur_rec1=$(changelog_users $SINGLEMDS |
13955                          awk '/^current.index:/ { print $NF }')
13956         changelog_clear 0 ||
13957                 error "clear all changelog records for $cl_user failed"
13958         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
13959         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
13960                 error "Fail to start $SINGLEMDS"
13961         local cur_rec2=$(changelog_users $SINGLEMDS |
13962                          awk '/^current.index:/ { print $NF }')
13963         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
13964         [ $cur_rec1 == $cur_rec2 ] ||
13965                 error "current index should be $cur_rec1 not $cur_rec2"
13966
13967         echo "verifying users from this test are deregistered"
13968         changelog_deregister || error "changelog_deregister failed"
13969         changelog_users $SINGLEMDS | grep -q $cl_user &&
13970                 error "User '$cl_user' still in changelog_users"
13971
13972         # lctl get_param -n mdd.*.changelog_users
13973         # current index: 144
13974         # ID    index (idle seconds)
13975         # cl3   144 (2)
13976         if ! changelog_users $SINGLEMDS | grep "^cl"; then
13977                 # this is the normal case where all users were deregistered
13978                 # make sure no new records are added when no users are present
13979                 local last_rec1=$(changelog_users $SINGLEMDS |
13980                                   awk '/^current.index:/ { print $NF }')
13981                 touch $DIR/$tdir/chloe
13982                 local last_rec2=$(changelog_users $SINGLEMDS |
13983                                   awk '/^current.index:/ { print $NF }')
13984                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
13985                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
13986         else
13987                 # any changelog users must be leftovers from a previous test
13988                 changelog_users $SINGLEMDS
13989                 echo "other changelog users; can't verify off"
13990         fi
13991 }
13992 run_test 160a "changelog sanity"
13993
13994 test_160b() { # LU-3587
13995         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13996         remote_mds_nodsh && skip "remote MDS with nodsh"
13997         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13998                 skip "Need MDS version at least 2.2.0"
13999
14000         changelog_register || error "changelog_register failed"
14001         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14002         changelog_users $SINGLEMDS | grep -q $cl_user ||
14003                 error "User '$cl_user' not found in changelog_users"
14004
14005         local longname1=$(str_repeat a 255)
14006         local longname2=$(str_repeat b 255)
14007
14008         cd $DIR
14009         echo "creating very long named file"
14010         touch $longname1 || error "create of '$longname1' failed"
14011         echo "renaming very long named file"
14012         mv $longname1 $longname2
14013
14014         changelog_dump | grep RENME | tail -n 5
14015         rm -f $longname2
14016 }
14017 run_test 160b "Verify that very long rename doesn't crash in changelog"
14018
14019 test_160c() {
14020         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14021         remote_mds_nodsh && skip "remote MDS with nodsh"
14022
14023         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
14024                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
14025                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
14026                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
14027
14028         local rc=0
14029
14030         # Registration step
14031         changelog_register || error "changelog_register failed"
14032
14033         rm -rf $DIR/$tdir
14034         mkdir -p $DIR/$tdir
14035         $MCREATE $DIR/$tdir/foo_160c
14036         changelog_chmask "-TRUNC"
14037         $TRUNCATE $DIR/$tdir/foo_160c 200
14038         changelog_chmask "+TRUNC"
14039         $TRUNCATE $DIR/$tdir/foo_160c 199
14040         changelog_dump | tail -n 5
14041         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
14042         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
14043 }
14044 run_test 160c "verify that changelog log catch the truncate event"
14045
14046 test_160d() {
14047         remote_mds_nodsh && skip "remote MDS with nodsh"
14048         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14049         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14050         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
14051                 skip "Need MDS version at least 2.7.60"
14052
14053         # Registration step
14054         changelog_register || error "changelog_register failed"
14055
14056         mkdir -p $DIR/$tdir/migrate_dir
14057         changelog_clear 0 || error "changelog_clear failed"
14058
14059         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
14060         changelog_dump | tail -n 5
14061         local migrates=$(changelog_dump | grep -c "MIGRT")
14062         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
14063 }
14064 run_test 160d "verify that changelog log catch the migrate event"
14065
14066 test_160e() {
14067         remote_mds_nodsh && skip "remote MDS with nodsh"
14068
14069         # Create a user
14070         changelog_register || error "changelog_register failed"
14071
14072         # Delete a future user (expect fail)
14073         local MDT0=$(facet_svc $SINGLEMDS)
14074         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
14075         local rc=$?
14076
14077         if [ $rc -eq 0 ]; then
14078                 error "Deleted non-existant user cl77"
14079         elif [ $rc -ne 2 ]; then
14080                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
14081         fi
14082
14083         # Clear to a bad index (1 billion should be safe)
14084         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
14085         rc=$?
14086
14087         if [ $rc -eq 0 ]; then
14088                 error "Successfully cleared to invalid CL index"
14089         elif [ $rc -ne 22 ]; then
14090                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
14091         fi
14092 }
14093 run_test 160e "changelog negative testing (should return errors)"
14094
14095 test_160f() {
14096         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14097         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14098                 skip "Need MDS version at least 2.10.56"
14099
14100         local mdts=$(comma_list $(mdts_nodes))
14101
14102         # Create a user
14103         changelog_register || error "first changelog_register failed"
14104         changelog_register || error "second changelog_register failed"
14105         local cl_users
14106         declare -A cl_user1
14107         declare -A cl_user2
14108         local user_rec1
14109         local user_rec2
14110         local i
14111
14112         # generate some changelog records to accumulate on each MDT
14113         # use fnv1a because created files should be evenly distributed
14114         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14115                 error "test_mkdir $tdir failed"
14116         log "$(date +%s): creating first files"
14117         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14118                 error "create $DIR/$tdir/$tfile failed"
14119
14120         # check changelogs have been generated
14121         local start=$SECONDS
14122         local idle_time=$((MDSCOUNT * 5 + 5))
14123         local nbcl=$(changelog_dump | wc -l)
14124         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14125
14126         for param in "changelog_max_idle_time=$idle_time" \
14127                      "changelog_gc=1" \
14128                      "changelog_min_gc_interval=2" \
14129                      "changelog_min_free_cat_entries=3"; do
14130                 local MDT0=$(facet_svc $SINGLEMDS)
14131                 local var="${param%=*}"
14132                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14133
14134                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14135                 do_nodes $mdts $LCTL set_param mdd.*.$param
14136         done
14137
14138         # force cl_user2 to be idle (1st part), but also cancel the
14139         # cl_user1 records so that it is not evicted later in the test.
14140         local sleep1=$((idle_time / 2))
14141         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
14142         sleep $sleep1
14143
14144         # simulate changelog catalog almost full
14145         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
14146         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
14147
14148         for i in $(seq $MDSCOUNT); do
14149                 cl_users=(${CL_USERS[mds$i]})
14150                 cl_user1[mds$i]="${cl_users[0]}"
14151                 cl_user2[mds$i]="${cl_users[1]}"
14152
14153                 [ -n "${cl_user1[mds$i]}" ] ||
14154                         error "mds$i: no user registered"
14155                 [ -n "${cl_user2[mds$i]}" ] ||
14156                         error "mds$i: only ${cl_user2[mds$i]} is registered"
14157
14158                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14159                 [ -n "$user_rec1" ] ||
14160                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14161                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14162                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14163                 [ -n "$user_rec2" ] ||
14164                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14165                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14166                      "$user_rec1 + 2 == $user_rec2"
14167                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14168                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14169                               "$user_rec1 + 2, but is $user_rec2"
14170                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14171                 [ -n "$user_rec2" ] ||
14172                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14173                 [ $user_rec1 == $user_rec2 ] ||
14174                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14175                               "$user_rec1, but is $user_rec2"
14176         done
14177
14178         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
14179         local sleep2=$((idle_time - (SECONDS - start) + 1))
14180         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
14181         sleep $sleep2
14182
14183         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
14184         # cl_user1 should be OK because it recently processed records.
14185         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
14186         createmany -m $DIR/$tdir/${tfile}b $((MDSCOUNT * 2)) ||
14187                 error "create $DIR/$tdir/${tfile}b failed"
14188
14189         # ensure gc thread is done
14190         for i in $(mdts_nodes); do
14191                 wait_update $i \
14192                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
14193                         error "$i: GC-thread not done"
14194         done
14195
14196         local first_rec
14197         for i in $(seq $MDSCOUNT); do
14198                 # check cl_user1 still registered
14199                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14200                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14201                 # check cl_user2 unregistered
14202                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14203                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14204
14205                 # check changelogs are present and starting at $user_rec1 + 1
14206                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14207                 [ -n "$user_rec1" ] ||
14208                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14209                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14210                             awk '{ print $1; exit; }')
14211
14212                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14213                 [ $((user_rec1 + 1)) == $first_rec ] ||
14214                         error "mds$i: first index should be $user_rec1 + 1, " \
14215                               "but is $first_rec"
14216         done
14217 }
14218 run_test 160f "changelog garbage collect (timestamped users)"
14219
14220 test_160g() {
14221         remote_mds_nodsh && skip "remote MDS with nodsh"
14222         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14223                 skip "Need MDS version at least 2.10.56"
14224
14225         local mdts=$(comma_list $(mdts_nodes))
14226
14227         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
14228         do_nodes $mdts $LCTL set_param fail_loc=0x1314
14229
14230         # Create a user
14231         changelog_register || error "first changelog_register failed"
14232         changelog_register || error "second changelog_register failed"
14233         local cl_users
14234         declare -A cl_user1
14235         declare -A cl_user2
14236         local user_rec1
14237         local user_rec2
14238         local i
14239
14240         # generate some changelog records to accumulate on each MDT
14241         # use fnv1a because created files should be evenly distributed
14242         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14243                 error "mkdir $tdir failed"
14244         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14245                 error "create $DIR/$tdir/$tfile failed"
14246
14247         # check changelogs have been generated
14248         local nbcl=$(changelog_dump | wc -l)
14249         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14250
14251         # reduce the max_idle_indexes value to make sure we exceed it
14252         max_ndx=$((nbcl / 2 - 1))
14253
14254         for param in "changelog_max_idle_indexes=$max_ndx" \
14255                      "changelog_gc=1" \
14256                      "changelog_min_gc_interval=2" \
14257                      "changelog_min_free_cat_entries=3"; do
14258                 local MDT0=$(facet_svc $SINGLEMDS)
14259                 local var="${param%=*}"
14260                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14261
14262                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14263                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
14264                         error "unable to set mdd.*.$param"
14265         done
14266
14267         # simulate changelog catalog almost full
14268         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
14269         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
14270
14271         for i in $(seq $MDSCOUNT); do
14272                 cl_users=(${CL_USERS[mds$i]})
14273                 cl_user1[mds$i]="${cl_users[0]}"
14274                 cl_user2[mds$i]="${cl_users[1]}"
14275
14276                 [ -n "${cl_user1[mds$i]}" ] ||
14277                         error "mds$i: no user registered"
14278                 [ -n "${cl_user2[mds$i]}" ] ||
14279                         error "mds$i: only ${cl_user1[mds$i]} is registered"
14280
14281                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14282                 [ -n "$user_rec1" ] ||
14283                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14284                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14285                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14286                 [ -n "$user_rec2" ] ||
14287                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14288                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14289                      "$user_rec1 + 2 == $user_rec2"
14290                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14291                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14292                               "$user_rec1 + 2, but is $user_rec2"
14293                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14294                 [ -n "$user_rec2" ] ||
14295                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14296                 [ $user_rec1 == $user_rec2 ] ||
14297                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14298                               "$user_rec1, but is $user_rec2"
14299         done
14300
14301         # ensure we are past the previous changelog_min_gc_interval set above
14302         sleep 2
14303
14304         # generate one more changelog to trigger fail_loc
14305         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14306                 error "create $DIR/$tdir/${tfile}bis failed"
14307
14308         # ensure gc thread is done
14309         for i in $(mdts_nodes); do
14310                 wait_update $i \
14311                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
14312                         error "$i: GC-thread not done"
14313         done
14314
14315         local first_rec
14316         for i in $(seq $MDSCOUNT); do
14317                 # check cl_user1 still registered
14318                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14319                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14320                 # check cl_user2 unregistered
14321                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14322                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14323
14324                 # check changelogs are present and starting at $user_rec1 + 1
14325                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14326                 [ -n "$user_rec1" ] ||
14327                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14328                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14329                             awk '{ print $1; exit; }')
14330
14331                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14332                 [ $((user_rec1 + 1)) == $first_rec ] ||
14333                         error "mds$i: first index should be $user_rec1 + 1, " \
14334                               "but is $first_rec"
14335         done
14336 }
14337 run_test 160g "changelog garbage collect (old users)"
14338
14339 test_160h() {
14340         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14341         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14342                 skip "Need MDS version at least 2.10.56"
14343
14344         local mdts=$(comma_list $(mdts_nodes))
14345
14346         # Create a user
14347         changelog_register || error "first changelog_register failed"
14348         changelog_register || error "second changelog_register failed"
14349         local cl_users
14350         declare -A cl_user1
14351         declare -A cl_user2
14352         local user_rec1
14353         local user_rec2
14354         local i
14355
14356         # generate some changelog records to accumulate on each MDT
14357         # use fnv1a because created files should be evenly distributed
14358         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14359                 error "test_mkdir $tdir failed"
14360         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14361                 error "create $DIR/$tdir/$tfile failed"
14362
14363         # check changelogs have been generated
14364         local nbcl=$(changelog_dump | wc -l)
14365         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14366
14367         for param in "changelog_max_idle_time=10" \
14368                      "changelog_gc=1" \
14369                      "changelog_min_gc_interval=2"; do
14370                 local MDT0=$(facet_svc $SINGLEMDS)
14371                 local var="${param%=*}"
14372                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14373
14374                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14375                 do_nodes $mdts $LCTL set_param mdd.*.$param
14376         done
14377
14378         # force cl_user2 to be idle (1st part)
14379         sleep 9
14380
14381         for i in $(seq $MDSCOUNT); do
14382                 cl_users=(${CL_USERS[mds$i]})
14383                 cl_user1[mds$i]="${cl_users[0]}"
14384                 cl_user2[mds$i]="${cl_users[1]}"
14385
14386                 [ -n "${cl_user1[mds$i]}" ] ||
14387                         error "mds$i: no user registered"
14388                 [ -n "${cl_user2[mds$i]}" ] ||
14389                         error "mds$i: only ${cl_user2[mds$i]} is registered"
14390
14391                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14392                 [ -n "$user_rec1" ] ||
14393                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14394                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14395                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14396                 [ -n "$user_rec2" ] ||
14397                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14398                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14399                      "$user_rec1 + 2 == $user_rec2"
14400                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14401                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14402                               "$user_rec1 + 2, but is $user_rec2"
14403                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14404                 [ -n "$user_rec2" ] ||
14405                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14406                 [ $user_rec1 == $user_rec2 ] ||
14407                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14408                               "$user_rec1, but is $user_rec2"
14409         done
14410
14411         # force cl_user2 to be idle (2nd part) and to reach
14412         # changelog_max_idle_time
14413         sleep 2
14414
14415         # force each GC-thread start and block then
14416         # one per MDT/MDD, set fail_val accordingly
14417         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
14418         do_nodes $mdts $LCTL set_param fail_loc=0x1316
14419
14420         # generate more changelogs to trigger fail_loc
14421         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14422                 error "create $DIR/$tdir/${tfile}bis failed"
14423
14424         # stop MDT to stop GC-thread, should be done in back-ground as it will
14425         # block waiting for the thread to be released and exit
14426         declare -A stop_pids
14427         for i in $(seq $MDSCOUNT); do
14428                 stop mds$i &
14429                 stop_pids[mds$i]=$!
14430         done
14431
14432         for i in $(mdts_nodes); do
14433                 local facet
14434                 local nb=0
14435                 local facets=$(facets_up_on_host $i)
14436
14437                 for facet in ${facets//,/ }; do
14438                         if [[ $facet == mds* ]]; then
14439                                 nb=$((nb + 1))
14440                         fi
14441                 done
14442                 # ensure each MDS's gc threads are still present and all in "R"
14443                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
14444                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
14445                         error "$i: expected $nb GC-thread"
14446                 wait_update $i \
14447                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
14448                         "R" 20 ||
14449                         error "$i: GC-thread not found in R-state"
14450                 # check umounts of each MDT on MDS have reached kthread_stop()
14451                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
14452                         error "$i: expected $nb umount"
14453                 wait_update $i \
14454                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
14455                         error "$i: umount not found in D-state"
14456         done
14457
14458         # release all GC-threads
14459         do_nodes $mdts $LCTL set_param fail_loc=0
14460
14461         # wait for MDT stop to complete
14462         for i in $(seq $MDSCOUNT); do
14463                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
14464         done
14465
14466         # XXX
14467         # may try to check if any orphan changelog records are present
14468         # via ldiskfs/zfs and llog_reader...
14469
14470         # re-start/mount MDTs
14471         for i in $(seq $MDSCOUNT); do
14472                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
14473                         error "Fail to start mds$i"
14474         done
14475
14476         local first_rec
14477         for i in $(seq $MDSCOUNT); do
14478                 # check cl_user1 still registered
14479                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14480                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14481                 # check cl_user2 unregistered
14482                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14483                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14484
14485                 # check changelogs are present and starting at $user_rec1 + 1
14486                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14487                 [ -n "$user_rec1" ] ||
14488                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14489                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14490                             awk '{ print $1; exit; }')
14491
14492                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14493                 [ $((user_rec1 + 1)) == $first_rec ] ||
14494                         error "mds$i: first index should be $user_rec1 + 1, " \
14495                               "but is $first_rec"
14496         done
14497 }
14498 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
14499               "during mount"
14500
14501 test_160i() {
14502
14503         local mdts=$(comma_list $(mdts_nodes))
14504
14505         changelog_register || error "first changelog_register failed"
14506
14507         # generate some changelog records to accumulate on each MDT
14508         # use fnv1a because created files should be evenly distributed
14509         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14510                 error "mkdir $tdir failed"
14511         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14512                 error "create $DIR/$tdir/$tfile failed"
14513
14514         # check changelogs have been generated
14515         local nbcl=$(changelog_dump | wc -l)
14516         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14517
14518         # simulate race between register and unregister
14519         # XXX as fail_loc is set per-MDS, with DNE configs the race
14520         # simulation will only occur for one MDT per MDS and for the
14521         # others the normal race scenario will take place
14522         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
14523         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
14524         do_nodes $mdts $LCTL set_param fail_val=1
14525
14526         # unregister 1st user
14527         changelog_deregister &
14528         local pid1=$!
14529         # wait some time for deregister work to reach race rdv
14530         sleep 2
14531         # register 2nd user
14532         changelog_register || error "2nd user register failed"
14533
14534         wait $pid1 || error "1st user deregister failed"
14535
14536         local i
14537         local last_rec
14538         declare -A LAST_REC
14539         for i in $(seq $MDSCOUNT); do
14540                 if changelog_users mds$i | grep "^cl"; then
14541                         # make sure new records are added with one user present
14542                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
14543                                           awk '/^current.index:/ { print $NF }')
14544                 else
14545                         error "mds$i has no user registered"
14546                 fi
14547         done
14548
14549         # generate more changelog records to accumulate on each MDT
14550         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14551                 error "create $DIR/$tdir/${tfile}bis failed"
14552
14553         for i in $(seq $MDSCOUNT); do
14554                 last_rec=$(changelog_users $SINGLEMDS |
14555                            awk '/^current.index:/ { print $NF }')
14556                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
14557                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
14558                         error "changelogs are off on mds$i"
14559         done
14560 }
14561 run_test 160i "changelog user register/unregister race"
14562
14563 test_160j() {
14564         remote_mds_nodsh && skip "remote MDS with nodsh"
14565         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
14566                 skip "Need MDS version at least 2.12.56"
14567
14568         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
14569         stack_trap "umount $MOUNT2" EXIT
14570
14571         changelog_register || error "first changelog_register failed"
14572         stack_trap "changelog_deregister" EXIT
14573
14574         # generate some changelog
14575         # use fnv1a because created files should be evenly distributed
14576         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14577                 error "mkdir $tdir failed"
14578         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14579                 error "create $DIR/$tdir/${tfile}bis failed"
14580
14581         # open the changelog device
14582         exec 3>/dev/changelog-$FSNAME-MDT0000
14583         stack_trap "exec 3>&-" EXIT
14584         exec 4</dev/changelog-$FSNAME-MDT0000
14585         stack_trap "exec 4<&-" EXIT
14586
14587         # umount the first lustre mount
14588         umount $MOUNT
14589         stack_trap "mount_client $MOUNT" EXIT
14590
14591         # read changelog
14592         cat <&4 >/dev/null || error "read changelog failed"
14593
14594         # clear changelog
14595         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14596         changelog_users $SINGLEMDS | grep -q $cl_user ||
14597                 error "User $cl_user not found in changelog_users"
14598
14599         printf 'clear:'$cl_user':0' >&3
14600 }
14601 run_test 160j "client can be umounted  while its chanangelog is being used"
14602
14603 test_160k() {
14604         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14605         remote_mds_nodsh && skip "remote MDS with nodsh"
14606
14607         mkdir -p $DIR/$tdir/1/1
14608
14609         changelog_register || error "changelog_register failed"
14610         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14611
14612         changelog_users $SINGLEMDS | grep -q $cl_user ||
14613                 error "User '$cl_user' not found in changelog_users"
14614 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
14615         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
14616         rmdir $DIR/$tdir/1/1 & sleep 1
14617         mkdir $DIR/$tdir/2
14618         touch $DIR/$tdir/2/2
14619         rm -rf $DIR/$tdir/2
14620
14621         wait
14622         sleep 4
14623
14624         changelog_dump | grep rmdir || error "rmdir not recorded"
14625
14626         rm -rf $DIR/$tdir
14627         changelog_deregister
14628 }
14629 run_test 160k "Verify that changelog records are not lost"
14630
14631 test_161a() {
14632         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14633
14634         test_mkdir -c1 $DIR/$tdir
14635         cp /etc/hosts $DIR/$tdir/$tfile
14636         test_mkdir -c1 $DIR/$tdir/foo1
14637         test_mkdir -c1 $DIR/$tdir/foo2
14638         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
14639         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
14640         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
14641         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
14642         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
14643         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14644                 $LFS fid2path $DIR $FID
14645                 error "bad link ea"
14646         fi
14647         # middle
14648         rm $DIR/$tdir/foo2/zachary
14649         # last
14650         rm $DIR/$tdir/foo2/thor
14651         # first
14652         rm $DIR/$tdir/$tfile
14653         # rename
14654         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
14655         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
14656                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
14657         rm $DIR/$tdir/foo2/maggie
14658
14659         # overflow the EA
14660         local longname=$tfile.avg_len_is_thirty_two_
14661         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
14662                 error_noexit 'failed to unlink many hardlinks'" EXIT
14663         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
14664                 error "failed to hardlink many files"
14665         links=$($LFS fid2path $DIR $FID | wc -l)
14666         echo -n "${links}/1000 links in link EA"
14667         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
14668 }
14669 run_test 161a "link ea sanity"
14670
14671 test_161b() {
14672         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14673         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
14674
14675         local MDTIDX=1
14676         local remote_dir=$DIR/$tdir/remote_dir
14677
14678         mkdir -p $DIR/$tdir
14679         $LFS mkdir -i $MDTIDX $remote_dir ||
14680                 error "create remote directory failed"
14681
14682         cp /etc/hosts $remote_dir/$tfile
14683         mkdir -p $remote_dir/foo1
14684         mkdir -p $remote_dir/foo2
14685         ln $remote_dir/$tfile $remote_dir/foo1/sofia
14686         ln $remote_dir/$tfile $remote_dir/foo2/zachary
14687         ln $remote_dir/$tfile $remote_dir/foo1/luna
14688         ln $remote_dir/$tfile $remote_dir/foo2/thor
14689
14690         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
14691                      tr -d ']')
14692         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14693                 $LFS fid2path $DIR $FID
14694                 error "bad link ea"
14695         fi
14696         # middle
14697         rm $remote_dir/foo2/zachary
14698         # last
14699         rm $remote_dir/foo2/thor
14700         # first
14701         rm $remote_dir/$tfile
14702         # rename
14703         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
14704         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
14705         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
14706                 $LFS fid2path $DIR $FID
14707                 error "bad link rename"
14708         fi
14709         rm $remote_dir/foo2/maggie
14710
14711         # overflow the EA
14712         local longname=filename_avg_len_is_thirty_two_
14713         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
14714                 error "failed to hardlink many files"
14715         links=$($LFS fid2path $DIR $FID | wc -l)
14716         echo -n "${links}/1000 links in link EA"
14717         [[ ${links} -gt 60 ]] ||
14718                 error "expected at least 60 links in link EA"
14719         unlinkmany $remote_dir/foo2/$longname 1000 ||
14720         error "failed to unlink many hardlinks"
14721 }
14722 run_test 161b "link ea sanity under remote directory"
14723
14724 test_161c() {
14725         remote_mds_nodsh && skip "remote MDS with nodsh"
14726         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14727         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
14728                 skip "Need MDS version at least 2.1.5"
14729
14730         # define CLF_RENAME_LAST 0x0001
14731         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
14732         changelog_register || error "changelog_register failed"
14733
14734         rm -rf $DIR/$tdir
14735         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
14736         touch $DIR/$tdir/foo_161c
14737         touch $DIR/$tdir/bar_161c
14738         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14739         changelog_dump | grep RENME | tail -n 5
14740         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14741         changelog_clear 0 || error "changelog_clear failed"
14742         if [ x$flags != "x0x1" ]; then
14743                 error "flag $flags is not 0x1"
14744         fi
14745
14746         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
14747         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
14748         touch $DIR/$tdir/foo_161c
14749         touch $DIR/$tdir/bar_161c
14750         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14751         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14752         changelog_dump | grep RENME | tail -n 5
14753         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14754         changelog_clear 0 || error "changelog_clear failed"
14755         if [ x$flags != "x0x0" ]; then
14756                 error "flag $flags is not 0x0"
14757         fi
14758         echo "rename overwrite a target having nlink > 1," \
14759                 "changelog record has flags of $flags"
14760
14761         # rename doesn't overwrite a target (changelog flag 0x0)
14762         touch $DIR/$tdir/foo_161c
14763         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
14764         changelog_dump | grep RENME | tail -n 5
14765         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
14766         changelog_clear 0 || error "changelog_clear failed"
14767         if [ x$flags != "x0x0" ]; then
14768                 error "flag $flags is not 0x0"
14769         fi
14770         echo "rename doesn't overwrite a target," \
14771                 "changelog record has flags of $flags"
14772
14773         # define CLF_UNLINK_LAST 0x0001
14774         # unlink a file having nlink = 1 (changelog flag 0x1)
14775         rm -f $DIR/$tdir/foo2_161c
14776         changelog_dump | grep UNLNK | tail -n 5
14777         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14778         changelog_clear 0 || error "changelog_clear failed"
14779         if [ x$flags != "x0x1" ]; then
14780                 error "flag $flags is not 0x1"
14781         fi
14782         echo "unlink a file having nlink = 1," \
14783                 "changelog record has flags of $flags"
14784
14785         # unlink a file having nlink > 1 (changelog flag 0x0)
14786         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14787         rm -f $DIR/$tdir/foobar_161c
14788         changelog_dump | grep UNLNK | tail -n 5
14789         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14790         changelog_clear 0 || error "changelog_clear failed"
14791         if [ x$flags != "x0x0" ]; then
14792                 error "flag $flags is not 0x0"
14793         fi
14794         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
14795 }
14796 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
14797
14798 test_161d() {
14799         remote_mds_nodsh && skip "remote MDS with nodsh"
14800         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
14801
14802         local pid
14803         local fid
14804
14805         changelog_register || error "changelog_register failed"
14806
14807         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
14808         # interfer with $MOUNT/.lustre/fid/ access
14809         mkdir $DIR/$tdir
14810         [[ $? -eq 0 ]] || error "mkdir failed"
14811
14812         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
14813         $LCTL set_param fail_loc=0x8000140c
14814         # 5s pause
14815         $LCTL set_param fail_val=5
14816
14817         # create file
14818         echo foofoo > $DIR/$tdir/$tfile &
14819         pid=$!
14820
14821         # wait for create to be delayed
14822         sleep 2
14823
14824         ps -p $pid
14825         [[ $? -eq 0 ]] || error "create should be blocked"
14826
14827         local tempfile="$(mktemp --tmpdir $tfile.XXXXXX)"
14828         stack_trap "rm -f $tempfile"
14829         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
14830         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
14831         # some delay may occur during ChangeLog publishing and file read just
14832         # above, that could allow file write to happen finally
14833         [[ -s $tempfile ]] && echo "file should be empty"
14834
14835         $LCTL set_param fail_loc=0
14836
14837         wait $pid
14838         [[ $? -eq 0 ]] || error "create failed"
14839 }
14840 run_test 161d "create with concurrent .lustre/fid access"
14841
14842 check_path() {
14843         local expected="$1"
14844         shift
14845         local fid="$2"
14846
14847         local path
14848         path=$($LFS fid2path "$@")
14849         local rc=$?
14850
14851         if [ $rc -ne 0 ]; then
14852                 error "path looked up of '$expected' failed: rc=$rc"
14853         elif [ "$path" != "$expected" ]; then
14854                 error "path looked up '$path' instead of '$expected'"
14855         else
14856                 echo "FID '$fid' resolves to path '$path' as expected"
14857         fi
14858 }
14859
14860 test_162a() { # was test_162
14861         test_mkdir -p -c1 $DIR/$tdir/d2
14862         touch $DIR/$tdir/d2/$tfile
14863         touch $DIR/$tdir/d2/x1
14864         touch $DIR/$tdir/d2/x2
14865         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
14866         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
14867         # regular file
14868         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
14869         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
14870
14871         # softlink
14872         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
14873         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
14874         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
14875
14876         # softlink to wrong file
14877         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
14878         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
14879         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
14880
14881         # hardlink
14882         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
14883         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
14884         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
14885         # fid2path dir/fsname should both work
14886         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
14887         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
14888
14889         # hardlink count: check that there are 2 links
14890         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
14891         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
14892
14893         # hardlink indexing: remove the first link
14894         rm $DIR/$tdir/d2/p/q/r/hlink
14895         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
14896 }
14897 run_test 162a "path lookup sanity"
14898
14899 test_162b() {
14900         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14901         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14902
14903         mkdir $DIR/$tdir
14904         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
14905                                 error "create striped dir failed"
14906
14907         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
14908                                         tail -n 1 | awk '{print $2}')
14909         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
14910
14911         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
14912         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
14913
14914         # regular file
14915         for ((i=0;i<5;i++)); do
14916                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
14917                         error "get fid for f$i failed"
14918                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
14919
14920                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
14921                         error "get fid for d$i failed"
14922                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
14923         done
14924
14925         return 0
14926 }
14927 run_test 162b "striped directory path lookup sanity"
14928
14929 # LU-4239: Verify fid2path works with paths 100 or more directories deep
14930 test_162c() {
14931         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
14932                 skip "Need MDS version at least 2.7.51"
14933
14934         local lpath=$tdir.local
14935         local rpath=$tdir.remote
14936
14937         test_mkdir $DIR/$lpath
14938         test_mkdir $DIR/$rpath
14939
14940         for ((i = 0; i <= 101; i++)); do
14941                 lpath="$lpath/$i"
14942                 mkdir $DIR/$lpath
14943                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
14944                         error "get fid for local directory $DIR/$lpath failed"
14945                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
14946
14947                 rpath="$rpath/$i"
14948                 test_mkdir $DIR/$rpath
14949                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
14950                         error "get fid for remote directory $DIR/$rpath failed"
14951                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
14952         done
14953
14954         return 0
14955 }
14956 run_test 162c "fid2path works with paths 100 or more directories deep"
14957
14958 test_169() {
14959         # do directio so as not to populate the page cache
14960         log "creating a 10 Mb file"
14961         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
14962         log "starting reads"
14963         dd if=$DIR/$tfile of=/dev/null bs=4096 &
14964         log "truncating the file"
14965         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
14966         log "killing dd"
14967         kill %+ || true # reads might have finished
14968         echo "wait until dd is finished"
14969         wait
14970         log "removing the temporary file"
14971         rm -rf $DIR/$tfile || error "tmp file removal failed"
14972 }
14973 run_test 169 "parallel read and truncate should not deadlock"
14974
14975 test_170() {
14976         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14977
14978         $LCTL clear     # bug 18514
14979         $LCTL debug_daemon start $TMP/${tfile}_log_good
14980         touch $DIR/$tfile
14981         $LCTL debug_daemon stop
14982         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
14983                 error "sed failed to read log_good"
14984
14985         $LCTL debug_daemon start $TMP/${tfile}_log_good
14986         rm -rf $DIR/$tfile
14987         $LCTL debug_daemon stop
14988
14989         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
14990                error "lctl df log_bad failed"
14991
14992         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14993         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14994
14995         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
14996         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
14997
14998         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
14999                 error "bad_line good_line1 good_line2 are empty"
15000
15001         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
15002         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
15003         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
15004
15005         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
15006         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
15007         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
15008
15009         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
15010                 error "bad_line_new good_line_new are empty"
15011
15012         local expected_good=$((good_line1 + good_line2*2))
15013
15014         rm -f $TMP/${tfile}*
15015         # LU-231, short malformed line may not be counted into bad lines
15016         if [ $bad_line -ne $bad_line_new ] &&
15017                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
15018                 error "expected $bad_line bad lines, but got $bad_line_new"
15019                 return 1
15020         fi
15021
15022         if [ $expected_good -ne $good_line_new ]; then
15023                 error "expected $expected_good good lines, but got $good_line_new"
15024                 return 2
15025         fi
15026         true
15027 }
15028 run_test 170 "test lctl df to handle corrupted log ====================="
15029
15030 test_171() { # bug20592
15031         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15032
15033         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
15034         $LCTL set_param fail_loc=0x50e
15035         $LCTL set_param fail_val=3000
15036         multiop_bg_pause $DIR/$tfile O_s || true
15037         local MULTIPID=$!
15038         kill -USR1 $MULTIPID
15039         # cause log dump
15040         sleep 3
15041         wait $MULTIPID
15042         if dmesg | grep "recursive fault"; then
15043                 error "caught a recursive fault"
15044         fi
15045         $LCTL set_param fail_loc=0
15046         true
15047 }
15048 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
15049
15050 # it would be good to share it with obdfilter-survey/iokit-libecho code
15051 setup_obdecho_osc () {
15052         local rc=0
15053         local ost_nid=$1
15054         local obdfilter_name=$2
15055         echo "Creating new osc for $obdfilter_name on $ost_nid"
15056         # make sure we can find loopback nid
15057         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
15058
15059         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
15060                            ${obdfilter_name}_osc_UUID || rc=2; }
15061         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
15062                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
15063         return $rc
15064 }
15065
15066 cleanup_obdecho_osc () {
15067         local obdfilter_name=$1
15068         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
15069         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
15070         return 0
15071 }
15072
15073 obdecho_test() {
15074         local OBD=$1
15075         local node=$2
15076         local pages=${3:-64}
15077         local rc=0
15078         local id
15079
15080         local count=10
15081         local obd_size=$(get_obd_size $node $OBD)
15082         local page_size=$(get_page_size $node)
15083         if [[ -n "$obd_size" ]]; then
15084                 local new_count=$((obd_size / (pages * page_size / 1024)))
15085                 [[ $new_count -ge $count ]] || count=$new_count
15086         fi
15087
15088         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
15089         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
15090                            rc=2; }
15091         if [ $rc -eq 0 ]; then
15092             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
15093             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
15094         fi
15095         echo "New object id is $id"
15096         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
15097                            rc=4; }
15098         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
15099                            "test_brw $count w v $pages $id" || rc=4; }
15100         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
15101                            rc=4; }
15102         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
15103                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
15104         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
15105                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
15106         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
15107         return $rc
15108 }
15109
15110 test_180a() {
15111         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15112
15113         if ! module_loaded obdecho; then
15114                 load_module obdecho/obdecho &&
15115                         stack_trap "rmmod obdecho" EXIT ||
15116                         error "unable to load obdecho on client"
15117         fi
15118
15119         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
15120         local host=$($LCTL get_param -n osc.$osc.import |
15121                      awk '/current_connection:/ { print $2 }' )
15122         local target=$($LCTL get_param -n osc.$osc.import |
15123                        awk '/target:/ { print $2 }' )
15124         target=${target%_UUID}
15125
15126         if [ -n "$target" ]; then
15127                 setup_obdecho_osc $host $target &&
15128                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
15129                         { error "obdecho setup failed with $?"; return; }
15130
15131                 obdecho_test ${target}_osc client ||
15132                         error "obdecho_test failed on ${target}_osc"
15133         else
15134                 $LCTL get_param osc.$osc.import
15135                 error "there is no osc.$osc.import target"
15136         fi
15137 }
15138 run_test 180a "test obdecho on osc"
15139
15140 test_180b() {
15141         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15142         remote_ost_nodsh && skip "remote OST with nodsh"
15143
15144         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
15145                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
15146                 error "failed to load module obdecho"
15147
15148         local target=$(do_facet ost1 $LCTL dl |
15149                        awk '/obdfilter/ { print $4; exit; }')
15150
15151         if [ -n "$target" ]; then
15152                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
15153         else
15154                 do_facet ost1 $LCTL dl
15155                 error "there is no obdfilter target on ost1"
15156         fi
15157 }
15158 run_test 180b "test obdecho directly on obdfilter"
15159
15160 test_180c() { # LU-2598
15161         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15162         remote_ost_nodsh && skip "remote OST with nodsh"
15163         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
15164                 skip "Need MDS version at least 2.4.0"
15165
15166         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
15167                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
15168                 error "failed to load module obdecho"
15169
15170         local target=$(do_facet ost1 $LCTL dl |
15171                        awk '/obdfilter/ { print $4; exit; }')
15172
15173         if [ -n "$target" ]; then
15174                 local pages=16384 # 64MB bulk I/O RPC size
15175
15176                 obdecho_test "$target" ost1 "$pages" ||
15177                         error "obdecho_test with pages=$pages failed with $?"
15178         else
15179                 do_facet ost1 $LCTL dl
15180                 error "there is no obdfilter target on ost1"
15181         fi
15182 }
15183 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
15184
15185 test_181() { # bug 22177
15186         test_mkdir $DIR/$tdir
15187         # create enough files to index the directory
15188         createmany -o $DIR/$tdir/foobar 4000
15189         # print attributes for debug purpose
15190         lsattr -d .
15191         # open dir
15192         multiop_bg_pause $DIR/$tdir D_Sc || return 1
15193         MULTIPID=$!
15194         # remove the files & current working dir
15195         unlinkmany $DIR/$tdir/foobar 4000
15196         rmdir $DIR/$tdir
15197         kill -USR1 $MULTIPID
15198         wait $MULTIPID
15199         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
15200         return 0
15201 }
15202 run_test 181 "Test open-unlinked dir ========================"
15203
15204 test_182() {
15205         local fcount=1000
15206         local tcount=10
15207
15208         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15209
15210         $LCTL set_param mdc.*.rpc_stats=clear
15211
15212         for (( i = 0; i < $tcount; i++ )) ; do
15213                 mkdir $DIR/$tdir/$i
15214         done
15215
15216         for (( i = 0; i < $tcount; i++ )) ; do
15217                 createmany -o $DIR/$tdir/$i/f- $fcount &
15218         done
15219         wait
15220
15221         for (( i = 0; i < $tcount; i++ )) ; do
15222                 unlinkmany $DIR/$tdir/$i/f- $fcount &
15223         done
15224         wait
15225
15226         $LCTL get_param mdc.*.rpc_stats
15227
15228         rm -rf $DIR/$tdir
15229 }
15230 run_test 182 "Test parallel modify metadata operations ================"
15231
15232 test_183() { # LU-2275
15233         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15234         remote_mds_nodsh && skip "remote MDS with nodsh"
15235         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
15236                 skip "Need MDS version at least 2.3.56"
15237
15238         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15239         echo aaa > $DIR/$tdir/$tfile
15240
15241 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
15242         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
15243
15244         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
15245         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
15246
15247         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
15248
15249         # Flush negative dentry cache
15250         touch $DIR/$tdir/$tfile
15251
15252         # We are not checking for any leaked references here, they'll
15253         # become evident next time we do cleanup with module unload.
15254         rm -rf $DIR/$tdir
15255 }
15256 run_test 183 "No crash or request leak in case of strange dispositions ========"
15257
15258 # test suite 184 is for LU-2016, LU-2017
15259 test_184a() {
15260         check_swap_layouts_support
15261
15262         dir0=$DIR/$tdir/$testnum
15263         test_mkdir -p -c1 $dir0
15264         ref1=/etc/passwd
15265         ref2=/etc/group
15266         file1=$dir0/f1
15267         file2=$dir0/f2
15268         $LFS setstripe -c1 $file1
15269         cp $ref1 $file1
15270         $LFS setstripe -c2 $file2
15271         cp $ref2 $file2
15272         gen1=$($LFS getstripe -g $file1)
15273         gen2=$($LFS getstripe -g $file2)
15274
15275         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
15276         gen=$($LFS getstripe -g $file1)
15277         [[ $gen1 != $gen ]] ||
15278                 "Layout generation on $file1 does not change"
15279         gen=$($LFS getstripe -g $file2)
15280         [[ $gen2 != $gen ]] ||
15281                 "Layout generation on $file2 does not change"
15282
15283         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
15284         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
15285
15286         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
15287 }
15288 run_test 184a "Basic layout swap"
15289
15290 test_184b() {
15291         check_swap_layouts_support
15292
15293         dir0=$DIR/$tdir/$testnum
15294         mkdir -p $dir0 || error "creating dir $dir0"
15295         file1=$dir0/f1
15296         file2=$dir0/f2
15297         file3=$dir0/f3
15298         dir1=$dir0/d1
15299         dir2=$dir0/d2
15300         mkdir $dir1 $dir2
15301         $LFS setstripe -c1 $file1
15302         $LFS setstripe -c2 $file2
15303         $LFS setstripe -c1 $file3
15304         chown $RUNAS_ID $file3
15305         gen1=$($LFS getstripe -g $file1)
15306         gen2=$($LFS getstripe -g $file2)
15307
15308         $LFS swap_layouts $dir1 $dir2 &&
15309                 error "swap of directories layouts should fail"
15310         $LFS swap_layouts $dir1 $file1 &&
15311                 error "swap of directory and file layouts should fail"
15312         $RUNAS $LFS swap_layouts $file1 $file2 &&
15313                 error "swap of file we cannot write should fail"
15314         $LFS swap_layouts $file1 $file3 &&
15315                 error "swap of file with different owner should fail"
15316         /bin/true # to clear error code
15317 }
15318 run_test 184b "Forbidden layout swap (will generate errors)"
15319
15320 test_184c() {
15321         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
15322         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
15323         check_swap_layouts_support
15324
15325         local dir0=$DIR/$tdir/$testnum
15326         mkdir -p $dir0 || error "creating dir $dir0"
15327
15328         local ref1=$dir0/ref1
15329         local ref2=$dir0/ref2
15330         local file1=$dir0/file1
15331         local file2=$dir0/file2
15332         # create a file large enough for the concurrent test
15333         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
15334         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
15335         echo "ref file size: ref1($(stat -c %s $ref1))," \
15336              "ref2($(stat -c %s $ref2))"
15337
15338         cp $ref2 $file2
15339         dd if=$ref1 of=$file1 bs=16k &
15340         local DD_PID=$!
15341
15342         # Make sure dd starts to copy file
15343         while [ ! -f $file1 ]; do sleep 0.1; done
15344
15345         $LFS swap_layouts $file1 $file2
15346         local rc=$?
15347         wait $DD_PID
15348         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
15349         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
15350
15351         # how many bytes copied before swapping layout
15352         local copied=$(stat -c %s $file2)
15353         local remaining=$(stat -c %s $ref1)
15354         remaining=$((remaining - copied))
15355         echo "Copied $copied bytes before swapping layout..."
15356
15357         cmp -n $copied $file1 $ref2 | grep differ &&
15358                 error "Content mismatch [0, $copied) of ref2 and file1"
15359         cmp -n $copied $file2 $ref1 ||
15360                 error "Content mismatch [0, $copied) of ref1 and file2"
15361         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
15362                 error "Content mismatch [$copied, EOF) of ref1 and file1"
15363
15364         # clean up
15365         rm -f $ref1 $ref2 $file1 $file2
15366 }
15367 run_test 184c "Concurrent write and layout swap"
15368
15369 test_184d() {
15370         check_swap_layouts_support
15371         [ -z "$(which getfattr 2>/dev/null)" ] &&
15372                 skip_env "no getfattr command"
15373
15374         local file1=$DIR/$tdir/$tfile-1
15375         local file2=$DIR/$tdir/$tfile-2
15376         local file3=$DIR/$tdir/$tfile-3
15377         local lovea1
15378         local lovea2
15379
15380         mkdir -p $DIR/$tdir
15381         touch $file1 || error "create $file1 failed"
15382         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15383                 error "create $file2 failed"
15384         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15385                 error "create $file3 failed"
15386         lovea1=$(get_layout_param $file1)
15387
15388         $LFS swap_layouts $file2 $file3 ||
15389                 error "swap $file2 $file3 layouts failed"
15390         $LFS swap_layouts $file1 $file2 ||
15391                 error "swap $file1 $file2 layouts failed"
15392
15393         lovea2=$(get_layout_param $file2)
15394         echo "$lovea1"
15395         echo "$lovea2"
15396         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
15397
15398         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15399         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
15400 }
15401 run_test 184d "allow stripeless layouts swap"
15402
15403 test_184e() {
15404         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
15405                 skip "Need MDS version at least 2.6.94"
15406         check_swap_layouts_support
15407         [ -z "$(which getfattr 2>/dev/null)" ] &&
15408                 skip_env "no getfattr command"
15409
15410         local file1=$DIR/$tdir/$tfile-1
15411         local file2=$DIR/$tdir/$tfile-2
15412         local file3=$DIR/$tdir/$tfile-3
15413         local lovea
15414
15415         mkdir -p $DIR/$tdir
15416         touch $file1 || error "create $file1 failed"
15417         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15418                 error "create $file2 failed"
15419         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15420                 error "create $file3 failed"
15421
15422         $LFS swap_layouts $file1 $file2 ||
15423                 error "swap $file1 $file2 layouts failed"
15424
15425         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15426         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
15427
15428         echo 123 > $file1 || error "Should be able to write into $file1"
15429
15430         $LFS swap_layouts $file1 $file3 ||
15431                 error "swap $file1 $file3 layouts failed"
15432
15433         echo 123 > $file1 || error "Should be able to write into $file1"
15434
15435         rm -rf $file1 $file2 $file3
15436 }
15437 run_test 184e "Recreate layout after stripeless layout swaps"
15438
15439 test_184f() {
15440         # Create a file with name longer than sizeof(struct stat) ==
15441         # 144 to see if we can get chars from the file name to appear
15442         # in the returned striping. Note that 'f' == 0x66.
15443         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
15444
15445         mkdir -p $DIR/$tdir
15446         mcreate $DIR/$tdir/$file
15447         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
15448                 error "IOC_MDC_GETFILEINFO returned garbage striping"
15449         fi
15450 }
15451 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
15452
15453 test_185() { # LU-2441
15454         # LU-3553 - no volatile file support in old servers
15455         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
15456                 skip "Need MDS version at least 2.3.60"
15457
15458         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15459         touch $DIR/$tdir/spoo
15460         local mtime1=$(stat -c "%Y" $DIR/$tdir)
15461         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
15462                 error "cannot create/write a volatile file"
15463         [ "$FILESET" == "" ] &&
15464         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
15465                 error "FID is still valid after close"
15466
15467         multiop_bg_pause $DIR/$tdir vVw4096_c
15468         local multi_pid=$!
15469
15470         local OLD_IFS=$IFS
15471         IFS=":"
15472         local fidv=($fid)
15473         IFS=$OLD_IFS
15474         # assume that the next FID for this client is sequential, since stdout
15475         # is unfortunately eaten by multiop_bg_pause
15476         local n=$((${fidv[1]} + 1))
15477         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
15478         if [ "$FILESET" == "" ]; then
15479                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
15480                         error "FID is missing before close"
15481         fi
15482         kill -USR1 $multi_pid
15483         # 1 second delay, so if mtime change we will see it
15484         sleep 1
15485         local mtime2=$(stat -c "%Y" $DIR/$tdir)
15486         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
15487 }
15488 run_test 185 "Volatile file support"
15489
15490 function create_check_volatile() {
15491         local idx=$1
15492         local tgt
15493
15494         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
15495         local PID=$!
15496         sleep 1
15497         local FID=$(cat /tmp/${tfile}.fid)
15498         [ "$FID" == "" ] && error "can't get FID for volatile"
15499         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
15500         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
15501         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
15502         kill -USR1 $PID
15503         wait
15504         sleep 1
15505         cancel_lru_locks mdc # flush opencache
15506         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
15507         return 0
15508 }
15509
15510 test_185a(){
15511         # LU-12516 - volatile creation via .lustre
15512         [[ $MDS1_VERSION -ge $(version_code 2.12.55) ]] ||
15513                 skip "Need MDS version at least 2.3.55"
15514
15515         create_check_volatile 0
15516         [ $MDSCOUNT -lt 2 ] && return 0
15517
15518         # DNE case
15519         create_check_volatile 1
15520
15521         return 0
15522 }
15523 run_test 185a "Volatile file creation in .lustre/fid/"
15524
15525 test_187a() {
15526         remote_mds_nodsh && skip "remote MDS with nodsh"
15527         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15528                 skip "Need MDS version at least 2.3.0"
15529
15530         local dir0=$DIR/$tdir/$testnum
15531         mkdir -p $dir0 || error "creating dir $dir0"
15532
15533         local file=$dir0/file1
15534         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
15535         local dv1=$($LFS data_version $file)
15536         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
15537         local dv2=$($LFS data_version $file)
15538         [[ $dv1 != $dv2 ]] ||
15539                 error "data version did not change on write $dv1 == $dv2"
15540
15541         # clean up
15542         rm -f $file1
15543 }
15544 run_test 187a "Test data version change"
15545
15546 test_187b() {
15547         remote_mds_nodsh && skip "remote MDS with nodsh"
15548         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15549                 skip "Need MDS version at least 2.3.0"
15550
15551         local dir0=$DIR/$tdir/$testnum
15552         mkdir -p $dir0 || error "creating dir $dir0"
15553
15554         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
15555         [[ ${DV[0]} != ${DV[1]} ]] ||
15556                 error "data version did not change on write"\
15557                       " ${DV[0]} == ${DV[1]}"
15558
15559         # clean up
15560         rm -f $file1
15561 }
15562 run_test 187b "Test data version change on volatile file"
15563
15564 test_200() {
15565         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15566         remote_mgs_nodsh && skip "remote MGS with nodsh"
15567         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15568
15569         local POOL=${POOL:-cea1}
15570         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
15571         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
15572         # Pool OST targets
15573         local first_ost=0
15574         local last_ost=$(($OSTCOUNT - 1))
15575         local ost_step=2
15576         local ost_list=$(seq $first_ost $ost_step $last_ost)
15577         local ost_range="$first_ost $last_ost $ost_step"
15578         local test_path=$POOL_ROOT/$POOL_DIR_NAME
15579         local file_dir=$POOL_ROOT/file_tst
15580         local subdir=$test_path/subdir
15581         local rc=0
15582
15583         while : ; do
15584                 # former test_200a test_200b
15585                 pool_add $POOL                          || { rc=$? ; break; }
15586                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
15587                 # former test_200c test_200d
15588                 mkdir -p $test_path
15589                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
15590                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
15591                 mkdir -p $subdir
15592                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
15593                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
15594                                                         || { rc=$? ; break; }
15595                 # former test_200e test_200f
15596                 local files=$((OSTCOUNT*3))
15597                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
15598                                                         || { rc=$? ; break; }
15599                 pool_create_files $POOL $file_dir $files "$ost_list" \
15600                                                         || { rc=$? ; break; }
15601                 # former test_200g test_200h
15602                 pool_lfs_df $POOL                       || { rc=$? ; break; }
15603                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
15604
15605                 # former test_201a test_201b test_201c
15606                 pool_remove_first_target $POOL          || { rc=$? ; break; }
15607
15608                 local f=$test_path/$tfile
15609                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
15610                 pool_remove $POOL $f                    || { rc=$? ; break; }
15611                 break
15612         done
15613
15614         destroy_test_pools
15615
15616         return $rc
15617 }
15618 run_test 200 "OST pools"
15619
15620 # usage: default_attr <count | size | offset>
15621 default_attr() {
15622         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
15623 }
15624
15625 # usage: check_default_stripe_attr
15626 check_default_stripe_attr() {
15627         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
15628         case $1 in
15629         --stripe-count|-c)
15630                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
15631         --stripe-size|-S)
15632                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
15633         --stripe-index|-i)
15634                 EXPECTED=-1;;
15635         *)
15636                 error "unknown getstripe attr '$1'"
15637         esac
15638
15639         [ $ACTUAL == $EXPECTED ] ||
15640                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
15641 }
15642
15643 test_204a() {
15644         test_mkdir $DIR/$tdir
15645         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
15646
15647         check_default_stripe_attr --stripe-count
15648         check_default_stripe_attr --stripe-size
15649         check_default_stripe_attr --stripe-index
15650 }
15651 run_test 204a "Print default stripe attributes"
15652
15653 test_204b() {
15654         test_mkdir $DIR/$tdir
15655         $LFS setstripe --stripe-count 1 $DIR/$tdir
15656
15657         check_default_stripe_attr --stripe-size
15658         check_default_stripe_attr --stripe-index
15659 }
15660 run_test 204b "Print default stripe size and offset"
15661
15662 test_204c() {
15663         test_mkdir $DIR/$tdir
15664         $LFS setstripe --stripe-size 65536 $DIR/$tdir
15665
15666         check_default_stripe_attr --stripe-count
15667         check_default_stripe_attr --stripe-index
15668 }
15669 run_test 204c "Print default stripe count and offset"
15670
15671 test_204d() {
15672         test_mkdir $DIR/$tdir
15673         $LFS setstripe --stripe-index 0 $DIR/$tdir
15674
15675         check_default_stripe_attr --stripe-count
15676         check_default_stripe_attr --stripe-size
15677 }
15678 run_test 204d "Print default stripe count and size"
15679
15680 test_204e() {
15681         test_mkdir $DIR/$tdir
15682         $LFS setstripe -d $DIR/$tdir
15683
15684         check_default_stripe_attr --stripe-count --raw
15685         check_default_stripe_attr --stripe-size --raw
15686         check_default_stripe_attr --stripe-index --raw
15687 }
15688 run_test 204e "Print raw stripe attributes"
15689
15690 test_204f() {
15691         test_mkdir $DIR/$tdir
15692         $LFS setstripe --stripe-count 1 $DIR/$tdir
15693
15694         check_default_stripe_attr --stripe-size --raw
15695         check_default_stripe_attr --stripe-index --raw
15696 }
15697 run_test 204f "Print raw stripe size and offset"
15698
15699 test_204g() {
15700         test_mkdir $DIR/$tdir
15701         $LFS setstripe --stripe-size 65536 $DIR/$tdir
15702
15703         check_default_stripe_attr --stripe-count --raw
15704         check_default_stripe_attr --stripe-index --raw
15705 }
15706 run_test 204g "Print raw stripe count and offset"
15707
15708 test_204h() {
15709         test_mkdir $DIR/$tdir
15710         $LFS setstripe --stripe-index 0 $DIR/$tdir
15711
15712         check_default_stripe_attr --stripe-count --raw
15713         check_default_stripe_attr --stripe-size --raw
15714 }
15715 run_test 204h "Print raw stripe count and size"
15716
15717 # Figure out which job scheduler is being used, if any,
15718 # or use a fake one
15719 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
15720         JOBENV=SLURM_JOB_ID
15721 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
15722         JOBENV=LSB_JOBID
15723 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
15724         JOBENV=PBS_JOBID
15725 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
15726         JOBENV=LOADL_STEP_ID
15727 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
15728         JOBENV=JOB_ID
15729 else
15730         $LCTL list_param jobid_name > /dev/null 2>&1
15731         if [ $? -eq 0 ]; then
15732                 JOBENV=nodelocal
15733         else
15734                 JOBENV=FAKE_JOBID
15735         fi
15736 fi
15737 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
15738
15739 verify_jobstats() {
15740         local cmd=($1)
15741         shift
15742         local facets="$@"
15743
15744 # we don't really need to clear the stats for this test to work, since each
15745 # command has a unique jobid, but it makes debugging easier if needed.
15746 #       for facet in $facets; do
15747 #               local dev=$(convert_facet2label $facet)
15748 #               # clear old jobstats
15749 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
15750 #       done
15751
15752         # use a new JobID for each test, or we might see an old one
15753         [ "$JOBENV" = "FAKE_JOBID" ] &&
15754                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
15755
15756         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
15757
15758         [ "$JOBENV" = "nodelocal" ] && {
15759                 FAKE_JOBID=id.$testnum.%e.$RANDOM
15760                 $LCTL set_param jobid_name=$FAKE_JOBID
15761                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
15762         }
15763
15764         log "Test: ${cmd[*]}"
15765         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
15766
15767         if [ $JOBENV = "FAKE_JOBID" ]; then
15768                 FAKE_JOBID=$JOBVAL ${cmd[*]}
15769         else
15770                 ${cmd[*]}
15771         fi
15772
15773         # all files are created on OST0000
15774         for facet in $facets; do
15775                 local stats="*.$(convert_facet2label $facet).job_stats"
15776
15777                 # strip out libtool wrappers for in-tree executables
15778                 if [ $(do_facet $facet lctl get_param $stats |
15779                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
15780                         do_facet $facet lctl get_param $stats
15781                         error "No jobstats for $JOBVAL found on $facet::$stats"
15782                 fi
15783         done
15784 }
15785
15786 jobstats_set() {
15787         local new_jobenv=$1
15788
15789         set_persistent_param_and_check client "jobid_var" \
15790                 "$FSNAME.sys.jobid_var" $new_jobenv
15791 }
15792
15793 test_205a() { # Job stats
15794         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15795         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
15796                 skip "Need MDS version with at least 2.7.1"
15797         remote_mgs_nodsh && skip "remote MGS with nodsh"
15798         remote_mds_nodsh && skip "remote MDS with nodsh"
15799         remote_ost_nodsh && skip "remote OST with nodsh"
15800         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
15801                 skip "Server doesn't support jobstats"
15802         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
15803
15804         local old_jobenv=$($LCTL get_param -n jobid_var)
15805         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
15806
15807         if [[ $PERM_CMD == *"set_param -P"* ]]; then
15808                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
15809         else
15810                 stack_trap "do_facet mgs $PERM_CMD \
15811                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
15812         fi
15813         changelog_register
15814
15815         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
15816                                 mdt.*.job_cleanup_interval | head -n 1)
15817         local new_interval=5
15818         do_facet $SINGLEMDS \
15819                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
15820         stack_trap "do_facet $SINGLEMDS \
15821                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
15822         local start=$SECONDS
15823
15824         local cmd
15825         # mkdir
15826         cmd="mkdir $DIR/$tdir"
15827         verify_jobstats "$cmd" "$SINGLEMDS"
15828         # rmdir
15829         cmd="rmdir $DIR/$tdir"
15830         verify_jobstats "$cmd" "$SINGLEMDS"
15831         # mkdir on secondary MDT
15832         if [ $MDSCOUNT -gt 1 ]; then
15833                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
15834                 verify_jobstats "$cmd" "mds2"
15835         fi
15836         # mknod
15837         cmd="mknod $DIR/$tfile c 1 3"
15838         verify_jobstats "$cmd" "$SINGLEMDS"
15839         # unlink
15840         cmd="rm -f $DIR/$tfile"
15841         verify_jobstats "$cmd" "$SINGLEMDS"
15842         # create all files on OST0000 so verify_jobstats can find OST stats
15843         # open & close
15844         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
15845         verify_jobstats "$cmd" "$SINGLEMDS"
15846         # setattr
15847         cmd="touch $DIR/$tfile"
15848         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15849         # write
15850         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
15851         verify_jobstats "$cmd" "ost1"
15852         # read
15853         cancel_lru_locks osc
15854         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
15855         verify_jobstats "$cmd" "ost1"
15856         # truncate
15857         cmd="$TRUNCATE $DIR/$tfile 0"
15858         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15859         # rename
15860         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
15861         verify_jobstats "$cmd" "$SINGLEMDS"
15862         # jobstats expiry - sleep until old stats should be expired
15863         local left=$((new_interval + 5 - (SECONDS - start)))
15864         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
15865                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
15866                         "0" $left
15867         cmd="mkdir $DIR/$tdir.expire"
15868         verify_jobstats "$cmd" "$SINGLEMDS"
15869         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
15870             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
15871
15872         # Ensure that jobid are present in changelog (if supported by MDS)
15873         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
15874                 changelog_dump | tail -10
15875                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
15876                 [ $jobids -eq 9 ] ||
15877                         error "Wrong changelog jobid count $jobids != 9"
15878
15879                 # LU-5862
15880                 JOBENV="disable"
15881                 jobstats_set $JOBENV
15882                 touch $DIR/$tfile
15883                 changelog_dump | grep $tfile
15884                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
15885                 [ $jobids -eq 0 ] ||
15886                         error "Unexpected jobids when jobid_var=$JOBENV"
15887         fi
15888
15889         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
15890         JOBENV="JOBCOMPLEX"
15891         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
15892
15893         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
15894 }
15895 run_test 205a "Verify job stats"
15896
15897 # LU-13117
15898 test_205b() {
15899         $LCTL set_param jobid_var=USER jobid_name="%e.%u"
15900         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
15901         do_facet $SINGLEMDS $LCTL get_param mdt.*.job_stats |
15902                 grep job_id: | grep foolish &&
15903                         error "Unexpected jobid found"
15904         true
15905 }
15906 run_test 205b "Verify job stats jobid parsing"
15907
15908 # LU-1480, LU-1773 and LU-1657
15909 test_206() {
15910         mkdir -p $DIR/$tdir
15911         $LFS setstripe -c -1 $DIR/$tdir
15912 #define OBD_FAIL_LOV_INIT 0x1403
15913         $LCTL set_param fail_loc=0xa0001403
15914         $LCTL set_param fail_val=1
15915         touch $DIR/$tdir/$tfile || true
15916 }
15917 run_test 206 "fail lov_init_raid0() doesn't lbug"
15918
15919 test_207a() {
15920         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15921         local fsz=`stat -c %s $DIR/$tfile`
15922         cancel_lru_locks mdc
15923
15924         # do not return layout in getattr intent
15925 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
15926         $LCTL set_param fail_loc=0x170
15927         local sz=`stat -c %s $DIR/$tfile`
15928
15929         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
15930
15931         rm -rf $DIR/$tfile
15932 }
15933 run_test 207a "can refresh layout at glimpse"
15934
15935 test_207b() {
15936         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15937         local cksum=`md5sum $DIR/$tfile`
15938         local fsz=`stat -c %s $DIR/$tfile`
15939         cancel_lru_locks mdc
15940         cancel_lru_locks osc
15941
15942         # do not return layout in getattr intent
15943 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
15944         $LCTL set_param fail_loc=0x171
15945
15946         # it will refresh layout after the file is opened but before read issues
15947         echo checksum is "$cksum"
15948         echo "$cksum" |md5sum -c --quiet || error "file differs"
15949
15950         rm -rf $DIR/$tfile
15951 }
15952 run_test 207b "can refresh layout at open"
15953
15954 test_208() {
15955         # FIXME: in this test suite, only RD lease is used. This is okay
15956         # for now as only exclusive open is supported. After generic lease
15957         # is done, this test suite should be revised. - Jinshan
15958
15959         remote_mds_nodsh && skip "remote MDS with nodsh"
15960         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
15961                 skip "Need MDS version at least 2.4.52"
15962
15963         echo "==== test 1: verify get lease work"
15964         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
15965
15966         echo "==== test 2: verify lease can be broken by upcoming open"
15967         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15968         local PID=$!
15969         sleep 1
15970
15971         $MULTIOP $DIR/$tfile oO_RDONLY:c
15972         kill -USR1 $PID && wait $PID || error "break lease error"
15973
15974         echo "==== test 3: verify lease can't be granted if an open already exists"
15975         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
15976         local PID=$!
15977         sleep 1
15978
15979         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
15980         kill -USR1 $PID && wait $PID || error "open file error"
15981
15982         echo "==== test 4: lease can sustain over recovery"
15983         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
15984         PID=$!
15985         sleep 1
15986
15987         fail mds1
15988
15989         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
15990
15991         echo "==== test 5: lease broken can't be regained by replay"
15992         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15993         PID=$!
15994         sleep 1
15995
15996         # open file to break lease and then recovery
15997         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
15998         fail mds1
15999
16000         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
16001
16002         rm -f $DIR/$tfile
16003 }
16004 run_test 208 "Exclusive open"
16005
16006 test_209() {
16007         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
16008                 skip_env "must have disp_stripe"
16009
16010         touch $DIR/$tfile
16011         sync; sleep 5; sync;
16012
16013         echo 3 > /proc/sys/vm/drop_caches
16014         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
16015
16016         # open/close 500 times
16017         for i in $(seq 500); do
16018                 cat $DIR/$tfile
16019         done
16020
16021         echo 3 > /proc/sys/vm/drop_caches
16022         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
16023
16024         echo "before: $req_before, after: $req_after"
16025         [ $((req_after - req_before)) -ge 300 ] &&
16026                 error "open/close requests are not freed"
16027         return 0
16028 }
16029 run_test 209 "read-only open/close requests should be freed promptly"
16030
16031 test_212() {
16032         size=`date +%s`
16033         size=$((size % 8192 + 1))
16034         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
16035         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
16036         rm -f $DIR/f212 $DIR/f212.xyz
16037 }
16038 run_test 212 "Sendfile test ============================================"
16039
16040 test_213() {
16041         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
16042         cancel_lru_locks osc
16043         lctl set_param fail_loc=0x8000040f
16044         # generate a read lock
16045         cat $DIR/$tfile > /dev/null
16046         # write to the file, it will try to cancel the above read lock.
16047         cat /etc/hosts >> $DIR/$tfile
16048 }
16049 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
16050
16051 test_214() { # for bug 20133
16052         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
16053         for (( i=0; i < 340; i++ )) ; do
16054                 touch $DIR/$tdir/d214c/a$i
16055         done
16056
16057         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
16058         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
16059         ls $DIR/d214c || error "ls $DIR/d214c failed"
16060         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
16061         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
16062 }
16063 run_test 214 "hash-indexed directory test - bug 20133"
16064
16065 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
16066 create_lnet_proc_files() {
16067         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
16068 }
16069
16070 # counterpart of create_lnet_proc_files
16071 remove_lnet_proc_files() {
16072         rm -f $TMP/lnet_$1.sys
16073 }
16074
16075 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
16076 # 3rd arg as regexp for body
16077 check_lnet_proc_stats() {
16078         local l=$(cat "$TMP/lnet_$1" |wc -l)
16079         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
16080
16081         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
16082 }
16083
16084 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
16085 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
16086 # optional and can be regexp for 2nd line (lnet.routes case)
16087 check_lnet_proc_entry() {
16088         local blp=2          # blp stands for 'position of 1st line of body'
16089         [ -z "$5" ] || blp=3 # lnet.routes case
16090
16091         local l=$(cat "$TMP/lnet_$1" |wc -l)
16092         # subtracting one from $blp because the body can be empty
16093         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
16094
16095         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
16096                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
16097
16098         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
16099                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
16100
16101         # bail out if any unexpected line happened
16102         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
16103         [ "$?" != 0 ] || error "$2 misformatted"
16104 }
16105
16106 test_215() { # for bugs 18102, 21079, 21517
16107         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16108
16109         local N='(0|[1-9][0-9]*)'       # non-negative numeric
16110         local P='[1-9][0-9]*'           # positive numeric
16111         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
16112         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
16113         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
16114         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
16115
16116         local L1 # regexp for 1st line
16117         local L2 # regexp for 2nd line (optional)
16118         local BR # regexp for the rest (body)
16119
16120         # lnet.stats should look as 11 space-separated non-negative numerics
16121         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
16122         create_lnet_proc_files "stats"
16123         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
16124         remove_lnet_proc_files "stats"
16125
16126         # lnet.routes should look like this:
16127         # Routing disabled/enabled
16128         # net hops priority state router
16129         # where net is a string like tcp0, hops > 0, priority >= 0,
16130         # state is up/down,
16131         # router is a string like 192.168.1.1@tcp2
16132         L1="^Routing (disabled|enabled)$"
16133         L2="^net +hops +priority +state +router$"
16134         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
16135         create_lnet_proc_files "routes"
16136         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
16137         remove_lnet_proc_files "routes"
16138
16139         # lnet.routers should look like this:
16140         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
16141         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
16142         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
16143         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
16144         L1="^ref +rtr_ref +alive +router$"
16145         BR="^$P +$P +(up|down) +$NID$"
16146         create_lnet_proc_files "routers"
16147         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
16148         remove_lnet_proc_files "routers"
16149
16150         # lnet.peers should look like this:
16151         # nid refs state last max rtr min tx min queue
16152         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
16153         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
16154         # numeric (0 or >0 or <0), queue >= 0.
16155         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
16156         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
16157         create_lnet_proc_files "peers"
16158         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
16159         remove_lnet_proc_files "peers"
16160
16161         # lnet.buffers  should look like this:
16162         # pages count credits min
16163         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
16164         L1="^pages +count +credits +min$"
16165         BR="^ +$N +$N +$I +$I$"
16166         create_lnet_proc_files "buffers"
16167         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
16168         remove_lnet_proc_files "buffers"
16169
16170         # lnet.nis should look like this:
16171         # nid status alive refs peer rtr max tx min
16172         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
16173         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
16174         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
16175         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
16176         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
16177         create_lnet_proc_files "nis"
16178         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
16179         remove_lnet_proc_files "nis"
16180
16181         # can we successfully write to lnet.stats?
16182         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
16183 }
16184 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
16185
16186 test_216() { # bug 20317
16187         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16188         remote_ost_nodsh && skip "remote OST with nodsh"
16189
16190         local node
16191         local facets=$(get_facets OST)
16192         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16193
16194         save_lustre_params client "osc.*.contention_seconds" > $p
16195         save_lustre_params $facets \
16196                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
16197         save_lustre_params $facets \
16198                 "ldlm.namespaces.filter-*.contended_locks" >> $p
16199         save_lustre_params $facets \
16200                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
16201         clear_stats osc.*.osc_stats
16202
16203         # agressive lockless i/o settings
16204         do_nodes $(comma_list $(osts_nodes)) \
16205                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
16206                         ldlm.namespaces.filter-*.contended_locks=0 \
16207                         ldlm.namespaces.filter-*.contention_seconds=60"
16208         lctl set_param -n osc.*.contention_seconds=60
16209
16210         $DIRECTIO write $DIR/$tfile 0 10 4096
16211         $CHECKSTAT -s 40960 $DIR/$tfile
16212
16213         # disable lockless i/o
16214         do_nodes $(comma_list $(osts_nodes)) \
16215                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
16216                         ldlm.namespaces.filter-*.contended_locks=32 \
16217                         ldlm.namespaces.filter-*.contention_seconds=0"
16218         lctl set_param -n osc.*.contention_seconds=0
16219         clear_stats osc.*.osc_stats
16220
16221         dd if=/dev/zero of=$DIR/$tfile count=0
16222         $CHECKSTAT -s 0 $DIR/$tfile
16223
16224         restore_lustre_params <$p
16225         rm -f $p
16226         rm $DIR/$tfile
16227 }
16228 run_test 216 "check lockless direct write updates file size and kms correctly"
16229
16230 test_217() { # bug 22430
16231         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16232
16233         local node
16234         local nid
16235
16236         for node in $(nodes_list); do
16237                 nid=$(host_nids_address $node $NETTYPE)
16238                 if [[ $nid = *-* ]] ; then
16239                         echo "lctl ping $(h2nettype $nid)"
16240                         lctl ping $(h2nettype $nid)
16241                 else
16242                         echo "skipping $node (no hyphen detected)"
16243                 fi
16244         done
16245 }
16246 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
16247
16248 test_218() {
16249        # do directio so as not to populate the page cache
16250        log "creating a 10 Mb file"
16251        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
16252        log "starting reads"
16253        dd if=$DIR/$tfile of=/dev/null bs=4096 &
16254        log "truncating the file"
16255        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
16256        log "killing dd"
16257        kill %+ || true # reads might have finished
16258        echo "wait until dd is finished"
16259        wait
16260        log "removing the temporary file"
16261        rm -rf $DIR/$tfile || error "tmp file removal failed"
16262 }
16263 run_test 218 "parallel read and truncate should not deadlock"
16264
16265 test_219() {
16266         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16267
16268         # write one partial page
16269         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
16270         # set no grant so vvp_io_commit_write will do sync write
16271         $LCTL set_param fail_loc=0x411
16272         # write a full page at the end of file
16273         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
16274
16275         $LCTL set_param fail_loc=0
16276         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
16277         $LCTL set_param fail_loc=0x411
16278         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
16279
16280         # LU-4201
16281         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
16282         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
16283 }
16284 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
16285
16286 test_220() { #LU-325
16287         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16288         remote_ost_nodsh && skip "remote OST with nodsh"
16289         remote_mds_nodsh && skip "remote MDS with nodsh"
16290         remote_mgs_nodsh && skip "remote MGS with nodsh"
16291
16292         local OSTIDX=0
16293
16294         # create on MDT0000 so the last_id and next_id are correct
16295         mkdir $DIR/$tdir
16296         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
16297         OST=${OST%_UUID}
16298
16299         # on the mdt's osc
16300         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
16301         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
16302                         osp.$mdtosc_proc1.prealloc_last_id)
16303         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
16304                         osp.$mdtosc_proc1.prealloc_next_id)
16305
16306         $LFS df -i
16307
16308         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
16309         #define OBD_FAIL_OST_ENOINO              0x229
16310         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
16311         create_pool $FSNAME.$TESTNAME || return 1
16312         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
16313
16314         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
16315
16316         MDSOBJS=$((last_id - next_id))
16317         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
16318
16319         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
16320         echo "OST still has $count kbytes free"
16321
16322         echo "create $MDSOBJS files @next_id..."
16323         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
16324
16325         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
16326                         osp.$mdtosc_proc1.prealloc_last_id)
16327         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
16328                         osp.$mdtosc_proc1.prealloc_next_id)
16329
16330         echo "after creation, last_id=$last_id2, next_id=$next_id2"
16331         $LFS df -i
16332
16333         echo "cleanup..."
16334
16335         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
16336         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
16337
16338         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
16339                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
16340         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
16341                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
16342         echo "unlink $MDSOBJS files @$next_id..."
16343         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
16344 }
16345 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
16346
16347 test_221() {
16348         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16349
16350         dd if=`which date` of=$MOUNT/date oflag=sync
16351         chmod +x $MOUNT/date
16352
16353         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
16354         $LCTL set_param fail_loc=0x80001401
16355
16356         $MOUNT/date > /dev/null
16357         rm -f $MOUNT/date
16358 }
16359 run_test 221 "make sure fault and truncate race to not cause OOM"
16360
16361 test_222a () {
16362         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16363
16364         rm -rf $DIR/$tdir
16365         test_mkdir $DIR/$tdir
16366         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16367         createmany -o $DIR/$tdir/$tfile 10
16368         cancel_lru_locks mdc
16369         cancel_lru_locks osc
16370         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16371         $LCTL set_param fail_loc=0x31a
16372         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
16373         $LCTL set_param fail_loc=0
16374         rm -r $DIR/$tdir
16375 }
16376 run_test 222a "AGL for ls should not trigger CLIO lock failure"
16377
16378 test_222b () {
16379         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16380
16381         rm -rf $DIR/$tdir
16382         test_mkdir $DIR/$tdir
16383         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16384         createmany -o $DIR/$tdir/$tfile 10
16385         cancel_lru_locks mdc
16386         cancel_lru_locks osc
16387         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16388         $LCTL set_param fail_loc=0x31a
16389         rm -r $DIR/$tdir || error "AGL for rmdir failed"
16390         $LCTL set_param fail_loc=0
16391 }
16392 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
16393
16394 test_223 () {
16395         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16396
16397         rm -rf $DIR/$tdir
16398         test_mkdir $DIR/$tdir
16399         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16400         createmany -o $DIR/$tdir/$tfile 10
16401         cancel_lru_locks mdc
16402         cancel_lru_locks osc
16403         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
16404         $LCTL set_param fail_loc=0x31b
16405         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
16406         $LCTL set_param fail_loc=0
16407         rm -r $DIR/$tdir
16408 }
16409 run_test 223 "osc reenqueue if without AGL lock granted ======================="
16410
16411 test_224a() { # LU-1039, MRP-303
16412         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16413
16414         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
16415         $LCTL set_param fail_loc=0x508
16416         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
16417         $LCTL set_param fail_loc=0
16418         df $DIR
16419 }
16420 run_test 224a "Don't panic on bulk IO failure"
16421
16422 test_224b() { # LU-1039, MRP-303
16423         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16424
16425         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
16426         cancel_lru_locks osc
16427         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
16428         $LCTL set_param fail_loc=0x515
16429         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
16430         $LCTL set_param fail_loc=0
16431         df $DIR
16432 }
16433 run_test 224b "Don't panic on bulk IO failure"
16434
16435 test_224c() { # LU-6441
16436         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16437         remote_mds_nodsh && skip "remote MDS with nodsh"
16438
16439         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16440         save_writethrough $p
16441         set_cache writethrough on
16442
16443         local pages_per_rpc=$($LCTL get_param osc.*.max_pages_per_rpc)
16444         local at_max=$($LCTL get_param -n at_max)
16445         local timeout=$($LCTL get_param -n timeout)
16446         local test_at="at_max"
16447         local param_at="$FSNAME.sys.at_max"
16448         local test_timeout="timeout"
16449         local param_timeout="$FSNAME.sys.timeout"
16450
16451         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
16452
16453         set_persistent_param_and_check client "$test_at" "$param_at" 0
16454         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
16455
16456         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
16457         do_facet ost1 "$LCTL set_param fail_loc=0x520"
16458         $LFS setstripe -c 1 -i 0 $DIR/$tfile
16459         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
16460         sync
16461         do_facet ost1 "$LCTL set_param fail_loc=0"
16462
16463         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
16464         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
16465                 $timeout
16466
16467         $LCTL set_param -n $pages_per_rpc
16468         restore_lustre_params < $p
16469         rm -f $p
16470 }
16471 run_test 224c "Don't hang if one of md lost during large bulk RPC"
16472
16473 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
16474 test_225a () {
16475         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16476         if [ -z ${MDSSURVEY} ]; then
16477                 skip_env "mds-survey not found"
16478         fi
16479         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16480                 skip "Need MDS version at least 2.2.51"
16481
16482         local mds=$(facet_host $SINGLEMDS)
16483         local target=$(do_nodes $mds 'lctl dl' |
16484                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16485
16486         local cmd1="file_count=1000 thrhi=4"
16487         local cmd2="dir_count=2 layer=mdd stripe_count=0"
16488         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16489         local cmd="$cmd1 $cmd2 $cmd3"
16490
16491         rm -f ${TMP}/mds_survey*
16492         echo + $cmd
16493         eval $cmd || error "mds-survey with zero-stripe failed"
16494         cat ${TMP}/mds_survey*
16495         rm -f ${TMP}/mds_survey*
16496 }
16497 run_test 225a "Metadata survey sanity with zero-stripe"
16498
16499 test_225b () {
16500         if [ -z ${MDSSURVEY} ]; then
16501                 skip_env "mds-survey not found"
16502         fi
16503         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16504                 skip "Need MDS version at least 2.2.51"
16505         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16506         remote_mds_nodsh && skip "remote MDS with nodsh"
16507         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
16508                 skip_env "Need to mount OST to test"
16509         fi
16510
16511         local mds=$(facet_host $SINGLEMDS)
16512         local target=$(do_nodes $mds 'lctl dl' |
16513                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16514
16515         local cmd1="file_count=1000 thrhi=4"
16516         local cmd2="dir_count=2 layer=mdd stripe_count=1"
16517         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16518         local cmd="$cmd1 $cmd2 $cmd3"
16519
16520         rm -f ${TMP}/mds_survey*
16521         echo + $cmd
16522         eval $cmd || error "mds-survey with stripe_count failed"
16523         cat ${TMP}/mds_survey*
16524         rm -f ${TMP}/mds_survey*
16525 }
16526 run_test 225b "Metadata survey sanity with stripe_count = 1"
16527
16528 mcreate_path2fid () {
16529         local mode=$1
16530         local major=$2
16531         local minor=$3
16532         local name=$4
16533         local desc=$5
16534         local path=$DIR/$tdir/$name
16535         local fid
16536         local rc
16537         local fid_path
16538
16539         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
16540                 error "cannot create $desc"
16541
16542         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
16543         rc=$?
16544         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
16545
16546         fid_path=$($LFS fid2path $MOUNT $fid)
16547         rc=$?
16548         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
16549
16550         [ "$path" == "$fid_path" ] ||
16551                 error "fid2path returned $fid_path, expected $path"
16552
16553         echo "pass with $path and $fid"
16554 }
16555
16556 test_226a () {
16557         rm -rf $DIR/$tdir
16558         mkdir -p $DIR/$tdir
16559
16560         mcreate_path2fid 0010666 0 0 fifo "FIFO"
16561         mcreate_path2fid 0020666 1 3 null "character special file (null)"
16562         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
16563         mcreate_path2fid 0040666 0 0 dir "directory"
16564         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
16565         mcreate_path2fid 0100666 0 0 file "regular file"
16566         mcreate_path2fid 0120666 0 0 link "symbolic link"
16567         mcreate_path2fid 0140666 0 0 sock "socket"
16568 }
16569 run_test 226a "call path2fid and fid2path on files of all type"
16570
16571 test_226b () {
16572         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16573
16574         local MDTIDX=1
16575
16576         rm -rf $DIR/$tdir
16577         mkdir -p $DIR/$tdir
16578         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
16579                 error "create remote directory failed"
16580         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
16581         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
16582                                 "character special file (null)"
16583         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
16584                                 "character special file (no device)"
16585         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
16586         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
16587                                 "block special file (loop)"
16588         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
16589         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
16590         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
16591 }
16592 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
16593
16594 # LU-1299 Executing or running ldd on a truncated executable does not
16595 # cause an out-of-memory condition.
16596 test_227() {
16597         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16598         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
16599
16600         dd if=$(which date) of=$MOUNT/date bs=1k count=1
16601         chmod +x $MOUNT/date
16602
16603         $MOUNT/date > /dev/null
16604         ldd $MOUNT/date > /dev/null
16605         rm -f $MOUNT/date
16606 }
16607 run_test 227 "running truncated executable does not cause OOM"
16608
16609 # LU-1512 try to reuse idle OI blocks
16610 test_228a() {
16611         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16612         remote_mds_nodsh && skip "remote MDS with nodsh"
16613         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16614
16615         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16616         local myDIR=$DIR/$tdir
16617
16618         mkdir -p $myDIR
16619         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16620         $LCTL set_param fail_loc=0x80001002
16621         createmany -o $myDIR/t- 10000
16622         $LCTL set_param fail_loc=0
16623         # The guard is current the largest FID holder
16624         touch $myDIR/guard
16625         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16626                     tr -d '[')
16627         local IDX=$(($SEQ % 64))
16628
16629         do_facet $SINGLEMDS sync
16630         # Make sure journal flushed.
16631         sleep 6
16632         local blk1=$(do_facet $SINGLEMDS \
16633                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16634                      grep Blockcount | awk '{print $4}')
16635
16636         # Remove old files, some OI blocks will become idle.
16637         unlinkmany $myDIR/t- 10000
16638         # Create new files, idle OI blocks should be reused.
16639         createmany -o $myDIR/t- 2000
16640         do_facet $SINGLEMDS sync
16641         # Make sure journal flushed.
16642         sleep 6
16643         local blk2=$(do_facet $SINGLEMDS \
16644                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16645                      grep Blockcount | awk '{print $4}')
16646
16647         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16648 }
16649 run_test 228a "try to reuse idle OI blocks"
16650
16651 test_228b() {
16652         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16653         remote_mds_nodsh && skip "remote MDS with nodsh"
16654         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16655
16656         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16657         local myDIR=$DIR/$tdir
16658
16659         mkdir -p $myDIR
16660         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16661         $LCTL set_param fail_loc=0x80001002
16662         createmany -o $myDIR/t- 10000
16663         $LCTL set_param fail_loc=0
16664         # The guard is current the largest FID holder
16665         touch $myDIR/guard
16666         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16667                     tr -d '[')
16668         local IDX=$(($SEQ % 64))
16669
16670         do_facet $SINGLEMDS sync
16671         # Make sure journal flushed.
16672         sleep 6
16673         local blk1=$(do_facet $SINGLEMDS \
16674                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16675                      grep Blockcount | awk '{print $4}')
16676
16677         # Remove old files, some OI blocks will become idle.
16678         unlinkmany $myDIR/t- 10000
16679
16680         # stop the MDT
16681         stop $SINGLEMDS || error "Fail to stop MDT."
16682         # remount the MDT
16683         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
16684
16685         df $MOUNT || error "Fail to df."
16686         # Create new files, idle OI blocks should be reused.
16687         createmany -o $myDIR/t- 2000
16688         do_facet $SINGLEMDS sync
16689         # Make sure journal flushed.
16690         sleep 6
16691         local blk2=$(do_facet $SINGLEMDS \
16692                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16693                      grep Blockcount | awk '{print $4}')
16694
16695         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16696 }
16697 run_test 228b "idle OI blocks can be reused after MDT restart"
16698
16699 #LU-1881
16700 test_228c() {
16701         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16702         remote_mds_nodsh && skip "remote MDS with nodsh"
16703         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16704
16705         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16706         local myDIR=$DIR/$tdir
16707
16708         mkdir -p $myDIR
16709         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16710         $LCTL set_param fail_loc=0x80001002
16711         # 20000 files can guarantee there are index nodes in the OI file
16712         createmany -o $myDIR/t- 20000
16713         $LCTL set_param fail_loc=0
16714         # The guard is current the largest FID holder
16715         touch $myDIR/guard
16716         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16717                     tr -d '[')
16718         local IDX=$(($SEQ % 64))
16719
16720         do_facet $SINGLEMDS sync
16721         # Make sure journal flushed.
16722         sleep 6
16723         local blk1=$(do_facet $SINGLEMDS \
16724                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16725                      grep Blockcount | awk '{print $4}')
16726
16727         # Remove old files, some OI blocks will become idle.
16728         unlinkmany $myDIR/t- 20000
16729         rm -f $myDIR/guard
16730         # The OI file should become empty now
16731
16732         # Create new files, idle OI blocks should be reused.
16733         createmany -o $myDIR/t- 2000
16734         do_facet $SINGLEMDS sync
16735         # Make sure journal flushed.
16736         sleep 6
16737         local blk2=$(do_facet $SINGLEMDS \
16738                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16739                      grep Blockcount | awk '{print $4}')
16740
16741         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16742 }
16743 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
16744
16745 test_229() { # LU-2482, LU-3448
16746         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16747         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
16748         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
16749                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
16750
16751         rm -f $DIR/$tfile
16752
16753         # Create a file with a released layout and stripe count 2.
16754         $MULTIOP $DIR/$tfile H2c ||
16755                 error "failed to create file with released layout"
16756
16757         $LFS getstripe -v $DIR/$tfile
16758
16759         local pattern=$($LFS getstripe -L $DIR/$tfile)
16760         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
16761
16762         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
16763                 error "getstripe"
16764         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
16765         stat $DIR/$tfile || error "failed to stat released file"
16766
16767         chown $RUNAS_ID $DIR/$tfile ||
16768                 error "chown $RUNAS_ID $DIR/$tfile failed"
16769
16770         chgrp $RUNAS_ID $DIR/$tfile ||
16771                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
16772
16773         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
16774         rm $DIR/$tfile || error "failed to remove released file"
16775 }
16776 run_test 229 "getstripe/stat/rm/attr changes work on released files"
16777
16778 test_230a() {
16779         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16780         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16781         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16782                 skip "Need MDS version at least 2.11.52"
16783
16784         local MDTIDX=1
16785
16786         test_mkdir $DIR/$tdir
16787         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
16788         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
16789         [ $mdt_idx -ne 0 ] &&
16790                 error "create local directory on wrong MDT $mdt_idx"
16791
16792         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
16793                         error "create remote directory failed"
16794         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
16795         [ $mdt_idx -ne $MDTIDX ] &&
16796                 error "create remote directory on wrong MDT $mdt_idx"
16797
16798         createmany -o $DIR/$tdir/test_230/t- 10 ||
16799                 error "create files on remote directory failed"
16800         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
16801         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
16802         rm -r $DIR/$tdir || error "unlink remote directory failed"
16803 }
16804 run_test 230a "Create remote directory and files under the remote directory"
16805
16806 test_230b() {
16807         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16808         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16809         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16810                 skip "Need MDS version at least 2.11.52"
16811
16812         local MDTIDX=1
16813         local mdt_index
16814         local i
16815         local file
16816         local pid
16817         local stripe_count
16818         local migrate_dir=$DIR/$tdir/migrate_dir
16819         local other_dir=$DIR/$tdir/other_dir
16820
16821         test_mkdir $DIR/$tdir
16822         test_mkdir -i0 -c1 $migrate_dir
16823         test_mkdir -i0 -c1 $other_dir
16824         for ((i=0; i<10; i++)); do
16825                 mkdir -p $migrate_dir/dir_${i}
16826                 createmany -o $migrate_dir/dir_${i}/f 10 ||
16827                         error "create files under remote dir failed $i"
16828         done
16829
16830         cp /etc/passwd $migrate_dir/$tfile
16831         cp /etc/passwd $other_dir/$tfile
16832         chattr +SAD $migrate_dir
16833         chattr +SAD $migrate_dir/$tfile
16834
16835         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16836         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16837         local old_dir_mode=$(stat -c%f $migrate_dir)
16838         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
16839
16840         mkdir -p $migrate_dir/dir_default_stripe2
16841         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
16842         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
16843
16844         mkdir -p $other_dir
16845         ln $migrate_dir/$tfile $other_dir/luna
16846         ln $migrate_dir/$tfile $migrate_dir/sofia
16847         ln $other_dir/$tfile $migrate_dir/david
16848         ln -s $migrate_dir/$tfile $other_dir/zachary
16849         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
16850         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
16851
16852         local len
16853         local lnktgt
16854
16855         # inline symlink
16856         for len in 58 59 60; do
16857                 lnktgt=$(str_repeat 'l' $len)
16858                 touch $migrate_dir/$lnktgt
16859                 ln -s $lnktgt $migrate_dir/${len}char_ln
16860         done
16861
16862         # PATH_MAX
16863         for len in 4094 4095; do
16864                 lnktgt=$(str_repeat 'l' $len)
16865                 ln -s $lnktgt $migrate_dir/${len}char_ln
16866         done
16867
16868         # NAME_MAX
16869         for len in 254 255; do
16870                 touch $migrate_dir/$(str_repeat 'l' $len)
16871         done
16872
16873         $LFS migrate -m $MDTIDX $migrate_dir ||
16874                 error "fails on migrating remote dir to MDT1"
16875
16876         echo "migratate to MDT1, then checking.."
16877         for ((i = 0; i < 10; i++)); do
16878                 for file in $(find $migrate_dir/dir_${i}); do
16879                         mdt_index=$($LFS getstripe -m $file)
16880                         # broken symlink getstripe will fail
16881                         [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
16882                                 error "$file is not on MDT${MDTIDX}"
16883                 done
16884         done
16885
16886         # the multiple link file should still in MDT0
16887         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
16888         [ $mdt_index == 0 ] ||
16889                 error "$file is not on MDT${MDTIDX}"
16890
16891         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16892         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16893                 error " expect $old_dir_flag get $new_dir_flag"
16894
16895         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16896         [ "$old_file_flag" = "$new_file_flag" ] ||
16897                 error " expect $old_file_flag get $new_file_flag"
16898
16899         local new_dir_mode=$(stat -c%f $migrate_dir)
16900         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16901                 error "expect mode $old_dir_mode get $new_dir_mode"
16902
16903         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16904         [ "$old_file_mode" = "$new_file_mode" ] ||
16905                 error "expect mode $old_file_mode get $new_file_mode"
16906
16907         diff /etc/passwd $migrate_dir/$tfile ||
16908                 error "$tfile different after migration"
16909
16910         diff /etc/passwd $other_dir/luna ||
16911                 error "luna different after migration"
16912
16913         diff /etc/passwd $migrate_dir/sofia ||
16914                 error "sofia different after migration"
16915
16916         diff /etc/passwd $migrate_dir/david ||
16917                 error "david different after migration"
16918
16919         diff /etc/passwd $other_dir/zachary ||
16920                 error "zachary different after migration"
16921
16922         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16923                 error "${tfile}_ln different after migration"
16924
16925         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16926                 error "${tfile}_ln_other different after migration"
16927
16928         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
16929         [ $stripe_count = 2 ] ||
16930                 error "dir strpe_count $d != 2 after migration."
16931
16932         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
16933         [ $stripe_count = 2 ] ||
16934                 error "file strpe_count $d != 2 after migration."
16935
16936         #migrate back to MDT0
16937         MDTIDX=0
16938
16939         $LFS migrate -m $MDTIDX $migrate_dir ||
16940                 error "fails on migrating remote dir to MDT0"
16941
16942         echo "migrate back to MDT0, checking.."
16943         for file in $(find $migrate_dir); do
16944                 mdt_index=$($LFS getstripe -m $file)
16945                 [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
16946                         error "$file is not on MDT${MDTIDX}"
16947         done
16948
16949         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16950         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16951                 error " expect $old_dir_flag get $new_dir_flag"
16952
16953         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16954         [ "$old_file_flag" = "$new_file_flag" ] ||
16955                 error " expect $old_file_flag get $new_file_flag"
16956
16957         local new_dir_mode=$(stat -c%f $migrate_dir)
16958         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16959                 error "expect mode $old_dir_mode get $new_dir_mode"
16960
16961         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16962         [ "$old_file_mode" = "$new_file_mode" ] ||
16963                 error "expect mode $old_file_mode get $new_file_mode"
16964
16965         diff /etc/passwd ${migrate_dir}/$tfile ||
16966                 error "$tfile different after migration"
16967
16968         diff /etc/passwd ${other_dir}/luna ||
16969                 error "luna different after migration"
16970
16971         diff /etc/passwd ${migrate_dir}/sofia ||
16972                 error "sofia different after migration"
16973
16974         diff /etc/passwd ${other_dir}/zachary ||
16975                 error "zachary different after migration"
16976
16977         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16978                 error "${tfile}_ln different after migration"
16979
16980         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16981                 error "${tfile}_ln_other different after migration"
16982
16983         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
16984         [ $stripe_count = 2 ] ||
16985                 error "dir strpe_count $d != 2 after migration."
16986
16987         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
16988         [ $stripe_count = 2 ] ||
16989                 error "file strpe_count $d != 2 after migration."
16990
16991         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16992 }
16993 run_test 230b "migrate directory"
16994
16995 test_230c() {
16996         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16997         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16998         remote_mds_nodsh && skip "remote MDS with nodsh"
16999         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17000                 skip "Need MDS version at least 2.11.52"
17001
17002         local MDTIDX=1
17003         local total=3
17004         local mdt_index
17005         local file
17006         local migrate_dir=$DIR/$tdir/migrate_dir
17007
17008         #If migrating directory fails in the middle, all entries of
17009         #the directory is still accessiable.
17010         test_mkdir $DIR/$tdir
17011         test_mkdir -i0 -c1 $migrate_dir
17012         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
17013         stat $migrate_dir
17014         createmany -o $migrate_dir/f $total ||
17015                 error "create files under ${migrate_dir} failed"
17016
17017         # fail after migrating top dir, and this will fail only once, so the
17018         # first sub file migration will fail (currently f3), others succeed.
17019         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
17020         do_facet mds1 lctl set_param fail_loc=0x1801
17021         local t=$(ls $migrate_dir | wc -l)
17022         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
17023                 error "migrate should fail"
17024         local u=$(ls $migrate_dir | wc -l)
17025         [ "$u" == "$t" ] || error "$u != $t during migration"
17026
17027         # add new dir/file should succeed
17028         mkdir $migrate_dir/dir ||
17029                 error "mkdir failed under migrating directory"
17030         touch $migrate_dir/file ||
17031                 error "create file failed under migrating directory"
17032
17033         # add file with existing name should fail
17034         for file in $migrate_dir/f*; do
17035                 stat $file > /dev/null || error "stat $file failed"
17036                 $OPENFILE -f O_CREAT:O_EXCL $file &&
17037                         error "open(O_CREAT|O_EXCL) $file should fail"
17038                 $MULTIOP $file m && error "create $file should fail"
17039                 touch $DIR/$tdir/remote_dir/$tfile ||
17040                         error "touch $tfile failed"
17041                 ln $DIR/$tdir/remote_dir/$tfile $file &&
17042                         error "link $file should fail"
17043                 mdt_index=$($LFS getstripe -m $file)
17044                 if [ $mdt_index == 0 ]; then
17045                         # file failed to migrate is not allowed to rename to
17046                         mv $DIR/$tdir/remote_dir/$tfile $file &&
17047                                 error "rename to $file should fail"
17048                 else
17049                         mv $DIR/$tdir/remote_dir/$tfile $file ||
17050                                 error "rename to $file failed"
17051                 fi
17052                 echo hello >> $file || error "write $file failed"
17053         done
17054
17055         # resume migration with different options should fail
17056         $LFS migrate -m 0 $migrate_dir &&
17057                 error "migrate -m 0 $migrate_dir should fail"
17058
17059         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
17060                 error "migrate -c 2 $migrate_dir should fail"
17061
17062         # resume migration should succeed
17063         $LFS migrate -m $MDTIDX $migrate_dir ||
17064                 error "migrate $migrate_dir failed"
17065
17066         echo "Finish migration, then checking.."
17067         for file in $(find $migrate_dir); do
17068                 mdt_index=$($LFS getstripe -m $file)
17069                 [ $mdt_index == $MDTIDX ] ||
17070                         error "$file is not on MDT${MDTIDX}"
17071         done
17072
17073         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17074 }
17075 run_test 230c "check directory accessiblity if migration failed"
17076
17077 test_230d() {
17078         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17079         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17080         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17081                 skip "Need MDS version at least 2.11.52"
17082         # LU-11235
17083         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
17084
17085         local migrate_dir=$DIR/$tdir/migrate_dir
17086         local old_index
17087         local new_index
17088         local old_count
17089         local new_count
17090         local new_hash
17091         local mdt_index
17092         local i
17093         local j
17094
17095         old_index=$((RANDOM % MDSCOUNT))
17096         old_count=$((MDSCOUNT - old_index))
17097         new_index=$((RANDOM % MDSCOUNT))
17098         new_count=$((MDSCOUNT - new_index))
17099         new_hash=1 # for all_char
17100
17101         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
17102         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
17103
17104         test_mkdir $DIR/$tdir
17105         test_mkdir -i $old_index -c $old_count $migrate_dir
17106
17107         for ((i=0; i<100; i++)); do
17108                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
17109                 createmany -o $migrate_dir/dir_${i}/f 100 ||
17110                         error "create files under remote dir failed $i"
17111         done
17112
17113         echo -n "Migrate from MDT$old_index "
17114         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
17115         echo -n "to MDT$new_index"
17116         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
17117         echo
17118
17119         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
17120         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
17121                 error "migrate remote dir error"
17122
17123         echo "Finish migration, then checking.."
17124         for file in $(find $migrate_dir); do
17125                 mdt_index=$($LFS getstripe -m $file)
17126                 if [ $mdt_index -lt $new_index ] ||
17127                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
17128                         error "$file is on MDT$mdt_index"
17129                 fi
17130         done
17131
17132         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17133 }
17134 run_test 230d "check migrate big directory"
17135
17136 test_230e() {
17137         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17138         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17139         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17140                 skip "Need MDS version at least 2.11.52"
17141
17142         local i
17143         local j
17144         local a_fid
17145         local b_fid
17146
17147         mkdir -p $DIR/$tdir
17148         mkdir $DIR/$tdir/migrate_dir
17149         mkdir $DIR/$tdir/other_dir
17150         touch $DIR/$tdir/migrate_dir/a
17151         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
17152         ls $DIR/$tdir/other_dir
17153
17154         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
17155                 error "migrate dir fails"
17156
17157         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
17158         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
17159
17160         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17161         [ $mdt_index == 0 ] || error "a is not on MDT0"
17162
17163         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
17164                 error "migrate dir fails"
17165
17166         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
17167         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
17168
17169         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17170         [ $mdt_index == 1 ] || error "a is not on MDT1"
17171
17172         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
17173         [ $mdt_index == 1 ] || error "b is not on MDT1"
17174
17175         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
17176         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
17177
17178         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
17179
17180         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17181 }
17182 run_test 230e "migrate mulitple local link files"
17183
17184 test_230f() {
17185         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17186         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17187         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17188                 skip "Need MDS version at least 2.11.52"
17189
17190         local a_fid
17191         local ln_fid
17192
17193         mkdir -p $DIR/$tdir
17194         mkdir $DIR/$tdir/migrate_dir
17195         $LFS mkdir -i1 $DIR/$tdir/other_dir
17196         touch $DIR/$tdir/migrate_dir/a
17197         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
17198         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
17199         ls $DIR/$tdir/other_dir
17200
17201         # a should be migrated to MDT1, since no other links on MDT0
17202         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
17203                 error "#1 migrate dir fails"
17204         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
17205         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
17206         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17207         [ $mdt_index == 1 ] || error "a is not on MDT1"
17208
17209         # a should stay on MDT1, because it is a mulitple link file
17210         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
17211                 error "#2 migrate dir fails"
17212         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17213         [ $mdt_index == 1 ] || error "a is not on MDT1"
17214
17215         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
17216                 error "#3 migrate dir fails"
17217
17218         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
17219         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
17220         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
17221
17222         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
17223         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
17224
17225         # a should be migrated to MDT0, since no other links on MDT1
17226         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
17227                 error "#4 migrate dir fails"
17228         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17229         [ $mdt_index == 0 ] || error "a is not on MDT0"
17230
17231         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17232 }
17233 run_test 230f "migrate mulitple remote link files"
17234
17235 test_230g() {
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         mkdir -p $DIR/$tdir/migrate_dir
17242
17243         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
17244                 error "migrating dir to non-exist MDT succeeds"
17245         true
17246 }
17247 run_test 230g "migrate dir to non-exist MDT"
17248
17249 test_230h() {
17250         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17251         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17252         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17253                 skip "Need MDS version at least 2.11.52"
17254
17255         local mdt_index
17256
17257         mkdir -p $DIR/$tdir/migrate_dir
17258
17259         $LFS migrate -m1 $DIR &&
17260                 error "migrating mountpoint1 should fail"
17261
17262         $LFS migrate -m1 $DIR/$tdir/.. &&
17263                 error "migrating mountpoint2 should fail"
17264
17265         # same as mv
17266         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
17267                 error "migrating $tdir/migrate_dir/.. should fail"
17268
17269         true
17270 }
17271 run_test 230h "migrate .. and root"
17272
17273 test_230i() {
17274         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17275         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17276         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17277                 skip "Need MDS version at least 2.11.52"
17278
17279         mkdir -p $DIR/$tdir/migrate_dir
17280
17281         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
17282                 error "migration fails with a tailing slash"
17283
17284         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
17285                 error "migration fails with two tailing slashes"
17286 }
17287 run_test 230i "lfs migrate -m tolerates trailing slashes"
17288
17289 test_230j() {
17290         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17291         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17292                 skip "Need MDS version at least 2.11.52"
17293
17294         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
17295         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
17296                 error "create $tfile failed"
17297         cat /etc/passwd > $DIR/$tdir/$tfile
17298
17299         $LFS migrate -m 1 $DIR/$tdir
17300
17301         cmp /etc/passwd $DIR/$tdir/$tfile ||
17302                 error "DoM file mismatch after migration"
17303 }
17304 run_test 230j "DoM file data not changed after dir migration"
17305
17306 test_230k() {
17307         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
17308         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17309                 skip "Need MDS version at least 2.11.56"
17310
17311         local total=20
17312         local files_on_starting_mdt=0
17313
17314         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
17315         $LFS getdirstripe $DIR/$tdir
17316         for i in $(seq $total); do
17317                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
17318                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
17319                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17320         done
17321
17322         echo "$files_on_starting_mdt files on MDT0"
17323
17324         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
17325         $LFS getdirstripe $DIR/$tdir
17326
17327         files_on_starting_mdt=0
17328         for i in $(seq $total); do
17329                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
17330                         error "file $tfile.$i mismatch after migration"
17331                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
17332                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17333         done
17334
17335         echo "$files_on_starting_mdt files on MDT1 after migration"
17336         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
17337
17338         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
17339         $LFS getdirstripe $DIR/$tdir
17340
17341         files_on_starting_mdt=0
17342         for i in $(seq $total); do
17343                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
17344                         error "file $tfile.$i mismatch after 2nd migration"
17345                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
17346                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17347         done
17348
17349         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
17350         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
17351
17352         true
17353 }
17354 run_test 230k "file data not changed after dir migration"
17355
17356 test_230l() {
17357         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17358         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17359                 skip "Need MDS version at least 2.11.56"
17360
17361         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
17362         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
17363                 error "create files under remote dir failed $i"
17364         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
17365 }
17366 run_test 230l "readdir between MDTs won't crash"
17367
17368 test_230m() {
17369         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17370         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17371                 skip "Need MDS version at least 2.11.56"
17372
17373         local MDTIDX=1
17374         local mig_dir=$DIR/$tdir/migrate_dir
17375         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
17376         local shortstr="b"
17377         local val
17378
17379         echo "Creating files and dirs with xattrs"
17380         test_mkdir $DIR/$tdir
17381         test_mkdir -i0 -c1 $mig_dir
17382         mkdir $mig_dir/dir
17383         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
17384                 error "cannot set xattr attr1 on dir"
17385         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
17386                 error "cannot set xattr attr2 on dir"
17387         touch $mig_dir/dir/f0
17388         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
17389                 error "cannot set xattr attr1 on file"
17390         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
17391                 error "cannot set xattr attr2 on file"
17392         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17393         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17394         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
17395         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17396         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
17397         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17398         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
17399         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17400         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
17401
17402         echo "Migrating to MDT1"
17403         $LFS migrate -m $MDTIDX $mig_dir ||
17404                 error "fails on migrating dir to MDT1"
17405
17406         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17407         echo "Checking xattrs"
17408         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17409         [ "$val" = $longstr ] ||
17410                 error "expecting xattr1 $longstr on dir, found $val"
17411         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17412         [ "$val" = $shortstr ] ||
17413                 error "expecting xattr2 $shortstr on dir, found $val"
17414         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17415         [ "$val" = $longstr ] ||
17416                 error "expecting xattr1 $longstr on file, found $val"
17417         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17418         [ "$val" = $shortstr ] ||
17419                 error "expecting xattr2 $shortstr on file, found $val"
17420 }
17421 run_test 230m "xattrs not changed after dir migration"
17422
17423 test_230n() {
17424         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17425         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
17426                 skip "Need MDS version at least 2.13.53"
17427
17428         $LFS mkdir -i 0 $DIR/$tdir || error "mkdir $tdir failed"
17429         cat /etc/hosts > $DIR/$tdir/$tfile
17430         $LFS mirror extend -N1 $DIR/$tdir/$tfile || error "Mirroring failed"
17431         $LFS migrate -m 1 $DIR/$tdir || error "Migration failed"
17432
17433         cmp /etc/hosts $DIR/$tdir/$tfile ||
17434                 error "File data mismatch after migration"
17435 }
17436 run_test 230n "Dir migration with mirrored file"
17437
17438 test_231a()
17439 {
17440         # For simplicity this test assumes that max_pages_per_rpc
17441         # is the same across all OSCs
17442         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
17443         local bulk_size=$((max_pages * PAGE_SIZE))
17444         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
17445                                        head -n 1)
17446
17447         mkdir -p $DIR/$tdir
17448         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
17449                 error "failed to set stripe with -S ${brw_size}M option"
17450
17451         # clear the OSC stats
17452         $LCTL set_param osc.*.stats=0 &>/dev/null
17453         stop_writeback
17454
17455         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
17456         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
17457                 oflag=direct &>/dev/null || error "dd failed"
17458
17459         sync; sleep 1; sync # just to be safe
17460         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
17461         if [ x$nrpcs != "x1" ]; then
17462                 $LCTL get_param osc.*.stats
17463                 error "found $nrpcs ost_write RPCs, not 1 as expected"
17464         fi
17465
17466         start_writeback
17467         # Drop the OSC cache, otherwise we will read from it
17468         cancel_lru_locks osc
17469
17470         # clear the OSC stats
17471         $LCTL set_param osc.*.stats=0 &>/dev/null
17472
17473         # Client reads $bulk_size.
17474         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
17475                 iflag=direct &>/dev/null || error "dd failed"
17476
17477         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
17478         if [ x$nrpcs != "x1" ]; then
17479                 $LCTL get_param osc.*.stats
17480                 error "found $nrpcs ost_read RPCs, not 1 as expected"
17481         fi
17482 }
17483 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
17484
17485 test_231b() {
17486         mkdir -p $DIR/$tdir
17487         local i
17488         for i in {0..1023}; do
17489                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
17490                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
17491                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
17492         done
17493         sync
17494 }
17495 run_test 231b "must not assert on fully utilized OST request buffer"
17496
17497 test_232a() {
17498         mkdir -p $DIR/$tdir
17499         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17500
17501         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17502         do_facet ost1 $LCTL set_param fail_loc=0x31c
17503
17504         # ignore dd failure
17505         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
17506
17507         do_facet ost1 $LCTL set_param fail_loc=0
17508         umount_client $MOUNT || error "umount failed"
17509         mount_client $MOUNT || error "mount failed"
17510         stop ost1 || error "cannot stop ost1"
17511         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17512 }
17513 run_test 232a "failed lock should not block umount"
17514
17515 test_232b() {
17516         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
17517                 skip "Need MDS version at least 2.10.58"
17518
17519         mkdir -p $DIR/$tdir
17520         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17521         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
17522         sync
17523         cancel_lru_locks osc
17524
17525         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17526         do_facet ost1 $LCTL set_param fail_loc=0x31c
17527
17528         # ignore failure
17529         $LFS data_version $DIR/$tdir/$tfile || true
17530
17531         do_facet ost1 $LCTL set_param fail_loc=0
17532         umount_client $MOUNT || error "umount failed"
17533         mount_client $MOUNT || error "mount failed"
17534         stop ost1 || error "cannot stop ost1"
17535         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17536 }
17537 run_test 232b "failed data version lock should not block umount"
17538
17539 test_233a() {
17540         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
17541                 skip "Need MDS version at least 2.3.64"
17542         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17543
17544         local fid=$($LFS path2fid $MOUNT)
17545
17546         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17547                 error "cannot access $MOUNT using its FID '$fid'"
17548 }
17549 run_test 233a "checking that OBF of the FS root succeeds"
17550
17551 test_233b() {
17552         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
17553                 skip "Need MDS version at least 2.5.90"
17554         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17555
17556         local fid=$($LFS path2fid $MOUNT/.lustre)
17557
17558         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17559                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
17560
17561         fid=$($LFS path2fid $MOUNT/.lustre/fid)
17562         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17563                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
17564 }
17565 run_test 233b "checking that OBF of the FS .lustre succeeds"
17566
17567 test_234() {
17568         local p="$TMP/sanityN-$TESTNAME.parameters"
17569         save_lustre_params client "llite.*.xattr_cache" > $p
17570         lctl set_param llite.*.xattr_cache 1 ||
17571                 skip_env "xattr cache is not supported"
17572
17573         mkdir -p $DIR/$tdir || error "mkdir failed"
17574         touch $DIR/$tdir/$tfile || error "touch failed"
17575         # OBD_FAIL_LLITE_XATTR_ENOMEM
17576         $LCTL set_param fail_loc=0x1405
17577         getfattr -n user.attr $DIR/$tdir/$tfile &&
17578                 error "getfattr should have failed with ENOMEM"
17579         $LCTL set_param fail_loc=0x0
17580         rm -rf $DIR/$tdir
17581
17582         restore_lustre_params < $p
17583         rm -f $p
17584 }
17585 run_test 234 "xattr cache should not crash on ENOMEM"
17586
17587 test_235() {
17588         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
17589                 skip "Need MDS version at least 2.4.52"
17590
17591         flock_deadlock $DIR/$tfile
17592         local RC=$?
17593         case $RC in
17594                 0)
17595                 ;;
17596                 124) error "process hangs on a deadlock"
17597                 ;;
17598                 *) error "error executing flock_deadlock $DIR/$tfile"
17599                 ;;
17600         esac
17601 }
17602 run_test 235 "LU-1715: flock deadlock detection does not work properly"
17603
17604 #LU-2935
17605 test_236() {
17606         check_swap_layouts_support
17607
17608         local ref1=/etc/passwd
17609         local ref2=/etc/group
17610         local file1=$DIR/$tdir/f1
17611         local file2=$DIR/$tdir/f2
17612
17613         test_mkdir -c1 $DIR/$tdir
17614         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
17615         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
17616         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
17617         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
17618         local fd=$(free_fd)
17619         local cmd="exec $fd<>$file2"
17620         eval $cmd
17621         rm $file2
17622         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
17623                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
17624         cmd="exec $fd>&-"
17625         eval $cmd
17626         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
17627
17628         #cleanup
17629         rm -rf $DIR/$tdir
17630 }
17631 run_test 236 "Layout swap on open unlinked file"
17632
17633 # LU-4659 linkea consistency
17634 test_238() {
17635         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
17636                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
17637                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
17638                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
17639
17640         touch $DIR/$tfile
17641         ln $DIR/$tfile $DIR/$tfile.lnk
17642         touch $DIR/$tfile.new
17643         mv $DIR/$tfile.new $DIR/$tfile
17644         local fid1=$($LFS path2fid $DIR/$tfile)
17645         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
17646         local path1=$($LFS fid2path $FSNAME "$fid1")
17647         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
17648         local path2=$($LFS fid2path $FSNAME "$fid2")
17649         [ $tfile.lnk == $path2 ] ||
17650                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
17651         rm -f $DIR/$tfile*
17652 }
17653 run_test 238 "Verify linkea consistency"
17654
17655 test_239A() { # was test_239
17656         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
17657                 skip "Need MDS version at least 2.5.60"
17658
17659         local list=$(comma_list $(mdts_nodes))
17660
17661         mkdir -p $DIR/$tdir
17662         createmany -o $DIR/$tdir/f- 5000
17663         unlinkmany $DIR/$tdir/f- 5000
17664         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
17665                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
17666         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
17667                         osp.*MDT*.sync_in_flight" | calc_sum)
17668         [ "$changes" -eq 0 ] || error "$changes not synced"
17669 }
17670 run_test 239A "osp_sync test"
17671
17672 test_239a() { #LU-5297
17673         remote_mds_nodsh && skip "remote MDS with nodsh"
17674
17675         touch $DIR/$tfile
17676         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
17677         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
17678         chgrp $RUNAS_GID $DIR/$tfile
17679         wait_delete_completed
17680 }
17681 run_test 239a "process invalid osp sync record correctly"
17682
17683 test_239b() { #LU-5297
17684         remote_mds_nodsh && skip "remote MDS with nodsh"
17685
17686         touch $DIR/$tfile1
17687         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
17688         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
17689         chgrp $RUNAS_GID $DIR/$tfile1
17690         wait_delete_completed
17691         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17692         touch $DIR/$tfile2
17693         chgrp $RUNAS_GID $DIR/$tfile2
17694         wait_delete_completed
17695 }
17696 run_test 239b "process osp sync record with ENOMEM error correctly"
17697
17698 test_240() {
17699         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17700         remote_mds_nodsh && skip "remote MDS with nodsh"
17701
17702         mkdir -p $DIR/$tdir
17703
17704         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
17705                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
17706         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
17707                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
17708
17709         umount_client $MOUNT || error "umount failed"
17710         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
17711         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
17712         mount_client $MOUNT || error "failed to mount client"
17713
17714         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
17715         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
17716 }
17717 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
17718
17719 test_241_bio() {
17720         local count=$1
17721         local bsize=$2
17722
17723         for LOOP in $(seq $count); do
17724                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
17725                 cancel_lru_locks $OSC || true
17726         done
17727 }
17728
17729 test_241_dio() {
17730         local count=$1
17731         local bsize=$2
17732
17733         for LOOP in $(seq $1); do
17734                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
17735                         2>/dev/null
17736         done
17737 }
17738
17739 test_241a() { # was test_241
17740         local bsize=$PAGE_SIZE
17741
17742         (( bsize < 40960 )) && bsize=40960
17743         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17744         ls -la $DIR/$tfile
17745         cancel_lru_locks $OSC
17746         test_241_bio 1000 $bsize &
17747         PID=$!
17748         test_241_dio 1000 $bsize
17749         wait $PID
17750 }
17751 run_test 241a "bio vs dio"
17752
17753 test_241b() {
17754         local bsize=$PAGE_SIZE
17755
17756         (( bsize < 40960 )) && bsize=40960
17757         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17758         ls -la $DIR/$tfile
17759         test_241_dio 1000 $bsize &
17760         PID=$!
17761         test_241_dio 1000 $bsize
17762         wait $PID
17763 }
17764 run_test 241b "dio vs dio"
17765
17766 test_242() {
17767         remote_mds_nodsh && skip "remote MDS with nodsh"
17768
17769         mkdir -p $DIR/$tdir
17770         touch $DIR/$tdir/$tfile
17771
17772         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
17773         do_facet mds1 lctl set_param fail_loc=0x105
17774         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
17775
17776         do_facet mds1 lctl set_param fail_loc=0
17777         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
17778 }
17779 run_test 242 "mdt_readpage failure should not cause directory unreadable"
17780
17781 test_243()
17782 {
17783         test_mkdir $DIR/$tdir
17784         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
17785 }
17786 run_test 243 "various group lock tests"
17787
17788 test_244a()
17789 {
17790         test_mkdir $DIR/$tdir
17791         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
17792         sendfile_grouplock $DIR/$tdir/$tfile || \
17793                 error "sendfile+grouplock failed"
17794         rm -rf $DIR/$tdir
17795 }
17796 run_test 244a "sendfile with group lock tests"
17797
17798 test_244b()
17799 {
17800         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
17801
17802         local threads=50
17803         local size=$((1024*1024))
17804
17805         test_mkdir $DIR/$tdir
17806         for i in $(seq 1 $threads); do
17807                 local file=$DIR/$tdir/file_$((i / 10))
17808                 $MULTIOP $file OG1234w$size_$((i % 3))w$size_$((i % 4))g1234c &
17809                 local pids[$i]=$!
17810         done
17811         for i in $(seq 1 $threads); do
17812                 wait ${pids[$i]}
17813         done
17814 }
17815 run_test 244b "multi-threaded write with group lock"
17816
17817 test_245() {
17818         local flagname="multi_mod_rpcs"
17819         local connect_data_name="max_mod_rpcs"
17820         local out
17821
17822         # check if multiple modify RPCs flag is set
17823         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
17824                 grep "connect_flags:")
17825         echo "$out"
17826
17827         echo "$out" | grep -qw $flagname
17828         if [ $? -ne 0 ]; then
17829                 echo "connect flag $flagname is not set"
17830                 return
17831         fi
17832
17833         # check if multiple modify RPCs data is set
17834         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
17835         echo "$out"
17836
17837         echo "$out" | grep -qw $connect_data_name ||
17838                 error "import should have connect data $connect_data_name"
17839 }
17840 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
17841
17842 cleanup_247() {
17843         local submount=$1
17844
17845         trap 0
17846         umount_client $submount
17847         rmdir $submount
17848 }
17849
17850 test_247a() {
17851         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17852                 grep -q subtree ||
17853                 skip_env "Fileset feature is not supported"
17854
17855         local submount=${MOUNT}_$tdir
17856
17857         mkdir $MOUNT/$tdir
17858         mkdir -p $submount || error "mkdir $submount failed"
17859         FILESET="$FILESET/$tdir" mount_client $submount ||
17860                 error "mount $submount failed"
17861         trap "cleanup_247 $submount" EXIT
17862         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
17863         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
17864                 error "read $MOUNT/$tdir/$tfile failed"
17865         cleanup_247 $submount
17866 }
17867 run_test 247a "mount subdir as fileset"
17868
17869 test_247b() {
17870         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17871                 skip_env "Fileset feature is not supported"
17872
17873         local submount=${MOUNT}_$tdir
17874
17875         rm -rf $MOUNT/$tdir
17876         mkdir -p $submount || error "mkdir $submount failed"
17877         SKIP_FILESET=1
17878         FILESET="$FILESET/$tdir" mount_client $submount &&
17879                 error "mount $submount should fail"
17880         rmdir $submount
17881 }
17882 run_test 247b "mount subdir that dose not exist"
17883
17884 test_247c() {
17885         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17886                 skip_env "Fileset feature is not supported"
17887
17888         local submount=${MOUNT}_$tdir
17889
17890         mkdir -p $MOUNT/$tdir/dir1
17891         mkdir -p $submount || error "mkdir $submount failed"
17892         trap "cleanup_247 $submount" EXIT
17893         FILESET="$FILESET/$tdir" mount_client $submount ||
17894                 error "mount $submount failed"
17895         local fid=$($LFS path2fid $MOUNT/)
17896         $LFS fid2path $submount $fid && error "fid2path should fail"
17897         cleanup_247 $submount
17898 }
17899 run_test 247c "running fid2path outside root"
17900
17901 test_247d() {
17902         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17903                 skip "Fileset feature is not supported"
17904
17905         local submount=${MOUNT}_$tdir
17906
17907         mkdir -p $MOUNT/$tdir/dir1
17908         mkdir -p $submount || error "mkdir $submount failed"
17909         FILESET="$FILESET/$tdir" mount_client $submount ||
17910                 error "mount $submount failed"
17911         trap "cleanup_247 $submount" EXIT
17912         local fid=$($LFS path2fid $submount/dir1)
17913         $LFS fid2path $submount $fid || error "fid2path should succeed"
17914         cleanup_247 $submount
17915 }
17916 run_test 247d "running fid2path inside root"
17917
17918 # LU-8037
17919 test_247e() {
17920         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17921                 grep -q subtree ||
17922                 skip "Fileset feature is not supported"
17923
17924         local submount=${MOUNT}_$tdir
17925
17926         mkdir $MOUNT/$tdir
17927         mkdir -p $submount || error "mkdir $submount failed"
17928         FILESET="$FILESET/.." mount_client $submount &&
17929                 error "mount $submount should fail"
17930         rmdir $submount
17931 }
17932 run_test 247e "mount .. as fileset"
17933
17934 test_248a() {
17935         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
17936         [ -z "$fast_read_sav" ] && skip "no fast read support"
17937
17938         # create a large file for fast read verification
17939         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
17940
17941         # make sure the file is created correctly
17942         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
17943                 { rm -f $DIR/$tfile; skip "file creation error"; }
17944
17945         echo "Test 1: verify that fast read is 4 times faster on cache read"
17946
17947         # small read with fast read enabled
17948         $LCTL set_param -n llite.*.fast_read=1
17949         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17950                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17951                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17952         # small read with fast read disabled
17953         $LCTL set_param -n llite.*.fast_read=0
17954         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17955                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17956                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17957
17958         # verify that fast read is 4 times faster for cache read
17959         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
17960                 error_not_in_vm "fast read was not 4 times faster: " \
17961                            "$t_fast vs $t_slow"
17962
17963         echo "Test 2: verify the performance between big and small read"
17964         $LCTL set_param -n llite.*.fast_read=1
17965
17966         # 1k non-cache read
17967         cancel_lru_locks osc
17968         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17969                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17970                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17971
17972         # 1M non-cache read
17973         cancel_lru_locks osc
17974         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17975                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17976                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17977
17978         # verify that big IO is not 4 times faster than small IO
17979         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
17980                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
17981
17982         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
17983         rm -f $DIR/$tfile
17984 }
17985 run_test 248a "fast read verification"
17986
17987 test_248b() {
17988         # Default short_io_bytes=16384, try both smaller and larger sizes.
17989         # Lustre O_DIRECT read and write needs to be a multiple of PAGE_SIZE.
17990         # 6017024 = 2^12*13*113 = 47008*128 = 11752*512 = 4096*1469 = 53248*113
17991         echo "bs=53248 count=113 normal buffered write"
17992         dd if=/dev/urandom of=$TMP/$tfile.0 bs=53248 count=113 ||
17993                 error "dd of initial data file failed"
17994         stack_trap "rm -f $DIR/$tfile.[0-3] $TMP/$tfile.[0-3]" EXIT
17995
17996         echo "bs=47008 count=128 oflag=dsync normal write $tfile.0"
17997         dd if=$TMP/$tfile.0 of=$DIR/$tfile.0 bs=47008 count=128 oflag=dsync ||
17998                 error "dd with sync normal writes failed"
17999         cmp $TMP/$tfile.0 $DIR/$tfile.0 || error "compare $DIR/$tfile.0 failed"
18000
18001         echo "bs=11752 count=512 oflag=dsync small write $tfile.1"
18002         dd if=$TMP/$tfile.0 of=$DIR/$tfile.1 bs=11752 count=512 oflag=dsync ||
18003                 error "dd with sync small writes failed"
18004         cmp $TMP/$tfile.0 $DIR/$tfile.1 || error "compare $DIR/$tfile.1 failed"
18005
18006         cancel_lru_locks osc
18007
18008         # calculate the small O_DIRECT size and count for the client PAGE_SIZE
18009         local num=$((13 * 113 / (PAGE_SIZE / 4096)))
18010         echo "bs=$PAGE_SIZE count=$num iflag=direct small read $tfile.1"
18011         dd if=$DIR/$tfile.1 of=$TMP/$tfile.1 bs=$PAGE_SIZE count=$num \
18012                 iflag=direct || error "dd with O_DIRECT small read failed"
18013         # adjust bytes checked to handle larger PAGE_SIZE for ARM/PPC
18014         cmp --bytes=$((PAGE_SIZE * num)) $TMP/$tfile.0 $TMP/$tfile.1 ||
18015                 error "compare $TMP/$tfile.1 failed"
18016
18017         local save=$($LCTL get_param -n osc.*OST000*.short_io_bytes | head -n 1)
18018         stack_trap "$LCTL set_param osc.$FSNAME-*.short_io_bytes=$save" EXIT
18019
18020         # just to see what the maximum tunable value is, and test parsing
18021         echo "test invalid parameter 2MB"
18022         $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=2M &&
18023                 error "too-large short_io_bytes allowed"
18024         echo "test maximum parameter 512KB"
18025         # if we can set a larger short_io_bytes, run test regardless of version
18026         if ! $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=512K; then
18027                 # older clients may not allow setting it this large, that's OK
18028                 [ $CLIENT_VERSION -ge $(version_code 2.13.50) ] ||
18029                         skip "Need at least client version 2.13.50"
18030                 error "medium short_io_bytes failed"
18031         fi
18032         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
18033         size=$($LCTL get_param -n osc.$FSNAME-OST0000*.short_io_bytes)
18034
18035         echo "test large parameter 64KB"
18036         $LCTL set_param osc.$FSNAME-*.short_io_bytes=65536
18037         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
18038
18039         echo "bs=47008 count=128 oflag=dsync large write $tfile.2"
18040         dd if=$TMP/$tfile.0 of=$DIR/$tfile.2 bs=47008 count=128 oflag=dsync ||
18041                 error "dd with sync large writes failed"
18042         cmp $TMP/$tfile.0 $DIR/$tfile.2 || error "compare $DIR/$tfile.2 failed"
18043
18044         # calculate the large O_DIRECT size and count for the client PAGE_SIZE
18045         local size=$(((4096 * 13 + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE))
18046         num=$((113 * 4096 / PAGE_SIZE))
18047         echo "bs=$size count=$num oflag=direct large write $tfile.3"
18048         dd if=$TMP/$tfile.0 of=$DIR/$tfile.3 bs=$size count=$num oflag=direct ||
18049                 error "dd with O_DIRECT large writes failed"
18050         cmp --bytes=$((size * num)) $TMP/$tfile.0 $DIR/$tfile.3 ||
18051                 error "compare $DIR/$tfile.3 failed"
18052
18053         cancel_lru_locks osc
18054
18055         echo "bs=$size count=$num iflag=direct large read $tfile.2"
18056         dd if=$DIR/$tfile.2 of=$TMP/$tfile.2 bs=$size count=$num iflag=direct ||
18057                 error "dd with O_DIRECT large read failed"
18058         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.2 ||
18059                 error "compare $TMP/$tfile.2 failed"
18060
18061         echo "bs=$size count=$num iflag=direct large read $tfile.3"
18062         dd if=$DIR/$tfile.3 of=$TMP/$tfile.3 bs=$size count=$num iflag=direct ||
18063                 error "dd with O_DIRECT large read failed"
18064         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.3 ||
18065                 error "compare $TMP/$tfile.3 failed"
18066 }
18067 run_test 248b "test short_io read and write for both small and large sizes"
18068
18069 test_249() { # LU-7890
18070         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
18071                 skip "Need at least version 2.8.54"
18072
18073         rm -f $DIR/$tfile
18074         $LFS setstripe -c 1 $DIR/$tfile
18075         # Offset 2T == 4k * 512M
18076         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
18077                 error "dd to 2T offset failed"
18078 }
18079 run_test 249 "Write above 2T file size"
18080
18081 test_250() {
18082         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
18083          && skip "no 16TB file size limit on ZFS"
18084
18085         $LFS setstripe -c 1 $DIR/$tfile
18086         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
18087         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
18088         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
18089         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
18090                 conv=notrunc,fsync && error "append succeeded"
18091         return 0
18092 }
18093 run_test 250 "Write above 16T limit"
18094
18095 test_251() {
18096         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
18097
18098         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
18099         #Skip once - writing the first stripe will succeed
18100         $LCTL set_param fail_loc=0xa0001407 fail_val=1
18101         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
18102                 error "short write happened"
18103
18104         $LCTL set_param fail_loc=0xa0001407 fail_val=1
18105         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
18106                 error "short read happened"
18107
18108         rm -f $DIR/$tfile
18109 }
18110 run_test 251 "Handling short read and write correctly"
18111
18112 test_252() {
18113         remote_mds_nodsh && skip "remote MDS with nodsh"
18114         remote_ost_nodsh && skip "remote OST with nodsh"
18115         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
18116                 skip_env "ldiskfs only test"
18117         fi
18118
18119         local tgt
18120         local dev
18121         local out
18122         local uuid
18123         local num
18124         local gen
18125
18126         # check lr_reader on OST0000
18127         tgt=ost1
18128         dev=$(facet_device $tgt)
18129         out=$(do_facet $tgt $LR_READER $dev)
18130         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
18131         echo "$out"
18132         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
18133         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
18134                 error "Invalid uuid returned by $LR_READER on target $tgt"
18135         echo -e "uuid returned by $LR_READER is '$uuid'\n"
18136
18137         # check lr_reader -c on MDT0000
18138         tgt=mds1
18139         dev=$(facet_device $tgt)
18140         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
18141                 skip "$LR_READER does not support additional options"
18142         fi
18143         out=$(do_facet $tgt $LR_READER -c $dev)
18144         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
18145         echo "$out"
18146         num=$(echo "$out" | grep -c "mdtlov")
18147         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
18148                 error "Invalid number of mdtlov clients returned by $LR_READER"
18149         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
18150
18151         # check lr_reader -cr on MDT0000
18152         out=$(do_facet $tgt $LR_READER -cr $dev)
18153         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
18154         echo "$out"
18155         echo "$out" | grep -q "^reply_data:$" ||
18156                 error "$LR_READER should have returned 'reply_data' section"
18157         num=$(echo "$out" | grep -c "client_generation")
18158         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
18159 }
18160 run_test 252 "check lr_reader tool"
18161
18162 test_253() {
18163         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18164         remote_mds_nodsh && skip "remote MDS with nodsh"
18165         remote_mgs_nodsh && skip "remote MGS with nodsh"
18166
18167         local ostidx=0
18168         local rc=0
18169         local ost_name=$(ostname_from_index $ostidx)
18170
18171         # on the mdt's osc
18172         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
18173         do_facet $SINGLEMDS $LCTL get_param -n \
18174                 osp.$mdtosc_proc1.reserved_mb_high ||
18175                 skip  "remote MDS does not support reserved_mb_high"
18176
18177         rm -rf $DIR/$tdir
18178         wait_mds_ost_sync
18179         wait_delete_completed
18180         mkdir $DIR/$tdir
18181
18182         pool_add $TESTNAME || error "Pool creation failed"
18183         pool_add_targets $TESTNAME 0 || error "Pool add targets failed"
18184
18185         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
18186                 error "Setstripe failed"
18187
18188         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M count=10
18189
18190         local wms=$(ost_watermarks_set_enospc $tfile $ostidx |
18191                     grep "watermarks")
18192         stack_trap "ost_watermarks_clear_enospc $tfile $ostidx $wms" EXIT
18193
18194         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
18195                         osp.$mdtosc_proc1.prealloc_status)
18196         echo "prealloc_status $oa_status"
18197
18198         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1M count=1 &&
18199                 error "File creation should fail"
18200
18201         #object allocation was stopped, but we still able to append files
18202         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M seek=6 count=5 \
18203                 oflag=append || error "Append failed"
18204
18205         rm -f $DIR/$tdir/$tfile.0
18206
18207         # For this test, we want to delete the files we created to go out of
18208         # space but leave the watermark, so we remain nearly out of space
18209         ost_watermarks_enospc_delete_files $tfile $ostidx
18210
18211         wait_delete_completed
18212
18213         sleep_maxage
18214
18215         for i in $(seq 10 12); do
18216                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$i bs=1M count=1 \
18217                         2>/dev/null || error "File creation failed after rm"
18218         done
18219
18220         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
18221                         osp.$mdtosc_proc1.prealloc_status)
18222         echo "prealloc_status $oa_status"
18223
18224         if (( oa_status != 0 )); then
18225                 error "Object allocation still disable after rm"
18226         fi
18227 }
18228 run_test 253 "Check object allocation limit"
18229
18230 test_254() {
18231         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18232         remote_mds_nodsh && skip "remote MDS with nodsh"
18233         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
18234                 skip "MDS does not support changelog_size"
18235
18236         local cl_user
18237         local MDT0=$(facet_svc $SINGLEMDS)
18238
18239         changelog_register || error "changelog_register failed"
18240
18241         changelog_clear 0 || error "changelog_clear failed"
18242
18243         local size1=$(do_facet $SINGLEMDS \
18244                       $LCTL get_param -n mdd.$MDT0.changelog_size)
18245         echo "Changelog size $size1"
18246
18247         rm -rf $DIR/$tdir
18248         $LFS mkdir -i 0 $DIR/$tdir
18249         # change something
18250         mkdir -p $DIR/$tdir/pics/2008/zachy
18251         touch $DIR/$tdir/pics/2008/zachy/timestamp
18252         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
18253         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
18254         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
18255         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
18256         rm $DIR/$tdir/pics/desktop.jpg
18257
18258         local size2=$(do_facet $SINGLEMDS \
18259                       $LCTL get_param -n mdd.$MDT0.changelog_size)
18260         echo "Changelog size after work $size2"
18261
18262         (( $size2 > $size1 )) ||
18263                 error "new Changelog size=$size2 less than old size=$size1"
18264 }
18265 run_test 254 "Check changelog size"
18266
18267 ladvise_no_type()
18268 {
18269         local type=$1
18270         local file=$2
18271
18272         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
18273                 awk -F: '{print $2}' | grep $type > /dev/null
18274         if [ $? -ne 0 ]; then
18275                 return 0
18276         fi
18277         return 1
18278 }
18279
18280 ladvise_no_ioctl()
18281 {
18282         local file=$1
18283
18284         lfs ladvise -a willread $file > /dev/null 2>&1
18285         if [ $? -eq 0 ]; then
18286                 return 1
18287         fi
18288
18289         lfs ladvise -a willread $file 2>&1 |
18290                 grep "Inappropriate ioctl for device" > /dev/null
18291         if [ $? -eq 0 ]; then
18292                 return 0
18293         fi
18294         return 1
18295 }
18296
18297 percent() {
18298         bc <<<"scale=2; ($1 - $2) * 100 / $2"
18299 }
18300
18301 # run a random read IO workload
18302 # usage: random_read_iops <filename> <filesize> <iosize>
18303 random_read_iops() {
18304         local file=$1
18305         local fsize=$2
18306         local iosize=${3:-4096}
18307
18308         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
18309                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
18310 }
18311
18312 drop_file_oss_cache() {
18313         local file="$1"
18314         local nodes="$2"
18315
18316         $LFS ladvise -a dontneed $file 2>/dev/null ||
18317                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
18318 }
18319
18320 ladvise_willread_performance()
18321 {
18322         local repeat=10
18323         local average_origin=0
18324         local average_cache=0
18325         local average_ladvise=0
18326
18327         for ((i = 1; i <= $repeat; i++)); do
18328                 echo "Iter $i/$repeat: reading without willread hint"
18329                 cancel_lru_locks osc
18330                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
18331                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
18332                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
18333                 average_origin=$(bc <<<"$average_origin + $speed_origin")
18334
18335                 cancel_lru_locks osc
18336                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
18337                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
18338                 average_cache=$(bc <<<"$average_cache + $speed_cache")
18339
18340                 cancel_lru_locks osc
18341                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
18342                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
18343                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
18344                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
18345                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
18346         done
18347         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
18348         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
18349         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
18350
18351         speedup_cache=$(percent $average_cache $average_origin)
18352         speedup_ladvise=$(percent $average_ladvise $average_origin)
18353
18354         echo "Average uncached read: $average_origin"
18355         echo "Average speedup with OSS cached read: " \
18356                 "$average_cache = +$speedup_cache%"
18357         echo "Average speedup with ladvise willread: " \
18358                 "$average_ladvise = +$speedup_ladvise%"
18359
18360         local lowest_speedup=20
18361         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
18362                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
18363                         "got $average_cache%. Skipping ladvise willread check."
18364                 return 0
18365         fi
18366
18367         # the test won't work on ZFS until it supports 'ladvise dontneed', but
18368         # it is still good to run until then to exercise 'ladvise willread'
18369         ! $LFS ladvise -a dontneed $DIR/$tfile &&
18370                 [ "$ost1_FSTYPE" = "zfs" ] &&
18371                 echo "osd-zfs does not support dontneed or drop_caches" &&
18372                 return 0
18373
18374         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
18375         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
18376                 error_not_in_vm "Speedup with willread is less than " \
18377                         "$lowest_speedup%, got $average_ladvise%"
18378 }
18379
18380 test_255a() {
18381         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
18382                 skip "lustre < 2.8.54 does not support ladvise "
18383         remote_ost_nodsh && skip "remote OST with nodsh"
18384
18385         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
18386
18387         ladvise_no_type willread $DIR/$tfile &&
18388                 skip "willread ladvise is not supported"
18389
18390         ladvise_no_ioctl $DIR/$tfile &&
18391                 skip "ladvise ioctl is not supported"
18392
18393         local size_mb=100
18394         local size=$((size_mb * 1048576))
18395         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
18396                 error "dd to $DIR/$tfile failed"
18397
18398         lfs ladvise -a willread $DIR/$tfile ||
18399                 error "Ladvise failed with no range argument"
18400
18401         lfs ladvise -a willread -s 0 $DIR/$tfile ||
18402                 error "Ladvise failed with no -l or -e argument"
18403
18404         lfs ladvise -a willread -e 1 $DIR/$tfile ||
18405                 error "Ladvise failed with only -e argument"
18406
18407         lfs ladvise -a willread -l 1 $DIR/$tfile ||
18408                 error "Ladvise failed with only -l argument"
18409
18410         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
18411                 error "End offset should not be smaller than start offset"
18412
18413         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
18414                 error "End offset should not be equal to start offset"
18415
18416         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
18417                 error "Ladvise failed with overflowing -s argument"
18418
18419         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
18420                 error "Ladvise failed with overflowing -e argument"
18421
18422         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
18423                 error "Ladvise failed with overflowing -l argument"
18424
18425         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
18426                 error "Ladvise succeeded with conflicting -l and -e arguments"
18427
18428         echo "Synchronous ladvise should wait"
18429         local delay=4
18430 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
18431         do_nodes $(comma_list $(osts_nodes)) \
18432                 $LCTL set_param fail_val=$delay fail_loc=0x237
18433
18434         local start_ts=$SECONDS
18435         lfs ladvise -a willread $DIR/$tfile ||
18436                 error "Ladvise failed with no range argument"
18437         local end_ts=$SECONDS
18438         local inteval_ts=$((end_ts - start_ts))
18439
18440         if [ $inteval_ts -lt $(($delay - 1)) ]; then
18441                 error "Synchronous advice didn't wait reply"
18442         fi
18443
18444         echo "Asynchronous ladvise shouldn't wait"
18445         local start_ts=$SECONDS
18446         lfs ladvise -a willread -b $DIR/$tfile ||
18447                 error "Ladvise failed with no range argument"
18448         local end_ts=$SECONDS
18449         local inteval_ts=$((end_ts - start_ts))
18450
18451         if [ $inteval_ts -gt $(($delay / 2)) ]; then
18452                 error "Asynchronous advice blocked"
18453         fi
18454
18455         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
18456         ladvise_willread_performance
18457 }
18458 run_test 255a "check 'lfs ladvise -a willread'"
18459
18460 facet_meminfo() {
18461         local facet=$1
18462         local info=$2
18463
18464         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
18465 }
18466
18467 test_255b() {
18468         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
18469                 skip "lustre < 2.8.54 does not support ladvise "
18470         remote_ost_nodsh && skip "remote OST with nodsh"
18471
18472         lfs setstripe -c 1 -i 0 $DIR/$tfile
18473
18474         ladvise_no_type dontneed $DIR/$tfile &&
18475                 skip "dontneed ladvise is not supported"
18476
18477         ladvise_no_ioctl $DIR/$tfile &&
18478                 skip "ladvise ioctl is not supported"
18479
18480         ! $LFS ladvise -a dontneed $DIR/$tfile &&
18481                 [ "$ost1_FSTYPE" = "zfs" ] &&
18482                 skip "zfs-osd does not support 'ladvise dontneed'"
18483
18484         local size_mb=100
18485         local size=$((size_mb * 1048576))
18486         # In order to prevent disturbance of other processes, only check 3/4
18487         # of the memory usage
18488         local kibibytes=$((size_mb * 1024 * 3 / 4))
18489
18490         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
18491                 error "dd to $DIR/$tfile failed"
18492
18493         #force write to complete before dropping OST cache & checking memory
18494         sync
18495
18496         local total=$(facet_meminfo ost1 MemTotal)
18497         echo "Total memory: $total KiB"
18498
18499         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
18500         local before_read=$(facet_meminfo ost1 Cached)
18501         echo "Cache used before read: $before_read KiB"
18502
18503         lfs ladvise -a willread $DIR/$tfile ||
18504                 error "Ladvise willread failed"
18505         local after_read=$(facet_meminfo ost1 Cached)
18506         echo "Cache used after read: $after_read KiB"
18507
18508         lfs ladvise -a dontneed $DIR/$tfile ||
18509                 error "Ladvise dontneed again failed"
18510         local no_read=$(facet_meminfo ost1 Cached)
18511         echo "Cache used after dontneed ladvise: $no_read KiB"
18512
18513         if [ $total -lt $((before_read + kibibytes)) ]; then
18514                 echo "Memory is too small, abort checking"
18515                 return 0
18516         fi
18517
18518         if [ $((before_read + kibibytes)) -gt $after_read ]; then
18519                 error "Ladvise willread should use more memory" \
18520                         "than $kibibytes KiB"
18521         fi
18522
18523         if [ $((no_read + kibibytes)) -gt $after_read ]; then
18524                 error "Ladvise dontneed should release more memory" \
18525                         "than $kibibytes KiB"
18526         fi
18527 }
18528 run_test 255b "check 'lfs ladvise -a dontneed'"
18529
18530 test_255c() {
18531         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
18532                 skip "lustre < 2.10.50 does not support lockahead"
18533
18534         local count
18535         local new_count
18536         local difference
18537         local i
18538         local rc
18539
18540         test_mkdir -p $DIR/$tdir
18541         $LFS setstripe -i 0 -c 1 $DIR/$tdir
18542
18543         #test 10 returns only success/failure
18544         i=10
18545         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18546         rc=$?
18547         if [ $rc -eq 255 ]; then
18548                 error "Ladvise test${i} failed, ${rc}"
18549         fi
18550
18551         #test 11 counts lock enqueue requests, all others count new locks
18552         i=11
18553         count=$(do_facet ost1 \
18554                 $LCTL get_param -n ost.OSS.ost.stats)
18555         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
18556
18557         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18558         rc=$?
18559         if [ $rc -eq 255 ]; then
18560                 error "Ladvise test${i} failed, ${rc}"
18561         fi
18562
18563         new_count=$(do_facet ost1 \
18564                 $LCTL get_param -n ost.OSS.ost.stats)
18565         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
18566                    awk '{ print $2 }')
18567
18568         difference="$((new_count - count))"
18569         if [ $difference -ne $rc ]; then
18570                 error "Ladvise test${i}, bad enqueue count, returned " \
18571                       "${rc}, actual ${difference}"
18572         fi
18573
18574         for i in $(seq 12 21); do
18575                 # If we do not do this, we run the risk of having too many
18576                 # locks and starting lock cancellation while we are checking
18577                 # lock counts.
18578                 cancel_lru_locks osc
18579
18580                 count=$($LCTL get_param -n \
18581                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
18582
18583                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
18584                 rc=$?
18585                 if [ $rc -eq 255 ]; then
18586                         error "Ladvise test ${i} failed, ${rc}"
18587                 fi
18588
18589                 new_count=$($LCTL get_param -n \
18590                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
18591                 difference="$((new_count - count))"
18592
18593                 # Test 15 output is divided by 100 to map down to valid return
18594                 if [ $i -eq 15 ]; then
18595                         rc="$((rc * 100))"
18596                 fi
18597
18598                 if [ $difference -ne $rc ]; then
18599                         error "Ladvise test ${i}, bad lock count, returned " \
18600                               "${rc}, actual ${difference}"
18601                 fi
18602         done
18603
18604         #test 22 returns only success/failure
18605         i=22
18606         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18607         rc=$?
18608         if [ $rc -eq 255 ]; then
18609                 error "Ladvise test${i} failed, ${rc}"
18610         fi
18611 }
18612 run_test 255c "suite of ladvise lockahead tests"
18613
18614 test_256() {
18615         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18616         remote_mds_nodsh && skip "remote MDS with nodsh"
18617         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18618         changelog_users $SINGLEMDS | grep "^cl" &&
18619                 skip "active changelog user"
18620
18621         local cl_user
18622         local cat_sl
18623         local mdt_dev
18624
18625         mdt_dev=$(mdsdevname 1)
18626         echo $mdt_dev
18627
18628         changelog_register || error "changelog_register failed"
18629
18630         rm -rf $DIR/$tdir
18631         mkdir -p $DIR/$tdir
18632
18633         changelog_clear 0 || error "changelog_clear failed"
18634
18635         # change something
18636         touch $DIR/$tdir/{1..10}
18637
18638         # stop the MDT
18639         stop $SINGLEMDS || error "Fail to stop MDT"
18640
18641         # remount the MDT
18642
18643         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
18644
18645         #after mount new plainllog is used
18646         touch $DIR/$tdir/{11..19}
18647         local tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
18648         stack_trap "rm -f $tmpfile"
18649         cat_sl=$(do_facet $SINGLEMDS "sync; \
18650                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18651                  llog_reader $tmpfile | grep -c type=1064553b")
18652         do_facet $SINGLEMDS llog_reader $tmpfile
18653
18654         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
18655
18656         changelog_clear 0 || error "changelog_clear failed"
18657
18658         cat_sl=$(do_facet $SINGLEMDS "sync; \
18659                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18660                  llog_reader $tmpfile | grep -c type=1064553b")
18661
18662         if (( cat_sl == 2 )); then
18663                 error "Empty plain llog was not deleted from changelog catalog"
18664         elif (( cat_sl != 1 )); then
18665                 error "Active plain llog shouldn't be deleted from catalog"
18666         fi
18667 }
18668 run_test 256 "Check llog delete for empty and not full state"
18669
18670 test_257() {
18671         remote_mds_nodsh && skip "remote MDS with nodsh"
18672         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
18673                 skip "Need MDS version at least 2.8.55"
18674
18675         test_mkdir $DIR/$tdir
18676
18677         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
18678                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
18679         stat $DIR/$tdir
18680
18681 #define OBD_FAIL_MDS_XATTR_REP                  0x161
18682         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18683         local facet=mds$((mdtidx + 1))
18684         set_nodes_failloc $(facet_active_host $facet) 0x80000161
18685         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
18686
18687         stop $facet || error "stop MDS failed"
18688         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
18689                 error "start MDS fail"
18690         wait_recovery_complete $facet
18691 }
18692 run_test 257 "xattr locks are not lost"
18693
18694 # Verify we take the i_mutex when security requires it
18695 test_258a() {
18696 #define OBD_FAIL_IMUTEX_SEC 0x141c
18697         $LCTL set_param fail_loc=0x141c
18698         touch $DIR/$tfile
18699         chmod u+s $DIR/$tfile
18700         chmod a+rwx $DIR/$tfile
18701         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18702         RC=$?
18703         if [ $RC -ne 0 ]; then
18704                 error "error, failed to take i_mutex, rc=$?"
18705         fi
18706         rm -f $DIR/$tfile
18707 }
18708 run_test 258a "verify i_mutex security behavior when suid attributes is set"
18709
18710 # Verify we do NOT take the i_mutex in the normal case
18711 test_258b() {
18712 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
18713         $LCTL set_param fail_loc=0x141d
18714         touch $DIR/$tfile
18715         chmod a+rwx $DIR
18716         chmod a+rw $DIR/$tfile
18717         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18718         RC=$?
18719         if [ $RC -ne 0 ]; then
18720                 error "error, took i_mutex unnecessarily, rc=$?"
18721         fi
18722         rm -f $DIR/$tfile
18723
18724 }
18725 run_test 258b "verify i_mutex security behavior"
18726
18727 test_259() {
18728         local file=$DIR/$tfile
18729         local before
18730         local after
18731
18732         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18733
18734         stack_trap "rm -f $file" EXIT
18735
18736         wait_delete_completed
18737         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18738         echo "before: $before"
18739
18740         $LFS setstripe -i 0 -c 1 $file
18741         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
18742         sync_all_data
18743         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18744         echo "after write: $after"
18745
18746 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
18747         do_facet ost1 $LCTL set_param fail_loc=0x2301
18748         $TRUNCATE $file 0
18749         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18750         echo "after truncate: $after"
18751
18752         stop ost1
18753         do_facet ost1 $LCTL set_param fail_loc=0
18754         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
18755         sleep 2
18756         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18757         echo "after restart: $after"
18758         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
18759                 error "missing truncate?"
18760
18761         return 0
18762 }
18763 run_test 259 "crash at delayed truncate"
18764
18765 test_260() {
18766 #define OBD_FAIL_MDC_CLOSE               0x806
18767         $LCTL set_param fail_loc=0x80000806
18768         touch $DIR/$tfile
18769
18770 }
18771 run_test 260 "Check mdc_close fail"
18772
18773 ### Data-on-MDT sanity tests ###
18774 test_270a() {
18775         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18776                 skip "Need MDS version at least 2.10.55 for DoM"
18777
18778         # create DoM file
18779         local dom=$DIR/$tdir/dom_file
18780         local tmp=$DIR/$tdir/tmp_file
18781
18782         mkdir -p $DIR/$tdir
18783
18784         # basic checks for DoM component creation
18785         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
18786                 error "Can set MDT layout to non-first entry"
18787
18788         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
18789                 error "Can define multiple entries as MDT layout"
18790
18791         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
18792
18793         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
18794         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
18795         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
18796
18797         local mdtidx=$($LFS getstripe -m $dom)
18798         local mdtname=MDT$(printf %04x $mdtidx)
18799         local facet=mds$((mdtidx + 1))
18800         local space_check=1
18801
18802         # Skip free space checks with ZFS
18803         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
18804
18805         # write
18806         sync
18807         local size_tmp=$((65536 * 3))
18808         local mdtfree1=$(do_facet $facet \
18809                          lctl get_param -n osd*.*$mdtname.kbytesfree)
18810
18811         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18812         # check also direct IO along write
18813         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
18814         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18815         sync
18816         cmp $tmp $dom || error "file data is different"
18817         [ $(stat -c%s $dom) == $size_tmp ] ||
18818                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18819         if [ $space_check == 1 ]; then
18820                 local mdtfree2=$(do_facet $facet \
18821                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
18822
18823                 # increase in usage from by $size_tmp
18824                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18825                         error "MDT free space wrong after write: " \
18826                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18827         fi
18828
18829         # truncate
18830         local size_dom=10000
18831
18832         $TRUNCATE $dom $size_dom
18833         [ $(stat -c%s $dom) == $size_dom ] ||
18834                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
18835         if [ $space_check == 1 ]; then
18836                 mdtfree1=$(do_facet $facet \
18837                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18838                 # decrease in usage from $size_tmp to new $size_dom
18839                 [ $(($mdtfree1 - $mdtfree2)) -ge \
18840                   $(((size_tmp - size_dom) / 1024)) ] ||
18841                         error "MDT free space is wrong after truncate: " \
18842                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
18843         fi
18844
18845         # append
18846         cat $tmp >> $dom
18847         sync
18848         size_dom=$((size_dom + size_tmp))
18849         [ $(stat -c%s $dom) == $size_dom ] ||
18850                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
18851         if [ $space_check == 1 ]; then
18852                 mdtfree2=$(do_facet $facet \
18853                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18854                 # increase in usage by $size_tmp from previous
18855                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18856                         error "MDT free space is wrong after append: " \
18857                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18858         fi
18859
18860         # delete
18861         rm $dom
18862         if [ $space_check == 1 ]; then
18863                 mdtfree1=$(do_facet $facet \
18864                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18865                 # decrease in usage by $size_dom from previous
18866                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
18867                         error "MDT free space is wrong after removal: " \
18868                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
18869         fi
18870
18871         # combined striping
18872         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
18873                 error "Can't create DoM + OST striping"
18874
18875         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
18876         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18877         # check also direct IO along write
18878         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18879         sync
18880         cmp $tmp $dom || error "file data is different"
18881         [ $(stat -c%s $dom) == $size_tmp ] ||
18882                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18883         rm $dom $tmp
18884
18885         return 0
18886 }
18887 run_test 270a "DoM: basic functionality tests"
18888
18889 test_270b() {
18890         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18891                 skip "Need MDS version at least 2.10.55"
18892
18893         local dom=$DIR/$tdir/dom_file
18894         local max_size=1048576
18895
18896         mkdir -p $DIR/$tdir
18897         $LFS setstripe -E $max_size -L mdt $dom
18898
18899         # truncate over the limit
18900         $TRUNCATE $dom $(($max_size + 1)) &&
18901                 error "successful truncate over the maximum size"
18902         # write over the limit
18903         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
18904                 error "successful write over the maximum size"
18905         # append over the limit
18906         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
18907         echo "12345" >> $dom && error "successful append over the maximum size"
18908         rm $dom
18909
18910         return 0
18911 }
18912 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
18913
18914 test_270c() {
18915         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18916                 skip "Need MDS version at least 2.10.55"
18917
18918         mkdir -p $DIR/$tdir
18919         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18920
18921         # check files inherit DoM EA
18922         touch $DIR/$tdir/first
18923         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
18924                 error "bad pattern"
18925         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
18926                 error "bad stripe count"
18927         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
18928                 error "bad stripe size"
18929
18930         # check directory inherits DoM EA and uses it as default
18931         mkdir $DIR/$tdir/subdir
18932         touch $DIR/$tdir/subdir/second
18933         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
18934                 error "bad pattern in sub-directory"
18935         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
18936                 error "bad stripe count in sub-directory"
18937         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
18938                 error "bad stripe size in sub-directory"
18939         return 0
18940 }
18941 run_test 270c "DoM: DoM EA inheritance tests"
18942
18943 test_270d() {
18944         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18945                 skip "Need MDS version at least 2.10.55"
18946
18947         mkdir -p $DIR/$tdir
18948         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18949
18950         # inherit default DoM striping
18951         mkdir $DIR/$tdir/subdir
18952         touch $DIR/$tdir/subdir/f1
18953
18954         # change default directory striping
18955         $LFS setstripe -c 1 $DIR/$tdir/subdir
18956         touch $DIR/$tdir/subdir/f2
18957         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
18958                 error "wrong default striping in file 2"
18959         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
18960                 error "bad pattern in file 2"
18961         return 0
18962 }
18963 run_test 270d "DoM: change striping from DoM to RAID0"
18964
18965 test_270e() {
18966         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18967                 skip "Need MDS version at least 2.10.55"
18968
18969         mkdir -p $DIR/$tdir/dom
18970         mkdir -p $DIR/$tdir/norm
18971         DOMFILES=20
18972         NORMFILES=10
18973         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
18974         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
18975
18976         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
18977         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
18978
18979         # find DoM files by layout
18980         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
18981         [ $NUM -eq  $DOMFILES ] ||
18982                 error "lfs find -L: found $NUM, expected $DOMFILES"
18983         echo "Test 1: lfs find 20 DOM files by layout: OK"
18984
18985         # there should be 1 dir with default DOM striping
18986         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
18987         [ $NUM -eq  1 ] ||
18988                 error "lfs find -L: found $NUM, expected 1 dir"
18989         echo "Test 2: lfs find 1 DOM dir by layout: OK"
18990
18991         # find DoM files by stripe size
18992         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
18993         [ $NUM -eq  $DOMFILES ] ||
18994                 error "lfs find -S: found $NUM, expected $DOMFILES"
18995         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
18996
18997         # find files by stripe offset except DoM files
18998         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
18999         [ $NUM -eq  $NORMFILES ] ||
19000                 error "lfs find -i: found $NUM, expected $NORMFILES"
19001         echo "Test 5: lfs find no DOM files by stripe index: OK"
19002         return 0
19003 }
19004 run_test 270e "DoM: lfs find with DoM files test"
19005
19006 test_270f() {
19007         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19008                 skip "Need MDS version at least 2.10.55"
19009
19010         local mdtname=${FSNAME}-MDT0000-mdtlov
19011         local dom=$DIR/$tdir/dom_file
19012         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
19013                                                 lod.$mdtname.dom_stripesize)
19014         local dom_limit=131072
19015
19016         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
19017         local dom_current=$(do_facet mds1 $LCTL get_param -n \
19018                                                 lod.$mdtname.dom_stripesize)
19019         [ ${dom_limit} -eq ${dom_current} ] ||
19020                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
19021
19022         $LFS mkdir -i 0 -c 1 $DIR/$tdir
19023         $LFS setstripe -d $DIR/$tdir
19024         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
19025                 error "Can't set directory default striping"
19026
19027         # exceed maximum stripe size
19028         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
19029                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
19030         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
19031                 error "Able to create DoM component size more than LOD limit"
19032
19033         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
19034         dom_current=$(do_facet mds1 $LCTL get_param -n \
19035                                                 lod.$mdtname.dom_stripesize)
19036         [ 0 -eq ${dom_current} ] ||
19037                 error "Can't set zero DoM stripe limit"
19038         rm $dom
19039
19040         # attempt to create DoM file on server with disabled DoM should
19041         # remove DoM entry from layout and be succeed
19042         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
19043                 error "Can't create DoM file (DoM is disabled)"
19044         [ $($LFS getstripe -L $dom) == "mdt" ] &&
19045                 error "File has DoM component while DoM is disabled"
19046         rm $dom
19047
19048         # attempt to create DoM file with only DoM stripe should return error
19049         $LFS setstripe -E $dom_limit -L mdt $dom &&
19050                 error "Able to create DoM-only file while DoM is disabled"
19051
19052         # too low values to be aligned with smallest stripe size 64K
19053         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
19054         dom_current=$(do_facet mds1 $LCTL get_param -n \
19055                                                 lod.$mdtname.dom_stripesize)
19056         [ 30000 -eq ${dom_current} ] &&
19057                 error "Can set too small DoM stripe limit"
19058
19059         # 64K is a minimal stripe size in Lustre, expect limit of that size
19060         [ 65536 -eq ${dom_current} ] ||
19061                 error "Limit is not set to 64K but ${dom_current}"
19062
19063         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
19064         dom_current=$(do_facet mds1 $LCTL get_param -n \
19065                                                 lod.$mdtname.dom_stripesize)
19066         echo $dom_current
19067         [ 2147483648 -eq ${dom_current} ] &&
19068                 error "Can set too large DoM stripe limit"
19069
19070         do_facet mds1 $LCTL set_param -n \
19071                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
19072         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
19073                 error "Can't create DoM component size after limit change"
19074         do_facet mds1 $LCTL set_param -n \
19075                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
19076         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
19077                 error "Can't create DoM file after limit decrease"
19078         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
19079                 error "Can create big DoM component after limit decrease"
19080         touch ${dom}_def ||
19081                 error "Can't create file with old default layout"
19082
19083         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
19084         return 0
19085 }
19086 run_test 270f "DoM: maximum DoM stripe size checks"
19087
19088 test_271a() {
19089         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19090                 skip "Need MDS version at least 2.10.55"
19091
19092         local dom=$DIR/$tdir/dom
19093
19094         mkdir -p $DIR/$tdir
19095
19096         $LFS setstripe -E 1024K -L mdt $dom
19097
19098         lctl set_param -n mdc.*.stats=clear
19099         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
19100         cat $dom > /dev/null
19101         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
19102         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
19103         ls $dom
19104         rm -f $dom
19105 }
19106 run_test 271a "DoM: data is cached for read after write"
19107
19108 test_271b() {
19109         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19110                 skip "Need MDS version at least 2.10.55"
19111
19112         local dom=$DIR/$tdir/dom
19113
19114         mkdir -p $DIR/$tdir
19115
19116         $LFS setstripe -E 1024K -L mdt -E EOF $dom
19117
19118         lctl set_param -n mdc.*.stats=clear
19119         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
19120         cancel_lru_locks mdc
19121         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
19122         # second stat to check size is cached on client
19123         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
19124         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
19125         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
19126         rm -f $dom
19127 }
19128 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
19129
19130 test_271ba() {
19131         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19132                 skip "Need MDS version at least 2.10.55"
19133
19134         local dom=$DIR/$tdir/dom
19135
19136         mkdir -p $DIR/$tdir
19137
19138         $LFS setstripe -E 1024K -L mdt -E EOF $dom
19139
19140         lctl set_param -n mdc.*.stats=clear
19141         lctl set_param -n osc.*.stats=clear
19142         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
19143         cancel_lru_locks mdc
19144         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
19145         # second stat to check size is cached on client
19146         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
19147         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
19148         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
19149         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
19150         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
19151         rm -f $dom
19152 }
19153 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
19154
19155
19156 get_mdc_stats() {
19157         local mdtidx=$1
19158         local param=$2
19159         local mdt=MDT$(printf %04x $mdtidx)
19160
19161         if [ -z $param ]; then
19162                 lctl get_param -n mdc.*$mdt*.stats
19163         else
19164                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
19165         fi
19166 }
19167
19168 test_271c() {
19169         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19170                 skip "Need MDS version at least 2.10.55"
19171
19172         local dom=$DIR/$tdir/dom
19173
19174         mkdir -p $DIR/$tdir
19175
19176         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19177
19178         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
19179         local facet=mds$((mdtidx + 1))
19180
19181         cancel_lru_locks mdc
19182         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
19183         createmany -o $dom 1000
19184         lctl set_param -n mdc.*.stats=clear
19185         smalliomany -w $dom 1000 200
19186         get_mdc_stats $mdtidx
19187         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
19188         # Each file has 1 open, 1 IO enqueues, total 2000
19189         # but now we have also +1 getxattr for security.capability, total 3000
19190         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
19191         unlinkmany $dom 1000
19192
19193         cancel_lru_locks mdc
19194         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
19195         createmany -o $dom 1000
19196         lctl set_param -n mdc.*.stats=clear
19197         smalliomany -w $dom 1000 200
19198         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
19199         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
19200         # for OPEN and IO lock.
19201         [ $((enq - enq_2)) -ge 1000 ] ||
19202                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
19203         unlinkmany $dom 1000
19204         return 0
19205 }
19206 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
19207
19208 cleanup_271def_tests() {
19209         trap 0
19210         rm -f $1
19211 }
19212
19213 test_271d() {
19214         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
19215                 skip "Need MDS version at least 2.10.57"
19216
19217         local dom=$DIR/$tdir/dom
19218         local tmp=$TMP/$tfile
19219         trap "cleanup_271def_tests $tmp" EXIT
19220
19221         mkdir -p $DIR/$tdir
19222
19223         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19224
19225         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
19226
19227         cancel_lru_locks mdc
19228         dd if=/dev/urandom of=$tmp bs=1000 count=1
19229         dd if=$tmp of=$dom bs=1000 count=1
19230         cancel_lru_locks mdc
19231
19232         cat /etc/hosts >> $tmp
19233         lctl set_param -n mdc.*.stats=clear
19234
19235         # append data to the same file it should update local page
19236         echo "Append to the same page"
19237         cat /etc/hosts >> $dom
19238         local num=$(get_mdc_stats $mdtidx ost_read)
19239         local ra=$(get_mdc_stats $mdtidx req_active)
19240         local rw=$(get_mdc_stats $mdtidx req_waittime)
19241
19242         [ -z $num ] || error "$num READ RPC occured"
19243         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19244         echo "... DONE"
19245
19246         # compare content
19247         cmp $tmp $dom || error "file miscompare"
19248
19249         cancel_lru_locks mdc
19250         lctl set_param -n mdc.*.stats=clear
19251
19252         echo "Open and read file"
19253         cat $dom > /dev/null
19254         local num=$(get_mdc_stats $mdtidx ost_read)
19255         local ra=$(get_mdc_stats $mdtidx req_active)
19256         local rw=$(get_mdc_stats $mdtidx req_waittime)
19257
19258         [ -z $num ] || error "$num READ RPC occured"
19259         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19260         echo "... DONE"
19261
19262         # compare content
19263         cmp $tmp $dom || error "file miscompare"
19264
19265         return 0
19266 }
19267 run_test 271d "DoM: read on open (1K file in reply buffer)"
19268
19269 test_271f() {
19270         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
19271                 skip "Need MDS version at least 2.10.57"
19272
19273         local dom=$DIR/$tdir/dom
19274         local tmp=$TMP/$tfile
19275         trap "cleanup_271def_tests $tmp" EXIT
19276
19277         mkdir -p $DIR/$tdir
19278
19279         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19280
19281         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
19282
19283         cancel_lru_locks mdc
19284         dd if=/dev/urandom of=$tmp bs=265000 count=1
19285         dd if=$tmp of=$dom bs=265000 count=1
19286         cancel_lru_locks mdc
19287         cat /etc/hosts >> $tmp
19288         lctl set_param -n mdc.*.stats=clear
19289
19290         echo "Append to the same page"
19291         cat /etc/hosts >> $dom
19292         local num=$(get_mdc_stats $mdtidx ost_read)
19293         local ra=$(get_mdc_stats $mdtidx req_active)
19294         local rw=$(get_mdc_stats $mdtidx req_waittime)
19295
19296         [ -z $num ] || error "$num READ RPC occured"
19297         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19298         echo "... DONE"
19299
19300         # compare content
19301         cmp $tmp $dom || error "file miscompare"
19302
19303         cancel_lru_locks mdc
19304         lctl set_param -n mdc.*.stats=clear
19305
19306         echo "Open and read file"
19307         cat $dom > /dev/null
19308         local num=$(get_mdc_stats $mdtidx ost_read)
19309         local ra=$(get_mdc_stats $mdtidx req_active)
19310         local rw=$(get_mdc_stats $mdtidx req_waittime)
19311
19312         [ -z $num ] && num=0
19313         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
19314         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19315         echo "... DONE"
19316
19317         # compare content
19318         cmp $tmp $dom || error "file miscompare"
19319
19320         return 0
19321 }
19322 run_test 271f "DoM: read on open (200K file and read tail)"
19323
19324 test_271g() {
19325         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
19326                 skip "Skipping due to old client or server version"
19327
19328         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
19329         # to get layout
19330         $CHECKSTAT -t file $DIR1/$tfile
19331
19332         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
19333         MULTIOP_PID=$!
19334         sleep 1
19335         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
19336         $LCTL set_param fail_loc=0x80000314
19337         rm $DIR1/$tfile || error "Unlink fails"
19338         RC=$?
19339         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
19340         [ $RC -eq 0 ] || error "Failed write to stale object"
19341 }
19342 run_test 271g "Discard DoM data vs client flush race"
19343
19344 test_272a() {
19345         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19346                 skip "Need MDS version at least 2.11.50"
19347
19348         local dom=$DIR/$tdir/dom
19349         mkdir -p $DIR/$tdir
19350
19351         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
19352         dd if=/dev/urandom of=$dom bs=512K count=1 ||
19353                 error "failed to write data into $dom"
19354         local old_md5=$(md5sum $dom)
19355
19356         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
19357                 error "failed to migrate to the same DoM component"
19358
19359         local new_md5=$(md5sum $dom)
19360
19361         [ "$old_md5" == "$new_md5" ] ||
19362                 error "md5sum differ: $old_md5, $new_md5"
19363
19364         [ $($LFS getstripe -c $dom) -eq 2 ] ||
19365                 error "bad final stripe count: $($LFS getstripe -c $dom) != 2"
19366 }
19367 run_test 272a "DoM migration: new layout with the same DOM component"
19368
19369 test_272b() {
19370         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19371                 skip "Need MDS version at least 2.11.50"
19372
19373         local dom=$DIR/$tdir/dom
19374         mkdir -p $DIR/$tdir
19375         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19376
19377         local mdtidx=$($LFS getstripe -m $dom)
19378         local mdtname=MDT$(printf %04x $mdtidx)
19379         local facet=mds$((mdtidx + 1))
19380
19381         local mdtfree1=$(do_facet $facet \
19382                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19383         dd if=/dev/urandom of=$dom bs=2M count=1 ||
19384                 error "failed to write data into $dom"
19385         local old_md5=$(md5sum $dom)
19386         cancel_lru_locks mdc
19387         local mdtfree1=$(do_facet $facet \
19388                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19389
19390         $LFS migrate -c2 $dom ||
19391                 error "failed to migrate to the new composite layout"
19392         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19393                 error "MDT stripe was not removed"
19394
19395         cancel_lru_locks mdc
19396         local new_md5=$(md5sum $dom)
19397         [ "$old_md5" == "$new_md5" ] ||
19398                 error "$old_md5 != $new_md5"
19399
19400         # Skip free space checks with ZFS
19401         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19402                 local mdtfree2=$(do_facet $facet \
19403                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19404                 [ $mdtfree2 -gt $mdtfree1 ] ||
19405                         error "MDT space is not freed after migration"
19406         fi
19407         return 0
19408 }
19409 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
19410
19411 test_272c() {
19412         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19413                 skip "Need MDS version at least 2.11.50"
19414
19415         local dom=$DIR/$tdir/$tfile
19416         mkdir -p $DIR/$tdir
19417         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19418
19419         local mdtidx=$($LFS getstripe -m $dom)
19420         local mdtname=MDT$(printf %04x $mdtidx)
19421         local facet=mds$((mdtidx + 1))
19422
19423         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
19424                 error "failed to write data into $dom"
19425         local old_md5=$(md5sum $dom)
19426         cancel_lru_locks mdc
19427         local mdtfree1=$(do_facet $facet \
19428                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19429
19430         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
19431                 error "failed to migrate to the new composite layout"
19432         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
19433                 error "MDT stripe was not removed"
19434
19435         cancel_lru_locks mdc
19436         local new_md5=$(md5sum $dom)
19437         [ "$old_md5" == "$new_md5" ] ||
19438                 error "$old_md5 != $new_md5"
19439
19440         # Skip free space checks with ZFS
19441         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19442                 local mdtfree2=$(do_facet $facet \
19443                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19444                 [ $mdtfree2 -gt $mdtfree1 ] ||
19445                         error "MDS space is not freed after migration"
19446         fi
19447         return 0
19448 }
19449 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
19450
19451 test_272d() {
19452         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19453                 skip "Need MDS version at least 2.12.55"
19454
19455         local dom=$DIR/$tdir/$tfile
19456         mkdir -p $DIR/$tdir
19457         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19458
19459         local mdtidx=$($LFS getstripe -m $dom)
19460         local mdtname=MDT$(printf %04x $mdtidx)
19461         local facet=mds$((mdtidx + 1))
19462
19463         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
19464                 error "failed to write data into $dom"
19465         local old_md5=$(md5sum $dom)
19466         cancel_lru_locks mdc
19467         local mdtfree1=$(do_facet $facet \
19468                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19469
19470         $LFS mirror extend -N -E 2M -c1 -E -1 -c2 $dom ||
19471                 error "failed mirroring to the new composite layout"
19472         $LFS mirror resync $dom ||
19473                 error "failed mirror resync"
19474         $LFS mirror split --mirror-id 1 -d $dom ||
19475                 error "failed mirror split"
19476
19477         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19478                 error "MDT stripe was not removed"
19479
19480         cancel_lru_locks mdc
19481         local new_md5=$(md5sum $dom)
19482         [ "$old_md5" == "$new_md5" ] ||
19483                 error "$old_md5 != $new_md5"
19484
19485         # Skip free space checks with ZFS
19486         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19487                 local mdtfree2=$(do_facet $facet \
19488                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19489                 [ $mdtfree2 -gt $mdtfree1 ] ||
19490                         error "MDS space is not freed after DOM mirror deletion"
19491         fi
19492         return 0
19493 }
19494 run_test 272d "DoM mirroring: OST-striped mirror to DOM file"
19495
19496 test_272e() {
19497         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19498                 skip "Need MDS version at least 2.12.55"
19499
19500         local dom=$DIR/$tdir/$tfile
19501         mkdir -p $DIR/$tdir
19502         $LFS setstripe -c 2 $dom
19503
19504         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
19505                 error "failed to write data into $dom"
19506         local old_md5=$(md5sum $dom)
19507         cancel_lru_locks mdc
19508
19509         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 $dom ||
19510                 error "failed mirroring to the DOM layout"
19511         $LFS mirror resync $dom ||
19512                 error "failed mirror resync"
19513         $LFS mirror split --mirror-id 1 -d $dom ||
19514                 error "failed mirror split"
19515
19516         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19517                 error "MDT stripe was not removed"
19518
19519         cancel_lru_locks mdc
19520         local new_md5=$(md5sum $dom)
19521         [ "$old_md5" == "$new_md5" ] ||
19522                 error "$old_md5 != $new_md5"
19523
19524         return 0
19525 }
19526 run_test 272e "DoM mirroring: DOM mirror to the OST-striped file"
19527
19528 test_272f() {
19529         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19530                 skip "Need MDS version at least 2.12.55"
19531
19532         local dom=$DIR/$tdir/$tfile
19533         mkdir -p $DIR/$tdir
19534         $LFS setstripe -c 2 $dom
19535
19536         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
19537                 error "failed to write data into $dom"
19538         local old_md5=$(md5sum $dom)
19539         cancel_lru_locks mdc
19540
19541         $LFS migrate -E 1M -L mdt -E eof -c2 -v $dom ||
19542                 error "failed migrating to the DOM file"
19543
19544         cancel_lru_locks mdc
19545         local new_md5=$(md5sum $dom)
19546         [ "$old_md5" != "$new_md5" ] &&
19547                 error "$old_md5 != $new_md5"
19548
19549         return 0
19550 }
19551 run_test 272f "DoM migration: OST-striped file to DOM file"
19552
19553 test_273a() {
19554         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19555                 skip "Need MDS version at least 2.11.50"
19556
19557         # Layout swap cannot be done if either file has DOM component,
19558         # this will never be supported, migration should be used instead
19559
19560         local dom=$DIR/$tdir/$tfile
19561         mkdir -p $DIR/$tdir
19562
19563         $LFS setstripe -c2 ${dom}_plain
19564         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
19565         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
19566                 error "can swap layout with DoM component"
19567         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
19568                 error "can swap layout with DoM component"
19569
19570         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
19571         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
19572                 error "can swap layout with DoM component"
19573         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
19574                 error "can swap layout with DoM component"
19575         return 0
19576 }
19577 run_test 273a "DoM: layout swapping should fail with DOM"
19578
19579 test_275() {
19580         remote_ost_nodsh && skip "remote OST with nodsh"
19581         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
19582                 skip "Need OST version >= 2.10.57"
19583
19584         local file=$DIR/$tfile
19585         local oss
19586
19587         oss=$(comma_list $(osts_nodes))
19588
19589         dd if=/dev/urandom of=$file bs=1M count=2 ||
19590                 error "failed to create a file"
19591         cancel_lru_locks osc
19592
19593         #lock 1
19594         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
19595                 error "failed to read a file"
19596
19597 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
19598         $LCTL set_param fail_loc=0x8000031f
19599
19600         cancel_lru_locks osc &
19601         sleep 1
19602
19603 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
19604         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
19605         #IO takes another lock, but matches the PENDING one
19606         #and places it to the IO RPC
19607         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
19608                 error "failed to read a file with PENDING lock"
19609 }
19610 run_test 275 "Read on a canceled duplicate lock"
19611
19612 test_276() {
19613         remote_ost_nodsh && skip "remote OST with nodsh"
19614         local pid
19615
19616         do_facet ost1 "(while true; do \
19617                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
19618                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
19619         pid=$!
19620
19621         for LOOP in $(seq 20); do
19622                 stop ost1
19623                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
19624         done
19625         kill -9 $pid
19626         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
19627                 rm $TMP/sanity_276_pid"
19628 }
19629 run_test 276 "Race between mount and obd_statfs"
19630
19631 test_277() {
19632         $LCTL set_param ldlm.namespaces.*.lru_size=0
19633         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
19634         local cached_mb=$($LCTL get_param llite.*.max_cached_mb |
19635                         grep ^used_mb | awk '{print $2}')
19636         [ $cached_mb -eq 1 ] || error "expected mb 1 got $cached_mb"
19637         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 \
19638                 oflag=direct conv=notrunc
19639         cached_mb=$($LCTL get_param llite.*.max_cached_mb |
19640                         grep ^used_mb | awk '{print $2}')
19641         [ $cached_mb -eq 0 ] || error "expected mb 0 got $cached_mb"
19642 }
19643 run_test 277 "Direct IO shall drop page cache"
19644
19645 test_278() {
19646         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
19647         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
19648         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] &&
19649                 skip "needs the same host for mdt1 mdt2" && return
19650
19651         local pid1
19652         local pid2
19653
19654 #define OBD_FAIL_OBD_STOP_MDS_RACE     0x60b
19655         do_facet mds2 $LCTL set_param fail_loc=0x8000060c
19656         stop mds2 &
19657         pid2=$!
19658
19659         stop mds1
19660
19661         echo "Starting MDTs"
19662         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
19663         wait $pid2
19664 #For the error assertion will happen. lu_env_get_key(..., &mdt_thread_key)
19665 #will return NULL
19666         do_facet mds2 $LCTL set_param fail_loc=0
19667
19668         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS
19669         wait_recovery_complete mds2
19670 }
19671 run_test 278 "Race starting MDS between MDTs stop/start"
19672
19673 test_280() {
19674         [ $MGS_VERSION -lt $(version_code 2.13.52) ] &&
19675                 skip "Need MGS version at least 2.13.52"
19676         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19677         combined_mgs_mds || skip "needs combined MGS/MDT"
19678
19679         umount_client $MOUNT
19680 #define OBD_FAIL_MDS_LLOG_UMOUNT_RACE   0x15e
19681         do_facet mgs $LCTL set_param fail_loc=0x8000015e fail_val=0
19682
19683         mount_client $MOUNT &
19684         sleep 1
19685         stop mgs || error "stop mgs failed"
19686         #for a race mgs would crash
19687         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
19688         mount_client $MOUNT || error "mount client failed"
19689 }
19690 run_test 280 "Race between MGS umount and client llog processing"
19691
19692 cleanup_test_300() {
19693         trap 0
19694         umask $SAVE_UMASK
19695 }
19696 test_striped_dir() {
19697         local mdt_index=$1
19698         local stripe_count
19699         local stripe_index
19700
19701         mkdir -p $DIR/$tdir
19702
19703         SAVE_UMASK=$(umask)
19704         trap cleanup_test_300 RETURN EXIT
19705
19706         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
19707                                                 $DIR/$tdir/striped_dir ||
19708                 error "set striped dir error"
19709
19710         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
19711         [ "$mode" = "755" ] || error "expect 755 got $mode"
19712
19713         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
19714                 error "getdirstripe failed"
19715         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
19716         if [ "$stripe_count" != "2" ]; then
19717                 error "1:stripe_count is $stripe_count, expect 2"
19718         fi
19719         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
19720         if [ "$stripe_count" != "2" ]; then
19721                 error "2:stripe_count is $stripe_count, expect 2"
19722         fi
19723
19724         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
19725         if [ "$stripe_index" != "$mdt_index" ]; then
19726                 error "stripe_index is $stripe_index, expect $mdt_index"
19727         fi
19728
19729         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19730                 error "nlink error after create striped dir"
19731
19732         mkdir $DIR/$tdir/striped_dir/a
19733         mkdir $DIR/$tdir/striped_dir/b
19734
19735         stat $DIR/$tdir/striped_dir/a ||
19736                 error "create dir under striped dir failed"
19737         stat $DIR/$tdir/striped_dir/b ||
19738                 error "create dir under striped dir failed"
19739
19740         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
19741                 error "nlink error after mkdir"
19742
19743         rmdir $DIR/$tdir/striped_dir/a
19744         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
19745                 error "nlink error after rmdir"
19746
19747         rmdir $DIR/$tdir/striped_dir/b
19748         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19749                 error "nlink error after rmdir"
19750
19751         chattr +i $DIR/$tdir/striped_dir
19752         createmany -o $DIR/$tdir/striped_dir/f 10 &&
19753                 error "immutable flags not working under striped dir!"
19754         chattr -i $DIR/$tdir/striped_dir
19755
19756         rmdir $DIR/$tdir/striped_dir ||
19757                 error "rmdir striped dir error"
19758
19759         cleanup_test_300
19760
19761         true
19762 }
19763
19764 test_300a() {
19765         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19766                 skip "skipped for lustre < 2.7.0"
19767         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19768         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19769
19770         test_striped_dir 0 || error "failed on striped dir on MDT0"
19771         test_striped_dir 1 || error "failed on striped dir on MDT0"
19772 }
19773 run_test 300a "basic striped dir sanity test"
19774
19775 test_300b() {
19776         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19777                 skip "skipped for lustre < 2.7.0"
19778         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19779         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19780
19781         local i
19782         local mtime1
19783         local mtime2
19784         local mtime3
19785
19786         test_mkdir $DIR/$tdir || error "mkdir fail"
19787         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19788                 error "set striped dir error"
19789         for i in {0..9}; do
19790                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
19791                 sleep 1
19792                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
19793                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
19794                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
19795                 sleep 1
19796                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
19797                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
19798                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
19799         done
19800         true
19801 }
19802 run_test 300b "check ctime/mtime for striped dir"
19803
19804 test_300c() {
19805         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19806                 skip "skipped for lustre < 2.7.0"
19807         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19808         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19809
19810         local file_count
19811
19812         mkdir -p $DIR/$tdir
19813         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
19814                 error "set striped dir error"
19815
19816         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
19817                 error "chown striped dir failed"
19818
19819         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
19820                 error "create 5k files failed"
19821
19822         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
19823
19824         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
19825
19826         rm -rf $DIR/$tdir
19827 }
19828 run_test 300c "chown && check ls under striped directory"
19829
19830 test_300d() {
19831         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19832                 skip "skipped for lustre < 2.7.0"
19833         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19834         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19835
19836         local stripe_count
19837         local file
19838
19839         mkdir -p $DIR/$tdir
19840         $LFS setstripe -c 2 $DIR/$tdir
19841
19842         #local striped directory
19843         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19844                 error "set striped dir error"
19845         #look at the directories for debug purposes
19846         ls -l $DIR/$tdir
19847         $LFS getdirstripe $DIR/$tdir
19848         ls -l $DIR/$tdir/striped_dir
19849         $LFS getdirstripe $DIR/$tdir/striped_dir
19850         createmany -o $DIR/$tdir/striped_dir/f 10 ||
19851                 error "create 10 files failed"
19852
19853         #remote striped directory
19854         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
19855                 error "set striped dir error"
19856         #look at the directories for debug purposes
19857         ls -l $DIR/$tdir
19858         $LFS getdirstripe $DIR/$tdir
19859         ls -l $DIR/$tdir/remote_striped_dir
19860         $LFS getdirstripe $DIR/$tdir/remote_striped_dir
19861         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
19862                 error "create 10 files failed"
19863
19864         for file in $(find $DIR/$tdir); do
19865                 stripe_count=$($LFS getstripe -c $file)
19866                 [ $stripe_count -eq 2 ] ||
19867                         error "wrong stripe $stripe_count for $file"
19868         done
19869
19870         rm -rf $DIR/$tdir
19871 }
19872 run_test 300d "check default stripe under striped directory"
19873
19874 test_300e() {
19875         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19876                 skip "Need MDS version at least 2.7.55"
19877         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19878         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19879
19880         local stripe_count
19881         local file
19882
19883         mkdir -p $DIR/$tdir
19884
19885         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19886                 error "set striped dir error"
19887
19888         touch $DIR/$tdir/striped_dir/a
19889         touch $DIR/$tdir/striped_dir/b
19890         touch $DIR/$tdir/striped_dir/c
19891
19892         mkdir $DIR/$tdir/striped_dir/dir_a
19893         mkdir $DIR/$tdir/striped_dir/dir_b
19894         mkdir $DIR/$tdir/striped_dir/dir_c
19895
19896         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
19897                 error "set striped adir under striped dir error"
19898
19899         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
19900                 error "set striped bdir under striped dir error"
19901
19902         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
19903                 error "set striped cdir under striped dir error"
19904
19905         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
19906                 error "rename dir under striped dir fails"
19907
19908         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
19909                 error "rename dir under different stripes fails"
19910
19911         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
19912                 error "rename file under striped dir should succeed"
19913
19914         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
19915                 error "rename dir under striped dir should succeed"
19916
19917         rm -rf $DIR/$tdir
19918 }
19919 run_test 300e "check rename under striped directory"
19920
19921 test_300f() {
19922         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19923         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19924         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19925                 skip "Need MDS version at least 2.7.55"
19926
19927         local stripe_count
19928         local file
19929
19930         rm -rf $DIR/$tdir
19931         mkdir -p $DIR/$tdir
19932
19933         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19934                 error "set striped dir error"
19935
19936         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
19937                 error "set striped dir error"
19938
19939         touch $DIR/$tdir/striped_dir/a
19940         mkdir $DIR/$tdir/striped_dir/dir_a
19941         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
19942                 error "create striped dir under striped dir fails"
19943
19944         touch $DIR/$tdir/striped_dir1/b
19945         mkdir $DIR/$tdir/striped_dir1/dir_b
19946         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
19947                 error "create striped dir under striped dir fails"
19948
19949         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
19950                 error "rename dir under different striped dir should fail"
19951
19952         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
19953                 error "rename striped dir under diff striped dir should fail"
19954
19955         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
19956                 error "rename file under diff striped dirs fails"
19957
19958         rm -rf $DIR/$tdir
19959 }
19960 run_test 300f "check rename cross striped directory"
19961
19962 test_300_check_default_striped_dir()
19963 {
19964         local dirname=$1
19965         local default_count=$2
19966         local default_index=$3
19967         local stripe_count
19968         local stripe_index
19969         local dir_stripe_index
19970         local dir
19971
19972         echo "checking $dirname $default_count $default_index"
19973         $LFS setdirstripe -D -c $default_count -i $default_index \
19974                                 -t all_char $DIR/$tdir/$dirname ||
19975                 error "set default stripe on striped dir error"
19976         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
19977         [ $stripe_count -eq $default_count ] ||
19978                 error "expect $default_count get $stripe_count for $dirname"
19979
19980         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
19981         [ $stripe_index -eq $default_index ] ||
19982                 error "expect $default_index get $stripe_index for $dirname"
19983
19984         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
19985                                                 error "create dirs failed"
19986
19987         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
19988         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
19989         for dir in $(find $DIR/$tdir/$dirname/*); do
19990                 stripe_count=$($LFS getdirstripe -c $dir)
19991                 [ $stripe_count -eq $default_count ] ||
19992                 [ $stripe_count -eq 0 ] || [ $default_count -eq 1 ] ||
19993                 error "stripe count $default_count != $stripe_count for $dir"
19994
19995                 stripe_index=$($LFS getdirstripe -i $dir)
19996                 [ $default_index -eq -1 ] ||
19997                         [ $stripe_index -eq $default_index ] ||
19998                         error "$stripe_index != $default_index for $dir"
19999
20000                 #check default stripe
20001                 stripe_count=$($LFS getdirstripe -D -c $dir)
20002                 [ $stripe_count -eq $default_count ] ||
20003                 error "default count $default_count != $stripe_count for $dir"
20004
20005                 stripe_index=$($LFS getdirstripe -D -i $dir)
20006                 [ $stripe_index -eq $default_index ] ||
20007                 error "default index $default_index != $stripe_index for $dir"
20008         done
20009         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
20010 }
20011
20012 test_300g() {
20013         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20014         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20015                 skip "Need MDS version at least 2.7.55"
20016
20017         local dir
20018         local stripe_count
20019         local stripe_index
20020
20021         mkdir $DIR/$tdir
20022         mkdir $DIR/$tdir/normal_dir
20023
20024         #Checking when client cache stripe index
20025         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
20026         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
20027                 error "create striped_dir failed"
20028
20029         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
20030                 error "create dir0 fails"
20031         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
20032         [ $stripe_index -eq 0 ] ||
20033                 error "dir0 expect index 0 got $stripe_index"
20034
20035         mkdir $DIR/$tdir/striped_dir/dir1 ||
20036                 error "create dir1 fails"
20037         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
20038         [ $stripe_index -eq 1 ] ||
20039                 error "dir1 expect index 1 got $stripe_index"
20040
20041         #check default stripe count/stripe index
20042         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
20043         test_300_check_default_striped_dir normal_dir 1 0
20044         test_300_check_default_striped_dir normal_dir 2 1
20045         test_300_check_default_striped_dir normal_dir 2 -1
20046
20047         #delete default stripe information
20048         echo "delete default stripeEA"
20049         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
20050                 error "set default stripe on striped dir error"
20051
20052         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
20053         for dir in $(find $DIR/$tdir/normal_dir/*); do
20054                 stripe_count=$($LFS getdirstripe -c $dir)
20055                 [ $stripe_count -eq 0 ] ||
20056                         error "expect 1 get $stripe_count for $dir"
20057                 stripe_index=$($LFS getdirstripe -i $dir)
20058                 [ $stripe_index -eq 0 ] ||
20059                         error "expect 0 get $stripe_index for $dir"
20060         done
20061 }
20062 run_test 300g "check default striped directory for normal directory"
20063
20064 test_300h() {
20065         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20066         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20067                 skip "Need MDS version at least 2.7.55"
20068
20069         local dir
20070         local stripe_count
20071
20072         mkdir $DIR/$tdir
20073         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
20074                 error "set striped dir error"
20075
20076         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
20077         test_300_check_default_striped_dir striped_dir 1 0
20078         test_300_check_default_striped_dir striped_dir 2 1
20079         test_300_check_default_striped_dir striped_dir 2 -1
20080
20081         #delete default stripe information
20082         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
20083                 error "set default stripe on striped dir error"
20084
20085         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
20086         for dir in $(find $DIR/$tdir/striped_dir/*); do
20087                 stripe_count=$($LFS getdirstripe -c $dir)
20088                 [ $stripe_count -eq 0 ] ||
20089                         error "expect 1 get $stripe_count for $dir"
20090         done
20091 }
20092 run_test 300h "check default striped directory for striped directory"
20093
20094 test_300i() {
20095         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20096         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20097         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20098                 skip "Need MDS version at least 2.7.55"
20099
20100         local stripe_count
20101         local file
20102
20103         mkdir $DIR/$tdir
20104
20105         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
20106                 error "set striped dir error"
20107
20108         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
20109                 error "create files under striped dir failed"
20110
20111         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
20112                 error "set striped hashdir error"
20113
20114         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
20115                 error "create dir0 under hash dir failed"
20116         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
20117                 error "create dir1 under hash dir failed"
20118
20119         # unfortunately, we need to umount to clear dir layout cache for now
20120         # once we fully implement dir layout, we can drop this
20121         umount_client $MOUNT || error "umount failed"
20122         mount_client $MOUNT || error "mount failed"
20123
20124         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
20125         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
20126         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
20127
20128         #set the stripe to be unknown hash type
20129         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
20130         $LCTL set_param fail_loc=0x1901
20131         for ((i = 0; i < 10; i++)); do
20132                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
20133                         error "stat f-$i failed"
20134                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
20135         done
20136
20137         touch $DIR/$tdir/striped_dir/f0 &&
20138                 error "create under striped dir with unknown hash should fail"
20139
20140         $LCTL set_param fail_loc=0
20141
20142         umount_client $MOUNT || error "umount failed"
20143         mount_client $MOUNT || error "mount failed"
20144
20145         return 0
20146 }
20147 run_test 300i "client handle unknown hash type striped directory"
20148
20149 test_300j() {
20150         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20151         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20152         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20153                 skip "Need MDS version at least 2.7.55"
20154
20155         local stripe_count
20156         local file
20157
20158         mkdir $DIR/$tdir
20159
20160         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
20161         $LCTL set_param fail_loc=0x1702
20162         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
20163                 error "set striped dir error"
20164
20165         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
20166                 error "create files under striped dir failed"
20167
20168         $LCTL set_param fail_loc=0
20169
20170         rm -rf $DIR/$tdir || error "unlink striped dir fails"
20171
20172         return 0
20173 }
20174 run_test 300j "test large update record"
20175
20176 test_300k() {
20177         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20178         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20179         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20180                 skip "Need MDS version at least 2.7.55"
20181
20182         # this test needs a huge transaction
20183         local kb
20184         kb=$(do_facet $SINGLEMDS "$LCTL get_param -n \
20185              osd*.$FSNAME-MDT0000.kbytestotal")
20186         [ $kb -lt $((1024*1024)) ] && skip "MDT0 too small: $kb"
20187
20188         local stripe_count
20189         local file
20190
20191         mkdir $DIR/$tdir
20192
20193         #define OBD_FAIL_LARGE_STRIPE   0x1703
20194         $LCTL set_param fail_loc=0x1703
20195         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
20196                 error "set striped dir error"
20197         $LCTL set_param fail_loc=0
20198
20199         $LFS getdirstripe $DIR/$tdir/striped_dir ||
20200                 error "getstripeddir fails"
20201         rm -rf $DIR/$tdir/striped_dir ||
20202                 error "unlink striped dir fails"
20203
20204         return 0
20205 }
20206 run_test 300k "test large striped directory"
20207
20208 test_300l() {
20209         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20210         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20211         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20212                 skip "Need MDS version at least 2.7.55"
20213
20214         local stripe_index
20215
20216         test_mkdir -p $DIR/$tdir/striped_dir
20217         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
20218                         error "chown $RUNAS_ID failed"
20219         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
20220                 error "set default striped dir failed"
20221
20222         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
20223         $LCTL set_param fail_loc=0x80000158
20224         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
20225
20226         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
20227         [ $stripe_index -eq 1 ] ||
20228                 error "expect 1 get $stripe_index for $dir"
20229 }
20230 run_test 300l "non-root user to create dir under striped dir with stale layout"
20231
20232 test_300m() {
20233         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20234         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
20235         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20236                 skip "Need MDS version at least 2.7.55"
20237
20238         mkdir -p $DIR/$tdir/striped_dir
20239         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
20240                 error "set default stripes dir error"
20241
20242         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
20243
20244         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
20245         [ $stripe_count -eq 0 ] ||
20246                         error "expect 0 get $stripe_count for a"
20247
20248         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
20249                 error "set default stripes dir error"
20250
20251         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
20252
20253         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
20254         [ $stripe_count -eq 0 ] ||
20255                         error "expect 0 get $stripe_count for b"
20256
20257         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
20258                 error "set default stripes dir error"
20259
20260         mkdir $DIR/$tdir/striped_dir/c &&
20261                 error "default stripe_index is invalid, mkdir c should fails"
20262
20263         rm -rf $DIR/$tdir || error "rmdir fails"
20264 }
20265 run_test 300m "setstriped directory on single MDT FS"
20266
20267 cleanup_300n() {
20268         local list=$(comma_list $(mdts_nodes))
20269
20270         trap 0
20271         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
20272 }
20273
20274 test_300n() {
20275         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20276         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20277         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20278                 skip "Need MDS version at least 2.7.55"
20279         remote_mds_nodsh && skip "remote MDS with nodsh"
20280
20281         local stripe_index
20282         local list=$(comma_list $(mdts_nodes))
20283
20284         trap cleanup_300n RETURN EXIT
20285         mkdir -p $DIR/$tdir
20286         chmod 777 $DIR/$tdir
20287         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
20288                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
20289                 error "create striped dir succeeds with gid=0"
20290
20291         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
20292         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
20293                 error "create striped dir fails with gid=-1"
20294
20295         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
20296         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
20297                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
20298                 error "set default striped dir succeeds with gid=0"
20299
20300
20301         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
20302         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
20303                 error "set default striped dir fails with gid=-1"
20304
20305
20306         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
20307         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
20308                                         error "create test_dir fails"
20309         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
20310                                         error "create test_dir1 fails"
20311         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
20312                                         error "create test_dir2 fails"
20313         cleanup_300n
20314 }
20315 run_test 300n "non-root user to create dir under striped dir with default EA"
20316
20317 test_300o() {
20318         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20319         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20320         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20321                 skip "Need MDS version at least 2.7.55"
20322
20323         local numfree1
20324         local numfree2
20325
20326         mkdir -p $DIR/$tdir
20327
20328         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
20329         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
20330         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
20331                 skip "not enough free inodes $numfree1 $numfree2"
20332         fi
20333
20334         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
20335         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
20336         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
20337                 skip "not enough free space $numfree1 $numfree2"
20338         fi
20339
20340         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
20341                 error "setdirstripe fails"
20342
20343         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
20344                 error "create dirs fails"
20345
20346         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
20347         ls $DIR/$tdir/striped_dir > /dev/null ||
20348                 error "ls striped dir fails"
20349         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
20350                 error "unlink big striped dir fails"
20351 }
20352 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
20353
20354 test_300p() {
20355         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20356         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20357         remote_mds_nodsh && skip "remote MDS with nodsh"
20358
20359         mkdir -p $DIR/$tdir
20360
20361         #define OBD_FAIL_OUT_ENOSPC     0x1704
20362         do_facet mds2 lctl set_param fail_loc=0x80001704
20363         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
20364                  && error "create striped directory should fail"
20365
20366         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
20367
20368         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
20369         true
20370 }
20371 run_test 300p "create striped directory without space"
20372
20373 test_300q() {
20374         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20375         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20376
20377         local fd=$(free_fd)
20378         local cmd="exec $fd<$tdir"
20379         cd $DIR
20380         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
20381         eval $cmd
20382         cmd="exec $fd<&-"
20383         trap "eval $cmd" EXIT
20384         cd $tdir || error "cd $tdir fails"
20385         rmdir  ../$tdir || error "rmdir $tdir fails"
20386         mkdir local_dir && error "create dir succeeds"
20387         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
20388         eval $cmd
20389         return 0
20390 }
20391 run_test 300q "create remote directory under orphan directory"
20392
20393 test_300r() {
20394         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20395                 skip "Need MDS version at least 2.7.55" && return
20396         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
20397
20398         mkdir $DIR/$tdir
20399
20400         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
20401                 error "set striped dir error"
20402
20403         $LFS getdirstripe $DIR/$tdir/striped_dir ||
20404                 error "getstripeddir fails"
20405
20406         local stripe_count
20407         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
20408                       awk '/lmv_stripe_count:/ { print $2 }')
20409
20410         [ $MDSCOUNT -ne $stripe_count ] &&
20411                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
20412
20413         rm -rf $DIR/$tdir/striped_dir ||
20414                 error "unlink striped dir fails"
20415 }
20416 run_test 300r "test -1 striped directory"
20417
20418 prepare_remote_file() {
20419         mkdir $DIR/$tdir/src_dir ||
20420                 error "create remote source failed"
20421
20422         cp /etc/hosts $DIR/$tdir/src_dir/a ||
20423                  error "cp to remote source failed"
20424         touch $DIR/$tdir/src_dir/a
20425
20426         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
20427                 error "create remote target dir failed"
20428
20429         touch $DIR/$tdir/tgt_dir/b
20430
20431         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
20432                 error "rename dir cross MDT failed!"
20433
20434         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
20435                 error "src_child still exists after rename"
20436
20437         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
20438                 error "missing file(a) after rename"
20439
20440         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
20441                 error "diff after rename"
20442 }
20443
20444 test_310a() {
20445         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
20446         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20447
20448         local remote_file=$DIR/$tdir/tgt_dir/b
20449
20450         mkdir -p $DIR/$tdir
20451
20452         prepare_remote_file || error "prepare remote file failed"
20453
20454         #open-unlink file
20455         $OPENUNLINK $remote_file $remote_file ||
20456                 error "openunlink $remote_file failed"
20457         $CHECKSTAT -a $remote_file || error "$remote_file exists"
20458 }
20459 run_test 310a "open unlink remote file"
20460
20461 test_310b() {
20462         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
20463         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20464
20465         local remote_file=$DIR/$tdir/tgt_dir/b
20466
20467         mkdir -p $DIR/$tdir
20468
20469         prepare_remote_file || error "prepare remote file failed"
20470
20471         ln $remote_file $DIR/$tfile || error "link failed for remote file"
20472         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
20473         $CHECKSTAT -t file $remote_file || error "check file failed"
20474 }
20475 run_test 310b "unlink remote file with multiple links while open"
20476
20477 test_310c() {
20478         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20479         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
20480
20481         local remote_file=$DIR/$tdir/tgt_dir/b
20482
20483         mkdir -p $DIR/$tdir
20484
20485         prepare_remote_file || error "prepare remote file failed"
20486
20487         ln $remote_file $DIR/$tfile || error "link failed for remote file"
20488         multiop_bg_pause $remote_file O_uc ||
20489                         error "mulitop failed for remote file"
20490         MULTIPID=$!
20491         $MULTIOP $DIR/$tfile Ouc
20492         kill -USR1 $MULTIPID
20493         wait $MULTIPID
20494 }
20495 run_test 310c "open-unlink remote file with multiple links"
20496
20497 #LU-4825
20498 test_311() {
20499         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20500         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
20501         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
20502                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
20503         remote_mds_nodsh && skip "remote MDS with nodsh"
20504
20505         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
20506         local mdts=$(comma_list $(mdts_nodes))
20507
20508         mkdir -p $DIR/$tdir
20509         $LFS setstripe -i 0 -c 1 $DIR/$tdir
20510         createmany -o $DIR/$tdir/$tfile. 1000
20511
20512         # statfs data is not real time, let's just calculate it
20513         old_iused=$((old_iused + 1000))
20514
20515         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
20516                         osp.*OST0000*MDT0000.create_count")
20517         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
20518                                 osp.*OST0000*MDT0000.max_create_count")
20519         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
20520
20521         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
20522         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
20523         [ $index -ne 0 ] || error "$tfile stripe index is 0"
20524
20525         unlinkmany $DIR/$tdir/$tfile. 1000
20526
20527         do_nodes $mdts "$LCTL set_param -n \
20528                         osp.*OST0000*.max_create_count=$max_count"
20529         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
20530                 do_nodes $mdts "$LCTL set_param -n \
20531                                 osp.*OST0000*.create_count=$count"
20532         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
20533                         grep "=0" && error "create_count is zero"
20534
20535         local new_iused
20536         for i in $(seq 120); do
20537                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
20538                 # system may be too busy to destroy all objs in time, use
20539                 # a somewhat small value to not fail autotest
20540                 [ $((old_iused - new_iused)) -gt 400 ] && break
20541                 sleep 1
20542         done
20543
20544         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
20545         [ $((old_iused - new_iused)) -gt 400 ] ||
20546                 error "objs not destroyed after unlink"
20547 }
20548 run_test 311 "disable OSP precreate, and unlink should destroy objs"
20549
20550 zfs_oid_to_objid()
20551 {
20552         local ost=$1
20553         local objid=$2
20554
20555         local vdevdir=$(dirname $(facet_vdevice $ost))
20556         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
20557         local zfs_zapid=$(do_facet $ost $cmd |
20558                           grep -w "/O/0/d$((objid%32))" -C 5 |
20559                           awk '/Object/{getline; print $1}')
20560         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
20561                           awk "/$objid = /"'{printf $3}')
20562
20563         echo $zfs_objid
20564 }
20565
20566 zfs_object_blksz() {
20567         local ost=$1
20568         local objid=$2
20569
20570         local vdevdir=$(dirname $(facet_vdevice $ost))
20571         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
20572         local blksz=$(do_facet $ost $cmd $objid |
20573                       awk '/dblk/{getline; printf $4}')
20574
20575         case "${blksz: -1}" in
20576                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
20577                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
20578                 *) ;;
20579         esac
20580
20581         echo $blksz
20582 }
20583
20584 test_312() { # LU-4856
20585         remote_ost_nodsh && skip "remote OST with nodsh"
20586         [ "$ost1_FSTYPE" = "zfs" ] ||
20587                 skip_env "the test only applies to zfs"
20588
20589         local max_blksz=$(do_facet ost1 \
20590                           $ZFS get -p recordsize $(facet_device ost1) |
20591                           awk '!/VALUE/{print $3}')
20592
20593         # to make life a little bit easier
20594         $LFS mkdir -c 1 -i 0 $DIR/$tdir
20595         $LFS setstripe -c 1 -i 0 $DIR/$tdir
20596
20597         local tf=$DIR/$tdir/$tfile
20598         touch $tf
20599         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20600
20601         # Get ZFS object id
20602         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20603         # block size change by sequential overwrite
20604         local bs
20605
20606         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
20607                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
20608
20609                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
20610                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
20611         done
20612         rm -f $tf
20613
20614         # block size change by sequential append write
20615         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
20616         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20617         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20618         local count
20619
20620         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
20621                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
20622                         oflag=sync conv=notrunc
20623
20624                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
20625                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
20626                         error "blksz error, actual $blksz, " \
20627                                 "expected: 2 * $count * $PAGE_SIZE"
20628         done
20629         rm -f $tf
20630
20631         # random write
20632         touch $tf
20633         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20634         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20635
20636         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
20637         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20638         [ $blksz -eq $PAGE_SIZE ] ||
20639                 error "blksz error: $blksz, expected: $PAGE_SIZE"
20640
20641         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
20642         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20643         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
20644
20645         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
20646         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20647         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
20648 }
20649 run_test 312 "make sure ZFS adjusts its block size by write pattern"
20650
20651 test_313() {
20652         remote_ost_nodsh && skip "remote OST with nodsh"
20653
20654         local file=$DIR/$tfile
20655
20656         rm -f $file
20657         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
20658
20659         # define OBD_FAIL_TGT_RCVD_EIO           0x720
20660         do_facet ost1 "$LCTL set_param fail_loc=0x720"
20661         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
20662                 error "write should failed"
20663         do_facet ost1 "$LCTL set_param fail_loc=0"
20664         rm -f $file
20665 }
20666 run_test 313 "io should fail after last_rcvd update fail"
20667
20668 test_314() {
20669         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
20670
20671         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
20672         do_facet ost1 "$LCTL set_param fail_loc=0x720"
20673         rm -f $DIR/$tfile
20674         wait_delete_completed
20675         do_facet ost1 "$LCTL set_param fail_loc=0"
20676 }
20677 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
20678
20679 test_315() { # LU-618
20680         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
20681
20682         local file=$DIR/$tfile
20683         rm -f $file
20684
20685         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
20686                 error "multiop file write failed"
20687         $MULTIOP $file oO_RDONLY:r4063232_c &
20688         PID=$!
20689
20690         sleep 2
20691
20692         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
20693         kill -USR1 $PID
20694
20695         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
20696         rm -f $file
20697 }
20698 run_test 315 "read should be accounted"
20699
20700 test_316() {
20701         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20702         large_xattr_enabled || skip_env "ea_inode feature disabled"
20703
20704         rm -rf $DIR/$tdir/d
20705         mkdir -p $DIR/$tdir/d
20706         chown nobody $DIR/$tdir/d
20707         touch $DIR/$tdir/d/file
20708
20709         $LFS mv -m1 $DIR/$tdir/d || error "lfs mv failed"
20710 }
20711 run_test 316 "lfs mv"
20712
20713 test_317() {
20714         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
20715                 skip "Need MDS version at least 2.11.53"
20716         if [ "$ost1_FSTYPE" == "zfs" ]; then
20717                 skip "LU-10370: no implementation for ZFS"
20718         fi
20719
20720         local trunc_sz
20721         local grant_blk_size
20722
20723         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
20724                         awk '/grant_block_size:/ { print $2; exit; }')
20725         #
20726         # Create File of size 5M. Truncate it to below size's and verify
20727         # blocks count.
20728         #
20729         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
20730                 error "Create file $DIR/$tfile failed"
20731         stack_trap "rm -f $DIR/$tfile" EXIT
20732
20733         for trunc_sz in 2097152 4097 4000 509 0; do
20734                 $TRUNCATE $DIR/$tfile $trunc_sz ||
20735                         error "truncate $tfile to $trunc_sz failed"
20736                 local sz=$(stat --format=%s $DIR/$tfile)
20737                 local blk=$(stat --format=%b $DIR/$tfile)
20738                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
20739                                      grant_blk_size) * 8))
20740
20741                 if [[ $blk -ne $trunc_blk ]]; then
20742                         $(which stat) $DIR/$tfile
20743                         error "Expected Block $trunc_blk got $blk for $tfile"
20744                 fi
20745
20746                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20747                         error "Expected Size $trunc_sz got $sz for $tfile"
20748         done
20749
20750         #
20751         # sparse file test
20752         # Create file with a hole and write actual two blocks. Block count
20753         # must be 16.
20754         #
20755         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
20756                 conv=fsync || error "Create file : $DIR/$tfile"
20757
20758         # Calculate the final truncate size.
20759         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
20760
20761         #
20762         # truncate to size $trunc_sz bytes. Strip the last block
20763         # The block count must drop to 8
20764         #
20765         $TRUNCATE $DIR/$tfile $trunc_sz ||
20766                 error "truncate $tfile to $trunc_sz failed"
20767
20768         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
20769         sz=$(stat --format=%s $DIR/$tfile)
20770         blk=$(stat --format=%b $DIR/$tfile)
20771
20772         if [[ $blk -ne $trunc_bsz ]]; then
20773                 $(which stat) $DIR/$tfile
20774                 error "Expected Block $trunc_bsz got $blk for $tfile"
20775         fi
20776
20777         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20778                 error "Expected Size $trunc_sz got $sz for $tfile"
20779 }
20780 run_test 317 "Verify blocks get correctly update after truncate"
20781
20782 test_318() {
20783         local old_max_active=$($LCTL get_param -n \
20784                             llite.*.max_read_ahead_async_active 2>/dev/null)
20785
20786         $LCTL set_param llite.*.max_read_ahead_async_active=256
20787         local max_active=$($LCTL get_param -n \
20788                            llite.*.max_read_ahead_async_active 2>/dev/null)
20789         [ $max_active -ne 256 ] && error "expected 256 but got $max_active"
20790
20791         $LCTL set_param llite.*.max_read_ahead_async_active=0 ||
20792                 error "set max_read_ahead_async_active should succeed"
20793
20794         $LCTL set_param llite.*.max_read_ahead_async_active=512
20795         max_active=$($LCTL get_param -n \
20796                      llite.*.max_read_ahead_async_active 2>/dev/null)
20797         [ $max_active -eq 512 ] || error "expected 512 but got $max_active"
20798
20799         # restore @max_active
20800         [ $old_max_active -ne 0 ] && $LCTL set_param \
20801                 llite.*.max_read_ahead_async_active=$old_max_active
20802
20803         local old_threshold=$($LCTL get_param -n \
20804                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
20805         local max_per_file_mb=$($LCTL get_param -n \
20806                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
20807
20808         local invalid=$(($max_per_file_mb + 1))
20809         $LCTL set_param \
20810                 llite.*.read_ahead_async_file_threshold_mb=$invalid\
20811                         && error "set $invalid should fail"
20812
20813         local valid=$(($invalid - 1))
20814         $LCTL set_param \
20815                 llite.*.read_ahead_async_file_threshold_mb=$valid ||
20816                         error "set $valid should succeed"
20817         local threshold=$($LCTL get_param -n \
20818                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
20819         [ $threshold -eq $valid ] || error \
20820                 "expect threshold $valid got $threshold"
20821         $LCTL set_param \
20822                 llite.*.read_ahead_async_file_threshold_mb=$old_threshold
20823 }
20824 run_test 318 "Verify async readahead tunables"
20825
20826 test_319() {
20827         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
20828
20829         local before=$(date +%s)
20830         local evict
20831         local mdir=$DIR/$tdir
20832         local file=$mdir/xxx
20833
20834         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
20835         touch $file
20836
20837 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
20838         $LCTL set_param fail_val=5 fail_loc=0x8000032c
20839         $LFS mv -m1 $file &
20840
20841         sleep 1
20842         dd if=$file of=/dev/null
20843         wait
20844         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
20845           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
20846
20847         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
20848 }
20849 run_test 319 "lost lease lock on migrate error"
20850
20851 test_398a() { # LU-4198
20852         $LFS setstripe -c 1 -i 0 $DIR/$tfile
20853         $LCTL set_param ldlm.namespaces.*.lru_size=clear
20854
20855         # request a new lock on client
20856         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
20857
20858         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
20859         local lock_count=$($LCTL get_param -n \
20860                            ldlm.namespaces.*-OST0000-osc-ffff*.lru_size)
20861         [[ $lock_count -eq 0 ]] || error "lock should be cancelled by direct IO"
20862
20863         $LCTL set_param ldlm.namespaces.*-OST0000-osc-ffff*.lru_size=clear
20864
20865         # no lock cached, should use lockless IO and not enqueue new lock
20866         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
20867         lock_count=$($LCTL get_param -n \
20868                      ldlm.namespaces.*-OST0000-osc-ffff*.lru_size)
20869         [[ $lock_count -eq 0 ]] || error "no lock should be held by direct IO"
20870 }
20871 run_test 398a "direct IO should cancel lock otherwise lockless"
20872
20873 test_398b() { # LU-4198
20874         which fio || skip_env "no fio installed"
20875         $LFS setstripe -c -1 $DIR/$tfile
20876
20877         local size=12
20878         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size
20879
20880         local njobs=4
20881         echo "mix direct rw ${size}M to OST0 by fio with $njobs jobs..."
20882         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
20883                 --numjobs=$njobs --fallocate=none \
20884                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
20885                 --filename=$DIR/$tfile &
20886         bg_pid=$!
20887
20888         echo "mix buffer rw ${size}M to OST0 by fio with $njobs jobs..."
20889         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE \
20890                 --numjobs=$njobs --fallocate=none \
20891                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
20892                 --filename=$DIR/$tfile || true
20893         wait $bg_pid
20894
20895         rm -rf $DIR/$tfile
20896 }
20897 run_test 398b "DIO and buffer IO race"
20898
20899 test_398c() { # LU-4198
20900         which fio || skip_env "no fio installed"
20901
20902         saved_debug=$($LCTL get_param -n debug)
20903         $LCTL set_param debug=0
20904
20905         local size=$(lctl get_param -n osc.$FSNAME-OST0000*.kbytesavail | head -1)
20906         ((size /= 1024)) # by megabytes
20907         ((size /= 2)) # write half of the OST at most
20908         [ $size -gt 40 ] && size=40 #reduce test time anyway
20909
20910         $LFS setstripe -c 1 $DIR/$tfile
20911
20912         # it seems like ldiskfs reserves more space than necessary if the
20913         # writing blocks are not mapped, so it extends the file firstly
20914         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size && sync
20915         cancel_lru_locks osc
20916
20917         # clear and verify rpc_stats later
20918         $LCTL set_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats=clear
20919
20920         local njobs=4
20921         echo "writing ${size}M to OST0 by fio with $njobs jobs..."
20922         fio --name=rand-write --rw=randwrite --bs=$PAGE_SIZE --direct=1 \
20923                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
20924                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
20925                 --filename=$DIR/$tfile
20926         [ $? -eq 0 ] || error "fio write error"
20927
20928         [ $($LCTL get_param -n \
20929          ldlm.namespaces.${FSNAME}-OST0000-osc-ffff*.lock_count) -eq 0 ] ||
20930                 error "Locks were requested while doing AIO"
20931
20932         # get the percentage of 1-page I/O
20933         pct=$($LCTL get_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats |
20934                 grep -A 1 'pages per rpc' | grep -v 'pages per rpc' |
20935                 awk '{print $7}')
20936         [ $pct -le 50 ] || error "$pct% of I/O are 1-page"
20937
20938         echo "mix rw ${size}M to OST0 by fio with $njobs jobs..."
20939         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
20940                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
20941                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
20942                 --filename=$DIR/$tfile
20943         [ $? -eq 0 ] || error "fio mixed read write error"
20944
20945         rm -rf $DIR/$tfile
20946         $LCTL set_param debug="$saved_debug"
20947 }
20948 run_test 398c "run fio to test AIO"
20949
20950 test_fake_rw() {
20951         local read_write=$1
20952         if [ "$read_write" = "write" ]; then
20953                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
20954         elif [ "$read_write" = "read" ]; then
20955                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
20956         else
20957                 error "argument error"
20958         fi
20959
20960         # turn off debug for performance testing
20961         local saved_debug=$($LCTL get_param -n debug)
20962         $LCTL set_param debug=0
20963
20964         $LFS setstripe -c 1 -i 0 $DIR/$tfile
20965
20966         # get ost1 size - $FSNAME-OST0000
20967         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
20968         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
20969         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
20970
20971         if [ "$read_write" = "read" ]; then
20972                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
20973         fi
20974
20975         local start_time=$(date +%s.%N)
20976         $dd_cmd bs=1M count=$blocks oflag=sync ||
20977                 error "real dd $read_write error"
20978         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
20979
20980         if [ "$read_write" = "write" ]; then
20981                 rm -f $DIR/$tfile
20982         fi
20983
20984         # define OBD_FAIL_OST_FAKE_RW           0x238
20985         do_facet ost1 $LCTL set_param fail_loc=0x238
20986
20987         local start_time=$(date +%s.%N)
20988         $dd_cmd bs=1M count=$blocks oflag=sync ||
20989                 error "fake dd $read_write error"
20990         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
20991
20992         if [ "$read_write" = "write" ]; then
20993                 # verify file size
20994                 cancel_lru_locks osc
20995                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
20996                         error "$tfile size not $blocks MB"
20997         fi
20998         do_facet ost1 $LCTL set_param fail_loc=0
20999
21000         echo "fake $read_write $duration_fake vs. normal $read_write" \
21001                 "$duration in seconds"
21002         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
21003                 error_not_in_vm "fake write is slower"
21004
21005         $LCTL set_param -n debug="$saved_debug"
21006         rm -f $DIR/$tfile
21007 }
21008 test_399a() { # LU-7655 for OST fake write
21009         remote_ost_nodsh && skip "remote OST with nodsh"
21010
21011         test_fake_rw write
21012 }
21013 run_test 399a "fake write should not be slower than normal write"
21014
21015 test_399b() { # LU-8726 for OST fake read
21016         remote_ost_nodsh && skip "remote OST with nodsh"
21017         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
21018                 skip_env "ldiskfs only test"
21019         fi
21020
21021         test_fake_rw read
21022 }
21023 run_test 399b "fake read should not be slower than normal read"
21024
21025 test_400a() { # LU-1606, was conf-sanity test_74
21026         if ! which $CC > /dev/null 2>&1; then
21027                 skip_env "$CC is not installed"
21028         fi
21029
21030         local extra_flags=''
21031         local out=$TMP/$tfile
21032         local prefix=/usr/include/lustre
21033         local prog
21034
21035         # Oleg removes c files in his test rig so test if any c files exist
21036         [ -z "$(ls -A $LUSTRE_TESTS_API_DIR)" ] && \
21037                 skip_env "Needed c test files are missing"
21038
21039         if ! [[ -d $prefix ]]; then
21040                 # Assume we're running in tree and fixup the include path.
21041                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
21042                 extra_flags+=" -L$LUSTRE/utils/.lib"
21043         fi
21044
21045         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
21046                 $CC -Wall -Werror -std=c99 $extra_flags -o $out $prog -llustreapi ||
21047                         error "client api broken"
21048         done
21049         rm -f $out
21050 }
21051 run_test 400a "Lustre client api program can compile and link"
21052
21053 test_400b() { # LU-1606, LU-5011
21054         local header
21055         local out=$TMP/$tfile
21056         local prefix=/usr/include/linux/lustre
21057
21058         # We use a hard coded prefix so that this test will not fail
21059         # when run in tree. There are headers in lustre/include/lustre/
21060         # that are not packaged (like lustre_idl.h) and have more
21061         # complicated include dependencies (like config.h and lnet/types.h).
21062         # Since this test about correct packaging we just skip them when
21063         # they don't exist (see below) rather than try to fixup cppflags.
21064
21065         if ! which $CC > /dev/null 2>&1; then
21066                 skip_env "$CC is not installed"
21067         fi
21068
21069         for header in $prefix/*.h; do
21070                 if ! [[ -f "$header" ]]; then
21071                         continue
21072                 fi
21073
21074                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
21075                         continue # lustre_ioctl.h is internal header
21076                 fi
21077
21078                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
21079                         error "cannot compile '$header'"
21080         done
21081         rm -f $out
21082 }
21083 run_test 400b "packaged headers can be compiled"
21084
21085 test_401a() { #LU-7437
21086         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
21087         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
21088
21089         #count the number of parameters by "list_param -R"
21090         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
21091         #count the number of parameters by listing proc files
21092         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
21093         echo "proc_dirs='$proc_dirs'"
21094         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
21095         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
21096                       sort -u | wc -l)
21097
21098         [ $params -eq $procs ] ||
21099                 error "found $params parameters vs. $procs proc files"
21100
21101         # test the list_param -D option only returns directories
21102         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
21103         #count the number of parameters by listing proc directories
21104         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
21105                 sort -u | wc -l)
21106
21107         [ $params -eq $procs ] ||
21108                 error "found $params parameters vs. $procs proc files"
21109 }
21110 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
21111
21112 test_401b() {
21113         local save=$($LCTL get_param -n jobid_var)
21114         local tmp=testing
21115
21116         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
21117                 error "no error returned when setting bad parameters"
21118
21119         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
21120         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
21121
21122         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
21123         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
21124         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
21125 }
21126 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
21127
21128 test_401c() {
21129         local jobid_var_old=$($LCTL get_param -n jobid_var)
21130         local jobid_var_new
21131
21132         $LCTL set_param jobid_var= &&
21133                 error "no error returned for 'set_param a='"
21134
21135         jobid_var_new=$($LCTL get_param -n jobid_var)
21136         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
21137                 error "jobid_var was changed by setting without value"
21138
21139         $LCTL set_param jobid_var &&
21140                 error "no error returned for 'set_param a'"
21141
21142         jobid_var_new=$($LCTL get_param -n jobid_var)
21143         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
21144                 error "jobid_var was changed by setting without value"
21145 }
21146 run_test 401c "Verify 'lctl set_param' without value fails in either format."
21147
21148 test_401d() {
21149         local jobid_var_old=$($LCTL get_param -n jobid_var)
21150         local jobid_var_new
21151         local new_value="foo=bar"
21152
21153         $LCTL set_param jobid_var=$new_value ||
21154                 error "'set_param a=b' did not accept a value containing '='"
21155
21156         jobid_var_new=$($LCTL get_param -n jobid_var)
21157         [[ "$jobid_var_new" == "$new_value" ]] ||
21158                 error "'set_param a=b' failed on a value containing '='"
21159
21160         # Reset the jobid_var to test the other format
21161         $LCTL set_param jobid_var=$jobid_var_old
21162         jobid_var_new=$($LCTL get_param -n jobid_var)
21163         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
21164                 error "failed to reset jobid_var"
21165
21166         $LCTL set_param jobid_var $new_value ||
21167                 error "'set_param a b' did not accept a value containing '='"
21168
21169         jobid_var_new=$($LCTL get_param -n jobid_var)
21170         [[ "$jobid_var_new" == "$new_value" ]] ||
21171                 error "'set_param a b' failed on a value containing '='"
21172
21173         $LCTL set_param jobid_var $jobid_var_old
21174         jobid_var_new=$($LCTL get_param -n jobid_var)
21175         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
21176                 error "failed to reset jobid_var"
21177 }
21178 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
21179
21180 test_402() {
21181         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
21182         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
21183                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
21184         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
21185                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
21186                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
21187         remote_mds_nodsh && skip "remote MDS with nodsh"
21188
21189         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
21190 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
21191         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
21192         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
21193                 echo "Touch failed - OK"
21194 }
21195 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
21196
21197 test_403() {
21198         local file1=$DIR/$tfile.1
21199         local file2=$DIR/$tfile.2
21200         local tfile=$TMP/$tfile
21201
21202         rm -f $file1 $file2 $tfile
21203
21204         touch $file1
21205         ln $file1 $file2
21206
21207         # 30 sec OBD_TIMEOUT in ll_getattr()
21208         # right before populating st_nlink
21209         $LCTL set_param fail_loc=0x80001409
21210         stat -c %h $file1 > $tfile &
21211
21212         # create an alias, drop all locks and reclaim the dentry
21213         < $file2
21214         cancel_lru_locks mdc
21215         cancel_lru_locks osc
21216         sysctl -w vm.drop_caches=2
21217
21218         wait
21219
21220         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
21221
21222         rm -f $tfile $file1 $file2
21223 }
21224 run_test 403 "i_nlink should not drop to zero due to aliasing"
21225
21226 test_404() { # LU-6601
21227         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
21228                 skip "Need server version newer than 2.8.52"
21229         remote_mds_nodsh && skip "remote MDS with nodsh"
21230
21231         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
21232                 awk '/osp .*-osc-MDT/ { print $4}')
21233
21234         local osp
21235         for osp in $mosps; do
21236                 echo "Deactivate: " $osp
21237                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
21238                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
21239                         awk -vp=$osp '$4 == p { print $2 }')
21240                 [ $stat = IN ] || {
21241                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
21242                         error "deactivate error"
21243                 }
21244                 echo "Activate: " $osp
21245                 do_facet $SINGLEMDS $LCTL --device %$osp activate
21246                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
21247                         awk -vp=$osp '$4 == p { print $2 }')
21248                 [ $stat = UP ] || {
21249                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
21250                         error "activate error"
21251                 }
21252         done
21253 }
21254 run_test 404 "validate manual {de}activated works properly for OSPs"
21255
21256 test_405() {
21257         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
21258         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
21259                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
21260                         skip "Layout swap lock is not supported"
21261
21262         check_swap_layouts_support
21263
21264         test_mkdir $DIR/$tdir
21265         swap_lock_test -d $DIR/$tdir ||
21266                 error "One layout swap locked test failed"
21267 }
21268 run_test 405 "Various layout swap lock tests"
21269
21270 test_406() {
21271         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21272         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
21273         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
21274         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21275         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
21276                 skip "Need MDS version at least 2.8.50"
21277
21278         local def_stripe_size=$($LFS getstripe -S $MOUNT)
21279         local test_pool=$TESTNAME
21280
21281         pool_add $test_pool || error "pool_add failed"
21282         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
21283                 error "pool_add_targets failed"
21284
21285         save_layout_restore_at_exit $MOUNT
21286
21287         # parent set default stripe count only, child will stripe from both
21288         # parent and fs default
21289         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
21290                 error "setstripe $MOUNT failed"
21291         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
21292         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
21293         for i in $(seq 10); do
21294                 local f=$DIR/$tdir/$tfile.$i
21295                 touch $f || error "touch failed"
21296                 local count=$($LFS getstripe -c $f)
21297                 [ $count -eq $OSTCOUNT ] ||
21298                         error "$f stripe count $count != $OSTCOUNT"
21299                 local offset=$($LFS getstripe -i $f)
21300                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
21301                 local size=$($LFS getstripe -S $f)
21302                 [ $size -eq $((def_stripe_size * 2)) ] ||
21303                         error "$f stripe size $size != $((def_stripe_size * 2))"
21304                 local pool=$($LFS getstripe -p $f)
21305                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
21306         done
21307
21308         # change fs default striping, delete parent default striping, now child
21309         # will stripe from new fs default striping only
21310         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
21311                 error "change $MOUNT default stripe failed"
21312         $LFS setstripe -c 0 $DIR/$tdir ||
21313                 error "delete $tdir default stripe failed"
21314         for i in $(seq 11 20); do
21315                 local f=$DIR/$tdir/$tfile.$i
21316                 touch $f || error "touch $f failed"
21317                 local count=$($LFS getstripe -c $f)
21318                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
21319                 local offset=$($LFS getstripe -i $f)
21320                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
21321                 local size=$($LFS getstripe -S $f)
21322                 [ $size -eq $def_stripe_size ] ||
21323                         error "$f stripe size $size != $def_stripe_size"
21324                 local pool=$($LFS getstripe -p $f)
21325                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
21326         done
21327
21328         unlinkmany $DIR/$tdir/$tfile. 1 20
21329
21330         local f=$DIR/$tdir/$tfile
21331         pool_remove_all_targets $test_pool $f
21332         pool_remove $test_pool $f
21333 }
21334 run_test 406 "DNE support fs default striping"
21335
21336 test_407() {
21337         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21338         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
21339                 skip "Need MDS version at least 2.8.55"
21340         remote_mds_nodsh && skip "remote MDS with nodsh"
21341
21342         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
21343                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
21344         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
21345                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
21346         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
21347
21348         #define OBD_FAIL_DT_TXN_STOP    0x2019
21349         for idx in $(seq $MDSCOUNT); do
21350                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
21351         done
21352         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
21353         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
21354                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
21355         true
21356 }
21357 run_test 407 "transaction fail should cause operation fail"
21358
21359 test_408() {
21360         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
21361
21362         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
21363         lctl set_param fail_loc=0x8000040a
21364         # let ll_prepare_partial_page() fail
21365         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
21366
21367         rm -f $DIR/$tfile
21368
21369         # create at least 100 unused inodes so that
21370         # shrink_icache_memory(0) should not return 0
21371         touch $DIR/$tfile-{0..100}
21372         rm -f $DIR/$tfile-{0..100}
21373         sync
21374
21375         echo 2 > /proc/sys/vm/drop_caches
21376 }
21377 run_test 408 "drop_caches should not hang due to page leaks"
21378
21379 test_409()
21380 {
21381         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21382
21383         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
21384         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
21385         touch $DIR/$tdir/guard || error "(2) Fail to create"
21386
21387         local PREFIX=$(str_repeat 'A' 128)
21388         echo "Create 1K hard links start at $(date)"
21389         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
21390                 error "(3) Fail to hard link"
21391
21392         echo "Links count should be right although linkEA overflow"
21393         stat $DIR/$tdir/guard || error "(4) Fail to stat"
21394         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
21395         [ $linkcount -eq 1001 ] ||
21396                 error "(5) Unexpected hard links count: $linkcount"
21397
21398         echo "List all links start at $(date)"
21399         ls -l $DIR/$tdir/foo > /dev/null ||
21400                 error "(6) Fail to list $DIR/$tdir/foo"
21401
21402         echo "Unlink hard links start at $(date)"
21403         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
21404                 error "(7) Fail to unlink"
21405         echo "Unlink hard links finished at $(date)"
21406 }
21407 run_test 409 "Large amount of cross-MDTs hard links on the same file"
21408
21409 test_410()
21410 {
21411         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
21412                 skip "Need client version at least 2.9.59"
21413
21414         # Create a file, and stat it from the kernel
21415         local testfile=$DIR/$tfile
21416         touch $testfile
21417
21418         local run_id=$RANDOM
21419         local my_ino=$(stat --format "%i" $testfile)
21420
21421         # Try to insert the module. This will always fail as the
21422         # module is designed to not be inserted.
21423         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
21424             &> /dev/null
21425
21426         # Anything but success is a test failure
21427         dmesg | grep -q \
21428             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
21429             error "no inode match"
21430 }
21431 run_test 410 "Test inode number returned from kernel thread"
21432
21433 cleanup_test411_cgroup() {
21434         trap 0
21435         rmdir "$1"
21436 }
21437
21438 test_411() {
21439         local cg_basedir=/sys/fs/cgroup/memory
21440         # LU-9966
21441         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
21442                 skip "no setup for cgroup"
21443
21444         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
21445                 error "test file creation failed"
21446         cancel_lru_locks osc
21447
21448         # Create a very small memory cgroup to force a slab allocation error
21449         local cgdir=$cg_basedir/osc_slab_alloc
21450         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
21451         trap "cleanup_test411_cgroup $cgdir" EXIT
21452         echo 2M > $cgdir/memory.kmem.limit_in_bytes
21453         echo 1M > $cgdir/memory.limit_in_bytes
21454
21455         # Should not LBUG, just be killed by oom-killer
21456         # dd will return 0 even allocation failure in some environment.
21457         # So don't check return value
21458         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
21459         cleanup_test411_cgroup $cgdir
21460
21461         return 0
21462 }
21463 run_test 411 "Slab allocation error with cgroup does not LBUG"
21464
21465 test_412() {
21466         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21467         if [ $MDS1_VERSION -lt $(version_code 2.10.55) ]; then
21468                 skip "Need server version at least 2.10.55"
21469         fi
21470
21471         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
21472                 error "mkdir failed"
21473         $LFS getdirstripe $DIR/$tdir
21474         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
21475         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
21476                 error "expect $((MDSCOUT - 1)) get $stripe_index"
21477         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
21478         [ $stripe_count -eq 2 ] ||
21479                 error "expect 2 get $stripe_count"
21480 }
21481 run_test 412 "mkdir on specific MDTs"
21482
21483 test_qos_mkdir() {
21484         local mkdir_cmd=$1
21485         local stripe_count=$2
21486         local mdts=$(comma_list $(mdts_nodes))
21487
21488         local testdir
21489         local lmv_qos_prio_free
21490         local lmv_qos_threshold_rr
21491         local lmv_qos_maxage
21492         local lod_qos_prio_free
21493         local lod_qos_threshold_rr
21494         local lod_qos_maxage
21495         local count
21496         local i
21497
21498         lmv_qos_prio_free=$($LCTL get_param -n lmv.*.qos_prio_free | head -n1)
21499         lmv_qos_prio_free=${lmv_qos_prio_free%%%}
21500         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
21501                 head -n1)
21502         lmv_qos_threshold_rr=${lmv_qos_threshold_rr%%%}
21503         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
21504         stack_trap "$LCTL set_param \
21505                 lmv.*.qos_prio_free=$lmv_qos_prio_free > /dev/null" EXIT
21506         stack_trap "$LCTL set_param \
21507                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null" EXIT
21508         stack_trap "$LCTL set_param \
21509                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null" EXIT
21510
21511         lod_qos_prio_free=$(do_facet mds1 $LCTL get_param -n \
21512                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_prio_free | head -n1)
21513         lod_qos_prio_free=${lod_qos_prio_free%%%}
21514         lod_qos_threshold_rr=$(do_facet mds1 $LCTL get_param -n \
21515                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_threshold_rr | head -n1)
21516         lod_qos_threshold_rr=${lod_qos_threshold_rr%%%}
21517         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
21518                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
21519         stack_trap "do_nodes $mdts $LCTL set_param \
21520                 lod.*.mdt_qos_prio_free=$lod_qos_prio_free > /dev/null" EXIT
21521         stack_trap "do_nodes $mdts $LCTL set_param \
21522                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null" \
21523                 EXIT
21524         stack_trap "do_nodes $mdts $LCTL set_param \
21525                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null" EXIT
21526
21527         echo
21528         echo "Mkdir (stripe_count $stripe_count) roundrobin:"
21529
21530         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
21531         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=100 > /dev/null
21532
21533         testdir=$DIR/$tdir-s$stripe_count/rr
21534
21535         for i in $(seq $((100 * MDSCOUNT))); do
21536                 eval $mkdir_cmd $testdir/subdir$i ||
21537                         error "$mkdir_cmd subdir$i failed"
21538         done
21539
21540         for i in $(seq $MDSCOUNT); do
21541                 count=$($LFS getdirstripe -i $testdir/* |
21542                                 grep ^$((i - 1))$ | wc -l)
21543                 echo "$count directories created on MDT$((i - 1))"
21544                 [ $count -eq 100 ] || error "subdirs are not evenly distributed"
21545
21546                 if [ $stripe_count -gt 1 ]; then
21547                         count=$($LFS getdirstripe $testdir/* |
21548                                 grep -P "^\s+$((i - 1))\t" | wc -l)
21549                         echo "$count stripes created on MDT$((i - 1))"
21550                         # deviation should < 5% of average
21551                         [ $count -lt $((95 * stripe_count)) ] ||
21552                         [ $count -gt $((105 * stripe_count)) ] &&
21553                                 error "stripes are not evenly distributed"
21554                 fi
21555         done
21556
21557         $LCTL set_param lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null
21558         do_nodes $mdts $LCTL set_param \
21559                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null
21560
21561         echo
21562         echo "Check for uneven MDTs: "
21563
21564         local ffree
21565         local bavail
21566         local max
21567         local min
21568         local max_index
21569         local min_index
21570         local tmp
21571
21572         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
21573         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
21574         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
21575
21576         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
21577         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
21578         max_index=0
21579         min_index=0
21580         for ((i = 1; i < ${#ffree[@]}; i++)); do
21581                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
21582                 if [ $tmp -gt $max ]; then
21583                         max=$tmp
21584                         max_index=$i
21585                 fi
21586                 if [ $tmp -lt $min ]; then
21587                         min=$tmp
21588                         min_index=$i
21589                 fi
21590         done
21591
21592         [ ${ffree[min_index]} -eq 0 ] &&
21593                 skip "no free files in MDT$min_index"
21594         [ ${ffree[min_index]} -gt 100000000 ] &&
21595                 skip "too much free files in MDT$min_index"
21596
21597         # Check if we need to generate uneven MDTs
21598         local threshold=50
21599         local diff=$(((max - min) * 100 / min))
21600         local value="$(generate_string 1024)"
21601
21602         while [ $diff -lt $threshold ]; do
21603                 # generate uneven MDTs, create till $threshold% diff
21604                 echo -n "weight diff=$diff% must be > $threshold% ..."
21605                 count=$((${ffree[min_index]} / 10))
21606                 # 50 sec per 10000 files in vm
21607                 [ $count -gt 40000 ] && [ "$SLOW" = "no" ] &&
21608                         skip "$count files to create"
21609                 echo "Fill MDT$min_index with $count files"
21610                 [ -d $DIR/$tdir-MDT$min_index ] ||
21611                         $LFS mkdir -i $min_index $DIR/$tdir-MDT$min_index ||
21612                         error "mkdir $tdir-MDT$min_index failed"
21613                 for i in $(seq $count); do
21614                         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE \
21615                                 $DIR/$tdir-MDT$min_index/f$j_$i > /dev/null ||
21616                                 error "create f$j_$i failed"
21617                         setfattr -n user.413b -v $value \
21618                                 $DIR/$tdir-MDT$min_index/f$j_$i ||
21619                                 error "setfattr f$j_$i failed"
21620                 done
21621
21622                 ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-*.filesfree))
21623                 bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-*.kbytesavail))
21624                 max=$(((${ffree[max_index]} >> 8) * \
21625                         (${bavail[max_index]} * bsize >> 16)))
21626                 min=$(((${ffree[min_index]} >> 8) * \
21627                         (${bavail[min_index]} * bsize >> 16)))
21628                 diff=$(((max - min) * 100 / min))
21629         done
21630
21631         echo "MDT filesfree available: ${ffree[@]}"
21632         echo "MDT blocks available: ${bavail[@]}"
21633         echo "weight diff=$diff%"
21634
21635         echo
21636         echo "Mkdir (stripe_count $stripe_count) with balanced space usage:"
21637
21638         $LCTL set_param lmv.*.qos_prio_free=100 > /dev/null
21639         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_prio_free=100 > /dev/null
21640         # decrease statfs age, so that it can be updated in time
21641         $LCTL set_param lmv.*.qos_maxage=1 > /dev/null
21642         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_maxage=1 > /dev/null
21643
21644         sleep 1
21645
21646         testdir=$DIR/$tdir-s$stripe_count/qos
21647
21648         for i in $(seq $((100 * MDSCOUNT))); do
21649                 eval $mkdir_cmd $testdir/subdir$i ||
21650                         error "$mkdir_cmd subdir$i failed"
21651         done
21652
21653         for i in $(seq $MDSCOUNT); do
21654                 count=$($LFS getdirstripe -i $testdir/* | grep ^$((i - 1))$ |
21655                         wc -l)
21656                 echo "$count directories created on MDT$((i - 1))"
21657
21658                 if [ $stripe_count -gt 1 ]; then
21659                         count=$($LFS getdirstripe $testdir/* |
21660                                 grep -P "^\s+$((i - 1))\t" | wc -l)
21661                         echo "$count stripes created on MDT$((i - 1))"
21662                 fi
21663         done
21664
21665         max=$($LFS getdirstripe -i $testdir/* | grep ^$max_index$ | wc -l)
21666         min=$($LFS getdirstripe -i $testdir/* | grep ^$min_index$ | wc -l)
21667
21668         # D-value should > 10% of averge
21669         [ $((max - min)) -lt 10 ] &&
21670                 error "subdirs shouldn't be evenly distributed"
21671
21672         # ditto
21673         if [ $stripe_count -gt 1 ]; then
21674                 max=$($LFS getdirstripe $testdir/* |
21675                         grep -P "^\s+$max_index\t" | wc -l)
21676                 min=$($LFS getdirstripe $testdir/* |
21677                         grep -P "^\s+$min_index\t" | wc -l)
21678                 [ $((max - min)) -le $((10 * stripe_count)) ] &&
21679                         error "stripes shouldn't be evenly distributed"|| true
21680         fi
21681 }
21682
21683 test_413a() {
21684         [ $MDSCOUNT -lt 2 ] &&
21685                 skip "We need at least 2 MDTs for this test"
21686
21687         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
21688                 skip "Need server version at least 2.12.52"
21689
21690         local stripe_count
21691
21692         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
21693                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
21694                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
21695                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
21696                 test_qos_mkdir "$LFS mkdir -c $stripe_count" $stripe_count
21697         done
21698 }
21699 run_test 413a "QoS mkdir with 'lfs mkdir -i -1'"
21700
21701 test_413b() {
21702         [ $MDSCOUNT -lt 2 ] &&
21703                 skip "We need at least 2 MDTs for this test"
21704
21705         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
21706                 skip "Need server version at least 2.12.52"
21707
21708         local stripe_count
21709
21710         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
21711                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
21712                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
21713                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
21714                 $LFS setdirstripe -D -c $stripe_count \
21715                         $DIR/$tdir-s$stripe_count/rr ||
21716                         error "setdirstripe failed"
21717                 $LFS setdirstripe -D -c $stripe_count \
21718                         $DIR/$tdir-s$stripe_count/qos ||
21719                         error "setdirstripe failed"
21720                 test_qos_mkdir "mkdir" $stripe_count
21721         done
21722 }
21723 run_test 413b "QoS mkdir under dir whose default LMV starting MDT offset is -1"
21724
21725 test_414() {
21726 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
21727         $LCTL set_param fail_loc=0x80000521
21728         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
21729         rm -f $DIR/$tfile
21730 }
21731 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
21732
21733 test_415() {
21734         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21735         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21736                 skip "Need server version at least 2.11.52"
21737
21738         # LU-11102
21739         local total
21740         local setattr_pid
21741         local start_time
21742         local end_time
21743         local duration
21744
21745         total=500
21746         # this test may be slow on ZFS
21747         [ "$mds1_FSTYPE" == "zfs" ] && total=100
21748
21749         # though this test is designed for striped directory, let's test normal
21750         # directory too since lock is always saved as CoS lock.
21751         test_mkdir $DIR/$tdir || error "mkdir $tdir"
21752         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
21753
21754         (
21755                 while true; do
21756                         touch $DIR/$tdir
21757                 done
21758         ) &
21759         setattr_pid=$!
21760
21761         start_time=$(date +%s)
21762         for i in $(seq $total); do
21763                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
21764                         > /dev/null
21765         done
21766         end_time=$(date +%s)
21767         duration=$((end_time - start_time))
21768
21769         kill -9 $setattr_pid
21770
21771         echo "rename $total files took $duration sec"
21772         [ $duration -lt 100 ] || error "rename took $duration sec"
21773 }
21774 run_test 415 "lock revoke is not missing"
21775
21776 test_416() {
21777         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
21778                 skip "Need server version at least 2.11.55"
21779
21780         # define OBD_FAIL_OSD_TXN_START    0x19a
21781         do_facet mds1 lctl set_param fail_loc=0x19a
21782
21783         lfs mkdir -c $MDSCOUNT $DIR/$tdir
21784
21785         true
21786 }
21787 run_test 416 "transaction start failure won't cause system hung"
21788
21789 cleanup_417() {
21790         trap 0
21791         do_nodes $(comma_list $(mdts_nodes)) \
21792                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
21793         do_nodes $(comma_list $(mdts_nodes)) \
21794                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
21795         do_nodes $(comma_list $(mdts_nodes)) \
21796                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
21797 }
21798
21799 test_417() {
21800         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21801         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
21802                 skip "Need MDS version at least 2.11.56"
21803
21804         trap cleanup_417 RETURN EXIT
21805
21806         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
21807         do_nodes $(comma_list $(mdts_nodes)) \
21808                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
21809         $LFS migrate -m 0 $DIR/$tdir.1 &&
21810                 error "migrate dir $tdir.1 should fail"
21811
21812         do_nodes $(comma_list $(mdts_nodes)) \
21813                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
21814         $LFS mkdir -i 1 $DIR/$tdir.2 &&
21815                 error "create remote dir $tdir.2 should fail"
21816
21817         do_nodes $(comma_list $(mdts_nodes)) \
21818                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
21819         $LFS mkdir -c 2 $DIR/$tdir.3 &&
21820                 error "create striped dir $tdir.3 should fail"
21821         true
21822 }
21823 run_test 417 "disable remote dir, striped dir and dir migration"
21824
21825 # Checks that the outputs of df [-i] and lfs df [-i] match
21826 #
21827 # usage: check_lfs_df <blocks | inodes> <mountpoint>
21828 check_lfs_df() {
21829         local dir=$2
21830         local inodes
21831         local df_out
21832         local lfs_df_out
21833         local count
21834         local passed=false
21835
21836         # blocks or inodes
21837         [ "$1" == "blocks" ] && inodes= || inodes="-i"
21838
21839         for count in {1..100}; do
21840                 cancel_lru_locks
21841                 sync; sleep 0.2
21842
21843                 # read the lines of interest
21844                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
21845                         error "df $inodes $dir | tail -n +2 failed"
21846                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
21847                         error "lfs df $inodes $dir | grep summary: failed"
21848
21849                 # skip first substrings of each output as they are different
21850                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
21851                 # compare the two outputs
21852                 passed=true
21853                 for i in {1..5}; do
21854                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
21855                 done
21856                 $passed && break
21857         done
21858
21859         if ! $passed; then
21860                 df -P $inodes $dir
21861                 echo
21862                 lfs df $inodes $dir
21863                 error "df and lfs df $1 output mismatch: "      \
21864                       "df ${inodes}: ${df_out[*]}, "            \
21865                       "lfs df ${inodes}: ${lfs_df_out[*]}"
21866         fi
21867 }
21868
21869 test_418() {
21870         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21871
21872         local dir=$DIR/$tdir
21873         local numfiles=$((RANDOM % 4096 + 2))
21874         local numblocks=$((RANDOM % 256 + 1))
21875
21876         wait_delete_completed
21877         test_mkdir $dir
21878
21879         # check block output
21880         check_lfs_df blocks $dir
21881         # check inode output
21882         check_lfs_df inodes $dir
21883
21884         # create a single file and retest
21885         echo "Creating a single file and testing"
21886         createmany -o $dir/$tfile- 1 &>/dev/null ||
21887                 error "creating 1 file in $dir failed"
21888         check_lfs_df blocks $dir
21889         check_lfs_df inodes $dir
21890
21891         # create a random number of files
21892         echo "Creating $((numfiles - 1)) files and testing"
21893         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
21894                 error "creating $((numfiles - 1)) files in $dir failed"
21895
21896         # write a random number of blocks to the first test file
21897         echo "Writing $numblocks 4K blocks and testing"
21898         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
21899                 count=$numblocks &>/dev/null ||
21900                 error "dd to $dir/${tfile}-0 failed"
21901
21902         # retest
21903         check_lfs_df blocks $dir
21904         check_lfs_df inodes $dir
21905
21906         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
21907                 error "unlinking $numfiles files in $dir failed"
21908 }
21909 run_test 418 "df and lfs df outputs match"
21910
21911 test_419()
21912 {
21913         local dir=$DIR/$tdir
21914
21915         mkdir -p $dir
21916         touch $dir/file
21917
21918         cancel_lru_locks mdc
21919
21920         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
21921         $LCTL set_param fail_loc=0x1410
21922         cat $dir/file
21923         $LCTL set_param fail_loc=0
21924         rm -rf $dir
21925 }
21926 run_test 419 "Verify open file by name doesn't crash kernel"
21927
21928 test_420()
21929 {
21930         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
21931                 skip "Need MDS version at least 2.12.53"
21932
21933         local SAVE_UMASK=$(umask)
21934         local dir=$DIR/$tdir
21935         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
21936
21937         mkdir -p $dir
21938         umask 0000
21939         mkdir -m03777 $dir/testdir
21940         ls -dn $dir/testdir
21941         # Need to remove trailing '.' when SELinux is enabled
21942         local dirperms=$(ls -dn $dir/testdir |
21943                          awk '{ sub(/\.$/, "", $1); print $1}')
21944         [ $dirperms == "drwxrwsrwt" ] ||
21945                 error "incorrect perms on $dir/testdir"
21946
21947         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
21948                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
21949         ls -n $dir/testdir/testfile
21950         local fileperms=$(ls -n $dir/testdir/testfile |
21951                           awk '{ sub(/\.$/, "", $1); print $1}')
21952         [ $fileperms == "-rwxr-xr-x" ] ||
21953                 error "incorrect perms on $dir/testdir/testfile"
21954
21955         umask $SAVE_UMASK
21956 }
21957 run_test 420 "clear SGID bit on non-directories for non-members"
21958
21959 test_421a() {
21960         local cnt
21961         local fid1
21962         local fid2
21963
21964         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
21965                 skip "Need MDS version at least 2.12.54"
21966
21967         test_mkdir $DIR/$tdir
21968         createmany -o $DIR/$tdir/f 3
21969         cnt=$(ls -1 $DIR/$tdir | wc -l)
21970         [ $cnt != 3 ] && error "unexpected #files: $cnt"
21971
21972         fid1=$(lfs path2fid $DIR/$tdir/f1)
21973         fid2=$(lfs path2fid $DIR/$tdir/f2)
21974         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
21975
21976         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
21977         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
21978
21979         cnt=$(ls -1 $DIR/$tdir | wc -l)
21980         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
21981
21982         rm -f $DIR/$tdir/f3 || error "can't remove f3"
21983         createmany -o $DIR/$tdir/f 3
21984         cnt=$(ls -1 $DIR/$tdir | wc -l)
21985         [ $cnt != 3 ] && error "unexpected #files: $cnt"
21986
21987         fid1=$(lfs path2fid $DIR/$tdir/f1)
21988         fid2=$(lfs path2fid $DIR/$tdir/f2)
21989         echo "remove using fsname $FSNAME"
21990         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
21991
21992         cnt=$(ls -1 $DIR/$tdir | wc -l)
21993         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
21994 }
21995 run_test 421a "simple rm by fid"
21996
21997 test_421b() {
21998         local cnt
21999         local FID1
22000         local FID2
22001
22002         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22003                 skip "Need MDS version at least 2.12.54"
22004
22005         test_mkdir $DIR/$tdir
22006         createmany -o $DIR/$tdir/f 3
22007         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
22008         MULTIPID=$!
22009
22010         FID1=$(lfs path2fid $DIR/$tdir/f1)
22011         FID2=$(lfs path2fid $DIR/$tdir/f2)
22012         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
22013
22014         kill -USR1 $MULTIPID
22015         wait
22016
22017         cnt=$(ls $DIR/$tdir | wc -l)
22018         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
22019 }
22020 run_test 421b "rm by fid on open file"
22021
22022 test_421c() {
22023         local cnt
22024         local FIDS
22025
22026         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22027                 skip "Need MDS version at least 2.12.54"
22028
22029         test_mkdir $DIR/$tdir
22030         createmany -o $DIR/$tdir/f 3
22031         touch $DIR/$tdir/$tfile
22032         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
22033         cnt=$(ls -1 $DIR/$tdir | wc -l)
22034         [ $cnt != 184 ] && error "unexpected #files: $cnt"
22035
22036         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
22037         $LFS rmfid $DIR $FID1 || error "rmfid failed"
22038
22039         cnt=$(ls $DIR/$tdir | wc -l)
22040         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
22041 }
22042 run_test 421c "rm by fid against hardlinked files"
22043
22044 test_421d() {
22045         local cnt
22046         local FIDS
22047
22048         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22049                 skip "Need MDS version at least 2.12.54"
22050
22051         test_mkdir $DIR/$tdir
22052         createmany -o $DIR/$tdir/f 4097
22053         cnt=$(ls -1 $DIR/$tdir | wc -l)
22054         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
22055
22056         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
22057         $LFS rmfid $DIR $FIDS || error "rmfid failed"
22058
22059         cnt=$(ls $DIR/$tdir | wc -l)
22060         rm -rf $DIR/$tdir
22061         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
22062 }
22063 run_test 421d "rmfid en masse"
22064
22065 test_421e() {
22066         local cnt
22067         local FID
22068
22069         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
22070         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22071                 skip "Need MDS version at least 2.12.54"
22072
22073         mkdir -p $DIR/$tdir
22074         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
22075         createmany -o $DIR/$tdir/striped_dir/f 512
22076         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
22077         [ $cnt != 512 ] && error "unexpected #files: $cnt"
22078
22079         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
22080                 sed "s/[/][^:]*://g")
22081         $LFS rmfid $DIR $FIDS || error "rmfid failed"
22082
22083         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
22084         rm -rf $DIR/$tdir
22085         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
22086 }
22087 run_test 421e "rmfid in DNE"
22088
22089 test_421f() {
22090         local cnt
22091         local FID
22092
22093         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22094                 skip "Need MDS version at least 2.12.54"
22095
22096         test_mkdir $DIR/$tdir
22097         touch $DIR/$tdir/f
22098         cnt=$(ls -1 $DIR/$tdir | wc -l)
22099         [ $cnt != 1 ] && error "unexpected #files: $cnt"
22100
22101         FID=$(lfs path2fid $DIR/$tdir/f)
22102         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
22103         # rmfid should fail
22104         cnt=$(ls -1 $DIR/$tdir | wc -l)
22105         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
22106
22107         chmod a+rw $DIR/$tdir
22108         ls -la $DIR/$tdir
22109         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
22110         # rmfid should fail
22111         cnt=$(ls -1 $DIR/$tdir | wc -l)
22112         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
22113
22114         rm -f $DIR/$tdir/f
22115         $RUNAS touch $DIR/$tdir/f
22116         FID=$(lfs path2fid $DIR/$tdir/f)
22117         echo "rmfid as root"
22118         $LFS rmfid $DIR $FID || error "rmfid as root failed"
22119         cnt=$(ls -1 $DIR/$tdir | wc -l)
22120         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
22121
22122         rm -f $DIR/$tdir/f
22123         $RUNAS touch $DIR/$tdir/f
22124         cnt=$(ls -1 $DIR/$tdir | wc -l)
22125         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
22126         FID=$(lfs path2fid $DIR/$tdir/f)
22127         # rmfid w/o user_fid2path mount option should fail
22128         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
22129         cnt=$(ls -1 $DIR/$tdir | wc -l)
22130         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
22131
22132         umount_client $MOUNT || error "failed to umount client"
22133         mount_client $MOUNT "$MOUNT_OPTS,user_fid2path" ||
22134                 error "failed to mount client'"
22135
22136         $RUNAS $LFS rmfid $DIR $FID || error "rmfid failed"
22137         # rmfid should succeed
22138         cnt=$(ls -1 $DIR/$tdir | wc -l)
22139         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
22140
22141         # rmfid shouldn't allow to remove files due to dir's permission
22142         chmod a+rwx $DIR/$tdir
22143         touch $DIR/$tdir/f
22144         ls -la $DIR/$tdir
22145         FID=$(lfs path2fid $DIR/$tdir/f)
22146         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail"
22147
22148         umount_client $MOUNT || error "failed to umount client"
22149         mount_client $MOUNT "$MOUNT_OPTS" ||
22150                 error "failed to mount client'"
22151
22152 }
22153 run_test 421f "rmfid checks permissions"
22154
22155 test_421g() {
22156         local cnt
22157         local FIDS
22158
22159         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
22160         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22161                 skip "Need MDS version at least 2.12.54"
22162
22163         mkdir -p $DIR/$tdir
22164         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
22165         createmany -o $DIR/$tdir/striped_dir/f 512
22166         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
22167         [ $cnt != 512 ] && error "unexpected #files: $cnt"
22168
22169         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
22170                 sed "s/[/][^:]*://g")
22171
22172         rm -f $DIR/$tdir/striped_dir/f1*
22173         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
22174         removed=$((512 - cnt))
22175
22176         # few files have been just removed, so we expect
22177         # rmfid to fail on their fids
22178         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
22179         [ $removed != $errors ] && error "$errors != $removed"
22180
22181         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
22182         rm -rf $DIR/$tdir
22183         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
22184 }
22185 run_test 421g "rmfid to return errors properly"
22186
22187 test_422() {
22188         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d1
22189         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d2
22190         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d3
22191         dd if=/dev/zero of=$DIR/$tdir/d1/file1 bs=1k count=1
22192         dd if=/dev/zero of=$DIR/$tdir/d2/file1 bs=1k count=1
22193
22194         local amc=$(at_max_get client)
22195         local amo=$(at_max_get mds1)
22196         local timeout=`lctl get_param -n timeout`
22197
22198         at_max_set 0 client
22199         at_max_set 0 mds1
22200
22201 #define OBD_FAIL_PTLRPC_PAUSE_REQ        0x50a
22202         do_facet mds1 $LCTL set_param fail_loc=0x8000050a \
22203                         fail_val=$(((2*timeout + 10)*1000))
22204         touch $DIR/$tdir/d3/file &
22205         sleep 2
22206 #define OBD_FAIL_TGT_REPLY_DATA_RACE     0x722
22207         do_facet mds1 $LCTL set_param fail_loc=0x80000722 \
22208                         fail_val=$((2*timeout + 5))
22209         mv $DIR/$tdir/d1/file1 $DIR/$tdir/d1/file2 &
22210         local pid=$!
22211         sleep 1
22212         kill -9 $pid
22213         sleep $((2 * timeout))
22214         echo kill $pid
22215         kill -9 $pid
22216         lctl mark touch
22217         touch $DIR/$tdir/d2/file3
22218         touch $DIR/$tdir/d2/file4
22219         touch $DIR/$tdir/d2/file5
22220
22221         wait
22222         at_max_set $amc client
22223         at_max_set $amo mds1
22224
22225         # LU-12838 - verify the ptlrpc thread watchdog is not always throttled
22226         do_facet mds1 "dmesg | grep 'Dumping the stack trace for debugging'" ||
22227                 error "Watchdog is always throttled"
22228 }
22229 run_test 422 "kill a process with RPC in progress"
22230
22231 stat_test() {
22232     df -h $MOUNT &
22233     df -h $MOUNT &
22234     df -h $MOUNT &
22235     df -h $MOUNT &
22236     df -h $MOUNT &
22237     df -h $MOUNT &
22238 }
22239
22240 test_423() {
22241     local _stats
22242     # ensure statfs cache is expired
22243     sleep 2;
22244
22245     _stats=$(stat_test | grep $MOUNT | sort -u | wc -l)
22246     [[ ${_stats} -ne 1 ]] && error "statfs wrong"
22247
22248     return 0
22249 }
22250 run_test 423 "statfs should return a right data"
22251
22252 prep_801() {
22253         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
22254         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
22255                 skip "Need server version at least 2.9.55"
22256
22257         start_full_debug_logging
22258 }
22259
22260 post_801() {
22261         stop_full_debug_logging
22262 }
22263
22264 barrier_stat() {
22265         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
22266                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
22267                            awk '/The barrier for/ { print $7 }')
22268                 echo $st
22269         else
22270                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
22271                 echo \'$st\'
22272         fi
22273 }
22274
22275 barrier_expired() {
22276         local expired
22277
22278         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
22279                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
22280                           awk '/will be expired/ { print $7 }')
22281         else
22282                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
22283         fi
22284
22285         echo $expired
22286 }
22287
22288 test_801a() {
22289         prep_801
22290
22291         echo "Start barrier_freeze at: $(date)"
22292         #define OBD_FAIL_BARRIER_DELAY          0x2202
22293         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
22294         # Do not reduce barrier time - See LU-11873
22295         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
22296
22297         sleep 2
22298         local b_status=$(barrier_stat)
22299         echo "Got barrier status at: $(date)"
22300         [ "$b_status" = "'freezing_p1'" ] ||
22301                 error "(1) unexpected barrier status $b_status"
22302
22303         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
22304         wait
22305         b_status=$(barrier_stat)
22306         [ "$b_status" = "'frozen'" ] ||
22307                 error "(2) unexpected barrier status $b_status"
22308
22309         local expired=$(barrier_expired)
22310         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
22311         sleep $((expired + 3))
22312
22313         b_status=$(barrier_stat)
22314         [ "$b_status" = "'expired'" ] ||
22315                 error "(3) unexpected barrier status $b_status"
22316
22317         # Do not reduce barrier time - See LU-11873
22318         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
22319                 error "(4) fail to freeze barrier"
22320
22321         b_status=$(barrier_stat)
22322         [ "$b_status" = "'frozen'" ] ||
22323                 error "(5) unexpected barrier status $b_status"
22324
22325         echo "Start barrier_thaw at: $(date)"
22326         #define OBD_FAIL_BARRIER_DELAY          0x2202
22327         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
22328         do_facet mgs $LCTL barrier_thaw $FSNAME &
22329
22330         sleep 2
22331         b_status=$(barrier_stat)
22332         echo "Got barrier status at: $(date)"
22333         [ "$b_status" = "'thawing'" ] ||
22334                 error "(6) unexpected barrier status $b_status"
22335
22336         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
22337         wait
22338         b_status=$(barrier_stat)
22339         [ "$b_status" = "'thawed'" ] ||
22340                 error "(7) unexpected barrier status $b_status"
22341
22342         #define OBD_FAIL_BARRIER_FAILURE        0x2203
22343         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
22344         do_facet mgs $LCTL barrier_freeze $FSNAME
22345
22346         b_status=$(barrier_stat)
22347         [ "$b_status" = "'failed'" ] ||
22348                 error "(8) unexpected barrier status $b_status"
22349
22350         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
22351         do_facet mgs $LCTL barrier_thaw $FSNAME
22352
22353         post_801
22354 }
22355 run_test 801a "write barrier user interfaces and stat machine"
22356
22357 test_801b() {
22358         prep_801
22359
22360         mkdir $DIR/$tdir || error "(1) fail to mkdir"
22361         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
22362         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
22363         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
22364         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
22365
22366         cancel_lru_locks mdc
22367
22368         # 180 seconds should be long enough
22369         do_facet mgs $LCTL barrier_freeze $FSNAME 180
22370
22371         local b_status=$(barrier_stat)
22372         [ "$b_status" = "'frozen'" ] ||
22373                 error "(6) unexpected barrier status $b_status"
22374
22375         mkdir $DIR/$tdir/d0/d10 &
22376         mkdir_pid=$!
22377
22378         touch $DIR/$tdir/d1/f13 &
22379         touch_pid=$!
22380
22381         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
22382         ln_pid=$!
22383
22384         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
22385         mv_pid=$!
22386
22387         rm -f $DIR/$tdir/d4/f12 &
22388         rm_pid=$!
22389
22390         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
22391
22392         # To guarantee taht the 'stat' is not blocked
22393         b_status=$(barrier_stat)
22394         [ "$b_status" = "'frozen'" ] ||
22395                 error "(8) unexpected barrier status $b_status"
22396
22397         # let above commands to run at background
22398         sleep 5
22399
22400         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
22401         ps -p $touch_pid || error "(10) touch should be blocked"
22402         ps -p $ln_pid || error "(11) link should be blocked"
22403         ps -p $mv_pid || error "(12) rename should be blocked"
22404         ps -p $rm_pid || error "(13) unlink should be blocked"
22405
22406         b_status=$(barrier_stat)
22407         [ "$b_status" = "'frozen'" ] ||
22408                 error "(14) unexpected barrier status $b_status"
22409
22410         do_facet mgs $LCTL barrier_thaw $FSNAME
22411         b_status=$(barrier_stat)
22412         [ "$b_status" = "'thawed'" ] ||
22413                 error "(15) unexpected barrier status $b_status"
22414
22415         wait $mkdir_pid || error "(16) mkdir should succeed"
22416         wait $touch_pid || error "(17) touch should succeed"
22417         wait $ln_pid || error "(18) link should succeed"
22418         wait $mv_pid || error "(19) rename should succeed"
22419         wait $rm_pid || error "(20) unlink should succeed"
22420
22421         post_801
22422 }
22423 run_test 801b "modification will be blocked by write barrier"
22424
22425 test_801c() {
22426         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22427
22428         prep_801
22429
22430         stop mds2 || error "(1) Fail to stop mds2"
22431
22432         do_facet mgs $LCTL barrier_freeze $FSNAME 30
22433
22434         local b_status=$(barrier_stat)
22435         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
22436                 do_facet mgs $LCTL barrier_thaw $FSNAME
22437                 error "(2) unexpected barrier status $b_status"
22438         }
22439
22440         do_facet mgs $LCTL barrier_rescan $FSNAME ||
22441                 error "(3) Fail to rescan barrier bitmap"
22442
22443         # Do not reduce barrier time - See LU-11873
22444         do_facet mgs $LCTL barrier_freeze $FSNAME 20
22445
22446         b_status=$(barrier_stat)
22447         [ "$b_status" = "'frozen'" ] ||
22448                 error "(4) unexpected barrier status $b_status"
22449
22450         do_facet mgs $LCTL barrier_thaw $FSNAME
22451         b_status=$(barrier_stat)
22452         [ "$b_status" = "'thawed'" ] ||
22453                 error "(5) unexpected barrier status $b_status"
22454
22455         local devname=$(mdsdevname 2)
22456
22457         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
22458
22459         do_facet mgs $LCTL barrier_rescan $FSNAME ||
22460                 error "(7) Fail to rescan barrier bitmap"
22461
22462         post_801
22463 }
22464 run_test 801c "rescan barrier bitmap"
22465
22466 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
22467 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
22468 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
22469 saved_MOUNT_OPTS=$MOUNT_OPTS
22470
22471 cleanup_802a() {
22472         trap 0
22473
22474         stopall
22475         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
22476         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
22477         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
22478         MOUNT_OPTS=$saved_MOUNT_OPTS
22479         setupall
22480 }
22481
22482 test_802a() {
22483         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
22484         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
22485         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
22486                 skip "Need server version at least 2.9.55"
22487
22488         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
22489
22490         mkdir $DIR/$tdir || error "(1) fail to mkdir"
22491
22492         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
22493                 error "(2) Fail to copy"
22494
22495         trap cleanup_802a EXIT
22496
22497         # sync by force before remount as readonly
22498         sync; sync_all_data; sleep 3; sync_all_data
22499
22500         stopall
22501
22502         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
22503         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
22504         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
22505
22506         echo "Mount the server as read only"
22507         setupall server_only || error "(3) Fail to start servers"
22508
22509         echo "Mount client without ro should fail"
22510         mount_client $MOUNT &&
22511                 error "(4) Mount client without 'ro' should fail"
22512
22513         echo "Mount client with ro should succeed"
22514         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
22515         mount_client $MOUNT ||
22516                 error "(5) Mount client with 'ro' should succeed"
22517
22518         echo "Modify should be refused"
22519         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
22520
22521         echo "Read should be allowed"
22522         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
22523                 error "(7) Read should succeed under ro mode"
22524
22525         cleanup_802a
22526 }
22527 run_test 802a "simulate readonly device"
22528
22529 test_802b() {
22530         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22531         remote_mds_nodsh && skip "remote MDS with nodsh"
22532
22533         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
22534                 skip "readonly option not available"
22535
22536         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
22537
22538         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
22539                 error "(2) Fail to copy"
22540
22541         # write back all cached data before setting MDT to readonly
22542         cancel_lru_locks
22543         sync_all_data
22544
22545         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
22546         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
22547
22548         echo "Modify should be refused"
22549         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
22550
22551         echo "Read should be allowed"
22552         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
22553                 error "(7) Read should succeed under ro mode"
22554
22555         # disable readonly
22556         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
22557 }
22558 run_test 802b "be able to set MDTs to readonly"
22559
22560 test_803() {
22561         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22562         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
22563                 skip "MDS needs to be newer than 2.10.54"
22564
22565         mkdir -p $DIR/$tdir
22566         # Create some objects on all MDTs to trigger related logs objects
22567         for idx in $(seq $MDSCOUNT); do
22568                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
22569                         $DIR/$tdir/dir${idx} ||
22570                         error "Fail to create $DIR/$tdir/dir${idx}"
22571         done
22572
22573         sync; sleep 3
22574         wait_delete_completed # ensure old test cleanups are finished
22575         echo "before create:"
22576         $LFS df -i $MOUNT
22577         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
22578
22579         for i in {1..10}; do
22580                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
22581                         error "Fail to create $DIR/$tdir/foo$i"
22582         done
22583
22584         sync; sleep 3
22585         echo "after create:"
22586         $LFS df -i $MOUNT
22587         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
22588
22589         # allow for an llog to be cleaned up during the test
22590         [ $after_used -ge $((before_used + 10 - 1)) ] ||
22591                 error "before ($before_used) + 10 > after ($after_used)"
22592
22593         for i in {1..10}; do
22594                 rm -rf $DIR/$tdir/foo$i ||
22595                         error "Fail to remove $DIR/$tdir/foo$i"
22596         done
22597
22598         sleep 3 # avoid MDT return cached statfs
22599         wait_delete_completed
22600         echo "after unlink:"
22601         $LFS df -i $MOUNT
22602         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
22603
22604         # allow for an llog to be created during the test
22605         [ $after_used -le $((before_used + 1)) ] ||
22606                 error "after ($after_used) > before ($before_used) + 1"
22607 }
22608 run_test 803 "verify agent object for remote object"
22609
22610 test_804() {
22611         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22612         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
22613                 skip "MDS needs to be newer than 2.10.54"
22614         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
22615
22616         mkdir -p $DIR/$tdir
22617         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
22618                 error "Fail to create $DIR/$tdir/dir0"
22619
22620         local fid=$($LFS path2fid $DIR/$tdir/dir0)
22621         local dev=$(mdsdevname 2)
22622
22623         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22624                 grep ${fid} || error "NOT found agent entry for dir0"
22625
22626         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
22627                 error "Fail to create $DIR/$tdir/dir1"
22628
22629         touch $DIR/$tdir/dir1/foo0 ||
22630                 error "Fail to create $DIR/$tdir/dir1/foo0"
22631         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
22632         local rc=0
22633
22634         for idx in $(seq $MDSCOUNT); do
22635                 dev=$(mdsdevname $idx)
22636                 do_facet mds${idx} \
22637                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22638                         grep ${fid} && rc=$idx
22639         done
22640
22641         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
22642                 error "Fail to rename foo0 to foo1"
22643         if [ $rc -eq 0 ]; then
22644                 for idx in $(seq $MDSCOUNT); do
22645                         dev=$(mdsdevname $idx)
22646                         do_facet mds${idx} \
22647                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22648                         grep ${fid} && rc=$idx
22649                 done
22650         fi
22651
22652         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
22653                 error "Fail to rename foo1 to foo2"
22654         if [ $rc -eq 0 ]; then
22655                 for idx in $(seq $MDSCOUNT); do
22656                         dev=$(mdsdevname $idx)
22657                         do_facet mds${idx} \
22658                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
22659                         grep ${fid} && rc=$idx
22660                 done
22661         fi
22662
22663         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
22664
22665         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
22666                 error "Fail to link to $DIR/$tdir/dir1/foo2"
22667         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
22668                 error "Fail to rename foo2 to foo0"
22669         unlink $DIR/$tdir/dir1/foo0 ||
22670                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
22671         rm -rf $DIR/$tdir/dir0 ||
22672                 error "Fail to rm $DIR/$tdir/dir0"
22673
22674         for idx in $(seq $MDSCOUNT); do
22675                 dev=$(mdsdevname $idx)
22676                 rc=0
22677
22678                 stop mds${idx}
22679                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
22680                         rc=$?
22681                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
22682                         error "mount mds$idx failed"
22683                 df $MOUNT > /dev/null 2>&1
22684
22685                 # e2fsck should not return error
22686                 [ $rc -eq 0 ] ||
22687                         error "e2fsck detected error on MDT${idx}: rc=$rc"
22688         done
22689 }
22690 run_test 804 "verify agent entry for remote entry"
22691
22692 cleanup_805() {
22693         do_facet $SINGLEMDS zfs set quota=$old $fsset
22694         unlinkmany $DIR/$tdir/f- 1000000
22695         trap 0
22696 }
22697
22698 test_805() {
22699         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
22700         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
22701         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
22702                 skip "netfree not implemented before 0.7"
22703         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
22704                 skip "Need MDS version at least 2.10.57"
22705
22706         local fsset
22707         local freekb
22708         local usedkb
22709         local old
22710         local quota
22711         local pref="osd-zfs.$FSNAME-MDT0000."
22712
22713         # limit available space on MDS dataset to meet nospace issue
22714         # quickly. then ZFS 0.7.2 can use reserved space if asked
22715         # properly (using netfree flag in osd_declare_destroy()
22716         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
22717         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
22718                 gawk '{print $3}')
22719         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
22720         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
22721         let "usedkb=usedkb-freekb"
22722         let "freekb=freekb/2"
22723         if let "freekb > 5000"; then
22724                 let "freekb=5000"
22725         fi
22726         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
22727         trap cleanup_805 EXIT
22728         mkdir $DIR/$tdir
22729         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
22730         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
22731         rm -rf $DIR/$tdir || error "not able to remove"
22732         do_facet $SINGLEMDS zfs set quota=$old $fsset
22733         trap 0
22734 }
22735 run_test 805 "ZFS can remove from full fs"
22736
22737 # Size-on-MDS test
22738 check_lsom_data()
22739 {
22740         local file=$1
22741         local size=$($LFS getsom -s $file)
22742         local expect=$(stat -c %s $file)
22743
22744         [[ $size == $expect ]] ||
22745                 error "$file expected size: $expect, got: $size"
22746
22747         local blocks=$($LFS getsom -b $file)
22748         expect=$(stat -c %b $file)
22749         [[ $blocks == $expect ]] ||
22750                 error "$file expected blocks: $expect, got: $blocks"
22751 }
22752
22753 check_lsom_size()
22754 {
22755         local size=$($LFS getsom -s $1)
22756         local expect=$2
22757
22758         [[ $size == $expect ]] ||
22759                 error "$file expected size: $expect, got: $size"
22760 }
22761
22762 test_806() {
22763         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
22764                 skip "Need MDS version at least 2.11.52"
22765
22766         local bs=1048576
22767
22768         touch $DIR/$tfile || error "touch $tfile failed"
22769
22770         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
22771         save_lustre_params client "llite.*.xattr_cache" > $save
22772         lctl set_param llite.*.xattr_cache=0
22773         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
22774
22775         # single-threaded write
22776         echo "Test SOM for single-threaded write"
22777         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
22778                 error "write $tfile failed"
22779         check_lsom_size $DIR/$tfile $bs
22780
22781         local num=32
22782         local size=$(($num * $bs))
22783         local offset=0
22784         local i
22785
22786         echo "Test SOM for single client multi-threaded($num) write"
22787         $TRUNCATE $DIR/$tfile 0
22788         for ((i = 0; i < $num; i++)); do
22789                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22790                 local pids[$i]=$!
22791                 offset=$((offset + $bs))
22792         done
22793         for (( i=0; i < $num; i++ )); do
22794                 wait ${pids[$i]}
22795         done
22796         check_lsom_size $DIR/$tfile $size
22797
22798         $TRUNCATE $DIR/$tfile 0
22799         for ((i = 0; i < $num; i++)); do
22800                 offset=$((offset - $bs))
22801                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22802                 local pids[$i]=$!
22803         done
22804         for (( i=0; i < $num; i++ )); do
22805                 wait ${pids[$i]}
22806         done
22807         check_lsom_size $DIR/$tfile $size
22808
22809         # multi-client writes
22810         num=$(get_node_count ${CLIENTS//,/ })
22811         size=$(($num * $bs))
22812         offset=0
22813         i=0
22814
22815         echo "Test SOM for multi-client ($num) writes"
22816         $TRUNCATE $DIR/$tfile 0
22817         for client in ${CLIENTS//,/ }; do
22818                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22819                 local pids[$i]=$!
22820                 i=$((i + 1))
22821                 offset=$((offset + $bs))
22822         done
22823         for (( i=0; i < $num; i++ )); do
22824                 wait ${pids[$i]}
22825         done
22826         check_lsom_size $DIR/$tfile $offset
22827
22828         i=0
22829         $TRUNCATE $DIR/$tfile 0
22830         for client in ${CLIENTS//,/ }; do
22831                 offset=$((offset - $bs))
22832                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22833                 local pids[$i]=$!
22834                 i=$((i + 1))
22835         done
22836         for (( i=0; i < $num; i++ )); do
22837                 wait ${pids[$i]}
22838         done
22839         check_lsom_size $DIR/$tfile $size
22840
22841         # verify truncate
22842         echo "Test SOM for truncate"
22843         $TRUNCATE $DIR/$tfile 1048576
22844         check_lsom_size $DIR/$tfile 1048576
22845         $TRUNCATE $DIR/$tfile 1234
22846         check_lsom_size $DIR/$tfile 1234
22847
22848         # verify SOM blocks count
22849         echo "Verify SOM block count"
22850         $TRUNCATE $DIR/$tfile 0
22851         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
22852                 error "failed to write file $tfile"
22853         check_lsom_data $DIR/$tfile
22854 }
22855 run_test 806 "Verify Lazy Size on MDS"
22856
22857 test_807() {
22858         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
22859         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
22860                 skip "Need MDS version at least 2.11.52"
22861
22862         # Registration step
22863         changelog_register || error "changelog_register failed"
22864         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
22865         changelog_users $SINGLEMDS | grep -q $cl_user ||
22866                 error "User $cl_user not found in changelog_users"
22867
22868         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
22869         save_lustre_params client "llite.*.xattr_cache" > $save
22870         lctl set_param llite.*.xattr_cache=0
22871         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
22872
22873         rm -rf $DIR/$tdir || error "rm $tdir failed"
22874         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
22875         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
22876         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
22877         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
22878                 error "truncate $tdir/trunc failed"
22879
22880         local bs=1048576
22881         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 conv=fsync ||
22882                 error "write $tfile failed"
22883
22884         # multi-client wirtes
22885         local num=$(get_node_count ${CLIENTS//,/ })
22886         local offset=0
22887         local i=0
22888
22889         echo "Test SOM for multi-client ($num) writes"
22890         touch $DIR/$tfile || error "touch $tfile failed"
22891         $TRUNCATE $DIR/$tfile 0
22892         for client in ${CLIENTS//,/ }; do
22893                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
22894                 local pids[$i]=$!
22895                 i=$((i + 1))
22896                 offset=$((offset + $bs))
22897         done
22898         for (( i=0; i < $num; i++ )); do
22899                 wait ${pids[$i]}
22900         done
22901
22902         do_rpc_nodes "$CLIENTS" cancel_lru_locks osc
22903         do_nodes "$CLIENTS" "sync ; sleep 5 ; sync"
22904         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
22905         check_lsom_data $DIR/$tdir/trunc
22906         check_lsom_data $DIR/$tdir/single_dd
22907         check_lsom_data $DIR/$tfile
22908
22909         rm -rf $DIR/$tdir
22910         # Deregistration step
22911         changelog_deregister || error "changelog_deregister failed"
22912 }
22913 run_test 807 "verify LSOM syncing tool"
22914
22915 check_som_nologged()
22916 {
22917         local lines=$($LFS changelog $FSNAME-MDT0000 |
22918                 grep 'x=trusted.som' | wc -l)
22919         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
22920 }
22921
22922 test_808() {
22923         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
22924                 skip "Need MDS version at least 2.11.55"
22925
22926         # Registration step
22927         changelog_register || error "changelog_register failed"
22928
22929         touch $DIR/$tfile || error "touch $tfile failed"
22930         check_som_nologged
22931
22932         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
22933                 error "write $tfile failed"
22934         check_som_nologged
22935
22936         $TRUNCATE $DIR/$tfile 1234
22937         check_som_nologged
22938
22939         $TRUNCATE $DIR/$tfile 1048576
22940         check_som_nologged
22941
22942         # Deregistration step
22943         changelog_deregister || error "changelog_deregister failed"
22944 }
22945 run_test 808 "Check trusted.som xattr not logged in Changelogs"
22946
22947 check_som_nodata()
22948 {
22949         $LFS getsom $1
22950         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
22951 }
22952
22953 test_809() {
22954         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
22955                 skip "Need MDS version at least 2.11.56"
22956
22957         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
22958                 error "failed to create DoM-only file $DIR/$tfile"
22959         touch $DIR/$tfile || error "touch $tfile failed"
22960         check_som_nodata $DIR/$tfile
22961
22962         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
22963                 error "write $tfile failed"
22964         check_som_nodata $DIR/$tfile
22965
22966         $TRUNCATE $DIR/$tfile 1234
22967         check_som_nodata $DIR/$tfile
22968
22969         $TRUNCATE $DIR/$tfile 4097
22970         check_som_nodata $DIR/$file
22971 }
22972 run_test 809 "Verify no SOM xattr store for DoM-only files"
22973
22974 test_810() {
22975         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22976         $GSS && skip_env "could not run with gss"
22977         [[ $OST1_VERSION -gt $(version_code 2.12.58) ]] ||
22978                 skip "OST < 2.12.58 doesn't align checksum"
22979
22980         set_checksums 1
22981         stack_trap "set_checksums $ORIG_CSUM" EXIT
22982         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
22983
22984         local csum
22985         local before
22986         local after
22987         for csum in $CKSUM_TYPES; do
22988                 #define OBD_FAIL_OSC_NO_GRANT   0x411
22989                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
22990                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
22991                         eval set -- $i
22992                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
22993                         before=$(md5sum $DIR/$tfile)
22994                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
22995                         after=$(md5sum $DIR/$tfile)
22996                         [ "$before" == "$after" ] ||
22997                                 error "$csum: $before != $after bs=$1 seek=$2"
22998                 done
22999         done
23000 }
23001 run_test 810 "partial page writes on ZFS (LU-11663)"
23002
23003 test_812a() {
23004         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
23005                 skip "OST < 2.12.51 doesn't support this fail_loc"
23006         [ "$SHARED_KEY" = true ] &&
23007                 skip "OSC connections never go IDLE with Shared-Keys enabled"
23008
23009         $LFS setstripe -c 1 -i 0 $DIR/$tfile
23010         # ensure ost1 is connected
23011         stat $DIR/$tfile >/dev/null || error "can't stat"
23012         wait_osc_import_state client ost1 FULL
23013         # no locks, no reqs to let the connection idle
23014         cancel_lru_locks osc
23015
23016         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
23017 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
23018         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
23019         wait_osc_import_state client ost1 CONNECTING
23020         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
23021
23022         stat $DIR/$tfile >/dev/null || error "can't stat file"
23023 }
23024 run_test 812a "do not drop reqs generated when imp is going to idle (LU-11951)"
23025
23026 test_812b() { # LU-12378
23027         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
23028                 skip "OST < 2.12.51 doesn't support this fail_loc"
23029         [ "$SHARED_KEY" = true ] &&
23030                 skip "OSC connections never go IDLE with Shared-Keys enabled"
23031
23032         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "setstripe failed"
23033         # ensure ost1 is connected
23034         stat $DIR/$tfile >/dev/null || error "can't stat"
23035         wait_osc_import_state client ost1 FULL
23036         # no locks, no reqs to let the connection idle
23037         cancel_lru_locks osc
23038
23039         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
23040 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
23041         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
23042         wait_osc_import_state client ost1 CONNECTING
23043         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
23044
23045         $LFS quota -u 0 $DIR/ || error "lfs quota should succeed"
23046         wait_osc_import_state client ost1 IDLE
23047 }
23048 run_test 812b "do not drop no resend request for idle connect"
23049
23050 test_813() {
23051         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
23052         [ -z "$file_heat_sav" ] && skip "no file heat support"
23053
23054         local readsample
23055         local writesample
23056         local readbyte
23057         local writebyte
23058         local readsample1
23059         local writesample1
23060         local readbyte1
23061         local writebyte1
23062
23063         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
23064         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
23065
23066         $LCTL set_param -n llite.*.file_heat=1
23067         echo "Turn on file heat"
23068         echo "Period second: $period_second, Decay percentage: $decay_pct"
23069
23070         echo "QQQQ" > $DIR/$tfile
23071         echo "QQQQ" > $DIR/$tfile
23072         echo "QQQQ" > $DIR/$tfile
23073         cat $DIR/$tfile > /dev/null
23074         cat $DIR/$tfile > /dev/null
23075         cat $DIR/$tfile > /dev/null
23076         cat $DIR/$tfile > /dev/null
23077
23078         local out=$($LFS heat_get $DIR/$tfile)
23079
23080         $LFS heat_get $DIR/$tfile
23081         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23082         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23083         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23084         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23085
23086         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
23087         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
23088         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
23089         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
23090
23091         sleep $((period_second + 3))
23092         echo "Sleep $((period_second + 3)) seconds..."
23093         # The recursion formula to calculate the heat of the file f is as
23094         # follow:
23095         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
23096         # Where Hi is the heat value in the period between time points i*I and
23097         # (i+1)*I; Ci is the access count in the period; the symbol P refers
23098         # to the weight of Ci.
23099         out=$($LFS heat_get $DIR/$tfile)
23100         $LFS heat_get $DIR/$tfile
23101         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23102         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23103         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23104         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23105
23106         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
23107                 error "read sample ($readsample) is wrong"
23108         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
23109                 error "write sample ($writesample) is wrong"
23110         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
23111                 error "read bytes ($readbyte) is wrong"
23112         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
23113                 error "write bytes ($writebyte) is wrong"
23114
23115         echo "QQQQ" > $DIR/$tfile
23116         echo "QQQQ" > $DIR/$tfile
23117         echo "QQQQ" > $DIR/$tfile
23118         cat $DIR/$tfile > /dev/null
23119         cat $DIR/$tfile > /dev/null
23120         cat $DIR/$tfile > /dev/null
23121         cat $DIR/$tfile > /dev/null
23122
23123         sleep $((period_second + 3))
23124         echo "Sleep $((period_second + 3)) seconds..."
23125
23126         out=$($LFS heat_get $DIR/$tfile)
23127         $LFS heat_get $DIR/$tfile
23128         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23129         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23130         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23131         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23132
23133         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
23134                 4 * $decay_pct) / 100") -eq 1 ] ||
23135                 error "read sample ($readsample1) is wrong"
23136         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
23137                 3 * $decay_pct) / 100") -eq 1 ] ||
23138                 error "write sample ($writesample1) is wrong"
23139         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
23140                 20 * $decay_pct) / 100") -eq 1 ] ||
23141                 error "read bytes ($readbyte1) is wrong"
23142         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
23143                 15 * $decay_pct) / 100") -eq 1 ] ||
23144                 error "write bytes ($writebyte1) is wrong"
23145
23146         echo "Turn off file heat for the file $DIR/$tfile"
23147         $LFS heat_set -o $DIR/$tfile
23148
23149         echo "QQQQ" > $DIR/$tfile
23150         echo "QQQQ" > $DIR/$tfile
23151         echo "QQQQ" > $DIR/$tfile
23152         cat $DIR/$tfile > /dev/null
23153         cat $DIR/$tfile > /dev/null
23154         cat $DIR/$tfile > /dev/null
23155         cat $DIR/$tfile > /dev/null
23156
23157         out=$($LFS heat_get $DIR/$tfile)
23158         $LFS heat_get $DIR/$tfile
23159         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23160         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23161         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23162         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23163
23164         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
23165         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
23166         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
23167         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
23168
23169         echo "Trun on file heat for the file $DIR/$tfile"
23170         $LFS heat_set -O $DIR/$tfile
23171
23172         echo "QQQQ" > $DIR/$tfile
23173         echo "QQQQ" > $DIR/$tfile
23174         echo "QQQQ" > $DIR/$tfile
23175         cat $DIR/$tfile > /dev/null
23176         cat $DIR/$tfile > /dev/null
23177         cat $DIR/$tfile > /dev/null
23178         cat $DIR/$tfile > /dev/null
23179
23180         out=$($LFS heat_get $DIR/$tfile)
23181         $LFS heat_get $DIR/$tfile
23182         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23183         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23184         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23185         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23186
23187         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
23188         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
23189         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
23190         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
23191
23192         $LFS heat_set -c $DIR/$tfile
23193         $LCTL set_param -n llite.*.file_heat=0
23194         echo "Turn off file heat support for the Lustre filesystem"
23195
23196         echo "QQQQ" > $DIR/$tfile
23197         echo "QQQQ" > $DIR/$tfile
23198         echo "QQQQ" > $DIR/$tfile
23199         cat $DIR/$tfile > /dev/null
23200         cat $DIR/$tfile > /dev/null
23201         cat $DIR/$tfile > /dev/null
23202         cat $DIR/$tfile > /dev/null
23203
23204         out=$($LFS heat_get $DIR/$tfile)
23205         $LFS heat_get $DIR/$tfile
23206         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23207         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23208         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23209         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23210
23211         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
23212         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
23213         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
23214         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
23215
23216         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
23217         rm -f $DIR/$tfile
23218 }
23219 run_test 813 "File heat verfication"
23220
23221 test_814()
23222 {
23223         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
23224         echo -n y >> $DIR/$tfile
23225         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
23226         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
23227 }
23228 run_test 814 "sparse cp works as expected (LU-12361)"
23229
23230 test_815()
23231 {
23232         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
23233         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
23234 }
23235 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
23236
23237 test_816() {
23238         [ "$SHARED_KEY" = true ] &&
23239                 skip "OSC connections never go IDLE with Shared-Keys enabled"
23240
23241         $LFS setstripe -c 1 -i 0 $DIR/$tfile
23242         # ensure ost1 is connected
23243         stat $DIR/$tfile >/dev/null || error "can't stat"
23244         wait_osc_import_state client ost1 FULL
23245         # no locks, no reqs to let the connection idle
23246         cancel_lru_locks osc
23247         lru_resize_disable osc
23248         local before
23249         local now
23250         before=$($LCTL get_param -n \
23251                  ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
23252
23253         wait_osc_import_state client ost1 IDLE
23254         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
23255         now=$($LCTL get_param -n \
23256               ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
23257         [ $before == $now ] || error "lru_size changed $before != $now"
23258 }
23259 run_test 816 "do not reset lru_resize on idle reconnect"
23260
23261 cleanup_817() {
23262         umount $tmpdir
23263         exportfs -u localhost:$DIR/nfsexp
23264         rm -rf $DIR/nfsexp
23265 }
23266
23267 test_817() {
23268         systemctl restart nfs-server.service || skip "failed to restart nfsd"
23269
23270         mkdir -p $DIR/nfsexp
23271         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
23272                 error "failed to export nfs"
23273
23274         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
23275         stack_trap cleanup_817 EXIT
23276
23277         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
23278                 error "failed to mount nfs to $tmpdir"
23279
23280         cp /bin/true $tmpdir
23281         $DIR/nfsexp/true || error "failed to execute 'true' command"
23282 }
23283 run_test 817 "nfsd won't cache write lock for exec file"
23284
23285 test_818() {
23286         mkdir $DIR/$tdir
23287         $LFS setstripe -c1 -i0 $DIR/$tfile
23288         $LFS setstripe -c1 -i1 $DIR/$tfile
23289         stop $SINGLEMDS
23290         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
23291         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
23292         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
23293                 error "start $SINGLEMDS failed"
23294         rm -rf $DIR/$tdir
23295 }
23296 run_test 818 "unlink with failed llog"
23297
23298 test_819a() {
23299         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23300         cancel_lru_locks osc
23301         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
23302         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
23303         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
23304         rm -f $TDIR/$tfile
23305 }
23306 run_test 819a "too big niobuf in read"
23307
23308 test_819b() {
23309         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
23310         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
23311         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23312         cancel_lru_locks osc
23313         sleep 1
23314         rm -f $TDIR/$tfile
23315 }
23316 run_test 819b "too big niobuf in write"
23317
23318
23319 function test_820_start_ost() {
23320         sleep 5
23321
23322         for num in $(seq $OSTCOUNT); do
23323                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS
23324         done
23325 }
23326
23327 test_820() {
23328         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
23329
23330         mkdir $DIR/$tdir
23331         umount_client $MOUNT || error "umount failed"
23332         for num in $(seq $OSTCOUNT); do
23333                 stop ost$num
23334         done
23335
23336         # mount client with no active OSTs
23337         # so that the client can't initialize max LOV EA size
23338         # from OSC notifications
23339         mount_client $MOUNT || error "mount failed"
23340         # delay OST starting to keep this 0 max EA size for a while
23341         test_820_start_ost &
23342
23343         # create a directory on MDS2
23344         test_mkdir -i 1 -c1 $DIR/$tdir/mds2 ||
23345                 error "Failed to create directory"
23346         # open intent should update default EA size
23347         # see mdc_update_max_ea_from_body()
23348         # notice this is the very first RPC to MDS2
23349         cp /etc/services $DIR/$tdir/mds2 ||
23350                 error "Failed to copy files to mds$n"
23351 }
23352 run_test 820 "update max EA from open intent"
23353
23354 #
23355 # tests that do cleanup/setup should be run at the end
23356 #
23357
23358 test_900() {
23359         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23360         local ls
23361
23362         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
23363         $LCTL set_param fail_loc=0x903
23364
23365         cancel_lru_locks MGC
23366
23367         FAIL_ON_ERROR=true cleanup
23368         FAIL_ON_ERROR=true setup
23369 }
23370 run_test 900 "umount should not race with any mgc requeue thread"
23371
23372 # LUS-6253/LU-11185
23373 test_901() {
23374         local oldc
23375         local newc
23376         local olds
23377         local news
23378         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23379
23380         # some get_param have a bug to handle dot in param name
23381         cancel_lru_locks MGC
23382         oldc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
23383         olds=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
23384         umount_client $MOUNT || error "umount failed"
23385         mount_client $MOUNT || error "mount failed"
23386         cancel_lru_locks MGC
23387         newc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
23388         news=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
23389
23390         [ $oldc -lt $newc ] && error "mgc lock leak ($oldc != $newc)"
23391         [ $olds -lt $news ] && error "mgs lock leak ($olds != $news)"
23392
23393         return 0
23394 }
23395 run_test 901 "don't leak a mgc lock on client umount"
23396
23397 # LU-13377
23398 test_902() {
23399         [ $CLIENT_VERSION -lt $(version_code 2.13.52) ] &&
23400                 skip "client does not have LU-13377 fix"
23401         #define OBD_FAIL_LLITE_SHORT_COMMIT 0x1415
23402         $LCTL set_param fail_loc=0x1415
23403         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23404         cancel_lru_locks osc
23405         rm -f $DIR/$tfile
23406 }
23407 run_test 902 "test short write doesn't hang lustre"
23408
23409 complete $SECONDS
23410 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
23411 check_and_cleanup_lustre
23412 if [ "$I_MOUNTED" != "yes" ]; then
23413         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
23414 fi
23415 exit_status