Whamcloud - gitweb
2a32e6886d85cf196a66ce6d37e29308d86e9839
[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 OSC=${OSC:-"osc"}
14
15 CC=${CC:-cc}
16 CREATETEST=${CREATETEST:-createtest}
17 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
18 OPENFILE=${OPENFILE:-openfile}
19 OPENUNLINK=${OPENUNLINK:-openunlink}
20 READS=${READS:-"reads"}
21 MUNLINK=${MUNLINK:-munlink}
22 SOCKETSERVER=${SOCKETSERVER:-socketserver}
23 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
24 MEMHOG=${MEMHOG:-memhog}
25 DIRECTIO=${DIRECTIO:-directio}
26 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
27 DEF_STRIPE_COUNT=-1
28 CHECK_GRANT=${CHECK_GRANT:-"yes"}
29 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
30 export PARALLEL=${PARALLEL:-"no"}
31
32 TRACE=${TRACE:-""}
33 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
34 LUSTRE=${LUSTRE:-$(dirname $0)/..}
35 . $LUSTRE/tests/test-framework.sh
36 init_test_env $@
37
38 init_logging
39
40 ALWAYS_EXCEPT="$SANITY_EXCEPT "
41 # bug number for skipped test: LU-9693 LU-6493 LU-9693 LU-11058
42 ALWAYS_EXCEPT+="                42a     42b     42c     77k"
43 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
44
45 # skipped tests: LU-8411 LU-9096 LU-9054 ..
46 ALWAYS_EXCEPT="  407     253     312     $ALWAYS_EXCEPT"
47
48 if $SHARED_KEY; then
49 # bug number for skipped tests: LU-9795 (all below)
50         ALWAYS_EXCEPT="$ALWAYS_EXCEPT   17n     60a     133g    300f"
51 fi
52
53 if [[ $(uname -m) = aarch64 ]]; then
54         # bug number:    LU-11596 (all below)
55         ALWAYS_EXCEPT+=" 42d 42e 63a 63b 64a 64b 64c"
56         # bug number:    LU-11671 LU-11665 LU-11594 LU-11667 LU-11729
57         ALWAYS_EXCEPT+=" 45       101c     103a     317      810"
58 fi
59
60 # skip nfs tests on kernels >= 4.14.0 until they are fixed
61 if [ $LINUX_VERSION_CODE -ge $(version_code 4.14.0) ]; then
62         # bug number:   LU-12661
63         ALWAYS_EXCEPT+=" 817"
64 fi
65
66 # skip cgroup tests on RHEL8.1 kernels until they are fixed
67 if (( $LINUX_VERSION_CODE >= $(version_code 4.18.0) &&
68       $LINUX_VERSION_CODE <  $(version_code 5.4.0) )); then
69         # bug number:   LU-13063
70         ALWAYS_EXCEPT+=" 411"
71 fi
72
73 #                                  5          12          (min)"
74 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 300o"
75
76 if [ "$mds1_FSTYPE" = "zfs" ]; then
77         # bug number for skipped test:
78         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  "
79         #                                               13    (min)"
80         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
81 fi
82
83 # Get the SLES distro version
84 #
85 # Returns a version string that should only be used in comparing
86 # strings returned by version_code()
87 sles_version_code()
88 {
89         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
90
91         # All SuSE Linux versions have one decimal. version_code expects two
92         local sles_version=$version.0
93         version_code $sles_version
94 }
95
96 # Check if we are running on Ubuntu or SLES so we can make decisions on
97 # what tests to run
98 if [ -r /etc/SuSE-release ]; then
99         sles_version=$(sles_version_code)
100         [ $sles_version -lt $(version_code 11.4.0) ] &&
101                 # bug number for skipped test: LU-4341
102                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
103         [ $sles_version -lt $(version_code 12.0.0) ] &&
104                 # bug number for skipped test: LU-3703
105                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
106 elif [ -r /etc/os-release ]; then
107         if grep -qi ubuntu /etc/os-release; then
108                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
109                                                 -e 's/^VERSION=//p' \
110                                                 /etc/os-release |
111                                                 awk '{ print $1 }'))
112
113                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
114                         # bug number for skipped test:
115                         #                LU-10334 LU-10366
116                         ALWAYS_EXCEPT+=" 103a     410"
117                 fi
118         fi
119 fi
120
121 build_test_filter
122 FAIL_ON_ERROR=false
123
124 cleanup() {
125         echo -n "cln.."
126         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
127         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
128 }
129 setup() {
130         echo -n "mnt.."
131         load_modules
132         setupall || exit 10
133         echo "done"
134 }
135
136 check_swap_layouts_support()
137 {
138         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
139                 skip "Does not support layout lock."
140 }
141
142 check_and_setup_lustre
143 DIR=${DIR:-$MOUNT}
144 assert_DIR
145
146 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
147
148 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
149 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
150 rm -rf $DIR/[Rdfs][0-9]*
151
152 # $RUNAS_ID may get set incorrectly somewhere else
153 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
154         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
155
156 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
157
158 if [ "${ONLY}" = "MOUNT" ] ; then
159         echo "Lustre is up, please go on"
160         exit
161 fi
162
163 echo "preparing for tests involving mounts"
164 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
165 touch $EXT2_DEV
166 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
167 echo # add a newline after mke2fs.
168
169 umask 077
170
171 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
172 lctl set_param debug=-1 2> /dev/null || true
173 test_0a() {
174         touch $DIR/$tfile
175         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
176         rm $DIR/$tfile
177         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
178 }
179 run_test 0a "touch; rm ====================="
180
181 test_0b() {
182         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
183         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
184 }
185 run_test 0b "chmod 0755 $DIR ============================="
186
187 test_0c() {
188         $LCTL get_param mdc.*.import | grep "state: FULL" ||
189                 error "import not FULL"
190         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
191                 error "bad target"
192 }
193 run_test 0c "check import proc"
194
195 test_0d() { # LU-3397
196         [ $MGS_VERSION -lt $(version_code 2.10.57) ] &&
197                 skip "proc exports not supported before 2.10.57"
198
199         local mgs_exp="mgs.MGS.exports"
200         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
201         local exp_client_nid
202         local exp_client_version
203         local exp_val
204         local imp_val
205         local temp_imp=$DIR/$tfile.import
206         local temp_exp=$DIR/$tfile.export
207
208         # save mgc import file to $temp_imp
209         $LCTL get_param mgc.*.import | tee $temp_imp
210         # Check if client uuid is found in MGS export
211         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
212                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
213                         $client_uuid ] &&
214                         break;
215         done
216         # save mgs export file to $temp_exp
217         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
218
219         # Compare the value of field "connect_flags"
220         imp_val=$(grep "connect_flags" $temp_imp)
221         exp_val=$(grep "connect_flags" $temp_exp)
222         [ "$exp_val" == "$imp_val" ] ||
223                 error "export flags '$exp_val' != import flags '$imp_val'"
224
225         # Compare the value of client version
226         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
227         exp_val=$(version_code $exp_client_version)
228         imp_val=$CLIENT_VERSION
229         [ "$exp_val" == "$imp_val" ] ||
230                 error "export client version '$exp_val' != '$imp_val'"
231 }
232 run_test 0d "check export proc ============================="
233
234 test_1() {
235         test_mkdir $DIR/$tdir
236         test_mkdir $DIR/$tdir/d2
237         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
238         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
239         rmdir $DIR/$tdir/d2
240         rmdir $DIR/$tdir
241         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
242 }
243 run_test 1 "mkdir; remkdir; rmdir"
244
245 test_2() {
246         test_mkdir $DIR/$tdir
247         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
248         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
249         rm -r $DIR/$tdir
250         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
251 }
252 run_test 2 "mkdir; touch; rmdir; check file"
253
254 test_3() {
255         test_mkdir $DIR/$tdir
256         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
257         touch $DIR/$tdir/$tfile
258         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
259         rm -r $DIR/$tdir
260         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
261 }
262 run_test 3 "mkdir; touch; rmdir; check dir"
263
264 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
265 test_4() {
266         test_mkdir -i 1 $DIR/$tdir
267
268         touch $DIR/$tdir/$tfile ||
269                 error "Create file under remote directory failed"
270
271         rmdir $DIR/$tdir &&
272                 error "Expect error removing in-use dir $DIR/$tdir"
273
274         test -d $DIR/$tdir || error "Remote directory disappeared"
275
276         rm -rf $DIR/$tdir || error "remove remote dir error"
277 }
278 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
279
280 test_5() {
281         test_mkdir $DIR/$tdir
282         test_mkdir $DIR/$tdir/d2
283         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
284         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
285         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
286 }
287 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
288
289 test_6a() {
290         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
291         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
292         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
293                 error "$tfile does not have perm 0666 or UID $UID"
294         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
295         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
296                 error "$tfile should be 0666 and owned by UID $UID"
297 }
298 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
299
300 test_6c() {
301         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
302
303         touch $DIR/$tfile
304         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
305         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
306                 error "$tfile should be owned by UID $RUNAS_ID"
307         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
308         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
309                 error "$tfile should be owned by UID $RUNAS_ID"
310 }
311 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
312
313 test_6e() {
314         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
315
316         touch $DIR/$tfile
317         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
318         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
319                 error "$tfile should be owned by GID $UID"
320         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
321         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
322                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
323 }
324 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
325
326 test_6g() {
327         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
328
329         test_mkdir $DIR/$tdir
330         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
331         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
332         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
333         test_mkdir $DIR/$tdir/d/subdir
334         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
335                 error "$tdir/d/subdir should be GID $RUNAS_GID"
336         if [[ $MDSCOUNT -gt 1 ]]; then
337                 # check remote dir sgid inherite
338                 $LFS mkdir -i 0 $DIR/$tdir.local ||
339                         error "mkdir $tdir.local failed"
340                 chmod g+s $DIR/$tdir.local ||
341                         error "chmod $tdir.local failed"
342                 chgrp $RUNAS_GID $DIR/$tdir.local ||
343                         error "chgrp $tdir.local failed"
344                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
345                         error "mkdir $tdir.remote failed"
346                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
347                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
348                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
349                         error "$tdir.remote should be mode 02755"
350         fi
351 }
352 run_test 6g "verify new dir in sgid dir inherits group"
353
354 test_6h() { # bug 7331
355         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
356
357         touch $DIR/$tfile || error "touch failed"
358         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
359         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
360                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
361         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
362                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
363 }
364 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
365
366 test_7a() {
367         test_mkdir $DIR/$tdir
368         $MCREATE $DIR/$tdir/$tfile
369         chmod 0666 $DIR/$tdir/$tfile
370         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
371                 error "$tdir/$tfile should be mode 0666"
372 }
373 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
374
375 test_7b() {
376         if [ ! -d $DIR/$tdir ]; then
377                 test_mkdir $DIR/$tdir
378         fi
379         $MCREATE $DIR/$tdir/$tfile
380         echo -n foo > $DIR/$tdir/$tfile
381         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
382         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
383 }
384 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
385
386 test_8() {
387         test_mkdir $DIR/$tdir
388         touch $DIR/$tdir/$tfile
389         chmod 0666 $DIR/$tdir/$tfile
390         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
391                 error "$tfile mode not 0666"
392 }
393 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
394
395 test_9() {
396         test_mkdir $DIR/$tdir
397         test_mkdir $DIR/$tdir/d2
398         test_mkdir $DIR/$tdir/d2/d3
399         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
400 }
401 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
402
403 test_10() {
404         test_mkdir $DIR/$tdir
405         test_mkdir $DIR/$tdir/d2
406         touch $DIR/$tdir/d2/$tfile
407         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
408                 error "$tdir/d2/$tfile not a file"
409 }
410 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
411
412 test_11() {
413         test_mkdir $DIR/$tdir
414         test_mkdir $DIR/$tdir/d2
415         chmod 0666 $DIR/$tdir/d2
416         chmod 0705 $DIR/$tdir/d2
417         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
418                 error "$tdir/d2 mode not 0705"
419 }
420 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
421
422 test_12() {
423         test_mkdir $DIR/$tdir
424         touch $DIR/$tdir/$tfile
425         chmod 0666 $DIR/$tdir/$tfile
426         chmod 0654 $DIR/$tdir/$tfile
427         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
428                 error "$tdir/d2 mode not 0654"
429 }
430 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
431
432 test_13() {
433         test_mkdir $DIR/$tdir
434         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
435         >  $DIR/$tdir/$tfile
436         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
437                 error "$tdir/$tfile size not 0 after truncate"
438 }
439 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
440
441 test_14() {
442         test_mkdir $DIR/$tdir
443         touch $DIR/$tdir/$tfile
444         rm $DIR/$tdir/$tfile
445         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
446 }
447 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
448
449 test_15() {
450         test_mkdir $DIR/$tdir
451         touch $DIR/$tdir/$tfile
452         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
453         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
454                 error "$tdir/${tfile_2} not a file after rename"
455         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
456 }
457 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
458
459 test_16() {
460         test_mkdir $DIR/$tdir
461         touch $DIR/$tdir/$tfile
462         rm -rf $DIR/$tdir/$tfile
463         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
464 }
465 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
466
467 test_17a() {
468         test_mkdir $DIR/$tdir
469         touch $DIR/$tdir/$tfile
470         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
471         ls -l $DIR/$tdir
472         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
473                 error "$tdir/l-exist not a symlink"
474         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
475                 error "$tdir/l-exist not referencing a file"
476         rm -f $DIR/$tdir/l-exist
477         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
478 }
479 run_test 17a "symlinks: create, remove (real)"
480
481 test_17b() {
482         test_mkdir $DIR/$tdir
483         ln -s no-such-file $DIR/$tdir/l-dangle
484         ls -l $DIR/$tdir
485         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
486                 error "$tdir/l-dangle not referencing no-such-file"
487         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
488                 error "$tdir/l-dangle not referencing non-existent file"
489         rm -f $DIR/$tdir/l-dangle
490         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
491 }
492 run_test 17b "symlinks: create, remove (dangling)"
493
494 test_17c() { # bug 3440 - don't save failed open RPC for replay
495         test_mkdir $DIR/$tdir
496         ln -s foo $DIR/$tdir/$tfile
497         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
498 }
499 run_test 17c "symlinks: open dangling (should return error)"
500
501 test_17d() {
502         test_mkdir $DIR/$tdir
503         ln -s foo $DIR/$tdir/$tfile
504         touch $DIR/$tdir/$tfile || error "creating to new symlink"
505 }
506 run_test 17d "symlinks: create dangling"
507
508 test_17e() {
509         test_mkdir $DIR/$tdir
510         local foo=$DIR/$tdir/$tfile
511         ln -s $foo $foo || error "create symlink failed"
512         ls -l $foo || error "ls -l failed"
513         ls $foo && error "ls not failed" || true
514 }
515 run_test 17e "symlinks: create recursive symlink (should return error)"
516
517 test_17f() {
518         test_mkdir $DIR/$tdir
519         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
520         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
521         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
522         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
523         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
524         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
525         ls -l  $DIR/$tdir
526 }
527 run_test 17f "symlinks: long and very long symlink name"
528
529 # str_repeat(S, N) generate a string that is string S repeated N times
530 str_repeat() {
531         local s=$1
532         local n=$2
533         local ret=''
534         while [ $((n -= 1)) -ge 0 ]; do
535                 ret=$ret$s
536         done
537         echo $ret
538 }
539
540 # Long symlinks and LU-2241
541 test_17g() {
542         test_mkdir $DIR/$tdir
543         local TESTS="59 60 61 4094 4095"
544
545         # Fix for inode size boundary in 2.1.4
546         [ $MDS1_VERSION -lt $(version_code 2.1.4) ] &&
547                 TESTS="4094 4095"
548
549         # Patch not applied to 2.2 or 2.3 branches
550         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
551         [ $MDS1_VERSION -le $(version_code 2.3.55) ] &&
552                 TESTS="4094 4095"
553
554         # skip long symlink name for rhel6.5.
555         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
556         grep -q '6.5' /etc/redhat-release &>/dev/null &&
557                 TESTS="59 60 61 4062 4063"
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" -o -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 -o $num_uniq -ne $nrfiles -o \
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 -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1324                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1325         fi
1326
1327         simple_cleanup_common || error "Can not delete directories"
1328 }
1329 run_test 24A "readdir() returns correct number of entries."
1330
1331 test_24B() { # LU-4805
1332         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1333
1334         local count
1335
1336         test_mkdir $DIR/$tdir
1337         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1338                 error "create striped dir failed"
1339
1340         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1341         [ $count -eq 2 ] || error "Expected 2, got $count"
1342
1343         touch $DIR/$tdir/striped_dir/a
1344
1345         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1346         [ $count -eq 3 ] || error "Expected 3, got $count"
1347
1348         touch $DIR/$tdir/striped_dir/.f
1349
1350         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1351         [ $count -eq 4 ] || error "Expected 4, got $count"
1352
1353         rm -rf $DIR/$tdir || error "Can not delete directories"
1354 }
1355 run_test 24B "readdir for striped dir return correct number of entries"
1356
1357 test_24C() {
1358         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1359
1360         mkdir $DIR/$tdir
1361         mkdir $DIR/$tdir/d0
1362         mkdir $DIR/$tdir/d1
1363
1364         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1365                 error "create striped dir failed"
1366
1367         cd $DIR/$tdir/d0/striped_dir
1368
1369         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1370         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1371         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1372
1373         [ "$d0_ino" = "$parent_ino" ] ||
1374                 error ".. wrong, expect $d0_ino, get $parent_ino"
1375
1376         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1377                 error "mv striped dir failed"
1378
1379         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1380
1381         [ "$d1_ino" = "$parent_ino" ] ||
1382                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1383 }
1384 run_test 24C "check .. in striped dir"
1385
1386 test_24E() {
1387         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1388         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1389
1390         mkdir -p $DIR/$tdir
1391         mkdir $DIR/$tdir/src_dir
1392         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1393                 error "create remote source failed"
1394
1395         touch $DIR/$tdir/src_dir/src_child/a
1396
1397         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1398                 error "create remote target dir failed"
1399
1400         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1401                 error "create remote target child failed"
1402
1403         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1404                 error "rename dir cross MDT failed!"
1405
1406         find $DIR/$tdir
1407
1408         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1409                 error "src_child still exists after rename"
1410
1411         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1412                 error "missing file(a) after rename"
1413
1414         rm -rf $DIR/$tdir || error "Can not delete directories"
1415 }
1416 run_test 24E "cross MDT rename/link"
1417
1418 test_24F () {
1419         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
1420
1421         local repeats=1000
1422         [ "$SLOW" = "no" ] && repeats=100
1423
1424         mkdir -p $DIR/$tdir
1425
1426         echo "$repeats repeats"
1427         for ((i = 0; i < repeats; i++)); do
1428                 $LFS mkdir -i0 -c2 $DIR/$tdir/test || error "mkdir fails"
1429                 touch $DIR/$tdir/test/a || error "touch fails"
1430                 mkdir $DIR/$tdir/test/b || error "mkdir fails"
1431                 rm -rf $DIR/$tdir/test || error "rmdir fails"
1432         done
1433
1434         true
1435 }
1436 run_test 24F "hash order vs readdir (LU-11330)"
1437
1438 test_25a() {
1439         echo '== symlink sanity ============================================='
1440
1441         test_mkdir $DIR/d25
1442         ln -s d25 $DIR/s25
1443         touch $DIR/s25/foo ||
1444                 error "File creation in symlinked directory failed"
1445 }
1446 run_test 25a "create file in symlinked directory ==============="
1447
1448 test_25b() {
1449         [ ! -d $DIR/d25 ] && test_25a
1450         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1451 }
1452 run_test 25b "lookup file in symlinked directory ==============="
1453
1454 test_26a() {
1455         test_mkdir $DIR/d26
1456         test_mkdir $DIR/d26/d26-2
1457         ln -s d26/d26-2 $DIR/s26
1458         touch $DIR/s26/foo || error "File creation failed"
1459 }
1460 run_test 26a "multiple component symlink ======================="
1461
1462 test_26b() {
1463         test_mkdir -p $DIR/$tdir/d26-2
1464         ln -s $tdir/d26-2/foo $DIR/s26-2
1465         touch $DIR/s26-2 || error "File creation failed"
1466 }
1467 run_test 26b "multiple component symlink at end of lookup ======"
1468
1469 test_26c() {
1470         test_mkdir $DIR/d26.2
1471         touch $DIR/d26.2/foo
1472         ln -s d26.2 $DIR/s26.2-1
1473         ln -s s26.2-1 $DIR/s26.2-2
1474         ln -s s26.2-2 $DIR/s26.2-3
1475         chmod 0666 $DIR/s26.2-3/foo
1476 }
1477 run_test 26c "chain of symlinks"
1478
1479 # recursive symlinks (bug 439)
1480 test_26d() {
1481         ln -s d26-3/foo $DIR/d26-3
1482 }
1483 run_test 26d "create multiple component recursive symlink"
1484
1485 test_26e() {
1486         [ ! -h $DIR/d26-3 ] && test_26d
1487         rm $DIR/d26-3
1488 }
1489 run_test 26e "unlink multiple component recursive symlink"
1490
1491 # recursive symlinks (bug 7022)
1492 test_26f() {
1493         test_mkdir $DIR/$tdir
1494         test_mkdir $DIR/$tdir/$tfile
1495         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1496         test_mkdir -p lndir/bar1
1497         test_mkdir $DIR/$tdir/$tfile/$tfile
1498         cd $tfile                || error "cd $tfile failed"
1499         ln -s .. dotdot          || error "ln dotdot failed"
1500         ln -s dotdot/lndir lndir || error "ln lndir failed"
1501         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1502         output=`ls $tfile/$tfile/lndir/bar1`
1503         [ "$output" = bar1 ] && error "unexpected output"
1504         rm -r $tfile             || error "rm $tfile failed"
1505         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1506 }
1507 run_test 26f "rm -r of a directory which has recursive symlink"
1508
1509 test_27a() {
1510         test_mkdir $DIR/$tdir
1511         $LFS getstripe $DIR/$tdir
1512         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1513         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1514         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1515 }
1516 run_test 27a "one stripe file"
1517
1518 test_27b() {
1519         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1520
1521         test_mkdir $DIR/$tdir
1522         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1523         $LFS getstripe -c $DIR/$tdir/$tfile
1524         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1525                 error "two-stripe file doesn't have two stripes"
1526
1527         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1528 }
1529 run_test 27b "create and write to two stripe file"
1530
1531 test_27d() {
1532         test_mkdir $DIR/$tdir
1533         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1534                 error "setstripe failed"
1535         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1536         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1537 }
1538 run_test 27d "create file with default settings"
1539
1540 test_27e() {
1541         # LU-5839 adds check for existed layout before setting it
1542         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1543                 skip "Need MDS version at least 2.7.56"
1544
1545         test_mkdir $DIR/$tdir
1546         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1547         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1548         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1549 }
1550 run_test 27e "setstripe existing file (should return error)"
1551
1552 test_27f() {
1553         test_mkdir $DIR/$tdir
1554         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1555                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1556         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1557                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1558         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1559         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1560 }
1561 run_test 27f "setstripe with bad stripe size (should return error)"
1562
1563 test_27g() {
1564         test_mkdir $DIR/$tdir
1565         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1566         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1567                 error "$DIR/$tdir/$tfile has object"
1568 }
1569 run_test 27g "$LFS getstripe with no objects"
1570
1571 test_27ga() {
1572         test_mkdir $DIR/$tdir
1573         touch $DIR/$tdir/$tfile || error "touch failed"
1574         ln -s bogus $DIR/$tdir/$tfile.2 || error "ln failed"
1575         $LFS getstripe -m $DIR/$tdir/$tfile $DIR/$tdir/$tfile.2
1576         local rc=$?
1577         (( rc == 2 )) || error "getstripe did not return ENOENT"
1578 }
1579 run_test 27ga "$LFS getstripe with missing file (should return error)"
1580
1581 test_27i() {
1582         test_mkdir $DIR/$tdir
1583         touch $DIR/$tdir/$tfile || error "touch failed"
1584         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1585                 error "missing objects"
1586 }
1587 run_test 27i "$LFS getstripe with some objects"
1588
1589 test_27j() {
1590         test_mkdir $DIR/$tdir
1591         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1592                 error "setstripe failed" || true
1593 }
1594 run_test 27j "setstripe with bad stripe offset (should return error)"
1595
1596 test_27k() { # bug 2844
1597         test_mkdir $DIR/$tdir
1598         local file=$DIR/$tdir/$tfile
1599         local ll_max_blksize=$((4 * 1024 * 1024))
1600         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1601         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1602         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1603         dd if=/dev/zero of=$file bs=4k count=1
1604         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1605         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1606 }
1607 run_test 27k "limit i_blksize for broken user apps"
1608
1609 test_27l() {
1610         mcreate $DIR/$tfile || error "creating file"
1611         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1612                 error "setstripe should have failed" || true
1613 }
1614 run_test 27l "check setstripe permissions (should return error)"
1615
1616 test_27m() {
1617         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1618
1619         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1620                    head -n1)
1621         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1622                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1623         fi
1624         trap simple_cleanup_common EXIT
1625         test_mkdir $DIR/$tdir
1626         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1627         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1628                 error "dd should fill OST0"
1629         i=2
1630         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1631                 i=$((i + 1))
1632                 [ $i -gt 256 ] && break
1633         done
1634         i=$((i + 1))
1635         touch $DIR/$tdir/$tfile.$i
1636         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1637             awk '{print $1}'| grep -w "0") ] &&
1638                 error "OST0 was full but new created file still use it"
1639         i=$((i + 1))
1640         touch $DIR/$tdir/$tfile.$i
1641         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1642             awk '{print $1}'| grep -w "0") ] &&
1643                 error "OST0 was full but new created file still use it"
1644         simple_cleanup_common
1645 }
1646 run_test 27m "create file while OST0 was full"
1647
1648 sleep_maxage() {
1649         local delay=$(do_facet $SINGLEMDS lctl get_param -n lo[vd].*.qos_maxage |
1650                       awk '{ print $1 * 2; exit; }')
1651         sleep $delay
1652 }
1653
1654 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1655 # if the OST isn't full anymore.
1656 reset_enospc() {
1657         local OSTIDX=${1:-""}
1658
1659         local list=$(comma_list $(osts_nodes))
1660         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1661
1662         do_nodes $list lctl set_param fail_loc=0
1663         sync    # initiate all OST_DESTROYs from MDS to OST
1664         sleep_maxage
1665 }
1666
1667 exhaust_precreations() {
1668         local OSTIDX=$1
1669         local FAILLOC=$2
1670         local FAILIDX=${3:-$OSTIDX}
1671         local ofacet=ost$((OSTIDX + 1))
1672
1673         test_mkdir -p -c1 $DIR/$tdir
1674         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1675         local mfacet=mds$((mdtidx + 1))
1676         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1677
1678         local OST=$(ostname_from_index $OSTIDX)
1679
1680         # on the mdt's osc
1681         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1682         local last_id=$(do_facet $mfacet lctl get_param -n \
1683                         osp.$mdtosc_proc1.prealloc_last_id)
1684         local next_id=$(do_facet $mfacet lctl get_param -n \
1685                         osp.$mdtosc_proc1.prealloc_next_id)
1686
1687         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1688         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1689
1690         test_mkdir -p $DIR/$tdir/${OST}
1691         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1692 #define OBD_FAIL_OST_ENOSPC              0x215
1693         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1694         echo "Creating to objid $last_id on ost $OST..."
1695         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1696         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1697         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1698         sleep_maxage
1699 }
1700
1701 exhaust_all_precreations() {
1702         local i
1703         for (( i=0; i < OSTCOUNT; i++ )) ; do
1704                 exhaust_precreations $i $1 -1
1705         done
1706 }
1707
1708 test_27n() {
1709         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1710         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1711         remote_mds_nodsh && skip "remote MDS with nodsh"
1712         remote_ost_nodsh && skip "remote OST with nodsh"
1713
1714         reset_enospc
1715         rm -f $DIR/$tdir/$tfile
1716         exhaust_precreations 0 0x80000215
1717         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1718         touch $DIR/$tdir/$tfile || error "touch failed"
1719         $LFS getstripe $DIR/$tdir/$tfile
1720         reset_enospc
1721 }
1722 run_test 27n "create file with some full OSTs"
1723
1724 test_27o() {
1725         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1726         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1727         remote_mds_nodsh && skip "remote MDS with nodsh"
1728         remote_ost_nodsh && skip "remote OST with nodsh"
1729
1730         reset_enospc
1731         rm -f $DIR/$tdir/$tfile
1732         exhaust_all_precreations 0x215
1733
1734         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1735
1736         reset_enospc
1737         rm -rf $DIR/$tdir/*
1738 }
1739 run_test 27o "create file with all full OSTs (should error)"
1740
1741 function create_and_checktime() {
1742         local fname=$1
1743         local loops=$2
1744         local i
1745
1746         for ((i=0; i < $loops; i++)); do
1747                 local start=$SECONDS
1748                 multiop $fname-$i Oc
1749                 ((SECONDS-start < TIMEOUT)) ||
1750                         error "creation took " $((SECONDS-$start)) && return 1
1751         done
1752 }
1753
1754 test_27oo() {
1755         local mdts=$(comma_list $(mdts_nodes))
1756
1757         [ $MDS1_VERSION -lt $(version_code 2.12.6) ] &&
1758                 skip "Need MDS version at least 2.12.6"
1759
1760         local f0=$DIR/${tfile}-0
1761         local f1=$DIR/${tfile}-1
1762
1763         wait_delete_completed
1764
1765         # refill precreated objects
1766         $LFS setstripe -i0 -c1 $f0
1767
1768         saved=$(do_facet mds1 $LCTL get_param -n lov.*0000*.qos_threshold_rr)
1769         # force QoS allocation policy
1770         do_nodes $mdts $LCTL set_param lov.*.qos_threshold_rr=0%
1771         stack_trap "do_nodes $mdts $LCTL set_param \
1772                 lov.*.qos_threshold_rr=$saved" EXIT
1773         sleep_maxage
1774
1775         # one OST is unavailable, but still have few objects preallocated
1776         stop ost1
1777         stack_trap "start ost1 $(ostdevname 1) $OST_MOUNT_OPTS; \
1778                 rm -rf $f1 $DIR/$tdir*" EXIT
1779
1780         for ((i=0; i < 7; i++)); do
1781                 mkdir $DIR/$tdir$i || error "can't create dir"
1782                 $LFS setstripe -c$((OSTCOUNT-1)) $DIR/$tdir$i ||
1783                         error "can't set striping"
1784         done
1785         for ((i=0; i < 7; i++)); do
1786                 create_and_checktime $DIR/$tdir$i/$tfile 100 &
1787         done
1788         wait
1789 }
1790 run_test 27oo "don't let few threads to reserve too many objects"
1791
1792 test_27p() {
1793         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1794         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1795         remote_mds_nodsh && skip "remote MDS with nodsh"
1796         remote_ost_nodsh && skip "remote OST with nodsh"
1797
1798         reset_enospc
1799         rm -f $DIR/$tdir/$tfile
1800         test_mkdir $DIR/$tdir
1801
1802         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1803         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1804         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1805
1806         exhaust_precreations 0 0x80000215
1807         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1808         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1809         $LFS getstripe $DIR/$tdir/$tfile
1810
1811         reset_enospc
1812 }
1813 run_test 27p "append to a truncated file with some full OSTs"
1814
1815 test_27q() {
1816         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1817         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1818         remote_mds_nodsh && skip "remote MDS with nodsh"
1819         remote_ost_nodsh && skip "remote OST with nodsh"
1820
1821         reset_enospc
1822         rm -f $DIR/$tdir/$tfile
1823
1824         test_mkdir $DIR/$tdir
1825         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1826         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1827                 error "truncate $DIR/$tdir/$tfile failed"
1828         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1829
1830         exhaust_all_precreations 0x215
1831
1832         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1833         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1834
1835         reset_enospc
1836 }
1837 run_test 27q "append to truncated file with all OSTs full (should error)"
1838
1839 test_27r() {
1840         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1841         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1842         remote_mds_nodsh && skip "remote MDS with nodsh"
1843         remote_ost_nodsh && skip "remote OST with nodsh"
1844
1845         reset_enospc
1846         rm -f $DIR/$tdir/$tfile
1847         exhaust_precreations 0 0x80000215
1848
1849         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1850
1851         reset_enospc
1852 }
1853 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1854
1855 test_27s() { # bug 10725
1856         test_mkdir $DIR/$tdir
1857         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1858         local stripe_count=0
1859         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1860         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1861                 error "stripe width >= 2^32 succeeded" || true
1862
1863 }
1864 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1865
1866 test_27t() { # bug 10864
1867         WDIR=$(pwd)
1868         WLFS=$(which lfs)
1869         cd $DIR
1870         touch $tfile
1871         $WLFS getstripe $tfile
1872         cd $WDIR
1873 }
1874 run_test 27t "check that utils parse path correctly"
1875
1876 test_27u() { # bug 4900
1877         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1878         remote_mds_nodsh && skip "remote MDS with nodsh"
1879
1880         local index
1881         local list=$(comma_list $(mdts_nodes))
1882
1883 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1884         do_nodes $list $LCTL set_param fail_loc=0x139
1885         test_mkdir -p $DIR/$tdir
1886         trap simple_cleanup_common EXIT
1887         createmany -o $DIR/$tdir/t- 1000
1888         do_nodes $list $LCTL set_param fail_loc=0
1889
1890         TLOG=$TMP/$tfile.getstripe
1891         $LFS getstripe $DIR/$tdir > $TLOG
1892         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1893         unlinkmany $DIR/$tdir/t- 1000
1894         trap 0
1895         [[ $OBJS -gt 0 ]] &&
1896                 error "$OBJS objects created on OST-0. See $TLOG" ||
1897                 rm -f $TLOG
1898 }
1899 run_test 27u "skip object creation on OSC w/o objects"
1900
1901 test_27v() { # bug 4900
1902         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1903         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1904         remote_mds_nodsh && skip "remote MDS with nodsh"
1905         remote_ost_nodsh && skip "remote OST with nodsh"
1906
1907         exhaust_all_precreations 0x215
1908         reset_enospc
1909
1910         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1911
1912         touch $DIR/$tdir/$tfile
1913         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1914         # all except ost1
1915         for (( i=1; i < OSTCOUNT; i++ )); do
1916                 do_facet ost$i lctl set_param fail_loc=0x705
1917         done
1918         local START=`date +%s`
1919         createmany -o $DIR/$tdir/$tfile 32
1920
1921         local FINISH=`date +%s`
1922         local TIMEOUT=`lctl get_param -n timeout`
1923         local PROCESS=$((FINISH - START))
1924         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1925                error "$FINISH - $START >= $TIMEOUT / 2"
1926         sleep $((TIMEOUT / 2 - PROCESS))
1927         reset_enospc
1928 }
1929 run_test 27v "skip object creation on slow OST"
1930
1931 test_27w() { # bug 10997
1932         test_mkdir $DIR/$tdir
1933         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1934         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1935                 error "stripe size $size != 65536" || true
1936         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1937                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1938 }
1939 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1940
1941 test_27wa() {
1942         [[ $OSTCOUNT -lt 2 ]] &&
1943                 skip_env "skipping multiple stripe count/offset test"
1944
1945         test_mkdir $DIR/$tdir
1946         for i in $(seq 1 $OSTCOUNT); do
1947                 offset=$((i - 1))
1948                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
1949                         error "setstripe -c $i -i $offset failed"
1950                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
1951                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
1952                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1953                 [ $index -ne $offset ] &&
1954                         error "stripe offset $index != $offset" || true
1955         done
1956 }
1957 run_test 27wa "check $LFS setstripe -c -i options"
1958
1959 test_27x() {
1960         remote_ost_nodsh && skip "remote OST with nodsh"
1961         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1962         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1963
1964         OFFSET=$(($OSTCOUNT - 1))
1965         OSTIDX=0
1966         local OST=$(ostname_from_index $OSTIDX)
1967
1968         test_mkdir $DIR/$tdir
1969         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
1970         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1971         sleep_maxage
1972         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1973         for i in $(seq 0 $OFFSET); do
1974                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
1975                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
1976                 error "OST0 was degraded but new created file still use it"
1977         done
1978         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1979 }
1980 run_test 27x "create files while OST0 is degraded"
1981
1982 test_27y() {
1983         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1984         remote_mds_nodsh && skip "remote MDS with nodsh"
1985         remote_ost_nodsh && skip "remote OST with nodsh"
1986         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1987
1988         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1989         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1990                 osp.$mdtosc.prealloc_last_id)
1991         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1992                 osp.$mdtosc.prealloc_next_id)
1993         local fcount=$((last_id - next_id))
1994         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
1995         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1996
1997         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1998                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1999         local OST_DEACTIVE_IDX=-1
2000         local OSC
2001         local OSTIDX
2002         local OST
2003
2004         for OSC in $MDS_OSCS; do
2005                 OST=$(osc_to_ost $OSC)
2006                 OSTIDX=$(index_from_ostuuid $OST)
2007                 if [ $OST_DEACTIVE_IDX == -1 ]; then
2008                         OST_DEACTIVE_IDX=$OSTIDX
2009                 fi
2010                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
2011                         echo $OSC "is Deactivated:"
2012                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
2013                 fi
2014         done
2015
2016         OSTIDX=$(index_from_ostuuid $OST)
2017         test_mkdir $DIR/$tdir
2018         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
2019
2020         for OSC in $MDS_OSCS; do
2021                 OST=$(osc_to_ost $OSC)
2022                 OSTIDX=$(index_from_ostuuid $OST)
2023                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2024                         echo $OST "is degraded:"
2025                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2026                                                 obdfilter.$OST.degraded=1
2027                 fi
2028         done
2029
2030         sleep_maxage
2031         createmany -o $DIR/$tdir/$tfile $fcount
2032
2033         for OSC in $MDS_OSCS; do
2034                 OST=$(osc_to_ost $OSC)
2035                 OSTIDX=$(index_from_ostuuid $OST)
2036                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2037                         echo $OST "is recovered from degraded:"
2038                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2039                                                 obdfilter.$OST.degraded=0
2040                 else
2041                         do_facet $SINGLEMDS lctl --device %$OSC activate
2042                 fi
2043         done
2044
2045         # all osp devices get activated, hence -1 stripe count restored
2046         local stripe_count=0
2047
2048         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
2049         # devices get activated.
2050         sleep_maxage
2051         $LFS setstripe -c -1 $DIR/$tfile
2052         stripe_count=$($LFS getstripe -c $DIR/$tfile)
2053         rm -f $DIR/$tfile
2054         [ $stripe_count -ne $OSTCOUNT ] &&
2055                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
2056         return 0
2057 }
2058 run_test 27y "create files while OST0 is degraded and the rest inactive"
2059
2060 check_seq_oid()
2061 {
2062         log "check file $1"
2063
2064         lmm_count=$($GETSTRIPE -c $1)
2065         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
2066         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
2067
2068         local old_ifs="$IFS"
2069         IFS=$'[:]'
2070         fid=($($LFS path2fid $1))
2071         IFS="$old_ifs"
2072
2073         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
2074         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
2075
2076         # compare lmm_seq and lu_fid->f_seq
2077         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
2078         # compare lmm_object_id and lu_fid->oid
2079         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
2080
2081         # check the trusted.fid attribute of the OST objects of the file
2082         local have_obdidx=false
2083         local stripe_nr=0
2084         $GETSTRIPE $1 | while read obdidx oid hex seq; do
2085                 # skip lines up to and including "obdidx"
2086                 [ -z "$obdidx" ] && break
2087                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
2088                 $have_obdidx || continue
2089
2090                 local ost=$((obdidx + 1))
2091                 local dev=$(ostdevname $ost)
2092                 local oid_hex
2093
2094                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
2095
2096                 seq=$(echo $seq | sed -e "s/^0x//g")
2097                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
2098                         oid_hex=$(echo $oid)
2099                 else
2100                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
2101                 fi
2102                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
2103
2104                 local ff=""
2105                 #
2106                 # Don't unmount/remount the OSTs if we don't need to do that.
2107                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2108                 # update too, until that use mount/ll_decode_filter_fid/mount.
2109                 # Re-enable when debugfs will understand new filter_fid.
2110                 #
2111                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2112                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2113                                 $dev 2>/dev/null" | grep "parent=")
2114                 fi
2115                 if [ -z "$ff" ]; then
2116                         stop ost$ost
2117                         mount_fstype ost$ost
2118                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2119                                 $(facet_mntpt ost$ost)/$obj_file)
2120                         unmount_fstype ost$ost
2121                         start ost$ost $dev $OST_MOUNT_OPTS
2122                         clients_up
2123                 fi
2124
2125                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2126
2127                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2128
2129                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2130                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2131                 #
2132                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2133                 #       stripe_size=1048576 component_id=1 component_start=0 \
2134                 #       component_end=33554432
2135                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2136                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2137                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2138                 local ff_pstripe
2139                 if grep -q 'stripe=' <<<$ff; then
2140                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2141                 else
2142                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2143                         # into f_ver in this case.  See comment on ff_parent.
2144                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2145                 fi
2146
2147                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2148                 [ $ff_pseq = $lmm_seq ] ||
2149                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2150                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2151                 [ $ff_poid = $lmm_oid ] ||
2152                         error "FF parent OID $ff_poid != $lmm_oid"
2153                 (($ff_pstripe == $stripe_nr)) ||
2154                         error "FF stripe $ff_pstripe != $stripe_nr"
2155
2156                 stripe_nr=$((stripe_nr + 1))
2157                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2158                         continue
2159                 if grep -q 'stripe_count=' <<<$ff; then
2160                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2161                                             -e 's/ .*//' <<<$ff)
2162                         [ $lmm_count = $ff_scnt ] ||
2163                                 error "FF stripe count $lmm_count != $ff_scnt"
2164                 fi
2165         done
2166 }
2167
2168 test_27z() {
2169         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2170         remote_ost_nodsh && skip "remote OST with nodsh"
2171
2172         test_mkdir $DIR/$tdir
2173         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2174                 { error "setstripe -c -1 failed"; return 1; }
2175         # We need to send a write to every object to get parent FID info set.
2176         # This _should_ also work for setattr, but does not currently.
2177         # touch $DIR/$tdir/$tfile-1 ||
2178         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2179                 { error "dd $tfile-1 failed"; return 2; }
2180         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2181                 { error "setstripe -c -1 failed"; return 3; }
2182         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2183                 { error "dd $tfile-2 failed"; return 4; }
2184
2185         # make sure write RPCs have been sent to OSTs
2186         sync; sleep 5; sync
2187
2188         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2189         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2190 }
2191 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2192
2193 test_27A() { # b=19102
2194         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2195
2196         save_layout_restore_at_exit $MOUNT
2197         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2198         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2199                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
2200         local default_size=$($GETSTRIPE -S $MOUNT)
2201         local default_offset=$($GETSTRIPE -i $MOUNT)
2202         local dsize=$(do_facet $SINGLEMDS \
2203                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2204         [ $default_size -eq $dsize ] ||
2205                 error "stripe size $default_size != $dsize"
2206         [ $default_offset -eq -1 ] ||
2207                 error "stripe offset $default_offset != -1"
2208 }
2209 run_test 27A "check filesystem-wide default LOV EA values"
2210
2211 test_27B() { # LU-2523
2212         test_mkdir $DIR/$tdir
2213         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2214         touch $DIR/$tdir/f0
2215         # open f1 with O_LOV_DELAY_CREATE
2216         # rename f0 onto f1
2217         # call setstripe ioctl on open file descriptor for f1
2218         # close
2219         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2220                 $DIR/$tdir/f0
2221
2222         rm -f $DIR/$tdir/f1
2223         # open f1 with O_LOV_DELAY_CREATE
2224         # unlink f1
2225         # call setstripe ioctl on open file descriptor for f1
2226         # close
2227         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2228
2229         # Allow multiop to fail in imitation of NFS's busted semantics.
2230         true
2231 }
2232 run_test 27B "call setstripe on open unlinked file/rename victim"
2233
2234 test_27C() { #LU-2871
2235         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2236
2237         declare -a ost_idx
2238         local index
2239         local found
2240         local i
2241         local j
2242
2243         test_mkdir $DIR/$tdir
2244         cd $DIR/$tdir
2245         for i in $(seq 0 $((OSTCOUNT - 1))); do
2246                 # set stripe across all OSTs starting from OST$i
2247                 $SETSTRIPE -i $i -c -1 $tfile$i
2248                 # get striping information
2249                 ost_idx=($($GETSTRIPE $tfile$i |
2250                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2251                 echo ${ost_idx[@]}
2252
2253                 # check the layout
2254                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2255                         error "${#ost_idx[@]} != $OSTCOUNT"
2256
2257                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2258                         found=0
2259                         for j in $(echo ${ost_idx[@]}); do
2260                                 if [ $index -eq $j ]; then
2261                                         found=1
2262                                         break
2263                                 fi
2264                         done
2265                         [ $found = 1 ] ||
2266                                 error "Can not find $index in ${ost_idx[@]}"
2267                 done
2268         done
2269 }
2270 run_test 27C "check full striping across all OSTs"
2271
2272 test_27D() {
2273         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2274         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2275         remote_mds_nodsh && skip "remote MDS with nodsh"
2276
2277         local POOL=${POOL:-testpool}
2278         local first_ost=0
2279         local last_ost=$(($OSTCOUNT - 1))
2280         local ost_step=1
2281         local ost_list=$(seq $first_ost $ost_step $last_ost)
2282         local ost_range="$first_ost $last_ost $ost_step"
2283
2284         test_mkdir $DIR/$tdir
2285         pool_add $POOL || error "pool_add failed"
2286         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2287
2288         local skip27D
2289         [ $MDS1_VERSION -lt $(version_code 2.8.55) ] &&
2290                 skip27D+="-s 29"
2291         [ $MDS1_VERSION -lt $(version_code 2.9.55) -o \
2292           $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2293                 skip27D+=" -s 30,31"
2294         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2295                 error "llapi_layout_test failed"
2296
2297         destroy_test_pools || error "destroy test pools failed"
2298 }
2299 run_test 27D "validate llapi_layout API"
2300
2301 # Verify that default_easize is increased from its initial value after
2302 # accessing a widely striped file.
2303 test_27E() {
2304         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2305         [ $CLIENT_VERSION -lt $(version_code 2.5.57) ] &&
2306                 skip "client does not have LU-3338 fix"
2307
2308         # 72 bytes is the minimum space required to store striping
2309         # information for a file striped across one OST:
2310         # (sizeof(struct lov_user_md_v3) +
2311         #  sizeof(struct lov_user_ost_data_v1))
2312         local min_easize=72
2313         $LCTL set_param -n llite.*.default_easize $min_easize ||
2314                 error "lctl set_param failed"
2315         local easize=$($LCTL get_param -n llite.*.default_easize)
2316
2317         [ $easize -eq $min_easize ] ||
2318                 error "failed to set default_easize"
2319
2320         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2321                 error "setstripe failed"
2322         cat $DIR/$tfile
2323         rm $DIR/$tfile
2324
2325         easize=$($LCTL get_param -n llite.*.default_easize)
2326
2327         [ $easize -gt $min_easize ] ||
2328                 error "default_easize not updated"
2329 }
2330 run_test 27E "check that default extended attribute size properly increases"
2331
2332 test_27F() { # LU-5346/LU-7975
2333         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2334         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2335         [[ $MDS1_VERSION -lt $(version_code 2.8.51) ]] &&
2336                 skip "Need MDS version at least 2.8.51"
2337         remote_ost_nodsh && skip "remote OST with nodsh"
2338
2339         test_mkdir $DIR/$tdir
2340         rm -f $DIR/$tdir/f0
2341         $SETSTRIPE -c 2 $DIR/$tdir
2342
2343         # stop all OSTs to reproduce situation for LU-7975 ticket
2344         for num in $(seq $OSTCOUNT); do
2345                 stop ost$num
2346         done
2347
2348         # open/create f0 with O_LOV_DELAY_CREATE
2349         # truncate f0 to a non-0 size
2350         # close
2351         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2352
2353         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2354         # open/write it again to force delayed layout creation
2355         cat /etc/hosts > $DIR/$tdir/f0 &
2356         catpid=$!
2357
2358         # restart OSTs
2359         for num in $(seq $OSTCOUNT); do
2360                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2361                         error "ost$num failed to start"
2362         done
2363
2364         wait $catpid || error "cat failed"
2365
2366         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2367         [[ $($GETSTRIPE -c $DIR/$tdir/f0) == 2 ]] || error "wrong stripecount"
2368
2369 }
2370 run_test 27F "Client resend delayed layout creation with non-zero size"
2371
2372 test_27G() { #LU-10629
2373         [ $MDS1_VERSION -lt $(version_code 2.11.51) ] &&
2374                 skip "Need MDS version at least 2.11.51"
2375         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2376         remote_mds_nodsh && skip "remote MDS with nodsh"
2377         local POOL=${POOL:-testpool}
2378         local ostrange="0 0 1"
2379
2380         test_mkdir $DIR/$tdir
2381         pool_add $POOL || error "pool_add failed"
2382         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2383         $LFS setstripe -p $POOL $DIR/$tdir
2384
2385         local pool=$($LFS getstripe -p $DIR/$tdir)
2386
2387         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2388
2389         $LFS setstripe -d $DIR/$tdir
2390
2391         pool=$($LFS getstripe -p $DIR/$tdir)
2392
2393         rmdir $DIR/$tdir
2394
2395         [ -z "$pool" ] || error "'$pool' is not empty"
2396 }
2397 run_test 27G "Clear OST pool from stripe"
2398
2399 test_27H() {
2400         [[ $MDS1_VERSION -le $(version_code 2.11.54) ]] &&
2401                 skip "Need MDS version newer than 2.11.54"
2402         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2403         test_mkdir $DIR/$tdir
2404         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2405         touch $DIR/$tdir/$tfile
2406         $LFS getstripe -c $DIR/$tdir/$tfile
2407         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2408                 error "two-stripe file doesn't have two stripes"
2409
2410         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2411         $LFS getstripe -y $DIR/$tdir/$tfile
2412         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2413              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2414                 error "expected l_ost_idx: [02]$ not matched"
2415
2416         # make sure ost list have been cleared
2417         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
2418         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2419                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2420         touch $DIR/$tdir/f3
2421         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2422 }
2423 run_test 27H "Set specific OSTs stripe"
2424
2425 test_27M() {
2426         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.12.57) ]] &&
2427                 skip "Need MDS version >= than 2.12.57"
2428         remote_mds_nodsh && skip "remote MDS with nodsh"
2429         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2430
2431         test_mkdir $DIR/$tdir
2432
2433         # Set default striping on directory
2434         $LFS setstripe -C 4 $DIR/$tdir
2435
2436         echo 1 > $DIR/$tdir/${tfile}.1
2437         local count=$($LFS getstripe -c $DIR/$tdir/${tfile}.1)
2438         local setcount=4
2439         [ $count -eq $setcount ] ||
2440                 error "(1) stripe count $count, should be $setcount"
2441
2442         # Capture existing append_stripe_count setting for restore
2443         local orig_count=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_stripe_count)
2444         local mdts=$(comma_list $(mdts_nodes))
2445         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=$orig_count" EXIT
2446
2447         local appendcount=$orig_count
2448         echo 1 >> $DIR/$tdir/${tfile}.2_append
2449         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.2_append)
2450         [ $count -eq $appendcount ] ||
2451                 error "(2)stripe count $count, should be $appendcount for append"
2452
2453         # Disable O_APPEND striping, verify it works
2454         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2455
2456         # Should now get the default striping, which is 4
2457         setcount=4
2458         echo 1 >> $DIR/$tdir/${tfile}.3_append
2459         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.3_append)
2460         [ $count -eq $setcount ] ||
2461                 error "(3) stripe count $count, should be $setcount"
2462
2463         # Try changing the stripe count for append files
2464         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=2
2465
2466         # Append striping is now 2 (directory default is still 4)
2467         appendcount=2
2468         echo 1 >> $DIR/$tdir/${tfile}.4_append
2469         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.4_append)
2470         [ $count -eq $appendcount ] ||
2471                 error "(4) stripe count $count, should be $appendcount for append"
2472
2473         # Test append stripe count of -1
2474         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=-1
2475         appendcount=$OSTCOUNT
2476         echo 1 >> $DIR/$tdir/${tfile}.5
2477         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.5)
2478         [ $count -eq $appendcount ] ||
2479                 error "(5) stripe count $count, should be $appendcount for append"
2480
2481         # Set append striping back to default of 1
2482         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=1
2483
2484         # Try a new default striping, PFL + DOM
2485         $LFS setstripe -L mdt -E 1M -E -1 -c 2 $DIR/$tdir
2486
2487         # Create normal DOM file, DOM returns stripe count == 0
2488         setcount=0
2489         touch $DIR/$tdir/${tfile}.6
2490         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.6)
2491         [ $count -eq $setcount ] ||
2492                 error "(6) stripe count $count, should be $setcount"
2493
2494         # Show
2495         appendcount=1
2496         echo 1 >> $DIR/$tdir/${tfile}.7_append
2497         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.7_append)
2498         [ $count -eq $appendcount ] ||
2499                 error "(7) stripe count $count, should be $appendcount for append"
2500
2501         # Clean up DOM layout
2502         $LFS setstripe -d $DIR/$tdir
2503
2504         # Now test that append striping works when layout is from root
2505         $LFS setstripe -c 2 $MOUNT
2506         # Make a special directory for this
2507         mkdir $DIR/${tdir}/${tdir}.2
2508         stack_trap "$LFS setstripe -d $MOUNT" EXIT
2509
2510         # Verify for normal file
2511         setcount=2
2512         echo 1 > $DIR/${tdir}/${tdir}.2/${tfile}.8
2513         count=$($LFS getstripe -c $DIR/$tdir/${tdir}.2/${tfile}.8)
2514         [ $count -eq $setcount ] ||
2515                 error "(8) stripe count $count, should be $setcount"
2516
2517         appendcount=1
2518         echo 1 >> $DIR/${tdir}/${tdir}.2/${tfile}.9_append
2519         count=$($LFS getstripe -c $DIR/${tdir}/${tdir}.2/${tfile}.9_append)
2520         [ $count -eq $appendcount ] ||
2521                 error "(9) stripe count $count, should be $appendcount for append"
2522
2523         # Now test O_APPEND striping with pools
2524         do_nodes $mdts $LCTL set_param mdd.*.append_pool="$TESTNAME"
2525         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'" EXIT
2526
2527         # Create the pool
2528         pool_add $TESTNAME || error "pool creation failed"
2529         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
2530
2531         echo 1 >> $DIR/$tdir/${tfile}.10_append
2532
2533         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.10_append)
2534         [ "$pool" = "$TESTNAME" ] || error "(10) incorrect pool: $pool"
2535
2536         # Check that count is still correct
2537         appendcount=1
2538         echo 1 >> $DIR/$tdir/${tfile}.11_append
2539         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.11_append)
2540         [ $count -eq $appendcount ] ||
2541                 error "(11) stripe count $count, should be $appendcount for append"
2542
2543         # Disable O_APPEND stripe count, verify pool works separately
2544         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2545
2546         echo 1 >> $DIR/$tdir/${tfile}.12_append
2547
2548         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.12_append)
2549         [ "$pool" = "$TESTNAME" ] || error "(12) incorrect pool: $pool"
2550
2551         # Remove pool setting, verify it's not applied
2552         do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'
2553
2554         echo 1 >> $DIR/$tdir/${tfile}.13_append
2555
2556         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.13_append)
2557         [ "$pool" = "" ] || error "(13) pool found: $pool"
2558 }
2559 run_test 27M "test O_APPEND striping"
2560
2561 # createtest also checks that device nodes are created and
2562 # then visible correctly (#2091)
2563 test_28() { # bug 2091
2564         test_mkdir $DIR/d28
2565         $CREATETEST $DIR/d28/ct || error "createtest failed"
2566 }
2567 run_test 28 "create/mknod/mkdir with bad file types ============"
2568
2569 test_29() {
2570         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2571
2572         sync; sleep 1; sync # flush out any dirty pages from previous tests
2573         cancel_lru_locks
2574         test_mkdir $DIR/d29
2575         touch $DIR/d29/foo
2576         log 'first d29'
2577         ls -l $DIR/d29
2578
2579         declare -i LOCKCOUNTORIG=0
2580         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2581                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2582         done
2583         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2584
2585         declare -i LOCKUNUSEDCOUNTORIG=0
2586         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2587                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2588         done
2589
2590         log 'second d29'
2591         ls -l $DIR/d29
2592         log 'done'
2593
2594         declare -i LOCKCOUNTCURRENT=0
2595         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2596                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2597         done
2598
2599         declare -i LOCKUNUSEDCOUNTCURRENT=0
2600         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2601                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2602         done
2603
2604         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2605                 $LCTL set_param -n ldlm.dump_namespaces ""
2606                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2607                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2608                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2609                 return 2
2610         fi
2611         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2612                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2613                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2614                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2615                 return 3
2616         fi
2617 }
2618 run_test 29 "IT_GETATTR regression  ============================"
2619
2620 test_30a() { # was test_30
2621         cp $(which ls) $DIR || cp /bin/ls $DIR
2622         $DIR/ls / || error "Can't execute binary from lustre"
2623         rm $DIR/ls
2624 }
2625 run_test 30a "execute binary from Lustre (execve) =============="
2626
2627 test_30b() {
2628         cp `which ls` $DIR || cp /bin/ls $DIR
2629         chmod go+rx $DIR/ls
2630         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
2631         rm $DIR/ls
2632 }
2633 run_test 30b "execute binary from Lustre as non-root ==========="
2634
2635 test_30c() { # b=22376
2636         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2637
2638         cp `which ls` $DIR || cp /bin/ls $DIR
2639         chmod a-rw $DIR/ls
2640         cancel_lru_locks mdc
2641         cancel_lru_locks osc
2642         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
2643         rm -f $DIR/ls
2644 }
2645 run_test 30c "execute binary from Lustre without read perms ===="
2646
2647 test_31a() {
2648         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
2649         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
2650 }
2651 run_test 31a "open-unlink file =================================="
2652
2653 test_31b() {
2654         touch $DIR/f31 || error "touch $DIR/f31 failed"
2655         ln $DIR/f31 $DIR/f31b || error "ln failed"
2656         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
2657         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
2658 }
2659 run_test 31b "unlink file with multiple links while open ======="
2660
2661 test_31c() {
2662         touch $DIR/f31 || error "touch $DIR/f31 failed"
2663         ln $DIR/f31 $DIR/f31c || error "ln failed"
2664         multiop_bg_pause $DIR/f31 O_uc ||
2665                 error "multiop_bg_pause for $DIR/f31 failed"
2666         MULTIPID=$!
2667         $MULTIOP $DIR/f31c Ouc
2668         kill -USR1 $MULTIPID
2669         wait $MULTIPID
2670 }
2671 run_test 31c "open-unlink file with multiple links ============="
2672
2673 test_31d() {
2674         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
2675         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
2676 }
2677 run_test 31d "remove of open directory ========================="
2678
2679 test_31e() { # bug 2904
2680         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
2681 }
2682 run_test 31e "remove of open non-empty directory ==============="
2683
2684 test_31f() { # bug 4554
2685         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2686
2687         set -vx
2688         test_mkdir $DIR/d31f
2689         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2690         cp /etc/hosts $DIR/d31f
2691         ls -l $DIR/d31f
2692         $GETSTRIPE $DIR/d31f/hosts
2693         multiop_bg_pause $DIR/d31f D_c || return 1
2694         MULTIPID=$!
2695
2696         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2697         test_mkdir $DIR/d31f
2698         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2699         cp /etc/hosts $DIR/d31f
2700         ls -l $DIR/d31f
2701         $GETSTRIPE $DIR/d31f/hosts
2702         multiop_bg_pause $DIR/d31f D_c || return 1
2703         MULTIPID2=$!
2704
2705         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2706         wait $MULTIPID || error "first opendir $MULTIPID failed"
2707
2708         sleep 6
2709
2710         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2711         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2712         set +vx
2713 }
2714 run_test 31f "remove of open directory with open-unlink file ==="
2715
2716 test_31g() {
2717         echo "-- cross directory link --"
2718         test_mkdir -c1 $DIR/${tdir}ga
2719         test_mkdir -c1 $DIR/${tdir}gb
2720         touch $DIR/${tdir}ga/f
2721         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2722         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2723         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2724         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2725         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2726 }
2727 run_test 31g "cross directory link==============="
2728
2729 test_31h() {
2730         echo "-- cross directory link --"
2731         test_mkdir -c1 $DIR/${tdir}
2732         test_mkdir -c1 $DIR/${tdir}/dir
2733         touch $DIR/${tdir}/f
2734         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2735         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2736         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2737         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2738         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2739 }
2740 run_test 31h "cross directory link under child==============="
2741
2742 test_31i() {
2743         echo "-- cross directory link --"
2744         test_mkdir -c1 $DIR/$tdir
2745         test_mkdir -c1 $DIR/$tdir/dir
2746         touch $DIR/$tdir/dir/f
2747         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2748         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2749         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2750         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2751         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2752 }
2753 run_test 31i "cross directory link under parent==============="
2754
2755 test_31j() {
2756         test_mkdir -c1 -p $DIR/$tdir
2757         test_mkdir -c1 -p $DIR/$tdir/dir1
2758         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2759         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2760         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2761         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2762         return 0
2763 }
2764 run_test 31j "link for directory==============="
2765
2766 test_31k() {
2767         test_mkdir -c1 -p $DIR/$tdir
2768         touch $DIR/$tdir/s
2769         touch $DIR/$tdir/exist
2770         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2771         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2772         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2773         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2774         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2775         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2776         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2777         return 0
2778 }
2779 run_test 31k "link to file: the same, non-existing, dir==============="
2780
2781 test_31m() {
2782         mkdir $DIR/d31m
2783         touch $DIR/d31m/s
2784         mkdir $DIR/d31m2
2785         touch $DIR/d31m2/exist
2786         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2787         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2788         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2789         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2790         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2791         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2792         return 0
2793 }
2794 run_test 31m "link to file: the same, non-existing, dir==============="
2795
2796 test_31n() {
2797         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2798         nlink=$(stat --format=%h $DIR/$tfile)
2799         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2800         local fd=$(free_fd)
2801         local cmd="exec $fd<$DIR/$tfile"
2802         eval $cmd
2803         cmd="exec $fd<&-"
2804         trap "eval $cmd" EXIT
2805         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2806         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2807         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2808         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2809         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2810         eval $cmd
2811 }
2812 run_test 31n "check link count of unlinked file"
2813
2814 link_one() {
2815         local TEMPNAME=$(mktemp $1_XXXXXX)
2816         mlink $TEMPNAME $1 2> /dev/null &&
2817                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2818         munlink $TEMPNAME
2819 }
2820
2821 test_31o() { # LU-2901
2822         test_mkdir $DIR/$tdir
2823         for LOOP in $(seq 100); do
2824                 rm -f $DIR/$tdir/$tfile*
2825                 for THREAD in $(seq 8); do
2826                         link_one $DIR/$tdir/$tfile.$LOOP &
2827                 done
2828                 wait
2829                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2830                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2831                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2832                         break || true
2833         done
2834 }
2835 run_test 31o "duplicate hard links with same filename"
2836
2837 test_31p() {
2838         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
2839
2840         test_mkdir $DIR/$tdir
2841         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2842         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
2843
2844         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2845                 error "open unlink test1 failed"
2846         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2847                 error "open unlink test2 failed"
2848
2849         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2850                 error "test1 still exists"
2851         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2852                 error "test2 still exists"
2853 }
2854 run_test 31p "remove of open striped directory"
2855
2856 cleanup_test32_mount() {
2857         local rc=0
2858         trap 0
2859         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2860         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2861         losetup -d $loopdev || true
2862         rm -rf $DIR/$tdir
2863         return $rc
2864 }
2865
2866 test_32a() {
2867         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2868
2869         echo "== more mountpoints and symlinks ================="
2870         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2871         trap cleanup_test32_mount EXIT
2872         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2873         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2874                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2875         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
2876                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
2877         cleanup_test32_mount
2878 }
2879 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2880
2881 test_32b() {
2882         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2883
2884         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2885         trap cleanup_test32_mount EXIT
2886         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2887         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2888                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2889         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
2890                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
2891         cleanup_test32_mount
2892 }
2893 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2894
2895 test_32c() {
2896         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2897
2898         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2899         trap cleanup_test32_mount EXIT
2900         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2901         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2902                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2903         test_mkdir -p $DIR/$tdir/d2/test_dir
2904         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2905                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
2906         cleanup_test32_mount
2907 }
2908 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2909
2910 test_32d() {
2911         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2912
2913         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2914         trap cleanup_test32_mount EXIT
2915         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2916         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2917                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2918         test_mkdir -p $DIR/$tdir/d2/test_dir
2919         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2920                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
2921         cleanup_test32_mount
2922 }
2923 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
2924
2925 test_32e() {
2926         rm -fr $DIR/$tdir
2927         test_mkdir -p $DIR/$tdir/tmp
2928         local tmp_dir=$DIR/$tdir/tmp
2929         ln -s $DIR/$tdir $tmp_dir/symlink11
2930         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2931         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
2932         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
2933 }
2934 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
2935
2936 test_32f() {
2937         rm -fr $DIR/$tdir
2938         test_mkdir -p $DIR/$tdir/tmp
2939         local tmp_dir=$DIR/$tdir/tmp
2940         ln -s $DIR/$tdir $tmp_dir/symlink11
2941         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2942         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
2943         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
2944 }
2945 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
2946
2947 test_32g() {
2948         local tmp_dir=$DIR/$tdir/tmp
2949         test_mkdir -p $tmp_dir
2950         test_mkdir $DIR/${tdir}2
2951         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2952         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2953         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
2954         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
2955         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
2956         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
2957 }
2958 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2959
2960 test_32h() {
2961         rm -fr $DIR/$tdir $DIR/${tdir}2
2962         tmp_dir=$DIR/$tdir/tmp
2963         test_mkdir -p $tmp_dir
2964         test_mkdir $DIR/${tdir}2
2965         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2966         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2967         ls $tmp_dir/symlink12 || error "listing symlink12"
2968         ls $DIR/$tdir/symlink02  || error "listing symlink02"
2969 }
2970 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2971
2972 test_32i() {
2973         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2974
2975         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2976         trap cleanup_test32_mount EXIT
2977         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2978         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2979                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2980         touch $DIR/$tdir/test_file
2981         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
2982                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
2983         cleanup_test32_mount
2984 }
2985 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2986
2987 test_32j() {
2988         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2989
2990         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2991         trap cleanup_test32_mount EXIT
2992         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2993         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2994                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2995         touch $DIR/$tdir/test_file
2996         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
2997                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
2998         cleanup_test32_mount
2999 }
3000 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
3001
3002 test_32k() {
3003         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3004
3005         rm -fr $DIR/$tdir
3006         trap cleanup_test32_mount EXIT
3007         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3008         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3009                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3010         test_mkdir -p $DIR/$tdir/d2
3011         touch $DIR/$tdir/d2/test_file || error "touch failed"
3012         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3013                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
3014         cleanup_test32_mount
3015 }
3016 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
3017
3018 test_32l() {
3019         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3020
3021         rm -fr $DIR/$tdir
3022         trap cleanup_test32_mount EXIT
3023         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3024         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3025                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3026         test_mkdir -p $DIR/$tdir/d2
3027         touch $DIR/$tdir/d2/test_file || error "touch failed"
3028         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3029                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
3030         cleanup_test32_mount
3031 }
3032 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
3033
3034 test_32m() {
3035         rm -fr $DIR/d32m
3036         test_mkdir -p $DIR/d32m/tmp
3037         TMP_DIR=$DIR/d32m/tmp
3038         ln -s $DIR $TMP_DIR/symlink11
3039         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3040         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
3041                 error "symlink11 not a link"
3042         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
3043                 error "symlink01 not a link"
3044 }
3045 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
3046
3047 test_32n() {
3048         rm -fr $DIR/d32n
3049         test_mkdir -p $DIR/d32n/tmp
3050         TMP_DIR=$DIR/d32n/tmp
3051         ln -s $DIR $TMP_DIR/symlink11
3052         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3053         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
3054         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
3055 }
3056 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
3057
3058 test_32o() {
3059         touch $DIR/$tfile
3060         test_mkdir -p $DIR/d32o/tmp
3061         TMP_DIR=$DIR/d32o/tmp
3062         ln -s $DIR/$tfile $TMP_DIR/symlink12
3063         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3064         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
3065                 error "symlink12 not a link"
3066         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
3067         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
3068                 error "$DIR/d32o/tmp/symlink12 not file type"
3069         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
3070                 error "$DIR/d32o/symlink02 not file type"
3071 }
3072 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
3073
3074 test_32p() {
3075         log 32p_1
3076         rm -fr $DIR/d32p
3077         log 32p_2
3078         rm -f $DIR/$tfile
3079         log 32p_3
3080         touch $DIR/$tfile
3081         log 32p_4
3082         test_mkdir -p $DIR/d32p/tmp
3083         log 32p_5
3084         TMP_DIR=$DIR/d32p/tmp
3085         log 32p_6
3086         ln -s $DIR/$tfile $TMP_DIR/symlink12
3087         log 32p_7
3088         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3089         log 32p_8
3090         cat $DIR/d32p/tmp/symlink12 ||
3091                 error "Can't open $DIR/d32p/tmp/symlink12"
3092         log 32p_9
3093         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
3094         log 32p_10
3095 }
3096 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
3097
3098 test_32q() {
3099         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3100
3101         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3102         trap cleanup_test32_mount EXIT
3103         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3104         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3105         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3106                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3107         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
3108         cleanup_test32_mount
3109 }
3110 run_test 32q "stat follows mountpoints in Lustre (should return error)"
3111
3112 test_32r() {
3113         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3114
3115         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3116         trap cleanup_test32_mount EXIT
3117         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3118         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3119         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3120                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3121         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
3122         cleanup_test32_mount
3123 }
3124 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
3125
3126 test_33aa() {
3127         rm -f $DIR/$tfile
3128         touch $DIR/$tfile
3129         chmod 444 $DIR/$tfile
3130         chown $RUNAS_ID $DIR/$tfile
3131         log 33_1
3132         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3133         log 33_2
3134 }
3135 run_test 33aa "write file with mode 444 (should return error)"
3136
3137 test_33a() {
3138         rm -fr $DIR/$tdir
3139         test_mkdir $DIR/$tdir
3140         chown $RUNAS_ID $DIR/$tdir
3141         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
3142                 error "$RUNAS create $tdir/$tfile failed"
3143         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
3144                 error "open RDWR" || true
3145 }
3146 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
3147
3148 test_33b() {
3149         rm -fr $DIR/$tdir
3150         test_mkdir $DIR/$tdir
3151         chown $RUNAS_ID $DIR/$tdir
3152         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
3153 }
3154 run_test 33b "test open file with malformed flags (No panic)"
3155
3156 test_33c() {
3157         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3158         remote_ost_nodsh && skip "remote OST with nodsh"
3159
3160         local ostnum
3161         local ostname
3162         local write_bytes
3163         local all_zeros
3164
3165         all_zeros=:
3166         rm -fr $DIR/$tdir
3167         test_mkdir $DIR/$tdir
3168         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
3169
3170         sync
3171         for ostnum in $(seq $OSTCOUNT); do
3172                 # test-framework's OST numbering is one-based, while Lustre's
3173                 # is zero-based
3174                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3175                 # Parsing llobdstat's output sucks; we could grep the /proc
3176                 # path, but that's likely to not be as portable as using the
3177                 # llobdstat utility.  So we parse lctl output instead.
3178                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3179                         obdfilter/$ostname/stats |
3180                         awk '/^write_bytes/ {print $7}' )
3181                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
3182                 if (( ${write_bytes:-0} > 0 ))
3183                 then
3184                         all_zeros=false
3185                         break;
3186                 fi
3187         done
3188
3189         $all_zeros || return 0
3190
3191         # Write four bytes
3192         echo foo > $DIR/$tdir/bar
3193         # Really write them
3194         sync
3195
3196         # Total up write_bytes after writing.  We'd better find non-zeros.
3197         for ostnum in $(seq $OSTCOUNT); do
3198                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3199                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3200                         obdfilter/$ostname/stats |
3201                         awk '/^write_bytes/ {print $7}' )
3202                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
3203                 if (( ${write_bytes:-0} > 0 ))
3204                 then
3205                         all_zeros=false
3206                         break;
3207                 fi
3208         done
3209
3210         if $all_zeros
3211         then
3212                 for ostnum in $(seq $OSTCOUNT); do
3213                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3214                         echo "Check that write_bytes is present in obdfilter/*/stats:"
3215                         do_facet ost$ostnum lctl get_param -n \
3216                                 obdfilter/$ostname/stats
3217                 done
3218                 error "OST not keeping write_bytes stats (b22312)"
3219         fi
3220 }
3221 run_test 33c "test llobdstat and write_bytes"
3222
3223 test_33d() {
3224         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3225         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3226
3227         local MDTIDX=1
3228         local remote_dir=$DIR/$tdir/remote_dir
3229
3230         test_mkdir $DIR/$tdir
3231         $LFS mkdir -i $MDTIDX $remote_dir ||
3232                 error "create remote directory failed"
3233
3234         touch $remote_dir/$tfile
3235         chmod 444 $remote_dir/$tfile
3236         chown $RUNAS_ID $remote_dir/$tfile
3237
3238         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3239
3240         chown $RUNAS_ID $remote_dir
3241         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
3242                                         error "create" || true
3243         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
3244                                     error "open RDWR" || true
3245         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
3246 }
3247 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
3248
3249 test_33e() {
3250         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3251
3252         mkdir $DIR/$tdir
3253
3254         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3255         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3256         mkdir $DIR/$tdir/local_dir
3257
3258         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3259         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3260         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3261
3262         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3263                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
3264
3265         rmdir $DIR/$tdir/* || error "rmdir failed"
3266
3267         umask 777
3268         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3269         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3270         mkdir $DIR/$tdir/local_dir
3271
3272         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3273         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3274         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3275
3276         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3277                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3278
3279         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3280
3281         umask 000
3282         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3283         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3284         mkdir $DIR/$tdir/local_dir
3285
3286         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3287         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3288         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3289
3290         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3291                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3292 }
3293 run_test 33e "mkdir and striped directory should have same mode"
3294
3295 cleanup_33f() {
3296         trap 0
3297         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3298 }
3299
3300 test_33f() {
3301         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3302         remote_mds_nodsh && skip "remote MDS with nodsh"
3303
3304         mkdir $DIR/$tdir
3305         chmod go+rwx $DIR/$tdir
3306         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3307         trap cleanup_33f EXIT
3308
3309         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3310                 error "cannot create striped directory"
3311
3312         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3313                 error "cannot create files in striped directory"
3314
3315         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3316                 error "cannot remove files in striped directory"
3317
3318         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3319                 error "cannot remove striped directory"
3320
3321         cleanup_33f
3322 }
3323 run_test 33f "nonroot user can create, access, and remove a striped directory"
3324
3325 test_33g() {
3326         mkdir -p $DIR/$tdir/dir2
3327
3328         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3329         echo $err
3330         [[ $err =~ "exists" ]] || error "Not exists error"
3331 }
3332 run_test 33g "nonroot user create already existing root created file"
3333
3334 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3335 test_34a() {
3336         rm -f $DIR/f34
3337         $MCREATE $DIR/f34 || error "mcreate failed"
3338         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3339                 error "getstripe failed"
3340         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3341         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3342                 error "getstripe failed"
3343         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3344                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3345 }
3346 run_test 34a "truncate file that has not been opened ==========="
3347
3348 test_34b() {
3349         [ ! -f $DIR/f34 ] && test_34a
3350         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3351                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3352         $OPENFILE -f O_RDONLY $DIR/f34
3353         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3354                 error "getstripe failed"
3355         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3356                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3357 }
3358 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3359
3360 test_34c() {
3361         [ ! -f $DIR/f34 ] && test_34a
3362         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3363                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3364         $OPENFILE -f O_RDWR $DIR/f34
3365         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
3366         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3367                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3368 }
3369 run_test 34c "O_RDWR opening file-with-size works =============="
3370
3371 test_34d() {
3372         [ ! -f $DIR/f34 ] && test_34a
3373         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3374                 error "dd failed"
3375         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3376                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3377         rm $DIR/f34
3378 }
3379 run_test 34d "write to sparse file ============================="
3380
3381 test_34e() {
3382         rm -f $DIR/f34e
3383         $MCREATE $DIR/f34e || error "mcreate failed"
3384         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3385         $CHECKSTAT -s 1000 $DIR/f34e ||
3386                 error "Size of $DIR/f34e not equal to 1000 bytes"
3387         $OPENFILE -f O_RDWR $DIR/f34e
3388         $CHECKSTAT -s 1000 $DIR/f34e ||
3389                 error "Size of $DIR/f34e not equal to 1000 bytes"
3390 }
3391 run_test 34e "create objects, some with size and some without =="
3392
3393 test_34f() { # bug 6242, 6243
3394         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3395
3396         SIZE34F=48000
3397         rm -f $DIR/f34f
3398         $MCREATE $DIR/f34f || error "mcreate failed"
3399         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3400         dd if=$DIR/f34f of=$TMP/f34f
3401         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3402         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3403         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3404         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3405         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3406 }
3407 run_test 34f "read from a file with no objects until EOF ======="
3408
3409 test_34g() {
3410         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3411
3412         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3413                 error "dd failed"
3414         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3415         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3416                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3417         cancel_lru_locks osc
3418         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3419                 error "wrong size after lock cancel"
3420
3421         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3422         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3423                 error "expanding truncate failed"
3424         cancel_lru_locks osc
3425         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3426                 error "wrong expanded size after lock cancel"
3427 }
3428 run_test 34g "truncate long file ==============================="
3429
3430 test_34h() {
3431         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3432
3433         local gid=10
3434         local sz=1000
3435
3436         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3437         sync # Flush the cache so that multiop below does not block on cache
3438              # flush when getting the group lock
3439         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3440         MULTIPID=$!
3441
3442         # Since just timed wait is not good enough, let's do a sync write
3443         # that way we are sure enough time for a roundtrip + processing
3444         # passed + 2 seconds of extra margin.
3445         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
3446         rm $DIR/${tfile}-1
3447         sleep 2
3448
3449         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3450                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3451                 kill -9 $MULTIPID
3452         fi
3453         wait $MULTIPID
3454         local nsz=`stat -c %s $DIR/$tfile`
3455         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3456 }
3457 run_test 34h "ftruncate file under grouplock should not block"
3458
3459 test_35a() {
3460         cp /bin/sh $DIR/f35a
3461         chmod 444 $DIR/f35a
3462         chown $RUNAS_ID $DIR/f35a
3463         $RUNAS $DIR/f35a && error || true
3464         rm $DIR/f35a
3465 }
3466 run_test 35a "exec file with mode 444 (should return and not leak)"
3467
3468 test_36a() {
3469         rm -f $DIR/f36
3470         utime $DIR/f36 || error "utime failed for MDS"
3471 }
3472 run_test 36a "MDS utime check (mknod, utime)"
3473
3474 test_36b() {
3475         echo "" > $DIR/f36
3476         utime $DIR/f36 || error "utime failed for OST"
3477 }
3478 run_test 36b "OST utime check (open, utime)"
3479
3480 test_36c() {
3481         rm -f $DIR/d36/f36
3482         test_mkdir $DIR/d36
3483         chown $RUNAS_ID $DIR/d36
3484         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3485 }
3486 run_test 36c "non-root MDS utime check (mknod, utime)"
3487
3488 test_36d() {
3489         [ ! -d $DIR/d36 ] && test_36c
3490         echo "" > $DIR/d36/f36
3491         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3492 }
3493 run_test 36d "non-root OST utime check (open, utime)"
3494
3495 test_36e() {
3496         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3497
3498         test_mkdir $DIR/$tdir
3499         touch $DIR/$tdir/$tfile
3500         $RUNAS utime $DIR/$tdir/$tfile &&
3501                 error "utime worked, expected failure" || true
3502 }
3503 run_test 36e "utime on non-owned file (should return error)"
3504
3505 subr_36fh() {
3506         local fl="$1"
3507         local LANG_SAVE=$LANG
3508         local LC_LANG_SAVE=$LC_LANG
3509         export LANG=C LC_LANG=C # for date language
3510
3511         DATESTR="Dec 20  2000"
3512         test_mkdir $DIR/$tdir
3513         lctl set_param fail_loc=$fl
3514         date; date +%s
3515         cp /etc/hosts $DIR/$tdir/$tfile
3516         sync & # write RPC generated with "current" inode timestamp, but delayed
3517         sleep 1
3518         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3519         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3520         cancel_lru_locks $OSC
3521         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3522         date; date +%s
3523         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3524                 echo "BEFORE: $LS_BEFORE" && \
3525                 echo "AFTER : $LS_AFTER" && \
3526                 echo "WANT  : $DATESTR" && \
3527                 error "$DIR/$tdir/$tfile timestamps changed" || true
3528
3529         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3530 }
3531
3532 test_36f() {
3533         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3534
3535         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3536         subr_36fh "0x80000214"
3537 }
3538 run_test 36f "utime on file racing with OST BRW write =========="
3539
3540 test_36g() {
3541         remote_ost_nodsh && skip "remote OST with nodsh"
3542         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3543         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
3544                 skip "Need MDS version at least 2.12.51"
3545
3546         local fmd_max_age
3547         local fmd
3548         local facet="ost1"
3549         local tgt="obdfilter"
3550
3551         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
3552
3553         test_mkdir $DIR/$tdir
3554         fmd_max_age=$(do_facet $facet \
3555                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
3556                 head -n 1")
3557
3558         echo "FMD max age: ${fmd_max_age}s"
3559         touch $DIR/$tdir/$tfile
3560         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3561                 gawk '{cnt=cnt+$1}  END{print cnt}')
3562         echo "FMD before: $fmd"
3563         [[ $fmd == 0 ]] &&
3564                 error "FMD wasn't create by touch"
3565         sleep $((fmd_max_age + 12))
3566         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3567                 gawk '{cnt=cnt+$1}  END{print cnt}')
3568         echo "FMD after: $fmd"
3569         [[ $fmd == 0 ]] ||
3570                 error "FMD wasn't expired by ping"
3571 }
3572 run_test 36g "FMD cache expiry ====================="
3573
3574 test_36h() {
3575         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3576
3577         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3578         subr_36fh "0x80000227"
3579 }
3580 run_test 36h "utime on file racing with OST BRW write =========="
3581
3582 test_36i() {
3583         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3584
3585         test_mkdir $DIR/$tdir
3586         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3587
3588         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3589         local new_mtime=$((mtime + 200))
3590
3591         #change Modify time of striped dir
3592         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3593                         error "change mtime failed"
3594
3595         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3596
3597         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3598 }
3599 run_test 36i "change mtime on striped directory"
3600
3601 # test_37 - duplicate with tests 32q 32r
3602
3603 test_38() {
3604         local file=$DIR/$tfile
3605         touch $file
3606         openfile -f O_DIRECTORY $file
3607         local RC=$?
3608         local ENOTDIR=20
3609         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3610         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3611 }
3612 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3613
3614 test_39a() { # was test_39
3615         touch $DIR/$tfile
3616         touch $DIR/${tfile}2
3617 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3618 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3619 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3620         sleep 2
3621         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3622         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3623                 echo "mtime"
3624                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3625                 echo "atime"
3626                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3627                 echo "ctime"
3628                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3629                 error "O_TRUNC didn't change timestamps"
3630         fi
3631 }
3632 run_test 39a "mtime changed on create"
3633
3634 test_39b() {
3635         test_mkdir -c1 $DIR/$tdir
3636         cp -p /etc/passwd $DIR/$tdir/fopen
3637         cp -p /etc/passwd $DIR/$tdir/flink
3638         cp -p /etc/passwd $DIR/$tdir/funlink
3639         cp -p /etc/passwd $DIR/$tdir/frename
3640         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3641
3642         sleep 1
3643         echo "aaaaaa" >> $DIR/$tdir/fopen
3644         echo "aaaaaa" >> $DIR/$tdir/flink
3645         echo "aaaaaa" >> $DIR/$tdir/funlink
3646         echo "aaaaaa" >> $DIR/$tdir/frename
3647
3648         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3649         local link_new=`stat -c %Y $DIR/$tdir/flink`
3650         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3651         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3652
3653         cat $DIR/$tdir/fopen > /dev/null
3654         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3655         rm -f $DIR/$tdir/funlink2
3656         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3657
3658         for (( i=0; i < 2; i++ )) ; do
3659                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3660                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3661                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3662                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3663
3664                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3665                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3666                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3667                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3668
3669                 cancel_lru_locks $OSC
3670                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3671         done
3672 }
3673 run_test 39b "mtime change on open, link, unlink, rename  ======"
3674
3675 # this should be set to past
3676 TEST_39_MTIME=`date -d "1 year ago" +%s`
3677
3678 # bug 11063
3679 test_39c() {
3680         touch $DIR1/$tfile
3681         sleep 2
3682         local mtime0=`stat -c %Y $DIR1/$tfile`
3683
3684         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3685         local mtime1=`stat -c %Y $DIR1/$tfile`
3686         [ "$mtime1" = $TEST_39_MTIME ] || \
3687                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3688
3689         local d1=`date +%s`
3690         echo hello >> $DIR1/$tfile
3691         local d2=`date +%s`
3692         local mtime2=`stat -c %Y $DIR1/$tfile`
3693         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3694                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3695
3696         mv $DIR1/$tfile $DIR1/$tfile-1
3697
3698         for (( i=0; i < 2; i++ )) ; do
3699                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3700                 [ "$mtime2" = "$mtime3" ] || \
3701                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3702
3703                 cancel_lru_locks $OSC
3704                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3705         done
3706 }
3707 run_test 39c "mtime change on rename ==========================="
3708
3709 # bug 21114
3710 test_39d() {
3711         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3712
3713         touch $DIR1/$tfile
3714         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3715
3716         for (( i=0; i < 2; i++ )) ; do
3717                 local mtime=`stat -c %Y $DIR1/$tfile`
3718                 [ $mtime = $TEST_39_MTIME ] || \
3719                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3720
3721                 cancel_lru_locks $OSC
3722                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3723         done
3724 }
3725 run_test 39d "create, utime, stat =============================="
3726
3727 # bug 21114
3728 test_39e() {
3729         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3730
3731         touch $DIR1/$tfile
3732         local mtime1=`stat -c %Y $DIR1/$tfile`
3733
3734         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3735
3736         for (( i=0; i < 2; i++ )) ; do
3737                 local mtime2=`stat -c %Y $DIR1/$tfile`
3738                 [ $mtime2 = $TEST_39_MTIME ] || \
3739                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3740
3741                 cancel_lru_locks $OSC
3742                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3743         done
3744 }
3745 run_test 39e "create, stat, utime, stat ========================"
3746
3747 # bug 21114
3748 test_39f() {
3749         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3750
3751         touch $DIR1/$tfile
3752         mtime1=`stat -c %Y $DIR1/$tfile`
3753
3754         sleep 2
3755         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3756
3757         for (( i=0; i < 2; i++ )) ; do
3758                 local mtime2=`stat -c %Y $DIR1/$tfile`
3759                 [ $mtime2 = $TEST_39_MTIME ] || \
3760                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3761
3762                 cancel_lru_locks $OSC
3763                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3764         done
3765 }
3766 run_test 39f "create, stat, sleep, utime, stat ================="
3767
3768 # bug 11063
3769 test_39g() {
3770         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3771
3772         echo hello >> $DIR1/$tfile
3773         local mtime1=`stat -c %Y $DIR1/$tfile`
3774
3775         sleep 2
3776         chmod o+r $DIR1/$tfile
3777
3778         for (( i=0; i < 2; i++ )) ; do
3779                 local mtime2=`stat -c %Y $DIR1/$tfile`
3780                 [ "$mtime1" = "$mtime2" ] || \
3781                         error "lost mtime: $mtime2, should be $mtime1"
3782
3783                 cancel_lru_locks $OSC
3784                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3785         done
3786 }
3787 run_test 39g "write, chmod, stat ==============================="
3788
3789 # bug 11063
3790 test_39h() {
3791         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3792
3793         touch $DIR1/$tfile
3794         sleep 1
3795
3796         local d1=`date`
3797         echo hello >> $DIR1/$tfile
3798         local mtime1=`stat -c %Y $DIR1/$tfile`
3799
3800         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3801         local d2=`date`
3802         if [ "$d1" != "$d2" ]; then
3803                 echo "write and touch not within one second"
3804         else
3805                 for (( i=0; i < 2; i++ )) ; do
3806                         local mtime2=`stat -c %Y $DIR1/$tfile`
3807                         [ "$mtime2" = $TEST_39_MTIME ] || \
3808                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3809
3810                         cancel_lru_locks $OSC
3811                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3812                 done
3813         fi
3814 }
3815 run_test 39h "write, utime within one second, stat ============="
3816
3817 test_39i() {
3818         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3819
3820         touch $DIR1/$tfile
3821         sleep 1
3822
3823         echo hello >> $DIR1/$tfile
3824         local mtime1=`stat -c %Y $DIR1/$tfile`
3825
3826         mv $DIR1/$tfile $DIR1/$tfile-1
3827
3828         for (( i=0; i < 2; i++ )) ; do
3829                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3830
3831                 [ "$mtime1" = "$mtime2" ] || \
3832                         error "lost mtime: $mtime2, should be $mtime1"
3833
3834                 cancel_lru_locks $OSC
3835                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3836         done
3837 }
3838 run_test 39i "write, rename, stat =============================="
3839
3840 test_39j() {
3841         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3842
3843         start_full_debug_logging
3844         touch $DIR1/$tfile
3845         sleep 1
3846
3847         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3848         lctl set_param fail_loc=0x80000412
3849         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3850                 error "multiop failed"
3851         local multipid=$!
3852         local mtime1=`stat -c %Y $DIR1/$tfile`
3853
3854         mv $DIR1/$tfile $DIR1/$tfile-1
3855
3856         kill -USR1 $multipid
3857         wait $multipid || error "multiop close failed"
3858
3859         for (( i=0; i < 2; i++ )) ; do
3860                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3861                 [ "$mtime1" = "$mtime2" ] ||
3862                         error "mtime is lost on close: $mtime2, " \
3863                               "should be $mtime1"
3864
3865                 cancel_lru_locks $OSC
3866                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3867         done
3868         lctl set_param fail_loc=0
3869         stop_full_debug_logging
3870 }
3871 run_test 39j "write, rename, close, stat ======================="
3872
3873 test_39k() {
3874         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3875
3876         touch $DIR1/$tfile
3877         sleep 1
3878
3879         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3880         local multipid=$!
3881         local mtime1=`stat -c %Y $DIR1/$tfile`
3882
3883         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3884
3885         kill -USR1 $multipid
3886         wait $multipid || error "multiop close failed"
3887
3888         for (( i=0; i < 2; i++ )) ; do
3889                 local mtime2=`stat -c %Y $DIR1/$tfile`
3890
3891                 [ "$mtime2" = $TEST_39_MTIME ] || \
3892                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3893
3894                 cancel_lru_locks osc
3895                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3896         done
3897 }
3898 run_test 39k "write, utime, close, stat ========================"
3899
3900 # this should be set to future
3901 TEST_39_ATIME=`date -d "1 year" +%s`
3902
3903 test_39l() {
3904         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3905         remote_mds_nodsh && skip "remote MDS with nodsh"
3906
3907         local atime_diff=$(do_facet $SINGLEMDS \
3908                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3909         rm -rf $DIR/$tdir
3910         mkdir -p $DIR/$tdir
3911
3912         # test setting directory atime to future
3913         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3914         local atime=$(stat -c %X $DIR/$tdir)
3915         [ "$atime" = $TEST_39_ATIME ] ||
3916                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3917
3918         # test setting directory atime from future to now
3919         local now=$(date +%s)
3920         touch -a -d @$now $DIR/$tdir
3921
3922         atime=$(stat -c %X $DIR/$tdir)
3923         [ "$atime" -eq "$now"  ] ||
3924                 error "atime is not updated from future: $atime, $now"
3925
3926         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3927         sleep 3
3928
3929         # test setting directory atime when now > dir atime + atime_diff
3930         local d1=$(date +%s)
3931         ls $DIR/$tdir
3932         local d2=$(date +%s)
3933         cancel_lru_locks mdc
3934         atime=$(stat -c %X $DIR/$tdir)
3935         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3936                 error "atime is not updated  : $atime, should be $d2"
3937
3938         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3939         sleep 3
3940
3941         # test not setting directory atime when now < dir atime + atime_diff
3942         ls $DIR/$tdir
3943         cancel_lru_locks mdc
3944         atime=$(stat -c %X $DIR/$tdir)
3945         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3946                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3947
3948         do_facet $SINGLEMDS \
3949                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3950 }
3951 run_test 39l "directory atime update ==========================="
3952
3953 test_39m() {
3954         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3955
3956         touch $DIR1/$tfile
3957         sleep 2
3958         local far_past_mtime=$(date -d "May 29 1953" +%s)
3959         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3960
3961         touch -m -d @$far_past_mtime $DIR1/$tfile
3962         touch -a -d @$far_past_atime $DIR1/$tfile
3963
3964         for (( i=0; i < 2; i++ )) ; do
3965                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3966                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3967                         error "atime or mtime set incorrectly"
3968
3969                 cancel_lru_locks $OSC
3970                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3971         done
3972 }
3973 run_test 39m "test atime and mtime before 1970"
3974
3975 test_39n() { # LU-3832
3976         remote_mds_nodsh && skip "remote MDS with nodsh"
3977
3978         local atime_diff=$(do_facet $SINGLEMDS \
3979                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3980         local atime0
3981         local atime1
3982         local atime2
3983
3984         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3985
3986         rm -rf $DIR/$tfile
3987         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3988         atime0=$(stat -c %X $DIR/$tfile)
3989
3990         sleep 5
3991         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3992         atime1=$(stat -c %X $DIR/$tfile)
3993
3994         sleep 5
3995         cancel_lru_locks mdc
3996         cancel_lru_locks osc
3997         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3998         atime2=$(stat -c %X $DIR/$tfile)
3999
4000         do_facet $SINGLEMDS \
4001                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4002
4003         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
4004         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
4005 }
4006 run_test 39n "check that O_NOATIME is honored"
4007
4008 test_39o() {
4009         TESTDIR=$DIR/$tdir/$tfile
4010         [ -e $TESTDIR ] && rm -rf $TESTDIR
4011         mkdir -p $TESTDIR
4012         cd $TESTDIR
4013         links1=2
4014         ls
4015         mkdir a b
4016         ls
4017         links2=$(stat -c %h .)
4018         [ $(($links1 + 2)) != $links2 ] &&
4019                 error "wrong links count $(($links1 + 2)) != $links2"
4020         rmdir b
4021         links3=$(stat -c %h .)
4022         [ $(($links1 + 1)) != $links3 ] &&
4023                 error "wrong links count $links1 != $links3"
4024         return 0
4025 }
4026 run_test 39o "directory cached attributes updated after create"
4027
4028 test_39p() {
4029         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4030
4031         local MDTIDX=1
4032         TESTDIR=$DIR/$tdir/$tdir
4033         [ -e $TESTDIR ] && rm -rf $TESTDIR
4034         test_mkdir -p $TESTDIR
4035         cd $TESTDIR
4036         links1=2
4037         ls
4038         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
4039         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
4040         ls
4041         links2=$(stat -c %h .)
4042         [ $(($links1 + 2)) != $links2 ] &&
4043                 error "wrong links count $(($links1 + 2)) != $links2"
4044         rmdir remote_dir2
4045         links3=$(stat -c %h .)
4046         [ $(($links1 + 1)) != $links3 ] &&
4047                 error "wrong links count $links1 != $links3"
4048         return 0
4049 }
4050 run_test 39p "remote directory cached attributes updated after create ========"
4051
4052
4053 test_39q() { # LU-8041
4054         local testdir=$DIR/$tdir
4055         mkdir -p $testdir
4056         multiop_bg_pause $testdir D_c || error "multiop failed"
4057         local multipid=$!
4058         cancel_lru_locks mdc
4059         kill -USR1 $multipid
4060         local atime=$(stat -c %X $testdir)
4061         [ "$atime" -ne 0 ] || error "atime is zero"
4062 }
4063 run_test 39q "close won't zero out atime"
4064
4065 test_40() {
4066         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
4067         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
4068                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
4069         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
4070                 error "$tfile is not 4096 bytes in size"
4071 }
4072 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
4073
4074 test_41() {
4075         # bug 1553
4076         small_write $DIR/f41 18
4077 }
4078 run_test 41 "test small file write + fstat ====================="
4079
4080 count_ost_writes() {
4081         lctl get_param -n ${OSC}.*.stats |
4082                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
4083                         END { printf("%0.0f", writes) }'
4084 }
4085
4086 # decent default
4087 WRITEBACK_SAVE=500
4088 DIRTY_RATIO_SAVE=40
4089 MAX_DIRTY_RATIO=50
4090 BG_DIRTY_RATIO_SAVE=10
4091 MAX_BG_DIRTY_RATIO=25
4092
4093 start_writeback() {
4094         trap 0
4095         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
4096         # dirty_ratio, dirty_background_ratio
4097         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4098                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
4099                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
4100                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
4101         else
4102                 # if file not here, we are a 2.4 kernel
4103                 kill -CONT `pidof kupdated`
4104         fi
4105 }
4106
4107 stop_writeback() {
4108         # setup the trap first, so someone cannot exit the test at the
4109         # exact wrong time and mess up a machine
4110         trap start_writeback EXIT
4111         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
4112         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4113                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
4114                 sysctl -w vm.dirty_writeback_centisecs=0
4115                 sysctl -w vm.dirty_writeback_centisecs=0
4116                 # save and increase /proc/sys/vm/dirty_ratio
4117                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
4118                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
4119                 # save and increase /proc/sys/vm/dirty_background_ratio
4120                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
4121                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
4122         else
4123                 # if file not here, we are a 2.4 kernel
4124                 kill -STOP `pidof kupdated`
4125         fi
4126 }
4127
4128 # ensure that all stripes have some grant before we test client-side cache
4129 setup_test42() {
4130         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
4131                 dd if=/dev/zero of=$i bs=4k count=1
4132                 rm $i
4133         done
4134 }
4135
4136 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
4137 # file truncation, and file removal.
4138 test_42a() {
4139         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4140
4141         setup_test42
4142         cancel_lru_locks $OSC
4143         stop_writeback
4144         sync; sleep 1; sync # just to be safe
4145         BEFOREWRITES=`count_ost_writes`
4146         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
4147         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
4148         AFTERWRITES=`count_ost_writes`
4149         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
4150                 error "$BEFOREWRITES < $AFTERWRITES"
4151         start_writeback
4152 }
4153 run_test 42a "ensure that we don't flush on close"
4154
4155 test_42b() {
4156         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4157
4158         setup_test42
4159         cancel_lru_locks $OSC
4160         stop_writeback
4161         sync
4162         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
4163         BEFOREWRITES=$(count_ost_writes)
4164         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
4165         AFTERWRITES=$(count_ost_writes)
4166         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4167                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
4168         fi
4169         BEFOREWRITES=$(count_ost_writes)
4170         sync || error "sync: $?"
4171         AFTERWRITES=$(count_ost_writes)
4172         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4173                 error "$BEFOREWRITES < $AFTERWRITES on sync"
4174         fi
4175         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
4176         start_writeback
4177         return 0
4178 }
4179 run_test 42b "test destroy of file with cached dirty data ======"
4180
4181 # if these tests just want to test the effect of truncation,
4182 # they have to be very careful.  consider:
4183 # - the first open gets a {0,EOF}PR lock
4184 # - the first write conflicts and gets a {0, count-1}PW
4185 # - the rest of the writes are under {count,EOF}PW
4186 # - the open for truncate tries to match a {0,EOF}PR
4187 #   for the filesize and cancels the PWs.
4188 # any number of fixes (don't get {0,EOF} on open, match
4189 # composite locks, do smarter file size management) fix
4190 # this, but for now we want these tests to verify that
4191 # the cancellation with truncate intent works, so we
4192 # start the file with a full-file pw lock to match against
4193 # until the truncate.
4194 trunc_test() {
4195         test=$1
4196         file=$DIR/$test
4197         offset=$2
4198         cancel_lru_locks $OSC
4199         stop_writeback
4200         # prime the file with 0,EOF PW to match
4201         touch $file
4202         $TRUNCATE $file 0
4203         sync; sync
4204         # now the real test..
4205         dd if=/dev/zero of=$file bs=1024 count=100
4206         BEFOREWRITES=`count_ost_writes`
4207         $TRUNCATE $file $offset
4208         cancel_lru_locks $OSC
4209         AFTERWRITES=`count_ost_writes`
4210         start_writeback
4211 }
4212
4213 test_42c() {
4214         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4215
4216         trunc_test 42c 1024
4217         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
4218                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
4219         rm $file
4220 }
4221 run_test 42c "test partial truncate of file with cached dirty data"
4222
4223 test_42d() {
4224         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4225
4226         trunc_test 42d 0
4227         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
4228                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
4229         rm $file
4230 }
4231 run_test 42d "test complete truncate of file with cached dirty data"
4232
4233 test_42e() { # bug22074
4234         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4235
4236         local TDIR=$DIR/${tdir}e
4237         local pages=16 # hardcoded 16 pages, don't change it.
4238         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
4239         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
4240         local max_dirty_mb
4241         local warmup_files
4242
4243         test_mkdir $DIR/${tdir}e
4244         $SETSTRIPE -c 1 $TDIR
4245         createmany -o $TDIR/f $files
4246
4247         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
4248
4249         # we assume that with $OSTCOUNT files, at least one of them will
4250         # be allocated on OST0.
4251         warmup_files=$((OSTCOUNT * max_dirty_mb))
4252         createmany -o $TDIR/w $warmup_files
4253
4254         # write a large amount of data into one file and sync, to get good
4255         # avail_grant number from OST.
4256         for ((i=0; i<$warmup_files; i++)); do
4257                 idx=$($GETSTRIPE -i $TDIR/w$i)
4258                 [ $idx -ne 0 ] && continue
4259                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
4260                 break
4261         done
4262         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
4263         sync
4264         $LCTL get_param $proc_osc0/cur_dirty_bytes
4265         $LCTL get_param $proc_osc0/cur_grant_bytes
4266
4267         # create as much dirty pages as we can while not to trigger the actual
4268         # RPCs directly. but depends on the env, VFS may trigger flush during this
4269         # period, hopefully we are good.
4270         for ((i=0; i<$warmup_files; i++)); do
4271                 idx=$($GETSTRIPE -i $TDIR/w$i)
4272                 [ $idx -ne 0 ] && continue
4273                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4274         done
4275         $LCTL get_param $proc_osc0/cur_dirty_bytes
4276         $LCTL get_param $proc_osc0/cur_grant_bytes
4277
4278         # perform the real test
4279         $LCTL set_param $proc_osc0/rpc_stats 0
4280         for ((;i<$files; i++)); do
4281                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
4282                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
4283         done
4284         sync
4285         $LCTL get_param $proc_osc0/rpc_stats
4286
4287         local percent=0
4288         local have_ppr=false
4289         $LCTL get_param $proc_osc0/rpc_stats |
4290                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4291                         # skip lines until we are at the RPC histogram data
4292                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4293                         $have_ppr || continue
4294
4295                         # we only want the percent stat for < 16 pages
4296                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4297
4298                         percent=$((percent + WPCT))
4299                         if [[ $percent -gt 15 ]]; then
4300                                 error "less than 16-pages write RPCs" \
4301                                       "$percent% > 15%"
4302                                 break
4303                         fi
4304                 done
4305         rm -rf $TDIR
4306 }
4307 run_test 42e "verify sub-RPC writes are not done synchronously"
4308
4309 test_43A() { # was test_43
4310         test_mkdir $DIR/$tdir
4311         cp -p /bin/ls $DIR/$tdir/$tfile
4312         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4313         pid=$!
4314         # give multiop a chance to open
4315         sleep 1
4316
4317         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4318         kill -USR1 $pid
4319 }
4320 run_test 43A "execution of file opened for write should return -ETXTBSY"
4321
4322 test_43a() {
4323         test_mkdir $DIR/$tdir
4324         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4325         $DIR/$tdir/sleep 60 &
4326         SLEEP_PID=$!
4327         # Make sure exec of $tdir/sleep wins race with truncate
4328         sleep 1
4329         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
4330         kill $SLEEP_PID
4331 }
4332 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4333
4334 test_43b() {
4335         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4336
4337         test_mkdir $DIR/$tdir
4338         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4339         $DIR/$tdir/sleep 60 &
4340         SLEEP_PID=$!
4341         # Make sure exec of $tdir/sleep wins race with truncate
4342         sleep 1
4343         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
4344         kill $SLEEP_PID
4345 }
4346 run_test 43b "truncate of file being executed should return -ETXTBSY"
4347
4348 test_43c() {
4349         local testdir="$DIR/$tdir"
4350         test_mkdir $testdir
4351         cp $SHELL $testdir/
4352         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4353                 ( cd $testdir && md5sum -c )
4354 }
4355 run_test 43c "md5sum of copy into lustre"
4356
4357 test_44A() { # was test_44
4358         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4359
4360         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4361         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4362 }
4363 run_test 44A "zero length read from a sparse stripe"
4364
4365 test_44a() {
4366         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4367                 awk '{ print $2 }')
4368         [ -z "$nstripe" ] && skip "can't get stripe info"
4369         [[ $nstripe -gt $OSTCOUNT ]] &&
4370                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4371
4372         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4373                 awk '{ print $2 }')
4374         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4375                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4376                         awk '{ print $2 }')
4377         fi
4378
4379         OFFSETS="0 $((stride/2)) $((stride-1))"
4380         for offset in $OFFSETS; do
4381                 for i in $(seq 0 $((nstripe-1))); do
4382                         local GLOBALOFFSETS=""
4383                         # size in Bytes
4384                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4385                         local myfn=$DIR/d44a-$size
4386                         echo "--------writing $myfn at $size"
4387                         ll_sparseness_write $myfn $size ||
4388                                 error "ll_sparseness_write"
4389                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4390                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4391                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4392
4393                         for j in $(seq 0 $((nstripe-1))); do
4394                                 # size in Bytes
4395                                 size=$((((j + $nstripe )*$stride + $offset)))
4396                                 ll_sparseness_write $myfn $size ||
4397                                         error "ll_sparseness_write"
4398                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4399                         done
4400                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4401                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4402                         rm -f $myfn
4403                 done
4404         done
4405 }
4406 run_test 44a "test sparse pwrite ==============================="
4407
4408 dirty_osc_total() {
4409         tot=0
4410         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4411                 tot=$(($tot + $d))
4412         done
4413         echo $tot
4414 }
4415 do_dirty_record() {
4416         before=`dirty_osc_total`
4417         echo executing "\"$*\""
4418         eval $*
4419         after=`dirty_osc_total`
4420         echo before $before, after $after
4421 }
4422 test_45() {
4423         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4424
4425         f="$DIR/f45"
4426         # Obtain grants from OST if it supports it
4427         echo blah > ${f}_grant
4428         stop_writeback
4429         sync
4430         do_dirty_record "echo blah > $f"
4431         [[ $before -eq $after ]] && error "write wasn't cached"
4432         do_dirty_record "> $f"
4433         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4434         do_dirty_record "echo blah > $f"
4435         [[ $before -eq $after ]] && error "write wasn't cached"
4436         do_dirty_record "sync"
4437         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4438         do_dirty_record "echo blah > $f"
4439         [[ $before -eq $after ]] && error "write wasn't cached"
4440         do_dirty_record "cancel_lru_locks osc"
4441         [[ $before -gt $after ]] ||
4442                 error "lock cancellation didn't lower dirty count"
4443         start_writeback
4444 }
4445 run_test 45 "osc io page accounting ============================"
4446
4447 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4448 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4449 # objects offset and an assert hit when an rpc was built with 1023's mapped
4450 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4451 test_46() {
4452         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4453
4454         f="$DIR/f46"
4455         stop_writeback
4456         sync
4457         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4458         sync
4459         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
4460         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4461         sync
4462         start_writeback
4463 }
4464 run_test 46 "dirtying a previously written page ================"
4465
4466 # test_47 is removed "Device nodes check" is moved to test_28
4467
4468 test_48a() { # bug 2399
4469         [ "$mds1_FSTYPE" = "zfs" ] &&
4470         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
4471                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
4472
4473         test_mkdir $DIR/$tdir
4474         cd $DIR/$tdir
4475         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
4476         test_mkdir $DIR/$tdir
4477         touch foo || error "'touch foo' failed after recreating cwd"
4478         test_mkdir bar
4479         touch .foo || error "'touch .foo' failed after recreating cwd"
4480         test_mkdir .bar
4481         ls . > /dev/null || error "'ls .' failed after recreating cwd"
4482         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4483         cd . || error "'cd .' failed after recreating cwd"
4484         mkdir . && error "'mkdir .' worked after recreating cwd"
4485         rmdir . && error "'rmdir .' worked after recreating cwd"
4486         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4487         cd .. || error "'cd ..' failed after recreating cwd"
4488 }
4489 run_test 48a "Access renamed working dir (should return errors)="
4490
4491 test_48b() { # bug 2399
4492         rm -rf $DIR/$tdir
4493         test_mkdir $DIR/$tdir
4494         cd $DIR/$tdir
4495         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4496         touch foo && error "'touch foo' worked after removing cwd"
4497         mkdir foo && error "'mkdir foo' worked after removing cwd"
4498         touch .foo && error "'touch .foo' worked after removing cwd"
4499         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4500         ls . > /dev/null && error "'ls .' worked after removing cwd"
4501         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4502         mkdir . && error "'mkdir .' worked after removing cwd"
4503         rmdir . && error "'rmdir .' worked after removing cwd"
4504         ln -s . foo && error "'ln -s .' worked after removing cwd"
4505         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4506 }
4507 run_test 48b "Access removed working dir (should return errors)="
4508
4509 test_48c() { # bug 2350
4510         #lctl set_param debug=-1
4511         #set -vx
4512         rm -rf $DIR/$tdir
4513         test_mkdir -p $DIR/$tdir/dir
4514         cd $DIR/$tdir/dir
4515         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4516         $TRACE touch foo && error "touch foo worked after removing cwd"
4517         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4518         touch .foo && error "touch .foo worked after removing cwd"
4519         mkdir .foo && error "mkdir .foo worked after removing cwd"
4520         $TRACE ls . && error "'ls .' worked after removing cwd"
4521         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4522         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4523         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4524         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4525         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4526 }
4527 run_test 48c "Access removed working subdir (should return errors)"
4528
4529 test_48d() { # bug 2350
4530         #lctl set_param debug=-1
4531         #set -vx
4532         rm -rf $DIR/$tdir
4533         test_mkdir -p $DIR/$tdir/dir
4534         cd $DIR/$tdir/dir
4535         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4536         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4537         $TRACE touch foo && error "'touch foo' worked after removing parent"
4538         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4539         touch .foo && error "'touch .foo' worked after removing parent"
4540         mkdir .foo && error "mkdir .foo worked after removing parent"
4541         $TRACE ls . && error "'ls .' worked after removing parent"
4542         $TRACE ls .. && error "'ls ..' worked after removing parent"
4543         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
4544         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4545         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4546         true
4547 }
4548 run_test 48d "Access removed parent subdir (should return errors)"
4549
4550 test_48e() { # bug 4134
4551         #lctl set_param debug=-1
4552         #set -vx
4553         rm -rf $DIR/$tdir
4554         test_mkdir -p $DIR/$tdir/dir
4555         cd $DIR/$tdir/dir
4556         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4557         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4558         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4559         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4560         # On a buggy kernel addition of "touch foo" after cd .. will
4561         # produce kernel oops in lookup_hash_it
4562         touch ../foo && error "'cd ..' worked after recreate parent"
4563         cd $DIR
4564         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4565 }
4566 run_test 48e "Access to recreated parent subdir (should return errors)"
4567
4568 test_49() { # LU-1030
4569         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4570         remote_ost_nodsh && skip "remote OST with nodsh"
4571
4572         # get ost1 size - lustre-OST0000
4573         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4574                 awk '{ print $4 }')
4575         # write 800M at maximum
4576         [[ $ost1_size -lt 2 ]] && ost1_size=2
4577         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4578
4579         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
4580         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4581         local dd_pid=$!
4582
4583         # change max_pages_per_rpc while writing the file
4584         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4585         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4586         # loop until dd process exits
4587         while ps ax -opid | grep -wq $dd_pid; do
4588                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4589                 sleep $((RANDOM % 5 + 1))
4590         done
4591         # restore original max_pages_per_rpc
4592         $LCTL set_param $osc1_mppc=$orig_mppc
4593         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4594 }
4595 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4596
4597 test_50() {
4598         # bug 1485
4599         test_mkdir $DIR/$tdir
4600         cd $DIR/$tdir
4601         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4602 }
4603 run_test 50 "special situations: /proc symlinks  ==============="
4604
4605 test_51a() {    # was test_51
4606         # bug 1516 - create an empty entry right after ".." then split dir
4607         test_mkdir -c1 $DIR/$tdir
4608         touch $DIR/$tdir/foo
4609         $MCREATE $DIR/$tdir/bar
4610         rm $DIR/$tdir/foo
4611         createmany -m $DIR/$tdir/longfile 201
4612         FNUM=202
4613         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4614                 $MCREATE $DIR/$tdir/longfile$FNUM
4615                 FNUM=$(($FNUM + 1))
4616                 echo -n "+"
4617         done
4618         echo
4619         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4620 }
4621 run_test 51a "special situations: split htree with empty entry =="
4622
4623 cleanup_print_lfs_df () {
4624         trap 0
4625         $LFS df
4626         $LFS df -i
4627 }
4628
4629 test_51b() {
4630         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4631
4632         local dir=$DIR/$tdir
4633         local nrdirs=$((65536 + 100))
4634
4635         # cleanup the directory
4636         rm -fr $dir
4637
4638         test_mkdir -c1 $dir
4639
4640         $LFS df
4641         $LFS df -i
4642         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
4643         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4644         [[ $numfree -lt $nrdirs ]] &&
4645                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
4646
4647         # need to check free space for the directories as well
4648         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4649         numfree=$(( blkfree / $(fs_inode_ksize) ))
4650         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
4651
4652         trap cleanup_print_lfs_df EXIT
4653
4654         # create files
4655         createmany -d $dir/d $nrdirs || {
4656                 unlinkmany $dir/d $nrdirs
4657                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
4658         }
4659
4660         # really created :
4661         nrdirs=$(ls -U $dir | wc -l)
4662
4663         # unlink all but 100 subdirectories, then check it still works
4664         local left=100
4665         local delete=$((nrdirs - left))
4666
4667         $LFS df
4668         $LFS df -i
4669
4670         # for ldiskfs the nlink count should be 1, but this is OSD specific
4671         # and so this is listed for informational purposes only
4672         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
4673         unlinkmany -d $dir/d $delete ||
4674                 error "unlink of first $delete subdirs failed"
4675
4676         echo "nlink between: $(stat -c %h $dir)"
4677         local found=$(ls -U $dir | wc -l)
4678         [ $found -ne $left ] &&
4679                 error "can't find subdirs: found only $found, expected $left"
4680
4681         unlinkmany -d $dir/d $delete $left ||
4682                 error "unlink of second $left subdirs failed"
4683         # regardless of whether the backing filesystem tracks nlink accurately
4684         # or not, the nlink count shouldn't be more than "." and ".." here
4685         local after=$(stat -c %h $dir)
4686         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
4687                 echo "nlink after: $after"
4688
4689         cleanup_print_lfs_df
4690 }
4691 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
4692
4693 test_51d() {
4694         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4695         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
4696
4697         test_mkdir $DIR/$tdir
4698         createmany -o $DIR/$tdir/t- 1000
4699         $LFS getstripe $DIR/$tdir > $TMP/$tfile
4700         for N in $(seq 0 $((OSTCOUNT - 1))); do
4701                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4702                         END { printf("%0.0f", objs) }' $TMP/$tfile)
4703                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
4704                         '($1 == '$N') { objs += 1 } \
4705                         END { printf("%0.0f", objs) }')
4706                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4707         done
4708         unlinkmany $DIR/$tdir/t- 1000
4709
4710         NLAST=0
4711         for N in $(seq 1 $((OSTCOUNT - 1))); do
4712                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4713                         error "OST $N has less objects vs OST $NLAST" \
4714                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4715                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4716                         error "OST $N has less objects vs OST $NLAST" \
4717                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4718
4719                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4720                         error "OST $N has less #0 objects vs OST $NLAST" \
4721                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4722                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4723                         error "OST $N has less #0 objects vs OST $NLAST" \
4724                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4725                 NLAST=$N
4726         done
4727         rm -f $TMP/$tfile
4728 }
4729 run_test 51d "check object distribution"
4730
4731 test_51e() {
4732         if [ "$mds1_FSTYPE" != ldiskfs ]; then
4733                 skip_env "ldiskfs only test"
4734         fi
4735
4736         test_mkdir -c1 $DIR/$tdir
4737         test_mkdir -c1 $DIR/$tdir/d0
4738
4739         touch $DIR/$tdir/d0/foo
4740         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4741                 error "file exceed 65000 nlink limit!"
4742         unlinkmany $DIR/$tdir/d0/f- 65001
4743         return 0
4744 }
4745 run_test 51e "check file nlink limit"
4746
4747 test_51f() {
4748         test_mkdir $DIR/$tdir
4749
4750         local max=100000
4751         local ulimit_old=$(ulimit -n)
4752         local spare=20 # number of spare fd's for scripts/libraries, etc.
4753         local mdt=$($LFS getstripe -m $DIR/$tdir)
4754         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
4755
4756         echo "MDT$mdt numfree=$numfree, max=$max"
4757         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
4758         if [ $((numfree + spare)) -gt $ulimit_old ]; then
4759                 while ! ulimit -n $((numfree + spare)); do
4760                         numfree=$((numfree * 3 / 4))
4761                 done
4762                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
4763         else
4764                 echo "left ulimit at $ulimit_old"
4765         fi
4766
4767         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
4768                 unlinkmany $DIR/$tdir/f $numfree
4769                 error "create+open $numfree files in $DIR/$tdir failed"
4770         }
4771         ulimit -n $ulimit_old
4772
4773         # if createmany exits at 120s there will be fewer than $numfree files
4774         unlinkmany $DIR/$tdir/f $numfree || true
4775 }
4776 run_test 51f "check many open files limit"
4777
4778 test_52a() {
4779         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4780         test_mkdir $DIR/$tdir
4781         touch $DIR/$tdir/foo
4782         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4783         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4784         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4785         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4786         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4787                                         error "link worked"
4788         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4789         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4790         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4791                                                      error "lsattr"
4792         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4793         cp -r $DIR/$tdir $TMP/
4794         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
4795 }
4796 run_test 52a "append-only flag test (should return errors)"
4797
4798 test_52b() {
4799         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4800         test_mkdir $DIR/$tdir
4801         touch $DIR/$tdir/foo
4802         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4803         cat test > $DIR/$tdir/foo && error "cat test worked"
4804         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4805         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4806         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4807                                         error "link worked"
4808         echo foo >> $DIR/$tdir/foo && error "echo worked"
4809         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4810         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4811         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4812         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4813                                                         error "lsattr"
4814         chattr -i $DIR/$tdir/foo || error "chattr failed"
4815
4816         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4817 }
4818 run_test 52b "immutable flag test (should return errors) ======="
4819
4820 test_53() {
4821         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4822         remote_mds_nodsh && skip "remote MDS with nodsh"
4823         remote_ost_nodsh && skip "remote OST with nodsh"
4824
4825         local param
4826         local param_seq
4827         local ostname
4828         local mds_last
4829         local mds_last_seq
4830         local ost_last
4831         local ost_last_seq
4832         local ost_last_id
4833         local ostnum
4834         local node
4835         local found=false
4836         local support_last_seq=true
4837
4838         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
4839                 support_last_seq=false
4840
4841         # only test MDT0000
4842         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4843         local value
4844         for value in $(do_facet $SINGLEMDS \
4845                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
4846                 param=$(echo ${value[0]} | cut -d "=" -f1)
4847                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4848
4849                 if $support_last_seq; then
4850                         param_seq=$(echo $param |
4851                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4852                         mds_last_seq=$(do_facet $SINGLEMDS \
4853                                        $LCTL get_param -n $param_seq)
4854                 fi
4855                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4856
4857                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4858                 node=$(facet_active_host ost$((ostnum+1)))
4859                 param="obdfilter.$ostname.last_id"
4860                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4861                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4862                         ost_last_id=$ost_last
4863
4864                         if $support_last_seq; then
4865                                 ost_last_id=$(echo $ost_last |
4866                                               awk -F':' '{print $2}' |
4867                                               sed -e "s/^0x//g")
4868                                 ost_last_seq=$(echo $ost_last |
4869                                                awk -F':' '{print $1}')
4870                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4871                         fi
4872
4873                         if [[ $ost_last_id != $mds_last ]]; then
4874                                 error "$ost_last_id != $mds_last"
4875                         else
4876                                 found=true
4877                                 break
4878                         fi
4879                 done
4880         done
4881         $found || error "can not match last_seq/last_id for $mdtosc"
4882         return 0
4883 }
4884 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4885
4886 test_54a() {
4887         perl -MSocket -e ';' || skip "no Socket perl module installed"
4888
4889         $SOCKETSERVER $DIR/socket ||
4890                 error "$SOCKETSERVER $DIR/socket failed: $?"
4891         $SOCKETCLIENT $DIR/socket ||
4892                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4893         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4894 }
4895 run_test 54a "unix domain socket test =========================="
4896
4897 test_54b() {
4898         f="$DIR/f54b"
4899         mknod $f c 1 3
4900         chmod 0666 $f
4901         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
4902 }
4903 run_test 54b "char device works in lustre ======================"
4904
4905 find_loop_dev() {
4906         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4907         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4908         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4909
4910         for i in $(seq 3 7); do
4911                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4912                 LOOPDEV=$LOOPBASE$i
4913                 LOOPNUM=$i
4914                 break
4915         done
4916 }
4917
4918 cleanup_54c() {
4919         local rc=0
4920         loopdev="$DIR/loop54c"
4921
4922         trap 0
4923         $UMOUNT $DIR/$tdir || rc=$?
4924         losetup -d $loopdev || true
4925         losetup -d $LOOPDEV || true
4926         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4927         return $rc
4928 }
4929
4930 test_54c() {
4931         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4932
4933         loopdev="$DIR/loop54c"
4934
4935         find_loop_dev
4936         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
4937         trap cleanup_54c EXIT
4938         mknod $loopdev b 7 $LOOPNUM
4939         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4940         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
4941         losetup $loopdev $DIR/$tfile ||
4942                 error "can't set up $loopdev for $DIR/$tfile"
4943         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4944         test_mkdir $DIR/$tdir
4945         mount -t ext2 $loopdev $DIR/$tdir ||
4946                 error "error mounting $loopdev on $DIR/$tdir"
4947         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
4948                 error "dd write"
4949         df $DIR/$tdir
4950         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
4951                 error "dd read"
4952         cleanup_54c
4953 }
4954 run_test 54c "block device works in lustre ====================="
4955
4956 test_54d() {
4957         f="$DIR/f54d"
4958         string="aaaaaa"
4959         mknod $f p
4960         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4961 }
4962 run_test 54d "fifo device works in lustre ======================"
4963
4964 test_54e() {
4965         f="$DIR/f54e"
4966         string="aaaaaa"
4967         cp -aL /dev/console $f
4968         echo $string > $f || error "echo $string to $f failed"
4969 }
4970 run_test 54e "console/tty device works in lustre ======================"
4971
4972 test_56a() {
4973         local numfiles=3
4974         local dir=$DIR/$tdir
4975
4976         rm -rf $dir
4977         test_mkdir -p $dir/dir
4978         for i in $(seq $numfiles); do
4979                 touch $dir/file$i
4980                 touch $dir/dir/file$i
4981         done
4982
4983         local numcomp=$($LFS getstripe --component-count $dir)
4984
4985         [[ $numcomp == 0 ]] && numcomp=1
4986
4987         # test lfs getstripe with --recursive
4988         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
4989
4990         [[ $filenum -eq $((numfiles * 2)) ]] ||
4991                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
4992         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
4993         [[ $filenum -eq $numfiles ]] ||
4994                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
4995         echo "$LFS getstripe showed obdidx or l_ost_idx"
4996
4997         # test lfs getstripe with file instead of dir
4998         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
4999         [[ $filenum -eq 1 ]] ||
5000                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
5001         echo "$LFS getstripe file1 passed"
5002
5003         #test lfs getstripe with --verbose
5004         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
5005         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5006                 error "$LFS getstripe --verbose $dir: "\
5007                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
5008         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
5009                 error "$LFS getstripe $dir: showed lmm_magic"
5010
5011         #test lfs getstripe with -v prints lmm_fid
5012         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
5013         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5014                 error "$LFS getstripe -v $dir: "\
5015                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
5016         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
5017                 error "$LFS getstripe $dir: showed lmm_fid by default"
5018         echo "$LFS getstripe --verbose passed"
5019
5020         #check for FID information
5021         local fid1=$($LFS getstripe --fid $dir/file1)
5022         local fid2=$($LFS getstripe --verbose $dir/file1 |
5023                      awk '/lmm_fid: / { print $2; exit; }')
5024         local fid3=$($LFS path2fid $dir/file1)
5025
5026         [ "$fid1" != "$fid2" ] &&
5027                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
5028         [ "$fid1" != "$fid3" ] &&
5029                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
5030         echo "$LFS getstripe --fid passed"
5031
5032         #test lfs getstripe with --obd
5033         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
5034                 error "$LFS getstripe --obd wrong_uuid: should return error"
5035
5036         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5037
5038         local ostidx=1
5039         local obduuid=$(ostuuid_from_index $ostidx)
5040         local found=$($LFS getstripe -r --obd $obduuid $dir |
5041                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
5042
5043         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
5044         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
5045                 ((filenum--))
5046         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
5047                 ((filenum--))
5048
5049         [[ $found -eq $filenum ]] ||
5050                 error "$LFS getstripe --obd: found $found expect $filenum"
5051         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
5052                 sed '/^[         ]*'${ostidx}'[  ]/d' |
5053                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
5054                 error "$LFS getstripe --obd: should not show file on other obd"
5055         echo "$LFS getstripe --obd passed"
5056 }
5057 run_test 56a "check $LFS getstripe"
5058
5059 test_56b() {
5060         local dir=$DIR/$tdir
5061         local numdirs=3
5062
5063         test_mkdir $dir
5064         for i in $(seq $numdirs); do
5065                 test_mkdir $dir/dir$i
5066         done
5067
5068         # test lfs getdirstripe default mode is non-recursion, which is
5069         # different from lfs getstripe
5070         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
5071
5072         [[ $dircnt -eq 1 ]] ||
5073                 error "$LFS getdirstripe: found $dircnt, not 1"
5074         dircnt=$($LFS getdirstripe --recursive $dir |
5075                 grep -c lmv_stripe_count)
5076         [[ $dircnt -eq $((numdirs + 1)) ]] ||
5077                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
5078 }
5079 run_test 56b "check $LFS getdirstripe"
5080
5081 test_56c() {
5082         remote_ost_nodsh && skip "remote OST with nodsh"
5083
5084         local ost_idx=0
5085         local ost_name=$(ostname_from_index $ost_idx)
5086         local old_status=$(ost_dev_status $ost_idx)
5087
5088         [[ -z "$old_status" ]] ||
5089                 skip_env "OST $ost_name is in $old_status status"
5090
5091         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
5092         [[ $OST1_VERSION -ge $(version_code 2.12.3) ]] && do_facet ost1 \
5093                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
5094         sleep_maxage
5095
5096         local new_status=$(ost_dev_status $ost_idx)
5097
5098         [[ "$new_status" =~ "D" ]] ||
5099                 error "$ost_name status is '$new_status', missing 'D'"
5100         if [[ $OST1_VERSION -ge $(version_code 2.12.3) ]]; then
5101                 [[ "$new_status" =~ "N" ]] ||
5102                         error "$ost_name status is '$new_status', missing 'N'"
5103         fi
5104
5105         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
5106         [[ $OST1_VERSION -ge $(version_code 2.12.3) ]] && do_facet ost1 \
5107                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
5108         sleep_maxage
5109
5110         new_status=$(ost_dev_status $ost_idx)
5111         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
5112                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
5113 }
5114 run_test 56c "check 'lfs df' showing device status"
5115
5116 NUMFILES=3
5117 NUMDIRS=3
5118 setup_56() {
5119         local local_tdir="$1"
5120         local local_numfiles="$2"
5121         local local_numdirs="$3"
5122         local dir_params="$4"
5123         local dir_stripe_params="$5"
5124
5125         if [ ! -d "$local_tdir" ] ; then
5126                 test_mkdir -p $dir_stripe_params $local_tdir
5127                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
5128                 for i in $(seq $local_numfiles) ; do
5129                         touch $local_tdir/file$i
5130                 done
5131                 for i in $(seq $local_numdirs) ; do
5132                         test_mkdir $dir_stripe_params $local_tdir/dir$i
5133                         for j in $(seq $local_numfiles) ; do
5134                                 touch $local_tdir/dir$i/file$j
5135                         done
5136                 done
5137         fi
5138 }
5139
5140 setup_56_special() {
5141         local local_tdir=$1
5142         local local_numfiles=$2
5143         local local_numdirs=$3
5144
5145         setup_56 $local_tdir $local_numfiles $local_numdirs
5146
5147         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
5148                 for i in $(seq $local_numfiles) ; do
5149                         mknod $local_tdir/loop${i}b b 7 $i
5150                         mknod $local_tdir/null${i}c c 1 3
5151                         ln -s $local_tdir/file1 $local_tdir/link${i}
5152                 done
5153                 for i in $(seq $local_numdirs) ; do
5154                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
5155                         mknod $local_tdir/dir$i/null${i}c c 1 3
5156                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
5157                 done
5158         fi
5159 }
5160
5161 test_56g() {
5162         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5163         local expected=$(($NUMDIRS + 2))
5164
5165         setup_56 $dir $NUMFILES $NUMDIRS
5166
5167         # test lfs find with -name
5168         for i in $(seq $NUMFILES) ; do
5169                 local nums=$($LFS find -name "*$i" $dir | wc -l)
5170
5171                 [ $nums -eq $expected ] ||
5172                         error "lfs find -name '*$i' $dir wrong: "\
5173                               "found $nums, expected $expected"
5174         done
5175 }
5176 run_test 56g "check lfs find -name"
5177
5178 test_56h() {
5179         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5180         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
5181
5182         setup_56 $dir $NUMFILES $NUMDIRS
5183
5184         # test lfs find with ! -name
5185         for i in $(seq $NUMFILES) ; do
5186                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
5187
5188                 [ $nums -eq $expected ] ||
5189                         error "lfs find ! -name '*$i' $dir wrong: "\
5190                               "found $nums, expected $expected"
5191         done
5192 }
5193 run_test 56h "check lfs find ! -name"
5194
5195 test_56i() {
5196         local dir=$DIR/$tdir
5197
5198         test_mkdir $dir
5199
5200         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
5201         local out=$($cmd)
5202
5203         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
5204 }
5205 run_test 56i "check 'lfs find -ost UUID' skips directories"
5206
5207 test_56j() {
5208         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5209
5210         setup_56_special $dir $NUMFILES $NUMDIRS
5211
5212         local expected=$((NUMDIRS + 1))
5213         local cmd="$LFS find -type d $dir"
5214         local nums=$($cmd | wc -l)
5215
5216         [ $nums -eq $expected ] ||
5217                 error "'$cmd' wrong: found $nums, expected $expected"
5218 }
5219 run_test 56j "check lfs find -type d"
5220
5221 test_56k() {
5222         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5223
5224         setup_56_special $dir $NUMFILES $NUMDIRS
5225
5226         local expected=$(((NUMDIRS + 1) * NUMFILES))
5227         local cmd="$LFS find -type f $dir"
5228         local nums=$($cmd | wc -l)
5229
5230         [ $nums -eq $expected ] ||
5231                 error "'$cmd' wrong: found $nums, expected $expected"
5232 }
5233 run_test 56k "check lfs find -type f"
5234
5235 test_56l() {
5236         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5237
5238         setup_56_special $dir $NUMFILES $NUMDIRS
5239
5240         local expected=$((NUMDIRS + NUMFILES))
5241         local cmd="$LFS find -type b $dir"
5242         local nums=$($cmd | wc -l)
5243
5244         [ $nums -eq $expected ] ||
5245                 error "'$cmd' wrong: found $nums, expected $expected"
5246 }
5247 run_test 56l "check lfs find -type b"
5248
5249 test_56m() {
5250         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5251
5252         setup_56_special $dir $NUMFILES $NUMDIRS
5253
5254         local expected=$((NUMDIRS + NUMFILES))
5255         local cmd="$LFS find -type c $dir"
5256         local nums=$($cmd | wc -l)
5257         [ $nums -eq $expected ] ||
5258                 error "'$cmd' wrong: found $nums, expected $expected"
5259 }
5260 run_test 56m "check lfs find -type c"
5261
5262 test_56n() {
5263         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5264         setup_56_special $dir $NUMFILES $NUMDIRS
5265
5266         local expected=$((NUMDIRS + NUMFILES))
5267         local cmd="$LFS find -type l $dir"
5268         local nums=$($cmd | wc -l)
5269
5270         [ $nums -eq $expected ] ||
5271                 error "'$cmd' wrong: found $nums, expected $expected"
5272 }
5273 run_test 56n "check lfs find -type l"
5274
5275 test_56o() {
5276         local dir=$DIR/$tdir
5277
5278         setup_56 $dir $NUMFILES $NUMDIRS
5279         utime $dir/file1 > /dev/null || error "utime (1)"
5280         utime $dir/file2 > /dev/null || error "utime (2)"
5281         utime $dir/dir1 > /dev/null || error "utime (3)"
5282         utime $dir/dir2 > /dev/null || error "utime (4)"
5283         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5284         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5285
5286         local expected=4
5287         local nums=$($LFS find -mtime +0 $dir | wc -l)
5288
5289         [ $nums -eq $expected ] ||
5290                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5291
5292         expected=12
5293         cmd="$LFS find -mtime 0 $dir"
5294         nums=$($cmd | wc -l)
5295         [ $nums -eq $expected ] ||
5296                 error "'$cmd' wrong: found $nums, expected $expected"
5297 }
5298 run_test 56o "check lfs find -mtime for old files"
5299
5300 test_56p() {
5301         [ $RUNAS_ID -eq $UID ] &&
5302                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5303
5304         local dir=$DIR/$tdir
5305
5306         setup_56 $dir $NUMFILES $NUMDIRS
5307         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
5308
5309         local expected=$NUMFILES
5310         local cmd="$LFS find -uid $RUNAS_ID $dir"
5311         local nums=$($cmd | wc -l)
5312
5313         [ $nums -eq $expected ] ||
5314                 error "'$cmd' wrong: found $nums, expected $expected"
5315
5316         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
5317         cmd="$LFS find ! -uid $RUNAS_ID $dir"
5318         nums=$($cmd | wc -l)
5319         [ $nums -eq $expected ] ||
5320                 error "'$cmd' wrong: found $nums, expected $expected"
5321 }
5322 run_test 56p "check lfs find -uid and ! -uid"
5323
5324 test_56q() {
5325         [ $RUNAS_ID -eq $UID ] &&
5326                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5327
5328         local dir=$DIR/$tdir
5329
5330         setup_56 $dir $NUMFILES $NUMDIRS
5331         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
5332
5333         local expected=$NUMFILES
5334         local cmd="$LFS find -gid $RUNAS_GID $dir"
5335         local nums=$($cmd | wc -l)
5336
5337         [ $nums -eq $expected ] ||
5338                 error "'$cmd' wrong: found $nums, expected $expected"
5339
5340         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
5341         cmd="$LFS find ! -gid $RUNAS_GID $dir"
5342         nums=$($cmd | wc -l)
5343         [ $nums -eq $expected ] ||
5344                 error "'$cmd' wrong: found $nums, expected $expected"
5345 }
5346 run_test 56q "check lfs find -gid and ! -gid"
5347
5348 test_56r() {
5349         local dir=$DIR/$tdir
5350
5351         setup_56 $dir $NUMFILES $NUMDIRS
5352
5353         local expected=12
5354         local cmd="$LFS find -size 0 -type f -lazy $dir"
5355         local nums=$($cmd | wc -l)
5356
5357         [ $nums -eq $expected ] ||
5358                 error "'$cmd' wrong: found $nums, expected $expected"
5359         cmd="$LFS find -size 0 -type f $dir"
5360         nums=$($cmd | wc -l)
5361         [ $nums -eq $expected ] ||
5362                 error "'$cmd' wrong: found $nums, expected $expected"
5363
5364         expected=0
5365         cmd="$LFS find ! -size 0 -type f -lazy $dir"
5366         nums=$($cmd | wc -l)
5367         [ $nums -eq $expected ] ||
5368                 error "'$cmd' wrong: found $nums, expected $expected"
5369         cmd="$LFS find ! -size 0 -type f $dir"
5370         nums=$($cmd | wc -l)
5371         [ $nums -eq $expected ] ||
5372                 error "'$cmd' wrong: found $nums, expected $expected"
5373
5374         echo "test" > $dir/$tfile
5375         echo "test2" > $dir/$tfile.2 && sync
5376         expected=1
5377         cmd="$LFS find -size 5 -type f -lazy $dir"
5378         nums=$($cmd | wc -l)
5379         [ $nums -eq $expected ] ||
5380                 error "'$cmd' wrong: found $nums, expected $expected"
5381         cmd="$LFS find -size 5 -type f $dir"
5382         nums=$($cmd | wc -l)
5383         [ $nums -eq $expected ] ||
5384                 error "'$cmd' wrong: found $nums, expected $expected"
5385
5386         expected=1
5387         cmd="$LFS find -size +5 -type f -lazy $dir"
5388         nums=$($cmd | wc -l)
5389         [ $nums -eq $expected ] ||
5390                 error "'$cmd' wrong: found $nums, expected $expected"
5391         cmd="$LFS find -size +5 -type f $dir"
5392         nums=$($cmd | wc -l)
5393         [ $nums -eq $expected ] ||
5394                 error "'$cmd' wrong: found $nums, expected $expected"
5395
5396         expected=2
5397         cmd="$LFS find -size +0 -type f -lazy $dir"
5398         nums=$($cmd | wc -l)
5399         [ $nums -eq $expected ] ||
5400                 error "'$cmd' wrong: found $nums, expected $expected"
5401         cmd="$LFS find -size +0 -type f $dir"
5402         nums=$($cmd | wc -l)
5403         [ $nums -eq $expected ] ||
5404                 error "'$cmd' wrong: found $nums, expected $expected"
5405
5406         expected=2
5407         cmd="$LFS find ! -size -5 -type f -lazy $dir"
5408         nums=$($cmd | wc -l)
5409         [ $nums -eq $expected ] ||
5410                 error "'$cmd' wrong: found $nums, expected $expected"
5411         cmd="$LFS find ! -size -5 -type f $dir"
5412         nums=$($cmd | wc -l)
5413         [ $nums -eq $expected ] ||
5414                 error "'$cmd' wrong: found $nums, expected $expected"
5415
5416         expected=12
5417         cmd="$LFS find -size -5 -type f -lazy $dir"
5418         nums=$($cmd | wc -l)
5419         [ $nums -eq $expected ] ||
5420                 error "'$cmd' wrong: found $nums, expected $expected"
5421         cmd="$LFS find -size -5 -type f $dir"
5422         nums=$($cmd | wc -l)
5423         [ $nums -eq $expected ] ||
5424                 error "'$cmd' wrong: found $nums, expected $expected"
5425 }
5426 run_test 56r "check lfs find -size works"
5427
5428 test_56ra_sub() {
5429         local expected=$1
5430         local glimpses=$2
5431         local cmd="$3"
5432
5433         cancel_lru_locks $OSC
5434
5435         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5436         local nums=$($cmd | wc -l)
5437
5438         [ $nums -eq $expected ] ||
5439                 error "'$cmd' wrong: found $nums, expected $expected"
5440
5441         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5442
5443         if (( rpcs_before + glimpses != rpcs_after )); then
5444                 echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
5445                 $LCTL get_param osc.*.stats | grep ldlm_glimpse_enqueue
5446
5447                 if [[ $glimpses == 0 ]]; then
5448                         error "'$cmd' should not send glimpse RPCs to OST"
5449                 else
5450                         error "'$cmd' should send $glimpses glimpse RPCs to OST"
5451                 fi
5452         fi
5453 }
5454
5455 test_56ra() {
5456         [[ $MDS1_VERSION -ge $(version_code 2.12.4) ]] ||
5457                 skip "MDS < 2.12.4 doesn't return LSOM data"
5458         local dir=$DIR/$tdir
5459
5460         [[ $OSC == "mdc" ]] && skip "DoM files"
5461
5462         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
5463         # open and close all files to ensure LSOM is updated
5464         cancel_lru_locks $OSC
5465         find $dir -type f | xargs cat > /dev/null
5466
5467         #   expect_found  glimpse_rpcs  command_to_run
5468         test_56ra_sub 12  0 "$LFS find -size 0 -type f -lazy $dir"
5469         test_56ra_sub 12 12 "$LFS find -size 0 -type f $dir"
5470         test_56ra_sub  0  0 "$LFS find ! -size 0 -type f -lazy $dir"
5471         test_56ra_sub  0 12 "$LFS find ! -size 0 -type f $dir"
5472
5473         echo "test" > $dir/$tfile
5474         echo "test2" > $dir/$tfile.2 && sync
5475         cancel_lru_locks $OSC
5476         cat $dir/$tfile $dir/$tfile.2 > /dev/null
5477
5478         test_56ra_sub  1  0 "$LFS find -size 5 -type f -lazy $dir"
5479         test_56ra_sub  1 14 "$LFS find -size 5 -type f $dir"
5480         test_56ra_sub  1  0 "$LFS find -size +5 -type f -lazy $dir"
5481         test_56ra_sub  1 14 "$LFS find -size +5 -type f $dir"
5482
5483         test_56ra_sub  2  0 "$LFS find -size +0 -type f -lazy $dir"
5484         test_56ra_sub  2 14 "$LFS find -size +0 -type f $dir"
5485         test_56ra_sub  2  0 "$LFS find ! -size -5 -type f -lazy $dir"
5486         test_56ra_sub  2 14 "$LFS find ! -size -5 -type f $dir"
5487         test_56ra_sub 12  0 "$LFS find -size -5 -type f -lazy $dir"
5488         test_56ra_sub 12 14 "$LFS find -size -5 -type f $dir"
5489 }
5490 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
5491
5492 test_56s() { # LU-611 #LU-9369
5493         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
5494
5495         local dir=$DIR/$tdir
5496         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
5497
5498         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
5499         for i in $(seq $NUMDIRS); do
5500                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
5501         done
5502
5503         local expected=$NUMDIRS
5504         local cmd="$LFS find -c $OSTCOUNT $dir"
5505         local nums=$($cmd | wc -l)
5506
5507         [ $nums -eq $expected ] || {
5508                 $LFS getstripe -R $dir
5509                 error "'$cmd' wrong: found $nums, expected $expected"
5510         }
5511
5512         expected=$((NUMDIRS + onestripe))
5513         cmd="$LFS find -stripe-count +0 -type f $dir"
5514         nums=$($cmd | wc -l)
5515         [ $nums -eq $expected ] || {
5516                 $LFS getstripe -R $dir
5517                 error "'$cmd' wrong: found $nums, expected $expected"
5518         }
5519
5520         expected=$onestripe
5521         cmd="$LFS find -stripe-count 1 -type f $dir"
5522         nums=$($cmd | wc -l)
5523         [ $nums -eq $expected ] || {
5524                 $LFS getstripe -R $dir
5525                 error "'$cmd' wrong: found $nums, expected $expected"
5526         }
5527
5528         cmd="$LFS find -stripe-count -2 -type f $dir"
5529         nums=$($cmd | wc -l)
5530         [ $nums -eq $expected ] || {
5531                 $LFS getstripe -R $dir
5532                 error "'$cmd' wrong: found $nums, expected $expected"
5533         }
5534
5535         expected=0
5536         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
5537         nums=$($cmd | wc -l)
5538         [ $nums -eq $expected ] || {
5539                 $LFS getstripe -R $dir
5540                 error "'$cmd' wrong: found $nums, expected $expected"
5541         }
5542 }
5543 run_test 56s "check lfs find -stripe-count works"
5544
5545 test_56t() { # LU-611 #LU-9369
5546         local dir=$DIR/$tdir
5547
5548         setup_56 $dir 0 $NUMDIRS
5549         for i in $(seq $NUMDIRS); do
5550                 $LFS setstripe -S 8M $dir/dir$i/$tfile
5551         done
5552
5553         local expected=$NUMDIRS
5554         local cmd="$LFS find -S 8M $dir"
5555         local nums=$($cmd | wc -l)
5556
5557         [ $nums -eq $expected ] || {
5558                 $LFS getstripe -R $dir
5559                 error "'$cmd' wrong: found $nums, expected $expected"
5560         }
5561         rm -rf $dir
5562
5563         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
5564
5565         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
5566
5567         expected=$(((NUMDIRS + 1) * NUMFILES))
5568         cmd="$LFS find -stripe-size 512k -type f $dir"
5569         nums=$($cmd | wc -l)
5570         [ $nums -eq $expected ] ||
5571                 error "'$cmd' wrong: found $nums, expected $expected"
5572
5573         cmd="$LFS find -stripe-size +320k -type f $dir"
5574         nums=$($cmd | wc -l)
5575         [ $nums -eq $expected ] ||
5576                 error "'$cmd' wrong: found $nums, expected $expected"
5577
5578         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
5579         cmd="$LFS find -stripe-size +200k -type f $dir"
5580         nums=$($cmd | wc -l)
5581         [ $nums -eq $expected ] ||
5582                 error "'$cmd' wrong: found $nums, expected $expected"
5583
5584         cmd="$LFS find -stripe-size -640k -type f $dir"
5585         nums=$($cmd | wc -l)
5586         [ $nums -eq $expected ] ||
5587                 error "'$cmd' wrong: found $nums, expected $expected"
5588
5589         expected=4
5590         cmd="$LFS find -stripe-size 256k -type f $dir"
5591         nums=$($cmd | wc -l)
5592         [ $nums -eq $expected ] ||
5593                 error "'$cmd' wrong: found $nums, expected $expected"
5594
5595         cmd="$LFS find -stripe-size -320k -type f $dir"
5596         nums=$($cmd | wc -l)
5597         [ $nums -eq $expected ] ||
5598                 error "'$cmd' wrong: found $nums, expected $expected"
5599
5600         expected=0
5601         cmd="$LFS find -stripe-size 1024k -type f $dir"
5602         nums=$($cmd | wc -l)
5603         [ $nums -eq $expected ] ||
5604                 error "'$cmd' wrong: found $nums, expected $expected"
5605 }
5606 run_test 56t "check lfs find -stripe-size works"
5607
5608 test_56u() { # LU-611
5609         local dir=$DIR/$tdir
5610
5611         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
5612
5613         if [[ $OSTCOUNT -gt 1 ]]; then
5614                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
5615                 onestripe=4
5616         else
5617                 onestripe=0
5618         fi
5619
5620         local expected=$(((NUMDIRS + 1) * NUMFILES))
5621         local cmd="$LFS find -stripe-index 0 -type f $dir"
5622         local nums=$($cmd | wc -l)
5623
5624         [ $nums -eq $expected ] ||
5625                 error "'$cmd' wrong: found $nums, expected $expected"
5626
5627         expected=$onestripe
5628         cmd="$LFS find -stripe-index 1 -type f $dir"
5629         nums=$($cmd | wc -l)
5630         [ $nums -eq $expected ] ||
5631                 error "'$cmd' wrong: found $nums, expected $expected"
5632
5633         cmd="$LFS find ! -stripe-index 0 -type f $dir"
5634         nums=$($cmd | wc -l)
5635         [ $nums -eq $expected ] ||
5636                 error "'$cmd' wrong: found $nums, expected $expected"
5637
5638         expected=0
5639         # This should produce an error and not return any files
5640         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
5641         nums=$($cmd 2>/dev/null | wc -l)
5642         [ $nums -eq $expected ] ||
5643                 error "'$cmd' wrong: found $nums, expected $expected"
5644
5645         if [[ $OSTCOUNT -gt 1 ]]; then
5646                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
5647                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
5648                 nums=$($cmd | wc -l)
5649                 [ $nums -eq $expected ] ||
5650                         error "'$cmd' wrong: found $nums, expected $expected"
5651         fi
5652 }
5653 run_test 56u "check lfs find -stripe-index works"
5654
5655 test_56v() {
5656         local mdt_idx=0
5657         local dir=$DIR/$tdir
5658
5659         setup_56 $dir $NUMFILES $NUMDIRS
5660
5661         UUID=$(mdtuuid_from_index $mdt_idx $dir)
5662         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
5663
5664         for file in $($LFS find -m $UUID $dir); do
5665                 file_midx=$($LFS getstripe -m $file)
5666                 [ $file_midx -eq $mdt_idx ] ||
5667                         error "lfs find -m $UUID != getstripe -m $file_midx"
5668         done
5669 }
5670 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
5671
5672 test_56w() {
5673         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5674         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5675
5676         local dir=$DIR/$tdir
5677
5678         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
5679
5680         local stripe_size=$($LFS getstripe -S -d $dir) ||
5681                 error "$LFS getstripe -S -d $dir failed"
5682         stripe_size=${stripe_size%% *}
5683
5684         local file_size=$((stripe_size * OSTCOUNT))
5685         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
5686         local required_space=$((file_num * file_size))
5687         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
5688                            head -n1)
5689         [[ $free_space -le $((required_space / 1024)) ]] &&
5690                 skip_env "need $required_space, have $free_space kbytes"
5691
5692         local dd_bs=65536
5693         local dd_count=$((file_size / dd_bs))
5694
5695         # write data into the files
5696         local i
5697         local j
5698         local file
5699
5700         for i in $(seq $NUMFILES); do
5701                 file=$dir/file$i
5702                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5703                         error "write data into $file failed"
5704         done
5705         for i in $(seq $NUMDIRS); do
5706                 for j in $(seq $NUMFILES); do
5707                         file=$dir/dir$i/file$j
5708                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5709                                 error "write data into $file failed"
5710                 done
5711         done
5712
5713         # $LFS_MIGRATE will fail if hard link migration is unsupported
5714         if [[ $(lustre_version_code mds1) -gt $(version_code 2.5.55) ]]; then
5715                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
5716                         error "creating links to $dir/dir1/file1 failed"
5717         fi
5718
5719         local expected=-1
5720
5721         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
5722
5723         # lfs_migrate file
5724         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
5725
5726         echo "$cmd"
5727         eval $cmd || error "$cmd failed"
5728
5729         check_stripe_count $dir/file1 $expected
5730
5731         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
5732         then
5733                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
5734                 # OST 1 if it is on OST 0. This file is small enough to
5735                 # be on only one stripe.
5736                 file=$dir/migr_1_ost
5737                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
5738                         error "write data into $file failed"
5739                 local obdidx=$($LFS getstripe -i $file)
5740                 local oldmd5=$(md5sum $file)
5741                 local newobdidx=0
5742
5743                 [[ $obdidx -eq 0 ]] && newobdidx=1
5744                 cmd="$LFS migrate -i $newobdidx $file"
5745                 echo $cmd
5746                 eval $cmd || error "$cmd failed"
5747
5748                 local realobdix=$($LFS getstripe -i $file)
5749                 local newmd5=$(md5sum $file)
5750
5751                 [[ $newobdidx -ne $realobdix ]] &&
5752                         error "new OST is different (was=$obdidx, "\
5753                               "wanted=$newobdidx, got=$realobdix)"
5754                 [[ "$oldmd5" != "$newmd5" ]] &&
5755                         error "md5sum differ: $oldmd5, $newmd5"
5756         fi
5757
5758         # lfs_migrate dir
5759         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
5760         echo "$cmd"
5761         eval $cmd || error "$cmd failed"
5762
5763         for j in $(seq $NUMFILES); do
5764                 check_stripe_count $dir/dir1/file$j $expected
5765         done
5766
5767         # lfs_migrate works with lfs find
5768         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
5769              $LFS_MIGRATE -y -c $expected"
5770         echo "$cmd"
5771         eval $cmd || error "$cmd failed"
5772
5773         for i in $(seq 2 $NUMFILES); do
5774                 check_stripe_count $dir/file$i $expected
5775         done
5776         for i in $(seq 2 $NUMDIRS); do
5777                 for j in $(seq $NUMFILES); do
5778                 check_stripe_count $dir/dir$i/file$j $expected
5779                 done
5780         done
5781 }
5782 run_test 56w "check lfs_migrate -c stripe_count works"
5783
5784 test_56wb() {
5785         local file1=$DIR/$tdir/file1
5786         local create_pool=false
5787         local initial_pool=$($LFS getstripe -p $DIR)
5788         local pool_list=()
5789         local pool=""
5790
5791         echo -n "Creating test dir..."
5792         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5793         echo "done."
5794
5795         echo -n "Creating test file..."
5796         touch $file1 || error "cannot create file"
5797         echo "done."
5798
5799         echo -n "Detecting existing pools..."
5800         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
5801
5802         if [ ${#pool_list[@]} -gt 0 ]; then
5803                 echo "${pool_list[@]}"
5804                 for thispool in "${pool_list[@]}"; do
5805                         if [[ -z "$initial_pool" ||
5806                               "$initial_pool" != "$thispool" ]]; then
5807                                 pool="$thispool"
5808                                 echo "Using existing pool '$pool'"
5809                                 break
5810                         fi
5811                 done
5812         else
5813                 echo "none detected."
5814         fi
5815         if [ -z "$pool" ]; then
5816                 pool=${POOL:-testpool}
5817                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
5818                 echo -n "Creating pool '$pool'..."
5819                 create_pool=true
5820                 pool_add $pool &> /dev/null ||
5821                         error "pool_add failed"
5822                 echo "done."
5823
5824                 echo -n "Adding target to pool..."
5825                 pool_add_targets $pool 0 0 1 &> /dev/null ||
5826                         error "pool_add_targets failed"
5827                 echo "done."
5828         fi
5829
5830         echo -n "Setting pool using -p option..."
5831         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
5832                 error "migrate failed rc = $?"
5833         echo "done."
5834
5835         echo -n "Verifying test file is in pool after migrating..."
5836         [ "$($LFS getstripe -p $file1)" = $pool ] ||
5837                 error "file was not migrated to pool $pool"
5838         echo "done."
5839
5840         echo -n "Removing test file from pool '$pool'..."
5841         # "lfs migrate $file" won't remove the file from the pool
5842         # until some striping information is changed.
5843         $LFS migrate -c 1 $file1 &> /dev/null ||
5844                 error "cannot remove from pool"
5845         [ "$($LFS getstripe -p $file1)" ] &&
5846                 error "pool still set"
5847         echo "done."
5848
5849         echo -n "Setting pool using --pool option..."
5850         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
5851                 error "migrate failed rc = $?"
5852         echo "done."
5853
5854         # Clean up
5855         rm -f $file1
5856         if $create_pool; then
5857                 destroy_test_pools 2> /dev/null ||
5858                         error "destroy test pools failed"
5859         fi
5860 }
5861 run_test 56wb "check lfs_migrate pool support"
5862
5863 test_56wc() {
5864         local file1="$DIR/$tdir/file1"
5865         local parent_ssize
5866         local parent_scount
5867         local cur_ssize
5868         local cur_scount
5869         local orig_ssize
5870
5871         echo -n "Creating test dir..."
5872         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5873         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
5874                 error "cannot set stripe by '-S 1M -c 1'"
5875         echo "done"
5876
5877         echo -n "Setting initial stripe for test file..."
5878         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
5879                 error "cannot set stripe"
5880         cur_ssize=$($LFS getstripe -S "$file1")
5881         [ $cur_ssize -eq 524288 ] || error "setstripe -S $cur_ssize != 524288"
5882         echo "done."
5883
5884         # File currently set to -S 512K -c 1
5885
5886         # Ensure -c and -S options are rejected when -R is set
5887         echo -n "Verifying incompatible options are detected..."
5888         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
5889                 error "incompatible -c and -R options not detected"
5890         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
5891                 error "incompatible -S and -R options not detected"
5892         echo "done."
5893
5894         # Ensure unrecognized options are passed through to 'lfs migrate'
5895         echo -n "Verifying -S option is passed through to lfs migrate..."
5896         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
5897                 error "migration failed"
5898         cur_ssize=$($LFS getstripe -S "$file1")
5899         [ $cur_ssize -eq 1048576 ] || error "migrate -S $cur_ssize != 1048576"
5900         echo "done."
5901
5902         # File currently set to -S 1M -c 1
5903
5904         # Ensure long options are supported
5905         echo -n "Verifying long options supported..."
5906         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
5907                 error "long option without argument not supported"
5908         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
5909                 error "long option with argument not supported"
5910         cur_ssize=$($LFS getstripe -S "$file1")
5911         [ $cur_ssize -eq 524288 ] ||
5912                 error "migrate --stripe-size $cur_ssize != 524288"
5913         echo "done."
5914
5915         # File currently set to -S 512K -c 1
5916
5917         if [ "$OSTCOUNT" -gt 1 ]; then
5918                 echo -n "Verifying explicit stripe count can be set..."
5919                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
5920                         error "migrate failed"
5921                 cur_scount=$($LFS getstripe -c "$file1")
5922                 [ $cur_scount -eq 2 ] || error "migrate -c $cur_scount != 2"
5923                 echo "done."
5924         fi
5925
5926         # File currently set to -S 512K -c 1 or -S 512K -c 2
5927
5928         # Ensure parent striping is used if -R is set, and no stripe
5929         # count or size is specified
5930         echo -n "Setting stripe for parent directory..."
5931         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
5932                 error "cannot set stripe '-S 2M -c 1'"
5933         echo "done."
5934
5935         echo -n "Verifying restripe option uses parent stripe settings..."
5936         parent_ssize=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
5937         parent_scount=$($LFS getstripe -c $DIR/$tdir 2>/dev/null)
5938         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
5939                 error "migrate failed"
5940         cur_ssize=$($LFS getstripe -S "$file1")
5941         [ $cur_ssize -eq $parent_ssize ] ||
5942                 error "migrate -R stripe_size $cur_ssize != $parent_ssize"
5943         cur_scount=$($LFS getstripe -c "$file1")
5944         [ $cur_scount -eq $parent_scount ] ||
5945                 error "migrate -R stripe_count $cur_scount != $parent_scount"
5946         echo "done."
5947
5948         # File currently set to -S 1M -c 1
5949
5950         # Ensure striping is preserved if -R is not set, and no stripe
5951         # count or size is specified
5952         echo -n "Verifying striping size preserved when not specified..."
5953         orig_ssize=$($LFS getstripe -S "$file1" 2>/dev/null)
5954         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
5955                 error "cannot set stripe on parent directory"
5956         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5957                 error "migrate failed"
5958         cur_ssize=$($LFS getstripe -S "$file1")
5959         [ $cur_ssize -eq $orig_ssize ] ||
5960                 error "migrate by default $cur_ssize != $orig_ssize"
5961         echo "done."
5962
5963         # Ensure file name properly detected when final option has no argument
5964         echo -n "Verifying file name properly detected..."
5965         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5966                 error "file name interpreted as option argument"
5967         echo "done."
5968
5969         # Clean up
5970         rm -f "$file1"
5971 }
5972 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
5973
5974 test_56wd() {
5975         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5976
5977         local file1=$DIR/$tdir/file1
5978
5979         echo -n "Creating test dir..."
5980         test_mkdir $DIR/$tdir || error "cannot create dir"
5981         echo "done."
5982
5983         echo -n "Creating test file..."
5984         touch $file1
5985         echo "done."
5986
5987         # Ensure 'lfs migrate' will fail by using a non-existent option,
5988         # and make sure rsync is not called to recover
5989         echo -n "Make sure --no-rsync option works..."
5990         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
5991                 grep -q 'refusing to fall back to rsync' ||
5992                 error "rsync was called with --no-rsync set"
5993         echo "done."
5994
5995         # Ensure rsync is called without trying 'lfs migrate' first
5996         echo -n "Make sure --rsync option works..."
5997         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
5998                 grep -q 'falling back to rsync' &&
5999                 error "lfs migrate was called with --rsync set"
6000         echo "done."
6001
6002         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
6003         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
6004                 grep -q 'at the same time' ||
6005                 error "--rsync and --no-rsync accepted concurrently"
6006         echo "done."
6007
6008         # Clean up
6009         rm -f $file1
6010 }
6011 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
6012
6013 test_56x() {
6014         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6015         check_swap_layouts_support
6016
6017         local dir=$DIR/$tdir
6018         local ref1=/etc/passwd
6019         local file1=$dir/file1
6020
6021         test_mkdir $dir || error "creating dir $dir"
6022         $LFS setstripe -c 2 $file1
6023         cp $ref1 $file1
6024         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
6025         stripe=$($LFS getstripe -c $file1)
6026         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6027         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6028
6029         # clean up
6030         rm -f $file1
6031 }
6032 run_test 56x "lfs migration support"
6033
6034 test_56xa() {
6035         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6036         check_swap_layouts_support
6037
6038         local dir=$DIR/$tdir/$testnum
6039
6040         test_mkdir -p $dir
6041
6042         local ref1=/etc/passwd
6043         local file1=$dir/file1
6044
6045         $LFS setstripe -c 2 $file1
6046         cp $ref1 $file1
6047         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
6048
6049         local stripe=$($LFS getstripe -c $file1)
6050
6051         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6052         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6053
6054         # clean up
6055         rm -f $file1
6056 }
6057 run_test 56xa "lfs migration --block support"
6058
6059 check_migrate_links() {
6060         local dir="$1"
6061         local file1="$dir/file1"
6062         local begin="$2"
6063         local count="$3"
6064         local total_count=$(($begin + $count - 1))
6065         local symlink_count=10
6066         local uniq_count=10
6067
6068         if [ ! -f "$file1" ]; then
6069                 echo -n "creating initial file..."
6070                 $LFS setstripe -c 1 -S "512k" "$file1" ||
6071                         error "cannot setstripe initial file"
6072                 echo "done"
6073
6074                 echo -n "creating symlinks..."
6075                 for s in $(seq 1 $symlink_count); do
6076                         ln -s "$file1" "$dir/slink$s" ||
6077                                 error "cannot create symlinks"
6078                 done
6079                 echo "done"
6080
6081                 echo -n "creating nonlinked files..."
6082                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
6083                         error "cannot create nonlinked files"
6084                 echo "done"
6085         fi
6086
6087         # create hard links
6088         if [ ! -f "$dir/file$total_count" ]; then
6089                 echo -n "creating hard links $begin:$total_count..."
6090                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
6091                         /dev/null || error "cannot create hard links"
6092                 echo "done"
6093         fi
6094
6095         echo -n "checking number of hard links listed in xattrs..."
6096         local fid=$($LFS getstripe -F "$file1")
6097         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
6098
6099         echo "${#paths[*]}"
6100         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
6101                         skip "hard link list has unexpected size, skipping test"
6102         fi
6103         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
6104                         error "link names should exceed xattrs size"
6105         fi
6106
6107         echo -n "migrating files..."
6108         local migrate_out=$($LFS_MIGRATE -y -S '1m' $dir)
6109         local rc=$?
6110         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
6111         echo "done"
6112
6113         # make sure all links have been properly migrated
6114         echo -n "verifying files..."
6115         fid=$($LFS getstripe -F "$file1") ||
6116                 error "cannot get fid for file $file1"
6117         for i in $(seq 2 $total_count); do
6118                 local fid2=$($LFS getstripe -F $dir/file$i)
6119
6120                 [ "$fid2" == "$fid" ] ||
6121                         error "migrated hard link has mismatched FID"
6122         done
6123
6124         # make sure hard links were properly detected, and migration was
6125         # performed only once for the entire link set; nonlinked files should
6126         # also be migrated
6127         local actual=$(grep -c 'done' <<< "$migrate_out")
6128         local expected=$(($uniq_count + 1))
6129
6130         [ "$actual" -eq  "$expected" ] ||
6131                 error "hard links individually migrated ($actual != $expected)"
6132
6133         # make sure the correct number of hard links are present
6134         local hardlinks=$(stat -c '%h' "$file1")
6135
6136         [ $hardlinks -eq $total_count ] ||
6137                 error "num hard links $hardlinks != $total_count"
6138         echo "done"
6139
6140         return 0
6141 }
6142
6143 test_56xb() {
6144         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
6145                 skip "Need MDS version at least 2.10.55"
6146
6147         local dir="$DIR/$tdir"
6148
6149         test_mkdir "$dir" || error "cannot create dir $dir"
6150
6151         echo "testing lfs migrate mode when all links fit within xattrs"
6152         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
6153
6154         echo "testing rsync mode when all links fit within xattrs"
6155         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
6156
6157         echo "testing lfs migrate mode when all links do not fit within xattrs"
6158         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
6159
6160         echo "testing rsync mode when all links do not fit within xattrs"
6161         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
6162
6163
6164         # clean up
6165         rm -rf $dir
6166 }
6167 run_test 56xb "lfs migration hard link support"
6168
6169 test_56xc() {
6170         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6171
6172         local dir="$DIR/$tdir"
6173
6174         test_mkdir "$dir" || error "cannot create dir $dir"
6175
6176         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
6177         echo -n "Setting initial stripe for 20MB test file..."
6178         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
6179                 error "cannot setstripe 20MB file"
6180         echo "done"
6181         echo -n "Sizing 20MB test file..."
6182         truncate "$dir/20mb" 20971520 || error "cannot create 20MB test file"
6183         echo "done"
6184         echo -n "Verifying small file autostripe count is 1..."
6185         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
6186                 error "cannot migrate 20MB file"
6187         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
6188                 error "cannot get stripe for $dir/20mb"
6189         [ $stripe_count -eq 1 ] ||
6190                 error "unexpected stripe count $stripe_count for 20MB file"
6191         rm -f "$dir/20mb"
6192         echo "done"
6193
6194         # Test 2: File is small enough to fit within the available space on
6195         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
6196         # have at least an additional 1KB for each desired stripe for test 3
6197         echo -n "Setting stripe for 1GB test file..."
6198         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
6199         echo "done"
6200         echo -n "Sizing 1GB test file..."
6201         # File size is 1GB + 3KB
6202         truncate "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
6203         echo "done"
6204
6205         # need at least 512MB per OST for 1GB file to fit in 2 stripes
6206         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
6207         if (( avail > 524288 * OSTCOUNT )); then
6208                 echo -n "Migrating 1GB file..."
6209                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
6210                         error "cannot migrate 1GB file"
6211                 echo "done"
6212                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
6213                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
6214                         error "cannot getstripe for 1GB file"
6215                 [ $stripe_count -eq 2 ] ||
6216                         error "unexpected stripe count $stripe_count != 2"
6217                 echo "done"
6218         fi
6219
6220         # Test 3: File is too large to fit within the available space on
6221         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
6222         if [ $OSTCOUNT -ge 3 ]; then
6223                 # The required available space is calculated as
6224                 # file size (1GB + 3KB) / OST count (3).
6225                 local kb_per_ost=349526
6226
6227                 echo -n "Migrating 1GB file with limit..."
6228                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
6229                         error "cannot migrate 1GB file with limit"
6230                 echo "done"
6231
6232                 stripe_count=$($LFS getstripe -c "$dir/1gb")
6233                 echo -n "Verifying 1GB autostripe count with limited space..."
6234                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
6235                         error "unexpected stripe count $stripe_count (min 3)"
6236                 echo "done"
6237         fi
6238
6239         # clean up
6240         rm -rf $dir
6241 }
6242 run_test 56xc "lfs migration autostripe"
6243
6244 test_56xd() {
6245         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6246
6247         local dir=$DIR/$tdir
6248         local f_mgrt=$dir/$tfile.mgrt
6249         local f_yaml=$dir/$tfile.yaml
6250         local f_copy=$dir/$tfile.copy
6251         local layout_yaml="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
6252         local layout_copy="-c 2 -S 2M -i 1"
6253         local yamlfile=$dir/yamlfile
6254         local layout_before;
6255         local layout_after;
6256
6257         test_mkdir "$dir" || error "cannot create dir $dir"
6258         $LFS setstripe $layout_yaml $f_yaml ||
6259                 error "cannot setstripe $f_yaml with layout $layout_yaml"
6260         $LFS getstripe --yaml $f_yaml > $yamlfile
6261         $LFS setstripe $layout_copy $f_copy ||
6262                 error "cannot setstripe $f_copy with layout $layout_copy"
6263         touch $f_mgrt
6264         dd if=/dev/zero of=$f_mgrt bs=1M count=4
6265
6266         # 1. test option --yaml
6267         $LFS_MIGRATE -y --yaml $yamlfile $f_mgrt ||
6268                 error "cannot migrate $f_mgrt with --yaml $yamlfile"
6269         layout_before=$(get_layout_param $f_yaml)
6270         layout_after=$(get_layout_param $f_mgrt)
6271         [ "$layout_after" == "$layout_before" ] ||
6272                 error "lfs_migrate --yaml: $layout_after != $layout_before"
6273
6274         # 2. test option --copy
6275         $LFS_MIGRATE -y --copy $f_copy $f_mgrt ||
6276                 error "cannot migrate $f_mgrt with --copy $f_copy"
6277         layout_before=$(get_layout_param $f_copy)
6278         layout_after=$(get_layout_param $f_mgrt)
6279         [ "$layout_after" == "$layout_before" ] ||
6280                 error "lfs_migrate --copy: $layout_after != $layout_before"
6281 }
6282 run_test 56xd "check lfs_migrate --yaml and --copy support"
6283
6284 test_56xe() {
6285         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6286
6287         local dir=$DIR/$tdir
6288         local f_comp=$dir/$tfile
6289         local layout="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
6290         local layout_before=""
6291         local layout_after=""
6292
6293         test_mkdir "$dir" || error "cannot create dir $dir"
6294         $LFS setstripe $layout $f_comp ||
6295                 error "cannot setstripe $f_comp with layout $layout"
6296         layout_before=$(get_layout_param $f_comp)
6297         dd if=/dev/zero of=$f_comp bs=1M count=4
6298
6299         # 1. migrate a comp layout file by lfs_migrate
6300         $LFS_MIGRATE -y $f_comp || error "cannot migrate $f_comp by lfs_migrate"
6301         layout_after=$(get_layout_param $f_comp)
6302         [ "$layout_before" == "$layout_after" ] ||
6303                 error "lfs_migrate: $layout_before != $layout_after"
6304
6305         # 2. migrate a comp layout file by lfs migrate
6306         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
6307         layout_after=$(get_layout_param $f_comp)
6308         [ "$layout_before" == "$layout_after" ] ||
6309                 error "lfs migrate: $layout_before != $layout_after"
6310 }
6311 run_test 56xe "migrate a composite layout file"
6312
6313 test_56y() {
6314         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
6315                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
6316
6317         local res=""
6318         local dir=$DIR/$tdir
6319         local f1=$dir/file1
6320         local f2=$dir/file2
6321
6322         test_mkdir -p $dir || error "creating dir $dir"
6323         touch $f1 || error "creating std file $f1"
6324         $MULTIOP $f2 H2c || error "creating released file $f2"
6325
6326         # a directory can be raid0, so ask only for files
6327         res=$($LFS find $dir -L raid0 -type f | wc -l)
6328         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
6329
6330         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
6331         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
6332
6333         # only files can be released, so no need to force file search
6334         res=$($LFS find $dir -L released)
6335         [[ $res == $f2 ]] || error "search released: found $res != $f2"
6336
6337         res=$($LFS find $dir -type f \! -L released)
6338         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
6339 }
6340 run_test 56y "lfs find -L raid0|released"
6341
6342 test_56z() { # LU-4824
6343         # This checks to make sure 'lfs find' continues after errors
6344         # There are two classes of errors that should be caught:
6345         # - If multiple paths are provided, all should be searched even if one
6346         #   errors out
6347         # - If errors are encountered during the search, it should not terminate
6348         #   early
6349         local dir=$DIR/$tdir
6350         local i
6351
6352         test_mkdir $dir
6353         for i in d{0..9}; do
6354                 test_mkdir $dir/$i
6355                 touch $dir/$i/$tfile
6356         done
6357         $LFS find $DIR/non_existent_dir $dir &&
6358                 error "$LFS find did not return an error"
6359         # Make a directory unsearchable. This should NOT be the last entry in
6360         # directory order.  Arbitrarily pick the 6th entry
6361         chmod 700 $($LFS find $dir -type d | sed '6!d')
6362
6363         $RUNAS $LFS find $DIR/non_existent $dir
6364         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
6365
6366         # The user should be able to see 10 directories and 9 files
6367         (( count == 19 )) ||
6368                 error "$LFS find found $count != 19 entries after error"
6369 }
6370 run_test 56z "lfs find should continue after an error"
6371
6372 test_56aa() { # LU-5937
6373         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
6374
6375         local dir=$DIR/$tdir
6376
6377         mkdir $dir
6378         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
6379
6380         createmany -o $dir/striped_dir/${tfile}- 1024
6381         local dirs=$($LFS find --size +8k $dir/)
6382
6383         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
6384 }
6385 run_test 56aa "lfs find --size under striped dir"
6386
6387 test_56ab() { # LU-10705
6388         test_mkdir $DIR/$tdir
6389         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
6390         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
6391         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
6392         # Flush writes to ensure valid blocks.  Need to be more thorough for
6393         # ZFS, since blocks are not allocated/returned to client immediately.
6394         sync_all_data
6395         wait_zfs_commit ost1 2
6396         cancel_lru_locks osc
6397         ls -ls $DIR/$tdir
6398
6399         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
6400
6401         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
6402
6403         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
6404         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
6405
6406         rm -f $DIR/$tdir/$tfile.[123]
6407 }
6408 run_test 56ab "lfs find --blocks"
6409
6410 test_56ba() {
6411         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
6412                 skip "Need MDS version at least 2.10.50"
6413
6414         # Create composite files with one component
6415         local dir=$DIR/$tdir
6416
6417         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
6418         # Create composite files with three components
6419         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
6420         # Create non-composite files
6421         createmany -o $dir/${tfile}- 10
6422
6423         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
6424
6425         [[ $nfiles == 10 ]] ||
6426                 error "lfs find -E 1M found $nfiles != 10 files"
6427
6428         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
6429         [[ $nfiles == 25 ]] ||
6430                 error "lfs find ! -E 1M found $nfiles != 25 files"
6431
6432         # All files have a component that starts at 0
6433         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
6434         [[ $nfiles == 35 ]] ||
6435                 error "lfs find --component-start 0 - $nfiles != 35 files"
6436
6437         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
6438         [[ $nfiles == 15 ]] ||
6439                 error "lfs find --component-start 2M - $nfiles != 15 files"
6440
6441         # All files created here have a componenet that does not starts at 2M
6442         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
6443         [[ $nfiles == 35 ]] ||
6444                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
6445
6446         # Find files with a specified number of components
6447         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
6448         [[ $nfiles == 15 ]] ||
6449                 error "lfs find --component-count 3 - $nfiles != 15 files"
6450
6451         # Remember non-composite files have a component count of zero
6452         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
6453         [[ $nfiles == 10 ]] ||
6454                 error "lfs find --component-count 0 - $nfiles != 10 files"
6455
6456         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
6457         [[ $nfiles == 20 ]] ||
6458                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
6459
6460         # All files have a flag called "init"
6461         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
6462         [[ $nfiles == 35 ]] ||
6463                 error "lfs find --component-flags init - $nfiles != 35 files"
6464
6465         # Multi-component files will have a component not initialized
6466         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
6467         [[ $nfiles == 15 ]] ||
6468                 error "lfs find !--component-flags init - $nfiles != 15 files"
6469
6470         rm -rf $dir
6471
6472 }
6473 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
6474
6475 test_56ca() {
6476         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
6477                 skip "Need MDS version at least 2.10.57"
6478
6479         local td=$DIR/$tdir
6480         local tf=$td/$tfile
6481         local dir
6482         local nfiles
6483         local cmd
6484         local i
6485         local j
6486
6487         # create mirrored directories and mirrored files
6488         mkdir $td || error "mkdir $td failed"
6489         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
6490         createmany -o $tf- 10 || error "create $tf- failed"
6491
6492         for i in $(seq 2); do
6493                 dir=$td/dir$i
6494                 mkdir $dir || error "mkdir $dir failed"
6495                 $LFS mirror create -N$((3 + i)) $dir ||
6496                         error "create mirrored dir $dir failed"
6497                 createmany -o $dir/$tfile- 10 ||
6498                         error "create $dir/$tfile- failed"
6499         done
6500
6501         # change the states of some mirrored files
6502         echo foo > $tf-6
6503         for i in $(seq 2); do
6504                 dir=$td/dir$i
6505                 for j in $(seq 4 9); do
6506                         echo foo > $dir/$tfile-$j
6507                 done
6508         done
6509
6510         # find mirrored files with specific mirror count
6511         cmd="$LFS find --mirror-count 3 --type f $td"
6512         nfiles=$($cmd | wc -l)
6513         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
6514
6515         cmd="$LFS find ! --mirror-count 3 --type f $td"
6516         nfiles=$($cmd | wc -l)
6517         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
6518
6519         cmd="$LFS find --mirror-count +2 --type f $td"
6520         nfiles=$($cmd | wc -l)
6521         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6522
6523         cmd="$LFS find --mirror-count -6 --type f $td"
6524         nfiles=$($cmd | wc -l)
6525         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6526
6527         # find mirrored files with specific file state
6528         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
6529         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
6530
6531         cmd="$LFS find --mirror-state=ro --type f $td"
6532         nfiles=$($cmd | wc -l)
6533         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
6534
6535         cmd="$LFS find ! --mirror-state=ro --type f $td"
6536         nfiles=$($cmd | wc -l)
6537         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6538
6539         cmd="$LFS find --mirror-state=wp --type f $td"
6540         nfiles=$($cmd | wc -l)
6541         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6542
6543         cmd="$LFS find ! --mirror-state=sp --type f $td"
6544         nfiles=$($cmd | wc -l)
6545         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6546 }
6547 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
6548
6549 test_57a() {
6550         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6551         # note test will not do anything if MDS is not local
6552         if [ "$mds1_FSTYPE" != ldiskfs ]; then
6553                 skip_env "ldiskfs only test"
6554         fi
6555         remote_mds_nodsh && skip "remote MDS with nodsh"
6556
6557         local MNTDEV="osd*.*MDT*.mntdev"
6558         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
6559         [ -z "$DEV" ] && error "can't access $MNTDEV"
6560         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
6561                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
6562                         error "can't access $DEV"
6563                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
6564                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
6565                 rm $TMP/t57a.dump
6566         done
6567 }
6568 run_test 57a "verify MDS filesystem created with large inodes =="
6569
6570 test_57b() {
6571         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6572         if [ "$mds1_FSTYPE" != ldiskfs ]; then
6573                 skip_env "ldiskfs only test"
6574         fi
6575         remote_mds_nodsh && skip "remote MDS with nodsh"
6576
6577         local dir=$DIR/$tdir
6578         local filecount=100
6579         local file1=$dir/f1
6580         local fileN=$dir/f$filecount
6581
6582         rm -rf $dir || error "removing $dir"
6583         test_mkdir -c1 $dir
6584         local mdtidx=$($LFS getstripe -m $dir)
6585         local mdtname=MDT$(printf %04x $mdtidx)
6586         local facet=mds$((mdtidx + 1))
6587
6588         echo "mcreating $filecount files"
6589         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
6590
6591         # verify that files do not have EAs yet
6592         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
6593                 error "$file1 has an EA"
6594         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
6595                 error "$fileN has an EA"
6596
6597         sync
6598         sleep 1
6599         df $dir  #make sure we get new statfs data
6600         local mdsfree=$(do_facet $facet \
6601                         lctl get_param -n osd*.*$mdtname.kbytesfree)
6602         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
6603         local file
6604
6605         echo "opening files to create objects/EAs"
6606         for file in $(seq -f $dir/f%g 1 $filecount); do
6607                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
6608                         error "opening $file"
6609         done
6610
6611         # verify that files have EAs now
6612         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
6613         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
6614
6615         sleep 1  #make sure we get new statfs data
6616         df $dir
6617         local mdsfree2=$(do_facet $facet \
6618                          lctl get_param -n osd*.*$mdtname.kbytesfree)
6619         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
6620
6621         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
6622                 if [ "$mdsfree" != "$mdsfree2" ]; then
6623                         error "MDC before $mdcfree != after $mdcfree2"
6624                 else
6625                         echo "MDC before $mdcfree != after $mdcfree2"
6626                         echo "unable to confirm if MDS has large inodes"
6627                 fi
6628         fi
6629         rm -rf $dir
6630 }
6631 run_test 57b "default LOV EAs are stored inside large inodes ==="
6632
6633 test_58() {
6634         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6635         [ -z "$(which wiretest 2>/dev/null)" ] &&
6636                         skip_env "could not find wiretest"
6637
6638         wiretest
6639 }
6640 run_test 58 "verify cross-platform wire constants =============="
6641
6642 test_59() {
6643         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6644
6645         echo "touch 130 files"
6646         createmany -o $DIR/f59- 130
6647         echo "rm 130 files"
6648         unlinkmany $DIR/f59- 130
6649         sync
6650         # wait for commitment of removal
6651         wait_delete_completed
6652 }
6653 run_test 59 "verify cancellation of llog records async ========="
6654
6655 TEST60_HEAD="test_60 run $RANDOM"
6656 test_60a() {
6657         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6658         remote_mgs_nodsh && skip "remote MGS with nodsh"
6659         do_facet mgs "! which run-llog.sh &> /dev/null" &&
6660                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
6661                         skip_env "missing subtest run-llog.sh"
6662
6663         log "$TEST60_HEAD - from kernel mode"
6664         do_facet mgs "$LCTL dk > /dev/null"
6665         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
6666         do_facet mgs $LCTL dk > $TMP/$tfile
6667
6668         # LU-6388: test llog_reader
6669         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
6670         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
6671         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
6672                         skip_env "missing llog_reader"
6673         local fstype=$(facet_fstype mgs)
6674         [ $fstype != ldiskfs -a $fstype != zfs ] &&
6675                 skip_env "Only for ldiskfs or zfs type mgs"
6676
6677         local mntpt=$(facet_mntpt mgs)
6678         local mgsdev=$(mgsdevname 1)
6679         local fid_list
6680         local fid
6681         local rec_list
6682         local rec
6683         local rec_type
6684         local obj_file
6685         local path
6686         local seq
6687         local oid
6688         local pass=true
6689
6690         #get fid and record list
6691         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
6692                 tail -n 4))
6693         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
6694                 tail -n 4))
6695         #remount mgs as ldiskfs or zfs type
6696         stop mgs || error "stop mgs failed"
6697         mount_fstype mgs || error "remount mgs failed"
6698         for ((i = 0; i < ${#fid_list[@]}; i++)); do
6699                 fid=${fid_list[i]}
6700                 rec=${rec_list[i]}
6701                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
6702                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
6703                 oid=$((16#$oid))
6704
6705                 case $fstype in
6706                         ldiskfs )
6707                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
6708                         zfs )
6709                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
6710                 esac
6711                 echo "obj_file is $obj_file"
6712                 do_facet mgs $llog_reader $obj_file
6713
6714                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
6715                         awk '{ print $3 }' | sed -e "s/^type=//g")
6716                 if [ $rec_type != $rec ]; then
6717                         echo "FAILED test_60a wrong record type $rec_type," \
6718                               "should be $rec"
6719                         pass=false
6720                         break
6721                 fi
6722
6723                 #check obj path if record type is LLOG_LOGID_MAGIC
6724                 if [ "$rec" == "1064553b" ]; then
6725                         path=$(do_facet mgs $llog_reader $obj_file |
6726                                 grep "path=" | awk '{ print $NF }' |
6727                                 sed -e "s/^path=//g")
6728                         if [ $obj_file != $mntpt/$path ]; then
6729                                 echo "FAILED test_60a wrong obj path" \
6730                                       "$montpt/$path, should be $obj_file"
6731                                 pass=false
6732                                 break
6733                         fi
6734                 fi
6735         done
6736         rm -f $TMP/$tfile
6737         #restart mgs before "error", otherwise it will block the next test
6738         stop mgs || error "stop mgs failed"
6739         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
6740         $pass || error "test failed, see FAILED test_60a messages for specifics"
6741 }
6742 run_test 60a "llog_test run from kernel module and test llog_reader"
6743
6744 test_60b() { # bug 6411
6745         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6746
6747         dmesg > $DIR/$tfile
6748         LLOG_COUNT=$(do_facet mgs dmesg |
6749                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
6750                           /llog_[a-z]*.c:[0-9]/ {
6751                                 if (marker)
6752                                         from_marker++
6753                                 from_begin++
6754                           }
6755                           END {
6756                                 if (marker)
6757                                         print from_marker
6758                                 else
6759                                         print from_begin
6760                           }")
6761
6762         [[ $LLOG_COUNT -gt 120 ]] &&
6763                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
6764 }
6765 run_test 60b "limit repeated messages from CERROR/CWARN"
6766
6767 test_60c() {
6768         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6769
6770         echo "create 5000 files"
6771         createmany -o $DIR/f60c- 5000
6772 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
6773         lctl set_param fail_loc=0x80000137
6774         unlinkmany $DIR/f60c- 5000
6775         lctl set_param fail_loc=0
6776 }
6777 run_test 60c "unlink file when mds full"
6778
6779 test_60d() {
6780         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6781
6782         SAVEPRINTK=$(lctl get_param -n printk)
6783         # verify "lctl mark" is even working"
6784         MESSAGE="test message ID $RANDOM $$"
6785         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
6786         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
6787
6788         lctl set_param printk=0 || error "set lnet.printk failed"
6789         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
6790         MESSAGE="new test message ID $RANDOM $$"
6791         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
6792         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
6793         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
6794
6795         lctl set_param -n printk="$SAVEPRINTK"
6796 }
6797 run_test 60d "test printk console message masking"
6798
6799 test_60e() {
6800         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6801         remote_mds_nodsh && skip "remote MDS with nodsh"
6802
6803         touch $DIR/$tfile
6804 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
6805         do_facet mds1 lctl set_param fail_loc=0x15b
6806         rm $DIR/$tfile
6807 }
6808 run_test 60e "no space while new llog is being created"
6809
6810 test_60g() {
6811         local pid
6812
6813         test_mkdir -c $MDSCOUNT $DIR/$tdir
6814         $LFS setdirstripe -D -i -1 -c $MDSCOUNT $DIR/$tdir
6815
6816         (
6817                 local index=0
6818                 while true; do
6819                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
6820                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
6821                         index=$((index + 1))
6822                 done
6823         ) &
6824
6825         pid=$!
6826
6827         for i in $(seq 100); do 
6828                 # define OBD_FAIL_OSD_TXN_START    0x19a
6829                 do_facet mds1 lctl set_param fail_loc=0x8000019a
6830                 usleep 100
6831         done
6832
6833         kill -9 $pid
6834
6835         mkdir $DIR/$tdir/new || error "mkdir failed"
6836         rmdir $DIR/$tdir/new || error "rmdir failed"
6837 }
6838 run_test 60g "transaction abort won't cause MDT hung"
6839
6840 test_60h() {
6841         [ $MDS1_VERSION -le $(version_code 2.12.52) ] ||
6842                 skip "Need MDS version at least 2.12.52"
6843         [ $MDSCOUNT -le 2 ] && skip "Need >= 2 MDTs"
6844
6845         local f
6846
6847         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
6848         #define OBD_FAIL_MDS_STRIPE_FID          0x189
6849         for fail_loc in 0x80000188 0x80000189; do
6850                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
6851                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
6852                         error "mkdir $dir-$fail_loc failed"
6853                 for i in {0..10}; do
6854                         # create may fail on missing stripe
6855                         echo $i > $DIR/$tdir-$fail_loc/$i
6856                 done
6857                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
6858                         error "getdirstripe $tdir-$fail_loc failed"
6859                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
6860                         error "migrate $tdir-$fail_loc failed"
6861                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
6862                         error "getdirstripe $tdir-$fail_loc failed"
6863                 pushd $DIR/$tdir-$fail_loc
6864                 for f in *; do
6865                         echo $f | cmp $f - || error "$f data mismatch"
6866                 done
6867                 popd
6868                 rm -rf $DIR/$tdir-$fail_loc
6869         done
6870 }
6871 run_test 60h "striped directory with missing stripes can be accessed"
6872
6873 test_61a() {
6874         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6875
6876         f="$DIR/f61"
6877         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
6878         cancel_lru_locks osc
6879         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
6880         sync
6881 }
6882 run_test 61a "mmap() writes don't make sync hang ================"
6883
6884 test_61b() {
6885         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
6886 }
6887 run_test 61b "mmap() of unstriped file is successful"
6888
6889 # bug 2330 - insufficient obd_match error checking causes LBUG
6890 test_62() {
6891         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6892
6893         f="$DIR/f62"
6894         echo foo > $f
6895         cancel_lru_locks osc
6896         lctl set_param fail_loc=0x405
6897         cat $f && error "cat succeeded, expect -EIO"
6898         lctl set_param fail_loc=0
6899 }
6900 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
6901 # match every page all of the time.
6902 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
6903
6904 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
6905 # Though this test is irrelevant anymore, it helped to reveal some
6906 # other grant bugs (LU-4482), let's keep it.
6907 test_63a() {   # was test_63
6908         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6909
6910         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
6911
6912         for i in `seq 10` ; do
6913                 dd if=/dev/zero of=$DIR/f63 bs=8k &
6914                 sleep 5
6915                 kill $!
6916                 sleep 1
6917         done
6918
6919         rm -f $DIR/f63 || true
6920 }
6921 run_test 63a "Verify oig_wait interruption does not crash ======="
6922
6923 # bug 2248 - async write errors didn't return to application on sync
6924 # bug 3677 - async write errors left page locked
6925 test_63b() {
6926         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6927
6928         debugsave
6929         lctl set_param debug=-1
6930
6931         # ensure we have a grant to do async writes
6932         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
6933         rm $DIR/$tfile
6934
6935         sync    # sync lest earlier test intercept the fail_loc
6936
6937         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6938         lctl set_param fail_loc=0x80000406
6939         $MULTIOP $DIR/$tfile Owy && \
6940                 error "sync didn't return ENOMEM"
6941         sync; sleep 2; sync     # do a real sync this time to flush page
6942         lctl get_param -n llite.*.dump_page_cache | grep locked && \
6943                 error "locked page left in cache after async error" || true
6944         debugrestore
6945 }
6946 run_test 63b "async write errors should be returned to fsync ==="
6947
6948 test_64a () {
6949         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6950
6951         df $DIR
6952         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
6953 }
6954 run_test 64a "verify filter grant calculations (in kernel) ====="
6955
6956 test_64b () {
6957         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6958
6959         sh oos.sh $MOUNT || error "oos.sh failed: $?"
6960 }
6961 run_test 64b "check out-of-space detection on client"
6962
6963 test_64c() {
6964         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
6965 }
6966 run_test 64c "verify grant shrink"
6967
6968 import_param() {
6969         local tgt=$1
6970         local param=$2
6971
6972         $LCTL get_param osc.$tgt.import | awk "/$param/ { print \$2 }"
6973 }
6974
6975 # this does exactly what osc_request.c:osc_announce_cached() does in
6976 # order to calculate max amount of grants to ask from server
6977 want_grant() {
6978         local tgt=$1
6979
6980         local nrpages=$($LCTL get_param -n osc.$tgt.max_pages_per_rpc)
6981         local rpc_in_flight=$($LCTL get_param -n osc.$tgt.max_rpcs_in_flight)
6982
6983         ((rpc_in_flight++));
6984         nrpages=$((nrpages * rpc_in_flight))
6985
6986         local dirty_max_pages=$($LCTL get_param -n osc.$tgt.max_dirty_mb)
6987
6988         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
6989
6990         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
6991         local undirty=$((nrpages * PAGE_SIZE))
6992
6993         local max_extent_pages
6994         max_extent_pages=$(import_param $tgt grant_max_extent_size)
6995         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
6996         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
6997         local grant_extent_tax
6998         grant_extent_tax=$(import_param $tgt grant_extent_tax)
6999
7000         undirty=$((undirty + nrextents * grant_extent_tax))
7001
7002         echo $undirty
7003 }
7004
7005 # this is size of unit for grant allocation. It should be equal to
7006 # what tgt_grant.c:tgt_grant_chunk() calculates
7007 grant_chunk() {
7008         local tgt=$1
7009         local max_brw_size
7010         local grant_extent_tax
7011
7012         max_brw_size=$(import_param $tgt max_brw_size)
7013
7014         grant_extent_tax=$(import_param $tgt grant_extent_tax)
7015
7016         echo $(((max_brw_size + grant_extent_tax) * 2))
7017 }
7018
7019 test_64d() {
7020         [ $OST1_VERSION -ge $(version_code 2.10.56) ] ||
7021                 skip "OST < 2.10.55 doesn't limit grants enough"
7022
7023         local tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
7024
7025         [[ "$($LCTL get_param osc.${tgt}.import)" =~ "grant_param" ]] ||
7026                 skip "no grant_param connect flag"
7027
7028         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
7029
7030         $LCTL set_param -n -n debug="$OLDDEBUG" || true
7031         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
7032
7033
7034         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
7035         stack_trap "rm -f $DIR/$tfile && wait_delete_completed" EXIT
7036
7037         $LFS setstripe $DIR/$tfile -i 0 -c 1
7038         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1000 &
7039         ddpid=$!
7040
7041         while kill -0 $ddpid; do
7042                 local cur_grant=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
7043
7044                 if [[ $cur_grant -gt $max_cur_granted ]]; then
7045                         kill $ddpid
7046                         error "cur_grant $cur_grant > $max_cur_granted"
7047                 fi
7048
7049                 sleep 1
7050         done
7051 }
7052 run_test 64d "check grant limit exceed"
7053
7054 check_grants() {
7055         local tgt=$1
7056         local expected=$2
7057         local msg=$3
7058         local cur_grants=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
7059
7060         ((cur_grants == expected)) ||
7061                 error "$msg: grants mismatch: $cur_grants, expected $expected"
7062 }
7063
7064 round_up_p2() {
7065         echo $((($1 + $2 - 1) & ~($2 - 1)))
7066 }
7067
7068 test_64e() {
7069         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7070         [ $OST1_VERSION -ge $(version_code 2.11.56) ] ||
7071                 skip "Need OSS version at least 2.11.56"
7072
7073         # Remount client to reset grant
7074         remount_client $MOUNT || error "failed to remount client"
7075         local osc_tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
7076         local init_grants=$(import_param $osc_tgt initial_grant)
7077
7078         check_grants $osc_tgt $init_grants "init grants"
7079
7080         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
7081         local max_brw_size=$(import_param $osc_tgt max_brw_size)
7082         local gbs=$(import_param $osc_tgt grant_block_size)
7083
7084         # write random number of bytes from max_brw_size / 4 to max_brw_size
7085         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
7086         # align for direct io
7087         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
7088         # round to grant consumption unit
7089         local wb_round_up=$(round_up_p2 $write_bytes gbs)
7090
7091         local grants=$((wb_round_up + extent_tax))
7092
7093         $LFS setstripe -c 1 -i 0 $DIR/$tfile  || error "lfs setstripe failed"
7094
7095         # define OBD_FAIL_TGT_NO_GRANT 0x725
7096         # make the server not grant more back
7097         do_facet ost1 $LCTL set_param fail_loc=0x725
7098         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct
7099
7100         do_facet ost1 $LCTL set_param fail_loc=0
7101
7102         check_grants $osc_tgt $((init_grants - grants)) "dio w/o grant alloc"
7103
7104         rm -f $DIR/$tfile || error "rm failed"
7105
7106         # Remount client to reset grant
7107         remount_client $MOUNT || error "failed to remount client"
7108         osc_tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
7109
7110         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
7111
7112         # define OBD_FAIL_TGT_NO_GRANT 0x725
7113         # make the server not grant more back
7114         do_facet ost1 $LCTL set_param fail_loc=0x725
7115         $MULTIOP $DIR/$tfile "oO_WRONLY:w${write_bytes}yc"
7116         do_facet ost1 $LCTL set_param fail_loc=0
7117
7118         check_grants $osc_tgt $((init_grants - grants)) "buf io w/o grant alloc"
7119 }
7120 run_test 64e "check grant consumption (no grant allocation)"
7121
7122 test_64f() {
7123         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7124
7125         # Remount client to reset grant
7126         remount_client $MOUNT || error "failed to remount client"
7127         local osc_tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
7128
7129         local init_grants=$(import_param $osc_tgt initial_grant)
7130         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
7131         local max_brw_size=$(import_param $osc_tgt max_brw_size)
7132         local gbs=$(import_param $osc_tgt grant_block_size)
7133         local chunk=$(grant_chunk $osc_tgt)
7134
7135         # write random number of bytes from max_brw_size / 4 to max_brw_size
7136         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
7137         # align for direct io
7138         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
7139         # round to grant consumption unit
7140         local wb_round_up=$(round_up_p2 $write_bytes gbs)
7141
7142         local grants=$((wb_round_up + extent_tax))
7143
7144         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
7145         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct ||
7146                 error "error writing to $DIR/$tfile"
7147
7148         check_grants $osc_tgt $((init_grants - grants + chunk)) \
7149                 "direct io with grant allocation"
7150
7151         rm -f $DIR/$tfile || error "rm failed"
7152
7153         # Remount client to reset grant
7154         remount_client $MOUNT || error "failed to remount client"
7155         osc_tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
7156
7157         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
7158
7159         local cmd="oO_WRONLY:w${write_bytes}_yc"
7160
7161         $MULTIOP $DIR/$tfile $cmd &
7162         MULTIPID=$!
7163         sleep 1
7164
7165         check_grants $osc_tgt $((init_grants - grants)) \
7166                 "buffered io, not write rpc"
7167
7168         kill -USR1 $MULTIPID
7169         wait
7170
7171         check_grants $osc_tgt $((init_grants - grants + chunk)) \
7172                 "buffered io, one RPC"
7173 }
7174 run_test 64f "check grant consumption (with grant allocation)"
7175
7176 test_64h() {
7177         local cli=$($LFS getname $DIR); cli=${cli%% *}; cli=${cli#*-}
7178         local osc_tgt="$FSNAME-OST0000-osc-$cli"
7179         local num_exps=$(do_facet ost1 \
7180             $LCTL get_param -n obdfilter.*OST0000*.num_exports)
7181         local max_brw_size=$(import_param $osc_tgt max_brw_size)
7182         local avail=$($LCTL get_param -n osc.*OST0000-osc-$cli.kbytesavail)
7183         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
7184
7185         # 10MiB is for file to be written, max_brw_size * 16 *
7186         # num_exps is space reserve so that tgt_grant_shrink() decided
7187         # to not shrink
7188         local expect=$((max_brw_size * 16 * num_exps + 10 * 1048576))
7189         (( avail * 1024 < expect )) &&
7190                 skip "need $expect bytes on ost1, have $(( avail * 1024 )) only"
7191
7192         save_lustre_params client "osc.*OST0000*.grant_shrink" > $p
7193         save_lustre_params client "osc.*OST0000*.grant_shrink_interval" >> $p
7194         stack_trap "restore_lustre_params < $p; rm -f $save" EXIT
7195         $LCTL set_param osc.*OST0000*.grant_shrink=1
7196         $LCTL set_param osc.*OST0000*.grant_shrink_interval=10
7197
7198         $LFS setstripe -c 1 -i 0 $DIR/$tfile
7199         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 oflag=sync
7200
7201         # drop cache so that coming read would do rpc
7202         cancel_lru_locks osc
7203
7204         # shrink interval is set to 10, pause for 7 seconds so that
7205         # grant thread did not wake up yet but coming read entered
7206         # shrink mode for rpc (osc_should_shrink_grant())
7207         sleep 7
7208
7209         declare -a cur_grant_bytes
7210         declare -a tot_granted
7211         cur_grant_bytes[0]=$($LCTL get_param -n osc.*OST0000*.cur_grant_bytes)
7212         tot_granted[0]=$(do_facet ost1 \
7213             $LCTL get_param -n obdfilter.*OST0000*.tot_granted)
7214
7215         dd if=$DIR/$tfile bs=4K count=1 of=/dev/null
7216
7217         cur_grant_bytes[1]=$($LCTL get_param -n osc.*OST0000*.cur_grant_bytes)
7218         tot_granted[1]=$(do_facet ost1 \
7219             $LCTL get_param -n obdfilter.*OST0000*.tot_granted)
7220
7221         # grant change should be equal on both sides
7222         (( cur_grant_bytes[0] - cur_grant_bytes[1] ==
7223                 tot_granted[0] - tot_granted[1])) ||
7224                 error "grant change mismatch, "                                \
7225                         "server: ${tot_granted[0]} to ${tot_granted[1]}, "     \
7226                         "client: ${cur_grant_bytes[0]} to ${cur_grant_bytes[1]}"
7227 }
7228 run_test 64h "grant shrink on read"
7229
7230 # bug 1414 - set/get directories' stripe info
7231 test_65a() {
7232         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7233
7234         test_mkdir $DIR/$tdir
7235         touch $DIR/$tdir/f1
7236         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
7237 }
7238 run_test 65a "directory with no stripe info"
7239
7240 test_65b() {
7241         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7242
7243         test_mkdir $DIR/$tdir
7244         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7245
7246         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7247                                                 error "setstripe"
7248         touch $DIR/$tdir/f2
7249         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
7250 }
7251 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
7252
7253 test_65c() {
7254         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7255         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
7256
7257         test_mkdir $DIR/$tdir
7258         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
7259
7260         $LFS setstripe -S $((stripesize * 4)) -i 1 \
7261                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
7262         touch $DIR/$tdir/f3
7263         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
7264 }
7265 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
7266
7267 test_65d() {
7268         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7269
7270         test_mkdir $DIR/$tdir
7271         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
7272         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7273
7274         if [[ $STRIPECOUNT -le 0 ]]; then
7275                 sc=1
7276         elif [[ $STRIPECOUNT -gt 2000 ]]; then
7277 #LOV_MAX_STRIPE_COUNT is 2000
7278                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
7279         else
7280                 sc=$(($STRIPECOUNT - 1))
7281         fi
7282         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
7283         touch $DIR/$tdir/f4 $DIR/$tdir/f5
7284         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
7285                 error "lverify failed"
7286 }
7287 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
7288
7289 test_65e() {
7290         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7291
7292         test_mkdir $DIR/$tdir
7293
7294         $SETSTRIPE $DIR/$tdir || error "setstripe"
7295         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
7296                                         error "no stripe info failed"
7297         touch $DIR/$tdir/f6
7298         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
7299 }
7300 run_test 65e "directory setstripe defaults"
7301
7302 test_65f() {
7303         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7304
7305         test_mkdir $DIR/${tdir}f
7306         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
7307 }
7308 run_test 65f "dir setstripe permission (should return error) ==="
7309
7310 test_65g() {
7311         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7312
7313         test_mkdir $DIR/$tdir
7314         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7315
7316         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7317                 error "setstripe -S failed"
7318         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
7319         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7320                 error "delete default stripe failed"
7321 }
7322 run_test 65g "directory setstripe -d"
7323
7324 test_65h() {
7325         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7326
7327         test_mkdir $DIR/$tdir
7328         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7329
7330         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7331                 error "setstripe -S failed"
7332         test_mkdir $DIR/$tdir/dd1
7333         [ $($LFS getstripe -c $DIR/$tdir) = $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
7334                 error "stripe info inherit failed"
7335 }
7336 run_test 65h "directory stripe info inherit ===================="
7337
7338 test_65i() {
7339         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7340
7341         save_layout_restore_at_exit $MOUNT
7342
7343         # bug6367: set non-default striping on root directory
7344         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
7345
7346         # bug12836: getstripe on -1 default directory striping
7347         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
7348
7349         # bug12836: getstripe -v on -1 default directory striping
7350         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
7351
7352         # bug12836: new find on -1 default directory striping
7353         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
7354 }
7355 run_test 65i "various tests to set root directory striping"
7356
7357 test_65j() { # bug6367
7358         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7359
7360         sync; sleep 1
7361
7362         # if we aren't already remounting for each test, do so for this test
7363         if [ "$I_MOUNTED" = "yes" ]; then
7364                 cleanup || error "failed to unmount"
7365                 setup
7366         fi
7367
7368         save_layout_restore_at_exit $MOUNT
7369
7370         $SETSTRIPE -d $MOUNT || error "setstripe failed"
7371 }
7372 run_test 65j "set default striping on root directory (bug 6367)="
7373
7374 cleanup_65k() {
7375         rm -rf $DIR/$tdir
7376         wait_delete_completed
7377         do_facet $SINGLEMDS "lctl set_param -n \
7378                 osp.$ost*MDT0000.max_create_count=$max_count"
7379         do_facet $SINGLEMDS "lctl set_param -n \
7380                 osp.$ost*MDT0000.create_count=$count"
7381         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7382         echo $INACTIVE_OSC "is Activate"
7383
7384         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7385 }
7386
7387 test_65k() { # bug11679
7388         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7389         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7390         remote_mds_nodsh && skip "remote MDS with nodsh"
7391
7392         local disable_precreate=true
7393         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
7394                 disable_precreate=false
7395
7396         echo "Check OST status: "
7397         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
7398                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
7399
7400         for OSC in $MDS_OSCS; do
7401                 echo $OSC "is active"
7402                 do_facet $SINGLEMDS lctl --device %$OSC activate
7403         done
7404
7405         for INACTIVE_OSC in $MDS_OSCS; do
7406                 local ost=$(osc_to_ost $INACTIVE_OSC)
7407                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
7408                                lov.*md*.target_obd |
7409                                awk -F: /$ost/'{ print $1 }' | head -n 1)
7410
7411                 mkdir -p $DIR/$tdir
7412                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
7413                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
7414
7415                 echo "Deactivate: " $INACTIVE_OSC
7416                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
7417
7418                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
7419                               osp.$ost*MDT0000.create_count")
7420                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
7421                                   osp.$ost*MDT0000.max_create_count")
7422                 $disable_precreate &&
7423                         do_facet $SINGLEMDS "lctl set_param -n \
7424                                 osp.$ost*MDT0000.max_create_count=0"
7425
7426                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
7427                         [ -f $DIR/$tdir/$idx ] && continue
7428                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
7429                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
7430                                 { cleanup_65k;
7431                                   error "setstripe $idx should succeed"; }
7432                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
7433                 done
7434                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
7435                 rmdir $DIR/$tdir
7436
7437                 do_facet $SINGLEMDS "lctl set_param -n \
7438                         osp.$ost*MDT0000.max_create_count=$max_count"
7439                 do_facet $SINGLEMDS "lctl set_param -n \
7440                         osp.$ost*MDT0000.create_count=$count"
7441                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7442                 echo $INACTIVE_OSC "is Activate"
7443
7444                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7445         done
7446 }
7447 run_test 65k "validate manual striping works properly with deactivated OSCs"
7448
7449 test_65l() { # bug 12836
7450         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7451
7452         test_mkdir -p $DIR/$tdir/test_dir
7453         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
7454         $LFS find -mtime -1 $DIR/$tdir >/dev/null
7455 }
7456 run_test 65l "lfs find on -1 stripe dir ========================"
7457
7458 test_65m() {
7459         local layout=$(save_layout $MOUNT)
7460         $RUNAS $SETSTRIPE -c 2 $MOUNT && {
7461                 restore_layout $MOUNT $layout
7462                 error "setstripe should fail by non-root users"
7463         }
7464         true
7465 }
7466 run_test 65m "normal user can't set filesystem default stripe"
7467
7468 test_65n() {
7469         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.12.0) ]] ||
7470                 skip "Need MDS version at least 2.12.0"
7471         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
7472
7473         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7474         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
7475         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
7476
7477         save_layout_restore_at_exit $MOUNT
7478
7479         # new subdirectory under root directory should not inherit
7480         # the default layout from root
7481         local dir1=$MOUNT/$tdir-1
7482         mkdir $dir1 || error "mkdir $dir1 failed"
7483         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
7484                 error "$dir1 shouldn't have LOV EA"
7485
7486         # delete the default layout on root directory
7487         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
7488
7489         local dir2=$MOUNT/$tdir-2
7490         mkdir $dir2 || error "mkdir $dir2 failed"
7491         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
7492                 error "$dir2 shouldn't have LOV EA"
7493
7494         # set a new striping pattern on root directory
7495         local def_stripe_size=$($LFS getstripe -S $MOUNT)
7496         local new_def_stripe_size=$((def_stripe_size * 2))
7497         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
7498                 error "set stripe size on $MOUNT failed"
7499
7500         # new file created in $dir2 should inherit the new stripe size from
7501         # the filesystem default
7502         local file2=$dir2/$tfile-2
7503         touch $file2 || error "touch $file2 failed"
7504
7505         local file2_stripe_size=$($LFS getstripe -S $file2)
7506         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
7507                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
7508
7509         local dir3=$MOUNT/$tdir-3
7510         mkdir $dir3 || error "mkdir $dir3 failed"
7511         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
7512         # the root layout, which is the actual default layout that will be used
7513         # when new files are created in $dir3.
7514         local dir3_layout=$(get_layout_param $dir3)
7515         local root_dir_layout=$(get_layout_param $MOUNT)
7516         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
7517                 error "$dir3 should show the default layout from $MOUNT"
7518
7519         # set OST pool on root directory
7520         local pool=$TESTNAME
7521         pool_add $pool || error "add $pool failed"
7522         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
7523                 error "add targets to $pool failed"
7524
7525         $LFS setstripe -p $pool $MOUNT ||
7526                 error "set OST pool on $MOUNT failed"
7527
7528         # new file created in $dir3 should inherit the pool from
7529         # the filesystem default
7530         local file3=$dir3/$tfile-3
7531         touch $file3 || error "touch $file3 failed"
7532
7533         local file3_pool=$($LFS getstripe -p $file3)
7534         [[ "$file3_pool" = "$pool" ]] ||
7535                 error "$file3 didn't inherit OST pool $pool"
7536
7537         local dir4=$MOUNT/$tdir-4
7538         mkdir $dir4 || error "mkdir $dir4 failed"
7539         local dir4_layout=$(get_layout_param $dir4)
7540         root_dir_layout=$(get_layout_param $MOUNT)
7541         echo "$LFS getstripe -d $dir4"
7542         $LFS getstripe -d $dir4
7543         echo "$LFS getstripe -d $MOUNT"
7544         $LFS getstripe -d $MOUNT
7545         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
7546                 error "$dir4 should show the default layout from $MOUNT"
7547
7548         # new file created in $dir4 should inherit the pool from
7549         # the filesystem default
7550         local file4=$dir4/$tfile-4
7551         touch $file4 || error "touch $file4 failed"
7552
7553         local file4_pool=$($LFS getstripe -p $file4)
7554         [[ "$file4_pool" = "$pool" ]] ||
7555                 error "$file4 didn't inherit OST pool $pool"
7556
7557         # new subdirectory under non-root directory should inherit
7558         # the default layout from its parent directory
7559         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
7560                 error "set directory layout on $dir4 failed"
7561
7562         local dir5=$dir4/$tdir-5
7563         mkdir $dir5 || error "mkdir $dir5 failed"
7564
7565         dir4_layout=$(get_layout_param $dir4)
7566         local dir5_layout=$(get_layout_param $dir5)
7567         [[ "$dir4_layout" = "$dir5_layout" ]] ||
7568                 error "$dir5 should inherit the default layout from $dir4"
7569
7570         # though subdir under ROOT doesn't inherit default layout, but
7571         # its sub dir/file should be created with default layout.
7572         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
7573         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
7574         [[ $MDS1_VERSION -ge $(version_code 2.12.3) &&
7575            $MDS1_VERSION -lt $(version_code 2.12.50) ]] ||
7576                 skip "Need MDS version at least 2.12.3 or 2.12.59"
7577
7578         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
7579         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
7580         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
7581
7582         if [ $default_lmv_hash == "none" ]; then
7583                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
7584         else
7585                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
7586                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
7587         fi
7588
7589         $LFS setdirstripe -D -c 2 $MOUNT ||
7590                 error "setdirstripe -D -c 2 failed"
7591         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
7592         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
7593         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
7594 }
7595 run_test 65n "don't inherit default layout from root for new subdirectories"
7596
7597 # bug 2543 - update blocks count on client
7598 test_66() {
7599         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7600
7601         COUNT=${COUNT:-8}
7602         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
7603         sync; sync_all_data; sync; sync_all_data
7604         cancel_lru_locks osc
7605         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
7606         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
7607 }
7608 run_test 66 "update inode blocks count on client ==============="
7609
7610 meminfo() {
7611         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
7612 }
7613
7614 swap_used() {
7615         swapon -s | awk '($1 == "'$1'") { print $4 }'
7616 }
7617
7618 # bug5265, obdfilter oa2dentry return -ENOENT
7619 # #define OBD_FAIL_SRV_ENOENT 0x217
7620 test_69() {
7621         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7622         remote_ost_nodsh && skip "remote OST with nodsh"
7623
7624         f="$DIR/$tfile"
7625         $SETSTRIPE -c 1 -i 0 $f
7626
7627         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
7628
7629         do_facet ost1 lctl set_param fail_loc=0x217
7630         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
7631         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
7632
7633         do_facet ost1 lctl set_param fail_loc=0
7634         $DIRECTIO write $f 0 2 || error "write error"
7635
7636         cancel_lru_locks osc
7637         $DIRECTIO read $f 0 1 || error "read error"
7638
7639         do_facet ost1 lctl set_param fail_loc=0x217
7640         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
7641
7642         do_facet ost1 lctl set_param fail_loc=0
7643         rm -f $f
7644 }
7645 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
7646
7647 test_71() {
7648         test_mkdir $DIR/$tdir
7649         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
7650         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
7651 }
7652 run_test 71 "Running dbench on lustre (don't segment fault) ===="
7653
7654 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
7655         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7656         [ "$RUNAS_ID" = "$UID" ] &&
7657                 skip_env "RUNAS_ID = UID = $UID -- skipping"
7658         # Check that testing environment is properly set up. Skip if not
7659         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
7660                 skip_env "User $RUNAS_ID does not exist - skipping"
7661
7662         touch $DIR/$tfile
7663         chmod 777 $DIR/$tfile
7664         chmod ug+s $DIR/$tfile
7665         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
7666                 error "$RUNAS dd $DIR/$tfile failed"
7667         # See if we are still setuid/sgid
7668         test -u $DIR/$tfile -o -g $DIR/$tfile &&
7669                 error "S/gid is not dropped on write"
7670         # Now test that MDS is updated too
7671         cancel_lru_locks mdc
7672         test -u $DIR/$tfile -o -g $DIR/$tfile &&
7673                 error "S/gid is not dropped on MDS"
7674         rm -f $DIR/$tfile
7675 }
7676 run_test 72a "Test that remove suid works properly (bug5695) ===="
7677
7678 test_72b() { # bug 24226 -- keep mode setting when size is not changing
7679         local perm
7680
7681         [ "$RUNAS_ID" = "$UID" ] &&
7682                 skip_env "RUNAS_ID = UID = $UID -- skipping"
7683         [ "$RUNAS_ID" -eq 0 ] &&
7684                 skip_env "RUNAS_ID = 0 -- skipping"
7685         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7686         # Check that testing environment is properly set up. Skip if not
7687         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
7688                 skip_env "User $RUNAS_ID does not exist - skipping"
7689
7690         touch $DIR/${tfile}-f{g,u}
7691         test_mkdir $DIR/${tfile}-dg
7692         test_mkdir $DIR/${tfile}-du
7693         chmod 770 $DIR/${tfile}-{f,d}{g,u}
7694         chmod g+s $DIR/${tfile}-{f,d}g
7695         chmod u+s $DIR/${tfile}-{f,d}u
7696         for perm in 777 2777 4777; do
7697                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
7698                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
7699                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
7700                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
7701         done
7702         true
7703 }
7704 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
7705
7706 # bug 3462 - multiple simultaneous MDC requests
7707 test_73() {
7708         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7709
7710         test_mkdir $DIR/d73-1
7711         test_mkdir $DIR/d73-2
7712         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
7713         pid1=$!
7714
7715         lctl set_param fail_loc=0x80000129
7716         $MULTIOP $DIR/d73-1/f73-2 Oc &
7717         sleep 1
7718         lctl set_param fail_loc=0
7719
7720         $MULTIOP $DIR/d73-2/f73-3 Oc &
7721         pid3=$!
7722
7723         kill -USR1 $pid1
7724         wait $pid1 || return 1
7725
7726         sleep 25
7727
7728         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
7729         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
7730         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
7731
7732         rm -rf $DIR/d73-*
7733 }
7734 run_test 73 "multiple MDC requests (should not deadlock)"
7735
7736 test_74a() { # bug 6149, 6184
7737         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7738
7739         touch $DIR/f74a
7740         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
7741         #
7742         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
7743         # will spin in a tight reconnection loop
7744         $LCTL set_param fail_loc=0x8000030e
7745         # get any lock that won't be difficult - lookup works.
7746         ls $DIR/f74a
7747         $LCTL set_param fail_loc=0
7748         rm -f $DIR/f74a
7749         true
7750 }
7751 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
7752
7753 test_74b() { # bug 13310
7754         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7755
7756         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
7757         #
7758         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
7759         # will spin in a tight reconnection loop
7760         $LCTL set_param fail_loc=0x8000030e
7761         # get a "difficult" lock
7762         touch $DIR/f74b
7763         $LCTL set_param fail_loc=0
7764         rm -f $DIR/f74b
7765         true
7766 }
7767 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
7768
7769 test_74c() {
7770         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7771
7772         #define OBD_FAIL_LDLM_NEW_LOCK
7773         $LCTL set_param fail_loc=0x319
7774         touch $DIR/$tfile && error "touch successful"
7775         $LCTL set_param fail_loc=0
7776         true
7777 }
7778 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
7779
7780 num_inodes() {
7781         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
7782 }
7783
7784 test_76() { # Now for bug 20433, added originally in bug 1443
7785         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7786
7787         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
7788
7789         cancel_lru_locks osc
7790         BEFORE_INODES=$(num_inodes)
7791         echo "before inodes: $BEFORE_INODES"
7792         local COUNT=1000
7793         [ "$SLOW" = "no" ] && COUNT=100
7794         for i in $(seq $COUNT); do
7795                 touch $DIR/$tfile
7796                 rm -f $DIR/$tfile
7797         done
7798         cancel_lru_locks osc
7799         AFTER_INODES=$(num_inodes)
7800         echo "after inodes: $AFTER_INODES"
7801         local wait=0
7802         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
7803                 sleep 2
7804                 AFTER_INODES=$(num_inodes)
7805                 wait=$((wait+2))
7806                 echo "wait $wait seconds inodes: $AFTER_INODES"
7807                 if [ $wait -gt 30 ]; then
7808                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
7809                 fi
7810         done
7811 }
7812 run_test 76 "confirm clients recycle inodes properly ===="
7813
7814
7815 export ORIG_CSUM=""
7816 set_checksums()
7817 {
7818         # Note: in sptlrpc modes which enable its own bulk checksum, the
7819         # original crc32_le bulk checksum will be automatically disabled,
7820         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
7821         # will be checked by sptlrpc code against sptlrpc bulk checksum.
7822         # In this case set_checksums() will not be no-op, because sptlrpc
7823         # bulk checksum will be enabled all through the test.
7824
7825         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
7826         lctl set_param -n osc.*.checksums $1
7827         return 0
7828 }
7829
7830 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
7831                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
7832 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
7833                              tr -d [] | head -n1)}
7834 set_checksum_type()
7835 {
7836         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
7837         log "set checksum type to $1"
7838         return 0
7839 }
7840 F77_TMP=$TMP/f77-temp
7841 F77SZ=8
7842 setup_f77() {
7843         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
7844                 error "error writing to $F77_TMP"
7845 }
7846
7847 test_77a() { # bug 10889
7848         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7849         $GSS && skip_env "could not run with gss"
7850
7851         [ ! -f $F77_TMP ] && setup_f77
7852         set_checksums 1
7853         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
7854         set_checksums 0
7855         rm -f $DIR/$tfile
7856 }
7857 run_test 77a "normal checksum read/write operation"
7858
7859 test_77b() { # bug 10889
7860         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7861         $GSS && skip_env "could not run with gss"
7862
7863         [ ! -f $F77_TMP ] && setup_f77
7864         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7865         $LCTL set_param fail_loc=0x80000409
7866         set_checksums 1
7867
7868         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7869                 error "dd error: $?"
7870         $LCTL set_param fail_loc=0
7871
7872         for algo in $CKSUM_TYPES; do
7873                 cancel_lru_locks osc
7874                 set_checksum_type $algo
7875                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7876                 $LCTL set_param fail_loc=0x80000408
7877                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
7878                 $LCTL set_param fail_loc=0
7879         done
7880         set_checksums 0
7881         set_checksum_type $ORIG_CSUM_TYPE
7882         rm -f $DIR/$tfile
7883 }
7884 run_test 77b "checksum error on client write, read"
7885
7886 cleanup_77c() {
7887         trap 0
7888         set_checksums 0
7889         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
7890         $check_ost &&
7891                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
7892         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
7893         $check_ost && [ -n "$ost_file_prefix" ] &&
7894                 do_facet ost1 rm -f ${ost_file_prefix}\*
7895 }
7896
7897 test_77c() {
7898         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7899         $GSS && skip_env "could not run with gss"
7900         remote_ost_nodsh && skip "remote OST with nodsh"
7901
7902         local bad1
7903         local osc_file_prefix
7904         local osc_file
7905         local check_ost=false
7906         local ost_file_prefix
7907         local ost_file
7908         local orig_cksum
7909         local dump_cksum
7910         local fid
7911
7912         # ensure corruption will occur on first OSS/OST
7913         $LFS setstripe -i 0 $DIR/$tfile
7914
7915         [ ! -f $F77_TMP ] && setup_f77
7916         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7917                 error "dd write error: $?"
7918         fid=$($LFS path2fid $DIR/$tfile)
7919
7920         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
7921         then
7922                 check_ost=true
7923                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
7924                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
7925         else
7926                 echo "OSS do not support bulk pages dump upon error"
7927         fi
7928
7929         osc_file_prefix=$($LCTL get_param -n debug_path)
7930         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
7931
7932         trap cleanup_77c EXIT
7933
7934         set_checksums 1
7935         # enable bulk pages dump upon error on Client
7936         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
7937         # enable bulk pages dump upon error on OSS
7938         $check_ost &&
7939                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
7940
7941         # flush Client cache to allow next read to reach OSS
7942         cancel_lru_locks osc
7943
7944         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
7945         $LCTL set_param fail_loc=0x80000408
7946         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
7947         $LCTL set_param fail_loc=0
7948
7949         rm -f $DIR/$tfile
7950
7951         # check cksum dump on Client
7952         osc_file=$(ls ${osc_file_prefix}*)
7953         [ -n "$osc_file" ] || error "no checksum dump file on Client"
7954         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
7955         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
7956         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
7957         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
7958                      cksum)
7959         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
7960         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7961                 error "dump content does not match on Client"
7962
7963         $check_ost || skip "No need to check cksum dump on OSS"
7964
7965         # check cksum dump on OSS
7966         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
7967         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
7968         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
7969         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
7970         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7971                 error "dump content does not match on OSS"
7972
7973         cleanup_77c
7974 }
7975 run_test 77c "checksum error on client read with debug"
7976
7977 test_77d() { # bug 10889
7978         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7979         $GSS && skip_env "could not run with gss"
7980
7981         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7982         $LCTL set_param fail_loc=0x80000409
7983         set_checksums 1
7984         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
7985                 error "direct write: rc=$?"
7986         $LCTL set_param fail_loc=0
7987         set_checksums 0
7988
7989         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7990         $LCTL set_param fail_loc=0x80000408
7991         set_checksums 1
7992         cancel_lru_locks osc
7993         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
7994                 error "direct read: rc=$?"
7995         $LCTL set_param fail_loc=0
7996         set_checksums 0
7997 }
7998 run_test 77d "checksum error on OST direct write, read"
7999
8000 test_77f() { # bug 10889
8001         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8002         $GSS && skip_env "could not run with gss"
8003
8004         set_checksums 1
8005         for algo in $CKSUM_TYPES; do
8006                 cancel_lru_locks osc
8007                 set_checksum_type $algo
8008                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8009                 $LCTL set_param fail_loc=0x409
8010                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
8011                         error "direct write succeeded"
8012                 $LCTL set_param fail_loc=0
8013         done
8014         set_checksum_type $ORIG_CSUM_TYPE
8015         set_checksums 0
8016 }
8017 run_test 77f "repeat checksum error on write (expect error)"
8018
8019 test_77g() { # bug 10889
8020         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8021         $GSS && skip_env "could not run with gss"
8022         remote_ost_nodsh && skip "remote OST with nodsh"
8023
8024         [ ! -f $F77_TMP ] && setup_f77
8025
8026         local file=$DIR/$tfile
8027         stack_trap "rm -f $file" EXIT
8028
8029         $SETSTRIPE -c 1 -i 0 $file
8030         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
8031         do_facet ost1 lctl set_param fail_loc=0x8000021a
8032         set_checksums 1
8033         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
8034                 error "write error: rc=$?"
8035         do_facet ost1 lctl set_param fail_loc=0
8036         set_checksums 0
8037
8038         cancel_lru_locks osc
8039         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
8040         do_facet ost1 lctl set_param fail_loc=0x8000021b
8041         set_checksums 1
8042         cmp $F77_TMP $file || error "file compare failed"
8043         do_facet ost1 lctl set_param fail_loc=0
8044         set_checksums 0
8045 }
8046 run_test 77g "checksum error on OST write, read"
8047
8048 test_77k() { # LU-10906
8049         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8050         $GSS && skip_env "could not run with gss"
8051
8052         local cksum_param="osc.$FSNAME*.checksums"
8053         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
8054         local checksum
8055         local i
8056
8057         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
8058         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
8059         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
8060                 EXIT
8061
8062         for i in 0 1; do
8063                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
8064                         error "failed to set checksum=$i on MGS"
8065                 wait_update $HOSTNAME "$get_checksum" $i
8066                 #remount
8067                 echo "remount client, checksum should be $i"
8068                 remount_client $MOUNT || "failed to remount client"
8069                 checksum=$(eval $get_checksum)
8070                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8071         done
8072
8073         for opt in "checksum" "nochecksum"; do
8074                 #remount with mount option
8075                 echo "remount client with option $opt, checksum should be $i"
8076                 umount_client $MOUNT || "failed to umount client"
8077                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
8078                         "failed to mount client with option '$opt'"
8079                 checksum=$(eval $get_checksum)
8080                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8081                 i=$((i - 1))
8082         done
8083
8084         remount_client $MOUNT || "failed to remount client"
8085 }
8086 run_test 77k "enable/disable checksum correctly"
8087
8088 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
8089 rm -f $F77_TMP
8090 unset F77_TMP
8091
8092 cleanup_test_78() {
8093         trap 0
8094         rm -f $DIR/$tfile
8095 }
8096
8097 test_78() { # bug 10901
8098         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8099         remote_ost || skip_env "local OST"
8100
8101         NSEQ=5
8102         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
8103         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
8104         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
8105         echo "MemTotal: $MEMTOTAL"
8106
8107         # reserve 256MB of memory for the kernel and other running processes,
8108         # and then take 1/2 of the remaining memory for the read/write buffers.
8109         if [ $MEMTOTAL -gt 512 ] ;then
8110                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
8111         else
8112                 # for those poor memory-starved high-end clusters...
8113                 MEMTOTAL=$((MEMTOTAL / 2))
8114         fi
8115         echo "Mem to use for directio: $MEMTOTAL"
8116
8117         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
8118         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
8119         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
8120         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
8121                 head -n1)
8122         echo "Smallest OST: $SMALLESTOST"
8123         [[ $SMALLESTOST -lt 10240 ]] &&
8124                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
8125
8126         trap cleanup_test_78 EXIT
8127
8128         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
8129                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
8130
8131         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
8132         echo "File size: $F78SIZE"
8133         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
8134         for i in $(seq 1 $NSEQ); do
8135                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
8136                 echo directIO rdwr round $i of $NSEQ
8137                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
8138         done
8139
8140         cleanup_test_78
8141 }
8142 run_test 78 "handle large O_DIRECT writes correctly ============"
8143
8144 test_79() { # bug 12743
8145         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8146
8147         wait_delete_completed
8148
8149         BKTOTAL=$(calc_osc_kbytes kbytestotal)
8150         BKFREE=$(calc_osc_kbytes kbytesfree)
8151         BKAVAIL=$(calc_osc_kbytes kbytesavail)
8152
8153         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
8154         DFTOTAL=`echo $STRING | cut -d, -f1`
8155         DFUSED=`echo $STRING  | cut -d, -f2`
8156         DFAVAIL=`echo $STRING | cut -d, -f3`
8157         DFFREE=$(($DFTOTAL - $DFUSED))
8158
8159         ALLOWANCE=$((64 * $OSTCOUNT))
8160
8161         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
8162            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
8163                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
8164         fi
8165         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
8166            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
8167                 error "df free($DFFREE) mismatch OST free($BKFREE)"
8168         fi
8169         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
8170            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
8171                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
8172         fi
8173 }
8174 run_test 79 "df report consistency check ======================="
8175
8176 test_80() { # bug 10718
8177         remote_ost_nodsh && skip "remote OST with nodsh"
8178         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8179
8180         # relax strong synchronous semantics for slow backends like ZFS
8181         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
8182                 local soc="obdfilter.*.sync_lock_cancel"
8183                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8184
8185                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
8186                 if [ -z "$save" ]; then
8187                         soc="obdfilter.*.sync_on_lock_cancel"
8188                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8189                 fi
8190
8191                 if [ "$save" != "never" ]; then
8192                         local hosts=$(comma_list $(osts_nodes))
8193
8194                         do_nodes $hosts $LCTL set_param $soc=never
8195                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
8196                 fi
8197         fi
8198
8199         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
8200         sync; sleep 1; sync
8201         local before=$(date +%s)
8202         cancel_lru_locks osc
8203         local after=$(date +%s)
8204         local diff=$((after - before))
8205         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
8206
8207         rm -f $DIR/$tfile
8208 }
8209 run_test 80 "Page eviction is equally fast at high offsets too"
8210
8211 test_81a() { # LU-456
8212         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8213         remote_ost_nodsh && skip "remote OST with nodsh"
8214
8215         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8216         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
8217         do_facet ost1 lctl set_param fail_loc=0x80000228
8218
8219         # write should trigger a retry and success
8220         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
8221         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8222         RC=$?
8223         if [ $RC -ne 0 ] ; then
8224                 error "write should success, but failed for $RC"
8225         fi
8226 }
8227 run_test 81a "OST should retry write when get -ENOSPC ==============="
8228
8229 test_81b() { # LU-456
8230         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8231         remote_ost_nodsh && skip "remote OST with nodsh"
8232
8233         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8234         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
8235         do_facet ost1 lctl set_param fail_loc=0x228
8236
8237         # write should retry several times and return -ENOSPC finally
8238         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
8239         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8240         RC=$?
8241         ENOSPC=28
8242         if [ $RC -ne $ENOSPC ] ; then
8243                 error "dd should fail for -ENOSPC, but succeed."
8244         fi
8245 }
8246 run_test 81b "OST should return -ENOSPC when retry still fails ======="
8247
8248 test_82() { # LU-1031
8249         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
8250         local gid1=14091995
8251         local gid2=16022000
8252
8253         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
8254         local MULTIPID1=$!
8255         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
8256         local MULTIPID2=$!
8257         kill -USR1 $MULTIPID2
8258         sleep 2
8259         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
8260                 error "First grouplock does not block second one"
8261         else
8262                 echo "Second grouplock blocks first one"
8263         fi
8264         kill -USR1 $MULTIPID1
8265         wait $MULTIPID1
8266         wait $MULTIPID2
8267 }
8268 run_test 82 "Basic grouplock test"
8269
8270 test_99() {
8271         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
8272
8273         test_mkdir $DIR/$tdir.cvsroot
8274         chown $RUNAS_ID $DIR/$tdir.cvsroot
8275
8276         cd $TMP
8277         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
8278
8279         cd /etc/init.d
8280         # some versions of cvs import exit(1) when asked to import links or
8281         # files they can't read.  ignore those files.
8282         local toignore=$(find . -type l -printf '-I %f\n' -o \
8283                          ! -perm /4 -printf '-I %f\n')
8284         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
8285                 $tdir.reposname vtag rtag
8286
8287         cd $DIR
8288         test_mkdir $DIR/$tdir.reposname
8289         chown $RUNAS_ID $DIR/$tdir.reposname
8290         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
8291
8292         cd $DIR/$tdir.reposname
8293         $RUNAS touch foo99
8294         $RUNAS cvs add -m 'addmsg' foo99
8295         $RUNAS cvs update
8296         $RUNAS cvs commit -m 'nomsg' foo99
8297         rm -fr $DIR/$tdir.cvsroot
8298 }
8299 run_test 99 "cvs strange file/directory operations"
8300
8301 test_100() {
8302         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8303         [[ "$NETTYPE" =~ tcp ]] ||
8304                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
8305         remote_ost_nodsh && skip "remote OST with nodsh"
8306         remote_mds_nodsh && skip "remote MDS with nodsh"
8307         remote_servers ||
8308                 skip "useless for local single node setup"
8309
8310         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
8311                 [ "$PROT" != "tcp" ] && continue
8312                 RPORT=$(echo $REMOTE | cut -d: -f2)
8313                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
8314
8315                 rc=0
8316                 LPORT=`echo $LOCAL | cut -d: -f2`
8317                 if [ $LPORT -ge 1024 ]; then
8318                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
8319                         netstat -tna
8320                         error_exit "local: $LPORT > 1024, remote: $RPORT"
8321                 fi
8322         done
8323         [ "$rc" = 0 ] || error_exit "privileged port not found" )
8324 }
8325 run_test 100 "check local port using privileged port ==========="
8326
8327 function get_named_value()
8328 {
8329     local tag
8330
8331     tag=$1
8332     while read ;do
8333         line=$REPLY
8334         case $line in
8335         $tag*)
8336             echo $line | sed "s/^$tag[ ]*//"
8337             break
8338             ;;
8339         esac
8340     done
8341 }
8342
8343 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
8344                    awk '/^max_cached_mb/ { print $2 }')
8345
8346 cleanup_101a() {
8347         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
8348         trap 0
8349 }
8350
8351 test_101a() {
8352         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8353         [ $MDSCOUNT -ge 2 ] && skip_env "needs < 2 MDTs" #LU-4322
8354
8355         local s
8356         local discard
8357         local nreads=10000
8358         local cache_limit=32
8359
8360         $LCTL set_param -n osc.*-osc*.rpc_stats 0
8361         trap cleanup_101a EXIT
8362         $LCTL set_param -n llite.*.read_ahead_stats 0
8363         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
8364
8365         #
8366         # randomly read 10000 of 64K chunks from file 3x 32MB in size
8367         #
8368         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
8369         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
8370
8371         discard=0
8372         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
8373                 get_named_value 'read but discarded' | cut -d" " -f1); do
8374                         discard=$(($discard + $s))
8375         done
8376         cleanup_101a
8377
8378         if [[ $(($discard * 10)) -gt $nreads ]]; then
8379                 $LCTL get_param osc.*-osc*.rpc_stats
8380                 $LCTL get_param llite.*.read_ahead_stats
8381                 error "too many ($discard) discarded pages"
8382         fi
8383         rm -f $DIR/$tfile || true
8384 }
8385 run_test 101a "check read-ahead for random reads"
8386
8387 setup_test101bc() {
8388         test_mkdir $DIR/$tdir
8389         local STRIPE_SIZE=$1
8390         local FILE_LENGTH=$2
8391         STRIPE_OFFSET=0
8392
8393         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
8394
8395         local list=$(comma_list $(osts_nodes))
8396         set_osd_param $list '' read_cache_enable 0
8397         set_osd_param $list '' writethrough_cache_enable 0
8398
8399         trap cleanup_test101bc EXIT
8400         # prepare the read-ahead file
8401         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
8402
8403         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
8404                                 count=$FILE_SIZE_MB 2> /dev/null
8405
8406 }
8407
8408 cleanup_test101bc() {
8409         trap 0
8410         rm -rf $DIR/$tdir
8411         rm -f $DIR/$tfile
8412
8413         local list=$(comma_list $(osts_nodes))
8414         set_osd_param $list '' read_cache_enable 1
8415         set_osd_param $list '' writethrough_cache_enable 1
8416 }
8417
8418 calc_total() {
8419         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
8420 }
8421
8422 ra_check_101() {
8423         local READ_SIZE=$1
8424         local STRIPE_SIZE=$2
8425         local FILE_LENGTH=$3
8426         local RA_INC=1048576
8427         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
8428         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
8429                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
8430         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
8431                         get_named_value 'read but discarded' |
8432                         cut -d" " -f1 | calc_total)
8433         if [[ $DISCARD -gt $discard_limit ]]; then
8434                 $LCTL get_param llite.*.read_ahead_stats
8435                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
8436         else
8437                 echo "Read-ahead success for size ${READ_SIZE}"
8438         fi
8439 }
8440
8441 test_101b() {
8442         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8443         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8444
8445         local STRIPE_SIZE=1048576
8446         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
8447
8448         if [ $SLOW == "yes" ]; then
8449                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
8450         else
8451                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
8452         fi
8453
8454         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
8455
8456         # prepare the read-ahead file
8457         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8458         cancel_lru_locks osc
8459         for BIDX in 2 4 8 16 32 64 128 256
8460         do
8461                 local BSIZE=$((BIDX*4096))
8462                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
8463                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
8464                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
8465                 $LCTL set_param -n llite.*.read_ahead_stats 0
8466                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
8467                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
8468                 cancel_lru_locks osc
8469                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
8470         done
8471         cleanup_test101bc
8472         true
8473 }
8474 run_test 101b "check stride-io mode read-ahead ================="
8475
8476 test_101c() {
8477         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8478
8479         local STRIPE_SIZE=1048576
8480         local FILE_LENGTH=$((STRIPE_SIZE*100))
8481         local nreads=10000
8482         local rsize=65536
8483         local osc_rpc_stats
8484
8485         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8486
8487         cancel_lru_locks osc
8488         $LCTL set_param osc.*.rpc_stats 0
8489         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
8490         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
8491                 local stats=$($LCTL get_param -n $osc_rpc_stats)
8492                 local lines=$(echo "$stats" | awk 'END {print NR;}')
8493                 local size
8494
8495                 if [ $lines -le 20 ]; then
8496                         continue
8497                 fi
8498                 for size in 1 2 4 8; do
8499                         local rpc=$(echo "$stats" |
8500                                     awk '($1 == "'$size':") {print $2; exit; }')
8501                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
8502                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
8503                 done
8504                 echo "$osc_rpc_stats check passed!"
8505         done
8506         cleanup_test101bc
8507         true
8508 }
8509 run_test 101c "check stripe_size aligned read-ahead ================="
8510
8511 set_read_ahead() {
8512         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
8513         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
8514 }
8515
8516 test_101d() {
8517         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8518
8519         local file=$DIR/$tfile
8520         local sz_MB=${FILESIZE_101d:-500}
8521         local ra_MB=${READAHEAD_MB:-40}
8522
8523         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
8524         [ $free_MB -lt $sz_MB ] &&
8525                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
8526
8527         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
8528         $SETSTRIPE -c -1 $file || error "setstripe failed"
8529
8530         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
8531         echo Cancel LRU locks on lustre client to flush the client cache
8532         cancel_lru_locks osc
8533
8534         echo Disable read-ahead
8535         local old_READAHEAD=$(set_read_ahead 0)
8536
8537         echo Reading the test file $file with read-ahead disabled
8538         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8539
8540         echo Cancel LRU locks on lustre client to flush the client cache
8541         cancel_lru_locks osc
8542         echo Enable read-ahead with ${ra_MB}MB
8543         set_read_ahead $ra_MB
8544
8545         echo Reading the test file $file with read-ahead enabled
8546         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8547
8548         echo "read-ahead disabled time read $raOFF"
8549         echo "read-ahead enabled  time read $raON"
8550
8551         set_read_ahead $old_READAHEAD
8552         rm -f $file
8553         wait_delete_completed
8554
8555         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
8556                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
8557 }
8558 run_test 101d "file read with and without read-ahead enabled"
8559
8560 test_101e() {
8561         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8562
8563         local file=$DIR/$tfile
8564         local size_KB=500  #KB
8565         local count=100
8566         local bsize=1024
8567
8568         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
8569         local need_KB=$((count * size_KB))
8570         [[ $free_KB -le $need_KB ]] &&
8571                 skip_env "Need free space $need_KB, have $free_KB"
8572
8573         echo "Creating $count ${size_KB}K test files"
8574         for ((i = 0; i < $count; i++)); do
8575                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
8576         done
8577
8578         echo "Cancel LRU locks on lustre client to flush the client cache"
8579         cancel_lru_locks $OSC
8580
8581         echo "Reset readahead stats"
8582         $LCTL set_param -n llite.*.read_ahead_stats 0
8583
8584         for ((i = 0; i < $count; i++)); do
8585                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
8586         done
8587
8588         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
8589                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
8590
8591         for ((i = 0; i < $count; i++)); do
8592                 rm -rf $file.$i 2>/dev/null
8593         done
8594
8595         #10000 means 20% reads are missing in readahead
8596         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
8597 }
8598 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
8599
8600 test_101f() {
8601         which iozone || skip_env "no iozone installed"
8602
8603         local old_debug=$($LCTL get_param debug)
8604         old_debug=${old_debug#*=}
8605         $LCTL set_param debug="reada mmap"
8606
8607         # create a test file
8608         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
8609
8610         echo Cancel LRU locks on lustre client to flush the client cache
8611         cancel_lru_locks osc
8612
8613         echo Reset readahead stats
8614         $LCTL set_param -n llite.*.read_ahead_stats 0
8615
8616         echo mmap read the file with small block size
8617         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
8618                 > /dev/null 2>&1
8619
8620         echo checking missing pages
8621         $LCTL get_param llite.*.read_ahead_stats
8622         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
8623                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
8624
8625         $LCTL set_param debug="$old_debug"
8626         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
8627         rm -f $DIR/$tfile
8628 }
8629 run_test 101f "check mmap read performance"
8630
8631 test_101g_brw_size_test() {
8632         local mb=$1
8633         local pages=$((mb * 1048576 / PAGE_SIZE))
8634         local file=$DIR/$tfile
8635
8636         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
8637                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
8638         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
8639                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
8640                         return 2
8641         done
8642
8643         stack_trap "rm -f $file" EXIT
8644         $LCTL set_param -n osc.*.rpc_stats=0
8645
8646         # 10 RPCs should be enough for the test
8647         local count=10
8648         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
8649                 { error "dd write ${mb} MB blocks failed"; return 3; }
8650         cancel_lru_locks osc
8651         dd of=/dev/null if=$file bs=${mb}M count=$count ||
8652                 { error "dd write ${mb} MB blocks failed"; return 4; }
8653
8654         # calculate number of full-sized read and write RPCs
8655         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
8656                 sed -n '/pages per rpc/,/^$/p' |
8657                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
8658                 END { print reads,writes }'))
8659         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
8660                 return 5
8661         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
8662                 return 6
8663
8664         return 0
8665 }
8666
8667 test_101g() {
8668         remote_ost_nodsh && skip "remote OST with nodsh"
8669
8670         local rpcs
8671         local osts=$(get_facets OST)
8672         local list=$(comma_list $(osts_nodes))
8673         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
8674         local brw_size="obdfilter.*.brw_size"
8675
8676         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8677
8678         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
8679         if [ $OST1_VERSION -ge $(version_code 2.8.52) -o \
8680              \( $OST1_VERSION -ge $(version_code 2.7.17) -a \
8681                 $OST1_VERSION -lt $(version_code 2.7.50) \) ] &&
8682            [ $CLIENT_VERSION -ge $(version_code 2.8.52) -o \
8683              \( $CLIENT_VERSION -ge $(version_code 2.7.17) -a \
8684                 $CLIENT_VERSION -lt $(version_code 2.7.50) \) ]; then
8685                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] && suffix="M"
8686                 if [[ $orig_mb -lt 16 ]]; then
8687                         save_lustre_params $osts "$brw_size" > $p
8688                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
8689                                 error "set 16MB RPC size failed"
8690
8691                         echo "remount client to enable new RPC size"
8692                         remount_client $MOUNT || error "remount_client failed"
8693                 fi
8694
8695                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
8696                 # should be able to set brw_size=12, but no rpc_stats for that
8697                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
8698         fi
8699
8700         test_101g_brw_size_test 4 || error "4MB RPC test failed"
8701
8702         if [[ $orig_mb -lt 16 ]]; then
8703                 restore_lustre_params < $p
8704                 remount_client $MOUNT || error "remount_client restore failed"
8705         fi
8706
8707         rm -f $p $DIR/$tfile
8708 }
8709 run_test 101g "Big bulk(4/16 MiB) readahead"
8710
8711 setup_test102() {
8712         test_mkdir $DIR/$tdir
8713         chown $RUNAS_ID $DIR/$tdir
8714         STRIPE_SIZE=65536
8715         STRIPE_OFFSET=1
8716         STRIPE_COUNT=$OSTCOUNT
8717         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
8718
8719         trap cleanup_test102 EXIT
8720         cd $DIR
8721         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
8722         cd $DIR/$tdir
8723         for num in 1 2 3 4; do
8724                 for count in $(seq 1 $STRIPE_COUNT); do
8725                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
8726                                 local size=`expr $STRIPE_SIZE \* $num`
8727                                 local file=file"$num-$idx-$count"
8728                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
8729                         done
8730                 done
8731         done
8732
8733         cd $DIR
8734         $1 tar cf $TMP/f102.tar $tdir --xattrs
8735 }
8736
8737 cleanup_test102() {
8738         trap 0
8739         rm -f $TMP/f102.tar
8740         rm -rf $DIR/d0.sanity/d102
8741 }
8742
8743 test_102a() {
8744         [ "$UID" != 0 ] && skip "must run as root"
8745         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
8746                 skip_env "must have user_xattr"
8747
8748         [ -z "$(which setfattr 2>/dev/null)" ] &&
8749                 skip_env "could not find setfattr"
8750
8751         local testfile=$DIR/$tfile
8752
8753         touch $testfile
8754         echo "set/get xattr..."
8755         setfattr -n trusted.name1 -v value1 $testfile ||
8756                 error "setfattr -n trusted.name1=value1 $testfile failed"
8757         getfattr -n trusted.name1 $testfile 2> /dev/null |
8758           grep "trusted.name1=.value1" ||
8759                 error "$testfile missing trusted.name1=value1"
8760
8761         setfattr -n user.author1 -v author1 $testfile ||
8762                 error "setfattr -n user.author1=author1 $testfile failed"
8763         getfattr -n user.author1 $testfile 2> /dev/null |
8764           grep "user.author1=.author1" ||
8765                 error "$testfile missing trusted.author1=author1"
8766
8767         echo "listxattr..."
8768         setfattr -n trusted.name2 -v value2 $testfile ||
8769                 error "$testfile unable to set trusted.name2"
8770         setfattr -n trusted.name3 -v value3 $testfile ||
8771                 error "$testfile unable to set trusted.name3"
8772         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
8773             grep "trusted.name" | wc -l) -eq 3 ] ||
8774                 error "$testfile missing 3 trusted.name xattrs"
8775
8776         setfattr -n user.author2 -v author2 $testfile ||
8777                 error "$testfile unable to set user.author2"
8778         setfattr -n user.author3 -v author3 $testfile ||
8779                 error "$testfile unable to set user.author3"
8780         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
8781             grep "user.author" | wc -l) -eq 3 ] ||
8782                 error "$testfile missing 3 user.author xattrs"
8783
8784         echo "remove xattr..."
8785         setfattr -x trusted.name1 $testfile ||
8786                 error "$testfile error deleting trusted.name1"
8787         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
8788                 error "$testfile did not delete trusted.name1 xattr"
8789
8790         setfattr -x user.author1 $testfile ||
8791                 error "$testfile error deleting user.author1"
8792         echo "set lustre special xattr ..."
8793         $LFS setstripe -c1 $testfile
8794         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
8795                 awk -F "=" '/trusted.lov/ { print $2 }' )
8796         setfattr -n "trusted.lov" -v $lovea $testfile ||
8797                 error "$testfile doesn't ignore setting trusted.lov again"
8798         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
8799                 error "$testfile allow setting invalid trusted.lov"
8800         rm -f $testfile
8801 }
8802 run_test 102a "user xattr test =================================="
8803
8804 check_102b_layout() {
8805         local layout="$*"
8806         local testfile=$DIR/$tfile
8807
8808         echo "test layout '$layout'"
8809         $LFS setstripe $layout $testfile || error "setstripe failed"
8810         $LFS getstripe -y $testfile
8811
8812         echo "get/set/list trusted.lov xattr ..." # b=10930
8813         local value=$(getfattr -n trusted.lov -e hex $testfile | grep trusted)
8814         [[ "$value" =~ "trusted.lov" ]] ||
8815                 error "can't get trusted.lov from $testfile"
8816         local stripe_count_orig=$($LFS getstripe -c $testfile) ||
8817                 error "getstripe failed"
8818
8819         $MCREATE $testfile.2 || error "mcreate $testfile.2 failed"
8820
8821         value=$(cut -d= -f2 <<<$value)
8822         # LU-13168: truncated xattr should fail if short lov_user_md header
8823         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
8824                 lens="${#value}" || lens="$(seq 4 2 ${#value})"
8825         for len in $lens; do
8826                 echo "setfattr $len $testfile.2"
8827                 setfattr -n trusted.lov -v ${value:0:$len} $testfile.2 &&
8828                         [ $len -lt 66 ] && error "short xattr len=$len worked"
8829         done
8830         local stripe_size=$($LFS getstripe -S $testfile.2)
8831         local stripe_count=$($LFS getstripe -c $testfile.2)
8832         [[ $stripe_size -eq 65536 ]] ||
8833                 error "stripe size $stripe_size != 65536"
8834         [[ $stripe_count -eq $stripe_count_orig ]] ||
8835                 error "stripe count $stripe_count != $stripe_count_orig"
8836         rm $testfile $testfile.2
8837 }
8838
8839 test_102b() {
8840         [ -z "$(which setfattr 2>/dev/null)" ] &&
8841                 skip_env "could not find setfattr"
8842         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8843
8844         # check plain layout
8845         check_102b_layout -S 65536 -i 1 -c $OSTCOUNT
8846
8847         # and also check composite layout
8848         check_102b_layout -E 1M -S 65536 -i 1 -c $OSTCOUNT -Eeof -S4M
8849
8850 }
8851 run_test 102b "getfattr/setfattr for trusted.lov EAs"
8852
8853 test_102c() {
8854         [ -z "$(which setfattr 2>/dev/null)" ] &&
8855                 skip_env "could not find setfattr"
8856         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8857
8858         # b10930: get/set/list lustre.lov xattr
8859         echo "get/set/list lustre.lov xattr ..."
8860         test_mkdir $DIR/$tdir
8861         chown $RUNAS_ID $DIR/$tdir
8862         local testfile=$DIR/$tdir/$tfile
8863         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
8864                 error "setstripe failed"
8865         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
8866                 error "getstripe failed"
8867         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
8868         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
8869
8870         local testfile2=${testfile}2
8871         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
8872                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
8873
8874         $RUNAS $MCREATE $testfile2
8875         $RUNAS setfattr -n lustre.lov -v $value $testfile2
8876         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
8877         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
8878         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
8879         [ $stripe_count -eq $STRIPECOUNT ] ||
8880                 error "stripe count $stripe_count != $STRIPECOUNT"
8881 }
8882 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
8883
8884 compare_stripe_info1() {
8885         local stripe_index_all_zero=true
8886
8887         for num in 1 2 3 4; do
8888                 for count in $(seq 1 $STRIPE_COUNT); do
8889                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
8890                                 local size=$((STRIPE_SIZE * num))
8891                                 local file=file"$num-$offset-$count"
8892                                 stripe_size=$($LFS getstripe -S $PWD/$file)
8893                                 [[ $stripe_size -ne $size ]] &&
8894                                     error "$file: size $stripe_size != $size"
8895                                 stripe_count=$($LFS getstripe -c $PWD/$file)
8896                                 # allow fewer stripes to be created, ORI-601
8897                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
8898                                     error "$file: count $stripe_count != $count"
8899                                 stripe_index=$($LFS getstripe -i $PWD/$file)
8900                                 [[ $stripe_index -ne 0 ]] &&
8901                                         stripe_index_all_zero=false
8902                         done
8903                 done
8904         done
8905         $stripe_index_all_zero &&
8906                 error "all files are being extracted starting from OST index 0"
8907         return 0
8908 }
8909
8910 have_xattrs_include() {
8911         tar --help | grep -q xattrs-include &&
8912                 echo --xattrs-include="lustre.*"
8913 }
8914
8915 test_102d() {
8916         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8917         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8918
8919         XINC=$(have_xattrs_include)
8920         setup_test102
8921         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
8922         cd $DIR/$tdir/$tdir
8923         compare_stripe_info1
8924 }
8925 run_test 102d "tar restore stripe info from tarfile,not keep osts"
8926
8927 test_102f() {
8928         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8929         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8930
8931         XINC=$(have_xattrs_include)
8932         setup_test102
8933         test_mkdir $DIR/$tdir.restore
8934         cd $DIR
8935         tar cf - --xattrs $tdir | tar xf - \
8936                 -C $DIR/$tdir.restore --xattrs $XINC
8937         cd $DIR/$tdir.restore/$tdir
8938         compare_stripe_info1
8939 }
8940 run_test 102f "tar copy files, not keep osts"
8941
8942 grow_xattr() {
8943         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
8944                 skip "must have user_xattr"
8945         [ -z "$(which setfattr 2>/dev/null)" ] &&
8946                 skip_env "could not find setfattr"
8947         [ -z "$(which getfattr 2>/dev/null)" ] &&
8948                 skip_env "could not find getfattr"
8949
8950         local xsize=${1:-1024}  # in bytes
8951         local file=$DIR/$tfile
8952         local value="$(generate_string $xsize)"
8953         local xbig=trusted.big
8954
8955         touch $file
8956         log "save $xbig on $file"
8957         setfattr -n $xbig -v $value $file ||
8958                 error "saving $xbig on $file failed"
8959
8960         local orig=$(get_xattr_value $xbig $file)
8961         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
8962
8963         local xsml=trusted.sml
8964         log "save $xsml on $file"
8965         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
8966
8967         local new=$(get_xattr_value $xbig $file)
8968         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
8969
8970         log "grow $xsml on $file"
8971         setfattr -n $xsml -v "$value" $file ||
8972                 error "growing $xsml on $file failed"
8973
8974         new=$(get_xattr_value $xbig $file)
8975         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
8976         log "$xbig still valid after growing $xsml"
8977
8978         rm -f $file
8979 }
8980
8981 test_102h() { # bug 15777
8982         grow_xattr 1024
8983 }
8984 run_test 102h "grow xattr from inside inode to external block"
8985
8986 test_102ha() {
8987         large_xattr_enabled || skip_env "ea_inode feature disabled"
8988
8989         grow_xattr $(max_xattr_size)
8990 }
8991 run_test 102ha "grow xattr from inside inode to external inode"
8992
8993 test_102i() { # bug 17038
8994         [ -z "$(which getfattr 2>/dev/null)" ] &&
8995                 skip "could not find getfattr"
8996
8997         touch $DIR/$tfile
8998         ln -s $DIR/$tfile $DIR/${tfile}link
8999         getfattr -n trusted.lov $DIR/$tfile ||
9000                 error "lgetxattr on $DIR/$tfile failed"
9001         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
9002                 grep -i "no such attr" ||
9003                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
9004         rm -f $DIR/$tfile $DIR/${tfile}link
9005 }
9006 run_test 102i "lgetxattr test on symbolic link ============"
9007
9008 test_102j() {
9009         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9010         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9011
9012         XINC=$(have_xattrs_include)
9013         setup_test102 "$RUNAS"
9014         chown $RUNAS_ID $DIR/$tdir
9015         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9016         cd $DIR/$tdir/$tdir
9017         compare_stripe_info1 "$RUNAS"
9018 }
9019 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
9020
9021 test_102k() {
9022         [ -z "$(which setfattr 2>/dev/null)" ] &&
9023                 skip "could not find setfattr"
9024
9025         touch $DIR/$tfile
9026         # b22187 just check that does not crash for regular file.
9027         setfattr -n trusted.lov $DIR/$tfile
9028         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
9029         local test_kdir=$DIR/$tdir
9030         test_mkdir $test_kdir
9031         local default_size=$($LFS getstripe -S $test_kdir)
9032         local default_count=$($LFS getstripe -c $test_kdir)
9033         local default_offset=$($LFS getstripe -i $test_kdir)
9034         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
9035                 error 'dir setstripe failed'
9036         setfattr -n trusted.lov $test_kdir
9037         local stripe_size=$($LFS getstripe -S $test_kdir)
9038         local stripe_count=$($LFS getstripe -c $test_kdir)
9039         local stripe_offset=$($LFS getstripe -i $test_kdir)
9040         [ $stripe_size -eq $default_size ] ||
9041                 error "stripe size $stripe_size != $default_size"
9042         [ $stripe_count -eq $default_count ] ||
9043                 error "stripe count $stripe_count != $default_count"
9044         [ $stripe_offset -eq $default_offset ] ||
9045                 error "stripe offset $stripe_offset != $default_offset"
9046         rm -rf $DIR/$tfile $test_kdir
9047 }
9048 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
9049
9050 test_102l() {
9051         [ -z "$(which getfattr 2>/dev/null)" ] &&
9052                 skip "could not find getfattr"
9053
9054         # LU-532 trusted. xattr is invisible to non-root
9055         local testfile=$DIR/$tfile
9056
9057         touch $testfile
9058
9059         echo "listxattr as user..."
9060         chown $RUNAS_ID $testfile
9061         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
9062             grep -q "trusted" &&
9063                 error "$testfile trusted xattrs are user visible"
9064
9065         return 0;
9066 }
9067 run_test 102l "listxattr size test =================================="
9068
9069 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
9070         local path=$DIR/$tfile
9071         touch $path
9072
9073         listxattr_size_check $path || error "listattr_size_check $path failed"
9074 }
9075 run_test 102m "Ensure listxattr fails on small bufffer ========"
9076
9077 cleanup_test102
9078
9079 getxattr() { # getxattr path name
9080         # Return the base64 encoding of the value of xattr name on path.
9081         local path=$1
9082         local name=$2
9083
9084         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
9085         # file: $path
9086         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9087         #
9088         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9089
9090         getfattr --absolute-names --encoding=base64 --name=$name $path |
9091                 awk -F= -v name=$name '$1 == name {
9092                         print substr($0, index($0, "=") + 1);
9093         }'
9094 }
9095
9096 test_102n() { # LU-4101 mdt: protect internal xattrs
9097         [ -z "$(which setfattr 2>/dev/null)" ] &&
9098                 skip "could not find setfattr"
9099         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
9100         then
9101                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
9102         fi
9103
9104         local file0=$DIR/$tfile.0
9105         local file1=$DIR/$tfile.1
9106         local xattr0=$TMP/$tfile.0
9107         local xattr1=$TMP/$tfile.1
9108         local namelist="lov lma lmv link fid version som hsm"
9109         local name
9110         local value
9111
9112         rm -rf $file0 $file1 $xattr0 $xattr1
9113         touch $file0 $file1
9114
9115         # Get 'before' xattrs of $file1.
9116         getfattr --absolute-names --dump --match=- $file1 > $xattr0
9117
9118         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
9119                 namelist+=" lfsck_namespace"
9120         for name in $namelist; do
9121                 # Try to copy xattr from $file0 to $file1.
9122                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9123
9124                 setfattr --name=trusted.$name --value="$value" $file1 ||
9125                         error "setxattr 'trusted.$name' failed"
9126
9127                 # Try to set a garbage xattr.
9128                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9129
9130                 if [[ x$name == "xlov" ]]; then
9131                         setfattr --name=trusted.lov --value="$value" $file1 &&
9132                         error "setxattr invalid 'trusted.lov' success"
9133                 else
9134                         setfattr --name=trusted.$name --value="$value" $file1 ||
9135                                 error "setxattr invalid 'trusted.$name' failed"
9136                 fi
9137
9138                 # Try to remove the xattr from $file1. We don't care if this
9139                 # appears to succeed or fail, we just don't want there to be
9140                 # any changes or crashes.
9141                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9142         done
9143
9144         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
9145         then
9146                 name="lfsck_ns"
9147                 # Try to copy xattr from $file0 to $file1.
9148                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9149
9150                 setfattr --name=trusted.$name --value="$value" $file1 ||
9151                         error "setxattr 'trusted.$name' failed"
9152
9153                 # Try to set a garbage xattr.
9154                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9155
9156                 setfattr --name=trusted.$name --value="$value" $file1 ||
9157                         error "setxattr 'trusted.$name' failed"
9158
9159                 # Try to remove the xattr from $file1. We don't care if this
9160                 # appears to succeed or fail, we just don't want there to be
9161                 # any changes or crashes.
9162                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9163         fi
9164
9165         # Get 'after' xattrs of file1.
9166         getfattr --absolute-names --dump --match=- $file1 > $xattr1
9167
9168         if ! diff $xattr0 $xattr1; then
9169                 error "before and after xattrs of '$file1' differ"
9170         fi
9171
9172         rm -rf $file0 $file1 $xattr0 $xattr1
9173
9174         return 0
9175 }
9176 run_test 102n "silently ignore setxattr on internal trusted xattrs"
9177
9178 test_102p() { # LU-4703 setxattr did not check ownership
9179         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
9180                 skip "MDS needs to be at least 2.5.56"
9181
9182         local testfile=$DIR/$tfile
9183
9184         touch $testfile
9185
9186         echo "setfacl as user..."
9187         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
9188         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
9189
9190         echo "setfattr as user..."
9191         setfacl -m "u:$RUNAS_ID:---" $testfile
9192         $RUNAS setfattr -x system.posix_acl_access $testfile
9193         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
9194 }
9195 run_test 102p "check setxattr(2) correctly fails without permission"
9196
9197 test_102q() {
9198         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
9199                 skip "MDS needs to be at least 2.6.92"
9200
9201         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
9202 }
9203 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
9204
9205 test_102r() {
9206         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
9207                 skip "MDS needs to be at least 2.6.93"
9208
9209         touch $DIR/$tfile || error "touch"
9210         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
9211         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
9212         rm $DIR/$tfile || error "rm"
9213
9214         #normal directory
9215         mkdir -p $DIR/$tdir || error "mkdir"
9216         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9217         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9218         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9219                 error "$testfile error deleting user.author1"
9220         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9221                 grep "user.$(basename $tdir)" &&
9222                 error "$tdir did not delete user.$(basename $tdir)"
9223         rmdir $DIR/$tdir || error "rmdir"
9224
9225         #striped directory
9226         test_mkdir $DIR/$tdir
9227         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9228         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9229         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9230                 error "$testfile error deleting user.author1"
9231         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9232                 grep "user.$(basename $tdir)" &&
9233                 error "$tdir did not delete user.$(basename $tdir)"
9234         rmdir $DIR/$tdir || error "rm striped dir"
9235 }
9236 run_test 102r "set EAs with empty values"
9237
9238 test_102s() {
9239         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9240                 skip "MDS needs to be at least 2.11.52"
9241
9242         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9243
9244         save_lustre_params client "llite.*.xattr_cache" > $save
9245
9246         for cache in 0 1; do
9247                 lctl set_param llite.*.xattr_cache=$cache
9248
9249                 rm -f $DIR/$tfile
9250                 touch $DIR/$tfile || error "touch"
9251                 for prefix in lustre security system trusted user; do
9252                         # Note getxattr() may fail with 'Operation not
9253                         # supported' or 'No such attribute' depending
9254                         # on prefix and cache.
9255                         getfattr -n $prefix.n102s $DIR/$tfile &&
9256                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
9257                 done
9258         done
9259
9260         restore_lustre_params < $save
9261 }
9262 run_test 102s "getting nonexistent xattrs should fail"
9263
9264 test_102t() {
9265         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9266                 skip "MDS needs to be at least 2.11.52"
9267
9268         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9269
9270         save_lustre_params client "llite.*.xattr_cache" > $save
9271
9272         for cache in 0 1; do
9273                 lctl set_param llite.*.xattr_cache=$cache
9274
9275                 for buf_size in 0 256; do
9276                         rm -f $DIR/$tfile
9277                         touch $DIR/$tfile || error "touch"
9278                         setfattr -n user.multiop $DIR/$tfile
9279                         $MULTIOP $DIR/$tfile oa$buf_size ||
9280                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
9281                 done
9282         done
9283
9284         restore_lustre_params < $save
9285 }
9286 run_test 102t "zero length xattr values handled correctly"
9287
9288 run_acl_subtest()
9289 {
9290     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
9291     return $?
9292 }
9293
9294 test_103a() {
9295         [ "$UID" != 0 ] && skip "must run as root"
9296         $GSS && skip_env "could not run under gss"
9297         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
9298                 skip_env "must have acl enabled"
9299         [ -z "$(which setfacl 2>/dev/null)" ] &&
9300                 skip_env "could not find setfacl"
9301         remote_mds_nodsh && skip "remote MDS with nodsh"
9302
9303         gpasswd -a daemon bin                           # LU-5641
9304         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
9305
9306         declare -a identity_old
9307
9308         for num in $(seq $MDSCOUNT); do
9309                 switch_identity $num true || identity_old[$num]=$?
9310         done
9311
9312         SAVE_UMASK=$(umask)
9313         umask 0022
9314         mkdir -p $DIR/$tdir
9315         cd $DIR/$tdir
9316
9317         echo "performing cp ..."
9318         run_acl_subtest cp || error "run_acl_subtest cp failed"
9319         echo "performing getfacl-noacl..."
9320         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
9321         echo "performing misc..."
9322         run_acl_subtest misc || error  "misc test failed"
9323         echo "performing permissions..."
9324         run_acl_subtest permissions || error "permissions failed"
9325         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
9326         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
9327                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
9328                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
9329         then
9330                 echo "performing permissions xattr..."
9331                 run_acl_subtest permissions_xattr ||
9332                         error "permissions_xattr failed"
9333         fi
9334         echo "performing setfacl..."
9335         run_acl_subtest setfacl || error  "setfacl test failed"
9336
9337         # inheritance test got from HP
9338         echo "performing inheritance..."
9339         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
9340         chmod +x make-tree || error "chmod +x failed"
9341         run_acl_subtest inheritance || error "inheritance test failed"
9342         rm -f make-tree
9343
9344         echo "LU-974 ignore umask when acl is enabled..."
9345         run_acl_subtest 974 || error "LU-974 umask test failed"
9346         if [ $MDSCOUNT -ge 2 ]; then
9347                 run_acl_subtest 974_remote ||
9348                         error "LU-974 umask test failed under remote dir"
9349         fi
9350
9351         echo "LU-2561 newly created file is same size as directory..."
9352         if [ "$mds1_FSTYPE" != "zfs" ]; then
9353                 run_acl_subtest 2561 || error "LU-2561 test failed"
9354         else
9355                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
9356         fi
9357
9358         run_acl_subtest 4924 || error "LU-4924 test failed"
9359
9360         cd $SAVE_PWD
9361         umask $SAVE_UMASK
9362
9363         for num in $(seq $MDSCOUNT); do
9364                 if [ "${identity_old[$num]}" = 1 ]; then
9365                         switch_identity $num false || identity_old[$num]=$?
9366                 fi
9367         done
9368 }
9369 run_test 103a "acl test"
9370
9371 test_103b() {
9372         declare -a pids
9373         local U
9374
9375         for U in {0..511}; do
9376                 {
9377                 local O=$(printf "%04o" $U)
9378
9379                 umask $(printf "%04o" $((511 ^ $O)))
9380                 $LFS setstripe -c 1 $DIR/$tfile.s$O
9381                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
9382
9383                 (( $S == ($O & 0666) )) ||
9384                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
9385
9386                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
9387                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
9388                 (( $S == ($O & 0666) )) ||
9389                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
9390
9391                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
9392                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
9393                 (( $S == ($O & 0666) )) ||
9394                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
9395                 rm -f $DIR/$tfile.[smp]$0
9396                 } &
9397                 local pid=$!
9398
9399                 # limit the concurrently running threads to 64. LU-11878
9400                 local idx=$((U % 64))
9401                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
9402                 pids[idx]=$pid
9403         done
9404         wait
9405 }
9406 run_test 103b "umask lfs setstripe"
9407
9408 test_103c() {
9409         mkdir -p $DIR/$tdir
9410         cp -rp $DIR/$tdir $DIR/$tdir.bak
9411
9412         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
9413                 error "$DIR/$tdir shouldn't contain default ACL"
9414         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
9415                 error "$DIR/$tdir.bak shouldn't contain default ACL"
9416         true
9417 }
9418 run_test 103c "'cp -rp' won't set empty acl"
9419
9420 test_104a() {
9421         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9422
9423         touch $DIR/$tfile
9424         lfs df || error "lfs df failed"
9425         lfs df -ih || error "lfs df -ih failed"
9426         lfs df -h $DIR || error "lfs df -h $DIR failed"
9427         lfs df -i $DIR || error "lfs df -i $DIR failed"
9428         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
9429         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
9430
9431         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
9432         lctl --device %$OSC deactivate
9433         lfs df || error "lfs df with deactivated OSC failed"
9434         lctl --device %$OSC activate
9435         # wait the osc back to normal
9436         wait_osc_import_ready client ost
9437
9438         lfs df || error "lfs df with reactivated OSC failed"
9439         rm -f $DIR/$tfile
9440 }
9441 run_test 104a "lfs df [-ih] [path] test ========================="
9442
9443 test_104b() {
9444         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9445         [ $RUNAS_ID -eq $UID ] &&
9446                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9447
9448         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
9449                         grep "Permission denied" | wc -l)))
9450         if [ $denied_cnt -ne 0 ]; then
9451                 error "lfs check servers test failed"
9452         fi
9453 }
9454 run_test 104b "$RUNAS lfs check servers test ===================="
9455
9456 #
9457 # Verify $1 is within range of $2.
9458 # Success when $1 is within range. That is, when $1 is >= 2% of $2 and
9459 # $1 is <= 2% of $2. Else Fail.
9460 #
9461 value_in_range() {
9462         # Strip all units (M, G, T)
9463         actual=$(echo $1 | tr -d A-Z)
9464         expect=$(echo $2 | tr -d A-Z)
9465
9466         expect_lo=$(($expect * 98 / 100)) # 2% below
9467         expect_hi=$(($expect * 102 / 100)) # 2% above
9468
9469         # permit 2% drift above and below
9470         (( $actual >= $expect_lo && $actual <= $expect_hi ))
9471 }
9472
9473 test_104c() {
9474         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9475         [ "$ost1_FSTYPE" == "zfs" ] || skip "zfs only test"
9476
9477         local ost_param="osd-zfs.$FSNAME-OST0000."
9478         local mdt_param="osd-zfs.$FSNAME-MDT0000."
9479         local ofacets=$(get_facets OST)
9480         local mfacets=$(get_facets MDS)
9481         local saved_ost_blocks=
9482         local saved_mdt_blocks=
9483
9484         echo "Before recordsize change"
9485         lfs_df=($($LFS df -h | grep "filesystem_summary:"))
9486         df=($(df -h | grep "/mnt/lustre"$))
9487
9488         # For checking.
9489         echo "lfs output : ${lfs_df[*]}"
9490         echo "df  output : ${df[*]}"
9491
9492         for facet in ${ofacets//,/ }; do
9493                 if [ -z $saved_ost_blocks ]; then
9494                         saved_ost_blocks=$(do_facet $facet \
9495                                 lctl get_param -n $ost_param.blocksize)
9496                         echo "OST Blocksize: $saved_ost_blocks"
9497                 fi
9498                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
9499                 do_facet $facet zfs set recordsize=32768 $ost
9500         done
9501
9502         # BS too small. Sufficient for functional testing.
9503         for facet in ${mfacets//,/ }; do
9504                 if [ -z $saved_mdt_blocks ]; then
9505                         saved_mdt_blocks=$(do_facet $facet \
9506                                 lctl get_param -n $mdt_param.blocksize)
9507                         echo "MDT Blocksize: $saved_mdt_blocks"
9508                 fi
9509                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
9510                 do_facet $facet zfs set recordsize=32768 $mdt
9511         done
9512
9513         # Give new values chance to reflect change
9514         sleep 2
9515
9516         echo "After recordsize change"
9517         lfs_df_after=($($LFS df -h | grep "filesystem_summary:"))
9518         df_after=($(df -h | grep "/mnt/lustre"$))
9519
9520         # For checking.
9521         echo "lfs output : ${lfs_df_after[*]}"
9522         echo "df  output : ${df_after[*]}"
9523
9524         # Verify lfs df
9525         value_in_range ${lfs_df_after[1]%.*} ${lfs_df[1]%.*} ||
9526                 error "lfs_df bytes: ${lfs_df_after[1]%.*} != ${lfs_df[1]%.*}"
9527         value_in_range ${lfs_df_after[2]%.*} ${lfs_df[2]%.*} ||
9528                 error "lfs_df used: ${lfs_df_after[2]%.*} != ${lfs_df[2]%.*}"
9529         value_in_range ${lfs_df_after[3]%.*} ${lfs_df[3]%.*} ||
9530                 error "lfs_df avail: ${lfs_df_after[3]%.*} != ${lfs_df[3]%.*}"
9531
9532         # Verify df
9533         value_in_range ${df_after[1]%.*} ${df[1]%.*} ||
9534                 error "df bytes: ${df_after[1]%.*} != ${df[1]%.*}"
9535         value_in_range ${df_after[2]%.*} ${df[2]%.*} ||
9536                 error "df used: ${df_after[2]%.*} != ${df[2]%.*}"
9537         value_in_range ${df_after[3]%.*} ${df[3]%.*} ||
9538                 error "df avail: ${df_after[3]%.*} != ${df[3]%.*}"
9539
9540         # Restore MDT recordize back to original
9541         for facet in ${mfacets//,/ }; do
9542                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
9543                 do_facet $facet zfs set recordsize=$saved_mdt_blocks $mdt
9544         done
9545
9546         # Restore OST recordize back to original
9547         for facet in ${ofacets//,/ }; do
9548                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
9549                 do_facet $facet zfs set recordsize=$saved_ost_blocks $ost
9550         done
9551
9552         return 0
9553 }
9554 run_test 104c "Verify df vs lfs_df stays same after recordsize change"
9555
9556 test_105a() {
9557         # doesn't work on 2.4 kernels
9558         touch $DIR/$tfile
9559         if $(flock_is_enabled); then
9560                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
9561         else
9562                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
9563         fi
9564         rm -f $DIR/$tfile
9565 }
9566 run_test 105a "flock when mounted without -o flock test ========"
9567
9568 test_105b() {
9569         touch $DIR/$tfile
9570         if $(flock_is_enabled); then
9571                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
9572         else
9573                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
9574         fi
9575         rm -f $DIR/$tfile
9576 }
9577 run_test 105b "fcntl when mounted without -o flock test ========"
9578
9579 test_105c() {
9580         touch $DIR/$tfile
9581         if $(flock_is_enabled); then
9582                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
9583         else
9584                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
9585         fi
9586         rm -f $DIR/$tfile
9587 }
9588 run_test 105c "lockf when mounted without -o flock test"
9589
9590 test_105d() { # bug 15924
9591         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9592
9593         test_mkdir $DIR/$tdir
9594         flock_is_enabled || skip_env "mount w/o flock enabled"
9595         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
9596         $LCTL set_param fail_loc=0x80000315
9597         flocks_test 2 $DIR/$tdir
9598 }
9599 run_test 105d "flock race (should not freeze) ========"
9600
9601 test_105e() { # bug 22660 && 22040
9602         flock_is_enabled || skip_env "mount w/o flock enabled"
9603
9604         touch $DIR/$tfile
9605         flocks_test 3 $DIR/$tfile
9606 }
9607 run_test 105e "Two conflicting flocks from same process"
9608
9609 test_106() { #bug 10921
9610         test_mkdir $DIR/$tdir
9611         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
9612         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
9613 }
9614 run_test 106 "attempt exec of dir followed by chown of that dir"
9615
9616 test_107() {
9617         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9618
9619         CDIR=`pwd`
9620         local file=core
9621
9622         cd $DIR
9623         rm -f $file
9624
9625         local save_pattern=$(sysctl -n kernel.core_pattern)
9626         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
9627         sysctl -w kernel.core_pattern=$file
9628         sysctl -w kernel.core_uses_pid=0
9629
9630         ulimit -c unlimited
9631         sleep 60 &
9632         SLEEPPID=$!
9633
9634         sleep 1
9635
9636         kill -s 11 $SLEEPPID
9637         wait $SLEEPPID
9638         if [ -e $file ]; then
9639                 size=`stat -c%s $file`
9640                 [ $size -eq 0 ] && error "Fail to create core file $file"
9641         else
9642                 error "Fail to create core file $file"
9643         fi
9644         rm -f $file
9645         sysctl -w kernel.core_pattern=$save_pattern
9646         sysctl -w kernel.core_uses_pid=$save_uses_pid
9647         cd $CDIR
9648 }
9649 run_test 107 "Coredump on SIG"
9650
9651 test_110() {
9652         test_mkdir $DIR/$tdir
9653         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
9654         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
9655                 error "mkdir with 256 char should fail, but did not"
9656         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
9657                 error "create with 255 char failed"
9658         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
9659                 error "create with 256 char should fail, but did not"
9660
9661         ls -l $DIR/$tdir
9662         rm -rf $DIR/$tdir
9663 }
9664 run_test 110 "filename length checking"
9665
9666 #
9667 # Purpose: To verify dynamic thread (OSS) creation.
9668 #
9669 test_115() {
9670         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9671         remote_ost_nodsh && skip "remote OST with nodsh"
9672
9673         # Lustre does not stop service threads once they are started.
9674         # Reset number of running threads to default.
9675         stopall
9676         setupall
9677
9678         local OSTIO_pre
9679         local save_params="$TMP/sanity-$TESTNAME.parameters"
9680
9681         # Get ll_ost_io count before I/O
9682         OSTIO_pre=$(do_facet ost1 \
9683                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
9684         # Exit if lustre is not running (ll_ost_io not running).
9685         [ -z "$OSTIO_pre" ] && error "no OSS threads"
9686
9687         echo "Starting with $OSTIO_pre threads"
9688         local thread_max=$((OSTIO_pre * 2))
9689         local rpc_in_flight=$((thread_max * 2))
9690         # Number of I/O Process proposed to be started.
9691         local nfiles
9692         local facets=$(get_facets OST)
9693
9694         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
9695         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
9696
9697         # Set in_flight to $rpc_in_flight
9698         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
9699                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
9700         nfiles=${rpc_in_flight}
9701         # Set ost thread_max to $thread_max
9702         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
9703
9704         # 5 Minutes should be sufficient for max number of OSS
9705         # threads(thread_max) to be created.
9706         local timeout=300
9707
9708         # Start I/O.
9709         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
9710         test_mkdir $DIR/$tdir
9711         for i in $(seq $nfiles); do
9712                 local file=$DIR/$tdir/${tfile}-$i
9713                 $LFS setstripe -c -1 -i 0 $file
9714                 ($WTL $file $timeout)&
9715         done
9716
9717         # I/O Started - Wait for thread_started to reach thread_max or report
9718         # error if thread_started is more than thread_max.
9719         echo "Waiting for thread_started to reach thread_max"
9720         local thread_started=0
9721         local end_time=$((SECONDS + timeout))
9722
9723         while [ $SECONDS -le $end_time ] ; do
9724                 echo -n "."
9725                 # Get ost i/o thread_started count.
9726                 thread_started=$(do_facet ost1 \
9727                         "$LCTL get_param \
9728                         ost.OSS.ost_io.threads_started | cut -d= -f2")
9729                 # Break out if thread_started is equal/greater than thread_max
9730                 if [[ $thread_started -ge $thread_max ]]; then
9731                         echo ll_ost_io thread_started $thread_started, \
9732                                 equal/greater than thread_max $thread_max
9733                         break
9734                 fi
9735                 sleep 1
9736         done
9737
9738         # Cleanup - We have the numbers, Kill i/o jobs if running.
9739         jobcount=($(jobs -p))
9740         for i in $(seq 0 $((${#jobcount[@]}-1)))
9741         do
9742                 kill -9 ${jobcount[$i]}
9743                 if [ $? -ne 0 ] ; then
9744                         echo Warning: \
9745                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
9746                 fi
9747         done
9748
9749         # Cleanup files left by WTL binary.
9750         for i in $(seq $nfiles); do
9751                 local file=$DIR/$tdir/${tfile}-$i
9752                 rm -rf $file
9753                 if [ $? -ne 0 ] ; then
9754                         echo "Warning: Failed to delete file $file"
9755                 fi
9756         done
9757
9758         restore_lustre_params <$save_params
9759         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
9760
9761         # Error out if no new thread has started or Thread started is greater
9762         # than thread max.
9763         if [[ $thread_started -le $OSTIO_pre ||
9764                         $thread_started -gt $thread_max ]]; then
9765                 error "ll_ost_io: thread_started $thread_started" \
9766                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
9767                       "No new thread started or thread started greater " \
9768                       "than thread_max."
9769         fi
9770 }
9771 run_test 115 "verify dynamic thread creation===================="
9772
9773 free_min_max () {
9774         wait_delete_completed
9775         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
9776         echo "OST kbytes available: ${AVAIL[@]}"
9777         MAXV=${AVAIL[0]}
9778         MAXI=0
9779         MINV=${AVAIL[0]}
9780         MINI=0
9781         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
9782                 #echo OST $i: ${AVAIL[i]}kb
9783                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
9784                         MAXV=${AVAIL[i]}
9785                         MAXI=$i
9786                 fi
9787                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
9788                         MINV=${AVAIL[i]}
9789                         MINI=$i
9790                 fi
9791         done
9792         echo "Min free space: OST $MINI: $MINV"
9793         echo "Max free space: OST $MAXI: $MAXV"
9794 }
9795
9796 test_116a() { # was previously test_116()
9797         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9798         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9799         remote_mds_nodsh && skip "remote MDS with nodsh"
9800
9801         echo -n "Free space priority "
9802         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
9803                 head -n1
9804         declare -a AVAIL
9805         free_min_max
9806
9807         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
9808         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
9809         trap simple_cleanup_common EXIT
9810
9811         # Check if we need to generate uneven OSTs
9812         test_mkdir -p $DIR/$tdir/OST${MINI}
9813         local FILL=$((MINV / 4))
9814         local DIFF=$((MAXV - MINV))
9815         local DIFF2=$((DIFF * 100 / MINV))
9816
9817         local threshold=$(do_facet $SINGLEMDS \
9818                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
9819         threshold=${threshold%%%}
9820         echo -n "Check for uneven OSTs: "
9821         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
9822
9823         if [[ $DIFF2 -gt $threshold ]]; then
9824                 echo "ok"
9825                 echo "Don't need to fill OST$MINI"
9826         else
9827                 # generate uneven OSTs. Write 2% over the QOS threshold value
9828                 echo "no"
9829                 DIFF=$((threshold - DIFF2 + 2))
9830                 DIFF2=$((MINV * DIFF / 100))
9831                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
9832                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
9833                         error "setstripe failed"
9834                 DIFF=$((DIFF2 / 2048))
9835                 i=0
9836                 while [ $i -lt $DIFF ]; do
9837                         i=$((i + 1))
9838                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
9839                                 bs=2M count=1 2>/dev/null
9840                         echo -n .
9841                 done
9842                 echo .
9843                 sync
9844                 sleep_maxage
9845                 free_min_max
9846         fi
9847
9848         DIFF=$((MAXV - MINV))
9849         DIFF2=$((DIFF * 100 / MINV))
9850         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
9851         if [ $DIFF2 -gt $threshold ]; then
9852                 echo "ok"
9853         else
9854                 echo "failed - QOS mode won't be used"
9855                 simple_cleanup_common
9856                 skip "QOS imbalance criteria not met"
9857         fi
9858
9859         MINI1=$MINI
9860         MINV1=$MINV
9861         MAXI1=$MAXI
9862         MAXV1=$MAXV
9863
9864         # now fill using QOS
9865         $SETSTRIPE -c 1 $DIR/$tdir
9866         FILL=$((FILL / 200))
9867         if [ $FILL -gt 600 ]; then
9868                 FILL=600
9869         fi
9870         echo "writing $FILL files to QOS-assigned OSTs"
9871         i=0
9872         while [ $i -lt $FILL ]; do
9873                 i=$((i + 1))
9874                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
9875                         count=1 2>/dev/null
9876                 echo -n .
9877         done
9878         echo "wrote $i 200k files"
9879         sync
9880         sleep_maxage
9881
9882         echo "Note: free space may not be updated, so measurements might be off"
9883         free_min_max
9884         DIFF2=$((MAXV - MINV))
9885         echo "free space delta: orig $DIFF final $DIFF2"
9886         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
9887         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
9888         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
9889         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
9890         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
9891         if [[ $DIFF -gt 0 ]]; then
9892                 FILL=$((DIFF2 * 100 / DIFF - 100))
9893                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
9894         fi
9895
9896         # Figure out which files were written where
9897         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
9898                awk '/'$MINI1': / {print $2; exit}')
9899         echo $UUID
9900         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
9901         echo "$MINC files created on smaller OST $MINI1"
9902         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
9903                awk '/'$MAXI1': / {print $2; exit}')
9904         echo $UUID
9905         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
9906         echo "$MAXC files created on larger OST $MAXI1"
9907         if [[ $MINC -gt 0 ]]; then
9908                 FILL=$((MAXC * 100 / MINC - 100))
9909                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
9910         fi
9911         [[ $MAXC -gt $MINC ]] ||
9912                 error_ignore LU-9 "stripe QOS didn't balance free space"
9913         simple_cleanup_common
9914 }
9915 run_test 116a "stripe QOS: free space balance ==================="
9916
9917 test_116b() { # LU-2093
9918         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9919         remote_mds_nodsh && skip "remote MDS with nodsh"
9920
9921 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
9922         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
9923                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
9924         [ -z "$old_rr" ] && skip "no QOS"
9925         do_facet $SINGLEMDS lctl set_param \
9926                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
9927         mkdir -p $DIR/$tdir
9928         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
9929         createmany -o $DIR/$tdir/f- 20 || error "can't create"
9930         do_facet $SINGLEMDS lctl set_param fail_loc=0
9931         rm -rf $DIR/$tdir
9932         do_facet $SINGLEMDS lctl set_param \
9933                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
9934 }
9935 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
9936
9937 test_117() # bug 10891
9938 {
9939         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9940
9941         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
9942         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
9943         lctl set_param fail_loc=0x21e
9944         > $DIR/$tfile || error "truncate failed"
9945         lctl set_param fail_loc=0
9946         echo "Truncate succeeded."
9947         rm -f $DIR/$tfile
9948 }
9949 run_test 117 "verify osd extend =========="
9950
9951 NO_SLOW_RESENDCOUNT=4
9952 export OLD_RESENDCOUNT=""
9953 set_resend_count () {
9954         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
9955         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
9956         lctl set_param -n $PROC_RESENDCOUNT $1
9957         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
9958 }
9959
9960 # for reduce test_118* time (b=14842)
9961 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
9962
9963 # Reset async IO behavior after error case
9964 reset_async() {
9965         FILE=$DIR/reset_async
9966
9967         # Ensure all OSCs are cleared
9968         $SETSTRIPE -c -1 $FILE
9969         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
9970         sync
9971         rm $FILE
9972 }
9973
9974 test_118a() #bug 11710
9975 {
9976         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9977
9978         reset_async
9979
9980         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9981         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9982         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9983
9984         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9985                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9986                 return 1;
9987         fi
9988         rm -f $DIR/$tfile
9989 }
9990 run_test 118a "verify O_SYNC works =========="
9991
9992 test_118b()
9993 {
9994         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9995         remote_ost_nodsh && skip "remote OST with nodsh"
9996
9997         reset_async
9998
9999         #define OBD_FAIL_SRV_ENOENT 0x217
10000         set_nodes_failloc "$(osts_nodes)" 0x217
10001         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10002         RC=$?
10003         set_nodes_failloc "$(osts_nodes)" 0
10004         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10005         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10006                     grep -c writeback)
10007
10008         if [[ $RC -eq 0 ]]; then
10009                 error "Must return error due to dropped pages, rc=$RC"
10010                 return 1;
10011         fi
10012
10013         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10014                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10015                 return 1;
10016         fi
10017
10018         echo "Dirty pages not leaked on ENOENT"
10019
10020         # Due to the above error the OSC will issue all RPCs syncronously
10021         # until a subsequent RPC completes successfully without error.
10022         $MULTIOP $DIR/$tfile Ow4096yc
10023         rm -f $DIR/$tfile
10024
10025         return 0
10026 }
10027 run_test 118b "Reclaim dirty pages on fatal error =========="
10028
10029 test_118c()
10030 {
10031         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10032
10033         # for 118c, restore the original resend count, LU-1940
10034         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
10035                                 set_resend_count $OLD_RESENDCOUNT
10036         remote_ost_nodsh && skip "remote OST with nodsh"
10037
10038         reset_async
10039
10040         #define OBD_FAIL_OST_EROFS               0x216
10041         set_nodes_failloc "$(osts_nodes)" 0x216
10042
10043         # multiop should block due to fsync until pages are written
10044         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10045         MULTIPID=$!
10046         sleep 1
10047
10048         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10049                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10050         fi
10051
10052         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10053                     grep -c writeback)
10054         if [[ $WRITEBACK -eq 0 ]]; then
10055                 error "No page in writeback, writeback=$WRITEBACK"
10056         fi
10057
10058         set_nodes_failloc "$(osts_nodes)" 0
10059         wait $MULTIPID
10060         RC=$?
10061         if [[ $RC -ne 0 ]]; then
10062                 error "Multiop fsync failed, rc=$RC"
10063         fi
10064
10065         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10066         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10067                     grep -c writeback)
10068         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10069                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10070         fi
10071
10072         rm -f $DIR/$tfile
10073         echo "Dirty pages flushed via fsync on EROFS"
10074         return 0
10075 }
10076 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
10077
10078 # continue to use small resend count to reduce test_118* time (b=14842)
10079 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10080
10081 test_118d()
10082 {
10083         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10084         remote_ost_nodsh && skip "remote OST with nodsh"
10085
10086         reset_async
10087
10088         #define OBD_FAIL_OST_BRW_PAUSE_BULK
10089         set_nodes_failloc "$(osts_nodes)" 0x214
10090         # multiop should block due to fsync until pages are written
10091         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10092         MULTIPID=$!
10093         sleep 1
10094
10095         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10096                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10097         fi
10098
10099         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10100                     grep -c writeback)
10101         if [[ $WRITEBACK -eq 0 ]]; then
10102                 error "No page in writeback, writeback=$WRITEBACK"
10103         fi
10104
10105         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
10106         set_nodes_failloc "$(osts_nodes)" 0
10107
10108         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10109         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10110                     grep -c writeback)
10111         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10112                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10113         fi
10114
10115         rm -f $DIR/$tfile
10116         echo "Dirty pages gaurenteed flushed via fsync"
10117         return 0
10118 }
10119 run_test 118d "Fsync validation inject a delay of the bulk =========="
10120
10121 test_118f() {
10122         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10123
10124         reset_async
10125
10126         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
10127         lctl set_param fail_loc=0x8000040a
10128
10129         # Should simulate EINVAL error which is fatal
10130         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10131         RC=$?
10132         if [[ $RC -eq 0 ]]; then
10133                 error "Must return error due to dropped pages, rc=$RC"
10134         fi
10135
10136         lctl set_param fail_loc=0x0
10137
10138         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10139         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10140         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10141                     grep -c writeback)
10142         if [[ $LOCKED -ne 0 ]]; then
10143                 error "Locked pages remain in cache, locked=$LOCKED"
10144         fi
10145
10146         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10147                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10148         fi
10149
10150         rm -f $DIR/$tfile
10151         echo "No pages locked after fsync"
10152
10153         reset_async
10154         return 0
10155 }
10156 run_test 118f "Simulate unrecoverable OSC side error =========="
10157
10158 test_118g() {
10159         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10160
10161         reset_async
10162
10163         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
10164         lctl set_param fail_loc=0x406
10165
10166         # simulate local -ENOMEM
10167         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10168         RC=$?
10169
10170         lctl set_param fail_loc=0
10171         if [[ $RC -eq 0 ]]; then
10172                 error "Must return error due to dropped pages, rc=$RC"
10173         fi
10174
10175         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10176         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10177         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10178                         grep -c writeback)
10179         if [[ $LOCKED -ne 0 ]]; then
10180                 error "Locked pages remain in cache, locked=$LOCKED"
10181         fi
10182
10183         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10184                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10185         fi
10186
10187         rm -f $DIR/$tfile
10188         echo "No pages locked after fsync"
10189
10190         reset_async
10191         return 0
10192 }
10193 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
10194
10195 test_118h() {
10196         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10197         remote_ost_nodsh && skip "remote OST with nodsh"
10198
10199         reset_async
10200
10201         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10202         set_nodes_failloc "$(osts_nodes)" 0x20e
10203         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10204         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10205         RC=$?
10206
10207         set_nodes_failloc "$(osts_nodes)" 0
10208         if [[ $RC -eq 0 ]]; then
10209                 error "Must return error due to dropped pages, rc=$RC"
10210         fi
10211
10212         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10213         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10214         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10215                     grep -c writeback)
10216         if [[ $LOCKED -ne 0 ]]; then
10217                 error "Locked pages remain in cache, locked=$LOCKED"
10218         fi
10219
10220         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10221                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10222         fi
10223
10224         rm -f $DIR/$tfile
10225         echo "No pages locked after fsync"
10226
10227         return 0
10228 }
10229 run_test 118h "Verify timeout in handling recoverables errors  =========="
10230
10231 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10232
10233 test_118i() {
10234         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10235         remote_ost_nodsh && skip "remote OST with nodsh"
10236
10237         reset_async
10238
10239         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10240         set_nodes_failloc "$(osts_nodes)" 0x20e
10241
10242         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10243         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10244         PID=$!
10245         sleep 5
10246         set_nodes_failloc "$(osts_nodes)" 0
10247
10248         wait $PID
10249         RC=$?
10250         if [[ $RC -ne 0 ]]; then
10251                 error "got error, but should be not, rc=$RC"
10252         fi
10253
10254         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10255         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10256         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10257         if [[ $LOCKED -ne 0 ]]; then
10258                 error "Locked pages remain in cache, locked=$LOCKED"
10259         fi
10260
10261         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10262                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10263         fi
10264
10265         rm -f $DIR/$tfile
10266         echo "No pages locked after fsync"
10267
10268         return 0
10269 }
10270 run_test 118i "Fix error before timeout in recoverable error  =========="
10271
10272 [ "$SLOW" = "no" ] && set_resend_count 4
10273
10274 test_118j() {
10275         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10276         remote_ost_nodsh && skip "remote OST with nodsh"
10277
10278         reset_async
10279
10280         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
10281         set_nodes_failloc "$(osts_nodes)" 0x220
10282
10283         # return -EIO from OST
10284         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10285         RC=$?
10286         set_nodes_failloc "$(osts_nodes)" 0x0
10287         if [[ $RC -eq 0 ]]; then
10288                 error "Must return error due to dropped pages, rc=$RC"
10289         fi
10290
10291         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10292         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10293         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10294         if [[ $LOCKED -ne 0 ]]; then
10295                 error "Locked pages remain in cache, locked=$LOCKED"
10296         fi
10297
10298         # in recoverable error on OST we want resend and stay until it finished
10299         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10300                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10301         fi
10302
10303         rm -f $DIR/$tfile
10304         echo "No pages locked after fsync"
10305
10306         return 0
10307 }
10308 run_test 118j "Simulate unrecoverable OST side error =========="
10309
10310 test_118k()
10311 {
10312         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10313         remote_ost_nodsh && skip "remote OSTs with nodsh"
10314
10315         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10316         set_nodes_failloc "$(osts_nodes)" 0x20e
10317         test_mkdir $DIR/$tdir
10318
10319         for ((i=0;i<10;i++)); do
10320                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
10321                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
10322                 SLEEPPID=$!
10323                 sleep 0.500s
10324                 kill $SLEEPPID
10325                 wait $SLEEPPID
10326         done
10327
10328         set_nodes_failloc "$(osts_nodes)" 0
10329         rm -rf $DIR/$tdir
10330 }
10331 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
10332
10333 test_118l() # LU-646
10334 {
10335         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10336
10337         test_mkdir $DIR/$tdir
10338         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
10339         rm -rf $DIR/$tdir
10340 }
10341 run_test 118l "fsync dir"
10342
10343 test_118m() # LU-3066
10344 {
10345         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10346
10347         test_mkdir $DIR/$tdir
10348         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
10349         rm -rf $DIR/$tdir
10350 }
10351 run_test 118m "fdatasync dir ========="
10352
10353 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10354
10355 test_118n()
10356 {
10357         local begin
10358         local end
10359
10360         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10361         remote_ost_nodsh && skip "remote OSTs with nodsh"
10362
10363         # Sleep to avoid a cached response.
10364         #define OBD_STATFS_CACHE_SECONDS 1
10365         sleep 2
10366
10367         # Inject a 10 second delay in the OST_STATFS handler.
10368         #define OBD_FAIL_OST_STATFS_DELAY 0x242
10369         set_nodes_failloc "$(osts_nodes)" 0x242
10370
10371         begin=$SECONDS
10372         stat --file-system $MOUNT > /dev/null
10373         end=$SECONDS
10374
10375         set_nodes_failloc "$(osts_nodes)" 0
10376
10377         if ((end - begin > 20)); then
10378             error "statfs took $((end - begin)) seconds, expected 10"
10379         fi
10380 }
10381 run_test 118n "statfs() sends OST_STATFS requests in parallel"
10382
10383 test_119a() # bug 11737
10384 {
10385         BSIZE=$((512 * 1024))
10386         directio write $DIR/$tfile 0 1 $BSIZE
10387         # We ask to read two blocks, which is more than a file size.
10388         # directio will indicate an error when requested and actual
10389         # sizes aren't equeal (a normal situation in this case) and
10390         # print actual read amount.
10391         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
10392         if [ "$NOB" != "$BSIZE" ]; then
10393                 error "read $NOB bytes instead of $BSIZE"
10394         fi
10395         rm -f $DIR/$tfile
10396 }
10397 run_test 119a "Short directIO read must return actual read amount"
10398
10399 test_119b() # bug 11737
10400 {
10401         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10402
10403         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
10404         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
10405         sync
10406         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
10407                 error "direct read failed"
10408         rm -f $DIR/$tfile
10409 }
10410 run_test 119b "Sparse directIO read must return actual read amount"
10411
10412 test_119c() # bug 13099
10413 {
10414         BSIZE=1048576
10415         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
10416         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
10417         rm -f $DIR/$tfile
10418 }
10419 run_test 119c "Testing for direct read hitting hole"
10420
10421 test_119d() # bug 15950
10422 {
10423         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10424
10425         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
10426         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
10427         BSIZE=1048576
10428         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
10429         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
10430         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
10431         lctl set_param fail_loc=0x40d
10432         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
10433         pid_dio=$!
10434         sleep 1
10435         cat $DIR/$tfile > /dev/null &
10436         lctl set_param fail_loc=0
10437         pid_reads=$!
10438         wait $pid_dio
10439         log "the DIO writes have completed, now wait for the reads (should not block very long)"
10440         sleep 2
10441         [ -n "`ps h -p $pid_reads -o comm`" ] && \
10442         error "the read rpcs have not completed in 2s"
10443         rm -f $DIR/$tfile
10444         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
10445 }
10446 run_test 119d "The DIO path should try to send a new rpc once one is completed"
10447
10448 test_120a() {
10449         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10450         remote_mds_nodsh && skip "remote MDS with nodsh"
10451         test_mkdir $DIR/$tdir
10452         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10453                 skip_env "no early lock cancel on server"
10454
10455         lru_resize_disable mdc
10456         lru_resize_disable osc
10457         cancel_lru_locks mdc
10458         # asynchronous object destroy at MDT could cause bl ast to client
10459         cancel_lru_locks osc
10460
10461         stat $DIR/$tdir > /dev/null
10462         can1=$(do_facet $SINGLEMDS \
10463                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10464                awk '/ldlm_cancel/ {print $2}')
10465         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10466                awk '/ldlm_bl_callback/ {print $2}')
10467         test_mkdir -c1 $DIR/$tdir/d1
10468         can2=$(do_facet $SINGLEMDS \
10469                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10470                awk '/ldlm_cancel/ {print $2}')
10471         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10472                awk '/ldlm_bl_callback/ {print $2}')
10473         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10474         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10475         lru_resize_enable mdc
10476         lru_resize_enable osc
10477 }
10478 run_test 120a "Early Lock Cancel: mkdir test"
10479
10480 test_120b() {
10481         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10482         remote_mds_nodsh && skip "remote MDS with nodsh"
10483         test_mkdir $DIR/$tdir
10484         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10485                 skip_env "no early lock cancel on server"
10486
10487         lru_resize_disable mdc
10488         lru_resize_disable osc
10489         cancel_lru_locks mdc
10490         stat $DIR/$tdir > /dev/null
10491         can1=$(do_facet $SINGLEMDS \
10492                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10493                awk '/ldlm_cancel/ {print $2}')
10494         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10495                awk '/ldlm_bl_callback/ {print $2}')
10496         touch $DIR/$tdir/f1
10497         can2=$(do_facet $SINGLEMDS \
10498                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10499                awk '/ldlm_cancel/ {print $2}')
10500         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10501                awk '/ldlm_bl_callback/ {print $2}')
10502         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10503         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10504         lru_resize_enable mdc
10505         lru_resize_enable osc
10506 }
10507 run_test 120b "Early Lock Cancel: create test"
10508
10509 test_120c() {
10510         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10511         remote_mds_nodsh && skip "remote MDS with nodsh"
10512         test_mkdir -c1 $DIR/$tdir
10513         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10514                 skip "no early lock cancel on server"
10515
10516         lru_resize_disable mdc
10517         lru_resize_disable osc
10518         test_mkdir -c1 $DIR/$tdir/d1
10519         test_mkdir -c1 $DIR/$tdir/d2
10520         touch $DIR/$tdir/d1/f1
10521         cancel_lru_locks mdc
10522         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
10523         can1=$(do_facet $SINGLEMDS \
10524                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10525                awk '/ldlm_cancel/ {print $2}')
10526         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10527                awk '/ldlm_bl_callback/ {print $2}')
10528         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10529         can2=$(do_facet $SINGLEMDS \
10530                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10531                awk '/ldlm_cancel/ {print $2}')
10532         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10533                awk '/ldlm_bl_callback/ {print $2}')
10534         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10535         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10536         lru_resize_enable mdc
10537         lru_resize_enable osc
10538 }
10539 run_test 120c "Early Lock Cancel: link test"
10540
10541 test_120d() {
10542         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10543         remote_mds_nodsh && skip "remote MDS with nodsh"
10544         test_mkdir -c1 $DIR/$tdir
10545         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10546                 skip_env "no early lock cancel on server"
10547
10548         lru_resize_disable mdc
10549         lru_resize_disable osc
10550         touch $DIR/$tdir
10551         cancel_lru_locks mdc
10552         stat $DIR/$tdir > /dev/null
10553         can1=$(do_facet $SINGLEMDS \
10554                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10555                awk '/ldlm_cancel/ {print $2}')
10556         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10557                awk '/ldlm_bl_callback/ {print $2}')
10558         chmod a+x $DIR/$tdir
10559         can2=$(do_facet $SINGLEMDS \
10560                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10561                awk '/ldlm_cancel/ {print $2}')
10562         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10563                awk '/ldlm_bl_callback/ {print $2}')
10564         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10565         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10566         lru_resize_enable mdc
10567         lru_resize_enable osc
10568 }
10569 run_test 120d "Early Lock Cancel: setattr test"
10570
10571 test_120e() {
10572         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10573         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10574                 skip_env "no early lock cancel on server"
10575         remote_mds_nodsh && skip "remote MDS with nodsh"
10576
10577         local dlmtrace_set=false
10578
10579         test_mkdir -c1 $DIR/$tdir
10580         lru_resize_disable mdc
10581         lru_resize_disable osc
10582         ! $LCTL get_param debug | grep -q dlmtrace &&
10583                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
10584         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
10585         cancel_lru_locks mdc
10586         cancel_lru_locks osc
10587         dd if=$DIR/$tdir/f1 of=/dev/null
10588         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
10589         # XXX client can not do early lock cancel of OST lock
10590         # during unlink (LU-4206), so cancel osc lock now.
10591         sleep 2
10592         cancel_lru_locks osc
10593         can1=$(do_facet $SINGLEMDS \
10594                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10595                awk '/ldlm_cancel/ {print $2}')
10596         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10597                awk '/ldlm_bl_callback/ {print $2}')
10598         unlink $DIR/$tdir/f1
10599         sleep 5
10600         can2=$(do_facet $SINGLEMDS \
10601                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10602                awk '/ldlm_cancel/ {print $2}')
10603         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10604                awk '/ldlm_bl_callback/ {print $2}')
10605         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
10606                 $LCTL dk $TMP/cancel.debug.txt
10607         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
10608                 $LCTL dk $TMP/blocking.debug.txt
10609         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
10610         lru_resize_enable mdc
10611         lru_resize_enable osc
10612 }
10613 run_test 120e "Early Lock Cancel: unlink test"
10614
10615 test_120f() {
10616         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10617         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10618                 skip_env "no early lock cancel on server"
10619         remote_mds_nodsh && skip "remote MDS with nodsh"
10620
10621         test_mkdir -c1 $DIR/$tdir
10622         lru_resize_disable mdc
10623         lru_resize_disable osc
10624         test_mkdir -c1 $DIR/$tdir/d1
10625         test_mkdir -c1 $DIR/$tdir/d2
10626         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
10627         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
10628         cancel_lru_locks mdc
10629         cancel_lru_locks osc
10630         dd if=$DIR/$tdir/d1/f1 of=/dev/null
10631         dd if=$DIR/$tdir/d2/f2 of=/dev/null
10632         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
10633         # XXX client can not do early lock cancel of OST lock
10634         # during rename (LU-4206), so cancel osc lock now.
10635         sleep 2
10636         cancel_lru_locks osc
10637         can1=$(do_facet $SINGLEMDS \
10638                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10639                awk '/ldlm_cancel/ {print $2}')
10640         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10641                awk '/ldlm_bl_callback/ {print $2}')
10642         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10643         sleep 5
10644         can2=$(do_facet $SINGLEMDS \
10645                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10646                awk '/ldlm_cancel/ {print $2}')
10647         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10648                awk '/ldlm_bl_callback/ {print $2}')
10649         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10650         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10651         lru_resize_enable mdc
10652         lru_resize_enable osc
10653 }
10654 run_test 120f "Early Lock Cancel: rename test"
10655
10656 test_120g() {
10657         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10658         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10659                 skip_env "no early lock cancel on server"
10660         remote_mds_nodsh && skip "remote MDS with nodsh"
10661
10662         lru_resize_disable mdc
10663         lru_resize_disable osc
10664         count=10000
10665         echo create $count files
10666         test_mkdir $DIR/$tdir
10667         cancel_lru_locks mdc
10668         cancel_lru_locks osc
10669         t0=$(date +%s)
10670
10671         can0=$(do_facet $SINGLEMDS \
10672                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10673                awk '/ldlm_cancel/ {print $2}')
10674         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10675                awk '/ldlm_bl_callback/ {print $2}')
10676         createmany -o $DIR/$tdir/f $count
10677         sync
10678         can1=$(do_facet $SINGLEMDS \
10679                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10680                awk '/ldlm_cancel/ {print $2}')
10681         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10682                awk '/ldlm_bl_callback/ {print $2}')
10683         t1=$(date +%s)
10684         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
10685         echo rm $count files
10686         rm -r $DIR/$tdir
10687         sync
10688         can2=$(do_facet $SINGLEMDS \
10689                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10690                awk '/ldlm_cancel/ {print $2}')
10691         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10692                awk '/ldlm_bl_callback/ {print $2}')
10693         t2=$(date +%s)
10694         echo total: $count removes in $((t2-t1))
10695         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
10696         sleep 2
10697         # wait for commitment of removal
10698         lru_resize_enable mdc
10699         lru_resize_enable osc
10700 }
10701 run_test 120g "Early Lock Cancel: performance test"
10702
10703 test_121() { #bug #10589
10704         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10705
10706         rm -rf $DIR/$tfile
10707         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
10708 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
10709         lctl set_param fail_loc=0x310
10710         cancel_lru_locks osc > /dev/null
10711         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
10712         lctl set_param fail_loc=0
10713         [[ $reads -eq $writes ]] ||
10714                 error "read $reads blocks, must be $writes blocks"
10715 }
10716 run_test 121 "read cancel race ========="
10717
10718 test_123a() { # was test 123, statahead(bug 11401)
10719         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10720
10721         SLOWOK=0
10722         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
10723                 log "testing UP system. Performance may be lower than expected."
10724                 SLOWOK=1
10725         fi
10726
10727         rm -rf $DIR/$tdir
10728         test_mkdir $DIR/$tdir
10729         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
10730         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
10731         MULT=10
10732         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
10733                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
10734
10735                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
10736                 lctl set_param -n llite.*.statahead_max 0
10737                 lctl get_param llite.*.statahead_max
10738                 cancel_lru_locks mdc
10739                 cancel_lru_locks osc
10740                 stime=`date +%s`
10741                 time ls -l $DIR/$tdir | wc -l
10742                 etime=`date +%s`
10743                 delta=$((etime - stime))
10744                 log "ls $i files without statahead: $delta sec"
10745                 lctl set_param llite.*.statahead_max=$max
10746
10747                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
10748                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
10749                 cancel_lru_locks mdc
10750                 cancel_lru_locks osc
10751                 stime=`date +%s`
10752                 time ls -l $DIR/$tdir | wc -l
10753                 etime=`date +%s`
10754                 delta_sa=$((etime - stime))
10755                 log "ls $i files with statahead: $delta_sa sec"
10756                 lctl get_param -n llite.*.statahead_stats
10757                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
10758
10759                 [[ $swrong -lt $ewrong ]] &&
10760                         log "statahead was stopped, maybe too many locks held!"
10761                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
10762
10763                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
10764                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
10765                     lctl set_param -n llite.*.statahead_max 0
10766                     lctl get_param llite.*.statahead_max
10767                     cancel_lru_locks mdc
10768                     cancel_lru_locks osc
10769                     stime=`date +%s`
10770                     time ls -l $DIR/$tdir | wc -l
10771                     etime=`date +%s`
10772                     delta=$((etime - stime))
10773                     log "ls $i files again without statahead: $delta sec"
10774                     lctl set_param llite.*.statahead_max=$max
10775                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
10776                         if [  $SLOWOK -eq 0 ]; then
10777                                 error "ls $i files is slower with statahead!"
10778                         else
10779                                 log "ls $i files is slower with statahead!"
10780                         fi
10781                         break
10782                     fi
10783                 fi
10784
10785                 [ $delta -gt 20 ] && break
10786                 [ $delta -gt 8 ] && MULT=$((50 / delta))
10787                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
10788         done
10789         log "ls done"
10790
10791         stime=`date +%s`
10792         rm -r $DIR/$tdir
10793         sync
10794         etime=`date +%s`
10795         delta=$((etime - stime))
10796         log "rm -r $DIR/$tdir/: $delta seconds"
10797         log "rm done"
10798         lctl get_param -n llite.*.statahead_stats
10799 }
10800 run_test 123a "verify statahead work"
10801
10802 test_123b () { # statahead(bug 15027)
10803         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10804
10805         test_mkdir $DIR/$tdir
10806         createmany -o $DIR/$tdir/$tfile-%d 1000
10807
10808         cancel_lru_locks mdc
10809         cancel_lru_locks osc
10810
10811 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
10812         lctl set_param fail_loc=0x80000803
10813         ls -lR $DIR/$tdir > /dev/null
10814         log "ls done"
10815         lctl set_param fail_loc=0x0
10816         lctl get_param -n llite.*.statahead_stats
10817         rm -r $DIR/$tdir
10818         sync
10819
10820 }
10821 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
10822
10823 test_123c() {
10824         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
10825
10826         test_mkdir -i 0 -c 1 $DIR/$tdir.0
10827         test_mkdir -i 1 -c 1 $DIR/$tdir.1
10828         touch $DIR/$tdir.1/{1..3}
10829         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
10830
10831         remount_client $MOUNT
10832
10833         $MULTIOP $DIR/$tdir.0 Q
10834
10835         # let statahead to complete
10836         ls -l $DIR/$tdir.0 > /dev/null
10837
10838         testid=$(echo $TESTNAME | tr '_' ' ')
10839         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
10840                 error "statahead warning" || true
10841 }
10842 run_test 123c "Can not initialize inode warning on DNE statahead"
10843
10844 test_124a() {
10845         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10846         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10847                 skip_env "no lru resize on server"
10848
10849         local NR=2000
10850
10851         test_mkdir $DIR/$tdir
10852
10853         log "create $NR files at $DIR/$tdir"
10854         createmany -o $DIR/$tdir/f $NR ||
10855                 error "failed to create $NR files in $DIR/$tdir"
10856
10857         cancel_lru_locks mdc
10858         ls -l $DIR/$tdir > /dev/null
10859
10860         local NSDIR=""
10861         local LRU_SIZE=0
10862         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
10863                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
10864                 LRU_SIZE=$($LCTL get_param -n $PARAM)
10865                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
10866                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
10867                         log "NSDIR=$NSDIR"
10868                         log "NS=$(basename $NSDIR)"
10869                         break
10870                 fi
10871         done
10872
10873         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
10874                 skip "Not enough cached locks created!"
10875         fi
10876         log "LRU=$LRU_SIZE"
10877
10878         local SLEEP=30
10879
10880         # We know that lru resize allows one client to hold $LIMIT locks
10881         # for 10h. After that locks begin to be killed by client.
10882         local MAX_HRS=10
10883         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
10884         log "LIMIT=$LIMIT"
10885         if [ $LIMIT -lt $LRU_SIZE ]; then
10886                 skip "Limit is too small $LIMIT"
10887         fi
10888
10889         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
10890         # killing locks. Some time was spent for creating locks. This means
10891         # that up to the moment of sleep finish we must have killed some of
10892         # them (10-100 locks). This depends on how fast ther were created.
10893         # Many of them were touched in almost the same moment and thus will
10894         # be killed in groups.
10895         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
10896
10897         # Use $LRU_SIZE_B here to take into account real number of locks
10898         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
10899         local LRU_SIZE_B=$LRU_SIZE
10900         log "LVF=$LVF"
10901         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
10902         log "OLD_LVF=$OLD_LVF"
10903         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
10904
10905         # Let's make sure that we really have some margin. Client checks
10906         # cached locks every 10 sec.
10907         SLEEP=$((SLEEP+20))
10908         log "Sleep ${SLEEP} sec"
10909         local SEC=0
10910         while ((SEC<$SLEEP)); do
10911                 echo -n "..."
10912                 sleep 5
10913                 SEC=$((SEC+5))
10914                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
10915                 echo -n "$LRU_SIZE"
10916         done
10917         echo ""
10918         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
10919         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
10920
10921         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
10922                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
10923                 unlinkmany $DIR/$tdir/f $NR
10924                 return
10925         }
10926
10927         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
10928         log "unlink $NR files at $DIR/$tdir"
10929         unlinkmany $DIR/$tdir/f $NR
10930 }
10931 run_test 124a "lru resize ======================================="
10932
10933 get_max_pool_limit()
10934 {
10935         local limit=$($LCTL get_param \
10936                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
10937         local max=0
10938         for l in $limit; do
10939                 if [[ $l -gt $max ]]; then
10940                         max=$l
10941                 fi
10942         done
10943         echo $max
10944 }
10945
10946 test_124b() {
10947         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10948         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10949                 skip_env "no lru resize on server"
10950
10951         LIMIT=$(get_max_pool_limit)
10952
10953         NR=$(($(default_lru_size)*20))
10954         if [[ $NR -gt $LIMIT ]]; then
10955                 log "Limit lock number by $LIMIT locks"
10956                 NR=$LIMIT
10957         fi
10958
10959         IFree=$(mdsrate_inodes_available)
10960         if [ $IFree -lt $NR ]; then
10961                 log "Limit lock number by $IFree inodes"
10962                 NR=$IFree
10963         fi
10964
10965         lru_resize_disable mdc
10966         test_mkdir -p $DIR/$tdir/disable_lru_resize
10967
10968         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
10969         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
10970         cancel_lru_locks mdc
10971         stime=`date +%s`
10972         PID=""
10973         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10974         PID="$PID $!"
10975         sleep 2
10976         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10977         PID="$PID $!"
10978         sleep 2
10979         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10980         PID="$PID $!"
10981         wait $PID
10982         etime=`date +%s`
10983         nolruresize_delta=$((etime-stime))
10984         log "ls -la time: $nolruresize_delta seconds"
10985         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
10986         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
10987
10988         lru_resize_enable mdc
10989         test_mkdir -p $DIR/$tdir/enable_lru_resize
10990
10991         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
10992         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
10993         cancel_lru_locks mdc
10994         stime=`date +%s`
10995         PID=""
10996         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
10997         PID="$PID $!"
10998         sleep 2
10999         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11000         PID="$PID $!"
11001         sleep 2
11002         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11003         PID="$PID $!"
11004         wait $PID
11005         etime=`date +%s`
11006         lruresize_delta=$((etime-stime))
11007         log "ls -la time: $lruresize_delta seconds"
11008         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11009
11010         if [ $lruresize_delta -gt $nolruresize_delta ]; then
11011                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
11012         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
11013                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
11014         else
11015                 log "lru resize performs the same with no lru resize"
11016         fi
11017         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
11018 }
11019 run_test 124b "lru resize (performance test) ======================="
11020
11021 test_124c() {
11022         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11023         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11024                 skip_env "no lru resize on server"
11025
11026         # cache ununsed locks on client
11027         local nr=100
11028         cancel_lru_locks mdc
11029         test_mkdir $DIR/$tdir
11030         createmany -o $DIR/$tdir/f $nr ||
11031                 error "failed to create $nr files in $DIR/$tdir"
11032         ls -l $DIR/$tdir > /dev/null
11033
11034         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11035         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11036         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11037         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11038         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11039
11040         # set lru_max_age to 1 sec
11041         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11042         echo "sleep $((recalc_p * 2)) seconds..."
11043         sleep $((recalc_p * 2))
11044
11045         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11046         # restore lru_max_age
11047         $LCTL set_param -n $nsdir.lru_max_age $max_age
11048         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11049         unlinkmany $DIR/$tdir/f $nr
11050 }
11051 run_test 124c "LRUR cancel very aged locks"
11052
11053 test_124d() {
11054         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11055         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11056                 skip_env "no lru resize on server"
11057
11058         # cache ununsed locks on client
11059         local nr=100
11060
11061         lru_resize_disable mdc
11062         stack_trap "lru_resize_enable mdc" EXIT
11063
11064         cancel_lru_locks mdc
11065
11066         # asynchronous object destroy at MDT could cause bl ast to client
11067         test_mkdir $DIR/$tdir
11068         createmany -o $DIR/$tdir/f $nr ||
11069                 error "failed to create $nr files in $DIR/$tdir"
11070         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
11071
11072         ls -l $DIR/$tdir > /dev/null
11073
11074         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11075         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11076         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11077         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11078
11079         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11080
11081         # set lru_max_age to 1 sec
11082         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11083         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
11084
11085         echo "sleep $((recalc_p * 2)) seconds..."
11086         sleep $((recalc_p * 2))
11087
11088         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11089
11090         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11091 }
11092 run_test 124d "cancel very aged locks if lru-resize diasbaled"
11093
11094 test_125() { # 13358
11095         $LCTL get_param -n llite.*.client_type | grep -q local ||
11096                 skip "must run as local client"
11097         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
11098                 skip_env "must have acl enabled"
11099         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
11100
11101         test_mkdir $DIR/$tdir
11102         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
11103         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
11104         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
11105 }
11106 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
11107
11108 test_126() { # bug 12829/13455
11109         $GSS && skip_env "must run as gss disabled"
11110         $LCTL get_param -n llite.*.client_type | grep -q local ||
11111                 skip "must run as local client"
11112         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
11113
11114         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
11115         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
11116         rm -f $DIR/$tfile
11117         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
11118 }
11119 run_test 126 "check that the fsgid provided by the client is taken into account"
11120
11121 test_127a() { # bug 15521
11122         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11123
11124         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
11125         $LCTL set_param osc.*.stats=0
11126         FSIZE=$((2048 * 1024))
11127         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
11128         cancel_lru_locks osc
11129         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
11130
11131         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
11132         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
11133                 echo "got $COUNT $NAME"
11134                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
11135                 eval $NAME=$COUNT || error "Wrong proc format"
11136
11137                 case $NAME in
11138                         read_bytes|write_bytes)
11139                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
11140                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
11141                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
11142                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
11143                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
11144                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
11145                                 error "sumsquare is too small: $SUMSQ"
11146                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
11147                                 error "sumsquare is too big: $SUMSQ"
11148                         ;;
11149                         *) ;;
11150                 esac
11151         done < $DIR/${tfile}.tmp
11152
11153         #check that we actually got some stats
11154         [ "$read_bytes" ] || error "Missing read_bytes stats"
11155         [ "$write_bytes" ] || error "Missing write_bytes stats"
11156         [ "$read_bytes" != 0 ] || error "no read done"
11157         [ "$write_bytes" != 0 ] || error "no write done"
11158 }
11159 run_test 127a "verify the client stats are sane"
11160
11161 test_127b() { # bug LU-333
11162         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11163         local name count samp unit min max sum sumsq
11164
11165         $LCTL set_param llite.*.stats=0
11166
11167         # perform 2 reads and writes so MAX is different from SUM.
11168         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11169         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11170         cancel_lru_locks osc
11171         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11172         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11173
11174         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
11175         while read name count samp unit min max sum sumsq; do
11176                 echo "got $count $name"
11177                 eval $name=$count || error "Wrong proc format"
11178
11179                 case $name in
11180                 read_bytes)
11181                         [ $count -ne 2 ] && error "count is not 2: $count"
11182                         [ $min -ne $PAGE_SIZE ] &&
11183                                 error "min is not $PAGE_SIZE: $min"
11184                         [ $max -ne $PAGE_SIZE ] &&
11185                                 error "max is incorrect: $max"
11186                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
11187                                 error "sum is wrong: $sum"
11188                         ;;
11189                 write_bytes)
11190                         [ $count -ne 2 ] && error "count is not 2: $count"
11191                         [ $min -ne $PAGE_SIZE ] &&
11192                                 error "min is not $PAGE_SIZE: $min"
11193                         [ $max -ne $PAGE_SIZE ] &&
11194                                 error "max is incorrect: $max"
11195                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
11196                                 error "sum is wrong: $sum"
11197                         ;;
11198                 *) ;;
11199                 esac
11200         done < $TMP/$tfile.tmp
11201
11202         #check that we actually got some stats
11203         [ "$read_bytes" ] || error "Missing read_bytes stats"
11204         [ "$write_bytes" ] || error "Missing write_bytes stats"
11205         [ "$read_bytes" != 0 ] || error "no read done"
11206         [ "$write_bytes" != 0 ] || error "no write done"
11207
11208         rm -f $TMP/${tfile}.tmp
11209 }
11210 run_test 127b "verify the llite client stats are sane"
11211
11212 test_127c() { # LU-12394
11213         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11214         local size
11215         local bsize
11216         local reads
11217         local writes
11218         local count
11219
11220         $LCTL set_param llite.*.extents_stats=1
11221         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
11222
11223         # Use two stripes so there is enough space in default config
11224         $LFS setstripe -c 2 $DIR/$tfile
11225
11226         # Extent stats start at 0-4K and go in power of two buckets
11227         # LL_HIST_START = 12 --> 2^12 = 4K
11228         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
11229         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
11230         # small configs
11231         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
11232                 do
11233                 # Write and read, 2x each, second time at a non-zero offset
11234                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
11235                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
11236                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
11237                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
11238                 rm -f $DIR/$tfile
11239         done
11240
11241         $LCTL get_param llite.*.extents_stats
11242
11243         count=2
11244         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
11245                 do
11246                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
11247                                 grep -m 1 $bsize)
11248                 reads=$(echo $bucket | awk '{print $5}')
11249                 writes=$(echo $bucket | awk '{print $9}')
11250                 [ "$reads" -eq $count ] ||
11251                         error "$reads reads in < $bsize bucket, expect $count"
11252                 [ "$writes" -eq $count ] ||
11253                         error "$writes writes in < $bsize bucket, expect $count"
11254         done
11255
11256         # Test mmap write and read
11257         $LCTL set_param llite.*.extents_stats=c
11258         size=512
11259         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
11260         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
11261         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
11262
11263         $LCTL get_param llite.*.extents_stats
11264
11265         count=$(((size*1024) / PAGE_SIZE))
11266
11267         bsize=$((2 * PAGE_SIZE / 1024))K
11268
11269         bucket=$($LCTL get_param -n llite.*.extents_stats |
11270                         grep -m 1 $bsize)
11271         reads=$(echo $bucket | awk '{print $5}')
11272         writes=$(echo $bucket | awk '{print $9}')
11273         # mmap writes fault in the page first, creating an additonal read
11274         [ "$reads" -eq $((2 * count)) ] ||
11275                 error "$reads reads in < $bsize bucket, expect $count"
11276         [ "$writes" -eq $count ] ||
11277                 error "$writes writes in < $bsize bucket, expect $count"
11278 }
11279 run_test 127c "test llite extent stats with regular & mmap i/o"
11280
11281 test_128() { # bug 15212
11282         touch $DIR/$tfile
11283         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
11284                 find $DIR/$tfile
11285                 find $DIR/$tfile
11286         EOF
11287
11288         result=$(grep error $TMP/$tfile.log)
11289         rm -f $DIR/$tfile $TMP/$tfile.log
11290         [ -z "$result" ] ||
11291                 error "consecutive find's under interactive lfs failed"
11292 }
11293 run_test 128 "interactive lfs for 2 consecutive find's"
11294
11295 set_dir_limits () {
11296         local mntdev
11297         local canondev
11298         local node
11299
11300         local ldproc=/proc/fs/ldiskfs
11301         local facets=$(get_facets MDS)
11302
11303         for facet in ${facets//,/ }; do
11304                 canondev=$(ldiskfs_canon \
11305                            *.$(convert_facet2label $facet).mntdev $facet)
11306                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
11307                         ldproc=/sys/fs/ldiskfs
11308                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
11309                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
11310         done
11311 }
11312
11313 check_mds_dmesg() {
11314         local facets=$(get_facets MDS)
11315         for facet in ${facets//,/ }; do
11316                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
11317         done
11318         return 1
11319 }
11320
11321 test_129() {
11322         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11323         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
11324                 skip "Need MDS version with at least 2.5.56"
11325         if [ "$mds1_FSTYPE" != ldiskfs ]; then
11326                 skip_env "ldiskfs only test"
11327         fi
11328         remote_mds_nodsh && skip "remote MDS with nodsh"
11329
11330         local ENOSPC=28
11331         local EFBIG=27
11332         local has_warning=false
11333
11334         rm -rf $DIR/$tdir
11335         mkdir -p $DIR/$tdir
11336
11337         # block size of mds1
11338         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
11339         set_dir_limits $maxsize $maxsize
11340         local dirsize=$(stat -c%s "$DIR/$tdir")
11341         local nfiles=0
11342         while [[ $dirsize -le $maxsize ]]; do
11343                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
11344                 rc=$?
11345                 if ! $has_warning; then
11346                         check_mds_dmesg '"is approaching"' && has_warning=true
11347                 fi
11348                 # check two errors:
11349                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
11350                 # EFBIG for previous versions included in ldiskfs series
11351                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
11352                         set_dir_limits 0 0
11353                         echo "return code $rc received as expected"
11354
11355                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
11356                                 error_exit "create failed w/o dir size limit"
11357
11358                         check_mds_dmesg '"has reached"' ||
11359                                 error_exit "reached message should be output"
11360
11361                         [ $has_warning = "false" ] &&
11362                                 error_exit "warning message should be output"
11363
11364                         dirsize=$(stat -c%s "$DIR/$tdir")
11365
11366                         [[ $dirsize -ge $maxsize ]] && return 0
11367                         error_exit "current dir size $dirsize, " \
11368                                    "previous limit $maxsize"
11369                 elif [ $rc -ne 0 ]; then
11370                         set_dir_limits 0 0
11371                         error_exit "return $rc received instead of expected " \
11372                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
11373                 fi
11374                 nfiles=$((nfiles + 1))
11375                 dirsize=$(stat -c%s "$DIR/$tdir")
11376         done
11377
11378         set_dir_limits 0 0
11379         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
11380 }
11381 run_test 129 "test directory size limit ========================"
11382
11383 OLDIFS="$IFS"
11384 cleanup_130() {
11385         trap 0
11386         IFS="$OLDIFS"
11387 }
11388
11389 test_130a() {
11390         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11391         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11392
11393         trap cleanup_130 EXIT RETURN
11394
11395         local fm_file=$DIR/$tfile
11396         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
11397         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
11398                 error "dd failed for $fm_file"
11399
11400         # LU-1795: test filefrag/FIEMAP once, even if unsupported
11401         filefrag -ves $fm_file
11402         RC=$?
11403         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11404                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11405         [ $RC != 0 ] && error "filefrag $fm_file failed"
11406
11407         filefrag_op=$(filefrag -ve -k $fm_file |
11408                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11409         lun=$($GETSTRIPE -i $fm_file)
11410
11411         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
11412         IFS=$'\n'
11413         tot_len=0
11414         for line in $filefrag_op
11415         do
11416                 frag_lun=`echo $line | cut -d: -f5`
11417                 ext_len=`echo $line | cut -d: -f4`
11418                 if (( $frag_lun != $lun )); then
11419                         cleanup_130
11420                         error "FIEMAP on 1-stripe file($fm_file) failed"
11421                         return
11422                 fi
11423                 (( tot_len += ext_len ))
11424         done
11425
11426         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
11427                 cleanup_130
11428                 error "FIEMAP on 1-stripe file($fm_file) failed;"
11429                 return
11430         fi
11431
11432         cleanup_130
11433
11434         echo "FIEMAP on single striped file succeeded"
11435 }
11436 run_test 130a "FIEMAP (1-stripe file)"
11437
11438 test_130b() {
11439         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11440
11441         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11442         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11443
11444         trap cleanup_130 EXIT RETURN
11445
11446         local fm_file=$DIR/$tfile
11447         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
11448                         error "setstripe on $fm_file"
11449         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11450                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11451
11452         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
11453                 error "dd failed on $fm_file"
11454
11455         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11456         filefrag_op=$(filefrag -ve -k $fm_file |
11457                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11458
11459         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11460                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11461
11462         IFS=$'\n'
11463         tot_len=0
11464         num_luns=1
11465         for line in $filefrag_op
11466         do
11467                 frag_lun=$(echo $line | cut -d: -f5 |
11468                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11469                 ext_len=$(echo $line | cut -d: -f4)
11470                 if (( $frag_lun != $last_lun )); then
11471                         if (( tot_len != 1024 )); then
11472                                 cleanup_130
11473                                 error "FIEMAP on $fm_file failed; returned " \
11474                                 "len $tot_len for OST $last_lun instead of 1024"
11475                                 return
11476                         else
11477                                 (( num_luns += 1 ))
11478                                 tot_len=0
11479                         fi
11480                 fi
11481                 (( tot_len += ext_len ))
11482                 last_lun=$frag_lun
11483         done
11484         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
11485                 cleanup_130
11486                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11487                         "luns or wrong len for OST $last_lun"
11488                 return
11489         fi
11490
11491         cleanup_130
11492
11493         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
11494 }
11495 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
11496
11497 test_130c() {
11498         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11499
11500         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11501         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11502
11503         trap cleanup_130 EXIT RETURN
11504
11505         local fm_file=$DIR/$tfile
11506         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
11507         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11508                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11509
11510         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
11511                         error "dd failed on $fm_file"
11512
11513         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11514         filefrag_op=$(filefrag -ve -k $fm_file |
11515                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11516
11517         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11518                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11519
11520         IFS=$'\n'
11521         tot_len=0
11522         num_luns=1
11523         for line in $filefrag_op
11524         do
11525                 frag_lun=$(echo $line | cut -d: -f5 |
11526                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11527                 ext_len=$(echo $line | cut -d: -f4)
11528                 if (( $frag_lun != $last_lun )); then
11529                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
11530                         if (( logical != 512 )); then
11531                                 cleanup_130
11532                                 error "FIEMAP on $fm_file failed; returned " \
11533                                 "logical start for lun $logical instead of 512"
11534                                 return
11535                         fi
11536                         if (( tot_len != 512 )); then
11537                                 cleanup_130
11538                                 error "FIEMAP on $fm_file failed; returned " \
11539                                 "len $tot_len for OST $last_lun instead of 1024"
11540                                 return
11541                         else
11542                                 (( num_luns += 1 ))
11543                                 tot_len=0
11544                         fi
11545                 fi
11546                 (( tot_len += ext_len ))
11547                 last_lun=$frag_lun
11548         done
11549         if (( num_luns != 2 || tot_len != 512 )); then
11550                 cleanup_130
11551                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11552                         "luns or wrong len for OST $last_lun"
11553                 return
11554         fi
11555
11556         cleanup_130
11557
11558         echo "FIEMAP on 2-stripe file with hole succeeded"
11559 }
11560 run_test 130c "FIEMAP (2-stripe file with hole)"
11561
11562 test_130d() {
11563         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
11564
11565         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11566         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11567
11568         trap cleanup_130 EXIT RETURN
11569
11570         local fm_file=$DIR/$tfile
11571         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
11572                         error "setstripe on $fm_file"
11573         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11574                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11575
11576         local actual_stripe_count=$($GETSTRIPE -c $fm_file)
11577         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
11578                 error "dd failed on $fm_file"
11579
11580         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11581         filefrag_op=$(filefrag -ve -k $fm_file |
11582                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11583
11584         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11585                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11586
11587         IFS=$'\n'
11588         tot_len=0
11589         num_luns=1
11590         for line in $filefrag_op
11591         do
11592                 frag_lun=$(echo $line | cut -d: -f5 |
11593                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11594                 ext_len=$(echo $line | cut -d: -f4)
11595                 if (( $frag_lun != $last_lun )); then
11596                         if (( tot_len != 1024 )); then
11597                                 cleanup_130
11598                                 error "FIEMAP on $fm_file failed; returned " \
11599                                 "len $tot_len for OST $last_lun instead of 1024"
11600                                 return
11601                         else
11602                                 (( num_luns += 1 ))
11603                                 tot_len=0
11604                         fi
11605                 fi
11606                 (( tot_len += ext_len ))
11607                 last_lun=$frag_lun
11608         done
11609         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
11610                 cleanup_130
11611                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11612                         "luns or wrong len for OST $last_lun"
11613                 return
11614         fi
11615
11616         cleanup_130
11617
11618         echo "FIEMAP on N-stripe file succeeded"
11619 }
11620 run_test 130d "FIEMAP (N-stripe file)"
11621
11622 test_130e() {
11623         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11624
11625         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11626         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11627
11628         trap cleanup_130 EXIT RETURN
11629
11630         local fm_file=$DIR/$tfile
11631         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
11632         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11633                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11634
11635         NUM_BLKS=512
11636         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
11637         for ((i = 0; i < $NUM_BLKS; i++))
11638         do
11639                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
11640         done
11641
11642         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11643         filefrag_op=$(filefrag -ve -k $fm_file |
11644                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11645
11646         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11647                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11648
11649         IFS=$'\n'
11650         tot_len=0
11651         num_luns=1
11652         for line in $filefrag_op
11653         do
11654                 frag_lun=$(echo $line | cut -d: -f5 |
11655                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11656                 ext_len=$(echo $line | cut -d: -f4)
11657                 if (( $frag_lun != $last_lun )); then
11658                         if (( tot_len != $EXPECTED_LEN )); then
11659                                 cleanup_130
11660                                 error "FIEMAP on $fm_file failed; returned " \
11661                                 "len $tot_len for OST $last_lun instead " \
11662                                 "of $EXPECTED_LEN"
11663                                 return
11664                         else
11665                                 (( num_luns += 1 ))
11666                                 tot_len=0
11667                         fi
11668                 fi
11669                 (( tot_len += ext_len ))
11670                 last_lun=$frag_lun
11671         done
11672         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
11673                 cleanup_130
11674                 error "FIEMAP on $fm_file failed; returned wrong number " \
11675                         "of luns or wrong len for OST $last_lun"
11676                 return
11677         fi
11678
11679         cleanup_130
11680
11681         echo "FIEMAP with continuation calls succeeded"
11682 }
11683 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
11684
11685 test_130f() {
11686         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11687         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11688
11689         local fm_file=$DIR/$tfile
11690         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
11691                 error "multiop create with lov_delay_create on $fm_file"
11692
11693         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11694         filefrag_extents=$(filefrag -vek $fm_file |
11695                            awk '/extents? found/ { print $2 }')
11696         if [[ "$filefrag_extents" != "0" ]]; then
11697                 error "FIEMAP on $fm_file failed; " \
11698                       "returned $filefrag_extents expected 0"
11699         fi
11700
11701         rm -f $fm_file
11702 }
11703 run_test 130f "FIEMAP (unstriped file)"
11704
11705 # Test for writev/readv
11706 test_131a() {
11707         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
11708                 error "writev test failed"
11709         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
11710                 error "readv failed"
11711         rm -f $DIR/$tfile
11712 }
11713 run_test 131a "test iov's crossing stripe boundary for writev/readv"
11714
11715 test_131b() {
11716         local fsize=$((524288 + 1048576 + 1572864))
11717         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
11718                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
11719                         error "append writev test failed"
11720
11721         ((fsize += 1572864 + 1048576))
11722         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
11723                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
11724                         error "append writev test failed"
11725         rm -f $DIR/$tfile
11726 }
11727 run_test 131b "test append writev"
11728
11729 test_131c() {
11730         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
11731         error "NOT PASS"
11732 }
11733 run_test 131c "test read/write on file w/o objects"
11734
11735 test_131d() {
11736         rwv -f $DIR/$tfile -w -n 1 1572864
11737         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
11738         if [ "$NOB" != 1572864 ]; then
11739                 error "Short read filed: read $NOB bytes instead of 1572864"
11740         fi
11741         rm -f $DIR/$tfile
11742 }
11743 run_test 131d "test short read"
11744
11745 test_131e() {
11746         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
11747         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
11748         error "read hitting hole failed"
11749         rm -f $DIR/$tfile
11750 }
11751 run_test 131e "test read hitting hole"
11752
11753 check_stats() {
11754         local facet=$1
11755         local op=$2
11756         local want=${3:-0}
11757         local res
11758
11759         case $facet in
11760         mds*) res=$(do_facet $facet \
11761                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
11762                  ;;
11763         ost*) res=$(do_facet $facet \
11764                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
11765                  ;;
11766         *) error "Wrong facet '$facet'" ;;
11767         esac
11768         [ "$res" ] || error "The counter for $op on $facet was not incremented"
11769         # if the argument $3 is zero, it means any stat increment is ok.
11770         if [[ $want -gt 0 ]]; then
11771                 local count=$(echo $res | awk '{ print $2 }')
11772                 [[ $count -ne $want ]] &&
11773                         error "The $op counter on $facet is $count, not $want"
11774         fi
11775 }
11776
11777 test_133a() {
11778         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11779         remote_ost_nodsh && skip "remote OST with nodsh"
11780         remote_mds_nodsh && skip "remote MDS with nodsh"
11781         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
11782                 skip_env "MDS doesn't support rename stats"
11783
11784         local testdir=$DIR/${tdir}/stats_testdir
11785
11786         mkdir -p $DIR/${tdir}
11787
11788         # clear stats.
11789         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11790         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11791
11792         # verify mdt stats first.
11793         mkdir ${testdir} || error "mkdir failed"
11794         check_stats $SINGLEMDS "mkdir" 1
11795         touch ${testdir}/${tfile} || error "touch failed"
11796         check_stats $SINGLEMDS "open" 1
11797         check_stats $SINGLEMDS "close" 1
11798         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
11799                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
11800                 check_stats $SINGLEMDS "mknod" 2
11801         }
11802         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
11803         check_stats $SINGLEMDS "unlink" 1
11804         rm -f ${testdir}/${tfile} || error "file remove failed"
11805         check_stats $SINGLEMDS "unlink" 2
11806
11807         # remove working dir and check mdt stats again.
11808         rmdir ${testdir} || error "rmdir failed"
11809         check_stats $SINGLEMDS "rmdir" 1
11810
11811         local testdir1=$DIR/${tdir}/stats_testdir1
11812         mkdir -p ${testdir}
11813         mkdir -p ${testdir1}
11814         touch ${testdir1}/test1
11815         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
11816         check_stats $SINGLEMDS "crossdir_rename" 1
11817
11818         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
11819         check_stats $SINGLEMDS "samedir_rename" 1
11820
11821         rm -rf $DIR/${tdir}
11822 }
11823 run_test 133a "Verifying MDT stats ========================================"
11824
11825 test_133b() {
11826         local res
11827
11828         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11829         remote_ost_nodsh && skip "remote OST with nodsh"
11830         remote_mds_nodsh && skip "remote MDS with nodsh"
11831
11832         local testdir=$DIR/${tdir}/stats_testdir
11833
11834         mkdir -p ${testdir} || error "mkdir failed"
11835         touch ${testdir}/${tfile} || error "touch failed"
11836         cancel_lru_locks mdc
11837
11838         # clear stats.
11839         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11840         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11841
11842         # extra mdt stats verification.
11843         chmod 444 ${testdir}/${tfile} || error "chmod failed"
11844         check_stats $SINGLEMDS "setattr" 1
11845         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11846         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
11847         then            # LU-1740
11848                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
11849                 check_stats $SINGLEMDS "getattr" 1
11850         fi
11851         rm -rf $DIR/${tdir}
11852
11853         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
11854         # so the check below is not reliable
11855         [ $MDSCOUNT -eq 1 ] || return 0
11856
11857         # Sleep to avoid a cached response.
11858         #define OBD_STATFS_CACHE_SECONDS 1
11859         sleep 2
11860         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11861         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
11862         $LFS df || error "lfs failed"
11863         check_stats $SINGLEMDS "statfs" 1
11864
11865         # check aggregated statfs (LU-10018)
11866         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
11867                 return 0
11868         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
11869                 return 0
11870         sleep 2
11871         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11872         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
11873         df $DIR
11874         check_stats $SINGLEMDS "statfs" 1
11875
11876         # We want to check that the client didn't send OST_STATFS to
11877         # ost1 but the MDT also uses OST_STATFS for precreate. So some
11878         # extra care is needed here.
11879         if remote_mds; then
11880                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
11881                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
11882
11883                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
11884                 [ "$res" ] && error "OST got STATFS"
11885         fi
11886
11887         return 0
11888 }
11889 run_test 133b "Verifying extra MDT stats =================================="
11890
11891 test_133c() {
11892         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11893         remote_ost_nodsh && skip "remote OST with nodsh"
11894         remote_mds_nodsh && skip "remote MDS with nodsh"
11895
11896         local testdir=$DIR/$tdir/stats_testdir
11897
11898         test_mkdir -p $testdir
11899
11900         # verify obdfilter stats.
11901         $SETSTRIPE -c 1 -i 0 $testdir/$tfile
11902         sync
11903         cancel_lru_locks osc
11904         wait_delete_completed
11905
11906         # clear stats.
11907         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11908         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11909
11910         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
11911                 error "dd failed"
11912         sync
11913         cancel_lru_locks osc
11914         check_stats ost1 "write" 1
11915
11916         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
11917         check_stats ost1 "read" 1
11918
11919         > $testdir/$tfile || error "truncate failed"
11920         check_stats ost1 "punch" 1
11921
11922         rm -f $testdir/$tfile || error "file remove failed"
11923         wait_delete_completed
11924         check_stats ost1 "destroy" 1
11925
11926         rm -rf $DIR/$tdir
11927 }
11928 run_test 133c "Verifying OST stats ========================================"
11929
11930 order_2() {
11931         local value=$1
11932         local orig=$value
11933         local order=1
11934
11935         while [ $value -ge 2 ]; do
11936                 order=$((order*2))
11937                 value=$((value/2))
11938         done
11939
11940         if [ $orig -gt $order ]; then
11941                 order=$((order*2))
11942         fi
11943         echo $order
11944 }
11945
11946 size_in_KMGT() {
11947     local value=$1
11948     local size=('K' 'M' 'G' 'T');
11949     local i=0
11950     local size_string=$value
11951
11952     while [ $value -ge 1024 ]; do
11953         if [ $i -gt 3 ]; then
11954             #T is the biggest unit we get here, if that is bigger,
11955             #just return XXXT
11956             size_string=${value}T
11957             break
11958         fi
11959         value=$((value >> 10))
11960         if [ $value -lt 1024 ]; then
11961             size_string=${value}${size[$i]}
11962             break
11963         fi
11964         i=$((i + 1))
11965     done
11966
11967     echo $size_string
11968 }
11969
11970 get_rename_size() {
11971         local size=$1
11972         local context=${2:-.}
11973         local sample=$(do_facet $SINGLEMDS $LCTL \
11974                 get_param mdt.$FSNAME-MDT0000.rename_stats |
11975                 grep -A1 $context |
11976                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
11977         echo $sample
11978 }
11979
11980 test_133d() {
11981         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11982         remote_ost_nodsh && skip "remote OST with nodsh"
11983         remote_mds_nodsh && skip "remote MDS with nodsh"
11984         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
11985                 skip_env "MDS doesn't support rename stats"
11986
11987         local testdir1=$DIR/${tdir}/stats_testdir1
11988         local testdir2=$DIR/${tdir}/stats_testdir2
11989         mkdir -p $DIR/${tdir}
11990
11991         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
11992
11993         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
11994         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
11995
11996         createmany -o $testdir1/test 512 || error "createmany failed"
11997
11998         # check samedir rename size
11999         mv ${testdir1}/test0 ${testdir1}/test_0
12000
12001         local testdir1_size=$(ls -l $DIR/${tdir} |
12002                 awk '/stats_testdir1/ {print $5}')
12003         local testdir2_size=$(ls -l $DIR/${tdir} |
12004                 awk '/stats_testdir2/ {print $5}')
12005
12006         testdir1_size=$(order_2 $testdir1_size)
12007         testdir2_size=$(order_2 $testdir2_size)
12008
12009         testdir1_size=$(size_in_KMGT $testdir1_size)
12010         testdir2_size=$(size_in_KMGT $testdir2_size)
12011
12012         echo "source rename dir size: ${testdir1_size}"
12013         echo "target rename dir size: ${testdir2_size}"
12014
12015         local cmd="do_facet $SINGLEMDS $LCTL "
12016         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
12017
12018         eval $cmd || error "$cmd failed"
12019         local samedir=$($cmd | grep 'same_dir')
12020         local same_sample=$(get_rename_size $testdir1_size)
12021         [ -z "$samedir" ] && error "samedir_rename_size count error"
12022         [[ $same_sample -eq 1 ]] ||
12023                 error "samedir_rename_size error $same_sample"
12024         echo "Check same dir rename stats success"
12025
12026         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12027
12028         # check crossdir rename size
12029         mv ${testdir1}/test_0 ${testdir2}/test_0
12030
12031         testdir1_size=$(ls -l $DIR/${tdir} |
12032                 awk '/stats_testdir1/ {print $5}')
12033         testdir2_size=$(ls -l $DIR/${tdir} |
12034                 awk '/stats_testdir2/ {print $5}')
12035
12036         testdir1_size=$(order_2 $testdir1_size)
12037         testdir2_size=$(order_2 $testdir2_size)
12038
12039         testdir1_size=$(size_in_KMGT $testdir1_size)
12040         testdir2_size=$(size_in_KMGT $testdir2_size)
12041
12042         echo "source rename dir size: ${testdir1_size}"
12043         echo "target rename dir size: ${testdir2_size}"
12044
12045         eval $cmd || error "$cmd failed"
12046         local crossdir=$($cmd | grep 'crossdir')
12047         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
12048         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
12049         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
12050         [[ $src_sample -eq 1 ]] ||
12051                 error "crossdir_rename_size error $src_sample"
12052         [[ $tgt_sample -eq 1 ]] ||
12053                 error "crossdir_rename_size error $tgt_sample"
12054         echo "Check cross dir rename stats success"
12055         rm -rf $DIR/${tdir}
12056 }
12057 run_test 133d "Verifying rename_stats ========================================"
12058
12059 test_133e() {
12060         remote_mds_nodsh && skip "remote MDS with nodsh"
12061         remote_ost_nodsh && skip "remote OST with nodsh"
12062         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12063
12064         local testdir=$DIR/${tdir}/stats_testdir
12065         local ctr f0 f1 bs=32768 count=42 sum
12066
12067         mkdir -p ${testdir} || error "mkdir failed"
12068
12069         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
12070
12071         for ctr in {write,read}_bytes; do
12072                 sync
12073                 cancel_lru_locks osc
12074
12075                 do_facet ost1 $LCTL set_param -n \
12076                         "obdfilter.*.exports.clear=clear"
12077
12078                 if [ $ctr = write_bytes ]; then
12079                         f0=/dev/zero
12080                         f1=${testdir}/${tfile}
12081                 else
12082                         f0=${testdir}/${tfile}
12083                         f1=/dev/null
12084                 fi
12085
12086                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
12087                         error "dd failed"
12088                 sync
12089                 cancel_lru_locks osc
12090
12091                 sum=$(do_facet ost1 $LCTL get_param \
12092                         "obdfilter.*.exports.*.stats" |
12093                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
12094                                 $1 == ctr { sum += $7 }
12095                                 END { printf("%0.0f", sum) }')
12096
12097                 if ((sum != bs * count)); then
12098                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
12099                 fi
12100         done
12101
12102         rm -rf $DIR/${tdir}
12103 }
12104 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
12105
12106 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
12107
12108 # Some versions of find (4.5.11, 4.5.14) included in CentOS 7.3-7.5 do
12109 # not honor the -ignore_readdir_race option correctly. So we call
12110 # error_ignore() rather than error() in these cases. See LU-11152.
12111 error_133() {
12112         if (find --version; do_facet mds1 find --version) |
12113                 grep -q '\b4\.5\.1[1-4]\b'; then
12114                 error_ignore LU-11152 "$@"
12115         else
12116                 error "$@"
12117         fi
12118 }
12119
12120 test_133f() {
12121         # First without trusting modes.
12122         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
12123         echo "proc_dirs='$proc_dirs'"
12124         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
12125         find $proc_dirs -exec cat '{}' \; &> /dev/null
12126
12127         # Second verifying readability.
12128         $LCTL get_param -R '*' &> /dev/null
12129
12130         # Verifing writability with badarea_io.
12131         find $proc_dirs \
12132                 -ignore_readdir_race \
12133                 -type f \
12134                 -not -name force_lbug \
12135                 -not -name changelog_mask \
12136                 -exec badarea_io '{}' \; ||
12137                         error_133 "find $proc_dirs failed"
12138 }
12139 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
12140
12141 test_133g() {
12142         remote_mds_nodsh && skip "remote MDS with nodsh"
12143         remote_ost_nodsh && skip "remote OST with nodsh"
12144
12145         # eventually, this can also be replaced with "lctl get_param -R",
12146         # but not until that option is always available on the server
12147         local facet
12148         for facet in mds1 ost1; do
12149                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
12150                         skip_noexit "Too old lustre on $facet"
12151                 local facet_proc_dirs=$(do_facet $facet \
12152                                         \\\ls -d $proc_regexp 2>/dev/null)
12153                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12154                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12155                 do_facet $facet find $facet_proc_dirs \
12156                         ! -name req_history \
12157                         -exec cat '{}' \\\; &> /dev/null
12158
12159                 do_facet $facet find $facet_proc_dirs \
12160                         ! -name req_history \
12161                         -type f \
12162                         -exec cat '{}' \\\; &> /dev/null ||
12163                                 error "proc file read failed"
12164
12165                 do_facet $facet find $facet_proc_dirs \
12166                         -ignore_readdir_race \
12167                         -type f \
12168                         -not -name force_lbug \
12169                         -not -name changelog_mask \
12170                         -exec badarea_io '{}' \\\; ||
12171                                 error_133 "$facet find $facet_proc_dirs failed"
12172         done
12173
12174         # remount the FS in case writes/reads /proc break the FS
12175         cleanup || error "failed to unmount"
12176         setup || error "failed to setup"
12177         true
12178 }
12179 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
12180
12181 test_133h() {
12182         remote_mds_nodsh && skip "remote MDS with nodsh"
12183         remote_ost_nodsh && skip "remote OST with nodsh"
12184         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
12185                 skip "Need MDS version at least 2.9.54"
12186
12187         local facet
12188
12189         for facet in client mds1 ost1; do
12190                 local facet_proc_dirs=$(do_facet $facet \
12191                                         \\\ls -d $proc_regexp 2> /dev/null)
12192                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12193                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12194                 # Get the list of files that are missing the terminating newline
12195                 local missing=($(do_facet $facet \
12196                         find ${facet_proc_dirs} -type f \|              \
12197                                 while read F\; do                       \
12198                                         awk -v FS='\v' -v RS='\v\v'     \
12199                                         "'END { if(NR>0 &&              \
12200                                         \\\$NF !~ /.*\\\n\$/)           \
12201                                                 print FILENAME}'"       \
12202                                         '\$F'\;                         \
12203                                 done 2>/dev/null))
12204                 [ ${#missing[*]} -eq 0 ] ||
12205                         error "files do not end with newline: ${missing[*]}"
12206         done
12207 }
12208 run_test 133h "Proc files should end with newlines"
12209
12210 test_134a() {
12211         remote_mds_nodsh && skip "remote MDS with nodsh"
12212         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12213                 skip "Need MDS version at least 2.7.54"
12214
12215         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12216         cancel_lru_locks mdc
12217
12218         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
12219         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12220         [ $unused -eq 0 ] || error "$unused locks are not cleared"
12221
12222         local nr=1000
12223         createmany -o $DIR/$tdir/f $nr ||
12224                 error "failed to create $nr files in $DIR/$tdir"
12225         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12226
12227         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
12228         do_facet mds1 $LCTL set_param fail_loc=0x327
12229         do_facet mds1 $LCTL set_param fail_val=500
12230         touch $DIR/$tdir/m
12231
12232         echo "sleep 10 seconds ..."
12233         sleep 10
12234         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
12235
12236         do_facet mds1 $LCTL set_param fail_loc=0
12237         do_facet mds1 $LCTL set_param fail_val=0
12238         [ $lck_cnt -lt $unused ] ||
12239                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
12240
12241         rm $DIR/$tdir/m
12242         unlinkmany $DIR/$tdir/f $nr
12243 }
12244 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
12245
12246 test_134b() {
12247         remote_mds_nodsh && skip "remote MDS with nodsh"
12248         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12249                 skip "Need MDS version at least 2.7.54"
12250
12251         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12252         cancel_lru_locks mdc
12253
12254         local low_wm=$(do_facet mds1 $LCTL get_param -n \
12255                         ldlm.lock_reclaim_threshold_mb)
12256         # disable reclaim temporarily
12257         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
12258
12259         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
12260         do_facet mds1 $LCTL set_param fail_loc=0x328
12261         do_facet mds1 $LCTL set_param fail_val=500
12262
12263         $LCTL set_param debug=+trace
12264
12265         local nr=600
12266         createmany -o $DIR/$tdir/f $nr &
12267         local create_pid=$!
12268
12269         echo "Sleep $TIMEOUT seconds ..."
12270         sleep $TIMEOUT
12271         if ! ps -p $create_pid  > /dev/null 2>&1; then
12272                 do_facet mds1 $LCTL set_param fail_loc=0
12273                 do_facet mds1 $LCTL set_param fail_val=0
12274                 do_facet mds1 $LCTL set_param \
12275                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
12276                 error "createmany finished incorrectly!"
12277         fi
12278         do_facet mds1 $LCTL set_param fail_loc=0
12279         do_facet mds1 $LCTL set_param fail_val=0
12280         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
12281         wait $create_pid || return 1
12282
12283         unlinkmany $DIR/$tdir/f $nr
12284 }
12285 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
12286
12287 test_140() { #bug-17379
12288         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12289
12290         test_mkdir $DIR/$tdir
12291         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
12292         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
12293
12294         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
12295         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
12296         local i=0
12297         while i=$((i + 1)); do
12298                 test_mkdir $i
12299                 cd $i || error "Changing to $i"
12300                 ln -s ../stat stat || error "Creating stat symlink"
12301                 # Read the symlink until ELOOP present,
12302                 # not LBUGing the system is considered success,
12303                 # we didn't overrun the stack.
12304                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
12305                 if [ $ret -ne 0 ]; then
12306                         if [ $ret -eq 40 ]; then
12307                                 break  # -ELOOP
12308                         else
12309                                 error "Open stat symlink"
12310                                         return
12311                         fi
12312                 fi
12313         done
12314         i=$((i - 1))
12315         echo "The symlink depth = $i"
12316         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
12317                                         error "Invalid symlink depth"
12318
12319         # Test recursive symlink
12320         ln -s symlink_self symlink_self
12321         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
12322         echo "open symlink_self returns $ret"
12323         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
12324 }
12325 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
12326
12327 test_150() {
12328         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12329
12330         local TF="$TMP/$tfile"
12331
12332         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12333         cp $TF $DIR/$tfile
12334         cancel_lru_locks $OSC
12335         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
12336         remount_client $MOUNT
12337         df -P $MOUNT
12338         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
12339
12340         $TRUNCATE $TF 6000
12341         $TRUNCATE $DIR/$tfile 6000
12342         cancel_lru_locks $OSC
12343         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
12344
12345         echo "12345" >>$TF
12346         echo "12345" >>$DIR/$tfile
12347         cancel_lru_locks $OSC
12348         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
12349
12350         echo "12345" >>$TF
12351         echo "12345" >>$DIR/$tfile
12352         cancel_lru_locks $OSC
12353         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
12354
12355         rm -f $TF
12356         true
12357 }
12358 run_test 150 "truncate/append tests"
12359
12360 #LU-2902 roc_hit was not able to read all values from lproc
12361 function roc_hit_init() {
12362         local list=$(comma_list $(osts_nodes))
12363         local dir=$DIR/$tdir-check
12364         local file=$dir/$tfile
12365         local BEFORE
12366         local AFTER
12367         local idx
12368
12369         test_mkdir $dir
12370         #use setstripe to do a write to every ost
12371         for i in $(seq 0 $((OSTCOUNT-1))); do
12372                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
12373                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
12374                 idx=$(printf %04x $i)
12375                 BEFORE=$(get_osd_param $list *OST*$idx stats |
12376                         awk '$1 == "cache_access" {sum += $7}
12377                                 END { printf("%0.0f", sum) }')
12378
12379                 cancel_lru_locks osc
12380                 cat $file >/dev/null
12381
12382                 AFTER=$(get_osd_param $list *OST*$idx stats |
12383                         awk '$1 == "cache_access" {sum += $7}
12384                                 END { printf("%0.0f", sum) }')
12385
12386                 echo BEFORE:$BEFORE AFTER:$AFTER
12387                 if ! let "AFTER - BEFORE == 4"; then
12388                         rm -rf $dir
12389                         error "roc_hit is not safe to use"
12390                 fi
12391                 rm $file
12392         done
12393
12394         rm -rf $dir
12395 }
12396
12397 function roc_hit() {
12398         local list=$(comma_list $(osts_nodes))
12399         echo $(get_osd_param $list '' stats |
12400                 awk '$1 == "cache_hit" {sum += $7}
12401                         END { printf("%0.0f", sum) }')
12402 }
12403
12404 function set_cache() {
12405         local on=1
12406
12407         if [ "$2" == "off" ]; then
12408                 on=0;
12409         fi
12410         local list=$(comma_list $(osts_nodes))
12411         set_osd_param $list '' $1_cache_enable $on
12412
12413         cancel_lru_locks osc
12414 }
12415
12416 test_151() {
12417         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12418         remote_ost_nodsh && skip "remote OST with nodsh"
12419
12420         local CPAGES=3
12421         local list=$(comma_list $(osts_nodes))
12422
12423         # check whether obdfilter is cache capable at all
12424         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
12425                 skip "not cache-capable obdfilter"
12426         fi
12427
12428         # check cache is enabled on all obdfilters
12429         if get_osd_param $list '' read_cache_enable | grep 0; then
12430                 skip "oss cache is disabled"
12431         fi
12432
12433         set_osd_param $list '' writethrough_cache_enable 1
12434
12435         # check write cache is enabled on all obdfilters
12436         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
12437                 skip "oss write cache is NOT enabled"
12438         fi
12439
12440         roc_hit_init
12441
12442         #define OBD_FAIL_OBD_NO_LRU  0x609
12443         do_nodes $list $LCTL set_param fail_loc=0x609
12444
12445         # pages should be in the case right after write
12446         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
12447                 error "dd failed"
12448
12449         local BEFORE=$(roc_hit)
12450         cancel_lru_locks osc
12451         cat $DIR/$tfile >/dev/null
12452         local AFTER=$(roc_hit)
12453
12454         do_nodes $list $LCTL set_param fail_loc=0
12455
12456         if ! let "AFTER - BEFORE == CPAGES"; then
12457                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12458         fi
12459
12460         # the following read invalidates the cache
12461         cancel_lru_locks osc
12462         set_osd_param $list '' read_cache_enable 0
12463         cat $DIR/$tfile >/dev/null
12464
12465         # now data shouldn't be found in the cache
12466         BEFORE=$(roc_hit)
12467         cancel_lru_locks osc
12468         cat $DIR/$tfile >/dev/null
12469         AFTER=$(roc_hit)
12470         if let "AFTER - BEFORE != 0"; then
12471                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12472         fi
12473
12474         set_osd_param $list '' read_cache_enable 1
12475         rm -f $DIR/$tfile
12476 }
12477 run_test 151 "test cache on oss and controls ==============================="
12478
12479 test_152() {
12480         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12481
12482         local TF="$TMP/$tfile"
12483
12484         # simulate ENOMEM during write
12485 #define OBD_FAIL_OST_NOMEM      0x226
12486         lctl set_param fail_loc=0x80000226
12487         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12488         cp $TF $DIR/$tfile
12489         sync || error "sync failed"
12490         lctl set_param fail_loc=0
12491
12492         # discard client's cache
12493         cancel_lru_locks osc
12494
12495         # simulate ENOMEM during read
12496         lctl set_param fail_loc=0x80000226
12497         cmp $TF $DIR/$tfile || error "cmp failed"
12498         lctl set_param fail_loc=0
12499
12500         rm -f $TF
12501 }
12502 run_test 152 "test read/write with enomem ============================"
12503
12504 test_153() {
12505         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
12506 }
12507 run_test 153 "test if fdatasync does not crash ======================="
12508
12509 dot_lustre_fid_permission_check() {
12510         local fid=$1
12511         local ffid=$MOUNT/.lustre/fid/$fid
12512         local test_dir=$2
12513
12514         echo "stat fid $fid"
12515         stat $ffid > /dev/null || error "stat $ffid failed."
12516         echo "touch fid $fid"
12517         touch $ffid || error "touch $ffid failed."
12518         echo "write to fid $fid"
12519         cat /etc/hosts > $ffid || error "write $ffid failed."
12520         echo "read fid $fid"
12521         diff /etc/hosts $ffid || error "read $ffid failed."
12522         echo "append write to fid $fid"
12523         cat /etc/hosts >> $ffid || error "append write $ffid failed."
12524         echo "rename fid $fid"
12525         mv $ffid $test_dir/$tfile.1 &&
12526                 error "rename $ffid to $tfile.1 should fail."
12527         touch $test_dir/$tfile.1
12528         mv $test_dir/$tfile.1 $ffid &&
12529                 error "rename $tfile.1 to $ffid should fail."
12530         rm -f $test_dir/$tfile.1
12531         echo "truncate fid $fid"
12532         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
12533         echo "link fid $fid"
12534         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
12535         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
12536                 echo "setfacl fid $fid"
12537                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
12538                 echo "getfacl fid $fid"
12539                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
12540         fi
12541         echo "unlink fid $fid"
12542         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
12543         echo "mknod fid $fid"
12544         mknod $ffid c 1 3 && error "mknod $ffid should fail."
12545
12546         fid=[0xf00000400:0x1:0x0]
12547         ffid=$MOUNT/.lustre/fid/$fid
12548
12549         echo "stat non-exist fid $fid"
12550         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
12551         echo "write to non-exist fid $fid"
12552         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
12553         echo "link new fid $fid"
12554         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
12555
12556         mkdir -p $test_dir/$tdir
12557         touch $test_dir/$tdir/$tfile
12558         fid=$($LFS path2fid $test_dir/$tdir)
12559         rc=$?
12560         [ $rc -ne 0 ] &&
12561                 error "error: could not get fid for $test_dir/$dir/$tfile."
12562
12563         ffid=$MOUNT/.lustre/fid/$fid
12564
12565         echo "ls $fid"
12566         ls $ffid > /dev/null || error "ls $ffid failed."
12567         echo "touch $fid/$tfile.1"
12568         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
12569
12570         echo "touch $MOUNT/.lustre/fid/$tfile"
12571         touch $MOUNT/.lustre/fid/$tfile && \
12572                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
12573
12574         echo "setxattr to $MOUNT/.lustre/fid"
12575         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
12576
12577         echo "listxattr for $MOUNT/.lustre/fid"
12578         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
12579
12580         echo "delxattr from $MOUNT/.lustre/fid"
12581         setfattr -x trusted.name1 $MOUNT/.lustre/fid
12582
12583         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
12584         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
12585                 error "touch invalid fid should fail."
12586
12587         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
12588         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
12589                 error "touch non-normal fid should fail."
12590
12591         echo "rename $tdir to $MOUNT/.lustre/fid"
12592         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
12593                 error "rename to $MOUNT/.lustre/fid should fail."
12594
12595         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
12596         then            # LU-3547
12597                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
12598                 local new_obf_mode=777
12599
12600                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
12601                 chmod $new_obf_mode $DIR/.lustre/fid ||
12602                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
12603
12604                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
12605                 [ $obf_mode -eq $new_obf_mode ] ||
12606                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
12607
12608                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
12609                 chmod $old_obf_mode $DIR/.lustre/fid ||
12610                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
12611         fi
12612
12613         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
12614         fid=$($LFS path2fid $test_dir/$tfile-2)
12615
12616         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
12617         then # LU-5424
12618                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
12619                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
12620                         error "create lov data thru .lustre failed"
12621         fi
12622         echo "cp /etc/passwd $test_dir/$tfile-2"
12623         cp /etc/passwd $test_dir/$tfile-2 ||
12624                 error "copy to $test_dir/$tfile-2 failed."
12625         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
12626         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
12627                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
12628
12629         rm -rf $test_dir/tfile.lnk
12630         rm -rf $test_dir/$tfile-2
12631 }
12632
12633 test_154A() {
12634         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12635                 skip "Need MDS version at least 2.4.1"
12636
12637         local tf=$DIR/$tfile
12638         touch $tf
12639
12640         local fid=$($LFS path2fid $tf)
12641         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
12642
12643         # check that we get the same pathname back
12644         local found=$($LFS fid2path $MOUNT "$fid")
12645         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
12646         [ "$found" == "$tf" ] ||
12647                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
12648 }
12649 run_test 154A "lfs path2fid and fid2path basic checks"
12650
12651 test_154B() {
12652         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12653                 skip "Need MDS version at least 2.4.1"
12654
12655         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
12656         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
12657         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
12658         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
12659
12660         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
12661         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
12662
12663         # check that we get the same pathname
12664         echo "PFID: $PFID, name: $name"
12665         local FOUND=$($LFS fid2path $MOUNT "$PFID")
12666         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
12667         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
12668                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
12669
12670         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
12671 }
12672 run_test 154B "verify the ll_decode_linkea tool"
12673
12674 test_154a() {
12675         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12676         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12677         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
12678                 skip "Need MDS version at least 2.2.51"
12679         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
12680
12681         cp /etc/hosts $DIR/$tfile
12682
12683         fid=$($LFS path2fid $DIR/$tfile)
12684         rc=$?
12685         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
12686
12687         dot_lustre_fid_permission_check "$fid" $DIR ||
12688                 error "dot lustre permission check $fid failed"
12689
12690         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
12691
12692         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
12693
12694         touch $MOUNT/.lustre/file &&
12695                 error "creation is not allowed under .lustre"
12696
12697         mkdir $MOUNT/.lustre/dir &&
12698                 error "mkdir is not allowed under .lustre"
12699
12700         rm -rf $DIR/$tfile
12701 }
12702 run_test 154a "Open-by-FID"
12703
12704 test_154b() {
12705         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12706         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12707         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
12708         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
12709                 skip "Need MDS version at least 2.2.51"
12710
12711         local remote_dir=$DIR/$tdir/remote_dir
12712         local MDTIDX=1
12713         local rc=0
12714
12715         mkdir -p $DIR/$tdir
12716         $LFS mkdir -i $MDTIDX $remote_dir ||
12717                 error "create remote directory failed"
12718
12719         cp /etc/hosts $remote_dir/$tfile
12720
12721         fid=$($LFS path2fid $remote_dir/$tfile)
12722         rc=$?
12723         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
12724
12725         dot_lustre_fid_permission_check "$fid" $remote_dir ||
12726                 error "dot lustre permission check $fid failed"
12727         rm -rf $DIR/$tdir
12728 }
12729 run_test 154b "Open-by-FID for remote directory"
12730
12731 test_154c() {
12732         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12733                 skip "Need MDS version at least 2.4.1"
12734
12735         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
12736         local FID1=$($LFS path2fid $DIR/$tfile.1)
12737         local FID2=$($LFS path2fid $DIR/$tfile.2)
12738         local FID3=$($LFS path2fid $DIR/$tfile.3)
12739
12740         local N=1
12741         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
12742                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
12743                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
12744                 local want=FID$N
12745                 [ "$FID" = "${!want}" ] ||
12746                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
12747                 N=$((N + 1))
12748         done
12749
12750         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
12751         do
12752                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
12753                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
12754                 N=$((N + 1))
12755         done
12756 }
12757 run_test 154c "lfs path2fid and fid2path multiple arguments"
12758
12759 test_154d() {
12760         remote_mds_nodsh && skip "remote MDS with nodsh"
12761         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
12762                 skip "Need MDS version at least 2.5.53"
12763
12764         if remote_mds; then
12765                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
12766         else
12767                 nid="0@lo"
12768         fi
12769         local proc_ofile="mdt.*.exports.'$nid'.open_files"
12770         local fd
12771         local cmd
12772
12773         rm -f $DIR/$tfile
12774         touch $DIR/$tfile
12775
12776         local fid=$($LFS path2fid $DIR/$tfile)
12777         # Open the file
12778         fd=$(free_fd)
12779         cmd="exec $fd<$DIR/$tfile"
12780         eval $cmd
12781         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
12782         echo "$fid_list" | grep "$fid"
12783         rc=$?
12784
12785         cmd="exec $fd>/dev/null"
12786         eval $cmd
12787         if [ $rc -ne 0 ]; then
12788                 error "FID $fid not found in open files list $fid_list"
12789         fi
12790 }
12791 run_test 154d "Verify open file fid"
12792
12793 test_154e()
12794 {
12795         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
12796                 skip "Need MDS version at least 2.6.50"
12797
12798         if ls -a $MOUNT | grep -q '^\.lustre$'; then
12799                 error ".lustre returned by readdir"
12800         fi
12801 }
12802 run_test 154e ".lustre is not returned by readdir"
12803
12804 test_154f() {
12805         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12806
12807         # create parent directory on a single MDT to avoid cross-MDT hardlinks
12808         test_mkdir -p -c1 $DIR/$tdir/d
12809         # test dirs inherit from its stripe
12810         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
12811         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
12812         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
12813         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
12814         touch $DIR/f
12815
12816         # get fid of parents
12817         local FID0=$($LFS path2fid $DIR/$tdir/d)
12818         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
12819         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
12820         local FID3=$($LFS path2fid $DIR)
12821
12822         # check that path2fid --parents returns expected <parent_fid>/name
12823         # 1) test for a directory (single parent)
12824         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
12825         [ "$parent" == "$FID0/foo1" ] ||
12826                 error "expected parent: $FID0/foo1, got: $parent"
12827
12828         # 2) test for a file with nlink > 1 (multiple parents)
12829         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
12830         echo "$parent" | grep -F "$FID1/$tfile" ||
12831                 error "$FID1/$tfile not returned in parent list"
12832         echo "$parent" | grep -F "$FID2/link" ||
12833                 error "$FID2/link not returned in parent list"
12834
12835         # 3) get parent by fid
12836         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
12837         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12838         echo "$parent" | grep -F "$FID1/$tfile" ||
12839                 error "$FID1/$tfile not returned in parent list (by fid)"
12840         echo "$parent" | grep -F "$FID2/link" ||
12841                 error "$FID2/link not returned in parent list (by fid)"
12842
12843         # 4) test for entry in root directory
12844         parent=$($LFS path2fid --parents $DIR/f)
12845         echo "$parent" | grep -F "$FID3/f" ||
12846                 error "$FID3/f not returned in parent list"
12847
12848         # 5) test it on root directory
12849         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
12850                 error "$MOUNT should not have parents"
12851
12852         # enable xattr caching and check that linkea is correctly updated
12853         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
12854         save_lustre_params client "llite.*.xattr_cache" > $save
12855         lctl set_param llite.*.xattr_cache 1
12856
12857         # 6.1) linkea update on rename
12858         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
12859
12860         # get parents by fid
12861         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12862         # foo1 should no longer be returned in parent list
12863         echo "$parent" | grep -F "$FID1" &&
12864                 error "$FID1 should no longer be in parent list"
12865         # the new path should appear
12866         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
12867                 error "$FID2/$tfile.moved is not in parent list"
12868
12869         # 6.2) linkea update on unlink
12870         rm -f $DIR/$tdir/d/foo2/link
12871         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12872         # foo2/link should no longer be returned in parent list
12873         echo "$parent" | grep -F "$FID2/link" &&
12874                 error "$FID2/link should no longer be in parent list"
12875         true
12876
12877         rm -f $DIR/f
12878         restore_lustre_params < $save
12879         rm -f $save
12880 }
12881 run_test 154f "get parent fids by reading link ea"
12882
12883 test_154g()
12884 {
12885         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12886         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
12887            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
12888                 skip "Need MDS version at least 2.6.92"
12889
12890         mkdir -p $DIR/$tdir
12891         llapi_fid_test -d $DIR/$tdir
12892 }
12893 run_test 154g "various llapi FID tests"
12894
12895 test_155_small_load() {
12896     local temp=$TMP/$tfile
12897     local file=$DIR/$tfile
12898
12899     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
12900         error "dd of=$temp bs=6096 count=1 failed"
12901     cp $temp $file
12902     cancel_lru_locks $OSC
12903     cmp $temp $file || error "$temp $file differ"
12904
12905     $TRUNCATE $temp 6000
12906     $TRUNCATE $file 6000
12907     cmp $temp $file || error "$temp $file differ (truncate1)"
12908
12909     echo "12345" >>$temp
12910     echo "12345" >>$file
12911     cmp $temp $file || error "$temp $file differ (append1)"
12912
12913     echo "12345" >>$temp
12914     echo "12345" >>$file
12915     cmp $temp $file || error "$temp $file differ (append2)"
12916
12917     rm -f $temp $file
12918     true
12919 }
12920
12921 test_155_big_load() {
12922         remote_ost_nodsh && skip "remote OST with nodsh"
12923
12924         local temp=$TMP/$tfile
12925         local file=$DIR/$tfile
12926
12927         free_min_max
12928         local cache_size=$(do_facet ost$((MAXI+1)) \
12929                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
12930         local large_file_size=$((cache_size * 2))
12931
12932         echo "OSS cache size: $cache_size KB"
12933         echo "Large file size: $large_file_size KB"
12934
12935         [ $MAXV -le $large_file_size ] &&
12936                 skip_env "max available OST size needs > $large_file_size KB"
12937
12938         $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
12939
12940         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
12941                 error "dd of=$temp bs=$large_file_size count=1k failed"
12942         cp $temp $file
12943         ls -lh $temp $file
12944         cancel_lru_locks osc
12945         cmp $temp $file || error "$temp $file differ"
12946
12947         rm -f $temp $file
12948         true
12949 }
12950
12951 save_writethrough() {
12952         local facets=$(get_facets OST)
12953
12954         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
12955 }
12956
12957 test_155a() {
12958         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12959
12960         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12961
12962         save_writethrough $p
12963
12964         set_cache read on
12965         set_cache writethrough on
12966         test_155_small_load
12967         restore_lustre_params < $p
12968         rm -f $p
12969 }
12970 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
12971
12972 test_155b() {
12973         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12974
12975         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12976
12977         save_writethrough $p
12978
12979         set_cache read on
12980         set_cache writethrough off
12981         test_155_small_load
12982         restore_lustre_params < $p
12983         rm -f $p
12984 }
12985 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
12986
12987 test_155c() {
12988         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12989
12990         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12991
12992         save_writethrough $p
12993
12994         set_cache read off
12995         set_cache writethrough on
12996         test_155_small_load
12997         restore_lustre_params < $p
12998         rm -f $p
12999 }
13000 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
13001
13002 test_155d() {
13003         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13004
13005         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13006
13007         save_writethrough $p
13008
13009         set_cache read off
13010         set_cache writethrough off
13011         test_155_small_load
13012         restore_lustre_params < $p
13013         rm -f $p
13014 }
13015 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
13016
13017 test_155e() {
13018         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13019
13020         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13021
13022         save_writethrough $p
13023
13024         set_cache read on
13025         set_cache writethrough on
13026         test_155_big_load
13027         restore_lustre_params < $p
13028         rm -f $p
13029 }
13030 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
13031
13032 test_155f() {
13033         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13034
13035         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13036
13037         save_writethrough $p
13038
13039         set_cache read on
13040         set_cache writethrough off
13041         test_155_big_load
13042         restore_lustre_params < $p
13043         rm -f $p
13044 }
13045 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
13046
13047 test_155g() {
13048         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13049
13050         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13051
13052         save_writethrough $p
13053
13054         set_cache read off
13055         set_cache writethrough on
13056         test_155_big_load
13057         restore_lustre_params < $p
13058         rm -f $p
13059 }
13060 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
13061
13062 test_155h() {
13063         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13064
13065         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13066
13067         save_writethrough $p
13068
13069         set_cache read off
13070         set_cache writethrough off
13071         test_155_big_load
13072         restore_lustre_params < $p
13073         rm -f $p
13074 }
13075 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
13076
13077 test_156() {
13078         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13079         remote_ost_nodsh && skip "remote OST with nodsh"
13080         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
13081                 skip "stats not implemented on old servers"
13082         [ "$ost1_FSTYPE" = "zfs" ] &&
13083                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
13084
13085         local CPAGES=3
13086         local BEFORE
13087         local AFTER
13088         local file="$DIR/$tfile"
13089         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13090
13091         save_writethrough $p
13092         roc_hit_init
13093
13094         log "Turn on read and write cache"
13095         set_cache read on
13096         set_cache writethrough on
13097
13098         log "Write data and read it back."
13099         log "Read should be satisfied from the cache."
13100         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13101         BEFORE=$(roc_hit)
13102         cancel_lru_locks osc
13103         cat $file >/dev/null
13104         AFTER=$(roc_hit)
13105         if ! let "AFTER - BEFORE == CPAGES"; then
13106                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13107         else
13108                 log "cache hits:: before: $BEFORE, after: $AFTER"
13109         fi
13110
13111         log "Read again; it should be satisfied from the cache."
13112         BEFORE=$AFTER
13113         cancel_lru_locks osc
13114         cat $file >/dev/null
13115         AFTER=$(roc_hit)
13116         if ! let "AFTER - BEFORE == CPAGES"; then
13117                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13118         else
13119                 log "cache hits:: before: $BEFORE, after: $AFTER"
13120         fi
13121
13122         log "Turn off the read cache and turn on the write cache"
13123         set_cache read off
13124         set_cache writethrough on
13125
13126         log "Read again; it should be satisfied from the cache."
13127         BEFORE=$(roc_hit)
13128         cancel_lru_locks osc
13129         cat $file >/dev/null
13130         AFTER=$(roc_hit)
13131         if ! let "AFTER - BEFORE == CPAGES"; then
13132                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13133         else
13134                 log "cache hits:: before: $BEFORE, after: $AFTER"
13135         fi
13136
13137         log "Read again; it should not be satisfied from the cache."
13138         BEFORE=$AFTER
13139         cancel_lru_locks osc
13140         cat $file >/dev/null
13141         AFTER=$(roc_hit)
13142         if ! let "AFTER - BEFORE == 0"; then
13143                 error "IN CACHE: before: $BEFORE, after: $AFTER"
13144         else
13145                 log "cache hits:: before: $BEFORE, after: $AFTER"
13146         fi
13147
13148         log "Write data and read it back."
13149         log "Read should be satisfied from the cache."
13150         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13151         BEFORE=$(roc_hit)
13152         cancel_lru_locks osc
13153         cat $file >/dev/null
13154         AFTER=$(roc_hit)
13155         if ! let "AFTER - BEFORE == CPAGES"; then
13156                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13157         else
13158                 log "cache hits:: before: $BEFORE, after: $AFTER"
13159         fi
13160
13161         log "Read again; it should not be satisfied from the cache."
13162         BEFORE=$AFTER
13163         cancel_lru_locks osc
13164         cat $file >/dev/null
13165         AFTER=$(roc_hit)
13166         if ! let "AFTER - BEFORE == 0"; then
13167                 error "IN CACHE: before: $BEFORE, after: $AFTER"
13168         else
13169                 log "cache hits:: before: $BEFORE, after: $AFTER"
13170         fi
13171
13172         log "Turn off read and write cache"
13173         set_cache read off
13174         set_cache writethrough off
13175
13176         log "Write data and read it back"
13177         log "It should not be satisfied from the cache."
13178         rm -f $file
13179         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13180         cancel_lru_locks osc
13181         BEFORE=$(roc_hit)
13182         cat $file >/dev/null
13183         AFTER=$(roc_hit)
13184         if ! let "AFTER - BEFORE == 0"; then
13185                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
13186         else
13187                 log "cache hits:: before: $BEFORE, after: $AFTER"
13188         fi
13189
13190         log "Turn on the read cache and turn off the write cache"
13191         set_cache read on
13192         set_cache writethrough off
13193
13194         log "Write data and read it back"
13195         log "It should not be satisfied from the cache."
13196         rm -f $file
13197         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13198         BEFORE=$(roc_hit)
13199         cancel_lru_locks osc
13200         cat $file >/dev/null
13201         AFTER=$(roc_hit)
13202         if ! let "AFTER - BEFORE == 0"; then
13203                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
13204         else
13205                 log "cache hits:: before: $BEFORE, after: $AFTER"
13206         fi
13207
13208         log "Read again; it should be satisfied from the cache."
13209         BEFORE=$(roc_hit)
13210         cancel_lru_locks osc
13211         cat $file >/dev/null
13212         AFTER=$(roc_hit)
13213         if ! let "AFTER - BEFORE == CPAGES"; then
13214                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13215         else
13216                 log "cache hits:: before: $BEFORE, after: $AFTER"
13217         fi
13218
13219         restore_lustre_params < $p
13220         rm -f $p $file
13221 }
13222 run_test 156 "Verification of tunables"
13223
13224 test_160a() {
13225         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13226         remote_mds_nodsh && skip "remote MDS with nodsh"
13227         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13228                 skip "Need MDS version at least 2.2.0"
13229
13230         changelog_register || error "changelog_register failed"
13231         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13232         changelog_users $SINGLEMDS | grep -q $cl_user ||
13233                 error "User $cl_user not found in changelog_users"
13234
13235         # change something
13236         test_mkdir -p $DIR/$tdir/pics/2008/zachy
13237         changelog_clear 0 || error "changelog_clear failed"
13238         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
13239         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
13240         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
13241         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
13242         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
13243         rm $DIR/$tdir/pics/desktop.jpg
13244
13245         changelog_dump | tail -10
13246
13247         echo "verifying changelog mask"
13248         changelog_chmask "-MKDIR"
13249         changelog_chmask "-CLOSE"
13250
13251         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
13252         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
13253
13254         changelog_chmask "+MKDIR"
13255         changelog_chmask "+CLOSE"
13256
13257         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
13258         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
13259
13260         changelog_dump | tail -10
13261         MKDIRS=$(changelog_dump | grep -c "MKDIR")
13262         CLOSES=$(changelog_dump | grep -c "CLOSE")
13263         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
13264         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
13265
13266         # verify contents
13267         echo "verifying target fid"
13268         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
13269         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
13270         [ "$fidc" == "$fidf" ] ||
13271                 error "changelog '$tfile' fid $fidc != file fid $fidf"
13272         echo "verifying parent fid"
13273         # The FID returned from the Changelog may be the directory shard on
13274         # a different MDT, and not the FID returned by path2fid on the parent.
13275         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
13276         # since this is what will matter when recreating this file in the tree.
13277         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
13278         local pathp=$($LFS fid2path $MOUNT "$fidp")
13279         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
13280                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
13281
13282         echo "getting records for $cl_user"
13283         changelog_users $SINGLEMDS
13284         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
13285         local nclr=3
13286         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
13287                 error "changelog_clear failed"
13288         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
13289         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
13290         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
13291                 error "user index expect $user_rec1 + $nclr != $user_rec2"
13292
13293         local min0_rec=$(changelog_users $SINGLEMDS |
13294                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
13295         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
13296                           awk '{ print $1; exit; }')
13297
13298         changelog_dump | tail -n 5
13299         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
13300         [ $first_rec == $((min0_rec + 1)) ] ||
13301                 error "first index should be $min0_rec + 1 not $first_rec"
13302
13303         # LU-3446 changelog index reset on MDT restart
13304         local cur_rec1=$(changelog_users $SINGLEMDS |
13305                          awk '/^current.index:/ { print $NF }')
13306         changelog_clear 0 ||
13307                 error "clear all changelog records for $cl_user failed"
13308         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
13309         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
13310                 error "Fail to start $SINGLEMDS"
13311         local cur_rec2=$(changelog_users $SINGLEMDS |
13312                          awk '/^current.index:/ { print $NF }')
13313         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
13314         [ $cur_rec1 == $cur_rec2 ] ||
13315                 error "current index should be $cur_rec1 not $cur_rec2"
13316
13317         echo "verifying users from this test are deregistered"
13318         changelog_deregister || error "changelog_deregister failed"
13319         changelog_users $SINGLEMDS | grep -q $cl_user &&
13320                 error "User '$cl_user' still in changelog_users"
13321
13322         # lctl get_param -n mdd.*.changelog_users
13323         # current index: 144
13324         # ID    index (idle seconds)
13325         # cl3   144 (2)
13326         if ! changelog_users $SINGLEMDS | grep "^cl"; then
13327                 # this is the normal case where all users were deregistered
13328                 # make sure no new records are added when no users are present
13329                 local last_rec1=$(changelog_users $SINGLEMDS |
13330                                   awk '/^current.index:/ { print $NF }')
13331                 touch $DIR/$tdir/chloe
13332                 local last_rec2=$(changelog_users $SINGLEMDS |
13333                                   awk '/^current.index:/ { print $NF }')
13334                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
13335                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
13336         else
13337                 # any changelog users must be leftovers from a previous test
13338                 changelog_users $SINGLEMDS
13339                 echo "other changelog users; can't verify off"
13340         fi
13341 }
13342 run_test 160a "changelog sanity"
13343
13344 test_160b() { # LU-3587
13345         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13346         remote_mds_nodsh && skip "remote MDS with nodsh"
13347         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13348                 skip "Need MDS version at least 2.2.0"
13349
13350         changelog_register || error "changelog_register failed"
13351         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13352         changelog_users $SINGLEMDS | grep -q $cl_user ||
13353                 error "User '$cl_user' not found in changelog_users"
13354
13355         local longname1=$(str_repeat a 255)
13356         local longname2=$(str_repeat b 255)
13357
13358         cd $DIR
13359         echo "creating very long named file"
13360         touch $longname1 || error "create of '$longname1' failed"
13361         echo "renaming very long named file"
13362         mv $longname1 $longname2
13363
13364         changelog_dump | grep RENME | tail -n 5
13365         rm -f $longname2
13366 }
13367 run_test 160b "Verify that very long rename doesn't crash in changelog"
13368
13369 test_160c() {
13370         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13371         remote_mds_nodsh && skip "remote MDS with nodsh"
13372
13373         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
13374                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
13375                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
13376                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
13377
13378         local rc=0
13379
13380         # Registration step
13381         changelog_register || error "changelog_register failed"
13382
13383         rm -rf $DIR/$tdir
13384         mkdir -p $DIR/$tdir
13385         $MCREATE $DIR/$tdir/foo_160c
13386         changelog_chmask "-TRUNC"
13387         $TRUNCATE $DIR/$tdir/foo_160c 200
13388         changelog_chmask "+TRUNC"
13389         $TRUNCATE $DIR/$tdir/foo_160c 199
13390         changelog_dump | tail -n 5
13391         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
13392         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
13393 }
13394 run_test 160c "verify that changelog log catch the truncate event"
13395
13396 test_160d() {
13397         remote_mds_nodsh && skip "remote MDS with nodsh"
13398         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13399         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13400         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
13401                 skip "Need MDS version at least 2.7.60"
13402
13403         # Registration step
13404         changelog_register || error "changelog_register failed"
13405
13406         mkdir -p $DIR/$tdir/migrate_dir
13407         changelog_clear 0 || error "changelog_clear failed"
13408
13409         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
13410         changelog_dump | tail -n 5
13411         local migrates=$(changelog_dump | grep -c "MIGRT")
13412         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
13413 }
13414 run_test 160d "verify that changelog log catch the migrate event"
13415
13416 test_160e() {
13417         remote_mds_nodsh && skip "remote MDS with nodsh"
13418
13419         # Create a user
13420         changelog_register || error "changelog_register failed"
13421
13422         # Delete a future user (expect fail)
13423         local MDT0=$(facet_svc $SINGLEMDS)
13424         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
13425         local rc=$?
13426
13427         if [ $rc -eq 0 ]; then
13428                 error "Deleted non-existant user cl77"
13429         elif [ $rc -ne 2 ]; then
13430                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
13431         fi
13432
13433         # Clear to a bad index (1 billion should be safe)
13434         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
13435         rc=$?
13436
13437         if [ $rc -eq 0 ]; then
13438                 error "Successfully cleared to invalid CL index"
13439         elif [ $rc -ne 22 ]; then
13440                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
13441         fi
13442 }
13443 run_test 160e "changelog negative testing (should return errors)"
13444
13445 test_160f() {
13446         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13447         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13448                 skip "Need MDS version at least 2.10.56"
13449
13450         local mdts=$(comma_list $(mdts_nodes))
13451
13452         # Create a user
13453         changelog_register || error "first changelog_register failed"
13454         changelog_register || error "second changelog_register failed"
13455         local cl_users
13456         declare -A cl_user1
13457         declare -A cl_user2
13458         local user_rec1
13459         local user_rec2
13460         local i
13461
13462         # generate some changelog records to accumulate on each MDT
13463         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
13464         log "$(date +%s): creating first files"
13465         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13466                 error "create $DIR/$tdir/$tfile failed"
13467
13468         # check changelogs have been generated
13469         local start=$SECONDS
13470         local idle_time=$((MDSCOUNT * 5 + 5))
13471         local nbcl=$(changelog_dump | wc -l)
13472         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13473
13474         for param in "changelog_max_idle_time=$idle_time" \
13475                      "changelog_gc=1" \
13476                      "changelog_min_gc_interval=2" \
13477                      "changelog_min_free_cat_entries=3"; do
13478                 local MDT0=$(facet_svc $SINGLEMDS)
13479                 local var="${param%=*}"
13480                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13481
13482                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13483                 do_nodes $mdts $LCTL set_param mdd.*.$param
13484         done
13485
13486         # force cl_user2 to be idle (1st part), but also cancel the
13487         # cl_user1 records so that it is not evicted later in the test.
13488         local sleep1=$((idle_time / 2))
13489         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
13490         sleep $sleep1
13491
13492         # simulate changelog catalog almost full
13493         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
13494         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13495
13496         for i in $(seq $MDSCOUNT); do
13497                 cl_users=(${CL_USERS[mds$i]})
13498                 cl_user1[mds$i]="${cl_users[0]}"
13499                 cl_user2[mds$i]="${cl_users[1]}"
13500
13501                 [ -n "${cl_user1[mds$i]}" ] ||
13502                         error "mds$i: no user registered"
13503                 [ -n "${cl_user2[mds$i]}" ] ||
13504                         error "mds$i: only ${cl_user2[mds$i]} is registered"
13505
13506                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13507                 [ -n "$user_rec1" ] ||
13508                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13509                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13510                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13511                 [ -n "$user_rec2" ] ||
13512                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13513                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13514                      "$user_rec1 + 2 == $user_rec2"
13515                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13516                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13517                               "$user_rec1 + 2, but is $user_rec2"
13518                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13519                 [ -n "$user_rec2" ] ||
13520                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13521                 [ $user_rec1 == $user_rec2 ] ||
13522                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13523                               "$user_rec1, but is $user_rec2"
13524         done
13525
13526         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
13527         local sleep2=$((idle_time - (SECONDS - start) + 1))
13528         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
13529         sleep $sleep2
13530
13531         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
13532         # cl_user1 should be OK because it recently processed records.
13533         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
13534         createmany -m $DIR/$tdir/${tfile}b $((MDSCOUNT * 2)) ||
13535                 error "create $DIR/$tdir/${tfile}b failed"
13536
13537         # ensure gc thread is done
13538         for i in $(mdts_nodes); do
13539                 wait_update $i \
13540                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13541                         error "$i: GC-thread not done"
13542         done
13543
13544         local first_rec
13545         for i in $(seq $MDSCOUNT); do
13546                 # check cl_user1 still registered
13547                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13548                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13549                 # check cl_user2 unregistered
13550                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13551                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13552
13553                 # check changelogs are present and starting at $user_rec1 + 1
13554                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13555                 [ -n "$user_rec1" ] ||
13556                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13557                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13558                             awk '{ print $1; exit; }')
13559
13560                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13561                 [ $((user_rec1 + 1)) == $first_rec ] ||
13562                         error "mds$i: first index should be $user_rec1 + 1, " \
13563                               "but is $first_rec"
13564         done
13565 }
13566 run_test 160f "changelog garbage collect (timestamped users)"
13567
13568 test_160g() {
13569         remote_mds_nodsh && skip "remote MDS with nodsh"
13570         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13571                 skip "Need MDS version at least 2.10.56"
13572
13573         local mdts=$(comma_list $(mdts_nodes))
13574
13575         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
13576         do_nodes $mdts $LCTL set_param fail_loc=0x1314
13577
13578         # Create a user
13579         changelog_register || error "first changelog_register failed"
13580         changelog_register || error "second changelog_register failed"
13581         local cl_users
13582         declare -A cl_user1
13583         declare -A cl_user2
13584         local user_rec1
13585         local user_rec2
13586         local i
13587
13588         # generate some changelog records to accumulate on each MDT
13589         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13590         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13591                 error "create $DIR/$tdir/$tfile failed"
13592
13593         # check changelogs have been generated
13594         local nbcl=$(changelog_dump | wc -l)
13595         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13596
13597         # reduce the max_idle_indexes value to make sure we exceed it
13598         max_ndx=$((nbcl / 2 - 1))
13599
13600         for param in "changelog_max_idle_indexes=$max_ndx" \
13601                      "changelog_gc=1" \
13602                      "changelog_min_gc_interval=2" \
13603                      "changelog_min_free_cat_entries=3"; do
13604                 local MDT0=$(facet_svc $SINGLEMDS)
13605                 local var="${param%=*}"
13606                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13607
13608                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13609                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
13610                         error "unable to set mdd.*.$param"
13611         done
13612
13613         # simulate changelog catalog almost full
13614         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
13615         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13616
13617         for i in $(seq $MDSCOUNT); do
13618                 cl_users=(${CL_USERS[mds$i]})
13619                 cl_user1[mds$i]="${cl_users[0]}"
13620                 cl_user2[mds$i]="${cl_users[1]}"
13621
13622                 [ -n "${cl_user1[mds$i]}" ] ||
13623                         error "mds$i: no user registered"
13624                 [ -n "${cl_user2[mds$i]}" ] ||
13625                         error "mds$i: only ${cl_user1[mds$i]} is registered"
13626
13627                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13628                 [ -n "$user_rec1" ] ||
13629                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13630                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13631                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13632                 [ -n "$user_rec2" ] ||
13633                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13634                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13635                      "$user_rec1 + 2 == $user_rec2"
13636                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13637                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13638                               "$user_rec1 + 2, but is $user_rec2"
13639                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13640                 [ -n "$user_rec2" ] ||
13641                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13642                 [ $user_rec1 == $user_rec2 ] ||
13643                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13644                               "$user_rec1, but is $user_rec2"
13645         done
13646
13647         # ensure we are past the previous changelog_min_gc_interval set above
13648         sleep 2
13649
13650         # generate one more changelog to trigger fail_loc
13651         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13652                 error "create $DIR/$tdir/${tfile}bis failed"
13653
13654         # ensure gc thread is done
13655         for i in $(mdts_nodes); do
13656                 wait_update $i \
13657                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13658                         error "$i: GC-thread not done"
13659         done
13660
13661         local first_rec
13662         for i in $(seq $MDSCOUNT); do
13663                 # check cl_user1 still registered
13664                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13665                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13666                 # check cl_user2 unregistered
13667                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13668                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13669
13670                 # check changelogs are present and starting at $user_rec1 + 1
13671                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13672                 [ -n "$user_rec1" ] ||
13673                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13674                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13675                             awk '{ print $1; exit; }')
13676
13677                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13678                 [ $((user_rec1 + 1)) == $first_rec ] ||
13679                         error "mds$i: first index should be $user_rec1 + 1, " \
13680                               "but is $first_rec"
13681         done
13682 }
13683 run_test 160g "changelog garbage collect (old users)"
13684
13685 test_160h() {
13686         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13687         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13688                 skip "Need MDS version at least 2.10.56"
13689
13690         local mdts=$(comma_list $(mdts_nodes))
13691
13692         # Create a user
13693         changelog_register || error "first changelog_register failed"
13694         changelog_register || error "second changelog_register failed"
13695         local cl_users
13696         declare -A cl_user1
13697         declare -A cl_user2
13698         local user_rec1
13699         local user_rec2
13700         local i
13701
13702         # generate some changelog records to accumulate on each MDT
13703         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
13704         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13705                 error "create $DIR/$tdir/$tfile failed"
13706
13707         # check changelogs have been generated
13708         local nbcl=$(changelog_dump | wc -l)
13709         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13710
13711         for param in "changelog_max_idle_time=10" \
13712                      "changelog_gc=1" \
13713                      "changelog_min_gc_interval=2"; do
13714                 local MDT0=$(facet_svc $SINGLEMDS)
13715                 local var="${param%=*}"
13716                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13717
13718                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13719                 do_nodes $mdts $LCTL set_param mdd.*.$param
13720         done
13721
13722         # force cl_user2 to be idle (1st part)
13723         sleep 9
13724
13725         for i in $(seq $MDSCOUNT); do
13726                 cl_users=(${CL_USERS[mds$i]})
13727                 cl_user1[mds$i]="${cl_users[0]}"
13728                 cl_user2[mds$i]="${cl_users[1]}"
13729
13730                 [ -n "${cl_user1[mds$i]}" ] ||
13731                         error "mds$i: no user registered"
13732                 [ -n "${cl_user2[mds$i]}" ] ||
13733                         error "mds$i: only ${cl_user2[mds$i]} is registered"
13734
13735                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13736                 [ -n "$user_rec1" ] ||
13737                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13738                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13739                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13740                 [ -n "$user_rec2" ] ||
13741                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13742                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13743                      "$user_rec1 + 2 == $user_rec2"
13744                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13745                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13746                               "$user_rec1 + 2, but is $user_rec2"
13747                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13748                 [ -n "$user_rec2" ] ||
13749                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13750                 [ $user_rec1 == $user_rec2 ] ||
13751                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13752                               "$user_rec1, but is $user_rec2"
13753         done
13754
13755         # force cl_user2 to be idle (2nd part) and to reach
13756         # changelog_max_idle_time
13757         sleep 2
13758
13759         # force each GC-thread start and block then
13760         # one per MDT/MDD, set fail_val accordingly
13761         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
13762         do_nodes $mdts $LCTL set_param fail_loc=0x1316
13763
13764         # generate more changelogs to trigger fail_loc
13765         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13766                 error "create $DIR/$tdir/${tfile}bis failed"
13767
13768         # stop MDT to stop GC-thread, should be done in back-ground as it will
13769         # block waiting for the thread to be released and exit
13770         declare -A stop_pids
13771         for i in $(seq $MDSCOUNT); do
13772                 stop mds$i &
13773                 stop_pids[mds$i]=$!
13774         done
13775
13776         for i in $(mdts_nodes); do
13777                 local facet
13778                 local nb=0
13779                 local facets=$(facets_up_on_host $i)
13780
13781                 for facet in ${facets//,/ }; do
13782                         if [[ $facet == mds* ]]; then
13783                                 nb=$((nb + 1))
13784                         fi
13785                 done
13786                 # ensure each MDS's gc threads are still present and all in "R"
13787                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
13788                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
13789                         error "$i: expected $nb GC-thread"
13790                 wait_update $i \
13791                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
13792                         "R" 20 ||
13793                         error "$i: GC-thread not found in R-state"
13794                 # check umounts of each MDT on MDS have reached kthread_stop()
13795                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
13796                         error "$i: expected $nb umount"
13797                 wait_update $i \
13798                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
13799                         error "$i: umount not found in D-state"
13800         done
13801
13802         # release all GC-threads
13803         do_nodes $mdts $LCTL set_param fail_loc=0
13804
13805         # wait for MDT stop to complete
13806         for i in $(seq $MDSCOUNT); do
13807                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
13808         done
13809
13810         # XXX
13811         # may try to check if any orphan changelog records are present
13812         # via ldiskfs/zfs and llog_reader...
13813
13814         # re-start/mount MDTs
13815         for i in $(seq $MDSCOUNT); do
13816                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
13817                         error "Fail to start mds$i"
13818         done
13819
13820         local first_rec
13821         for i in $(seq $MDSCOUNT); do
13822                 # check cl_user1 still registered
13823                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13824                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13825                 # check cl_user2 unregistered
13826                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13827                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13828
13829                 # check changelogs are present and starting at $user_rec1 + 1
13830                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13831                 [ -n "$user_rec1" ] ||
13832                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13833                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13834                             awk '{ print $1; exit; }')
13835
13836                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13837                 [ $((user_rec1 + 1)) == $first_rec ] ||
13838                         error "mds$i: first index should be $user_rec1 + 1, " \
13839                               "but is $first_rec"
13840         done
13841 }
13842 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
13843               "during mount"
13844
13845 test_160i() {
13846
13847         local mdts=$(comma_list $(mdts_nodes))
13848
13849         changelog_register || error "first changelog_register failed"
13850
13851         # generate some changelog records to accumulate on each MDT
13852         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13853         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13854                 error "create $DIR/$tdir/$tfile failed"
13855
13856         # check changelogs have been generated
13857         local nbcl=$(changelog_dump | wc -l)
13858         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13859
13860         # simulate race between register and unregister
13861         # XXX as fail_loc is set per-MDS, with DNE configs the race
13862         # simulation will only occur for one MDT per MDS and for the
13863         # others the normal race scenario will take place
13864         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
13865         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
13866         do_nodes $mdts $LCTL set_param fail_val=1
13867
13868         # unregister 1st user
13869         changelog_deregister &
13870         local pid1=$!
13871         # wait some time for deregister work to reach race rdv
13872         sleep 2
13873         # register 2nd user
13874         changelog_register || error "2nd user register failed"
13875
13876         wait $pid1 || error "1st user deregister failed"
13877
13878         local i
13879         local last_rec
13880         declare -A LAST_REC
13881         for i in $(seq $MDSCOUNT); do
13882                 if changelog_users mds$i | grep "^cl"; then
13883                         # make sure new records are added with one user present
13884                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
13885                                           awk '/^current.index:/ { print $NF }')
13886                 else
13887                         error "mds$i has no user registered"
13888                 fi
13889         done
13890
13891         # generate more changelog records to accumulate on each MDT
13892         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13893                 error "create $DIR/$tdir/${tfile}bis failed"
13894
13895         for i in $(seq $MDSCOUNT); do
13896                 last_rec=$(changelog_users $SINGLEMDS |
13897                            awk '/^current.index:/ { print $NF }')
13898                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
13899                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
13900                         error "changelogs are off on mds$i"
13901         done
13902 }
13903 run_test 160i "changelog user register/unregister race"
13904
13905 test_160k() {
13906         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13907         remote_mds_nodsh && skip "remote MDS with nodsh"
13908
13909         mkdir -p $DIR/$tdir/1/1
13910
13911         changelog_register || error "changelog_register failed"
13912         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13913
13914         changelog_users $SINGLEMDS | grep -q $cl_user ||
13915                 error "User '$cl_user' not found in changelog_users"
13916 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
13917         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
13918         rmdir $DIR/$tdir/1/1 & sleep 1
13919         mkdir $DIR/$tdir/2
13920         touch $DIR/$tdir/2/2
13921         rm -rf $DIR/$tdir/2
13922
13923         wait
13924         sleep 4
13925
13926         changelog_dump | grep rmdir || error "rmdir not recorded"
13927
13928         rm -rf $DIR/$tdir
13929         changelog_deregister
13930 }
13931 run_test 160k "Verify that changelog records are not lost"
13932
13933 test_160j() {
13934         remote_mds_nodsh && skip "remote MDS with nodsh"
13935         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
13936                 skip "Need MDS version at least 2.12.56"
13937
13938         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
13939
13940         changelog_register || error "first changelog_register failed"
13941
13942         # generate some changelog
13943         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13944         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13945                 error "create $DIR/$tdir/${tfile}bis failed"
13946
13947         # open the changelog device
13948         exec 3>/dev/changelog-$FSNAME-MDT0000
13949         exec 4</dev/changelog-$FSNAME-MDT0000
13950
13951         # umount the first lustre mount
13952         umount $MOUNT
13953
13954         # read changelog
13955         cat <&4 >/dev/null || error "read changelog failed"
13956
13957         # clear changelog
13958         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13959         changelog_users $SINGLEMDS | grep -q $cl_user ||
13960                 error "User $cl_user not found in changelog_users"
13961
13962         printf 'clear:'$cl_user':0' >&3
13963
13964         # close
13965         exec 3>&-
13966         exec 4<&-
13967
13968         # cleanup
13969         changelog_deregister || error "changelog_deregister failed"
13970
13971         umount $MOUNT2
13972         mount_client $MOUNT || error "mount_client on $MOUNT failed"
13973 }
13974 run_test 160j "client can be umounted  while its chanangelog is being used"
13975
13976 test_161a() {
13977         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13978
13979         test_mkdir -c1 $DIR/$tdir
13980         cp /etc/hosts $DIR/$tdir/$tfile
13981         test_mkdir -c1 $DIR/$tdir/foo1
13982         test_mkdir -c1 $DIR/$tdir/foo2
13983         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
13984         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
13985         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
13986         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
13987         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
13988         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
13989                 $LFS fid2path $DIR $FID
13990                 error "bad link ea"
13991         fi
13992         # middle
13993         rm $DIR/$tdir/foo2/zachary
13994         # last
13995         rm $DIR/$tdir/foo2/thor
13996         # first
13997         rm $DIR/$tdir/$tfile
13998         # rename
13999         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
14000         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
14001                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
14002         rm $DIR/$tdir/foo2/maggie
14003
14004         # overflow the EA
14005         local longname=$tfile.avg_len_is_thirty_two_
14006         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
14007                 error_noexit 'failed to unlink many hardlinks'" EXIT
14008         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
14009                 error "failed to hardlink many files"
14010         links=$($LFS fid2path $DIR $FID | wc -l)
14011         echo -n "${links}/1000 links in link EA"
14012         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
14013 }
14014 run_test 161a "link ea sanity"
14015
14016 test_161b() {
14017         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14018         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
14019
14020         local MDTIDX=1
14021         local remote_dir=$DIR/$tdir/remote_dir
14022
14023         mkdir -p $DIR/$tdir
14024         $LFS mkdir -i $MDTIDX $remote_dir ||
14025                 error "create remote directory failed"
14026
14027         cp /etc/hosts $remote_dir/$tfile
14028         mkdir -p $remote_dir/foo1
14029         mkdir -p $remote_dir/foo2
14030         ln $remote_dir/$tfile $remote_dir/foo1/sofia
14031         ln $remote_dir/$tfile $remote_dir/foo2/zachary
14032         ln $remote_dir/$tfile $remote_dir/foo1/luna
14033         ln $remote_dir/$tfile $remote_dir/foo2/thor
14034
14035         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
14036                      tr -d ']')
14037         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14038                 $LFS fid2path $DIR $FID
14039                 error "bad link ea"
14040         fi
14041         # middle
14042         rm $remote_dir/foo2/zachary
14043         # last
14044         rm $remote_dir/foo2/thor
14045         # first
14046         rm $remote_dir/$tfile
14047         # rename
14048         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
14049         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
14050         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
14051                 $LFS fid2path $DIR $FID
14052                 error "bad link rename"
14053         fi
14054         rm $remote_dir/foo2/maggie
14055
14056         # overflow the EA
14057         local longname=filename_avg_len_is_thirty_two_
14058         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
14059                 error "failed to hardlink many files"
14060         links=$($LFS fid2path $DIR $FID | wc -l)
14061         echo -n "${links}/1000 links in link EA"
14062         [[ ${links} -gt 60 ]] ||
14063                 error "expected at least 60 links in link EA"
14064         unlinkmany $remote_dir/foo2/$longname 1000 ||
14065         error "failed to unlink many hardlinks"
14066 }
14067 run_test 161b "link ea sanity under remote directory"
14068
14069 test_161c() {
14070         remote_mds_nodsh && skip "remote MDS with nodsh"
14071         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14072         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
14073                 skip "Need MDS version at least 2.1.5"
14074
14075         # define CLF_RENAME_LAST 0x0001
14076         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
14077         changelog_register || error "changelog_register failed"
14078
14079         rm -rf $DIR/$tdir
14080         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
14081         touch $DIR/$tdir/foo_161c
14082         touch $DIR/$tdir/bar_161c
14083         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14084         changelog_dump | grep RENME | tail -n 5
14085         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14086         changelog_clear 0 || error "changelog_clear failed"
14087         if [ x$flags != "x0x1" ]; then
14088                 error "flag $flags is not 0x1"
14089         fi
14090
14091         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
14092         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
14093         touch $DIR/$tdir/foo_161c
14094         touch $DIR/$tdir/bar_161c
14095         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14096         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14097         changelog_dump | grep RENME | tail -n 5
14098         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14099         changelog_clear 0 || error "changelog_clear failed"
14100         if [ x$flags != "x0x0" ]; then
14101                 error "flag $flags is not 0x0"
14102         fi
14103         echo "rename overwrite a target having nlink > 1," \
14104                 "changelog record has flags of $flags"
14105
14106         # rename doesn't overwrite a target (changelog flag 0x0)
14107         touch $DIR/$tdir/foo_161c
14108         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
14109         changelog_dump | grep RENME | tail -n 5
14110         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
14111         changelog_clear 0 || error "changelog_clear failed"
14112         if [ x$flags != "x0x0" ]; then
14113                 error "flag $flags is not 0x0"
14114         fi
14115         echo "rename doesn't overwrite a target," \
14116                 "changelog record has flags of $flags"
14117
14118         # define CLF_UNLINK_LAST 0x0001
14119         # unlink a file having nlink = 1 (changelog flag 0x1)
14120         rm -f $DIR/$tdir/foo2_161c
14121         changelog_dump | grep UNLNK | tail -n 5
14122         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14123         changelog_clear 0 || error "changelog_clear failed"
14124         if [ x$flags != "x0x1" ]; then
14125                 error "flag $flags is not 0x1"
14126         fi
14127         echo "unlink a file having nlink = 1," \
14128                 "changelog record has flags of $flags"
14129
14130         # unlink a file having nlink > 1 (changelog flag 0x0)
14131         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14132         rm -f $DIR/$tdir/foobar_161c
14133         changelog_dump | grep UNLNK | tail -n 5
14134         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14135         changelog_clear 0 || error "changelog_clear failed"
14136         if [ x$flags != "x0x0" ]; then
14137                 error "flag $flags is not 0x0"
14138         fi
14139         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
14140 }
14141 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
14142
14143 test_161d() {
14144         remote_mds_nodsh && skip "remote MDS with nodsh"
14145
14146         local pid
14147         local fid
14148
14149         changelog_register || error "changelog_register failed"
14150
14151         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
14152         # interfer with $MOUNT/.lustre/fid/ access
14153         mkdir $DIR/$tdir
14154         [[ $? -eq 0 ]] || error "mkdir failed"
14155
14156         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
14157         $LCTL set_param fail_loc=0x8000140c
14158         # 5s pause
14159         $LCTL set_param fail_val=5
14160
14161         # create file
14162         echo foofoo > $DIR/$tdir/$tfile &
14163         pid=$!
14164
14165         # wait for create to be delayed
14166         sleep 2
14167
14168         ps -p $pid
14169         [[ $? -eq 0 ]] || error "create should be blocked"
14170
14171         local tempfile=$(mktemp)
14172         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
14173         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
14174         # some delay may occur during ChangeLog publishing and file read just
14175         # above, that could allow file write to happen finally
14176         [[ -s $tempfile ]] && echo "file should be empty"
14177
14178         $LCTL set_param fail_loc=0
14179
14180         wait $pid
14181         [[ $? -eq 0 ]] || error "create failed"
14182 }
14183 run_test 161d "create with concurrent .lustre/fid access"
14184
14185 check_path() {
14186         local expected="$1"
14187         shift
14188         local fid="$2"
14189
14190         local path
14191         path=$($LFS fid2path "$@")
14192         local rc=$?
14193
14194         if [ $rc -ne 0 ]; then
14195                 error "path looked up of '$expected' failed: rc=$rc"
14196         elif [ "$path" != "$expected" ]; then
14197                 error "path looked up '$path' instead of '$expected'"
14198         else
14199                 echo "FID '$fid' resolves to path '$path' as expected"
14200         fi
14201 }
14202
14203 test_162a() { # was test_162
14204         test_mkdir -p -c1 $DIR/$tdir/d2
14205         touch $DIR/$tdir/d2/$tfile
14206         touch $DIR/$tdir/d2/x1
14207         touch $DIR/$tdir/d2/x2
14208         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
14209         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
14210         # regular file
14211         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
14212         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
14213
14214         # softlink
14215         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
14216         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
14217         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
14218
14219         # softlink to wrong file
14220         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
14221         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
14222         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
14223
14224         # hardlink
14225         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
14226         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
14227         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
14228         # fid2path dir/fsname should both work
14229         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
14230         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
14231
14232         # hardlink count: check that there are 2 links
14233         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
14234         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
14235
14236         # hardlink indexing: remove the first link
14237         rm $DIR/$tdir/d2/p/q/r/hlink
14238         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
14239 }
14240 run_test 162a "path lookup sanity"
14241
14242 test_162b() {
14243         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14244         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14245
14246         mkdir $DIR/$tdir
14247         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
14248                                 error "create striped dir failed"
14249
14250         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
14251                                         tail -n 1 | awk '{print $2}')
14252         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
14253
14254         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
14255         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
14256
14257         # regular file
14258         for ((i=0;i<5;i++)); do
14259                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
14260                         error "get fid for f$i failed"
14261                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
14262
14263                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
14264                         error "get fid for d$i failed"
14265                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
14266         done
14267
14268         return 0
14269 }
14270 run_test 162b "striped directory path lookup sanity"
14271
14272 # LU-4239: Verify fid2path works with paths 100 or more directories deep
14273 test_162c() {
14274         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
14275                 skip "Need MDS version at least 2.7.51"
14276
14277         local lpath=$tdir.local
14278         local rpath=$tdir.remote
14279
14280         test_mkdir $DIR/$lpath
14281         test_mkdir $DIR/$rpath
14282
14283         for ((i = 0; i <= 101; i++)); do
14284                 lpath="$lpath/$i"
14285                 mkdir $DIR/$lpath
14286                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
14287                         error "get fid for local directory $DIR/$lpath failed"
14288                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
14289
14290                 rpath="$rpath/$i"
14291                 test_mkdir $DIR/$rpath
14292                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
14293                         error "get fid for remote directory $DIR/$rpath failed"
14294                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
14295         done
14296
14297         return 0
14298 }
14299 run_test 162c "fid2path works with paths 100 or more directories deep"
14300
14301 test_169() {
14302         # do directio so as not to populate the page cache
14303         log "creating a 10 Mb file"
14304         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
14305         log "starting reads"
14306         dd if=$DIR/$tfile of=/dev/null bs=4096 &
14307         log "truncating the file"
14308         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
14309         log "killing dd"
14310         kill %+ || true # reads might have finished
14311         echo "wait until dd is finished"
14312         wait
14313         log "removing the temporary file"
14314         rm -rf $DIR/$tfile || error "tmp file removal failed"
14315 }
14316 run_test 169 "parallel read and truncate should not deadlock"
14317
14318 test_170() {
14319         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14320
14321         $LCTL clear     # bug 18514
14322         $LCTL debug_daemon start $TMP/${tfile}_log_good
14323         touch $DIR/$tfile
14324         $LCTL debug_daemon stop
14325         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
14326                 error "sed failed to read log_good"
14327
14328         $LCTL debug_daemon start $TMP/${tfile}_log_good
14329         rm -rf $DIR/$tfile
14330         $LCTL debug_daemon stop
14331
14332         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
14333                error "lctl df log_bad failed"
14334
14335         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14336         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14337
14338         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
14339         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
14340
14341         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
14342                 error "bad_line good_line1 good_line2 are empty"
14343
14344         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14345         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
14346         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14347
14348         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
14349         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14350         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14351
14352         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
14353                 error "bad_line_new good_line_new are empty"
14354
14355         local expected_good=$((good_line1 + good_line2*2))
14356
14357         rm -f $TMP/${tfile}*
14358         # LU-231, short malformed line may not be counted into bad lines
14359         if [ $bad_line -ne $bad_line_new ] &&
14360                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
14361                 error "expected $bad_line bad lines, but got $bad_line_new"
14362                 return 1
14363         fi
14364
14365         if [ $expected_good -ne $good_line_new ]; then
14366                 error "expected $expected_good good lines, but got $good_line_new"
14367                 return 2
14368         fi
14369         true
14370 }
14371 run_test 170 "test lctl df to handle corrupted log ====================="
14372
14373 test_171() { # bug20592
14374         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14375
14376         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
14377         $LCTL set_param fail_loc=0x50e
14378         $LCTL set_param fail_val=3000
14379         multiop_bg_pause $DIR/$tfile O_s || true
14380         local MULTIPID=$!
14381         kill -USR1 $MULTIPID
14382         # cause log dump
14383         sleep 3
14384         wait $MULTIPID
14385         if dmesg | grep "recursive fault"; then
14386                 error "caught a recursive fault"
14387         fi
14388         $LCTL set_param fail_loc=0
14389         true
14390 }
14391 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
14392
14393 # it would be good to share it with obdfilter-survey/iokit-libecho code
14394 setup_obdecho_osc () {
14395         local rc=0
14396         local ost_nid=$1
14397         local obdfilter_name=$2
14398         echo "Creating new osc for $obdfilter_name on $ost_nid"
14399         # make sure we can find loopback nid
14400         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
14401
14402         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
14403                            ${obdfilter_name}_osc_UUID || rc=2; }
14404         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
14405                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
14406         return $rc
14407 }
14408
14409 cleanup_obdecho_osc () {
14410         local obdfilter_name=$1
14411         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
14412         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
14413         return 0
14414 }
14415
14416 obdecho_test() {
14417         local OBD=$1
14418         local node=$2
14419         local pages=${3:-64}
14420         local rc=0
14421         local id
14422
14423         local count=10
14424         local obd_size=$(get_obd_size $node $OBD)
14425         local page_size=$(get_page_size $node)
14426         if [[ -n "$obd_size" ]]; then
14427                 local new_count=$((obd_size / (pages * page_size / 1024)))
14428                 [[ $new_count -ge $count ]] || count=$new_count
14429         fi
14430
14431         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
14432         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
14433                            rc=2; }
14434         if [ $rc -eq 0 ]; then
14435             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
14436             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
14437         fi
14438         echo "New object id is $id"
14439         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
14440                            rc=4; }
14441         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
14442                            "test_brw $count w v $pages $id" || rc=4; }
14443         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
14444                            rc=4; }
14445         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
14446                                         "cleanup" || rc=5; }
14447         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
14448                                         "detach" || rc=6; }
14449         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
14450         return $rc
14451 }
14452
14453 test_180a() {
14454         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14455
14456         if ! module_loaded obdecho; then
14457                 load_module obdecho/obdecho &&
14458                         stack_trap "rmmod obdecho" EXIT ||
14459                         error "unable to load obdecho on client"
14460         fi
14461
14462         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
14463         local host=$($LCTL get_param -n osc.$osc.import |
14464                      awk '/current_connection:/ { print $2 }' )
14465         local target=$($LCTL get_param -n osc.$osc.import |
14466                        awk '/target:/ { print $2 }' )
14467         target=${target%_UUID}
14468
14469         if [ -n "$target" ]; then
14470                 setup_obdecho_osc $host $target &&
14471                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
14472                         { error "obdecho setup failed with $?"; return; }
14473
14474                 obdecho_test ${target}_osc client ||
14475                         error "obdecho_test failed on ${target}_osc"
14476         else
14477                 $LCTL get_param osc.$osc.import
14478                 error "there is no osc.$osc.import target"
14479         fi
14480 }
14481 run_test 180a "test obdecho on osc"
14482
14483 test_180b() {
14484         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14485         remote_ost_nodsh && skip "remote OST with nodsh"
14486
14487         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14488                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14489                 error "failed to load module obdecho"
14490
14491         local target=$(do_facet ost1 $LCTL dl |
14492                        awk '/obdfilter/ { print $4; exit; }')
14493
14494         if [ -n "$target" ]; then
14495                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
14496         else
14497                 do_facet ost1 $LCTL dl
14498                 error "there is no obdfilter target on ost1"
14499         fi
14500 }
14501 run_test 180b "test obdecho directly on obdfilter"
14502
14503 test_180c() { # LU-2598
14504         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14505         remote_ost_nodsh && skip "remote OST with nodsh"
14506         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
14507                 skip "Need MDS version at least 2.4.0"
14508
14509         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14510                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14511                 error "failed to load module obdecho"
14512
14513         local target=$(do_facet ost1 $LCTL dl |
14514                        awk '/obdfilter/ { print $4; exit; }')
14515
14516         if [ -n "$target" ]; then
14517                 local pages=16384 # 64MB bulk I/O RPC size
14518
14519                 obdecho_test "$target" ost1 "$pages" ||
14520                         error "obdecho_test with pages=$pages failed with $?"
14521         else
14522                 do_facet ost1 $LCTL dl
14523                 error "there is no obdfilter target on ost1"
14524         fi
14525 }
14526 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
14527
14528 test_181() { # bug 22177
14529         test_mkdir $DIR/$tdir
14530         # create enough files to index the directory
14531         createmany -o $DIR/$tdir/foobar 4000
14532         # print attributes for debug purpose
14533         lsattr -d .
14534         # open dir
14535         multiop_bg_pause $DIR/$tdir D_Sc || return 1
14536         MULTIPID=$!
14537         # remove the files & current working dir
14538         unlinkmany $DIR/$tdir/foobar 4000
14539         rmdir $DIR/$tdir
14540         kill -USR1 $MULTIPID
14541         wait $MULTIPID
14542         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
14543         return 0
14544 }
14545 run_test 181 "Test open-unlinked dir ========================"
14546
14547 test_182() {
14548         local fcount=1000
14549         local tcount=10
14550
14551         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14552
14553         $LCTL set_param mdc.*.rpc_stats=clear
14554
14555         for (( i = 0; i < $tcount; i++ )) ; do
14556                 mkdir $DIR/$tdir/$i
14557         done
14558
14559         for (( i = 0; i < $tcount; i++ )) ; do
14560                 createmany -o $DIR/$tdir/$i/f- $fcount &
14561         done
14562         wait
14563
14564         for (( i = 0; i < $tcount; i++ )) ; do
14565                 unlinkmany $DIR/$tdir/$i/f- $fcount &
14566         done
14567         wait
14568
14569         $LCTL get_param mdc.*.rpc_stats
14570
14571         rm -rf $DIR/$tdir
14572 }
14573 run_test 182 "Test parallel modify metadata operations ================"
14574
14575 test_183() { # LU-2275
14576         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14577         remote_mds_nodsh && skip "remote MDS with nodsh"
14578         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
14579                 skip "Need MDS version at least 2.3.56"
14580
14581         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14582         echo aaa > $DIR/$tdir/$tfile
14583
14584 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
14585         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
14586
14587         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
14588         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
14589
14590         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
14591
14592         # Flush negative dentry cache
14593         touch $DIR/$tdir/$tfile
14594
14595         # We are not checking for any leaked references here, they'll
14596         # become evident next time we do cleanup with module unload.
14597         rm -rf $DIR/$tdir
14598 }
14599 run_test 183 "No crash or request leak in case of strange dispositions ========"
14600
14601 # test suite 184 is for LU-2016, LU-2017
14602 test_184a() {
14603         check_swap_layouts_support
14604
14605         dir0=$DIR/$tdir/$testnum
14606         test_mkdir -p -c1 $dir0
14607         ref1=/etc/passwd
14608         ref2=/etc/group
14609         file1=$dir0/f1
14610         file2=$dir0/f2
14611         $SETSTRIPE -c1 $file1
14612         cp $ref1 $file1
14613         $SETSTRIPE -c2 $file2
14614         cp $ref2 $file2
14615         gen1=$($GETSTRIPE -g $file1)
14616         gen2=$($GETSTRIPE -g $file2)
14617
14618         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
14619         gen=$($GETSTRIPE -g $file1)
14620         [[ $gen1 != $gen ]] ||
14621                 "Layout generation on $file1 does not change"
14622         gen=$($GETSTRIPE -g $file2)
14623         [[ $gen2 != $gen ]] ||
14624                 "Layout generation on $file2 does not change"
14625
14626         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
14627         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
14628
14629         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
14630 }
14631 run_test 184a "Basic layout swap"
14632
14633 test_184b() {
14634         check_swap_layouts_support
14635
14636         dir0=$DIR/$tdir/$testnum
14637         mkdir -p $dir0 || error "creating dir $dir0"
14638         file1=$dir0/f1
14639         file2=$dir0/f2
14640         file3=$dir0/f3
14641         dir1=$dir0/d1
14642         dir2=$dir0/d2
14643         mkdir $dir1 $dir2
14644         $SETSTRIPE -c1 $file1
14645         $SETSTRIPE -c2 $file2
14646         $SETSTRIPE -c1 $file3
14647         chown $RUNAS_ID $file3
14648         gen1=$($GETSTRIPE -g $file1)
14649         gen2=$($GETSTRIPE -g $file2)
14650
14651         $LFS swap_layouts $dir1 $dir2 &&
14652                 error "swap of directories layouts should fail"
14653         $LFS swap_layouts $dir1 $file1 &&
14654                 error "swap of directory and file layouts should fail"
14655         $RUNAS $LFS swap_layouts $file1 $file2 &&
14656                 error "swap of file we cannot write should fail"
14657         $LFS swap_layouts $file1 $file3 &&
14658                 error "swap of file with different owner should fail"
14659         /bin/true # to clear error code
14660 }
14661 run_test 184b "Forbidden layout swap (will generate errors)"
14662
14663 test_184c() {
14664         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
14665         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
14666         check_swap_layouts_support
14667
14668         local dir0=$DIR/$tdir/$testnum
14669         mkdir -p $dir0 || error "creating dir $dir0"
14670
14671         local ref1=$dir0/ref1
14672         local ref2=$dir0/ref2
14673         local file1=$dir0/file1
14674         local file2=$dir0/file2
14675         # create a file large enough for the concurrent test
14676         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
14677         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
14678         echo "ref file size: ref1($(stat -c %s $ref1))," \
14679              "ref2($(stat -c %s $ref2))"
14680
14681         cp $ref2 $file2
14682         dd if=$ref1 of=$file1 bs=16k &
14683         local DD_PID=$!
14684
14685         # Make sure dd starts to copy file
14686         while [ ! -f $file1 ]; do sleep 0.1; done
14687
14688         $LFS swap_layouts $file1 $file2
14689         local rc=$?
14690         wait $DD_PID
14691         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
14692         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
14693
14694         # how many bytes copied before swapping layout
14695         local copied=$(stat -c %s $file2)
14696         local remaining=$(stat -c %s $ref1)
14697         remaining=$((remaining - copied))
14698         echo "Copied $copied bytes before swapping layout..."
14699
14700         cmp -n $copied $file1 $ref2 | grep differ &&
14701                 error "Content mismatch [0, $copied) of ref2 and file1"
14702         cmp -n $copied $file2 $ref1 ||
14703                 error "Content mismatch [0, $copied) of ref1 and file2"
14704         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
14705                 error "Content mismatch [$copied, EOF) of ref1 and file1"
14706
14707         # clean up
14708         rm -f $ref1 $ref2 $file1 $file2
14709 }
14710 run_test 184c "Concurrent write and layout swap"
14711
14712 test_184d() {
14713         check_swap_layouts_support
14714         [ -z "$(which getfattr 2>/dev/null)" ] &&
14715                 skip_env "no getfattr command"
14716
14717         local file1=$DIR/$tdir/$tfile-1
14718         local file2=$DIR/$tdir/$tfile-2
14719         local file3=$DIR/$tdir/$tfile-3
14720         local lovea1
14721         local lovea2
14722
14723         mkdir -p $DIR/$tdir
14724         touch $file1 || error "create $file1 failed"
14725         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
14726                 error "create $file2 failed"
14727         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
14728                 error "create $file3 failed"
14729         lovea1=$(get_layout_param $file1)
14730
14731         $LFS swap_layouts $file2 $file3 ||
14732                 error "swap $file2 $file3 layouts failed"
14733         $LFS swap_layouts $file1 $file2 ||
14734                 error "swap $file1 $file2 layouts failed"
14735
14736         lovea2=$(get_layout_param $file2)
14737         echo "$lovea1"
14738         echo "$lovea2"
14739         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
14740
14741         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
14742         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
14743 }
14744 run_test 184d "allow stripeless layouts swap"
14745
14746 test_184e() {
14747         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
14748                 skip "Need MDS version at least 2.6.94"
14749         check_swap_layouts_support
14750         [ -z "$(which getfattr 2>/dev/null)" ] &&
14751                 skip_env "no getfattr command"
14752
14753         local file1=$DIR/$tdir/$tfile-1
14754         local file2=$DIR/$tdir/$tfile-2
14755         local file3=$DIR/$tdir/$tfile-3
14756         local lovea
14757
14758         mkdir -p $DIR/$tdir
14759         touch $file1 || error "create $file1 failed"
14760         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
14761                 error "create $file2 failed"
14762         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
14763                 error "create $file3 failed"
14764
14765         $LFS swap_layouts $file1 $file2 ||
14766                 error "swap $file1 $file2 layouts failed"
14767
14768         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
14769         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
14770
14771         echo 123 > $file1 || error "Should be able to write into $file1"
14772
14773         $LFS swap_layouts $file1 $file3 ||
14774                 error "swap $file1 $file3 layouts failed"
14775
14776         echo 123 > $file1 || error "Should be able to write into $file1"
14777
14778         rm -rf $file1 $file2 $file3
14779 }
14780 run_test 184e "Recreate layout after stripeless layout swaps"
14781
14782 test_184f() {
14783         # Create a file with name longer than sizeof(struct stat) ==
14784         # 144 to see if we can get chars from the file name to appear
14785         # in the returned striping. Note that 'f' == 0x66.
14786         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
14787
14788         mkdir -p $DIR/$tdir
14789         mcreate $DIR/$tdir/$file
14790         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
14791                 error "IOC_MDC_GETFILEINFO returned garbage striping"
14792         fi
14793 }
14794 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
14795
14796 test_185() { # LU-2441
14797         # LU-3553 - no volatile file support in old servers
14798         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
14799                 skip "Need MDS version at least 2.3.60"
14800
14801         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14802         touch $DIR/$tdir/spoo
14803         local mtime1=$(stat -c "%Y" $DIR/$tdir)
14804         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
14805                 error "cannot create/write a volatile file"
14806         [ "$FILESET" == "" ] &&
14807         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
14808                 error "FID is still valid after close"
14809
14810         multiop_bg_pause $DIR/$tdir vVw4096_c
14811         local multi_pid=$!
14812
14813         local OLD_IFS=$IFS
14814         IFS=":"
14815         local fidv=($fid)
14816         IFS=$OLD_IFS
14817         # assume that the next FID for this client is sequential, since stdout
14818         # is unfortunately eaten by multiop_bg_pause
14819         local n=$((${fidv[1]} + 1))
14820         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
14821         if [ "$FILESET" == "" ]; then
14822                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
14823                         error "FID is missing before close"
14824         fi
14825         kill -USR1 $multi_pid
14826         # 1 second delay, so if mtime change we will see it
14827         sleep 1
14828         local mtime2=$(stat -c "%Y" $DIR/$tdir)
14829         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
14830 }
14831 run_test 185 "Volatile file support"
14832
14833 function create_check_volatile() {
14834         local idx=$1
14835         local tgt
14836
14837         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
14838         local PID=$!
14839         sleep 1
14840         local FID=$(cat /tmp/${tfile}.fid)
14841         [ "$FID" == "" ] && error "can't get FID for volatile"
14842         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
14843         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
14844         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
14845         kill -USR1 $PID
14846         wait
14847         sleep 1
14848         cancel_lru_locks mdc # flush opencache
14849         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
14850         return 0
14851 }
14852
14853 test_185a(){
14854         # LU-12516 - volatile creation via .lustre
14855         [[ $MDS1_VERSION -ge $(version_code 2.12.2) ]] ||
14856                 skip "Need MDS version at least 2.12.2"
14857
14858         create_check_volatile 0
14859         [ $MDSCOUNT -lt 2 ] && return 0
14860
14861         # DNE case
14862         create_check_volatile 1
14863
14864         return 0
14865 }
14866 run_test 185a "Volatile file creation in .lustre/fid/"
14867
14868 test_187a() {
14869         remote_mds_nodsh && skip "remote MDS with nodsh"
14870         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
14871                 skip "Need MDS version at least 2.3.0"
14872
14873         local dir0=$DIR/$tdir/$testnum
14874         mkdir -p $dir0 || error "creating dir $dir0"
14875
14876         local file=$dir0/file1
14877         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
14878         local dv1=$($LFS data_version $file)
14879         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
14880         local dv2=$($LFS data_version $file)
14881         [[ $dv1 != $dv2 ]] ||
14882                 error "data version did not change on write $dv1 == $dv2"
14883
14884         # clean up
14885         rm -f $file1
14886 }
14887 run_test 187a "Test data version change"
14888
14889 test_187b() {
14890         remote_mds_nodsh && skip "remote MDS with nodsh"
14891         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
14892                 skip "Need MDS version at least 2.3.0"
14893
14894         local dir0=$DIR/$tdir/$testnum
14895         mkdir -p $dir0 || error "creating dir $dir0"
14896
14897         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
14898         [[ ${DV[0]} != ${DV[1]} ]] ||
14899                 error "data version did not change on write"\
14900                       " ${DV[0]} == ${DV[1]}"
14901
14902         # clean up
14903         rm -f $file1
14904 }
14905 run_test 187b "Test data version change on volatile file"
14906
14907 test_200() {
14908         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14909         remote_mgs_nodsh && skip "remote MGS with nodsh"
14910         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
14911
14912         local POOL=${POOL:-cea1}
14913         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
14914         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
14915         # Pool OST targets
14916         local first_ost=0
14917         local last_ost=$(($OSTCOUNT - 1))
14918         local ost_step=2
14919         local ost_list=$(seq $first_ost $ost_step $last_ost)
14920         local ost_range="$first_ost $last_ost $ost_step"
14921         local test_path=$POOL_ROOT/$POOL_DIR_NAME
14922         local file_dir=$POOL_ROOT/file_tst
14923         local subdir=$test_path/subdir
14924         local rc=0
14925
14926         while : ; do
14927                 # former test_200a test_200b
14928                 pool_add $POOL                          || { rc=$? ; break; }
14929                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
14930                 # former test_200c test_200d
14931                 mkdir -p $test_path
14932                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
14933                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
14934                 mkdir -p $subdir
14935                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
14936                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
14937                                                         || { rc=$? ; break; }
14938                 # former test_200e test_200f
14939                 local files=$((OSTCOUNT*3))
14940                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
14941                                                         || { rc=$? ; break; }
14942                 pool_create_files $POOL $file_dir $files "$ost_list" \
14943                                                         || { rc=$? ; break; }
14944                 # former test_200g test_200h
14945                 pool_lfs_df $POOL                       || { rc=$? ; break; }
14946                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
14947
14948                 # former test_201a test_201b test_201c
14949                 pool_remove_first_target $POOL          || { rc=$? ; break; }
14950
14951                 local f=$test_path/$tfile
14952                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
14953                 pool_remove $POOL $f                    || { rc=$? ; break; }
14954                 break
14955         done
14956
14957         destroy_test_pools
14958
14959         return $rc
14960 }
14961 run_test 200 "OST pools"
14962
14963 # usage: default_attr <count | size | offset>
14964 default_attr() {
14965         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
14966 }
14967
14968 # usage: check_default_stripe_attr
14969 check_default_stripe_attr() {
14970         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
14971         case $1 in
14972         --stripe-count|-c)
14973                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
14974         --stripe-size|-S)
14975                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
14976         --stripe-index|-i)
14977                 EXPECTED=-1;;
14978         *)
14979                 error "unknown getstripe attr '$1'"
14980         esac
14981
14982         [ $ACTUAL == $EXPECTED ] ||
14983                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
14984 }
14985
14986 test_204a() {
14987         test_mkdir $DIR/$tdir
14988         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
14989
14990         check_default_stripe_attr --stripe-count
14991         check_default_stripe_attr --stripe-size
14992         check_default_stripe_attr --stripe-index
14993 }
14994 run_test 204a "Print default stripe attributes"
14995
14996 test_204b() {
14997         test_mkdir $DIR/$tdir
14998         $SETSTRIPE --stripe-count 1 $DIR/$tdir
14999
15000         check_default_stripe_attr --stripe-size
15001         check_default_stripe_attr --stripe-index
15002 }
15003 run_test 204b "Print default stripe size and offset"
15004
15005 test_204c() {
15006         test_mkdir $DIR/$tdir
15007         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
15008
15009         check_default_stripe_attr --stripe-count
15010         check_default_stripe_attr --stripe-index
15011 }
15012 run_test 204c "Print default stripe count and offset"
15013
15014 test_204d() {
15015         test_mkdir $DIR/$tdir
15016         $SETSTRIPE --stripe-index 0 $DIR/$tdir
15017
15018         check_default_stripe_attr --stripe-count
15019         check_default_stripe_attr --stripe-size
15020 }
15021 run_test 204d "Print default stripe count and size"
15022
15023 test_204e() {
15024         test_mkdir $DIR/$tdir
15025         $SETSTRIPE -d $DIR/$tdir
15026
15027         check_default_stripe_attr --stripe-count --raw
15028         check_default_stripe_attr --stripe-size --raw
15029         check_default_stripe_attr --stripe-index --raw
15030 }
15031 run_test 204e "Print raw stripe attributes"
15032
15033 test_204f() {
15034         test_mkdir $DIR/$tdir
15035         $SETSTRIPE --stripe-count 1 $DIR/$tdir
15036
15037         check_default_stripe_attr --stripe-size --raw
15038         check_default_stripe_attr --stripe-index --raw
15039 }
15040 run_test 204f "Print raw stripe size and offset"
15041
15042 test_204g() {
15043         test_mkdir $DIR/$tdir
15044         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
15045
15046         check_default_stripe_attr --stripe-count --raw
15047         check_default_stripe_attr --stripe-index --raw
15048 }
15049 run_test 204g "Print raw stripe count and offset"
15050
15051 test_204h() {
15052         test_mkdir $DIR/$tdir
15053         $SETSTRIPE --stripe-index 0 $DIR/$tdir
15054
15055         check_default_stripe_attr --stripe-count --raw
15056         check_default_stripe_attr --stripe-size --raw
15057 }
15058 run_test 204h "Print raw stripe count and size"
15059
15060 # Figure out which job scheduler is being used, if any,
15061 # or use a fake one
15062 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
15063         JOBENV=SLURM_JOB_ID
15064 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
15065         JOBENV=LSB_JOBID
15066 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
15067         JOBENV=PBS_JOBID
15068 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
15069         JOBENV=LOADL_STEP_ID
15070 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
15071         JOBENV=JOB_ID
15072 else
15073         $LCTL list_param jobid_name > /dev/null 2>&1
15074         if [ $? -eq 0 ]; then
15075                 JOBENV=nodelocal
15076         else
15077                 JOBENV=FAKE_JOBID
15078         fi
15079 fi
15080 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
15081
15082 verify_jobstats() {
15083         local cmd=($1)
15084         shift
15085         local facets="$@"
15086
15087 # we don't really need to clear the stats for this test to work, since each
15088 # command has a unique jobid, but it makes debugging easier if needed.
15089 #       for facet in $facets; do
15090 #               local dev=$(convert_facet2label $facet)
15091 #               # clear old jobstats
15092 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
15093 #       done
15094
15095         # use a new JobID for each test, or we might see an old one
15096         [ "$JOBENV" = "FAKE_JOBID" ] &&
15097                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
15098
15099         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
15100
15101         [ "$JOBENV" = "nodelocal" ] && {
15102                 FAKE_JOBID=id.$testnum.%e.$RANDOM
15103                 $LCTL set_param jobid_name=$FAKE_JOBID
15104                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
15105         }
15106
15107         log "Test: ${cmd[*]}"
15108         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
15109
15110         if [ $JOBENV = "FAKE_JOBID" ]; then
15111                 FAKE_JOBID=$JOBVAL ${cmd[*]}
15112         else
15113                 ${cmd[*]}
15114         fi
15115
15116         # all files are created on OST0000
15117         for facet in $facets; do
15118                 local stats="*.$(convert_facet2label $facet).job_stats"
15119
15120                 # strip out libtool wrappers for in-tree executables
15121                 if [ $(do_facet $facet lctl get_param $stats |
15122                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
15123                         do_facet $facet lctl get_param $stats
15124                         error "No jobstats for $JOBVAL found on $facet::$stats"
15125                 fi
15126         done
15127 }
15128
15129 jobstats_set() {
15130         local new_jobenv=$1
15131
15132         set_persistent_param_and_check client "jobid_var" \
15133                 "$FSNAME.sys.jobid_var" $new_jobenv
15134 }
15135
15136 test_205a() { # Job stats
15137         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15138         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
15139                 skip "Need MDS version with at least 2.7.1"
15140         remote_mgs_nodsh && skip "remote MGS with nodsh"
15141         remote_mds_nodsh && skip "remote MDS with nodsh"
15142         remote_ost_nodsh && skip "remote OST with nodsh"
15143         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
15144                 skip "Server doesn't support jobstats"
15145         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
15146
15147         local old_jobenv=$($LCTL get_param -n jobid_var)
15148         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
15149
15150         if [[ $PERM_CMD = *"set_param -P"* ]]; then
15151                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
15152         else
15153                 stack_trap "do_facet mgs $PERM_CMD \
15154                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
15155         fi
15156         changelog_register
15157
15158         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
15159                                 mdt.*.job_cleanup_interval | head -n 1)
15160         local new_interval=5
15161         do_facet $SINGLEMDS \
15162                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
15163         stack_trap "do_facet $SINGLEMDS \
15164                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
15165         local start=$SECONDS
15166
15167         local cmd
15168         # mkdir
15169         cmd="mkdir $DIR/$tdir"
15170         verify_jobstats "$cmd" "$SINGLEMDS"
15171         # rmdir
15172         cmd="rmdir $DIR/$tdir"
15173         verify_jobstats "$cmd" "$SINGLEMDS"
15174         # mkdir on secondary MDT
15175         if [ $MDSCOUNT -gt 1 ]; then
15176                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
15177                 verify_jobstats "$cmd" "mds2"
15178         fi
15179         # mknod
15180         cmd="mknod $DIR/$tfile c 1 3"
15181         verify_jobstats "$cmd" "$SINGLEMDS"
15182         # unlink
15183         cmd="rm -f $DIR/$tfile"
15184         verify_jobstats "$cmd" "$SINGLEMDS"
15185         # create all files on OST0000 so verify_jobstats can find OST stats
15186         # open & close
15187         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
15188         verify_jobstats "$cmd" "$SINGLEMDS"
15189         # setattr
15190         cmd="touch $DIR/$tfile"
15191         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15192         # write
15193         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
15194         verify_jobstats "$cmd" "ost1"
15195         # read
15196         cancel_lru_locks osc
15197         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
15198         verify_jobstats "$cmd" "ost1"
15199         # truncate
15200         cmd="$TRUNCATE $DIR/$tfile 0"
15201         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15202         # rename
15203         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
15204         verify_jobstats "$cmd" "$SINGLEMDS"
15205         # jobstats expiry - sleep until old stats should be expired
15206         local left=$((new_interval + 5 - (SECONDS - start)))
15207         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
15208                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
15209                         "0" $left
15210         cmd="mkdir $DIR/$tdir.expire"
15211         verify_jobstats "$cmd" "$SINGLEMDS"
15212         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
15213             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
15214
15215         # Ensure that jobid are present in changelog (if supported by MDS)
15216         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
15217                 changelog_dump | tail -10
15218                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
15219                 [ $jobids -eq 9 ] ||
15220                         error "Wrong changelog jobid count $jobids != 9"
15221
15222                 # LU-5862
15223                 JOBENV="disable"
15224                 jobstats_set $JOBENV
15225                 touch $DIR/$tfile
15226                 changelog_dump | grep $tfile
15227                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
15228                 [ $jobids -eq 0 ] ||
15229                         error "Unexpected jobids when jobid_var=$JOBENV"
15230         fi
15231
15232         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
15233         JOBENV="JOBCOMPLEX"
15234         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
15235
15236         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
15237 }
15238 run_test 205a "Verify job stats"
15239
15240 # LU-13117
15241 test_205b() {
15242         $LCTL set_param jobid_var=USER jobid_name="%e.%u"
15243         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
15244         do_facet $SINGLEMDS $LCTL get_param mdt.*.job_stats |
15245                 grep job_id: | grep foolish &&
15246                         error "Unexpected jobid found"
15247         true
15248 }
15249 run_test 205b "Verify job stats jobid parsing"
15250
15251 # LU-1480, LU-1773 and LU-1657
15252 test_206() {
15253         mkdir -p $DIR/$tdir
15254         $SETSTRIPE -c -1 $DIR/$tdir
15255 #define OBD_FAIL_LOV_INIT 0x1403
15256         $LCTL set_param fail_loc=0xa0001403
15257         $LCTL set_param fail_val=1
15258         touch $DIR/$tdir/$tfile || true
15259 }
15260 run_test 206 "fail lov_init_raid0() doesn't lbug"
15261
15262 test_207a() {
15263         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15264         local fsz=`stat -c %s $DIR/$tfile`
15265         cancel_lru_locks mdc
15266
15267         # do not return layout in getattr intent
15268 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
15269         $LCTL set_param fail_loc=0x170
15270         local sz=`stat -c %s $DIR/$tfile`
15271
15272         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
15273
15274         rm -rf $DIR/$tfile
15275 }
15276 run_test 207a "can refresh layout at glimpse"
15277
15278 test_207b() {
15279         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15280         local cksum=`md5sum $DIR/$tfile`
15281         local fsz=`stat -c %s $DIR/$tfile`
15282         cancel_lru_locks mdc
15283         cancel_lru_locks osc
15284
15285         # do not return layout in getattr intent
15286 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
15287         $LCTL set_param fail_loc=0x171
15288
15289         # it will refresh layout after the file is opened but before read issues
15290         echo checksum is "$cksum"
15291         echo "$cksum" |md5sum -c --quiet || error "file differs"
15292
15293         rm -rf $DIR/$tfile
15294 }
15295 run_test 207b "can refresh layout at open"
15296
15297 test_208() {
15298         # FIXME: in this test suite, only RD lease is used. This is okay
15299         # for now as only exclusive open is supported. After generic lease
15300         # is done, this test suite should be revised. - Jinshan
15301
15302         remote_mds_nodsh && skip "remote MDS with nodsh"
15303         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
15304                 skip "Need MDS version at least 2.4.52"
15305
15306         echo "==== test 1: verify get lease work"
15307         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
15308
15309         echo "==== test 2: verify lease can be broken by upcoming open"
15310         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15311         local PID=$!
15312         sleep 1
15313
15314         $MULTIOP $DIR/$tfile oO_RDONLY:c
15315         kill -USR1 $PID && wait $PID || error "break lease error"
15316
15317         echo "==== test 3: verify lease can't be granted if an open already exists"
15318         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
15319         local PID=$!
15320         sleep 1
15321
15322         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
15323         kill -USR1 $PID && wait $PID || error "open file error"
15324
15325         echo "==== test 4: lease can sustain over recovery"
15326         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
15327         PID=$!
15328         sleep 1
15329
15330         fail mds1
15331
15332         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
15333
15334         echo "==== test 5: lease broken can't be regained by replay"
15335         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15336         PID=$!
15337         sleep 1
15338
15339         # open file to break lease and then recovery
15340         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
15341         fail mds1
15342
15343         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
15344
15345         rm -f $DIR/$tfile
15346 }
15347 run_test 208 "Exclusive open"
15348
15349 test_209() {
15350         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
15351                 skip_env "must have disp_stripe"
15352
15353         touch $DIR/$tfile
15354         sync; sleep 5; sync;
15355
15356         echo 3 > /proc/sys/vm/drop_caches
15357         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15358
15359         # open/close 500 times
15360         for i in $(seq 500); do
15361                 cat $DIR/$tfile
15362         done
15363
15364         echo 3 > /proc/sys/vm/drop_caches
15365         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15366
15367         echo "before: $req_before, after: $req_after"
15368         [ $((req_after - req_before)) -ge 300 ] &&
15369                 error "open/close requests are not freed"
15370         return 0
15371 }
15372 run_test 209 "read-only open/close requests should be freed promptly"
15373
15374 test_212() {
15375         size=`date +%s`
15376         size=$((size % 8192 + 1))
15377         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
15378         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
15379         rm -f $DIR/f212 $DIR/f212.xyz
15380 }
15381 run_test 212 "Sendfile test ============================================"
15382
15383 test_213() {
15384         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
15385         cancel_lru_locks osc
15386         lctl set_param fail_loc=0x8000040f
15387         # generate a read lock
15388         cat $DIR/$tfile > /dev/null
15389         # write to the file, it will try to cancel the above read lock.
15390         cat /etc/hosts >> $DIR/$tfile
15391 }
15392 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
15393
15394 test_214() { # for bug 20133
15395         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
15396         for (( i=0; i < 340; i++ )) ; do
15397                 touch $DIR/$tdir/d214c/a$i
15398         done
15399
15400         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
15401         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
15402         ls $DIR/d214c || error "ls $DIR/d214c failed"
15403         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
15404         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
15405 }
15406 run_test 214 "hash-indexed directory test - bug 20133"
15407
15408 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
15409 create_lnet_proc_files() {
15410         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
15411 }
15412
15413 # counterpart of create_lnet_proc_files
15414 remove_lnet_proc_files() {
15415         rm -f $TMP/lnet_$1.sys
15416 }
15417
15418 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15419 # 3rd arg as regexp for body
15420 check_lnet_proc_stats() {
15421         local l=$(cat "$TMP/lnet_$1" |wc -l)
15422         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
15423
15424         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
15425 }
15426
15427 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15428 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
15429 # optional and can be regexp for 2nd line (lnet.routes case)
15430 check_lnet_proc_entry() {
15431         local blp=2          # blp stands for 'position of 1st line of body'
15432         [ -z "$5" ] || blp=3 # lnet.routes case
15433
15434         local l=$(cat "$TMP/lnet_$1" |wc -l)
15435         # subtracting one from $blp because the body can be empty
15436         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
15437
15438         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
15439                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
15440
15441         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
15442                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
15443
15444         # bail out if any unexpected line happened
15445         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
15446         [ "$?" != 0 ] || error "$2 misformatted"
15447 }
15448
15449 test_215() { # for bugs 18102, 21079, 21517
15450         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15451
15452         local N='(0|[1-9][0-9]*)'       # non-negative numeric
15453         local P='[1-9][0-9]*'           # positive numeric
15454         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
15455         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
15456         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
15457         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
15458
15459         local L1 # regexp for 1st line
15460         local L2 # regexp for 2nd line (optional)
15461         local BR # regexp for the rest (body)
15462
15463         # lnet.stats should look as 11 space-separated non-negative numerics
15464         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
15465         create_lnet_proc_files "stats"
15466         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
15467         remove_lnet_proc_files "stats"
15468
15469         # lnet.routes should look like this:
15470         # Routing disabled/enabled
15471         # net hops priority state router
15472         # where net is a string like tcp0, hops > 0, priority >= 0,
15473         # state is up/down,
15474         # router is a string like 192.168.1.1@tcp2
15475         L1="^Routing (disabled|enabled)$"
15476         L2="^net +hops +priority +state +router$"
15477         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
15478         create_lnet_proc_files "routes"
15479         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
15480         remove_lnet_proc_files "routes"
15481
15482         # lnet.routers should look like this:
15483         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
15484         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
15485         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
15486         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
15487         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
15488         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
15489         create_lnet_proc_files "routers"
15490         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
15491         remove_lnet_proc_files "routers"
15492
15493         # lnet.peers should look like this:
15494         # nid refs state last max rtr min tx min queue
15495         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
15496         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
15497         # numeric (0 or >0 or <0), queue >= 0.
15498         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
15499         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
15500         create_lnet_proc_files "peers"
15501         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
15502         remove_lnet_proc_files "peers"
15503
15504         # lnet.buffers  should look like this:
15505         # pages count credits min
15506         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
15507         L1="^pages +count +credits +min$"
15508         BR="^ +$N +$N +$I +$I$"
15509         create_lnet_proc_files "buffers"
15510         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
15511         remove_lnet_proc_files "buffers"
15512
15513         # lnet.nis should look like this:
15514         # nid status alive refs peer rtr max tx min
15515         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
15516         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
15517         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
15518         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
15519         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
15520         create_lnet_proc_files "nis"
15521         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
15522         remove_lnet_proc_files "nis"
15523
15524         # can we successfully write to lnet.stats?
15525         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
15526 }
15527 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
15528
15529 test_216() { # bug 20317
15530         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15531         remote_ost_nodsh && skip "remote OST with nodsh"
15532
15533         local node
15534         local facets=$(get_facets OST)
15535         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15536
15537         save_lustre_params client "osc.*.contention_seconds" > $p
15538         save_lustre_params $facets \
15539                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
15540         save_lustre_params $facets \
15541                 "ldlm.namespaces.filter-*.contended_locks" >> $p
15542         save_lustre_params $facets \
15543                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
15544         clear_stats osc.*.osc_stats
15545
15546         # agressive lockless i/o settings
15547         do_nodes $(comma_list $(osts_nodes)) \
15548                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
15549                         ldlm.namespaces.filter-*.contended_locks=0 \
15550                         ldlm.namespaces.filter-*.contention_seconds=60"
15551         lctl set_param -n osc.*.contention_seconds=60
15552
15553         $DIRECTIO write $DIR/$tfile 0 10 4096
15554         $CHECKSTAT -s 40960 $DIR/$tfile
15555
15556         # disable lockless i/o
15557         do_nodes $(comma_list $(osts_nodes)) \
15558                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
15559                         ldlm.namespaces.filter-*.contended_locks=32 \
15560                         ldlm.namespaces.filter-*.contention_seconds=0"
15561         lctl set_param -n osc.*.contention_seconds=0
15562         clear_stats osc.*.osc_stats
15563
15564         dd if=/dev/zero of=$DIR/$tfile count=0
15565         $CHECKSTAT -s 0 $DIR/$tfile
15566
15567         restore_lustre_params <$p
15568         rm -f $p
15569         rm $DIR/$tfile
15570 }
15571 run_test 216 "check lockless direct write updates file size and kms correctly"
15572
15573 test_217() { # bug 22430
15574         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15575
15576         local node
15577         local nid
15578
15579         for node in $(nodes_list); do
15580                 nid=$(host_nids_address $node $NETTYPE)
15581                 if [[ $nid = *-* ]] ; then
15582                         echo "lctl ping $(h2nettype $nid)"
15583                         lctl ping $(h2nettype $nid)
15584                 else
15585                         echo "skipping $node (no hyphen detected)"
15586                 fi
15587         done
15588 }
15589 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
15590
15591 test_218() {
15592        # do directio so as not to populate the page cache
15593        log "creating a 10 Mb file"
15594        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
15595        log "starting reads"
15596        dd if=$DIR/$tfile of=/dev/null bs=4096 &
15597        log "truncating the file"
15598        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
15599        log "killing dd"
15600        kill %+ || true # reads might have finished
15601        echo "wait until dd is finished"
15602        wait
15603        log "removing the temporary file"
15604        rm -rf $DIR/$tfile || error "tmp file removal failed"
15605 }
15606 run_test 218 "parallel read and truncate should not deadlock"
15607
15608 test_219() {
15609         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15610
15611         # write one partial page
15612         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
15613         # set no grant so vvp_io_commit_write will do sync write
15614         $LCTL set_param fail_loc=0x411
15615         # write a full page at the end of file
15616         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
15617
15618         $LCTL set_param fail_loc=0
15619         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
15620         $LCTL set_param fail_loc=0x411
15621         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
15622
15623         # LU-4201
15624         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
15625         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
15626 }
15627 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
15628
15629 test_220() { #LU-325
15630         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15631         remote_ost_nodsh && skip "remote OST with nodsh"
15632         remote_mds_nodsh && skip "remote MDS with nodsh"
15633         remote_mgs_nodsh && skip "remote MGS with nodsh"
15634
15635         local OSTIDX=0
15636
15637         # create on MDT0000 so the last_id and next_id are correct
15638         mkdir $DIR/$tdir
15639         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
15640         OST=${OST%_UUID}
15641
15642         # on the mdt's osc
15643         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
15644         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
15645                         osp.$mdtosc_proc1.prealloc_last_id)
15646         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
15647                         osp.$mdtosc_proc1.prealloc_next_id)
15648
15649         $LFS df -i
15650
15651         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
15652         #define OBD_FAIL_OST_ENOINO              0x229
15653         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
15654         create_pool $FSNAME.$TESTNAME || return 1
15655         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
15656
15657         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
15658
15659         MDSOBJS=$((last_id - next_id))
15660         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
15661
15662         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
15663         echo "OST still has $count kbytes free"
15664
15665         echo "create $MDSOBJS files @next_id..."
15666         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
15667
15668         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
15669                         osp.$mdtosc_proc1.prealloc_last_id)
15670         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
15671                         osp.$mdtosc_proc1.prealloc_next_id)
15672
15673         echo "after creation, last_id=$last_id2, next_id=$next_id2"
15674         $LFS df -i
15675
15676         echo "cleanup..."
15677
15678         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
15679         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
15680
15681         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
15682                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
15683         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
15684                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
15685         echo "unlink $MDSOBJS files @$next_id..."
15686         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
15687 }
15688 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
15689
15690 test_221() {
15691         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15692
15693         dd if=`which date` of=$MOUNT/date oflag=sync
15694         chmod +x $MOUNT/date
15695
15696         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
15697         $LCTL set_param fail_loc=0x80001401
15698
15699         $MOUNT/date > /dev/null
15700         rm -f $MOUNT/date
15701 }
15702 run_test 221 "make sure fault and truncate race to not cause OOM"
15703
15704 test_222a () {
15705         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15706
15707         rm -rf $DIR/$tdir
15708         test_mkdir $DIR/$tdir
15709         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15710         createmany -o $DIR/$tdir/$tfile 10
15711         cancel_lru_locks mdc
15712         cancel_lru_locks osc
15713         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
15714         $LCTL set_param fail_loc=0x31a
15715         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
15716         $LCTL set_param fail_loc=0
15717         rm -r $DIR/$tdir
15718 }
15719 run_test 222a "AGL for ls should not trigger CLIO lock failure"
15720
15721 test_222b () {
15722         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15723
15724         rm -rf $DIR/$tdir
15725         test_mkdir $DIR/$tdir
15726         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15727         createmany -o $DIR/$tdir/$tfile 10
15728         cancel_lru_locks mdc
15729         cancel_lru_locks osc
15730         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
15731         $LCTL set_param fail_loc=0x31a
15732         rm -r $DIR/$tdir || error "AGL for rmdir failed"
15733         $LCTL set_param fail_loc=0
15734 }
15735 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
15736
15737 test_223 () {
15738         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15739
15740         rm -rf $DIR/$tdir
15741         test_mkdir $DIR/$tdir
15742         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15743         createmany -o $DIR/$tdir/$tfile 10
15744         cancel_lru_locks mdc
15745         cancel_lru_locks osc
15746         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
15747         $LCTL set_param fail_loc=0x31b
15748         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
15749         $LCTL set_param fail_loc=0
15750         rm -r $DIR/$tdir
15751 }
15752 run_test 223 "osc reenqueue if without AGL lock granted ======================="
15753
15754 test_224a() { # LU-1039, MRP-303
15755         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15756
15757         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
15758         $LCTL set_param fail_loc=0x508
15759         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
15760         $LCTL set_param fail_loc=0
15761         df $DIR
15762 }
15763 run_test 224a "Don't panic on bulk IO failure"
15764
15765 test_224b() { # LU-1039, MRP-303
15766         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15767
15768         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
15769         cancel_lru_locks osc
15770         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
15771         $LCTL set_param fail_loc=0x515
15772         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
15773         $LCTL set_param fail_loc=0
15774         df $DIR
15775 }
15776 run_test 224b "Don't panic on bulk IO failure"
15777
15778 test_224c() { # LU-6441
15779         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15780         remote_mds_nodsh && skip "remote MDS with nodsh"
15781
15782         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15783         save_writethrough $p
15784         set_cache writethrough on
15785
15786         local pages_per_rpc=$($LCTL get_param \
15787                                 osc.*.max_pages_per_rpc)
15788         local at_max=$($LCTL get_param -n at_max)
15789         local timeout=$($LCTL get_param -n timeout)
15790         local test_at="at_max"
15791         local param_at="$FSNAME.sys.at_max"
15792         local test_timeout="timeout"
15793         local param_timeout="$FSNAME.sys.timeout"
15794
15795         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
15796
15797         set_persistent_param_and_check client "$test_at" "$param_at" 0
15798         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
15799
15800         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
15801         do_facet ost1 $LCTL set_param fail_loc=0x520
15802         $LFS setstripe -c 1 -i 0 $DIR/$tfile
15803         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
15804         sync
15805         do_facet ost1 $LCTL set_param fail_loc=0
15806
15807         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
15808         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
15809                 $timeout
15810
15811         $LCTL set_param -n $pages_per_rpc
15812         restore_lustre_params < $p
15813         rm -f $p
15814 }
15815 run_test 224c "Don't hang if one of md lost during large bulk RPC"
15816
15817 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
15818 test_225a () {
15819         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15820         if [ -z ${MDSSURVEY} ]; then
15821                 skip_env "mds-survey not found"
15822         fi
15823         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
15824                 skip "Need MDS version at least 2.2.51"
15825
15826         local mds=$(facet_host $SINGLEMDS)
15827         local target=$(do_nodes $mds 'lctl dl' |
15828                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
15829
15830         local cmd1="file_count=1000 thrhi=4"
15831         local cmd2="dir_count=2 layer=mdd stripe_count=0"
15832         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
15833         local cmd="$cmd1 $cmd2 $cmd3"
15834
15835         rm -f ${TMP}/mds_survey*
15836         echo + $cmd
15837         eval $cmd || error "mds-survey with zero-stripe failed"
15838         cat ${TMP}/mds_survey*
15839         rm -f ${TMP}/mds_survey*
15840 }
15841 run_test 225a "Metadata survey sanity with zero-stripe"
15842
15843 test_225b () {
15844         if [ -z ${MDSSURVEY} ]; then
15845                 skip_env "mds-survey not found"
15846         fi
15847         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
15848                 skip "Need MDS version at least 2.2.51"
15849         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15850         remote_mds_nodsh && skip "remote MDS with nodsh"
15851         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
15852                 skip_env "Need to mount OST to test"
15853         fi
15854
15855         local mds=$(facet_host $SINGLEMDS)
15856         local target=$(do_nodes $mds 'lctl dl' |
15857                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
15858
15859         local cmd1="file_count=1000 thrhi=4"
15860         local cmd2="dir_count=2 layer=mdd stripe_count=1"
15861         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
15862         local cmd="$cmd1 $cmd2 $cmd3"
15863
15864         rm -f ${TMP}/mds_survey*
15865         echo + $cmd
15866         eval $cmd || error "mds-survey with stripe_count failed"
15867         cat ${TMP}/mds_survey*
15868         rm -f ${TMP}/mds_survey*
15869 }
15870 run_test 225b "Metadata survey sanity with stripe_count = 1"
15871
15872 mcreate_path2fid () {
15873         local mode=$1
15874         local major=$2
15875         local minor=$3
15876         local name=$4
15877         local desc=$5
15878         local path=$DIR/$tdir/$name
15879         local fid
15880         local rc
15881         local fid_path
15882
15883         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
15884                 error "cannot create $desc"
15885
15886         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
15887         rc=$?
15888         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
15889
15890         fid_path=$($LFS fid2path $MOUNT $fid)
15891         rc=$?
15892         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
15893
15894         [ "$path" == "$fid_path" ] ||
15895                 error "fid2path returned $fid_path, expected $path"
15896
15897         echo "pass with $path and $fid"
15898 }
15899
15900 test_226a () {
15901         rm -rf $DIR/$tdir
15902         mkdir -p $DIR/$tdir
15903
15904         mcreate_path2fid 0010666 0 0 fifo "FIFO"
15905         mcreate_path2fid 0020666 1 3 null "character special file (null)"
15906         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
15907         mcreate_path2fid 0040666 0 0 dir "directory"
15908         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
15909         mcreate_path2fid 0100666 0 0 file "regular file"
15910         mcreate_path2fid 0120666 0 0 link "symbolic link"
15911         mcreate_path2fid 0140666 0 0 sock "socket"
15912 }
15913 run_test 226a "call path2fid and fid2path on files of all type"
15914
15915 test_226b () {
15916         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15917
15918         local MDTIDX=1
15919
15920         rm -rf $DIR/$tdir
15921         mkdir -p $DIR/$tdir
15922         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
15923                 error "create remote directory failed"
15924         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
15925         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
15926                                 "character special file (null)"
15927         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
15928                                 "character special file (no device)"
15929         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
15930         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
15931                                 "block special file (loop)"
15932         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
15933         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
15934         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
15935 }
15936 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
15937
15938 # LU-1299 Executing or running ldd on a truncated executable does not
15939 # cause an out-of-memory condition.
15940 test_227() {
15941         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15942         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
15943
15944         dd if=$(which date) of=$MOUNT/date bs=1k count=1
15945         chmod +x $MOUNT/date
15946
15947         $MOUNT/date > /dev/null
15948         ldd $MOUNT/date > /dev/null
15949         rm -f $MOUNT/date
15950 }
15951 run_test 227 "running truncated executable does not cause OOM"
15952
15953 # LU-1512 try to reuse idle OI blocks
15954 test_228a() {
15955         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15956         remote_mds_nodsh && skip "remote MDS with nodsh"
15957         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
15958
15959         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
15960         local myDIR=$DIR/$tdir
15961
15962         mkdir -p $myDIR
15963         #define OBD_FAIL_SEQ_EXHAUST             0x1002
15964         $LCTL set_param fail_loc=0x80001002
15965         createmany -o $myDIR/t- 10000
15966         $LCTL set_param fail_loc=0
15967         # The guard is current the largest FID holder
15968         touch $myDIR/guard
15969         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
15970                     tr -d '[')
15971         local IDX=$(($SEQ % 64))
15972
15973         do_facet $SINGLEMDS sync
15974         # Make sure journal flushed.
15975         sleep 6
15976         local blk1=$(do_facet $SINGLEMDS \
15977                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15978                      grep Blockcount | awk '{print $4}')
15979
15980         # Remove old files, some OI blocks will become idle.
15981         unlinkmany $myDIR/t- 10000
15982         # Create new files, idle OI blocks should be reused.
15983         createmany -o $myDIR/t- 2000
15984         do_facet $SINGLEMDS sync
15985         # Make sure journal flushed.
15986         sleep 6
15987         local blk2=$(do_facet $SINGLEMDS \
15988                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15989                      grep Blockcount | awk '{print $4}')
15990
15991         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
15992 }
15993 run_test 228a "try to reuse idle OI blocks"
15994
15995 test_228b() {
15996         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15997         remote_mds_nodsh && skip "remote MDS with nodsh"
15998         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
15999
16000         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16001         local myDIR=$DIR/$tdir
16002
16003         mkdir -p $myDIR
16004         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16005         $LCTL set_param fail_loc=0x80001002
16006         createmany -o $myDIR/t- 10000
16007         $LCTL set_param fail_loc=0
16008         # The guard is current the largest FID holder
16009         touch $myDIR/guard
16010         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16011                     tr -d '[')
16012         local IDX=$(($SEQ % 64))
16013
16014         do_facet $SINGLEMDS sync
16015         # Make sure journal flushed.
16016         sleep 6
16017         local blk1=$(do_facet $SINGLEMDS \
16018                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16019                      grep Blockcount | awk '{print $4}')
16020
16021         # Remove old files, some OI blocks will become idle.
16022         unlinkmany $myDIR/t- 10000
16023
16024         # stop the MDT
16025         stop $SINGLEMDS || error "Fail to stop MDT."
16026         # remount the MDT
16027         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
16028
16029         df $MOUNT || error "Fail to df."
16030         # Create new files, idle OI blocks should be reused.
16031         createmany -o $myDIR/t- 2000
16032         do_facet $SINGLEMDS sync
16033         # Make sure journal flushed.
16034         sleep 6
16035         local blk2=$(do_facet $SINGLEMDS \
16036                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16037                      grep Blockcount | awk '{print $4}')
16038
16039         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16040 }
16041 run_test 228b "idle OI blocks can be reused after MDT restart"
16042
16043 #LU-1881
16044 test_228c() {
16045         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16046         remote_mds_nodsh && skip "remote MDS with nodsh"
16047         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16048
16049         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16050         local myDIR=$DIR/$tdir
16051
16052         mkdir -p $myDIR
16053         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16054         $LCTL set_param fail_loc=0x80001002
16055         # 20000 files can guarantee there are index nodes in the OI file
16056         createmany -o $myDIR/t- 20000
16057         $LCTL set_param fail_loc=0
16058         # The guard is current the largest FID holder
16059         touch $myDIR/guard
16060         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16061                     tr -d '[')
16062         local IDX=$(($SEQ % 64))
16063
16064         do_facet $SINGLEMDS sync
16065         # Make sure journal flushed.
16066         sleep 6
16067         local blk1=$(do_facet $SINGLEMDS \
16068                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16069                      grep Blockcount | awk '{print $4}')
16070
16071         # Remove old files, some OI blocks will become idle.
16072         unlinkmany $myDIR/t- 20000
16073         rm -f $myDIR/guard
16074         # The OI file should become empty now
16075
16076         # Create new files, idle OI blocks should be reused.
16077         createmany -o $myDIR/t- 2000
16078         do_facet $SINGLEMDS sync
16079         # Make sure journal flushed.
16080         sleep 6
16081         local blk2=$(do_facet $SINGLEMDS \
16082                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16083                      grep Blockcount | awk '{print $4}')
16084
16085         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16086 }
16087 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
16088
16089 test_229() { # LU-2482, LU-3448
16090         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16091         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
16092         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
16093                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
16094
16095         rm -f $DIR/$tfile
16096
16097         # Create a file with a released layout and stripe count 2.
16098         $MULTIOP $DIR/$tfile H2c ||
16099                 error "failed to create file with released layout"
16100
16101         $GETSTRIPE -v $DIR/$tfile
16102
16103         local pattern=$($GETSTRIPE -L $DIR/$tfile)
16104         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
16105
16106         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
16107         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
16108         stat $DIR/$tfile || error "failed to stat released file"
16109
16110         chown $RUNAS_ID $DIR/$tfile ||
16111                 error "chown $RUNAS_ID $DIR/$tfile failed"
16112
16113         chgrp $RUNAS_ID $DIR/$tfile ||
16114                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
16115
16116         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
16117         rm $DIR/$tfile || error "failed to remove released file"
16118 }
16119 run_test 229 "getstripe/stat/rm/attr changes work on released files"
16120
16121 test_230a() {
16122         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16123         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16124         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16125                 skip "Need MDS version at least 2.11.52"
16126
16127         local MDTIDX=1
16128
16129         test_mkdir $DIR/$tdir
16130         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
16131         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
16132         [ $mdt_idx -ne 0 ] &&
16133                 error "create local directory on wrong MDT $mdt_idx"
16134
16135         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
16136                         error "create remote directory failed"
16137         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
16138         [ $mdt_idx -ne $MDTIDX ] &&
16139                 error "create remote directory on wrong MDT $mdt_idx"
16140
16141         createmany -o $DIR/$tdir/test_230/t- 10 ||
16142                 error "create files on remote directory failed"
16143         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
16144         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
16145         rm -r $DIR/$tdir || error "unlink remote directory failed"
16146 }
16147 run_test 230a "Create remote directory and files under the remote directory"
16148
16149 test_230b() {
16150         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16151         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16152         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16153                 skip "Need MDS version at least 2.11.52"
16154
16155         local MDTIDX=1
16156         local mdt_index
16157         local i
16158         local file
16159         local pid
16160         local stripe_count
16161         local migrate_dir=$DIR/$tdir/migrate_dir
16162         local other_dir=$DIR/$tdir/other_dir
16163
16164         test_mkdir $DIR/$tdir
16165         test_mkdir -i0 -c1 $migrate_dir
16166         test_mkdir -i0 -c1 $other_dir
16167         for ((i=0; i<10; i++)); do
16168                 mkdir -p $migrate_dir/dir_${i}
16169                 createmany -o $migrate_dir/dir_${i}/f 10 ||
16170                         error "create files under remote dir failed $i"
16171         done
16172
16173         cp /etc/passwd $migrate_dir/$tfile
16174         cp /etc/passwd $other_dir/$tfile
16175         chattr +SAD $migrate_dir
16176         chattr +SAD $migrate_dir/$tfile
16177
16178         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16179         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16180         local old_dir_mode=$(stat -c%f $migrate_dir)
16181         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
16182
16183         mkdir -p $migrate_dir/dir_default_stripe2
16184         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
16185         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
16186
16187         mkdir -p $other_dir
16188         ln $migrate_dir/$tfile $other_dir/luna
16189         ln $migrate_dir/$tfile $migrate_dir/sofia
16190         ln $other_dir/$tfile $migrate_dir/david
16191         ln -s $migrate_dir/$tfile $other_dir/zachary
16192         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
16193         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
16194
16195         local len
16196         local lnktgt
16197
16198         # inline symlink
16199         for len in 58 59 60; do
16200                 lnktgt=$(str_repeat 'l' $len)
16201                 touch $migrate_dir/$lnktgt
16202                 ln -s $lnktgt $migrate_dir/${len}char_ln
16203         done
16204
16205         # PATH_MAX
16206         for len in 4094 4095; do
16207                 lnktgt=$(str_repeat 'l' $len)
16208                 ln -s $lnktgt $migrate_dir/${len}char_ln
16209         done
16210
16211         # NAME_MAX
16212         for len in 254 255; do
16213                 touch $migrate_dir/$(str_repeat 'l' $len)
16214         done
16215
16216         $LFS migrate -m $MDTIDX $migrate_dir ||
16217                 error "fails on migrating remote dir to MDT1"
16218
16219         echo "migratate to MDT1, then checking.."
16220         for ((i = 0; i < 10; i++)); do
16221                 for file in $(find $migrate_dir/dir_${i}); do
16222                         mdt_index=$($LFS getstripe -m $file)
16223                         # broken symlink getstripe will fail
16224                         [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
16225                                 error "$file is not on MDT${MDTIDX}"
16226                 done
16227         done
16228
16229         # the multiple link file should still in MDT0
16230         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
16231         [ $mdt_index == 0 ] ||
16232                 error "$file is not on MDT${MDTIDX}"
16233
16234         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16235         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16236                 error " expect $old_dir_flag get $new_dir_flag"
16237
16238         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16239         [ "$old_file_flag" = "$new_file_flag" ] ||
16240                 error " expect $old_file_flag get $new_file_flag"
16241
16242         local new_dir_mode=$(stat -c%f $migrate_dir)
16243         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16244                 error "expect mode $old_dir_mode get $new_dir_mode"
16245
16246         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16247         [ "$old_file_mode" = "$new_file_mode" ] ||
16248                 error "expect mode $old_file_mode get $new_file_mode"
16249
16250         diff /etc/passwd $migrate_dir/$tfile ||
16251                 error "$tfile different after migration"
16252
16253         diff /etc/passwd $other_dir/luna ||
16254                 error "luna different after migration"
16255
16256         diff /etc/passwd $migrate_dir/sofia ||
16257                 error "sofia different after migration"
16258
16259         diff /etc/passwd $migrate_dir/david ||
16260                 error "david different after migration"
16261
16262         diff /etc/passwd $other_dir/zachary ||
16263                 error "zachary different after migration"
16264
16265         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16266                 error "${tfile}_ln different after migration"
16267
16268         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16269                 error "${tfile}_ln_other different after migration"
16270
16271         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
16272         [ $stripe_count = 2 ] ||
16273                 error "dir strpe_count $d != 2 after migration."
16274
16275         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
16276         [ $stripe_count = 2 ] ||
16277                 error "file strpe_count $d != 2 after migration."
16278
16279         #migrate back to MDT0
16280         MDTIDX=0
16281
16282         $LFS migrate -m $MDTIDX $migrate_dir ||
16283                 error "fails on migrating remote dir to MDT0"
16284
16285         echo "migrate back to MDT0, checking.."
16286         for file in $(find $migrate_dir); do
16287                 mdt_index=$($LFS getstripe -m $file)
16288                 [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
16289                         error "$file is not on MDT${MDTIDX}"
16290         done
16291
16292         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16293         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16294                 error " expect $old_dir_flag get $new_dir_flag"
16295
16296         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16297         [ "$old_file_flag" = "$new_file_flag" ] ||
16298                 error " expect $old_file_flag get $new_file_flag"
16299
16300         local new_dir_mode=$(stat -c%f $migrate_dir)
16301         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16302                 error "expect mode $old_dir_mode get $new_dir_mode"
16303
16304         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16305         [ "$old_file_mode" = "$new_file_mode" ] ||
16306                 error "expect mode $old_file_mode get $new_file_mode"
16307
16308         diff /etc/passwd ${migrate_dir}/$tfile ||
16309                 error "$tfile different after migration"
16310
16311         diff /etc/passwd ${other_dir}/luna ||
16312                 error "luna different after migration"
16313
16314         diff /etc/passwd ${migrate_dir}/sofia ||
16315                 error "sofia different after migration"
16316
16317         diff /etc/passwd ${other_dir}/zachary ||
16318                 error "zachary different after migration"
16319
16320         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16321                 error "${tfile}_ln different after migration"
16322
16323         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16324                 error "${tfile}_ln_other different after migration"
16325
16326         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
16327         [ $stripe_count = 2 ] ||
16328                 error "dir strpe_count $d != 2 after migration."
16329
16330         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
16331         [ $stripe_count = 2 ] ||
16332                 error "file strpe_count $d != 2 after migration."
16333
16334         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16335 }
16336 run_test 230b "migrate directory"
16337
16338 test_230c() {
16339         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16340         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16341         remote_mds_nodsh && skip "remote MDS with nodsh"
16342         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16343                 skip "Need MDS version at least 2.11.52"
16344
16345         local MDTIDX=1
16346         local total=3
16347         local mdt_index
16348         local file
16349         local migrate_dir=$DIR/$tdir/migrate_dir
16350
16351         #If migrating directory fails in the middle, all entries of
16352         #the directory is still accessiable.
16353         test_mkdir $DIR/$tdir
16354         test_mkdir -i0 -c1 $migrate_dir
16355         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
16356         stat $migrate_dir
16357         createmany -o $migrate_dir/f $total ||
16358                 error "create files under ${migrate_dir} failed"
16359
16360         # fail after migrating top dir, and this will fail only once, so the
16361         # first sub file migration will fail (currently f3), others succeed.
16362         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
16363         do_facet mds1 lctl set_param fail_loc=0x1801
16364         local t=$(ls $migrate_dir | wc -l)
16365         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
16366                 error "migrate should fail"
16367         local u=$(ls $migrate_dir | wc -l)
16368         [ "$u" == "$t" ] || error "$u != $t during migration"
16369
16370         # add new dir/file should succeed
16371         mkdir $migrate_dir/dir ||
16372                 error "mkdir failed under migrating directory"
16373         touch $migrate_dir/file ||
16374                 error "create file failed under migrating directory"
16375
16376         # add file with existing name should fail
16377         for file in $migrate_dir/f*; do
16378                 stat $file > /dev/null || error "stat $file failed"
16379                 $OPENFILE -f O_CREAT:O_EXCL $file &&
16380                         error "open(O_CREAT|O_EXCL) $file should fail"
16381                 $MULTIOP $file m && error "create $file should fail"
16382                 touch $DIR/$tdir/remote_dir/$tfile ||
16383                         error "touch $tfile failed"
16384                 ln $DIR/$tdir/remote_dir/$tfile $file &&
16385                         error "link $file should fail"
16386                 mdt_index=$($LFS getstripe -m $file)
16387                 if [ $mdt_index == 0 ]; then
16388                         # file failed to migrate is not allowed to rename to
16389                         mv $DIR/$tdir/remote_dir/$tfile $file &&
16390                                 error "rename to $file should fail"
16391                 else
16392                         mv $DIR/$tdir/remote_dir/$tfile $file ||
16393                                 error "rename to $file failed"
16394                 fi
16395                 echo hello >> $file || error "write $file failed"
16396         done
16397
16398         # resume migration with different options should fail
16399         $LFS migrate -m 0 $migrate_dir &&
16400                 error "migrate -m 0 $migrate_dir should fail"
16401
16402         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
16403                 error "migrate -c 2 $migrate_dir should fail"
16404
16405         # resume migration should succeed
16406         $LFS migrate -m $MDTIDX $migrate_dir ||
16407                 error "migrate $migrate_dir failed"
16408
16409         echo "Finish migration, then checking.."
16410         for file in $(find $migrate_dir); do
16411                 mdt_index=$($LFS getstripe -m $file)
16412                 [ $mdt_index == $MDTIDX ] ||
16413                         error "$file is not on MDT${MDTIDX}"
16414         done
16415
16416         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16417 }
16418 run_test 230c "check directory accessiblity if migration failed"
16419
16420 test_230d() {
16421         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16422         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16423         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16424                 skip "Need MDS version at least 2.11.52"
16425         # LU-11235
16426         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
16427
16428         local migrate_dir=$DIR/$tdir/migrate_dir
16429         local old_index
16430         local new_index
16431         local old_count
16432         local new_count
16433         local new_hash
16434         local mdt_index
16435         local i
16436         local j
16437
16438         old_index=$((RANDOM % MDSCOUNT))
16439         old_count=$((MDSCOUNT - old_index))
16440         new_index=$((RANDOM % MDSCOUNT))
16441         new_count=$((MDSCOUNT - new_index))
16442         new_hash="all_char"
16443
16444         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
16445         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
16446
16447         test_mkdir $DIR/$tdir
16448         test_mkdir -i $old_index -c $old_count $migrate_dir
16449
16450         for ((i=0; i<100; i++)); do
16451                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
16452                 createmany -o $migrate_dir/dir_${i}/f 100 ||
16453                         error "create files under remote dir failed $i"
16454         done
16455
16456         echo -n "Migrate from MDT$old_index "
16457         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
16458         echo -n "to MDT$new_index"
16459         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
16460         echo
16461
16462         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
16463         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
16464                 error "migrate remote dir error"
16465
16466         echo "Finish migration, then checking.."
16467         for file in $(find $migrate_dir); do
16468                 mdt_index=$($LFS getstripe -m $file)
16469                 if [ $mdt_index -lt $new_index ] ||
16470                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
16471                         error "$file is on MDT$mdt_index"
16472                 fi
16473         done
16474
16475         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16476 }
16477 run_test 230d "check migrate big directory"
16478
16479 test_230e() {
16480         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16481         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16482         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16483                 skip "Need MDS version at least 2.11.52"
16484
16485         local i
16486         local j
16487         local a_fid
16488         local b_fid
16489
16490         mkdir -p $DIR/$tdir
16491         mkdir $DIR/$tdir/migrate_dir
16492         mkdir $DIR/$tdir/other_dir
16493         touch $DIR/$tdir/migrate_dir/a
16494         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
16495         ls $DIR/$tdir/other_dir
16496
16497         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16498                 error "migrate dir fails"
16499
16500         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16501         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16502
16503         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16504         [ $mdt_index == 0 ] || error "a is not on MDT0"
16505
16506         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
16507                 error "migrate dir fails"
16508
16509         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
16510         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
16511
16512         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16513         [ $mdt_index == 1 ] || error "a is not on MDT1"
16514
16515         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
16516         [ $mdt_index == 1 ] || error "b is not on MDT1"
16517
16518         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16519         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
16520
16521         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
16522
16523         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16524 }
16525 run_test 230e "migrate mulitple local link files"
16526
16527 test_230f() {
16528         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16529         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16530         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16531                 skip "Need MDS version at least 2.11.52"
16532
16533         local a_fid
16534         local ln_fid
16535
16536         mkdir -p $DIR/$tdir
16537         mkdir $DIR/$tdir/migrate_dir
16538         $LFS mkdir -i1 $DIR/$tdir/other_dir
16539         touch $DIR/$tdir/migrate_dir/a
16540         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
16541         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
16542         ls $DIR/$tdir/other_dir
16543
16544         # a should be migrated to MDT1, since no other links on MDT0
16545         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16546                 error "#1 migrate dir fails"
16547         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16548         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16549         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16550         [ $mdt_index == 1 ] || error "a is not on MDT1"
16551
16552         # a should stay on MDT1, because it is a mulitple link file
16553         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16554                 error "#2 migrate dir fails"
16555         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16556         [ $mdt_index == 1 ] || error "a is not on MDT1"
16557
16558         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16559                 error "#3 migrate dir fails"
16560
16561         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16562         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
16563         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
16564
16565         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
16566         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
16567
16568         # a should be migrated to MDT0, since no other links on MDT1
16569         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16570                 error "#4 migrate dir fails"
16571         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16572         [ $mdt_index == 0 ] || error "a is not on MDT0"
16573
16574         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16575 }
16576 run_test 230f "migrate mulitple remote link files"
16577
16578 test_230g() {
16579         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16580         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16581         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16582                 skip "Need MDS version at least 2.11.52"
16583
16584         mkdir -p $DIR/$tdir/migrate_dir
16585
16586         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
16587                 error "migrating dir to non-exist MDT succeeds"
16588         true
16589 }
16590 run_test 230g "migrate dir to non-exist MDT"
16591
16592 test_230h() {
16593         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16594         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16595         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16596                 skip "Need MDS version at least 2.11.52"
16597
16598         local mdt_index
16599
16600         mkdir -p $DIR/$tdir/migrate_dir
16601
16602         $LFS migrate -m1 $DIR &&
16603                 error "migrating mountpoint1 should fail"
16604
16605         $LFS migrate -m1 $DIR/$tdir/.. &&
16606                 error "migrating mountpoint2 should fail"
16607
16608         # same as mv
16609         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
16610                 error "migrating $tdir/migrate_dir/.. should fail"
16611
16612         true
16613 }
16614 run_test 230h "migrate .. and root"
16615
16616 test_230i() {
16617         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16618         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16619         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16620                 skip "Need MDS version at least 2.11.52"
16621
16622         mkdir -p $DIR/$tdir/migrate_dir
16623
16624         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
16625                 error "migration fails with a tailing slash"
16626
16627         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
16628                 error "migration fails with two tailing slashes"
16629 }
16630 run_test 230i "lfs migrate -m tolerates trailing slashes"
16631
16632 test_230j() {
16633         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16634         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16635                 skip "Need MDS version at least 2.11.52"
16636
16637         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
16638         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
16639                 error "create $tfile failed"
16640         cat /etc/passwd > $DIR/$tdir/$tfile
16641
16642         $LFS migrate -m 1 $DIR/$tdir
16643
16644         cmp /etc/passwd $DIR/$tdir/$tfile ||
16645                 error "DoM file mismatch after migration"
16646 }
16647 run_test 230j "DoM file data not changed after dir migration"
16648
16649 test_230k() {
16650         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
16651         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16652                 skip "Need MDS version at least 2.11.56"
16653
16654         local total=20
16655         local files_on_starting_mdt=0
16656
16657         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
16658         $LFS getdirstripe $DIR/$tdir
16659         for i in $(seq $total); do
16660                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
16661                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
16662                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16663         done
16664
16665         echo "$files_on_starting_mdt files on MDT0"
16666
16667         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
16668         $LFS getdirstripe $DIR/$tdir
16669
16670         files_on_starting_mdt=0
16671         for i in $(seq $total); do
16672                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
16673                         error "file $tfile.$i mismatch after migration"
16674                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
16675                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16676         done
16677
16678         echo "$files_on_starting_mdt files on MDT1 after migration"
16679         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
16680
16681         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
16682         $LFS getdirstripe $DIR/$tdir
16683
16684         files_on_starting_mdt=0
16685         for i in $(seq $total); do
16686                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
16687                         error "file $tfile.$i mismatch after 2nd migration"
16688                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
16689                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16690         done
16691
16692         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
16693         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
16694
16695         true
16696 }
16697 run_test 230k "file data not changed after dir migration"
16698
16699 test_230l() {
16700         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16701         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16702                 skip "Need MDS version at least 2.11.56"
16703
16704         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
16705         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
16706                 error "create files under remote dir failed $i"
16707         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
16708 }
16709 run_test 230l "readdir between MDTs won't crash"
16710
16711 test_230m() {
16712         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16713         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16714                 skip "Need MDS version at least 2.11.56"
16715
16716         local MDTIDX=1
16717         local mig_dir=$DIR/$tdir/migrate_dir
16718         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
16719         local shortstr="b"
16720         local val
16721
16722         echo "Creating files and dirs with xattrs"
16723         test_mkdir $DIR/$tdir
16724         test_mkdir -i0 -c1 $mig_dir
16725         mkdir $mig_dir/dir
16726         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
16727                 error "cannot set xattr attr1 on dir"
16728         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
16729                 error "cannot set xattr attr2 on dir"
16730         touch $mig_dir/dir/f0
16731         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
16732                 error "cannot set xattr attr1 on file"
16733         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
16734                 error "cannot set xattr attr2 on file"
16735         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
16736         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
16737         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
16738         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
16739         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
16740         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
16741         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
16742         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
16743         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
16744
16745         echo "Migrating to MDT1"
16746         $LFS migrate -m $MDTIDX $mig_dir ||
16747                 error "fails on migrating dir to MDT1"
16748
16749         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
16750         echo "Checking xattrs"
16751         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
16752         [ "$val" = $longstr ] ||
16753                 error "expecting xattr1 $longstr on dir, found $val"
16754         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
16755         [ "$val" = $shortstr ] ||
16756                 error "expecting xattr2 $shortstr on dir, found $val"
16757         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
16758         [ "$val" = $longstr ] ||
16759                 error "expecting xattr1 $longstr on file, found $val"
16760         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
16761         [ "$val" = $shortstr ] ||
16762                 error "expecting xattr2 $shortstr on file, found $val"
16763 }
16764 run_test 230m "xattrs not changed after dir migration"
16765
16766 test_230r() {
16767         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
16768         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
16769         [[ $MDS1_VERSION -ge $(version_code 2.12.5) ]] ||
16770                 skip "Need MDS version at least 2.12.5"
16771
16772         # maximum amount of local locks:
16773         # parent striped dir - 2 locks
16774         # new stripe in parent to migrate to - 1 lock
16775         # source and target - 2 locks
16776         # Total 5 locks for regular file
16777         mkdir -p $DIR/$tdir
16778         $LFS mkdir -i1 -c2 $DIR/$tdir/dir1
16779         touch $DIR/$tdir/dir1/eee
16780
16781         # create 4 hardlink for 4 more locks
16782         # Total: 9 locks > RS_MAX_LOCKS (8)
16783         $LFS mkdir -i1 -c1 $DIR/$tdir/dir2
16784         $LFS mkdir -i1 -c1 $DIR/$tdir/dir3
16785         $LFS mkdir -i1 -c1 $DIR/$tdir/dir4
16786         $LFS mkdir -i1 -c1 $DIR/$tdir/dir5
16787         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir2/eee
16788         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir3/eee
16789         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir4/eee
16790         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir5/eee
16791
16792         cancel_lru_locks mdc
16793
16794         $LFS migrate -m1 -c1 $DIR/$tdir/dir1 ||
16795                 error "migrate dir fails"
16796
16797         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16798 }
16799 run_test 230r "migrate with too many local locks"
16800
16801 test_231a()
16802 {
16803         # For simplicity this test assumes that max_pages_per_rpc
16804         # is the same across all OSCs
16805         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
16806         local bulk_size=$((max_pages * PAGE_SIZE))
16807         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
16808                                        head -n 1)
16809
16810         mkdir -p $DIR/$tdir
16811         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
16812                 error "failed to set stripe with -S ${brw_size}M option"
16813
16814         # clear the OSC stats
16815         $LCTL set_param osc.*.stats=0 &>/dev/null
16816         stop_writeback
16817
16818         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
16819         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
16820                 oflag=direct &>/dev/null || error "dd failed"
16821
16822         sync; sleep 1; sync # just to be safe
16823         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
16824         if [ x$nrpcs != "x1" ]; then
16825                 $LCTL get_param osc.*.stats
16826                 error "found $nrpcs ost_write RPCs, not 1 as expected"
16827         fi
16828
16829         start_writeback
16830         # Drop the OSC cache, otherwise we will read from it
16831         cancel_lru_locks osc
16832
16833         # clear the OSC stats
16834         $LCTL set_param osc.*.stats=0 &>/dev/null
16835
16836         # Client reads $bulk_size.
16837         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
16838                 iflag=direct &>/dev/null || error "dd failed"
16839
16840         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
16841         if [ x$nrpcs != "x1" ]; then
16842                 $LCTL get_param osc.*.stats
16843                 error "found $nrpcs ost_read RPCs, not 1 as expected"
16844         fi
16845 }
16846 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
16847
16848 test_231b() {
16849         mkdir -p $DIR/$tdir
16850         local i
16851         for i in {0..1023}; do
16852                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
16853                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
16854                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
16855         done
16856         sync
16857 }
16858 run_test 231b "must not assert on fully utilized OST request buffer"
16859
16860 test_232a() {
16861         mkdir -p $DIR/$tdir
16862         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
16863
16864         #define OBD_FAIL_LDLM_OST_LVB            0x31c
16865         do_facet ost1 $LCTL set_param fail_loc=0x31c
16866
16867         # ignore dd failure
16868         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
16869
16870         do_facet ost1 $LCTL set_param fail_loc=0
16871         umount_client $MOUNT || error "umount failed"
16872         mount_client $MOUNT || error "mount failed"
16873         stop ost1 || error "cannot stop ost1"
16874         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
16875 }
16876 run_test 232a "failed lock should not block umount"
16877
16878 test_232b() {
16879         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
16880                 skip "Need MDS version at least 2.10.58"
16881
16882         mkdir -p $DIR/$tdir
16883         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
16884         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
16885         sync
16886         cancel_lru_locks osc
16887
16888         #define OBD_FAIL_LDLM_OST_LVB            0x31c
16889         do_facet ost1 $LCTL set_param fail_loc=0x31c
16890
16891         # ignore failure
16892         $LFS data_version $DIR/$tdir/$tfile || true
16893
16894         do_facet ost1 $LCTL set_param fail_loc=0
16895         umount_client $MOUNT || error "umount failed"
16896         mount_client $MOUNT || error "mount failed"
16897         stop ost1 || error "cannot stop ost1"
16898         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
16899 }
16900 run_test 232b "failed data version lock should not block umount"
16901
16902 test_233a() {
16903         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
16904                 skip "Need MDS version at least 2.3.64"
16905         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
16906
16907         local fid=$($LFS path2fid $MOUNT)
16908
16909         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16910                 error "cannot access $MOUNT using its FID '$fid'"
16911 }
16912 run_test 233a "checking that OBF of the FS root succeeds"
16913
16914 test_233b() {
16915         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
16916                 skip "Need MDS version at least 2.5.90"
16917         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
16918
16919         local fid=$($LFS path2fid $MOUNT/.lustre)
16920
16921         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16922                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
16923
16924         fid=$($LFS path2fid $MOUNT/.lustre/fid)
16925         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16926                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
16927 }
16928 run_test 233b "checking that OBF of the FS .lustre succeeds"
16929
16930 test_234() {
16931         local p="$TMP/sanityN-$TESTNAME.parameters"
16932         save_lustre_params client "llite.*.xattr_cache" > $p
16933         lctl set_param llite.*.xattr_cache 1 ||
16934                 skip_env "xattr cache is not supported"
16935
16936         mkdir -p $DIR/$tdir || error "mkdir failed"
16937         touch $DIR/$tdir/$tfile || error "touch failed"
16938         # OBD_FAIL_LLITE_XATTR_ENOMEM
16939         $LCTL set_param fail_loc=0x1405
16940         getfattr -n user.attr $DIR/$tdir/$tfile &&
16941                 error "getfattr should have failed with ENOMEM"
16942         $LCTL set_param fail_loc=0x0
16943         rm -rf $DIR/$tdir
16944
16945         restore_lustre_params < $p
16946         rm -f $p
16947 }
16948 run_test 234 "xattr cache should not crash on ENOMEM"
16949
16950 test_235() {
16951         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
16952                 skip "Need MDS version at least 2.4.52"
16953
16954         flock_deadlock $DIR/$tfile
16955         local RC=$?
16956         case $RC in
16957                 0)
16958                 ;;
16959                 124) error "process hangs on a deadlock"
16960                 ;;
16961                 *) error "error executing flock_deadlock $DIR/$tfile"
16962                 ;;
16963         esac
16964 }
16965 run_test 235 "LU-1715: flock deadlock detection does not work properly"
16966
16967 #LU-2935
16968 test_236() {
16969         check_swap_layouts_support
16970
16971         local ref1=/etc/passwd
16972         local ref2=/etc/group
16973         local file1=$DIR/$tdir/f1
16974         local file2=$DIR/$tdir/f2
16975
16976         test_mkdir -c1 $DIR/$tdir
16977         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
16978         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
16979         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
16980         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
16981         local fd=$(free_fd)
16982         local cmd="exec $fd<>$file2"
16983         eval $cmd
16984         rm $file2
16985         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
16986                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
16987         cmd="exec $fd>&-"
16988         eval $cmd
16989         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
16990
16991         #cleanup
16992         rm -rf $DIR/$tdir
16993 }
16994 run_test 236 "Layout swap on open unlinked file"
16995
16996 # LU-4659 linkea consistency
16997 test_238() {
16998         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
16999                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
17000                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
17001                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
17002
17003         touch $DIR/$tfile
17004         ln $DIR/$tfile $DIR/$tfile.lnk
17005         touch $DIR/$tfile.new
17006         mv $DIR/$tfile.new $DIR/$tfile
17007         local fid1=$($LFS path2fid $DIR/$tfile)
17008         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
17009         local path1=$($LFS fid2path $FSNAME "$fid1")
17010         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
17011         local path2=$($LFS fid2path $FSNAME "$fid2")
17012         [ $tfile.lnk == $path2 ] ||
17013                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
17014         rm -f $DIR/$tfile*
17015 }
17016 run_test 238 "Verify linkea consistency"
17017
17018 test_239A() { # was test_239
17019         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
17020                 skip "Need MDS version at least 2.5.60"
17021
17022         local list=$(comma_list $(mdts_nodes))
17023
17024         mkdir -p $DIR/$tdir
17025         createmany -o $DIR/$tdir/f- 5000
17026         unlinkmany $DIR/$tdir/f- 5000
17027         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
17028                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
17029         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
17030                         osp.*MDT*.sync_in_flight" | calc_sum)
17031         [ "$changes" -eq 0 ] || error "$changes not synced"
17032 }
17033 run_test 239A "osp_sync test"
17034
17035 test_239a() { #LU-5297
17036         remote_mds_nodsh && skip "remote MDS with nodsh"
17037
17038         touch $DIR/$tfile
17039         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
17040         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
17041         chgrp $RUNAS_GID $DIR/$tfile
17042         wait_delete_completed
17043 }
17044 run_test 239a "process invalid osp sync record correctly"
17045
17046 test_239b() { #LU-5297
17047         remote_mds_nodsh && skip "remote MDS with nodsh"
17048
17049         touch $DIR/$tfile1
17050         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
17051         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
17052         chgrp $RUNAS_GID $DIR/$tfile1
17053         wait_delete_completed
17054         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17055         touch $DIR/$tfile2
17056         chgrp $RUNAS_GID $DIR/$tfile2
17057         wait_delete_completed
17058 }
17059 run_test 239b "process osp sync record with ENOMEM error correctly"
17060
17061 test_240() {
17062         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17063         remote_mds_nodsh && skip "remote MDS with nodsh"
17064
17065         mkdir -p $DIR/$tdir
17066
17067         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
17068                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
17069         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
17070                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
17071
17072         umount_client $MOUNT || error "umount failed"
17073         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
17074         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
17075         mount_client $MOUNT || error "failed to mount client"
17076
17077         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
17078         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
17079 }
17080 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
17081
17082 test_241_bio() {
17083         local count=$1
17084         local bsize=$2
17085
17086         for LOOP in $(seq $count); do
17087                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
17088                 cancel_lru_locks $OSC || true
17089         done
17090 }
17091
17092 test_241_dio() {
17093         local count=$1
17094         local bsize=$2
17095
17096         for LOOP in $(seq $1); do
17097                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
17098                         2>/dev/null
17099         done
17100 }
17101
17102 test_241a() { # was test_241
17103         local bsize=$PAGE_SIZE
17104
17105         (( bsize < 40960 )) && bsize=40960
17106         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17107         ls -la $DIR/$tfile
17108         cancel_lru_locks $OSC
17109         test_241_bio 1000 $bsize &
17110         PID=$!
17111         test_241_dio 1000 $bsize
17112         wait $PID
17113 }
17114 run_test 241a "bio vs dio"
17115
17116 test_241b() {
17117         local bsize=$PAGE_SIZE
17118
17119         (( bsize < 40960 )) && bsize=40960
17120         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17121         ls -la $DIR/$tfile
17122         test_241_dio 1000 $bsize &
17123         PID=$!
17124         test_241_dio 1000 $bsize
17125         wait $PID
17126 }
17127 run_test 241b "dio vs dio"
17128
17129 test_242() {
17130         remote_mds_nodsh && skip "remote MDS with nodsh"
17131
17132         mkdir -p $DIR/$tdir
17133         touch $DIR/$tdir/$tfile
17134
17135         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
17136         do_facet mds1 lctl set_param fail_loc=0x105
17137         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
17138
17139         do_facet mds1 lctl set_param fail_loc=0
17140         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
17141 }
17142 run_test 242 "mdt_readpage failure should not cause directory unreadable"
17143
17144 test_243()
17145 {
17146         test_mkdir $DIR/$tdir
17147         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
17148 }
17149 run_test 243 "various group lock tests"
17150
17151 test_244()
17152 {
17153         test_mkdir $DIR/$tdir
17154         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
17155         sendfile_grouplock $DIR/$tdir/$tfile || \
17156                 error "sendfile+grouplock failed"
17157         rm -rf $DIR/$tdir
17158 }
17159 run_test 244 "sendfile with group lock tests"
17160
17161 test_245() {
17162         local flagname="multi_mod_rpcs"
17163         local connect_data_name="max_mod_rpcs"
17164         local out
17165
17166         # check if multiple modify RPCs flag is set
17167         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
17168                 grep "connect_flags:")
17169         echo "$out"
17170
17171         echo "$out" | grep -qw $flagname
17172         if [ $? -ne 0 ]; then
17173                 echo "connect flag $flagname is not set"
17174                 return
17175         fi
17176
17177         # check if multiple modify RPCs data is set
17178         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
17179         echo "$out"
17180
17181         echo "$out" | grep -qw $connect_data_name ||
17182                 error "import should have connect data $connect_data_name"
17183 }
17184 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
17185
17186 test_246() { # LU-7371
17187         remote_ost_nodsh && skip "remote OST with nodsh"
17188         [ $OST1_VERSION -lt $(version_code 2.7.62) ] &&
17189                 skip "Need OST version >= 2.7.62"
17190
17191         do_facet ost1 $LCTL set_param fail_val=4095
17192 #define OBD_FAIL_OST_READ_SIZE          0x234
17193         do_facet ost1 $LCTL set_param fail_loc=0x234
17194         $LFS setstripe $DIR/$tfile -i 0 -c 1
17195         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
17196         cancel_lru_locks $FSNAME-OST0000
17197         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
17198 }
17199 run_test 246 "Read file of size 4095 should return right length"
17200
17201 cleanup_247() {
17202         local submount=$1
17203
17204         trap 0
17205         umount_client $submount
17206         rmdir $submount
17207 }
17208
17209 test_247a() {
17210         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17211                 grep -q subtree ||
17212                 skip_env "Fileset feature is not supported"
17213
17214         local submount=${MOUNT}_$tdir
17215
17216         mkdir $MOUNT/$tdir
17217         mkdir -p $submount || error "mkdir $submount failed"
17218         FILESET="$FILESET/$tdir" mount_client $submount ||
17219                 error "mount $submount failed"
17220         trap "cleanup_247 $submount" EXIT
17221         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
17222         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
17223                 error "read $MOUNT/$tdir/$tfile failed"
17224         cleanup_247 $submount
17225 }
17226 run_test 247a "mount subdir as fileset"
17227
17228 test_247b() {
17229         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17230                 skip_env "Fileset feature is not supported"
17231
17232         local submount=${MOUNT}_$tdir
17233
17234         rm -rf $MOUNT/$tdir
17235         mkdir -p $submount || error "mkdir $submount failed"
17236         SKIP_FILESET=1
17237         FILESET="$FILESET/$tdir" mount_client $submount &&
17238                 error "mount $submount should fail"
17239         rmdir $submount
17240 }
17241 run_test 247b "mount subdir that dose not exist"
17242
17243 test_247c() {
17244         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17245                 skip_env "Fileset feature is not supported"
17246
17247         local submount=${MOUNT}_$tdir
17248
17249         mkdir -p $MOUNT/$tdir/dir1
17250         mkdir -p $submount || error "mkdir $submount failed"
17251         trap "cleanup_247 $submount" EXIT
17252         FILESET="$FILESET/$tdir" mount_client $submount ||
17253                 error "mount $submount failed"
17254         local fid=$($LFS path2fid $MOUNT/)
17255         $LFS fid2path $submount $fid && error "fid2path should fail"
17256         cleanup_247 $submount
17257 }
17258 run_test 247c "running fid2path outside root"
17259
17260 test_247d() {
17261         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17262                 skip "Fileset feature is not supported"
17263
17264         local submount=${MOUNT}_$tdir
17265
17266         mkdir -p $MOUNT/$tdir/dir1
17267         mkdir -p $submount || error "mkdir $submount failed"
17268         FILESET="$FILESET/$tdir" mount_client $submount ||
17269                 error "mount $submount failed"
17270         trap "cleanup_247 $submount" EXIT
17271         local fid=$($LFS path2fid $submount/dir1)
17272         $LFS fid2path $submount $fid || error "fid2path should succeed"
17273         cleanup_247 $submount
17274 }
17275 run_test 247d "running fid2path inside root"
17276
17277 # LU-8037
17278 test_247e() {
17279         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17280                 grep -q subtree ||
17281                 skip "Fileset feature is not supported"
17282
17283         local submount=${MOUNT}_$tdir
17284
17285         mkdir $MOUNT/$tdir
17286         mkdir -p $submount || error "mkdir $submount failed"
17287         FILESET="$FILESET/.." mount_client $submount &&
17288                 error "mount $submount should fail"
17289         rmdir $submount
17290 }
17291 run_test 247e "mount .. as fileset"
17292
17293 test_247f() {
17294         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17295         [ $MDS1_VERSION -lt $(version_code 2.12.6) ] &&
17296                 skip "Need at least version 2.12.6"
17297         [ $CLIENT_VERSION -lt $(version_code 2.12.6) ] &&
17298                 skip "Need at least version 2.12.6"
17299         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17300                 grep -q subtree ||
17301                 skip "Fileset feature is not supported"
17302
17303         mkdir $DIR/$tdir || error "mkdir $tdir failed"
17304         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote ||
17305                 error "mkdir remote failed"
17306         mkdir $DIR/$tdir/remote/subdir || error "mkdir remote/subdir failed"
17307         $LFS mkdir -i 0 -c $MDSCOUNT $DIR/$tdir/striped ||
17308                 error "mkdir striped failed"
17309         mkdir $DIR/$tdir/striped/subdir || error "mkdir striped/subdir failed"
17310
17311         local submount=${MOUNT}_$tdir
17312
17313         mkdir -p $submount || error "mkdir $submount failed"
17314         stack_trap "rmdir $submount"
17315
17316         local dir
17317         local stat
17318         local fileset=$FILESET
17319         local mdts=$(comma_list $(mdts_nodes))
17320
17321         stat=$(do_facet mds1 $LCTL get_param -n \
17322                 mdt.*MDT0000.enable_remote_subdir_mount)
17323         stack_trap "do_nodes $mdts $LCTL set_param \
17324                 mdt.*.enable_remote_subdir_mount=$stat"
17325
17326         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=0"
17327         stack_trap "umount_client $submount"
17328         FILESET="$fileset/$tdir/remote" mount_client $submount &&
17329                 error "mount remote dir $dir should fail"
17330
17331         for dir in $tdir/remote/subdir $tdir/striped $tdir/striped/subdir \
17332                 $tdir/striped/. ; do
17333                 FILESET="$fileset/$dir" mount_client $submount ||
17334                         error "mount $dir failed"
17335                 umount_client $submount
17336         done
17337
17338         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=1"
17339         FILESET="$fileset/$tdir/remote" mount_client $submount ||
17340                 error "mount $tdir/remote failed"
17341 }
17342 run_test 247f "mount striped or remote directory as fileset"
17343
17344 test_247g() {
17345         [ $MDSCOUNT -lt 4 ] && skip_env "needs >= 4 MDTs"
17346         [ $CLIENT_VERSION -lt $(version_code 2.12.6) ] &&
17347                 skip "Need at least version 2.12.6"
17348
17349         $LFS mkdir -i 0 -c 4 -H fnv_1a_64 $DIR/$tdir ||
17350                 error "mkdir $tdir failed"
17351         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
17352
17353         local submount=${MOUNT}_$tdir
17354
17355         mkdir -p $submount || error "mkdir $submount failed"
17356         stack_trap "rmdir $submount"
17357
17358         FILESET="$fileset/$tdir" mount_client $submount ||
17359                 error "mount $dir failed"
17360         stack_trap "umount $submount"
17361
17362         local mdts=$(comma_list $(mdts_nodes))
17363
17364         local nrpcs
17365
17366         stat $submount > /dev/null
17367         cancel_lru_locks $MDC
17368         stat $submount > /dev/null
17369         stat $submount/$tfile > /dev/null
17370         do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear > /dev/null"
17371         stat $submount/$tfile > /dev/null
17372         nrpcs=$(do_nodes $mdts "lctl get_param -n mdt.*.md_stats" |
17373                 awk '/getattr/ {sum += $2} END {print sum}')
17374
17375         [ -z "$nrpcs" ] || error "$nrpcs extra getattr sent"
17376 }
17377 run_test 247g "mount striped directory as fileset caches ROOT lookup lock"
17378
17379 test_248() {
17380         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
17381         [ -z "$fast_read_sav" ] && skip "no fast read support"
17382
17383         # create a large file for fast read verification
17384         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
17385
17386         # make sure the file is created correctly
17387         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
17388                 { rm -f $DIR/$tfile; skip "file creation error"; }
17389
17390         echo "Test 1: verify that fast read is 4 times faster on cache read"
17391
17392         # small read with fast read enabled
17393         $LCTL set_param -n llite.*.fast_read=1
17394         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17395                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17396                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17397         # small read with fast read disabled
17398         $LCTL set_param -n llite.*.fast_read=0
17399         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17400                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17401                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17402
17403         # verify that fast read is 4 times faster for cache read
17404         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
17405                 error_not_in_vm "fast read was not 4 times faster: " \
17406                            "$t_fast vs $t_slow"
17407
17408         echo "Test 2: verify the performance between big and small read"
17409         $LCTL set_param -n llite.*.fast_read=1
17410
17411         # 1k non-cache read
17412         cancel_lru_locks osc
17413         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17414                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17415                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17416
17417         # 1M non-cache read
17418         cancel_lru_locks osc
17419         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17420                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17421                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17422
17423         # verify that big IO is not 4 times faster than small IO
17424         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
17425                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
17426
17427         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
17428         rm -f $DIR/$tfile
17429 }
17430 run_test 248 "fast read verification"
17431
17432 test_249() { # LU-7890
17433         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
17434                 skip "Need at least version 2.8.54"
17435
17436         rm -f $DIR/$tfile
17437         $SETSTRIPE -c 1 $DIR/$tfile
17438         # Offset 2T == 4k * 512M
17439         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
17440                 error "dd to 2T offset failed"
17441 }
17442 run_test 249 "Write above 2T file size"
17443
17444 test_250() {
17445         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
17446          && skip "no 16TB file size limit on ZFS"
17447
17448         $SETSTRIPE -c 1 $DIR/$tfile
17449         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
17450         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
17451         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
17452         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
17453                 conv=notrunc,fsync && error "append succeeded"
17454         return 0
17455 }
17456 run_test 250 "Write above 16T limit"
17457
17458 test_251() {
17459         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
17460
17461         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
17462         #Skip once - writing the first stripe will succeed
17463         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17464         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
17465                 error "short write happened"
17466
17467         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17468         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
17469                 error "short read happened"
17470
17471         rm -f $DIR/$tfile
17472 }
17473 run_test 251 "Handling short read and write correctly"
17474
17475 test_252() {
17476         remote_mds_nodsh && skip "remote MDS with nodsh"
17477         remote_ost_nodsh && skip "remote OST with nodsh"
17478         if [ "$ost1_FSTYPE" != "ldiskfs" -o "$mds1_FSTYPE" != "ldiskfs" ]; then
17479                 skip_env "ldiskfs only test"
17480         fi
17481
17482         local tgt
17483         local dev
17484         local out
17485         local uuid
17486         local num
17487         local gen
17488
17489         # check lr_reader on OST0000
17490         tgt=ost1
17491         dev=$(facet_device $tgt)
17492         out=$(do_facet $tgt $LR_READER $dev)
17493         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17494         echo "$out"
17495         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
17496         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
17497                 error "Invalid uuid returned by $LR_READER on target $tgt"
17498         echo -e "uuid returned by $LR_READER is '$uuid'\n"
17499
17500         # check lr_reader -c on MDT0000
17501         tgt=mds1
17502         dev=$(facet_device $tgt)
17503         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
17504                 skip "$LR_READER does not support additional options"
17505         fi
17506         out=$(do_facet $tgt $LR_READER -c $dev)
17507         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17508         echo "$out"
17509         num=$(echo "$out" | grep -c "mdtlov")
17510         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
17511                 error "Invalid number of mdtlov clients returned by $LR_READER"
17512         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
17513
17514         # check lr_reader -cr on MDT0000
17515         out=$(do_facet $tgt $LR_READER -cr $dev)
17516         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17517         echo "$out"
17518         echo "$out" | grep -q "^reply_data:$" ||
17519                 error "$LR_READER should have returned 'reply_data' section"
17520         num=$(echo "$out" | grep -c "client_generation")
17521         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
17522 }
17523 run_test 252 "check lr_reader tool"
17524
17525 test_253_fill_ost() {
17526         local size_mb #how many MB should we write to pass watermark
17527         local lwm=$3  #low watermark
17528         local free_10mb #10% of free space
17529
17530         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
17531         size_mb=$((free_kb / 1024 - lwm))
17532         free_10mb=$((free_kb / 10240))
17533         #If 10% of free space cross low watermark use it
17534         if (( free_10mb > size_mb )); then
17535                 size_mb=$free_10mb
17536         else
17537                 #At least we need to store 1.1 of difference between
17538                 #free space and low watermark
17539                 size_mb=$((size_mb + size_mb / 10))
17540         fi
17541         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
17542                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
17543                          oflag=append conv=notrunc
17544         fi
17545
17546         sleep_maxage
17547
17548         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
17549         echo "OST still has $((free_kb / 1024)) mbytes free"
17550 }
17551
17552 test_253() {
17553         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17554         remote_mds_nodsh && skip "remote MDS with nodsh"
17555         remote_mgs_nodsh && skip "remote MGS with nodsh"
17556
17557         local ostidx=0
17558         local rc=0
17559
17560         local ost_name=$($LFS osts |
17561                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
17562         # on the mdt's osc
17563         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
17564         do_facet $SINGLEMDS $LCTL get_param -n \
17565                 osp.$mdtosc_proc1.reserved_mb_high ||
17566                 skip  "remote MDS does not support reserved_mb_high"
17567
17568         rm -rf $DIR/$tdir
17569         wait_mds_ost_sync
17570         wait_delete_completed
17571         mkdir $DIR/$tdir
17572
17573         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
17574                         osp.$mdtosc_proc1.reserved_mb_high)
17575         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
17576                         osp.$mdtosc_proc1.reserved_mb_low)
17577         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
17578
17579         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
17580         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
17581                 error "Adding $ost_name to pool failed"
17582
17583         # Wait for client to see a OST at pool
17584         wait_update $HOSTNAME "$LCTL get_param -n
17585                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
17586                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
17587                 error "Client can not see the pool"
17588         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
17589                 error "Setstripe failed"
17590
17591         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
17592         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
17593         echo "OST still has $((blocks/1024)) mbytes free"
17594
17595         local new_lwm=$((blocks/1024-10))
17596         do_facet $SINGLEMDS $LCTL set_param \
17597                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
17598         do_facet $SINGLEMDS $LCTL set_param \
17599                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
17600
17601         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
17602
17603         #First enospc could execute orphan deletion so repeat.
17604         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
17605
17606         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17607                         osp.$mdtosc_proc1.prealloc_status)
17608         echo "prealloc_status $oa_status"
17609
17610         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
17611                 error "File creation should fail"
17612         #object allocation was stopped, but we still able to append files
17613         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
17614                 error "Append failed"
17615         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
17616
17617         wait_delete_completed
17618
17619         sleep_maxage
17620
17621         for i in $(seq 10 12); do
17622                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
17623                         error "File creation failed after rm";
17624         done
17625
17626         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17627                         osp.$mdtosc_proc1.prealloc_status)
17628         echo "prealloc_status $oa_status"
17629
17630         if (( oa_status != 0 )); then
17631                 error "Object allocation still disable after rm"
17632         fi
17633         do_facet $SINGLEMDS $LCTL set_param \
17634                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
17635         do_facet $SINGLEMDS $LCTL set_param \
17636                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
17637
17638
17639         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
17640                 error "Remove $ost_name from pool failed"
17641         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
17642                 error "Pool destroy fialed"
17643 }
17644 run_test 253 "Check object allocation limit"
17645
17646 test_254() {
17647         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17648         remote_mds_nodsh && skip "remote MDS with nodsh"
17649         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
17650                 skip "MDS does not support changelog_size"
17651
17652         local cl_user
17653         local MDT0=$(facet_svc $SINGLEMDS)
17654
17655         changelog_register || error "changelog_register failed"
17656
17657         changelog_clear 0 || error "changelog_clear failed"
17658
17659         local size1=$(do_facet $SINGLEMDS \
17660                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17661         echo "Changelog size $size1"
17662
17663         rm -rf $DIR/$tdir
17664         $LFS mkdir -i 0 $DIR/$tdir
17665         # change something
17666         mkdir -p $DIR/$tdir/pics/2008/zachy
17667         touch $DIR/$tdir/pics/2008/zachy/timestamp
17668         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
17669         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
17670         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
17671         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
17672         rm $DIR/$tdir/pics/desktop.jpg
17673
17674         local size2=$(do_facet $SINGLEMDS \
17675                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17676         echo "Changelog size after work $size2"
17677
17678         (( $size2 > $size1 )) ||
17679                 error "new Changelog size=$size2 less than old size=$size1"
17680 }
17681 run_test 254 "Check changelog size"
17682
17683 ladvise_no_type()
17684 {
17685         local type=$1
17686         local file=$2
17687
17688         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
17689                 awk -F: '{print $2}' | grep $type > /dev/null
17690         if [ $? -ne 0 ]; then
17691                 return 0
17692         fi
17693         return 1
17694 }
17695
17696 ladvise_no_ioctl()
17697 {
17698         local file=$1
17699
17700         lfs ladvise -a willread $file > /dev/null 2>&1
17701         if [ $? -eq 0 ]; then
17702                 return 1
17703         fi
17704
17705         lfs ladvise -a willread $file 2>&1 |
17706                 grep "Inappropriate ioctl for device" > /dev/null
17707         if [ $? -eq 0 ]; then
17708                 return 0
17709         fi
17710         return 1
17711 }
17712
17713 percent() {
17714         bc <<<"scale=2; ($1 - $2) * 100 / $2"
17715 }
17716
17717 # run a random read IO workload
17718 # usage: random_read_iops <filename> <filesize> <iosize>
17719 random_read_iops() {
17720         local file=$1
17721         local fsize=$2
17722         local iosize=${3:-4096}
17723
17724         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
17725                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
17726 }
17727
17728 drop_file_oss_cache() {
17729         local file="$1"
17730         local nodes="$2"
17731
17732         $LFS ladvise -a dontneed $file 2>/dev/null ||
17733                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
17734 }
17735
17736 ladvise_willread_performance()
17737 {
17738         local repeat=10
17739         local average_origin=0
17740         local average_cache=0
17741         local average_ladvise=0
17742
17743         for ((i = 1; i <= $repeat; i++)); do
17744                 echo "Iter $i/$repeat: reading without willread hint"
17745                 cancel_lru_locks osc
17746                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
17747                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
17748                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
17749                 average_origin=$(bc <<<"$average_origin + $speed_origin")
17750
17751                 cancel_lru_locks osc
17752                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
17753                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
17754                 average_cache=$(bc <<<"$average_cache + $speed_cache")
17755
17756                 cancel_lru_locks osc
17757                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
17758                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
17759                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
17760                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
17761                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
17762         done
17763         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
17764         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
17765         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
17766
17767         speedup_cache=$(percent $average_cache $average_origin)
17768         speedup_ladvise=$(percent $average_ladvise $average_origin)
17769
17770         echo "Average uncached read: $average_origin"
17771         echo "Average speedup with OSS cached read: " \
17772                 "$average_cache = +$speedup_cache%"
17773         echo "Average speedup with ladvise willread: " \
17774                 "$average_ladvise = +$speedup_ladvise%"
17775
17776         local lowest_speedup=20
17777         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
17778                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
17779                         "got $average_cache%. Skipping ladvise willread check."
17780                 return 0
17781         fi
17782
17783         # the test won't work on ZFS until it supports 'ladvise dontneed', but
17784         # it is still good to run until then to exercise 'ladvise willread'
17785         ! $LFS ladvise -a dontneed $DIR/$tfile &&
17786                 [ "$ost1_FSTYPE" = "zfs" ] &&
17787                 echo "osd-zfs does not support dontneed or drop_caches" &&
17788                 return 0
17789
17790         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
17791         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
17792                 error_not_in_vm "Speedup with willread is less than " \
17793                         "$lowest_speedup%, got $average_ladvise%"
17794 }
17795
17796 test_255a() {
17797         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
17798                 skip "lustre < 2.8.54 does not support ladvise "
17799         remote_ost_nodsh && skip "remote OST with nodsh"
17800
17801         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
17802
17803         ladvise_no_type willread $DIR/$tfile &&
17804                 skip "willread ladvise is not supported"
17805
17806         ladvise_no_ioctl $DIR/$tfile &&
17807                 skip "ladvise ioctl is not supported"
17808
17809         local size_mb=100
17810         local size=$((size_mb * 1048576))
17811         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
17812                 error "dd to $DIR/$tfile failed"
17813
17814         lfs ladvise -a willread $DIR/$tfile ||
17815                 error "Ladvise failed with no range argument"
17816
17817         lfs ladvise -a willread -s 0 $DIR/$tfile ||
17818                 error "Ladvise failed with no -l or -e argument"
17819
17820         lfs ladvise -a willread -e 1 $DIR/$tfile ||
17821                 error "Ladvise failed with only -e argument"
17822
17823         lfs ladvise -a willread -l 1 $DIR/$tfile ||
17824                 error "Ladvise failed with only -l argument"
17825
17826         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
17827                 error "End offset should not be smaller than start offset"
17828
17829         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
17830                 error "End offset should not be equal to start offset"
17831
17832         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
17833                 error "Ladvise failed with overflowing -s argument"
17834
17835         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
17836                 error "Ladvise failed with overflowing -e argument"
17837
17838         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
17839                 error "Ladvise failed with overflowing -l argument"
17840
17841         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
17842                 error "Ladvise succeeded with conflicting -l and -e arguments"
17843
17844         echo "Synchronous ladvise should wait"
17845         local delay=4
17846 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
17847         do_nodes $(comma_list $(osts_nodes)) \
17848                 $LCTL set_param fail_val=$delay fail_loc=0x237
17849
17850         local start_ts=$SECONDS
17851         lfs ladvise -a willread $DIR/$tfile ||
17852                 error "Ladvise failed with no range argument"
17853         local end_ts=$SECONDS
17854         local inteval_ts=$((end_ts - start_ts))
17855
17856         if [ $inteval_ts -lt $(($delay - 1)) ]; then
17857                 error "Synchronous advice didn't wait reply"
17858         fi
17859
17860         echo "Asynchronous ladvise shouldn't wait"
17861         local start_ts=$SECONDS
17862         lfs ladvise -a willread -b $DIR/$tfile ||
17863                 error "Ladvise failed with no range argument"
17864         local end_ts=$SECONDS
17865         local inteval_ts=$((end_ts - start_ts))
17866
17867         if [ $inteval_ts -gt $(($delay / 2)) ]; then
17868                 error "Asynchronous advice blocked"
17869         fi
17870
17871         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
17872         ladvise_willread_performance
17873 }
17874 run_test 255a "check 'lfs ladvise -a willread'"
17875
17876 facet_meminfo() {
17877         local facet=$1
17878         local info=$2
17879
17880         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
17881 }
17882
17883 test_255b() {
17884         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
17885                 skip "lustre < 2.8.54 does not support ladvise "
17886         remote_ost_nodsh && skip "remote OST with nodsh"
17887
17888         lfs setstripe -c 1 -i 0 $DIR/$tfile
17889
17890         ladvise_no_type dontneed $DIR/$tfile &&
17891                 skip "dontneed ladvise is not supported"
17892
17893         ladvise_no_ioctl $DIR/$tfile &&
17894                 skip "ladvise ioctl is not supported"
17895
17896         ! $LFS ladvise -a dontneed $DIR/$tfile &&
17897                 [ "$ost1_FSTYPE" = "zfs" ] &&
17898                 skip "zfs-osd does not support 'ladvise dontneed'"
17899
17900         local size_mb=100
17901         local size=$((size_mb * 1048576))
17902         # In order to prevent disturbance of other processes, only check 3/4
17903         # of the memory usage
17904         local kibibytes=$((size_mb * 1024 * 3 / 4))
17905
17906         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
17907                 error "dd to $DIR/$tfile failed"
17908
17909         #force write to complete before dropping OST cache & checking memory
17910         sync
17911
17912         local total=$(facet_meminfo ost1 MemTotal)
17913         echo "Total memory: $total KiB"
17914
17915         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
17916         local before_read=$(facet_meminfo ost1 Cached)
17917         echo "Cache used before read: $before_read KiB"
17918
17919         lfs ladvise -a willread $DIR/$tfile ||
17920                 error "Ladvise willread failed"
17921         local after_read=$(facet_meminfo ost1 Cached)
17922         echo "Cache used after read: $after_read KiB"
17923
17924         lfs ladvise -a dontneed $DIR/$tfile ||
17925                 error "Ladvise dontneed again failed"
17926         local no_read=$(facet_meminfo ost1 Cached)
17927         echo "Cache used after dontneed ladvise: $no_read KiB"
17928
17929         if [ $total -lt $((before_read + kibibytes)) ]; then
17930                 echo "Memory is too small, abort checking"
17931                 return 0
17932         fi
17933
17934         if [ $((before_read + kibibytes)) -gt $after_read ]; then
17935                 error "Ladvise willread should use more memory" \
17936                         "than $kibibytes KiB"
17937         fi
17938
17939         if [ $((no_read + kibibytes)) -gt $after_read ]; then
17940                 error "Ladvise dontneed should release more memory" \
17941                         "than $kibibytes KiB"
17942         fi
17943 }
17944 run_test 255b "check 'lfs ladvise -a dontneed'"
17945
17946 test_255c() {
17947         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
17948                 skip "lustre < 2.10.50 does not support lockahead"
17949
17950         local ost1_imp=$(get_osc_import_name client ost1)
17951         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
17952                          cut -d'.' -f2)
17953         local count
17954         local new_count
17955         local difference
17956         local i
17957         local rc
17958
17959         test_mkdir -p $DIR/$tdir
17960         $SETSTRIPE -i 0 $DIR/$tdir
17961
17962         #test 10 returns only success/failure
17963         i=10
17964         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17965         rc=$?
17966         if [ $rc -eq 255 ]; then
17967                 error "Ladvise test${i} failed, ${rc}"
17968         fi
17969
17970         #test 11 counts lock enqueue requests, all others count new locks
17971         i=11
17972         count=$(do_facet ost1 \
17973                 $LCTL get_param -n ost.OSS.ost.stats)
17974         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
17975
17976         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17977         rc=$?
17978         if [ $rc -eq 255 ]; then
17979                 error "Ladvise test${i} failed, ${rc}"
17980         fi
17981
17982         new_count=$(do_facet ost1 \
17983                 $LCTL get_param -n ost.OSS.ost.stats)
17984         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
17985                    awk '{ print $2 }')
17986
17987         difference="$((new_count - count))"
17988         if [ $difference -ne $rc ]; then
17989                 error "Ladvise test${i}, bad enqueue count, returned " \
17990                       "${rc}, actual ${difference}"
17991         fi
17992
17993         for i in $(seq 12 21); do
17994                 # If we do not do this, we run the risk of having too many
17995                 # locks and starting lock cancellation while we are checking
17996                 # lock counts.
17997                 cancel_lru_locks osc
17998
17999                 count=$($LCTL get_param -n \
18000                        ldlm.namespaces.$imp_name.lock_unused_count)
18001
18002                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
18003                 rc=$?
18004                 if [ $rc -eq 255 ]; then
18005                         error "Ladvise test ${i} failed, ${rc}"
18006                 fi
18007
18008                 new_count=$($LCTL get_param -n \
18009                        ldlm.namespaces.$imp_name.lock_unused_count)
18010                 difference="$((new_count - count))"
18011
18012                 # Test 15 output is divided by 100 to map down to valid return
18013                 if [ $i -eq 15 ]; then
18014                         rc="$((rc * 100))"
18015                 fi
18016
18017                 if [ $difference -ne $rc ]; then
18018                         error "Ladvise test ${i}, bad lock count, returned " \
18019                               "${rc}, actual ${difference}"
18020                 fi
18021         done
18022
18023         #test 22 returns only success/failure
18024         i=22
18025         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18026         rc=$?
18027         if [ $rc -eq 255 ]; then
18028                 error "Ladvise test${i} failed, ${rc}"
18029         fi
18030 }
18031 run_test 255c "suite of ladvise lockahead tests"
18032
18033 test_256() {
18034         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18035         remote_mds_nodsh && skip "remote MDS with nodsh"
18036         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18037         changelog_users $SINGLEMDS | grep "^cl" &&
18038                 skip "active changelog user"
18039
18040         local cl_user
18041         local cat_sl
18042         local mdt_dev
18043
18044         mdt_dev=$(mdsdevname 1)
18045         echo $mdt_dev
18046
18047         changelog_register || error "changelog_register failed"
18048
18049         rm -rf $DIR/$tdir
18050         mkdir -p $DIR/$tdir
18051
18052         changelog_clear 0 || error "changelog_clear failed"
18053
18054         # change something
18055         touch $DIR/$tdir/{1..10}
18056
18057         # stop the MDT
18058         stop $SINGLEMDS || error "Fail to stop MDT"
18059
18060         # remount the MDT
18061
18062         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
18063
18064         #after mount new plainllog is used
18065         touch $DIR/$tdir/{11..19}
18066         local tmpfile=$(mktemp -u $tfile.XXXXXX)
18067         cat_sl=$(do_facet $SINGLEMDS "sync; \
18068                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18069                  llog_reader $tmpfile | grep -c type=1064553b")
18070         do_facet $SINGLEMDS llog_reader $tmpfile
18071
18072         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
18073
18074         changelog_clear 0 || error "changelog_clear failed"
18075
18076         cat_sl=$(do_facet $SINGLEMDS "sync; \
18077                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18078                  llog_reader $tmpfile | grep -c type=1064553b; rm -f $tmpfile")
18079
18080         if (( cat_sl == 2 )); then
18081                 error "Empty plain llog was not deleted from changelog catalog"
18082         elif (( cat_sl != 1 )); then
18083                 error "Active plain llog shouldn't be deleted from catalog"
18084         fi
18085 }
18086 run_test 256 "Check llog delete for empty and not full state"
18087
18088 test_257() {
18089         remote_mds_nodsh && skip "remote MDS with nodsh"
18090         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
18091                 skip "Need MDS version at least 2.8.55"
18092
18093         test_mkdir $DIR/$tdir
18094
18095         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
18096                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
18097         stat $DIR/$tdir
18098
18099 #define OBD_FAIL_MDS_XATTR_REP                  0x161
18100         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18101         local facet=mds$((mdtidx + 1))
18102         set_nodes_failloc $(facet_active_host $facet) 0x80000161
18103         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
18104
18105         stop $facet || error "stop MDS failed"
18106         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
18107                 error "start MDS fail"
18108 }
18109 run_test 257 "xattr locks are not lost"
18110
18111 # Verify we take the i_mutex when security requires it
18112 test_258a() {
18113 #define OBD_FAIL_IMUTEX_SEC 0x141c
18114         $LCTL set_param fail_loc=0x141c
18115         touch $DIR/$tfile
18116         chmod u+s $DIR/$tfile
18117         chmod a+rwx $DIR/$tfile
18118         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18119         RC=$?
18120         if [ $RC -ne 0 ]; then
18121                 error "error, failed to take i_mutex, rc=$?"
18122         fi
18123         rm -f $DIR/$tfile
18124 }
18125 run_test 258a "verify i_mutex security behavior when suid attributes is set"
18126
18127 # Verify we do NOT take the i_mutex in the normal case
18128 test_258b() {
18129 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
18130         $LCTL set_param fail_loc=0x141d
18131         touch $DIR/$tfile
18132         chmod a+rwx $DIR
18133         chmod a+rw $DIR/$tfile
18134         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18135         RC=$?
18136         if [ $RC -ne 0 ]; then
18137                 error "error, took i_mutex unnecessarily, rc=$?"
18138         fi
18139         rm -f $DIR/$tfile
18140
18141 }
18142 run_test 258b "verify i_mutex security behavior"
18143
18144 test_259() {
18145         local file=$DIR/$tfile
18146         local before
18147         local after
18148
18149         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18150
18151         stack_trap "rm -f $file" EXIT
18152
18153         wait_delete_completed
18154         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18155         echo "before: $before"
18156
18157         $LFS setstripe -i 0 -c 1 $file
18158         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
18159         sync_all_data
18160         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18161         echo "after write: $after"
18162
18163 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
18164         do_facet ost1 $LCTL set_param fail_loc=0x2301
18165         $TRUNCATE $file 0
18166         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18167         echo "after truncate: $after"
18168
18169         stop ost1
18170         do_facet ost1 $LCTL set_param fail_loc=0
18171         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
18172         sleep 2
18173         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18174         echo "after restart: $after"
18175         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
18176                 error "missing truncate?"
18177
18178         return 0
18179 }
18180 run_test 259 "crash at delayed truncate"
18181
18182 test_260() {
18183 #define OBD_FAIL_MDC_CLOSE               0x806
18184         $LCTL set_param fail_loc=0x80000806
18185         touch $DIR/$tfile
18186
18187 }
18188 run_test 260 "Check mdc_close fail"
18189
18190 ### Data-on-MDT sanity tests ###
18191 test_270a() {
18192         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18193                 skip "Need MDS version at least 2.10.55 for DoM"
18194
18195         # create DoM file
18196         local dom=$DIR/$tdir/dom_file
18197         local tmp=$DIR/$tdir/tmp_file
18198
18199         mkdir -p $DIR/$tdir
18200
18201         # basic checks for DoM component creation
18202         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
18203                 error "Can set MDT layout to non-first entry"
18204
18205         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
18206                 error "Can define multiple entries as MDT layout"
18207
18208         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
18209
18210         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
18211         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
18212         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
18213
18214         local mdtidx=$($LFS getstripe -m $dom)
18215         local mdtname=MDT$(printf %04x $mdtidx)
18216         local facet=mds$((mdtidx + 1))
18217         local space_check=1
18218
18219         # Skip free space checks with ZFS
18220         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
18221
18222         # write
18223         sync
18224         local size_tmp=$((65536 * 3))
18225         local mdtfree1=$(do_facet $facet \
18226                          lctl get_param -n osd*.*$mdtname.kbytesfree)
18227
18228         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18229         # check also direct IO along write
18230         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
18231         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18232         sync
18233         cmp $tmp $dom || error "file data is different"
18234         [ $(stat -c%s $dom) == $size_tmp ] ||
18235                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18236         if [ $space_check == 1 ]; then
18237                 local mdtfree2=$(do_facet $facet \
18238                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
18239
18240                 # increase in usage from by $size_tmp
18241                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18242                         error "MDT free space wrong after write: " \
18243                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18244         fi
18245
18246         # truncate
18247         local size_dom=10000
18248
18249         $TRUNCATE $dom $size_dom
18250         [ $(stat -c%s $dom) == $size_dom ] ||
18251                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
18252         if [ $space_check == 1 ]; then
18253                 mdtfree1=$(do_facet $facet \
18254                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18255                 # decrease in usage from $size_tmp to new $size_dom
18256                 [ $(($mdtfree1 - $mdtfree2)) -ge \
18257                   $(((size_tmp - size_dom) / 1024)) ] ||
18258                         error "MDT free space is wrong after truncate: " \
18259                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
18260         fi
18261
18262         # append
18263         cat $tmp >> $dom
18264         sync
18265         size_dom=$((size_dom + size_tmp))
18266         [ $(stat -c%s $dom) == $size_dom ] ||
18267                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
18268         if [ $space_check == 1 ]; then
18269                 mdtfree2=$(do_facet $facet \
18270                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18271                 # increase in usage by $size_tmp from previous
18272                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18273                         error "MDT free space is wrong after append: " \
18274                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18275         fi
18276
18277         # delete
18278         rm $dom
18279         if [ $space_check == 1 ]; then
18280                 mdtfree1=$(do_facet $facet \
18281                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18282                 # decrease in usage by $size_dom from previous
18283                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
18284                         error "MDT free space is wrong after removal: " \
18285                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
18286         fi
18287
18288         # combined striping
18289         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
18290                 error "Can't create DoM + OST striping"
18291
18292         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
18293         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18294         # check also direct IO along write
18295         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18296         sync
18297         cmp $tmp $dom || error "file data is different"
18298         [ $(stat -c%s $dom) == $size_tmp ] ||
18299                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18300         rm $dom $tmp
18301
18302         return 0
18303 }
18304 run_test 270a "DoM: basic functionality tests"
18305
18306 test_270b() {
18307         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18308                 skip "Need MDS version at least 2.10.55"
18309
18310         local dom=$DIR/$tdir/dom_file
18311         local max_size=1048576
18312
18313         mkdir -p $DIR/$tdir
18314         $LFS setstripe -E $max_size -L mdt $dom
18315
18316         # truncate over the limit
18317         $TRUNCATE $dom $(($max_size + 1)) &&
18318                 error "successful truncate over the maximum size"
18319         # write over the limit
18320         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
18321                 error "successful write over the maximum size"
18322         # append over the limit
18323         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
18324         echo "12345" >> $dom && error "successful append over the maximum size"
18325         rm $dom
18326
18327         return 0
18328 }
18329 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
18330
18331 test_270c() {
18332         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18333                 skip "Need MDS version at least 2.10.55"
18334
18335         mkdir -p $DIR/$tdir
18336         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18337
18338         # check files inherit DoM EA
18339         touch $DIR/$tdir/first
18340         [ $($GETSTRIPE -L $DIR/$tdir/first) == "mdt" ] ||
18341                 error "bad pattern"
18342         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
18343                 error "bad stripe count"
18344         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
18345                 error "bad stripe size"
18346
18347         # check directory inherits DoM EA and uses it as default
18348         mkdir $DIR/$tdir/subdir
18349         touch $DIR/$tdir/subdir/second
18350         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
18351                 error "bad pattern in sub-directory"
18352         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
18353                 error "bad stripe count in sub-directory"
18354         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
18355                 error "bad stripe size in sub-directory"
18356         return 0
18357 }
18358 run_test 270c "DoM: DoM EA inheritance tests"
18359
18360 test_270d() {
18361         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18362                 skip "Need MDS version at least 2.10.55"
18363
18364         mkdir -p $DIR/$tdir
18365         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18366
18367         # inherit default DoM striping
18368         mkdir $DIR/$tdir/subdir
18369         touch $DIR/$tdir/subdir/f1
18370
18371         # change default directory striping
18372         $LFS setstripe -c 1 $DIR/$tdir/subdir
18373         touch $DIR/$tdir/subdir/f2
18374         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
18375                 error "wrong default striping in file 2"
18376         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
18377                 error "bad pattern in file 2"
18378         return 0
18379 }
18380 run_test 270d "DoM: change striping from DoM to RAID0"
18381
18382 test_270e() {
18383         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18384                 skip "Need MDS version at least 2.10.55"
18385
18386         mkdir -p $DIR/$tdir/dom
18387         mkdir -p $DIR/$tdir/norm
18388         DOMFILES=20
18389         NORMFILES=10
18390         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
18391         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
18392
18393         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
18394         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
18395
18396         # find DoM files by layout
18397         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
18398         [ $NUM -eq  $DOMFILES ] ||
18399                 error "lfs find -L: found $NUM, expected $DOMFILES"
18400         echo "Test 1: lfs find 20 DOM files by layout: OK"
18401
18402         # there should be 1 dir with default DOM striping
18403         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
18404         [ $NUM -eq  1 ] ||
18405                 error "lfs find -L: found $NUM, expected 1 dir"
18406         echo "Test 2: lfs find 1 DOM dir by layout: OK"
18407
18408         # find DoM files by stripe size
18409         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
18410         [ $NUM -eq  $DOMFILES ] ||
18411                 error "lfs find -S: found $NUM, expected $DOMFILES"
18412         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
18413
18414         # find files by stripe offset except DoM files
18415         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
18416         [ $NUM -eq  $NORMFILES ] ||
18417                 error "lfs find -i: found $NUM, expected $NORMFILES"
18418         echo "Test 5: lfs find no DOM files by stripe index: OK"
18419         return 0
18420 }
18421 run_test 270e "DoM: lfs find with DoM files test"
18422
18423 test_270f() {
18424         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18425                 skip "Need MDS version at least 2.10.55"
18426
18427         local mdtname=${FSNAME}-MDT0000-mdtlov
18428         local dom=$DIR/$tdir/dom_file
18429         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
18430                                                 lod.$mdtname.dom_stripesize)
18431         local dom_limit=131072
18432
18433         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
18434         local dom_current=$(do_facet mds1 $LCTL get_param -n \
18435                                                 lod.$mdtname.dom_stripesize)
18436         [ ${dom_limit} -eq ${dom_current} ] ||
18437                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
18438
18439         $LFS mkdir -i 0 -c 1 $DIR/$tdir
18440         $LFS setstripe -d $DIR/$tdir
18441         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
18442                 error "Can't set directory default striping"
18443
18444         # exceed maximum stripe size
18445         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18446                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
18447         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
18448                 error "Able to create DoM component size more than LOD limit"
18449
18450         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
18451         dom_current=$(do_facet mds1 $LCTL get_param -n \
18452                                                 lod.$mdtname.dom_stripesize)
18453         [ 0 -eq ${dom_current} ] ||
18454                 error "Can't set zero DoM stripe limit"
18455         rm $dom
18456
18457         # attempt to create DoM file on server with disabled DoM should
18458         # remove DoM entry from layout and be succeed
18459         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
18460                 error "Can't create DoM file (DoM is disabled)"
18461         [ $($LFS getstripe -L $dom) == "mdt" ] &&
18462                 error "File has DoM component while DoM is disabled"
18463         rm $dom
18464
18465         # attempt to create DoM file with only DoM stripe should return error
18466         $LFS setstripe -E $dom_limit -L mdt $dom &&
18467                 error "Able to create DoM-only file while DoM is disabled"
18468
18469         # too low values to be aligned with smallest stripe size 64K
18470         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
18471         dom_current=$(do_facet mds1 $LCTL get_param -n \
18472                                                 lod.$mdtname.dom_stripesize)
18473         [ 30000 -eq ${dom_current} ] &&
18474                 error "Can set too small DoM stripe limit"
18475
18476         # 64K is a minimal stripe size in Lustre, expect limit of that size
18477         [ 65536 -eq ${dom_current} ] ||
18478                 error "Limit is not set to 64K but ${dom_current}"
18479
18480         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
18481         dom_current=$(do_facet mds1 $LCTL get_param -n \
18482                                                 lod.$mdtname.dom_stripesize)
18483         echo $dom_current
18484         [ 2147483648 -eq ${dom_current} ] &&
18485                 error "Can set too large DoM stripe limit"
18486
18487         do_facet mds1 $LCTL set_param -n \
18488                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
18489         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18490                 error "Can't create DoM component size after limit change"
18491         do_facet mds1 $LCTL set_param -n \
18492                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
18493         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
18494                 error "Can't create DoM file after limit decrease"
18495         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
18496                 error "Can create big DoM component after limit decrease"
18497         touch ${dom}_def ||
18498                 error "Can't create file with old default layout"
18499
18500         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
18501         return 0
18502 }
18503 run_test 270f "DoM: maximum DoM stripe size checks"
18504
18505 test_271a() {
18506         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18507                 skip "Need MDS version at least 2.10.55"
18508
18509         local dom=$DIR/$tdir/dom
18510
18511         mkdir -p $DIR/$tdir
18512
18513         $LFS setstripe -E 1024K -L mdt $dom
18514
18515         lctl set_param -n mdc.*.stats=clear
18516         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18517         cat $dom > /dev/null
18518         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
18519         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
18520         ls $dom
18521         rm -f $dom
18522 }
18523 run_test 271a "DoM: data is cached for read after write"
18524
18525 test_271b() {
18526         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18527                 skip "Need MDS version at least 2.10.55"
18528
18529         local dom=$DIR/$tdir/dom
18530
18531         mkdir -p $DIR/$tdir
18532
18533         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18534
18535         lctl set_param -n mdc.*.stats=clear
18536         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18537         cancel_lru_locks mdc
18538         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
18539         # second stat to check size is cached on client
18540         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
18541         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18542         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
18543         rm -f $dom
18544 }
18545 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
18546
18547 test_271ba() {
18548         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18549                 skip "Need MDS version at least 2.10.55"
18550
18551         local dom=$DIR/$tdir/dom
18552
18553         mkdir -p $DIR/$tdir
18554
18555         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18556
18557         lctl set_param -n mdc.*.stats=clear
18558         lctl set_param -n osc.*.stats=clear
18559         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
18560         cancel_lru_locks mdc
18561         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18562         # second stat to check size is cached on client
18563         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18564         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18565         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
18566         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
18567         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
18568         rm -f $dom
18569 }
18570 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
18571
18572
18573 get_mdc_stats() {
18574         local mdtidx=$1
18575         local param=$2
18576         local mdt=MDT$(printf %04x $mdtidx)
18577
18578         if [ -z $param ]; then
18579                 lctl get_param -n mdc.*$mdt*.stats
18580         else
18581                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
18582         fi
18583 }
18584
18585 test_271c() {
18586         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18587                 skip "Need MDS version at least 2.10.55"
18588
18589         local dom=$DIR/$tdir/dom
18590
18591         mkdir -p $DIR/$tdir
18592
18593         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18594
18595         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18596         local facet=mds$((mdtidx + 1))
18597
18598         cancel_lru_locks mdc
18599         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
18600         createmany -o $dom 1000
18601         lctl set_param -n mdc.*.stats=clear
18602         smalliomany -w $dom 1000 200
18603         get_mdc_stats $mdtidx
18604         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18605         # Each file has 1 open, 1 IO enqueues, total 2000
18606         # but now we have also +1 getxattr for security.capability, total 3000
18607         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
18608         unlinkmany $dom 1000
18609
18610         cancel_lru_locks mdc
18611         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
18612         createmany -o $dom 1000
18613         lctl set_param -n mdc.*.stats=clear
18614         smalliomany -w $dom 1000 200
18615         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18616         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
18617         # for OPEN and IO lock.
18618         [ $((enq - enq_2)) -ge 1000 ] ||
18619                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
18620         unlinkmany $dom 1000
18621         return 0
18622 }
18623 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
18624
18625 cleanup_271def_tests() {
18626         trap 0
18627         rm -f $1
18628 }
18629
18630 test_271d() {
18631         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
18632                 skip "Need MDS version at least 2.10.57"
18633
18634         local dom=$DIR/$tdir/dom
18635         local tmp=$TMP/$tfile
18636         trap "cleanup_271def_tests $tmp" EXIT
18637
18638         mkdir -p $DIR/$tdir
18639
18640         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18641
18642         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
18643
18644         cancel_lru_locks mdc
18645         dd if=/dev/urandom of=$tmp bs=1000 count=1
18646         dd if=$tmp of=$dom bs=1000 count=1
18647         cancel_lru_locks mdc
18648
18649         cat /etc/hosts >> $tmp
18650         lctl set_param -n mdc.*.stats=clear
18651
18652         # append data to the same file it should update local page
18653         echo "Append to the same page"
18654         cat /etc/hosts >> $dom
18655         local num=$(get_mdc_stats $mdtidx ost_read)
18656         local ra=$(get_mdc_stats $mdtidx req_active)
18657         local rw=$(get_mdc_stats $mdtidx req_waittime)
18658
18659         [ -z $num ] || error "$num READ RPC occured"
18660         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18661         echo "... DONE"
18662
18663         # compare content
18664         cmp $tmp $dom || error "file miscompare"
18665
18666         cancel_lru_locks mdc
18667         lctl set_param -n mdc.*.stats=clear
18668
18669         echo "Open and read file"
18670         cat $dom > /dev/null
18671         local num=$(get_mdc_stats $mdtidx ost_read)
18672         local ra=$(get_mdc_stats $mdtidx req_active)
18673         local rw=$(get_mdc_stats $mdtidx req_waittime)
18674
18675         [ -z $num ] || error "$num READ RPC occured"
18676         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18677         echo "... DONE"
18678
18679         # compare content
18680         cmp $tmp $dom || error "file miscompare"
18681
18682         return 0
18683 }
18684 run_test 271d "DoM: read on open (1K file in reply buffer)"
18685
18686 test_271f() {
18687         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
18688                 skip "Need MDS version at least 2.10.57"
18689
18690         local dom=$DIR/$tdir/dom
18691         local tmp=$TMP/$tfile
18692         trap "cleanup_271def_tests $tmp" EXIT
18693
18694         mkdir -p $DIR/$tdir
18695
18696         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18697
18698         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
18699
18700         cancel_lru_locks mdc
18701         dd if=/dev/urandom of=$tmp bs=200000 count=1
18702         dd if=$tmp of=$dom bs=200000 count=1
18703         cancel_lru_locks mdc
18704         cat /etc/hosts >> $tmp
18705         lctl set_param -n mdc.*.stats=clear
18706
18707         echo "Append to the same page"
18708         cat /etc/hosts >> $dom
18709         local num=$(get_mdc_stats $mdtidx ost_read)
18710         local ra=$(get_mdc_stats $mdtidx req_active)
18711         local rw=$(get_mdc_stats $mdtidx req_waittime)
18712
18713         [ -z $num ] || error "$num READ RPC occured"
18714         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18715         echo "... DONE"
18716
18717         # compare content
18718         cmp $tmp $dom || error "file miscompare"
18719
18720         cancel_lru_locks mdc
18721         lctl set_param -n mdc.*.stats=clear
18722
18723         echo "Open and read file"
18724         cat $dom > /dev/null
18725         local num=$(get_mdc_stats $mdtidx ost_read)
18726         local ra=$(get_mdc_stats $mdtidx req_active)
18727         local rw=$(get_mdc_stats $mdtidx req_waittime)
18728
18729         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
18730         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18731         echo "... DONE"
18732
18733         # compare content
18734         cmp $tmp $dom || error "file miscompare"
18735
18736         return 0
18737 }
18738 run_test 271f "DoM: read on open (200K file and read tail)"
18739
18740 test_271g() {
18741         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
18742                 skip "Skipping due to old client or server version"
18743
18744         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
18745         # to get layout
18746         $CHECKSTAT -t file $DIR1/$tfile
18747
18748         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
18749         MULTIOP_PID=$!
18750         sleep 1
18751         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
18752         $LCTL set_param fail_loc=0x80000314
18753         rm $DIR1/$tfile || error "Unlink fails"
18754         RC=$?
18755         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
18756         [ $RC -eq 0 ] || error "Failed write to stale object"
18757 }
18758 run_test 271g "Discard DoM data vs client flush race"
18759
18760 test_272a() {
18761         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18762                 skip "Need MDS version at least 2.11.50"
18763
18764         local dom=$DIR/$tdir/dom
18765         mkdir -p $DIR/$tdir
18766
18767         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
18768         dd if=/dev/urandom of=$dom bs=512K count=1 ||
18769                 error "failed to write data into $dom"
18770         local old_md5=$(md5sum $dom)
18771
18772         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
18773                 error "failed to migrate to the same DoM component"
18774
18775         [ $($LFS getstripe -c $dom) -eq 2 ] ||
18776                 error "layout was not changed silently"
18777
18778         local new_md5=$(md5sum $dom)
18779
18780         [ "$old_md5" != "$new_md5" ] &&
18781                 error "md5sum differ: $old_md5, $new_md5"
18782         return 0
18783 }
18784 run_test 272a "DoM migration: new layout with the same DOM component"
18785
18786 test_272b() {
18787         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18788                 skip "Need MDS version at least 2.11.50"
18789
18790         local dom=$DIR/$tdir/dom
18791         mkdir -p $DIR/$tdir
18792         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
18793
18794         local mdtidx=$($LFS getstripe -m $dom)
18795         local mdtname=MDT$(printf %04x $mdtidx)
18796         local facet=mds$((mdtidx + 1))
18797
18798         local mdtfree1=$(do_facet $facet \
18799                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18800         dd if=/dev/urandom of=$dom bs=2M count=1 ||
18801                 error "failed to write data into $dom"
18802         local old_md5=$(md5sum $dom)
18803         cancel_lru_locks mdc
18804         local mdtfree1=$(do_facet $facet \
18805                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18806
18807         $LFS migrate -c2 $dom ||
18808                 error "failed to migrate to the new composite layout"
18809         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
18810                 error "MDT stripe was not removed"
18811
18812         cancel_lru_locks mdc
18813         local new_md5=$(md5sum $dom)
18814         [ "$old_md5" != "$new_md5" ] &&
18815                 error "$old_md5 != $new_md5"
18816
18817         # Skip free space checks with ZFS
18818         if [ "$(facet_fstype $facet)" != "zfs" ]; then
18819                 local mdtfree2=$(do_facet $facet \
18820                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18821                 [ $mdtfree2 -gt $mdtfree1 ] ||
18822                         error "MDT space is not freed after migration"
18823         fi
18824         return 0
18825 }
18826 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
18827
18828 test_272c() {
18829         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18830                 skip "Need MDS version at least 2.11.50"
18831
18832         local dom=$DIR/$tdir/$tfile
18833         mkdir -p $DIR/$tdir
18834         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
18835
18836         local mdtidx=$($LFS getstripe -m $dom)
18837         local mdtname=MDT$(printf %04x $mdtidx)
18838         local facet=mds$((mdtidx + 1))
18839
18840         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
18841                 error "failed to write data into $dom"
18842         local old_md5=$(md5sum $dom)
18843         cancel_lru_locks mdc
18844         local mdtfree1=$(do_facet $facet \
18845                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18846
18847         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
18848                 error "failed to migrate to the new composite layout"
18849         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
18850                 error "MDT stripe was not removed"
18851
18852         cancel_lru_locks mdc
18853         local new_md5=$(md5sum $dom)
18854         [ "$old_md5" != "$new_md5" ] &&
18855                 error "$old_md5 != $new_md5"
18856
18857         # Skip free space checks with ZFS
18858         if [ "$(facet_fstype $facet)" != "zfs" ]; then
18859                 local mdtfree2=$(do_facet $facet \
18860                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18861                 [ $mdtfree2 -gt $mdtfree1 ] ||
18862                         error "MDS space is not freed after migration"
18863         fi
18864         return 0
18865 }
18866 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
18867
18868 test_273a() {
18869         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18870                 skip "Need MDS version at least 2.11.50"
18871
18872         # Layout swap cannot be done if either file has DOM component,
18873         # this will never be supported, migration should be used instead
18874
18875         local dom=$DIR/$tdir/$tfile
18876         mkdir -p $DIR/$tdir
18877
18878         $LFS setstripe -c2 ${dom}_plain
18879         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
18880         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
18881                 error "can swap layout with DoM component"
18882         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
18883                 error "can swap layout with DoM component"
18884
18885         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
18886         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
18887                 error "can swap layout with DoM component"
18888         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
18889                 error "can swap layout with DoM component"
18890         return 0
18891 }
18892 run_test 273a "DoM: layout swapping should fail with DOM"
18893
18894 test_275() {
18895         remote_ost_nodsh && skip "remote OST with nodsh"
18896         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
18897                 skip "Need OST version >= 2.10.57"
18898
18899         local file=$DIR/$tfile
18900         local oss
18901
18902         oss=$(comma_list $(osts_nodes))
18903
18904         dd if=/dev/urandom of=$file bs=1M count=2 ||
18905                 error "failed to create a file"
18906         cancel_lru_locks osc
18907
18908         #lock 1
18909         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
18910                 error "failed to read a file"
18911
18912 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
18913         $LCTL set_param fail_loc=0x8000031f
18914
18915         cancel_lru_locks osc &
18916         sleep 1
18917
18918 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
18919         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
18920         #IO takes another lock, but matches the PENDING one
18921         #and places it to the IO RPC
18922         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
18923                 error "failed to read a file with PENDING lock"
18924 }
18925 run_test 275 "Read on a canceled duplicate lock"
18926
18927 test_276() {
18928         remote_ost_nodsh && skip "remote OST with nodsh"
18929         local pid
18930
18931         do_facet ost1 "(while true; do \
18932                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
18933                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
18934         pid=$!
18935
18936         for LOOP in $(seq 20); do
18937                 stop ost1
18938                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
18939         done
18940         kill -9 $pid
18941         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
18942                 rm $TMP/sanity_276_pid"
18943 }
18944 run_test 276 "Race between mount and obd_statfs"
18945
18946 cleanup_test_300() {
18947         trap 0
18948         umask $SAVE_UMASK
18949 }
18950 test_striped_dir() {
18951         local mdt_index=$1
18952         local stripe_count
18953         local stripe_index
18954
18955         mkdir -p $DIR/$tdir
18956
18957         SAVE_UMASK=$(umask)
18958         trap cleanup_test_300 RETURN EXIT
18959
18960         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
18961                                                 $DIR/$tdir/striped_dir ||
18962                 error "set striped dir error"
18963
18964         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
18965         [ "$mode" = "755" ] || error "expect 755 got $mode"
18966
18967         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
18968                 error "getdirstripe failed"
18969         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
18970         if [ "$stripe_count" != "2" ]; then
18971                 error "1:stripe_count is $stripe_count, expect 2"
18972         fi
18973         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
18974         if [ "$stripe_count" != "2" ]; then
18975                 error "2:stripe_count is $stripe_count, expect 2"
18976         fi
18977
18978         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
18979         if [ "$stripe_index" != "$mdt_index" ]; then
18980                 error "stripe_index is $stripe_index, expect $mdt_index"
18981         fi
18982
18983         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
18984                 error "nlink error after create striped dir"
18985
18986         mkdir $DIR/$tdir/striped_dir/a
18987         mkdir $DIR/$tdir/striped_dir/b
18988
18989         stat $DIR/$tdir/striped_dir/a ||
18990                 error "create dir under striped dir failed"
18991         stat $DIR/$tdir/striped_dir/b ||
18992                 error "create dir under striped dir failed"
18993
18994         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
18995                 error "nlink error after mkdir"
18996
18997         rmdir $DIR/$tdir/striped_dir/a
18998         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
18999                 error "nlink error after rmdir"
19000
19001         rmdir $DIR/$tdir/striped_dir/b
19002         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19003                 error "nlink error after rmdir"
19004
19005         chattr +i $DIR/$tdir/striped_dir
19006         createmany -o $DIR/$tdir/striped_dir/f 10 &&
19007                 error "immutable flags not working under striped dir!"
19008         chattr -i $DIR/$tdir/striped_dir
19009
19010         rmdir $DIR/$tdir/striped_dir ||
19011                 error "rmdir striped dir error"
19012
19013         cleanup_test_300
19014
19015         true
19016 }
19017
19018 test_300a() {
19019         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19020                 skip "skipped for lustre < 2.7.0"
19021         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19022         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19023
19024         test_striped_dir 0 || error "failed on striped dir on MDT0"
19025         test_striped_dir 1 || error "failed on striped dir on MDT0"
19026 }
19027 run_test 300a "basic striped dir sanity test"
19028
19029 test_300b() {
19030         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19031                 skip "skipped for lustre < 2.7.0"
19032         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19033         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19034
19035         local i
19036         local mtime1
19037         local mtime2
19038         local mtime3
19039
19040         test_mkdir $DIR/$tdir || error "mkdir fail"
19041         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19042                 error "set striped dir error"
19043         for i in {0..9}; do
19044                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
19045                 sleep 1
19046                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
19047                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
19048                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
19049                 sleep 1
19050                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
19051                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
19052                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
19053         done
19054         true
19055 }
19056 run_test 300b "check ctime/mtime for striped dir"
19057
19058 test_300c() {
19059         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19060                 skip "skipped for lustre < 2.7.0"
19061         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19062         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19063
19064         local file_count
19065
19066         mkdir -p $DIR/$tdir
19067         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
19068                 error "set striped dir error"
19069
19070         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
19071                 error "chown striped dir failed"
19072
19073         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
19074                 error "create 5k files failed"
19075
19076         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
19077
19078         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
19079
19080         rm -rf $DIR/$tdir
19081 }
19082 run_test 300c "chown && check ls under striped directory"
19083
19084 test_300d() {
19085         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19086                 skip "skipped for lustre < 2.7.0"
19087         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19088         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19089
19090         local stripe_count
19091         local file
19092
19093         mkdir -p $DIR/$tdir
19094         $SETSTRIPE -c 2 $DIR/$tdir
19095
19096         #local striped directory
19097         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19098                 error "set striped dir error"
19099         #look at the directories for debug purposes
19100         ls -l $DIR/$tdir
19101         $LFS getdirstripe $DIR/$tdir
19102         ls -l $DIR/$tdir/striped_dir
19103         $LFS getdirstripe $DIR/$tdir/striped_dir
19104         createmany -o $DIR/$tdir/striped_dir/f 10 ||
19105                 error "create 10 files failed"
19106
19107         #remote striped directory
19108         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
19109                 error "set striped dir error"
19110         #look at the directories for debug purposes
19111         ls -l $DIR/$tdir
19112         $LFS getdirstripe $DIR/$tdir
19113         ls -l $DIR/$tdir/remote_striped_dir
19114         $LFS getdirstripe $DIR/$tdir/remote_striped_dir
19115         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
19116                 error "create 10 files failed"
19117
19118         for file in $(find $DIR/$tdir); do
19119                 stripe_count=$($LFS getstripe -c $file)
19120                 [ $stripe_count -eq 2 ] ||
19121                         error "wrong stripe $stripe_count for $file"
19122         done
19123
19124         rm -rf $DIR/$tdir
19125 }
19126 run_test 300d "check default stripe under striped directory"
19127
19128 test_300e() {
19129         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19130                 skip "Need MDS version at least 2.7.55"
19131         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19132         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19133
19134         local stripe_count
19135         local file
19136
19137         mkdir -p $DIR/$tdir
19138
19139         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19140                 error "set striped dir error"
19141
19142         touch $DIR/$tdir/striped_dir/a
19143         touch $DIR/$tdir/striped_dir/b
19144         touch $DIR/$tdir/striped_dir/c
19145
19146         mkdir $DIR/$tdir/striped_dir/dir_a
19147         mkdir $DIR/$tdir/striped_dir/dir_b
19148         mkdir $DIR/$tdir/striped_dir/dir_c
19149
19150         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
19151                 error "set striped adir under striped dir error"
19152
19153         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
19154                 error "set striped bdir under striped dir error"
19155
19156         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
19157                 error "set striped cdir under striped dir error"
19158
19159         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
19160                 error "rename dir under striped dir fails"
19161
19162         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
19163                 error "rename dir under different stripes fails"
19164
19165         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
19166                 error "rename file under striped dir should succeed"
19167
19168         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
19169                 error "rename dir under striped dir should succeed"
19170
19171         rm -rf $DIR/$tdir
19172 }
19173 run_test 300e "check rename under striped directory"
19174
19175 test_300f() {
19176         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19177         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19178         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19179                 skip "Need MDS version at least 2.7.55"
19180
19181         local stripe_count
19182         local file
19183
19184         rm -rf $DIR/$tdir
19185         mkdir -p $DIR/$tdir
19186
19187         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19188                 error "set striped dir error"
19189
19190         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
19191                 error "set striped dir error"
19192
19193         touch $DIR/$tdir/striped_dir/a
19194         mkdir $DIR/$tdir/striped_dir/dir_a
19195         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
19196                 error "create striped dir under striped dir fails"
19197
19198         touch $DIR/$tdir/striped_dir1/b
19199         mkdir $DIR/$tdir/striped_dir1/dir_b
19200         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
19201                 error "create striped dir under striped dir fails"
19202
19203         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
19204                 error "rename dir under different striped dir should fail"
19205
19206         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
19207                 error "rename striped dir under diff striped dir should fail"
19208
19209         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
19210                 error "rename file under diff striped dirs fails"
19211
19212         rm -rf $DIR/$tdir
19213 }
19214 run_test 300f "check rename cross striped directory"
19215
19216 test_300_check_default_striped_dir()
19217 {
19218         local dirname=$1
19219         local default_count=$2
19220         local default_index=$3
19221         local stripe_count
19222         local stripe_index
19223         local dir_stripe_index
19224         local dir
19225
19226         echo "checking $dirname $default_count $default_index"
19227         $LFS setdirstripe -D -c $default_count -i $default_index \
19228                                 -t all_char $DIR/$tdir/$dirname ||
19229                 error "set default stripe on striped dir error"
19230         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
19231         [ $stripe_count -eq $default_count ] ||
19232                 error "expect $default_count get $stripe_count for $dirname"
19233
19234         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
19235         [ $stripe_index -eq $default_index ] ||
19236                 error "expect $default_index get $stripe_index for $dirname"
19237
19238         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
19239                                                 error "create dirs failed"
19240
19241         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
19242         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
19243         for dir in $(find $DIR/$tdir/$dirname/*); do
19244                 stripe_count=$($LFS getdirstripe -c $dir)
19245                 [ $stripe_count -eq $default_count ] ||
19246                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
19247                 error "stripe count $default_count != $stripe_count for $dir"
19248
19249                 stripe_index=$($LFS getdirstripe -i $dir)
19250                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
19251                         error "$stripe_index != $default_index for $dir"
19252
19253                 #check default stripe
19254                 stripe_count=$($LFS getdirstripe -D -c $dir)
19255                 [ $stripe_count -eq $default_count ] ||
19256                 error "default count $default_count != $stripe_count for $dir"
19257
19258                 stripe_index=$($LFS getdirstripe -D -i $dir)
19259                 [ $stripe_index -eq $default_index ] ||
19260                 error "default index $default_index != $stripe_index for $dir"
19261         done
19262         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
19263 }
19264
19265 test_300g() {
19266         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19267         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19268                 skip "Need MDS version at least 2.7.55"
19269
19270         local dir
19271         local stripe_count
19272         local stripe_index
19273
19274         mkdir $DIR/$tdir
19275         mkdir $DIR/$tdir/normal_dir
19276
19277         #Checking when client cache stripe index
19278         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
19279         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
19280                 error "create striped_dir failed"
19281
19282         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
19283                 error "create dir0 fails"
19284         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
19285         [ $stripe_index -eq 0 ] ||
19286                 error "dir0 expect index 0 got $stripe_index"
19287
19288         mkdir $DIR/$tdir/striped_dir/dir1 ||
19289                 error "create dir1 fails"
19290         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
19291         [ $stripe_index -eq 1 ] ||
19292                 error "dir1 expect index 1 got $stripe_index"
19293
19294         #check default stripe count/stripe index
19295         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
19296         test_300_check_default_striped_dir normal_dir 1 0
19297         test_300_check_default_striped_dir normal_dir 2 1
19298         test_300_check_default_striped_dir normal_dir 2 -1
19299
19300         #delete default stripe information
19301         echo "delete default stripeEA"
19302         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
19303                 error "set default stripe on striped dir error"
19304
19305         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
19306         for dir in $(find $DIR/$tdir/normal_dir/*); do
19307                 stripe_count=$($LFS getdirstripe -c $dir)
19308                 [ $stripe_count -eq 0 ] ||
19309                         error "expect 1 get $stripe_count for $dir"
19310                 stripe_index=$($LFS getdirstripe -i $dir)
19311                 [ $stripe_index -eq 0 ] ||
19312                         error "expect 0 get $stripe_index for $dir"
19313         done
19314 }
19315 run_test 300g "check default striped directory for normal directory"
19316
19317 test_300h() {
19318         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19319         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19320                 skip "Need MDS version at least 2.7.55"
19321
19322         local dir
19323         local stripe_count
19324
19325         mkdir $DIR/$tdir
19326         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19327                 error "set striped dir error"
19328
19329         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
19330         test_300_check_default_striped_dir striped_dir 1 0
19331         test_300_check_default_striped_dir striped_dir 2 1
19332         test_300_check_default_striped_dir striped_dir 2 -1
19333
19334         #delete default stripe information
19335         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
19336                 error "set default stripe on striped dir error"
19337
19338         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
19339         for dir in $(find $DIR/$tdir/striped_dir/*); do
19340                 stripe_count=$($LFS getdirstripe -c $dir)
19341                 [ $stripe_count -eq 0 ] ||
19342                         error "expect 1 get $stripe_count for $dir"
19343         done
19344 }
19345 run_test 300h "check default striped directory for striped directory"
19346
19347 test_300i() {
19348         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19349         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19350         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19351                 skip "Need MDS version at least 2.7.55"
19352
19353         local stripe_count
19354         local file
19355
19356         mkdir $DIR/$tdir
19357
19358         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19359                 error "set striped dir error"
19360
19361         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
19362                 error "create files under striped dir failed"
19363
19364         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
19365                 error "set striped hashdir error"
19366
19367         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
19368                 error "create dir0 under hash dir failed"
19369         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
19370                 error "create dir1 under hash dir failed"
19371
19372         # unfortunately, we need to umount to clear dir layout cache for now
19373         # once we fully implement dir layout, we can drop this
19374         umount_client $MOUNT || error "umount failed"
19375         mount_client $MOUNT || error "mount failed"
19376
19377         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
19378         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
19379         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
19380
19381         #set the stripe to be unknown hash type
19382         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
19383         $LCTL set_param fail_loc=0x1901
19384         for ((i = 0; i < 10; i++)); do
19385                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
19386                         error "stat f-$i failed"
19387                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
19388         done
19389
19390         touch $DIR/$tdir/striped_dir/f0 &&
19391                 error "create under striped dir with unknown hash should fail"
19392
19393         $LCTL set_param fail_loc=0
19394
19395         umount_client $MOUNT || error "umount failed"
19396         mount_client $MOUNT || error "mount failed"
19397
19398         return 0
19399 }
19400 run_test 300i "client handle unknown hash type striped directory"
19401
19402 test_300j() {
19403         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19404         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19405         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19406                 skip "Need MDS version at least 2.7.55"
19407
19408         local stripe_count
19409         local file
19410
19411         mkdir $DIR/$tdir
19412
19413         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
19414         $LCTL set_param fail_loc=0x1702
19415         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19416                 error "set striped dir error"
19417
19418         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
19419                 error "create files under striped dir failed"
19420
19421         $LCTL set_param fail_loc=0
19422
19423         rm -rf $DIR/$tdir || error "unlink striped dir fails"
19424
19425         return 0
19426 }
19427 run_test 300j "test large update record"
19428
19429 test_300k() {
19430         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19431         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19432         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19433                 skip "Need MDS version at least 2.7.55"
19434
19435         local stripe_count
19436         local file
19437
19438         mkdir $DIR/$tdir
19439
19440         #define OBD_FAIL_LARGE_STRIPE   0x1703
19441         $LCTL set_param fail_loc=0x1703
19442         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
19443                 error "set striped dir error"
19444         $LCTL set_param fail_loc=0
19445
19446         $LFS getdirstripe $DIR/$tdir/striped_dir ||
19447                 error "getstripeddir fails"
19448         rm -rf $DIR/$tdir/striped_dir ||
19449                 error "unlink striped dir fails"
19450
19451         return 0
19452 }
19453 run_test 300k "test large striped directory"
19454
19455 test_300l() {
19456         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19457         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19458         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19459                 skip "Need MDS version at least 2.7.55"
19460
19461         local stripe_index
19462
19463         test_mkdir -p $DIR/$tdir/striped_dir
19464         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
19465                         error "chown $RUNAS_ID failed"
19466         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
19467                 error "set default striped dir failed"
19468
19469         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
19470         $LCTL set_param fail_loc=0x80000158
19471         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
19472
19473         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
19474         [ $stripe_index -eq 1 ] ||
19475                 error "expect 1 get $stripe_index for $dir"
19476 }
19477 run_test 300l "non-root user to create dir under striped dir with stale layout"
19478
19479 test_300m() {
19480         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19481         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
19482         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19483                 skip "Need MDS version at least 2.7.55"
19484
19485         mkdir -p $DIR/$tdir/striped_dir
19486         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
19487                 error "set default stripes dir error"
19488
19489         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
19490
19491         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
19492         [ $stripe_count -eq 0 ] ||
19493                         error "expect 0 get $stripe_count for a"
19494
19495         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
19496                 error "set default stripes dir error"
19497
19498         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
19499
19500         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
19501         [ $stripe_count -eq 0 ] ||
19502                         error "expect 0 get $stripe_count for b"
19503
19504         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
19505                 error "set default stripes dir error"
19506
19507         mkdir $DIR/$tdir/striped_dir/c &&
19508                 error "default stripe_index is invalid, mkdir c should fails"
19509
19510         rm -rf $DIR/$tdir || error "rmdir fails"
19511 }
19512 run_test 300m "setstriped directory on single MDT FS"
19513
19514 cleanup_300n() {
19515         local list=$(comma_list $(mdts_nodes))
19516
19517         trap 0
19518         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19519 }
19520
19521 test_300n() {
19522         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19523         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19524         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19525                 skip "Need MDS version at least 2.7.55"
19526         remote_mds_nodsh && skip "remote MDS with nodsh"
19527
19528         local stripe_index
19529         local list=$(comma_list $(mdts_nodes))
19530
19531         trap cleanup_300n RETURN EXIT
19532         mkdir -p $DIR/$tdir
19533         chmod 777 $DIR/$tdir
19534         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
19535                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
19536                 error "create striped dir succeeds with gid=0"
19537
19538         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
19539         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
19540                 error "create striped dir fails with gid=-1"
19541
19542         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19543         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
19544                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
19545                 error "set default striped dir succeeds with gid=0"
19546
19547
19548         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
19549         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
19550                 error "set default striped dir fails with gid=-1"
19551
19552
19553         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19554         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
19555                                         error "create test_dir fails"
19556         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
19557                                         error "create test_dir1 fails"
19558         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
19559                                         error "create test_dir2 fails"
19560         cleanup_300n
19561 }
19562 run_test 300n "non-root user to create dir under striped dir with default EA"
19563
19564 test_300o() {
19565         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19566         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19567         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19568                 skip "Need MDS version at least 2.7.55"
19569
19570         local numfree1
19571         local numfree2
19572
19573         mkdir -p $DIR/$tdir
19574
19575         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
19576         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
19577         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
19578                 skip "not enough free inodes $numfree1 $numfree2"
19579         fi
19580
19581         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
19582         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
19583         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
19584                 skip "not enough free space $numfree1 $numfree2"
19585         fi
19586
19587         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
19588                 error "setdirstripe fails"
19589
19590         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
19591                 error "create dirs fails"
19592
19593         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
19594         ls $DIR/$tdir/striped_dir > /dev/null ||
19595                 error "ls striped dir fails"
19596         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
19597                 error "unlink big striped dir fails"
19598 }
19599 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
19600
19601 test_300p() {
19602         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19603         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19604         remote_mds_nodsh && skip "remote MDS with nodsh"
19605
19606         mkdir -p $DIR/$tdir
19607
19608         #define OBD_FAIL_OUT_ENOSPC     0x1704
19609         do_facet mds2 lctl set_param fail_loc=0x80001704
19610         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
19611                  && error "create striped directory should fail"
19612
19613         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
19614
19615         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
19616         true
19617 }
19618 run_test 300p "create striped directory without space"
19619
19620 test_300q() {
19621         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19622         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19623
19624         local fd=$(free_fd)
19625         local cmd="exec $fd<$tdir"
19626         cd $DIR
19627         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
19628         eval $cmd
19629         cmd="exec $fd<&-"
19630         trap "eval $cmd" EXIT
19631         cd $tdir || error "cd $tdir fails"
19632         rmdir  ../$tdir || error "rmdir $tdir fails"
19633         mkdir local_dir && error "create dir succeeds"
19634         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
19635         eval $cmd
19636         return 0
19637 }
19638 run_test 300q "create remote directory under orphan directory"
19639
19640 test_300r() {
19641         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
19642                 skip "Need MDS version at least 2.7.55" && return
19643         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
19644
19645         mkdir $DIR/$tdir
19646
19647         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
19648                 error "set striped dir error"
19649
19650         $LFS getdirstripe $DIR/$tdir/striped_dir ||
19651                 error "getstripeddir fails"
19652
19653         local stripe_count
19654         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
19655                       awk '/lmv_stripe_count:/ { print $2 }')
19656
19657         [ $MDSCOUNT -ne $stripe_count ] &&
19658                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
19659
19660         rm -rf $DIR/$tdir/striped_dir ||
19661                 error "unlink striped dir fails"
19662 }
19663 run_test 300r "test -1 striped directory"
19664
19665 test_300s_helper() {
19666         local count=$1
19667
19668         local stripe_dir=$DIR/$tdir/striped_dir.$count
19669
19670         $LFS mkdir -c $count $stripe_dir ||
19671                 error "lfs mkdir -c error"
19672
19673         $LFS getdirstripe $stripe_dir ||
19674                 error "lfs getdirstripe fails"
19675
19676         local stripe_count
19677         stripe_count=$($LFS getdirstripe $stripe_dir |
19678                       awk '/lmv_stripe_count:/ { print $2 }')
19679
19680         [ $count -ne $stripe_count ] &&
19681                 error_noexit "bad stripe count $stripe_count expected $count"
19682
19683         local dupe_stripes
19684         dupe_stripes=$($LFS getdirstripe $stripe_dir |
19685                 awk '/0x/ {count[$1] += 1}; END {
19686                         for (idx in count) {
19687                                 if (count[idx]>1) {
19688                                         print "index " idx " count " count[idx]
19689                                 }
19690                         }
19691                 }')
19692
19693         if [[ -n "$dupe_stripes" ]] ; then
19694                 lfs getdirstripe $stripe_dir
19695                 error_noexit "Dupe MDT above: $dupe_stripes "
19696         fi
19697
19698         rm -rf $stripe_dir ||
19699                 error_noexit "unlink $stripe_dir fails"
19700 }
19701
19702 test_300s() {
19703         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19704                 skip "Need MDS version at least 2.7.55" && return
19705         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
19706
19707         mkdir $DIR/$tdir
19708         for count in $(seq 2 $MDSCOUNT); do
19709                 test_300s_helper $count
19710         done
19711 }
19712 run_test 300s "test lfs mkdir -c without -i"
19713
19714
19715 prepare_remote_file() {
19716         mkdir $DIR/$tdir/src_dir ||
19717                 error "create remote source failed"
19718
19719         cp /etc/hosts $DIR/$tdir/src_dir/a ||
19720                  error "cp to remote source failed"
19721         touch $DIR/$tdir/src_dir/a
19722
19723         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
19724                 error "create remote target dir failed"
19725
19726         touch $DIR/$tdir/tgt_dir/b
19727
19728         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
19729                 error "rename dir cross MDT failed!"
19730
19731         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
19732                 error "src_child still exists after rename"
19733
19734         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
19735                 error "missing file(a) after rename"
19736
19737         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
19738                 error "diff after rename"
19739 }
19740
19741 test_310a() {
19742         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
19743         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19744
19745         local remote_file=$DIR/$tdir/tgt_dir/b
19746
19747         mkdir -p $DIR/$tdir
19748
19749         prepare_remote_file || error "prepare remote file failed"
19750
19751         #open-unlink file
19752         $OPENUNLINK $remote_file $remote_file ||
19753                 error "openunlink $remote_file failed"
19754         $CHECKSTAT -a $remote_file || error "$remote_file exists"
19755 }
19756 run_test 310a "open unlink remote file"
19757
19758 test_310b() {
19759         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
19760         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19761
19762         local remote_file=$DIR/$tdir/tgt_dir/b
19763
19764         mkdir -p $DIR/$tdir
19765
19766         prepare_remote_file || error "prepare remote file failed"
19767
19768         ln $remote_file $DIR/$tfile || error "link failed for remote file"
19769         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
19770         $CHECKSTAT -t file $remote_file || error "check file failed"
19771 }
19772 run_test 310b "unlink remote file with multiple links while open"
19773
19774 test_310c() {
19775         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19776         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
19777
19778         local remote_file=$DIR/$tdir/tgt_dir/b
19779
19780         mkdir -p $DIR/$tdir
19781
19782         prepare_remote_file || error "prepare remote file failed"
19783
19784         ln $remote_file $DIR/$tfile || error "link failed for remote file"
19785         multiop_bg_pause $remote_file O_uc ||
19786                         error "mulitop failed for remote file"
19787         MULTIPID=$!
19788         $MULTIOP $DIR/$tfile Ouc
19789         kill -USR1 $MULTIPID
19790         wait $MULTIPID
19791 }
19792 run_test 310c "open-unlink remote file with multiple links"
19793
19794 #LU-4825
19795 test_311() {
19796         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19797         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
19798         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
19799                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
19800         remote_mds_nodsh && skip "remote MDS with nodsh"
19801
19802         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
19803         local mdts=$(comma_list $(mdts_nodes))
19804
19805         mkdir -p $DIR/$tdir
19806         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
19807         createmany -o $DIR/$tdir/$tfile. 1000
19808
19809         # statfs data is not real time, let's just calculate it
19810         old_iused=$((old_iused + 1000))
19811
19812         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
19813                         osp.*OST0000*MDT0000.create_count")
19814         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
19815                                 osp.*OST0000*MDT0000.max_create_count")
19816         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
19817
19818         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
19819         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
19820         [ $index -ne 0 ] || error "$tfile stripe index is 0"
19821
19822         unlinkmany $DIR/$tdir/$tfile. 1000
19823
19824         do_nodes $mdts "$LCTL set_param -n \
19825                         osp.*OST0000*.max_create_count=$max_count"
19826         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
19827                 do_nodes $mdts "$LCTL set_param -n \
19828                                 osp.*OST0000*.create_count=$count"
19829         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
19830                         grep "=0" && error "create_count is zero"
19831
19832         local new_iused
19833         for i in $(seq 120); do
19834                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
19835                 # system may be too busy to destroy all objs in time, use
19836                 # a somewhat small value to not fail autotest
19837                 [ $((old_iused - new_iused)) -gt 400 ] && break
19838                 sleep 1
19839         done
19840
19841         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
19842         [ $((old_iused - new_iused)) -gt 400 ] ||
19843                 error "objs not destroyed after unlink"
19844 }
19845 run_test 311 "disable OSP precreate, and unlink should destroy objs"
19846
19847 zfs_oid_to_objid()
19848 {
19849         local ost=$1
19850         local objid=$2
19851
19852         local vdevdir=$(dirname $(facet_vdevice $ost))
19853         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
19854         local zfs_zapid=$(do_facet $ost $cmd |
19855                           grep -w "/O/0/d$((objid%32))" -C 5 |
19856                           awk '/Object/{getline; print $1}')
19857         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
19858                           awk "/$objid = /"'{printf $3}')
19859
19860         echo $zfs_objid
19861 }
19862
19863 zfs_object_blksz() {
19864         local ost=$1
19865         local objid=$2
19866
19867         local vdevdir=$(dirname $(facet_vdevice $ost))
19868         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
19869         local blksz=$(do_facet $ost $cmd $objid |
19870                       awk '/dblk/{getline; printf $4}')
19871
19872         case "${blksz: -1}" in
19873                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
19874                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
19875                 *) ;;
19876         esac
19877
19878         echo $blksz
19879 }
19880
19881 test_312() { # LU-4856
19882         remote_ost_nodsh && skip "remote OST with nodsh"
19883         [ "$ost1_FSTYPE" = "zfs" ] ||
19884                 skip_env "the test only applies to zfs"
19885
19886         local max_blksz=$(do_facet ost1 \
19887                           $ZFS get -p recordsize $(facet_device ost1) |
19888                           awk '!/VALUE/{print $3}')
19889
19890         # to make life a little bit easier
19891         $LFS mkdir -c 1 -i 0 $DIR/$tdir
19892         $LFS setstripe -c 1 -i 0 $DIR/$tdir
19893
19894         local tf=$DIR/$tdir/$tfile
19895         touch $tf
19896         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
19897
19898         # Get ZFS object id
19899         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
19900         # block size change by sequential overwrite
19901         local bs
19902
19903         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
19904                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
19905
19906                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
19907                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
19908         done
19909         rm -f $tf
19910
19911         # block size change by sequential append write
19912         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
19913         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
19914         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
19915         local count
19916
19917         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
19918                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
19919                         oflag=sync conv=notrunc
19920
19921                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
19922                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
19923                         error "blksz error, actual $blksz, " \
19924                                 "expected: 2 * $count * $PAGE_SIZE"
19925         done
19926         rm -f $tf
19927
19928         # random write
19929         touch $tf
19930         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
19931         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
19932
19933         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
19934         blksz=$(zfs_object_blksz ost1 $zfs_objid)
19935         [ $blksz -eq $PAGE_SIZE ] ||
19936                 error "blksz error: $blksz, expected: $PAGE_SIZE"
19937
19938         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
19939         blksz=$(zfs_object_blksz ost1 $zfs_objid)
19940         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
19941
19942         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
19943         blksz=$(zfs_object_blksz ost1 $zfs_objid)
19944         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
19945 }
19946 run_test 312 "make sure ZFS adjusts its block size by write pattern"
19947
19948 test_313() {
19949         remote_ost_nodsh && skip "remote OST with nodsh"
19950
19951         local file=$DIR/$tfile
19952
19953         rm -f $file
19954         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
19955
19956         # define OBD_FAIL_TGT_RCVD_EIO           0x720
19957         do_facet ost1 "$LCTL set_param fail_loc=0x720"
19958         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
19959                 error "write should failed"
19960         do_facet ost1 "$LCTL set_param fail_loc=0"
19961         rm -f $file
19962 }
19963 run_test 313 "io should fail after last_rcvd update fail"
19964
19965 test_314() {
19966         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
19967
19968         $SETSTRIPE -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
19969         do_facet ost1 "$LCTL set_param fail_loc=0x720"
19970         rm -f $DIR/$tfile
19971         wait_delete_completed
19972         do_facet ost1 "$LCTL set_param fail_loc=0"
19973 }
19974 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
19975
19976 test_315() { # LU-618
19977         local file=$DIR/$tfile
19978         rm -f $file
19979
19980         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
19981                 error "multiop file write failed"
19982         $MULTIOP $file oO_RDONLY:r4063232_c &
19983         PID=$!
19984
19985         sleep 2
19986
19987         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
19988         kill -USR1 $PID
19989
19990         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
19991         rm -f $file
19992 }
19993 run_test 315 "read should be accounted"
19994
19995 test_316() {
19996         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19997         large_xattr_enabled || skip_env "ea_inode feature disabled"
19998
19999         rm -rf $DIR/$tdir/d
20000         mkdir -p $DIR/$tdir/d
20001         chown nobody $DIR/$tdir/d
20002         touch $DIR/$tdir/d/file
20003
20004         $LFS mv -M1 $DIR/$tdir/d || error "lfs mv failed"
20005 }
20006 run_test 316 "lfs mv"
20007
20008 test_317() {
20009         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
20010                 skip "Need MDS version at least 2.11.53"
20011         if [ "$ost1_FSTYPE" == "zfs" ]; then
20012                 skip "LU-10370: no implementation for ZFS"
20013         fi
20014
20015         local trunc_sz
20016         local grant_blk_size
20017
20018         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
20019                         awk '/grant_block_size:/ { print $2; exit; }')
20020         #
20021         # Create File of size 5M. Truncate it to below size's and verify
20022         # blocks count.
20023         #
20024         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
20025                 error "Create file $DIR/$tfile failed"
20026         stack_trap "rm -f $DIR/$tfile" EXIT
20027
20028         for trunc_sz in 2097152 4097 4000 509 0; do
20029                 $TRUNCATE $DIR/$tfile $trunc_sz ||
20030                         error "truncate $tfile to $trunc_sz failed"
20031                 local sz=$(stat --format=%s $DIR/$tfile)
20032                 local blk=$(stat --format=%b $DIR/$tfile)
20033                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
20034                                      grant_blk_size) * 8))
20035
20036                 if [[ $blk -ne $trunc_blk ]]; then
20037                         $(which stat) $DIR/$tfile
20038                         error "Expected Block $trunc_blk got $blk for $tfile"
20039                 fi
20040
20041                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20042                         error "Expected Size $trunc_sz got $sz for $tfile"
20043         done
20044
20045         #
20046         # sparse file test
20047         # Create file with a hole and write actual two blocks. Block count
20048         # must be 16.
20049         #
20050         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
20051                 conv=fsync || error "Create file : $DIR/$tfile"
20052
20053         # Calculate the final truncate size.
20054         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
20055
20056         #
20057         # truncate to size $trunc_sz bytes. Strip the last block
20058         # The block count must drop to 8
20059         #
20060         $TRUNCATE $DIR/$tfile $trunc_sz ||
20061                 error "truncate $tfile to $trunc_sz failed"
20062
20063         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
20064         sz=$(stat --format=%s $DIR/$tfile)
20065         blk=$(stat --format=%b $DIR/$tfile)
20066
20067         if [[ $blk -ne $trunc_bsz ]]; then
20068                 $(which stat) $DIR/$tfile
20069                 error "Expected Block $trunc_bsz got $blk for $tfile"
20070         fi
20071
20072         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20073                 error "Expected Size $trunc_sz got $sz for $tfile"
20074 }
20075 run_test 317 "Verify blocks get correctly update after truncate"
20076
20077 test_fake_rw() {
20078         local read_write=$1
20079         if [ "$read_write" = "write" ]; then
20080                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
20081         elif [ "$read_write" = "read" ]; then
20082                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
20083         else
20084                 error "argument error"
20085         fi
20086
20087         # turn off debug for performance testing
20088         local saved_debug=$($LCTL get_param -n debug)
20089         $LCTL set_param debug=0
20090
20091         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
20092
20093         # get ost1 size - lustre-OST0000
20094         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
20095         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
20096         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
20097
20098         if [ "$read_write" = "read" ]; then
20099                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
20100         fi
20101
20102         local start_time=$(date +%s.%N)
20103         $dd_cmd bs=1M count=$blocks oflag=sync ||
20104                 error "real dd $read_write error"
20105         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
20106
20107         if [ "$read_write" = "write" ]; then
20108                 rm -f $DIR/$tfile
20109         fi
20110
20111         # define OBD_FAIL_OST_FAKE_RW           0x238
20112         do_facet ost1 $LCTL set_param fail_loc=0x238
20113
20114         local start_time=$(date +%s.%N)
20115         $dd_cmd bs=1M count=$blocks oflag=sync ||
20116                 error "fake dd $read_write error"
20117         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
20118
20119         if [ "$read_write" = "write" ]; then
20120                 # verify file size
20121                 cancel_lru_locks osc
20122                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
20123                         error "$tfile size not $blocks MB"
20124         fi
20125         do_facet ost1 $LCTL set_param fail_loc=0
20126
20127         echo "fake $read_write $duration_fake vs. normal $read_write" \
20128                 "$duration in seconds"
20129         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
20130                 error_not_in_vm "fake write is slower"
20131
20132         $LCTL set_param -n debug="$saved_debug"
20133         rm -f $DIR/$tfile
20134 }
20135 test_399a() { # LU-7655 for OST fake write
20136         remote_ost_nodsh && skip "remote OST with nodsh"
20137
20138         test_fake_rw write
20139 }
20140 run_test 399a "fake write should not be slower than normal write"
20141
20142 test_399b() { # LU-8726 for OST fake read
20143         remote_ost_nodsh && skip "remote OST with nodsh"
20144         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
20145                 skip_env "ldiskfs only test"
20146         fi
20147
20148         test_fake_rw read
20149 }
20150 run_test 399b "fake read should not be slower than normal read"
20151
20152 test_400a() { # LU-1606, was conf-sanity test_74
20153         if ! which $CC > /dev/null 2>&1; then
20154                 skip_env "$CC is not installed"
20155         fi
20156
20157         local extra_flags=''
20158         local out=$TMP/$tfile
20159         local prefix=/usr/include/lustre
20160         local prog
20161
20162         # Oleg removes c files in his test rig so test if any c files exist
20163         [ -z "$(ls -A $LUSTRE_TESTS_API_DIR)" ] && \
20164                 skip_env "Needed c test files are missing"
20165
20166         if ! [[ -d $prefix ]]; then
20167                 # Assume we're running in tree and fixup the include path.
20168                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
20169                 extra_flags+=" -L$LUSTRE/utils/.lib"
20170         fi
20171
20172         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
20173                 $CC -Wall -Werror -std=c99 $extra_flags -o $out $prog -llustreapi ||
20174                         error "client api broken"
20175         done
20176         rm -f $out
20177 }
20178 run_test 400a "Lustre client api program can compile and link"
20179
20180 test_400b() { # LU-1606, LU-5011
20181         local header
20182         local out=$TMP/$tfile
20183         local prefix=/usr/include/linux/lustre
20184
20185         # We use a hard coded prefix so that this test will not fail
20186         # when run in tree. There are headers in lustre/include/lustre/
20187         # that are not packaged (like lustre_idl.h) and have more
20188         # complicated include dependencies (like config.h and lnet/types.h).
20189         # Since this test about correct packaging we just skip them when
20190         # they don't exist (see below) rather than try to fixup cppflags.
20191
20192         if ! which $CC > /dev/null 2>&1; then
20193                 skip_env "$CC is not installed"
20194         fi
20195
20196         for header in $prefix/*.h; do
20197                 if ! [[ -f "$header" ]]; then
20198                         continue
20199                 fi
20200
20201                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
20202                         continue # lustre_ioctl.h is internal header
20203                 fi
20204
20205                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
20206                         error "cannot compile '$header'"
20207         done
20208         rm -f $out
20209 }
20210 run_test 400b "packaged headers can be compiled"
20211
20212 test_401a() { #LU-7437
20213         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
20214         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
20215
20216         #count the number of parameters by "list_param -R"
20217         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
20218         #count the number of parameters by listing proc files
20219         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
20220         echo "proc_dirs='$proc_dirs'"
20221         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
20222         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
20223                       sort -u | wc -l)
20224
20225         [ $params -eq $procs ] ||
20226                 error "found $params parameters vs. $procs proc files"
20227
20228         # test the list_param -D option only returns directories
20229         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
20230         #count the number of parameters by listing proc directories
20231         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
20232                 sort -u | wc -l)
20233
20234         [ $params -eq $procs ] ||
20235                 error "found $params parameters vs. $procs proc files"
20236 }
20237 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
20238
20239 test_401b() {
20240         local save=$($LCTL get_param -n jobid_var)
20241         local tmp=testing
20242
20243         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
20244                 error "no error returned when setting bad parameters"
20245
20246         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
20247         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
20248
20249         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
20250         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
20251         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
20252 }
20253 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
20254
20255 test_401c() {
20256         local jobid_var_old=$($LCTL get_param -n jobid_var)
20257         local jobid_var_new
20258
20259         $LCTL set_param jobid_var= &&
20260                 error "no error returned for 'set_param a='"
20261
20262         jobid_var_new=$($LCTL get_param -n jobid_var)
20263         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
20264                 error "jobid_var was changed by setting without value"
20265
20266         $LCTL set_param jobid_var &&
20267                 error "no error returned for 'set_param a'"
20268
20269         jobid_var_new=$($LCTL get_param -n jobid_var)
20270         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
20271                 error "jobid_var was changed by setting without value"
20272 }
20273 run_test 401c "Verify 'lctl set_param' without value fails in either format."
20274
20275 test_401d() {
20276         local jobid_var_old=$($LCTL get_param -n jobid_var)
20277         local jobid_var_new
20278         local new_value="foo=bar"
20279
20280         $LCTL set_param jobid_var=$new_value ||
20281                 error "'set_param a=b' did not accept a value containing '='"
20282
20283         jobid_var_new=$($LCTL get_param -n jobid_var)
20284         [[ "$jobid_var_new" == "$new_value" ]] ||
20285                 error "'set_param a=b' failed on a value containing '='"
20286
20287         # Reset the jobid_var to test the other format
20288         $LCTL set_param jobid_var=$jobid_var_old
20289         jobid_var_new=$($LCTL get_param -n jobid_var)
20290         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
20291                 error "failed to reset jobid_var"
20292
20293         $LCTL set_param jobid_var $new_value ||
20294                 error "'set_param a b' did not accept a value containing '='"
20295
20296         jobid_var_new=$($LCTL get_param -n jobid_var)
20297         [[ "$jobid_var_new" == "$new_value" ]] ||
20298                 error "'set_param a b' failed on a value containing '='"
20299
20300         $LCTL set_param jobid_var $jobid_var_old
20301         jobid_var_new=$($LCTL get_param -n jobid_var)
20302         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
20303                 error "failed to reset jobid_var"
20304 }
20305 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
20306
20307 test_402() {
20308         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
20309         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
20310                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
20311         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
20312                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
20313                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
20314         remote_mds_nodsh && skip "remote MDS with nodsh"
20315
20316         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
20317 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
20318         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
20319         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
20320                 echo "Touch failed - OK"
20321 }
20322 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
20323
20324 test_403() {
20325         local file1=$DIR/$tfile.1
20326         local file2=$DIR/$tfile.2
20327         local tfile=$TMP/$tfile
20328
20329         rm -f $file1 $file2 $tfile
20330
20331         touch $file1
20332         ln $file1 $file2
20333
20334         # 30 sec OBD_TIMEOUT in ll_getattr()
20335         # right before populating st_nlink
20336         $LCTL set_param fail_loc=0x80001409
20337         stat -c %h $file1 > $tfile &
20338
20339         # create an alias, drop all locks and reclaim the dentry
20340         < $file2
20341         cancel_lru_locks mdc
20342         cancel_lru_locks osc
20343         sysctl -w vm.drop_caches=2
20344
20345         wait
20346
20347         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
20348
20349         rm -f $tfile $file1 $file2
20350 }
20351 run_test 403 "i_nlink should not drop to zero due to aliasing"
20352
20353 test_404() { # LU-6601
20354         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
20355                 skip "Need server version newer than 2.8.52"
20356         remote_mds_nodsh && skip "remote MDS with nodsh"
20357
20358         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
20359                 awk '/osp .*-osc-MDT/ { print $4}')
20360
20361         local osp
20362         for osp in $mosps; do
20363                 echo "Deactivate: " $osp
20364                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
20365                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
20366                         awk -vp=$osp '$4 == p { print $2 }')
20367                 [ $stat = IN ] || {
20368                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
20369                         error "deactivate error"
20370                 }
20371                 echo "Activate: " $osp
20372                 do_facet $SINGLEMDS $LCTL --device %$osp activate
20373                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
20374                         awk -vp=$osp '$4 == p { print $2 }')
20375                 [ $stat = UP ] || {
20376                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
20377                         error "activate error"
20378                 }
20379         done
20380 }
20381 run_test 404 "validate manual {de}activated works properly for OSPs"
20382
20383 test_405() {
20384         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
20385                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
20386                         skip "Layout swap lock is not supported"
20387
20388         check_swap_layouts_support
20389
20390         test_mkdir $DIR/$tdir
20391         swap_lock_test -d $DIR/$tdir ||
20392                 error "One layout swap locked test failed"
20393 }
20394 run_test 405 "Various layout swap lock tests"
20395
20396 test_406() {
20397         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20398         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
20399         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
20400         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20401         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
20402                 skip "Need MDS version at least 2.8.50"
20403
20404         local def_stripe_size=$($LFS getstripe -S $MOUNT)
20405         local test_pool=$TESTNAME
20406
20407         pool_add $test_pool || error "pool_add failed"
20408         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
20409                 error "pool_add_targets failed"
20410
20411         save_layout_restore_at_exit $MOUNT
20412
20413         # parent set default stripe count only, child will stripe from both
20414         # parent and fs default
20415         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
20416                 error "setstripe $MOUNT failed"
20417         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
20418         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
20419         for i in $(seq 10); do
20420                 local f=$DIR/$tdir/$tfile.$i
20421                 touch $f || error "touch failed"
20422                 local count=$($LFS getstripe -c $f)
20423                 [ $count -eq $OSTCOUNT ] ||
20424                         error "$f stripe count $count != $OSTCOUNT"
20425                 local offset=$($LFS getstripe -i $f)
20426                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
20427                 local size=$($LFS getstripe -S $f)
20428                 [ $size -eq $((def_stripe_size * 2)) ] ||
20429                         error "$f stripe size $size != $((def_stripe_size * 2))"
20430                 local pool=$($LFS getstripe -p $f)
20431                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
20432         done
20433
20434         # change fs default striping, delete parent default striping, now child
20435         # will stripe from new fs default striping only
20436         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
20437                 error "change $MOUNT default stripe failed"
20438         $LFS setstripe -c 0 $DIR/$tdir ||
20439                 error "delete $tdir default stripe failed"
20440         for i in $(seq 11 20); do
20441                 local f=$DIR/$tdir/$tfile.$i
20442                 touch $f || error "touch $f failed"
20443                 local count=$($LFS getstripe -c $f)
20444                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
20445                 local offset=$($LFS getstripe -i $f)
20446                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
20447                 local size=$($LFS getstripe -S $f)
20448                 [ $size -eq $def_stripe_size ] ||
20449                         error "$f stripe size $size != $def_stripe_size"
20450                 local pool=$($LFS getstripe -p $f)
20451                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
20452         done
20453
20454         unlinkmany $DIR/$tdir/$tfile. 1 20
20455
20456         local f=$DIR/$tdir/$tfile
20457         pool_remove_all_targets $test_pool $f
20458         pool_remove $test_pool $f
20459 }
20460 run_test 406 "DNE support fs default striping"
20461
20462 test_407() {
20463         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20464         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
20465                 skip "Need MDS version at least 2.8.55"
20466         remote_mds_nodsh && skip "remote MDS with nodsh"
20467
20468         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
20469                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
20470         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
20471                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
20472         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
20473
20474         #define OBD_FAIL_DT_TXN_STOP    0x2019
20475         for idx in $(seq $MDSCOUNT); do
20476                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
20477         done
20478         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
20479         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
20480                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
20481         true
20482 }
20483 run_test 407 "transaction fail should cause operation fail"
20484
20485 test_408() {
20486         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
20487
20488         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
20489         lctl set_param fail_loc=0x8000040a
20490         # let ll_prepare_partial_page() fail
20491         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
20492
20493         rm -f $DIR/$tfile
20494
20495         # create at least 100 unused inodes so that
20496         # shrink_icache_memory(0) should not return 0
20497         touch $DIR/$tfile-{0..100}
20498         rm -f $DIR/$tfile-{0..100}
20499         sync
20500
20501         echo 2 > /proc/sys/vm/drop_caches
20502 }
20503 run_test 408 "drop_caches should not hang due to page leaks"
20504
20505 test_409()
20506 {
20507         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20508         check_mount_and_prep
20509
20510         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
20511         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
20512         touch $DIR/$tdir/guard || error "(2) Fail to create"
20513
20514         local PREFIX=$(str_repeat 'A' 128)
20515         echo "Create 1K hard links start at $(date)"
20516         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
20517                 error "(3) Fail to hard link"
20518
20519         echo "Links count should be right although linkEA overflow"
20520         stat $DIR/$tdir/guard || error "(4) Fail to stat"
20521         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
20522         [ $linkcount -eq 1001 ] ||
20523                 error "(5) Unexpected hard links count: $linkcount"
20524
20525         echo "List all links start at $(date)"
20526         ls -l $DIR/$tdir/foo > /dev/null ||
20527                 error "(6) Fail to list $DIR/$tdir/foo"
20528
20529         echo "Unlink hard links start at $(date)"
20530         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
20531                 error "(7) Fail to unlink"
20532 }
20533 run_test 409 "Large amount of cross-MDTs hard links on the same file"
20534
20535 test_410()
20536 {
20537         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
20538                 skip "Need client version at least 2.9.59"
20539
20540         # Create a file, and stat it from the kernel
20541         local testfile=$DIR/$tfile
20542         touch $testfile
20543
20544         local run_id=$RANDOM
20545         local my_ino=$(stat --format "%i" $testfile)
20546
20547         # Try to insert the module. This will always fail as the
20548         # module is designed to not be inserted.
20549         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
20550             &> /dev/null
20551
20552         # Anything but success is a test failure
20553         dmesg | grep -q \
20554             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
20555             error "no inode match"
20556 }
20557 run_test 410 "Test inode number returned from kernel thread"
20558
20559 cleanup_test411_cgroup() {
20560         trap 0
20561         rmdir "$1"
20562 }
20563
20564 test_411() {
20565         local cg_basedir=/sys/fs/cgroup/memory
20566         # LU-9966
20567         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
20568                 skip "no setup for cgroup"
20569
20570         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
20571                 error "test file creation failed"
20572         cancel_lru_locks osc
20573
20574         # Create a very small memory cgroup to force a slab allocation error
20575         local cgdir=$cg_basedir/osc_slab_alloc
20576         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
20577         trap "cleanup_test411_cgroup $cgdir" EXIT
20578         echo 2M > $cgdir/memory.kmem.limit_in_bytes
20579         echo 1M > $cgdir/memory.limit_in_bytes
20580
20581         # Should not LBUG, just be killed by oom-killer
20582         # dd will return 0 even allocation failure in some environment.
20583         # So don't check return value
20584         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
20585         cleanup_test411_cgroup $cgdir
20586
20587         return 0
20588 }
20589 run_test 411 "Slab allocation error with cgroup does not LBUG"
20590
20591 test_412() {
20592         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20593         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
20594                 skip "Need server version at least 2.10.55"
20595         fi
20596
20597         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
20598                 error "mkdir failed"
20599         $LFS getdirstripe $DIR/$tdir
20600         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
20601         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
20602                 error "expect $((MDSCOUT - 1)) get $stripe_index"
20603         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
20604         [ $stripe_count -eq 2 ] ||
20605                 error "expect 2 get $stripe_count"
20606 }
20607 run_test 412 "mkdir on specific MDTs"
20608
20609 test_413() {
20610         [ $MDSCOUNT -lt 2 ] &&
20611                 skip "We need at least 2 MDTs for this test"
20612
20613         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
20614                 skip "Need server version at least 2.10.55"
20615         fi
20616
20617         mkdir $DIR/$tdir || error "mkdir failed"
20618
20619         # find MDT that is the most full
20620         local max=$($LFS df | grep MDT |
20621                 awk 'BEGIN { a=0 }
20622                         { sub("%", "", $5)
20623                           if (0+$5 >= a)
20624                           {
20625                                 a = $5
20626                                 b = $6
20627                           }
20628                         }
20629                      END { split(b, c, ":")
20630                            sub("]", "", c[2])
20631                            print c[2]
20632                          }')
20633
20634         for i in $(seq $((MDSCOUNT - 1))); do
20635                 $LFS mkdir -c $i $DIR/$tdir/d$i ||
20636                         error "mkdir d$i failed"
20637                 $LFS getdirstripe $DIR/$tdir/d$i
20638                 local stripe_index=$($LFS getdirstripe -i $DIR/$tdir/d$i)
20639                 [ $stripe_index -ne $max ] ||
20640                         error "don't expect $max"
20641         done
20642 }
20643 run_test 413 "mkdir on less full MDTs"
20644
20645 test_414() {
20646 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
20647         $LCTL set_param fail_loc=0x80000521
20648         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
20649         rm -f $DIR/$tfile
20650 }
20651 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
20652
20653 test_415() {
20654         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20655         [ $(lustre_version_code mds1) -lt $(version_code 2.11.52) ] &&
20656                 skip "Need server version at least 2.11.52"
20657
20658         # LU-11102
20659         local total
20660         local setattr_pid
20661         local start_time
20662         local end_time
20663         local duration
20664
20665         total=500
20666         # this test may be slow on ZFS
20667         [ "$mds1_FSTYPE" == "zfs" ] && total=100
20668
20669         # though this test is designed for striped directory, let's test normal
20670         # directory too since lock is always saved as CoS lock.
20671         test_mkdir $DIR/$tdir || error "mkdir $tdir"
20672         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
20673
20674         (
20675                 while true; do
20676                         touch $DIR/$tdir
20677                 done
20678         ) &
20679         setattr_pid=$!
20680
20681         start_time=$(date +%s)
20682         for i in $(seq $total); do
20683                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
20684                         > /dev/null
20685         done
20686         end_time=$(date +%s)
20687         duration=$((end_time - start_time))
20688
20689         kill -9 $setattr_pid
20690
20691         echo "rename $total files took $duration sec"
20692         [ $duration -lt 100 ] || error "rename took $duration sec"
20693 }
20694 run_test 415 "lock revoke is not missing"
20695
20696 test_416() {
20697         [ $(lustre_version_code mds1) -lt $(version_code 2.11.55) ] &&
20698                 skip "Need server version at least 2.11.55"
20699
20700         # define OBD_FAIL_OSD_TXN_START    0x19a
20701         do_facet mds1 lctl set_param fail_loc=0x19a
20702
20703         lfs mkdir -c $MDSCOUNT $DIR/$tdir
20704
20705         true
20706 }
20707 run_test 416 "transaction start failure won't cause system hung"
20708
20709 cleanup_417() {
20710         trap 0
20711         do_nodes $(comma_list $(mdts_nodes)) \
20712                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
20713         do_nodes $(comma_list $(mdts_nodes)) \
20714                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
20715         do_nodes $(comma_list $(mdts_nodes)) \
20716                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
20717 }
20718
20719 test_417() {
20720         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20721         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
20722                 skip "Need MDS version at least 2.11.56"
20723
20724         trap cleanup_417 RETURN EXIT
20725
20726         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
20727         do_nodes $(comma_list $(mdts_nodes)) \
20728                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
20729         $LFS migrate -m 0 $DIR/$tdir.1 &&
20730                 error "migrate dir $tdir.1 should fail"
20731
20732         do_nodes $(comma_list $(mdts_nodes)) \
20733                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
20734         $LFS mkdir -i 1 $DIR/$tdir.2 &&
20735                 error "create remote dir $tdir.2 should fail"
20736
20737         do_nodes $(comma_list $(mdts_nodes)) \
20738                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
20739         $LFS mkdir -c 2 $DIR/$tdir.3 &&
20740                 error "create striped dir $tdir.3 should fail"
20741         true
20742 }
20743 run_test 417 "disable remote dir, striped dir and dir migration"
20744
20745 # Checks that the outputs of df [-i] and lfs df [-i] match
20746 #
20747 # usage: check_lfs_df <blocks | inodes> <mountpoint>
20748 check_lfs_df() {
20749         local dir=$2
20750         local inodes
20751         local df_out
20752         local lfs_df_out
20753         local count
20754         local passed=false
20755
20756         # blocks or inodes
20757         [ "$1" == "blocks" ] && inodes= || inodes="-i"
20758
20759         for count in {1..100}; do
20760                 cancel_lru_locks
20761                 sync; sleep 2
20762
20763                 # read the lines of interest
20764                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
20765                         error "df $inodes $dir | tail -n +2 failed"
20766                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
20767                         error "lfs df $inodes $dir | grep summary: failed"
20768
20769                 # skip first substrings of each output as they are different
20770                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
20771                 # compare the remaining fields of the two outputs
20772                 passed=true
20773                 for i in {1..5}; do
20774                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
20775                 done
20776                 $passed && break
20777         done
20778
20779         if ! $passed; then
20780                 df -P $inodes $dir
20781                 echo
20782                 lfs df $inodes $dir
20783                 error "df and lfs df $1 output mismatch: "      \
20784                       "df ${inodes}: ${df_out[*]}, "            \
20785                       "lfs df ${inodes}: ${lfs_df_out[*]}"
20786         fi
20787 }
20788
20789 test_418() {
20790         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20791
20792         local dir=$DIR/$tdir
20793         local numfiles=$((RANDOM % 4096 + 2))
20794         local numblocks=$((RANDOM % 256 + 1))
20795
20796         wait_delete_completed
20797         test_mkdir $dir
20798
20799         # check block output
20800         check_lfs_df blocks $dir
20801         # check inode output
20802         check_lfs_df inodes $dir
20803
20804         # create a single file and retest
20805         echo "Creating a single file and testing"
20806         createmany -o $dir/$tfile- 1 &>/dev/null ||
20807                 error "creating 1 file in $dir failed"
20808         check_lfs_df blocks $dir
20809         check_lfs_df inodes $dir
20810
20811         # create a random number of files
20812         echo "Creating $((numfiles - 1)) files and testing"
20813         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
20814                 error "creating $((numfiles - 1)) files in $dir failed"
20815
20816         # write a random number of blocks to the first test file
20817         echo "Writing $numblocks 4K blocks and testing"
20818         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
20819                 count=$numblocks &>/dev/null ||
20820                 error "dd to $dir/${tfile}-0 failed"
20821
20822         # retest
20823         check_lfs_df blocks $dir
20824         check_lfs_df inodes $dir
20825
20826         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
20827                 error "unlinking $numfiles files in $dir failed"
20828 }
20829 run_test 418 "df and lfs df outputs match"
20830
20831 test_419()
20832 {
20833         local dir=$DIR/$tdir
20834
20835         mkdir -p $dir
20836         touch $dir/file
20837
20838         cancel_lru_locks mdc
20839
20840         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
20841         $LCTL set_param fail_loc=0x1410
20842         cat $dir/file
20843         $LCTL set_param fail_loc=0
20844         rm -rf $dir
20845 }
20846 run_test 419 "Verify open file by name doesn't crash kernel"
20847
20848 test_420()
20849 {
20850         [[ $MDS1_VERSION -ge $(version_code 2.12.1) ]] ||
20851                 skip "Need MDS version at least 2.12.1"
20852
20853         local SAVE_UMASK=$(umask)
20854         local dir=$DIR/$tdir
20855         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
20856
20857         mkdir -p $dir
20858         umask 0000
20859         mkdir -m03777 $dir/testdir
20860         ls -dn $dir/testdir
20861         # Need to remove trailing '.' when SELinux is enabled
20862         local dirperms=$(ls -dn $dir/testdir |
20863                          awk '{ sub(/\.$/, "", $1); print $1}')
20864         [ $dirperms == "drwxrwsrwt" ] ||
20865                 error "incorrect perms on $dir/testdir"
20866
20867         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
20868                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
20869         ls -n $dir/testdir/testfile
20870         local fileperms=$(ls -n $dir/testdir/testfile |
20871                           awk '{ sub(/\.$/, "", $1); print $1}')
20872         [ $fileperms == "-rwxr-xr-x" ] ||
20873                 error "incorrect perms on $dir/testdir/testfile"
20874
20875         umask $SAVE_UMASK
20876 }
20877 run_test 420 "clear SGID bit on non-directories for non-members"
20878
20879 test_421a() {
20880         local cnt
20881         local fid1
20882         local fid2
20883
20884         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20885                 skip "Need MDS version at least 2.12.2"
20886
20887         test_mkdir $DIR/$tdir
20888         createmany -o $DIR/$tdir/f 3
20889         cnt=$(ls -1 $DIR/$tdir | wc -l)
20890         [ $cnt != 3 ] && error "unexpected #files: $cnt"
20891
20892         fid1=$(lfs path2fid $DIR/$tdir/f1)
20893         fid2=$(lfs path2fid $DIR/$tdir/f2)
20894         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
20895
20896         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
20897         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
20898
20899         cnt=$(ls -1 $DIR/$tdir | wc -l)
20900         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
20901
20902         rm -f $DIR/$tdir/f3 || error "can't remove f3"
20903         createmany -o $DIR/$tdir/f 3
20904         cnt=$(ls -1 $DIR/$tdir | wc -l)
20905         [ $cnt != 3 ] && error "unexpected #files: $cnt"
20906
20907         fid1=$(lfs path2fid $DIR/$tdir/f1)
20908         fid2=$(lfs path2fid $DIR/$tdir/f2)
20909         echo "remove using fsname $FSNAME"
20910         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
20911
20912         cnt=$(ls -1 $DIR/$tdir | wc -l)
20913         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
20914 }
20915 run_test 421a "simple rm by fid"
20916
20917 test_421b() {
20918         local cnt
20919         local FID1
20920         local FID2
20921
20922         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20923                 skip "Need MDS version at least 2.12.2"
20924
20925         test_mkdir $DIR/$tdir
20926         createmany -o $DIR/$tdir/f 3
20927         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
20928         MULTIPID=$!
20929
20930         FID1=$(lfs path2fid $DIR/$tdir/f1)
20931         FID2=$(lfs path2fid $DIR/$tdir/f2)
20932         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
20933
20934         kill -USR1 $MULTIPID
20935         wait
20936
20937         cnt=$(ls $DIR/$tdir | wc -l)
20938         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
20939 }
20940 run_test 421b "rm by fid on open file"
20941
20942 test_421c() {
20943         local cnt
20944         local FIDS
20945
20946         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20947                 skip "Need MDS version at least 2.12.2"
20948
20949         test_mkdir $DIR/$tdir
20950         createmany -o $DIR/$tdir/f 3
20951         touch $DIR/$tdir/$tfile
20952         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
20953         cnt=$(ls -1 $DIR/$tdir | wc -l)
20954         [ $cnt != 184 ] && error "unexpected #files: $cnt"
20955
20956         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
20957         $LFS rmfid $DIR $FID1 || error "rmfid failed"
20958
20959         cnt=$(ls $DIR/$tdir | wc -l)
20960         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
20961 }
20962 run_test 421c "rm by fid against hardlinked files"
20963
20964 test_421d() {
20965         local cnt
20966         local FIDS
20967
20968         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20969                 skip "Need MDS version at least 2.12.2"
20970
20971         test_mkdir $DIR/$tdir
20972         createmany -o $DIR/$tdir/f 4097
20973         cnt=$(ls -1 $DIR/$tdir | wc -l)
20974         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
20975
20976         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
20977         $LFS rmfid $DIR $FIDS || error "rmfid failed"
20978
20979         cnt=$(ls $DIR/$tdir | wc -l)
20980         rm -rf $DIR/$tdir
20981         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
20982 }
20983 run_test 421d "rmfid en masse"
20984
20985 test_421e() {
20986         local cnt
20987         local FID
20988
20989         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20990         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20991                 skip "Need MDS version at least 2.12.2"
20992
20993         mkdir -p $DIR/$tdir
20994         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
20995         createmany -o $DIR/$tdir/striped_dir/f 512
20996         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
20997         [ $cnt != 512 ] && error "unexpected #files: $cnt"
20998
20999         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
21000                 sed "s/[/][^:]*://g")
21001         $LFS rmfid $DIR $FIDS || error "rmfid failed"
21002
21003         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
21004         rm -rf $DIR/$tdir
21005         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21006 }
21007 run_test 421e "rmfid in DNE"
21008
21009 test_421f() {
21010         local cnt
21011         local FID
21012
21013         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
21014                 skip "Need MDS version at least 2.12.2"
21015
21016         test_mkdir $DIR/$tdir
21017         touch $DIR/$tdir/f
21018         cnt=$(ls -1 $DIR/$tdir | wc -l)
21019         [ $cnt != 1 ] && error "unexpected #files: $cnt"
21020
21021         FID=$(lfs path2fid $DIR/$tdir/f)
21022         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
21023         # rmfid should fail
21024         cnt=$(ls -1 $DIR/$tdir | wc -l)
21025         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
21026
21027         chmod a+rw $DIR/$tdir
21028         ls -la $DIR/$tdir
21029         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
21030         # rmfid should fail
21031         cnt=$(ls -1 $DIR/$tdir | wc -l)
21032         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
21033
21034         rm -f $DIR/$tdir/f
21035         $RUNAS touch $DIR/$tdir/f
21036         FID=$(lfs path2fid $DIR/$tdir/f)
21037         echo "rmfid as root"
21038         $LFS rmfid $DIR $FID || error "rmfid as root failed"
21039         cnt=$(ls -1 $DIR/$tdir | wc -l)
21040         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
21041
21042         rm -f $DIR/$tdir/f
21043         $RUNAS touch $DIR/$tdir/f
21044         cnt=$(ls -1 $DIR/$tdir | wc -l)
21045         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
21046         FID=$(lfs path2fid $DIR/$tdir/f)
21047         # rmfid w/o user_fid2path mount option should fail
21048         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
21049         cnt=$(ls -1 $DIR/$tdir | wc -l)
21050         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
21051
21052         umount_client $MOUNT || "failed to umount client"
21053         mount_client $MOUNT "$MOUNT_OPTS,user_fid2path" ||
21054                 "failed to mount client'"
21055
21056         $RUNAS $LFS rmfid $DIR $FID || error "rmfid failed"
21057         # rmfid should succeed
21058         cnt=$(ls -1 $DIR/$tdir | wc -l)
21059         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
21060
21061         # rmfid shouldn't allow to remove files due to dir's permission
21062         chmod a+rwx $DIR/$tdir
21063         touch $DIR/$tdir/f
21064         ls -la $DIR/$tdir
21065         FID=$(lfs path2fid $DIR/$tdir/f)
21066         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail"
21067
21068         umount_client $MOUNT || "failed to umount client"
21069         mount_client $MOUNT "$MOUNT_OPTS" ||
21070                 "failed to mount client'"
21071
21072 }
21073 run_test 421f "rmfid checks permissions"
21074
21075 test_421g() {
21076         local cnt
21077         local FIDS
21078
21079         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21080         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
21081                 skip "Need MDS version at least 2.12.2"
21082
21083         mkdir -p $DIR/$tdir
21084         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
21085         createmany -o $DIR/$tdir/striped_dir/f 512
21086         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21087         [ $cnt != 512 ] && error "unexpected #files: $cnt"
21088
21089         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
21090                 sed "s/[/][^:]*://g")
21091
21092         rm -f $DIR/$tdir/striped_dir/f1*
21093         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21094         removed=$((512 - cnt))
21095
21096         # few files have been just removed, so we expect
21097         # rmfid to fail on their fids
21098         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
21099         [ $removed != $errors ] && error "$errors != $removed"
21100
21101         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
21102         rm -rf $DIR/$tdir
21103         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21104 }
21105 run_test 421g "rmfid to return errors properly"
21106
21107 stat_test() {
21108     df -h $MOUNT &
21109     df -h $MOUNT &
21110     df -h $MOUNT &
21111     df -h $MOUNT &
21112     df -h $MOUNT &
21113     df -h $MOUNT &
21114 }
21115
21116 test_423() {
21117     local _stats
21118     # ensure statfs cache is expired
21119     sleep 2;
21120
21121     _stats=$(stat_test | grep $MOUNT | sort -u | wc -l)
21122     [[ ${_stats} -ne 1 ]] && error "statfs wrong"
21123
21124     return 0
21125 }
21126 run_test 423 "statfs should return a right data"
21127
21128 prep_801() {
21129         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
21130         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
21131                 skip "Need server version at least 2.9.55"
21132
21133         start_full_debug_logging
21134 }
21135
21136 post_801() {
21137         stop_full_debug_logging
21138 }
21139
21140 barrier_stat() {
21141         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
21142                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
21143                            awk '/The barrier for/ { print $7 }')
21144                 echo $st
21145         else
21146                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
21147                 echo \'$st\'
21148         fi
21149 }
21150
21151 barrier_expired() {
21152         local expired
21153
21154         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
21155                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
21156                           awk '/will be expired/ { print $7 }')
21157         else
21158                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
21159         fi
21160
21161         echo $expired
21162 }
21163
21164 test_801a() {
21165         prep_801
21166
21167         echo "Start barrier_freeze at: $(date)"
21168         #define OBD_FAIL_BARRIER_DELAY          0x2202
21169         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
21170         # Do not reduce barrier time - See LU-11873
21171         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
21172
21173         sleep 2
21174         local b_status=$(barrier_stat)
21175         echo "Got barrier status at: $(date)"
21176         [ "$b_status" = "'freezing_p1'" ] ||
21177                 error "(1) unexpected barrier status $b_status"
21178
21179         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
21180         wait
21181         b_status=$(barrier_stat)
21182         [ "$b_status" = "'frozen'" ] ||
21183                 error "(2) unexpected barrier status $b_status"
21184
21185         local expired=$(barrier_expired)
21186         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
21187         sleep $((expired + 3))
21188
21189         b_status=$(barrier_stat)
21190         [ "$b_status" = "'expired'" ] ||
21191                 error "(3) unexpected barrier status $b_status"
21192
21193         # Do not reduce barrier time - See LU-11873
21194         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
21195                 error "(4) fail to freeze barrier"
21196
21197         b_status=$(barrier_stat)
21198         [ "$b_status" = "'frozen'" ] ||
21199                 error "(5) unexpected barrier status $b_status"
21200
21201         echo "Start barrier_thaw at: $(date)"
21202         #define OBD_FAIL_BARRIER_DELAY          0x2202
21203         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
21204         do_facet mgs $LCTL barrier_thaw $FSNAME &
21205
21206         sleep 2
21207         b_status=$(barrier_stat)
21208         echo "Got barrier status at: $(date)"
21209         [ "$b_status" = "'thawing'" ] ||
21210                 error "(6) unexpected barrier status $b_status"
21211
21212         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
21213         wait
21214         b_status=$(barrier_stat)
21215         [ "$b_status" = "'thawed'" ] ||
21216                 error "(7) unexpected barrier status $b_status"
21217
21218         #define OBD_FAIL_BARRIER_FAILURE        0x2203
21219         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
21220         do_facet mgs $LCTL barrier_freeze $FSNAME
21221
21222         b_status=$(barrier_stat)
21223         [ "$b_status" = "'failed'" ] ||
21224                 error "(8) unexpected barrier status $b_status"
21225
21226         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
21227         do_facet mgs $LCTL barrier_thaw $FSNAME
21228
21229         post_801
21230 }
21231 run_test 801a "write barrier user interfaces and stat machine"
21232
21233 test_801b() {
21234         prep_801
21235
21236         mkdir $DIR/$tdir || error "(1) fail to mkdir"
21237         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
21238         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
21239         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
21240         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
21241
21242         cancel_lru_locks mdc
21243
21244         # 180 seconds should be long enough
21245         do_facet mgs $LCTL barrier_freeze $FSNAME 180
21246
21247         local b_status=$(barrier_stat)
21248         [ "$b_status" = "'frozen'" ] ||
21249                 error "(6) unexpected barrier status $b_status"
21250
21251         mkdir $DIR/$tdir/d0/d10 &
21252         mkdir_pid=$!
21253
21254         touch $DIR/$tdir/d1/f13 &
21255         touch_pid=$!
21256
21257         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
21258         ln_pid=$!
21259
21260         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
21261         mv_pid=$!
21262
21263         rm -f $DIR/$tdir/d4/f12 &
21264         rm_pid=$!
21265
21266         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
21267
21268         # To guarantee taht the 'stat' is not blocked
21269         b_status=$(barrier_stat)
21270         [ "$b_status" = "'frozen'" ] ||
21271                 error "(8) unexpected barrier status $b_status"
21272
21273         # let above commands to run at background
21274         sleep 5
21275
21276         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
21277         ps -p $touch_pid || error "(10) touch should be blocked"
21278         ps -p $ln_pid || error "(11) link should be blocked"
21279         ps -p $mv_pid || error "(12) rename should be blocked"
21280         ps -p $rm_pid || error "(13) unlink should be blocked"
21281
21282         b_status=$(barrier_stat)
21283         [ "$b_status" = "'frozen'" ] ||
21284                 error "(14) unexpected barrier status $b_status"
21285
21286         do_facet mgs $LCTL barrier_thaw $FSNAME
21287         b_status=$(barrier_stat)
21288         [ "$b_status" = "'thawed'" ] ||
21289                 error "(15) unexpected barrier status $b_status"
21290
21291         wait $mkdir_pid || error "(16) mkdir should succeed"
21292         wait $touch_pid || error "(17) touch should succeed"
21293         wait $ln_pid || error "(18) link should succeed"
21294         wait $mv_pid || error "(19) rename should succeed"
21295         wait $rm_pid || error "(20) unlink should succeed"
21296
21297         post_801
21298 }
21299 run_test 801b "modification will be blocked by write barrier"
21300
21301 test_801c() {
21302         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21303
21304         prep_801
21305
21306         stop mds2 || error "(1) Fail to stop mds2"
21307
21308         do_facet mgs $LCTL barrier_freeze $FSNAME 30
21309
21310         local b_status=$(barrier_stat)
21311         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
21312                 do_facet mgs $LCTL barrier_thaw $FSNAME
21313                 error "(2) unexpected barrier status $b_status"
21314         }
21315
21316         do_facet mgs $LCTL barrier_rescan $FSNAME ||
21317                 error "(3) Fail to rescan barrier bitmap"
21318
21319         # Do not reduce barrier time - See LU-11873
21320         do_facet mgs $LCTL barrier_freeze $FSNAME 20
21321
21322         b_status=$(barrier_stat)
21323         [ "$b_status" = "'frozen'" ] ||
21324                 error "(4) unexpected barrier status $b_status"
21325
21326         do_facet mgs $LCTL barrier_thaw $FSNAME
21327         b_status=$(barrier_stat)
21328         [ "$b_status" = "'thawed'" ] ||
21329                 error "(5) unexpected barrier status $b_status"
21330
21331         local devname=$(mdsdevname 2)
21332
21333         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
21334
21335         do_facet mgs $LCTL barrier_rescan $FSNAME ||
21336                 error "(7) Fail to rescan barrier bitmap"
21337
21338         post_801
21339 }
21340 run_test 801c "rescan barrier bitmap"
21341
21342 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
21343 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
21344 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
21345 saved_MOUNT_OPTS=$MOUNT_OPTS
21346
21347 cleanup_802() {
21348         trap 0
21349
21350         stopall
21351         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
21352         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
21353         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
21354         MOUNT_OPTS=$saved_MOUNT_OPTS
21355         setupall
21356 }
21357
21358 test_802() {
21359         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
21360         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
21361         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
21362                 skip "Need server version at least 2.9.55"
21363
21364         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
21365
21366         mkdir $DIR/$tdir || error "(1) fail to mkdir"
21367
21368         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
21369                 error "(2) Fail to copy"
21370
21371         trap cleanup_802 EXIT
21372
21373         # sync by force before remount as readonly
21374         sync; sync_all_data; sleep 3; sync_all_data
21375
21376         stopall
21377
21378         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
21379         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
21380         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
21381
21382         echo "Mount the server as read only"
21383         setupall server_only || error "(3) Fail to start servers"
21384
21385         echo "Mount client without ro should fail"
21386         mount_client $MOUNT &&
21387                 error "(4) Mount client without 'ro' should fail"
21388
21389         echo "Mount client with ro should succeed"
21390         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
21391         mount_client $MOUNT ||
21392                 error "(5) Mount client with 'ro' should succeed"
21393
21394         echo "Modify should be refused"
21395         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
21396
21397         echo "Read should be allowed"
21398         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
21399                 error "(7) Read should succeed under ro mode"
21400
21401         cleanup_802
21402 }
21403 run_test 802 "simulate readonly device"
21404
21405 test_803a() {
21406         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21407         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
21408                 skip "MDS needs to be newer than 2.10.54"
21409
21410         mkdir -p $DIR/$tdir
21411         # Create some objects on all MDTs to trigger related logs objects
21412         for idx in $(seq $MDSCOUNT); do
21413                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
21414                         $DIR/$tdir/dir${idx} ||
21415                         error "Fail to create $DIR/$tdir/dir${idx}"
21416         done
21417
21418         sync; sleep 3
21419         wait_delete_completed # ensure old test cleanups are finished
21420         echo "before create:"
21421         $LFS df -i $MOUNT
21422         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
21423
21424         for i in {1..10}; do
21425                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
21426                         error "Fail to create $DIR/$tdir/foo$i"
21427         done
21428
21429         sync; sleep 3
21430         echo "after create:"
21431         $LFS df -i $MOUNT
21432         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
21433
21434         # allow for an llog to be cleaned up during the test
21435         [ $after_used -ge $((before_used + 10 - 1)) ] ||
21436                 error "before ($before_used) + 10 > after ($after_used)"
21437
21438         for i in {1..10}; do
21439                 rm -rf $DIR/$tdir/foo$i ||
21440                         error "Fail to remove $DIR/$tdir/foo$i"
21441         done
21442
21443         sleep 3 # avoid MDT return cached statfs
21444         wait_delete_completed
21445         echo "after unlink:"
21446         $LFS df -i $MOUNT
21447         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
21448
21449         # allow for an llog to be created during the test
21450         [ $after_used -le $((before_used + 1)) ] ||
21451                 error "after ($after_used) > before ($before_used) + 1"
21452 }
21453 run_test 803a "verify agent object for remote object"
21454
21455 test_803b() {
21456         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21457         [ $MDS1_VERSION -lt $(version_code 2.13.56) ] &&
21458                 skip "MDS needs to be newer than 2.13.56"
21459         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21460
21461         for i in $(seq 0 $((MDSCOUNT - 1))); do
21462                 $LFS mkdir -i $i $DIR/$tdir.$i || error "mkdir $tdir.$i"
21463         done
21464
21465         local before=0
21466         local after=0
21467
21468         local tmp
21469
21470         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
21471         for i in $(seq 0 $((MDSCOUNT - 1))); do
21472                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
21473                         awk '/getattr/ { print $2 }')
21474                 before=$((before + tmp))
21475         done
21476         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
21477         for i in $(seq 0 $((MDSCOUNT - 1))); do
21478                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
21479                         awk '/getattr/ { print $2 }')
21480                 after=$((after + tmp))
21481         done
21482
21483         [ $before -eq $after ] || error "getattr count $before != $after"
21484 }
21485 run_test 803b "remote object can getattr from cache"
21486
21487 test_804() {
21488         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21489         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
21490                 skip "MDS needs to be newer than 2.10.54"
21491         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
21492
21493         mkdir -p $DIR/$tdir
21494         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
21495                 error "Fail to create $DIR/$tdir/dir0"
21496
21497         local fid=$($LFS path2fid $DIR/$tdir/dir0)
21498         local dev=$(mdsdevname 2)
21499
21500         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21501                 grep ${fid} || error "NOT found agent entry for dir0"
21502
21503         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
21504                 error "Fail to create $DIR/$tdir/dir1"
21505
21506         touch $DIR/$tdir/dir1/foo0 ||
21507                 error "Fail to create $DIR/$tdir/dir1/foo0"
21508         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
21509         local rc=0
21510
21511         for idx in $(seq $MDSCOUNT); do
21512                 dev=$(mdsdevname $idx)
21513                 do_facet mds${idx} \
21514                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21515                         grep ${fid} && rc=$idx
21516         done
21517
21518         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
21519                 error "Fail to rename foo0 to foo1"
21520         if [ $rc -eq 0 ]; then
21521                 for idx in $(seq $MDSCOUNT); do
21522                         dev=$(mdsdevname $idx)
21523                         do_facet mds${idx} \
21524                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21525                         grep ${fid} && rc=$idx
21526                 done
21527         fi
21528
21529         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
21530                 error "Fail to rename foo1 to foo2"
21531         if [ $rc -eq 0 ]; then
21532                 for idx in $(seq $MDSCOUNT); do
21533                         dev=$(mdsdevname $idx)
21534                         do_facet mds${idx} \
21535                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21536                         grep ${fid} && rc=$idx
21537                 done
21538         fi
21539
21540         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
21541
21542         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
21543                 error "Fail to link to $DIR/$tdir/dir1/foo2"
21544         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
21545                 error "Fail to rename foo2 to foo0"
21546         unlink $DIR/$tdir/dir1/foo0 ||
21547                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
21548         rm -rf $DIR/$tdir/dir0 ||
21549                 error "Fail to rm $DIR/$tdir/dir0"
21550
21551         for idx in $(seq $MDSCOUNT); do
21552                 dev=$(mdsdevname $idx)
21553                 rc=0
21554
21555                 stop mds${idx}
21556                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
21557                         rc=$?
21558                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
21559                         error "mount mds$idx failed"
21560                 df $MOUNT > /dev/null 2>&1
21561
21562                 # e2fsck should not return error
21563                 [ $rc -eq 0 ] ||
21564                         error "e2fsck detected error on MDT${idx}: rc=$rc"
21565         done
21566 }
21567 run_test 804 "verify agent entry for remote entry"
21568
21569 cleanup_805() {
21570         do_facet $SINGLEMDS zfs set quota=$old $fsset
21571         unlinkmany $DIR/$tdir/f- 1000000
21572         trap 0
21573 }
21574
21575 test_805() {
21576         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
21577         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
21578         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
21579                 skip "netfree not implemented before 0.7"
21580         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
21581                 skip "Need MDS version at least 2.10.57"
21582
21583         local fsset
21584         local freekb
21585         local usedkb
21586         local old
21587         local quota
21588         local pref="osd-zfs.lustre-MDT0000."
21589
21590         # limit available space on MDS dataset to meet nospace issue
21591         # quickly. then ZFS 0.7.2 can use reserved space if asked
21592         # properly (using netfree flag in osd_declare_destroy()
21593         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
21594         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
21595                 gawk '{print $3}')
21596         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
21597         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
21598         let "usedkb=usedkb-freekb"
21599         let "freekb=freekb/2"
21600         if let "freekb > 5000"; then
21601                 let "freekb=5000"
21602         fi
21603         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
21604         trap cleanup_805 EXIT
21605         mkdir $DIR/$tdir
21606         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
21607         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
21608         rm -rf $DIR/$tdir || error "not able to remove"
21609         do_facet $SINGLEMDS zfs set quota=$old $fsset
21610         trap 0
21611 }
21612 run_test 805 "ZFS can remove from full fs"
21613
21614 # Size-on-MDS test
21615 check_lsom_data()
21616 {
21617         local file=$1
21618         local size=$($LFS getsom -s $file)
21619         local expect=$(stat -c %s $file)
21620
21621         [[ $size == $expect ]] ||
21622                 error "$file expected size: $expect, got: $size"
21623
21624         local blocks=$($LFS getsom -b $file)
21625         expect=$(stat -c %b $file)
21626         [[ $blocks == $expect ]] ||
21627                 error "$file expected blocks: $expect, got: $blocks"
21628 }
21629
21630 check_lsom_size()
21631 {
21632         local size=$($LFS getsom -s $1)
21633         local expect=$2
21634
21635         [[ $size == $expect ]] ||
21636                 error "$file expected size: $expect, got: $size"
21637 }
21638
21639 test_806() {
21640         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21641                 skip "Need MDS version at least 2.11.52"
21642
21643         local bs=1048576
21644
21645         touch $DIR/$tfile || error "touch $tfile failed"
21646
21647         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
21648         save_lustre_params client "llite.*.xattr_cache" > $save
21649         lctl set_param llite.*.xattr_cache=0
21650         stack_trap "restore_lustre_params < $save" EXIT
21651
21652         # single-threaded write
21653         echo "Test SOM for single-threaded write"
21654         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
21655                 error "write $tfile failed"
21656         check_lsom_size $DIR/$tfile $bs
21657
21658         local num=32
21659         local size=$(($num * $bs))
21660         local offset=0
21661         local i
21662
21663         echo "Test SOM for single client multi-threaded($num) write"
21664         $TRUNCATE $DIR/$tfile 0
21665         for ((i = 0; i < $num; i++)); do
21666                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21667                 local pids[$i]=$!
21668                 offset=$((offset + $bs))
21669         done
21670         for (( i=0; i < $num; i++ )); do
21671                 wait ${pids[$i]}
21672         done
21673         check_lsom_size $DIR/$tfile $size
21674
21675         $TRUNCATE $DIR/$tfile 0
21676         for ((i = 0; i < $num; i++)); do
21677                 offset=$((offset - $bs))
21678                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21679                 local pids[$i]=$!
21680         done
21681         for (( i=0; i < $num; i++ )); do
21682                 wait ${pids[$i]}
21683         done
21684         check_lsom_size $DIR/$tfile $size
21685
21686         # multi-client writes
21687         num=$(get_node_count ${CLIENTS//,/ })
21688         size=$(($num * $bs))
21689         offset=0
21690         i=0
21691
21692         echo "Test SOM for multi-client ($num) writes"
21693         $TRUNCATE $DIR/$tfile 0
21694         for client in ${CLIENTS//,/ }; do
21695                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21696                 local pids[$i]=$!
21697                 i=$((i + 1))
21698                 offset=$((offset + $bs))
21699         done
21700         for (( i=0; i < $num; i++ )); do
21701                 wait ${pids[$i]}
21702         done
21703         check_lsom_size $DIR/$tfile $offset
21704
21705         i=0
21706         $TRUNCATE $DIR/$tfile 0
21707         for client in ${CLIENTS//,/ }; do
21708                 offset=$((offset - $bs))
21709                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21710                 local pids[$i]=$!
21711                 i=$((i + 1))
21712         done
21713         for (( i=0; i < $num; i++ )); do
21714                 wait ${pids[$i]}
21715         done
21716         check_lsom_size $DIR/$tfile $size
21717
21718         # verify truncate
21719         echo "Test SOM for truncate"
21720         $TRUNCATE $DIR/$tfile 1048576
21721         check_lsom_size $DIR/$tfile 1048576
21722         $TRUNCATE $DIR/$tfile 1234
21723         check_lsom_size $DIR/$tfile 1234
21724
21725         # verify SOM blocks count
21726         echo "Verify SOM block count"
21727         $TRUNCATE $DIR/$tfile 0
21728         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
21729                 error "failed to write file $tfile"
21730         check_lsom_data $DIR/$tfile
21731 }
21732 run_test 806 "Verify Lazy Size on MDS"
21733
21734 test_807() {
21735         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21736                 skip "Need MDS version at least 2.11.52"
21737
21738         # Registration step
21739         changelog_register || error "changelog_register failed"
21740         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
21741         changelog_users $SINGLEMDS | grep -q $cl_user ||
21742                 error "User $cl_user not found in changelog_users"
21743
21744         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
21745         save_lustre_params client "llite.*.xattr_cache" > $save
21746         lctl set_param llite.*.xattr_cache=0
21747         stack_trap "restore_lustre_params < $save" EXIT
21748
21749         rm -rf $DIR/$tdir || error "rm $tdir failed"
21750         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
21751         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
21752         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
21753         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
21754                 error "truncate $tdir/trunc failed"
21755
21756         local bs=1048576
21757         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 ||
21758                 error "write $tfile failed"
21759
21760         # multi-client wirtes
21761         local num=$(get_node_count ${CLIENTS//,/ })
21762         local offset=0
21763         local i=0
21764
21765         echo "Test SOM for multi-client ($num) writes"
21766         touch $DIR/$tfile || error "touch $tfile failed"
21767         $TRUNCATE $DIR/$tfile 0
21768         for client in ${CLIENTS//,/ }; do
21769                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21770                 local pids[$i]=$!
21771                 i=$((i + 1))
21772                 offset=$((offset + $bs))
21773         done
21774         for (( i=0; i < $num; i++ )); do
21775                 wait ${pids[$i]}
21776         done
21777
21778         sleep 5
21779         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
21780         check_lsom_data $DIR/$tdir/trunc
21781         check_lsom_data $DIR/$tdir/single_dd
21782         check_lsom_data $DIR/$tfile
21783
21784         rm -rf $DIR/$tdir
21785         # Deregistration step
21786         changelog_deregister || error "changelog_deregister failed"
21787 }
21788 run_test 807 "verify LSOM syncing tool"
21789
21790 check_som_nologged()
21791 {
21792         local lines=$($LFS changelog $FSNAME-MDT0000 |
21793                 grep 'x=trusted.som' | wc -l)
21794         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
21795 }
21796
21797 test_808() {
21798         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
21799                 skip "Need MDS version at least 2.11.55"
21800
21801         # Registration step
21802         changelog_register || error "changelog_register failed"
21803
21804         touch $DIR/$tfile || error "touch $tfile failed"
21805         check_som_nologged
21806
21807         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
21808                 error "write $tfile failed"
21809         check_som_nologged
21810
21811         $TRUNCATE $DIR/$tfile 1234
21812         check_som_nologged
21813
21814         $TRUNCATE $DIR/$tfile 1048576
21815         check_som_nologged
21816
21817         # Deregistration step
21818         changelog_deregister || error "changelog_deregister failed"
21819 }
21820 run_test 808 "Check trusted.som xattr not logged in Changelogs"
21821
21822 check_som_nodata()
21823 {
21824         $LFS getsom $1
21825         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
21826 }
21827
21828 test_809() {
21829         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
21830                 skip "Need MDS version at least 2.11.56"
21831
21832         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
21833                 error "failed to create DoM-only file $DIR/$tfile"
21834         touch $DIR/$tfile || error "touch $tfile failed"
21835         check_som_nodata $DIR/$tfile
21836
21837         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
21838                 error "write $tfile failed"
21839         check_som_nodata $DIR/$tfile
21840
21841         $TRUNCATE $DIR/$tfile 1234
21842         check_som_nodata $DIR/$tfile
21843
21844         $TRUNCATE $DIR/$tfile 4097
21845         check_som_nodata $DIR/$file
21846 }
21847 run_test 809 "Verify no SOM xattr store for DoM-only files"
21848
21849 test_810() {
21850         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21851         $GSS && skip_env "could not run with gss"
21852
21853         set_checksums 1
21854         stack_trap "set_checksums $ORIG_CSUM" EXIT
21855         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
21856
21857         local csum
21858         local before
21859         local after
21860         for csum in $CKSUM_TYPES; do
21861                 #define OBD_FAIL_OSC_NO_GRANT   0x411
21862                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
21863                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
21864                         eval set -- $i
21865                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
21866                         before=$(md5sum $DIR/$tfile)
21867                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
21868                         after=$(md5sum $DIR/$tfile)
21869                         [ "$before" == "$after" ] ||
21870                                 error "$csum: $before != $after bs=$1 seek=$2"
21871                 done
21872         done
21873 }
21874 run_test 810 "partial page writes on ZFS (LU-11663)"
21875
21876 test_812() {
21877         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
21878                 skip "OST < 2.12.51 doesn't support this fail_loc"
21879         [ "$SHARED_KEY" = true ] &&
21880                 skip "OSC connections never go IDLE with Shared-Keys enabled"
21881
21882         $LFS setstripe -c 1 -i 0 $DIR/$tfile
21883         # ensure ost1 is connected
21884         stat $DIR/$tfile >/dev/null || error "can't stat"
21885         wait_osc_import_state client ost1 FULL
21886         # no locks, no reqs to let the connection idle
21887         cancel_lru_locks osc
21888
21889         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
21890 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
21891         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
21892         wait_osc_import_state client ost1 CONNECTING
21893         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
21894
21895         stat $DIR/$tfile >/dev/null || error "can't stat file"
21896 }
21897 run_test 812 "do not drop reqs generated when imp is going to idle (LU-11951)"
21898
21899 test_814()
21900 {
21901         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
21902         echo -n y >> $DIR/$tfile
21903         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
21904         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
21905 }
21906 run_test 814 "sparse cp works as expected (LU-12361)"
21907
21908 test_815()
21909 {
21910         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
21911         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
21912 }
21913 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
21914
21915 test_816() {
21916         local ost1_imp=$(get_osc_import_name client ost1)
21917         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
21918                          cut -d'.' -f2)
21919
21920         [ "$SHARED_KEY" = true ] &&
21921                 skip "OSC connections never go IDLE with Shared-Keys enabled"
21922
21923         $LFS setstripe -c 1 -i 0 $DIR/$tfile
21924         # ensure ost1 is connected
21925         stat $DIR/$tfile >/dev/null || error "can't stat"
21926         wait_osc_import_state client ost1 FULL
21927         # no locks, no reqs to let the connection idle
21928         cancel_lru_locks osc
21929         lru_resize_disable osc
21930         local before
21931         local now
21932         before=$($LCTL get_param -n \
21933                  ldlm.namespaces.$imp_name.lru_size)
21934
21935         wait_osc_import_state client ost1 IDLE
21936         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
21937         now=$($LCTL get_param -n \
21938               ldlm.namespaces.$imp_name.lru_size)
21939         [ $before == $now ] || error "lru_size changed $before != $now"
21940 }
21941 run_test 816 "do not reset lru_resize on idle reconnect"
21942
21943 cleanup_817() {
21944         umount $tmpdir
21945         exportfs -u localhost:$DIR/nfsexp
21946         rm -rf $DIR/nfsexp
21947 }
21948
21949 test_817() {
21950         systemctl restart nfs-server.service || skip "failed to restart nfsd"
21951
21952         mkdir -p $DIR/nfsexp
21953         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
21954                 error "failed to export nfs"
21955
21956         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
21957         stack_trap cleanup_817 EXIT
21958
21959         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
21960                 error "failed to mount nfs to $tmpdir"
21961
21962         cp /bin/true $tmpdir
21963         $DIR/nfsexp/true || error "failed to execute 'true' command"
21964 }
21965 run_test 817 "nfsd won't cache write lock for exec file"
21966
21967 test_819a() {
21968         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
21969         cancel_lru_locks osc
21970         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
21971         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
21972         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
21973         rm -f $TDIR/$tfile
21974 }
21975 run_test 819a "too big niobuf in read"
21976
21977 test_819b() {
21978         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
21979         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
21980         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
21981         cancel_lru_locks osc
21982         sleep 1
21983         rm -f $TDIR/$tfile
21984 }
21985 run_test 819b "too big niobuf in write"
21986
21987 test_818() {
21988         mkdir $DIR/$tdir
21989         $LFS setstripe -c1 -i0 $DIR/$tfile
21990         $LFS setstripe -c1 -i1 $DIR/$tfile
21991         stop $SINGLEMDS
21992         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
21993         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
21994         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
21995                 error "start $SINGLEMDS failed"
21996         rm -rf $DIR/$tdir
21997 }
21998 run_test 818 "unlink with failed llog"
21999
22000 #
22001 # tests that do cleanup/setup should be run at the end
22002 #
22003
22004 test_900() {
22005         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22006         local ls
22007
22008         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
22009         $LCTL set_param fail_loc=0x903
22010
22011         cancel_lru_locks MGC
22012
22013         FAIL_ON_ERROR=true cleanup
22014         FAIL_ON_ERROR=true setup
22015 }
22016 run_test 900 "umount should not race with any mgc requeue thread"
22017
22018 # LU-13377
22019 test_902() {
22020         [ $CLIENT_VERSION -lt $(version_code 2.13.52) ] &&
22021                 skip "client does not have LU-13377 fix"
22022         #define OBD_FAIL_LLITE_SHORT_COMMIT 0x1415
22023         $LCTL set_param fail_loc=0x1415
22024         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22025         cancel_lru_locks osc
22026         rm -f $DIR/$tfile
22027 }
22028 run_test 902 "test short write doesn't hang lustre"
22029
22030 complete $SECONDS
22031 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
22032 check_and_cleanup_lustre
22033 if [ "$I_MOUNTED" != "yes" ]; then
22034         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
22035 fi
22036 exit_status