Whamcloud - gitweb
a2895163aee90dd8f59c923547d8c0abf5a9d9a9
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 8; indent-tabs-mode: t; -*-
3 #
4 # Run select tests by setting ONLY, or as arguments to the script.
5 # Skip specific tests by setting EXCEPT.
6 #
7 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
8 set -e
9
10 ONLY=${ONLY:-"$*"}
11 # bug number for skipped test: LU-9693 LU-6493 LU-9693 LU-11058
12 ALWAYS_EXCEPT="$SANITY_EXCEPT  42a     42b     42c     77k"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # skipped tests: LU-8411 LU-9096 LU-9054 ..
16 ALWAYS_EXCEPT="  407     253     312     $ALWAYS_EXCEPT"
17
18 if $SHARED_KEY; then
19 # bug number for skipped tests: LU-9795 (all below)
20         ALWAYS_EXCEPT="$ALWAYS_EXCEPT   17n     60a     133g    300f"
21 fi
22
23 if [[ $(uname -m) = aarch64 ]]; then
24         # bug number:    LU-11596 (all below)
25         ALWAYS_EXCEPT+=" 42d 42e 63a 63b 64a 64b 64c"
26         # bug number:    LU-11671 LU-11665 LU-11594 LU-11667 LU-11729
27         ALWAYS_EXCEPT+=" 45       101c     103a     317      810"
28 fi
29
30 # Check Grants after these tests
31 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c"
32 SRCDIR=$(cd $(dirname $0); echo $PWD)
33 export PATH=$PATH:/sbin
34
35 TMP=${TMP:-/tmp}
36 OSC=${OSC:-"osc"}
37
38 CC=${CC:-cc}
39 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
40 CREATETEST=${CREATETEST:-createtest}
41 LFS=${LFS:-lfs}
42 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
43 LCTL=${LCTL:-lctl}
44 OPENFILE=${OPENFILE:-openfile}
45 OPENUNLINK=${OPENUNLINK:-openunlink}
46 export MULTIOP=${MULTIOP:-multiop}
47 READS=${READS:-"reads"}
48 MUNLINK=${MUNLINK:-munlink}
49 SOCKETSERVER=${SOCKETSERVER:-socketserver}
50 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
51 MEMHOG=${MEMHOG:-memhog}
52 DIRECTIO=${DIRECTIO:-directio}
53 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
54 DEF_STRIPE_COUNT=-1
55 CHECK_GRANT=${CHECK_GRANT:-"yes"}
56 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
57 export PARALLEL=${PARALLEL:-"no"}
58
59 export NAME=${NAME:-local}
60
61 SAVE_PWD=$PWD
62
63 CLEANUP=${CLEANUP:-:}
64 SETUP=${SETUP:-:}
65 TRACE=${TRACE:-""}
66 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
67 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
68 . $LUSTRE/tests/test-framework.sh
69 init_test_env $@
70 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
71 get_lustre_env
72 init_logging
73
74 #                                  5          12          (min)"
75 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 300o"
76
77 if [ "$mds1_FSTYPE" = "zfs" ]; then
78         # bug number for skipped test: LU-1957
79         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  180"
80         #                                               13    (min)"
81         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
82 fi
83
84 # Get the SLES distro version
85 #
86 # Returns a version string that should only be used in comparing
87 # strings returned by version_code()
88 sles_version_code()
89 {
90         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
91
92         # All SuSE Linux versions have one decimal. version_code expects two
93         local sles_version=$version.0
94         version_code $sles_version
95 }
96
97 # Check if we are running on Ubuntu or SLES so we can make decisions on
98 # what tests to run
99 if [ -r /etc/SuSE-release ]; then
100         sles_version=$(sles_version_code)
101         [ $sles_version -lt $(version_code 11.4.0) ] &&
102                 # bug number for skipped test: LU-4341
103                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
104         [ $sles_version -lt $(version_code 12.0.0) ] &&
105                 # bug number for skipped test: LU-3703
106                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
107 elif [ -r /etc/os-release ]; then
108         if grep -qi ubuntu /etc/os-release; then
109                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
110                                                 -e 's/^VERSION=//p' \
111                                                 /etc/os-release |
112                                                 awk '{ print $1 }'))
113
114                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
115                         # bug number for skipped test:
116                         #                LU-10334 LU-10366
117                         ALWAYS_EXCEPT+=" 103a     410"
118                 fi
119         fi
120 fi
121
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 build_test_filter
159
160 if [ "${ONLY}" = "MOUNT" ] ; then
161         echo "Lustre is up, please go on"
162         exit
163 fi
164
165 echo "preparing for tests involving mounts"
166 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
167 touch $EXT2_DEV
168 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
169 echo # add a newline after mke2fs.
170
171 umask 077
172
173 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
174 lctl set_param debug=-1 2> /dev/null || true
175 test_0a() {
176         touch $DIR/$tfile
177         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
178         rm $DIR/$tfile
179         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
180 }
181 run_test 0a "touch; rm ====================="
182
183 test_0b() {
184         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
185         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
186 }
187 run_test 0b "chmod 0755 $DIR ============================="
188
189 test_0c() {
190         $LCTL get_param mdc.*.import | grep "state: FULL" ||
191                 error "import not FULL"
192         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
193                 error "bad target"
194 }
195 run_test 0c "check import proc"
196
197 test_0d() { # LU-3397
198         [ $MGS_VERSION -lt $(version_code 2.10.57) ] &&
199                 skip "proc exports not supported before 2.10.57"
200
201         local mgs_exp="mgs.MGS.exports"
202         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
203         local exp_client_nid
204         local exp_client_version
205         local exp_val
206         local imp_val
207         local temp_imp=$DIR/$tfile.import
208         local temp_exp=$DIR/$tfile.export
209
210         # save mgc import file to $temp_imp
211         $LCTL get_param mgc.*.import | tee $temp_imp
212         # Check if client uuid is found in MGS export
213         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
214                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
215                         $client_uuid ] &&
216                         break;
217         done
218         # save mgs export file to $temp_exp
219         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
220
221         # Compare the value of field "connect_flags"
222         imp_val=$(grep "connect_flags" $temp_imp)
223         exp_val=$(grep "connect_flags" $temp_exp)
224         [ "$exp_val" == "$imp_val" ] ||
225                 error "export flags '$exp_val' != import flags '$imp_val'"
226
227         # Compare the value of client version
228         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
229         exp_val=$(version_code $exp_client_version)
230         imp_val=$CLIENT_VERSION
231         [ "$exp_val" == "$imp_val" ] ||
232                 error "export client version '$exp_val' != '$imp_val'"
233 }
234 run_test 0d "check export proc ============================="
235
236 test_1() {
237         test_mkdir $DIR/$tdir
238         test_mkdir $DIR/$tdir/d2
239         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
240         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
241         rmdir $DIR/$tdir/d2
242         rmdir $DIR/$tdir
243         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
244 }
245 run_test 1 "mkdir; remkdir; rmdir"
246
247 test_2() {
248         test_mkdir $DIR/$tdir
249         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
250         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
251         rm -r $DIR/$tdir
252         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
253 }
254 run_test 2 "mkdir; touch; rmdir; check file"
255
256 test_3() {
257         test_mkdir $DIR/$tdir
258         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
259         touch $DIR/$tdir/$tfile
260         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
261         rm -r $DIR/$tdir
262         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
263 }
264 run_test 3 "mkdir; touch; rmdir; check dir"
265
266 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
267 test_4() {
268         test_mkdir -i 1 $DIR/$tdir
269
270         touch $DIR/$tdir/$tfile ||
271                 error "Create file under remote directory failed"
272
273         rmdir $DIR/$tdir &&
274                 error "Expect error removing in-use dir $DIR/$tdir"
275
276         test -d $DIR/$tdir || error "Remote directory disappeared"
277
278         rm -rf $DIR/$tdir || error "remove remote dir error"
279 }
280 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
281
282 test_5() {
283         test_mkdir $DIR/$tdir
284         test_mkdir $DIR/$tdir/d2
285         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
286         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
287         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
288 }
289 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
290
291 test_6a() {
292         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
293         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
294         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
295                 error "$tfile does not have perm 0666 or UID $UID"
296         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
297         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
298                 error "$tfile should be 0666 and owned by UID $UID"
299 }
300 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
301
302 test_6c() {
303         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
304
305         touch $DIR/$tfile
306         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
307         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
308                 error "$tfile should be owned by UID $RUNAS_ID"
309         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
310         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
311                 error "$tfile should be owned by UID $RUNAS_ID"
312 }
313 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
314
315 test_6e() {
316         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
317
318         touch $DIR/$tfile
319         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
320         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
321                 error "$tfile should be owned by GID $UID"
322         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
323         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
324                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
325 }
326 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
327
328 test_6g() {
329         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
330
331         test_mkdir $DIR/$tdir
332         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
333         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
334         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
335         test_mkdir $DIR/$tdir/d/subdir
336         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
337                 error "$tdir/d/subdir should be GID $RUNAS_GID"
338         if [[ $MDSCOUNT -gt 1 ]]; then
339                 # check remote dir sgid inherite
340                 $LFS mkdir -i 0 $DIR/$tdir.local ||
341                         error "mkdir $tdir.local failed"
342                 chmod g+s $DIR/$tdir.local ||
343                         error "chmod $tdir.local failed"
344                 chgrp $RUNAS_GID $DIR/$tdir.local ||
345                         error "chgrp $tdir.local failed"
346                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
347                         error "mkdir $tdir.remote failed"
348                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
349                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
350                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
351                         error "$tdir.remote should be mode 02755"
352         fi
353 }
354 run_test 6g "verify new dir in sgid dir inherits group"
355
356 test_6h() { # bug 7331
357         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
358
359         touch $DIR/$tfile || error "touch failed"
360         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
361         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
362                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
363         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
364                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
365 }
366 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
367
368 test_7a() {
369         test_mkdir $DIR/$tdir
370         $MCREATE $DIR/$tdir/$tfile
371         chmod 0666 $DIR/$tdir/$tfile
372         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
373                 error "$tdir/$tfile should be mode 0666"
374 }
375 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
376
377 test_7b() {
378         if [ ! -d $DIR/$tdir ]; then
379                 test_mkdir $DIR/$tdir
380         fi
381         $MCREATE $DIR/$tdir/$tfile
382         echo -n foo > $DIR/$tdir/$tfile
383         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
384         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
385 }
386 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
387
388 test_8() {
389         test_mkdir $DIR/$tdir
390         touch $DIR/$tdir/$tfile
391         chmod 0666 $DIR/$tdir/$tfile
392         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
393                 error "$tfile mode not 0666"
394 }
395 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
396
397 test_9() {
398         test_mkdir $DIR/$tdir
399         test_mkdir $DIR/$tdir/d2
400         test_mkdir $DIR/$tdir/d2/d3
401         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
402 }
403 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
404
405 test_10() {
406         test_mkdir $DIR/$tdir
407         test_mkdir $DIR/$tdir/d2
408         touch $DIR/$tdir/d2/$tfile
409         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
410                 error "$tdir/d2/$tfile not a file"
411 }
412 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
413
414 test_11() {
415         test_mkdir $DIR/$tdir
416         test_mkdir $DIR/$tdir/d2
417         chmod 0666 $DIR/$tdir/d2
418         chmod 0705 $DIR/$tdir/d2
419         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
420                 error "$tdir/d2 mode not 0705"
421 }
422 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
423
424 test_12() {
425         test_mkdir $DIR/$tdir
426         touch $DIR/$tdir/$tfile
427         chmod 0666 $DIR/$tdir/$tfile
428         chmod 0654 $DIR/$tdir/$tfile
429         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
430                 error "$tdir/d2 mode not 0654"
431 }
432 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
433
434 test_13() {
435         test_mkdir $DIR/$tdir
436         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
437         >  $DIR/$tdir/$tfile
438         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
439                 error "$tdir/$tfile size not 0 after truncate"
440 }
441 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
442
443 test_14() {
444         test_mkdir $DIR/$tdir
445         touch $DIR/$tdir/$tfile
446         rm $DIR/$tdir/$tfile
447         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
448 }
449 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
450
451 test_15() {
452         test_mkdir $DIR/$tdir
453         touch $DIR/$tdir/$tfile
454         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
455         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
456                 error "$tdir/${tfile_2} not a file after rename"
457         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
458 }
459 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
460
461 test_16() {
462         test_mkdir $DIR/$tdir
463         touch $DIR/$tdir/$tfile
464         rm -rf $DIR/$tdir/$tfile
465         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
466 }
467 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
468
469 test_17a() {
470         test_mkdir $DIR/$tdir
471         touch $DIR/$tdir/$tfile
472         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
473         ls -l $DIR/$tdir
474         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
475                 error "$tdir/l-exist not a symlink"
476         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
477                 error "$tdir/l-exist not referencing a file"
478         rm -f $DIR/$tdir/l-exist
479         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
480 }
481 run_test 17a "symlinks: create, remove (real)"
482
483 test_17b() {
484         test_mkdir $DIR/$tdir
485         ln -s no-such-file $DIR/$tdir/l-dangle
486         ls -l $DIR/$tdir
487         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
488                 error "$tdir/l-dangle not referencing no-such-file"
489         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
490                 error "$tdir/l-dangle not referencing non-existent file"
491         rm -f $DIR/$tdir/l-dangle
492         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
493 }
494 run_test 17b "symlinks: create, remove (dangling)"
495
496 test_17c() { # bug 3440 - don't save failed open RPC for replay
497         test_mkdir $DIR/$tdir
498         ln -s foo $DIR/$tdir/$tfile
499         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
500 }
501 run_test 17c "symlinks: open dangling (should return error)"
502
503 test_17d() {
504         test_mkdir $DIR/$tdir
505         ln -s foo $DIR/$tdir/$tfile
506         touch $DIR/$tdir/$tfile || error "creating to new symlink"
507 }
508 run_test 17d "symlinks: create dangling"
509
510 test_17e() {
511         test_mkdir $DIR/$tdir
512         local foo=$DIR/$tdir/$tfile
513         ln -s $foo $foo || error "create symlink failed"
514         ls -l $foo || error "ls -l failed"
515         ls $foo && error "ls not failed" || true
516 }
517 run_test 17e "symlinks: create recursive symlink (should return error)"
518
519 test_17f() {
520         test_mkdir $DIR/$tdir
521         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
522         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
523         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
524         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
525         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
526         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
527         ls -l  $DIR/$tdir
528 }
529 run_test 17f "symlinks: long and very long symlink name"
530
531 # str_repeat(S, N) generate a string that is string S repeated N times
532 str_repeat() {
533         local s=$1
534         local n=$2
535         local ret=''
536         while [ $((n -= 1)) -ge 0 ]; do
537                 ret=$ret$s
538         done
539         echo $ret
540 }
541
542 # Long symlinks and LU-2241
543 test_17g() {
544         test_mkdir $DIR/$tdir
545         local TESTS="59 60 61 4094 4095"
546
547         # Fix for inode size boundary in 2.1.4
548         [ $MDS1_VERSION -lt $(version_code 2.1.4) ] &&
549                 TESTS="4094 4095"
550
551         # Patch not applied to 2.2 or 2.3 branches
552         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
553         [ $MDS1_VERSION -le $(version_code 2.3.55) ] &&
554                 TESTS="4094 4095"
555
556         # skip long symlink name for rhel6.5.
557         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
558         grep -q '6.5' /etc/redhat-release &>/dev/null &&
559                 TESTS="59 60 61 4062 4063"
560
561         for i in $TESTS; do
562                 local SYMNAME=$(str_repeat 'x' $i)
563                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
564                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
565         done
566 }
567 run_test 17g "symlinks: really long symlink name and inode boundaries"
568
569 test_17h() { #bug 17378
570         [ $PARALLEL == "yes" ] && skip "skip parallel run"
571         remote_mds_nodsh && skip "remote MDS with nodsh"
572
573         local mdt_idx
574
575         test_mkdir $DIR/$tdir
576         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
577         $LFS setstripe -c -1 $DIR/$tdir
578         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
579         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
580         touch $DIR/$tdir/$tfile || true
581 }
582 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
583
584 test_17i() { #bug 20018
585         [ $PARALLEL == "yes" ] && skip "skip parallel run"
586         remote_mds_nodsh && skip "remote MDS with nodsh"
587
588         local foo=$DIR/$tdir/$tfile
589         local mdt_idx
590
591         test_mkdir -c1 $DIR/$tdir
592         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
593         ln -s $foo $foo || error "create symlink failed"
594 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
595         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
596         ls -l $foo && error "error not detected"
597         return 0
598 }
599 run_test 17i "don't panic on short symlink (should return error)"
600
601 test_17k() { #bug 22301
602         [ $PARALLEL == "yes" ] && skip "skip parallel run"
603         [[ -z "$(which rsync 2>/dev/null)" ]] &&
604                 skip "no rsync command"
605         rsync --help | grep -q xattr ||
606                 skip_env "$(rsync --version | head -n1) does not support xattrs"
607         test_mkdir $DIR/$tdir
608         test_mkdir $DIR/$tdir.new
609         touch $DIR/$tdir/$tfile
610         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
611         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
612                 error "rsync failed with xattrs enabled"
613 }
614 run_test 17k "symlinks: rsync with xattrs enabled"
615
616 test_17l() { # LU-279
617         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
618                 skip "no getfattr command"
619
620         test_mkdir $DIR/$tdir
621         touch $DIR/$tdir/$tfile
622         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
623         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
624                 # -h to not follow symlinks. -m '' to list all the xattrs.
625                 # grep to remove first line: '# file: $path'.
626                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
627                 do
628                         lgetxattr_size_check $path $xattr ||
629                                 error "lgetxattr_size_check $path $xattr failed"
630                 done
631         done
632 }
633 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
634
635 # LU-1540
636 test_17m() {
637         [ $PARALLEL == "yes" ] && skip "skip parallel run"
638         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
639         remote_mds_nodsh && skip "remote MDS with nodsh"
640         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
641         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
642                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
643
644         local short_sym="0123456789"
645         local wdir=$DIR/$tdir
646         local i
647
648         test_mkdir $wdir
649         long_sym=$short_sym
650         # create a long symlink file
651         for ((i = 0; i < 4; ++i)); do
652                 long_sym=${long_sym}${long_sym}
653         done
654
655         echo "create 512 short and long symlink files under $wdir"
656         for ((i = 0; i < 256; ++i)); do
657                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
658                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
659         done
660
661         echo "erase them"
662         rm -f $wdir/*
663         sync
664         wait_delete_completed
665
666         echo "recreate the 512 symlink files with a shorter string"
667         for ((i = 0; i < 512; ++i)); do
668                 # rewrite the symlink file with a shorter string
669                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
670                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
671         done
672
673         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
674         local devname=$(mdsdevname $mds_index)
675
676         echo "stop and checking mds${mds_index}:"
677         # e2fsck should not return error
678         stop mds${mds_index}
679         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
680         rc=$?
681
682         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
683                 error "start mds${mds_index} failed"
684         df $MOUNT > /dev/null 2>&1
685         [ $rc -eq 0 ] ||
686                 error "e2fsck detected error for short/long symlink: rc=$rc"
687         rm -f $wdir/*
688 }
689 run_test 17m "run e2fsck against MDT which contains short/long symlink"
690
691 check_fs_consistency_17n() {
692         local mdt_index
693         local rc=0
694
695         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
696         # so it only check MDT1/MDT2 instead of all of MDTs.
697         for mdt_index in 1 2; do
698                 local devname=$(mdsdevname $mdt_index)
699                 # e2fsck should not return error
700                 stop mds${mdt_index}
701                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
702                         rc=$((rc + $?))
703
704                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
705                         error "mount mds$mdt_index failed"
706                 df $MOUNT > /dev/null 2>&1
707         done
708         return $rc
709 }
710
711 test_17n() {
712         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
713         [ $PARALLEL == "yes" ] && skip "skip parallel run"
714         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
715         remote_mds_nodsh && skip "remote MDS with nodsh"
716         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
717         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
718                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
719
720         local i
721
722         test_mkdir $DIR/$tdir
723         for ((i=0; i<10; i++)); do
724                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
725                         error "create remote dir error $i"
726                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
727                         error "create files under remote dir failed $i"
728         done
729
730         check_fs_consistency_17n ||
731                 error "e2fsck report error after create files under remote dir"
732
733         for ((i = 0; i < 10; i++)); do
734                 rm -rf $DIR/$tdir/remote_dir_${i} ||
735                         error "destroy remote dir error $i"
736         done
737
738         check_fs_consistency_17n ||
739                 error "e2fsck report error after unlink files under remote dir"
740
741         [ $MDS1_VERSION -lt $(version_code 2.4.50) ] &&
742                 skip "lustre < 2.4.50 does not support migrate mv"
743
744         for ((i = 0; i < 10; i++)); do
745                 mkdir -p $DIR/$tdir/remote_dir_${i}
746                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
747                         error "create files under remote dir failed $i"
748                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
749                         error "migrate remote dir error $i"
750         done
751         check_fs_consistency_17n || error "e2fsck report error after migration"
752
753         for ((i = 0; i < 10; i++)); do
754                 rm -rf $DIR/$tdir/remote_dir_${i} ||
755                         error "destroy remote dir error $i"
756         done
757
758         check_fs_consistency_17n || error "e2fsck report error after unlink"
759 }
760 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
761
762 test_17o() {
763         remote_mds_nodsh && skip "remote MDS with nodsh"
764         [ $MDS1_VERSION -lt $(version_code 2.3.64) ] &&
765                 skip "Need MDS version at least 2.3.64"
766
767         local wdir=$DIR/${tdir}o
768         local mdt_index
769         local rc=0
770
771         test_mkdir $wdir
772         touch $wdir/$tfile
773         mdt_index=$($LFS getstripe -m $wdir/$tfile)
774         mdt_index=$((mdt_index + 1))
775
776         cancel_lru_locks mdc
777         #fail mds will wait the failover finish then set
778         #following fail_loc to avoid interfer the recovery process.
779         fail mds${mdt_index}
780
781         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
782         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
783         ls -l $wdir/$tfile && rc=1
784         do_facet mds${mdt_index} lctl set_param fail_loc=0
785         [[ $rc -eq 0 ]] || error "stat file should fail"
786 }
787 run_test 17o "stat file with incompat LMA feature"
788
789 test_18() {
790         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
791         ls $DIR || error "Failed to ls $DIR: $?"
792 }
793 run_test 18 "touch .../f ; ls ... =============================="
794
795 test_19a() {
796         touch $DIR/$tfile
797         ls -l $DIR
798         rm $DIR/$tfile
799         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
800 }
801 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
802
803 test_19b() {
804         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
805 }
806 run_test 19b "ls -l .../f19 (should return error) =============="
807
808 test_19c() {
809         [ $RUNAS_ID -eq $UID ] &&
810                 skip_env "RUNAS_ID = UID = $UID -- skipping"
811
812         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
813 }
814 run_test 19c "$RUNAS touch .../f19 (should return error) =="
815
816 test_19d() {
817         cat $DIR/f19 && error || true
818 }
819 run_test 19d "cat .../f19 (should return error) =============="
820
821 test_20() {
822         touch $DIR/$tfile
823         rm $DIR/$tfile
824         touch $DIR/$tfile
825         rm $DIR/$tfile
826         touch $DIR/$tfile
827         rm $DIR/$tfile
828         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
829 }
830 run_test 20 "touch .../f ; ls -l ..."
831
832 test_21() {
833         test_mkdir $DIR/$tdir
834         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
835         ln -s dangle $DIR/$tdir/link
836         echo foo >> $DIR/$tdir/link
837         cat $DIR/$tdir/dangle
838         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
839         $CHECKSTAT -f -t file $DIR/$tdir/link ||
840                 error "$tdir/link not linked to a file"
841 }
842 run_test 21 "write to dangling link"
843
844 test_22() {
845         local wdir=$DIR/$tdir
846         test_mkdir $wdir
847         chown $RUNAS_ID:$RUNAS_GID $wdir
848         (cd $wdir || error "cd $wdir failed";
849                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
850                 $RUNAS tar xf -)
851         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
852         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
853         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
854                 error "checkstat -u failed"
855 }
856 run_test 22 "unpack tar archive as non-root user"
857
858 # was test_23
859 test_23a() {
860         test_mkdir $DIR/$tdir
861         local file=$DIR/$tdir/$tfile
862
863         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
864         openfile -f O_CREAT:O_EXCL $file &&
865                 error "$file recreate succeeded" || true
866 }
867 run_test 23a "O_CREAT|O_EXCL in subdir"
868
869 test_23b() { # bug 18988
870         test_mkdir $DIR/$tdir
871         local file=$DIR/$tdir/$tfile
872
873         rm -f $file
874         echo foo > $file || error "write filed"
875         echo bar >> $file || error "append filed"
876         $CHECKSTAT -s 8 $file || error "wrong size"
877         rm $file
878 }
879 run_test 23b "O_APPEND check"
880
881 # LU-9409, size with O_APPEND and tiny writes
882 test_23c() {
883         local file=$DIR/$tfile
884
885         # single dd
886         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
887         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
888         rm -f $file
889
890         # racing tiny writes
891         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
892         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
893         wait
894         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
895         rm -f $file
896
897         #racing tiny & normal writes
898         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
899         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
900         wait
901         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
902         rm -f $file
903
904         #racing tiny & normal writes 2, ugly numbers
905         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
906         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
907         wait
908         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
909         rm -f $file
910 }
911 run_test 23c "O_APPEND size checks for tiny writes"
912
913 # LU-11069 file offset is correct after appending writes
914 test_23d() {
915         local file=$DIR/$tfile
916         local offset
917
918         echo CentaurHauls > $file
919         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
920         if ((offset != 26)); then
921                 error "wrong offset, expected 26, got '$offset'"
922         fi
923 }
924 run_test 23d "file offset is correct after appending writes"
925
926 # rename sanity
927 test_24a() {
928         echo '-- same directory rename'
929         test_mkdir $DIR/$tdir
930         touch $DIR/$tdir/$tfile.1
931         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
932         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
933 }
934 run_test 24a "rename file to non-existent target"
935
936 test_24b() {
937         test_mkdir $DIR/$tdir
938         touch $DIR/$tdir/$tfile.{1,2}
939         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
940         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
941         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
942 }
943 run_test 24b "rename file to existing target"
944
945 test_24c() {
946         test_mkdir $DIR/$tdir
947         test_mkdir $DIR/$tdir/d$testnum.1
948         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
949         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
950         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
951 }
952 run_test 24c "rename directory to non-existent target"
953
954 test_24d() {
955         test_mkdir -c1 $DIR/$tdir
956         test_mkdir -c1 $DIR/$tdir/d$testnum.1
957         test_mkdir -c1 $DIR/$tdir/d$testnum.2
958         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
959         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
960         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
961 }
962 run_test 24d "rename directory to existing target"
963
964 test_24e() {
965         echo '-- cross directory renames --'
966         test_mkdir $DIR/R5a
967         test_mkdir $DIR/R5b
968         touch $DIR/R5a/f
969         mv $DIR/R5a/f $DIR/R5b/g
970         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
971         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
972 }
973 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
974
975 test_24f() {
976         test_mkdir $DIR/R6a
977         test_mkdir $DIR/R6b
978         touch $DIR/R6a/f $DIR/R6b/g
979         mv $DIR/R6a/f $DIR/R6b/g
980         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
981         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
982 }
983 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
984
985 test_24g() {
986         test_mkdir $DIR/R7a
987         test_mkdir $DIR/R7b
988         test_mkdir $DIR/R7a/d
989         mv $DIR/R7a/d $DIR/R7b/e
990         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
991         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
992 }
993 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
994
995 test_24h() {
996         test_mkdir -c1 $DIR/R8a
997         test_mkdir -c1 $DIR/R8b
998         test_mkdir -c1 $DIR/R8a/d
999         test_mkdir -c1 $DIR/R8b/e
1000         mrename $DIR/R8a/d $DIR/R8b/e
1001         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
1002         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
1003 }
1004 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
1005
1006 test_24i() {
1007         echo "-- rename error cases"
1008         test_mkdir $DIR/R9
1009         test_mkdir $DIR/R9/a
1010         touch $DIR/R9/f
1011         mrename $DIR/R9/f $DIR/R9/a
1012         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1013         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1014         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1015 }
1016 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1017
1018 test_24j() {
1019         test_mkdir $DIR/R10
1020         mrename $DIR/R10/f $DIR/R10/g
1021         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1022         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1023         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1024 }
1025 run_test 24j "source does not exist ============================"
1026
1027 test_24k() {
1028         test_mkdir $DIR/R11a
1029         test_mkdir $DIR/R11a/d
1030         touch $DIR/R11a/f
1031         mv $DIR/R11a/f $DIR/R11a/d
1032         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1033         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1034 }
1035 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1036
1037 # bug 2429 - rename foo foo foo creates invalid file
1038 test_24l() {
1039         f="$DIR/f24l"
1040         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1041 }
1042 run_test 24l "Renaming a file to itself ========================"
1043
1044 test_24m() {
1045         f="$DIR/f24m"
1046         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1047         # on ext3 this does not remove either the source or target files
1048         # though the "expected" operation would be to remove the source
1049         $CHECKSTAT -t file ${f} || error "${f} missing"
1050         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1051 }
1052 run_test 24m "Renaming a file to a hard link to itself ========="
1053
1054 test_24n() {
1055     f="$DIR/f24n"
1056     # this stats the old file after it was renamed, so it should fail
1057     touch ${f}
1058     $CHECKSTAT ${f} || error "${f} missing"
1059     mv ${f} ${f}.rename
1060     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1061     $CHECKSTAT -a ${f} || error "${f} exists"
1062 }
1063 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1064
1065 test_24o() {
1066         test_mkdir $DIR/$tdir
1067         rename_many -s random -v -n 10 $DIR/$tdir
1068 }
1069 run_test 24o "rename of files during htree split"
1070
1071 test_24p() {
1072         test_mkdir $DIR/R12a
1073         test_mkdir $DIR/R12b
1074         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1075         mrename $DIR/R12a $DIR/R12b
1076         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1077         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1078         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1079         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1080 }
1081 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1082
1083 cleanup_multiop_pause() {
1084         trap 0
1085         kill -USR1 $MULTIPID
1086 }
1087
1088 test_24q() {
1089         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1090
1091         test_mkdir $DIR/R13a
1092         test_mkdir $DIR/R13b
1093         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1094         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1095         MULTIPID=$!
1096
1097         trap cleanup_multiop_pause EXIT
1098         mrename $DIR/R13a $DIR/R13b
1099         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1100         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1101         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1102         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1103         cleanup_multiop_pause
1104         wait $MULTIPID || error "multiop close failed"
1105 }
1106 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1107
1108 test_24r() { #bug 3789
1109         test_mkdir $DIR/R14a
1110         test_mkdir $DIR/R14a/b
1111         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1112         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1113         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1114 }
1115 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1116
1117 test_24s() {
1118         test_mkdir $DIR/R15a
1119         test_mkdir $DIR/R15a/b
1120         test_mkdir $DIR/R15a/b/c
1121         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1122         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1123         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1124 }
1125 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1126 test_24t() {
1127         test_mkdir $DIR/R16a
1128         test_mkdir $DIR/R16a/b
1129         test_mkdir $DIR/R16a/b/c
1130         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1131         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1132         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1133 }
1134 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1135
1136 test_24u() { # bug12192
1137         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1138         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1139 }
1140 run_test 24u "create stripe file"
1141
1142 simple_cleanup_common() {
1143         local rc=0
1144         trap 0
1145         [ -z "$DIR" -o -z "$tdir" ] && return 0
1146
1147         local start=$SECONDS
1148         rm -rf $DIR/$tdir
1149         rc=$?
1150         wait_delete_completed
1151         echo "cleanup time $((SECONDS - start))"
1152         return $rc
1153 }
1154
1155 max_pages_per_rpc() {
1156         local mdtname="$(printf "MDT%04x" ${1:-0})"
1157         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1158 }
1159
1160 test_24v() {
1161         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1162
1163         local nrfiles=${COUNT:-100000}
1164         local fname="$DIR/$tdir/$tfile"
1165
1166         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1167         [ "$mds1_FSTYPE" = "zfs" ] && nrfiles=${COUNT:-10000}
1168
1169         test_mkdir "$(dirname $fname)"
1170         # assume MDT0000 has the fewest inodes
1171         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1172         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1173         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1174
1175         trap simple_cleanup_common EXIT
1176
1177         createmany -m "$fname" $nrfiles
1178
1179         cancel_lru_locks mdc
1180         lctl set_param mdc.*.stats clear
1181
1182         # was previously test_24D: LU-6101
1183         # readdir() returns correct number of entries after cursor reload
1184         local num_ls=$(ls $DIR/$tdir | wc -l)
1185         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1186         local num_all=$(ls -a $DIR/$tdir | wc -l)
1187         if [ $num_ls -ne $nrfiles -o $num_uniq -ne $nrfiles -o \
1188              $num_all -ne $((nrfiles + 2)) ]; then
1189                 error "Expected $nrfiles files, got $num_ls " \
1190                         "($num_uniq unique $num_all .&..)"
1191         fi
1192         # LU-5 large readdir
1193         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1194         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1195         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1196         # take into account of overhead in lu_dirpage header and end mark in
1197         # each page, plus one in rpc_num calculation.
1198         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1199         local page_entries=$(((PAGE_SIZE - 24) / dirent_size))
1200         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1201         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1202         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1203         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1204         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1205         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1206                 error "large readdir doesn't take effect: " \
1207                       "$mds_readpage should be about $rpc_max"
1208
1209         simple_cleanup_common
1210 }
1211 run_test 24v "list large directory (test hash collision, b=17560)"
1212
1213 test_24w() { # bug21506
1214         SZ1=234852
1215         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1216         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1217         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1218         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1219         [[ "$SZ1" -eq "$SZ2" ]] ||
1220                 error "Error reading at the end of the file $tfile"
1221 }
1222 run_test 24w "Reading a file larger than 4Gb"
1223
1224 test_24x() {
1225         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1226         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1227         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1228                 skip "Need MDS version at least 2.7.56"
1229
1230         local MDTIDX=1
1231         local remote_dir=$DIR/$tdir/remote_dir
1232
1233         test_mkdir $DIR/$tdir
1234         $LFS mkdir -i $MDTIDX $remote_dir ||
1235                 error "create remote directory failed"
1236
1237         test_mkdir $DIR/$tdir/src_dir
1238         touch $DIR/$tdir/src_file
1239         test_mkdir $remote_dir/tgt_dir
1240         touch $remote_dir/tgt_file
1241
1242         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1243                 error "rename dir cross MDT failed!"
1244
1245         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1246                 error "rename file cross MDT failed!"
1247
1248         touch $DIR/$tdir/ln_file
1249         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1250                 error "ln file cross MDT failed"
1251
1252         rm -rf $DIR/$tdir || error "Can not delete directories"
1253 }
1254 run_test 24x "cross MDT rename/link"
1255
1256 test_24y() {
1257         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1258         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1259
1260         local remote_dir=$DIR/$tdir/remote_dir
1261         local mdtidx=1
1262
1263         test_mkdir $DIR/$tdir
1264         $LFS mkdir -i $mdtidx $remote_dir ||
1265                 error "create remote directory failed"
1266
1267         test_mkdir $remote_dir/src_dir
1268         touch $remote_dir/src_file
1269         test_mkdir $remote_dir/tgt_dir
1270         touch $remote_dir/tgt_file
1271
1272         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1273                 error "rename subdir in the same remote dir failed!"
1274
1275         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1276                 error "rename files in the same remote dir failed!"
1277
1278         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1279                 error "link files in the same remote dir failed!"
1280
1281         rm -rf $DIR/$tdir || error "Can not delete directories"
1282 }
1283 run_test 24y "rename/link on the same dir should succeed"
1284
1285 test_24z() {
1286         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1287         [[ $MDS1_VERSION -lt $(version_code 2.12.51) ]] &&
1288                 skip "Need MDS version at least 2.12.51"
1289
1290         local index
1291
1292         for index in 0 1; do
1293                 $LFS mkdir -i $index $DIR/$tdir.$index || error "mkdir failed"
1294                 touch $DIR/$tdir.0/$tfile.$index || error "touch failed"
1295         done
1296
1297         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1 || error "mv $tfile.0 failed"
1298
1299         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.0)
1300         [ $index -eq 0 ] || error "$tfile.0 is on MDT$index"
1301
1302         local mdts=$(comma_list $(mdts_nodes))
1303
1304         do_nodes $mdts $LCTL set_param mdt.*.enable_remote_rename=0
1305         stack_trap "do_nodes $mdts $LCTL \
1306                 set_param mdt.*.enable_remote_rename=1" EXIT
1307
1308         mv $DIR/$tdir.0/$tfile.1 $DIR/$tdir.1 || error "mv $tfile.1 failed"
1309
1310         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.1)
1311         [ $index -eq 1 ] || error "$tfile.1 is on MDT$index"
1312 }
1313 run_test 24z "cross-MDT rename is done as cp"
1314
1315 test_24A() { # LU-3182
1316         local NFILES=5000
1317
1318         rm -rf $DIR/$tdir
1319         test_mkdir $DIR/$tdir
1320         trap simple_cleanup_common EXIT
1321         createmany -m $DIR/$tdir/$tfile $NFILES
1322         local t=$(ls $DIR/$tdir | wc -l)
1323         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1324         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1325         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1326                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1327         fi
1328
1329         simple_cleanup_common || error "Can not delete directories"
1330 }
1331 run_test 24A "readdir() returns correct number of entries."
1332
1333 test_24B() { # LU-4805
1334         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1335
1336         local count
1337
1338         test_mkdir $DIR/$tdir
1339         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1340                 error "create striped dir failed"
1341
1342         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1343         [ $count -eq 2 ] || error "Expected 2, got $count"
1344
1345         touch $DIR/$tdir/striped_dir/a
1346
1347         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1348         [ $count -eq 3 ] || error "Expected 3, got $count"
1349
1350         touch $DIR/$tdir/striped_dir/.f
1351
1352         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1353         [ $count -eq 4 ] || error "Expected 4, got $count"
1354
1355         rm -rf $DIR/$tdir || error "Can not delete directories"
1356 }
1357 run_test 24B "readdir for striped dir return correct number of entries"
1358
1359 test_24C() {
1360         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1361
1362         mkdir $DIR/$tdir
1363         mkdir $DIR/$tdir/d0
1364         mkdir $DIR/$tdir/d1
1365
1366         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1367                 error "create striped dir failed"
1368
1369         cd $DIR/$tdir/d0/striped_dir
1370
1371         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1372         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1373         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1374
1375         [ "$d0_ino" = "$parent_ino" ] ||
1376                 error ".. wrong, expect $d0_ino, get $parent_ino"
1377
1378         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1379                 error "mv striped dir failed"
1380
1381         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1382
1383         [ "$d1_ino" = "$parent_ino" ] ||
1384                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1385 }
1386 run_test 24C "check .. in striped dir"
1387
1388 test_24E() {
1389         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1390         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1391
1392         mkdir -p $DIR/$tdir
1393         mkdir $DIR/$tdir/src_dir
1394         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1395                 error "create remote source failed"
1396
1397         touch $DIR/$tdir/src_dir/src_child/a
1398
1399         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1400                 error "create remote target dir failed"
1401
1402         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1403                 error "create remote target child failed"
1404
1405         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1406                 error "rename dir cross MDT failed!"
1407
1408         find $DIR/$tdir
1409
1410         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1411                 error "src_child still exists after rename"
1412
1413         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1414                 error "missing file(a) after rename"
1415
1416         rm -rf $DIR/$tdir || error "Can not delete directories"
1417 }
1418 run_test 24E "cross MDT rename/link"
1419
1420 test_24F () {
1421         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
1422
1423         local repeats=1000
1424         [ "$SLOW" = "no" ] && repeats=100
1425
1426         mkdir -p $DIR/$tdir
1427
1428         echo "$repeats repeats"
1429         for ((i = 0; i < repeats; i++)); do
1430                 $LFS mkdir -i0 -c2 $DIR/$tdir/test || error "mkdir fails"
1431                 touch $DIR/$tdir/test/a || error "touch fails"
1432                 mkdir $DIR/$tdir/test/b || error "mkdir fails"
1433                 rm -rf $DIR/$tdir/test || error "rmdir fails"
1434         done
1435
1436         true
1437 }
1438 run_test 24F "hash order vs readdir (LU-11330)"
1439
1440 test_25a() {
1441         echo '== symlink sanity ============================================='
1442
1443         test_mkdir $DIR/d25
1444         ln -s d25 $DIR/s25
1445         touch $DIR/s25/foo ||
1446                 error "File creation in symlinked directory failed"
1447 }
1448 run_test 25a "create file in symlinked directory ==============="
1449
1450 test_25b() {
1451         [ ! -d $DIR/d25 ] && test_25a
1452         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1453 }
1454 run_test 25b "lookup file in symlinked directory ==============="
1455
1456 test_26a() {
1457         test_mkdir $DIR/d26
1458         test_mkdir $DIR/d26/d26-2
1459         ln -s d26/d26-2 $DIR/s26
1460         touch $DIR/s26/foo || error "File creation failed"
1461 }
1462 run_test 26a "multiple component symlink ======================="
1463
1464 test_26b() {
1465         test_mkdir -p $DIR/$tdir/d26-2
1466         ln -s $tdir/d26-2/foo $DIR/s26-2
1467         touch $DIR/s26-2 || error "File creation failed"
1468 }
1469 run_test 26b "multiple component symlink at end of lookup ======"
1470
1471 test_26c() {
1472         test_mkdir $DIR/d26.2
1473         touch $DIR/d26.2/foo
1474         ln -s d26.2 $DIR/s26.2-1
1475         ln -s s26.2-1 $DIR/s26.2-2
1476         ln -s s26.2-2 $DIR/s26.2-3
1477         chmod 0666 $DIR/s26.2-3/foo
1478 }
1479 run_test 26c "chain of symlinks"
1480
1481 # recursive symlinks (bug 439)
1482 test_26d() {
1483         ln -s d26-3/foo $DIR/d26-3
1484 }
1485 run_test 26d "create multiple component recursive symlink"
1486
1487 test_26e() {
1488         [ ! -h $DIR/d26-3 ] && test_26d
1489         rm $DIR/d26-3
1490 }
1491 run_test 26e "unlink multiple component recursive symlink"
1492
1493 # recursive symlinks (bug 7022)
1494 test_26f() {
1495         test_mkdir $DIR/$tdir
1496         test_mkdir $DIR/$tdir/$tfile
1497         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1498         test_mkdir -p lndir/bar1
1499         test_mkdir $DIR/$tdir/$tfile/$tfile
1500         cd $tfile                || error "cd $tfile failed"
1501         ln -s .. dotdot          || error "ln dotdot failed"
1502         ln -s dotdot/lndir lndir || error "ln lndir failed"
1503         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1504         output=`ls $tfile/$tfile/lndir/bar1`
1505         [ "$output" = bar1 ] && error "unexpected output"
1506         rm -r $tfile             || error "rm $tfile failed"
1507         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1508 }
1509 run_test 26f "rm -r of a directory which has recursive symlink"
1510
1511 test_27a() {
1512         test_mkdir $DIR/$tdir
1513         $LFS getstripe $DIR/$tdir
1514         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1515         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1516         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1517 }
1518 run_test 27a "one stripe file"
1519
1520 test_27b() {
1521         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1522
1523         test_mkdir $DIR/$tdir
1524         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1525         $LFS getstripe -c $DIR/$tdir/$tfile
1526         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1527                 error "two-stripe file doesn't have two stripes"
1528
1529         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1530 }
1531 run_test 27b "create and write to two stripe file"
1532
1533 test_27d() {
1534         test_mkdir $DIR/$tdir
1535         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1536                 error "setstripe failed"
1537         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1538         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1539 }
1540 run_test 27d "create file with default settings"
1541
1542 test_27e() {
1543         # LU-5839 adds check for existed layout before setting it
1544         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1545                 skip "Need MDS version at least 2.7.56"
1546
1547         test_mkdir $DIR/$tdir
1548         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1549         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1550         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1551 }
1552 run_test 27e "setstripe existing file (should return error)"
1553
1554 test_27f() {
1555         test_mkdir $DIR/$tdir
1556         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1557                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1558         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1559                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1560         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1561         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1562 }
1563 run_test 27f "setstripe with bad stripe size (should return error)"
1564
1565 test_27g() {
1566         test_mkdir $DIR/$tdir
1567         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1568         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1569                 error "$DIR/$tdir/$tfile has object"
1570 }
1571 run_test 27g "$LFS getstripe with no objects"
1572
1573 test_27ga() {
1574         test_mkdir $DIR/$tdir
1575         touch $DIR/$tdir/$tfile || error "touch failed"
1576         ln -s bogus $DIR/$tdir/$tfile.2 || error "ln failed"
1577         $LFS getstripe -m $DIR/$tdir/$tfile $DIR/$tdir/$tfile.2
1578         local rc=$?
1579         (( rc == 2 )) || error "getstripe did not return ENOENT"
1580 }
1581 run_test 27ga "$LFS getstripe with missing file (should return error)"
1582
1583 test_27i() {
1584         test_mkdir $DIR/$tdir
1585         touch $DIR/$tdir/$tfile || error "touch failed"
1586         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1587                 error "missing objects"
1588 }
1589 run_test 27i "$LFS getstripe with some objects"
1590
1591 test_27j() {
1592         test_mkdir $DIR/$tdir
1593         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1594                 error "setstripe failed" || true
1595 }
1596 run_test 27j "setstripe with bad stripe offset (should return error)"
1597
1598 test_27k() { # bug 2844
1599         test_mkdir $DIR/$tdir
1600         local file=$DIR/$tdir/$tfile
1601         local ll_max_blksize=$((4 * 1024 * 1024))
1602         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1603         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1604         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1605         dd if=/dev/zero of=$file bs=4k count=1
1606         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1607         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1608 }
1609 run_test 27k "limit i_blksize for broken user apps"
1610
1611 test_27l() {
1612         mcreate $DIR/$tfile || error "creating file"
1613         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1614                 error "setstripe should have failed" || true
1615 }
1616 run_test 27l "check setstripe permissions (should return error)"
1617
1618 test_27m() {
1619         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1620
1621         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1622                    head -n1)
1623         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1624                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1625         fi
1626         trap simple_cleanup_common EXIT
1627         test_mkdir $DIR/$tdir
1628         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1629         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1630                 error "dd should fill OST0"
1631         i=2
1632         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1633                 i=$((i + 1))
1634                 [ $i -gt 256 ] && break
1635         done
1636         i=$((i + 1))
1637         touch $DIR/$tdir/$tfile.$i
1638         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1639             awk '{print $1}'| grep -w "0") ] &&
1640                 error "OST0 was full but new created file still use it"
1641         i=$((i + 1))
1642         touch $DIR/$tdir/$tfile.$i
1643         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1644             awk '{print $1}'| grep -w "0") ] &&
1645                 error "OST0 was full but new created file still use it"
1646         simple_cleanup_common
1647 }
1648 run_test 27m "create file while OST0 was full"
1649
1650 sleep_maxage() {
1651         local delay=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage |
1652                       awk '{ print $1 * 2; exit; }')
1653         sleep $delay
1654 }
1655
1656 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1657 # if the OST isn't full anymore.
1658 reset_enospc() {
1659         local OSTIDX=${1:-""}
1660
1661         local list=$(comma_list $(osts_nodes))
1662         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1663
1664         do_nodes $list lctl set_param fail_loc=0
1665         sync    # initiate all OST_DESTROYs from MDS to OST
1666         sleep_maxage
1667 }
1668
1669 exhaust_precreations() {
1670         local OSTIDX=$1
1671         local FAILLOC=$2
1672         local FAILIDX=${3:-$OSTIDX}
1673         local ofacet=ost$((OSTIDX + 1))
1674
1675         test_mkdir -p -c1 $DIR/$tdir
1676         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1677         local mfacet=mds$((mdtidx + 1))
1678         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1679
1680         local OST=$(ostname_from_index $OSTIDX)
1681
1682         # on the mdt's osc
1683         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1684         local last_id=$(do_facet $mfacet lctl get_param -n \
1685                         osc.$mdtosc_proc1.prealloc_last_id)
1686         local next_id=$(do_facet $mfacet lctl get_param -n \
1687                         osc.$mdtosc_proc1.prealloc_next_id)
1688
1689         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1690         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1691
1692         test_mkdir -p $DIR/$tdir/${OST}
1693         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1694 #define OBD_FAIL_OST_ENOSPC              0x215
1695         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1696         echo "Creating to objid $last_id on ost $OST..."
1697         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1698         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1699         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1700         sleep_maxage
1701 }
1702
1703 exhaust_all_precreations() {
1704         local i
1705         for (( i=0; i < OSTCOUNT; i++ )) ; do
1706                 exhaust_precreations $i $1 -1
1707         done
1708 }
1709
1710 test_27n() {
1711         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1712         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1713         remote_mds_nodsh && skip "remote MDS with nodsh"
1714         remote_ost_nodsh && skip "remote OST with nodsh"
1715
1716         reset_enospc
1717         rm -f $DIR/$tdir/$tfile
1718         exhaust_precreations 0 0x80000215
1719         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1720         touch $DIR/$tdir/$tfile || error "touch failed"
1721         $LFS getstripe $DIR/$tdir/$tfile
1722         reset_enospc
1723 }
1724 run_test 27n "create file with some full OSTs"
1725
1726 test_27o() {
1727         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1728         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1729         remote_mds_nodsh && skip "remote MDS with nodsh"
1730         remote_ost_nodsh && skip "remote OST with nodsh"
1731
1732         reset_enospc
1733         rm -f $DIR/$tdir/$tfile
1734         exhaust_all_precreations 0x215
1735
1736         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1737
1738         reset_enospc
1739         rm -rf $DIR/$tdir/*
1740 }
1741 run_test 27o "create file with all full OSTs (should error)"
1742
1743 test_27p() {
1744         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1745         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1746         remote_mds_nodsh && skip "remote MDS with nodsh"
1747         remote_ost_nodsh && skip "remote OST with nodsh"
1748
1749         reset_enospc
1750         rm -f $DIR/$tdir/$tfile
1751         test_mkdir $DIR/$tdir
1752
1753         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1754         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1755         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1756
1757         exhaust_precreations 0 0x80000215
1758         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1759         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1760         $LFS getstripe $DIR/$tdir/$tfile
1761
1762         reset_enospc
1763 }
1764 run_test 27p "append to a truncated file with some full OSTs"
1765
1766 test_27q() {
1767         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1768         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1769         remote_mds_nodsh && skip "remote MDS with nodsh"
1770         remote_ost_nodsh && skip "remote OST with nodsh"
1771
1772         reset_enospc
1773         rm -f $DIR/$tdir/$tfile
1774
1775         test_mkdir $DIR/$tdir
1776         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1777         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1778                 error "truncate $DIR/$tdir/$tfile failed"
1779         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1780
1781         exhaust_all_precreations 0x215
1782
1783         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1784         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1785
1786         reset_enospc
1787 }
1788 run_test 27q "append to truncated file with all OSTs full (should error)"
1789
1790 test_27r() {
1791         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1792         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1793         remote_mds_nodsh && skip "remote MDS with nodsh"
1794         remote_ost_nodsh && skip "remote OST with nodsh"
1795
1796         reset_enospc
1797         rm -f $DIR/$tdir/$tfile
1798         exhaust_precreations 0 0x80000215
1799
1800         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1801
1802         reset_enospc
1803 }
1804 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1805
1806 test_27s() { # bug 10725
1807         test_mkdir $DIR/$tdir
1808         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1809         local stripe_count=0
1810         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1811         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1812                 error "stripe width >= 2^32 succeeded" || true
1813
1814 }
1815 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1816
1817 test_27t() { # bug 10864
1818         WDIR=$(pwd)
1819         WLFS=$(which lfs)
1820         cd $DIR
1821         touch $tfile
1822         $WLFS getstripe $tfile
1823         cd $WDIR
1824 }
1825 run_test 27t "check that utils parse path correctly"
1826
1827 test_27u() { # bug 4900
1828         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1829         remote_mds_nodsh && skip "remote MDS with nodsh"
1830
1831         local index
1832         local list=$(comma_list $(mdts_nodes))
1833
1834 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1835         do_nodes $list $LCTL set_param fail_loc=0x139
1836         test_mkdir -p $DIR/$tdir
1837         trap simple_cleanup_common EXIT
1838         createmany -o $DIR/$tdir/t- 1000
1839         do_nodes $list $LCTL set_param fail_loc=0
1840
1841         TLOG=$TMP/$tfile.getstripe
1842         $LFS getstripe $DIR/$tdir > $TLOG
1843         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1844         unlinkmany $DIR/$tdir/t- 1000
1845         trap 0
1846         [[ $OBJS -gt 0 ]] &&
1847                 error "$OBJS objects created on OST-0. See $TLOG" ||
1848                 rm -f $TLOG
1849 }
1850 run_test 27u "skip object creation on OSC w/o objects"
1851
1852 test_27v() { # bug 4900
1853         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1854         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1855         remote_mds_nodsh && skip "remote MDS with nodsh"
1856         remote_ost_nodsh && skip "remote OST with nodsh"
1857
1858         exhaust_all_precreations 0x215
1859         reset_enospc
1860
1861         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1862
1863         touch $DIR/$tdir/$tfile
1864         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1865         # all except ost1
1866         for (( i=1; i < OSTCOUNT; i++ )); do
1867                 do_facet ost$i lctl set_param fail_loc=0x705
1868         done
1869         local START=`date +%s`
1870         createmany -o $DIR/$tdir/$tfile 32
1871
1872         local FINISH=`date +%s`
1873         local TIMEOUT=`lctl get_param -n timeout`
1874         local PROCESS=$((FINISH - START))
1875         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1876                error "$FINISH - $START >= $TIMEOUT / 2"
1877         sleep $((TIMEOUT / 2 - PROCESS))
1878         reset_enospc
1879 }
1880 run_test 27v "skip object creation on slow OST"
1881
1882 test_27w() { # bug 10997
1883         test_mkdir $DIR/$tdir
1884         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1885         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1886                 error "stripe size $size != 65536" || true
1887         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1888                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1889 }
1890 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1891
1892 test_27wa() {
1893         [[ $OSTCOUNT -lt 2 ]] &&
1894                 skip_env "skipping multiple stripe count/offset test"
1895
1896         test_mkdir $DIR/$tdir
1897         for i in $(seq 1 $OSTCOUNT); do
1898                 offset=$((i - 1))
1899                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
1900                         error "setstripe -c $i -i $offset failed"
1901                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
1902                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
1903                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1904                 [ $index -ne $offset ] &&
1905                         error "stripe offset $index != $offset" || true
1906         done
1907 }
1908 run_test 27wa "check $LFS setstripe -c -i options"
1909
1910 test_27x() {
1911         remote_ost_nodsh && skip "remote OST with nodsh"
1912         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1913         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1914
1915         OFFSET=$(($OSTCOUNT - 1))
1916         OSTIDX=0
1917         local OST=$(ostname_from_index $OSTIDX)
1918
1919         test_mkdir $DIR/$tdir
1920         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
1921         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1922         sleep_maxage
1923         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1924         for i in $(seq 0 $OFFSET); do
1925                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
1926                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
1927                 error "OST0 was degraded but new created file still use it"
1928         done
1929         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1930 }
1931 run_test 27x "create files while OST0 is degraded"
1932
1933 test_27y() {
1934         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1935         remote_mds_nodsh && skip "remote MDS with nodsh"
1936         remote_ost_nodsh && skip "remote OST with nodsh"
1937         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1938
1939         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1940         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1941                 osc.$mdtosc.prealloc_last_id)
1942         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1943                 osc.$mdtosc.prealloc_next_id)
1944         local fcount=$((last_id - next_id))
1945         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
1946         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1947
1948         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1949                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1950         local OST_DEACTIVE_IDX=-1
1951         local OSC
1952         local OSTIDX
1953         local OST
1954
1955         for OSC in $MDS_OSCS; do
1956                 OST=$(osc_to_ost $OSC)
1957                 OSTIDX=$(index_from_ostuuid $OST)
1958                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1959                         OST_DEACTIVE_IDX=$OSTIDX
1960                 fi
1961                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1962                         echo $OSC "is Deactivated:"
1963                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1964                 fi
1965         done
1966
1967         OSTIDX=$(index_from_ostuuid $OST)
1968         test_mkdir $DIR/$tdir
1969         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
1970
1971         for OSC in $MDS_OSCS; do
1972                 OST=$(osc_to_ost $OSC)
1973                 OSTIDX=$(index_from_ostuuid $OST)
1974                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1975                         echo $OST "is degraded:"
1976                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1977                                                 obdfilter.$OST.degraded=1
1978                 fi
1979         done
1980
1981         sleep_maxage
1982         createmany -o $DIR/$tdir/$tfile $fcount
1983
1984         for OSC in $MDS_OSCS; do
1985                 OST=$(osc_to_ost $OSC)
1986                 OSTIDX=$(index_from_ostuuid $OST)
1987                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1988                         echo $OST "is recovered from degraded:"
1989                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1990                                                 obdfilter.$OST.degraded=0
1991                 else
1992                         do_facet $SINGLEMDS lctl --device %$OSC activate
1993                 fi
1994         done
1995
1996         # all osp devices get activated, hence -1 stripe count restored
1997         local stripe_count=0
1998
1999         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
2000         # devices get activated.
2001         sleep_maxage
2002         $LFS setstripe -c -1 $DIR/$tfile
2003         stripe_count=$($LFS getstripe -c $DIR/$tfile)
2004         rm -f $DIR/$tfile
2005         [ $stripe_count -ne $OSTCOUNT ] &&
2006                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
2007         return 0
2008 }
2009 run_test 27y "create files while OST0 is degraded and the rest inactive"
2010
2011 check_seq_oid()
2012 {
2013         log "check file $1"
2014
2015         lmm_count=$($GETSTRIPE -c $1)
2016         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
2017         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
2018
2019         local old_ifs="$IFS"
2020         IFS=$'[:]'
2021         fid=($($LFS path2fid $1))
2022         IFS="$old_ifs"
2023
2024         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
2025         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
2026
2027         # compare lmm_seq and lu_fid->f_seq
2028         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
2029         # compare lmm_object_id and lu_fid->oid
2030         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
2031
2032         # check the trusted.fid attribute of the OST objects of the file
2033         local have_obdidx=false
2034         local stripe_nr=0
2035         $GETSTRIPE $1 | while read obdidx oid hex seq; do
2036                 # skip lines up to and including "obdidx"
2037                 [ -z "$obdidx" ] && break
2038                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
2039                 $have_obdidx || continue
2040
2041                 local ost=$((obdidx + 1))
2042                 local dev=$(ostdevname $ost)
2043                 local oid_hex
2044
2045                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
2046
2047                 seq=$(echo $seq | sed -e "s/^0x//g")
2048                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
2049                         oid_hex=$(echo $oid)
2050                 else
2051                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
2052                 fi
2053                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
2054
2055                 local ff=""
2056                 #
2057                 # Don't unmount/remount the OSTs if we don't need to do that.
2058                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2059                 # update too, until that use mount/ll_decode_filter_fid/mount.
2060                 # Re-enable when debugfs will understand new filter_fid.
2061                 #
2062                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2063                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2064                                 $dev 2>/dev/null" | grep "parent=")
2065                 fi
2066                 if [ -z "$ff" ]; then
2067                         stop ost$ost
2068                         mount_fstype ost$ost
2069                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2070                                 $(facet_mntpt ost$ost)/$obj_file)
2071                         unmount_fstype ost$ost
2072                         start ost$ost $dev $OST_MOUNT_OPTS
2073                         clients_up
2074                 fi
2075
2076                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2077
2078                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2079
2080                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2081                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2082                 #
2083                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2084                 #       stripe_size=1048576 component_id=1 component_start=0 \
2085                 #       component_end=33554432
2086                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2087                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2088                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2089                 local ff_pstripe
2090                 if grep -q 'stripe=' <<<$ff; then
2091                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2092                 else
2093                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2094                         # into f_ver in this case.  See comment on ff_parent.
2095                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2096                 fi
2097
2098                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2099                 [ $ff_pseq = $lmm_seq ] ||
2100                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2101                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2102                 [ $ff_poid = $lmm_oid ] ||
2103                         error "FF parent OID $ff_poid != $lmm_oid"
2104                 (($ff_pstripe == $stripe_nr)) ||
2105                         error "FF stripe $ff_pstripe != $stripe_nr"
2106
2107                 stripe_nr=$((stripe_nr + 1))
2108                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2109                         continue
2110                 if grep -q 'stripe_count=' <<<$ff; then
2111                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2112                                             -e 's/ .*//' <<<$ff)
2113                         [ $lmm_count = $ff_scnt ] ||
2114                                 error "FF stripe count $lmm_count != $ff_scnt"
2115                 fi
2116         done
2117 }
2118
2119 test_27z() {
2120         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2121         remote_ost_nodsh && skip "remote OST with nodsh"
2122
2123         test_mkdir $DIR/$tdir
2124         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2125                 { error "setstripe -c -1 failed"; return 1; }
2126         # We need to send a write to every object to get parent FID info set.
2127         # This _should_ also work for setattr, but does not currently.
2128         # touch $DIR/$tdir/$tfile-1 ||
2129         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2130                 { error "dd $tfile-1 failed"; return 2; }
2131         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2132                 { error "setstripe -c -1 failed"; return 3; }
2133         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2134                 { error "dd $tfile-2 failed"; return 4; }
2135
2136         # make sure write RPCs have been sent to OSTs
2137         sync; sleep 5; sync
2138
2139         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2140         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2141 }
2142 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2143
2144 test_27A() { # b=19102
2145         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2146
2147         save_layout_restore_at_exit $MOUNT
2148         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2149         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2150                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
2151         local default_size=$($GETSTRIPE -S $MOUNT)
2152         local default_offset=$($GETSTRIPE -i $MOUNT)
2153         local dsize=$(do_facet $SINGLEMDS \
2154                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2155         [ $default_size -eq $dsize ] ||
2156                 error "stripe size $default_size != $dsize"
2157         [ $default_offset -eq -1 ] ||
2158                 error "stripe offset $default_offset != -1"
2159 }
2160 run_test 27A "check filesystem-wide default LOV EA values"
2161
2162 test_27B() { # LU-2523
2163         test_mkdir $DIR/$tdir
2164         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2165         touch $DIR/$tdir/f0
2166         # open f1 with O_LOV_DELAY_CREATE
2167         # rename f0 onto f1
2168         # call setstripe ioctl on open file descriptor for f1
2169         # close
2170         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2171                 $DIR/$tdir/f0
2172
2173         rm -f $DIR/$tdir/f1
2174         # open f1 with O_LOV_DELAY_CREATE
2175         # unlink f1
2176         # call setstripe ioctl on open file descriptor for f1
2177         # close
2178         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2179
2180         # Allow multiop to fail in imitation of NFS's busted semantics.
2181         true
2182 }
2183 run_test 27B "call setstripe on open unlinked file/rename victim"
2184
2185 test_27C() { #LU-2871
2186         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2187
2188         declare -a ost_idx
2189         local index
2190         local found
2191         local i
2192         local j
2193
2194         test_mkdir $DIR/$tdir
2195         cd $DIR/$tdir
2196         for i in $(seq 0 $((OSTCOUNT - 1))); do
2197                 # set stripe across all OSTs starting from OST$i
2198                 $SETSTRIPE -i $i -c -1 $tfile$i
2199                 # get striping information
2200                 ost_idx=($($GETSTRIPE $tfile$i |
2201                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2202                 echo ${ost_idx[@]}
2203
2204                 # check the layout
2205                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2206                         error "${#ost_idx[@]} != $OSTCOUNT"
2207
2208                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2209                         found=0
2210                         for j in $(echo ${ost_idx[@]}); do
2211                                 if [ $index -eq $j ]; then
2212                                         found=1
2213                                         break
2214                                 fi
2215                         done
2216                         [ $found = 1 ] ||
2217                                 error "Can not find $index in ${ost_idx[@]}"
2218                 done
2219         done
2220 }
2221 run_test 27C "check full striping across all OSTs"
2222
2223 test_27D() {
2224         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2225         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2226         remote_mds_nodsh && skip "remote MDS with nodsh"
2227
2228         local POOL=${POOL:-testpool}
2229         local first_ost=0
2230         local last_ost=$(($OSTCOUNT - 1))
2231         local ost_step=1
2232         local ost_list=$(seq $first_ost $ost_step $last_ost)
2233         local ost_range="$first_ost $last_ost $ost_step"
2234
2235         if ! combined_mgs_mds ; then
2236                 mount_mgs_client
2237         fi
2238
2239         test_mkdir $DIR/$tdir
2240         pool_add $POOL || error "pool_add failed"
2241         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2242
2243         local skip27D
2244         [ $MDS1_VERSION -lt $(version_code 2.8.55) ] &&
2245                 skip27D+="-s 29"
2246         [ $MDS1_VERSION -lt $(version_code 2.9.55) -o \
2247           $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2248                 skip27D+=" -s 30,31"
2249         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2250                 error "llapi_layout_test failed"
2251
2252         destroy_test_pools || error "destroy test pools failed"
2253
2254         if ! combined_mgs_mds ; then
2255                 umount_mgs_client
2256         fi
2257 }
2258 run_test 27D "validate llapi_layout API"
2259
2260 # Verify that default_easize is increased from its initial value after
2261 # accessing a widely striped file.
2262 test_27E() {
2263         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2264         [ $CLIENT_VERSION -lt $(version_code 2.5.57) ] &&
2265                 skip "client does not have LU-3338 fix"
2266
2267         # 72 bytes is the minimum space required to store striping
2268         # information for a file striped across one OST:
2269         # (sizeof(struct lov_user_md_v3) +
2270         #  sizeof(struct lov_user_ost_data_v1))
2271         local min_easize=72
2272         $LCTL set_param -n llite.*.default_easize $min_easize ||
2273                 error "lctl set_param failed"
2274         local easize=$($LCTL get_param -n llite.*.default_easize)
2275
2276         [ $easize -eq $min_easize ] ||
2277                 error "failed to set default_easize"
2278
2279         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2280                 error "setstripe failed"
2281         cat $DIR/$tfile
2282         rm $DIR/$tfile
2283
2284         easize=$($LCTL get_param -n llite.*.default_easize)
2285
2286         [ $easize -gt $min_easize ] ||
2287                 error "default_easize not updated"
2288 }
2289 run_test 27E "check that default extended attribute size properly increases"
2290
2291 test_27F() { # LU-5346/LU-7975
2292         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2293         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2294         [[ $MDS1_VERSION -lt $(version_code 2.8.51) ]] &&
2295                 skip "Need MDS version at least 2.8.51"
2296         remote_ost_nodsh && skip "remote OST with nodsh"
2297
2298         test_mkdir $DIR/$tdir
2299         rm -f $DIR/$tdir/f0
2300         $SETSTRIPE -c 2 $DIR/$tdir
2301
2302         # stop all OSTs to reproduce situation for LU-7975 ticket
2303         for num in $(seq $OSTCOUNT); do
2304                 stop ost$num
2305         done
2306
2307         # open/create f0 with O_LOV_DELAY_CREATE
2308         # truncate f0 to a non-0 size
2309         # close
2310         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2311
2312         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2313         # open/write it again to force delayed layout creation
2314         cat /etc/hosts > $DIR/$tdir/f0 &
2315         catpid=$!
2316
2317         # restart OSTs
2318         for num in $(seq $OSTCOUNT); do
2319                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2320                         error "ost$num failed to start"
2321         done
2322
2323         wait $catpid || error "cat failed"
2324
2325         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2326         [[ $($GETSTRIPE -c $DIR/$tdir/f0) == 2 ]] || error "wrong stripecount"
2327
2328 }
2329 run_test 27F "Client resend delayed layout creation with non-zero size"
2330
2331 test_27G() { #LU-10629
2332         [ $MDS1_VERSION -lt $(version_code 2.11.51) ] &&
2333                 skip "Need MDS version at least 2.11.51"
2334         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2335         remote_mds_nodsh && skip "remote MDS with nodsh"
2336         local POOL=${POOL:-testpool}
2337         local ostrange="0 0 1"
2338
2339         test_mkdir $DIR/$tdir
2340         pool_add $POOL || error "pool_add failed"
2341         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2342         $LFS setstripe -p $POOL $DIR/$tdir
2343
2344         local pool=$($LFS getstripe -p $DIR/$tdir)
2345
2346         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2347
2348         $LFS setstripe -d $DIR/$tdir
2349
2350         pool=$($LFS getstripe -p $DIR/$tdir)
2351
2352         rmdir $DIR/$tdir
2353
2354         [ -z "$pool" ] || error "'$pool' is not empty"
2355 }
2356 run_test 27G "Clear OST pool from stripe"
2357
2358 test_27H() {
2359         [[ $MDS1_VERSION -le $(version_code 2.11.54) ]] &&
2360                 skip "Need MDS version newer than 2.11.54"
2361         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2362         test_mkdir $DIR/$tdir
2363         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2364         touch $DIR/$tdir/$tfile
2365         $LFS getstripe -c $DIR/$tdir/$tfile
2366         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2367                 error "two-stripe file doesn't have two stripes"
2368
2369         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2370         $LFS getstripe -y $DIR/$tdir/$tfile
2371         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2372              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2373                 error "expected l_ost_idx: [02]$ not matched"
2374
2375         # make sure ost list have been cleared
2376         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
2377         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2378                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2379         touch $DIR/$tdir/f3
2380         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2381 }
2382 run_test 27H "Set specific OSTs stripe"
2383
2384 # createtest also checks that device nodes are created and
2385 # then visible correctly (#2091)
2386 test_28() { # bug 2091
2387         test_mkdir $DIR/d28
2388         $CREATETEST $DIR/d28/ct || error "createtest failed"
2389 }
2390 run_test 28 "create/mknod/mkdir with bad file types ============"
2391
2392 test_29() {
2393         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2394
2395         sync; sleep 1; sync # flush out any dirty pages from previous tests
2396         cancel_lru_locks
2397         test_mkdir $DIR/d29
2398         touch $DIR/d29/foo
2399         log 'first d29'
2400         ls -l $DIR/d29
2401
2402         declare -i LOCKCOUNTORIG=0
2403         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2404                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2405         done
2406         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2407
2408         declare -i LOCKUNUSEDCOUNTORIG=0
2409         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2410                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2411         done
2412
2413         log 'second d29'
2414         ls -l $DIR/d29
2415         log 'done'
2416
2417         declare -i LOCKCOUNTCURRENT=0
2418         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2419                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2420         done
2421
2422         declare -i LOCKUNUSEDCOUNTCURRENT=0
2423         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2424                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2425         done
2426
2427         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2428                 $LCTL set_param -n ldlm.dump_namespaces ""
2429                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2430                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2431                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2432                 return 2
2433         fi
2434         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2435                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2436                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2437                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2438                 return 3
2439         fi
2440 }
2441 run_test 29 "IT_GETATTR regression  ============================"
2442
2443 test_30a() { # was test_30
2444         cp $(which ls) $DIR || cp /bin/ls $DIR
2445         $DIR/ls / || error "Can't execute binary from lustre"
2446         rm $DIR/ls
2447 }
2448 run_test 30a "execute binary from Lustre (execve) =============="
2449
2450 test_30b() {
2451         cp `which ls` $DIR || cp /bin/ls $DIR
2452         chmod go+rx $DIR/ls
2453         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
2454         rm $DIR/ls
2455 }
2456 run_test 30b "execute binary from Lustre as non-root ==========="
2457
2458 test_30c() { # b=22376
2459         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2460
2461         cp `which ls` $DIR || cp /bin/ls $DIR
2462         chmod a-rw $DIR/ls
2463         cancel_lru_locks mdc
2464         cancel_lru_locks osc
2465         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
2466         rm -f $DIR/ls
2467 }
2468 run_test 30c "execute binary from Lustre without read perms ===="
2469
2470 test_31a() {
2471         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
2472         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
2473 }
2474 run_test 31a "open-unlink file =================================="
2475
2476 test_31b() {
2477         touch $DIR/f31 || error "touch $DIR/f31 failed"
2478         ln $DIR/f31 $DIR/f31b || error "ln failed"
2479         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
2480         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
2481 }
2482 run_test 31b "unlink file with multiple links while open ======="
2483
2484 test_31c() {
2485         touch $DIR/f31 || error "touch $DIR/f31 failed"
2486         ln $DIR/f31 $DIR/f31c || error "ln failed"
2487         multiop_bg_pause $DIR/f31 O_uc ||
2488                 error "multiop_bg_pause for $DIR/f31 failed"
2489         MULTIPID=$!
2490         $MULTIOP $DIR/f31c Ouc
2491         kill -USR1 $MULTIPID
2492         wait $MULTIPID
2493 }
2494 run_test 31c "open-unlink file with multiple links ============="
2495
2496 test_31d() {
2497         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
2498         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
2499 }
2500 run_test 31d "remove of open directory ========================="
2501
2502 test_31e() { # bug 2904
2503         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
2504 }
2505 run_test 31e "remove of open non-empty directory ==============="
2506
2507 test_31f() { # bug 4554
2508         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2509
2510         set -vx
2511         test_mkdir $DIR/d31f
2512         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2513         cp /etc/hosts $DIR/d31f
2514         ls -l $DIR/d31f
2515         $GETSTRIPE $DIR/d31f/hosts
2516         multiop_bg_pause $DIR/d31f D_c || return 1
2517         MULTIPID=$!
2518
2519         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2520         test_mkdir $DIR/d31f
2521         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2522         cp /etc/hosts $DIR/d31f
2523         ls -l $DIR/d31f
2524         $GETSTRIPE $DIR/d31f/hosts
2525         multiop_bg_pause $DIR/d31f D_c || return 1
2526         MULTIPID2=$!
2527
2528         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2529         wait $MULTIPID || error "first opendir $MULTIPID failed"
2530
2531         sleep 6
2532
2533         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2534         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2535         set +vx
2536 }
2537 run_test 31f "remove of open directory with open-unlink file ==="
2538
2539 test_31g() {
2540         echo "-- cross directory link --"
2541         test_mkdir -c1 $DIR/${tdir}ga
2542         test_mkdir -c1 $DIR/${tdir}gb
2543         touch $DIR/${tdir}ga/f
2544         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2545         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2546         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2547         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2548         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2549 }
2550 run_test 31g "cross directory link==============="
2551
2552 test_31h() {
2553         echo "-- cross directory link --"
2554         test_mkdir -c1 $DIR/${tdir}
2555         test_mkdir -c1 $DIR/${tdir}/dir
2556         touch $DIR/${tdir}/f
2557         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2558         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2559         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2560         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2561         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2562 }
2563 run_test 31h "cross directory link under child==============="
2564
2565 test_31i() {
2566         echo "-- cross directory link --"
2567         test_mkdir -c1 $DIR/$tdir
2568         test_mkdir -c1 $DIR/$tdir/dir
2569         touch $DIR/$tdir/dir/f
2570         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2571         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2572         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2573         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2574         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2575 }
2576 run_test 31i "cross directory link under parent==============="
2577
2578 test_31j() {
2579         test_mkdir -c1 -p $DIR/$tdir
2580         test_mkdir -c1 -p $DIR/$tdir/dir1
2581         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2582         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2583         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2584         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2585         return 0
2586 }
2587 run_test 31j "link for directory==============="
2588
2589 test_31k() {
2590         test_mkdir -c1 -p $DIR/$tdir
2591         touch $DIR/$tdir/s
2592         touch $DIR/$tdir/exist
2593         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2594         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2595         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2596         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2597         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2598         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2599         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2600         return 0
2601 }
2602 run_test 31k "link to file: the same, non-existing, dir==============="
2603
2604 test_31m() {
2605         mkdir $DIR/d31m
2606         touch $DIR/d31m/s
2607         mkdir $DIR/d31m2
2608         touch $DIR/d31m2/exist
2609         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2610         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2611         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2612         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2613         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2614         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2615         return 0
2616 }
2617 run_test 31m "link to file: the same, non-existing, dir==============="
2618
2619 test_31n() {
2620         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2621         nlink=$(stat --format=%h $DIR/$tfile)
2622         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2623         local fd=$(free_fd)
2624         local cmd="exec $fd<$DIR/$tfile"
2625         eval $cmd
2626         cmd="exec $fd<&-"
2627         trap "eval $cmd" EXIT
2628         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2629         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2630         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2631         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2632         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2633         eval $cmd
2634 }
2635 run_test 31n "check link count of unlinked file"
2636
2637 link_one() {
2638         local TEMPNAME=$(mktemp $1_XXXXXX)
2639         mlink $TEMPNAME $1 2> /dev/null &&
2640                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2641         munlink $TEMPNAME
2642 }
2643
2644 test_31o() { # LU-2901
2645         test_mkdir $DIR/$tdir
2646         for LOOP in $(seq 100); do
2647                 rm -f $DIR/$tdir/$tfile*
2648                 for THREAD in $(seq 8); do
2649                         link_one $DIR/$tdir/$tfile.$LOOP &
2650                 done
2651                 wait
2652                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2653                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2654                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2655                         break || true
2656         done
2657 }
2658 run_test 31o "duplicate hard links with same filename"
2659
2660 test_31p() {
2661         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
2662
2663         test_mkdir $DIR/$tdir
2664         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2665         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
2666
2667         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2668                 error "open unlink test1 failed"
2669         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2670                 error "open unlink test2 failed"
2671
2672         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2673                 error "test1 still exists"
2674         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2675                 error "test2 still exists"
2676 }
2677 run_test 31p "remove of open striped directory"
2678
2679 cleanup_test32_mount() {
2680         local rc=0
2681         trap 0
2682         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2683         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2684         losetup -d $loopdev || true
2685         rm -rf $DIR/$tdir
2686         return $rc
2687 }
2688
2689 test_32a() {
2690         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2691
2692         echo "== more mountpoints and symlinks ================="
2693         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2694         trap cleanup_test32_mount EXIT
2695         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2696         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2697                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2698         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
2699                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
2700         cleanup_test32_mount
2701 }
2702 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2703
2704 test_32b() {
2705         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2706
2707         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2708         trap cleanup_test32_mount EXIT
2709         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2710         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2711                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2712         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
2713                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
2714         cleanup_test32_mount
2715 }
2716 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2717
2718 test_32c() {
2719         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2720
2721         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2722         trap cleanup_test32_mount EXIT
2723         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2724         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2725                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2726         test_mkdir -p $DIR/$tdir/d2/test_dir
2727         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2728                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
2729         cleanup_test32_mount
2730 }
2731 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2732
2733 test_32d() {
2734         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2735
2736         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2737         trap cleanup_test32_mount EXIT
2738         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2739         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2740                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2741         test_mkdir -p $DIR/$tdir/d2/test_dir
2742         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2743                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
2744         cleanup_test32_mount
2745 }
2746 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
2747
2748 test_32e() {
2749         rm -fr $DIR/$tdir
2750         test_mkdir -p $DIR/$tdir/tmp
2751         local tmp_dir=$DIR/$tdir/tmp
2752         ln -s $DIR/$tdir $tmp_dir/symlink11
2753         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2754         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
2755         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
2756 }
2757 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
2758
2759 test_32f() {
2760         rm -fr $DIR/$tdir
2761         test_mkdir -p $DIR/$tdir/tmp
2762         local tmp_dir=$DIR/$tdir/tmp
2763         ln -s $DIR/$tdir $tmp_dir/symlink11
2764         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2765         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
2766         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
2767 }
2768 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
2769
2770 test_32g() {
2771         local tmp_dir=$DIR/$tdir/tmp
2772         test_mkdir -p $tmp_dir
2773         test_mkdir $DIR/${tdir}2
2774         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2775         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2776         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
2777         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
2778         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
2779         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
2780 }
2781 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2782
2783 test_32h() {
2784         rm -fr $DIR/$tdir $DIR/${tdir}2
2785         tmp_dir=$DIR/$tdir/tmp
2786         test_mkdir -p $tmp_dir
2787         test_mkdir $DIR/${tdir}2
2788         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2789         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2790         ls $tmp_dir/symlink12 || error "listing symlink12"
2791         ls $DIR/$tdir/symlink02  || error "listing symlink02"
2792 }
2793 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2794
2795 test_32i() {
2796         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2797
2798         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2799         trap cleanup_test32_mount EXIT
2800         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2801         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2802                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2803         touch $DIR/$tdir/test_file
2804         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
2805                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
2806         cleanup_test32_mount
2807 }
2808 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2809
2810 test_32j() {
2811         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2812
2813         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2814         trap cleanup_test32_mount EXIT
2815         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2816         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2817                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2818         touch $DIR/$tdir/test_file
2819         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
2820                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
2821         cleanup_test32_mount
2822 }
2823 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2824
2825 test_32k() {
2826         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2827
2828         rm -fr $DIR/$tdir
2829         trap cleanup_test32_mount EXIT
2830         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2831         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2832                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2833         test_mkdir -p $DIR/$tdir/d2
2834         touch $DIR/$tdir/d2/test_file || error "touch failed"
2835         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
2836                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
2837         cleanup_test32_mount
2838 }
2839 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2840
2841 test_32l() {
2842         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2843
2844         rm -fr $DIR/$tdir
2845         trap cleanup_test32_mount EXIT
2846         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2847         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2848                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2849         test_mkdir -p $DIR/$tdir/d2
2850         touch $DIR/$tdir/d2/test_file || error "touch failed"
2851         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
2852                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
2853         cleanup_test32_mount
2854 }
2855 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2856
2857 test_32m() {
2858         rm -fr $DIR/d32m
2859         test_mkdir -p $DIR/d32m/tmp
2860         TMP_DIR=$DIR/d32m/tmp
2861         ln -s $DIR $TMP_DIR/symlink11
2862         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2863         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
2864                 error "symlink11 not a link"
2865         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
2866                 error "symlink01 not a link"
2867 }
2868 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2869
2870 test_32n() {
2871         rm -fr $DIR/d32n
2872         test_mkdir -p $DIR/d32n/tmp
2873         TMP_DIR=$DIR/d32n/tmp
2874         ln -s $DIR $TMP_DIR/symlink11
2875         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2876         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
2877         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
2878 }
2879 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2880
2881 test_32o() {
2882         touch $DIR/$tfile
2883         test_mkdir -p $DIR/d32o/tmp
2884         TMP_DIR=$DIR/d32o/tmp
2885         ln -s $DIR/$tfile $TMP_DIR/symlink12
2886         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2887         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
2888                 error "symlink12 not a link"
2889         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
2890         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
2891                 error "$DIR/d32o/tmp/symlink12 not file type"
2892         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
2893                 error "$DIR/d32o/symlink02 not file type"
2894 }
2895 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2896
2897 test_32p() {
2898         log 32p_1
2899         rm -fr $DIR/d32p
2900         log 32p_2
2901         rm -f $DIR/$tfile
2902         log 32p_3
2903         touch $DIR/$tfile
2904         log 32p_4
2905         test_mkdir -p $DIR/d32p/tmp
2906         log 32p_5
2907         TMP_DIR=$DIR/d32p/tmp
2908         log 32p_6
2909         ln -s $DIR/$tfile $TMP_DIR/symlink12
2910         log 32p_7
2911         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2912         log 32p_8
2913         cat $DIR/d32p/tmp/symlink12 ||
2914                 error "Can't open $DIR/d32p/tmp/symlink12"
2915         log 32p_9
2916         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
2917         log 32p_10
2918 }
2919 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2920
2921 test_32q() {
2922         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2923
2924         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2925         trap cleanup_test32_mount EXIT
2926         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2927         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
2928         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2929                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2930         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
2931         cleanup_test32_mount
2932 }
2933 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2934
2935 test_32r() {
2936         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2937
2938         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2939         trap cleanup_test32_mount EXIT
2940         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2941         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
2942         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2943                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2944         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
2945         cleanup_test32_mount
2946 }
2947 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2948
2949 test_33aa() {
2950         rm -f $DIR/$tfile
2951         touch $DIR/$tfile
2952         chmod 444 $DIR/$tfile
2953         chown $RUNAS_ID $DIR/$tfile
2954         log 33_1
2955         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2956         log 33_2
2957 }
2958 run_test 33aa "write file with mode 444 (should return error)"
2959
2960 test_33a() {
2961         rm -fr $DIR/$tdir
2962         test_mkdir $DIR/$tdir
2963         chown $RUNAS_ID $DIR/$tdir
2964         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
2965                 error "$RUNAS create $tdir/$tfile failed"
2966         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
2967                 error "open RDWR" || true
2968 }
2969 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2970
2971 test_33b() {
2972         rm -fr $DIR/$tdir
2973         test_mkdir $DIR/$tdir
2974         chown $RUNAS_ID $DIR/$tdir
2975         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
2976 }
2977 run_test 33b "test open file with malformed flags (No panic)"
2978
2979 test_33c() {
2980         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2981         remote_ost_nodsh && skip "remote OST with nodsh"
2982
2983         local ostnum
2984         local ostname
2985         local write_bytes
2986         local all_zeros
2987
2988         all_zeros=:
2989         rm -fr $DIR/$tdir
2990         test_mkdir $DIR/$tdir
2991         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2992
2993         sync
2994         for ostnum in $(seq $OSTCOUNT); do
2995                 # test-framework's OST numbering is one-based, while Lustre's
2996                 # is zero-based
2997                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2998                 # Parsing llobdstat's output sucks; we could grep the /proc
2999                 # path, but that's likely to not be as portable as using the
3000                 # llobdstat utility.  So we parse lctl output instead.
3001                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3002                         obdfilter/$ostname/stats |
3003                         awk '/^write_bytes/ {print $7}' )
3004                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
3005                 if (( ${write_bytes:-0} > 0 ))
3006                 then
3007                         all_zeros=false
3008                         break;
3009                 fi
3010         done
3011
3012         $all_zeros || return 0
3013
3014         # Write four bytes
3015         echo foo > $DIR/$tdir/bar
3016         # Really write them
3017         sync
3018
3019         # Total up write_bytes after writing.  We'd better find non-zeros.
3020         for ostnum in $(seq $OSTCOUNT); do
3021                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3022                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3023                         obdfilter/$ostname/stats |
3024                         awk '/^write_bytes/ {print $7}' )
3025                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
3026                 if (( ${write_bytes:-0} > 0 ))
3027                 then
3028                         all_zeros=false
3029                         break;
3030                 fi
3031         done
3032
3033         if $all_zeros
3034         then
3035                 for ostnum in $(seq $OSTCOUNT); do
3036                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3037                         echo "Check that write_bytes is present in obdfilter/*/stats:"
3038                         do_facet ost$ostnum lctl get_param -n \
3039                                 obdfilter/$ostname/stats
3040                 done
3041                 error "OST not keeping write_bytes stats (b22312)"
3042         fi
3043 }
3044 run_test 33c "test llobdstat and write_bytes"
3045
3046 test_33d() {
3047         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3048         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3049
3050         local MDTIDX=1
3051         local remote_dir=$DIR/$tdir/remote_dir
3052
3053         test_mkdir $DIR/$tdir
3054         $LFS mkdir -i $MDTIDX $remote_dir ||
3055                 error "create remote directory failed"
3056
3057         touch $remote_dir/$tfile
3058         chmod 444 $remote_dir/$tfile
3059         chown $RUNAS_ID $remote_dir/$tfile
3060
3061         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3062
3063         chown $RUNAS_ID $remote_dir
3064         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
3065                                         error "create" || true
3066         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
3067                                     error "open RDWR" || true
3068         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
3069 }
3070 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
3071
3072 test_33e() {
3073         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3074
3075         mkdir $DIR/$tdir
3076
3077         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3078         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3079         mkdir $DIR/$tdir/local_dir
3080
3081         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3082         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3083         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3084
3085         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3086                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
3087
3088         rmdir $DIR/$tdir/* || error "rmdir failed"
3089
3090         umask 777
3091         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3092         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3093         mkdir $DIR/$tdir/local_dir
3094
3095         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3096         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3097         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3098
3099         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3100                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3101
3102         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3103
3104         umask 000
3105         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3106         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3107         mkdir $DIR/$tdir/local_dir
3108
3109         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3110         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3111         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3112
3113         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3114                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3115 }
3116 run_test 33e "mkdir and striped directory should have same mode"
3117
3118 cleanup_33f() {
3119         trap 0
3120         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3121 }
3122
3123 test_33f() {
3124         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3125         remote_mds_nodsh && skip "remote MDS with nodsh"
3126
3127         mkdir $DIR/$tdir
3128         chmod go+rwx $DIR/$tdir
3129         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3130         trap cleanup_33f EXIT
3131
3132         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3133                 error "cannot create striped directory"
3134
3135         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3136                 error "cannot create files in striped directory"
3137
3138         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3139                 error "cannot remove files in striped directory"
3140
3141         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3142                 error "cannot remove striped directory"
3143
3144         cleanup_33f
3145 }
3146 run_test 33f "nonroot user can create, access, and remove a striped directory"
3147
3148 test_33g() {
3149         mkdir -p $DIR/$tdir/dir2
3150
3151         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3152         echo $err
3153         [[ $err =~ "exists" ]] || error "Not exists error"
3154 }
3155 run_test 33g "nonroot user create already existing root created file"
3156
3157 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3158 test_34a() {
3159         rm -f $DIR/f34
3160         $MCREATE $DIR/f34 || error "mcreate failed"
3161         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3162                 error "getstripe failed"
3163         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3164         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3165                 error "getstripe failed"
3166         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3167                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3168 }
3169 run_test 34a "truncate file that has not been opened ==========="
3170
3171 test_34b() {
3172         [ ! -f $DIR/f34 ] && test_34a
3173         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3174                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3175         $OPENFILE -f O_RDONLY $DIR/f34
3176         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3177                 error "getstripe failed"
3178         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3179                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3180 }
3181 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3182
3183 test_34c() {
3184         [ ! -f $DIR/f34 ] && test_34a
3185         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3186                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3187         $OPENFILE -f O_RDWR $DIR/f34
3188         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
3189         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3190                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3191 }
3192 run_test 34c "O_RDWR opening file-with-size works =============="
3193
3194 test_34d() {
3195         [ ! -f $DIR/f34 ] && test_34a
3196         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3197                 error "dd failed"
3198         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3199                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3200         rm $DIR/f34
3201 }
3202 run_test 34d "write to sparse file ============================="
3203
3204 test_34e() {
3205         rm -f $DIR/f34e
3206         $MCREATE $DIR/f34e || error "mcreate failed"
3207         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3208         $CHECKSTAT -s 1000 $DIR/f34e ||
3209                 error "Size of $DIR/f34e not equal to 1000 bytes"
3210         $OPENFILE -f O_RDWR $DIR/f34e
3211         $CHECKSTAT -s 1000 $DIR/f34e ||
3212                 error "Size of $DIR/f34e not equal to 1000 bytes"
3213 }
3214 run_test 34e "create objects, some with size and some without =="
3215
3216 test_34f() { # bug 6242, 6243
3217         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3218
3219         SIZE34F=48000
3220         rm -f $DIR/f34f
3221         $MCREATE $DIR/f34f || error "mcreate failed"
3222         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3223         dd if=$DIR/f34f of=$TMP/f34f
3224         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3225         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3226         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3227         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3228         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3229 }
3230 run_test 34f "read from a file with no objects until EOF ======="
3231
3232 test_34g() {
3233         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3234
3235         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3236                 error "dd failed"
3237         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3238         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3239                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3240         cancel_lru_locks osc
3241         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3242                 error "wrong size after lock cancel"
3243
3244         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3245         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3246                 error "expanding truncate failed"
3247         cancel_lru_locks osc
3248         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3249                 error "wrong expanded size after lock cancel"
3250 }
3251 run_test 34g "truncate long file ==============================="
3252
3253 test_34h() {
3254         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3255
3256         local gid=10
3257         local sz=1000
3258
3259         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3260         sync # Flush the cache so that multiop below does not block on cache
3261              # flush when getting the group lock
3262         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3263         MULTIPID=$!
3264
3265         # Since just timed wait is not good enough, let's do a sync write
3266         # that way we are sure enough time for a roundtrip + processing
3267         # passed + 2 seconds of extra margin.
3268         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
3269         rm $DIR/${tfile}-1
3270         sleep 2
3271
3272         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3273                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3274                 kill -9 $MULTIPID
3275         fi
3276         wait $MULTIPID
3277         local nsz=`stat -c %s $DIR/$tfile`
3278         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3279 }
3280 run_test 34h "ftruncate file under grouplock should not block"
3281
3282 test_35a() {
3283         cp /bin/sh $DIR/f35a
3284         chmod 444 $DIR/f35a
3285         chown $RUNAS_ID $DIR/f35a
3286         $RUNAS $DIR/f35a && error || true
3287         rm $DIR/f35a
3288 }
3289 run_test 35a "exec file with mode 444 (should return and not leak)"
3290
3291 test_36a() {
3292         rm -f $DIR/f36
3293         utime $DIR/f36 || error "utime failed for MDS"
3294 }
3295 run_test 36a "MDS utime check (mknod, utime)"
3296
3297 test_36b() {
3298         echo "" > $DIR/f36
3299         utime $DIR/f36 || error "utime failed for OST"
3300 }
3301 run_test 36b "OST utime check (open, utime)"
3302
3303 test_36c() {
3304         rm -f $DIR/d36/f36
3305         test_mkdir $DIR/d36
3306         chown $RUNAS_ID $DIR/d36
3307         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3308 }
3309 run_test 36c "non-root MDS utime check (mknod, utime)"
3310
3311 test_36d() {
3312         [ ! -d $DIR/d36 ] && test_36c
3313         echo "" > $DIR/d36/f36
3314         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3315 }
3316 run_test 36d "non-root OST utime check (open, utime)"
3317
3318 test_36e() {
3319         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3320
3321         test_mkdir $DIR/$tdir
3322         touch $DIR/$tdir/$tfile
3323         $RUNAS utime $DIR/$tdir/$tfile &&
3324                 error "utime worked, expected failure" || true
3325 }
3326 run_test 36e "utime on non-owned file (should return error)"
3327
3328 subr_36fh() {
3329         local fl="$1"
3330         local LANG_SAVE=$LANG
3331         local LC_LANG_SAVE=$LC_LANG
3332         export LANG=C LC_LANG=C # for date language
3333
3334         DATESTR="Dec 20  2000"
3335         test_mkdir $DIR/$tdir
3336         lctl set_param fail_loc=$fl
3337         date; date +%s
3338         cp /etc/hosts $DIR/$tdir/$tfile
3339         sync & # write RPC generated with "current" inode timestamp, but delayed
3340         sleep 1
3341         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3342         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3343         cancel_lru_locks $OSC
3344         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3345         date; date +%s
3346         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3347                 echo "BEFORE: $LS_BEFORE" && \
3348                 echo "AFTER : $LS_AFTER" && \
3349                 echo "WANT  : $DATESTR" && \
3350                 error "$DIR/$tdir/$tfile timestamps changed" || true
3351
3352         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3353 }
3354
3355 test_36f() {
3356         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3357
3358         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3359         subr_36fh "0x80000214"
3360 }
3361 run_test 36f "utime on file racing with OST BRW write =========="
3362
3363 test_36g() {
3364         remote_ost_nodsh && skip "remote OST with nodsh"
3365         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3366         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
3367                 skip "Need MDS version at least 2.12.51"
3368
3369         local fmd_max_age
3370         local fmd
3371         local facet="ost1"
3372         local tgt="obdfilter"
3373
3374         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
3375
3376         test_mkdir $DIR/$tdir
3377         fmd_max_age=$(do_facet $facet \
3378                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
3379                 head -n 1")
3380
3381         echo "FMD max age: ${fmd_max_age}s"
3382         touch $DIR/$tdir/$tfile
3383         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3384                 gawk '{cnt=cnt+$1}  END{print cnt}')
3385         echo "FMD before: $fmd"
3386         [[ $fmd == 0 ]] &&
3387                 error "FMD wasn't create by touch"
3388         sleep $((fmd_max_age + 12))
3389         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3390                 gawk '{cnt=cnt+$1}  END{print cnt}')
3391         echo "FMD after: $fmd"
3392         [[ $fmd == 0 ]] ||
3393                 error "FMD wasn't expired by ping"
3394 }
3395 run_test 36g "FMD cache expiry ====================="
3396
3397 test_36h() {
3398         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3399
3400         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3401         subr_36fh "0x80000227"
3402 }
3403 run_test 36h "utime on file racing with OST BRW write =========="
3404
3405 test_36i() {
3406         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3407
3408         test_mkdir $DIR/$tdir
3409         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3410
3411         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3412         local new_mtime=$((mtime + 200))
3413
3414         #change Modify time of striped dir
3415         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3416                         error "change mtime failed"
3417
3418         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3419
3420         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3421 }
3422 run_test 36i "change mtime on striped directory"
3423
3424 # test_37 - duplicate with tests 32q 32r
3425
3426 test_38() {
3427         local file=$DIR/$tfile
3428         touch $file
3429         openfile -f O_DIRECTORY $file
3430         local RC=$?
3431         local ENOTDIR=20
3432         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3433         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3434 }
3435 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3436
3437 test_39a() { # was test_39
3438         touch $DIR/$tfile
3439         touch $DIR/${tfile}2
3440 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3441 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3442 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3443         sleep 2
3444         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3445         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3446                 echo "mtime"
3447                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3448                 echo "atime"
3449                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3450                 echo "ctime"
3451                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3452                 error "O_TRUNC didn't change timestamps"
3453         fi
3454 }
3455 run_test 39a "mtime changed on create"
3456
3457 test_39b() {
3458         test_mkdir -c1 $DIR/$tdir
3459         cp -p /etc/passwd $DIR/$tdir/fopen
3460         cp -p /etc/passwd $DIR/$tdir/flink
3461         cp -p /etc/passwd $DIR/$tdir/funlink
3462         cp -p /etc/passwd $DIR/$tdir/frename
3463         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3464
3465         sleep 1
3466         echo "aaaaaa" >> $DIR/$tdir/fopen
3467         echo "aaaaaa" >> $DIR/$tdir/flink
3468         echo "aaaaaa" >> $DIR/$tdir/funlink
3469         echo "aaaaaa" >> $DIR/$tdir/frename
3470
3471         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3472         local link_new=`stat -c %Y $DIR/$tdir/flink`
3473         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3474         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3475
3476         cat $DIR/$tdir/fopen > /dev/null
3477         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3478         rm -f $DIR/$tdir/funlink2
3479         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3480
3481         for (( i=0; i < 2; i++ )) ; do
3482                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3483                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3484                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3485                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3486
3487                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3488                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3489                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3490                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3491
3492                 cancel_lru_locks $OSC
3493                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3494         done
3495 }
3496 run_test 39b "mtime change on open, link, unlink, rename  ======"
3497
3498 # this should be set to past
3499 TEST_39_MTIME=`date -d "1 year ago" +%s`
3500
3501 # bug 11063
3502 test_39c() {
3503         touch $DIR1/$tfile
3504         sleep 2
3505         local mtime0=`stat -c %Y $DIR1/$tfile`
3506
3507         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3508         local mtime1=`stat -c %Y $DIR1/$tfile`
3509         [ "$mtime1" = $TEST_39_MTIME ] || \
3510                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3511
3512         local d1=`date +%s`
3513         echo hello >> $DIR1/$tfile
3514         local d2=`date +%s`
3515         local mtime2=`stat -c %Y $DIR1/$tfile`
3516         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3517                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3518
3519         mv $DIR1/$tfile $DIR1/$tfile-1
3520
3521         for (( i=0; i < 2; i++ )) ; do
3522                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3523                 [ "$mtime2" = "$mtime3" ] || \
3524                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3525
3526                 cancel_lru_locks $OSC
3527                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3528         done
3529 }
3530 run_test 39c "mtime change on rename ==========================="
3531
3532 # bug 21114
3533 test_39d() {
3534         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3535
3536         touch $DIR1/$tfile
3537         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3538
3539         for (( i=0; i < 2; i++ )) ; do
3540                 local mtime=`stat -c %Y $DIR1/$tfile`
3541                 [ $mtime = $TEST_39_MTIME ] || \
3542                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3543
3544                 cancel_lru_locks $OSC
3545                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3546         done
3547 }
3548 run_test 39d "create, utime, stat =============================="
3549
3550 # bug 21114
3551 test_39e() {
3552         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3553
3554         touch $DIR1/$tfile
3555         local mtime1=`stat -c %Y $DIR1/$tfile`
3556
3557         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3558
3559         for (( i=0; i < 2; i++ )) ; do
3560                 local mtime2=`stat -c %Y $DIR1/$tfile`
3561                 [ $mtime2 = $TEST_39_MTIME ] || \
3562                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3563
3564                 cancel_lru_locks $OSC
3565                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3566         done
3567 }
3568 run_test 39e "create, stat, utime, stat ========================"
3569
3570 # bug 21114
3571 test_39f() {
3572         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3573
3574         touch $DIR1/$tfile
3575         mtime1=`stat -c %Y $DIR1/$tfile`
3576
3577         sleep 2
3578         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3579
3580         for (( i=0; i < 2; i++ )) ; do
3581                 local mtime2=`stat -c %Y $DIR1/$tfile`
3582                 [ $mtime2 = $TEST_39_MTIME ] || \
3583                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3584
3585                 cancel_lru_locks $OSC
3586                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3587         done
3588 }
3589 run_test 39f "create, stat, sleep, utime, stat ================="
3590
3591 # bug 11063
3592 test_39g() {
3593         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3594
3595         echo hello >> $DIR1/$tfile
3596         local mtime1=`stat -c %Y $DIR1/$tfile`
3597
3598         sleep 2
3599         chmod o+r $DIR1/$tfile
3600
3601         for (( i=0; i < 2; i++ )) ; do
3602                 local mtime2=`stat -c %Y $DIR1/$tfile`
3603                 [ "$mtime1" = "$mtime2" ] || \
3604                         error "lost mtime: $mtime2, should be $mtime1"
3605
3606                 cancel_lru_locks $OSC
3607                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3608         done
3609 }
3610 run_test 39g "write, chmod, stat ==============================="
3611
3612 # bug 11063
3613 test_39h() {
3614         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3615
3616         touch $DIR1/$tfile
3617         sleep 1
3618
3619         local d1=`date`
3620         echo hello >> $DIR1/$tfile
3621         local mtime1=`stat -c %Y $DIR1/$tfile`
3622
3623         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3624         local d2=`date`
3625         if [ "$d1" != "$d2" ]; then
3626                 echo "write and touch not within one second"
3627         else
3628                 for (( i=0; i < 2; i++ )) ; do
3629                         local mtime2=`stat -c %Y $DIR1/$tfile`
3630                         [ "$mtime2" = $TEST_39_MTIME ] || \
3631                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3632
3633                         cancel_lru_locks $OSC
3634                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3635                 done
3636         fi
3637 }
3638 run_test 39h "write, utime within one second, stat ============="
3639
3640 test_39i() {
3641         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3642
3643         touch $DIR1/$tfile
3644         sleep 1
3645
3646         echo hello >> $DIR1/$tfile
3647         local mtime1=`stat -c %Y $DIR1/$tfile`
3648
3649         mv $DIR1/$tfile $DIR1/$tfile-1
3650
3651         for (( i=0; i < 2; i++ )) ; do
3652                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3653
3654                 [ "$mtime1" = "$mtime2" ] || \
3655                         error "lost mtime: $mtime2, should be $mtime1"
3656
3657                 cancel_lru_locks $OSC
3658                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3659         done
3660 }
3661 run_test 39i "write, rename, stat =============================="
3662
3663 test_39j() {
3664         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3665
3666         start_full_debug_logging
3667         touch $DIR1/$tfile
3668         sleep 1
3669
3670         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3671         lctl set_param fail_loc=0x80000412
3672         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3673                 error "multiop failed"
3674         local multipid=$!
3675         local mtime1=`stat -c %Y $DIR1/$tfile`
3676
3677         mv $DIR1/$tfile $DIR1/$tfile-1
3678
3679         kill -USR1 $multipid
3680         wait $multipid || error "multiop close failed"
3681
3682         for (( i=0; i < 2; i++ )) ; do
3683                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3684                 [ "$mtime1" = "$mtime2" ] ||
3685                         error "mtime is lost on close: $mtime2, " \
3686                               "should be $mtime1"
3687
3688                 cancel_lru_locks $OSC
3689                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3690         done
3691         lctl set_param fail_loc=0
3692         stop_full_debug_logging
3693 }
3694 run_test 39j "write, rename, close, stat ======================="
3695
3696 test_39k() {
3697         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3698
3699         touch $DIR1/$tfile
3700         sleep 1
3701
3702         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3703         local multipid=$!
3704         local mtime1=`stat -c %Y $DIR1/$tfile`
3705
3706         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3707
3708         kill -USR1 $multipid
3709         wait $multipid || error "multiop close failed"
3710
3711         for (( i=0; i < 2; i++ )) ; do
3712                 local mtime2=`stat -c %Y $DIR1/$tfile`
3713
3714                 [ "$mtime2" = $TEST_39_MTIME ] || \
3715                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3716
3717                 cancel_lru_locks osc
3718                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3719         done
3720 }
3721 run_test 39k "write, utime, close, stat ========================"
3722
3723 # this should be set to future
3724 TEST_39_ATIME=`date -d "1 year" +%s`
3725
3726 test_39l() {
3727         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3728         remote_mds_nodsh && skip "remote MDS with nodsh"
3729
3730         local atime_diff=$(do_facet $SINGLEMDS \
3731                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3732         rm -rf $DIR/$tdir
3733         mkdir -p $DIR/$tdir
3734
3735         # test setting directory atime to future
3736         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3737         local atime=$(stat -c %X $DIR/$tdir)
3738         [ "$atime" = $TEST_39_ATIME ] ||
3739                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3740
3741         # test setting directory atime from future to now
3742         local now=$(date +%s)
3743         touch -a -d @$now $DIR/$tdir
3744
3745         atime=$(stat -c %X $DIR/$tdir)
3746         [ "$atime" -eq "$now"  ] ||
3747                 error "atime is not updated from future: $atime, $now"
3748
3749         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3750         sleep 3
3751
3752         # test setting directory atime when now > dir atime + atime_diff
3753         local d1=$(date +%s)
3754         ls $DIR/$tdir
3755         local d2=$(date +%s)
3756         cancel_lru_locks mdc
3757         atime=$(stat -c %X $DIR/$tdir)
3758         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3759                 error "atime is not updated  : $atime, should be $d2"
3760
3761         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3762         sleep 3
3763
3764         # test not setting directory atime when now < dir atime + atime_diff
3765         ls $DIR/$tdir
3766         cancel_lru_locks mdc
3767         atime=$(stat -c %X $DIR/$tdir)
3768         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3769                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3770
3771         do_facet $SINGLEMDS \
3772                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3773 }
3774 run_test 39l "directory atime update ==========================="
3775
3776 test_39m() {
3777         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3778
3779         touch $DIR1/$tfile
3780         sleep 2
3781         local far_past_mtime=$(date -d "May 29 1953" +%s)
3782         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3783
3784         touch -m -d @$far_past_mtime $DIR1/$tfile
3785         touch -a -d @$far_past_atime $DIR1/$tfile
3786
3787         for (( i=0; i < 2; i++ )) ; do
3788                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3789                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3790                         error "atime or mtime set incorrectly"
3791
3792                 cancel_lru_locks $OSC
3793                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3794         done
3795 }
3796 run_test 39m "test atime and mtime before 1970"
3797
3798 test_39n() { # LU-3832
3799         remote_mds_nodsh && skip "remote MDS with nodsh"
3800
3801         local atime_diff=$(do_facet $SINGLEMDS \
3802                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3803         local atime0
3804         local atime1
3805         local atime2
3806
3807         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3808
3809         rm -rf $DIR/$tfile
3810         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3811         atime0=$(stat -c %X $DIR/$tfile)
3812
3813         sleep 5
3814         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3815         atime1=$(stat -c %X $DIR/$tfile)
3816
3817         sleep 5
3818         cancel_lru_locks mdc
3819         cancel_lru_locks osc
3820         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3821         atime2=$(stat -c %X $DIR/$tfile)
3822
3823         do_facet $SINGLEMDS \
3824                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3825
3826         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3827         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3828 }
3829 run_test 39n "check that O_NOATIME is honored"
3830
3831 test_39o() {
3832         TESTDIR=$DIR/$tdir/$tfile
3833         [ -e $TESTDIR ] && rm -rf $TESTDIR
3834         mkdir -p $TESTDIR
3835         cd $TESTDIR
3836         links1=2
3837         ls
3838         mkdir a b
3839         ls
3840         links2=$(stat -c %h .)
3841         [ $(($links1 + 2)) != $links2 ] &&
3842                 error "wrong links count $(($links1 + 2)) != $links2"
3843         rmdir b
3844         links3=$(stat -c %h .)
3845         [ $(($links1 + 1)) != $links3 ] &&
3846                 error "wrong links count $links1 != $links3"
3847         return 0
3848 }
3849 run_test 39o "directory cached attributes updated after create"
3850
3851 test_39p() {
3852         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3853
3854         local MDTIDX=1
3855         TESTDIR=$DIR/$tdir/$tdir
3856         [ -e $TESTDIR ] && rm -rf $TESTDIR
3857         test_mkdir -p $TESTDIR
3858         cd $TESTDIR
3859         links1=2
3860         ls
3861         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
3862         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
3863         ls
3864         links2=$(stat -c %h .)
3865         [ $(($links1 + 2)) != $links2 ] &&
3866                 error "wrong links count $(($links1 + 2)) != $links2"
3867         rmdir remote_dir2
3868         links3=$(stat -c %h .)
3869         [ $(($links1 + 1)) != $links3 ] &&
3870                 error "wrong links count $links1 != $links3"
3871         return 0
3872 }
3873 run_test 39p "remote directory cached attributes updated after create ========"
3874
3875
3876 test_39q() { # LU-8041
3877         local testdir=$DIR/$tdir
3878         mkdir -p $testdir
3879         multiop_bg_pause $testdir D_c || error "multiop failed"
3880         local multipid=$!
3881         cancel_lru_locks mdc
3882         kill -USR1 $multipid
3883         local atime=$(stat -c %X $testdir)
3884         [ "$atime" -ne 0 ] || error "atime is zero"
3885 }
3886 run_test 39q "close won't zero out atime"
3887
3888 test_40() {
3889         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3890         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3891                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3892         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3893                 error "$tfile is not 4096 bytes in size"
3894 }
3895 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3896
3897 test_41() {
3898         # bug 1553
3899         small_write $DIR/f41 18
3900 }
3901 run_test 41 "test small file write + fstat ====================="
3902
3903 count_ost_writes() {
3904         lctl get_param -n ${OSC}.*.stats |
3905                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3906                         END { printf("%0.0f", writes) }'
3907 }
3908
3909 # decent default
3910 WRITEBACK_SAVE=500
3911 DIRTY_RATIO_SAVE=40
3912 MAX_DIRTY_RATIO=50
3913 BG_DIRTY_RATIO_SAVE=10
3914 MAX_BG_DIRTY_RATIO=25
3915
3916 start_writeback() {
3917         trap 0
3918         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3919         # dirty_ratio, dirty_background_ratio
3920         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3921                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3922                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3923                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3924         else
3925                 # if file not here, we are a 2.4 kernel
3926                 kill -CONT `pidof kupdated`
3927         fi
3928 }
3929
3930 stop_writeback() {
3931         # setup the trap first, so someone cannot exit the test at the
3932         # exact wrong time and mess up a machine
3933         trap start_writeback EXIT
3934         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3935         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3936                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3937                 sysctl -w vm.dirty_writeback_centisecs=0
3938                 sysctl -w vm.dirty_writeback_centisecs=0
3939                 # save and increase /proc/sys/vm/dirty_ratio
3940                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3941                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3942                 # save and increase /proc/sys/vm/dirty_background_ratio
3943                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3944                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3945         else
3946                 # if file not here, we are a 2.4 kernel
3947                 kill -STOP `pidof kupdated`
3948         fi
3949 }
3950
3951 # ensure that all stripes have some grant before we test client-side cache
3952 setup_test42() {
3953         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3954                 dd if=/dev/zero of=$i bs=4k count=1
3955                 rm $i
3956         done
3957 }
3958
3959 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3960 # file truncation, and file removal.
3961 test_42a() {
3962         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3963
3964         setup_test42
3965         cancel_lru_locks $OSC
3966         stop_writeback
3967         sync; sleep 1; sync # just to be safe
3968         BEFOREWRITES=`count_ost_writes`
3969         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3970         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3971         AFTERWRITES=`count_ost_writes`
3972         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3973                 error "$BEFOREWRITES < $AFTERWRITES"
3974         start_writeback
3975 }
3976 run_test 42a "ensure that we don't flush on close"
3977
3978 test_42b() {
3979         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3980
3981         setup_test42
3982         cancel_lru_locks $OSC
3983         stop_writeback
3984         sync
3985         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3986         BEFOREWRITES=$(count_ost_writes)
3987         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3988         AFTERWRITES=$(count_ost_writes)
3989         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3990                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3991         fi
3992         BEFOREWRITES=$(count_ost_writes)
3993         sync || error "sync: $?"
3994         AFTERWRITES=$(count_ost_writes)
3995         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3996                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3997         fi
3998         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3999         start_writeback
4000         return 0
4001 }
4002 run_test 42b "test destroy of file with cached dirty data ======"
4003
4004 # if these tests just want to test the effect of truncation,
4005 # they have to be very careful.  consider:
4006 # - the first open gets a {0,EOF}PR lock
4007 # - the first write conflicts and gets a {0, count-1}PW
4008 # - the rest of the writes are under {count,EOF}PW
4009 # - the open for truncate tries to match a {0,EOF}PR
4010 #   for the filesize and cancels the PWs.
4011 # any number of fixes (don't get {0,EOF} on open, match
4012 # composite locks, do smarter file size management) fix
4013 # this, but for now we want these tests to verify that
4014 # the cancellation with truncate intent works, so we
4015 # start the file with a full-file pw lock to match against
4016 # until the truncate.
4017 trunc_test() {
4018         test=$1
4019         file=$DIR/$test
4020         offset=$2
4021         cancel_lru_locks $OSC
4022         stop_writeback
4023         # prime the file with 0,EOF PW to match
4024         touch $file
4025         $TRUNCATE $file 0
4026         sync; sync
4027         # now the real test..
4028         dd if=/dev/zero of=$file bs=1024 count=100
4029         BEFOREWRITES=`count_ost_writes`
4030         $TRUNCATE $file $offset
4031         cancel_lru_locks $OSC
4032         AFTERWRITES=`count_ost_writes`
4033         start_writeback
4034 }
4035
4036 test_42c() {
4037         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4038
4039         trunc_test 42c 1024
4040         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
4041                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
4042         rm $file
4043 }
4044 run_test 42c "test partial truncate of file with cached dirty data"
4045
4046 test_42d() {
4047         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4048
4049         trunc_test 42d 0
4050         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
4051                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
4052         rm $file
4053 }
4054 run_test 42d "test complete truncate of file with cached dirty data"
4055
4056 test_42e() { # bug22074
4057         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4058
4059         local TDIR=$DIR/${tdir}e
4060         local pages=16 # hardcoded 16 pages, don't change it.
4061         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
4062         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
4063         local max_dirty_mb
4064         local warmup_files
4065
4066         test_mkdir $DIR/${tdir}e
4067         $SETSTRIPE -c 1 $TDIR
4068         createmany -o $TDIR/f $files
4069
4070         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
4071
4072         # we assume that with $OSTCOUNT files, at least one of them will
4073         # be allocated on OST0.
4074         warmup_files=$((OSTCOUNT * max_dirty_mb))
4075         createmany -o $TDIR/w $warmup_files
4076
4077         # write a large amount of data into one file and sync, to get good
4078         # avail_grant number from OST.
4079         for ((i=0; i<$warmup_files; i++)); do
4080                 idx=$($GETSTRIPE -i $TDIR/w$i)
4081                 [ $idx -ne 0 ] && continue
4082                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
4083                 break
4084         done
4085         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
4086         sync
4087         $LCTL get_param $proc_osc0/cur_dirty_bytes
4088         $LCTL get_param $proc_osc0/cur_grant_bytes
4089
4090         # create as much dirty pages as we can while not to trigger the actual
4091         # RPCs directly. but depends on the env, VFS may trigger flush during this
4092         # period, hopefully we are good.
4093         for ((i=0; i<$warmup_files; i++)); do
4094                 idx=$($GETSTRIPE -i $TDIR/w$i)
4095                 [ $idx -ne 0 ] && continue
4096                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4097         done
4098         $LCTL get_param $proc_osc0/cur_dirty_bytes
4099         $LCTL get_param $proc_osc0/cur_grant_bytes
4100
4101         # perform the real test
4102         $LCTL set_param $proc_osc0/rpc_stats 0
4103         for ((;i<$files; i++)); do
4104                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
4105                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
4106         done
4107         sync
4108         $LCTL get_param $proc_osc0/rpc_stats
4109
4110         local percent=0
4111         local have_ppr=false
4112         $LCTL get_param $proc_osc0/rpc_stats |
4113                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4114                         # skip lines until we are at the RPC histogram data
4115                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4116                         $have_ppr || continue
4117
4118                         # we only want the percent stat for < 16 pages
4119                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4120
4121                         percent=$((percent + WPCT))
4122                         if [[ $percent -gt 15 ]]; then
4123                                 error "less than 16-pages write RPCs" \
4124                                       "$percent% > 15%"
4125                                 break
4126                         fi
4127                 done
4128         rm -rf $TDIR
4129 }
4130 run_test 42e "verify sub-RPC writes are not done synchronously"
4131
4132 test_43A() { # was test_43
4133         test_mkdir $DIR/$tdir
4134         cp -p /bin/ls $DIR/$tdir/$tfile
4135         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4136         pid=$!
4137         # give multiop a chance to open
4138         sleep 1
4139
4140         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4141         kill -USR1 $pid
4142 }
4143 run_test 43A "execution of file opened for write should return -ETXTBSY"
4144
4145 test_43a() {
4146         test_mkdir $DIR/$tdir
4147         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
4148                 cp -p multiop $DIR/$tdir/multiop
4149         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
4150                 error "multiop open $TMP/$tfile.junk failed"
4151         rm $TMP/$tfile.junk     # delete junk file on close (not part of test)
4152         MULTIOP_PID=$!
4153         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
4154         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
4155         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
4156 }
4157 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4158
4159 test_43b() {
4160         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4161
4162         test_mkdir $DIR/$tdir
4163         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
4164                 cp -p multiop $DIR/$tdir/multiop
4165         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
4166                 error "multiop open $TMP/$tfile.junk failed"
4167         rm $TMP/$tfile.junk     # delete junk file on close (not part of test)
4168         MULTIOP_PID=$!
4169         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
4170         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
4171         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
4172 }
4173 run_test 43b "truncate of file being executed should return -ETXTBSY"
4174
4175 test_43c() {
4176         local testdir="$DIR/$tdir"
4177         test_mkdir $testdir
4178         cp $SHELL $testdir/
4179         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4180                 ( cd $testdir && md5sum -c )
4181 }
4182 run_test 43c "md5sum of copy into lustre"
4183
4184 test_44A() { # was test_44
4185         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4186
4187         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4188         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4189 }
4190 run_test 44A "zero length read from a sparse stripe"
4191
4192 test_44a() {
4193         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4194                 awk '{ print $2 }')
4195         [ -z "$nstripe" ] && skip "can't get stripe info"
4196         [[ $nstripe -gt $OSTCOUNT ]] &&
4197                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4198
4199         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4200                 awk '{ print $2 }')
4201         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4202                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4203                         awk '{ print $2 }')
4204         fi
4205
4206         OFFSETS="0 $((stride/2)) $((stride-1))"
4207         for offset in $OFFSETS; do
4208                 for i in $(seq 0 $((nstripe-1))); do
4209                         local GLOBALOFFSETS=""
4210                         # size in Bytes
4211                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4212                         local myfn=$DIR/d44a-$size
4213                         echo "--------writing $myfn at $size"
4214                         ll_sparseness_write $myfn $size ||
4215                                 error "ll_sparseness_write"
4216                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4217                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4218                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4219
4220                         for j in $(seq 0 $((nstripe-1))); do
4221                                 # size in Bytes
4222                                 size=$((((j + $nstripe )*$stride + $offset)))
4223                                 ll_sparseness_write $myfn $size ||
4224                                         error "ll_sparseness_write"
4225                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4226                         done
4227                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4228                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4229                         rm -f $myfn
4230                 done
4231         done
4232 }
4233 run_test 44a "test sparse pwrite ==============================="
4234
4235 dirty_osc_total() {
4236         tot=0
4237         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4238                 tot=$(($tot + $d))
4239         done
4240         echo $tot
4241 }
4242 do_dirty_record() {
4243         before=`dirty_osc_total`
4244         echo executing "\"$*\""
4245         eval $*
4246         after=`dirty_osc_total`
4247         echo before $before, after $after
4248 }
4249 test_45() {
4250         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4251
4252         f="$DIR/f45"
4253         # Obtain grants from OST if it supports it
4254         echo blah > ${f}_grant
4255         stop_writeback
4256         sync
4257         do_dirty_record "echo blah > $f"
4258         [[ $before -eq $after ]] && error "write wasn't cached"
4259         do_dirty_record "> $f"
4260         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4261         do_dirty_record "echo blah > $f"
4262         [[ $before -eq $after ]] && error "write wasn't cached"
4263         do_dirty_record "sync"
4264         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4265         do_dirty_record "echo blah > $f"
4266         [[ $before -eq $after ]] && error "write wasn't cached"
4267         do_dirty_record "cancel_lru_locks osc"
4268         [[ $before -gt $after ]] ||
4269                 error "lock cancellation didn't lower dirty count"
4270         start_writeback
4271 }
4272 run_test 45 "osc io page accounting ============================"
4273
4274 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4275 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4276 # objects offset and an assert hit when an rpc was built with 1023's mapped
4277 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4278 test_46() {
4279         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4280
4281         f="$DIR/f46"
4282         stop_writeback
4283         sync
4284         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4285         sync
4286         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
4287         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4288         sync
4289         start_writeback
4290 }
4291 run_test 46 "dirtying a previously written page ================"
4292
4293 # test_47 is removed "Device nodes check" is moved to test_28
4294
4295 test_48a() { # bug 2399
4296         [ "$mds1_FSTYPE" = "zfs" ] &&
4297         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
4298                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
4299
4300         test_mkdir $DIR/$tdir
4301         cd $DIR/$tdir
4302         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
4303         test_mkdir $DIR/$tdir
4304         touch foo || error "'touch foo' failed after recreating cwd"
4305         test_mkdir bar
4306         touch .foo || error "'touch .foo' failed after recreating cwd"
4307         test_mkdir .bar
4308         ls . > /dev/null || error "'ls .' failed after recreating cwd"
4309         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4310         cd . || error "'cd .' failed after recreating cwd"
4311         mkdir . && error "'mkdir .' worked after recreating cwd"
4312         rmdir . && error "'rmdir .' worked after recreating cwd"
4313         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4314         cd .. || error "'cd ..' failed after recreating cwd"
4315 }
4316 run_test 48a "Access renamed working dir (should return errors)="
4317
4318 test_48b() { # bug 2399
4319         rm -rf $DIR/$tdir
4320         test_mkdir $DIR/$tdir
4321         cd $DIR/$tdir
4322         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4323         touch foo && error "'touch foo' worked after removing cwd"
4324         mkdir foo && error "'mkdir foo' worked after removing cwd"
4325         touch .foo && error "'touch .foo' worked after removing cwd"
4326         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4327         ls . > /dev/null && error "'ls .' worked after removing cwd"
4328         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4329         mkdir . && error "'mkdir .' worked after removing cwd"
4330         rmdir . && error "'rmdir .' worked after removing cwd"
4331         ln -s . foo && error "'ln -s .' worked after removing cwd"
4332         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4333 }
4334 run_test 48b "Access removed working dir (should return errors)="
4335
4336 test_48c() { # bug 2350
4337         #lctl set_param debug=-1
4338         #set -vx
4339         rm -rf $DIR/$tdir
4340         test_mkdir -p $DIR/$tdir/dir
4341         cd $DIR/$tdir/dir
4342         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4343         $TRACE touch foo && error "touch foo worked after removing cwd"
4344         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4345         touch .foo && error "touch .foo worked after removing cwd"
4346         mkdir .foo && error "mkdir .foo worked after removing cwd"
4347         $TRACE ls . && error "'ls .' worked after removing cwd"
4348         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4349         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4350         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4351         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4352         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4353 }
4354 run_test 48c "Access removed working subdir (should return errors)"
4355
4356 test_48d() { # bug 2350
4357         #lctl set_param debug=-1
4358         #set -vx
4359         rm -rf $DIR/$tdir
4360         test_mkdir -p $DIR/$tdir/dir
4361         cd $DIR/$tdir/dir
4362         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4363         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4364         $TRACE touch foo && error "'touch foo' worked after removing parent"
4365         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4366         touch .foo && error "'touch .foo' worked after removing parent"
4367         mkdir .foo && error "mkdir .foo worked after removing parent"
4368         $TRACE ls . && error "'ls .' worked after removing parent"
4369         $TRACE ls .. && error "'ls ..' worked after removing parent"
4370         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
4371         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4372         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4373         true
4374 }
4375 run_test 48d "Access removed parent subdir (should return errors)"
4376
4377 test_48e() { # bug 4134
4378         #lctl set_param debug=-1
4379         #set -vx
4380         rm -rf $DIR/$tdir
4381         test_mkdir -p $DIR/$tdir/dir
4382         cd $DIR/$tdir/dir
4383         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4384         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4385         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4386         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4387         # On a buggy kernel addition of "touch foo" after cd .. will
4388         # produce kernel oops in lookup_hash_it
4389         touch ../foo && error "'cd ..' worked after recreate parent"
4390         cd $DIR
4391         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4392 }
4393 run_test 48e "Access to recreated parent subdir (should return errors)"
4394
4395 test_49() { # LU-1030
4396         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4397         remote_ost_nodsh && skip "remote OST with nodsh"
4398
4399         # get ost1 size - lustre-OST0000
4400         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4401                 awk '{ print $4 }')
4402         # write 800M at maximum
4403         [[ $ost1_size -lt 2 ]] && ost1_size=2
4404         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4405
4406         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
4407         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4408         local dd_pid=$!
4409
4410         # change max_pages_per_rpc while writing the file
4411         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4412         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4413         # loop until dd process exits
4414         while ps ax -opid | grep -wq $dd_pid; do
4415                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4416                 sleep $((RANDOM % 5 + 1))
4417         done
4418         # restore original max_pages_per_rpc
4419         $LCTL set_param $osc1_mppc=$orig_mppc
4420         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4421 }
4422 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4423
4424 test_50() {
4425         # bug 1485
4426         test_mkdir $DIR/$tdir
4427         cd $DIR/$tdir
4428         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4429 }
4430 run_test 50 "special situations: /proc symlinks  ==============="
4431
4432 test_51a() {    # was test_51
4433         # bug 1516 - create an empty entry right after ".." then split dir
4434         test_mkdir -c1 $DIR/$tdir
4435         touch $DIR/$tdir/foo
4436         $MCREATE $DIR/$tdir/bar
4437         rm $DIR/$tdir/foo
4438         createmany -m $DIR/$tdir/longfile 201
4439         FNUM=202
4440         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4441                 $MCREATE $DIR/$tdir/longfile$FNUM
4442                 FNUM=$(($FNUM + 1))
4443                 echo -n "+"
4444         done
4445         echo
4446         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4447 }
4448 run_test 51a "special situations: split htree with empty entry =="
4449
4450 cleanup_print_lfs_df () {
4451         trap 0
4452         $LFS df
4453         $LFS df -i
4454 }
4455
4456 test_51b() {
4457         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4458
4459         local dir=$DIR/$tdir
4460         local nrdirs=$((65536 + 100))
4461
4462         # cleanup the directory
4463         rm -fr $dir
4464
4465         test_mkdir -c1 $dir
4466
4467         $LFS df
4468         $LFS df -i
4469         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
4470         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4471         [[ $numfree -lt $nrdirs ]] &&
4472                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
4473
4474         # need to check free space for the directories as well
4475         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4476         numfree=$(( blkfree / $(fs_inode_ksize) ))
4477         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
4478
4479         trap cleanup_print_lfs_df EXIT
4480
4481         # create files
4482         createmany -d $dir/d $nrdirs || {
4483                 unlinkmany $dir/d $nrdirs
4484                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
4485         }
4486
4487         # really created :
4488         nrdirs=$(ls -U $dir | wc -l)
4489
4490         # unlink all but 100 subdirectories, then check it still works
4491         local left=100
4492         local delete=$((nrdirs - left))
4493
4494         $LFS df
4495         $LFS df -i
4496
4497         # for ldiskfs the nlink count should be 1, but this is OSD specific
4498         # and so this is listed for informational purposes only
4499         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
4500         unlinkmany -d $dir/d $delete ||
4501                 error "unlink of first $delete subdirs failed"
4502
4503         echo "nlink between: $(stat -c %h $dir)"
4504         local found=$(ls -U $dir | wc -l)
4505         [ $found -ne $left ] &&
4506                 error "can't find subdirs: found only $found, expected $left"
4507
4508         unlinkmany -d $dir/d $delete $left ||
4509                 error "unlink of second $left subdirs failed"
4510         # regardless of whether the backing filesystem tracks nlink accurately
4511         # or not, the nlink count shouldn't be more than "." and ".." here
4512         local after=$(stat -c %h $dir)
4513         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
4514                 echo "nlink after: $after"
4515
4516         cleanup_print_lfs_df
4517 }
4518 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
4519
4520 test_51d() {
4521         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4522         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
4523
4524         test_mkdir $DIR/$tdir
4525         createmany -o $DIR/$tdir/t- 1000
4526         $LFS getstripe $DIR/$tdir > $TMP/$tfile
4527         for N in $(seq 0 $((OSTCOUNT - 1))); do
4528                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4529                         END { printf("%0.0f", objs) }' $TMP/$tfile)
4530                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
4531                         '($1 == '$N') { objs += 1 } \
4532                         END { printf("%0.0f", objs) }')
4533                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4534         done
4535         unlinkmany $DIR/$tdir/t- 1000
4536
4537         NLAST=0
4538         for N in $(seq 1 $((OSTCOUNT - 1))); do
4539                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4540                         error "OST $N has less objects vs OST $NLAST" \
4541                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4542                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4543                         error "OST $N has less objects vs OST $NLAST" \
4544                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4545
4546                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4547                         error "OST $N has less #0 objects vs OST $NLAST" \
4548                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4549                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4550                         error "OST $N has less #0 objects vs OST $NLAST" \
4551                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4552                 NLAST=$N
4553         done
4554         rm -f $TMP/$tfile
4555 }
4556 run_test 51d "check object distribution"
4557
4558 test_51e() {
4559         if [ "$mds1_FSTYPE" != ldiskfs ]; then
4560                 skip_env "ldiskfs only test"
4561         fi
4562
4563         test_mkdir -c1 $DIR/$tdir
4564         test_mkdir -c1 $DIR/$tdir/d0
4565
4566         touch $DIR/$tdir/d0/foo
4567         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4568                 error "file exceed 65000 nlink limit!"
4569         unlinkmany $DIR/$tdir/d0/f- 65001
4570         return 0
4571 }
4572 run_test 51e "check file nlink limit"
4573
4574 test_51f() {
4575         test_mkdir $DIR/$tdir
4576
4577         local max=100000
4578         local ulimit_old=$(ulimit -n)
4579         local spare=20 # number of spare fd's for scripts/libraries, etc.
4580         local mdt=$($LFS getstripe -m $DIR/$tdir)
4581         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
4582
4583         echo "MDT$mdt numfree=$numfree, max=$max"
4584         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
4585         if [ $((numfree + spare)) -gt $ulimit_old ]; then
4586                 while ! ulimit -n $((numfree + spare)); do
4587                         numfree=$((numfree * 3 / 4))
4588                 done
4589                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
4590         else
4591                 echo "left ulimit at $ulimit_old"
4592         fi
4593
4594         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
4595                 unlinkmany $DIR/$tdir/f $numfree
4596                 error "create+open $numfree files in $DIR/$tdir failed"
4597         }
4598         ulimit -n $ulimit_old
4599
4600         # if createmany exits at 120s there will be fewer than $numfree files
4601         unlinkmany $DIR/$tdir/f $numfree || true
4602 }
4603 run_test 51f "check many open files limit"
4604
4605 test_52a() {
4606         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4607         test_mkdir $DIR/$tdir
4608         touch $DIR/$tdir/foo
4609         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4610         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4611         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4612         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4613         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4614                                         error "link worked"
4615         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4616         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4617         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4618                                                      error "lsattr"
4619         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4620         cp -r $DIR/$tdir $TMP/
4621         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
4622 }
4623 run_test 52a "append-only flag test (should return errors)"
4624
4625 test_52b() {
4626         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4627         test_mkdir $DIR/$tdir
4628         touch $DIR/$tdir/foo
4629         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4630         cat test > $DIR/$tdir/foo && error "cat test worked"
4631         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4632         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4633         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4634                                         error "link worked"
4635         echo foo >> $DIR/$tdir/foo && error "echo worked"
4636         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4637         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4638         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4639         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4640                                                         error "lsattr"
4641         chattr -i $DIR/$tdir/foo || error "chattr failed"
4642
4643         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4644 }
4645 run_test 52b "immutable flag test (should return errors) ======="
4646
4647 test_53() {
4648         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4649         remote_mds_nodsh && skip "remote MDS with nodsh"
4650         remote_ost_nodsh && skip "remote OST with nodsh"
4651
4652         local param
4653         local param_seq
4654         local ostname
4655         local mds_last
4656         local mds_last_seq
4657         local ost_last
4658         local ost_last_seq
4659         local ost_last_id
4660         local ostnum
4661         local node
4662         local found=false
4663         local support_last_seq=true
4664
4665         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
4666                 support_last_seq=false
4667
4668         # only test MDT0000
4669         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4670         local value
4671         for value in $(do_facet $SINGLEMDS \
4672                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4673                 param=$(echo ${value[0]} | cut -d "=" -f1)
4674                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4675
4676                 if $support_last_seq; then
4677                         param_seq=$(echo $param |
4678                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4679                         mds_last_seq=$(do_facet $SINGLEMDS \
4680                                        $LCTL get_param -n $param_seq)
4681                 fi
4682                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4683
4684                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4685                 node=$(facet_active_host ost$((ostnum+1)))
4686                 param="obdfilter.$ostname.last_id"
4687                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4688                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4689                         ost_last_id=$ost_last
4690
4691                         if $support_last_seq; then
4692                                 ost_last_id=$(echo $ost_last |
4693                                               awk -F':' '{print $2}' |
4694                                               sed -e "s/^0x//g")
4695                                 ost_last_seq=$(echo $ost_last |
4696                                                awk -F':' '{print $1}')
4697                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4698                         fi
4699
4700                         if [[ $ost_last_id != $mds_last ]]; then
4701                                 error "$ost_last_id != $mds_last"
4702                         else
4703                                 found=true
4704                                 break
4705                         fi
4706                 done
4707         done
4708         $found || error "can not match last_seq/last_id for $mdtosc"
4709         return 0
4710 }
4711 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4712
4713 test_54a() {
4714         perl -MSocket -e ';' || skip "no Socket perl module installed"
4715
4716         $SOCKETSERVER $DIR/socket ||
4717                 error "$SOCKETSERVER $DIR/socket failed: $?"
4718         $SOCKETCLIENT $DIR/socket ||
4719                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4720         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4721 }
4722 run_test 54a "unix domain socket test =========================="
4723
4724 test_54b() {
4725         f="$DIR/f54b"
4726         mknod $f c 1 3
4727         chmod 0666 $f
4728         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
4729 }
4730 run_test 54b "char device works in lustre ======================"
4731
4732 find_loop_dev() {
4733         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4734         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4735         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4736
4737         for i in $(seq 3 7); do
4738                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4739                 LOOPDEV=$LOOPBASE$i
4740                 LOOPNUM=$i
4741                 break
4742         done
4743 }
4744
4745 cleanup_54c() {
4746         local rc=0
4747         loopdev="$DIR/loop54c"
4748
4749         trap 0
4750         $UMOUNT $DIR/$tdir || rc=$?
4751         losetup -d $loopdev || true
4752         losetup -d $LOOPDEV || true
4753         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4754         return $rc
4755 }
4756
4757 test_54c() {
4758         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4759
4760         loopdev="$DIR/loop54c"
4761
4762         find_loop_dev
4763         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
4764         trap cleanup_54c EXIT
4765         mknod $loopdev b 7 $LOOPNUM
4766         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4767         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
4768         losetup $loopdev $DIR/$tfile ||
4769                 error "can't set up $loopdev for $DIR/$tfile"
4770         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4771         test_mkdir $DIR/$tdir
4772         mount -t ext2 $loopdev $DIR/$tdir ||
4773                 error "error mounting $loopdev on $DIR/$tdir"
4774         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
4775                 error "dd write"
4776         df $DIR/$tdir
4777         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
4778                 error "dd read"
4779         cleanup_54c
4780 }
4781 run_test 54c "block device works in lustre ====================="
4782
4783 test_54d() {
4784         f="$DIR/f54d"
4785         string="aaaaaa"
4786         mknod $f p
4787         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4788 }
4789 run_test 54d "fifo device works in lustre ======================"
4790
4791 test_54e() {
4792         f="$DIR/f54e"
4793         string="aaaaaa"
4794         cp -aL /dev/console $f
4795         echo $string > $f || error "echo $string to $f failed"
4796 }
4797 run_test 54e "console/tty device works in lustre ======================"
4798
4799 test_56a() {
4800         local numfiles=3
4801         local dir=$DIR/$tdir
4802
4803         rm -rf $dir
4804         test_mkdir -p $dir/dir
4805         for i in $(seq $numfiles); do
4806                 touch $dir/file$i
4807                 touch $dir/dir/file$i
4808         done
4809
4810         local numcomp=$($LFS getstripe --component-count $dir)
4811
4812         [[ $numcomp == 0 ]] && numcomp=1
4813
4814         # test lfs getstripe with --recursive
4815         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
4816
4817         [[ $filenum -eq $((numfiles * 2)) ]] ||
4818                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
4819         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
4820         [[ $filenum -eq $numfiles ]] ||
4821                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
4822         echo "$LFS getstripe showed obdidx or l_ost_idx"
4823
4824         # test lfs getstripe with file instead of dir
4825         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
4826         [[ $filenum -eq 1 ]] ||
4827                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
4828         echo "$LFS getstripe file1 passed"
4829
4830         #test lfs getstripe with --verbose
4831         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
4832         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
4833                 error "$LFS getstripe --verbose $dir: "\
4834                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
4835         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
4836                 error "$LFS getstripe $dir: showed lmm_magic"
4837
4838         #test lfs getstripe with -v prints lmm_fid
4839         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
4840         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
4841                 error "$LFS getstripe -v $dir: "\
4842                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
4843         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
4844                 error "$LFS getstripe $dir: showed lmm_fid by default"
4845         echo "$LFS getstripe --verbose passed"
4846
4847         #check for FID information
4848         local fid1=$($LFS getstripe --fid $dir/file1)
4849         local fid2=$($LFS getstripe --verbose $dir/file1 |
4850                      awk '/lmm_fid: / { print $2; exit; }')
4851         local fid3=$($LFS path2fid $dir/file1)
4852
4853         [ "$fid1" != "$fid2" ] &&
4854                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
4855         [ "$fid1" != "$fid3" ] &&
4856                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
4857         echo "$LFS getstripe --fid passed"
4858
4859         #test lfs getstripe with --obd
4860         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
4861                 error "$LFS getstripe --obd wrong_uuid: should return error"
4862
4863         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4864
4865         local ostidx=1
4866         local obduuid=$(ostuuid_from_index $ostidx)
4867         local found=$($LFS getstripe -r --obd $obduuid $dir |
4868                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
4869
4870         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
4871         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
4872                 ((filenum--))
4873         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
4874                 ((filenum--))
4875
4876         [[ $found -eq $filenum ]] ||
4877                 error "$LFS getstripe --obd: found $found expect $filenum"
4878         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
4879                 sed '/^[         ]*'${ostidx}'[  ]/d' |
4880                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4881                 error "$LFS getstripe --obd: should not show file on other obd"
4882         echo "$LFS getstripe --obd passed"
4883 }
4884 run_test 56a "check $LFS getstripe"
4885
4886 test_56b() {
4887         local dir=$DIR/$tdir
4888         local numdirs=3
4889
4890         test_mkdir $dir
4891         for i in $(seq $numdirs); do
4892                 test_mkdir $dir/dir$i
4893         done
4894
4895         # test lfs getdirstripe default mode is non-recursion, which is
4896         # different from lfs getstripe
4897         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
4898
4899         [[ $dircnt -eq 1 ]] ||
4900                 error "$LFS getdirstripe: found $dircnt, not 1"
4901         dircnt=$($LFS getdirstripe --recursive $dir |
4902                 grep -c lmv_stripe_count)
4903         [[ $dircnt -eq $((numdirs + 1)) ]] ||
4904                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
4905 }
4906 run_test 56b "check $LFS getdirstripe"
4907
4908 test_56c() {
4909         remote_ost_nodsh && skip "remote OST with nodsh"
4910
4911         local ost_idx=0
4912         local ost_name=$(ostname_from_index $ost_idx)
4913         local old_status=$(ost_dev_status $ost_idx)
4914
4915         [[ -z "$old_status" ]] ||
4916                 skip_env "OST $ost_name is in $old_status status"
4917
4918         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
4919         sleep_maxage
4920
4921         local new_status=$(ost_dev_status $ost_idx)
4922
4923         [[ "$new_status" = "D" ]] ||
4924                 error "OST $ost_name is in status of '$new_status', not 'D'"
4925
4926         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
4927         sleep_maxage
4928
4929         new_status=$(ost_dev_status $ost_idx)
4930         [[ -z "$new_status" ]] ||
4931                 error "OST $ost_name is in status of '$new_status', not ''"
4932 }
4933 run_test 56c "check 'lfs df' showing device status"
4934
4935 NUMFILES=3
4936 NUMDIRS=3
4937 setup_56() {
4938         local local_tdir="$1"
4939         local local_numfiles="$2"
4940         local local_numdirs="$3"
4941         local dir_params="$4"
4942         local dir_stripe_params="$5"
4943
4944         if [ ! -d "$local_tdir" ] ; then
4945                 test_mkdir -p $dir_stripe_params $local_tdir
4946                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
4947                 for i in $(seq $local_numfiles) ; do
4948                         touch $local_tdir/file$i
4949                 done
4950                 for i in $(seq $local_numdirs) ; do
4951                         test_mkdir $dir_stripe_params $local_tdir/dir$i
4952                         for j in $(seq $local_numfiles) ; do
4953                                 touch $local_tdir/dir$i/file$j
4954                         done
4955                 done
4956         fi
4957 }
4958
4959 setup_56_special() {
4960         local local_tdir=$1
4961         local local_numfiles=$2
4962         local local_numdirs=$3
4963
4964         setup_56 $local_tdir $local_numfiles $local_numdirs
4965
4966         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
4967                 for i in $(seq $local_numfiles) ; do
4968                         mknod $local_tdir/loop${i}b b 7 $i
4969                         mknod $local_tdir/null${i}c c 1 3
4970                         ln -s $local_tdir/file1 $local_tdir/link${i}
4971                 done
4972                 for i in $(seq $local_numdirs) ; do
4973                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
4974                         mknod $local_tdir/dir$i/null${i}c c 1 3
4975                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
4976                 done
4977         fi
4978 }
4979
4980 test_56g() {
4981         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4982         local expected=$(($NUMDIRS + 2))
4983
4984         setup_56 $dir $NUMFILES $NUMDIRS
4985
4986         # test lfs find with -name
4987         for i in $(seq $NUMFILES) ; do
4988                 local nums=$($LFS find -name "*$i" $dir | wc -l)
4989
4990                 [ $nums -eq $expected ] ||
4991                         error "lfs find -name '*$i' $dir wrong: "\
4992                               "found $nums, expected $expected"
4993         done
4994 }
4995 run_test 56g "check lfs find -name"
4996
4997 test_56h() {
4998         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4999         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
5000
5001         setup_56 $dir $NUMFILES $NUMDIRS
5002
5003         # test lfs find with ! -name
5004         for i in $(seq $NUMFILES) ; do
5005                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
5006
5007                 [ $nums -eq $expected ] ||
5008                         error "lfs find ! -name '*$i' $dir wrong: "\
5009                               "found $nums, expected $expected"
5010         done
5011 }
5012 run_test 56h "check lfs find ! -name"
5013
5014 test_56i() {
5015         local dir=$DIR/$tdir
5016
5017         test_mkdir $dir
5018
5019         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
5020         local out=$($cmd)
5021
5022         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
5023 }
5024 run_test 56i "check 'lfs find -ost UUID' skips directories"
5025
5026 test_56j() {
5027         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5028
5029         setup_56_special $dir $NUMFILES $NUMDIRS
5030
5031         local expected=$((NUMDIRS + 1))
5032         local cmd="$LFS find -type d $dir"
5033         local nums=$($cmd | wc -l)
5034
5035         [ $nums -eq $expected ] ||
5036                 error "'$cmd' wrong: found $nums, expected $expected"
5037 }
5038 run_test 56j "check lfs find -type d"
5039
5040 test_56k() {
5041         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5042
5043         setup_56_special $dir $NUMFILES $NUMDIRS
5044
5045         local expected=$(((NUMDIRS + 1) * NUMFILES))
5046         local cmd="$LFS find -type f $dir"
5047         local nums=$($cmd | wc -l)
5048
5049         [ $nums -eq $expected ] ||
5050                 error "'$cmd' wrong: found $nums, expected $expected"
5051 }
5052 run_test 56k "check lfs find -type f"
5053
5054 test_56l() {
5055         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5056
5057         setup_56_special $dir $NUMFILES $NUMDIRS
5058
5059         local expected=$((NUMDIRS + NUMFILES))
5060         local cmd="$LFS find -type b $dir"
5061         local nums=$($cmd | wc -l)
5062
5063         [ $nums -eq $expected ] ||
5064                 error "'$cmd' wrong: found $nums, expected $expected"
5065 }
5066 run_test 56l "check lfs find -type b"
5067
5068 test_56m() {
5069         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5070
5071         setup_56_special $dir $NUMFILES $NUMDIRS
5072
5073         local expected=$((NUMDIRS + NUMFILES))
5074         local cmd="$LFS find -type c $dir"
5075         local nums=$($cmd | wc -l)
5076         [ $nums -eq $expected ] ||
5077                 error "'$cmd' wrong: found $nums, expected $expected"
5078 }
5079 run_test 56m "check lfs find -type c"
5080
5081 test_56n() {
5082         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5083         setup_56_special $dir $NUMFILES $NUMDIRS
5084
5085         local expected=$((NUMDIRS + NUMFILES))
5086         local cmd="$LFS find -type l $dir"
5087         local nums=$($cmd | wc -l)
5088
5089         [ $nums -eq $expected ] ||
5090                 error "'$cmd' wrong: found $nums, expected $expected"
5091 }
5092 run_test 56n "check lfs find -type l"
5093
5094 test_56o() {
5095         local dir=$DIR/$tdir
5096
5097         setup_56 $dir $NUMFILES $NUMDIRS
5098         utime $dir/file1 > /dev/null || error "utime (1)"
5099         utime $dir/file2 > /dev/null || error "utime (2)"
5100         utime $dir/dir1 > /dev/null || error "utime (3)"
5101         utime $dir/dir2 > /dev/null || error "utime (4)"
5102         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5103         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5104
5105         local expected=4
5106         local nums=$($LFS find -mtime +0 $dir | wc -l)
5107
5108         [ $nums -eq $expected ] ||
5109                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5110
5111         expected=12
5112         cmd="$LFS find -mtime 0 $dir"
5113         nums=$($cmd | wc -l)
5114         [ $nums -eq $expected ] ||
5115                 error "'$cmd' wrong: found $nums, expected $expected"
5116 }
5117 run_test 56o "check lfs find -mtime for old files"
5118
5119 test_56p() {
5120         [ $RUNAS_ID -eq $UID ] &&
5121                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5122
5123         local dir=$DIR/$tdir
5124
5125         setup_56 $dir $NUMFILES $NUMDIRS
5126         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
5127
5128         local expected=$NUMFILES
5129         local cmd="$LFS find -uid $RUNAS_ID $dir"
5130         local nums=$($cmd | wc -l)
5131
5132         [ $nums -eq $expected ] ||
5133                 error "'$cmd' wrong: found $nums, expected $expected"
5134
5135         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
5136         cmd="$LFS find ! -uid $RUNAS_ID $dir"
5137         nums=$($cmd | wc -l)
5138         [ $nums -eq $expected ] ||
5139                 error "'$cmd' wrong: found $nums, expected $expected"
5140 }
5141 run_test 56p "check lfs find -uid and ! -uid"
5142
5143 test_56q() {
5144         [ $RUNAS_ID -eq $UID ] &&
5145                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5146
5147         local dir=$DIR/$tdir
5148
5149         setup_56 $dir $NUMFILES $NUMDIRS
5150         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
5151
5152         local expected=$NUMFILES
5153         local cmd="$LFS find -gid $RUNAS_GID $dir"
5154         local nums=$($cmd | wc -l)
5155
5156         [ $nums -eq $expected ] ||
5157                 error "'$cmd' wrong: found $nums, expected $expected"
5158
5159         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
5160         cmd="$LFS find ! -gid $RUNAS_GID $dir"
5161         nums=$($cmd | wc -l)
5162         [ $nums -eq $expected ] ||
5163                 error "'$cmd' wrong: found $nums, expected $expected"
5164 }
5165 run_test 56q "check lfs find -gid and ! -gid"
5166
5167 test_56r() {
5168         local dir=$DIR/$tdir
5169
5170         setup_56 $dir $NUMFILES $NUMDIRS
5171
5172         local expected=12
5173         local cmd="$LFS find -size 0 -type f $dir"
5174         local nums=$($cmd | wc -l)
5175
5176         [ $nums -eq $expected ] ||
5177                 error "'$cmd' wrong: found $nums, expected $expected"
5178         expected=0
5179         cmd="$LFS find ! -size 0 -type f $dir"
5180         nums=$($cmd | wc -l)
5181         [ $nums -eq $expected ] ||
5182                 error "'$cmd' wrong: found $nums, expected $expected"
5183         echo "test" > $dir/$tfile
5184         echo "test2" > $dir/$tfile.2 && sync
5185         expected=1
5186         cmd="$LFS find -size 5 -type f $dir"
5187         nums=$($cmd | wc -l)
5188         [ $nums -eq $expected ] ||
5189                 error "'$cmd' wrong: found $nums, expected $expected"
5190         expected=1
5191         cmd="$LFS find -size +5 -type f $dir"
5192         nums=$($cmd | wc -l)
5193         [ $nums -eq $expected ] ||
5194                 error "'$cmd' wrong: found $nums, expected $expected"
5195         expected=2
5196         cmd="$LFS find -size +0 -type f $dir"
5197         nums=$($cmd | wc -l)
5198         [ $nums -eq $expected ] ||
5199                 error "'$cmd' wrong: found $nums, expected $expected"
5200         expected=2
5201         cmd="$LFS find ! -size -5 -type f $dir"
5202         nums=$($cmd | wc -l)
5203         [ $nums -eq $expected ] ||
5204                 error "'$cmd' wrong: found $nums, expected $expected"
5205         expected=12
5206         cmd="$LFS find -size -5 -type f $dir"
5207         nums=$($cmd | wc -l)
5208         [ $nums -eq $expected ] ||
5209                 error "'$cmd' wrong: found $nums, expected $expected"
5210 }
5211 run_test 56r "check lfs find -size works"
5212
5213 test_56s() { # LU-611 #LU-9369
5214         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
5215
5216         local dir=$DIR/$tdir
5217         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
5218
5219         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
5220         for i in $(seq $NUMDIRS); do
5221                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
5222         done
5223
5224         local expected=$NUMDIRS
5225         local cmd="$LFS find -c $OSTCOUNT $dir"
5226         local nums=$($cmd | wc -l)
5227
5228         [ $nums -eq $expected ] || {
5229                 $LFS getstripe -R $dir
5230                 error "'$cmd' wrong: found $nums, expected $expected"
5231         }
5232
5233         expected=$((NUMDIRS + onestripe))
5234         cmd="$LFS find -stripe-count +0 -type f $dir"
5235         nums=$($cmd | wc -l)
5236         [ $nums -eq $expected ] || {
5237                 $LFS getstripe -R $dir
5238                 error "'$cmd' wrong: found $nums, expected $expected"
5239         }
5240
5241         expected=$onestripe
5242         cmd="$LFS find -stripe-count 1 -type f $dir"
5243         nums=$($cmd | wc -l)
5244         [ $nums -eq $expected ] || {
5245                 $LFS getstripe -R $dir
5246                 error "'$cmd' wrong: found $nums, expected $expected"
5247         }
5248
5249         cmd="$LFS find -stripe-count -2 -type f $dir"
5250         nums=$($cmd | wc -l)
5251         [ $nums -eq $expected ] || {
5252                 $LFS getstripe -R $dir
5253                 error "'$cmd' wrong: found $nums, expected $expected"
5254         }
5255
5256         expected=0
5257         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
5258         nums=$($cmd | wc -l)
5259         [ $nums -eq $expected ] || {
5260                 $LFS getstripe -R $dir
5261                 error "'$cmd' wrong: found $nums, expected $expected"
5262         }
5263 }
5264 run_test 56s "check lfs find -stripe-count works"
5265
5266 test_56t() { # LU-611 #LU-9369
5267         local dir=$DIR/$tdir
5268
5269         setup_56 $dir 0 $NUMDIRS
5270         for i in $(seq $NUMDIRS); do
5271                 $LFS setstripe -S 8M $dir/dir$i/$tfile
5272         done
5273
5274         local expected=$NUMDIRS
5275         local cmd="$LFS find -S 8M $dir"
5276         local nums=$($cmd | wc -l)
5277
5278         [ $nums -eq $expected ] || {
5279                 $LFS getstripe -R $dir
5280                 error "'$cmd' wrong: found $nums, expected $expected"
5281         }
5282         rm -rf $dir
5283
5284         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
5285
5286         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
5287
5288         expected=$(((NUMDIRS + 1) * NUMFILES))
5289         cmd="$LFS find -stripe-size 512k -type f $dir"
5290         nums=$($cmd | wc -l)
5291         [ $nums -eq $expected ] ||
5292                 error "'$cmd' wrong: found $nums, expected $expected"
5293
5294         cmd="$LFS find -stripe-size +320k -type f $dir"
5295         nums=$($cmd | wc -l)
5296         [ $nums -eq $expected ] ||
5297                 error "'$cmd' wrong: found $nums, expected $expected"
5298
5299         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
5300         cmd="$LFS find -stripe-size +200k -type f $dir"
5301         nums=$($cmd | wc -l)
5302         [ $nums -eq $expected ] ||
5303                 error "'$cmd' wrong: found $nums, expected $expected"
5304
5305         cmd="$LFS find -stripe-size -640k -type f $dir"
5306         nums=$($cmd | wc -l)
5307         [ $nums -eq $expected ] ||
5308                 error "'$cmd' wrong: found $nums, expected $expected"
5309
5310         expected=4
5311         cmd="$LFS find -stripe-size 256k -type f $dir"
5312         nums=$($cmd | wc -l)
5313         [ $nums -eq $expected ] ||
5314                 error "'$cmd' wrong: found $nums, expected $expected"
5315
5316         cmd="$LFS find -stripe-size -320k -type f $dir"
5317         nums=$($cmd | wc -l)
5318         [ $nums -eq $expected ] ||
5319                 error "'$cmd' wrong: found $nums, expected $expected"
5320
5321         expected=0
5322         cmd="$LFS find -stripe-size 1024k -type f $dir"
5323         nums=$($cmd | wc -l)
5324         [ $nums -eq $expected ] ||
5325                 error "'$cmd' wrong: found $nums, expected $expected"
5326 }
5327 run_test 56t "check lfs find -stripe-size works"
5328
5329 test_56u() { # LU-611
5330         local dir=$DIR/$tdir
5331
5332         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
5333
5334         if [[ $OSTCOUNT -gt 1 ]]; then
5335                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
5336                 onestripe=4
5337         else
5338                 onestripe=0
5339         fi
5340
5341         local expected=$(((NUMDIRS + 1) * NUMFILES))
5342         local cmd="$LFS find -stripe-index 0 -type f $dir"
5343         local nums=$($cmd | wc -l)
5344
5345         [ $nums -eq $expected ] ||
5346                 error "'$cmd' wrong: found $nums, expected $expected"
5347
5348         expected=$onestripe
5349         cmd="$LFS find -stripe-index 1 -type f $dir"
5350         nums=$($cmd | wc -l)
5351         [ $nums -eq $expected ] ||
5352                 error "'$cmd' wrong: found $nums, expected $expected"
5353
5354         cmd="$LFS find ! -stripe-index 0 -type f $dir"
5355         nums=$($cmd | wc -l)
5356         [ $nums -eq $expected ] ||
5357                 error "'$cmd' wrong: found $nums, expected $expected"
5358
5359         expected=0
5360         # This should produce an error and not return any files
5361         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
5362         nums=$($cmd 2>/dev/null | wc -l)
5363         [ $nums -eq $expected ] ||
5364                 error "'$cmd' wrong: found $nums, expected $expected"
5365
5366         if [[ $OSTCOUNT -gt 1 ]]; then
5367                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
5368                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
5369                 nums=$($cmd | wc -l)
5370                 [ $nums -eq $expected ] ||
5371                         error "'$cmd' wrong: found $nums, expected $expected"
5372         fi
5373 }
5374 run_test 56u "check lfs find -stripe-index works"
5375
5376 test_56v() {
5377         local mdt_idx=0
5378         local dir=$DIR/$tdir
5379
5380         setup_56 $dir $NUMFILES $NUMDIRS
5381
5382         UUID=$(mdtuuid_from_index $mdt_idx $dir)
5383         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
5384
5385         for file in $($LFS find -m $UUID $dir); do
5386                 file_midx=$($LFS getstripe -m $file)
5387                 [ $file_midx -eq $mdt_idx ] ||
5388                         error "lfs find -m $UUID != getstripe -m $file_midx"
5389         done
5390 }
5391 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
5392
5393 test_56w() {
5394         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5395         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5396
5397         local dir=$DIR/$tdir
5398
5399         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
5400
5401         local stripe_size=$($LFS getstripe -S -d $dir) ||
5402                 error "$LFS getstripe -S -d $dir failed"
5403         stripe_size=${stripe_size%% *}
5404
5405         local file_size=$((stripe_size * OSTCOUNT))
5406         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
5407         local required_space=$((file_num * file_size))
5408         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
5409                            head -n1)
5410         [[ $free_space -le $((required_space / 1024)) ]] &&
5411                 skip_env "need $required_space, have $free_space kbytes"
5412
5413         local dd_bs=65536
5414         local dd_count=$((file_size / dd_bs))
5415
5416         # write data into the files
5417         local i
5418         local j
5419         local file
5420
5421         for i in $(seq $NUMFILES); do
5422                 file=$dir/file$i
5423                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5424                         error "write data into $file failed"
5425         done
5426         for i in $(seq $NUMDIRS); do
5427                 for j in $(seq $NUMFILES); do
5428                         file=$dir/dir$i/file$j
5429                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5430                                 error "write data into $file failed"
5431                 done
5432         done
5433
5434         # $LFS_MIGRATE will fail if hard link migration is unsupported
5435         if [[ $(lustre_version_code mds1) -gt $(version_code 2.5.55) ]]; then
5436                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
5437                         error "creating links to $dir/dir1/file1 failed"
5438         fi
5439
5440         local expected=-1
5441
5442         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
5443
5444         # lfs_migrate file
5445         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
5446
5447         echo "$cmd"
5448         eval $cmd || error "$cmd failed"
5449
5450         check_stripe_count $dir/file1 $expected
5451
5452         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
5453         then
5454                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
5455                 # OST 1 if it is on OST 0. This file is small enough to
5456                 # be on only one stripe.
5457                 file=$dir/migr_1_ost
5458                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
5459                         error "write data into $file failed"
5460                 local obdidx=$($LFS getstripe -i $file)
5461                 local oldmd5=$(md5sum $file)
5462                 local newobdidx=0
5463
5464                 [[ $obdidx -eq 0 ]] && newobdidx=1
5465                 cmd="$LFS migrate -i $newobdidx $file"
5466                 echo $cmd
5467                 eval $cmd || error "$cmd failed"
5468
5469                 local realobdix=$($LFS getstripe -i $file)
5470                 local newmd5=$(md5sum $file)
5471
5472                 [[ $newobdidx -ne $realobdix ]] &&
5473                         error "new OST is different (was=$obdidx, "\
5474                               "wanted=$newobdidx, got=$realobdix)"
5475                 [[ "$oldmd5" != "$newmd5" ]] &&
5476                         error "md5sum differ: $oldmd5, $newmd5"
5477         fi
5478
5479         # lfs_migrate dir
5480         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
5481         echo "$cmd"
5482         eval $cmd || error "$cmd failed"
5483
5484         for j in $(seq $NUMFILES); do
5485                 check_stripe_count $dir/dir1/file$j $expected
5486         done
5487
5488         # lfs_migrate works with lfs find
5489         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
5490              $LFS_MIGRATE -y -c $expected"
5491         echo "$cmd"
5492         eval $cmd || error "$cmd failed"
5493
5494         for i in $(seq 2 $NUMFILES); do
5495                 check_stripe_count $dir/file$i $expected
5496         done
5497         for i in $(seq 2 $NUMDIRS); do
5498                 for j in $(seq $NUMFILES); do
5499                 check_stripe_count $dir/dir$i/file$j $expected
5500                 done
5501         done
5502 }
5503 run_test 56w "check lfs_migrate -c stripe_count works"
5504
5505 test_56wb() {
5506         local file1=$DIR/$tdir/file1
5507         local create_pool=false
5508         local initial_pool=$($LFS getstripe -p $DIR)
5509         local pool_list=()
5510         local pool=""
5511
5512         echo -n "Creating test dir..."
5513         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5514         echo "done."
5515
5516         echo -n "Creating test file..."
5517         touch $file1 || error "cannot create file"
5518         echo "done."
5519
5520         echo -n "Detecting existing pools..."
5521         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
5522
5523         if [ ${#pool_list[@]} -gt 0 ]; then
5524                 echo "${pool_list[@]}"
5525                 for thispool in "${pool_list[@]}"; do
5526                         if [[ -z "$initial_pool" ||
5527                               "$initial_pool" != "$thispool" ]]; then
5528                                 pool="$thispool"
5529                                 echo "Using existing pool '$pool'"
5530                                 break
5531                         fi
5532                 done
5533         else
5534                 echo "none detected."
5535         fi
5536         if [ -z "$pool" ]; then
5537                 pool=${POOL:-testpool}
5538                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
5539                 echo -n "Creating pool '$pool'..."
5540                 create_pool=true
5541                 pool_add $pool &> /dev/null ||
5542                         error "pool_add failed"
5543                 echo "done."
5544
5545                 echo -n "Adding target to pool..."
5546                 pool_add_targets $pool 0 0 1 &> /dev/null ||
5547                         error "pool_add_targets failed"
5548                 echo "done."
5549         fi
5550
5551         echo -n "Setting pool using -p option..."
5552         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
5553                 error "migrate failed rc = $?"
5554         echo "done."
5555
5556         echo -n "Verifying test file is in pool after migrating..."
5557         [ "$($LFS getstripe -p $file1)" = $pool ] ||
5558                 error "file was not migrated to pool $pool"
5559         echo "done."
5560
5561         echo -n "Removing test file from pool '$pool'..."
5562         $LFS migrate $file1 &> /dev/null ||
5563                 error "cannot remove from pool"
5564         [ "$($LFS getstripe -p $file1)" ] &&
5565                 error "pool still set"
5566         echo "done."
5567
5568         echo -n "Setting pool using --pool option..."
5569         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
5570                 error "migrate failed rc = $?"
5571         echo "done."
5572
5573         # Clean up
5574         rm -f $file1
5575         if $create_pool; then
5576                 destroy_test_pools 2> /dev/null ||
5577                         error "destroy test pools failed"
5578         fi
5579 }
5580 run_test 56wb "check lfs_migrate pool support"
5581
5582 test_56wc() {
5583         local file1="$DIR/$tdir/file1"
5584
5585         echo -n "Creating test dir..."
5586         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5587         local def_stripe_size=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
5588         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
5589                 error "cannot set stripe"
5590         echo "done"
5591
5592         echo -n "Setting initial stripe for test file..."
5593         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
5594                 error "cannot set stripe"
5595         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
5596                 error "stripe size not set"
5597         echo "done."
5598
5599         # File currently set to -S 512K -c 1
5600
5601         # Ensure -c and -S options are rejected when -R is set
5602         echo -n "Verifying incompatible options are detected..."
5603         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
5604                 error "incompatible -c and -R options not detected"
5605         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
5606                 error "incompatible -S and -R options not detected"
5607         echo "done."
5608
5609         # Ensure unrecognized options are passed through to 'lfs migrate'
5610         echo -n "Verifying -S option is passed through to lfs migrate..."
5611         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
5612                 error "migration failed"
5613         [ $($LFS getstripe -S "$file1") -eq 1048576 ] ||
5614                 error "file was not restriped"
5615         echo "done."
5616
5617         # File currently set to -S 1M -c 1
5618
5619         # Ensure long options are supported
5620         echo -n "Verifying long options supported..."
5621         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
5622                 error "long option without argument not supported"
5623         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
5624                 error "long option with argument not supported"
5625         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
5626                 error "file not restriped with --stripe-size option"
5627         echo "done."
5628
5629         # File currently set to -S 512K -c 1
5630
5631         if [ "$OSTCOUNT" -gt 1 ]; then
5632                 echo -n "Verifying explicit stripe count can be set..."
5633                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
5634                         error "migrate failed"
5635                 [ $($LFS getstripe -c "$file1") -eq 2 ] ||
5636                         error "file not restriped to explicit count"
5637                 echo "done."
5638         fi
5639
5640         # File currently set to -S 512K -c 1 or -S 512K -c 2
5641
5642         # Ensure parent striping is used if -R is set, and no stripe
5643         # count or size is specified
5644         echo -n "Setting stripe for parent directory..."
5645         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
5646                 error "cannot set stripe"
5647         echo "done."
5648
5649         echo -n "Verifying restripe option uses parent stripe settings..."
5650         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
5651                 error "migrate failed"
5652         [ $($LFS getstripe -S "$file1") -eq $def_stripe_size ] ||
5653                 error "file not restriped to parent settings"
5654         [ $($LFS getstripe -c "$file1") -eq 1 ] ||
5655                 error "file not restriped to parent settings"
5656         echo "done."
5657
5658         # File currently set to -S 1M -c 1
5659
5660         # Ensure striping is preserved if -R is not set, and no stripe
5661         # count or size is specified
5662         echo -n "Verifying striping size preserved when not specified..."
5663         local orig_stripe_size=$($LFS getstripe -S "$file1" 2>/dev/null)
5664         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
5665                 error "cannot set stripe on parent directory"
5666         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5667                 error "migrate failed"
5668         [ $($LFS getstripe -S "$file1") -eq $orig_stripe_size ] ||
5669                 error "file was restriped"
5670         echo "done."
5671
5672         # Ensure file name properly detected when final option has no argument
5673         echo -n "Verifying file name properly detected..."
5674         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5675                 error "file name interpreted as option argument"
5676         echo "done."
5677
5678         # Clean up
5679         rm -f "$file1"
5680 }
5681 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
5682
5683 test_56wd() {
5684         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5685
5686         local file1=$DIR/$tdir/file1
5687
5688         echo -n "Creating test dir..."
5689         test_mkdir $DIR/$tdir || error "cannot create dir"
5690         echo "done."
5691
5692         echo -n "Creating test file..."
5693         touch $file1
5694         echo "done."
5695
5696         # Ensure 'lfs migrate' will fail by using a non-existent option,
5697         # and make sure rsync is not called to recover
5698         echo -n "Make sure --no-rsync option works..."
5699         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
5700                 grep -q 'refusing to fall back to rsync' ||
5701                 error "rsync was called with --no-rsync set"
5702         echo "done."
5703
5704         # Ensure rsync is called without trying 'lfs migrate' first
5705         echo -n "Make sure --rsync option works..."
5706         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
5707                 grep -q 'falling back to rsync' &&
5708                 error "lfs migrate was called with --rsync set"
5709         echo "done."
5710
5711         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
5712         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
5713                 grep -q 'at the same time' ||
5714                 error "--rsync and --no-rsync accepted concurrently"
5715         echo "done."
5716
5717         # Clean up
5718         rm -f $file1
5719 }
5720 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
5721
5722 test_56x() {
5723         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5724         check_swap_layouts_support
5725
5726         local dir=$DIR/$tdir
5727         local ref1=/etc/passwd
5728         local file1=$dir/file1
5729
5730         test_mkdir $dir || error "creating dir $dir"
5731         $LFS setstripe -c 2 $file1
5732         cp $ref1 $file1
5733         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
5734         stripe=$($LFS getstripe -c $file1)
5735         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5736         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5737
5738         # clean up
5739         rm -f $file1
5740 }
5741 run_test 56x "lfs migration support"
5742
5743 test_56xa() {
5744         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5745         check_swap_layouts_support
5746
5747         local dir=$DIR/$tdir/$testnum
5748
5749         test_mkdir -p $dir
5750
5751         local ref1=/etc/passwd
5752         local file1=$dir/file1
5753
5754         $LFS setstripe -c 2 $file1
5755         cp $ref1 $file1
5756         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
5757
5758         local stripe=$($LFS getstripe -c $file1)
5759
5760         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5761         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5762
5763         # clean up
5764         rm -f $file1
5765 }
5766 run_test 56xa "lfs migration --block support"
5767
5768 check_migrate_links() {
5769         local dir="$1"
5770         local file1="$dir/file1"
5771         local begin="$2"
5772         local count="$3"
5773         local total_count=$(($begin + $count - 1))
5774         local symlink_count=10
5775         local uniq_count=10
5776
5777         if [ ! -f "$file1" ]; then
5778                 echo -n "creating initial file..."
5779                 $LFS setstripe -c 1 -S "512k" "$file1" ||
5780                         error "cannot setstripe initial file"
5781                 echo "done"
5782
5783                 echo -n "creating symlinks..."
5784                 for s in $(seq 1 $symlink_count); do
5785                         ln -s "$file1" "$dir/slink$s" ||
5786                                 error "cannot create symlinks"
5787                 done
5788                 echo "done"
5789
5790                 echo -n "creating nonlinked files..."
5791                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
5792                         error "cannot create nonlinked files"
5793                 echo "done"
5794         fi
5795
5796         # create hard links
5797         if [ ! -f "$dir/file$total_count" ]; then
5798                 echo -n "creating hard links $begin:$total_count..."
5799                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
5800                         /dev/null || error "cannot create hard links"
5801                 echo "done"
5802         fi
5803
5804         echo -n "checking number of hard links listed in xattrs..."
5805         local fid=$($LFS getstripe -F "$file1")
5806         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
5807
5808         echo "${#paths[*]}"
5809         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
5810                         skip "hard link list has unexpected size, skipping test"
5811         fi
5812         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
5813                         error "link names should exceed xattrs size"
5814         fi
5815
5816         echo -n "migrating files..."
5817         local migrate_out=$($LFS_MIGRATE -y -S '1m' $dir)
5818         local rc=$?
5819         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
5820         echo "done"
5821
5822         # make sure all links have been properly migrated
5823         echo -n "verifying files..."
5824         fid=$($LFS getstripe -F "$file1") ||
5825                 error "cannot get fid for file $file1"
5826         for i in $(seq 2 $total_count); do
5827                 local fid2=$($LFS getstripe -F $dir/file$i)
5828
5829                 [ "$fid2" == "$fid" ] ||
5830                         error "migrated hard link has mismatched FID"
5831         done
5832
5833         # make sure hard links were properly detected, and migration was
5834         # performed only once for the entire link set; nonlinked files should
5835         # also be migrated
5836         local actual=$(grep -c 'done migrate' <<< "$migrate_out")
5837         local expected=$(($uniq_count + 1))
5838
5839         [ "$actual" -eq  "$expected" ] ||
5840                 error "hard links individually migrated ($actual != $expected)"
5841
5842         # make sure the correct number of hard links are present
5843         local hardlinks=$(stat -c '%h' "$file1")
5844
5845         [ $hardlinks -eq $total_count ] ||
5846                 error "num hard links $hardlinks != $total_count"
5847         echo "done"
5848
5849         return 0
5850 }
5851
5852 test_56xb() {
5853         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
5854                 skip "Need MDS version at least 2.10.55"
5855
5856         local dir="$DIR/$tdir"
5857
5858         test_mkdir "$dir" || error "cannot create dir $dir"
5859
5860         echo "testing lfs migrate mode when all links fit within xattrs"
5861         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir" 2 99
5862
5863         echo "testing rsync mode when all links fit within xattrs"
5864         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir" 2 99
5865
5866         echo "testing lfs migrate mode when all links do not fit within xattrs"
5867         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir" 101 100
5868
5869         echo "testing rsync mode when all links do not fit within xattrs"
5870         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir" 101 100
5871
5872         # clean up
5873         rm -rf $dir
5874 }
5875 run_test 56xb "lfs migration hard link support"
5876
5877 test_56y() {
5878         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
5879                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
5880
5881         local res=""
5882         local dir=$DIR/$tdir
5883         local f1=$dir/file1
5884         local f2=$dir/file2
5885
5886         test_mkdir -p $dir || error "creating dir $dir"
5887         touch $f1 || error "creating std file $f1"
5888         $MULTIOP $f2 H2c || error "creating released file $f2"
5889
5890         # a directory can be raid0, so ask only for files
5891         res=$($LFS find $dir -L raid0 -type f | wc -l)
5892         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
5893
5894         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
5895         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
5896
5897         # only files can be released, so no need to force file search
5898         res=$($LFS find $dir -L released)
5899         [[ $res == $f2 ]] || error "search released: found $res != $f2"
5900
5901         res=$($LFS find $dir -type f \! -L released)
5902         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
5903 }
5904 run_test 56y "lfs find -L raid0|released"
5905
5906 test_56z() { # LU-4824
5907         # This checks to make sure 'lfs find' continues after errors
5908         # There are two classes of errors that should be caught:
5909         # - If multiple paths are provided, all should be searched even if one
5910         #   errors out
5911         # - If errors are encountered during the search, it should not terminate
5912         #   early
5913         local dir=$DIR/$tdir
5914         local i
5915
5916         test_mkdir $dir
5917         for i in d{0..9}; do
5918                 test_mkdir $dir/$i
5919         done
5920         touch $dir/d{0..9}/$tfile
5921         $LFS find $DIR/non_existent_dir $dir &&
5922                 error "$LFS find did not return an error"
5923         # Make a directory unsearchable. This should NOT be the last entry in
5924         # directory order.  Arbitrarily pick the 6th entry
5925         chmod 700 $($LFS find $dir -type d | sed '6!d')
5926
5927         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
5928
5929         # The user should be able to see 10 directories and 9 files
5930         [ $count == 19 ] || error "$LFS find did not continue after error"
5931 }
5932 run_test 56z "lfs find should continue after an error"
5933
5934 test_56aa() { # LU-5937
5935         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
5936
5937         local dir=$DIR/$tdir
5938
5939         mkdir $dir
5940         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
5941
5942         createmany -o $dir/striped_dir/${tfile}- 1024
5943         local dirs=$($LFS find --size +8k $dir/)
5944
5945         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5946 }
5947 run_test 56aa "lfs find --size under striped dir"
5948
5949 test_56ab() { # LU-10705
5950         test_mkdir $DIR/$tdir
5951         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
5952         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
5953         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
5954         # Flush writes to ensure valid blocks.  Need to be more thorough for
5955         # ZFS, since blocks are not allocated/returned to client immediately.
5956         sync_all_data
5957         wait_zfs_commit ost1 2
5958         cancel_lru_locks osc
5959         ls -ls $DIR/$tdir
5960
5961         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
5962
5963         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
5964
5965         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
5966         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
5967
5968         rm -f $DIR/$tdir/$tfile.[123]
5969 }
5970 run_test 56ab "lfs find --blocks"
5971
5972 test_56ba() {
5973         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
5974                 skip "Need MDS version at least 2.10.50"
5975
5976         # Create composite files with one component
5977         local dir=$DIR/$tdir
5978
5979         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
5980         # Create composite files with three components
5981         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
5982         # Create non-composite files
5983         createmany -o $dir/${tfile}- 10
5984
5985         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
5986
5987         [[ $nfiles == 10 ]] ||
5988                 error "lfs find -E 1M found $nfiles != 10 files"
5989
5990         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
5991         [[ $nfiles == 25 ]] ||
5992                 error "lfs find ! -E 1M found $nfiles != 25 files"
5993
5994         # All files have a component that starts at 0
5995         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
5996         [[ $nfiles == 35 ]] ||
5997                 error "lfs find --component-start 0 - $nfiles != 35 files"
5998
5999         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
6000         [[ $nfiles == 15 ]] ||
6001                 error "lfs find --component-start 2M - $nfiles != 15 files"
6002
6003         # All files created here have a componenet that does not starts at 2M
6004         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
6005         [[ $nfiles == 35 ]] ||
6006                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
6007
6008         # Find files with a specified number of components
6009         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
6010         [[ $nfiles == 15 ]] ||
6011                 error "lfs find --component-count 3 - $nfiles != 15 files"
6012
6013         # Remember non-composite files have a component count of zero
6014         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
6015         [[ $nfiles == 10 ]] ||
6016                 error "lfs find --component-count 0 - $nfiles != 10 files"
6017
6018         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
6019         [[ $nfiles == 20 ]] ||
6020                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
6021
6022         # All files have a flag called "init"
6023         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
6024         [[ $nfiles == 35 ]] ||
6025                 error "lfs find --component-flags init - $nfiles != 35 files"
6026
6027         # Multi-component files will have a component not initialized
6028         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
6029         [[ $nfiles == 15 ]] ||
6030                 error "lfs find !--component-flags init - $nfiles != 15 files"
6031
6032         rm -rf $dir
6033
6034 }
6035 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
6036
6037 test_56ca() {
6038         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
6039                 skip "Need MDS version at least 2.10.57"
6040
6041         local td=$DIR/$tdir
6042         local tf=$td/$tfile
6043         local dir
6044         local nfiles
6045         local cmd
6046         local i
6047         local j
6048
6049         # create mirrored directories and mirrored files
6050         mkdir $td || error "mkdir $td failed"
6051         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
6052         createmany -o $tf- 10 || error "create $tf- failed"
6053
6054         for i in $(seq 2); do
6055                 dir=$td/dir$i
6056                 mkdir $dir || error "mkdir $dir failed"
6057                 $LFS mirror create -N$((3 + i)) $dir ||
6058                         error "create mirrored dir $dir failed"
6059                 createmany -o $dir/$tfile- 10 ||
6060                         error "create $dir/$tfile- failed"
6061         done
6062
6063         # change the states of some mirrored files
6064         echo foo > $tf-6
6065         for i in $(seq 2); do
6066                 dir=$td/dir$i
6067                 for j in $(seq 4 9); do
6068                         echo foo > $dir/$tfile-$j
6069                 done
6070         done
6071
6072         # find mirrored files with specific mirror count
6073         cmd="$LFS find --mirror-count 3 --type f $td"
6074         nfiles=$($cmd | wc -l)
6075         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
6076
6077         cmd="$LFS find ! --mirror-count 3 --type f $td"
6078         nfiles=$($cmd | wc -l)
6079         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
6080
6081         cmd="$LFS find --mirror-count +2 --type f $td"
6082         nfiles=$($cmd | wc -l)
6083         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6084
6085         cmd="$LFS find --mirror-count -6 --type f $td"
6086         nfiles=$($cmd | wc -l)
6087         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6088
6089         # find mirrored files with specific file state
6090         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
6091         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
6092
6093         cmd="$LFS find --mirror-state=ro --type f $td"
6094         nfiles=$($cmd | wc -l)
6095         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
6096
6097         cmd="$LFS find ! --mirror-state=ro --type f $td"
6098         nfiles=$($cmd | wc -l)
6099         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6100
6101         cmd="$LFS find --mirror-state=wp --type f $td"
6102         nfiles=$($cmd | wc -l)
6103         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6104
6105         cmd="$LFS find ! --mirror-state=sp --type f $td"
6106         nfiles=$($cmd | wc -l)
6107         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6108 }
6109 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
6110
6111 test_57a() {
6112         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6113         # note test will not do anything if MDS is not local
6114         if [ "$mds1_FSTYPE" != ldiskfs ]; then
6115                 skip_env "ldiskfs only test"
6116         fi
6117         remote_mds_nodsh && skip "remote MDS with nodsh"
6118
6119         local MNTDEV="osd*.*MDT*.mntdev"
6120         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
6121         [ -z "$DEV" ] && error "can't access $MNTDEV"
6122         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
6123                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
6124                         error "can't access $DEV"
6125                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
6126                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
6127                 rm $TMP/t57a.dump
6128         done
6129 }
6130 run_test 57a "verify MDS filesystem created with large inodes =="
6131
6132 test_57b() {
6133         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6134         if [ "$mds1_FSTYPE" != ldiskfs ]; then
6135                 skip_env "ldiskfs only test"
6136         fi
6137         remote_mds_nodsh && skip "remote MDS with nodsh"
6138
6139         local dir=$DIR/$tdir
6140         local filecount=100
6141         local file1=$dir/f1
6142         local fileN=$dir/f$filecount
6143
6144         rm -rf $dir || error "removing $dir"
6145         test_mkdir -c1 $dir
6146         local mdtidx=$($LFS getstripe -m $dir)
6147         local mdtname=MDT$(printf %04x $mdtidx)
6148         local facet=mds$((mdtidx + 1))
6149
6150         echo "mcreating $filecount files"
6151         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
6152
6153         # verify that files do not have EAs yet
6154         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
6155                 error "$file1 has an EA"
6156         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
6157                 error "$fileN has an EA"
6158
6159         sync
6160         sleep 1
6161         df $dir  #make sure we get new statfs data
6162         local mdsfree=$(do_facet $facet \
6163                         lctl get_param -n osd*.*$mdtname.kbytesfree)
6164         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
6165         local file
6166
6167         echo "opening files to create objects/EAs"
6168         for file in $(seq -f $dir/f%g 1 $filecount); do
6169                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
6170                         error "opening $file"
6171         done
6172
6173         # verify that files have EAs now
6174         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
6175         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
6176
6177         sleep 1  #make sure we get new statfs data
6178         df $dir
6179         local mdsfree2=$(do_facet $facet \
6180                          lctl get_param -n osd*.*$mdtname.kbytesfree)
6181         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
6182
6183         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
6184                 if [ "$mdsfree" != "$mdsfree2" ]; then
6185                         error "MDC before $mdcfree != after $mdcfree2"
6186                 else
6187                         echo "MDC before $mdcfree != after $mdcfree2"
6188                         echo "unable to confirm if MDS has large inodes"
6189                 fi
6190         fi
6191         rm -rf $dir
6192 }
6193 run_test 57b "default LOV EAs are stored inside large inodes ==="
6194
6195 test_58() {
6196         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6197         [ -z "$(which wiretest 2>/dev/null)" ] &&
6198                         skip_env "could not find wiretest"
6199
6200         wiretest
6201 }
6202 run_test 58 "verify cross-platform wire constants =============="
6203
6204 test_59() {
6205         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6206
6207         echo "touch 130 files"
6208         createmany -o $DIR/f59- 130
6209         echo "rm 130 files"
6210         unlinkmany $DIR/f59- 130
6211         sync
6212         # wait for commitment of removal
6213         wait_delete_completed
6214 }
6215 run_test 59 "verify cancellation of llog records async ========="
6216
6217 TEST60_HEAD="test_60 run $RANDOM"
6218 test_60a() {
6219         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6220         remote_mgs_nodsh && skip "remote MGS with nodsh"
6221         do_facet mgs "! which run-llog.sh &> /dev/null" &&
6222                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
6223                         skip_env "missing subtest run-llog.sh"
6224
6225         log "$TEST60_HEAD - from kernel mode"
6226         do_facet mgs "$LCTL dk > /dev/null"
6227         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
6228         do_facet mgs $LCTL dk > $TMP/$tfile
6229
6230         # LU-6388: test llog_reader
6231         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
6232         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
6233         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
6234                         skip_env "missing llog_reader"
6235         local fstype=$(facet_fstype mgs)
6236         [ $fstype != ldiskfs -a $fstype != zfs ] &&
6237                 skip_env "Only for ldiskfs or zfs type mgs"
6238
6239         local mntpt=$(facet_mntpt mgs)
6240         local mgsdev=$(mgsdevname 1)
6241         local fid_list
6242         local fid
6243         local rec_list
6244         local rec
6245         local rec_type
6246         local obj_file
6247         local path
6248         local seq
6249         local oid
6250         local pass=true
6251
6252         #get fid and record list
6253         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
6254                 tail -n 4))
6255         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
6256                 tail -n 4))
6257         #remount mgs as ldiskfs or zfs type
6258         stop mgs || error "stop mgs failed"
6259         mount_fstype mgs || error "remount mgs failed"
6260         for ((i = 0; i < ${#fid_list[@]}; i++)); do
6261                 fid=${fid_list[i]}
6262                 rec=${rec_list[i]}
6263                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
6264                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
6265                 oid=$((16#$oid))
6266
6267                 case $fstype in
6268                         ldiskfs )
6269                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
6270                         zfs )
6271                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
6272                 esac
6273                 echo "obj_file is $obj_file"
6274                 do_facet mgs $llog_reader $obj_file
6275
6276                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
6277                         awk '{ print $3 }' | sed -e "s/^type=//g")
6278                 if [ $rec_type != $rec ]; then
6279                         echo "FAILED test_60a wrong record type $rec_type," \
6280                               "should be $rec"
6281                         pass=false
6282                         break
6283                 fi
6284
6285                 #check obj path if record type is LLOG_LOGID_MAGIC
6286                 if [ "$rec" == "1064553b" ]; then
6287                         path=$(do_facet mgs $llog_reader $obj_file |
6288                                 grep "path=" | awk '{ print $NF }' |
6289                                 sed -e "s/^path=//g")
6290                         if [ $obj_file != $mntpt/$path ]; then
6291                                 echo "FAILED test_60a wrong obj path" \
6292                                       "$montpt/$path, should be $obj_file"
6293                                 pass=false
6294                                 break
6295                         fi
6296                 fi
6297         done
6298         rm -f $TMP/$tfile
6299         #restart mgs before "error", otherwise it will block the next test
6300         stop mgs || error "stop mgs failed"
6301         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
6302         $pass || error "test failed, see FAILED test_60a messages for specifics"
6303 }
6304 run_test 60a "llog_test run from kernel module and test llog_reader"
6305
6306 test_60b() { # bug 6411
6307         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6308
6309         dmesg > $DIR/$tfile
6310         LLOG_COUNT=$(do_facet mgs dmesg |
6311                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
6312                           /llog_[a-z]*.c:[0-9]/ {
6313                                 if (marker)
6314                                         from_marker++
6315                                 from_begin++
6316                           }
6317                           END {
6318                                 if (marker)
6319                                         print from_marker
6320                                 else
6321                                         print from_begin
6322                           }")
6323
6324         [[ $LLOG_COUNT -gt 120 ]] &&
6325                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
6326 }
6327 run_test 60b "limit repeated messages from CERROR/CWARN"
6328
6329 test_60c() {
6330         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6331
6332         echo "create 5000 files"
6333         createmany -o $DIR/f60c- 5000
6334 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
6335         lctl set_param fail_loc=0x80000137
6336         unlinkmany $DIR/f60c- 5000
6337         lctl set_param fail_loc=0
6338 }
6339 run_test 60c "unlink file when mds full"
6340
6341 test_60d() {
6342         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6343
6344         SAVEPRINTK=$(lctl get_param -n printk)
6345         # verify "lctl mark" is even working"
6346         MESSAGE="test message ID $RANDOM $$"
6347         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
6348         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
6349
6350         lctl set_param printk=0 || error "set lnet.printk failed"
6351         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
6352         MESSAGE="new test message ID $RANDOM $$"
6353         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
6354         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
6355         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
6356
6357         lctl set_param -n printk="$SAVEPRINTK"
6358 }
6359 run_test 60d "test printk console message masking"
6360
6361 test_60e() {
6362         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6363         remote_mds_nodsh && skip "remote MDS with nodsh"
6364
6365         touch $DIR/$tfile
6366 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
6367         do_facet mds1 lctl set_param fail_loc=0x15b
6368         rm $DIR/$tfile
6369 }
6370 run_test 60e "no space while new llog is being created"
6371
6372 test_60g() {
6373         local pid
6374
6375         test_mkdir -c $MDSCOUNT $DIR/$tdir
6376         $LFS setdirstripe -D -i -1 -c $MDSCOUNT $DIR/$tdir
6377
6378         (
6379                 local index=0
6380                 while true; do
6381                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
6382                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
6383                         index=$((index + 1))
6384                 done
6385         ) &
6386
6387         pid=$!
6388
6389         for i in $(seq 100); do 
6390                 # define OBD_FAIL_OSD_TXN_START    0x19a
6391                 do_facet mds1 lctl set_param fail_loc=0x8000019a
6392                 usleep 100
6393         done
6394
6395         kill -9 $pid
6396
6397         mkdir $DIR/$tdir/new || error "mkdir failed"
6398         rmdir $DIR/$tdir/new || error "rmdir failed"
6399 }
6400 run_test 60g "transaction abort won't cause MDT hung"
6401
6402 test_61a() {
6403         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6404
6405         f="$DIR/f61"
6406         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
6407         cancel_lru_locks osc
6408         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
6409         sync
6410 }
6411 run_test 61a "mmap() writes don't make sync hang ================"
6412
6413 test_61b() {
6414         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
6415 }
6416 run_test 61b "mmap() of unstriped file is successful"
6417
6418 # bug 2330 - insufficient obd_match error checking causes LBUG
6419 test_62() {
6420         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6421
6422         f="$DIR/f62"
6423         echo foo > $f
6424         cancel_lru_locks osc
6425         lctl set_param fail_loc=0x405
6426         cat $f && error "cat succeeded, expect -EIO"
6427         lctl set_param fail_loc=0
6428 }
6429 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
6430 # match every page all of the time.
6431 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
6432
6433 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
6434 # Though this test is irrelevant anymore, it helped to reveal some
6435 # other grant bugs (LU-4482), let's keep it.
6436 test_63a() {   # was test_63
6437         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6438
6439         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
6440
6441         for i in `seq 10` ; do
6442                 dd if=/dev/zero of=$DIR/f63 bs=8k &
6443                 sleep 5
6444                 kill $!
6445                 sleep 1
6446         done
6447
6448         rm -f $DIR/f63 || true
6449 }
6450 run_test 63a "Verify oig_wait interruption does not crash ======="
6451
6452 # bug 2248 - async write errors didn't return to application on sync
6453 # bug 3677 - async write errors left page locked
6454 test_63b() {
6455         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6456
6457         debugsave
6458         lctl set_param debug=-1
6459
6460         # ensure we have a grant to do async writes
6461         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
6462         rm $DIR/$tfile
6463
6464         sync    # sync lest earlier test intercept the fail_loc
6465
6466         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6467         lctl set_param fail_loc=0x80000406
6468         $MULTIOP $DIR/$tfile Owy && \
6469                 error "sync didn't return ENOMEM"
6470         sync; sleep 2; sync     # do a real sync this time to flush page
6471         lctl get_param -n llite.*.dump_page_cache | grep locked && \
6472                 error "locked page left in cache after async error" || true
6473         debugrestore
6474 }
6475 run_test 63b "async write errors should be returned to fsync ==="
6476
6477 test_64a () {
6478         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6479
6480         df $DIR
6481         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
6482 }
6483 run_test 64a "verify filter grant calculations (in kernel) ====="
6484
6485 test_64b () {
6486         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6487
6488         sh oos.sh $MOUNT || error "oos.sh failed: $?"
6489 }
6490 run_test 64b "check out-of-space detection on client"
6491
6492 test_64c() {
6493         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
6494 }
6495 run_test 64c "verify grant shrink"
6496
6497 # this does exactly what osc_request.c:osc_announce_cached() does in
6498 # order to calculate max amount of grants to ask from server
6499 want_grant() {
6500         local tgt=$1
6501
6502         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
6503         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
6504
6505         ((rpc_in_flight ++));
6506         nrpages=$((nrpages * rpc_in_flight))
6507
6508         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
6509
6510         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
6511
6512         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
6513         local undirty=$((nrpages * PAGE_SIZE))
6514
6515         local max_extent_pages
6516         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
6517             grep grant_max_extent_size | awk '{print $2}')
6518         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
6519         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
6520         local grant_extent_tax
6521         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6522             grep grant_extent_tax | awk '{print $2}')
6523
6524         undirty=$((undirty + nrextents * grant_extent_tax))
6525
6526         echo $undirty
6527 }
6528
6529 # this is size of unit for grant allocation. It should be equal to
6530 # what tgt_grant.c:tgt_grant_chunk() calculates
6531 grant_chunk() {
6532         local tgt=$1
6533         local max_brw_size
6534         local grant_extent_tax
6535
6536         max_brw_size=$($LCTL get_param osc.${tgt}.import |
6537             grep max_brw_size | awk '{print $2}')
6538
6539         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6540             grep grant_extent_tax | awk '{print $2}')
6541
6542         echo $(((max_brw_size + grant_extent_tax) * 2))
6543 }
6544
6545 test_64d() {
6546         [ $OST1_VERSION -lt $(version_code 2.10.56) ] &&
6547                 skip "OST < 2.10.55 doesn't limit grants enough"
6548
6549         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
6550         local file=$DIR/$tfile
6551
6552         [[ $($LCTL get_param osc.${tgt}.import |
6553              grep "connect_flags:.*grant_param") ]] ||
6554                 skip "no grant_param connect flag"
6555
6556         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
6557
6558         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
6559
6560         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
6561         stack_trap "rm -f $file" EXIT
6562
6563         $SETSTRIPE $file -i 0 -c 1
6564         dd if=/dev/zero of=$file bs=1M count=1000 &
6565         ddpid=$!
6566
6567         while true
6568         do
6569                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
6570                 if [[ $cur_grant -gt $max_cur_granted ]]
6571                 then
6572                         kill $ddpid
6573                         error "cur_grant $cur_grant > $max_cur_granted"
6574                 fi
6575                 kill -0 $ddpid
6576                 [[ $? -ne 0 ]] && break;
6577                 sleep 2
6578         done
6579
6580         rm -f $DIR/$tfile
6581         wait_delete_completed
6582         $LCTL set_param debug="$olddebug" 2> /dev/null || true
6583 }
6584 run_test 64d "check grant limit exceed"
6585
6586 # bug 1414 - set/get directories' stripe info
6587 test_65a() {
6588         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6589
6590         test_mkdir $DIR/$tdir
6591         touch $DIR/$tdir/f1
6592         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
6593 }
6594 run_test 65a "directory with no stripe info"
6595
6596 test_65b() {
6597         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6598
6599         test_mkdir $DIR/$tdir
6600         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6601
6602         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6603                                                 error "setstripe"
6604         touch $DIR/$tdir/f2
6605         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
6606 }
6607 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
6608
6609 test_65c() {
6610         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6611         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
6612
6613         test_mkdir $DIR/$tdir
6614         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
6615
6616         $LFS setstripe -S $((stripesize * 4)) -i 1 \
6617                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
6618         touch $DIR/$tdir/f3
6619         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
6620 }
6621 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
6622
6623 test_65d() {
6624         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6625
6626         test_mkdir $DIR/$tdir
6627         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
6628         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6629
6630         if [[ $STRIPECOUNT -le 0 ]]; then
6631                 sc=1
6632         elif [[ $STRIPECOUNT -gt 2000 ]]; then
6633 #LOV_MAX_STRIPE_COUNT is 2000
6634                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
6635         else
6636                 sc=$(($STRIPECOUNT - 1))
6637         fi
6638         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
6639         touch $DIR/$tdir/f4 $DIR/$tdir/f5
6640         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
6641                 error "lverify failed"
6642 }
6643 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
6644
6645 test_65e() {
6646         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6647
6648         test_mkdir $DIR/$tdir
6649
6650         $SETSTRIPE $DIR/$tdir || error "setstripe"
6651         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
6652                                         error "no stripe info failed"
6653         touch $DIR/$tdir/f6
6654         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
6655 }
6656 run_test 65e "directory setstripe defaults"
6657
6658 test_65f() {
6659         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6660
6661         test_mkdir $DIR/${tdir}f
6662         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
6663 }
6664 run_test 65f "dir setstripe permission (should return error) ==="
6665
6666 test_65g() {
6667         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6668
6669         test_mkdir $DIR/$tdir
6670         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6671
6672         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6673                 error "setstripe -S failed"
6674         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
6675         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
6676                 error "delete default stripe failed"
6677 }
6678 run_test 65g "directory setstripe -d"
6679
6680 test_65h() {
6681         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6682
6683         test_mkdir $DIR/$tdir
6684         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6685
6686         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6687                 error "setstripe -S failed"
6688         test_mkdir $DIR/$tdir/dd1
6689         [ $($LFS getstripe -c $DIR/$tdir) = $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
6690                 error "stripe info inherit failed"
6691 }
6692 run_test 65h "directory stripe info inherit ===================="
6693
6694 test_65i() {
6695         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6696
6697         save_layout_restore_at_exit $MOUNT
6698
6699         # bug6367: set non-default striping on root directory
6700         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
6701
6702         # bug12836: getstripe on -1 default directory striping
6703         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
6704
6705         # bug12836: getstripe -v on -1 default directory striping
6706         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
6707
6708         # bug12836: new find on -1 default directory striping
6709         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
6710 }
6711 run_test 65i "various tests to set root directory striping"
6712
6713 test_65j() { # bug6367
6714         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6715
6716         sync; sleep 1
6717
6718         # if we aren't already remounting for each test, do so for this test
6719         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
6720                 cleanup || error "failed to unmount"
6721                 setup
6722         fi
6723
6724         save_layout_restore_at_exit $MOUNT
6725
6726         $SETSTRIPE -d $MOUNT || error "setstripe failed"
6727 }
6728 run_test 65j "set default striping on root directory (bug 6367)="
6729
6730 cleanup_65k() {
6731         rm -rf $DIR/$tdir
6732         wait_delete_completed
6733         do_facet $SINGLEMDS "lctl set_param -n \
6734                 osp.$ost*MDT0000.max_create_count=$max_count"
6735         do_facet $SINGLEMDS "lctl set_param -n \
6736                 osp.$ost*MDT0000.create_count=$count"
6737         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
6738         echo $INACTIVE_OSC "is Activate"
6739
6740         wait_osc_import_state mds ost$ostnum FULL
6741 }
6742
6743 test_65k() { # bug11679
6744         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6745         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6746         remote_mds_nodsh && skip "remote MDS with nodsh"
6747
6748         local disable_precreate=true
6749         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
6750                 disable_precreate=false
6751
6752         echo "Check OST status: "
6753         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
6754                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
6755
6756         for OSC in $MDS_OSCS; do
6757                 echo $OSC "is active"
6758                 do_facet $SINGLEMDS lctl --device %$OSC activate
6759         done
6760
6761         for INACTIVE_OSC in $MDS_OSCS; do
6762                 local ost=$(osc_to_ost $INACTIVE_OSC)
6763                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
6764                                lov.*md*.target_obd |
6765                                awk -F: /$ost/'{ print $1 }' | head -n 1)
6766
6767                 mkdir -p $DIR/$tdir
6768                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
6769                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
6770
6771                 echo "Deactivate: " $INACTIVE_OSC
6772                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
6773
6774                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
6775                               osp.$ost*MDT0000.create_count")
6776                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
6777                                   osp.$ost*MDT0000.max_create_count")
6778                 $disable_precreate &&
6779                         do_facet $SINGLEMDS "lctl set_param -n \
6780                                 osp.$ost*MDT0000.max_create_count=0"
6781
6782                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
6783                         [ -f $DIR/$tdir/$idx ] && continue
6784                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
6785                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
6786                                 { cleanup_65k;
6787                                   error "setstripe $idx should succeed"; }
6788                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
6789                 done
6790                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
6791                 rmdir $DIR/$tdir
6792
6793                 do_facet $SINGLEMDS "lctl set_param -n \
6794                         osp.$ost*MDT0000.max_create_count=$max_count"
6795                 do_facet $SINGLEMDS "lctl set_param -n \
6796                         osp.$ost*MDT0000.create_count=$count"
6797                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
6798                 echo $INACTIVE_OSC "is Activate"
6799
6800                 wait_osc_import_state mds ost$ostnum FULL
6801         done
6802 }
6803 run_test 65k "validate manual striping works properly with deactivated OSCs"
6804
6805 test_65l() { # bug 12836
6806         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6807
6808         test_mkdir -p $DIR/$tdir/test_dir
6809         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
6810         $LFS find -mtime -1 $DIR/$tdir >/dev/null
6811 }
6812 run_test 65l "lfs find on -1 stripe dir ========================"
6813
6814 test_65m() {
6815         local layout=$(save_layout $MOUNT)
6816         $RUNAS $SETSTRIPE -c 2 $MOUNT && {
6817                 restore_layout $MOUNT $layout
6818                 error "setstripe should fail by non-root users"
6819         }
6820         true
6821 }
6822 run_test 65m "normal user can't set filesystem default stripe"
6823
6824 test_65n() {
6825         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.12.0) ]] ||
6826                 skip "Need MDS version at least 2.12.0"
6827         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
6828
6829         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
6830         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
6831         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
6832
6833         local root_layout=$(save_layout $MOUNT)
6834         stack_trap "restore_layout $MOUNT $root_layout" EXIT
6835
6836         # new subdirectory under root directory should not inherit
6837         # the default layout from root
6838         local dir1=$MOUNT/$tdir-1
6839         mkdir $dir1 || error "mkdir $dir1 failed"
6840         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
6841                 error "$dir1 shouldn't have LOV EA"
6842
6843         # delete the default layout on root directory
6844         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
6845
6846         local dir2=$MOUNT/$tdir-2
6847         mkdir $dir2 || error "mkdir $dir2 failed"
6848         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
6849                 error "$dir2 shouldn't have LOV EA"
6850
6851         # set a new striping pattern on root directory
6852         local def_stripe_size=$($LFS getstripe -S $MOUNT)
6853         local new_def_stripe_size=$((def_stripe_size * 2))
6854         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
6855                 error "set stripe size on $MOUNT failed"
6856
6857         # new file created in $dir2 should inherit the new stripe size from
6858         # the filesystem default
6859         local file2=$dir2/$tfile-2
6860         touch $file2 || error "touch $file2 failed"
6861
6862         local file2_stripe_size=$($LFS getstripe -S $file2)
6863         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
6864                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
6865
6866         local dir3=$MOUNT/$tdir-3
6867         mkdir $dir3 || error "mkdir $dir3 failed"
6868         ! getfattr -n trusted.lov $dir3 &> /dev/null ||
6869                 error "$dir3 shouldn't have LOV EA"
6870
6871         # set OST pool on root directory
6872         local pool=$TESTNAME
6873         pool_add $pool || error "add $pool failed"
6874         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
6875                 error "add targets to $pool failed"
6876
6877         $LFS setstripe -p $pool $MOUNT ||
6878                 error "set OST pool on $MOUNT failed"
6879
6880         # new file created in $dir3 should inherit the pool from
6881         # the filesystem default
6882         local file3=$dir3/$tfile-3
6883         touch $file3 || error "touch $file3 failed"
6884
6885         local file3_pool=$($LFS getstripe -p $file3)
6886         [[ "$file3_pool" = "$pool" ]] ||
6887                 error "$file3 didn't inherit OST pool $pool"
6888
6889         local dir4=$MOUNT/$tdir-4
6890         mkdir $dir4 || error "mkdir $dir4 failed"
6891         ! getfattr -n trusted.lov $dir4 &> /dev/null ||
6892                 error "$dir4 shouldn't have LOV EA"
6893
6894         # new file created in $dir4 should inherit the pool from
6895         # the filesystem default
6896         local file4=$dir4/$tfile-4
6897         touch $file4 || error "touch $file4 failed"
6898
6899         local file4_pool=$($LFS getstripe -p $file4)
6900         [[ "$file4_pool" = "$pool" ]] ||
6901                 error "$file4 didn't inherit OST pool $pool"
6902
6903         # new subdirectory under non-root directory should inherit
6904         # the default layout from its parent directory
6905         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
6906                 error "set directory layout on $dir4 failed"
6907
6908         local dir5=$dir4/$tdir-5
6909         mkdir $dir5 || error "mkdir $dir5 failed"
6910
6911         local dir4_layout=$(get_layout_param $dir4)
6912         local dir5_layout=$(get_layout_param $dir5)
6913         [[ "$dir4_layout" = "$dir5_layout" ]] ||
6914                 error "$dir5 should inherit the default layout from $dir4"
6915 }
6916 run_test 65n "don't inherit default layout from root for new subdirectories"
6917
6918 # bug 2543 - update blocks count on client
6919 test_66() {
6920         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6921
6922         COUNT=${COUNT:-8}
6923         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
6924         sync; sync_all_data; sync; sync_all_data
6925         cancel_lru_locks osc
6926         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
6927         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
6928 }
6929 run_test 66 "update inode blocks count on client ==============="
6930
6931 meminfo() {
6932         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
6933 }
6934
6935 swap_used() {
6936         swapon -s | awk '($1 == "'$1'") { print $4 }'
6937 }
6938
6939 # bug5265, obdfilter oa2dentry return -ENOENT
6940 # #define OBD_FAIL_SRV_ENOENT 0x217
6941 test_69() {
6942         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6943         remote_ost_nodsh && skip "remote OST with nodsh"
6944
6945         f="$DIR/$tfile"
6946         $SETSTRIPE -c 1 -i 0 $f
6947
6948         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
6949
6950         do_facet ost1 lctl set_param fail_loc=0x217
6951         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
6952         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
6953
6954         do_facet ost1 lctl set_param fail_loc=0
6955         $DIRECTIO write $f 0 2 || error "write error"
6956
6957         cancel_lru_locks osc
6958         $DIRECTIO read $f 0 1 || error "read error"
6959
6960         do_facet ost1 lctl set_param fail_loc=0x217
6961         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
6962
6963         do_facet ost1 lctl set_param fail_loc=0
6964         rm -f $f
6965 }
6966 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
6967
6968 test_71() {
6969         test_mkdir $DIR/$tdir
6970         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
6971         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
6972 }
6973 run_test 71 "Running dbench on lustre (don't segment fault) ===="
6974
6975 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
6976         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6977         [ "$RUNAS_ID" = "$UID" ] &&
6978                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6979         # Check that testing environment is properly set up. Skip if not
6980         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
6981                 skip_env "User $RUNAS_ID does not exist - skipping"
6982
6983         touch $DIR/$tfile
6984         chmod 777 $DIR/$tfile
6985         chmod ug+s $DIR/$tfile
6986         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
6987                 error "$RUNAS dd $DIR/$tfile failed"
6988         # See if we are still setuid/sgid
6989         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6990                 error "S/gid is not dropped on write"
6991         # Now test that MDS is updated too
6992         cancel_lru_locks mdc
6993         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6994                 error "S/gid is not dropped on MDS"
6995         rm -f $DIR/$tfile
6996 }
6997 run_test 72a "Test that remove suid works properly (bug5695) ===="
6998
6999 test_72b() { # bug 24226 -- keep mode setting when size is not changing
7000         local perm
7001
7002         [ "$RUNAS_ID" = "$UID" ] &&
7003                 skip_env "RUNAS_ID = UID = $UID -- skipping"
7004         [ "$RUNAS_ID" -eq 0 ] &&
7005                 skip_env "RUNAS_ID = 0 -- skipping"
7006         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7007         # Check that testing environment is properly set up. Skip if not
7008         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
7009                 skip_env "User $RUNAS_ID does not exist - skipping"
7010
7011         touch $DIR/${tfile}-f{g,u}
7012         test_mkdir $DIR/${tfile}-dg
7013         test_mkdir $DIR/${tfile}-du
7014         chmod 770 $DIR/${tfile}-{f,d}{g,u}
7015         chmod g+s $DIR/${tfile}-{f,d}g
7016         chmod u+s $DIR/${tfile}-{f,d}u
7017         for perm in 777 2777 4777; do
7018                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
7019                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
7020                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
7021                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
7022         done
7023         true
7024 }
7025 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
7026
7027 # bug 3462 - multiple simultaneous MDC requests
7028 test_73() {
7029         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7030
7031         test_mkdir $DIR/d73-1
7032         test_mkdir $DIR/d73-2
7033         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
7034         pid1=$!
7035
7036         lctl set_param fail_loc=0x80000129
7037         $MULTIOP $DIR/d73-1/f73-2 Oc &
7038         sleep 1
7039         lctl set_param fail_loc=0
7040
7041         $MULTIOP $DIR/d73-2/f73-3 Oc &
7042         pid3=$!
7043
7044         kill -USR1 $pid1
7045         wait $pid1 || return 1
7046
7047         sleep 25
7048
7049         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
7050         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
7051         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
7052
7053         rm -rf $DIR/d73-*
7054 }
7055 run_test 73 "multiple MDC requests (should not deadlock)"
7056
7057 test_74a() { # bug 6149, 6184
7058         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7059
7060         touch $DIR/f74a
7061         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
7062         #
7063         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
7064         # will spin in a tight reconnection loop
7065         $LCTL set_param fail_loc=0x8000030e
7066         # get any lock that won't be difficult - lookup works.
7067         ls $DIR/f74a
7068         $LCTL set_param fail_loc=0
7069         rm -f $DIR/f74a
7070         true
7071 }
7072 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
7073
7074 test_74b() { # bug 13310
7075         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7076
7077         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
7078         #
7079         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
7080         # will spin in a tight reconnection loop
7081         $LCTL set_param fail_loc=0x8000030e
7082         # get a "difficult" lock
7083         touch $DIR/f74b
7084         $LCTL set_param fail_loc=0
7085         rm -f $DIR/f74b
7086         true
7087 }
7088 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
7089
7090 test_74c() {
7091         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7092
7093         #define OBD_FAIL_LDLM_NEW_LOCK
7094         $LCTL set_param fail_loc=0x319
7095         touch $DIR/$tfile && error "touch successful"
7096         $LCTL set_param fail_loc=0
7097         true
7098 }
7099 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
7100
7101 num_inodes() {
7102         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
7103 }
7104
7105 test_76() { # Now for bug 20433, added originally in bug 1443
7106         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7107
7108         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
7109
7110         cancel_lru_locks osc
7111         BEFORE_INODES=$(num_inodes)
7112         echo "before inodes: $BEFORE_INODES"
7113         local COUNT=1000
7114         [ "$SLOW" = "no" ] && COUNT=100
7115         for i in $(seq $COUNT); do
7116                 touch $DIR/$tfile
7117                 rm -f $DIR/$tfile
7118         done
7119         cancel_lru_locks osc
7120         AFTER_INODES=$(num_inodes)
7121         echo "after inodes: $AFTER_INODES"
7122         local wait=0
7123         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
7124                 sleep 2
7125                 AFTER_INODES=$(num_inodes)
7126                 wait=$((wait+2))
7127                 echo "wait $wait seconds inodes: $AFTER_INODES"
7128                 if [ $wait -gt 30 ]; then
7129                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
7130                 fi
7131         done
7132 }
7133 run_test 76 "confirm clients recycle inodes properly ===="
7134
7135
7136 export ORIG_CSUM=""
7137 set_checksums()
7138 {
7139         # Note: in sptlrpc modes which enable its own bulk checksum, the
7140         # original crc32_le bulk checksum will be automatically disabled,
7141         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
7142         # will be checked by sptlrpc code against sptlrpc bulk checksum.
7143         # In this case set_checksums() will not be no-op, because sptlrpc
7144         # bulk checksum will be enabled all through the test.
7145
7146         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
7147         lctl set_param -n osc.*.checksums $1
7148         return 0
7149 }
7150
7151 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
7152                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
7153 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
7154                              tr -d [] | head -n1)}
7155 set_checksum_type()
7156 {
7157         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
7158         log "set checksum type to $1"
7159         return 0
7160 }
7161 F77_TMP=$TMP/f77-temp
7162 F77SZ=8
7163 setup_f77() {
7164         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
7165                 error "error writing to $F77_TMP"
7166 }
7167
7168 test_77a() { # bug 10889
7169         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7170         $GSS && skip_env "could not run with gss"
7171
7172         [ ! -f $F77_TMP ] && setup_f77
7173         set_checksums 1
7174         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
7175         set_checksums 0
7176         rm -f $DIR/$tfile
7177 }
7178 run_test 77a "normal checksum read/write operation"
7179
7180 test_77b() { # bug 10889
7181         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7182         $GSS && skip_env "could not run with gss"
7183
7184         [ ! -f $F77_TMP ] && setup_f77
7185         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7186         $LCTL set_param fail_loc=0x80000409
7187         set_checksums 1
7188
7189         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7190                 error "dd error: $?"
7191         $LCTL set_param fail_loc=0
7192
7193         for algo in $CKSUM_TYPES; do
7194                 cancel_lru_locks osc
7195                 set_checksum_type $algo
7196                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7197                 $LCTL set_param fail_loc=0x80000408
7198                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
7199                 $LCTL set_param fail_loc=0
7200         done
7201         set_checksums 0
7202         set_checksum_type $ORIG_CSUM_TYPE
7203         rm -f $DIR/$tfile
7204 }
7205 run_test 77b "checksum error on client write, read"
7206
7207 cleanup_77c() {
7208         trap 0
7209         set_checksums 0
7210         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
7211         $check_ost &&
7212                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
7213         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
7214         $check_ost && [ -n "$ost_file_prefix" ] &&
7215                 do_facet ost1 rm -f ${ost_file_prefix}\*
7216 }
7217
7218 test_77c() {
7219         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7220         $GSS && skip_env "could not run with gss"
7221         remote_ost_nodsh && skip "remote OST with nodsh"
7222
7223         local bad1
7224         local osc_file_prefix
7225         local osc_file
7226         local check_ost=false
7227         local ost_file_prefix
7228         local ost_file
7229         local orig_cksum
7230         local dump_cksum
7231         local fid
7232
7233         # ensure corruption will occur on first OSS/OST
7234         $LFS setstripe -i 0 $DIR/$tfile
7235
7236         [ ! -f $F77_TMP ] && setup_f77
7237         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7238                 error "dd write error: $?"
7239         fid=$($LFS path2fid $DIR/$tfile)
7240
7241         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
7242         then
7243                 check_ost=true
7244                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
7245                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
7246         else
7247                 echo "OSS do not support bulk pages dump upon error"
7248         fi
7249
7250         osc_file_prefix=$($LCTL get_param -n debug_path)
7251         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
7252
7253         trap cleanup_77c EXIT
7254
7255         set_checksums 1
7256         # enable bulk pages dump upon error on Client
7257         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
7258         # enable bulk pages dump upon error on OSS
7259         $check_ost &&
7260                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
7261
7262         # flush Client cache to allow next read to reach OSS
7263         cancel_lru_locks osc
7264
7265         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
7266         $LCTL set_param fail_loc=0x80000408
7267         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
7268         $LCTL set_param fail_loc=0
7269
7270         rm -f $DIR/$tfile
7271
7272         # check cksum dump on Client
7273         osc_file=$(ls ${osc_file_prefix}*)
7274         [ -n "$osc_file" ] || error "no checksum dump file on Client"
7275         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
7276         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
7277         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
7278         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
7279                      cksum)
7280         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
7281         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7282                 error "dump content does not match on Client"
7283
7284         $check_ost || skip "No need to check cksum dump on OSS"
7285
7286         # check cksum dump on OSS
7287         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
7288         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
7289         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
7290         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
7291         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7292                 error "dump content does not match on OSS"
7293
7294         cleanup_77c
7295 }
7296 run_test 77c "checksum error on client read with debug"
7297
7298 test_77d() { # bug 10889
7299         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7300         $GSS && skip_env "could not run with gss"
7301
7302         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7303         $LCTL set_param fail_loc=0x80000409
7304         set_checksums 1
7305         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
7306                 error "direct write: rc=$?"
7307         $LCTL set_param fail_loc=0
7308         set_checksums 0
7309
7310         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7311         $LCTL set_param fail_loc=0x80000408
7312         set_checksums 1
7313         cancel_lru_locks osc
7314         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
7315                 error "direct read: rc=$?"
7316         $LCTL set_param fail_loc=0
7317         set_checksums 0
7318 }
7319 run_test 77d "checksum error on OST direct write, read"
7320
7321 test_77f() { # bug 10889
7322         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7323         $GSS && skip_env "could not run with gss"
7324
7325         set_checksums 1
7326         for algo in $CKSUM_TYPES; do
7327                 cancel_lru_locks osc
7328                 set_checksum_type $algo
7329                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7330                 $LCTL set_param fail_loc=0x409
7331                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
7332                         error "direct write succeeded"
7333                 $LCTL set_param fail_loc=0
7334         done
7335         set_checksum_type $ORIG_CSUM_TYPE
7336         set_checksums 0
7337 }
7338 run_test 77f "repeat checksum error on write (expect error)"
7339
7340 test_77g() { # bug 10889
7341         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7342         $GSS && skip_env "could not run with gss"
7343         remote_ost_nodsh && skip "remote OST with nodsh"
7344
7345         [ ! -f $F77_TMP ] && setup_f77
7346
7347         local file=$DIR/$tfile
7348         stack_trap "rm -f $file" EXIT
7349
7350         $SETSTRIPE -c 1 -i 0 $file
7351         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
7352         do_facet ost1 lctl set_param fail_loc=0x8000021a
7353         set_checksums 1
7354         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
7355                 error "write error: rc=$?"
7356         do_facet ost1 lctl set_param fail_loc=0
7357         set_checksums 0
7358
7359         cancel_lru_locks osc
7360         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
7361         do_facet ost1 lctl set_param fail_loc=0x8000021b
7362         set_checksums 1
7363         cmp $F77_TMP $file || error "file compare failed"
7364         do_facet ost1 lctl set_param fail_loc=0
7365         set_checksums 0
7366 }
7367 run_test 77g "checksum error on OST write, read"
7368
7369 test_77k() { # LU-10906
7370         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7371         $GSS && skip_env "could not run with gss"
7372
7373         local cksum_param="osc.$FSNAME*.checksums"
7374         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
7375         local checksum
7376         local i
7377
7378         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
7379         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
7380         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
7381                 EXIT
7382
7383         for i in 0 1; do
7384                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
7385                         error "failed to set checksum=$i on MGS"
7386                 wait_update $HOSTNAME "$get_checksum" $i
7387                 #remount
7388                 echo "remount client, checksum should be $i"
7389                 remount_client $MOUNT || "failed to remount client"
7390                 checksum=$(eval $get_checksum)
7391                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
7392         done
7393
7394         for opt in "checksum" "nochecksum"; do
7395                 #remount with mount option
7396                 echo "remount client with option $opt, checksum should be $i"
7397                 umount_client $MOUNT || "failed to umount client"
7398                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
7399                         "failed to mount client with option '$opt'"
7400                 checksum=$(eval $get_checksum)
7401                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
7402                 i=$((i - 1))
7403         done
7404
7405         remount_client $MOUNT || "failed to remount client"
7406 }
7407 run_test 77k "enable/disable checksum correctly"
7408
7409 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
7410 rm -f $F77_TMP
7411 unset F77_TMP
7412
7413 cleanup_test_78() {
7414         trap 0
7415         rm -f $DIR/$tfile
7416 }
7417
7418 test_78() { # bug 10901
7419         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7420         remote_ost || skip_env "local OST"
7421
7422         NSEQ=5
7423         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
7424         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
7425         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
7426         echo "MemTotal: $MEMTOTAL"
7427
7428         # reserve 256MB of memory for the kernel and other running processes,
7429         # and then take 1/2 of the remaining memory for the read/write buffers.
7430         if [ $MEMTOTAL -gt 512 ] ;then
7431                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
7432         else
7433                 # for those poor memory-starved high-end clusters...
7434                 MEMTOTAL=$((MEMTOTAL / 2))
7435         fi
7436         echo "Mem to use for directio: $MEMTOTAL"
7437
7438         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
7439         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
7440         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
7441         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
7442                 head -n1)
7443         echo "Smallest OST: $SMALLESTOST"
7444         [[ $SMALLESTOST -lt 10240 ]] &&
7445                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
7446
7447         trap cleanup_test_78 EXIT
7448
7449         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
7450                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
7451
7452         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
7453         echo "File size: $F78SIZE"
7454         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
7455         for i in $(seq 1 $NSEQ); do
7456                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
7457                 echo directIO rdwr round $i of $NSEQ
7458                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
7459         done
7460
7461         cleanup_test_78
7462 }
7463 run_test 78 "handle large O_DIRECT writes correctly ============"
7464
7465 test_79() { # bug 12743
7466         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7467
7468         wait_delete_completed
7469
7470         BKTOTAL=$(calc_osc_kbytes kbytestotal)
7471         BKFREE=$(calc_osc_kbytes kbytesfree)
7472         BKAVAIL=$(calc_osc_kbytes kbytesavail)
7473
7474         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
7475         DFTOTAL=`echo $STRING | cut -d, -f1`
7476         DFUSED=`echo $STRING  | cut -d, -f2`
7477         DFAVAIL=`echo $STRING | cut -d, -f3`
7478         DFFREE=$(($DFTOTAL - $DFUSED))
7479
7480         ALLOWANCE=$((64 * $OSTCOUNT))
7481
7482         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
7483            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
7484                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
7485         fi
7486         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
7487            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
7488                 error "df free($DFFREE) mismatch OST free($BKFREE)"
7489         fi
7490         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
7491            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
7492                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
7493         fi
7494 }
7495 run_test 79 "df report consistency check ======================="
7496
7497 test_80() { # bug 10718
7498         remote_ost_nodsh && skip "remote OST with nodsh"
7499         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7500
7501         # relax strong synchronous semantics for slow backends like ZFS
7502         local soc="obdfilter.*.sync_on_lock_cancel"
7503         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
7504         local hosts=
7505         if [ "$soc_old" != "never" ] &&
7506                 [ "$ost1_FSTYPE" != "ldiskfs" ]; then
7507                         hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
7508                                 facet_active_host $host; done | sort -u)
7509                         do_nodes $hosts lctl set_param $soc=never
7510         fi
7511
7512         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
7513         sync; sleep 1; sync
7514         local BEFORE=`date +%s`
7515         cancel_lru_locks osc
7516         local AFTER=`date +%s`
7517         local DIFF=$((AFTER-BEFORE))
7518         if [ $DIFF -gt 1 ] ; then
7519                 error "elapsed for 1M@1T = $DIFF"
7520         fi
7521
7522         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
7523
7524         rm -f $DIR/$tfile
7525 }
7526 run_test 80 "Page eviction is equally fast at high offsets too  ===="
7527
7528 test_81a() { # LU-456
7529         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7530         remote_ost_nodsh && skip "remote OST with nodsh"
7531
7532         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
7533         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
7534         do_facet ost1 lctl set_param fail_loc=0x80000228
7535
7536         # write should trigger a retry and success
7537         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
7538         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7539         RC=$?
7540         if [ $RC -ne 0 ] ; then
7541                 error "write should success, but failed for $RC"
7542         fi
7543 }
7544 run_test 81a "OST should retry write when get -ENOSPC ==============="
7545
7546 test_81b() { # LU-456
7547         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7548         remote_ost_nodsh && skip "remote OST with nodsh"
7549
7550         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
7551         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
7552         do_facet ost1 lctl set_param fail_loc=0x228
7553
7554         # write should retry several times and return -ENOSPC finally
7555         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
7556         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7557         RC=$?
7558         ENOSPC=28
7559         if [ $RC -ne $ENOSPC ] ; then
7560                 error "dd should fail for -ENOSPC, but succeed."
7561         fi
7562 }
7563 run_test 81b "OST should return -ENOSPC when retry still fails ======="
7564
7565 test_82() { # LU-1031
7566         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
7567         local gid1=14091995
7568         local gid2=16022000
7569
7570         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
7571         local MULTIPID1=$!
7572         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
7573         local MULTIPID2=$!
7574         kill -USR1 $MULTIPID2
7575         sleep 2
7576         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
7577                 error "First grouplock does not block second one"
7578         else
7579                 echo "Second grouplock blocks first one"
7580         fi
7581         kill -USR1 $MULTIPID1
7582         wait $MULTIPID1
7583         wait $MULTIPID2
7584 }
7585 run_test 82 "Basic grouplock test"
7586
7587 test_99() {
7588         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
7589
7590         test_mkdir $DIR/$tdir.cvsroot
7591         chown $RUNAS_ID $DIR/$tdir.cvsroot
7592
7593         cd $TMP
7594         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
7595
7596         cd /etc/init.d
7597         # some versions of cvs import exit(1) when asked to import links or
7598         # files they can't read.  ignore those files.
7599         local toignore=$(find . -type l -printf '-I %f\n' -o \
7600                          ! -perm /4 -printf '-I %f\n')
7601         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
7602                 $tdir.reposname vtag rtag
7603
7604         cd $DIR
7605         test_mkdir $DIR/$tdir.reposname
7606         chown $RUNAS_ID $DIR/$tdir.reposname
7607         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
7608
7609         cd $DIR/$tdir.reposname
7610         $RUNAS touch foo99
7611         $RUNAS cvs add -m 'addmsg' foo99
7612         $RUNAS cvs update
7613         $RUNAS cvs commit -m 'nomsg' foo99
7614         rm -fr $DIR/$tdir.cvsroot
7615 }
7616 run_test 99 "cvs strange file/directory operations"
7617
7618 test_100() {
7619         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7620         [[ "$NETTYPE" =~ tcp ]] ||
7621                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
7622         remote_ost_nodsh && skip "remote OST with nodsh"
7623         remote_mds_nodsh && skip "remote MDS with nodsh"
7624         remote_servers ||
7625                 skip "useless for local single node setup"
7626
7627         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
7628                 [ "$PROT" != "tcp" ] && continue
7629                 RPORT=$(echo $REMOTE | cut -d: -f2)
7630                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
7631
7632                 rc=0
7633                 LPORT=`echo $LOCAL | cut -d: -f2`
7634                 if [ $LPORT -ge 1024 ]; then
7635                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
7636                         netstat -tna
7637                         error_exit "local: $LPORT > 1024, remote: $RPORT"
7638                 fi
7639         done
7640         [ "$rc" = 0 ] || error_exit "privileged port not found" )
7641 }
7642 run_test 100 "check local port using privileged port ==========="
7643
7644 function get_named_value()
7645 {
7646     local tag
7647
7648     tag=$1
7649     while read ;do
7650         line=$REPLY
7651         case $line in
7652         $tag*)
7653             echo $line | sed "s/^$tag[ ]*//"
7654             break
7655             ;;
7656         esac
7657     done
7658 }
7659
7660 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
7661                    awk '/^max_cached_mb/ { print $2 }')
7662
7663 cleanup_101a() {
7664         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
7665         trap 0
7666 }
7667
7668 test_101a() {
7669         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7670         [ $MDSCOUNT -ge 2 ] && skip_env "needs < 2 MDTs" #LU-4322
7671
7672         local s
7673         local discard
7674         local nreads=10000
7675         local cache_limit=32
7676
7677         $LCTL set_param -n osc.*-osc*.rpc_stats 0
7678         trap cleanup_101a EXIT
7679         $LCTL set_param -n llite.*.read_ahead_stats 0
7680         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
7681
7682         #
7683         # randomly read 10000 of 64K chunks from file 3x 32MB in size
7684         #
7685         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
7686         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
7687
7688         discard=0
7689         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
7690                 get_named_value 'read but discarded' | cut -d" " -f1); do
7691                         discard=$(($discard + $s))
7692         done
7693         cleanup_101a
7694
7695         if [[ $(($discard * 10)) -gt $nreads ]]; then
7696                 $LCTL get_param osc.*-osc*.rpc_stats
7697                 $LCTL get_param llite.*.read_ahead_stats
7698                 error "too many ($discard) discarded pages"
7699         fi
7700         rm -f $DIR/$tfile || true
7701 }
7702 run_test 101a "check read-ahead for random reads"
7703
7704 setup_test101bc() {
7705         test_mkdir $DIR/$tdir
7706         local STRIPE_SIZE=$1
7707         local FILE_LENGTH=$2
7708         STRIPE_OFFSET=0
7709
7710         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
7711
7712         local list=$(comma_list $(osts_nodes))
7713         set_osd_param $list '' read_cache_enable 0
7714         set_osd_param $list '' writethrough_cache_enable 0
7715
7716         trap cleanup_test101bc EXIT
7717         # prepare the read-ahead file
7718         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
7719
7720         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
7721                                 count=$FILE_SIZE_MB 2> /dev/null
7722
7723 }
7724
7725 cleanup_test101bc() {
7726         trap 0
7727         rm -rf $DIR/$tdir
7728         rm -f $DIR/$tfile
7729
7730         local list=$(comma_list $(osts_nodes))
7731         set_osd_param $list '' read_cache_enable 1
7732         set_osd_param $list '' writethrough_cache_enable 1
7733 }
7734
7735 calc_total() {
7736         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
7737 }
7738
7739 ra_check_101() {
7740         local READ_SIZE=$1
7741         local STRIPE_SIZE=$2
7742         local FILE_LENGTH=$3
7743         local RA_INC=1048576
7744         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
7745         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
7746                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
7747         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
7748                         get_named_value 'read but discarded' |
7749                         cut -d" " -f1 | calc_total)
7750         if [[ $DISCARD -gt $discard_limit ]]; then
7751                 $LCTL get_param llite.*.read_ahead_stats
7752                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
7753         else
7754                 echo "Read-ahead success for size ${READ_SIZE}"
7755         fi
7756 }
7757
7758 test_101b() {
7759         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7760         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7761
7762         local STRIPE_SIZE=1048576
7763         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
7764
7765         if [ $SLOW == "yes" ]; then
7766                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
7767         else
7768                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
7769         fi
7770
7771         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
7772
7773         # prepare the read-ahead file
7774         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
7775         cancel_lru_locks osc
7776         for BIDX in 2 4 8 16 32 64 128 256
7777         do
7778                 local BSIZE=$((BIDX*4096))
7779                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
7780                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
7781                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
7782                 $LCTL set_param -n llite.*.read_ahead_stats 0
7783                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
7784                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
7785                 cancel_lru_locks osc
7786                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
7787         done
7788         cleanup_test101bc
7789         true
7790 }
7791 run_test 101b "check stride-io mode read-ahead ================="
7792
7793 test_101c() {
7794         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7795
7796         local STRIPE_SIZE=1048576
7797         local FILE_LENGTH=$((STRIPE_SIZE*100))
7798         local nreads=10000
7799         local osc_rpc_stats
7800
7801         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
7802
7803         cancel_lru_locks osc
7804         $LCTL set_param osc.*.rpc_stats 0
7805         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
7806         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
7807                 local stats=$($LCTL get_param -n $osc_rpc_stats)
7808                 local lines=$(echo "$stats" | awk 'END {print NR;}')
7809                 local size
7810
7811                 if [ $lines -le 20 ]; then
7812                         continue
7813                 fi
7814                 for size in 1 2 4 8; do
7815                         local rpc=$(echo "$stats" |
7816                                     awk '($1 == "'$size':") {print $2; exit; }')
7817                         [ $rpc != 0 ] &&
7818                                 error "Small $((size*4))k read IO $rpc !"
7819                 done
7820                 echo "$osc_rpc_stats check passed!"
7821         done
7822         cleanup_test101bc
7823         true
7824 }
7825 run_test 101c "check stripe_size aligned read-ahead ================="
7826
7827 set_read_ahead() {
7828         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
7829         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
7830 }
7831
7832 test_101d() {
7833         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7834
7835         local file=$DIR/$tfile
7836         local sz_MB=${FILESIZE_101d:-500}
7837         local ra_MB=${READAHEAD_MB:-40}
7838
7839         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
7840         [ $free_MB -lt $sz_MB ] &&
7841                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
7842
7843         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
7844         $SETSTRIPE -c -1 $file || error "setstripe failed"
7845
7846         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
7847         echo Cancel LRU locks on lustre client to flush the client cache
7848         cancel_lru_locks osc
7849
7850         echo Disable read-ahead
7851         local old_READAHEAD=$(set_read_ahead 0)
7852
7853         echo Reading the test file $file with read-ahead disabled
7854         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
7855
7856         echo Cancel LRU locks on lustre client to flush the client cache
7857         cancel_lru_locks osc
7858         echo Enable read-ahead with ${ra_MB}MB
7859         set_read_ahead $ra_MB
7860
7861         echo Reading the test file $file with read-ahead enabled
7862         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
7863
7864         echo "read-ahead disabled time read $raOFF"
7865         echo "read-ahead enabled  time read $raON"
7866
7867         set_read_ahead $old_READAHEAD
7868         rm -f $file
7869         wait_delete_completed
7870
7871         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
7872                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
7873 }
7874 run_test 101d "file read with and without read-ahead enabled"
7875
7876 test_101e() {
7877         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7878
7879         local file=$DIR/$tfile
7880         local size_KB=500  #KB
7881         local count=100
7882         local bsize=1024
7883
7884         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
7885         local need_KB=$((count * size_KB))
7886         [[ $free_KB -le $need_KB ]] &&
7887                 skip_env "Need free space $need_KB, have $free_KB"
7888
7889         echo "Creating $count ${size_KB}K test files"
7890         for ((i = 0; i < $count; i++)); do
7891                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
7892         done
7893
7894         echo "Cancel LRU locks on lustre client to flush the client cache"
7895         cancel_lru_locks $OSC
7896
7897         echo "Reset readahead stats"
7898         $LCTL set_param -n llite.*.read_ahead_stats 0
7899
7900         for ((i = 0; i < $count; i++)); do
7901                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
7902         done
7903
7904         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
7905                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
7906
7907         for ((i = 0; i < $count; i++)); do
7908                 rm -rf $file.$i 2>/dev/null
7909         done
7910
7911         #10000 means 20% reads are missing in readahead
7912         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
7913 }
7914 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
7915
7916 test_101f() {
7917         which iozone || skip_env "no iozone installed"
7918
7919         local old_debug=$($LCTL get_param debug)
7920         old_debug=${old_debug#*=}
7921         $LCTL set_param debug="reada mmap"
7922
7923         # create a test file
7924         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
7925
7926         echo Cancel LRU locks on lustre client to flush the client cache
7927         cancel_lru_locks osc
7928
7929         echo Reset readahead stats
7930         $LCTL set_param -n llite.*.read_ahead_stats 0
7931
7932         echo mmap read the file with small block size
7933         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
7934                 > /dev/null 2>&1
7935
7936         echo checking missing pages
7937         $LCTL get_param llite.*.read_ahead_stats
7938         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
7939                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
7940
7941         $LCTL set_param debug="$old_debug"
7942         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
7943         rm -f $DIR/$tfile
7944 }
7945 run_test 101f "check mmap read performance"
7946
7947 test_101g_brw_size_test() {
7948         local mb=$1
7949         local pages=$((mb * 1048576 / PAGE_SIZE))
7950         local file=$DIR/$tfile
7951
7952         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
7953                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
7954         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
7955                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
7956                         return 2
7957         done
7958
7959         stack_trap "rm -f $file" EXIT
7960         $LCTL set_param -n osc.*.rpc_stats=0
7961
7962         # 10 RPCs should be enough for the test
7963         local count=10
7964         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
7965                 { error "dd write ${mb} MB blocks failed"; return 3; }
7966         cancel_lru_locks osc
7967         dd of=/dev/null if=$file bs=${mb}M count=$count ||
7968                 { error "dd write ${mb} MB blocks failed"; return 4; }
7969
7970         # calculate number of full-sized read and write RPCs
7971         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
7972                 sed -n '/pages per rpc/,/^$/p' |
7973                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
7974                 END { print reads,writes }'))
7975         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
7976                 return 5
7977         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
7978                 return 6
7979
7980         return 0
7981 }
7982
7983 test_101g() {
7984         remote_ost_nodsh && skip "remote OST with nodsh"
7985
7986         local rpcs
7987         local osts=$(get_facets OST)
7988         local list=$(comma_list $(osts_nodes))
7989         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
7990         local brw_size="obdfilter.*.brw_size"
7991
7992         $LFS setstripe -i 0 -c 1 $DIR/$tfile
7993
7994         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
7995         if [ $OST1_VERSION -ge $(version_code 2.8.52) -o \
7996              \( $OST1_VERSION -ge $(version_code 2.7.17) -a \
7997                 $OST1_VERSION -lt $(version_code 2.7.50) \) ] &&
7998            [ $CLIENT_VERSION -ge $(version_code 2.8.52) -o \
7999              \( $CLIENT_VERSION -ge $(version_code 2.7.17) -a \
8000                 $CLIENT_VERSION -lt $(version_code 2.7.50) \) ]; then
8001                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] && suffix="M"
8002                 if [[ $orig_mb -lt 16 ]]; then
8003                         save_lustre_params $osts "$brw_size" > $p
8004                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
8005                                 error "set 16MB RPC size failed"
8006
8007                         echo "remount client to enable new RPC size"
8008                         remount_client $MOUNT || error "remount_client failed"
8009                 fi
8010
8011                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
8012                 # should be able to set brw_size=12, but no rpc_stats for that
8013                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
8014         fi
8015
8016         test_101g_brw_size_test 4 || error "4MB RPC test failed"
8017
8018         if [[ $orig_mb -lt 16 ]]; then
8019                 restore_lustre_params < $p
8020                 remount_client $MOUNT || error "remount_client restore failed"
8021         fi
8022
8023         rm -f $p $DIR/$tfile
8024 }
8025 run_test 101g "Big bulk(4/16 MiB) readahead"
8026
8027 setup_test102() {
8028         test_mkdir $DIR/$tdir
8029         chown $RUNAS_ID $DIR/$tdir
8030         STRIPE_SIZE=65536
8031         STRIPE_OFFSET=1
8032         STRIPE_COUNT=$OSTCOUNT
8033         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
8034
8035         trap cleanup_test102 EXIT
8036         cd $DIR
8037         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
8038         cd $DIR/$tdir
8039         for num in 1 2 3 4; do
8040                 for count in $(seq 1 $STRIPE_COUNT); do
8041                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
8042                                 local size=`expr $STRIPE_SIZE \* $num`
8043                                 local file=file"$num-$idx-$count"
8044                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
8045                         done
8046                 done
8047         done
8048
8049         cd $DIR
8050         $1 tar cf $TMP/f102.tar $tdir --xattrs
8051 }
8052
8053 cleanup_test102() {
8054         trap 0
8055         rm -f $TMP/f102.tar
8056         rm -rf $DIR/d0.sanity/d102
8057 }
8058
8059 test_102a() {
8060         [ "$UID" != 0 ] && skip "must run as root"
8061         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
8062                 skip_env "must have user_xattr"
8063
8064         [ -z "$(which setfattr 2>/dev/null)" ] &&
8065                 skip_env "could not find setfattr"
8066
8067         local testfile=$DIR/$tfile
8068
8069         touch $testfile
8070         echo "set/get xattr..."
8071         setfattr -n trusted.name1 -v value1 $testfile ||
8072                 error "setfattr -n trusted.name1=value1 $testfile failed"
8073         getfattr -n trusted.name1 $testfile 2> /dev/null |
8074           grep "trusted.name1=.value1" ||
8075                 error "$testfile missing trusted.name1=value1"
8076
8077         setfattr -n user.author1 -v author1 $testfile ||
8078                 error "setfattr -n user.author1=author1 $testfile failed"
8079         getfattr -n user.author1 $testfile 2> /dev/null |
8080           grep "user.author1=.author1" ||
8081                 error "$testfile missing trusted.author1=author1"
8082
8083         echo "listxattr..."
8084         setfattr -n trusted.name2 -v value2 $testfile ||
8085                 error "$testfile unable to set trusted.name2"
8086         setfattr -n trusted.name3 -v value3 $testfile ||
8087                 error "$testfile unable to set trusted.name3"
8088         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
8089             grep "trusted.name" | wc -l) -eq 3 ] ||
8090                 error "$testfile missing 3 trusted.name xattrs"
8091
8092         setfattr -n user.author2 -v author2 $testfile ||
8093                 error "$testfile unable to set user.author2"
8094         setfattr -n user.author3 -v author3 $testfile ||
8095                 error "$testfile unable to set user.author3"
8096         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
8097             grep "user.author" | wc -l) -eq 3 ] ||
8098                 error "$testfile missing 3 user.author xattrs"
8099
8100         echo "remove xattr..."
8101         setfattr -x trusted.name1 $testfile ||
8102                 error "$testfile error deleting trusted.name1"
8103         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
8104                 error "$testfile did not delete trusted.name1 xattr"
8105
8106         setfattr -x user.author1 $testfile ||
8107                 error "$testfile error deleting user.author1"
8108         echo "set lustre special xattr ..."
8109         $LFS setstripe -c1 $testfile
8110         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
8111                 awk -F "=" '/trusted.lov/ { print $2 }' )
8112         setfattr -n "trusted.lov" -v $lovea $testfile ||
8113                 error "$testfile doesn't ignore setting trusted.lov again"
8114         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
8115                 error "$testfile allow setting invalid trusted.lov"
8116         rm -f $testfile
8117 }
8118 run_test 102a "user xattr test =================================="
8119
8120 test_102b() {
8121         [ -z "$(which setfattr 2>/dev/null)" ] &&
8122                 skip_env "could not find setfattr"
8123         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8124
8125         # b10930: get/set/list trusted.lov xattr
8126         echo "get/set/list trusted.lov xattr ..."
8127         local testfile=$DIR/$tfile
8128         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
8129                 error "setstripe failed"
8130         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
8131                 error "getstripe failed"
8132         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
8133                 error "can't get trusted.lov from $testfile"
8134
8135         local testfile2=${testfile}2
8136         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
8137                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
8138
8139         $MCREATE $testfile2
8140         setfattr -n trusted.lov -v $value $testfile2
8141         local stripe_size=$($GETSTRIPE -S $testfile2)
8142         local stripe_count=$($GETSTRIPE -c $testfile2)
8143         [[ $stripe_size -eq 65536 ]] ||
8144                 error "stripe size $stripe_size != 65536"
8145         [[ $stripe_count -eq $STRIPECOUNT ]] ||
8146                 error "stripe count $stripe_count != $STRIPECOUNT"
8147         rm -f $DIR/$tfile
8148 }
8149 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
8150
8151 test_102c() {
8152         [ -z "$(which setfattr 2>/dev/null)" ] &&
8153                 skip_env "could not find setfattr"
8154         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8155
8156         # b10930: get/set/list lustre.lov xattr
8157         echo "get/set/list lustre.lov xattr ..."
8158         test_mkdir $DIR/$tdir
8159         chown $RUNAS_ID $DIR/$tdir
8160         local testfile=$DIR/$tdir/$tfile
8161         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
8162                 error "setstripe failed"
8163         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
8164                 error "getstripe failed"
8165         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
8166         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
8167
8168         local testfile2=${testfile}2
8169         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
8170                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
8171
8172         $RUNAS $MCREATE $testfile2
8173         $RUNAS setfattr -n lustre.lov -v $value $testfile2
8174         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
8175         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
8176         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
8177         [ $stripe_count -eq $STRIPECOUNT ] ||
8178                 error "stripe count $stripe_count != $STRIPECOUNT"
8179 }
8180 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
8181
8182 compare_stripe_info1() {
8183         local stripe_index_all_zero=true
8184
8185         for num in 1 2 3 4; do
8186                 for count in $(seq 1 $STRIPE_COUNT); do
8187                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
8188                                 local size=$((STRIPE_SIZE * num))
8189                                 local file=file"$num-$offset-$count"
8190                                 stripe_size=$($LFS getstripe -S $PWD/$file)
8191                                 [[ $stripe_size -ne $size ]] &&
8192                                     error "$file: size $stripe_size != $size"
8193                                 stripe_count=$($LFS getstripe -c $PWD/$file)
8194                                 # allow fewer stripes to be created, ORI-601
8195                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
8196                                     error "$file: count $stripe_count != $count"
8197                                 stripe_index=$($LFS getstripe -i $PWD/$file)
8198                                 [[ $stripe_index -ne 0 ]] &&
8199                                         stripe_index_all_zero=false
8200                         done
8201                 done
8202         done
8203         $stripe_index_all_zero &&
8204                 error "all files are being extracted starting from OST index 0"
8205         return 0
8206 }
8207
8208 have_xattrs_include() {
8209         tar --help | grep -q xattrs-include &&
8210                 echo --xattrs-include="lustre.*"
8211 }
8212
8213 test_102d() {
8214         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8215         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8216
8217         XINC=$(have_xattrs_include)
8218         setup_test102
8219         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
8220         cd $DIR/$tdir/$tdir
8221         compare_stripe_info1
8222 }
8223 run_test 102d "tar restore stripe info from tarfile,not keep osts"
8224
8225 test_102f() {
8226         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8227         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8228
8229         XINC=$(have_xattrs_include)
8230         setup_test102
8231         test_mkdir $DIR/$tdir.restore
8232         cd $DIR
8233         tar cf - --xattrs $tdir | tar xf - \
8234                 -C $DIR/$tdir.restore --xattrs $XINC
8235         cd $DIR/$tdir.restore/$tdir
8236         compare_stripe_info1
8237 }
8238 run_test 102f "tar copy files, not keep osts"
8239
8240 grow_xattr() {
8241         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
8242                 skip "must have user_xattr"
8243         [ -z "$(which setfattr 2>/dev/null)" ] &&
8244                 skip_env "could not find setfattr"
8245         [ -z "$(which getfattr 2>/dev/null)" ] &&
8246                 skip_env "could not find getfattr"
8247
8248         local xsize=${1:-1024}  # in bytes
8249         local file=$DIR/$tfile
8250         local value="$(generate_string $xsize)"
8251         local xbig=trusted.big
8252
8253         touch $file
8254         log "save $xbig on $file"
8255         setfattr -n $xbig -v $value $file ||
8256                 error "saving $xbig on $file failed"
8257
8258         local orig=$(get_xattr_value $xbig $file)
8259         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
8260
8261         local xsml=trusted.sml
8262         log "save $xsml on $file"
8263         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
8264
8265         local new=$(get_xattr_value $xbig $file)
8266         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
8267
8268         log "grow $xsml on $file"
8269         setfattr -n $xsml -v "$value" $file ||
8270                 error "growing $xsml on $file failed"
8271
8272         new=$(get_xattr_value $xbig $file)
8273         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
8274         log "$xbig still valid after growing $xsml"
8275
8276         rm -f $file
8277 }
8278
8279 test_102h() { # bug 15777
8280         grow_xattr 1024
8281 }
8282 run_test 102h "grow xattr from inside inode to external block"
8283
8284 test_102ha() {
8285         large_xattr_enabled || skip_env "ea_inode feature disabled"
8286
8287         grow_xattr $(max_xattr_size)
8288 }
8289 run_test 102ha "grow xattr from inside inode to external inode"
8290
8291 test_102i() { # bug 17038
8292         [ -z "$(which getfattr 2>/dev/null)" ] &&
8293                 skip "could not find getfattr"
8294
8295         touch $DIR/$tfile
8296         ln -s $DIR/$tfile $DIR/${tfile}link
8297         getfattr -n trusted.lov $DIR/$tfile ||
8298                 error "lgetxattr on $DIR/$tfile failed"
8299         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
8300                 grep -i "no such attr" ||
8301                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
8302         rm -f $DIR/$tfile $DIR/${tfile}link
8303 }
8304 run_test 102i "lgetxattr test on symbolic link ============"
8305
8306 test_102j() {
8307         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8308         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8309
8310         XINC=$(have_xattrs_include)
8311         setup_test102 "$RUNAS"
8312         chown $RUNAS_ID $DIR/$tdir
8313         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
8314         cd $DIR/$tdir/$tdir
8315         compare_stripe_info1 "$RUNAS"
8316 }
8317 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
8318
8319 test_102k() {
8320         [ -z "$(which setfattr 2>/dev/null)" ] &&
8321                 skip "could not find setfattr"
8322
8323         touch $DIR/$tfile
8324         # b22187 just check that does not crash for regular file.
8325         setfattr -n trusted.lov $DIR/$tfile
8326         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
8327         local test_kdir=$DIR/$tdir
8328         test_mkdir $test_kdir
8329         local default_size=$($LFS getstripe -S $test_kdir)
8330         local default_count=$($LFS getstripe -c $test_kdir)
8331         local default_offset=$($LFS getstripe -i $test_kdir)
8332         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
8333                 error 'dir setstripe failed'
8334         setfattr -n trusted.lov $test_kdir
8335         local stripe_size=$($LFS getstripe -S $test_kdir)
8336         local stripe_count=$($LFS getstripe -c $test_kdir)
8337         local stripe_offset=$($LFS getstripe -i $test_kdir)
8338         [ $stripe_size -eq $default_size ] ||
8339                 error "stripe size $stripe_size != $default_size"
8340         [ $stripe_count -eq $default_count ] ||
8341                 error "stripe count $stripe_count != $default_count"
8342         [ $stripe_offset -eq $default_offset ] ||
8343                 error "stripe offset $stripe_offset != $default_offset"
8344         rm -rf $DIR/$tfile $test_kdir
8345 }
8346 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
8347
8348 test_102l() {
8349         [ -z "$(which getfattr 2>/dev/null)" ] &&
8350                 skip "could not find getfattr"
8351
8352         # LU-532 trusted. xattr is invisible to non-root
8353         local testfile=$DIR/$tfile
8354
8355         touch $testfile
8356
8357         echo "listxattr as user..."
8358         chown $RUNAS_ID $testfile
8359         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
8360             grep -q "trusted" &&
8361                 error "$testfile trusted xattrs are user visible"
8362
8363         return 0;
8364 }
8365 run_test 102l "listxattr size test =================================="
8366
8367 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
8368         local path=$DIR/$tfile
8369         touch $path
8370
8371         listxattr_size_check $path || error "listattr_size_check $path failed"
8372 }
8373 run_test 102m "Ensure listxattr fails on small bufffer ========"
8374
8375 cleanup_test102
8376
8377 getxattr() { # getxattr path name
8378         # Return the base64 encoding of the value of xattr name on path.
8379         local path=$1
8380         local name=$2
8381
8382         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
8383         # file: $path
8384         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
8385         #
8386         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
8387
8388         getfattr --absolute-names --encoding=base64 --name=$name $path |
8389                 awk -F= -v name=$name '$1 == name {
8390                         print substr($0, index($0, "=") + 1);
8391         }'
8392 }
8393
8394 test_102n() { # LU-4101 mdt: protect internal xattrs
8395         [ -z "$(which setfattr 2>/dev/null)" ] &&
8396                 skip "could not find setfattr"
8397         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
8398         then
8399                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
8400         fi
8401
8402         local file0=$DIR/$tfile.0
8403         local file1=$DIR/$tfile.1
8404         local xattr0=$TMP/$tfile.0
8405         local xattr1=$TMP/$tfile.1
8406         local namelist="lov lma lmv link fid version som hsm"
8407         local name
8408         local value
8409
8410         rm -rf $file0 $file1 $xattr0 $xattr1
8411         touch $file0 $file1
8412
8413         # Get 'before' xattrs of $file1.
8414         getfattr --absolute-names --dump --match=- $file1 > $xattr0
8415
8416         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
8417                 namelist+=" lfsck_namespace"
8418         for name in $namelist; do
8419                 # Try to copy xattr from $file0 to $file1.
8420                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
8421
8422                 setfattr --name=trusted.$name --value="$value" $file1 ||
8423                         error "setxattr 'trusted.$name' failed"
8424
8425                 # Try to set a garbage xattr.
8426                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
8427
8428                 if [[ x$name == "xlov" ]]; then
8429                         setfattr --name=trusted.lov --value="$value" $file1 &&
8430                         error "setxattr invalid 'trusted.lov' success"
8431                 else
8432                         setfattr --name=trusted.$name --value="$value" $file1 ||
8433                                 error "setxattr invalid 'trusted.$name' failed"
8434                 fi
8435
8436                 # Try to remove the xattr from $file1. We don't care if this
8437                 # appears to succeed or fail, we just don't want there to be
8438                 # any changes or crashes.
8439                 setfattr --remove=$trusted.$name $file1 2> /dev/null
8440         done
8441
8442         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
8443         then
8444                 name="lfsck_ns"
8445                 # Try to copy xattr from $file0 to $file1.
8446                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
8447
8448                 setfattr --name=trusted.$name --value="$value" $file1 ||
8449                         error "setxattr 'trusted.$name' failed"
8450
8451                 # Try to set a garbage xattr.
8452                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
8453
8454                 setfattr --name=trusted.$name --value="$value" $file1 ||
8455                         error "setxattr 'trusted.$name' failed"
8456
8457                 # Try to remove the xattr from $file1. We don't care if this
8458                 # appears to succeed or fail, we just don't want there to be
8459                 # any changes or crashes.
8460                 setfattr --remove=$trusted.$name $file1 2> /dev/null
8461         fi
8462
8463         # Get 'after' xattrs of file1.
8464         getfattr --absolute-names --dump --match=- $file1 > $xattr1
8465
8466         if ! diff $xattr0 $xattr1; then
8467                 error "before and after xattrs of '$file1' differ"
8468         fi
8469
8470         rm -rf $file0 $file1 $xattr0 $xattr1
8471
8472         return 0
8473 }
8474 run_test 102n "silently ignore setxattr on internal trusted xattrs"
8475
8476 test_102p() { # LU-4703 setxattr did not check ownership
8477         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
8478                 skip "MDS needs to be at least 2.5.56"
8479
8480         local testfile=$DIR/$tfile
8481
8482         touch $testfile
8483
8484         echo "setfacl as user..."
8485         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
8486         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
8487
8488         echo "setfattr as user..."
8489         setfacl -m "u:$RUNAS_ID:---" $testfile
8490         $RUNAS setfattr -x system.posix_acl_access $testfile
8491         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
8492 }
8493 run_test 102p "check setxattr(2) correctly fails without permission"
8494
8495 test_102q() {
8496         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
8497                 skip "MDS needs to be at least 2.6.92"
8498
8499         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
8500 }
8501 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
8502
8503 test_102r() {
8504         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
8505                 skip "MDS needs to be at least 2.6.93"
8506
8507         touch $DIR/$tfile || error "touch"
8508         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
8509         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
8510         rm $DIR/$tfile || error "rm"
8511
8512         #normal directory
8513         mkdir -p $DIR/$tdir || error "mkdir"
8514         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
8515         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
8516         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
8517                 error "$testfile error deleting user.author1"
8518         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
8519                 grep "user.$(basename $tdir)" &&
8520                 error "$tdir did not delete user.$(basename $tdir)"
8521         rmdir $DIR/$tdir || error "rmdir"
8522
8523         #striped directory
8524         test_mkdir $DIR/$tdir
8525         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
8526         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
8527         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
8528                 error "$testfile error deleting user.author1"
8529         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
8530                 grep "user.$(basename $tdir)" &&
8531                 error "$tdir did not delete user.$(basename $tdir)"
8532         rmdir $DIR/$tdir || error "rm striped dir"
8533 }
8534 run_test 102r "set EAs with empty values"
8535
8536 test_102s() {
8537         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
8538                 skip "MDS needs to be at least 2.11.52"
8539
8540         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
8541
8542         save_lustre_params client "llite.*.xattr_cache" > $save
8543
8544         for cache in 0 1; do
8545                 lctl set_param llite.*.xattr_cache=$cache
8546
8547                 rm -f $DIR/$tfile
8548                 touch $DIR/$tfile || error "touch"
8549                 for prefix in lustre security system trusted user; do
8550                         # Note getxattr() may fail with 'Operation not
8551                         # supported' or 'No such attribute' depending
8552                         # on prefix and cache.
8553                         getfattr -n $prefix.n102s $DIR/$tfile &&
8554                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
8555                 done
8556         done
8557
8558         restore_lustre_params < $save
8559 }
8560 run_test 102s "getting nonexistent xattrs should fail"
8561
8562 test_102t() {
8563         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
8564                 skip "MDS needs to be at least 2.11.52"
8565
8566         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
8567
8568         save_lustre_params client "llite.*.xattr_cache" > $save
8569
8570         for cache in 0 1; do
8571                 lctl set_param llite.*.xattr_cache=$cache
8572
8573                 for buf_size in 0 256; do
8574                         rm -f $DIR/$tfile
8575                         touch $DIR/$tfile || error "touch"
8576                         setfattr -n user.multiop $DIR/$tfile
8577                         $MULTIOP $DIR/$tfile oa$buf_size ||
8578                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
8579                 done
8580         done
8581
8582         restore_lustre_params < $save
8583 }
8584 run_test 102t "zero length xattr values handled correctly"
8585
8586 run_acl_subtest()
8587 {
8588     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
8589     return $?
8590 }
8591
8592 test_103a() {
8593         [ "$UID" != 0 ] && skip "must run as root"
8594         $GSS && skip_env "could not run under gss"
8595         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
8596                 skip_env "must have acl enabled"
8597         [ -z "$(which setfacl 2>/dev/null)" ] &&
8598                 skip_env "could not find setfacl"
8599         remote_mds_nodsh && skip "remote MDS with nodsh"
8600
8601         gpasswd -a daemon bin                           # LU-5641
8602         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
8603
8604         declare -a identity_old
8605
8606         for num in $(seq $MDSCOUNT); do
8607                 switch_identity $num true || identity_old[$num]=$?
8608         done
8609
8610         SAVE_UMASK=$(umask)
8611         umask 0022
8612         mkdir -p $DIR/$tdir
8613         cd $DIR/$tdir
8614
8615         echo "performing cp ..."
8616         run_acl_subtest cp || error "run_acl_subtest cp failed"
8617         echo "performing getfacl-noacl..."
8618         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
8619         echo "performing misc..."
8620         run_acl_subtest misc || error  "misc test failed"
8621         echo "performing permissions..."
8622         run_acl_subtest permissions || error "permissions failed"
8623         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
8624         if [ $MDS1_VERSION -gt $(version_code 2.8.55) -o \
8625              \( $MDS1_VERSION -lt $(version_code 2.6) -a \
8626              $MDS1_VERSION -ge $(version_code 2.5.29) \) ]
8627         then
8628                 echo "performing permissions xattr..."
8629                 run_acl_subtest permissions_xattr ||
8630                         error "permissions_xattr failed"
8631         fi
8632         echo "performing setfacl..."
8633         run_acl_subtest setfacl || error  "setfacl test failed"
8634
8635         # inheritance test got from HP
8636         echo "performing inheritance..."
8637         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
8638         chmod +x make-tree || error "chmod +x failed"
8639         run_acl_subtest inheritance || error "inheritance test failed"
8640         rm -f make-tree
8641
8642         echo "LU-974 ignore umask when acl is enabled..."
8643         run_acl_subtest 974 || error "LU-974 umask test failed"
8644         if [ $MDSCOUNT -ge 2 ]; then
8645                 run_acl_subtest 974_remote ||
8646                         error "LU-974 umask test failed under remote dir"
8647         fi
8648
8649         echo "LU-2561 newly created file is same size as directory..."
8650         if [ "$mds1_FSTYPE" != "zfs" ]; then
8651                 run_acl_subtest 2561 || error "LU-2561 test failed"
8652         else
8653                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
8654         fi
8655
8656         run_acl_subtest 4924 || error "LU-4924 test failed"
8657
8658         cd $SAVE_PWD
8659         umask $SAVE_UMASK
8660
8661         for num in $(seq $MDSCOUNT); do
8662                 if [ "${identity_old[$num]}" = 1 ]; then
8663                         switch_identity $num false || identity_old[$num]=$?
8664                 fi
8665         done
8666 }
8667 run_test 103a "acl test"
8668
8669 test_103b() {
8670         declare -a pids
8671         local U
8672
8673         for U in {0..511}; do
8674                 {
8675                 local O=$(printf "%04o" $U)
8676
8677                 umask $(printf "%04o" $((511 ^ $O)))
8678                 $LFS setstripe -c 1 $DIR/$tfile.s$O
8679                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
8680
8681                 (( $S == ($O & 0666) )) ||
8682                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
8683
8684                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
8685                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
8686                 (( $S == ($O & 0666) )) ||
8687                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
8688
8689                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
8690                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
8691                 (( $S == ($O & 0666) )) ||
8692                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
8693                 rm -f $DIR/$tfile.[smp]$0
8694                 } &
8695                 local pid=$!
8696
8697                 # limit the concurrently running threads to 64. LU-11878
8698                 local idx=$((U % 64))
8699                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
8700                 pids[idx]=$pid
8701         done
8702         wait
8703 }
8704 run_test 103b "umask lfs setstripe"
8705
8706 test_103c() {
8707         mkdir -p $DIR/$tdir
8708         cp -rp $DIR/$tdir $DIR/$tdir.bak
8709
8710         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
8711                 error "$DIR/$tdir shouldn't contain default ACL"
8712         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
8713                 error "$DIR/$tdir.bak shouldn't contain default ACL"
8714         true
8715 }
8716 run_test 103c "'cp -rp' won't set empty acl"
8717
8718 test_104a() {
8719         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8720
8721         touch $DIR/$tfile
8722         lfs df || error "lfs df failed"
8723         lfs df -ih || error "lfs df -ih failed"
8724         lfs df -h $DIR || error "lfs df -h $DIR failed"
8725         lfs df -i $DIR || error "lfs df -i $DIR failed"
8726         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
8727         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
8728
8729         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
8730         lctl --device %$OSC deactivate
8731         lfs df || error "lfs df with deactivated OSC failed"
8732         lctl --device %$OSC activate
8733         # wait the osc back to normal
8734         wait_osc_import_ready client ost
8735
8736         lfs df || error "lfs df with reactivated OSC failed"
8737         rm -f $DIR/$tfile
8738 }
8739 run_test 104a "lfs df [-ih] [path] test ========================="
8740
8741 test_104b() {
8742         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8743         [ $RUNAS_ID -eq $UID ] &&
8744                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8745
8746         chmod 666 /dev/obd
8747         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
8748                         grep "Permission denied" | wc -l)))
8749         if [ $denied_cnt -ne 0 ]; then
8750                 error "lfs check servers test failed"
8751         fi
8752 }
8753 run_test 104b "$RUNAS lfs check servers test ===================="
8754
8755 test_105a() {
8756         # doesn't work on 2.4 kernels
8757         touch $DIR/$tfile
8758         if $(flock_is_enabled); then
8759                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
8760         else
8761                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
8762         fi
8763         rm -f $DIR/$tfile
8764 }
8765 run_test 105a "flock when mounted without -o flock test ========"
8766
8767 test_105b() {
8768         touch $DIR/$tfile
8769         if $(flock_is_enabled); then
8770                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
8771         else
8772                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
8773         fi
8774         rm -f $DIR/$tfile
8775 }
8776 run_test 105b "fcntl when mounted without -o flock test ========"
8777
8778 test_105c() {
8779         touch $DIR/$tfile
8780         if $(flock_is_enabled); then
8781                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
8782         else
8783                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
8784         fi
8785         rm -f $DIR/$tfile
8786 }
8787 run_test 105c "lockf when mounted without -o flock test"
8788
8789 test_105d() { # bug 15924
8790         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8791
8792         test_mkdir $DIR/$tdir
8793         flock_is_enabled || skip_env "mount w/o flock enabled"
8794         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
8795         $LCTL set_param fail_loc=0x80000315
8796         flocks_test 2 $DIR/$tdir
8797 }
8798 run_test 105d "flock race (should not freeze) ========"
8799
8800 test_105e() { # bug 22660 && 22040
8801         flock_is_enabled || skip_env "mount w/o flock enabled"
8802
8803         touch $DIR/$tfile
8804         flocks_test 3 $DIR/$tfile
8805 }
8806 run_test 105e "Two conflicting flocks from same process"
8807
8808 test_106() { #bug 10921
8809         test_mkdir $DIR/$tdir
8810         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
8811         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
8812 }
8813 run_test 106 "attempt exec of dir followed by chown of that dir"
8814
8815 test_107() {
8816         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8817
8818         CDIR=`pwd`
8819         local file=core
8820
8821         cd $DIR
8822         rm -f $file
8823
8824         local save_pattern=$(sysctl -n kernel.core_pattern)
8825         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
8826         sysctl -w kernel.core_pattern=$file
8827         sysctl -w kernel.core_uses_pid=0
8828
8829         ulimit -c unlimited
8830         sleep 60 &
8831         SLEEPPID=$!
8832
8833         sleep 1
8834
8835         kill -s 11 $SLEEPPID
8836         wait $SLEEPPID
8837         if [ -e $file ]; then
8838                 size=`stat -c%s $file`
8839                 [ $size -eq 0 ] && error "Fail to create core file $file"
8840         else
8841                 error "Fail to create core file $file"
8842         fi
8843         rm -f $file
8844         sysctl -w kernel.core_pattern=$save_pattern
8845         sysctl -w kernel.core_uses_pid=$save_uses_pid
8846         cd $CDIR
8847 }
8848 run_test 107 "Coredump on SIG"
8849
8850 test_110() {
8851         test_mkdir $DIR/$tdir
8852         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
8853         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
8854                 error "mkdir with 256 char should fail, but did not"
8855         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
8856                 error "create with 255 char failed"
8857         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
8858                 error "create with 256 char should fail, but did not"
8859
8860         ls -l $DIR/$tdir
8861         rm -rf $DIR/$tdir
8862 }
8863 run_test 110 "filename length checking"
8864
8865 #
8866 # Purpose: To verify dynamic thread (OSS) creation.
8867 #
8868 test_115() {
8869         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8870         remote_ost_nodsh && skip "remote OST with nodsh"
8871
8872         # Lustre does not stop service threads once they are started.
8873         # Reset number of running threads to default.
8874         stopall
8875         setupall
8876
8877         local OSTIO_pre
8878         local save_params="$TMP/sanity-$TESTNAME.parameters"
8879
8880         # Get ll_ost_io count before I/O
8881         OSTIO_pre=$(do_facet ost1 \
8882                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
8883         # Exit if lustre is not running (ll_ost_io not running).
8884         [ -z "$OSTIO_pre" ] && error "no OSS threads"
8885
8886         echo "Starting with $OSTIO_pre threads"
8887         local thread_max=$((OSTIO_pre * 2))
8888         local rpc_in_flight=$((thread_max * 2))
8889         # Number of I/O Process proposed to be started.
8890         local nfiles
8891         local facets=$(get_facets OST)
8892
8893         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
8894         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
8895
8896         # Set in_flight to $rpc_in_flight
8897         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
8898                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
8899         nfiles=${rpc_in_flight}
8900         # Set ost thread_max to $thread_max
8901         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
8902
8903         # 5 Minutes should be sufficient for max number of OSS
8904         # threads(thread_max) to be created.
8905         local timeout=300
8906
8907         # Start I/O.
8908         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
8909         test_mkdir $DIR/$tdir
8910         for i in $(seq $nfiles); do
8911                 local file=$DIR/$tdir/${tfile}-$i
8912                 $LFS setstripe -c -1 -i 0 $file
8913                 ($WTL $file $timeout)&
8914         done
8915
8916         # I/O Started - Wait for thread_started to reach thread_max or report
8917         # error if thread_started is more than thread_max.
8918         echo "Waiting for thread_started to reach thread_max"
8919         local thread_started=0
8920         local end_time=$((SECONDS + timeout))
8921
8922         while [ $SECONDS -le $end_time ] ; do
8923                 echo -n "."
8924                 # Get ost i/o thread_started count.
8925                 thread_started=$(do_facet ost1 \
8926                         "$LCTL get_param \
8927                         ost.OSS.ost_io.threads_started | cut -d= -f2")
8928                 # Break out if thread_started is equal/greater than thread_max
8929                 if [[ $thread_started -ge $thread_max ]]; then
8930                         echo ll_ost_io thread_started $thread_started, \
8931                                 equal/greater than thread_max $thread_max
8932                         break
8933                 fi
8934                 sleep 1
8935         done
8936
8937         # Cleanup - We have the numbers, Kill i/o jobs if running.
8938         jobcount=($(jobs -p))
8939         for i in $(seq 0 $((${#jobcount[@]}-1)))
8940         do
8941                 kill -9 ${jobcount[$i]}
8942                 if [ $? -ne 0 ] ; then
8943                         echo Warning: \
8944                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
8945                 fi
8946         done
8947
8948         # Cleanup files left by WTL binary.
8949         for i in $(seq $nfiles); do
8950                 local file=$DIR/$tdir/${tfile}-$i
8951                 rm -rf $file
8952                 if [ $? -ne 0 ] ; then
8953                         echo "Warning: Failed to delete file $file"
8954                 fi
8955         done
8956
8957         restore_lustre_params <$save_params
8958         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
8959
8960         # Error out if no new thread has started or Thread started is greater
8961         # than thread max.
8962         if [[ $thread_started -le $OSTIO_pre ||
8963                         $thread_started -gt $thread_max ]]; then
8964                 error "ll_ost_io: thread_started $thread_started" \
8965                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
8966                       "No new thread started or thread started greater " \
8967                       "than thread_max."
8968         fi
8969 }
8970 run_test 115 "verify dynamic thread creation===================="
8971
8972 free_min_max () {
8973         wait_delete_completed
8974         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
8975         echo "OST kbytes available: ${AVAIL[@]}"
8976         MAXV=${AVAIL[0]}
8977         MAXI=0
8978         MINV=${AVAIL[0]}
8979         MINI=0
8980         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
8981                 #echo OST $i: ${AVAIL[i]}kb
8982                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
8983                         MAXV=${AVAIL[i]}
8984                         MAXI=$i
8985                 fi
8986                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
8987                         MINV=${AVAIL[i]}
8988                         MINI=$i
8989                 fi
8990         done
8991         echo "Min free space: OST $MINI: $MINV"
8992         echo "Max free space: OST $MAXI: $MAXV"
8993 }
8994
8995 test_116a() { # was previously test_116()
8996         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8997         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8998         remote_mds_nodsh && skip "remote MDS with nodsh"
8999
9000         echo -n "Free space priority "
9001         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
9002                 head -n1
9003         declare -a AVAIL
9004         free_min_max
9005
9006         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
9007         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
9008         trap simple_cleanup_common EXIT
9009
9010         # Check if we need to generate uneven OSTs
9011         test_mkdir -p $DIR/$tdir/OST${MINI}
9012         local FILL=$((MINV / 4))
9013         local DIFF=$((MAXV - MINV))
9014         local DIFF2=$((DIFF * 100 / MINV))
9015
9016         local threshold=$(do_facet $SINGLEMDS \
9017                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
9018         threshold=${threshold%%%}
9019         echo -n "Check for uneven OSTs: "
9020         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
9021
9022         if [[ $DIFF2 -gt $threshold ]]; then
9023                 echo "ok"
9024                 echo "Don't need to fill OST$MINI"
9025         else
9026                 # generate uneven OSTs. Write 2% over the QOS threshold value
9027                 echo "no"
9028                 DIFF=$((threshold - DIFF2 + 2))
9029                 DIFF2=$((MINV * DIFF / 100))
9030                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
9031                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
9032                         error "setstripe failed"
9033                 DIFF=$((DIFF2 / 2048))
9034                 i=0
9035                 while [ $i -lt $DIFF ]; do
9036                         i=$((i + 1))
9037                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
9038                                 bs=2M count=1 2>/dev/null
9039                         echo -n .
9040                 done
9041                 echo .
9042                 sync
9043                 sleep_maxage
9044                 free_min_max
9045         fi
9046
9047         DIFF=$((MAXV - MINV))
9048         DIFF2=$((DIFF * 100 / MINV))
9049         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
9050         if [ $DIFF2 -gt $threshold ]; then
9051                 echo "ok"
9052         else
9053                 echo "failed - QOS mode won't be used"
9054                 simple_cleanup_common
9055                 skip "QOS imbalance criteria not met"
9056         fi
9057
9058         MINI1=$MINI
9059         MINV1=$MINV
9060         MAXI1=$MAXI
9061         MAXV1=$MAXV
9062
9063         # now fill using QOS
9064         $SETSTRIPE -c 1 $DIR/$tdir
9065         FILL=$((FILL / 200))
9066         if [ $FILL -gt 600 ]; then
9067                 FILL=600
9068         fi
9069         echo "writing $FILL files to QOS-assigned OSTs"
9070         i=0
9071         while [ $i -lt $FILL ]; do
9072                 i=$((i + 1))
9073                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
9074                         count=1 2>/dev/null
9075                 echo -n .
9076         done
9077         echo "wrote $i 200k files"
9078         sync
9079         sleep_maxage
9080
9081         echo "Note: free space may not be updated, so measurements might be off"
9082         free_min_max
9083         DIFF2=$((MAXV - MINV))
9084         echo "free space delta: orig $DIFF final $DIFF2"
9085         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
9086         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
9087         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
9088         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
9089         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
9090         if [[ $DIFF -gt 0 ]]; then
9091                 FILL=$((DIFF2 * 100 / DIFF - 100))
9092                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
9093         fi
9094
9095         # Figure out which files were written where
9096         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
9097                awk '/'$MINI1': / {print $2; exit}')
9098         echo $UUID
9099         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
9100         echo "$MINC files created on smaller OST $MINI1"
9101         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
9102                awk '/'$MAXI1': / {print $2; exit}')
9103         echo $UUID
9104         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
9105         echo "$MAXC files created on larger OST $MAXI1"
9106         if [[ $MINC -gt 0 ]]; then
9107                 FILL=$((MAXC * 100 / MINC - 100))
9108                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
9109         fi
9110         [[ $MAXC -gt $MINC ]] ||
9111                 error_ignore LU-9 "stripe QOS didn't balance free space"
9112         simple_cleanup_common
9113 }
9114 run_test 116a "stripe QOS: free space balance ==================="
9115
9116 test_116b() { # LU-2093
9117         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9118         remote_mds_nodsh && skip "remote MDS with nodsh"
9119
9120 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
9121         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
9122                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
9123         [ -z "$old_rr" ] && skip "no QOS"
9124         do_facet $SINGLEMDS lctl set_param \
9125                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
9126         mkdir -p $DIR/$tdir
9127         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
9128         createmany -o $DIR/$tdir/f- 20 || error "can't create"
9129         do_facet $SINGLEMDS lctl set_param fail_loc=0
9130         rm -rf $DIR/$tdir
9131         do_facet $SINGLEMDS lctl set_param \
9132                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
9133 }
9134 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
9135
9136 test_117() # bug 10891
9137 {
9138         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9139
9140         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
9141         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
9142         lctl set_param fail_loc=0x21e
9143         > $DIR/$tfile || error "truncate failed"
9144         lctl set_param fail_loc=0
9145         echo "Truncate succeeded."
9146         rm -f $DIR/$tfile
9147 }
9148 run_test 117 "verify osd extend =========="
9149
9150 NO_SLOW_RESENDCOUNT=4
9151 export OLD_RESENDCOUNT=""
9152 set_resend_count () {
9153         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
9154         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
9155         lctl set_param -n $PROC_RESENDCOUNT $1
9156         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
9157 }
9158
9159 # for reduce test_118* time (b=14842)
9160 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
9161
9162 # Reset async IO behavior after error case
9163 reset_async() {
9164         FILE=$DIR/reset_async
9165
9166         # Ensure all OSCs are cleared
9167         $SETSTRIPE -c -1 $FILE
9168         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
9169         sync
9170         rm $FILE
9171 }
9172
9173 test_118a() #bug 11710
9174 {
9175         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9176
9177         reset_async
9178
9179         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9180         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9181         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9182
9183         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9184                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9185                 return 1;
9186         fi
9187         rm -f $DIR/$tfile
9188 }
9189 run_test 118a "verify O_SYNC works =========="
9190
9191 test_118b()
9192 {
9193         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9194         remote_ost_nodsh && skip "remote OST with nodsh"
9195
9196         reset_async
9197
9198         #define OBD_FAIL_SRV_ENOENT 0x217
9199         set_nodes_failloc "$(osts_nodes)" 0x217
9200         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9201         RC=$?
9202         set_nodes_failloc "$(osts_nodes)" 0
9203         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9204         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9205                     grep -c writeback)
9206
9207         if [[ $RC -eq 0 ]]; then
9208                 error "Must return error due to dropped pages, rc=$RC"
9209                 return 1;
9210         fi
9211
9212         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9213                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9214                 return 1;
9215         fi
9216
9217         echo "Dirty pages not leaked on ENOENT"
9218
9219         # Due to the above error the OSC will issue all RPCs syncronously
9220         # until a subsequent RPC completes successfully without error.
9221         $MULTIOP $DIR/$tfile Ow4096yc
9222         rm -f $DIR/$tfile
9223
9224         return 0
9225 }
9226 run_test 118b "Reclaim dirty pages on fatal error =========="
9227
9228 test_118c()
9229 {
9230         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9231
9232         # for 118c, restore the original resend count, LU-1940
9233         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
9234                                 set_resend_count $OLD_RESENDCOUNT
9235         remote_ost_nodsh && skip "remote OST with nodsh"
9236
9237         reset_async
9238
9239         #define OBD_FAIL_OST_EROFS               0x216
9240         set_nodes_failloc "$(osts_nodes)" 0x216
9241
9242         # multiop should block due to fsync until pages are written
9243         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9244         MULTIPID=$!
9245         sleep 1
9246
9247         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
9248                 error "Multiop failed to block on fsync, pid=$MULTIPID"
9249         fi
9250
9251         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9252                     grep -c writeback)
9253         if [[ $WRITEBACK -eq 0 ]]; then
9254                 error "No page in writeback, writeback=$WRITEBACK"
9255         fi
9256
9257         set_nodes_failloc "$(osts_nodes)" 0
9258         wait $MULTIPID
9259         RC=$?
9260         if [[ $RC -ne 0 ]]; then
9261                 error "Multiop fsync failed, rc=$RC"
9262         fi
9263
9264         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9265         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9266                     grep -c writeback)
9267         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9268                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9269         fi
9270
9271         rm -f $DIR/$tfile
9272         echo "Dirty pages flushed via fsync on EROFS"
9273         return 0
9274 }
9275 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
9276
9277 # continue to use small resend count to reduce test_118* time (b=14842)
9278 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
9279
9280 test_118d()
9281 {
9282         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9283         remote_ost_nodsh && skip "remote OST with nodsh"
9284
9285         reset_async
9286
9287         #define OBD_FAIL_OST_BRW_PAUSE_BULK
9288         set_nodes_failloc "$(osts_nodes)" 0x214
9289         # multiop should block due to fsync until pages are written
9290         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9291         MULTIPID=$!
9292         sleep 1
9293
9294         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
9295                 error "Multiop failed to block on fsync, pid=$MULTIPID"
9296         fi
9297
9298         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9299                     grep -c writeback)
9300         if [[ $WRITEBACK -eq 0 ]]; then
9301                 error "No page in writeback, writeback=$WRITEBACK"
9302         fi
9303
9304         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
9305         set_nodes_failloc "$(osts_nodes)" 0
9306
9307         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9308         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9309                     grep -c writeback)
9310         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9311                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9312         fi
9313
9314         rm -f $DIR/$tfile
9315         echo "Dirty pages gaurenteed flushed via fsync"
9316         return 0
9317 }
9318 run_test 118d "Fsync validation inject a delay of the bulk =========="
9319
9320 test_118f() {
9321         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9322
9323         reset_async
9324
9325         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
9326         lctl set_param fail_loc=0x8000040a
9327
9328         # Should simulate EINVAL error which is fatal
9329         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9330         RC=$?
9331         if [[ $RC -eq 0 ]]; then
9332                 error "Must return error due to dropped pages, rc=$RC"
9333         fi
9334
9335         lctl set_param fail_loc=0x0
9336
9337         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9338         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9339         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9340                     grep -c writeback)
9341         if [[ $LOCKED -ne 0 ]]; then
9342                 error "Locked pages remain in cache, locked=$LOCKED"
9343         fi
9344
9345         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9346                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9347         fi
9348
9349         rm -f $DIR/$tfile
9350         echo "No pages locked after fsync"
9351
9352         reset_async
9353         return 0
9354 }
9355 run_test 118f "Simulate unrecoverable OSC side error =========="
9356
9357 test_118g() {
9358         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9359
9360         reset_async
9361
9362         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
9363         lctl set_param fail_loc=0x406
9364
9365         # simulate local -ENOMEM
9366         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9367         RC=$?
9368
9369         lctl set_param fail_loc=0
9370         if [[ $RC -eq 0 ]]; then
9371                 error "Must return error due to dropped pages, rc=$RC"
9372         fi
9373
9374         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9375         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9376         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9377                         grep -c writeback)
9378         if [[ $LOCKED -ne 0 ]]; then
9379                 error "Locked pages remain in cache, locked=$LOCKED"
9380         fi
9381
9382         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9383                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9384         fi
9385
9386         rm -f $DIR/$tfile
9387         echo "No pages locked after fsync"
9388
9389         reset_async
9390         return 0
9391 }
9392 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
9393
9394 test_118h() {
9395         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9396         remote_ost_nodsh && skip "remote OST with nodsh"
9397
9398         reset_async
9399
9400         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9401         set_nodes_failloc "$(osts_nodes)" 0x20e
9402         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
9403         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9404         RC=$?
9405
9406         set_nodes_failloc "$(osts_nodes)" 0
9407         if [[ $RC -eq 0 ]]; then
9408                 error "Must return error due to dropped pages, rc=$RC"
9409         fi
9410
9411         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9412         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9413         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9414                     grep -c writeback)
9415         if [[ $LOCKED -ne 0 ]]; then
9416                 error "Locked pages remain in cache, locked=$LOCKED"
9417         fi
9418
9419         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9420                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9421         fi
9422
9423         rm -f $DIR/$tfile
9424         echo "No pages locked after fsync"
9425
9426         return 0
9427 }
9428 run_test 118h "Verify timeout in handling recoverables errors  =========="
9429
9430 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
9431
9432 test_118i() {
9433         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9434         remote_ost_nodsh && skip "remote OST with nodsh"
9435
9436         reset_async
9437
9438         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9439         set_nodes_failloc "$(osts_nodes)" 0x20e
9440
9441         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
9442         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9443         PID=$!
9444         sleep 5
9445         set_nodes_failloc "$(osts_nodes)" 0
9446
9447         wait $PID
9448         RC=$?
9449         if [[ $RC -ne 0 ]]; then
9450                 error "got error, but should be not, rc=$RC"
9451         fi
9452
9453         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9454         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9455         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9456         if [[ $LOCKED -ne 0 ]]; then
9457                 error "Locked pages remain in cache, locked=$LOCKED"
9458         fi
9459
9460         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9461                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9462         fi
9463
9464         rm -f $DIR/$tfile
9465         echo "No pages locked after fsync"
9466
9467         return 0
9468 }
9469 run_test 118i "Fix error before timeout in recoverable error  =========="
9470
9471 [ "$SLOW" = "no" ] && set_resend_count 4
9472
9473 test_118j() {
9474         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9475         remote_ost_nodsh && skip "remote OST with nodsh"
9476
9477         reset_async
9478
9479         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
9480         set_nodes_failloc "$(osts_nodes)" 0x220
9481
9482         # return -EIO from OST
9483         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9484         RC=$?
9485         set_nodes_failloc "$(osts_nodes)" 0x0
9486         if [[ $RC -eq 0 ]]; then
9487                 error "Must return error due to dropped pages, rc=$RC"
9488         fi
9489
9490         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9491         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9492         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9493         if [[ $LOCKED -ne 0 ]]; then
9494                 error "Locked pages remain in cache, locked=$LOCKED"
9495         fi
9496
9497         # in recoverable error on OST we want resend and stay until it finished
9498         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9499                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9500         fi
9501
9502         rm -f $DIR/$tfile
9503         echo "No pages locked after fsync"
9504
9505         return 0
9506 }
9507 run_test 118j "Simulate unrecoverable OST side error =========="
9508
9509 test_118k()
9510 {
9511         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9512         remote_ost_nodsh && skip "remote OSTs with nodsh"
9513
9514         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9515         set_nodes_failloc "$(osts_nodes)" 0x20e
9516         test_mkdir $DIR/$tdir
9517
9518         for ((i=0;i<10;i++)); do
9519                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
9520                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
9521                 SLEEPPID=$!
9522                 sleep 0.500s
9523                 kill $SLEEPPID
9524                 wait $SLEEPPID
9525         done
9526
9527         set_nodes_failloc "$(osts_nodes)" 0
9528         rm -rf $DIR/$tdir
9529 }
9530 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
9531
9532 test_118l() # LU-646
9533 {
9534         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9535
9536         test_mkdir $DIR/$tdir
9537         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
9538         rm -rf $DIR/$tdir
9539 }
9540 run_test 118l "fsync dir"
9541
9542 test_118m() # LU-3066
9543 {
9544         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9545
9546         test_mkdir $DIR/$tdir
9547         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
9548         rm -rf $DIR/$tdir
9549 }
9550 run_test 118m "fdatasync dir ========="
9551
9552 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
9553
9554 test_118n()
9555 {
9556         local begin
9557         local end
9558
9559         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9560         remote_ost_nodsh && skip "remote OSTs with nodsh"
9561
9562         # Sleep to avoid a cached response.
9563         #define OBD_STATFS_CACHE_SECONDS 1
9564         sleep 2
9565
9566         # Inject a 10 second delay in the OST_STATFS handler.
9567         #define OBD_FAIL_OST_STATFS_DELAY 0x242
9568         set_nodes_failloc "$(osts_nodes)" 0x242
9569
9570         begin=$SECONDS
9571         stat --file-system $MOUNT > /dev/null
9572         end=$SECONDS
9573
9574         set_nodes_failloc "$(osts_nodes)" 0
9575
9576         if ((end - begin > 20)); then
9577             error "statfs took $((end - begin)) seconds, expected 10"
9578         fi
9579 }
9580 run_test 118n "statfs() sends OST_STATFS requests in parallel"
9581
9582 test_119a() # bug 11737
9583 {
9584         BSIZE=$((512 * 1024))
9585         directio write $DIR/$tfile 0 1 $BSIZE
9586         # We ask to read two blocks, which is more than a file size.
9587         # directio will indicate an error when requested and actual
9588         # sizes aren't equeal (a normal situation in this case) and
9589         # print actual read amount.
9590         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
9591         if [ "$NOB" != "$BSIZE" ]; then
9592                 error "read $NOB bytes instead of $BSIZE"
9593         fi
9594         rm -f $DIR/$tfile
9595 }
9596 run_test 119a "Short directIO read must return actual read amount"
9597
9598 test_119b() # bug 11737
9599 {
9600         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9601
9602         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
9603         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
9604         sync
9605         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
9606                 error "direct read failed"
9607         rm -f $DIR/$tfile
9608 }
9609 run_test 119b "Sparse directIO read must return actual read amount"
9610
9611 test_119c() # bug 13099
9612 {
9613         BSIZE=1048576
9614         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
9615         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
9616         rm -f $DIR/$tfile
9617 }
9618 run_test 119c "Testing for direct read hitting hole"
9619
9620 test_119d() # bug 15950
9621 {
9622         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9623
9624         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
9625         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
9626         BSIZE=1048576
9627         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
9628         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
9629         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
9630         lctl set_param fail_loc=0x40d
9631         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
9632         pid_dio=$!
9633         sleep 1
9634         cat $DIR/$tfile > /dev/null &
9635         lctl set_param fail_loc=0
9636         pid_reads=$!
9637         wait $pid_dio
9638         log "the DIO writes have completed, now wait for the reads (should not block very long)"
9639         sleep 2
9640         [ -n "`ps h -p $pid_reads -o comm`" ] && \
9641         error "the read rpcs have not completed in 2s"
9642         rm -f $DIR/$tfile
9643         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
9644 }
9645 run_test 119d "The DIO path should try to send a new rpc once one is completed"
9646
9647 test_120a() {
9648         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9649         remote_mds_nodsh && skip "remote MDS with nodsh"
9650         test_mkdir $DIR/$tdir
9651         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9652                 skip_env "no early lock cancel on server"
9653
9654         lru_resize_disable mdc
9655         lru_resize_disable osc
9656         cancel_lru_locks mdc
9657         # asynchronous object destroy at MDT could cause bl ast to client
9658         cancel_lru_locks osc
9659
9660         stat $DIR/$tdir > /dev/null
9661         can1=$(do_facet $SINGLEMDS \
9662                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9663                awk '/ldlm_cancel/ {print $2}')
9664         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9665                awk '/ldlm_bl_callback/ {print $2}')
9666         test_mkdir -c1 $DIR/$tdir/d1
9667         can2=$(do_facet $SINGLEMDS \
9668                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9669                awk '/ldlm_cancel/ {print $2}')
9670         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9671                awk '/ldlm_bl_callback/ {print $2}')
9672         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9673         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9674         lru_resize_enable mdc
9675         lru_resize_enable osc
9676 }
9677 run_test 120a "Early Lock Cancel: mkdir test"
9678
9679 test_120b() {
9680         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9681         remote_mds_nodsh && skip "remote MDS with nodsh"
9682         test_mkdir $DIR/$tdir
9683         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9684                 skip_env "no early lock cancel on server"
9685
9686         lru_resize_disable mdc
9687         lru_resize_disable osc
9688         cancel_lru_locks mdc
9689         stat $DIR/$tdir > /dev/null
9690         can1=$(do_facet $SINGLEMDS \
9691                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9692                awk '/ldlm_cancel/ {print $2}')
9693         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9694                awk '/ldlm_bl_callback/ {print $2}')
9695         touch $DIR/$tdir/f1
9696         can2=$(do_facet $SINGLEMDS \
9697                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9698                awk '/ldlm_cancel/ {print $2}')
9699         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9700                awk '/ldlm_bl_callback/ {print $2}')
9701         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9702         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9703         lru_resize_enable mdc
9704         lru_resize_enable osc
9705 }
9706 run_test 120b "Early Lock Cancel: create test"
9707
9708 test_120c() {
9709         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9710         remote_mds_nodsh && skip "remote MDS with nodsh"
9711         test_mkdir -c1 $DIR/$tdir
9712         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9713                 skip "no early lock cancel on server"
9714
9715         lru_resize_disable mdc
9716         lru_resize_disable osc
9717         test_mkdir -c1 $DIR/$tdir/d1
9718         test_mkdir -c1 $DIR/$tdir/d2
9719         touch $DIR/$tdir/d1/f1
9720         cancel_lru_locks mdc
9721         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
9722         can1=$(do_facet $SINGLEMDS \
9723                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9724                awk '/ldlm_cancel/ {print $2}')
9725         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9726                awk '/ldlm_bl_callback/ {print $2}')
9727         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
9728         can2=$(do_facet $SINGLEMDS \
9729                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9730                awk '/ldlm_cancel/ {print $2}')
9731         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9732                awk '/ldlm_bl_callback/ {print $2}')
9733         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9734         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9735         lru_resize_enable mdc
9736         lru_resize_enable osc
9737 }
9738 run_test 120c "Early Lock Cancel: link test"
9739
9740 test_120d() {
9741         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9742         remote_mds_nodsh && skip "remote MDS with nodsh"
9743         test_mkdir -c1 $DIR/$tdir
9744         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9745                 skip_env "no early lock cancel on server"
9746
9747         lru_resize_disable mdc
9748         lru_resize_disable osc
9749         touch $DIR/$tdir
9750         cancel_lru_locks mdc
9751         stat $DIR/$tdir > /dev/null
9752         can1=$(do_facet $SINGLEMDS \
9753                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9754                awk '/ldlm_cancel/ {print $2}')
9755         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9756                awk '/ldlm_bl_callback/ {print $2}')
9757         chmod a+x $DIR/$tdir
9758         can2=$(do_facet $SINGLEMDS \
9759                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9760                awk '/ldlm_cancel/ {print $2}')
9761         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9762                awk '/ldlm_bl_callback/ {print $2}')
9763         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9764         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9765         lru_resize_enable mdc
9766         lru_resize_enable osc
9767 }
9768 run_test 120d "Early Lock Cancel: setattr test"
9769
9770 test_120e() {
9771         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9772         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9773                 skip_env "no early lock cancel on server"
9774         remote_mds_nodsh && skip "remote MDS with nodsh"
9775
9776         local dlmtrace_set=false
9777
9778         test_mkdir -c1 $DIR/$tdir
9779         lru_resize_disable mdc
9780         lru_resize_disable osc
9781         ! $LCTL get_param debug | grep -q dlmtrace &&
9782                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
9783         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
9784         cancel_lru_locks mdc
9785         cancel_lru_locks osc
9786         dd if=$DIR/$tdir/f1 of=/dev/null
9787         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
9788         # XXX client can not do early lock cancel of OST lock
9789         # during unlink (LU-4206), so cancel osc lock now.
9790         sleep 2
9791         cancel_lru_locks osc
9792         can1=$(do_facet $SINGLEMDS \
9793                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9794                awk '/ldlm_cancel/ {print $2}')
9795         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9796                awk '/ldlm_bl_callback/ {print $2}')
9797         unlink $DIR/$tdir/f1
9798         sleep 5
9799         can2=$(do_facet $SINGLEMDS \
9800                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9801                awk '/ldlm_cancel/ {print $2}')
9802         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9803                awk '/ldlm_bl_callback/ {print $2}')
9804         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
9805                 $LCTL dk $TMP/cancel.debug.txt
9806         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
9807                 $LCTL dk $TMP/blocking.debug.txt
9808         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
9809         lru_resize_enable mdc
9810         lru_resize_enable osc
9811 }
9812 run_test 120e "Early Lock Cancel: unlink test"
9813
9814 test_120f() {
9815         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9816         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9817                 skip_env "no early lock cancel on server"
9818         remote_mds_nodsh && skip "remote MDS with nodsh"
9819
9820         test_mkdir -c1 $DIR/$tdir
9821         lru_resize_disable mdc
9822         lru_resize_disable osc
9823         test_mkdir -c1 $DIR/$tdir/d1
9824         test_mkdir -c1 $DIR/$tdir/d2
9825         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
9826         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
9827         cancel_lru_locks mdc
9828         cancel_lru_locks osc
9829         dd if=$DIR/$tdir/d1/f1 of=/dev/null
9830         dd if=$DIR/$tdir/d2/f2 of=/dev/null
9831         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
9832         # XXX client can not do early lock cancel of OST lock
9833         # during rename (LU-4206), so cancel osc lock now.
9834         sleep 2
9835         cancel_lru_locks osc
9836         can1=$(do_facet $SINGLEMDS \
9837                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9838                awk '/ldlm_cancel/ {print $2}')
9839         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9840                awk '/ldlm_bl_callback/ {print $2}')
9841         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
9842         sleep 5
9843         can2=$(do_facet $SINGLEMDS \
9844                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9845                awk '/ldlm_cancel/ {print $2}')
9846         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9847                awk '/ldlm_bl_callback/ {print $2}')
9848         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9849         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9850         lru_resize_enable mdc
9851         lru_resize_enable osc
9852 }
9853 run_test 120f "Early Lock Cancel: rename test"
9854
9855 test_120g() {
9856         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9857         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9858                 skip_env "no early lock cancel on server"
9859         remote_mds_nodsh && skip "remote MDS with nodsh"
9860
9861         lru_resize_disable mdc
9862         lru_resize_disable osc
9863         count=10000
9864         echo create $count files
9865         test_mkdir $DIR/$tdir
9866         cancel_lru_locks mdc
9867         cancel_lru_locks osc
9868         t0=$(date +%s)
9869
9870         can0=$(do_facet $SINGLEMDS \
9871                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9872                awk '/ldlm_cancel/ {print $2}')
9873         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9874                awk '/ldlm_bl_callback/ {print $2}')
9875         createmany -o $DIR/$tdir/f $count
9876         sync
9877         can1=$(do_facet $SINGLEMDS \
9878                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9879                awk '/ldlm_cancel/ {print $2}')
9880         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9881                awk '/ldlm_bl_callback/ {print $2}')
9882         t1=$(date +%s)
9883         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
9884         echo rm $count files
9885         rm -r $DIR/$tdir
9886         sync
9887         can2=$(do_facet $SINGLEMDS \
9888                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9889                awk '/ldlm_cancel/ {print $2}')
9890         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9891                awk '/ldlm_bl_callback/ {print $2}')
9892         t2=$(date +%s)
9893         echo total: $count removes in $((t2-t1))
9894         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
9895         sleep 2
9896         # wait for commitment of removal
9897         lru_resize_enable mdc
9898         lru_resize_enable osc
9899 }
9900 run_test 120g "Early Lock Cancel: performance test"
9901
9902 test_121() { #bug #10589
9903         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9904
9905         rm -rf $DIR/$tfile
9906         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
9907 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
9908         lctl set_param fail_loc=0x310
9909         cancel_lru_locks osc > /dev/null
9910         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
9911         lctl set_param fail_loc=0
9912         [[ $reads -eq $writes ]] ||
9913                 error "read $reads blocks, must be $writes blocks"
9914 }
9915 run_test 121 "read cancel race ========="
9916
9917 test_123a() { # was test 123, statahead(bug 11401)
9918         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9919
9920         SLOWOK=0
9921         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
9922                 log "testing UP system. Performance may be lower than expected."
9923                 SLOWOK=1
9924         fi
9925
9926         rm -rf $DIR/$tdir
9927         test_mkdir $DIR/$tdir
9928         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
9929         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
9930         MULT=10
9931         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
9932                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
9933
9934                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
9935                 lctl set_param -n llite.*.statahead_max 0
9936                 lctl get_param llite.*.statahead_max
9937                 cancel_lru_locks mdc
9938                 cancel_lru_locks osc
9939                 stime=`date +%s`
9940                 time ls -l $DIR/$tdir | wc -l
9941                 etime=`date +%s`
9942                 delta=$((etime - stime))
9943                 log "ls $i files without statahead: $delta sec"
9944                 lctl set_param llite.*.statahead_max=$max
9945
9946                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
9947                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
9948                 cancel_lru_locks mdc
9949                 cancel_lru_locks osc
9950                 stime=`date +%s`
9951                 time ls -l $DIR/$tdir | wc -l
9952                 etime=`date +%s`
9953                 delta_sa=$((etime - stime))
9954                 log "ls $i files with statahead: $delta_sa sec"
9955                 lctl get_param -n llite.*.statahead_stats
9956                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
9957
9958                 [[ $swrong -lt $ewrong ]] &&
9959                         log "statahead was stopped, maybe too many locks held!"
9960                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
9961
9962                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
9963                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
9964                     lctl set_param -n llite.*.statahead_max 0
9965                     lctl get_param llite.*.statahead_max
9966                     cancel_lru_locks mdc
9967                     cancel_lru_locks osc
9968                     stime=`date +%s`
9969                     time ls -l $DIR/$tdir | wc -l
9970                     etime=`date +%s`
9971                     delta=$((etime - stime))
9972                     log "ls $i files again without statahead: $delta sec"
9973                     lctl set_param llite.*.statahead_max=$max
9974                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
9975                         if [  $SLOWOK -eq 0 ]; then
9976                                 error "ls $i files is slower with statahead!"
9977                         else
9978                                 log "ls $i files is slower with statahead!"
9979                         fi
9980                         break
9981                     fi
9982                 fi
9983
9984                 [ $delta -gt 20 ] && break
9985                 [ $delta -gt 8 ] && MULT=$((50 / delta))
9986                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
9987         done
9988         log "ls done"
9989
9990         stime=`date +%s`
9991         rm -r $DIR/$tdir
9992         sync
9993         etime=`date +%s`
9994         delta=$((etime - stime))
9995         log "rm -r $DIR/$tdir/: $delta seconds"
9996         log "rm done"
9997         lctl get_param -n llite.*.statahead_stats
9998 }
9999 run_test 123a "verify statahead work"
10000
10001 test_123b () { # statahead(bug 15027)
10002         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10003
10004         test_mkdir $DIR/$tdir
10005         createmany -o $DIR/$tdir/$tfile-%d 1000
10006
10007         cancel_lru_locks mdc
10008         cancel_lru_locks osc
10009
10010 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
10011         lctl set_param fail_loc=0x80000803
10012         ls -lR $DIR/$tdir > /dev/null
10013         log "ls done"
10014         lctl set_param fail_loc=0x0
10015         lctl get_param -n llite.*.statahead_stats
10016         rm -r $DIR/$tdir
10017         sync
10018
10019 }
10020 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
10021
10022 test_124a() {
10023         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10024         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10025                 skip_env "no lru resize on server"
10026
10027         local NR=2000
10028
10029         test_mkdir $DIR/$tdir
10030
10031         log "create $NR files at $DIR/$tdir"
10032         createmany -o $DIR/$tdir/f $NR ||
10033                 error "failed to create $NR files in $DIR/$tdir"
10034
10035         cancel_lru_locks mdc
10036         ls -l $DIR/$tdir > /dev/null
10037
10038         local NSDIR=""
10039         local LRU_SIZE=0
10040         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
10041                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
10042                 LRU_SIZE=$($LCTL get_param -n $PARAM)
10043                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
10044                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
10045                         log "NSDIR=$NSDIR"
10046                         log "NS=$(basename $NSDIR)"
10047                         break
10048                 fi
10049         done
10050
10051         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
10052                 skip "Not enough cached locks created!"
10053         fi
10054         log "LRU=$LRU_SIZE"
10055
10056         local SLEEP=30
10057
10058         # We know that lru resize allows one client to hold $LIMIT locks
10059         # for 10h. After that locks begin to be killed by client.
10060         local MAX_HRS=10
10061         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
10062         log "LIMIT=$LIMIT"
10063         if [ $LIMIT -lt $LRU_SIZE ]; then
10064                 skip "Limit is too small $LIMIT"
10065         fi
10066
10067         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
10068         # killing locks. Some time was spent for creating locks. This means
10069         # that up to the moment of sleep finish we must have killed some of
10070         # them (10-100 locks). This depends on how fast ther were created.
10071         # Many of them were touched in almost the same moment and thus will
10072         # be killed in groups.
10073         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
10074
10075         # Use $LRU_SIZE_B here to take into account real number of locks
10076         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
10077         local LRU_SIZE_B=$LRU_SIZE
10078         log "LVF=$LVF"
10079         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
10080         log "OLD_LVF=$OLD_LVF"
10081         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
10082
10083         # Let's make sure that we really have some margin. Client checks
10084         # cached locks every 10 sec.
10085         SLEEP=$((SLEEP+20))
10086         log "Sleep ${SLEEP} sec"
10087         local SEC=0
10088         while ((SEC<$SLEEP)); do
10089                 echo -n "..."
10090                 sleep 5
10091                 SEC=$((SEC+5))
10092                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
10093                 echo -n "$LRU_SIZE"
10094         done
10095         echo ""
10096         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
10097         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
10098
10099         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
10100                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
10101                 unlinkmany $DIR/$tdir/f $NR
10102                 return
10103         }
10104
10105         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
10106         log "unlink $NR files at $DIR/$tdir"
10107         unlinkmany $DIR/$tdir/f $NR
10108 }
10109 run_test 124a "lru resize ======================================="
10110
10111 get_max_pool_limit()
10112 {
10113         local limit=$($LCTL get_param \
10114                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
10115         local max=0
10116         for l in $limit; do
10117                 if [[ $l -gt $max ]]; then
10118                         max=$l
10119                 fi
10120         done
10121         echo $max
10122 }
10123
10124 test_124b() {
10125         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10126         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10127                 skip_env "no lru resize on server"
10128
10129         LIMIT=$(get_max_pool_limit)
10130
10131         NR=$(($(default_lru_size)*20))
10132         if [[ $NR -gt $LIMIT ]]; then
10133                 log "Limit lock number by $LIMIT locks"
10134                 NR=$LIMIT
10135         fi
10136
10137         IFree=$(mdsrate_inodes_available)
10138         if [ $IFree -lt $NR ]; then
10139                 log "Limit lock number by $IFree inodes"
10140                 NR=$IFree
10141         fi
10142
10143         lru_resize_disable mdc
10144         test_mkdir -p $DIR/$tdir/disable_lru_resize
10145
10146         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
10147         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
10148         cancel_lru_locks mdc
10149         stime=`date +%s`
10150         PID=""
10151         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10152         PID="$PID $!"
10153         sleep 2
10154         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10155         PID="$PID $!"
10156         sleep 2
10157         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10158         PID="$PID $!"
10159         wait $PID
10160         etime=`date +%s`
10161         nolruresize_delta=$((etime-stime))
10162         log "ls -la time: $nolruresize_delta seconds"
10163         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
10164         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
10165
10166         lru_resize_enable mdc
10167         test_mkdir -p $DIR/$tdir/enable_lru_resize
10168
10169         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
10170         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
10171         cancel_lru_locks mdc
10172         stime=`date +%s`
10173         PID=""
10174         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
10175         PID="$PID $!"
10176         sleep 2
10177         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
10178         PID="$PID $!"
10179         sleep 2
10180         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
10181         PID="$PID $!"
10182         wait $PID
10183         etime=`date +%s`
10184         lruresize_delta=$((etime-stime))
10185         log "ls -la time: $lruresize_delta seconds"
10186         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
10187
10188         if [ $lruresize_delta -gt $nolruresize_delta ]; then
10189                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
10190         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
10191                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
10192         else
10193                 log "lru resize performs the same with no lru resize"
10194         fi
10195         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
10196 }
10197 run_test 124b "lru resize (performance test) ======================="
10198
10199 test_124c() {
10200         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10201         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10202                 skip_env "no lru resize on server"
10203
10204         # cache ununsed locks on client
10205         local nr=100
10206         cancel_lru_locks mdc
10207         test_mkdir $DIR/$tdir
10208         createmany -o $DIR/$tdir/f $nr ||
10209                 error "failed to create $nr files in $DIR/$tdir"
10210         ls -l $DIR/$tdir > /dev/null
10211
10212         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
10213         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10214         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
10215         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
10216         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
10217
10218         # set lru_max_age to 1 sec
10219         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
10220         echo "sleep $((recalc_p * 2)) seconds..."
10221         sleep $((recalc_p * 2))
10222
10223         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
10224         # restore lru_max_age
10225         $LCTL set_param -n $nsdir.lru_max_age $max_age
10226         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
10227         unlinkmany $DIR/$tdir/f $nr
10228 }
10229 run_test 124c "LRUR cancel very aged locks"
10230
10231 test_125() { # 13358
10232         $LCTL get_param -n llite.*.client_type | grep -q local ||
10233                 skip "must run as local client"
10234         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
10235                 skip_env "must have acl enabled"
10236         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
10237
10238         test_mkdir $DIR/$tdir
10239         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
10240         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
10241         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
10242 }
10243 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
10244
10245 test_126() { # bug 12829/13455
10246         $GSS && skip_env "must run as gss disabled"
10247         $LCTL get_param -n llite.*.client_type | grep -q local ||
10248                 skip "must run as local client"
10249         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
10250
10251         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
10252         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
10253         rm -f $DIR/$tfile
10254         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
10255 }
10256 run_test 126 "check that the fsgid provided by the client is taken into account"
10257
10258 test_127a() { # bug 15521
10259         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10260
10261         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
10262         $LCTL set_param osc.*.stats=0
10263         FSIZE=$((2048 * 1024))
10264         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
10265         cancel_lru_locks osc
10266         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
10267
10268         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
10269         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
10270                 echo "got $COUNT $NAME"
10271                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
10272                 eval $NAME=$COUNT || error "Wrong proc format"
10273
10274                 case $NAME in
10275                         read_bytes|write_bytes)
10276                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
10277                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
10278                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
10279                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
10280                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
10281                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
10282                                 error "sumsquare is too small: $SUMSQ"
10283                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
10284                                 error "sumsquare is too big: $SUMSQ"
10285                         ;;
10286                         *) ;;
10287                 esac
10288         done < $DIR/${tfile}.tmp
10289
10290         #check that we actually got some stats
10291         [ "$read_bytes" ] || error "Missing read_bytes stats"
10292         [ "$write_bytes" ] || error "Missing write_bytes stats"
10293         [ "$read_bytes" != 0 ] || error "no read done"
10294         [ "$write_bytes" != 0 ] || error "no write done"
10295 }
10296 run_test 127a "verify the client stats are sane"
10297
10298 test_127b() { # bug LU-333
10299         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10300         local name count samp unit min max sum sumsq
10301
10302         $LCTL set_param llite.*.stats=0
10303
10304         # perform 2 reads and writes so MAX is different from SUM.
10305         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
10306         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
10307         cancel_lru_locks osc
10308         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
10309         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
10310
10311         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
10312         while read name count samp unit min max sum sumsq; do
10313                 echo "got $count $name"
10314                 eval $name=$count || error "Wrong proc format"
10315
10316                 case $name in
10317                 read_bytes)
10318                         [ $count -ne 2 ] && error "count is not 2: $count"
10319                         [ $min -ne $PAGE_SIZE ] &&
10320                                 error "min is not $PAGE_SIZE: $min"
10321                         [ $max -ne $PAGE_SIZE ] &&
10322                                 error "max is incorrect: $max"
10323                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
10324                                 error "sum is wrong: $sum"
10325                         ;;
10326                 write_bytes)
10327                         [ $count -ne 2 ] && error "count is not 2: $count"
10328                         [ $min -ne $PAGE_SIZE ] &&
10329                                 error "min is not $PAGE_SIZE: $min"
10330                         [ $max -ne $PAGE_SIZE ] &&
10331                                 error "max is incorrect: $max"
10332                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
10333                                 error "sum is wrong: $sum"
10334                         ;;
10335                 *) ;;
10336                 esac
10337         done < $TMP/$tfile.tmp
10338
10339         #check that we actually got some stats
10340         [ "$read_bytes" ] || error "Missing read_bytes stats"
10341         [ "$write_bytes" ] || error "Missing write_bytes stats"
10342         [ "$read_bytes" != 0 ] || error "no read done"
10343         [ "$write_bytes" != 0 ] || error "no write done"
10344
10345         rm -f $TMP/${tfile}.tmp
10346 }
10347 run_test 127b "verify the llite client stats are sane"
10348
10349 test_128() { # bug 15212
10350         touch $DIR/$tfile
10351         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
10352                 find $DIR/$tfile
10353                 find $DIR/$tfile
10354         EOF
10355
10356         result=$(grep error $TMP/$tfile.log)
10357         rm -f $DIR/$tfile $TMP/$tfile.log
10358         [ -z "$result" ] ||
10359                 error "consecutive find's under interactive lfs failed"
10360 }
10361 run_test 128 "interactive lfs for 2 consecutive find's"
10362
10363 set_dir_limits () {
10364         local mntdev
10365         local canondev
10366         local node
10367
10368         local ldproc=/proc/fs/ldiskfs
10369         local facets=$(get_facets MDS)
10370
10371         for facet in ${facets//,/ }; do
10372                 canondev=$(ldiskfs_canon \
10373                            *.$(convert_facet2label $facet).mntdev $facet)
10374                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
10375                         ldproc=/sys/fs/ldiskfs
10376                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
10377                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
10378         done
10379 }
10380
10381 check_mds_dmesg() {
10382         local facets=$(get_facets MDS)
10383         for facet in ${facets//,/ }; do
10384                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
10385         done
10386         return 1
10387 }
10388
10389 test_129() {
10390         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10391         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
10392                 skip "Need MDS version with at least 2.5.56"
10393         if [ "$mds1_FSTYPE" != ldiskfs ]; then
10394                 skip_env "ldiskfs only test"
10395         fi
10396         remote_mds_nodsh && skip "remote MDS with nodsh"
10397
10398         local ENOSPC=28
10399         local EFBIG=27
10400         local has_warning=false
10401
10402         rm -rf $DIR/$tdir
10403         mkdir -p $DIR/$tdir
10404
10405         # block size of mds1
10406         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
10407         set_dir_limits $maxsize $maxsize
10408         local dirsize=$(stat -c%s "$DIR/$tdir")
10409         local nfiles=0
10410         while [[ $dirsize -le $maxsize ]]; do
10411                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
10412                 rc=$?
10413                 if ! $has_warning; then
10414                         check_mds_dmesg '"is approaching"' && has_warning=true
10415                 fi
10416                 # check two errors:
10417                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
10418                 # EFBIG for previous versions included in ldiskfs series
10419                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
10420                         set_dir_limits 0 0
10421                         echo "return code $rc received as expected"
10422
10423                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
10424                                 error_exit "create failed w/o dir size limit"
10425
10426                         check_mds_dmesg '"has reached"' ||
10427                                 error_exit "reached message should be output"
10428
10429                         [ $has_warning = "false" ] &&
10430                                 error_exit "warning message should be output"
10431
10432                         dirsize=$(stat -c%s "$DIR/$tdir")
10433
10434                         [[ $dirsize -ge $maxsize ]] && return 0
10435                         error_exit "current dir size $dirsize, " \
10436                                    "previous limit $maxsize"
10437                 elif [ $rc -ne 0 ]; then
10438                         set_dir_limits 0 0
10439                         error_exit "return $rc received instead of expected " \
10440                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
10441                 fi
10442                 nfiles=$((nfiles + 1))
10443                 dirsize=$(stat -c%s "$DIR/$tdir")
10444         done
10445
10446         set_dir_limits 0 0
10447         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
10448 }
10449 run_test 129 "test directory size limit ========================"
10450
10451 OLDIFS="$IFS"
10452 cleanup_130() {
10453         trap 0
10454         IFS="$OLDIFS"
10455 }
10456
10457 test_130a() {
10458         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10459         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
10460
10461         trap cleanup_130 EXIT RETURN
10462
10463         local fm_file=$DIR/$tfile
10464         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
10465         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
10466                 error "dd failed for $fm_file"
10467
10468         # LU-1795: test filefrag/FIEMAP once, even if unsupported
10469         filefrag -ves $fm_file
10470         RC=$?
10471         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
10472                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10473         [ $RC != 0 ] && error "filefrag $fm_file failed"
10474
10475         filefrag_op=$(filefrag -ve -k $fm_file |
10476                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10477         lun=$($GETSTRIPE -i $fm_file)
10478
10479         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
10480         IFS=$'\n'
10481         tot_len=0
10482         for line in $filefrag_op
10483         do
10484                 frag_lun=`echo $line | cut -d: -f5`
10485                 ext_len=`echo $line | cut -d: -f4`
10486                 if (( $frag_lun != $lun )); then
10487                         cleanup_130
10488                         error "FIEMAP on 1-stripe file($fm_file) failed"
10489                         return
10490                 fi
10491                 (( tot_len += ext_len ))
10492         done
10493
10494         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
10495                 cleanup_130
10496                 error "FIEMAP on 1-stripe file($fm_file) failed;"
10497                 return
10498         fi
10499
10500         cleanup_130
10501
10502         echo "FIEMAP on single striped file succeeded"
10503 }
10504 run_test 130a "FIEMAP (1-stripe file)"
10505
10506 test_130b() {
10507         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
10508
10509         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10510         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
10511
10512         trap cleanup_130 EXIT RETURN
10513
10514         local fm_file=$DIR/$tfile
10515         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
10516                         error "setstripe on $fm_file"
10517         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
10518                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10519
10520         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
10521                 error "dd failed on $fm_file"
10522
10523         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10524         filefrag_op=$(filefrag -ve -k $fm_file |
10525                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10526
10527         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10528                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10529
10530         IFS=$'\n'
10531         tot_len=0
10532         num_luns=1
10533         for line in $filefrag_op
10534         do
10535                 frag_lun=$(echo $line | cut -d: -f5 |
10536                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10537                 ext_len=$(echo $line | cut -d: -f4)
10538                 if (( $frag_lun != $last_lun )); then
10539                         if (( tot_len != 1024 )); then
10540                                 cleanup_130
10541                                 error "FIEMAP on $fm_file failed; returned " \
10542                                 "len $tot_len for OST $last_lun instead of 1024"
10543                                 return
10544                         else
10545                                 (( num_luns += 1 ))
10546                                 tot_len=0
10547                         fi
10548                 fi
10549                 (( tot_len += ext_len ))
10550                 last_lun=$frag_lun
10551         done
10552         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
10553                 cleanup_130
10554                 error "FIEMAP on $fm_file failed; returned wrong number of " \
10555                         "luns or wrong len for OST $last_lun"
10556                 return
10557         fi
10558
10559         cleanup_130
10560
10561         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
10562 }
10563 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
10564
10565 test_130c() {
10566         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10567
10568         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10569         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
10570
10571         trap cleanup_130 EXIT RETURN
10572
10573         local fm_file=$DIR/$tfile
10574         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
10575         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
10576                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10577
10578         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
10579                         error "dd failed on $fm_file"
10580
10581         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10582         filefrag_op=$(filefrag -ve -k $fm_file |
10583                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10584
10585         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10586                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10587
10588         IFS=$'\n'
10589         tot_len=0
10590         num_luns=1
10591         for line in $filefrag_op
10592         do
10593                 frag_lun=$(echo $line | cut -d: -f5 |
10594                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10595                 ext_len=$(echo $line | cut -d: -f4)
10596                 if (( $frag_lun != $last_lun )); then
10597                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
10598                         if (( logical != 512 )); then
10599                                 cleanup_130
10600                                 error "FIEMAP on $fm_file failed; returned " \
10601                                 "logical start for lun $logical instead of 512"
10602                                 return
10603                         fi
10604                         if (( tot_len != 512 )); then
10605                                 cleanup_130
10606                                 error "FIEMAP on $fm_file failed; returned " \
10607                                 "len $tot_len for OST $last_lun instead of 1024"
10608                                 return
10609                         else
10610                                 (( num_luns += 1 ))
10611                                 tot_len=0
10612                         fi
10613                 fi
10614                 (( tot_len += ext_len ))
10615                 last_lun=$frag_lun
10616         done
10617         if (( num_luns != 2 || tot_len != 512 )); then
10618                 cleanup_130
10619                 error "FIEMAP on $fm_file failed; returned wrong number of " \
10620                         "luns or wrong len for OST $last_lun"
10621                 return
10622         fi
10623
10624         cleanup_130
10625
10626         echo "FIEMAP on 2-stripe file with hole succeeded"
10627 }
10628 run_test 130c "FIEMAP (2-stripe file with hole)"
10629
10630 test_130d() {
10631         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
10632
10633         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10634         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
10635
10636         trap cleanup_130 EXIT RETURN
10637
10638         local fm_file=$DIR/$tfile
10639         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
10640                         error "setstripe on $fm_file"
10641         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
10642                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10643
10644         local actual_stripe_count=$($GETSTRIPE -c $fm_file)
10645         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
10646                 error "dd failed on $fm_file"
10647
10648         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10649         filefrag_op=$(filefrag -ve -k $fm_file |
10650                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10651
10652         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10653                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10654
10655         IFS=$'\n'
10656         tot_len=0
10657         num_luns=1
10658         for line in $filefrag_op
10659         do
10660                 frag_lun=$(echo $line | cut -d: -f5 |
10661                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10662                 ext_len=$(echo $line | cut -d: -f4)
10663                 if (( $frag_lun != $last_lun )); then
10664                         if (( tot_len != 1024 )); then
10665                                 cleanup_130
10666                                 error "FIEMAP on $fm_file failed; returned " \
10667                                 "len $tot_len for OST $last_lun instead of 1024"
10668                                 return
10669                         else
10670                                 (( num_luns += 1 ))
10671                                 tot_len=0
10672                         fi
10673                 fi
10674                 (( tot_len += ext_len ))
10675                 last_lun=$frag_lun
10676         done
10677         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
10678                 cleanup_130
10679                 error "FIEMAP on $fm_file failed; returned wrong number of " \
10680                         "luns or wrong len for OST $last_lun"
10681                 return
10682         fi
10683
10684         cleanup_130
10685
10686         echo "FIEMAP on N-stripe file succeeded"
10687 }
10688 run_test 130d "FIEMAP (N-stripe file)"
10689
10690 test_130e() {
10691         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10692
10693         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10694         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
10695
10696         trap cleanup_130 EXIT RETURN
10697
10698         local fm_file=$DIR/$tfile
10699         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
10700         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
10701                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10702
10703         NUM_BLKS=512
10704         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
10705         for ((i = 0; i < $NUM_BLKS; i++))
10706         do
10707                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
10708         done
10709
10710         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10711         filefrag_op=$(filefrag -ve -k $fm_file |
10712                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10713
10714         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10715                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10716
10717         IFS=$'\n'
10718         tot_len=0
10719         num_luns=1
10720         for line in $filefrag_op
10721         do
10722                 frag_lun=$(echo $line | cut -d: -f5 |
10723                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10724                 ext_len=$(echo $line | cut -d: -f4)
10725                 if (( $frag_lun != $last_lun )); then
10726                         if (( tot_len != $EXPECTED_LEN )); then
10727                                 cleanup_130
10728                                 error "FIEMAP on $fm_file failed; returned " \
10729                                 "len $tot_len for OST $last_lun instead " \
10730                                 "of $EXPECTED_LEN"
10731                                 return
10732                         else
10733                                 (( num_luns += 1 ))
10734                                 tot_len=0
10735                         fi
10736                 fi
10737                 (( tot_len += ext_len ))
10738                 last_lun=$frag_lun
10739         done
10740         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
10741                 cleanup_130
10742                 error "FIEMAP on $fm_file failed; returned wrong number " \
10743                         "of luns or wrong len for OST $last_lun"
10744                 return
10745         fi
10746
10747         cleanup_130
10748
10749         echo "FIEMAP with continuation calls succeeded"
10750 }
10751 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
10752
10753 test_130f() {
10754         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10755         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
10756
10757         local fm_file=$DIR/$tfile
10758         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
10759                 error "multiop create with lov_delay_create on $fm_file"
10760
10761         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10762         filefrag_extents=$(filefrag -vek $fm_file |
10763                            awk '/extents? found/ { print $2 }')
10764         if [[ "$filefrag_extents" != "0" ]]; then
10765                 error "FIEMAP on $fm_file failed; " \
10766                       "returned $filefrag_extents expected 0"
10767         fi
10768
10769         rm -f $fm_file
10770 }
10771 run_test 130f "FIEMAP (unstriped file)"
10772
10773 # Test for writev/readv
10774 test_131a() {
10775         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
10776                 error "writev test failed"
10777         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
10778                 error "readv failed"
10779         rm -f $DIR/$tfile
10780 }
10781 run_test 131a "test iov's crossing stripe boundary for writev/readv"
10782
10783 test_131b() {
10784         local fsize=$((524288 + 1048576 + 1572864))
10785         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
10786                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
10787                         error "append writev test failed"
10788
10789         ((fsize += 1572864 + 1048576))
10790         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
10791                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
10792                         error "append writev test failed"
10793         rm -f $DIR/$tfile
10794 }
10795 run_test 131b "test append writev"
10796
10797 test_131c() {
10798         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
10799         error "NOT PASS"
10800 }
10801 run_test 131c "test read/write on file w/o objects"
10802
10803 test_131d() {
10804         rwv -f $DIR/$tfile -w -n 1 1572864
10805         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
10806         if [ "$NOB" != 1572864 ]; then
10807                 error "Short read filed: read $NOB bytes instead of 1572864"
10808         fi
10809         rm -f $DIR/$tfile
10810 }
10811 run_test 131d "test short read"
10812
10813 test_131e() {
10814         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
10815         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
10816         error "read hitting hole failed"
10817         rm -f $DIR/$tfile
10818 }
10819 run_test 131e "test read hitting hole"
10820
10821 check_stats() {
10822         local facet=$1
10823         local op=$2
10824         local want=${3:-0}
10825         local res
10826
10827         case $facet in
10828         mds*) res=$(do_facet $facet \
10829                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
10830                  ;;
10831         ost*) res=$(do_facet $facet \
10832                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
10833                  ;;
10834         *) error "Wrong facet '$facet'" ;;
10835         esac
10836         [ "$res" ] || error "The counter for $op on $facet was not incremented"
10837         # if the argument $3 is zero, it means any stat increment is ok.
10838         if [[ $want -gt 0 ]]; then
10839                 local count=$(echo $res | awk '{ print $2 }')
10840                 [[ $count -ne $want ]] &&
10841                         error "The $op counter on $facet is $count, not $want"
10842         fi
10843 }
10844
10845 test_133a() {
10846         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10847         remote_ost_nodsh && skip "remote OST with nodsh"
10848         remote_mds_nodsh && skip "remote MDS with nodsh"
10849         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
10850                 skip_env "MDS doesn't support rename stats"
10851
10852         local testdir=$DIR/${tdir}/stats_testdir
10853
10854         mkdir -p $DIR/${tdir}
10855
10856         # clear stats.
10857         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10858         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10859
10860         # verify mdt stats first.
10861         mkdir ${testdir} || error "mkdir failed"
10862         check_stats $SINGLEMDS "mkdir" 1
10863         touch ${testdir}/${tfile} || error "touch failed"
10864         check_stats $SINGLEMDS "open" 1
10865         check_stats $SINGLEMDS "close" 1
10866         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
10867                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
10868                 check_stats $SINGLEMDS "mknod" 2
10869         }
10870         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
10871         check_stats $SINGLEMDS "unlink" 1
10872         rm -f ${testdir}/${tfile} || error "file remove failed"
10873         check_stats $SINGLEMDS "unlink" 2
10874
10875         # remove working dir and check mdt stats again.
10876         rmdir ${testdir} || error "rmdir failed"
10877         check_stats $SINGLEMDS "rmdir" 1
10878
10879         local testdir1=$DIR/${tdir}/stats_testdir1
10880         mkdir -p ${testdir}
10881         mkdir -p ${testdir1}
10882         touch ${testdir1}/test1
10883         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
10884         check_stats $SINGLEMDS "crossdir_rename" 1
10885
10886         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
10887         check_stats $SINGLEMDS "samedir_rename" 1
10888
10889         rm -rf $DIR/${tdir}
10890 }
10891 run_test 133a "Verifying MDT stats ========================================"
10892
10893 test_133b() {
10894         local res
10895
10896         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10897         remote_ost_nodsh && skip "remote OST with nodsh"
10898         remote_mds_nodsh && skip "remote MDS with nodsh"
10899
10900         local testdir=$DIR/${tdir}/stats_testdir
10901
10902         mkdir -p ${testdir} || error "mkdir failed"
10903         touch ${testdir}/${tfile} || error "touch failed"
10904         cancel_lru_locks mdc
10905
10906         # clear stats.
10907         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10908         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10909
10910         # extra mdt stats verification.
10911         chmod 444 ${testdir}/${tfile} || error "chmod failed"
10912         check_stats $SINGLEMDS "setattr" 1
10913         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10914         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
10915         then            # LU-1740
10916                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
10917                 check_stats $SINGLEMDS "getattr" 1
10918         fi
10919         rm -rf $DIR/${tdir}
10920
10921         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
10922         # so the check below is not reliable
10923         [ $MDSCOUNT -eq 1 ] || return 0
10924
10925         # Sleep to avoid a cached response.
10926         #define OBD_STATFS_CACHE_SECONDS 1
10927         sleep 2
10928         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10929         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
10930         $LFS df || error "lfs failed"
10931         check_stats $SINGLEMDS "statfs" 1
10932
10933         # check aggregated statfs (LU-10018)
10934         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
10935                 return 0
10936         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
10937                 return 0
10938         sleep 2
10939         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10940         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
10941         df $DIR
10942         check_stats $SINGLEMDS "statfs" 1
10943
10944         # We want to check that the client didn't send OST_STATFS to
10945         # ost1 but the MDT also uses OST_STATFS for precreate. So some
10946         # extra care is needed here.
10947         if remote_mds; then
10948                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
10949                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
10950
10951                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
10952                 [ "$res" ] && error "OST got STATFS"
10953         fi
10954
10955         return 0
10956 }
10957 run_test 133b "Verifying extra MDT stats =================================="
10958
10959 test_133c() {
10960         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10961         remote_ost_nodsh && skip "remote OST with nodsh"
10962         remote_mds_nodsh && skip "remote MDS with nodsh"
10963
10964         local testdir=$DIR/$tdir/stats_testdir
10965
10966         test_mkdir -p $testdir
10967
10968         # verify obdfilter stats.
10969         $SETSTRIPE -c 1 -i 0 $testdir/$tfile
10970         sync
10971         cancel_lru_locks osc
10972         wait_delete_completed
10973
10974         # clear stats.
10975         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10976         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10977
10978         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
10979                 error "dd failed"
10980         sync
10981         cancel_lru_locks osc
10982         check_stats ost1 "write" 1
10983
10984         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
10985         check_stats ost1 "read" 1
10986
10987         > $testdir/$tfile || error "truncate failed"
10988         check_stats ost1 "punch" 1
10989
10990         rm -f $testdir/$tfile || error "file remove failed"
10991         wait_delete_completed
10992         check_stats ost1 "destroy" 1
10993
10994         rm -rf $DIR/$tdir
10995 }
10996 run_test 133c "Verifying OST stats ========================================"
10997
10998 order_2() {
10999         local value=$1
11000         local orig=$value
11001         local order=1
11002
11003         while [ $value -ge 2 ]; do
11004                 order=$((order*2))
11005                 value=$((value/2))
11006         done
11007
11008         if [ $orig -gt $order ]; then
11009                 order=$((order*2))
11010         fi
11011         echo $order
11012 }
11013
11014 size_in_KMGT() {
11015     local value=$1
11016     local size=('K' 'M' 'G' 'T');
11017     local i=0
11018     local size_string=$value
11019
11020     while [ $value -ge 1024 ]; do
11021         if [ $i -gt 3 ]; then
11022             #T is the biggest unit we get here, if that is bigger,
11023             #just return XXXT
11024             size_string=${value}T
11025             break
11026         fi
11027         value=$((value >> 10))
11028         if [ $value -lt 1024 ]; then
11029             size_string=${value}${size[$i]}
11030             break
11031         fi
11032         i=$((i + 1))
11033     done
11034
11035     echo $size_string
11036 }
11037
11038 get_rename_size() {
11039         local size=$1
11040         local context=${2:-.}
11041         local sample=$(do_facet $SINGLEMDS $LCTL \
11042                 get_param mdt.$FSNAME-MDT0000.rename_stats |
11043                 grep -A1 $context |
11044                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
11045         echo $sample
11046 }
11047
11048 test_133d() {
11049         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11050         remote_ost_nodsh && skip "remote OST with nodsh"
11051         remote_mds_nodsh && skip "remote MDS with nodsh"
11052         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
11053                 skip_env "MDS doesn't support rename stats"
11054
11055         local testdir1=$DIR/${tdir}/stats_testdir1
11056         local testdir2=$DIR/${tdir}/stats_testdir2
11057         mkdir -p $DIR/${tdir}
11058
11059         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
11060
11061         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
11062         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
11063
11064         createmany -o $testdir1/test 512 || error "createmany failed"
11065
11066         # check samedir rename size
11067         mv ${testdir1}/test0 ${testdir1}/test_0
11068
11069         local testdir1_size=$(ls -l $DIR/${tdir} |
11070                 awk '/stats_testdir1/ {print $5}')
11071         local testdir2_size=$(ls -l $DIR/${tdir} |
11072                 awk '/stats_testdir2/ {print $5}')
11073
11074         testdir1_size=$(order_2 $testdir1_size)
11075         testdir2_size=$(order_2 $testdir2_size)
11076
11077         testdir1_size=$(size_in_KMGT $testdir1_size)
11078         testdir2_size=$(size_in_KMGT $testdir2_size)
11079
11080         echo "source rename dir size: ${testdir1_size}"
11081         echo "target rename dir size: ${testdir2_size}"
11082
11083         local cmd="do_facet $SINGLEMDS $LCTL "
11084         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
11085
11086         eval $cmd || error "$cmd failed"
11087         local samedir=$($cmd | grep 'same_dir')
11088         local same_sample=$(get_rename_size $testdir1_size)
11089         [ -z "$samedir" ] && error "samedir_rename_size count error"
11090         [[ $same_sample -eq 1 ]] ||
11091                 error "samedir_rename_size error $same_sample"
11092         echo "Check same dir rename stats success"
11093
11094         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
11095
11096         # check crossdir rename size
11097         mv ${testdir1}/test_0 ${testdir2}/test_0
11098
11099         testdir1_size=$(ls -l $DIR/${tdir} |
11100                 awk '/stats_testdir1/ {print $5}')
11101         testdir2_size=$(ls -l $DIR/${tdir} |
11102                 awk '/stats_testdir2/ {print $5}')
11103
11104         testdir1_size=$(order_2 $testdir1_size)
11105         testdir2_size=$(order_2 $testdir2_size)
11106
11107         testdir1_size=$(size_in_KMGT $testdir1_size)
11108         testdir2_size=$(size_in_KMGT $testdir2_size)
11109
11110         echo "source rename dir size: ${testdir1_size}"
11111         echo "target rename dir size: ${testdir2_size}"
11112
11113         eval $cmd || error "$cmd failed"
11114         local crossdir=$($cmd | grep 'crossdir')
11115         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
11116         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
11117         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
11118         [[ $src_sample -eq 1 ]] ||
11119                 error "crossdir_rename_size error $src_sample"
11120         [[ $tgt_sample -eq 1 ]] ||
11121                 error "crossdir_rename_size error $tgt_sample"
11122         echo "Check cross dir rename stats success"
11123         rm -rf $DIR/${tdir}
11124 }
11125 run_test 133d "Verifying rename_stats ========================================"
11126
11127 test_133e() {
11128         remote_mds_nodsh && skip "remote MDS with nodsh"
11129         remote_ost_nodsh && skip "remote OST with nodsh"
11130         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11131
11132         local testdir=$DIR/${tdir}/stats_testdir
11133         local ctr f0 f1 bs=32768 count=42 sum
11134
11135         mkdir -p ${testdir} || error "mkdir failed"
11136
11137         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
11138
11139         for ctr in {write,read}_bytes; do
11140                 sync
11141                 cancel_lru_locks osc
11142
11143                 do_facet ost1 $LCTL set_param -n \
11144                         "obdfilter.*.exports.clear=clear"
11145
11146                 if [ $ctr = write_bytes ]; then
11147                         f0=/dev/zero
11148                         f1=${testdir}/${tfile}
11149                 else
11150                         f0=${testdir}/${tfile}
11151                         f1=/dev/null
11152                 fi
11153
11154                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
11155                         error "dd failed"
11156                 sync
11157                 cancel_lru_locks osc
11158
11159                 sum=$(do_facet ost1 $LCTL get_param \
11160                         "obdfilter.*.exports.*.stats" |
11161                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
11162                                 $1 == ctr { sum += $7 }
11163                                 END { printf("%0.0f", sum) }')
11164
11165                 if ((sum != bs * count)); then
11166                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
11167                 fi
11168         done
11169
11170         rm -rf $DIR/${tdir}
11171 }
11172 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
11173
11174 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
11175
11176 # Some versions of find (4.5.11, 4.5.14) included in CentOS 7.3-7.5 do
11177 # not honor the -ignore_readdir_race option correctly. So we call
11178 # error_ignore() rather than error() in these cases. See LU-11152.
11179 error_133() {
11180         if (find --version; do_facet mds1 find --version) |
11181                 grep -q '\b4\.5\.1[1-4]\b'; then
11182                 error_ignore LU-11152 "$@"
11183         else
11184                 error "$@"
11185         fi
11186 }
11187
11188 test_133f() {
11189         # First without trusting modes.
11190         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
11191         echo "proc_dirs='$proc_dirs'"
11192         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
11193         find $proc_dirs -exec cat '{}' \; &> /dev/null
11194
11195         # Second verifying readability.
11196         $LCTL get_param -R '*' &> /dev/null
11197
11198         # Verifing writability with badarea_io.
11199         find $proc_dirs \
11200                 -ignore_readdir_race \
11201                 -type f \
11202                 -not -name force_lbug \
11203                 -not -name changelog_mask \
11204                 -exec badarea_io '{}' \; ||
11205                         error_133 "find $proc_dirs failed"
11206 }
11207 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
11208
11209 test_133g() {
11210         remote_mds_nodsh && skip "remote MDS with nodsh"
11211         remote_ost_nodsh && skip "remote OST with nodsh"
11212
11213         # eventually, this can also be replaced with "lctl get_param -R",
11214         # but not until that option is always available on the server
11215         local facet
11216         for facet in mds1 ost1; do
11217                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
11218                         skip_noexit "Too old lustre on $facet"
11219                 local facet_proc_dirs=$(do_facet $facet \
11220                                         \\\ls -d $proc_regexp 2>/dev/null)
11221                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
11222                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
11223                 do_facet $facet find $facet_proc_dirs \
11224                         ! -name req_history \
11225                         -exec cat '{}' \\\; &> /dev/null
11226
11227                 do_facet $facet find $facet_proc_dirs \
11228                         ! -name req_history \
11229                         -type f \
11230                         -exec cat '{}' \\\; &> /dev/null ||
11231                                 error "proc file read failed"
11232
11233                 do_facet $facet find $facet_proc_dirs \
11234                         -ignore_readdir_race \
11235                         -type f \
11236                         -not -name force_lbug \
11237                         -not -name changelog_mask \
11238                         -exec badarea_io '{}' \\\; ||
11239                                 error_133 "$facet find $facet_proc_dirs failed"
11240         done
11241
11242         # remount the FS in case writes/reads /proc break the FS
11243         cleanup || error "failed to unmount"
11244         setup || error "failed to setup"
11245         true
11246 }
11247 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
11248
11249 test_133h() {
11250         remote_mds_nodsh && skip "remote MDS with nodsh"
11251         remote_ost_nodsh && skip "remote OST with nodsh"
11252         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
11253                 skip "Need MDS version at least 2.9.54"
11254
11255         local facet
11256
11257         for facet in client mds1 ost1; do
11258                 local facet_proc_dirs=$(do_facet $facet \
11259                                         \\\ls -d $proc_regexp 2> /dev/null)
11260                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
11261                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
11262                 # Get the list of files that are missing the terminating newline
11263                 local missing=($(do_facet $facet \
11264                         find ${facet_proc_dirs} -type f \|              \
11265                                 while read F\; do                       \
11266                                         awk -v FS='\v' -v RS='\v\v'     \
11267                                         "'END { if(NR>0 &&              \
11268                                         \\\$NF !~ /.*\\\n\$/)           \
11269                                                 print FILENAME}'"       \
11270                                         '\$F'\;                         \
11271                                 done 2>/dev/null))
11272                 [ ${#missing[*]} -eq 0 ] ||
11273                         error "files do not end with newline: ${missing[*]}"
11274         done
11275 }
11276 run_test 133h "Proc files should end with newlines"
11277
11278 test_134a() {
11279         remote_mds_nodsh && skip "remote MDS with nodsh"
11280         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
11281                 skip "Need MDS version at least 2.7.54"
11282
11283         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
11284         cancel_lru_locks mdc
11285
11286         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11287         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11288         [ $unused -eq 0 ] || error "$unused locks are not cleared"
11289
11290         local nr=1000
11291         createmany -o $DIR/$tdir/f $nr ||
11292                 error "failed to create $nr files in $DIR/$tdir"
11293         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11294
11295         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
11296         do_facet mds1 $LCTL set_param fail_loc=0x327
11297         do_facet mds1 $LCTL set_param fail_val=500
11298         touch $DIR/$tdir/m
11299
11300         echo "sleep 10 seconds ..."
11301         sleep 10
11302         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
11303
11304         do_facet mds1 $LCTL set_param fail_loc=0
11305         do_facet mds1 $LCTL set_param fail_val=0
11306         [ $lck_cnt -lt $unused ] ||
11307                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
11308
11309         rm $DIR/$tdir/m
11310         unlinkmany $DIR/$tdir/f $nr
11311 }
11312 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
11313
11314 test_134b() {
11315         remote_mds_nodsh && skip "remote MDS with nodsh"
11316         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
11317                 skip "Need MDS version at least 2.7.54"
11318
11319         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
11320         cancel_lru_locks mdc
11321
11322         local low_wm=$(do_facet mds1 $LCTL get_param -n \
11323                         ldlm.lock_reclaim_threshold_mb)
11324         # disable reclaim temporarily
11325         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
11326
11327         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
11328         do_facet mds1 $LCTL set_param fail_loc=0x328
11329         do_facet mds1 $LCTL set_param fail_val=500
11330
11331         $LCTL set_param debug=+trace
11332
11333         local nr=600
11334         createmany -o $DIR/$tdir/f $nr &
11335         local create_pid=$!
11336
11337         echo "Sleep $TIMEOUT seconds ..."
11338         sleep $TIMEOUT
11339         if ! ps -p $create_pid  > /dev/null 2>&1; then
11340                 do_facet mds1 $LCTL set_param fail_loc=0
11341                 do_facet mds1 $LCTL set_param fail_val=0
11342                 do_facet mds1 $LCTL set_param \
11343                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
11344                 error "createmany finished incorrectly!"
11345         fi
11346         do_facet mds1 $LCTL set_param fail_loc=0
11347         do_facet mds1 $LCTL set_param fail_val=0
11348         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
11349         wait $create_pid || return 1
11350
11351         unlinkmany $DIR/$tdir/f $nr
11352 }
11353 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
11354
11355 test_140() { #bug-17379
11356         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11357
11358         test_mkdir $DIR/$tdir
11359         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
11360         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
11361
11362         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
11363         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
11364         local i=0
11365         while i=$((i + 1)); do
11366                 test_mkdir $i
11367                 cd $i || error "Changing to $i"
11368                 ln -s ../stat stat || error "Creating stat symlink"
11369                 # Read the symlink until ELOOP present,
11370                 # not LBUGing the system is considered success,
11371                 # we didn't overrun the stack.
11372                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
11373                 if [ $ret -ne 0 ]; then
11374                         if [ $ret -eq 40 ]; then
11375                                 break  # -ELOOP
11376                         else
11377                                 error "Open stat symlink"
11378                                         return
11379                         fi
11380                 fi
11381         done
11382         i=$((i - 1))
11383         echo "The symlink depth = $i"
11384         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
11385                                         error "Invalid symlink depth"
11386
11387         # Test recursive symlink
11388         ln -s symlink_self symlink_self
11389         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
11390         echo "open symlink_self returns $ret"
11391         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
11392 }
11393 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
11394
11395 test_150() {
11396         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11397
11398         local TF="$TMP/$tfile"
11399
11400         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
11401         cp $TF $DIR/$tfile
11402         cancel_lru_locks $OSC
11403         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
11404         remount_client $MOUNT
11405         df -P $MOUNT
11406         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
11407
11408         $TRUNCATE $TF 6000
11409         $TRUNCATE $DIR/$tfile 6000
11410         cancel_lru_locks $OSC
11411         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
11412
11413         echo "12345" >>$TF
11414         echo "12345" >>$DIR/$tfile
11415         cancel_lru_locks $OSC
11416         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
11417
11418         echo "12345" >>$TF
11419         echo "12345" >>$DIR/$tfile
11420         cancel_lru_locks $OSC
11421         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
11422
11423         rm -f $TF
11424         true
11425 }
11426 run_test 150 "truncate/append tests"
11427
11428 #LU-2902 roc_hit was not able to read all values from lproc
11429 function roc_hit_init() {
11430         local list=$(comma_list $(osts_nodes))
11431         local dir=$DIR/$tdir-check
11432         local file=$dir/$tfile
11433         local BEFORE
11434         local AFTER
11435         local idx
11436
11437         test_mkdir $dir
11438         #use setstripe to do a write to every ost
11439         for i in $(seq 0 $((OSTCOUNT-1))); do
11440                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
11441                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
11442                 idx=$(printf %04x $i)
11443                 BEFORE=$(get_osd_param $list *OST*$idx stats |
11444                         awk '$1 == "cache_access" {sum += $7}
11445                                 END { printf("%0.0f", sum) }')
11446
11447                 cancel_lru_locks osc
11448                 cat $file >/dev/null
11449
11450                 AFTER=$(get_osd_param $list *OST*$idx stats |
11451                         awk '$1 == "cache_access" {sum += $7}
11452                                 END { printf("%0.0f", sum) }')
11453
11454                 echo BEFORE:$BEFORE AFTER:$AFTER
11455                 if ! let "AFTER - BEFORE == 4"; then
11456                         rm -rf $dir
11457                         error "roc_hit is not safe to use"
11458                 fi
11459                 rm $file
11460         done
11461
11462         rm -rf $dir
11463 }
11464
11465 function roc_hit() {
11466         local list=$(comma_list $(osts_nodes))
11467         echo $(get_osd_param $list '' stats |
11468                 awk '$1 == "cache_hit" {sum += $7}
11469                         END { printf("%0.0f", sum) }')
11470 }
11471
11472 function set_cache() {
11473         local on=1
11474
11475         if [ "$2" == "off" ]; then
11476                 on=0;
11477         fi
11478         local list=$(comma_list $(osts_nodes))
11479         set_osd_param $list '' $1_cache_enable $on
11480
11481         cancel_lru_locks osc
11482 }
11483
11484 test_151() {
11485         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11486         remote_ost_nodsh && skip "remote OST with nodsh"
11487
11488         local CPAGES=3
11489         local list=$(comma_list $(osts_nodes))
11490
11491         # check whether obdfilter is cache capable at all
11492         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
11493                 skip "not cache-capable obdfilter"
11494         fi
11495
11496         # check cache is enabled on all obdfilters
11497         if get_osd_param $list '' read_cache_enable | grep 0; then
11498                 skip "oss cache is disabled"
11499         fi
11500
11501         set_osd_param $list '' writethrough_cache_enable 1
11502
11503         # check write cache is enabled on all obdfilters
11504         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
11505                 skip "oss write cache is NOT enabled"
11506         fi
11507
11508         roc_hit_init
11509
11510         #define OBD_FAIL_OBD_NO_LRU  0x609
11511         do_nodes $list $LCTL set_param fail_loc=0x609
11512
11513         # pages should be in the case right after write
11514         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
11515                 error "dd failed"
11516
11517         local BEFORE=$(roc_hit)
11518         cancel_lru_locks osc
11519         cat $DIR/$tfile >/dev/null
11520         local AFTER=$(roc_hit)
11521
11522         do_nodes $list $LCTL set_param fail_loc=0
11523
11524         if ! let "AFTER - BEFORE == CPAGES"; then
11525                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11526         fi
11527
11528         # the following read invalidates the cache
11529         cancel_lru_locks osc
11530         set_osd_param $list '' read_cache_enable 0
11531         cat $DIR/$tfile >/dev/null
11532
11533         # now data shouldn't be found in the cache
11534         BEFORE=$(roc_hit)
11535         cancel_lru_locks osc
11536         cat $DIR/$tfile >/dev/null
11537         AFTER=$(roc_hit)
11538         if let "AFTER - BEFORE != 0"; then
11539                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11540         fi
11541
11542         set_osd_param $list '' read_cache_enable 1
11543         rm -f $DIR/$tfile
11544 }
11545 run_test 151 "test cache on oss and controls ==============================="
11546
11547 test_152() {
11548         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11549
11550         local TF="$TMP/$tfile"
11551
11552         # simulate ENOMEM during write
11553 #define OBD_FAIL_OST_NOMEM      0x226
11554         lctl set_param fail_loc=0x80000226
11555         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
11556         cp $TF $DIR/$tfile
11557         sync || error "sync failed"
11558         lctl set_param fail_loc=0
11559
11560         # discard client's cache
11561         cancel_lru_locks osc
11562
11563         # simulate ENOMEM during read
11564         lctl set_param fail_loc=0x80000226
11565         cmp $TF $DIR/$tfile || error "cmp failed"
11566         lctl set_param fail_loc=0
11567
11568         rm -f $TF
11569 }
11570 run_test 152 "test read/write with enomem ============================"
11571
11572 test_153() {
11573         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
11574 }
11575 run_test 153 "test if fdatasync does not crash ======================="
11576
11577 dot_lustre_fid_permission_check() {
11578         local fid=$1
11579         local ffid=$MOUNT/.lustre/fid/$fid
11580         local test_dir=$2
11581
11582         echo "stat fid $fid"
11583         stat $ffid > /dev/null || error "stat $ffid failed."
11584         echo "touch fid $fid"
11585         touch $ffid || error "touch $ffid failed."
11586         echo "write to fid $fid"
11587         cat /etc/hosts > $ffid || error "write $ffid failed."
11588         echo "read fid $fid"
11589         diff /etc/hosts $ffid || error "read $ffid failed."
11590         echo "append write to fid $fid"
11591         cat /etc/hosts >> $ffid || error "append write $ffid failed."
11592         echo "rename fid $fid"
11593         mv $ffid $test_dir/$tfile.1 &&
11594                 error "rename $ffid to $tfile.1 should fail."
11595         touch $test_dir/$tfile.1
11596         mv $test_dir/$tfile.1 $ffid &&
11597                 error "rename $tfile.1 to $ffid should fail."
11598         rm -f $test_dir/$tfile.1
11599         echo "truncate fid $fid"
11600         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
11601         echo "link fid $fid"
11602         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
11603         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
11604                 echo "setfacl fid $fid"
11605                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
11606                 echo "getfacl fid $fid"
11607                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
11608         fi
11609         echo "unlink fid $fid"
11610         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
11611         echo "mknod fid $fid"
11612         mknod $ffid c 1 3 && error "mknod $ffid should fail."
11613
11614         fid=[0xf00000400:0x1:0x0]
11615         ffid=$MOUNT/.lustre/fid/$fid
11616
11617         echo "stat non-exist fid $fid"
11618         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
11619         echo "write to non-exist fid $fid"
11620         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
11621         echo "link new fid $fid"
11622         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
11623
11624         mkdir -p $test_dir/$tdir
11625         touch $test_dir/$tdir/$tfile
11626         fid=$($LFS path2fid $test_dir/$tdir)
11627         rc=$?
11628         [ $rc -ne 0 ] &&
11629                 error "error: could not get fid for $test_dir/$dir/$tfile."
11630
11631         ffid=$MOUNT/.lustre/fid/$fid
11632
11633         echo "ls $fid"
11634         ls $ffid > /dev/null || error "ls $ffid failed."
11635         echo "touch $fid/$tfile.1"
11636         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
11637
11638         echo "touch $MOUNT/.lustre/fid/$tfile"
11639         touch $MOUNT/.lustre/fid/$tfile && \
11640                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
11641
11642         echo "setxattr to $MOUNT/.lustre/fid"
11643         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
11644
11645         echo "listxattr for $MOUNT/.lustre/fid"
11646         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
11647
11648         echo "delxattr from $MOUNT/.lustre/fid"
11649         setfattr -x trusted.name1 $MOUNT/.lustre/fid
11650
11651         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
11652         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
11653                 error "touch invalid fid should fail."
11654
11655         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
11656         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
11657                 error "touch non-normal fid should fail."
11658
11659         echo "rename $tdir to $MOUNT/.lustre/fid"
11660         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
11661                 error "rename to $MOUNT/.lustre/fid should fail."
11662
11663         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
11664         then            # LU-3547
11665                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
11666                 local new_obf_mode=777
11667
11668                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
11669                 chmod $new_obf_mode $DIR/.lustre/fid ||
11670                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
11671
11672                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
11673                 [ $obf_mode -eq $new_obf_mode ] ||
11674                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
11675
11676                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
11677                 chmod $old_obf_mode $DIR/.lustre/fid ||
11678                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
11679         fi
11680
11681         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
11682         fid=$($LFS path2fid $test_dir/$tfile-2)
11683
11684         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
11685         then # LU-5424
11686                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
11687                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
11688                         error "create lov data thru .lustre failed"
11689         fi
11690         echo "cp /etc/passwd $test_dir/$tfile-2"
11691         cp /etc/passwd $test_dir/$tfile-2 ||
11692                 error "copy to $test_dir/$tfile-2 failed."
11693         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
11694         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
11695                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
11696
11697         rm -rf $test_dir/tfile.lnk
11698         rm -rf $test_dir/$tfile-2
11699 }
11700
11701 test_154A() {
11702         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
11703                 skip "Need MDS version at least 2.4.1"
11704
11705         local tf=$DIR/$tfile
11706         touch $tf
11707
11708         local fid=$($LFS path2fid $tf)
11709         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
11710
11711         # check that we get the same pathname back
11712         local found=$($LFS fid2path $MOUNT "$fid")
11713         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
11714         [ "$found" == "$tf" ] ||
11715                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
11716 }
11717 run_test 154A "lfs path2fid and fid2path basic checks"
11718
11719 test_154B() {
11720         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
11721                 skip "Need MDS version at least 2.4.1"
11722
11723         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
11724         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
11725         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
11726         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
11727
11728         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
11729         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
11730
11731         # check that we get the same pathname
11732         echo "PFID: $PFID, name: $name"
11733         local FOUND=$($LFS fid2path $MOUNT "$PFID")
11734         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
11735         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
11736                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
11737
11738         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
11739 }
11740 run_test 154B "verify the ll_decode_linkea tool"
11741
11742 test_154a() {
11743         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11744         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
11745         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
11746                 skip "Need MDS version at least 2.2.51"
11747         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
11748
11749         cp /etc/hosts $DIR/$tfile
11750
11751         fid=$($LFS path2fid $DIR/$tfile)
11752         rc=$?
11753         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
11754
11755         dot_lustre_fid_permission_check "$fid" $DIR ||
11756                 error "dot lustre permission check $fid failed"
11757
11758         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
11759
11760         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
11761
11762         touch $MOUNT/.lustre/file &&
11763                 error "creation is not allowed under .lustre"
11764
11765         mkdir $MOUNT/.lustre/dir &&
11766                 error "mkdir is not allowed under .lustre"
11767
11768         rm -rf $DIR/$tfile
11769 }
11770 run_test 154a "Open-by-FID"
11771
11772 test_154b() {
11773         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11774         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
11775         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
11776         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
11777                 skip "Need MDS version at least 2.2.51"
11778
11779         local remote_dir=$DIR/$tdir/remote_dir
11780         local MDTIDX=1
11781         local rc=0
11782
11783         mkdir -p $DIR/$tdir
11784         $LFS mkdir -i $MDTIDX $remote_dir ||
11785                 error "create remote directory failed"
11786
11787         cp /etc/hosts $remote_dir/$tfile
11788
11789         fid=$($LFS path2fid $remote_dir/$tfile)
11790         rc=$?
11791         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
11792
11793         dot_lustre_fid_permission_check "$fid" $remote_dir ||
11794                 error "dot lustre permission check $fid failed"
11795         rm -rf $DIR/$tdir
11796 }
11797 run_test 154b "Open-by-FID for remote directory"
11798
11799 test_154c() {
11800         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
11801                 skip "Need MDS version at least 2.4.1"
11802
11803         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
11804         local FID1=$($LFS path2fid $DIR/$tfile.1)
11805         local FID2=$($LFS path2fid $DIR/$tfile.2)
11806         local FID3=$($LFS path2fid $DIR/$tfile.3)
11807
11808         local N=1
11809         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
11810                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
11811                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
11812                 local want=FID$N
11813                 [ "$FID" = "${!want}" ] ||
11814                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
11815                 N=$((N + 1))
11816         done
11817
11818         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
11819         do
11820                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
11821                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
11822                 N=$((N + 1))
11823         done
11824 }
11825 run_test 154c "lfs path2fid and fid2path multiple arguments"
11826
11827 test_154d() {
11828         remote_mds_nodsh && skip "remote MDS with nodsh"
11829         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
11830                 skip "Need MDS version at least 2.5.53"
11831
11832         if remote_mds; then
11833                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
11834         else
11835                 nid="0@lo"
11836         fi
11837         local proc_ofile="mdt.*.exports.'$nid'.open_files"
11838         local fd
11839         local cmd
11840
11841         rm -f $DIR/$tfile
11842         touch $DIR/$tfile
11843
11844         local fid=$($LFS path2fid $DIR/$tfile)
11845         # Open the file
11846         fd=$(free_fd)
11847         cmd="exec $fd<$DIR/$tfile"
11848         eval $cmd
11849         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
11850         echo "$fid_list" | grep "$fid"
11851         rc=$?
11852
11853         cmd="exec $fd>/dev/null"
11854         eval $cmd
11855         if [ $rc -ne 0 ]; then
11856                 error "FID $fid not found in open files list $fid_list"
11857         fi
11858 }
11859 run_test 154d "Verify open file fid"
11860
11861 test_154e()
11862 {
11863         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
11864                 skip "Need MDS version at least 2.6.50"
11865
11866         if ls -a $MOUNT | grep -q '^\.lustre$'; then
11867                 error ".lustre returned by readdir"
11868         fi
11869 }
11870 run_test 154e ".lustre is not returned by readdir"
11871
11872 test_154f() {
11873         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
11874
11875         # create parent directory on a single MDT to avoid cross-MDT hardlinks
11876         test_mkdir -p -c1 $DIR/$tdir/d
11877         # test dirs inherit from its stripe
11878         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
11879         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
11880         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
11881         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
11882         touch $DIR/f
11883
11884         # get fid of parents
11885         local FID0=$($LFS path2fid $DIR/$tdir/d)
11886         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
11887         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
11888         local FID3=$($LFS path2fid $DIR)
11889
11890         # check that path2fid --parents returns expected <parent_fid>/name
11891         # 1) test for a directory (single parent)
11892         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
11893         [ "$parent" == "$FID0/foo1" ] ||
11894                 error "expected parent: $FID0/foo1, got: $parent"
11895
11896         # 2) test for a file with nlink > 1 (multiple parents)
11897         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
11898         echo "$parent" | grep -F "$FID1/$tfile" ||
11899                 error "$FID1/$tfile not returned in parent list"
11900         echo "$parent" | grep -F "$FID2/link" ||
11901                 error "$FID2/link not returned in parent list"
11902
11903         # 3) get parent by fid
11904         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
11905         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11906         echo "$parent" | grep -F "$FID1/$tfile" ||
11907                 error "$FID1/$tfile not returned in parent list (by fid)"
11908         echo "$parent" | grep -F "$FID2/link" ||
11909                 error "$FID2/link not returned in parent list (by fid)"
11910
11911         # 4) test for entry in root directory
11912         parent=$($LFS path2fid --parents $DIR/f)
11913         echo "$parent" | grep -F "$FID3/f" ||
11914                 error "$FID3/f not returned in parent list"
11915
11916         # 5) test it on root directory
11917         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
11918                 error "$MOUNT should not have parents"
11919
11920         # enable xattr caching and check that linkea is correctly updated
11921         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
11922         save_lustre_params client "llite.*.xattr_cache" > $save
11923         lctl set_param llite.*.xattr_cache 1
11924
11925         # 6.1) linkea update on rename
11926         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
11927
11928         # get parents by fid
11929         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11930         # foo1 should no longer be returned in parent list
11931         echo "$parent" | grep -F "$FID1" &&
11932                 error "$FID1 should no longer be in parent list"
11933         # the new path should appear
11934         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
11935                 error "$FID2/$tfile.moved is not in parent list"
11936
11937         # 6.2) linkea update on unlink
11938         rm -f $DIR/$tdir/d/foo2/link
11939         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11940         # foo2/link should no longer be returned in parent list
11941         echo "$parent" | grep -F "$FID2/link" &&
11942                 error "$FID2/link should no longer be in parent list"
11943         true
11944
11945         rm -f $DIR/f
11946         restore_lustre_params < $save
11947         rm -f $save
11948 }
11949 run_test 154f "get parent fids by reading link ea"
11950
11951 test_154g()
11952 {
11953         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
11954         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
11955            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
11956                 skip "Need MDS version at least 2.6.92"
11957
11958         mkdir -p $DIR/$tdir
11959         llapi_fid_test -d $DIR/$tdir
11960 }
11961 run_test 154g "various llapi FID tests"
11962
11963 test_155_small_load() {
11964     local temp=$TMP/$tfile
11965     local file=$DIR/$tfile
11966
11967     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
11968         error "dd of=$temp bs=6096 count=1 failed"
11969     cp $temp $file
11970     cancel_lru_locks $OSC
11971     cmp $temp $file || error "$temp $file differ"
11972
11973     $TRUNCATE $temp 6000
11974     $TRUNCATE $file 6000
11975     cmp $temp $file || error "$temp $file differ (truncate1)"
11976
11977     echo "12345" >>$temp
11978     echo "12345" >>$file
11979     cmp $temp $file || error "$temp $file differ (append1)"
11980
11981     echo "12345" >>$temp
11982     echo "12345" >>$file
11983     cmp $temp $file || error "$temp $file differ (append2)"
11984
11985     rm -f $temp $file
11986     true
11987 }
11988
11989 test_155_big_load() {
11990         remote_ost_nodsh && skip "remote OST with nodsh"
11991
11992         local temp=$TMP/$tfile
11993         local file=$DIR/$tfile
11994
11995         free_min_max
11996         local cache_size=$(do_facet ost$((MAXI+1)) \
11997                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
11998         local large_file_size=$((cache_size * 2))
11999
12000         echo "OSS cache size: $cache_size KB"
12001         echo "Large file size: $large_file_size KB"
12002
12003         [ $MAXV -le $large_file_size ] &&
12004                 skip_env "max available OST size needs > $large_file_size KB"
12005
12006         $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
12007
12008         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
12009                 error "dd of=$temp bs=$large_file_size count=1k failed"
12010         cp $temp $file
12011         ls -lh $temp $file
12012         cancel_lru_locks osc
12013         cmp $temp $file || error "$temp $file differ"
12014
12015         rm -f $temp $file
12016         true
12017 }
12018
12019 save_writethrough() {
12020         local facets=$(get_facets OST)
12021
12022         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
12023 }
12024
12025 test_155a() {
12026         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12027
12028         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12029
12030         save_writethrough $p
12031
12032         set_cache read on
12033         set_cache writethrough on
12034         test_155_small_load
12035         restore_lustre_params < $p
12036         rm -f $p
12037 }
12038 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
12039
12040 test_155b() {
12041         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12042
12043         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12044
12045         save_writethrough $p
12046
12047         set_cache read on
12048         set_cache writethrough off
12049         test_155_small_load
12050         restore_lustre_params < $p
12051         rm -f $p
12052 }
12053 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
12054
12055 test_155c() {
12056         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12057
12058         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12059
12060         save_writethrough $p
12061
12062         set_cache read off
12063         set_cache writethrough on
12064         test_155_small_load
12065         restore_lustre_params < $p
12066         rm -f $p
12067 }
12068 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
12069
12070 test_155d() {
12071         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12072
12073         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12074
12075         save_writethrough $p
12076
12077         set_cache read off
12078         set_cache writethrough off
12079         test_155_small_load
12080         restore_lustre_params < $p
12081         rm -f $p
12082 }
12083 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
12084
12085 test_155e() {
12086         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12087
12088         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12089
12090         save_writethrough $p
12091
12092         set_cache read on
12093         set_cache writethrough on
12094         test_155_big_load
12095         restore_lustre_params < $p
12096         rm -f $p
12097 }
12098 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
12099
12100 test_155f() {
12101         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12102
12103         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12104
12105         save_writethrough $p
12106
12107         set_cache read on
12108         set_cache writethrough off
12109         test_155_big_load
12110         restore_lustre_params < $p
12111         rm -f $p
12112 }
12113 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
12114
12115 test_155g() {
12116         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12117
12118         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12119
12120         save_writethrough $p
12121
12122         set_cache read off
12123         set_cache writethrough on
12124         test_155_big_load
12125         restore_lustre_params < $p
12126         rm -f $p
12127 }
12128 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
12129
12130 test_155h() {
12131         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12132
12133         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12134
12135         save_writethrough $p
12136
12137         set_cache read off
12138         set_cache writethrough off
12139         test_155_big_load
12140         restore_lustre_params < $p
12141         rm -f $p
12142 }
12143 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
12144
12145 test_156() {
12146         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12147         remote_ost_nodsh && skip "remote OST with nodsh"
12148         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
12149                 skip "stats not implemented on old servers"
12150         [ "$ost1_FSTYPE" = "zfs" ] &&
12151                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
12152
12153         local CPAGES=3
12154         local BEFORE
12155         local AFTER
12156         local file="$DIR/$tfile"
12157         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12158
12159         save_writethrough $p
12160         roc_hit_init
12161
12162         log "Turn on read and write cache"
12163         set_cache read on
12164         set_cache writethrough on
12165
12166         log "Write data and read it back."
12167         log "Read should be satisfied from the cache."
12168         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12169         BEFORE=$(roc_hit)
12170         cancel_lru_locks osc
12171         cat $file >/dev/null
12172         AFTER=$(roc_hit)
12173         if ! let "AFTER - BEFORE == CPAGES"; then
12174                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12175         else
12176                 log "cache hits:: before: $BEFORE, after: $AFTER"
12177         fi
12178
12179         log "Read again; it should be satisfied from the cache."
12180         BEFORE=$AFTER
12181         cancel_lru_locks osc
12182         cat $file >/dev/null
12183         AFTER=$(roc_hit)
12184         if ! let "AFTER - BEFORE == CPAGES"; then
12185                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12186         else
12187                 log "cache hits:: before: $BEFORE, after: $AFTER"
12188         fi
12189
12190         log "Turn off the read cache and turn on the write cache"
12191         set_cache read off
12192         set_cache writethrough on
12193
12194         log "Read again; it should be satisfied from the cache."
12195         BEFORE=$(roc_hit)
12196         cancel_lru_locks osc
12197         cat $file >/dev/null
12198         AFTER=$(roc_hit)
12199         if ! let "AFTER - BEFORE == CPAGES"; then
12200                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12201         else
12202                 log "cache hits:: before: $BEFORE, after: $AFTER"
12203         fi
12204
12205         log "Read again; it should not be satisfied from the cache."
12206         BEFORE=$AFTER
12207         cancel_lru_locks osc
12208         cat $file >/dev/null
12209         AFTER=$(roc_hit)
12210         if ! let "AFTER - BEFORE == 0"; then
12211                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12212         else
12213                 log "cache hits:: before: $BEFORE, after: $AFTER"
12214         fi
12215
12216         log "Write data and read it back."
12217         log "Read should be satisfied from the cache."
12218         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12219         BEFORE=$(roc_hit)
12220         cancel_lru_locks osc
12221         cat $file >/dev/null
12222         AFTER=$(roc_hit)
12223         if ! let "AFTER - BEFORE == CPAGES"; then
12224                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12225         else
12226                 log "cache hits:: before: $BEFORE, after: $AFTER"
12227         fi
12228
12229         log "Read again; it should not be satisfied from the cache."
12230         BEFORE=$AFTER
12231         cancel_lru_locks osc
12232         cat $file >/dev/null
12233         AFTER=$(roc_hit)
12234         if ! let "AFTER - BEFORE == 0"; then
12235                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12236         else
12237                 log "cache hits:: before: $BEFORE, after: $AFTER"
12238         fi
12239
12240         log "Turn off read and write cache"
12241         set_cache read off
12242         set_cache writethrough off
12243
12244         log "Write data and read it back"
12245         log "It should not be satisfied from the cache."
12246         rm -f $file
12247         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12248         cancel_lru_locks osc
12249         BEFORE=$(roc_hit)
12250         cat $file >/dev/null
12251         AFTER=$(roc_hit)
12252         if ! let "AFTER - BEFORE == 0"; then
12253                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
12254         else
12255                 log "cache hits:: before: $BEFORE, after: $AFTER"
12256         fi
12257
12258         log "Turn on the read cache and turn off the write cache"
12259         set_cache read on
12260         set_cache writethrough off
12261
12262         log "Write data and read it back"
12263         log "It should not be satisfied from the cache."
12264         rm -f $file
12265         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12266         BEFORE=$(roc_hit)
12267         cancel_lru_locks osc
12268         cat $file >/dev/null
12269         AFTER=$(roc_hit)
12270         if ! let "AFTER - BEFORE == 0"; then
12271                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
12272         else
12273                 log "cache hits:: before: $BEFORE, after: $AFTER"
12274         fi
12275
12276         log "Read again; it should be satisfied from the cache."
12277         BEFORE=$(roc_hit)
12278         cancel_lru_locks osc
12279         cat $file >/dev/null
12280         AFTER=$(roc_hit)
12281         if ! let "AFTER - BEFORE == CPAGES"; then
12282                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12283         else
12284                 log "cache hits:: before: $BEFORE, after: $AFTER"
12285         fi
12286
12287         restore_lustre_params < $p
12288         rm -f $p $file
12289 }
12290 run_test 156 "Verification of tunables"
12291
12292 test_160a() {
12293         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12294         remote_mds_nodsh && skip "remote MDS with nodsh"
12295         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
12296                 skip "Need MDS version at least 2.2.0"
12297
12298         changelog_register || error "changelog_register failed"
12299         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
12300         changelog_users $SINGLEMDS | grep -q $cl_user ||
12301                 error "User $cl_user not found in changelog_users"
12302
12303         # change something
12304         test_mkdir -p $DIR/$tdir/pics/2008/zachy
12305         changelog_clear 0 || error "changelog_clear failed"
12306         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
12307         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
12308         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
12309         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
12310         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
12311         rm $DIR/$tdir/pics/desktop.jpg
12312
12313         changelog_dump | tail -10
12314
12315         echo "verifying changelog mask"
12316         changelog_chmask "-MKDIR"
12317         changelog_chmask "-CLOSE"
12318
12319         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
12320         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
12321
12322         changelog_chmask "+MKDIR"
12323         changelog_chmask "+CLOSE"
12324
12325         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
12326         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
12327
12328         changelog_dump | tail -10
12329         MKDIRS=$(changelog_dump | grep -c "MKDIR")
12330         CLOSES=$(changelog_dump | grep -c "CLOSE")
12331         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
12332         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
12333
12334         # verify contents
12335         echo "verifying target fid"
12336         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
12337         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
12338         [ "$fidc" == "$fidf" ] ||
12339                 error "changelog '$tfile' fid $fidc != file fid $fidf"
12340         echo "verifying parent fid"
12341         # The FID returned from the Changelog may be the directory shard on
12342         # a different MDT, and not the FID returned by path2fid on the parent.
12343         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
12344         # since this is what will matter when recreating this file in the tree.
12345         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
12346         local pathp=$($LFS fid2path $MOUNT "$fidp")
12347         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
12348                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
12349
12350         echo "getting records for $cl_user"
12351         changelog_users $SINGLEMDS
12352         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
12353         local nclr=3
12354         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
12355                 error "changelog_clear failed"
12356         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
12357         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
12358         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
12359                 error "user index expect $user_rec1 + $nclr != $user_rec2"
12360
12361         local min0_rec=$(changelog_users $SINGLEMDS |
12362                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
12363         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
12364                           awk '{ print $1; exit; }')
12365
12366         changelog_dump | tail -n 5
12367         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
12368         [ $first_rec == $((min0_rec + 1)) ] ||
12369                 error "first index should be $min0_rec + 1 not $first_rec"
12370
12371         # LU-3446 changelog index reset on MDT restart
12372         local cur_rec1=$(changelog_users $SINGLEMDS |
12373                          awk '/^current.index:/ { print $NF }')
12374         changelog_clear 0 ||
12375                 error "clear all changelog records for $cl_user failed"
12376         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
12377         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
12378                 error "Fail to start $SINGLEMDS"
12379         local cur_rec2=$(changelog_users $SINGLEMDS |
12380                          awk '/^current.index:/ { print $NF }')
12381         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
12382         [ $cur_rec1 == $cur_rec2 ] ||
12383                 error "current index should be $cur_rec1 not $cur_rec2"
12384
12385         echo "verifying users from this test are deregistered"
12386         changelog_deregister || error "changelog_deregister failed"
12387         changelog_users $SINGLEMDS | grep -q $cl_user &&
12388                 error "User '$cl_user' still in changelog_users"
12389
12390         # lctl get_param -n mdd.*.changelog_users
12391         # current index: 144
12392         # ID    index (idle seconds)
12393         # cl3   144 (2)
12394         if ! changelog_users $SINGLEMDS | grep "^cl"; then
12395                 # this is the normal case where all users were deregistered
12396                 # make sure no new records are added when no users are present
12397                 local last_rec1=$(changelog_users $SINGLEMDS |
12398                                   awk '/^current.index:/ { print $NF }')
12399                 touch $DIR/$tdir/chloe
12400                 local last_rec2=$(changelog_users $SINGLEMDS |
12401                                   awk '/^current.index:/ { print $NF }')
12402                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
12403                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
12404         else
12405                 # any changelog users must be leftovers from a previous test
12406                 changelog_users $SINGLEMDS
12407                 echo "other changelog users; can't verify off"
12408         fi
12409 }
12410 run_test 160a "changelog sanity"
12411
12412 test_160b() { # LU-3587
12413         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12414         remote_mds_nodsh && skip "remote MDS with nodsh"
12415         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
12416                 skip "Need MDS version at least 2.2.0"
12417
12418         changelog_register || error "changelog_register failed"
12419         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
12420         changelog_users $SINGLEMDS | grep -q $cl_user ||
12421                 error "User '$cl_user' not found in changelog_users"
12422
12423         local longname1=$(str_repeat a 255)
12424         local longname2=$(str_repeat b 255)
12425
12426         cd $DIR
12427         echo "creating very long named file"
12428         touch $longname1 || error "create of '$longname1' failed"
12429         echo "renaming very long named file"
12430         mv $longname1 $longname2
12431
12432         changelog_dump | grep RENME | tail -n 5
12433         rm -f $longname2
12434 }
12435 run_test 160b "Verify that very long rename doesn't crash in changelog"
12436
12437 test_160c() {
12438         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12439         remote_mds_nodsh && skip "remote MDS with nodsh"
12440
12441         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
12442                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
12443                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
12444                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
12445
12446         local rc=0
12447
12448         # Registration step
12449         changelog_register || error "changelog_register failed"
12450
12451         rm -rf $DIR/$tdir
12452         mkdir -p $DIR/$tdir
12453         $MCREATE $DIR/$tdir/foo_160c
12454         changelog_chmask "-TRUNC"
12455         $TRUNCATE $DIR/$tdir/foo_160c 200
12456         changelog_chmask "+TRUNC"
12457         $TRUNCATE $DIR/$tdir/foo_160c 199
12458         changelog_dump | tail -n 5
12459         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
12460         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
12461 }
12462 run_test 160c "verify that changelog log catch the truncate event"
12463
12464 test_160d() {
12465         remote_mds_nodsh && skip "remote MDS with nodsh"
12466         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
12467         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12468         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
12469                 skip "Need MDS version at least 2.7.60"
12470
12471         # Registration step
12472         changelog_register || error "changelog_register failed"
12473
12474         mkdir -p $DIR/$tdir/migrate_dir
12475         changelog_clear 0 || error "changelog_clear failed"
12476
12477         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
12478         changelog_dump | tail -n 5
12479         local migrates=$(changelog_dump | grep -c "MIGRT")
12480         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
12481 }
12482 run_test 160d "verify that changelog log catch the migrate event"
12483
12484 test_160e() {
12485         remote_mds_nodsh && skip "remote MDS with nodsh"
12486
12487         # Create a user
12488         changelog_register || error "changelog_register failed"
12489
12490         # Delete a future user (expect fail)
12491         local MDT0=$(facet_svc $SINGLEMDS)
12492         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
12493         local rc=$?
12494
12495         if [ $rc -eq 0 ]; then
12496                 error "Deleted non-existant user cl77"
12497         elif [ $rc -ne 2 ]; then
12498                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
12499         fi
12500
12501         # Clear to a bad index (1 billion should be safe)
12502         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
12503         rc=$?
12504
12505         if [ $rc -eq 0 ]; then
12506                 error "Successfully cleared to invalid CL index"
12507         elif [ $rc -ne 22 ]; then
12508                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
12509         fi
12510 }
12511 run_test 160e "changelog negative testing (should return errors)"
12512
12513 test_160f() {
12514         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12515         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
12516                 skip "Need MDS version at least 2.10.56"
12517
12518         local mdts=$(comma_list $(mdts_nodes))
12519
12520         # Create a user
12521         changelog_register || error "first changelog_register failed"
12522         changelog_register || error "second changelog_register failed"
12523         local cl_users
12524         declare -A cl_user1
12525         declare -A cl_user2
12526         local user_rec1
12527         local user_rec2
12528         local i
12529
12530         # generate some changelog records to accumulate on each MDT
12531         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
12532         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
12533                 error "create $DIR/$tdir/$tfile failed"
12534
12535         # check changelogs have been generated
12536         local nbcl=$(changelog_dump | wc -l)
12537         [[ $nbcl -eq 0 ]] && error "no changelogs found"
12538
12539         for param in "changelog_max_idle_time=10" \
12540                      "changelog_gc=1" \
12541                      "changelog_min_gc_interval=2" \
12542                      "changelog_min_free_cat_entries=3"; do
12543                 local MDT0=$(facet_svc $SINGLEMDS)
12544                 local var="${param%=*}"
12545                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
12546
12547                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
12548                 do_nodes $mdts $LCTL set_param mdd.*.$param
12549         done
12550
12551         # force cl_user2 to be idle (1st part)
12552         sleep 9
12553
12554         # simulate changelog catalog almost full
12555         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
12556         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
12557
12558         for i in $(seq $MDSCOUNT); do
12559                 cl_users=(${CL_USERS[mds$i]})
12560                 cl_user1[mds$i]="${cl_users[0]}"
12561                 cl_user2[mds$i]="${cl_users[1]}"
12562
12563                 [ -n "${cl_user1[mds$i]}" ] ||
12564                         error "mds$i: no user registered"
12565                 [ -n "${cl_user2[mds$i]}" ] ||
12566                         error "mds$i: only ${cl_user2[mds$i]} is registered"
12567
12568                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12569                 [ -n "$user_rec1" ] ||
12570                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12571                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
12572                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12573                 [ -n "$user_rec2" ] ||
12574                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12575                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
12576                      "$user_rec1 + 2 == $user_rec2"
12577                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
12578                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
12579                               "$user_rec1 + 2, but is $user_rec2"
12580                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
12581                 [ -n "$user_rec2" ] ||
12582                         error "mds$i: User ${cl_user2[mds$i]} not registered"
12583                 [ $user_rec1 == $user_rec2 ] ||
12584                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
12585                               "$user_rec1, but is $user_rec2"
12586         done
12587
12588         # force cl_user2 to be idle (2nd part) and to reach
12589         # changelog_max_idle_time
12590         sleep 2
12591
12592         # generate one more changelog to trigger fail_loc
12593         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
12594                 error "create $DIR/$tdir/${tfile}bis failed"
12595
12596         # ensure gc thread is done
12597         for i in $(mdts_nodes); do
12598                 wait_update $i \
12599                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
12600                         error "$i: GC-thread not done"
12601         done
12602
12603         local first_rec
12604         for i in $(seq $MDSCOUNT); do
12605                 # check cl_user1 still registered
12606                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
12607                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12608                 # check cl_user2 unregistered
12609                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
12610                         error "mds$i: User ${cl_user2[mds$i]} still registered"
12611
12612                 # check changelogs are present and starting at $user_rec1 + 1
12613                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12614                 [ -n "$user_rec1" ] ||
12615                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12616                 first_rec=$($LFS changelog $(facet_svc mds$i) |
12617                             awk '{ print $1; exit; }')
12618
12619                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
12620                 [ $((user_rec1 + 1)) == $first_rec ] ||
12621                         error "mds$i: first index should be $user_rec1 + 1, " \
12622                               "but is $first_rec"
12623         done
12624 }
12625 run_test 160f "changelog garbage collect (timestamped users)"
12626
12627 test_160g() {
12628         remote_mds_nodsh && skip "remote MDS with nodsh"
12629         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
12630                 skip "Need MDS version at least 2.10.56"
12631
12632         local mdts=$(comma_list $(mdts_nodes))
12633
12634         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
12635         do_nodes $mdts $LCTL set_param fail_loc=0x1314
12636
12637         # Create a user
12638         changelog_register || error "first changelog_register failed"
12639         changelog_register || error "second changelog_register failed"
12640         local cl_users
12641         declare -A cl_user1
12642         declare -A cl_user2
12643         local user_rec1
12644         local user_rec2
12645         local i
12646
12647         # generate some changelog records to accumulate on each MDT
12648         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
12649         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
12650                 error "create $DIR/$tdir/$tfile failed"
12651
12652         # check changelogs have been generated
12653         local nbcl=$(changelog_dump | wc -l)
12654         [[ $nbcl -eq 0 ]] && error "no changelogs found"
12655
12656         # reduce the max_idle_indexes value to make sure we exceed it
12657         max_ndx=$((nbcl / 2 - 1))
12658
12659         for param in "changelog_max_idle_indexes=$max_ndx" \
12660                      "changelog_gc=1" \
12661                      "changelog_min_gc_interval=2" \
12662                      "changelog_min_free_cat_entries=3"; do
12663                 local MDT0=$(facet_svc $SINGLEMDS)
12664                 local var="${param%=*}"
12665                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
12666
12667                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
12668                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
12669                         error "unable to set mdd.*.$param"
12670         done
12671
12672         # simulate changelog catalog almost full
12673         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
12674         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
12675
12676         for i in $(seq $MDSCOUNT); do
12677                 cl_users=(${CL_USERS[mds$i]})
12678                 cl_user1[mds$i]="${cl_users[0]}"
12679                 cl_user2[mds$i]="${cl_users[1]}"
12680
12681                 [ -n "${cl_user1[mds$i]}" ] ||
12682                         error "mds$i: no user registered"
12683                 [ -n "${cl_user2[mds$i]}" ] ||
12684                         error "mds$i: only ${cl_user1[mds$i]} is registered"
12685
12686                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12687                 [ -n "$user_rec1" ] ||
12688                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12689                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
12690                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12691                 [ -n "$user_rec2" ] ||
12692                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12693                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
12694                      "$user_rec1 + 2 == $user_rec2"
12695                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
12696                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
12697                               "$user_rec1 + 2, but is $user_rec2"
12698                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
12699                 [ -n "$user_rec2" ] ||
12700                         error "mds$i: User ${cl_user2[mds$i]} not registered"
12701                 [ $user_rec1 == $user_rec2 ] ||
12702                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
12703                               "$user_rec1, but is $user_rec2"
12704         done
12705
12706         # ensure we are past the previous changelog_min_gc_interval set above
12707         sleep 2
12708
12709         # generate one more changelog to trigger fail_loc
12710         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
12711                 error "create $DIR/$tdir/${tfile}bis failed"
12712
12713         # ensure gc thread is done
12714         for i in $(mdts_nodes); do
12715                 wait_update $i \
12716                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
12717                         error "$i: GC-thread not done"
12718         done
12719
12720         local first_rec
12721         for i in $(seq $MDSCOUNT); do
12722                 # check cl_user1 still registered
12723                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
12724                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12725                 # check cl_user2 unregistered
12726                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
12727                         error "mds$i: User ${cl_user2[mds$i]} still registered"
12728
12729                 # check changelogs are present and starting at $user_rec1 + 1
12730                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12731                 [ -n "$user_rec1" ] ||
12732                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12733                 first_rec=$($LFS changelog $(facet_svc mds$i) |
12734                             awk '{ print $1; exit; }')
12735
12736                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
12737                 [ $((user_rec1 + 1)) == $first_rec ] ||
12738                         error "mds$i: first index should be $user_rec1 + 1, " \
12739                               "but is $first_rec"
12740         done
12741 }
12742 run_test 160g "changelog garbage collect (old users)"
12743
12744 test_160h() {
12745         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12746         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
12747                 skip "Need MDS version at least 2.10.56"
12748
12749         local mdts=$(comma_list $(mdts_nodes))
12750
12751         # Create a user
12752         changelog_register || error "first changelog_register failed"
12753         changelog_register || error "second changelog_register failed"
12754         local cl_users
12755         declare -A cl_user1
12756         declare -A cl_user2
12757         local user_rec1
12758         local user_rec2
12759         local i
12760
12761         # generate some changelog records to accumulate on each MDT
12762         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
12763         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
12764                 error "create $DIR/$tdir/$tfile failed"
12765
12766         # check changelogs have been generated
12767         local nbcl=$(changelog_dump | wc -l)
12768         [[ $nbcl -eq 0 ]] && error "no changelogs found"
12769
12770         for param in "changelog_max_idle_time=10" \
12771                      "changelog_gc=1" \
12772                      "changelog_min_gc_interval=2"; do
12773                 local MDT0=$(facet_svc $SINGLEMDS)
12774                 local var="${param%=*}"
12775                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
12776
12777                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
12778                 do_nodes $mdts $LCTL set_param mdd.*.$param
12779         done
12780
12781         # force cl_user2 to be idle (1st part)
12782         sleep 9
12783
12784         for i in $(seq $MDSCOUNT); do
12785                 cl_users=(${CL_USERS[mds$i]})
12786                 cl_user1[mds$i]="${cl_users[0]}"
12787                 cl_user2[mds$i]="${cl_users[1]}"
12788
12789                 [ -n "${cl_user1[mds$i]}" ] ||
12790                         error "mds$i: no user registered"
12791                 [ -n "${cl_user2[mds$i]}" ] ||
12792                         error "mds$i: only ${cl_user2[mds$i]} is registered"
12793
12794                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12795                 [ -n "$user_rec1" ] ||
12796                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12797                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
12798                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12799                 [ -n "$user_rec2" ] ||
12800                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12801                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
12802                      "$user_rec1 + 2 == $user_rec2"
12803                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
12804                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
12805                               "$user_rec1 + 2, but is $user_rec2"
12806                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
12807                 [ -n "$user_rec2" ] ||
12808                         error "mds$i: User ${cl_user2[mds$i]} not registered"
12809                 [ $user_rec1 == $user_rec2 ] ||
12810                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
12811                               "$user_rec1, but is $user_rec2"
12812         done
12813
12814         # force cl_user2 to be idle (2nd part) and to reach
12815         # changelog_max_idle_time
12816         sleep 2
12817
12818         # force each GC-thread start and block then
12819         # one per MDT/MDD, set fail_val accordingly
12820         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
12821         do_nodes $mdts $LCTL set_param fail_loc=0x1316
12822
12823         # generate more changelogs to trigger fail_loc
12824         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
12825                 error "create $DIR/$tdir/${tfile}bis failed"
12826
12827         # stop MDT to stop GC-thread, should be done in back-ground as it will
12828         # block waiting for the thread to be released and exit
12829         declare -A stop_pids
12830         for i in $(seq $MDSCOUNT); do
12831                 stop mds$i &
12832                 stop_pids[mds$i]=$!
12833         done
12834
12835         for i in $(mdts_nodes); do
12836                 local facet
12837                 local nb=0
12838                 local facets=$(facets_up_on_host $i)
12839
12840                 for facet in ${facets//,/ }; do
12841                         if [[ $facet == mds* ]]; then
12842                                 nb=$((nb + 1))
12843                         fi
12844                 done
12845                 # ensure each MDS's gc threads are still present and all in "R"
12846                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
12847                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
12848                         error "$i: expected $nb GC-thread"
12849                 wait_update $i \
12850                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
12851                         "R" 20 ||
12852                         error "$i: GC-thread not found in R-state"
12853                 # check umounts of each MDT on MDS have reached kthread_stop()
12854                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
12855                         error "$i: expected $nb umount"
12856                 wait_update $i \
12857                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
12858                         error "$i: umount not found in D-state"
12859         done
12860
12861         # release all GC-threads
12862         do_nodes $mdts $LCTL set_param fail_loc=0
12863
12864         # wait for MDT stop to complete
12865         for i in $(seq $MDSCOUNT); do
12866                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
12867         done
12868
12869         # XXX
12870         # may try to check if any orphan changelog records are present
12871         # via ldiskfs/zfs and llog_reader...
12872
12873         # re-start/mount MDTs
12874         for i in $(seq $MDSCOUNT); do
12875                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
12876                         error "Fail to start mds$i"
12877         done
12878
12879         local first_rec
12880         for i in $(seq $MDSCOUNT); do
12881                 # check cl_user1 still registered
12882                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
12883                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12884                 # check cl_user2 unregistered
12885                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
12886                         error "mds$i: User ${cl_user2[mds$i]} still registered"
12887
12888                 # check changelogs are present and starting at $user_rec1 + 1
12889                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12890                 [ -n "$user_rec1" ] ||
12891                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12892                 first_rec=$($LFS changelog $(facet_svc mds$i) |
12893                             awk '{ print $1; exit; }')
12894
12895                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
12896                 [ $((user_rec1 + 1)) == $first_rec ] ||
12897                         error "mds$i: first index should be $user_rec1 + 1, " \
12898                               "but is $first_rec"
12899         done
12900 }
12901 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
12902               "during mount"
12903
12904 test_160i() {
12905
12906         local mdts=$(comma_list $(mdts_nodes))
12907
12908         changelog_register || error "first changelog_register failed"
12909
12910         # generate some changelog records to accumulate on each MDT
12911         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
12912         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
12913                 error "create $DIR/$tdir/$tfile failed"
12914
12915         # check changelogs have been generated
12916         local nbcl=$(changelog_dump | wc -l)
12917         [[ $nbcl -eq 0 ]] && error "no changelogs found"
12918
12919         # simulate race between register and unregister
12920         # XXX as fail_loc is set per-MDS, with DNE configs the race
12921         # simulation will only occur for one MDT per MDS and for the
12922         # others the normal race scenario will take place
12923         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
12924         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
12925         do_nodes $mdts $LCTL set_param fail_val=1
12926
12927         # unregister 1st user
12928         changelog_deregister &
12929         local pid1=$!
12930         # wait some time for deregister work to reach race rdv
12931         sleep 2
12932         # register 2nd user
12933         changelog_register || error "2nd user register failed"
12934
12935         wait $pid1 || error "1st user deregister failed"
12936
12937         local i
12938         local last_rec
12939         declare -A LAST_REC
12940         for i in $(seq $MDSCOUNT); do
12941                 if changelog_users mds$i | grep "^cl"; then
12942                         # make sure new records are added with one user present
12943                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
12944                                           awk '/^current.index:/ { print $NF }')
12945                 else
12946                         error "mds$i has no user registered"
12947                 fi
12948         done
12949
12950         # generate more changelog records to accumulate on each MDT
12951         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
12952                 error "create $DIR/$tdir/${tfile}bis failed"
12953
12954         for i in $(seq $MDSCOUNT); do
12955                 last_rec=$(changelog_users $SINGLEMDS |
12956                            awk '/^current.index:/ { print $NF }')
12957                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
12958                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
12959                         error "changelogs are off on mds$i"
12960         done
12961 }
12962 run_test 160i "changelog user register/unregister race"
12963
12964 test_161a() {
12965         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12966
12967         test_mkdir -c1 $DIR/$tdir
12968         cp /etc/hosts $DIR/$tdir/$tfile
12969         test_mkdir -c1 $DIR/$tdir/foo1
12970         test_mkdir -c1 $DIR/$tdir/foo2
12971         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
12972         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
12973         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
12974         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
12975         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
12976         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
12977                 $LFS fid2path $DIR $FID
12978                 error "bad link ea"
12979         fi
12980         # middle
12981         rm $DIR/$tdir/foo2/zachary
12982         # last
12983         rm $DIR/$tdir/foo2/thor
12984         # first
12985         rm $DIR/$tdir/$tfile
12986         # rename
12987         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
12988         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
12989                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
12990         rm $DIR/$tdir/foo2/maggie
12991
12992         # overflow the EA
12993         local longname=$tfile.avg_len_is_thirty_two_
12994         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
12995                 error_noexit 'failed to unlink many hardlinks'" EXIT
12996         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
12997                 error "failed to hardlink many files"
12998         links=$($LFS fid2path $DIR $FID | wc -l)
12999         echo -n "${links}/1000 links in link EA"
13000         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
13001 }
13002 run_test 161a "link ea sanity"
13003
13004 test_161b() {
13005         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13006         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
13007
13008         local MDTIDX=1
13009         local remote_dir=$DIR/$tdir/remote_dir
13010
13011         mkdir -p $DIR/$tdir
13012         $LFS mkdir -i $MDTIDX $remote_dir ||
13013                 error "create remote directory failed"
13014
13015         cp /etc/hosts $remote_dir/$tfile
13016         mkdir -p $remote_dir/foo1
13017         mkdir -p $remote_dir/foo2
13018         ln $remote_dir/$tfile $remote_dir/foo1/sofia
13019         ln $remote_dir/$tfile $remote_dir/foo2/zachary
13020         ln $remote_dir/$tfile $remote_dir/foo1/luna
13021         ln $remote_dir/$tfile $remote_dir/foo2/thor
13022
13023         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
13024                      tr -d ']')
13025         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
13026                 $LFS fid2path $DIR $FID
13027                 error "bad link ea"
13028         fi
13029         # middle
13030         rm $remote_dir/foo2/zachary
13031         # last
13032         rm $remote_dir/foo2/thor
13033         # first
13034         rm $remote_dir/$tfile
13035         # rename
13036         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
13037         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
13038         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
13039                 $LFS fid2path $DIR $FID
13040                 error "bad link rename"
13041         fi
13042         rm $remote_dir/foo2/maggie
13043
13044         # overflow the EA
13045         local longname=filename_avg_len_is_thirty_two_
13046         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
13047                 error "failed to hardlink many files"
13048         links=$($LFS fid2path $DIR $FID | wc -l)
13049         echo -n "${links}/1000 links in link EA"
13050         [[ ${links} -gt 60 ]] ||
13051                 error "expected at least 60 links in link EA"
13052         unlinkmany $remote_dir/foo2/$longname 1000 ||
13053         error "failed to unlink many hardlinks"
13054 }
13055 run_test 161b "link ea sanity under remote directory"
13056
13057 test_161c() {
13058         remote_mds_nodsh && skip "remote MDS with nodsh"
13059         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13060         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
13061                 skip "Need MDS version at least 2.1.5"
13062
13063         # define CLF_RENAME_LAST 0x0001
13064         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
13065         changelog_register || error "changelog_register failed"
13066
13067         rm -rf $DIR/$tdir
13068         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
13069         touch $DIR/$tdir/foo_161c
13070         touch $DIR/$tdir/bar_161c
13071         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
13072         changelog_dump | grep RENME | tail -n 5
13073         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
13074         changelog_clear 0 || error "changelog_clear failed"
13075         if [ x$flags != "x0x1" ]; then
13076                 error "flag $flags is not 0x1"
13077         fi
13078
13079         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
13080         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
13081         touch $DIR/$tdir/foo_161c
13082         touch $DIR/$tdir/bar_161c
13083         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
13084         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
13085         changelog_dump | grep RENME | tail -n 5
13086         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
13087         changelog_clear 0 || error "changelog_clear failed"
13088         if [ x$flags != "x0x0" ]; then
13089                 error "flag $flags is not 0x0"
13090         fi
13091         echo "rename overwrite a target having nlink > 1," \
13092                 "changelog record has flags of $flags"
13093
13094         # rename doesn't overwrite a target (changelog flag 0x0)
13095         touch $DIR/$tdir/foo_161c
13096         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
13097         changelog_dump | grep RENME | tail -n 5
13098         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
13099         changelog_clear 0 || error "changelog_clear failed"
13100         if [ x$flags != "x0x0" ]; then
13101                 error "flag $flags is not 0x0"
13102         fi
13103         echo "rename doesn't overwrite a target," \
13104                 "changelog record has flags of $flags"
13105
13106         # define CLF_UNLINK_LAST 0x0001
13107         # unlink a file having nlink = 1 (changelog flag 0x1)
13108         rm -f $DIR/$tdir/foo2_161c
13109         changelog_dump | grep UNLNK | tail -n 5
13110         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
13111         changelog_clear 0 || error "changelog_clear failed"
13112         if [ x$flags != "x0x1" ]; then
13113                 error "flag $flags is not 0x1"
13114         fi
13115         echo "unlink a file having nlink = 1," \
13116                 "changelog record has flags of $flags"
13117
13118         # unlink a file having nlink > 1 (changelog flag 0x0)
13119         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
13120         rm -f $DIR/$tdir/foobar_161c
13121         changelog_dump | grep UNLNK | tail -n 5
13122         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
13123         changelog_clear 0 || error "changelog_clear failed"
13124         if [ x$flags != "x0x0" ]; then
13125                 error "flag $flags is not 0x0"
13126         fi
13127         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
13128 }
13129 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
13130
13131 test_161d() {
13132         remote_mds_nodsh && skip "remote MDS with nodsh"
13133
13134         local pid
13135         local fid
13136
13137         changelog_register || error "changelog_register failed"
13138
13139         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
13140         # interfer with $MOUNT/.lustre/fid/ access
13141         mkdir $DIR/$tdir
13142         [[ $? -eq 0 ]] || error "mkdir failed"
13143
13144         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
13145         $LCTL set_param fail_loc=0x8000140c
13146         # 5s pause
13147         $LCTL set_param fail_val=5
13148
13149         # create file
13150         echo foofoo > $DIR/$tdir/$tfile &
13151         pid=$!
13152
13153         # wait for create to be delayed
13154         sleep 2
13155
13156         ps -p $pid
13157         [[ $? -eq 0 ]] || error "create should be blocked"
13158
13159         local tempfile=$(mktemp)
13160         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
13161         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
13162         # some delay may occur during ChangeLog publishing and file read just
13163         # above, that could allow file write to happen finally
13164         [[ -s $tempfile ]] && echo "file should be empty"
13165
13166         $LCTL set_param fail_loc=0
13167
13168         wait $pid
13169         [[ $? -eq 0 ]] || error "create failed"
13170 }
13171 run_test 161d "create with concurrent .lustre/fid access"
13172
13173 check_path() {
13174         local expected="$1"
13175         shift
13176         local fid="$2"
13177
13178         local path
13179         path=$($LFS fid2path "$@")
13180         local rc=$?
13181
13182         if [ $rc -ne 0 ]; then
13183                 error "path looked up of '$expected' failed: rc=$rc"
13184         elif [ "$path" != "$expected" ]; then
13185                 error "path looked up '$path' instead of '$expected'"
13186         else
13187                 echo "FID '$fid' resolves to path '$path' as expected"
13188         fi
13189 }
13190
13191 test_162a() { # was test_162
13192         test_mkdir -p -c1 $DIR/$tdir/d2
13193         touch $DIR/$tdir/d2/$tfile
13194         touch $DIR/$tdir/d2/x1
13195         touch $DIR/$tdir/d2/x2
13196         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
13197         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
13198         # regular file
13199         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
13200         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
13201
13202         # softlink
13203         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
13204         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
13205         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
13206
13207         # softlink to wrong file
13208         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
13209         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
13210         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
13211
13212         # hardlink
13213         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
13214         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
13215         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
13216         # fid2path dir/fsname should both work
13217         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
13218         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
13219
13220         # hardlink count: check that there are 2 links
13221         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
13222         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
13223
13224         # hardlink indexing: remove the first link
13225         rm $DIR/$tdir/d2/p/q/r/hlink
13226         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
13227 }
13228 run_test 162a "path lookup sanity"
13229
13230 test_162b() {
13231         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13232         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13233
13234         mkdir $DIR/$tdir
13235         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
13236                                 error "create striped dir failed"
13237
13238         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
13239                                         tail -n 1 | awk '{print $2}')
13240         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
13241
13242         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
13243         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
13244
13245         # regular file
13246         for ((i=0;i<5;i++)); do
13247                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
13248                         error "get fid for f$i failed"
13249                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
13250
13251                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
13252                         error "get fid for d$i failed"
13253                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
13254         done
13255
13256         return 0
13257 }
13258 run_test 162b "striped directory path lookup sanity"
13259
13260 # LU-4239: Verify fid2path works with paths 100 or more directories deep
13261 test_162c() {
13262         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
13263                 skip "Need MDS version at least 2.7.51"
13264
13265         local lpath=$tdir.local
13266         local rpath=$tdir.remote
13267
13268         test_mkdir $DIR/$lpath
13269         test_mkdir $DIR/$rpath
13270
13271         for ((i = 0; i <= 101; i++)); do
13272                 lpath="$lpath/$i"
13273                 mkdir $DIR/$lpath
13274                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
13275                         error "get fid for local directory $DIR/$lpath failed"
13276                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
13277
13278                 rpath="$rpath/$i"
13279                 test_mkdir $DIR/$rpath
13280                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
13281                         error "get fid for remote directory $DIR/$rpath failed"
13282                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
13283         done
13284
13285         return 0
13286 }
13287 run_test 162c "fid2path works with paths 100 or more directories deep"
13288
13289 test_169() {
13290         # do directio so as not to populate the page cache
13291         log "creating a 10 Mb file"
13292         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
13293         log "starting reads"
13294         dd if=$DIR/$tfile of=/dev/null bs=4096 &
13295         log "truncating the file"
13296         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
13297         log "killing dd"
13298         kill %+ || true # reads might have finished
13299         echo "wait until dd is finished"
13300         wait
13301         log "removing the temporary file"
13302         rm -rf $DIR/$tfile || error "tmp file removal failed"
13303 }
13304 run_test 169 "parallel read and truncate should not deadlock"
13305
13306 test_170() {
13307         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13308
13309         $LCTL clear     # bug 18514
13310         $LCTL debug_daemon start $TMP/${tfile}_log_good
13311         touch $DIR/$tfile
13312         $LCTL debug_daemon stop
13313         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
13314                 error "sed failed to read log_good"
13315
13316         $LCTL debug_daemon start $TMP/${tfile}_log_good
13317         rm -rf $DIR/$tfile
13318         $LCTL debug_daemon stop
13319
13320         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
13321                error "lctl df log_bad failed"
13322
13323         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
13324         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
13325
13326         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
13327         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
13328
13329         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
13330                 error "bad_line good_line1 good_line2 are empty"
13331
13332         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
13333         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
13334         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
13335
13336         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
13337         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
13338         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
13339
13340         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
13341                 error "bad_line_new good_line_new are empty"
13342
13343         local expected_good=$((good_line1 + good_line2*2))
13344
13345         rm -f $TMP/${tfile}*
13346         # LU-231, short malformed line may not be counted into bad lines
13347         if [ $bad_line -ne $bad_line_new ] &&
13348                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
13349                 error "expected $bad_line bad lines, but got $bad_line_new"
13350                 return 1
13351         fi
13352
13353         if [ $expected_good -ne $good_line_new ]; then
13354                 error "expected $expected_good good lines, but got $good_line_new"
13355                 return 2
13356         fi
13357         true
13358 }
13359 run_test 170 "test lctl df to handle corrupted log ====================="
13360
13361 test_171() { # bug20592
13362         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13363
13364         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
13365         $LCTL set_param fail_loc=0x50e
13366         $LCTL set_param fail_val=3000
13367         multiop_bg_pause $DIR/$tfile O_s || true
13368         local MULTIPID=$!
13369         kill -USR1 $MULTIPID
13370         # cause log dump
13371         sleep 3
13372         wait $MULTIPID
13373         if dmesg | grep "recursive fault"; then
13374                 error "caught a recursive fault"
13375         fi
13376         $LCTL set_param fail_loc=0
13377         true
13378 }
13379 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
13380
13381 # it would be good to share it with obdfilter-survey/iokit-libecho code
13382 setup_obdecho_osc () {
13383         local rc=0
13384         local ost_nid=$1
13385         local obdfilter_name=$2
13386         echo "Creating new osc for $obdfilter_name on $ost_nid"
13387         # make sure we can find loopback nid
13388         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
13389
13390         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
13391                            ${obdfilter_name}_osc_UUID || rc=2; }
13392         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
13393                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
13394         return $rc
13395 }
13396
13397 cleanup_obdecho_osc () {
13398         local obdfilter_name=$1
13399         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
13400         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
13401         return 0
13402 }
13403
13404 obdecho_test() {
13405         local OBD=$1
13406         local node=$2
13407         local pages=${3:-64}
13408         local rc=0
13409         local id
13410
13411         local count=10
13412         local obd_size=$(get_obd_size $node $OBD)
13413         local page_size=$(get_page_size $node)
13414         if [[ -n "$obd_size" ]]; then
13415                 local new_count=$((obd_size / (pages * page_size / 1024)))
13416                 [[ $new_count -ge $count ]] || count=$new_count
13417         fi
13418
13419         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
13420         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
13421                            rc=2; }
13422         if [ $rc -eq 0 ]; then
13423             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
13424             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
13425         fi
13426         echo "New object id is $id"
13427         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
13428                            rc=4; }
13429         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
13430                            "test_brw $count w v $pages $id" || rc=4; }
13431         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
13432                            rc=4; }
13433         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
13434                                         "cleanup" || rc=5; }
13435         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
13436                                         "detach" || rc=6; }
13437         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
13438         return $rc
13439 }
13440
13441 test_180a() {
13442         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13443
13444         if ! module_loaded obdecho; then
13445                 load_module obdecho/obdecho &&
13446                         stack_trap "rmmod obdecho" EXIT ||
13447                         error "unable to load obdecho on client"
13448         fi
13449
13450         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
13451         local host=$($LCTL get_param -n osc.$osc.import |
13452                      awk '/current_connection:/ { print $2 }' )
13453         local target=$($LCTL get_param -n osc.$osc.import |
13454                        awk '/target:/ { print $2 }' )
13455         target=${target%_UUID}
13456
13457         if [ -n "$target" ]; then
13458                 setup_obdecho_osc $host $target &&
13459                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
13460                         { error "obdecho setup failed with $?"; return; }
13461
13462                 obdecho_test ${target}_osc client ||
13463                         error "obdecho_test failed on ${target}_osc"
13464         else
13465                 $LCTL get_param osc.$osc.import
13466                 error "there is no osc.$osc.import target"
13467         fi
13468 }
13469 run_test 180a "test obdecho on osc"
13470
13471 test_180b() {
13472         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13473         remote_ost_nodsh && skip "remote OST with nodsh"
13474
13475         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
13476                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
13477                 error "failed to load module obdecho"
13478
13479         local target=$(do_facet ost1 $LCTL dl |
13480                        awk '/obdfilter/ { print $4; exit; }')
13481
13482         if [ -n "$target" ]; then
13483                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
13484         else
13485                 do_facet ost1 $LCTL dl
13486                 error "there is no obdfilter target on ost1"
13487         fi
13488 }
13489 run_test 180b "test obdecho directly on obdfilter"
13490
13491 test_180c() { # LU-2598
13492         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13493         remote_ost_nodsh && skip "remote OST with nodsh"
13494         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
13495                 skip "Need MDS version at least 2.4.0"
13496
13497         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
13498                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
13499                 error "failed to load module obdecho"
13500
13501         local target=$(do_facet ost1 $LCTL dl |
13502                        awk '/obdfilter/ { print $4; exit; }')
13503
13504         if [ -n "$target" ]; then
13505                 local pages=16384 # 64MB bulk I/O RPC size
13506
13507                 obdecho_test "$target" ost1 "$pages" ||
13508                         error "obdecho_test with pages=$pages failed with $?"
13509         else
13510                 do_facet ost1 $LCTL dl
13511                 error "there is no obdfilter target on ost1"
13512         fi
13513 }
13514 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
13515
13516 test_181() { # bug 22177
13517         test_mkdir $DIR/$tdir
13518         # create enough files to index the directory
13519         createmany -o $DIR/$tdir/foobar 4000
13520         # print attributes for debug purpose
13521         lsattr -d .
13522         # open dir
13523         multiop_bg_pause $DIR/$tdir D_Sc || return 1
13524         MULTIPID=$!
13525         # remove the files & current working dir
13526         unlinkmany $DIR/$tdir/foobar 4000
13527         rmdir $DIR/$tdir
13528         kill -USR1 $MULTIPID
13529         wait $MULTIPID
13530         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
13531         return 0
13532 }
13533 run_test 181 "Test open-unlinked dir ========================"
13534
13535 test_182() {
13536         local fcount=1000
13537         local tcount=10
13538
13539         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
13540
13541         $LCTL set_param mdc.*.rpc_stats=clear
13542
13543         for (( i = 0; i < $tcount; i++ )) ; do
13544                 mkdir $DIR/$tdir/$i
13545         done
13546
13547         for (( i = 0; i < $tcount; i++ )) ; do
13548                 createmany -o $DIR/$tdir/$i/f- $fcount &
13549         done
13550         wait
13551
13552         for (( i = 0; i < $tcount; i++ )) ; do
13553                 unlinkmany $DIR/$tdir/$i/f- $fcount &
13554         done
13555         wait
13556
13557         $LCTL get_param mdc.*.rpc_stats
13558
13559         rm -rf $DIR/$tdir
13560 }
13561 run_test 182 "Test parallel modify metadata operations ================"
13562
13563 test_183() { # LU-2275
13564         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13565         remote_mds_nodsh && skip "remote MDS with nodsh"
13566         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
13567                 skip "Need MDS version at least 2.3.56"
13568
13569         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
13570         echo aaa > $DIR/$tdir/$tfile
13571
13572 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
13573         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
13574
13575         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
13576         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
13577
13578         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
13579
13580         # Flush negative dentry cache
13581         touch $DIR/$tdir/$tfile
13582
13583         # We are not checking for any leaked references here, they'll
13584         # become evident next time we do cleanup with module unload.
13585         rm -rf $DIR/$tdir
13586 }
13587 run_test 183 "No crash or request leak in case of strange dispositions ========"
13588
13589 # test suite 184 is for LU-2016, LU-2017
13590 test_184a() {
13591         check_swap_layouts_support
13592
13593         dir0=$DIR/$tdir/$testnum
13594         test_mkdir -p -c1 $dir0
13595         ref1=/etc/passwd
13596         ref2=/etc/group
13597         file1=$dir0/f1
13598         file2=$dir0/f2
13599         $SETSTRIPE -c1 $file1
13600         cp $ref1 $file1
13601         $SETSTRIPE -c2 $file2
13602         cp $ref2 $file2
13603         gen1=$($GETSTRIPE -g $file1)
13604         gen2=$($GETSTRIPE -g $file2)
13605
13606         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
13607         gen=$($GETSTRIPE -g $file1)
13608         [[ $gen1 != $gen ]] ||
13609                 "Layout generation on $file1 does not change"
13610         gen=$($GETSTRIPE -g $file2)
13611         [[ $gen2 != $gen ]] ||
13612                 "Layout generation on $file2 does not change"
13613
13614         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
13615         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
13616
13617         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
13618 }
13619 run_test 184a "Basic layout swap"
13620
13621 test_184b() {
13622         check_swap_layouts_support
13623
13624         dir0=$DIR/$tdir/$testnum
13625         mkdir -p $dir0 || error "creating dir $dir0"
13626         file1=$dir0/f1
13627         file2=$dir0/f2
13628         file3=$dir0/f3
13629         dir1=$dir0/d1
13630         dir2=$dir0/d2
13631         mkdir $dir1 $dir2
13632         $SETSTRIPE -c1 $file1
13633         $SETSTRIPE -c2 $file2
13634         $SETSTRIPE -c1 $file3
13635         chown $RUNAS_ID $file3
13636         gen1=$($GETSTRIPE -g $file1)
13637         gen2=$($GETSTRIPE -g $file2)
13638
13639         $LFS swap_layouts $dir1 $dir2 &&
13640                 error "swap of directories layouts should fail"
13641         $LFS swap_layouts $dir1 $file1 &&
13642                 error "swap of directory and file layouts should fail"
13643         $RUNAS $LFS swap_layouts $file1 $file2 &&
13644                 error "swap of file we cannot write should fail"
13645         $LFS swap_layouts $file1 $file3 &&
13646                 error "swap of file with different owner should fail"
13647         /bin/true # to clear error code
13648 }
13649 run_test 184b "Forbidden layout swap (will generate errors)"
13650
13651 test_184c() {
13652         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
13653         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
13654         check_swap_layouts_support
13655
13656         local dir0=$DIR/$tdir/$testnum
13657         mkdir -p $dir0 || error "creating dir $dir0"
13658
13659         local ref1=$dir0/ref1
13660         local ref2=$dir0/ref2
13661         local file1=$dir0/file1
13662         local file2=$dir0/file2
13663         # create a file large enough for the concurrent test
13664         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
13665         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
13666         echo "ref file size: ref1($(stat -c %s $ref1))," \
13667              "ref2($(stat -c %s $ref2))"
13668
13669         cp $ref2 $file2
13670         dd if=$ref1 of=$file1 bs=16k &
13671         local DD_PID=$!
13672
13673         # Make sure dd starts to copy file
13674         while [ ! -f $file1 ]; do sleep 0.1; done
13675
13676         $LFS swap_layouts $file1 $file2
13677         local rc=$?
13678         wait $DD_PID
13679         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
13680         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
13681
13682         # how many bytes copied before swapping layout
13683         local copied=$(stat -c %s $file2)
13684         local remaining=$(stat -c %s $ref1)
13685         remaining=$((remaining - copied))
13686         echo "Copied $copied bytes before swapping layout..."
13687
13688         cmp -n $copied $file1 $ref2 | grep differ &&
13689                 error "Content mismatch [0, $copied) of ref2 and file1"
13690         cmp -n $copied $file2 $ref1 ||
13691                 error "Content mismatch [0, $copied) of ref1 and file2"
13692         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
13693                 error "Content mismatch [$copied, EOF) of ref1 and file1"
13694
13695         # clean up
13696         rm -f $ref1 $ref2 $file1 $file2
13697 }
13698 run_test 184c "Concurrent write and layout swap"
13699
13700 test_184d() {
13701         check_swap_layouts_support
13702         [ -z "$(which getfattr 2>/dev/null)" ] &&
13703                 skip_env "no getfattr command"
13704
13705         local file1=$DIR/$tdir/$tfile-1
13706         local file2=$DIR/$tdir/$tfile-2
13707         local file3=$DIR/$tdir/$tfile-3
13708         local lovea1
13709         local lovea2
13710
13711         mkdir -p $DIR/$tdir
13712         touch $file1 || error "create $file1 failed"
13713         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
13714                 error "create $file2 failed"
13715         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
13716                 error "create $file3 failed"
13717         lovea1=$(get_layout_param $file1)
13718
13719         $LFS swap_layouts $file2 $file3 ||
13720                 error "swap $file2 $file3 layouts failed"
13721         $LFS swap_layouts $file1 $file2 ||
13722                 error "swap $file1 $file2 layouts failed"
13723
13724         lovea2=$(get_layout_param $file2)
13725         echo "$lovea1"
13726         echo "$lovea2"
13727         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
13728
13729         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
13730         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
13731 }
13732 run_test 184d "allow stripeless layouts swap"
13733
13734 test_184e() {
13735         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
13736                 skip "Need MDS version at least 2.6.94"
13737         check_swap_layouts_support
13738         [ -z "$(which getfattr 2>/dev/null)" ] &&
13739                 skip_env "no getfattr command"
13740
13741         local file1=$DIR/$tdir/$tfile-1
13742         local file2=$DIR/$tdir/$tfile-2
13743         local file3=$DIR/$tdir/$tfile-3
13744         local lovea
13745
13746         mkdir -p $DIR/$tdir
13747         touch $file1 || error "create $file1 failed"
13748         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
13749                 error "create $file2 failed"
13750         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
13751                 error "create $file3 failed"
13752
13753         $LFS swap_layouts $file1 $file2 ||
13754                 error "swap $file1 $file2 layouts failed"
13755
13756         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
13757         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
13758
13759         echo 123 > $file1 || error "Should be able to write into $file1"
13760
13761         $LFS swap_layouts $file1 $file3 ||
13762                 error "swap $file1 $file3 layouts failed"
13763
13764         echo 123 > $file1 || error "Should be able to write into $file1"
13765
13766         rm -rf $file1 $file2 $file3
13767 }
13768 run_test 184e "Recreate layout after stripeless layout swaps"
13769
13770 test_184f() {
13771         # Create a file with name longer than sizeof(struct stat) ==
13772         # 144 to see if we can get chars from the file name to appear
13773         # in the returned striping. Note that 'f' == 0x66.
13774         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
13775
13776         mkdir -p $DIR/$tdir
13777         mcreate $DIR/$tdir/$file
13778         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
13779                 error "IOC_MDC_GETFILEINFO returned garbage striping"
13780         fi
13781 }
13782 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
13783
13784 test_185() { # LU-2441
13785         # LU-3553 - no volatile file support in old servers
13786         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
13787                 skip "Need MDS version at least 2.3.60"
13788
13789         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
13790         touch $DIR/$tdir/spoo
13791         local mtime1=$(stat -c "%Y" $DIR/$tdir)
13792         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
13793                 error "cannot create/write a volatile file"
13794         [ "$FILESET" == "" ] &&
13795         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
13796                 error "FID is still valid after close"
13797
13798         multiop_bg_pause $DIR/$tdir vVw4096_c
13799         local multi_pid=$!
13800
13801         local OLD_IFS=$IFS
13802         IFS=":"
13803         local fidv=($fid)
13804         IFS=$OLD_IFS
13805         # assume that the next FID for this client is sequential, since stdout
13806         # is unfortunately eaten by multiop_bg_pause
13807         local n=$((${fidv[1]} + 1))
13808         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
13809         if [ "$FILESET" == "" ]; then
13810                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
13811                         error "FID is missing before close"
13812         fi
13813         kill -USR1 $multi_pid
13814         # 1 second delay, so if mtime change we will see it
13815         sleep 1
13816         local mtime2=$(stat -c "%Y" $DIR/$tdir)
13817         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
13818 }
13819 run_test 185 "Volatile file support"
13820
13821 test_187a() {
13822         remote_mds_nodsh && skip "remote MDS with nodsh"
13823         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
13824                 skip "Need MDS version at least 2.3.0"
13825
13826         local dir0=$DIR/$tdir/$testnum
13827         mkdir -p $dir0 || error "creating dir $dir0"
13828
13829         local file=$dir0/file1
13830         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
13831         local dv1=$($LFS data_version $file)
13832         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
13833         local dv2=$($LFS data_version $file)
13834         [[ $dv1 != $dv2 ]] ||
13835                 error "data version did not change on write $dv1 == $dv2"
13836
13837         # clean up
13838         rm -f $file1
13839 }
13840 run_test 187a "Test data version change"
13841
13842 test_187b() {
13843         remote_mds_nodsh && skip "remote MDS with nodsh"
13844         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
13845                 skip "Need MDS version at least 2.3.0"
13846
13847         local dir0=$DIR/$tdir/$testnum
13848         mkdir -p $dir0 || error "creating dir $dir0"
13849
13850         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
13851         [[ ${DV[0]} != ${DV[1]} ]] ||
13852                 error "data version did not change on write"\
13853                       " ${DV[0]} == ${DV[1]}"
13854
13855         # clean up
13856         rm -f $file1
13857 }
13858 run_test 187b "Test data version change on volatile file"
13859
13860 test_200() {
13861         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13862         remote_mgs_nodsh && skip "remote MGS with nodsh"
13863         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13864
13865         local POOL=${POOL:-cea1}
13866         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
13867         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
13868         # Pool OST targets
13869         local first_ost=0
13870         local last_ost=$(($OSTCOUNT - 1))
13871         local ost_step=2
13872         local ost_list=$(seq $first_ost $ost_step $last_ost)
13873         local ost_range="$first_ost $last_ost $ost_step"
13874         local test_path=$POOL_ROOT/$POOL_DIR_NAME
13875         local file_dir=$POOL_ROOT/file_tst
13876         local subdir=$test_path/subdir
13877         local rc=0
13878
13879         if ! combined_mgs_mds ; then
13880                 mount_mgs_client
13881         fi
13882
13883         while : ; do
13884                 # former test_200a test_200b
13885                 pool_add $POOL                          || { rc=$? ; break; }
13886                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
13887                 # former test_200c test_200d
13888                 mkdir -p $test_path
13889                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
13890                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
13891                 mkdir -p $subdir
13892                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
13893                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
13894                                                         || { rc=$? ; break; }
13895                 # former test_200e test_200f
13896                 local files=$((OSTCOUNT*3))
13897                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
13898                                                         || { rc=$? ; break; }
13899                 pool_create_files $POOL $file_dir $files "$ost_list" \
13900                                                         || { rc=$? ; break; }
13901                 # former test_200g test_200h
13902                 pool_lfs_df $POOL                       || { rc=$? ; break; }
13903                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
13904
13905                 # former test_201a test_201b test_201c
13906                 pool_remove_first_target $POOL          || { rc=$? ; break; }
13907
13908                 local f=$test_path/$tfile
13909                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
13910                 pool_remove $POOL $f                    || { rc=$? ; break; }
13911                 break
13912         done
13913
13914         destroy_test_pools
13915
13916         if ! combined_mgs_mds ; then
13917                 umount_mgs_client
13918         fi
13919         return $rc
13920 }
13921 run_test 200 "OST pools"
13922
13923 # usage: default_attr <count | size | offset>
13924 default_attr() {
13925         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
13926 }
13927
13928 # usage: check_default_stripe_attr
13929 check_default_stripe_attr() {
13930         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
13931         case $1 in
13932         --stripe-count|-c)
13933                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
13934         --stripe-size|-S)
13935                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
13936         --stripe-index|-i)
13937                 EXPECTED=-1;;
13938         *)
13939                 error "unknown getstripe attr '$1'"
13940         esac
13941
13942         [ $ACTUAL == $EXPECTED ] ||
13943                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
13944 }
13945
13946 test_204a() {
13947         test_mkdir $DIR/$tdir
13948         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
13949
13950         check_default_stripe_attr --stripe-count
13951         check_default_stripe_attr --stripe-size
13952         check_default_stripe_attr --stripe-index
13953 }
13954 run_test 204a "Print default stripe attributes"
13955
13956 test_204b() {
13957         test_mkdir $DIR/$tdir
13958         $SETSTRIPE --stripe-count 1 $DIR/$tdir
13959
13960         check_default_stripe_attr --stripe-size
13961         check_default_stripe_attr --stripe-index
13962 }
13963 run_test 204b "Print default stripe size and offset"
13964
13965 test_204c() {
13966         test_mkdir $DIR/$tdir
13967         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
13968
13969         check_default_stripe_attr --stripe-count
13970         check_default_stripe_attr --stripe-index
13971 }
13972 run_test 204c "Print default stripe count and offset"
13973
13974 test_204d() {
13975         test_mkdir $DIR/$tdir
13976         $SETSTRIPE --stripe-index 0 $DIR/$tdir
13977
13978         check_default_stripe_attr --stripe-count
13979         check_default_stripe_attr --stripe-size
13980 }
13981 run_test 204d "Print default stripe count and size"
13982
13983 test_204e() {
13984         test_mkdir $DIR/$tdir
13985         $SETSTRIPE -d $DIR/$tdir
13986
13987         check_default_stripe_attr --stripe-count --raw
13988         check_default_stripe_attr --stripe-size --raw
13989         check_default_stripe_attr --stripe-index --raw
13990 }
13991 run_test 204e "Print raw stripe attributes"
13992
13993 test_204f() {
13994         test_mkdir $DIR/$tdir
13995         $SETSTRIPE --stripe-count 1 $DIR/$tdir
13996
13997         check_default_stripe_attr --stripe-size --raw
13998         check_default_stripe_attr --stripe-index --raw
13999 }
14000 run_test 204f "Print raw stripe size and offset"
14001
14002 test_204g() {
14003         test_mkdir $DIR/$tdir
14004         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
14005
14006         check_default_stripe_attr --stripe-count --raw
14007         check_default_stripe_attr --stripe-index --raw
14008 }
14009 run_test 204g "Print raw stripe count and offset"
14010
14011 test_204h() {
14012         test_mkdir $DIR/$tdir
14013         $SETSTRIPE --stripe-index 0 $DIR/$tdir
14014
14015         check_default_stripe_attr --stripe-count --raw
14016         check_default_stripe_attr --stripe-size --raw
14017 }
14018 run_test 204h "Print raw stripe count and size"
14019
14020 # Figure out which job scheduler is being used, if any,
14021 # or use a fake one
14022 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
14023         JOBENV=SLURM_JOB_ID
14024 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
14025         JOBENV=LSB_JOBID
14026 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
14027         JOBENV=PBS_JOBID
14028 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
14029         JOBENV=LOADL_STEP_ID
14030 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
14031         JOBENV=JOB_ID
14032 else
14033         $LCTL list_param jobid_name > /dev/null 2>&1
14034         if [ $? -eq 0 ]; then
14035                 JOBENV=nodelocal
14036         else
14037                 JOBENV=FAKE_JOBID
14038         fi
14039 fi
14040 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
14041
14042 verify_jobstats() {
14043         local cmd=($1)
14044         shift
14045         local facets="$@"
14046
14047 # we don't really need to clear the stats for this test to work, since each
14048 # command has a unique jobid, but it makes debugging easier if needed.
14049 #       for facet in $facets; do
14050 #               local dev=$(convert_facet2label $facet)
14051 #               # clear old jobstats
14052 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
14053 #       done
14054
14055         # use a new JobID for each test, or we might see an old one
14056         [ "$JOBENV" = "FAKE_JOBID" ] &&
14057                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
14058
14059         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
14060
14061         [ "$JOBENV" = "nodelocal" ] && {
14062                 FAKE_JOBID=id.$testnum.%e.$RANDOM
14063                 $LCTL set_param jobid_name=$FAKE_JOBID
14064                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
14065         }
14066
14067         log "Test: ${cmd[*]}"
14068         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
14069
14070         if [ $JOBENV = "FAKE_JOBID" ]; then
14071                 FAKE_JOBID=$JOBVAL ${cmd[*]}
14072         else
14073                 ${cmd[*]}
14074         fi
14075
14076         # all files are created on OST0000
14077         for facet in $facets; do
14078                 local stats="*.$(convert_facet2label $facet).job_stats"
14079
14080                 # strip out libtool wrappers for in-tree executables
14081                 if [ $(do_facet $facet lctl get_param $stats |
14082                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
14083                         do_facet $facet lctl get_param $stats
14084                         error "No jobstats for $JOBVAL found on $facet::$stats"
14085                 fi
14086         done
14087 }
14088
14089 jobstats_set() {
14090         local new_jobenv=$1
14091
14092         set_persistent_param_and_check client "jobid_var" \
14093                 "$FSNAME.sys.jobid_var" $new_jobenv
14094 }
14095
14096 test_205() { # Job stats
14097         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14098         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
14099                 skip "Need MDS version with at least 2.7.1"
14100         remote_mgs_nodsh && skip "remote MGS with nodsh"
14101         remote_mds_nodsh && skip "remote MDS with nodsh"
14102         remote_ost_nodsh && skip "remote OST with nodsh"
14103         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
14104                 skip "Server doesn't support jobstats"
14105         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
14106
14107         local old_jobenv=$($LCTL get_param -n jobid_var)
14108         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
14109
14110         if [[ $PERM_CMD = *"set_param -P"* ]]; then
14111                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
14112         else
14113                 stack_trap "do_facet mgs $PERM_CMD \
14114                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
14115         fi
14116         changelog_register
14117
14118         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
14119                                 mdt.*.job_cleanup_interval | head -n 1)
14120         local new_interval=5
14121         do_facet $SINGLEMDS \
14122                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
14123         stack_trap "do_facet $SINGLEMDS \
14124                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
14125         local start=$SECONDS
14126
14127         local cmd
14128         # mkdir
14129         cmd="mkdir $DIR/$tdir"
14130         verify_jobstats "$cmd" "$SINGLEMDS"
14131         # rmdir
14132         cmd="rmdir $DIR/$tdir"
14133         verify_jobstats "$cmd" "$SINGLEMDS"
14134         # mkdir on secondary MDT
14135         if [ $MDSCOUNT -gt 1 ]; then
14136                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
14137                 verify_jobstats "$cmd" "mds2"
14138         fi
14139         # mknod
14140         cmd="mknod $DIR/$tfile c 1 3"
14141         verify_jobstats "$cmd" "$SINGLEMDS"
14142         # unlink
14143         cmd="rm -f $DIR/$tfile"
14144         verify_jobstats "$cmd" "$SINGLEMDS"
14145         # create all files on OST0000 so verify_jobstats can find OST stats
14146         # open & close
14147         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
14148         verify_jobstats "$cmd" "$SINGLEMDS"
14149         # setattr
14150         cmd="touch $DIR/$tfile"
14151         verify_jobstats "$cmd" "$SINGLEMDS ost1"
14152         # write
14153         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
14154         verify_jobstats "$cmd" "ost1"
14155         # read
14156         cancel_lru_locks osc
14157         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
14158         verify_jobstats "$cmd" "ost1"
14159         # truncate
14160         cmd="$TRUNCATE $DIR/$tfile 0"
14161         verify_jobstats "$cmd" "$SINGLEMDS ost1"
14162         # rename
14163         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
14164         verify_jobstats "$cmd" "$SINGLEMDS"
14165         # jobstats expiry - sleep until old stats should be expired
14166         local left=$((new_interval + 5 - (SECONDS - start)))
14167         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
14168                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
14169                         "0" $left
14170         cmd="mkdir $DIR/$tdir.expire"
14171         verify_jobstats "$cmd" "$SINGLEMDS"
14172         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
14173             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
14174
14175         # Ensure that jobid are present in changelog (if supported by MDS)
14176         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
14177                 changelog_dump | tail -10
14178                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
14179                 [ $jobids -eq 9 ] ||
14180                         error "Wrong changelog jobid count $jobids != 9"
14181
14182                 # LU-5862
14183                 JOBENV="disable"
14184                 jobstats_set $JOBENV
14185                 touch $DIR/$tfile
14186                 changelog_dump | grep $tfile
14187                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
14188                 [ $jobids -eq 0 ] ||
14189                         error "Unexpected jobids when jobid_var=$JOBENV"
14190         fi
14191
14192         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
14193         JOBENV="JOBCOMPLEX"
14194         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
14195
14196         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
14197 }
14198 run_test 205 "Verify job stats"
14199
14200 # LU-1480, LU-1773 and LU-1657
14201 test_206() {
14202         mkdir -p $DIR/$tdir
14203         $SETSTRIPE -c -1 $DIR/$tdir
14204 #define OBD_FAIL_LOV_INIT 0x1403
14205         $LCTL set_param fail_loc=0xa0001403
14206         $LCTL set_param fail_val=1
14207         touch $DIR/$tdir/$tfile || true
14208 }
14209 run_test 206 "fail lov_init_raid0() doesn't lbug"
14210
14211 test_207a() {
14212         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
14213         local fsz=`stat -c %s $DIR/$tfile`
14214         cancel_lru_locks mdc
14215
14216         # do not return layout in getattr intent
14217 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
14218         $LCTL set_param fail_loc=0x170
14219         local sz=`stat -c %s $DIR/$tfile`
14220
14221         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
14222
14223         rm -rf $DIR/$tfile
14224 }
14225 run_test 207a "can refresh layout at glimpse"
14226
14227 test_207b() {
14228         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
14229         local cksum=`md5sum $DIR/$tfile`
14230         local fsz=`stat -c %s $DIR/$tfile`
14231         cancel_lru_locks mdc
14232         cancel_lru_locks osc
14233
14234         # do not return layout in getattr intent
14235 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
14236         $LCTL set_param fail_loc=0x171
14237
14238         # it will refresh layout after the file is opened but before read issues
14239         echo checksum is "$cksum"
14240         echo "$cksum" |md5sum -c --quiet || error "file differs"
14241
14242         rm -rf $DIR/$tfile
14243 }
14244 run_test 207b "can refresh layout at open"
14245
14246 test_208() {
14247         # FIXME: in this test suite, only RD lease is used. This is okay
14248         # for now as only exclusive open is supported. After generic lease
14249         # is done, this test suite should be revised. - Jinshan
14250
14251         remote_mds_nodsh && skip "remote MDS with nodsh"
14252         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
14253                 skip "Need MDS version at least 2.4.52"
14254
14255         echo "==== test 1: verify get lease work"
14256         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
14257
14258         echo "==== test 2: verify lease can be broken by upcoming open"
14259         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
14260         local PID=$!
14261         sleep 1
14262
14263         $MULTIOP $DIR/$tfile oO_RDONLY:c
14264         kill -USR1 $PID && wait $PID || error "break lease error"
14265
14266         echo "==== test 3: verify lease can't be granted if an open already exists"
14267         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
14268         local PID=$!
14269         sleep 1
14270
14271         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
14272         kill -USR1 $PID && wait $PID || error "open file error"
14273
14274         echo "==== test 4: lease can sustain over recovery"
14275         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
14276         PID=$!
14277         sleep 1
14278
14279         fail mds1
14280
14281         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
14282
14283         echo "==== test 5: lease broken can't be regained by replay"
14284         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
14285         PID=$!
14286         sleep 1
14287
14288         # open file to break lease and then recovery
14289         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
14290         fail mds1
14291
14292         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
14293
14294         rm -f $DIR/$tfile
14295 }
14296 run_test 208 "Exclusive open"
14297
14298 test_209() {
14299         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
14300                 skip_env "must have disp_stripe"
14301
14302         touch $DIR/$tfile
14303         sync; sleep 5; sync;
14304
14305         echo 3 > /proc/sys/vm/drop_caches
14306         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
14307
14308         # open/close 500 times
14309         for i in $(seq 500); do
14310                 cat $DIR/$tfile
14311         done
14312
14313         echo 3 > /proc/sys/vm/drop_caches
14314         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
14315
14316         echo "before: $req_before, after: $req_after"
14317         [ $((req_after - req_before)) -ge 300 ] &&
14318                 error "open/close requests are not freed"
14319         return 0
14320 }
14321 run_test 209 "read-only open/close requests should be freed promptly"
14322
14323 test_212() {
14324         size=`date +%s`
14325         size=$((size % 8192 + 1))
14326         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
14327         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
14328         rm -f $DIR/f212 $DIR/f212.xyz
14329 }
14330 run_test 212 "Sendfile test ============================================"
14331
14332 test_213() {
14333         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
14334         cancel_lru_locks osc
14335         lctl set_param fail_loc=0x8000040f
14336         # generate a read lock
14337         cat $DIR/$tfile > /dev/null
14338         # write to the file, it will try to cancel the above read lock.
14339         cat /etc/hosts >> $DIR/$tfile
14340 }
14341 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
14342
14343 test_214() { # for bug 20133
14344         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
14345         for (( i=0; i < 340; i++ )) ; do
14346                 touch $DIR/$tdir/d214c/a$i
14347         done
14348
14349         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
14350         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
14351         ls $DIR/d214c || error "ls $DIR/d214c failed"
14352         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
14353         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
14354 }
14355 run_test 214 "hash-indexed directory test - bug 20133"
14356
14357 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
14358 create_lnet_proc_files() {
14359         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
14360 }
14361
14362 # counterpart of create_lnet_proc_files
14363 remove_lnet_proc_files() {
14364         rm -f $TMP/lnet_$1.sys
14365 }
14366
14367 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
14368 # 3rd arg as regexp for body
14369 check_lnet_proc_stats() {
14370         local l=$(cat "$TMP/lnet_$1" |wc -l)
14371         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
14372
14373         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
14374 }
14375
14376 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
14377 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
14378 # optional and can be regexp for 2nd line (lnet.routes case)
14379 check_lnet_proc_entry() {
14380         local blp=2          # blp stands for 'position of 1st line of body'
14381         [ -z "$5" ] || blp=3 # lnet.routes case
14382
14383         local l=$(cat "$TMP/lnet_$1" |wc -l)
14384         # subtracting one from $blp because the body can be empty
14385         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
14386
14387         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
14388                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
14389
14390         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
14391                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
14392
14393         # bail out if any unexpected line happened
14394         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
14395         [ "$?" != 0 ] || error "$2 misformatted"
14396 }
14397
14398 test_215() { # for bugs 18102, 21079, 21517
14399         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14400
14401         local N='(0|[1-9][0-9]*)'       # non-negative numeric
14402         local P='[1-9][0-9]*'           # positive numeric
14403         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
14404         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
14405         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
14406         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
14407
14408         local L1 # regexp for 1st line
14409         local L2 # regexp for 2nd line (optional)
14410         local BR # regexp for the rest (body)
14411
14412         # lnet.stats should look as 11 space-separated non-negative numerics
14413         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
14414         create_lnet_proc_files "stats"
14415         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
14416         remove_lnet_proc_files "stats"
14417
14418         # lnet.routes should look like this:
14419         # Routing disabled/enabled
14420         # net hops priority state router
14421         # where net is a string like tcp0, hops > 0, priority >= 0,
14422         # state is up/down,
14423         # router is a string like 192.168.1.1@tcp2
14424         L1="^Routing (disabled|enabled)$"
14425         L2="^net +hops +priority +state +router$"
14426         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
14427         create_lnet_proc_files "routes"
14428         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
14429         remove_lnet_proc_files "routes"
14430
14431         # lnet.routers should look like this:
14432         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
14433         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
14434         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
14435         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
14436         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
14437         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
14438         create_lnet_proc_files "routers"
14439         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
14440         remove_lnet_proc_files "routers"
14441
14442         # lnet.peers should look like this:
14443         # nid refs state last max rtr min tx min queue
14444         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
14445         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
14446         # numeric (0 or >0 or <0), queue >= 0.
14447         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
14448         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
14449         create_lnet_proc_files "peers"
14450         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
14451         remove_lnet_proc_files "peers"
14452
14453         # lnet.buffers  should look like this:
14454         # pages count credits min
14455         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
14456         L1="^pages +count +credits +min$"
14457         BR="^ +$N +$N +$I +$I$"
14458         create_lnet_proc_files "buffers"
14459         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
14460         remove_lnet_proc_files "buffers"
14461
14462         # lnet.nis should look like this:
14463         # nid status alive refs peer rtr max tx min
14464         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
14465         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
14466         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
14467         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
14468         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
14469         create_lnet_proc_files "nis"
14470         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
14471         remove_lnet_proc_files "nis"
14472
14473         # can we successfully write to lnet.stats?
14474         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
14475 }
14476 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
14477
14478 test_216() { # bug 20317
14479         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14480         remote_ost_nodsh && skip "remote OST with nodsh"
14481
14482         local node
14483         local facets=$(get_facets OST)
14484         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
14485
14486         save_lustre_params client "osc.*.contention_seconds" > $p
14487         save_lustre_params $facets \
14488                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
14489         save_lustre_params $facets \
14490                 "ldlm.namespaces.filter-*.contended_locks" >> $p
14491         save_lustre_params $facets \
14492                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
14493         clear_stats osc.*.osc_stats
14494
14495         # agressive lockless i/o settings
14496         do_nodes $(comma_list $(osts_nodes)) \
14497                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
14498                         ldlm.namespaces.filter-*.contended_locks=0 \
14499                         ldlm.namespaces.filter-*.contention_seconds=60"
14500         lctl set_param -n osc.*.contention_seconds=60
14501
14502         $DIRECTIO write $DIR/$tfile 0 10 4096
14503         $CHECKSTAT -s 40960 $DIR/$tfile
14504
14505         # disable lockless i/o
14506         do_nodes $(comma_list $(osts_nodes)) \
14507                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
14508                         ldlm.namespaces.filter-*.contended_locks=32 \
14509                         ldlm.namespaces.filter-*.contention_seconds=0"
14510         lctl set_param -n osc.*.contention_seconds=0
14511         clear_stats osc.*.osc_stats
14512
14513         dd if=/dev/zero of=$DIR/$tfile count=0
14514         $CHECKSTAT -s 0 $DIR/$tfile
14515
14516         restore_lustre_params <$p
14517         rm -f $p
14518         rm $DIR/$tfile
14519 }
14520 run_test 216 "check lockless direct write updates file size and kms correctly"
14521
14522 test_217() { # bug 22430
14523         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14524
14525         local node
14526         local nid
14527
14528         for node in $(nodes_list); do
14529                 nid=$(host_nids_address $node $NETTYPE)
14530                 if [[ $nid = *-* ]] ; then
14531                         echo "lctl ping $(h2nettype $nid)"
14532                         lctl ping $(h2nettype $nid)
14533                 else
14534                         echo "skipping $node (no hyphen detected)"
14535                 fi
14536         done
14537 }
14538 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
14539
14540 test_218() {
14541        # do directio so as not to populate the page cache
14542        log "creating a 10 Mb file"
14543        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
14544        log "starting reads"
14545        dd if=$DIR/$tfile of=/dev/null bs=4096 &
14546        log "truncating the file"
14547        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
14548        log "killing dd"
14549        kill %+ || true # reads might have finished
14550        echo "wait until dd is finished"
14551        wait
14552        log "removing the temporary file"
14553        rm -rf $DIR/$tfile || error "tmp file removal failed"
14554 }
14555 run_test 218 "parallel read and truncate should not deadlock"
14556
14557 test_219() {
14558         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14559
14560         # write one partial page
14561         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
14562         # set no grant so vvp_io_commit_write will do sync write
14563         $LCTL set_param fail_loc=0x411
14564         # write a full page at the end of file
14565         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
14566
14567         $LCTL set_param fail_loc=0
14568         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
14569         $LCTL set_param fail_loc=0x411
14570         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
14571
14572         # LU-4201
14573         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
14574         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
14575 }
14576 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
14577
14578 test_220() { #LU-325
14579         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14580         remote_ost_nodsh && skip "remote OST with nodsh"
14581         remote_mds_nodsh && skip "remote MDS with nodsh"
14582         remote_mgs_nodsh && skip "remote MGS with nodsh"
14583
14584         local OSTIDX=0
14585
14586         # create on MDT0000 so the last_id and next_id are correct
14587         mkdir $DIR/$tdir
14588         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
14589         OST=${OST%_UUID}
14590
14591         # on the mdt's osc
14592         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
14593         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
14594                         osc.$mdtosc_proc1.prealloc_last_id)
14595         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
14596                         osc.$mdtosc_proc1.prealloc_next_id)
14597
14598         $LFS df -i
14599
14600         if ! combined_mgs_mds ; then
14601                 mount_mgs_client
14602         fi
14603
14604         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
14605         #define OBD_FAIL_OST_ENOINO              0x229
14606         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
14607         create_pool $FSNAME.$TESTNAME || return 1
14608         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
14609
14610         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
14611
14612         MDSOBJS=$((last_id - next_id))
14613         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
14614
14615         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
14616         echo "OST still has $count kbytes free"
14617
14618         echo "create $MDSOBJS files @next_id..."
14619         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
14620
14621         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
14622                         osc.$mdtosc_proc1.prealloc_last_id)
14623         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
14624                         osc.$mdtosc_proc1.prealloc_next_id)
14625
14626         echo "after creation, last_id=$last_id2, next_id=$next_id2"
14627         $LFS df -i
14628
14629         echo "cleanup..."
14630
14631         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
14632         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
14633
14634         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
14635                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
14636         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
14637                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
14638         echo "unlink $MDSOBJS files @$next_id..."
14639         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
14640
14641         if ! combined_mgs_mds ; then
14642                 umount_mgs_client
14643         fi
14644 }
14645 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
14646
14647 test_221() {
14648         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14649
14650         dd if=`which date` of=$MOUNT/date oflag=sync
14651         chmod +x $MOUNT/date
14652
14653         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
14654         $LCTL set_param fail_loc=0x80001401
14655
14656         $MOUNT/date > /dev/null
14657         rm -f $MOUNT/date
14658 }
14659 run_test 221 "make sure fault and truncate race to not cause OOM"
14660
14661 test_222a () {
14662         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14663
14664         rm -rf $DIR/$tdir
14665         test_mkdir $DIR/$tdir
14666         $LFS setstripe -c 1 -i 0 $DIR/$tdir
14667         createmany -o $DIR/$tdir/$tfile 10
14668         cancel_lru_locks mdc
14669         cancel_lru_locks osc
14670         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
14671         $LCTL set_param fail_loc=0x31a
14672         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
14673         $LCTL set_param fail_loc=0
14674         rm -r $DIR/$tdir
14675 }
14676 run_test 222a "AGL for ls should not trigger CLIO lock failure"
14677
14678 test_222b () {
14679         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14680
14681         rm -rf $DIR/$tdir
14682         test_mkdir $DIR/$tdir
14683         $LFS setstripe -c 1 -i 0 $DIR/$tdir
14684         createmany -o $DIR/$tdir/$tfile 10
14685         cancel_lru_locks mdc
14686         cancel_lru_locks osc
14687         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
14688         $LCTL set_param fail_loc=0x31a
14689         rm -r $DIR/$tdir || error "AGL for rmdir failed"
14690         $LCTL set_param fail_loc=0
14691 }
14692 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
14693
14694 test_223 () {
14695         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14696
14697         rm -rf $DIR/$tdir
14698         test_mkdir $DIR/$tdir
14699         $LFS setstripe -c 1 -i 0 $DIR/$tdir
14700         createmany -o $DIR/$tdir/$tfile 10
14701         cancel_lru_locks mdc
14702         cancel_lru_locks osc
14703         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
14704         $LCTL set_param fail_loc=0x31b
14705         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
14706         $LCTL set_param fail_loc=0
14707         rm -r $DIR/$tdir
14708 }
14709 run_test 223 "osc reenqueue if without AGL lock granted ======================="
14710
14711 test_224a() { # LU-1039, MRP-303
14712         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14713
14714         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
14715         $LCTL set_param fail_loc=0x508
14716         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
14717         $LCTL set_param fail_loc=0
14718         df $DIR
14719 }
14720 run_test 224a "Don't panic on bulk IO failure"
14721
14722 test_224b() { # LU-1039, MRP-303
14723         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14724
14725         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
14726         cancel_lru_locks osc
14727         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
14728         $LCTL set_param fail_loc=0x515
14729         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
14730         $LCTL set_param fail_loc=0
14731         df $DIR
14732 }
14733 run_test 224b "Don't panic on bulk IO failure"
14734
14735 test_224c() { # LU-6441
14736         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14737         remote_mds_nodsh && skip "remote MDS with nodsh"
14738
14739         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
14740         save_writethrough $p
14741         set_cache writethrough on
14742
14743         local pages_per_rpc=$($LCTL get_param \
14744                                 osc.*.max_pages_per_rpc)
14745         local at_max=$($LCTL get_param -n at_max)
14746         local timeout=$($LCTL get_param -n timeout)
14747         local test_at="at_max"
14748         local param_at="$FSNAME.sys.at_max"
14749         local test_timeout="timeout"
14750         local param_timeout="$FSNAME.sys.timeout"
14751
14752         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
14753
14754         set_persistent_param_and_check client "$test_at" "$param_at" 0
14755         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
14756
14757         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
14758         do_facet ost1 $LCTL set_param fail_loc=0x520
14759         $LFS setstripe -c 1 -i 0 $DIR/$tfile
14760         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
14761         sync
14762         do_facet ost1 $LCTL set_param fail_loc=0
14763
14764         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
14765         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
14766                 $timeout
14767
14768         $LCTL set_param -n $pages_per_rpc
14769         restore_lustre_params < $p
14770         rm -f $p
14771 }
14772 run_test 224c "Don't hang if one of md lost during large bulk RPC"
14773
14774 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
14775 test_225a () {
14776         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14777         if [ -z ${MDSSURVEY} ]; then
14778                 skip_env "mds-survey not found"
14779         fi
14780         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
14781                 skip "Need MDS version at least 2.2.51"
14782
14783         local mds=$(facet_host $SINGLEMDS)
14784         local target=$(do_nodes $mds 'lctl dl' |
14785                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
14786
14787         local cmd1="file_count=1000 thrhi=4"
14788         local cmd2="dir_count=2 layer=mdd stripe_count=0"
14789         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
14790         local cmd="$cmd1 $cmd2 $cmd3"
14791
14792         rm -f ${TMP}/mds_survey*
14793         echo + $cmd
14794         eval $cmd || error "mds-survey with zero-stripe failed"
14795         cat ${TMP}/mds_survey*
14796         rm -f ${TMP}/mds_survey*
14797 }
14798 run_test 225a "Metadata survey sanity with zero-stripe"
14799
14800 test_225b () {
14801         if [ -z ${MDSSURVEY} ]; then
14802                 skip_env "mds-survey not found"
14803         fi
14804         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
14805                 skip "Need MDS version at least 2.2.51"
14806         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14807         remote_mds_nodsh && skip "remote MDS with nodsh"
14808         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
14809                 skip_env "Need to mount OST to test"
14810         fi
14811
14812         local mds=$(facet_host $SINGLEMDS)
14813         local target=$(do_nodes $mds 'lctl dl' |
14814                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
14815
14816         local cmd1="file_count=1000 thrhi=4"
14817         local cmd2="dir_count=2 layer=mdd stripe_count=1"
14818         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
14819         local cmd="$cmd1 $cmd2 $cmd3"
14820
14821         rm -f ${TMP}/mds_survey*
14822         echo + $cmd
14823         eval $cmd || error "mds-survey with stripe_count failed"
14824         cat ${TMP}/mds_survey*
14825         rm -f ${TMP}/mds_survey*
14826 }
14827 run_test 225b "Metadata survey sanity with stripe_count = 1"
14828
14829 mcreate_path2fid () {
14830         local mode=$1
14831         local major=$2
14832         local minor=$3
14833         local name=$4
14834         local desc=$5
14835         local path=$DIR/$tdir/$name
14836         local fid
14837         local rc
14838         local fid_path
14839
14840         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
14841                 error "cannot create $desc"
14842
14843         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
14844         rc=$?
14845         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
14846
14847         fid_path=$($LFS fid2path $MOUNT $fid)
14848         rc=$?
14849         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
14850
14851         [ "$path" == "$fid_path" ] ||
14852                 error "fid2path returned $fid_path, expected $path"
14853
14854         echo "pass with $path and $fid"
14855 }
14856
14857 test_226a () {
14858         rm -rf $DIR/$tdir
14859         mkdir -p $DIR/$tdir
14860
14861         mcreate_path2fid 0010666 0 0 fifo "FIFO"
14862         mcreate_path2fid 0020666 1 3 null "character special file (null)"
14863         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
14864         mcreate_path2fid 0040666 0 0 dir "directory"
14865         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
14866         mcreate_path2fid 0100666 0 0 file "regular file"
14867         mcreate_path2fid 0120666 0 0 link "symbolic link"
14868         mcreate_path2fid 0140666 0 0 sock "socket"
14869 }
14870 run_test 226a "call path2fid and fid2path on files of all type"
14871
14872 test_226b () {
14873         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14874
14875         local MDTIDX=1
14876
14877         rm -rf $DIR/$tdir
14878         mkdir -p $DIR/$tdir
14879         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
14880                 error "create remote directory failed"
14881         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
14882         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
14883                                 "character special file (null)"
14884         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
14885                                 "character special file (no device)"
14886         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
14887         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
14888                                 "block special file (loop)"
14889         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
14890         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
14891         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
14892 }
14893 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
14894
14895 # LU-1299 Executing or running ldd on a truncated executable does not
14896 # cause an out-of-memory condition.
14897 test_227() {
14898         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14899         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
14900
14901         dd if=$(which date) of=$MOUNT/date bs=1k count=1
14902         chmod +x $MOUNT/date
14903
14904         $MOUNT/date > /dev/null
14905         ldd $MOUNT/date > /dev/null
14906         rm -f $MOUNT/date
14907 }
14908 run_test 227 "running truncated executable does not cause OOM"
14909
14910 # LU-1512 try to reuse idle OI blocks
14911 test_228a() {
14912         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14913         remote_mds_nodsh && skip "remote MDS with nodsh"
14914         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
14915
14916         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
14917         local myDIR=$DIR/$tdir
14918
14919         mkdir -p $myDIR
14920         #define OBD_FAIL_SEQ_EXHAUST             0x1002
14921         $LCTL set_param fail_loc=0x80001002
14922         createmany -o $myDIR/t- 10000
14923         $LCTL set_param fail_loc=0
14924         # The guard is current the largest FID holder
14925         touch $myDIR/guard
14926         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
14927                     tr -d '[')
14928         local IDX=$(($SEQ % 64))
14929
14930         do_facet $SINGLEMDS sync
14931         # Make sure journal flushed.
14932         sleep 6
14933         local blk1=$(do_facet $SINGLEMDS \
14934                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14935                      grep Blockcount | awk '{print $4}')
14936
14937         # Remove old files, some OI blocks will become idle.
14938         unlinkmany $myDIR/t- 10000
14939         # Create new files, idle OI blocks should be reused.
14940         createmany -o $myDIR/t- 2000
14941         do_facet $SINGLEMDS sync
14942         # Make sure journal flushed.
14943         sleep 6
14944         local blk2=$(do_facet $SINGLEMDS \
14945                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14946                      grep Blockcount | awk '{print $4}')
14947
14948         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
14949 }
14950 run_test 228a "try to reuse idle OI blocks"
14951
14952 test_228b() {
14953         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14954         remote_mds_nodsh && skip "remote MDS with nodsh"
14955         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
14956
14957         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
14958         local myDIR=$DIR/$tdir
14959
14960         mkdir -p $myDIR
14961         #define OBD_FAIL_SEQ_EXHAUST             0x1002
14962         $LCTL set_param fail_loc=0x80001002
14963         createmany -o $myDIR/t- 10000
14964         $LCTL set_param fail_loc=0
14965         # The guard is current the largest FID holder
14966         touch $myDIR/guard
14967         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
14968                     tr -d '[')
14969         local IDX=$(($SEQ % 64))
14970
14971         do_facet $SINGLEMDS sync
14972         # Make sure journal flushed.
14973         sleep 6
14974         local blk1=$(do_facet $SINGLEMDS \
14975                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14976                      grep Blockcount | awk '{print $4}')
14977
14978         # Remove old files, some OI blocks will become idle.
14979         unlinkmany $myDIR/t- 10000
14980
14981         # stop the MDT
14982         stop $SINGLEMDS || error "Fail to stop MDT."
14983         # remount the MDT
14984         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
14985
14986         df $MOUNT || error "Fail to df."
14987         # Create new files, idle OI blocks should be reused.
14988         createmany -o $myDIR/t- 2000
14989         do_facet $SINGLEMDS sync
14990         # Make sure journal flushed.
14991         sleep 6
14992         local blk2=$(do_facet $SINGLEMDS \
14993                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14994                      grep Blockcount | awk '{print $4}')
14995
14996         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
14997 }
14998 run_test 228b "idle OI blocks can be reused after MDT restart"
14999
15000 #LU-1881
15001 test_228c() {
15002         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15003         remote_mds_nodsh && skip "remote MDS with nodsh"
15004         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
15005
15006         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
15007         local myDIR=$DIR/$tdir
15008
15009         mkdir -p $myDIR
15010         #define OBD_FAIL_SEQ_EXHAUST             0x1002
15011         $LCTL set_param fail_loc=0x80001002
15012         # 20000 files can guarantee there are index nodes in the OI file
15013         createmany -o $myDIR/t- 20000
15014         $LCTL set_param fail_loc=0
15015         # The guard is current the largest FID holder
15016         touch $myDIR/guard
15017         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
15018                     tr -d '[')
15019         local IDX=$(($SEQ % 64))
15020
15021         do_facet $SINGLEMDS sync
15022         # Make sure journal flushed.
15023         sleep 6
15024         local blk1=$(do_facet $SINGLEMDS \
15025                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15026                      grep Blockcount | awk '{print $4}')
15027
15028         # Remove old files, some OI blocks will become idle.
15029         unlinkmany $myDIR/t- 20000
15030         rm -f $myDIR/guard
15031         # The OI file should become empty now
15032
15033         # Create new files, idle OI blocks should be reused.
15034         createmany -o $myDIR/t- 2000
15035         do_facet $SINGLEMDS sync
15036         # Make sure journal flushed.
15037         sleep 6
15038         local blk2=$(do_facet $SINGLEMDS \
15039                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15040                      grep Blockcount | awk '{print $4}')
15041
15042         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
15043 }
15044 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
15045
15046 test_229() { # LU-2482, LU-3448
15047         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15048         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
15049         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
15050                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
15051
15052         rm -f $DIR/$tfile
15053
15054         # Create a file with a released layout and stripe count 2.
15055         $MULTIOP $DIR/$tfile H2c ||
15056                 error "failed to create file with released layout"
15057
15058         $GETSTRIPE -v $DIR/$tfile
15059
15060         local pattern=$($GETSTRIPE -L $DIR/$tfile)
15061         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
15062
15063         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
15064         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
15065         stat $DIR/$tfile || error "failed to stat released file"
15066
15067         chown $RUNAS_ID $DIR/$tfile ||
15068                 error "chown $RUNAS_ID $DIR/$tfile failed"
15069
15070         chgrp $RUNAS_ID $DIR/$tfile ||
15071                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
15072
15073         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
15074         rm $DIR/$tfile || error "failed to remove released file"
15075 }
15076 run_test 229 "getstripe/stat/rm/attr changes work on released files"
15077
15078 test_230a() {
15079         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15080         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15081         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15082                 skip "Need MDS version at least 2.11.52"
15083
15084         local MDTIDX=1
15085
15086         test_mkdir $DIR/$tdir
15087         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
15088         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
15089         [ $mdt_idx -ne 0 ] &&
15090                 error "create local directory on wrong MDT $mdt_idx"
15091
15092         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
15093                         error "create remote directory failed"
15094         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
15095         [ $mdt_idx -ne $MDTIDX ] &&
15096                 error "create remote directory on wrong MDT $mdt_idx"
15097
15098         createmany -o $DIR/$tdir/test_230/t- 10 ||
15099                 error "create files on remote directory failed"
15100         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
15101         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
15102         rm -r $DIR/$tdir || error "unlink remote directory failed"
15103 }
15104 run_test 230a "Create remote directory and files under the remote directory"
15105
15106 test_230b() {
15107         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15108         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15109         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15110                 skip "Need MDS version at least 2.11.52"
15111
15112         local MDTIDX=1
15113         local mdt_index
15114         local i
15115         local file
15116         local pid
15117         local stripe_count
15118         local migrate_dir=$DIR/$tdir/migrate_dir
15119         local other_dir=$DIR/$tdir/other_dir
15120
15121         test_mkdir $DIR/$tdir
15122         test_mkdir -i0 -c1 $migrate_dir
15123         test_mkdir -i0 -c1 $other_dir
15124         for ((i=0; i<10; i++)); do
15125                 mkdir -p $migrate_dir/dir_${i}
15126                 createmany -o $migrate_dir/dir_${i}/f 10 ||
15127                         error "create files under remote dir failed $i"
15128         done
15129
15130         cp /etc/passwd $migrate_dir/$tfile
15131         cp /etc/passwd $other_dir/$tfile
15132         chattr +SAD $migrate_dir
15133         chattr +SAD $migrate_dir/$tfile
15134
15135         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
15136         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
15137         local old_dir_mode=$(stat -c%f $migrate_dir)
15138         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
15139
15140         mkdir -p $migrate_dir/dir_default_stripe2
15141         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
15142         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
15143
15144         mkdir -p $other_dir
15145         ln $migrate_dir/$tfile $other_dir/luna
15146         ln $migrate_dir/$tfile $migrate_dir/sofia
15147         ln $other_dir/$tfile $migrate_dir/david
15148         ln -s $migrate_dir/$tfile $other_dir/zachary
15149         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
15150         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
15151
15152         $LFS migrate -m $MDTIDX $migrate_dir ||
15153                 error "fails on migrating remote dir to MDT1"
15154
15155         echo "migratate to MDT1, then checking.."
15156         for ((i = 0; i < 10; i++)); do
15157                 for file in $(find $migrate_dir/dir_${i}); do
15158                         mdt_index=$($LFS getstripe -m $file)
15159                         [ $mdt_index == $MDTIDX ] ||
15160                                 error "$file is not on MDT${MDTIDX}"
15161                 done
15162         done
15163
15164         # the multiple link file should still in MDT0
15165         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
15166         [ $mdt_index == 0 ] ||
15167                 error "$file is not on MDT${MDTIDX}"
15168
15169         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
15170         [ "$old_dir_flag" = "$new_dir_flag" ] ||
15171                 error " expect $old_dir_flag get $new_dir_flag"
15172
15173         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
15174         [ "$old_file_flag" = "$new_file_flag" ] ||
15175                 error " expect $old_file_flag get $new_file_flag"
15176
15177         local new_dir_mode=$(stat -c%f $migrate_dir)
15178         [ "$old_dir_mode" = "$new_dir_mode" ] ||
15179                 error "expect mode $old_dir_mode get $new_dir_mode"
15180
15181         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
15182         [ "$old_file_mode" = "$new_file_mode" ] ||
15183                 error "expect mode $old_file_mode get $new_file_mode"
15184
15185         diff /etc/passwd $migrate_dir/$tfile ||
15186                 error "$tfile different after migration"
15187
15188         diff /etc/passwd $other_dir/luna ||
15189                 error "luna different after migration"
15190
15191         diff /etc/passwd $migrate_dir/sofia ||
15192                 error "sofia different after migration"
15193
15194         diff /etc/passwd $migrate_dir/david ||
15195                 error "david different after migration"
15196
15197         diff /etc/passwd $other_dir/zachary ||
15198                 error "zachary different after migration"
15199
15200         diff /etc/passwd $migrate_dir/${tfile}_ln ||
15201                 error "${tfile}_ln different after migration"
15202
15203         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
15204                 error "${tfile}_ln_other different after migration"
15205
15206         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
15207         [ $stripe_count = 2 ] ||
15208                 error "dir strpe_count $d != 2 after migration."
15209
15210         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
15211         [ $stripe_count = 2 ] ||
15212                 error "file strpe_count $d != 2 after migration."
15213
15214         #migrate back to MDT0
15215         MDTIDX=0
15216
15217         $LFS migrate -m $MDTIDX $migrate_dir ||
15218                 error "fails on migrating remote dir to MDT0"
15219
15220         echo "migrate back to MDT0, checking.."
15221         for file in $(find $migrate_dir); do
15222                 mdt_index=$($LFS getstripe -m $file)
15223                 [ $mdt_index == $MDTIDX ] ||
15224                         error "$file is not on MDT${MDTIDX}"
15225         done
15226
15227         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
15228         [ "$old_dir_flag" = "$new_dir_flag" ] ||
15229                 error " expect $old_dir_flag get $new_dir_flag"
15230
15231         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
15232         [ "$old_file_flag" = "$new_file_flag" ] ||
15233                 error " expect $old_file_flag get $new_file_flag"
15234
15235         local new_dir_mode=$(stat -c%f $migrate_dir)
15236         [ "$old_dir_mode" = "$new_dir_mode" ] ||
15237                 error "expect mode $old_dir_mode get $new_dir_mode"
15238
15239         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
15240         [ "$old_file_mode" = "$new_file_mode" ] ||
15241                 error "expect mode $old_file_mode get $new_file_mode"
15242
15243         diff /etc/passwd ${migrate_dir}/$tfile ||
15244                 error "$tfile different after migration"
15245
15246         diff /etc/passwd ${other_dir}/luna ||
15247                 error "luna different after migration"
15248
15249         diff /etc/passwd ${migrate_dir}/sofia ||
15250                 error "sofia different after migration"
15251
15252         diff /etc/passwd ${other_dir}/zachary ||
15253                 error "zachary different after migration"
15254
15255         diff /etc/passwd $migrate_dir/${tfile}_ln ||
15256                 error "${tfile}_ln different after migration"
15257
15258         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
15259                 error "${tfile}_ln_other different after migration"
15260
15261         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
15262         [ $stripe_count = 2 ] ||
15263                 error "dir strpe_count $d != 2 after migration."
15264
15265         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
15266         [ $stripe_count = 2 ] ||
15267                 error "file strpe_count $d != 2 after migration."
15268
15269         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15270 }
15271 run_test 230b "migrate directory"
15272
15273 test_230c() {
15274         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15275         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15276         remote_mds_nodsh && skip "remote MDS with nodsh"
15277         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15278                 skip "Need MDS version at least 2.11.52"
15279
15280         local MDTIDX=1
15281         local total=3
15282         local mdt_index
15283         local file
15284         local migrate_dir=$DIR/$tdir/migrate_dir
15285
15286         #If migrating directory fails in the middle, all entries of
15287         #the directory is still accessiable.
15288         test_mkdir $DIR/$tdir
15289         test_mkdir -i0 -c1 $migrate_dir
15290         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
15291         stat $migrate_dir
15292         createmany -o $migrate_dir/f $total ||
15293                 error "create files under ${migrate_dir} failed"
15294
15295         # fail after migrating top dir, and this will fail only once, so the
15296         # first sub file migration will fail (currently f3), others succeed.
15297         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
15298         do_facet mds1 lctl set_param fail_loc=0x1801
15299         local t=$(ls $migrate_dir | wc -l)
15300         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
15301                 error "migrate should fail"
15302         local u=$(ls $migrate_dir | wc -l)
15303         [ "$u" == "$t" ] || error "$u != $t during migration"
15304
15305         # add new dir/file should succeed
15306         mkdir $migrate_dir/dir ||
15307                 error "mkdir failed under migrating directory"
15308         touch $migrate_dir/file ||
15309                 error "create file failed under migrating directory"
15310
15311         # add file with existing name should fail
15312         for file in $migrate_dir/f*; do
15313                 stat $file > /dev/null || error "stat $file failed"
15314                 $OPENFILE -f O_CREAT:O_EXCL $file &&
15315                         error "open(O_CREAT|O_EXCL) $file should fail"
15316                 $MULTIOP $file m && error "create $file should fail"
15317                 touch $DIR/$tdir/remote_dir/$tfile ||
15318                         error "touch $tfile failed"
15319                 ln $DIR/$tdir/remote_dir/$tfile $file &&
15320                         error "link $file should fail"
15321                 mdt_index=$($LFS getstripe -m $file)
15322                 if [ $mdt_index == 0 ]; then
15323                         # file failed to migrate is not allowed to rename to
15324                         mv $DIR/$tdir/remote_dir/$tfile $file &&
15325                                 error "rename to $file should fail"
15326                 else
15327                         mv $DIR/$tdir/remote_dir/$tfile $file ||
15328                                 error "rename to $file failed"
15329                 fi
15330                 echo hello >> $file || error "write $file failed"
15331         done
15332
15333         # resume migration with different options should fail
15334         $LFS migrate -m 0 $migrate_dir &&
15335                 error "migrate -m 0 $migrate_dir should fail"
15336
15337         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
15338                 error "migrate -c 2 $migrate_dir should fail"
15339
15340         # resume migration should succeed
15341         $LFS migrate -m $MDTIDX $migrate_dir ||
15342                 error "migrate $migrate_dir failed"
15343
15344         echo "Finish migration, then checking.."
15345         for file in $(find $migrate_dir); do
15346                 mdt_index=$($LFS getstripe -m $file)
15347                 [ $mdt_index == $MDTIDX ] ||
15348                         error "$file is not on MDT${MDTIDX}"
15349         done
15350
15351         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15352 }
15353 run_test 230c "check directory accessiblity if migration failed"
15354
15355 test_230d() {
15356         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15357         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15358         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15359                 skip "Need MDS version at least 2.11.52"
15360         # LU-11235
15361         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
15362
15363         local migrate_dir=$DIR/$tdir/migrate_dir
15364         local old_index
15365         local new_index
15366         local old_count
15367         local new_count
15368         local new_hash
15369         local mdt_index
15370         local i
15371         local j
15372
15373         old_index=$((RANDOM % MDSCOUNT))
15374         old_count=$((MDSCOUNT - old_index))
15375         new_index=$((RANDOM % MDSCOUNT))
15376         new_count=$((MDSCOUNT - new_index))
15377         new_hash="all_char"
15378
15379         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
15380         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
15381
15382         test_mkdir $DIR/$tdir
15383         test_mkdir -i $old_index -c $old_count $migrate_dir
15384
15385         for ((i=0; i<100; i++)); do
15386                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
15387                 createmany -o $migrate_dir/dir_${i}/f 100 ||
15388                         error "create files under remote dir failed $i"
15389         done
15390
15391         echo -n "Migrate from MDT$old_index "
15392         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
15393         echo -n "to MDT$new_index"
15394         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
15395         echo
15396
15397         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
15398         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
15399                 error "migrate remote dir error"
15400
15401         echo "Finish migration, then checking.."
15402         for file in $(find $migrate_dir); do
15403                 mdt_index=$($LFS getstripe -m $file)
15404                 if [ $mdt_index -lt $new_index ] ||
15405                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
15406                         error "$file is on MDT$mdt_index"
15407                 fi
15408         done
15409
15410         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15411 }
15412 run_test 230d "check migrate big directory"
15413
15414 test_230e() {
15415         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15416         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15417         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15418                 skip "Need MDS version at least 2.11.52"
15419
15420         local i
15421         local j
15422         local a_fid
15423         local b_fid
15424
15425         mkdir -p $DIR/$tdir
15426         mkdir $DIR/$tdir/migrate_dir
15427         mkdir $DIR/$tdir/other_dir
15428         touch $DIR/$tdir/migrate_dir/a
15429         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
15430         ls $DIR/$tdir/other_dir
15431
15432         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
15433                 error "migrate dir fails"
15434
15435         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
15436         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
15437
15438         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15439         [ $mdt_index == 0 ] || error "a is not on MDT0"
15440
15441         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
15442                 error "migrate dir fails"
15443
15444         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
15445         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
15446
15447         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15448         [ $mdt_index == 1 ] || error "a is not on MDT1"
15449
15450         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
15451         [ $mdt_index == 1 ] || error "b is not on MDT1"
15452
15453         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
15454         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
15455
15456         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
15457
15458         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15459 }
15460 run_test 230e "migrate mulitple local link files"
15461
15462 test_230f() {
15463         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15464         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15465         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15466                 skip "Need MDS version at least 2.11.52"
15467
15468         local a_fid
15469         local ln_fid
15470
15471         mkdir -p $DIR/$tdir
15472         mkdir $DIR/$tdir/migrate_dir
15473         $LFS mkdir -i1 $DIR/$tdir/other_dir
15474         touch $DIR/$tdir/migrate_dir/a
15475         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
15476         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
15477         ls $DIR/$tdir/other_dir
15478
15479         # a should be migrated to MDT1, since no other links on MDT0
15480         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
15481                 error "#1 migrate dir fails"
15482         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
15483         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
15484         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15485         [ $mdt_index == 1 ] || error "a is not on MDT1"
15486
15487         # a should stay on MDT1, because it is a mulitple link file
15488         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
15489                 error "#2 migrate dir fails"
15490         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15491         [ $mdt_index == 1 ] || error "a is not on MDT1"
15492
15493         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
15494                 error "#3 migrate dir fails"
15495
15496         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
15497         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
15498         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
15499
15500         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
15501         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
15502
15503         # a should be migrated to MDT0, since no other links on MDT1
15504         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
15505                 error "#4 migrate dir fails"
15506         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15507         [ $mdt_index == 0 ] || error "a is not on MDT0"
15508
15509         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15510 }
15511 run_test 230f "migrate mulitple remote link files"
15512
15513 test_230g() {
15514         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15515         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15516         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15517                 skip "Need MDS version at least 2.11.52"
15518
15519         mkdir -p $DIR/$tdir/migrate_dir
15520
15521         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
15522                 error "migrating dir to non-exist MDT succeeds"
15523         true
15524 }
15525 run_test 230g "migrate dir to non-exist MDT"
15526
15527 test_230h() {
15528         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15529         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15530         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15531                 skip "Need MDS version at least 2.11.52"
15532
15533         local mdt_index
15534
15535         mkdir -p $DIR/$tdir/migrate_dir
15536
15537         $LFS migrate -m1 $DIR &&
15538                 error "migrating mountpoint1 should fail"
15539
15540         $LFS migrate -m1 $DIR/$tdir/.. &&
15541                 error "migrating mountpoint2 should fail"
15542
15543         # same as mv
15544         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
15545                 error "migrating $tdir/migrate_dir/.. should fail"
15546
15547         true
15548 }
15549 run_test 230h "migrate .. and root"
15550
15551 test_230i() {
15552         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15553         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15554         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15555                 skip "Need MDS version at least 2.11.52"
15556
15557         mkdir -p $DIR/$tdir/migrate_dir
15558
15559         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
15560                 error "migration fails with a tailing slash"
15561
15562         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
15563                 error "migration fails with two tailing slashes"
15564 }
15565 run_test 230i "lfs migrate -m tolerates trailing slashes"
15566
15567 test_230j() {
15568         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
15569         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15570                 skip "Need MDS version at least 2.11.52"
15571
15572         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
15573         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
15574                 error "create $tfile failed"
15575         cat /etc/passwd > $DIR/$tdir/$tfile
15576
15577         $LFS migrate -m 1 $DIR/$tdir
15578
15579         cmp /etc/passwd $DIR/$tdir/$tfile ||
15580                 error "DoM file mismatch after migration"
15581 }
15582 run_test 230j "DoM file data not changed after dir migration"
15583
15584 test_230k() {
15585         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
15586         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
15587                 skip "Need MDS version at least 2.11.56"
15588
15589         local total=20
15590         local files_on_starting_mdt=0
15591
15592         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
15593         $LFS getdirstripe $DIR/$tdir
15594         for i in $(seq $total); do
15595                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
15596                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
15597                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
15598         done
15599
15600         echo "$files_on_starting_mdt files on MDT0"
15601
15602         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
15603         $LFS getdirstripe $DIR/$tdir
15604
15605         files_on_starting_mdt=0
15606         for i in $(seq $total); do
15607                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
15608                         error "file $tfile.$i mismatch after migration"
15609                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
15610                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
15611         done
15612
15613         echo "$files_on_starting_mdt files on MDT1 after migration"
15614         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
15615
15616         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
15617         $LFS getdirstripe $DIR/$tdir
15618
15619         files_on_starting_mdt=0
15620         for i in $(seq $total); do
15621                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
15622                         error "file $tfile.$i mismatch after 2nd migration"
15623                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
15624                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
15625         done
15626
15627         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
15628         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
15629
15630         true
15631 }
15632 run_test 230k "file data not changed after dir migration"
15633
15634 test_230l() {
15635         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
15636         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
15637                 skip "Need MDS version at least 2.11.56"
15638
15639         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
15640         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
15641                 error "create files under remote dir failed $i"
15642         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
15643 }
15644 run_test 230l "readdir between MDTs won't crash"
15645
15646 test_231a()
15647 {
15648         # For simplicity this test assumes that max_pages_per_rpc
15649         # is the same across all OSCs
15650         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
15651         local bulk_size=$((max_pages * PAGE_SIZE))
15652         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
15653                                        head -n 1)
15654
15655         mkdir -p $DIR/$tdir
15656         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
15657                 error "failed to set stripe with -S ${brw_size}M option"
15658
15659         # clear the OSC stats
15660         $LCTL set_param osc.*.stats=0 &>/dev/null
15661         stop_writeback
15662
15663         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
15664         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
15665                 oflag=direct &>/dev/null || error "dd failed"
15666
15667         sync; sleep 1; sync # just to be safe
15668         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
15669         if [ x$nrpcs != "x1" ]; then
15670                 $LCTL get_param osc.*.stats
15671                 error "found $nrpcs ost_write RPCs, not 1 as expected"
15672         fi
15673
15674         start_writeback
15675         # Drop the OSC cache, otherwise we will read from it
15676         cancel_lru_locks osc
15677
15678         # clear the OSC stats
15679         $LCTL set_param osc.*.stats=0 &>/dev/null
15680
15681         # Client reads $bulk_size.
15682         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
15683                 iflag=direct &>/dev/null || error "dd failed"
15684
15685         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
15686         if [ x$nrpcs != "x1" ]; then
15687                 $LCTL get_param osc.*.stats
15688                 error "found $nrpcs ost_read RPCs, not 1 as expected"
15689         fi
15690 }
15691 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
15692
15693 test_231b() {
15694         mkdir -p $DIR/$tdir
15695         local i
15696         for i in {0..1023}; do
15697                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
15698                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
15699                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
15700         done
15701         sync
15702 }
15703 run_test 231b "must not assert on fully utilized OST request buffer"
15704
15705 test_232a() {
15706         mkdir -p $DIR/$tdir
15707         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
15708
15709         #define OBD_FAIL_LDLM_OST_LVB            0x31c
15710         do_facet ost1 $LCTL set_param fail_loc=0x31c
15711
15712         # ignore dd failure
15713         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
15714
15715         do_facet ost1 $LCTL set_param fail_loc=0
15716         umount_client $MOUNT || error "umount failed"
15717         mount_client $MOUNT || error "mount failed"
15718         stop ost1 || error "cannot stop ost1"
15719         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
15720 }
15721 run_test 232a "failed lock should not block umount"
15722
15723 test_232b() {
15724         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
15725                 skip "Need MDS version at least 2.10.58"
15726
15727         mkdir -p $DIR/$tdir
15728         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
15729         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
15730         sync
15731         cancel_lru_locks osc
15732
15733         #define OBD_FAIL_LDLM_OST_LVB            0x31c
15734         do_facet ost1 $LCTL set_param fail_loc=0x31c
15735
15736         # ignore failure
15737         $LFS data_version $DIR/$tdir/$tfile || true
15738
15739         do_facet ost1 $LCTL set_param fail_loc=0
15740         umount_client $MOUNT || error "umount failed"
15741         mount_client $MOUNT || error "mount failed"
15742         stop ost1 || error "cannot stop ost1"
15743         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
15744 }
15745 run_test 232b "failed data version lock should not block umount"
15746
15747 test_233a() {
15748         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
15749                 skip "Need MDS version at least 2.3.64"
15750         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
15751
15752         local fid=$($LFS path2fid $MOUNT)
15753
15754         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
15755                 error "cannot access $MOUNT using its FID '$fid'"
15756 }
15757 run_test 233a "checking that OBF of the FS root succeeds"
15758
15759 test_233b() {
15760         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
15761                 skip "Need MDS version at least 2.5.90"
15762         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
15763
15764         local fid=$($LFS path2fid $MOUNT/.lustre)
15765
15766         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
15767                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
15768
15769         fid=$($LFS path2fid $MOUNT/.lustre/fid)
15770         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
15771                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
15772 }
15773 run_test 233b "checking that OBF of the FS .lustre succeeds"
15774
15775 test_234() {
15776         local p="$TMP/sanityN-$TESTNAME.parameters"
15777         save_lustre_params client "llite.*.xattr_cache" > $p
15778         lctl set_param llite.*.xattr_cache 1 ||
15779                 skip_env "xattr cache is not supported"
15780
15781         mkdir -p $DIR/$tdir || error "mkdir failed"
15782         touch $DIR/$tdir/$tfile || error "touch failed"
15783         # OBD_FAIL_LLITE_XATTR_ENOMEM
15784         $LCTL set_param fail_loc=0x1405
15785         getfattr -n user.attr $DIR/$tdir/$tfile &&
15786                 error "getfattr should have failed with ENOMEM"
15787         $LCTL set_param fail_loc=0x0
15788         rm -rf $DIR/$tdir
15789
15790         restore_lustre_params < $p
15791         rm -f $p
15792 }
15793 run_test 234 "xattr cache should not crash on ENOMEM"
15794
15795 test_235() {
15796         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
15797                 skip "Need MDS version at least 2.4.52"
15798
15799         flock_deadlock $DIR/$tfile
15800         local RC=$?
15801         case $RC in
15802                 0)
15803                 ;;
15804                 124) error "process hangs on a deadlock"
15805                 ;;
15806                 *) error "error executing flock_deadlock $DIR/$tfile"
15807                 ;;
15808         esac
15809 }
15810 run_test 235 "LU-1715: flock deadlock detection does not work properly"
15811
15812 #LU-2935
15813 test_236() {
15814         check_swap_layouts_support
15815
15816         local ref1=/etc/passwd
15817         local ref2=/etc/group
15818         local file1=$DIR/$tdir/f1
15819         local file2=$DIR/$tdir/f2
15820
15821         test_mkdir -c1 $DIR/$tdir
15822         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
15823         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
15824         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
15825         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
15826         local fd=$(free_fd)
15827         local cmd="exec $fd<>$file2"
15828         eval $cmd
15829         rm $file2
15830         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
15831                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
15832         cmd="exec $fd>&-"
15833         eval $cmd
15834         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
15835
15836         #cleanup
15837         rm -rf $DIR/$tdir
15838 }
15839 run_test 236 "Layout swap on open unlinked file"
15840
15841 # LU-4659 linkea consistency
15842 test_238() {
15843         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
15844                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
15845                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
15846                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
15847
15848         touch $DIR/$tfile
15849         ln $DIR/$tfile $DIR/$tfile.lnk
15850         touch $DIR/$tfile.new
15851         mv $DIR/$tfile.new $DIR/$tfile
15852         local fid1=$($LFS path2fid $DIR/$tfile)
15853         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
15854         local path1=$($LFS fid2path $FSNAME "$fid1")
15855         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
15856         local path2=$($LFS fid2path $FSNAME "$fid2")
15857         [ $tfile.lnk == $path2 ] ||
15858                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
15859         rm -f $DIR/$tfile*
15860 }
15861 run_test 238 "Verify linkea consistency"
15862
15863 test_239A() { # was test_239
15864         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
15865                 skip "Need MDS version at least 2.5.60"
15866
15867         local list=$(comma_list $(mdts_nodes))
15868
15869         mkdir -p $DIR/$tdir
15870         createmany -o $DIR/$tdir/f- 5000
15871         unlinkmany $DIR/$tdir/f- 5000
15872         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
15873                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
15874         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
15875                         osp.*MDT*.sync_in_flight" | calc_sum)
15876         [ "$changes" -eq 0 ] || error "$changes not synced"
15877 }
15878 run_test 239A "osp_sync test"
15879
15880 test_239a() { #LU-5297
15881         remote_mds_nodsh && skip "remote MDS with nodsh"
15882
15883         touch $DIR/$tfile
15884         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
15885         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
15886         chgrp $RUNAS_GID $DIR/$tfile
15887         wait_delete_completed
15888 }
15889 run_test 239a "process invalid osp sync record correctly"
15890
15891 test_239b() { #LU-5297
15892         remote_mds_nodsh && skip "remote MDS with nodsh"
15893
15894         touch $DIR/$tfile1
15895         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
15896         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
15897         chgrp $RUNAS_GID $DIR/$tfile1
15898         wait_delete_completed
15899         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
15900         touch $DIR/$tfile2
15901         chgrp $RUNAS_GID $DIR/$tfile2
15902         wait_delete_completed
15903 }
15904 run_test 239b "process osp sync record with ENOMEM error correctly"
15905
15906 test_240() {
15907         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15908         remote_mds_nodsh && skip "remote MDS with nodsh"
15909
15910         mkdir -p $DIR/$tdir
15911
15912         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
15913                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
15914         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
15915                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
15916
15917         umount_client $MOUNT || error "umount failed"
15918         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
15919         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
15920         mount_client $MOUNT || error "failed to mount client"
15921
15922         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
15923         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
15924 }
15925 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
15926
15927 test_241_bio() {
15928         local count=$1
15929         local bsize=$2
15930
15931         for LOOP in $(seq $count); do
15932                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
15933                 cancel_lru_locks $OSC || true
15934         done
15935 }
15936
15937 test_241_dio() {
15938         local count=$1
15939         local bsize=$2
15940
15941         for LOOP in $(seq $1); do
15942                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
15943                         2>/dev/null
15944         done
15945 }
15946
15947 test_241a() { # was test_241
15948         local bsize=$PAGE_SIZE
15949
15950         (( bsize < 40960 )) && bsize=40960
15951         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
15952         ls -la $DIR/$tfile
15953         cancel_lru_locks $OSC
15954         test_241_bio 1000 $bsize &
15955         PID=$!
15956         test_241_dio 1000 $bsize
15957         wait $PID
15958 }
15959 run_test 241a "bio vs dio"
15960
15961 test_241b() {
15962         local bsize=$PAGE_SIZE
15963
15964         (( bsize < 40960 )) && bsize=40960
15965         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
15966         ls -la $DIR/$tfile
15967         test_241_dio 1000 $bsize &
15968         PID=$!
15969         test_241_dio 1000 $bsize
15970         wait $PID
15971 }
15972 run_test 241b "dio vs dio"
15973
15974 test_242() {
15975         remote_mds_nodsh && skip "remote MDS with nodsh"
15976
15977         mkdir -p $DIR/$tdir
15978         touch $DIR/$tdir/$tfile
15979
15980         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
15981         do_facet mds1 lctl set_param fail_loc=0x105
15982         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
15983
15984         do_facet mds1 lctl set_param fail_loc=0
15985         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
15986 }
15987 run_test 242 "mdt_readpage failure should not cause directory unreadable"
15988
15989 test_243()
15990 {
15991         test_mkdir $DIR/$tdir
15992         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
15993 }
15994 run_test 243 "various group lock tests"
15995
15996 test_244()
15997 {
15998         test_mkdir $DIR/$tdir
15999         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
16000         sendfile_grouplock $DIR/$tdir/$tfile || \
16001                 error "sendfile+grouplock failed"
16002         rm -rf $DIR/$tdir
16003 }
16004 run_test 244 "sendfile with group lock tests"
16005
16006 test_245() {
16007         local flagname="multi_mod_rpcs"
16008         local connect_data_name="max_mod_rpcs"
16009         local out
16010
16011         # check if multiple modify RPCs flag is set
16012         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
16013                 grep "connect_flags:")
16014         echo "$out"
16015
16016         echo "$out" | grep -qw $flagname
16017         if [ $? -ne 0 ]; then
16018                 echo "connect flag $flagname is not set"
16019                 return
16020         fi
16021
16022         # check if multiple modify RPCs data is set
16023         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
16024         echo "$out"
16025
16026         echo "$out" | grep -qw $connect_data_name ||
16027                 error "import should have connect data $connect_data_name"
16028 }
16029 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
16030
16031 test_246() { # LU-7371
16032         remote_ost_nodsh && skip "remote OST with nodsh"
16033         [ $OST1_VERSION -lt $(version_code 2.7.62) ] &&
16034                 skip "Need OST version >= 2.7.62"
16035
16036         do_facet ost1 $LCTL set_param fail_val=4095
16037 #define OBD_FAIL_OST_READ_SIZE          0x234
16038         do_facet ost1 $LCTL set_param fail_loc=0x234
16039         $LFS setstripe $DIR/$tfile -i 0 -c 1
16040         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
16041         cancel_lru_locks $FSNAME-OST0000
16042         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
16043 }
16044 run_test 246 "Read file of size 4095 should return right length"
16045
16046 cleanup_247() {
16047         local submount=$1
16048
16049         trap 0
16050         umount_client $submount
16051         rmdir $submount
16052 }
16053
16054 test_247a() {
16055         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
16056                 grep -q subtree ||
16057                 skip_env "Fileset feature is not supported"
16058
16059         local submount=${MOUNT}_$tdir
16060
16061         mkdir $MOUNT/$tdir
16062         mkdir -p $submount || error "mkdir $submount failed"
16063         FILESET="$FILESET/$tdir" mount_client $submount ||
16064                 error "mount $submount failed"
16065         trap "cleanup_247 $submount" EXIT
16066         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
16067         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
16068                 error "read $MOUNT/$tdir/$tfile failed"
16069         cleanup_247 $submount
16070 }
16071 run_test 247a "mount subdir as fileset"
16072
16073 test_247b() {
16074         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
16075                 skip_env "Fileset feature is not supported"
16076
16077         local submount=${MOUNT}_$tdir
16078
16079         rm -rf $MOUNT/$tdir
16080         mkdir -p $submount || error "mkdir $submount failed"
16081         SKIP_FILESET=1
16082         FILESET="$FILESET/$tdir" mount_client $submount &&
16083                 error "mount $submount should fail"
16084         rmdir $submount
16085 }
16086 run_test 247b "mount subdir that dose not exist"
16087
16088 test_247c() {
16089         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
16090                 skip_env "Fileset feature is not supported"
16091
16092         local submount=${MOUNT}_$tdir
16093
16094         mkdir -p $MOUNT/$tdir/dir1
16095         mkdir -p $submount || error "mkdir $submount failed"
16096         trap "cleanup_247 $submount" EXIT
16097         FILESET="$FILESET/$tdir" mount_client $submount ||
16098                 error "mount $submount failed"
16099         local fid=$($LFS path2fid $MOUNT/)
16100         $LFS fid2path $submount $fid && error "fid2path should fail"
16101         cleanup_247 $submount
16102 }
16103 run_test 247c "running fid2path outside root"
16104
16105 test_247d() {
16106         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
16107                 skip "Fileset feature is not supported"
16108
16109         local submount=${MOUNT}_$tdir
16110
16111         mkdir -p $MOUNT/$tdir/dir1
16112         mkdir -p $submount || error "mkdir $submount failed"
16113         FILESET="$FILESET/$tdir" mount_client $submount ||
16114                 error "mount $submount failed"
16115         trap "cleanup_247 $submount" EXIT
16116         local fid=$($LFS path2fid $submount/dir1)
16117         $LFS fid2path $submount $fid || error "fid2path should succeed"
16118         cleanup_247 $submount
16119 }
16120 run_test 247d "running fid2path inside root"
16121
16122 # LU-8037
16123 test_247e() {
16124         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
16125                 grep -q subtree ||
16126                 skip "Fileset feature is not supported"
16127
16128         local submount=${MOUNT}_$tdir
16129
16130         mkdir $MOUNT/$tdir
16131         mkdir -p $submount || error "mkdir $submount failed"
16132         FILESET="$FILESET/.." mount_client $submount &&
16133                 error "mount $submount should fail"
16134         rmdir $submount
16135 }
16136 run_test 247e "mount .. as fileset"
16137
16138 test_248() {
16139         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
16140         [ -z "$fast_read_sav" ] && skip "no fast read support"
16141
16142         # create a large file for fast read verification
16143         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
16144
16145         # make sure the file is created correctly
16146         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
16147                 { rm -f $DIR/$tfile; skip "file creation error"; }
16148
16149         echo "Test 1: verify that fast read is 4 times faster on cache read"
16150
16151         # small read with fast read enabled
16152         $LCTL set_param -n llite.*.fast_read=1
16153         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
16154                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16155                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16156         # small read with fast read disabled
16157         $LCTL set_param -n llite.*.fast_read=0
16158         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
16159                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16160                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16161
16162         # verify that fast read is 4 times faster for cache read
16163         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
16164                 error_not_in_vm "fast read was not 4 times faster: " \
16165                            "$t_fast vs $t_slow"
16166
16167         echo "Test 2: verify the performance between big and small read"
16168         $LCTL set_param -n llite.*.fast_read=1
16169
16170         # 1k non-cache read
16171         cancel_lru_locks osc
16172         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
16173                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16174                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16175
16176         # 1M non-cache read
16177         cancel_lru_locks osc
16178         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
16179                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16180                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16181
16182         # verify that big IO is not 4 times faster than small IO
16183         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
16184                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
16185
16186         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
16187         rm -f $DIR/$tfile
16188 }
16189 run_test 248 "fast read verification"
16190
16191 test_249() { # LU-7890
16192         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
16193                 skip "Need at least version 2.8.54"
16194
16195         rm -f $DIR/$tfile
16196         $SETSTRIPE -c 1 $DIR/$tfile
16197         # Offset 2T == 4k * 512M
16198         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
16199                 error "dd to 2T offset failed"
16200 }
16201 run_test 249 "Write above 2T file size"
16202
16203 test_250() {
16204         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
16205          && skip "no 16TB file size limit on ZFS"
16206
16207         $SETSTRIPE -c 1 $DIR/$tfile
16208         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
16209         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
16210         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
16211         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
16212                 conv=notrunc,fsync && error "append succeeded"
16213         return 0
16214 }
16215 run_test 250 "Write above 16T limit"
16216
16217 test_251() {
16218         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
16219
16220         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
16221         #Skip once - writing the first stripe will succeed
16222         $LCTL set_param fail_loc=0xa0001407 fail_val=1
16223         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
16224                 error "short write happened"
16225
16226         $LCTL set_param fail_loc=0xa0001407 fail_val=1
16227         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
16228                 error "short read happened"
16229
16230         rm -f $DIR/$tfile
16231 }
16232 run_test 251 "Handling short read and write correctly"
16233
16234 test_252() {
16235         remote_mds_nodsh && skip "remote MDS with nodsh"
16236         remote_ost_nodsh && skip "remote OST with nodsh"
16237         if [ "$ost1_FSTYPE" != "ldiskfs" -o "$mds1_FSTYPE" != "ldiskfs" ]; then
16238                 skip_env "ldiskfs only test"
16239         fi
16240
16241         local tgt
16242         local dev
16243         local out
16244         local uuid
16245         local num
16246         local gen
16247
16248         # check lr_reader on OST0000
16249         tgt=ost1
16250         dev=$(facet_device $tgt)
16251         out=$(do_facet $tgt $LR_READER $dev)
16252         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
16253         echo "$out"
16254         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
16255         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
16256                 error "Invalid uuid returned by $LR_READER on target $tgt"
16257         echo -e "uuid returned by $LR_READER is '$uuid'\n"
16258
16259         # check lr_reader -c on MDT0000
16260         tgt=mds1
16261         dev=$(facet_device $tgt)
16262         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
16263                 skip "$LR_READER does not support additional options"
16264         fi
16265         out=$(do_facet $tgt $LR_READER -c $dev)
16266         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
16267         echo "$out"
16268         num=$(echo "$out" | grep -c "mdtlov")
16269         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
16270                 error "Invalid number of mdtlov clients returned by $LR_READER"
16271         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
16272
16273         # check lr_reader -cr on MDT0000
16274         out=$(do_facet $tgt $LR_READER -cr $dev)
16275         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
16276         echo "$out"
16277         echo "$out" | grep -q "^reply_data:$" ||
16278                 error "$LR_READER should have returned 'reply_data' section"
16279         num=$(echo "$out" | grep -c "client_generation")
16280         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
16281 }
16282 run_test 252 "check lr_reader tool"
16283
16284 test_253_fill_ost() {
16285         local size_mb #how many MB should we write to pass watermark
16286         local lwm=$3  #low watermark
16287         local free_10mb #10% of free space
16288
16289         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
16290         size_mb=$((free_kb / 1024 - lwm))
16291         free_10mb=$((free_kb / 10240))
16292         #If 10% of free space cross low watermark use it
16293         if (( free_10mb > size_mb )); then
16294                 size_mb=$free_10mb
16295         else
16296                 #At least we need to store 1.1 of difference between
16297                 #free space and low watermark
16298                 size_mb=$((size_mb + size_mb / 10))
16299         fi
16300         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
16301                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
16302                          oflag=append conv=notrunc
16303         fi
16304
16305         sleep_maxage
16306
16307         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
16308         echo "OST still has $((free_kb / 1024)) mbytes free"
16309 }
16310
16311 test_253() {
16312         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16313         remote_mds_nodsh && skip "remote MDS with nodsh"
16314         remote_mgs_nodsh && skip "remote MGS with nodsh"
16315
16316         local ostidx=0
16317         local rc=0
16318
16319         local ost_name=$($LFS osts |
16320                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
16321         # on the mdt's osc
16322         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
16323         do_facet $SINGLEMDS $LCTL get_param -n \
16324                 osp.$mdtosc_proc1.reserved_mb_high ||
16325                 skip  "remote MDS does not support reserved_mb_high"
16326
16327         rm -rf $DIR/$tdir
16328         wait_mds_ost_sync
16329         wait_delete_completed
16330         mkdir $DIR/$tdir
16331
16332         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
16333                         osp.$mdtosc_proc1.reserved_mb_high)
16334         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
16335                         osp.$mdtosc_proc1.reserved_mb_low)
16336         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
16337
16338         if ! combined_mgs_mds ; then
16339                 mount_mgs_client
16340         fi
16341         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
16342         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
16343                 error "Adding $ost_name to pool failed"
16344
16345         # Wait for client to see a OST at pool
16346         wait_update $HOSTNAME "$LCTL get_param -n
16347                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
16348                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
16349                 error "Client can not see the pool"
16350         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
16351                 error "Setstripe failed"
16352
16353         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
16354         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
16355         echo "OST still has $((blocks/1024)) mbytes free"
16356
16357         local new_lwm=$((blocks/1024-10))
16358         do_facet $SINGLEMDS $LCTL set_param \
16359                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
16360         do_facet $SINGLEMDS $LCTL set_param \
16361                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
16362
16363         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
16364
16365         #First enospc could execute orphan deletion so repeat.
16366         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
16367
16368         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
16369                         osp.$mdtosc_proc1.prealloc_status)
16370         echo "prealloc_status $oa_status"
16371
16372         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
16373                 error "File creation should fail"
16374         #object allocation was stopped, but we still able to append files
16375         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
16376                 error "Append failed"
16377         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
16378
16379         wait_delete_completed
16380
16381         sleep_maxage
16382
16383         for i in $(seq 10 12); do
16384                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
16385                         error "File creation failed after rm";
16386         done
16387
16388         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
16389                         osp.$mdtosc_proc1.prealloc_status)
16390         echo "prealloc_status $oa_status"
16391
16392         if (( oa_status != 0 )); then
16393                 error "Object allocation still disable after rm"
16394         fi
16395         do_facet $SINGLEMDS $LCTL set_param \
16396                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
16397         do_facet $SINGLEMDS $LCTL set_param \
16398                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
16399
16400
16401         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
16402                 error "Remove $ost_name from pool failed"
16403         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
16404                 error "Pool destroy fialed"
16405
16406         if ! combined_mgs_mds ; then
16407                 umount_mgs_client
16408         fi
16409 }
16410 run_test 253 "Check object allocation limit"
16411
16412 test_254() {
16413         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16414         remote_mds_nodsh && skip "remote MDS with nodsh"
16415         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
16416                 skip "MDS does not support changelog_size"
16417
16418         local cl_user
16419         local MDT0=$(facet_svc $SINGLEMDS)
16420
16421         changelog_register || error "changelog_register failed"
16422
16423         changelog_clear 0 || error "changelog_clear failed"
16424
16425         local size1=$(do_facet $SINGLEMDS \
16426                       $LCTL get_param -n mdd.$MDT0.changelog_size)
16427         echo "Changelog size $size1"
16428
16429         rm -rf $DIR/$tdir
16430         $LFS mkdir -i 0 $DIR/$tdir
16431         # change something
16432         mkdir -p $DIR/$tdir/pics/2008/zachy
16433         touch $DIR/$tdir/pics/2008/zachy/timestamp
16434         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
16435         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
16436         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
16437         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
16438         rm $DIR/$tdir/pics/desktop.jpg
16439
16440         local size2=$(do_facet $SINGLEMDS \
16441                       $LCTL get_param -n mdd.$MDT0.changelog_size)
16442         echo "Changelog size after work $size2"
16443
16444         (( $size2 > $size1 )) ||
16445                 error "new Changelog size=$size2 less than old size=$size1"
16446 }
16447 run_test 254 "Check changelog size"
16448
16449 ladvise_no_type()
16450 {
16451         local type=$1
16452         local file=$2
16453
16454         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
16455                 awk -F: '{print $2}' | grep $type > /dev/null
16456         if [ $? -ne 0 ]; then
16457                 return 0
16458         fi
16459         return 1
16460 }
16461
16462 ladvise_no_ioctl()
16463 {
16464         local file=$1
16465
16466         lfs ladvise -a willread $file > /dev/null 2>&1
16467         if [ $? -eq 0 ]; then
16468                 return 1
16469         fi
16470
16471         lfs ladvise -a willread $file 2>&1 |
16472                 grep "Inappropriate ioctl for device" > /dev/null
16473         if [ $? -eq 0 ]; then
16474                 return 0
16475         fi
16476         return 1
16477 }
16478
16479 percent() {
16480         bc <<<"scale=2; ($1 - $2) * 100 / $2"
16481 }
16482
16483 # run a random read IO workload
16484 # usage: random_read_iops <filename> <filesize> <iosize>
16485 random_read_iops() {
16486         local file=$1
16487         local fsize=$2
16488         local iosize=${3:-4096}
16489
16490         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
16491                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
16492 }
16493
16494 drop_file_oss_cache() {
16495         local file="$1"
16496         local nodes="$2"
16497
16498         $LFS ladvise -a dontneed $file 2>/dev/null ||
16499                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
16500 }
16501
16502 ladvise_willread_performance()
16503 {
16504         local repeat=10
16505         local average_origin=0
16506         local average_cache=0
16507         local average_ladvise=0
16508
16509         for ((i = 1; i <= $repeat; i++)); do
16510                 echo "Iter $i/$repeat: reading without willread hint"
16511                 cancel_lru_locks osc
16512                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
16513                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
16514                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
16515                 average_origin=$(bc <<<"$average_origin + $speed_origin")
16516
16517                 cancel_lru_locks osc
16518                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
16519                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
16520                 average_cache=$(bc <<<"$average_cache + $speed_cache")
16521
16522                 cancel_lru_locks osc
16523                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
16524                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
16525                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
16526                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
16527                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
16528         done
16529         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
16530         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
16531         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
16532
16533         speedup_cache=$(percent $average_cache $average_origin)
16534         speedup_ladvise=$(percent $average_ladvise $average_origin)
16535
16536         echo "Average uncached read: $average_origin"
16537         echo "Average speedup with OSS cached read: " \
16538                 "$average_cache = +$speedup_cache%"
16539         echo "Average speedup with ladvise willread: " \
16540                 "$average_ladvise = +$speedup_ladvise%"
16541
16542         local lowest_speedup=20
16543         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
16544                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
16545                         "got $average_cache%. Skipping ladvise willread check."
16546                 return 0
16547         fi
16548
16549         # the test won't work on ZFS until it supports 'ladvise dontneed', but
16550         # it is still good to run until then to exercise 'ladvise willread'
16551         ! $LFS ladvise -a dontneed $DIR/$tfile &&
16552                 [ "$ost1_FSTYPE" = "zfs" ] &&
16553                 echo "osd-zfs does not support dontneed or drop_caches" &&
16554                 return 0
16555
16556         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
16557         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
16558                 error_not_in_vm "Speedup with willread is less than " \
16559                         "$lowest_speedup%, got $average_ladvise%"
16560 }
16561
16562 test_255a() {
16563         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
16564                 skip "lustre < 2.8.54 does not support ladvise "
16565         remote_ost_nodsh && skip "remote OST with nodsh"
16566
16567         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
16568
16569         ladvise_no_type willread $DIR/$tfile &&
16570                 skip "willread ladvise is not supported"
16571
16572         ladvise_no_ioctl $DIR/$tfile &&
16573                 skip "ladvise ioctl is not supported"
16574
16575         local size_mb=100
16576         local size=$((size_mb * 1048576))
16577         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
16578                 error "dd to $DIR/$tfile failed"
16579
16580         lfs ladvise -a willread $DIR/$tfile ||
16581                 error "Ladvise failed with no range argument"
16582
16583         lfs ladvise -a willread -s 0 $DIR/$tfile ||
16584                 error "Ladvise failed with no -l or -e argument"
16585
16586         lfs ladvise -a willread -e 1 $DIR/$tfile ||
16587                 error "Ladvise failed with only -e argument"
16588
16589         lfs ladvise -a willread -l 1 $DIR/$tfile ||
16590                 error "Ladvise failed with only -l argument"
16591
16592         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
16593                 error "End offset should not be smaller than start offset"
16594
16595         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
16596                 error "End offset should not be equal to start offset"
16597
16598         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
16599                 error "Ladvise failed with overflowing -s argument"
16600
16601         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
16602                 error "Ladvise failed with overflowing -e argument"
16603
16604         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
16605                 error "Ladvise failed with overflowing -l argument"
16606
16607         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
16608                 error "Ladvise succeeded with conflicting -l and -e arguments"
16609
16610         echo "Synchronous ladvise should wait"
16611         local delay=4
16612 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
16613         do_nodes $(comma_list $(osts_nodes)) \
16614                 $LCTL set_param fail_val=$delay fail_loc=0x237
16615
16616         local start_ts=$SECONDS
16617         lfs ladvise -a willread $DIR/$tfile ||
16618                 error "Ladvise failed with no range argument"
16619         local end_ts=$SECONDS
16620         local inteval_ts=$((end_ts - start_ts))
16621
16622         if [ $inteval_ts -lt $(($delay - 1)) ]; then
16623                 error "Synchronous advice didn't wait reply"
16624         fi
16625
16626         echo "Asynchronous ladvise shouldn't wait"
16627         local start_ts=$SECONDS
16628         lfs ladvise -a willread -b $DIR/$tfile ||
16629                 error "Ladvise failed with no range argument"
16630         local end_ts=$SECONDS
16631         local inteval_ts=$((end_ts - start_ts))
16632
16633         if [ $inteval_ts -gt $(($delay / 2)) ]; then
16634                 error "Asynchronous advice blocked"
16635         fi
16636
16637         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
16638         ladvise_willread_performance
16639 }
16640 run_test 255a "check 'lfs ladvise -a willread'"
16641
16642 facet_meminfo() {
16643         local facet=$1
16644         local info=$2
16645
16646         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
16647 }
16648
16649 test_255b() {
16650         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
16651                 skip "lustre < 2.8.54 does not support ladvise "
16652         remote_ost_nodsh && skip "remote OST with nodsh"
16653
16654         lfs setstripe -c 1 -i 0 $DIR/$tfile
16655
16656         ladvise_no_type dontneed $DIR/$tfile &&
16657                 skip "dontneed ladvise is not supported"
16658
16659         ladvise_no_ioctl $DIR/$tfile &&
16660                 skip "ladvise ioctl is not supported"
16661
16662         ! $LFS ladvise -a dontneed $DIR/$tfile &&
16663                 [ "$ost1_FSTYPE" = "zfs" ] &&
16664                 skip "zfs-osd does not support 'ladvise dontneed'"
16665
16666         local size_mb=100
16667         local size=$((size_mb * 1048576))
16668         # In order to prevent disturbance of other processes, only check 3/4
16669         # of the memory usage
16670         local kibibytes=$((size_mb * 1024 * 3 / 4))
16671
16672         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
16673                 error "dd to $DIR/$tfile failed"
16674
16675         #force write to complete before dropping OST cache & checking memory
16676         sync
16677
16678         local total=$(facet_meminfo ost1 MemTotal)
16679         echo "Total memory: $total KiB"
16680
16681         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
16682         local before_read=$(facet_meminfo ost1 Cached)
16683         echo "Cache used before read: $before_read KiB"
16684
16685         lfs ladvise -a willread $DIR/$tfile ||
16686                 error "Ladvise willread failed"
16687         local after_read=$(facet_meminfo ost1 Cached)
16688         echo "Cache used after read: $after_read KiB"
16689
16690         lfs ladvise -a dontneed $DIR/$tfile ||
16691                 error "Ladvise dontneed again failed"
16692         local no_read=$(facet_meminfo ost1 Cached)
16693         echo "Cache used after dontneed ladvise: $no_read KiB"
16694
16695         if [ $total -lt $((before_read + kibibytes)) ]; then
16696                 echo "Memory is too small, abort checking"
16697                 return 0
16698         fi
16699
16700         if [ $((before_read + kibibytes)) -gt $after_read ]; then
16701                 error "Ladvise willread should use more memory" \
16702                         "than $kibibytes KiB"
16703         fi
16704
16705         if [ $((no_read + kibibytes)) -gt $after_read ]; then
16706                 error "Ladvise dontneed should release more memory" \
16707                         "than $kibibytes KiB"
16708         fi
16709 }
16710 run_test 255b "check 'lfs ladvise -a dontneed'"
16711
16712 test_255c() {
16713         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
16714                 skip "lustre < 2.10.53 does not support lockahead"
16715
16716         local count
16717         local new_count
16718         local difference
16719         local i
16720         local rc
16721
16722         test_mkdir -p $DIR/$tdir
16723         $SETSTRIPE -i 0 $DIR/$tdir
16724
16725         #test 10 returns only success/failure
16726         i=10
16727         lockahead_test -d $DIR/$tdir -t $i -f $tfile
16728         rc=$?
16729         if [ $rc -eq 255 ]; then
16730                 error "Ladvise test${i} failed, ${rc}"
16731         fi
16732
16733         #test 11 counts lock enqueue requests, all others count new locks
16734         i=11
16735         count=$(do_facet ost1 \
16736                 $LCTL get_param -n ost.OSS.ost.stats)
16737         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
16738
16739         lockahead_test -d $DIR/$tdir -t $i -f $tfile
16740         rc=$?
16741         if [ $rc -eq 255 ]; then
16742                 error "Ladvise test${i} failed, ${rc}"
16743         fi
16744
16745         new_count=$(do_facet ost1 \
16746                 $LCTL get_param -n ost.OSS.ost.stats)
16747         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
16748                    awk '{ print $2 }')
16749
16750         difference="$((new_count - count))"
16751         if [ $difference -ne $rc ]; then
16752                 error "Ladvise test${i}, bad enqueue count, returned " \
16753                       "${rc}, actual ${difference}"
16754         fi
16755
16756         for i in $(seq 12 21); do
16757                 # If we do not do this, we run the risk of having too many
16758                 # locks and starting lock cancellation while we are checking
16759                 # lock counts.
16760                 cancel_lru_locks osc
16761
16762                 count=$($LCTL get_param -n \
16763                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
16764
16765                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
16766                 rc=$?
16767                 if [ $rc -eq 255 ]; then
16768                         error "Ladvise test ${i} failed, ${rc}"
16769                 fi
16770
16771                 new_count=$($LCTL get_param -n \
16772                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
16773                 difference="$((new_count - count))"
16774
16775                 # Test 15 output is divided by 100 to map down to valid return
16776                 if [ $i -eq 15 ]; then
16777                         rc="$((rc * 100))"
16778                 fi
16779
16780                 if [ $difference -ne $rc ]; then
16781                         error "Ladvise test ${i}, bad lock count, returned " \
16782                               "${rc}, actual ${difference}"
16783                 fi
16784         done
16785
16786         #test 22 returns only success/failure
16787         i=22
16788         lockahead_test -d $DIR/$tdir -t $i -f $tfile
16789         rc=$?
16790         if [ $rc -eq 255 ]; then
16791                 error "Ladvise test${i} failed, ${rc}"
16792         fi
16793 }
16794 run_test 255c "suite of ladvise lockahead tests"
16795
16796 test_256() {
16797         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16798         remote_mds_nodsh && skip "remote MDS with nodsh"
16799         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
16800         changelog_users $SINGLEMDS | grep "^cl" &&
16801                 skip "active changelog user"
16802
16803         local cl_user
16804         local cat_sl
16805         local mdt_dev
16806
16807         mdt_dev=$(mdsdevname 1)
16808         echo $mdt_dev
16809
16810         changelog_register || error "changelog_register failed"
16811
16812         rm -rf $DIR/$tdir
16813         mkdir -p $DIR/$tdir
16814
16815         changelog_clear 0 || error "changelog_clear failed"
16816
16817         # change something
16818         touch $DIR/$tdir/{1..10}
16819
16820         # stop the MDT
16821         stop $SINGLEMDS || error "Fail to stop MDT"
16822
16823         # remount the MDT
16824
16825         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
16826
16827         #after mount new plainllog is used
16828         touch $DIR/$tdir/{11..19}
16829         local tmpfile=$(mktemp -u $tfile.XXXXXX)
16830         cat_sl=$(do_facet $SINGLEMDS "sync; \
16831                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
16832                  llog_reader $tmpfile | grep -c type=1064553b")
16833         do_facet $SINGLEMDS llog_reader $tmpfile
16834
16835         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
16836
16837         changelog_clear 0 || error "changelog_clear failed"
16838
16839         cat_sl=$(do_facet $SINGLEMDS "sync; \
16840                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
16841                  llog_reader $tmpfile | grep -c type=1064553b; rm -f $tmpfile")
16842
16843         if (( cat_sl == 2 )); then
16844                 error "Empty plain llog was not deleted from changelog catalog"
16845         elif (( cat_sl != 1 )); then
16846                 error "Active plain llog shouldn't be deleted from catalog"
16847         fi
16848 }
16849 run_test 256 "Check llog delete for empty and not full state"
16850
16851 test_257() {
16852         remote_mds_nodsh && skip "remote MDS with nodsh"
16853         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
16854                 skip "Need MDS version at least 2.8.55"
16855
16856         test_mkdir $DIR/$tdir
16857
16858         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
16859                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
16860         stat $DIR/$tdir
16861
16862 #define OBD_FAIL_MDS_XATTR_REP                  0x161
16863         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
16864         local facet=mds$((mdtidx + 1))
16865         set_nodes_failloc $(facet_active_host $facet) 0x80000161
16866         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
16867
16868         stop $facet || error "stop MDS failed"
16869         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
16870                 error "start MDS fail"
16871 }
16872 run_test 257 "xattr locks are not lost"
16873
16874 # Verify we take the i_mutex when security requires it
16875 test_258a() {
16876 #define OBD_FAIL_IMUTEX_SEC 0x141c
16877         $LCTL set_param fail_loc=0x141c
16878         touch $DIR/$tfile
16879         chmod u+s $DIR/$tfile
16880         chmod a+rwx $DIR/$tfile
16881         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
16882         RC=$?
16883         if [ $RC -ne 0 ]; then
16884                 error "error, failed to take i_mutex, rc=$?"
16885         fi
16886         rm -f $DIR/$tfile
16887 }
16888 run_test 258a "verify i_mutex security behavior when suid attributes is set"
16889
16890 # Verify we do NOT take the i_mutex in the normal case
16891 test_258b() {
16892 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
16893         $LCTL set_param fail_loc=0x141d
16894         touch $DIR/$tfile
16895         chmod a+rwx $DIR
16896         chmod a+rw $DIR/$tfile
16897         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
16898         RC=$?
16899         if [ $RC -ne 0 ]; then
16900                 error "error, took i_mutex unnecessarily, rc=$?"
16901         fi
16902         rm -f $DIR/$tfile
16903
16904 }
16905 run_test 258b "verify i_mutex security behavior"
16906
16907 test_259() {
16908         local file=$DIR/$tfile
16909         local before
16910         local after
16911
16912         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
16913
16914         stack_trap "rm -f $file" EXIT
16915
16916         wait_delete_completed
16917         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
16918         echo "before: $before"
16919
16920         $LFS setstripe -i 0 -c 1 $file
16921         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
16922         sync_all_data
16923         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
16924         echo "after write: $after"
16925
16926 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
16927         do_facet ost1 $LCTL set_param fail_loc=0x2301
16928         $TRUNCATE $file 0
16929         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
16930         echo "after truncate: $after"
16931
16932         stop ost1
16933         do_facet ost1 $LCTL set_param fail_loc=0
16934         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
16935         sleep 2
16936         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
16937         echo "after restart: $after"
16938         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
16939                 error "missing truncate?"
16940
16941         return 0
16942 }
16943 run_test 259 "crash at delayed truncate"
16944
16945 test_260() {
16946 #define OBD_FAIL_MDC_CLOSE               0x806
16947         $LCTL set_param fail_loc=0x80000806
16948         touch $DIR/$tfile
16949
16950 }
16951 run_test 260 "Check mdc_close fail"
16952
16953 ### Data-on-MDT sanity tests ###
16954 test_270a() {
16955         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
16956                 skip "Need MDS version at least 2.10.55 for DoM"
16957
16958         # create DoM file
16959         local dom=$DIR/$tdir/dom_file
16960         local tmp=$DIR/$tdir/tmp_file
16961
16962         mkdir -p $DIR/$tdir
16963
16964         # basic checks for DoM component creation
16965         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
16966                 error "Can set MDT layout to non-first entry"
16967
16968         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
16969                 error "Can define multiple entries as MDT layout"
16970
16971         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
16972
16973         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
16974         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
16975         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
16976
16977         local mdtidx=$($LFS getstripe -m $dom)
16978         local mdtname=MDT$(printf %04x $mdtidx)
16979         local facet=mds$((mdtidx + 1))
16980         local space_check=1
16981
16982         # Skip free space checks with ZFS
16983         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
16984
16985         # write
16986         sync
16987         local size_tmp=$((65536 * 3))
16988         local mdtfree1=$(do_facet $facet \
16989                          lctl get_param -n osd*.*$mdtname.kbytesfree)
16990
16991         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
16992         # check also direct IO along write
16993         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
16994         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
16995         sync
16996         cmp $tmp $dom || error "file data is different"
16997         [ $(stat -c%s $dom) == $size_tmp ] ||
16998                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
16999         if [ $space_check == 1 ]; then
17000                 local mdtfree2=$(do_facet $facet \
17001                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
17002
17003                 # increase in usage from by $size_tmp
17004                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
17005                         error "MDT free space wrong after write: " \
17006                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
17007         fi
17008
17009         # truncate
17010         local size_dom=10000
17011
17012         $TRUNCATE $dom $size_dom
17013         [ $(stat -c%s $dom) == $size_dom ] ||
17014                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
17015         if [ $space_check == 1 ]; then
17016                 mdtfree1=$(do_facet $facet \
17017                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17018                 # decrease in usage from $size_tmp to new $size_dom
17019                 [ $(($mdtfree1 - $mdtfree2)) -ge \
17020                   $(((size_tmp - size_dom) / 1024)) ] ||
17021                         error "MDT free space is wrong after truncate: " \
17022                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
17023         fi
17024
17025         # append
17026         cat $tmp >> $dom
17027         sync
17028         size_dom=$((size_dom + size_tmp))
17029         [ $(stat -c%s $dom) == $size_dom ] ||
17030                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
17031         if [ $space_check == 1 ]; then
17032                 mdtfree2=$(do_facet $facet \
17033                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17034                 # increase in usage by $size_tmp from previous
17035                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
17036                         error "MDT free space is wrong after append: " \
17037                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
17038         fi
17039
17040         # delete
17041         rm $dom
17042         if [ $space_check == 1 ]; then
17043                 mdtfree1=$(do_facet $facet \
17044                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17045                 # decrease in usage by $size_dom from previous
17046                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
17047                         error "MDT free space is wrong after removal: " \
17048                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
17049         fi
17050
17051         # combined striping
17052         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
17053                 error "Can't create DoM + OST striping"
17054
17055         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
17056         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
17057         # check also direct IO along write
17058         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
17059         sync
17060         cmp $tmp $dom || error "file data is different"
17061         [ $(stat -c%s $dom) == $size_tmp ] ||
17062                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
17063         rm $dom $tmp
17064
17065         return 0
17066 }
17067 run_test 270a "DoM: basic functionality tests"
17068
17069 test_270b() {
17070         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17071                 skip "Need MDS version at least 2.10.55"
17072
17073         local dom=$DIR/$tdir/dom_file
17074         local max_size=1048576
17075
17076         mkdir -p $DIR/$tdir
17077         $LFS setstripe -E $max_size -L mdt $dom
17078
17079         # truncate over the limit
17080         $TRUNCATE $dom $(($max_size + 1)) &&
17081                 error "successful truncate over the maximum size"
17082         # write over the limit
17083         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
17084                 error "successful write over the maximum size"
17085         # append over the limit
17086         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
17087         echo "12345" >> $dom && error "successful append over the maximum size"
17088         rm $dom
17089
17090         return 0
17091 }
17092 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
17093
17094 test_270c() {
17095         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17096                 skip "Need MDS version at least 2.10.55"
17097
17098         mkdir -p $DIR/$tdir
17099         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17100
17101         # check files inherit DoM EA
17102         touch $DIR/$tdir/first
17103         [ $($GETSTRIPE -L $DIR/$tdir/first) == "mdt" ] ||
17104                 error "bad pattern"
17105         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
17106                 error "bad stripe count"
17107         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
17108                 error "bad stripe size"
17109
17110         # check directory inherits DoM EA and uses it as default
17111         mkdir $DIR/$tdir/subdir
17112         touch $DIR/$tdir/subdir/second
17113         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
17114                 error "bad pattern in sub-directory"
17115         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
17116                 error "bad stripe count in sub-directory"
17117         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
17118                 error "bad stripe size in sub-directory"
17119         return 0
17120 }
17121 run_test 270c "DoM: DoM EA inheritance tests"
17122
17123 test_270d() {
17124         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17125                 skip "Need MDS version at least 2.10.55"
17126
17127         mkdir -p $DIR/$tdir
17128         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17129
17130         # inherit default DoM striping
17131         mkdir $DIR/$tdir/subdir
17132         touch $DIR/$tdir/subdir/f1
17133
17134         # change default directory striping
17135         $LFS setstripe -c 1 $DIR/$tdir/subdir
17136         touch $DIR/$tdir/subdir/f2
17137         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
17138                 error "wrong default striping in file 2"
17139         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
17140                 error "bad pattern in file 2"
17141         return 0
17142 }
17143 run_test 270d "DoM: change striping from DoM to RAID0"
17144
17145 test_270e() {
17146         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17147                 skip "Need MDS version at least 2.10.55"
17148
17149         mkdir -p $DIR/$tdir/dom
17150         mkdir -p $DIR/$tdir/norm
17151         DOMFILES=20
17152         NORMFILES=10
17153         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
17154         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
17155
17156         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
17157         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
17158
17159         # find DoM files by layout
17160         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
17161         [ $NUM -eq  $DOMFILES ] ||
17162                 error "lfs find -L: found $NUM, expected $DOMFILES"
17163         echo "Test 1: lfs find 20 DOM files by layout: OK"
17164
17165         # there should be 1 dir with default DOM striping
17166         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
17167         [ $NUM -eq  1 ] ||
17168                 error "lfs find -L: found $NUM, expected 1 dir"
17169         echo "Test 2: lfs find 1 DOM dir by layout: OK"
17170
17171         # find DoM files by stripe size
17172         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
17173         [ $NUM -eq  $DOMFILES ] ||
17174                 error "lfs find -S: found $NUM, expected $DOMFILES"
17175         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
17176
17177         # find files by stripe offset except DoM files
17178         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
17179         [ $NUM -eq  $NORMFILES ] ||
17180                 error "lfs find -i: found $NUM, expected $NORMFILES"
17181         echo "Test 5: lfs find no DOM files by stripe index: OK"
17182         return 0
17183 }
17184 run_test 270e "DoM: lfs find with DoM files test"
17185
17186 test_270f() {
17187         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17188                 skip "Need MDS version at least 2.10.55"
17189
17190         local mdtname=${FSNAME}-MDT0000-mdtlov
17191         local dom=$DIR/$tdir/dom_file
17192         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
17193                                                 lod.$mdtname.dom_stripesize)
17194         local dom_limit=131072
17195
17196         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
17197         local dom_current=$(do_facet mds1 $LCTL get_param -n \
17198                                                 lod.$mdtname.dom_stripesize)
17199         [ ${dom_limit} -eq ${dom_current} ] ||
17200                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
17201
17202         $LFS mkdir -i 0 -c 1 $DIR/$tdir
17203         $LFS setstripe -d $DIR/$tdir
17204         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
17205                 error "Can't set directory default striping"
17206
17207         # exceed maximum stripe size
17208         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
17209                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
17210         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
17211                 error "Able to create DoM component size more than LOD limit"
17212
17213         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
17214         dom_current=$(do_facet mds1 $LCTL get_param -n \
17215                                                 lod.$mdtname.dom_stripesize)
17216         [ 0 -eq ${dom_current} ] ||
17217                 error "Can't set zero DoM stripe limit"
17218         rm $dom
17219
17220         # attempt to create DoM file on server with disabled DoM should
17221         # remove DoM entry from layout and be succeed
17222         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
17223                 error "Can't create DoM file (DoM is disabled)"
17224         [ $($LFS getstripe -L $dom) == "mdt" ] &&
17225                 error "File has DoM component while DoM is disabled"
17226         rm $dom
17227
17228         # attempt to create DoM file with only DoM stripe should return error
17229         $LFS setstripe -E $dom_limit -L mdt $dom &&
17230                 error "Able to create DoM-only file while DoM is disabled"
17231
17232         # too low values to be aligned with smallest stripe size 64K
17233         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
17234         dom_current=$(do_facet mds1 $LCTL get_param -n \
17235                                                 lod.$mdtname.dom_stripesize)
17236         [ 30000 -eq ${dom_current} ] &&
17237                 error "Can set too small DoM stripe limit"
17238
17239         # 64K is a minimal stripe size in Lustre, expect limit of that size
17240         [ 65536 -eq ${dom_current} ] ||
17241                 error "Limit is not set to 64K but ${dom_current}"
17242
17243         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
17244         dom_current=$(do_facet mds1 $LCTL get_param -n \
17245                                                 lod.$mdtname.dom_stripesize)
17246         echo $dom_current
17247         [ 2147483648 -eq ${dom_current} ] &&
17248                 error "Can set too large DoM stripe limit"
17249
17250         do_facet mds1 $LCTL set_param -n \
17251                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
17252         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
17253                 error "Can't create DoM component size after limit change"
17254         do_facet mds1 $LCTL set_param -n \
17255                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
17256         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
17257                 error "Can't create DoM file after limit decrease"
17258         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
17259                 error "Can create big DoM component after limit decrease"
17260         touch ${dom}_def ||
17261                 error "Can't create file with old default layout"
17262
17263         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
17264         return 0
17265 }
17266 run_test 270f "DoM: maximum DoM stripe size checks"
17267
17268 test_271a() {
17269         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17270                 skip "Need MDS version at least 2.10.55"
17271
17272         local dom=$DIR/$tdir/dom
17273
17274         mkdir -p $DIR/$tdir
17275
17276         $LFS setstripe -E 1024K -L mdt $dom
17277
17278         lctl set_param -n mdc.*.stats=clear
17279         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
17280         cat $dom > /dev/null
17281         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
17282         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
17283         ls $dom
17284         rm -f $dom
17285 }
17286 run_test 271a "DoM: data is cached for read after write"
17287
17288 test_271b() {
17289         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17290                 skip "Need MDS version at least 2.10.55"
17291
17292         local dom=$DIR/$tdir/dom
17293
17294         mkdir -p $DIR/$tdir
17295
17296         $LFS setstripe -E 1024K -L mdt -E EOF $dom
17297
17298         lctl set_param -n mdc.*.stats=clear
17299         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
17300         cancel_lru_locks mdc
17301         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
17302         # second stat to check size is cached on client
17303         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
17304         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
17305         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
17306         rm -f $dom
17307 }
17308 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
17309
17310 test_271ba() {
17311         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17312                 skip "Need MDS version at least 2.10.55"
17313
17314         local dom=$DIR/$tdir/dom
17315
17316         mkdir -p $DIR/$tdir
17317
17318         $LFS setstripe -E 1024K -L mdt -E EOF $dom
17319
17320         lctl set_param -n mdc.*.stats=clear
17321         lctl set_param -n osc.*.stats=clear
17322         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
17323         cancel_lru_locks mdc
17324         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
17325         # second stat to check size is cached on client
17326         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
17327         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
17328         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
17329         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
17330         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
17331         rm -f $dom
17332 }
17333 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
17334
17335
17336 get_mdc_stats() {
17337         local mdtidx=$1
17338         local param=$2
17339         local mdt=MDT$(printf %04x $mdtidx)
17340
17341         if [ -z $param ]; then
17342                 lctl get_param -n mdc.*$mdt*.stats
17343         else
17344                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
17345         fi
17346 }
17347
17348 test_271c() {
17349         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17350                 skip "Need MDS version at least 2.10.55"
17351
17352         local dom=$DIR/$tdir/dom
17353
17354         mkdir -p $DIR/$tdir
17355
17356         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17357
17358         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
17359         local facet=mds$((mdtidx + 1))
17360
17361         cancel_lru_locks mdc
17362         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
17363         createmany -o $dom 1000
17364         lctl set_param -n mdc.*.stats=clear
17365         smalliomany -w $dom 1000 200
17366         get_mdc_stats $mdtidx
17367         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
17368         # Each file has 1 open, 1 IO enqueues, total 2000
17369         # but now we have also +1 getxattr for security.capability, total 3000
17370         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
17371         unlinkmany $dom 1000
17372
17373         cancel_lru_locks mdc
17374         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
17375         createmany -o $dom 1000
17376         lctl set_param -n mdc.*.stats=clear
17377         smalliomany -w $dom 1000 200
17378         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
17379         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
17380         # for OPEN and IO lock.
17381         [ $((enq - enq_2)) -ge 1000 ] ||
17382                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
17383         unlinkmany $dom 1000
17384         return 0
17385 }
17386 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
17387
17388 cleanup_271def_tests() {
17389         trap 0
17390         rm -f $1
17391 }
17392
17393 test_271d() {
17394         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
17395                 skip "Need MDS version at least 2.10.57"
17396
17397         local dom=$DIR/$tdir/dom
17398         local tmp=$TMP/$tfile
17399         trap "cleanup_271def_tests $tmp" EXIT
17400
17401         mkdir -p $DIR/$tdir
17402
17403         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17404
17405         local mdtidx=$($GETSTRIPE -M $DIR/$tdir)
17406
17407         cancel_lru_locks mdc
17408         dd if=/dev/urandom of=$tmp bs=1000 count=1
17409         dd if=$tmp of=$dom bs=1000 count=1
17410         cancel_lru_locks mdc
17411
17412         cat /etc/hosts >> $tmp
17413         lctl set_param -n mdc.*.stats=clear
17414
17415         # append data to the same file it should update local page
17416         echo "Append to the same page"
17417         cat /etc/hosts >> $dom
17418         local num=$(get_mdc_stats $mdtidx ost_read)
17419         local ra=$(get_mdc_stats $mdtidx req_active)
17420         local rw=$(get_mdc_stats $mdtidx req_waittime)
17421
17422         [ -z $num ] || error "$num READ RPC occured"
17423         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
17424         echo "... DONE"
17425
17426         # compare content
17427         cmp $tmp $dom || error "file miscompare"
17428
17429         cancel_lru_locks mdc
17430         lctl set_param -n mdc.*.stats=clear
17431
17432         echo "Open and read file"
17433         cat $dom > /dev/null
17434         local num=$(get_mdc_stats $mdtidx ost_read)
17435         local ra=$(get_mdc_stats $mdtidx req_active)
17436         local rw=$(get_mdc_stats $mdtidx req_waittime)
17437
17438         [ -z $num ] || error "$num READ RPC occured"
17439         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
17440         echo "... DONE"
17441
17442         # compare content
17443         cmp $tmp $dom || error "file miscompare"
17444
17445         return 0
17446 }
17447 run_test 271d "DoM: read on open (1K file in reply buffer)"
17448
17449 test_271f() {
17450         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
17451                 skip "Need MDS version at least 2.10.57"
17452
17453         local dom=$DIR/$tdir/dom
17454         local tmp=$TMP/$tfile
17455         trap "cleanup_271def_tests $tmp" EXIT
17456
17457         mkdir -p $DIR/$tdir
17458
17459         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17460
17461         local mdtidx=$($GETSTRIPE -M $DIR/$tdir)
17462
17463         cancel_lru_locks mdc
17464         dd if=/dev/urandom of=$tmp bs=200000 count=1
17465         dd if=$tmp of=$dom bs=200000 count=1
17466         cancel_lru_locks mdc
17467         cat /etc/hosts >> $tmp
17468         lctl set_param -n mdc.*.stats=clear
17469
17470         echo "Append to the same page"
17471         cat /etc/hosts >> $dom
17472         local num=$(get_mdc_stats $mdtidx ost_read)
17473         local ra=$(get_mdc_stats $mdtidx req_active)
17474         local rw=$(get_mdc_stats $mdtidx req_waittime)
17475
17476         [ -z $num ] || error "$num READ RPC occured"
17477         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
17478         echo "... DONE"
17479
17480         # compare content
17481         cmp $tmp $dom || error "file miscompare"
17482
17483         cancel_lru_locks mdc
17484         lctl set_param -n mdc.*.stats=clear
17485
17486         echo "Open and read file"
17487         cat $dom > /dev/null
17488         local num=$(get_mdc_stats $mdtidx ost_read)
17489         local ra=$(get_mdc_stats $mdtidx req_active)
17490         local rw=$(get_mdc_stats $mdtidx req_waittime)
17491
17492         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
17493         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
17494         echo "... DONE"
17495
17496         # compare content
17497         cmp $tmp $dom || error "file miscompare"
17498
17499         return 0
17500 }
17501 run_test 271f "DoM: read on open (200K file and read tail)"
17502
17503 test_271g() {
17504         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
17505                 skip "Skipping due to old client or server version"
17506
17507         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
17508         # to get layout
17509         $CHECKSTAT -t file $DIR1/$tfile
17510
17511         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
17512         MULTIOP_PID=$!
17513         sleep 1
17514         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
17515         $LCTL set_param fail_loc=0x80000314
17516         rm $DIR1/$tfile || error "Unlink fails"
17517         RC=$?
17518         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
17519         [ $RC -eq 0 ] || error "Failed write to stale object"
17520 }
17521 run_test 271g "Discard DoM data vs client flush race"
17522
17523 test_272a() {
17524         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
17525                 skip "Need MDS version at least 2.11.50"
17526
17527         local dom=$DIR/$tdir/dom
17528         mkdir -p $DIR/$tdir
17529
17530         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
17531         dd if=/dev/urandom of=$dom bs=512K count=1 ||
17532                 error "failed to write data into $dom"
17533         local old_md5=$(md5sum $dom)
17534
17535         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
17536                 error "failed to migrate to the same DoM component"
17537
17538         [ $($LFS getstripe -c $dom) -eq 2 ] ||
17539                 error "layout was not changed silently"
17540
17541         local new_md5=$(md5sum $dom)
17542
17543         [ "$old_md5" != "$new_md5" ] &&
17544                 error "md5sum differ: $old_md5, $new_md5"
17545         return 0
17546 }
17547 run_test 272a "DoM migration: new layout with the same DOM component"
17548
17549 test_272b() {
17550         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
17551                 skip "Need MDS version at least 2.11.50"
17552
17553         local dom=$DIR/$tdir/dom
17554         mkdir -p $DIR/$tdir
17555         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
17556
17557         local mdtidx=$($LFS getstripe -m $dom)
17558         local mdtname=MDT$(printf %04x $mdtidx)
17559         local facet=mds$((mdtidx + 1))
17560
17561         local mdtfree1=$(do_facet $facet \
17562                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17563         dd if=/dev/urandom of=$dom bs=2M count=1 ||
17564                 error "failed to write data into $dom"
17565         local old_md5=$(md5sum $dom)
17566         cancel_lru_locks mdc
17567         local mdtfree1=$(do_facet $facet \
17568                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17569
17570         $LFS migrate -c2 $dom ||
17571                 error "failed to migrate to the new composite layout"
17572         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
17573                 error "MDT stripe was not removed"
17574
17575         cancel_lru_locks mdc
17576         local new_md5=$(md5sum $dom)
17577         [ "$old_md5" != "$new_md5" ] &&
17578                 error "$old_md5 != $new_md5"
17579
17580         # Skip free space checks with ZFS
17581         if [ "$(facet_fstype $facet)" != "zfs" ]; then
17582                 local mdtfree2=$(do_facet $facet \
17583                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17584                 [ $mdtfree2 -gt $mdtfree1 ] ||
17585                         error "MDT space is not freed after migration"
17586         fi
17587         return 0
17588 }
17589 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
17590
17591 test_272c() {
17592         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
17593                 skip "Need MDS version at least 2.11.50"
17594
17595         local dom=$DIR/$tdir/$tfile
17596         mkdir -p $DIR/$tdir
17597         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
17598
17599         local mdtidx=$($LFS getstripe -m $dom)
17600         local mdtname=MDT$(printf %04x $mdtidx)
17601         local facet=mds$((mdtidx + 1))
17602
17603         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
17604                 error "failed to write data into $dom"
17605         local old_md5=$(md5sum $dom)
17606         cancel_lru_locks mdc
17607         local mdtfree1=$(do_facet $facet \
17608                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17609
17610         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
17611                 error "failed to migrate to the new composite layout"
17612         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
17613                 error "MDT stripe was not removed"
17614
17615         cancel_lru_locks mdc
17616         local new_md5=$(md5sum $dom)
17617         [ "$old_md5" != "$new_md5" ] &&
17618                 error "$old_md5 != $new_md5"
17619
17620         # Skip free space checks with ZFS
17621         if [ "$(facet_fstype $facet)" != "zfs" ]; then
17622                 local mdtfree2=$(do_facet $facet \
17623                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17624                 [ $mdtfree2 -gt $mdtfree1 ] ||
17625                         error "MDS space is not freed after migration"
17626         fi
17627         return 0
17628 }
17629 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
17630
17631 test_273a() {
17632         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
17633                 skip "Need MDS version at least 2.11.50"
17634
17635         # Layout swap cannot be done if either file has DOM component,
17636         # this will never be supported, migration should be used instead
17637
17638         local dom=$DIR/$tdir/$tfile
17639         mkdir -p $DIR/$tdir
17640
17641         $LFS setstripe -c2 ${dom}_plain
17642         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
17643         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
17644                 error "can swap layout with DoM component"
17645         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
17646                 error "can swap layout with DoM component"
17647
17648         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
17649         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
17650                 error "can swap layout with DoM component"
17651         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
17652                 error "can swap layout with DoM component"
17653         return 0
17654 }
17655 run_test 273a "DoM: layout swapping should fail with DOM"
17656
17657 test_275() {
17658         remote_ost_nodsh && skip "remote OST with nodsh"
17659         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
17660                 skip "Need OST version >= 2.10.57"
17661
17662         local file=$DIR/$tfile
17663         local oss
17664
17665         oss=$(comma_list $(osts_nodes))
17666
17667         dd if=/dev/urandom of=$file bs=1M count=2 ||
17668                 error "failed to create a file"
17669         cancel_lru_locks osc
17670
17671         #lock 1
17672         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
17673                 error "failed to read a file"
17674
17675 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
17676         $LCTL set_param fail_loc=0x8000031f
17677
17678         cancel_lru_locks osc &
17679         sleep 1
17680
17681 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
17682         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
17683         #IO takes another lock, but matches the PENDING one
17684         #and places it to the IO RPC
17685         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
17686                 error "failed to read a file with PENDING lock"
17687 }
17688 run_test 275 "Read on a canceled duplicate lock"
17689
17690 test_276() {
17691         remote_ost_nodsh && skip "remote OST with nodsh"
17692         local pid
17693
17694         do_facet ost1 "(while true; do \
17695                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
17696                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
17697         pid=$!
17698
17699         for LOOP in $(seq 20); do
17700                 stop ost1
17701                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
17702         done
17703         kill -9 $pid
17704         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
17705                 rm $TMP/sanity_276_pid"
17706 }
17707 run_test 276 "Race between mount and obd_statfs"
17708
17709 cleanup_test_300() {
17710         trap 0
17711         umask $SAVE_UMASK
17712 }
17713 test_striped_dir() {
17714         local mdt_index=$1
17715         local stripe_count
17716         local stripe_index
17717
17718         mkdir -p $DIR/$tdir
17719
17720         SAVE_UMASK=$(umask)
17721         trap cleanup_test_300 RETURN EXIT
17722
17723         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
17724                                                 $DIR/$tdir/striped_dir ||
17725                 error "set striped dir error"
17726
17727         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
17728         [ "$mode" = "755" ] || error "expect 755 got $mode"
17729
17730         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
17731                 error "getdirstripe failed"
17732         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
17733         if [ "$stripe_count" != "2" ]; then
17734                 error "1:stripe_count is $stripe_count, expect 2"
17735         fi
17736         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
17737         if [ "$stripe_count" != "2" ]; then
17738                 error "2:stripe_count is $stripe_count, expect 2"
17739         fi
17740
17741         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
17742         if [ "$stripe_index" != "$mdt_index" ]; then
17743                 error "stripe_index is $stripe_index, expect $mdt_index"
17744         fi
17745
17746         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
17747                 error "nlink error after create striped dir"
17748
17749         mkdir $DIR/$tdir/striped_dir/a
17750         mkdir $DIR/$tdir/striped_dir/b
17751
17752         stat $DIR/$tdir/striped_dir/a ||
17753                 error "create dir under striped dir failed"
17754         stat $DIR/$tdir/striped_dir/b ||
17755                 error "create dir under striped dir failed"
17756
17757         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
17758                 error "nlink error after mkdir"
17759
17760         rmdir $DIR/$tdir/striped_dir/a
17761         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
17762                 error "nlink error after rmdir"
17763
17764         rmdir $DIR/$tdir/striped_dir/b
17765         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
17766                 error "nlink error after rmdir"
17767
17768         chattr +i $DIR/$tdir/striped_dir
17769         createmany -o $DIR/$tdir/striped_dir/f 10 &&
17770                 error "immutable flags not working under striped dir!"
17771         chattr -i $DIR/$tdir/striped_dir
17772
17773         rmdir $DIR/$tdir/striped_dir ||
17774                 error "rmdir striped dir error"
17775
17776         cleanup_test_300
17777
17778         true
17779 }
17780
17781 test_300a() {
17782         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
17783                 skip "skipped for lustre < 2.7.0"
17784         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17785         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17786
17787         test_striped_dir 0 || error "failed on striped dir on MDT0"
17788         test_striped_dir 1 || error "failed on striped dir on MDT0"
17789 }
17790 run_test 300a "basic striped dir sanity test"
17791
17792 test_300b() {
17793         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
17794                 skip "skipped for lustre < 2.7.0"
17795         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17796         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17797
17798         local i
17799         local mtime1
17800         local mtime2
17801         local mtime3
17802
17803         test_mkdir $DIR/$tdir || error "mkdir fail"
17804         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
17805                 error "set striped dir error"
17806         for i in {0..9}; do
17807                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
17808                 sleep 1
17809                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
17810                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
17811                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
17812                 sleep 1
17813                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
17814                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
17815                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
17816         done
17817         true
17818 }
17819 run_test 300b "check ctime/mtime for striped dir"
17820
17821 test_300c() {
17822         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
17823                 skip "skipped for lustre < 2.7.0"
17824         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17825         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17826
17827         local file_count
17828
17829         mkdir -p $DIR/$tdir
17830         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
17831                 error "set striped dir error"
17832
17833         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
17834                 error "chown striped dir failed"
17835
17836         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
17837                 error "create 5k files failed"
17838
17839         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
17840
17841         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
17842
17843         rm -rf $DIR/$tdir
17844 }
17845 run_test 300c "chown && check ls under striped directory"
17846
17847 test_300d() {
17848         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
17849                 skip "skipped for lustre < 2.7.0"
17850         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17851         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17852
17853         local stripe_count
17854         local file
17855
17856         mkdir -p $DIR/$tdir
17857         $SETSTRIPE -c 2 $DIR/$tdir
17858
17859         #local striped directory
17860         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
17861                 error "set striped dir error"
17862         createmany -o $DIR/$tdir/striped_dir/f 10 ||
17863                 error "create 10 files failed"
17864
17865         #remote striped directory
17866         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
17867                 error "set striped dir error"
17868         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
17869                 error "create 10 files failed"
17870
17871         for file in $(find $DIR/$tdir); do
17872                 stripe_count=$($LFS getstripe -c $file)
17873                 [ $stripe_count -eq 2 ] ||
17874                         error "wrong stripe $stripe_count for $file"
17875         done
17876
17877         rm -rf $DIR/$tdir
17878 }
17879 run_test 300d "check default stripe under striped directory"
17880
17881 test_300e() {
17882         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
17883                 skip "Need MDS version at least 2.7.55"
17884         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17885         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17886
17887         local stripe_count
17888         local file
17889
17890         mkdir -p $DIR/$tdir
17891
17892         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
17893                 error "set striped dir error"
17894
17895         touch $DIR/$tdir/striped_dir/a
17896         touch $DIR/$tdir/striped_dir/b
17897         touch $DIR/$tdir/striped_dir/c
17898
17899         mkdir $DIR/$tdir/striped_dir/dir_a
17900         mkdir $DIR/$tdir/striped_dir/dir_b
17901         mkdir $DIR/$tdir/striped_dir/dir_c
17902
17903         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
17904                 error "set striped adir under striped dir error"
17905
17906         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
17907                 error "set striped bdir under striped dir error"
17908
17909         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
17910                 error "set striped cdir under striped dir error"
17911
17912         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
17913                 error "rename dir under striped dir fails"
17914
17915         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
17916                 error "rename dir under different stripes fails"
17917
17918         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
17919                 error "rename file under striped dir should succeed"
17920
17921         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
17922                 error "rename dir under striped dir should succeed"
17923
17924         rm -rf $DIR/$tdir
17925 }
17926 run_test 300e "check rename under striped directory"
17927
17928 test_300f() {
17929         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17930         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17931         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
17932                 skip "Need MDS version at least 2.7.55"
17933
17934         local stripe_count
17935         local file
17936
17937         rm -rf $DIR/$tdir
17938         mkdir -p $DIR/$tdir
17939
17940         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
17941                 error "set striped dir error"
17942
17943         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
17944                 error "set striped dir error"
17945
17946         touch $DIR/$tdir/striped_dir/a
17947         mkdir $DIR/$tdir/striped_dir/dir_a
17948         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
17949                 error "create striped dir under striped dir fails"
17950
17951         touch $DIR/$tdir/striped_dir1/b
17952         mkdir $DIR/$tdir/striped_dir1/dir_b
17953         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
17954                 error "create striped dir under striped dir fails"
17955
17956         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
17957                 error "rename dir under different striped dir should fail"
17958
17959         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
17960                 error "rename striped dir under diff striped dir should fail"
17961
17962         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
17963                 error "rename file under diff striped dirs fails"
17964
17965         rm -rf $DIR/$tdir
17966 }
17967 run_test 300f "check rename cross striped directory"
17968
17969 test_300_check_default_striped_dir()
17970 {
17971         local dirname=$1
17972         local default_count=$2
17973         local default_index=$3
17974         local stripe_count
17975         local stripe_index
17976         local dir_stripe_index
17977         local dir
17978
17979         echo "checking $dirname $default_count $default_index"
17980         $LFS setdirstripe -D -c $default_count -i $default_index \
17981                                 -t all_char $DIR/$tdir/$dirname ||
17982                 error "set default stripe on striped dir error"
17983         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
17984         [ $stripe_count -eq $default_count ] ||
17985                 error "expect $default_count get $stripe_count for $dirname"
17986
17987         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
17988         [ $stripe_index -eq $default_index ] ||
17989                 error "expect $default_index get $stripe_index for $dirname"
17990
17991         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
17992                                                 error "create dirs failed"
17993
17994         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
17995         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
17996         for dir in $(find $DIR/$tdir/$dirname/*); do
17997                 stripe_count=$($LFS getdirstripe -c $dir)
17998                 [ $stripe_count -eq $default_count ] ||
17999                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
18000                 error "stripe count $default_count != $stripe_count for $dir"
18001
18002                 stripe_index=$($LFS getdirstripe -i $dir)
18003                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
18004                         error "$stripe_index != $default_index for $dir"
18005
18006                 #check default stripe
18007                 stripe_count=$($LFS getdirstripe -D -c $dir)
18008                 [ $stripe_count -eq $default_count ] ||
18009                 error "default count $default_count != $stripe_count for $dir"
18010
18011                 stripe_index=$($LFS getdirstripe -D -i $dir)
18012                 [ $stripe_index -eq $default_index ] ||
18013                 error "default index $default_index != $stripe_index for $dir"
18014         done
18015         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
18016 }
18017
18018 test_300g() {
18019         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18020         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18021                 skip "Need MDS version at least 2.7.55"
18022
18023         local dir
18024         local stripe_count
18025         local stripe_index
18026
18027         mkdir $DIR/$tdir
18028         mkdir $DIR/$tdir/normal_dir
18029
18030         #Checking when client cache stripe index
18031         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
18032         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
18033                 error "create striped_dir failed"
18034
18035         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
18036                 error "create dir0 fails"
18037         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
18038         [ $stripe_index -eq 0 ] ||
18039                 error "dir0 expect index 0 got $stripe_index"
18040
18041         mkdir $DIR/$tdir/striped_dir/dir1 ||
18042                 error "create dir1 fails"
18043         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
18044         [ $stripe_index -eq 1 ] ||
18045                 error "dir1 expect index 1 got $stripe_index"
18046
18047         #check default stripe count/stripe index
18048         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
18049         test_300_check_default_striped_dir normal_dir 1 0
18050         test_300_check_default_striped_dir normal_dir 2 1
18051         test_300_check_default_striped_dir normal_dir 2 -1
18052
18053         #delete default stripe information
18054         echo "delete default stripeEA"
18055         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
18056                 error "set default stripe on striped dir error"
18057
18058         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
18059         for dir in $(find $DIR/$tdir/normal_dir/*); do
18060                 stripe_count=$($LFS getdirstripe -c $dir)
18061                 [ $stripe_count -eq 0 ] ||
18062                         error "expect 1 get $stripe_count for $dir"
18063                 stripe_index=$($LFS getdirstripe -i $dir)
18064                 [ $stripe_index -eq 0 ] ||
18065                         error "expect 0 get $stripe_index for $dir"
18066         done
18067 }
18068 run_test 300g "check default striped directory for normal directory"
18069
18070 test_300h() {
18071         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18072         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18073                 skip "Need MDS version at least 2.7.55"
18074
18075         local dir
18076         local stripe_count
18077
18078         mkdir $DIR/$tdir
18079         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
18080                 error "set striped dir error"
18081
18082         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
18083         test_300_check_default_striped_dir striped_dir 1 0
18084         test_300_check_default_striped_dir striped_dir 2 1
18085         test_300_check_default_striped_dir striped_dir 2 -1
18086
18087         #delete default stripe information
18088         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
18089                 error "set default stripe on striped dir error"
18090
18091         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
18092         for dir in $(find $DIR/$tdir/striped_dir/*); do
18093                 stripe_count=$($LFS getdirstripe -c $dir)
18094                 [ $stripe_count -eq 0 ] ||
18095                         error "expect 1 get $stripe_count for $dir"
18096         done
18097 }
18098 run_test 300h "check default striped directory for striped directory"
18099
18100 test_300i() {
18101         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18102         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18103         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18104                 skip "Need MDS version at least 2.7.55"
18105
18106         local stripe_count
18107         local file
18108
18109         mkdir $DIR/$tdir
18110
18111         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
18112                 error "set striped dir error"
18113
18114         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
18115                 error "create files under striped dir failed"
18116
18117         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
18118                 error "set striped hashdir error"
18119
18120         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
18121                 error "create dir0 under hash dir failed"
18122         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
18123                 error "create dir1 under hash dir failed"
18124
18125         # unfortunately, we need to umount to clear dir layout cache for now
18126         # once we fully implement dir layout, we can drop this
18127         umount_client $MOUNT || error "umount failed"
18128         mount_client $MOUNT || error "mount failed"
18129
18130         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
18131         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
18132         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
18133
18134         #set the stripe to be unknown hash type
18135         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
18136         $LCTL set_param fail_loc=0x1901
18137         for ((i = 0; i < 10; i++)); do
18138                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
18139                         error "stat f-$i failed"
18140                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
18141         done
18142
18143         touch $DIR/$tdir/striped_dir/f0 &&
18144                 error "create under striped dir with unknown hash should fail"
18145
18146         $LCTL set_param fail_loc=0
18147
18148         umount_client $MOUNT || error "umount failed"
18149         mount_client $MOUNT || error "mount failed"
18150
18151         return 0
18152 }
18153 run_test 300i "client handle unknown hash type striped directory"
18154
18155 test_300j() {
18156         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18157         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18158         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18159                 skip "Need MDS version at least 2.7.55"
18160
18161         local stripe_count
18162         local file
18163
18164         mkdir $DIR/$tdir
18165
18166         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
18167         $LCTL set_param fail_loc=0x1702
18168         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
18169                 error "set striped dir error"
18170
18171         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
18172                 error "create files under striped dir failed"
18173
18174         $LCTL set_param fail_loc=0
18175
18176         rm -rf $DIR/$tdir || error "unlink striped dir fails"
18177
18178         return 0
18179 }
18180 run_test 300j "test large update record"
18181
18182 test_300k() {
18183         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18184         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18185         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18186                 skip "Need MDS version at least 2.7.55"
18187
18188         local stripe_count
18189         local file
18190
18191         mkdir $DIR/$tdir
18192
18193         #define OBD_FAIL_LARGE_STRIPE   0x1703
18194         $LCTL set_param fail_loc=0x1703
18195         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
18196                 error "set striped dir error"
18197         $LCTL set_param fail_loc=0
18198
18199         $LFS getdirstripe $DIR/$tdir/striped_dir ||
18200                 error "getstripeddir fails"
18201         rm -rf $DIR/$tdir/striped_dir ||
18202                 error "unlink striped dir fails"
18203
18204         return 0
18205 }
18206 run_test 300k "test large striped directory"
18207
18208 test_300l() {
18209         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18210         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18211         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18212                 skip "Need MDS version at least 2.7.55"
18213
18214         local stripe_index
18215
18216         test_mkdir -p $DIR/$tdir/striped_dir
18217         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
18218                         error "chown $RUNAS_ID failed"
18219         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
18220                 error "set default striped dir failed"
18221
18222         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
18223         $LCTL set_param fail_loc=0x80000158
18224         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
18225
18226         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
18227         [ $stripe_index -eq 1 ] ||
18228                 error "expect 1 get $stripe_index for $dir"
18229 }
18230 run_test 300l "non-root user to create dir under striped dir with stale layout"
18231
18232 test_300m() {
18233         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18234         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
18235         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18236                 skip "Need MDS version at least 2.7.55"
18237
18238         mkdir -p $DIR/$tdir/striped_dir
18239         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
18240                 error "set default stripes dir error"
18241
18242         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
18243
18244         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
18245         [ $stripe_count -eq 0 ] ||
18246                         error "expect 0 get $stripe_count for a"
18247
18248         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
18249                 error "set default stripes dir error"
18250
18251         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
18252
18253         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
18254         [ $stripe_count -eq 0 ] ||
18255                         error "expect 0 get $stripe_count for b"
18256
18257         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
18258                 error "set default stripes dir error"
18259
18260         mkdir $DIR/$tdir/striped_dir/c &&
18261                 error "default stripe_index is invalid, mkdir c should fails"
18262
18263         rm -rf $DIR/$tdir || error "rmdir fails"
18264 }
18265 run_test 300m "setstriped directory on single MDT FS"
18266
18267 cleanup_300n() {
18268         local list=$(comma_list $(mdts_nodes))
18269
18270         trap 0
18271         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
18272 }
18273
18274 test_300n() {
18275         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18276         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18277         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18278                 skip "Need MDS version at least 2.7.55"
18279         remote_mds_nodsh && skip "remote MDS with nodsh"
18280
18281         local stripe_index
18282         local list=$(comma_list $(mdts_nodes))
18283
18284         trap cleanup_300n RETURN EXIT
18285         mkdir -p $DIR/$tdir
18286         chmod 777 $DIR/$tdir
18287         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
18288                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
18289                 error "create striped dir succeeds with gid=0"
18290
18291         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
18292         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
18293                 error "create striped dir fails with gid=-1"
18294
18295         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
18296         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
18297                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
18298                 error "set default striped dir succeeds with gid=0"
18299
18300
18301         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
18302         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
18303                 error "set default striped dir fails with gid=-1"
18304
18305
18306         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
18307         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
18308                                         error "create test_dir fails"
18309         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
18310                                         error "create test_dir1 fails"
18311         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
18312                                         error "create test_dir2 fails"
18313         cleanup_300n
18314 }
18315 run_test 300n "non-root user to create dir under striped dir with default EA"
18316
18317 test_300o() {
18318         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18319         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18320         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18321                 skip "Need MDS version at least 2.7.55"
18322
18323         local numfree1
18324         local numfree2
18325
18326         mkdir -p $DIR/$tdir
18327
18328         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
18329         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
18330         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
18331                 skip "not enough free inodes $numfree1 $numfree2"
18332         fi
18333
18334         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
18335         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
18336         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
18337                 skip "not enough free space $numfree1 $numfree2"
18338         fi
18339
18340         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
18341                 error "setdirstripe fails"
18342
18343         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
18344                 error "create dirs fails"
18345
18346         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
18347         ls $DIR/$tdir/striped_dir > /dev/null ||
18348                 error "ls striped dir fails"
18349         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
18350                 error "unlink big striped dir fails"
18351 }
18352 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
18353
18354 test_300p() {
18355         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18356         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18357         remote_mds_nodsh && skip "remote MDS with nodsh"
18358
18359         mkdir -p $DIR/$tdir
18360
18361         #define OBD_FAIL_OUT_ENOSPC     0x1704
18362         do_facet mds2 lctl set_param fail_loc=0x80001704
18363         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
18364                  && error "create striped directory should fail"
18365
18366         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
18367
18368         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
18369         true
18370 }
18371 run_test 300p "create striped directory without space"
18372
18373 test_300q() {
18374         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18375         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18376
18377         local fd=$(free_fd)
18378         local cmd="exec $fd<$tdir"
18379         cd $DIR
18380         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
18381         eval $cmd
18382         cmd="exec $fd<&-"
18383         trap "eval $cmd" EXIT
18384         cd $tdir || error "cd $tdir fails"
18385         rmdir  ../$tdir || error "rmdir $tdir fails"
18386         mkdir local_dir && error "create dir succeeds"
18387         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
18388         eval $cmd
18389         return 0
18390 }
18391 run_test 300q "create remote directory under orphan directory"
18392
18393 test_300r() {
18394         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
18395                 skip "Need MDS version at least 2.7.55" && return
18396         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
18397
18398         mkdir $DIR/$tdir
18399
18400         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
18401                 error "set striped dir error"
18402
18403         $LFS getdirstripe $DIR/$tdir/striped_dir ||
18404                 error "getstripeddir fails"
18405
18406         local stripe_count
18407         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
18408                       awk '/lmv_stripe_count:/ { print $2 }')
18409
18410         [ $MDSCOUNT -ne $stripe_count ] &&
18411                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
18412
18413         rm -rf $DIR/$tdir/striped_dir ||
18414                 error "unlink striped dir fails"
18415 }
18416 run_test 300r "test -1 striped directory"
18417
18418 prepare_remote_file() {
18419         mkdir $DIR/$tdir/src_dir ||
18420                 error "create remote source failed"
18421
18422         cp /etc/hosts $DIR/$tdir/src_dir/a ||
18423                  error "cp to remote source failed"
18424         touch $DIR/$tdir/src_dir/a
18425
18426         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
18427                 error "create remote target dir failed"
18428
18429         touch $DIR/$tdir/tgt_dir/b
18430
18431         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
18432                 error "rename dir cross MDT failed!"
18433
18434         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
18435                 error "src_child still exists after rename"
18436
18437         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
18438                 error "missing file(a) after rename"
18439
18440         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
18441                 error "diff after rename"
18442 }
18443
18444 test_310a() {
18445         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
18446         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18447
18448         local remote_file=$DIR/$tdir/tgt_dir/b
18449
18450         mkdir -p $DIR/$tdir
18451
18452         prepare_remote_file || error "prepare remote file failed"
18453
18454         #open-unlink file
18455         $OPENUNLINK $remote_file $remote_file ||
18456                 error "openunlink $remote_file failed"
18457         $CHECKSTAT -a $remote_file || error "$remote_file exists"
18458 }
18459 run_test 310a "open unlink remote file"
18460
18461 test_310b() {
18462         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
18463         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18464
18465         local remote_file=$DIR/$tdir/tgt_dir/b
18466
18467         mkdir -p $DIR/$tdir
18468
18469         prepare_remote_file || error "prepare remote file failed"
18470
18471         ln $remote_file $DIR/$tfile || error "link failed for remote file"
18472         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
18473         $CHECKSTAT -t file $remote_file || error "check file failed"
18474 }
18475 run_test 310b "unlink remote file with multiple links while open"
18476
18477 test_310c() {
18478         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18479         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
18480
18481         local remote_file=$DIR/$tdir/tgt_dir/b
18482
18483         mkdir -p $DIR/$tdir
18484
18485         prepare_remote_file || error "prepare remote file failed"
18486
18487         ln $remote_file $DIR/$tfile || error "link failed for remote file"
18488         multiop_bg_pause $remote_file O_uc ||
18489                         error "mulitop failed for remote file"
18490         MULTIPID=$!
18491         $MULTIOP $DIR/$tfile Ouc
18492         kill -USR1 $MULTIPID
18493         wait $MULTIPID
18494 }
18495 run_test 310c "open-unlink remote file with multiple links"
18496
18497 #LU-4825
18498 test_311() {
18499         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18500         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
18501         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
18502                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
18503         remote_mds_nodsh && skip "remote MDS with nodsh"
18504
18505         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
18506         local mdts=$(comma_list $(mdts_nodes))
18507
18508         mkdir -p $DIR/$tdir
18509         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
18510         createmany -o $DIR/$tdir/$tfile. 1000
18511
18512         # statfs data is not real time, let's just calculate it
18513         old_iused=$((old_iused + 1000))
18514
18515         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
18516                         osp.*OST0000*MDT0000.create_count")
18517         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
18518                                 osp.*OST0000*MDT0000.max_create_count")
18519         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
18520
18521         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
18522         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
18523         [ $index -ne 0 ] || error "$tfile stripe index is 0"
18524
18525         unlinkmany $DIR/$tdir/$tfile. 1000
18526
18527         do_nodes $mdts "$LCTL set_param -n \
18528                         osp.*OST0000*.max_create_count=$max_count"
18529         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
18530                 do_nodes $mdts "$LCTL set_param -n \
18531                                 osp.*OST0000*.create_count=$count"
18532         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
18533                         grep "=0" && error "create_count is zero"
18534
18535         local new_iused
18536         for i in $(seq 120); do
18537                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
18538                 # system may be too busy to destroy all objs in time, use
18539                 # a somewhat small value to not fail autotest
18540                 [ $((old_iused - new_iused)) -gt 400 ] && break
18541                 sleep 1
18542         done
18543
18544         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
18545         [ $((old_iused - new_iused)) -gt 400 ] ||
18546                 error "objs not destroyed after unlink"
18547 }
18548 run_test 311 "disable OSP precreate, and unlink should destroy objs"
18549
18550 zfs_oid_to_objid()
18551 {
18552         local ost=$1
18553         local objid=$2
18554
18555         local vdevdir=$(dirname $(facet_vdevice $ost))
18556         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
18557         local zfs_zapid=$(do_facet $ost $cmd |
18558                           grep -w "/O/0/d$((objid%32))" -C 5 |
18559                           awk '/Object/{getline; print $1}')
18560         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
18561                           awk "/$objid = /"'{printf $3}')
18562
18563         echo $zfs_objid
18564 }
18565
18566 zfs_object_blksz() {
18567         local ost=$1
18568         local objid=$2
18569
18570         local vdevdir=$(dirname $(facet_vdevice $ost))
18571         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
18572         local blksz=$(do_facet $ost $cmd $objid |
18573                       awk '/dblk/{getline; printf $4}')
18574
18575         case "${blksz: -1}" in
18576                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
18577                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
18578                 *) ;;
18579         esac
18580
18581         echo $blksz
18582 }
18583
18584 test_312() { # LU-4856
18585         remote_ost_nodsh && skip "remote OST with nodsh"
18586         [ "$ost1_FSTYPE" = "zfs" ] ||
18587                 skip_env "the test only applies to zfs"
18588
18589         local max_blksz=$(do_facet ost1 \
18590                           $ZFS get -p recordsize $(facet_device ost1) |
18591                           awk '!/VALUE/{print $3}')
18592
18593         # to make life a little bit easier
18594         $LFS mkdir -c 1 -i 0 $DIR/$tdir
18595         $LFS setstripe -c 1 -i 0 $DIR/$tdir
18596
18597         local tf=$DIR/$tdir/$tfile
18598         touch $tf
18599         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
18600
18601         # Get ZFS object id
18602         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
18603         # block size change by sequential overwrite
18604         local bs
18605
18606         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
18607                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
18608
18609                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
18610                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
18611         done
18612         rm -f $tf
18613
18614         # block size change by sequential append write
18615         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
18616         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
18617         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
18618         local count
18619
18620         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
18621                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
18622                         oflag=sync conv=notrunc
18623
18624                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
18625                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
18626                         error "blksz error, actual $blksz, " \
18627                                 "expected: 2 * $count * $PAGE_SIZE"
18628         done
18629         rm -f $tf
18630
18631         # random write
18632         touch $tf
18633         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
18634         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
18635
18636         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
18637         blksz=$(zfs_object_blksz ost1 $zfs_objid)
18638         [ $blksz -eq $PAGE_SIZE ] ||
18639                 error "blksz error: $blksz, expected: $PAGE_SIZE"
18640
18641         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
18642         blksz=$(zfs_object_blksz ost1 $zfs_objid)
18643         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
18644
18645         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
18646         blksz=$(zfs_object_blksz ost1 $zfs_objid)
18647         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
18648 }
18649 run_test 312 "make sure ZFS adjusts its block size by write pattern"
18650
18651 test_313() {
18652         remote_ost_nodsh && skip "remote OST with nodsh"
18653
18654         local file=$DIR/$tfile
18655
18656         rm -f $file
18657         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
18658
18659         # define OBD_FAIL_TGT_RCVD_EIO           0x720
18660         do_facet ost1 "$LCTL set_param fail_loc=0x720"
18661         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
18662                 error "write should failed"
18663         do_facet ost1 "$LCTL set_param fail_loc=0"
18664         rm -f $file
18665 }
18666 run_test 313 "io should fail after last_rcvd update fail"
18667
18668 test_314() {
18669         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
18670
18671         $SETSTRIPE -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
18672         do_facet ost1 "$LCTL set_param fail_loc=0x720"
18673         rm -f $DIR/$tfile
18674         wait_delete_completed
18675         do_facet ost1 "$LCTL set_param fail_loc=0"
18676 }
18677 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
18678
18679 test_315() { # LU-618
18680         local file=$DIR/$tfile
18681         rm -f $file
18682
18683         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
18684                 error "multiop file write failed"
18685         $MULTIOP $file oO_RDONLY:r4063232_c &
18686         PID=$!
18687
18688         sleep 2
18689
18690         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
18691         kill -USR1 $PID
18692
18693         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
18694         rm -f $file
18695 }
18696 run_test 315 "read should be accounted"
18697
18698 test_316() {
18699         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
18700         large_xattr_enabled || skip_env "ea_inode feature disabled"
18701
18702         rm -rf $DIR/$tdir/d
18703         mkdir -p $DIR/$tdir/d
18704         chown nobody $DIR/$tdir/d
18705         touch $DIR/$tdir/d/file
18706
18707         $LFS mv -M1 $DIR/$tdir/d || error "lfs mv failed"
18708 }
18709 run_test 316 "lfs mv"
18710
18711 test_317() {
18712         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
18713                 skip "Need MDS version at least 2.11.53"
18714         local trunc_sz
18715         local grant_blk_size
18716
18717         if [ "$(facet_fstype $facet)" == "zfs" ]; then
18718                 skip "LU-10370: no implementation for ZFS" && return
18719         fi
18720
18721         stack_trap "rm -f $DIR/$tfile" EXIT
18722         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
18723                         awk '/grant_block_size:/ { print $2; exit; }')
18724         #
18725         # Create File of size 5M. Truncate it to below size's and verify
18726         # blocks count.
18727         #
18728         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
18729                 error "Create file : $DIR/$tfile"
18730
18731         for trunc_sz in 2097152 4097 4000 509 0; do
18732                 $TRUNCATE $DIR/$tfile $trunc_sz ||
18733                         error "truncate $tfile to $trunc_sz failed"
18734                 local sz=$(stat --format=%s $DIR/$tfile)
18735                 local blk=$(stat --format=%b $DIR/$tfile)
18736                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
18737                                      grant_blk_size) * 8))
18738
18739                 if [[ $blk -ne $trunc_blk ]]; then
18740                         $(which stat) $DIR/$tfile
18741                         error "Expected Block $trunc_blk got $blk for $tfile"
18742                 fi
18743
18744                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
18745                         error "Expected Size $trunc_sz got $sz for $tfile"
18746         done
18747
18748         #
18749         # sparse file test
18750         # Create file with a hole and write actual two blocks. Block count
18751         # must be 16.
18752         #
18753         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
18754                 conv=fsync || error "Create file : $DIR/$tfile"
18755
18756         # Calculate the final truncate size.
18757         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
18758
18759         #
18760         # truncate to size $trunc_sz bytes. Strip the last block
18761         # The block count must drop to 8
18762         #
18763         $TRUNCATE $DIR/$tfile $trunc_sz ||
18764                 error "truncate $tfile to $trunc_sz failed"
18765
18766         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
18767         sz=$(stat --format=%s $DIR/$tfile)
18768         blk=$(stat --format=%b $DIR/$tfile)
18769
18770         if [[ $blk -ne $trunc_bsz ]]; then
18771                 $(which stat) $DIR/$tfile
18772                 error "Expected Block $trunc_bsz got $blk for $tfile"
18773         fi
18774
18775         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
18776                 error "Expected Size $trunc_sz got $sz for $tfile"
18777 }
18778 run_test 317 "Verify blocks get correctly update after truncate"
18779
18780 test_fake_rw() {
18781         local read_write=$1
18782         if [ "$read_write" = "write" ]; then
18783                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
18784         elif [ "$read_write" = "read" ]; then
18785                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
18786         else
18787                 error "argument error"
18788         fi
18789
18790         # turn off debug for performance testing
18791         local saved_debug=$($LCTL get_param -n debug)
18792         $LCTL set_param debug=0
18793
18794         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
18795
18796         # get ost1 size - lustre-OST0000
18797         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
18798         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
18799         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
18800
18801         if [ "$read_write" = "read" ]; then
18802                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
18803         fi
18804
18805         local start_time=$(date +%s.%N)
18806         $dd_cmd bs=1M count=$blocks oflag=sync ||
18807                 error "real dd $read_write error"
18808         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
18809
18810         if [ "$read_write" = "write" ]; then
18811                 rm -f $DIR/$tfile
18812         fi
18813
18814         # define OBD_FAIL_OST_FAKE_RW           0x238
18815         do_facet ost1 $LCTL set_param fail_loc=0x238
18816
18817         local start_time=$(date +%s.%N)
18818         $dd_cmd bs=1M count=$blocks oflag=sync ||
18819                 error "fake dd $read_write error"
18820         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
18821
18822         if [ "$read_write" = "write" ]; then
18823                 # verify file size
18824                 cancel_lru_locks osc
18825                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
18826                         error "$tfile size not $blocks MB"
18827         fi
18828         do_facet ost1 $LCTL set_param fail_loc=0
18829
18830         echo "fake $read_write $duration_fake vs. normal $read_write" \
18831                 "$duration in seconds"
18832         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
18833                 error_not_in_vm "fake write is slower"
18834
18835         $LCTL set_param -n debug="$saved_debug"
18836         rm -f $DIR/$tfile
18837 }
18838 test_399a() { # LU-7655 for OST fake write
18839         remote_ost_nodsh && skip "remote OST with nodsh"
18840
18841         test_fake_rw write
18842 }
18843 run_test 399a "fake write should not be slower than normal write"
18844
18845 test_399b() { # LU-8726 for OST fake read
18846         remote_ost_nodsh && skip "remote OST with nodsh"
18847         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
18848                 skip_env "ldiskfs only test"
18849         fi
18850
18851         test_fake_rw read
18852 }
18853 run_test 399b "fake read should not be slower than normal read"
18854
18855 test_400a() { # LU-1606, was conf-sanity test_74
18856         if ! which $CC > /dev/null 2>&1; then
18857                 skip_env "$CC is not installed"
18858         fi
18859
18860         local extra_flags=''
18861         local out=$TMP/$tfile
18862         local prefix=/usr/include/lustre
18863         local prog
18864
18865         if ! [[ -d $prefix ]]; then
18866                 # Assume we're running in tree and fixup the include path.
18867                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
18868                 extra_flags+=" -L$LUSTRE/utils/.lib"
18869         fi
18870
18871         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
18872                 $CC -Wall -Werror $extra_flags -o $out $prog -llustreapi ||
18873                         error "client api broken"
18874         done
18875         rm -f $out
18876 }
18877 run_test 400a "Lustre client api program can compile and link"
18878
18879 test_400b() { # LU-1606, LU-5011
18880         local header
18881         local out=$TMP/$tfile
18882         local prefix=/usr/include/linux/lustre
18883
18884         # We use a hard coded prefix so that this test will not fail
18885         # when run in tree. There are headers in lustre/include/lustre/
18886         # that are not packaged (like lustre_idl.h) and have more
18887         # complicated include dependencies (like config.h and lnet/types.h).
18888         # Since this test about correct packaging we just skip them when
18889         # they don't exist (see below) rather than try to fixup cppflags.
18890
18891         if ! which $CC > /dev/null 2>&1; then
18892                 skip_env "$CC is not installed"
18893         fi
18894
18895         for header in $prefix/*.h; do
18896                 if ! [[ -f "$header" ]]; then
18897                         continue
18898                 fi
18899
18900                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
18901                         continue # lustre_ioctl.h is internal header
18902                 fi
18903
18904                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
18905                         error "cannot compile '$header'"
18906         done
18907         rm -f $out
18908 }
18909 run_test 400b "packaged headers can be compiled"
18910
18911 test_401a() { #LU-7437
18912         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
18913         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
18914
18915         #count the number of parameters by "list_param -R"
18916         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
18917         #count the number of parameters by listing proc files
18918         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
18919         echo "proc_dirs='$proc_dirs'"
18920         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
18921         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
18922                       sort -u | wc -l)
18923
18924         [ $params -eq $procs ] ||
18925                 error "found $params parameters vs. $procs proc files"
18926
18927         # test the list_param -D option only returns directories
18928         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
18929         #count the number of parameters by listing proc directories
18930         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
18931                 sort -u | wc -l)
18932
18933         [ $params -eq $procs ] ||
18934                 error "found $params parameters vs. $procs proc files"
18935 }
18936 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
18937
18938 test_401b() {
18939         local save=$($LCTL get_param -n jobid_var)
18940         local tmp=testing
18941
18942         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
18943                 error "no error returned when setting bad parameters"
18944
18945         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
18946         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
18947
18948         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
18949         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
18950         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
18951 }
18952 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
18953
18954 test_401c() {
18955         local jobid_var_old=$($LCTL get_param -n jobid_var)
18956         local jobid_var_new
18957
18958         $LCTL set_param jobid_var= &&
18959                 error "no error returned for 'set_param a='"
18960
18961         jobid_var_new=$($LCTL get_param -n jobid_var)
18962         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
18963                 error "jobid_var was changed by setting without value"
18964
18965         $LCTL set_param jobid_var &&
18966                 error "no error returned for 'set_param a'"
18967
18968         jobid_var_new=$($LCTL get_param -n jobid_var)
18969         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
18970                 error "jobid_var was changed by setting without value"
18971 }
18972 run_test 401c "Verify 'lctl set_param' without value fails in either format."
18973
18974 test_401d() {
18975         local jobid_var_old=$($LCTL get_param -n jobid_var)
18976         local jobid_var_new
18977         local new_value="foo=bar"
18978
18979         $LCTL set_param jobid_var=$new_value ||
18980                 error "'set_param a=b' did not accept a value containing '='"
18981
18982         jobid_var_new=$($LCTL get_param -n jobid_var)
18983         [[ "$jobid_var_new" == "$new_value" ]] ||
18984                 error "'set_param a=b' failed on a value containing '='"
18985
18986         # Reset the jobid_var to test the other format
18987         $LCTL set_param jobid_var=$jobid_var_old
18988         jobid_var_new=$($LCTL get_param -n jobid_var)
18989         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
18990                 error "failed to reset jobid_var"
18991
18992         $LCTL set_param jobid_var $new_value ||
18993                 error "'set_param a b' did not accept a value containing '='"
18994
18995         jobid_var_new=$($LCTL get_param -n jobid_var)
18996         [[ "$jobid_var_new" == "$new_value" ]] ||
18997                 error "'set_param a b' failed on a value containing '='"
18998
18999         $LCTL set_param jobid_var $jobid_var_old
19000         jobid_var_new=$($LCTL get_param -n jobid_var)
19001         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
19002                 error "failed to reset jobid_var"
19003 }
19004 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
19005
19006 test_402() {
19007         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
19008         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
19009                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
19010         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
19011                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
19012                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
19013         remote_mds_nodsh && skip "remote MDS with nodsh"
19014
19015         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
19016 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
19017         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
19018         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
19019                 echo "Touch failed - OK"
19020 }
19021 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
19022
19023 test_403() {
19024         local file1=$DIR/$tfile.1
19025         local file2=$DIR/$tfile.2
19026         local tfile=$TMP/$tfile
19027
19028         rm -f $file1 $file2 $tfile
19029
19030         touch $file1
19031         ln $file1 $file2
19032
19033         # 30 sec OBD_TIMEOUT in ll_getattr()
19034         # right before populating st_nlink
19035         $LCTL set_param fail_loc=0x80001409
19036         stat -c %h $file1 > $tfile &
19037
19038         # create an alias, drop all locks and reclaim the dentry
19039         < $file2
19040         cancel_lru_locks mdc
19041         cancel_lru_locks osc
19042         sysctl -w vm.drop_caches=2
19043
19044         wait
19045
19046         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
19047
19048         rm -f $tfile $file1 $file2
19049 }
19050 run_test 403 "i_nlink should not drop to zero due to aliasing"
19051
19052 test_404() { # LU-6601
19053         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
19054                 skip "Need server version newer than 2.8.52"
19055         remote_mds_nodsh && skip "remote MDS with nodsh"
19056
19057         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
19058                 awk '/osp .*-osc-MDT/ { print $4}')
19059
19060         local osp
19061         for osp in $mosps; do
19062                 echo "Deactivate: " $osp
19063                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
19064                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
19065                         awk -vp=$osp '$4 == p { print $2 }')
19066                 [ $stat = IN ] || {
19067                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
19068                         error "deactivate error"
19069                 }
19070                 echo "Activate: " $osp
19071                 do_facet $SINGLEMDS $LCTL --device %$osp activate
19072                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
19073                         awk -vp=$osp '$4 == p { print $2 }')
19074                 [ $stat = UP ] || {
19075                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
19076                         error "activate error"
19077                 }
19078         done
19079 }
19080 run_test 404 "validate manual {de}activated works properly for OSPs"
19081
19082 test_405() {
19083         [ $MDS1_VERSION -lt $(version_code 2.6.92) -o \
19084         [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
19085                 skip "Layout swap lock is not supported"
19086         check_swap_layouts_support
19087
19088         test_mkdir $DIR/$tdir
19089         swap_lock_test -d $DIR/$tdir ||
19090                 error "One layout swap locked test failed"
19091 }
19092 run_test 405 "Various layout swap lock tests"
19093
19094 test_406() {
19095         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19096         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
19097         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
19098         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19099         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
19100                 skip "Need MDS version at least 2.8.50"
19101
19102         local def_stripe_size=$($LFS getstripe -S $MOUNT)
19103         local test_pool=$TESTNAME
19104
19105         if ! combined_mgs_mds ; then
19106                 mount_mgs_client
19107         fi
19108         pool_add $test_pool || error "pool_add failed"
19109         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
19110                 error "pool_add_targets failed"
19111
19112         save_layout_restore_at_exit $MOUNT
19113
19114         # parent set default stripe count only, child will stripe from both
19115         # parent and fs default
19116         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
19117                 error "setstripe $MOUNT failed"
19118         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
19119         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
19120         for i in $(seq 10); do
19121                 local f=$DIR/$tdir/$tfile.$i
19122                 touch $f || error "touch failed"
19123                 local count=$($LFS getstripe -c $f)
19124                 [ $count -eq $OSTCOUNT ] ||
19125                         error "$f stripe count $count != $OSTCOUNT"
19126                 local offset=$($LFS getstripe -i $f)
19127                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
19128                 local size=$($LFS getstripe -S $f)
19129                 [ $size -eq $((def_stripe_size * 2)) ] ||
19130                         error "$f stripe size $size != $((def_stripe_size * 2))"
19131                 local pool=$($LFS getstripe -p $f)
19132                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
19133         done
19134
19135         # change fs default striping, delete parent default striping, now child
19136         # will stripe from new fs default striping only
19137         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
19138                 error "change $MOUNT default stripe failed"
19139         $LFS setstripe -c 0 $DIR/$tdir ||
19140                 error "delete $tdir default stripe failed"
19141         for i in $(seq 11 20); do
19142                 local f=$DIR/$tdir/$tfile.$i
19143                 touch $f || error "touch $f failed"
19144                 local count=$($LFS getstripe -c $f)
19145                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
19146                 local offset=$($LFS getstripe -i $f)
19147                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
19148                 local size=$($LFS getstripe -S $f)
19149                 [ $size -eq $def_stripe_size ] ||
19150                         error "$f stripe size $size != $def_stripe_size"
19151                 local pool=$($LFS getstripe -p $f)
19152                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
19153         done
19154
19155         unlinkmany $DIR/$tdir/$tfile. 1 20
19156
19157         local f=$DIR/$tdir/$tfile
19158         pool_remove_all_targets $test_pool $f
19159         pool_remove $test_pool $f
19160
19161         if ! combined_mgs_mds ; then
19162                 umount_mgs_client
19163         fi
19164 }
19165 run_test 406 "DNE support fs default striping"
19166
19167 test_407() {
19168         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19169         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
19170                 skip "Need MDS version at least 2.8.55"
19171         remote_mds_nodsh && skip "remote MDS with nodsh"
19172
19173         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
19174                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
19175         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
19176                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
19177         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
19178
19179         #define OBD_FAIL_DT_TXN_STOP    0x2019
19180         for idx in $(seq $MDSCOUNT); do
19181                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
19182         done
19183         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
19184         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
19185                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
19186         true
19187 }
19188 run_test 407 "transaction fail should cause operation fail"
19189
19190 test_408() {
19191         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
19192
19193         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
19194         lctl set_param fail_loc=0x8000040a
19195         # let ll_prepare_partial_page() fail
19196         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
19197
19198         rm -f $DIR/$tfile
19199
19200         # create at least 100 unused inodes so that
19201         # shrink_icache_memory(0) should not return 0
19202         touch $DIR/$tfile-{0..100}
19203         rm -f $DIR/$tfile-{0..100}
19204         sync
19205
19206         echo 2 > /proc/sys/vm/drop_caches
19207 }
19208 run_test 408 "drop_caches should not hang due to page leaks"
19209
19210 test_409()
19211 {
19212         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19213         check_mount_and_prep
19214
19215         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
19216         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
19217         touch $DIR/$tdir/guard || error "(2) Fail to create"
19218
19219         local PREFIX=$(str_repeat 'A' 128)
19220         echo "Create 1K hard links start at $(date)"
19221         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
19222                 error "(3) Fail to hard link"
19223
19224         echo "Links count should be right although linkEA overflow"
19225         stat $DIR/$tdir/guard || error "(4) Fail to stat"
19226         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
19227         [ $linkcount -eq 1001 ] ||
19228                 error "(5) Unexpected hard links count: $linkcount"
19229
19230         echo "List all links start at $(date)"
19231         ls -l $DIR/$tdir/foo > /dev/null ||
19232                 error "(6) Fail to list $DIR/$tdir/foo"
19233
19234         echo "Unlink hard links start at $(date)"
19235         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
19236                 error "(7) Fail to unlink"
19237 }
19238 run_test 409 "Large amount of cross-MDTs hard links on the same file"
19239
19240 test_410()
19241 {
19242         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
19243                 skip "Need client version at least 2.9.59"
19244
19245         # Create a file, and stat it from the kernel
19246         local testfile=$DIR/$tfile
19247         touch $testfile
19248
19249         local run_id=$RANDOM
19250         local my_ino=$(stat --format "%i" $testfile)
19251
19252         # Try to insert the module. This will always fail as the
19253         # module is designed to not be inserted.
19254         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
19255             &> /dev/null
19256
19257         # Anything but success is a test failure
19258         dmesg | grep -q \
19259             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
19260             error "no inode match"
19261 }
19262 run_test 410 "Test inode number returned from kernel thread"
19263
19264 cleanup_test411_cgroup() {
19265         trap 0
19266         rmdir "$1"
19267 }
19268
19269 test_411() {
19270         local cg_basedir=/sys/fs/cgroup/memory
19271         # LU-9966
19272         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
19273                 skip "no setup for cgroup"
19274
19275         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
19276                 error "test file creation failed"
19277         cancel_lru_locks osc
19278
19279         # Create a very small memory cgroup to force a slab allocation error
19280         local cgdir=$cg_basedir/osc_slab_alloc
19281         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
19282         trap "cleanup_test411_cgroup $cgdir" EXIT
19283         echo 2M > $cgdir/memory.kmem.limit_in_bytes
19284         echo 1M > $cgdir/memory.limit_in_bytes
19285
19286         # Should not LBUG, just be killed by oom-killer
19287         # dd will return 0 even allocation failure in some environment.
19288         # So don't check return value
19289         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
19290         cleanup_test411_cgroup $cgdir
19291
19292         return 0
19293 }
19294 run_test 411 "Slab allocation error with cgroup does not LBUG"
19295
19296 test_412() {
19297         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19298         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
19299                 skip "Need server version at least 2.10.55"
19300         fi
19301
19302         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
19303                 error "mkdir failed"
19304         $LFS getdirstripe $DIR/$tdir
19305         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
19306         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
19307                 error "expect $((MDSCOUT - 1)) get $stripe_index"
19308         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
19309         [ $stripe_count -eq 2 ] ||
19310                 error "expect 2 get $stripe_count"
19311 }
19312 run_test 412 "mkdir on specific MDTs"
19313
19314 test_413() {
19315         [ $MDSCOUNT -lt 2 ] &&
19316                 skip "We need at least 2 MDTs for this test"
19317
19318         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
19319                 skip "Need server version at least 2.10.55"
19320         fi
19321
19322         mkdir $DIR/$tdir || error "mkdir failed"
19323
19324         # find MDT that is the most full
19325         local max=$($LFS df | grep MDT |
19326                 awk 'BEGIN { a=0 }
19327                         { sub("%", "", $5)
19328                           if (0+$5 >= a)
19329                           {
19330                                 a = $5
19331                                 b = $6
19332                           }
19333                         }
19334                      END { split(b, c, ":")
19335                            sub("]", "", c[2])
19336                            print c[2]
19337                          }')
19338
19339         for i in $(seq $((MDSCOUNT - 1))); do
19340                 $LFS mkdir -c $i $DIR/$tdir/d$i ||
19341                         error "mkdir d$i failed"
19342                 $LFS getdirstripe $DIR/$tdir/d$i
19343                 local stripe_index=$($LFS getdirstripe -i $DIR/$tdir/d$i)
19344                 [ $stripe_index -ne $max ] ||
19345                         error "don't expect $max"
19346         done
19347 }
19348 run_test 413 "mkdir on less full MDTs"
19349
19350 test_414() {
19351 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
19352         $LCTL set_param fail_loc=0x80000521
19353         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
19354         rm -f $DIR/$tfile
19355 }
19356 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
19357
19358 test_415() {
19359         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19360         [ $(lustre_version_code mds1) -lt $(version_code 2.11.52) ] &&
19361                 skip "Need server version at least 2.11.52"
19362
19363         # LU-11102
19364         local total
19365         local setattr_pid
19366         local start_time
19367         local end_time
19368         local duration
19369
19370         total=500
19371         # this test may be slow on ZFS
19372         [ "$mds1_FSTYPE" == "zfs" ] && total=100
19373
19374         # though this test is designed for striped directory, let's test normal
19375         # directory too since lock is always saved as CoS lock.
19376         test_mkdir $DIR/$tdir || error "mkdir $tdir"
19377         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
19378
19379         (
19380                 while true; do
19381                         touch $DIR/$tdir
19382                 done
19383         ) &
19384         setattr_pid=$!
19385
19386         start_time=$(date +%s)
19387         for i in $(seq $total); do
19388                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
19389                         > /dev/null
19390         done
19391         end_time=$(date +%s)
19392         duration=$((end_time - start_time))
19393
19394         kill -9 $setattr_pid
19395
19396         echo "rename $total files took $duration sec"
19397         [ $duration -lt 100 ] || error "rename took $duration sec"
19398 }
19399 run_test 415 "lock revoke is not missing"
19400
19401 test_416() {
19402         [ $(lustre_version_code mds1) -lt $(version_code 2.11.55) ] &&
19403                 skip "Need server version at least 2.11.55"
19404
19405         # define OBD_FAIL_OSD_TXN_START    0x19a
19406         do_facet mds1 lctl set_param fail_loc=0x19a
19407
19408         lfs mkdir -c $MDSCOUNT $DIR/$tdir
19409
19410         true
19411 }
19412 run_test 416 "transaction start failure won't cause system hung"
19413
19414 cleanup_417() {
19415         trap 0
19416         do_nodes $(comma_list $(mdts_nodes)) \
19417                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
19418         do_nodes $(comma_list $(mdts_nodes)) \
19419                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
19420         do_nodes $(comma_list $(mdts_nodes)) \
19421                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
19422 }
19423
19424 test_417() {
19425         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19426         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
19427                 skip "Need MDS version at least 2.11.56"
19428
19429         trap cleanup_417 RETURN EXIT
19430
19431         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
19432         do_nodes $(comma_list $(mdts_nodes)) \
19433                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
19434         $LFS migrate -m 0 $DIR/$tdir.1 &&
19435                 error "migrate dir $tdir.1 should fail"
19436
19437         do_nodes $(comma_list $(mdts_nodes)) \
19438                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
19439         $LFS mkdir -i 1 $DIR/$tdir.2 &&
19440                 error "create remote dir $tdir.2 should fail"
19441
19442         do_nodes $(comma_list $(mdts_nodes)) \
19443                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
19444         $LFS mkdir -c 2 $DIR/$tdir.3 &&
19445                 error "create striped dir $tdir.3 should fail"
19446         true
19447 }
19448 run_test 417 "disable remote dir, striped dir and dir migration"
19449
19450 # Checks that the outputs of df [-i] and lfs df [-i] match
19451 #
19452 # usage: check_lfs_df <blocks | inodes> <mountpoint>
19453 check_lfs_df() {
19454         local dir=$2
19455         local inodes
19456         local df_out
19457         local lfs_df_out
19458         local count
19459         local passed=false
19460
19461         # blocks or inodes
19462         [ "$1" == "blocks" ] && inodes= || inodes="-i"
19463
19464         for count in {1..100}; do
19465                 cancel_lru_locks
19466                 sync; sleep 2
19467
19468                 # read the lines of interest
19469                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
19470                         error "df $inodes $dir | tail -n +2 failed"
19471                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
19472                         error "lfs df $inodes $dir | grep summary: failed"
19473
19474                 # skip first substrings of each output as they are different
19475                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
19476                 # compare the remaining fields of the two outputs
19477                 passed=true
19478                 for i in {1..5}; do
19479                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
19480                 done
19481                 $passed && break
19482         done
19483
19484         if ! $passed; then
19485                 df -P $inodes $dir
19486                 echo
19487                 lfs df $inodes $dir
19488                 error "df and lfs df $1 output mismatch: "      \
19489                       "df ${inodes}: ${df_out[*]}, "            \
19490                       "lfs df ${inodes}: ${lfs_df_out[*]}"
19491         fi
19492 }
19493
19494 test_418() {
19495         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19496
19497         local dir=$DIR/$tdir
19498         local numfiles=$((RANDOM % 4096 + 2))
19499         local numblocks=$((RANDOM % 256 + 1))
19500
19501         wait_delete_completed
19502         test_mkdir $dir
19503
19504         # check block output
19505         check_lfs_df blocks $dir
19506         # check inode output
19507         check_lfs_df inodes $dir
19508
19509         # create a single file and retest
19510         echo "Creating a single file and testing"
19511         createmany -o $dir/$tfile- 1 &>/dev/null ||
19512                 error "creating 1 file in $dir failed"
19513         check_lfs_df blocks $dir
19514         check_lfs_df inodes $dir
19515
19516         # create a random number of files
19517         echo "Creating $((numfiles - 1)) files and testing"
19518         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
19519                 error "creating $((numfiles - 1)) files in $dir failed"
19520
19521         # write a random number of blocks to the first test file
19522         echo "Writing $numblocks 4K blocks and testing"
19523         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
19524                 count=$numblocks &>/dev/null ||
19525                 error "dd to $dir/${tfile}-0 failed"
19526
19527         # retest
19528         check_lfs_df blocks $dir
19529         check_lfs_df inodes $dir
19530
19531         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
19532                 error "unlinking $numfiles files in $dir failed"
19533 }
19534 run_test 418 "df and lfs df outputs match"
19535
19536 test_419()
19537 {
19538         local dir=$DIR/$tdir
19539
19540         mkdir -p $dir
19541         touch $dir/file
19542
19543         cancel_lru_locks mdc
19544
19545         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
19546         $LCTL set_param fail_loc=0x1410
19547         cat $dir/file
19548         $LCTL set_param fail_loc=0
19549         rm -rf $dir
19550 }
19551 run_test 419 "Verify open file by name doesn't crash kernel"
19552
19553 test_420()
19554 {
19555         [[ $MDS1_VERSION -ge $(version_code 2.12.1) ]] ||
19556                 skip "Need MDS version at least 2.12.1"
19557
19558         local SAVE_UMASK=$(umask)
19559         local dir=$DIR/$tdir
19560         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
19561
19562         mkdir -p $dir
19563         umask 0000
19564         mkdir -m03777 $dir/testdir
19565         ls -dn $dir/testdir
19566         local dirperms=$(ls -dn $dir/testdir | awk '{print $1}')
19567         [ $dirperms == "drwxrwsrwt" ] ||
19568                 error "incorrect perms on $dir/testdir"
19569
19570         $PDSH ${uname}@localhost "PATH=$LUSTRE/tests:\$PATH; \
19571                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
19572         ls -n $dir/testdir/testfile
19573         local fileperms=$(ls -n $dir/testdir/testfile | awk '{print $1}')
19574         [ $fileperms == "-rwxr-xr-x" ] ||
19575                 error "incorrect perms on $dir/testdir/testfile"
19576
19577         umask $SAVE_UMASK
19578 }
19579 run_test 420 "clear SGID bit on non-directories for non-members"
19580
19581 prep_801() {
19582         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
19583         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
19584                 skip "Need server version at least 2.9.55"
19585
19586         start_full_debug_logging
19587 }
19588
19589 post_801() {
19590         stop_full_debug_logging
19591 }
19592
19593 barrier_stat() {
19594         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
19595                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
19596                            awk '/The barrier for/ { print $7 }')
19597                 echo $st
19598         else
19599                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
19600                 echo \'$st\'
19601         fi
19602 }
19603
19604 barrier_expired() {
19605         local expired
19606
19607         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
19608                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
19609                           awk '/will be expired/ { print $7 }')
19610         else
19611                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
19612         fi
19613
19614         echo $expired
19615 }
19616
19617 test_801a() {
19618         prep_801
19619
19620         echo "Start barrier_freeze at: $(date)"
19621         #define OBD_FAIL_BARRIER_DELAY          0x2202
19622         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
19623         do_facet mgs $LCTL barrier_freeze $FSNAME 10 &
19624
19625         sleep 2
19626         local b_status=$(barrier_stat)
19627         echo "Got barrier status at: $(date)"
19628         [ "$b_status" = "'freezing_p1'" ] ||
19629                 error "(1) unexpected barrier status $b_status"
19630
19631         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
19632         wait
19633         b_status=$(barrier_stat)
19634         [ "$b_status" = "'frozen'" ] ||
19635                 error "(2) unexpected barrier status $b_status"
19636
19637         local expired=$(barrier_expired)
19638         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
19639         sleep $((expired + 3))
19640
19641         b_status=$(barrier_stat)
19642         [ "$b_status" = "'expired'" ] ||
19643                 error "(3) unexpected barrier status $b_status"
19644
19645         do_facet mgs $LCTL barrier_freeze $FSNAME 10 ||
19646                 error "(4) fail to freeze barrier"
19647
19648         b_status=$(barrier_stat)
19649         [ "$b_status" = "'frozen'" ] ||
19650                 error "(5) unexpected barrier status $b_status"
19651
19652         echo "Start barrier_thaw at: $(date)"
19653         #define OBD_FAIL_BARRIER_DELAY          0x2202
19654         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
19655         do_facet mgs $LCTL barrier_thaw $FSNAME &
19656
19657         sleep 2
19658         b_status=$(barrier_stat)
19659         echo "Got barrier status at: $(date)"
19660         [ "$b_status" = "'thawing'" ] ||
19661                 error "(6) unexpected barrier status $b_status"
19662
19663         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
19664         wait
19665         b_status=$(barrier_stat)
19666         [ "$b_status" = "'thawed'" ] ||
19667                 error "(7) unexpected barrier status $b_status"
19668
19669         #define OBD_FAIL_BARRIER_FAILURE        0x2203
19670         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
19671         do_facet mgs $LCTL barrier_freeze $FSNAME
19672
19673         b_status=$(barrier_stat)
19674         [ "$b_status" = "'failed'" ] ||
19675                 error "(8) unexpected barrier status $b_status"
19676
19677         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
19678         do_facet mgs $LCTL barrier_thaw $FSNAME
19679
19680         post_801
19681 }
19682 run_test 801a "write barrier user interfaces and stat machine"
19683
19684 test_801b() {
19685         prep_801
19686
19687         mkdir $DIR/$tdir || error "(1) fail to mkdir"
19688         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
19689         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
19690         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
19691         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
19692
19693         cancel_lru_locks mdc
19694
19695         # 180 seconds should be long enough
19696         do_facet mgs $LCTL barrier_freeze $FSNAME 180
19697
19698         local b_status=$(barrier_stat)
19699         [ "$b_status" = "'frozen'" ] ||
19700                 error "(6) unexpected barrier status $b_status"
19701
19702         mkdir $DIR/$tdir/d0/d10 &
19703         mkdir_pid=$!
19704
19705         touch $DIR/$tdir/d1/f13 &
19706         touch_pid=$!
19707
19708         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
19709         ln_pid=$!
19710
19711         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
19712         mv_pid=$!
19713
19714         rm -f $DIR/$tdir/d4/f12 &
19715         rm_pid=$!
19716
19717         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
19718
19719         # To guarantee taht the 'stat' is not blocked
19720         b_status=$(barrier_stat)
19721         [ "$b_status" = "'frozen'" ] ||
19722                 error "(8) unexpected barrier status $b_status"
19723
19724         # let above commands to run at background
19725         sleep 5
19726
19727         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
19728         ps -p $touch_pid || error "(10) touch should be blocked"
19729         ps -p $ln_pid || error "(11) link should be blocked"
19730         ps -p $mv_pid || error "(12) rename should be blocked"
19731         ps -p $rm_pid || error "(13) unlink should be blocked"
19732
19733         b_status=$(barrier_stat)
19734         [ "$b_status" = "'frozen'" ] ||
19735                 error "(14) unexpected barrier status $b_status"
19736
19737         do_facet mgs $LCTL barrier_thaw $FSNAME
19738         b_status=$(barrier_stat)
19739         [ "$b_status" = "'thawed'" ] ||
19740                 error "(15) unexpected barrier status $b_status"
19741
19742         wait $mkdir_pid || error "(16) mkdir should succeed"
19743         wait $touch_pid || error "(17) touch should succeed"
19744         wait $ln_pid || error "(18) link should succeed"
19745         wait $mv_pid || error "(19) rename should succeed"
19746         wait $rm_pid || error "(20) unlink should succeed"
19747
19748         post_801
19749 }
19750 run_test 801b "modification will be blocked by write barrier"
19751
19752 test_801c() {
19753         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
19754
19755         prep_801
19756
19757         stop mds2 || error "(1) Fail to stop mds2"
19758
19759         do_facet mgs $LCTL barrier_freeze $FSNAME 30
19760
19761         local b_status=$(barrier_stat)
19762         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
19763                 do_facet mgs $LCTL barrier_thaw $FSNAME
19764                 error "(2) unexpected barrier status $b_status"
19765         }
19766
19767         do_facet mgs $LCTL barrier_rescan $FSNAME ||
19768                 error "(3) Fail to rescan barrier bitmap"
19769
19770         do_facet mgs $LCTL barrier_freeze $FSNAME 10
19771
19772         b_status=$(barrier_stat)
19773         [ "$b_status" = "'frozen'" ] ||
19774                 error "(4) unexpected barrier status $b_status"
19775
19776         do_facet mgs $LCTL barrier_thaw $FSNAME
19777         b_status=$(barrier_stat)
19778         [ "$b_status" = "'thawed'" ] ||
19779                 error "(5) unexpected barrier status $b_status"
19780
19781         local devname=$(mdsdevname 2)
19782
19783         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
19784
19785         do_facet mgs $LCTL barrier_rescan $FSNAME ||
19786                 error "(7) Fail to rescan barrier bitmap"
19787
19788         post_801
19789 }
19790 run_test 801c "rescan barrier bitmap"
19791
19792 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
19793 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
19794 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
19795
19796 cleanup_802() {
19797         trap 0
19798
19799         stopall
19800         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
19801         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
19802         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
19803         setupall
19804 }
19805
19806 test_802() {
19807
19808         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
19809         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
19810                 skip "Need server version at least 2.9.55"
19811
19812         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
19813
19814         mkdir $DIR/$tdir || error "(1) fail to mkdir"
19815
19816         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
19817                 error "(2) Fail to copy"
19818
19819         trap cleanup_802 EXIT
19820
19821         # sync by force before remount as readonly
19822         sync; sync_all_data; sleep 3; sync_all_data
19823
19824         stopall
19825
19826         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
19827         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
19828         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
19829
19830         echo "Mount the server as read only"
19831         setupall server_only || error "(3) Fail to start servers"
19832
19833         echo "Mount client without ro should fail"
19834         mount_client $MOUNT &&
19835                 error "(4) Mount client without 'ro' should fail"
19836
19837         echo "Mount client with ro should succeed"
19838         mount_client $MOUNT ro ||
19839                 error "(5) Mount client with 'ro' should succeed"
19840
19841         echo "Modify should be refused"
19842         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
19843
19844         echo "Read should be allowed"
19845         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
19846                 error "(7) Read should succeed under ro mode"
19847
19848         cleanup_802
19849 }
19850 run_test 802 "simulate readonly device"
19851
19852 test_803() {
19853         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
19854         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
19855                 skip "MDS needs to be newer than 2.10.54"
19856
19857         mkdir -p $DIR/$tdir
19858         # Create some objects on all MDTs to trigger related logs objects
19859         for idx in $(seq $MDSCOUNT); do
19860                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
19861                         $DIR/$tdir/dir${idx} ||
19862                         error "Fail to create $DIR/$tdir/dir${idx}"
19863         done
19864
19865         sync; sleep 3
19866         wait_delete_completed # ensure old test cleanups are finished
19867         echo "before create:"
19868         $LFS df -i $MOUNT
19869         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
19870
19871         for i in {1..10}; do
19872                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
19873                         error "Fail to create $DIR/$tdir/foo$i"
19874         done
19875
19876         sync; sleep 3
19877         echo "after create:"
19878         $LFS df -i $MOUNT
19879         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
19880
19881         # allow for an llog to be cleaned up during the test
19882         [ $after_used -ge $((before_used + 10 - 1)) ] ||
19883                 error "before ($before_used) + 10 > after ($after_used)"
19884
19885         for i in {1..10}; do
19886                 rm -rf $DIR/$tdir/foo$i ||
19887                         error "Fail to remove $DIR/$tdir/foo$i"
19888         done
19889
19890         sleep 3 # avoid MDT return cached statfs
19891         wait_delete_completed
19892         echo "after unlink:"
19893         $LFS df -i $MOUNT
19894         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
19895
19896         # allow for an llog to be created during the test
19897         [ $after_used -le $((before_used + 1)) ] ||
19898                 error "after ($after_used) > before ($before_used) + 1"
19899 }
19900 run_test 803 "verify agent object for remote object"
19901
19902 test_804() {
19903         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
19904         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
19905                 skip "MDS needs to be newer than 2.10.54"
19906         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
19907
19908         mkdir -p $DIR/$tdir
19909         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
19910                 error "Fail to create $DIR/$tdir/dir0"
19911
19912         local fid=$($LFS path2fid $DIR/$tdir/dir0)
19913         local dev=$(mdsdevname 2)
19914
19915         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
19916                 grep ${fid} || error "NOT found agent entry for dir0"
19917
19918         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
19919                 error "Fail to create $DIR/$tdir/dir1"
19920
19921         touch $DIR/$tdir/dir1/foo0 ||
19922                 error "Fail to create $DIR/$tdir/dir1/foo0"
19923         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
19924         local rc=0
19925
19926         for idx in $(seq $MDSCOUNT); do
19927                 dev=$(mdsdevname $idx)
19928                 do_facet mds${idx} \
19929                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
19930                         grep ${fid} && rc=$idx
19931         done
19932
19933         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
19934                 error "Fail to rename foo0 to foo1"
19935         if [ $rc -eq 0 ]; then
19936                 for idx in $(seq $MDSCOUNT); do
19937                         dev=$(mdsdevname $idx)
19938                         do_facet mds${idx} \
19939                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
19940                         grep ${fid} && rc=$idx
19941                 done
19942         fi
19943
19944         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
19945                 error "Fail to rename foo1 to foo2"
19946         if [ $rc -eq 0 ]; then
19947                 for idx in $(seq $MDSCOUNT); do
19948                         dev=$(mdsdevname $idx)
19949                         do_facet mds${idx} \
19950                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
19951                         grep ${fid} && rc=$idx
19952                 done
19953         fi
19954
19955         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
19956
19957         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
19958                 error "Fail to link to $DIR/$tdir/dir1/foo2"
19959         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
19960                 error "Fail to rename foo2 to foo0"
19961         unlink $DIR/$tdir/dir1/foo0 ||
19962                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
19963         rm -rf $DIR/$tdir/dir0 ||
19964                 error "Fail to rm $DIR/$tdir/dir0"
19965
19966         for idx in $(seq $MDSCOUNT); do
19967                 dev=$(mdsdevname $idx)
19968                 rc=0
19969
19970                 stop mds${idx}
19971                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
19972                         rc=$?
19973                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
19974                         error "mount mds$idx failed"
19975                 df $MOUNT > /dev/null 2>&1
19976
19977                 # e2fsck should not return error
19978                 [ $rc -eq 0 ] ||
19979                         error "e2fsck detected error on MDT${idx}: rc=$rc"
19980         done
19981 }
19982 run_test 804 "verify agent entry for remote entry"
19983
19984 cleanup_805() {
19985         do_facet $SINGLEMDS zfs set quota=$old $fsset
19986         unlinkmany $DIR/$tdir/f- 1000000
19987         trap 0
19988 }
19989
19990 test_805() {
19991         local zfs_version=$(do_node $SINGLEMDS cat /sys/module/zfs/version)
19992         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
19993         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
19994                 skip "netfree not implemented before 0.7"
19995         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
19996                 skip "Need MDS version at least 2.10.57"
19997
19998         local fsset
19999         local freekb
20000         local usedkb
20001         local old
20002         local quota
20003         local pref="osd-zfs.lustre-MDT0000."
20004
20005         # limit available space on MDS dataset to meet nospace issue
20006         # quickly. then ZFS 0.7.2 can use reserved space if asked
20007         # properly (using netfree flag in osd_declare_destroy()
20008         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
20009         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
20010                 gawk '{print $3}')
20011         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
20012         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
20013         let "usedkb=usedkb-freekb"
20014         let "freekb=freekb/2"
20015         if let "freekb > 5000"; then
20016                 let "freekb=5000"
20017         fi
20018         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
20019         trap cleanup_805 EXIT
20020         mkdir $DIR/$tdir
20021         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
20022         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
20023         rm -rf $DIR/$tdir || error "not able to remove"
20024         do_facet $SINGLEMDS zfs set quota=$old $fsset
20025         trap 0
20026 }
20027 run_test 805 "ZFS can remove from full fs"
20028
20029 # Size-on-MDS test
20030 check_lsom_data()
20031 {
20032         local file=$1
20033         local size=$($LFS getsom -s $file)
20034         local expect=$(stat -c %s $file)
20035
20036         [[ $size == $expect ]] ||
20037                 error "$file expected size: $expect, got: $size"
20038
20039         local blocks=$($LFS getsom -b $file)
20040         expect=$(stat -c %b $file)
20041         [[ $blocks == $expect ]] ||
20042                 error "$file expected blocks: $expect, got: $blocks"
20043 }
20044
20045 check_lsom_size()
20046 {
20047         local size=$($LFS getsom -s $1)
20048         local expect=$2
20049
20050         [[ $size == $expect ]] ||
20051                 error "$file expected size: $expect, got: $size"
20052 }
20053
20054 test_806() {
20055         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20056                 skip "Need MDS version at least 2.11.52"
20057
20058         local bs=1048576
20059
20060         touch $DIR/$tfile || error "touch $tfile failed"
20061
20062         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
20063         save_lustre_params client "llite.*.xattr_cache" > $save
20064         lctl set_param llite.*.xattr_cache=0
20065         stack_trap "restore_lustre_params < $save" EXIT
20066
20067         # single-threaded write
20068         echo "Test SOM for single-threaded write"
20069         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
20070                 error "write $tfile failed"
20071         check_lsom_size $DIR/$tfile $bs
20072
20073         local num=32
20074         local size=$(($num * $bs))
20075         local offset=0
20076         local i
20077
20078         echo "Test SOM for single client multi-threaded($num) write"
20079         $TRUNCATE $DIR/$tfile 0
20080         for ((i = 0; i < $num; i++)); do
20081                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
20082                 local pids[$i]=$!
20083                 offset=$((offset + $bs))
20084         done
20085         for (( i=0; i < $num; i++ )); do
20086                 wait ${pids[$i]}
20087         done
20088         check_lsom_size $DIR/$tfile $size
20089
20090         $TRUNCATE $DIR/$tfile 0
20091         for ((i = 0; i < $num; i++)); do
20092                 offset=$((offset - $bs))
20093                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
20094                 local pids[$i]=$!
20095         done
20096         for (( i=0; i < $num; i++ )); do
20097                 wait ${pids[$i]}
20098         done
20099         check_lsom_size $DIR/$tfile $size
20100
20101         # multi-client wirtes
20102         num=$(get_node_count ${CLIENTS//,/ })
20103         size=$(($num * $bs))
20104         offset=0
20105         i=0
20106
20107         echo "Test SOM for multi-client ($num) writes"
20108         $TRUNCATE $DIR/$tfile 0
20109         for client in ${CLIENTS//,/ }; do
20110                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
20111                 local pids[$i]=$!
20112                 i=$((i + 1))
20113                 offset=$((offset + $bs))
20114         done
20115         for (( i=0; i < $num; i++ )); do
20116                 wait ${pids[$i]}
20117         done
20118         check_lsom_size $DIR/$tfile $offset
20119
20120         i=0
20121         $TRUNCATE $DIR/$tfile 0
20122         for client in ${CLIENTS//,/ }; do
20123                 offset=$((offset - $bs))
20124                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
20125                 local pids[$i]=$!
20126                 i=$((i + 1))
20127         done
20128         for (( i=0; i < $num; i++ )); do
20129                 wait ${pids[$i]}
20130         done
20131         check_lsom_size $DIR/$tfile $size
20132
20133         # verify truncate
20134         echo "Test SOM for truncate"
20135         $TRUNCATE $DIR/$tfile 1048576
20136         check_lsom_size $DIR/$tfile 1048576
20137         $TRUNCATE $DIR/$tfile 1234
20138         check_lsom_size $DIR/$tfile 1234
20139
20140         # verify SOM blocks count
20141         echo "Verify SOM block count"
20142         $TRUNCATE $DIR/$tfile 0
20143         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
20144                 error "failed to write file $tfile"
20145         check_lsom_data $DIR/$tfile
20146 }
20147 run_test 806 "Verify Lazy Size on MDS"
20148
20149 test_807() {
20150         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20151                 skip "Need MDS version at least 2.11.52"
20152
20153         # Registration step
20154         changelog_register || error "changelog_register failed"
20155         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
20156         changelog_users $SINGLEMDS | grep -q $cl_user ||
20157                 error "User $cl_user not found in changelog_users"
20158
20159         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
20160         save_lustre_params client "llite.*.xattr_cache" > $save
20161         lctl set_param llite.*.xattr_cache=0
20162         stack_trap "restore_lustre_params < $save" EXIT
20163
20164         rm -rf $DIR/$tdir || error "rm $tdir failed"
20165         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
20166         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
20167         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
20168         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
20169                 error "truncate $tdir/trunc failed"
20170
20171         local bs=1048576
20172         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 ||
20173                 error "write $tfile failed"
20174
20175         # multi-client wirtes
20176         local num=$(get_node_count ${CLIENTS//,/ })
20177         local offset=0
20178         local i=0
20179
20180         echo "Test SOM for multi-client ($num) writes"
20181         touch $DIR/$tfile || error "touch $tfile failed"
20182         $TRUNCATE $DIR/$tfile 0
20183         for client in ${CLIENTS//,/ }; do
20184                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
20185                 local pids[$i]=$!
20186                 i=$((i + 1))
20187                 offset=$((offset + $bs))
20188         done
20189         for (( i=0; i < $num; i++ )); do
20190                 wait ${pids[$i]}
20191         done
20192
20193         sleep 5
20194         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
20195         check_lsom_data $DIR/$tdir/trunc
20196         check_lsom_data $DIR/$tdir/single_dd
20197         check_lsom_data $DIR/$tfile
20198
20199         rm -rf $DIR/$tdir
20200         # Deregistration step
20201         changelog_deregister || error "changelog_deregister failed"
20202 }
20203 run_test 807 "verify LSOM syncing tool"
20204
20205 check_som_nologged()
20206 {
20207         local lines=$($LFS changelog $FSNAME-MDT0000 |
20208                 grep 'x=trusted.som' | wc -l)
20209         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
20210 }
20211
20212 test_808() {
20213         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
20214                 skip "Need MDS version at least 2.11.55"
20215
20216         # Registration step
20217         changelog_register || error "changelog_register failed"
20218
20219         touch $DIR/$tfile || error "touch $tfile failed"
20220         check_som_nologged
20221
20222         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
20223                 error "write $tfile failed"
20224         check_som_nologged
20225
20226         $TRUNCATE $DIR/$tfile 1234
20227         check_som_nologged
20228
20229         $TRUNCATE $DIR/$tfile 1048576
20230         check_som_nologged
20231
20232         # Deregistration step
20233         changelog_deregister || error "changelog_deregister failed"
20234 }
20235 run_test 808 "Check trusted.som xattr not logged in Changelogs"
20236
20237 check_som_nodata()
20238 {
20239         $LFS getsom $1
20240         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
20241 }
20242
20243 test_809() {
20244         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
20245                 skip "Need MDS version at least 2.11.56"
20246
20247         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
20248                 error "failed to create DoM-only file $DIR/$tfile"
20249         touch $DIR/$tfile || error "touch $tfile failed"
20250         check_som_nodata $DIR/$tfile
20251
20252         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
20253                 error "write $tfile failed"
20254         check_som_nodata $DIR/$tfile
20255
20256         $TRUNCATE $DIR/$tfile 1234
20257         check_som_nodata $DIR/$tfile
20258
20259         $TRUNCATE $DIR/$tfile 4097
20260         check_som_nodata $DIR/$file
20261 }
20262 run_test 809 "Verify no SOM xattr store for DoM-only files"
20263
20264 test_810() {
20265         local ORIG
20266         local CSUM
20267
20268         # t10 seem to dislike partial pages
20269         lctl set_param osc.*.checksum_type=adler
20270         lctl set_param fail_loc=0x411
20271         dd if=/dev/urandom of=$DIR/$tfile bs=10240 count=2
20272         ORIG=$(md5sum $DIR/$tfile)
20273         lctl set_param ldlm.namespaces.*osc*.lru_size=clear
20274         CSUM=$(md5sum $DIR/$tfile)
20275         set_checksum_type adler
20276         if [ "$ORIG" != "$CSUM" ]; then
20277                 error "$ORIG != $CSUM"
20278         fi
20279 }
20280 run_test 810 "partial page writes on ZFS (LU-11663)"
20281
20282 test_811() {
20283         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.56) ] &&
20284                 skip "Need MDS version at least 2.11.56"
20285
20286         #define OBD_FAIL_MDS_ORPHAN_DELETE      0x165
20287         do_facet mds1 $LCTL set_param fail_loc=0x165
20288         $MULTIOP $DIR/$tfile Ouc || error "multiop failed"
20289
20290         stop mds1
20291         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
20292
20293         sleep 5
20294         [[ $(do_facet mds1 pgrep orph_.*-MDD | wc -l) -eq 0 ]] ||
20295                 error "MDD orphan cleanup thread not quit"
20296 }
20297 run_test 811 "orphan name stub can be cleaned up in startup"
20298
20299 test_812() {
20300         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
20301                 skip "OST < 2.12.51 doesn't support this fail_loc"
20302
20303         $LFS setstripe -c 1 -i 0 $DIR/$tfile
20304         # ensure ost1 is connected
20305         stat $DIR/$tfile >/dev/null || error "can't stat"
20306         wait_osc_import_state client ost1 FULL
20307         # no locks, no reqs to let the connection idle
20308         cancel_lru_locks osc
20309
20310         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
20311 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
20312         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
20313         wait_osc_import_state client ost1 CONNECTING
20314         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
20315
20316         stat $DIR/$tfile >/dev/null || error "can't stat file"
20317 }
20318 run_test 812 "do not drop reqs generated when imp is going to idle (LU-11951)"
20319
20320 test_814()
20321 {
20322         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
20323         echo -n y >> $DIR/$tfile
20324         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
20325         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
20326 }
20327 run_test 814 "sparse cp works as expected (LU-12361)"
20328
20329 #
20330 # tests that do cleanup/setup should be run at the end
20331 #
20332
20333 test_900() {
20334         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20335         local ls
20336
20337         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
20338         $LCTL set_param fail_loc=0x903
20339
20340         cancel_lru_locks MGC
20341
20342         FAIL_ON_ERROR=true cleanup
20343         FAIL_ON_ERROR=true setup
20344 }
20345 run_test 900 "umount should not race with any mgc requeue thread"
20346
20347 complete $SECONDS
20348 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
20349 check_and_cleanup_lustre
20350 if [ "$I_MOUNTED" != "yes" ]; then
20351         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
20352 fi
20353 exit_status