Whamcloud - gitweb
LU-11376 lmv: new foreign LMV format
[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
12 ALWAYS_EXCEPT="$SANITY_EXCEPT  42a     42b     42c"
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 # Check Grants after these tests
24 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c 64d"
25
26 # skip the grant tests for ARM until they are fixed
27 if [[ $(uname -m) = aarch64 ]]; then
28         # bug number:    LU-11596 (all below)
29         ALWAYS_EXCEPT+=" $GRANT_CHECK_LIST"
30         # bug number:    LU-11671 LU-11594 LU-11667 LU-11729
31         ALWAYS_EXCEPT+=" 45       103a      317      810"
32 fi
33
34 SRCDIR=$(cd $(dirname $0); echo $PWD)
35 export PATH=$PATH:/sbin
36
37 TMP=${TMP:-/tmp}
38 OSC=${OSC:-"osc"}
39
40 CC=${CC:-cc}
41 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
42 CREATETEST=${CREATETEST:-createtest}
43 LFS=${LFS:-lfs}
44 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
45 LCTL=${LCTL:-lctl}
46 OPENFILE=${OPENFILE:-openfile}
47 OPENUNLINK=${OPENUNLINK:-openunlink}
48 export MULTIOP=${MULTIOP:-multiop}
49 READS=${READS:-"reads"}
50 MUNLINK=${MUNLINK:-munlink}
51 SOCKETSERVER=${SOCKETSERVER:-socketserver}
52 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
53 MEMHOG=${MEMHOG:-memhog}
54 DIRECTIO=${DIRECTIO:-directio}
55 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
56 DEF_STRIPE_COUNT=-1
57 CHECK_GRANT=${CHECK_GRANT:-"yes"}
58 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
59 export PARALLEL=${PARALLEL:-"no"}
60
61 export NAME=${NAME:-local}
62
63 SAVE_PWD=$PWD
64
65 CLEANUP=${CLEANUP:-:}
66 SETUP=${SETUP:-:}
67 TRACE=${TRACE:-""}
68 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
69 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
70 . $LUSTRE/tests/test-framework.sh
71 init_test_env $@
72 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
73
74 init_logging
75
76 #                                  5          12          (min)"
77 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 300o"
78
79 if [ "$mds1_FSTYPE" = "zfs" ]; then
80         # bug number for skipped test: LU-1957
81         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  180"
82         #                                               13    (min)"
83         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
84 fi
85
86 # Get the SLES distro version
87 #
88 # Returns a version string that should only be used in comparing
89 # strings returned by version_code()
90 sles_version_code()
91 {
92         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
93
94         # All SuSE Linux versions have one decimal. version_code expects two
95         local sles_version=$version.0
96         version_code $sles_version
97 }
98
99 # Check if we are running on Ubuntu or SLES so we can make decisions on
100 # what tests to run
101 if [ -r /etc/SuSE-release ]; then
102         sles_version=$(sles_version_code)
103         [ $sles_version -lt $(version_code 11.4.0) ] &&
104                 # bug number for skipped test: LU-4341
105                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
106         [ $sles_version -lt $(version_code 12.0.0) ] &&
107                 # bug number for skipped test: LU-3703
108                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
109 elif [ -r /etc/os-release ]; then
110         if grep -qi ubuntu /etc/os-release; then
111                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
112                                                 -e 's/^VERSION=//p' \
113                                                 /etc/os-release |
114                                                 awk '{ print $1 }'))
115
116                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
117                         # bug number for skipped test:
118                         #                LU-10334 LU-10366
119                         ALWAYS_EXCEPT+=" 103a     410"
120                 fi
121         fi
122 fi
123
124 FAIL_ON_ERROR=false
125
126 cleanup() {
127         echo -n "cln.."
128         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
129         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
130 }
131 setup() {
132         echo -n "mnt.."
133         load_modules
134         setupall || exit 10
135         echo "done"
136 }
137
138 check_swap_layouts_support()
139 {
140         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
141                 skip "Does not support layout lock."
142 }
143
144 check_and_setup_lustre
145 DIR=${DIR:-$MOUNT}
146 assert_DIR
147
148 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
149
150 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
151 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
152 rm -rf $DIR/[Rdfs][0-9]*
153
154 # $RUNAS_ID may get set incorrectly somewhere else
155 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
156         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
157
158 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
159
160 build_test_filter
161
162 if [ "${ONLY}" = "MOUNT" ] ; then
163         echo "Lustre is up, please go on"
164         exit
165 fi
166
167 echo "preparing for tests involving mounts"
168 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
169 touch $EXT2_DEV
170 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
171 echo # add a newline after mke2fs.
172
173 umask 077
174
175 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
176 lctl set_param debug=-1 2> /dev/null || true
177 test_0a() {
178         touch $DIR/$tfile
179         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
180         rm $DIR/$tfile
181         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
182 }
183 run_test 0a "touch; rm ====================="
184
185 test_0b() {
186         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
187         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
188 }
189 run_test 0b "chmod 0755 $DIR ============================="
190
191 test_0c() {
192         $LCTL get_param mdc.*.import | grep "state: FULL" ||
193                 error "import not FULL"
194         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
195                 error "bad target"
196 }
197 run_test 0c "check import proc"
198
199 test_0d() { # LU-3397
200         [ $MGS_VERSION -lt $(version_code 2.10.57) ] &&
201                 skip "proc exports not supported before 2.10.57"
202
203         local mgs_exp="mgs.MGS.exports"
204         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
205         local exp_client_nid
206         local exp_client_version
207         local exp_val
208         local imp_val
209         local temp_imp=$DIR/$tfile.import
210         local temp_exp=$DIR/$tfile.export
211
212         # save mgc import file to $temp_imp
213         $LCTL get_param mgc.*.import | tee $temp_imp
214         # Check if client uuid is found in MGS export
215         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
216                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
217                         $client_uuid ] &&
218                         break;
219         done
220         # save mgs export file to $temp_exp
221         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
222
223         # Compare the value of field "connect_flags"
224         imp_val=$(grep "connect_flags" $temp_imp)
225         exp_val=$(grep "connect_flags" $temp_exp)
226         [ "$exp_val" == "$imp_val" ] ||
227                 error "export flags '$exp_val' != import flags '$imp_val'"
228
229         # Compare the value of client version
230         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
231         exp_val=$(version_code $exp_client_version)
232         imp_val=$CLIENT_VERSION
233         [ "$exp_val" == "$imp_val" ] ||
234                 error "export client version '$exp_val' != '$imp_val'"
235 }
236 run_test 0d "check export proc ============================="
237
238 test_1() {
239         test_mkdir $DIR/$tdir
240         test_mkdir $DIR/$tdir/d2
241         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
242         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
243         rmdir $DIR/$tdir/d2
244         rmdir $DIR/$tdir
245         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
246 }
247 run_test 1 "mkdir; remkdir; rmdir"
248
249 test_2() {
250         test_mkdir $DIR/$tdir
251         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
252         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
253         rm -r $DIR/$tdir
254         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
255 }
256 run_test 2 "mkdir; touch; rmdir; check file"
257
258 test_3() {
259         test_mkdir $DIR/$tdir
260         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
261         touch $DIR/$tdir/$tfile
262         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
263         rm -r $DIR/$tdir
264         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
265 }
266 run_test 3 "mkdir; touch; rmdir; check dir"
267
268 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
269 test_4() {
270         test_mkdir -i 1 $DIR/$tdir
271
272         touch $DIR/$tdir/$tfile ||
273                 error "Create file under remote directory failed"
274
275         rmdir $DIR/$tdir &&
276                 error "Expect error removing in-use dir $DIR/$tdir"
277
278         test -d $DIR/$tdir || error "Remote directory disappeared"
279
280         rm -rf $DIR/$tdir || error "remove remote dir error"
281 }
282 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
283
284 test_5() {
285         test_mkdir $DIR/$tdir
286         test_mkdir $DIR/$tdir/d2
287         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
288         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
289         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
290 }
291 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
292
293 test_6a() {
294         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
295         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
296         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
297                 error "$tfile does not have perm 0666 or UID $UID"
298         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
299         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
300                 error "$tfile should be 0666 and owned by UID $UID"
301 }
302 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
303
304 test_6c() {
305         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
306
307         touch $DIR/$tfile
308         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
309         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
310                 error "$tfile should be owned by UID $RUNAS_ID"
311         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
312         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
313                 error "$tfile should be owned by UID $RUNAS_ID"
314 }
315 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
316
317 test_6e() {
318         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
319
320         touch $DIR/$tfile
321         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
322         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
323                 error "$tfile should be owned by GID $UID"
324         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
325         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
326                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
327 }
328 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
329
330 test_6g() {
331         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
332
333         test_mkdir $DIR/$tdir
334         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
335         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
336         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
337         test_mkdir $DIR/$tdir/d/subdir
338         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
339                 error "$tdir/d/subdir should be GID $RUNAS_GID"
340         if [[ $MDSCOUNT -gt 1 ]]; then
341                 # check remote dir sgid inherite
342                 $LFS mkdir -i 0 $DIR/$tdir.local ||
343                         error "mkdir $tdir.local failed"
344                 chmod g+s $DIR/$tdir.local ||
345                         error "chmod $tdir.local failed"
346                 chgrp $RUNAS_GID $DIR/$tdir.local ||
347                         error "chgrp $tdir.local failed"
348                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
349                         error "mkdir $tdir.remote failed"
350                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
351                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
352                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
353                         error "$tdir.remote should be mode 02755"
354         fi
355 }
356 run_test 6g "verify new dir in sgid dir inherits group"
357
358 test_6h() { # bug 7331
359         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
360
361         touch $DIR/$tfile || error "touch failed"
362         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
363         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
364                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
365         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
366                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
367 }
368 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
369
370 test_7a() {
371         test_mkdir $DIR/$tdir
372         $MCREATE $DIR/$tdir/$tfile
373         chmod 0666 $DIR/$tdir/$tfile
374         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
375                 error "$tdir/$tfile should be mode 0666"
376 }
377 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
378
379 test_7b() {
380         if [ ! -d $DIR/$tdir ]; then
381                 test_mkdir $DIR/$tdir
382         fi
383         $MCREATE $DIR/$tdir/$tfile
384         echo -n foo > $DIR/$tdir/$tfile
385         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
386         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
387 }
388 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
389
390 test_8() {
391         test_mkdir $DIR/$tdir
392         touch $DIR/$tdir/$tfile
393         chmod 0666 $DIR/$tdir/$tfile
394         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
395                 error "$tfile mode not 0666"
396 }
397 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
398
399 test_9() {
400         test_mkdir $DIR/$tdir
401         test_mkdir $DIR/$tdir/d2
402         test_mkdir $DIR/$tdir/d2/d3
403         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
404 }
405 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
406
407 test_10() {
408         test_mkdir $DIR/$tdir
409         test_mkdir $DIR/$tdir/d2
410         touch $DIR/$tdir/d2/$tfile
411         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
412                 error "$tdir/d2/$tfile not a file"
413 }
414 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
415
416 test_11() {
417         test_mkdir $DIR/$tdir
418         test_mkdir $DIR/$tdir/d2
419         chmod 0666 $DIR/$tdir/d2
420         chmod 0705 $DIR/$tdir/d2
421         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
422                 error "$tdir/d2 mode not 0705"
423 }
424 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
425
426 test_12() {
427         test_mkdir $DIR/$tdir
428         touch $DIR/$tdir/$tfile
429         chmod 0666 $DIR/$tdir/$tfile
430         chmod 0654 $DIR/$tdir/$tfile
431         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
432                 error "$tdir/d2 mode not 0654"
433 }
434 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
435
436 test_13() {
437         test_mkdir $DIR/$tdir
438         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
439         >  $DIR/$tdir/$tfile
440         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
441                 error "$tdir/$tfile size not 0 after truncate"
442 }
443 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
444
445 test_14() {
446         test_mkdir $DIR/$tdir
447         touch $DIR/$tdir/$tfile
448         rm $DIR/$tdir/$tfile
449         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
450 }
451 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
452
453 test_15() {
454         test_mkdir $DIR/$tdir
455         touch $DIR/$tdir/$tfile
456         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
457         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
458                 error "$tdir/${tfile_2} not a file after rename"
459         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
460 }
461 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
462
463 test_16() {
464         test_mkdir $DIR/$tdir
465         touch $DIR/$tdir/$tfile
466         rm -rf $DIR/$tdir/$tfile
467         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
468 }
469 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
470
471 test_17a() {
472         test_mkdir $DIR/$tdir
473         touch $DIR/$tdir/$tfile
474         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
475         ls -l $DIR/$tdir
476         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
477                 error "$tdir/l-exist not a symlink"
478         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
479                 error "$tdir/l-exist not referencing a file"
480         rm -f $DIR/$tdir/l-exist
481         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
482 }
483 run_test 17a "symlinks: create, remove (real)"
484
485 test_17b() {
486         test_mkdir $DIR/$tdir
487         ln -s no-such-file $DIR/$tdir/l-dangle
488         ls -l $DIR/$tdir
489         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
490                 error "$tdir/l-dangle not referencing no-such-file"
491         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
492                 error "$tdir/l-dangle not referencing non-existent file"
493         rm -f $DIR/$tdir/l-dangle
494         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
495 }
496 run_test 17b "symlinks: create, remove (dangling)"
497
498 test_17c() { # bug 3440 - don't save failed open RPC for replay
499         test_mkdir $DIR/$tdir
500         ln -s foo $DIR/$tdir/$tfile
501         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
502 }
503 run_test 17c "symlinks: open dangling (should return error)"
504
505 test_17d() {
506         test_mkdir $DIR/$tdir
507         ln -s foo $DIR/$tdir/$tfile
508         touch $DIR/$tdir/$tfile || error "creating to new symlink"
509 }
510 run_test 17d "symlinks: create dangling"
511
512 test_17e() {
513         test_mkdir $DIR/$tdir
514         local foo=$DIR/$tdir/$tfile
515         ln -s $foo $foo || error "create symlink failed"
516         ls -l $foo || error "ls -l failed"
517         ls $foo && error "ls not failed" || true
518 }
519 run_test 17e "symlinks: create recursive symlink (should return error)"
520
521 test_17f() {
522         test_mkdir $DIR/$tdir
523         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
524         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
525         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
526         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
527         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
528         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
529         ls -l  $DIR/$tdir
530 }
531 run_test 17f "symlinks: long and very long symlink name"
532
533 # str_repeat(S, N) generate a string that is string S repeated N times
534 str_repeat() {
535         local s=$1
536         local n=$2
537         local ret=''
538         while [ $((n -= 1)) -ge 0 ]; do
539                 ret=$ret$s
540         done
541         echo $ret
542 }
543
544 # Long symlinks and LU-2241
545 test_17g() {
546         test_mkdir $DIR/$tdir
547         local TESTS="59 60 61 4094 4095"
548
549         # Fix for inode size boundary in 2.1.4
550         [ $MDS1_VERSION -lt $(version_code 2.1.4) ] &&
551                 TESTS="4094 4095"
552
553         # Patch not applied to 2.2 or 2.3 branches
554         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
555         [ $MDS1_VERSION -le $(version_code 2.3.55) ] &&
556                 TESTS="4094 4095"
557
558         # skip long symlink name for rhel6.5.
559         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
560         grep -q '6.5' /etc/redhat-release &>/dev/null &&
561                 TESTS="59 60 61 4062 4063"
562
563         for i in $TESTS; do
564                 local SYMNAME=$(str_repeat 'x' $i)
565                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
566                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
567         done
568 }
569 run_test 17g "symlinks: really long symlink name and inode boundaries"
570
571 test_17h() { #bug 17378
572         [ $PARALLEL == "yes" ] && skip "skip parallel run"
573         remote_mds_nodsh && skip "remote MDS with nodsh"
574
575         local mdt_idx
576
577         test_mkdir $DIR/$tdir
578         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
579         $LFS setstripe -c -1 $DIR/$tdir
580         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
581         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
582         touch $DIR/$tdir/$tfile || true
583 }
584 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
585
586 test_17i() { #bug 20018
587         [ $PARALLEL == "yes" ] && skip "skip parallel run"
588         remote_mds_nodsh && skip "remote MDS with nodsh"
589
590         local foo=$DIR/$tdir/$tfile
591         local mdt_idx
592
593         test_mkdir -c1 $DIR/$tdir
594         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
595         ln -s $foo $foo || error "create symlink failed"
596 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
597         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
598         ls -l $foo && error "error not detected"
599         return 0
600 }
601 run_test 17i "don't panic on short symlink (should return error)"
602
603 test_17k() { #bug 22301
604         [ $PARALLEL == "yes" ] && skip "skip parallel run"
605         [[ -z "$(which rsync 2>/dev/null)" ]] &&
606                 skip "no rsync command"
607         rsync --help | grep -q xattr ||
608                 skip_env "$(rsync --version | head -n1) does not support xattrs"
609         test_mkdir $DIR/$tdir
610         test_mkdir $DIR/$tdir.new
611         touch $DIR/$tdir/$tfile
612         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
613         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
614                 error "rsync failed with xattrs enabled"
615 }
616 run_test 17k "symlinks: rsync with xattrs enabled"
617
618 test_17l() { # LU-279
619         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
620                 skip "no getfattr command"
621
622         test_mkdir $DIR/$tdir
623         touch $DIR/$tdir/$tfile
624         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
625         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
626                 # -h to not follow symlinks. -m '' to list all the xattrs.
627                 # grep to remove first line: '# file: $path'.
628                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
629                 do
630                         lgetxattr_size_check $path $xattr ||
631                                 error "lgetxattr_size_check $path $xattr failed"
632                 done
633         done
634 }
635 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
636
637 # LU-1540
638 test_17m() {
639         [ $PARALLEL == "yes" ] && skip "skip parallel run"
640         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
641         remote_mds_nodsh && skip "remote MDS with nodsh"
642         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
643         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
644                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
645
646         local short_sym="0123456789"
647         local wdir=$DIR/$tdir
648         local i
649
650         test_mkdir $wdir
651         long_sym=$short_sym
652         # create a long symlink file
653         for ((i = 0; i < 4; ++i)); do
654                 long_sym=${long_sym}${long_sym}
655         done
656
657         echo "create 512 short and long symlink files under $wdir"
658         for ((i = 0; i < 256; ++i)); do
659                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
660                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
661         done
662
663         echo "erase them"
664         rm -f $wdir/*
665         sync
666         wait_delete_completed
667
668         echo "recreate the 512 symlink files with a shorter string"
669         for ((i = 0; i < 512; ++i)); do
670                 # rewrite the symlink file with a shorter string
671                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
672                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
673         done
674
675         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
676         local devname=$(mdsdevname $mds_index)
677
678         echo "stop and checking mds${mds_index}:"
679         # e2fsck should not return error
680         stop mds${mds_index}
681         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
682         rc=$?
683
684         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
685                 error "start mds${mds_index} failed"
686         df $MOUNT > /dev/null 2>&1
687         [ $rc -eq 0 ] ||
688                 error "e2fsck detected error for short/long symlink: rc=$rc"
689         rm -f $wdir/*
690 }
691 run_test 17m "run e2fsck against MDT which contains short/long symlink"
692
693 check_fs_consistency_17n() {
694         local mdt_index
695         local rc=0
696
697         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
698         # so it only check MDT1/MDT2 instead of all of MDTs.
699         for mdt_index in 1 2; do
700                 local devname=$(mdsdevname $mdt_index)
701                 # e2fsck should not return error
702                 stop mds${mdt_index}
703                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
704                         rc=$((rc + $?))
705
706                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
707                         error "mount mds$mdt_index failed"
708                 df $MOUNT > /dev/null 2>&1
709         done
710         return $rc
711 }
712
713 test_17n() {
714         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
715         [ $PARALLEL == "yes" ] && skip "skip parallel run"
716         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
717         remote_mds_nodsh && skip "remote MDS with nodsh"
718         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
719         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
720                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
721
722         local i
723
724         test_mkdir $DIR/$tdir
725         for ((i=0; i<10; i++)); do
726                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
727                         error "create remote dir error $i"
728                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
729                         error "create files under remote dir failed $i"
730         done
731
732         check_fs_consistency_17n ||
733                 error "e2fsck report error after create files under remote dir"
734
735         for ((i = 0; i < 10; i++)); do
736                 rm -rf $DIR/$tdir/remote_dir_${i} ||
737                         error "destroy remote dir error $i"
738         done
739
740         check_fs_consistency_17n ||
741                 error "e2fsck report error after unlink files under remote dir"
742
743         [ $MDS1_VERSION -lt $(version_code 2.4.50) ] &&
744                 skip "lustre < 2.4.50 does not support migrate mv"
745
746         for ((i = 0; i < 10; i++)); do
747                 mkdir -p $DIR/$tdir/remote_dir_${i}
748                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
749                         error "create files under remote dir failed $i"
750                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
751                         error "migrate remote dir error $i"
752         done
753         check_fs_consistency_17n || error "e2fsck report error after migration"
754
755         for ((i = 0; i < 10; i++)); do
756                 rm -rf $DIR/$tdir/remote_dir_${i} ||
757                         error "destroy remote dir error $i"
758         done
759
760         check_fs_consistency_17n || error "e2fsck report error after unlink"
761 }
762 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
763
764 test_17o() {
765         remote_mds_nodsh && skip "remote MDS with nodsh"
766         [ $MDS1_VERSION -lt $(version_code 2.3.64) ] &&
767                 skip "Need MDS version at least 2.3.64"
768
769         local wdir=$DIR/${tdir}o
770         local mdt_index
771         local rc=0
772
773         test_mkdir $wdir
774         touch $wdir/$tfile
775         mdt_index=$($LFS getstripe -m $wdir/$tfile)
776         mdt_index=$((mdt_index + 1))
777
778         cancel_lru_locks mdc
779         #fail mds will wait the failover finish then set
780         #following fail_loc to avoid interfer the recovery process.
781         fail mds${mdt_index}
782
783         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
784         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
785         ls -l $wdir/$tfile && rc=1
786         do_facet mds${mdt_index} lctl set_param fail_loc=0
787         [[ $rc -eq 0 ]] || error "stat file should fail"
788 }
789 run_test 17o "stat file with incompat LMA feature"
790
791 test_18() {
792         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
793         ls $DIR || error "Failed to ls $DIR: $?"
794 }
795 run_test 18 "touch .../f ; ls ... =============================="
796
797 test_19a() {
798         touch $DIR/$tfile
799         ls -l $DIR
800         rm $DIR/$tfile
801         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
802 }
803 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
804
805 test_19b() {
806         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
807 }
808 run_test 19b "ls -l .../f19 (should return error) =============="
809
810 test_19c() {
811         [ $RUNAS_ID -eq $UID ] &&
812                 skip_env "RUNAS_ID = UID = $UID -- skipping"
813
814         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
815 }
816 run_test 19c "$RUNAS touch .../f19 (should return error) =="
817
818 test_19d() {
819         cat $DIR/f19 && error || true
820 }
821 run_test 19d "cat .../f19 (should return error) =============="
822
823 test_20() {
824         touch $DIR/$tfile
825         rm $DIR/$tfile
826         touch $DIR/$tfile
827         rm $DIR/$tfile
828         touch $DIR/$tfile
829         rm $DIR/$tfile
830         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
831 }
832 run_test 20 "touch .../f ; ls -l ..."
833
834 test_21() {
835         test_mkdir $DIR/$tdir
836         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
837         ln -s dangle $DIR/$tdir/link
838         echo foo >> $DIR/$tdir/link
839         cat $DIR/$tdir/dangle
840         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
841         $CHECKSTAT -f -t file $DIR/$tdir/link ||
842                 error "$tdir/link not linked to a file"
843 }
844 run_test 21 "write to dangling link"
845
846 test_22() {
847         local wdir=$DIR/$tdir
848         test_mkdir $wdir
849         chown $RUNAS_ID:$RUNAS_GID $wdir
850         (cd $wdir || error "cd $wdir failed";
851                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
852                 $RUNAS tar xf -)
853         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
854         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
855         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
856                 error "checkstat -u failed"
857 }
858 run_test 22 "unpack tar archive as non-root user"
859
860 # was test_23
861 test_23a() {
862         test_mkdir $DIR/$tdir
863         local file=$DIR/$tdir/$tfile
864
865         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
866         openfile -f O_CREAT:O_EXCL $file &&
867                 error "$file recreate succeeded" || true
868 }
869 run_test 23a "O_CREAT|O_EXCL in subdir"
870
871 test_23b() { # bug 18988
872         test_mkdir $DIR/$tdir
873         local file=$DIR/$tdir/$tfile
874
875         rm -f $file
876         echo foo > $file || error "write filed"
877         echo bar >> $file || error "append filed"
878         $CHECKSTAT -s 8 $file || error "wrong size"
879         rm $file
880 }
881 run_test 23b "O_APPEND check"
882
883 # LU-9409, size with O_APPEND and tiny writes
884 test_23c() {
885         local file=$DIR/$tfile
886
887         # single dd
888         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
889         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
890         rm -f $file
891
892         # racing tiny writes
893         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
894         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
895         wait
896         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
897         rm -f $file
898
899         #racing tiny & normal writes
900         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
901         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
902         wait
903         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
904         rm -f $file
905
906         #racing tiny & normal writes 2, ugly numbers
907         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
908         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
909         wait
910         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
911         rm -f $file
912 }
913 run_test 23c "O_APPEND size checks for tiny writes"
914
915 # LU-11069 file offset is correct after appending writes
916 test_23d() {
917         local file=$DIR/$tfile
918         local offset
919
920         echo CentaurHauls > $file
921         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
922         if ((offset != 26)); then
923                 error "wrong offset, expected 26, got '$offset'"
924         fi
925 }
926 run_test 23d "file offset is correct after appending writes"
927
928 # rename sanity
929 test_24a() {
930         echo '-- same directory rename'
931         test_mkdir $DIR/$tdir
932         touch $DIR/$tdir/$tfile.1
933         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
934         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
935 }
936 run_test 24a "rename file to non-existent target"
937
938 test_24b() {
939         test_mkdir $DIR/$tdir
940         touch $DIR/$tdir/$tfile.{1,2}
941         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
942         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
943         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
944 }
945 run_test 24b "rename file to existing target"
946
947 test_24c() {
948         test_mkdir $DIR/$tdir
949         test_mkdir $DIR/$tdir/d$testnum.1
950         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
951         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
952         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
953 }
954 run_test 24c "rename directory to non-existent target"
955
956 test_24d() {
957         test_mkdir -c1 $DIR/$tdir
958         test_mkdir -c1 $DIR/$tdir/d$testnum.1
959         test_mkdir -c1 $DIR/$tdir/d$testnum.2
960         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
961         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
962         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
963 }
964 run_test 24d "rename directory to existing target"
965
966 test_24e() {
967         echo '-- cross directory renames --'
968         test_mkdir $DIR/R5a
969         test_mkdir $DIR/R5b
970         touch $DIR/R5a/f
971         mv $DIR/R5a/f $DIR/R5b/g
972         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
973         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
974 }
975 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
976
977 test_24f() {
978         test_mkdir $DIR/R6a
979         test_mkdir $DIR/R6b
980         touch $DIR/R6a/f $DIR/R6b/g
981         mv $DIR/R6a/f $DIR/R6b/g
982         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
983         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
984 }
985 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
986
987 test_24g() {
988         test_mkdir $DIR/R7a
989         test_mkdir $DIR/R7b
990         test_mkdir $DIR/R7a/d
991         mv $DIR/R7a/d $DIR/R7b/e
992         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
993         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
994 }
995 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
996
997 test_24h() {
998         test_mkdir -c1 $DIR/R8a
999         test_mkdir -c1 $DIR/R8b
1000         test_mkdir -c1 $DIR/R8a/d
1001         test_mkdir -c1 $DIR/R8b/e
1002         mrename $DIR/R8a/d $DIR/R8b/e
1003         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
1004         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
1005 }
1006 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
1007
1008 test_24i() {
1009         echo "-- rename error cases"
1010         test_mkdir $DIR/R9
1011         test_mkdir $DIR/R9/a
1012         touch $DIR/R9/f
1013         mrename $DIR/R9/f $DIR/R9/a
1014         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1015         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1016         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1017 }
1018 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1019
1020 test_24j() {
1021         test_mkdir $DIR/R10
1022         mrename $DIR/R10/f $DIR/R10/g
1023         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1024         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1025         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1026 }
1027 run_test 24j "source does not exist ============================"
1028
1029 test_24k() {
1030         test_mkdir $DIR/R11a
1031         test_mkdir $DIR/R11a/d
1032         touch $DIR/R11a/f
1033         mv $DIR/R11a/f $DIR/R11a/d
1034         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1035         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1036 }
1037 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1038
1039 # bug 2429 - rename foo foo foo creates invalid file
1040 test_24l() {
1041         f="$DIR/f24l"
1042         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1043 }
1044 run_test 24l "Renaming a file to itself ========================"
1045
1046 test_24m() {
1047         f="$DIR/f24m"
1048         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1049         # on ext3 this does not remove either the source or target files
1050         # though the "expected" operation would be to remove the source
1051         $CHECKSTAT -t file ${f} || error "${f} missing"
1052         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1053 }
1054 run_test 24m "Renaming a file to a hard link to itself ========="
1055
1056 test_24n() {
1057     f="$DIR/f24n"
1058     # this stats the old file after it was renamed, so it should fail
1059     touch ${f}
1060     $CHECKSTAT ${f} || error "${f} missing"
1061     mv ${f} ${f}.rename
1062     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1063     $CHECKSTAT -a ${f} || error "${f} exists"
1064 }
1065 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1066
1067 test_24o() {
1068         test_mkdir $DIR/$tdir
1069         rename_many -s random -v -n 10 $DIR/$tdir
1070 }
1071 run_test 24o "rename of files during htree split"
1072
1073 test_24p() {
1074         test_mkdir $DIR/R12a
1075         test_mkdir $DIR/R12b
1076         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1077         mrename $DIR/R12a $DIR/R12b
1078         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1079         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1080         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1081         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1082 }
1083 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1084
1085 cleanup_multiop_pause() {
1086         trap 0
1087         kill -USR1 $MULTIPID
1088 }
1089
1090 test_24q() {
1091         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1092
1093         test_mkdir $DIR/R13a
1094         test_mkdir $DIR/R13b
1095         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1096         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1097         MULTIPID=$!
1098
1099         trap cleanup_multiop_pause EXIT
1100         mrename $DIR/R13a $DIR/R13b
1101         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1102         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1103         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1104         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1105         cleanup_multiop_pause
1106         wait $MULTIPID || error "multiop close failed"
1107 }
1108 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1109
1110 test_24r() { #bug 3789
1111         test_mkdir $DIR/R14a
1112         test_mkdir $DIR/R14a/b
1113         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1114         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1115         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1116 }
1117 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1118
1119 test_24s() {
1120         test_mkdir $DIR/R15a
1121         test_mkdir $DIR/R15a/b
1122         test_mkdir $DIR/R15a/b/c
1123         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1124         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1125         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1126 }
1127 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1128 test_24t() {
1129         test_mkdir $DIR/R16a
1130         test_mkdir $DIR/R16a/b
1131         test_mkdir $DIR/R16a/b/c
1132         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1133         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1134         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1135 }
1136 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1137
1138 test_24u() { # bug12192
1139         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1140         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1141 }
1142 run_test 24u "create stripe file"
1143
1144 simple_cleanup_common() {
1145         local rc=0
1146         trap 0
1147         [ -z "$DIR" ] || [ -z "$tdir" ] && return 0
1148
1149         local start=$SECONDS
1150         rm -rf $DIR/$tdir
1151         rc=$?
1152         wait_delete_completed
1153         echo "cleanup time $((SECONDS - start))"
1154         return $rc
1155 }
1156
1157 max_pages_per_rpc() {
1158         local mdtname="$(printf "MDT%04x" ${1:-0})"
1159         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1160 }
1161
1162 test_24v() {
1163         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1164
1165         local nrfiles=${COUNT:-100000}
1166         local fname="$DIR/$tdir/$tfile"
1167
1168         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1169         [ "$mds1_FSTYPE" = "zfs" ] && nrfiles=${COUNT:-10000}
1170
1171         test_mkdir "$(dirname $fname)"
1172         # assume MDT0000 has the fewest inodes
1173         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1174         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1175         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1176
1177         trap simple_cleanup_common EXIT
1178
1179         createmany -m "$fname" $nrfiles
1180
1181         cancel_lru_locks mdc
1182         lctl set_param mdc.*.stats clear
1183
1184         # was previously test_24D: LU-6101
1185         # readdir() returns correct number of entries after cursor reload
1186         local num_ls=$(ls $DIR/$tdir | wc -l)
1187         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1188         local num_all=$(ls -a $DIR/$tdir | wc -l)
1189         if [ $num_ls -ne $nrfiles ] || [ $num_uniq -ne $nrfiles ] ||
1190                 [ $num_all -ne $((nrfiles + 2)) ]; then
1191                         error "Expected $nrfiles files, got $num_ls " \
1192                                 "($num_uniq unique $num_all .&..)"
1193         fi
1194         # LU-5 large readdir
1195         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1196         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1197         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1198         # take into account of overhead in lu_dirpage header and end mark in
1199         # each page, plus one in rpc_num calculation.
1200         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1201         local page_entries=$(((PAGE_SIZE - 24) / dirent_size))
1202         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1203         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1204         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1205         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1206         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1207         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1208                 error "large readdir doesn't take effect: " \
1209                       "$mds_readpage should be about $rpc_max"
1210
1211         simple_cleanup_common
1212 }
1213 run_test 24v "list large directory (test hash collision, b=17560)"
1214
1215 test_24w() { # bug21506
1216         SZ1=234852
1217         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1218         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1219         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1220         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1221         [[ "$SZ1" -eq "$SZ2" ]] ||
1222                 error "Error reading at the end of the file $tfile"
1223 }
1224 run_test 24w "Reading a file larger than 4Gb"
1225
1226 test_24x() {
1227         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1228         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1229         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1230                 skip "Need MDS version at least 2.7.56"
1231
1232         local MDTIDX=1
1233         local remote_dir=$DIR/$tdir/remote_dir
1234
1235         test_mkdir $DIR/$tdir
1236         $LFS mkdir -i $MDTIDX $remote_dir ||
1237                 error "create remote directory failed"
1238
1239         test_mkdir $DIR/$tdir/src_dir
1240         touch $DIR/$tdir/src_file
1241         test_mkdir $remote_dir/tgt_dir
1242         touch $remote_dir/tgt_file
1243
1244         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1245                 error "rename dir cross MDT failed!"
1246
1247         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1248                 error "rename file cross MDT failed!"
1249
1250         touch $DIR/$tdir/ln_file
1251         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1252                 error "ln file cross MDT failed"
1253
1254         rm -rf $DIR/$tdir || error "Can not delete directories"
1255 }
1256 run_test 24x "cross MDT rename/link"
1257
1258 test_24y() {
1259         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1260         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1261
1262         local remote_dir=$DIR/$tdir/remote_dir
1263         local mdtidx=1
1264
1265         test_mkdir $DIR/$tdir
1266         $LFS mkdir -i $mdtidx $remote_dir ||
1267                 error "create remote directory failed"
1268
1269         test_mkdir $remote_dir/src_dir
1270         touch $remote_dir/src_file
1271         test_mkdir $remote_dir/tgt_dir
1272         touch $remote_dir/tgt_file
1273
1274         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1275                 error "rename subdir in the same remote dir failed!"
1276
1277         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1278                 error "rename files in the same remote dir failed!"
1279
1280         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1281                 error "link files in the same remote dir failed!"
1282
1283         rm -rf $DIR/$tdir || error "Can not delete directories"
1284 }
1285 run_test 24y "rename/link on the same dir should succeed"
1286
1287 test_24z() {
1288         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1289         [[ $MDS1_VERSION -lt $(version_code 2.12.51) ]] &&
1290                 skip "Need MDS version at least 2.12.51"
1291
1292         local index
1293
1294         for index in 0 1; do
1295                 $LFS mkdir -i $index $DIR/$tdir.$index || error "mkdir failed"
1296                 touch $DIR/$tdir.0/$tfile.$index || error "touch failed"
1297         done
1298
1299         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1 || error "mv $tfile.0 failed"
1300
1301         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.0)
1302         [ $index -eq 0 ] || error "$tfile.0 is on MDT$index"
1303
1304         local mdts=$(comma_list $(mdts_nodes))
1305
1306         do_nodes $mdts $LCTL set_param mdt.*.enable_remote_rename=0
1307         stack_trap "do_nodes $mdts $LCTL \
1308                 set_param mdt.*.enable_remote_rename=1" EXIT
1309
1310         mv $DIR/$tdir.0/$tfile.1 $DIR/$tdir.1 || error "mv $tfile.1 failed"
1311
1312         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.1)
1313         [ $index -eq 1 ] || error "$tfile.1 is on MDT$index"
1314 }
1315 run_test 24z "cross-MDT rename is done as cp"
1316
1317 test_24A() { # LU-3182
1318         local NFILES=5000
1319
1320         rm -rf $DIR/$tdir
1321         test_mkdir $DIR/$tdir
1322         trap simple_cleanup_common EXIT
1323         createmany -m $DIR/$tdir/$tfile $NFILES
1324         local t=$(ls $DIR/$tdir | wc -l)
1325         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1326         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1327         if [ $t -ne $NFILES ] || [ $u -ne $NFILES ] ||
1328            [ $v -ne $((NFILES + 2)) ] ; then
1329                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1330         fi
1331
1332         simple_cleanup_common || error "Can not delete directories"
1333 }
1334 run_test 24A "readdir() returns correct number of entries."
1335
1336 test_24B() { # LU-4805
1337         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1338
1339         local count
1340
1341         test_mkdir $DIR/$tdir
1342         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1343                 error "create striped dir failed"
1344
1345         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1346         [ $count -eq 2 ] || error "Expected 2, got $count"
1347
1348         touch $DIR/$tdir/striped_dir/a
1349
1350         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1351         [ $count -eq 3 ] || error "Expected 3, got $count"
1352
1353         touch $DIR/$tdir/striped_dir/.f
1354
1355         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1356         [ $count -eq 4 ] || error "Expected 4, got $count"
1357
1358         rm -rf $DIR/$tdir || error "Can not delete directories"
1359 }
1360 run_test 24B "readdir for striped dir return correct number of entries"
1361
1362 test_24C() {
1363         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1364
1365         mkdir $DIR/$tdir
1366         mkdir $DIR/$tdir/d0
1367         mkdir $DIR/$tdir/d1
1368
1369         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1370                 error "create striped dir failed"
1371
1372         cd $DIR/$tdir/d0/striped_dir
1373
1374         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1375         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1376         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1377
1378         [ "$d0_ino" = "$parent_ino" ] ||
1379                 error ".. wrong, expect $d0_ino, get $parent_ino"
1380
1381         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1382                 error "mv striped dir failed"
1383
1384         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1385
1386         [ "$d1_ino" = "$parent_ino" ] ||
1387                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1388 }
1389 run_test 24C "check .. in striped dir"
1390
1391 test_24E() {
1392         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1393         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1394
1395         mkdir -p $DIR/$tdir
1396         mkdir $DIR/$tdir/src_dir
1397         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1398                 error "create remote source failed"
1399
1400         touch $DIR/$tdir/src_dir/src_child/a
1401
1402         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1403                 error "create remote target dir failed"
1404
1405         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1406                 error "create remote target child failed"
1407
1408         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1409                 error "rename dir cross MDT failed!"
1410
1411         find $DIR/$tdir
1412
1413         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1414                 error "src_child still exists after rename"
1415
1416         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1417                 error "missing file(a) after rename"
1418
1419         rm -rf $DIR/$tdir || error "Can not delete directories"
1420 }
1421 run_test 24E "cross MDT rename/link"
1422
1423 test_24F () {
1424         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
1425
1426         local repeats=1000
1427         [ "$SLOW" = "no" ] && repeats=100
1428
1429         mkdir -p $DIR/$tdir
1430
1431         echo "$repeats repeats"
1432         for ((i = 0; i < repeats; i++)); do
1433                 $LFS mkdir -i0 -c2 $DIR/$tdir/test || error "mkdir fails"
1434                 touch $DIR/$tdir/test/a || error "touch fails"
1435                 mkdir $DIR/$tdir/test/b || error "mkdir fails"
1436                 rm -rf $DIR/$tdir/test || error "rmdir fails"
1437         done
1438
1439         true
1440 }
1441 run_test 24F "hash order vs readdir (LU-11330)"
1442
1443 test_25a() {
1444         echo '== symlink sanity ============================================='
1445
1446         test_mkdir $DIR/d25
1447         ln -s d25 $DIR/s25
1448         touch $DIR/s25/foo ||
1449                 error "File creation in symlinked directory failed"
1450 }
1451 run_test 25a "create file in symlinked directory ==============="
1452
1453 test_25b() {
1454         [ ! -d $DIR/d25 ] && test_25a
1455         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1456 }
1457 run_test 25b "lookup file in symlinked directory ==============="
1458
1459 test_26a() {
1460         test_mkdir $DIR/d26
1461         test_mkdir $DIR/d26/d26-2
1462         ln -s d26/d26-2 $DIR/s26
1463         touch $DIR/s26/foo || error "File creation failed"
1464 }
1465 run_test 26a "multiple component symlink ======================="
1466
1467 test_26b() {
1468         test_mkdir -p $DIR/$tdir/d26-2
1469         ln -s $tdir/d26-2/foo $DIR/s26-2
1470         touch $DIR/s26-2 || error "File creation failed"
1471 }
1472 run_test 26b "multiple component symlink at end of lookup ======"
1473
1474 test_26c() {
1475         test_mkdir $DIR/d26.2
1476         touch $DIR/d26.2/foo
1477         ln -s d26.2 $DIR/s26.2-1
1478         ln -s s26.2-1 $DIR/s26.2-2
1479         ln -s s26.2-2 $DIR/s26.2-3
1480         chmod 0666 $DIR/s26.2-3/foo
1481 }
1482 run_test 26c "chain of symlinks"
1483
1484 # recursive symlinks (bug 439)
1485 test_26d() {
1486         ln -s d26-3/foo $DIR/d26-3
1487 }
1488 run_test 26d "create multiple component recursive symlink"
1489
1490 test_26e() {
1491         [ ! -h $DIR/d26-3 ] && test_26d
1492         rm $DIR/d26-3
1493 }
1494 run_test 26e "unlink multiple component recursive symlink"
1495
1496 # recursive symlinks (bug 7022)
1497 test_26f() {
1498         test_mkdir $DIR/$tdir
1499         test_mkdir $DIR/$tdir/$tfile
1500         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1501         test_mkdir -p lndir/bar1
1502         test_mkdir $DIR/$tdir/$tfile/$tfile
1503         cd $tfile                || error "cd $tfile failed"
1504         ln -s .. dotdot          || error "ln dotdot failed"
1505         ln -s dotdot/lndir lndir || error "ln lndir failed"
1506         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1507         output=`ls $tfile/$tfile/lndir/bar1`
1508         [ "$output" = bar1 ] && error "unexpected output"
1509         rm -r $tfile             || error "rm $tfile failed"
1510         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1511 }
1512 run_test 26f "rm -r of a directory which has recursive symlink"
1513
1514 test_27a() {
1515         test_mkdir $DIR/$tdir
1516         $LFS getstripe $DIR/$tdir
1517         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1518         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1519         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1520 }
1521 run_test 27a "one stripe file"
1522
1523 test_27b() {
1524         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1525
1526         test_mkdir $DIR/$tdir
1527         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1528         $LFS getstripe -c $DIR/$tdir/$tfile
1529         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1530                 error "two-stripe file doesn't have two stripes"
1531
1532         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1533 }
1534 run_test 27b "create and write to two stripe file"
1535
1536 test_27d() {
1537         test_mkdir $DIR/$tdir
1538         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1539                 error "setstripe failed"
1540         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1541         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1542 }
1543 run_test 27d "create file with default settings"
1544
1545 test_27e() {
1546         # LU-5839 adds check for existed layout before setting it
1547         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1548                 skip "Need MDS version at least 2.7.56"
1549
1550         test_mkdir $DIR/$tdir
1551         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1552         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1553         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1554 }
1555 run_test 27e "setstripe existing file (should return error)"
1556
1557 test_27f() {
1558         test_mkdir $DIR/$tdir
1559         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1560                 error "$LFS setstripe $DIR/$tdir/$tfile failed"
1561         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1562                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1563         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1564         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1565 }
1566 run_test 27f "setstripe with bad stripe size (should return error)"
1567
1568 test_27g() {
1569         test_mkdir $DIR/$tdir
1570         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1571         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1572                 error "$DIR/$tdir/$tfile has object"
1573 }
1574 run_test 27g "$LFS getstripe with no objects"
1575
1576 test_27ga() {
1577         test_mkdir $DIR/$tdir
1578         touch $DIR/$tdir/$tfile || error "touch failed"
1579         ln -s bogus $DIR/$tdir/$tfile.2 || error "ln failed"
1580         $LFS getstripe -m $DIR/$tdir/$tfile $DIR/$tdir/$tfile.2
1581         local rc=$?
1582         (( rc == 2 )) || error "getstripe did not return ENOENT"
1583 }
1584 run_test 27ga "$LFS getstripe with missing file (should return error)"
1585
1586 test_27i() {
1587         test_mkdir $DIR/$tdir
1588         touch $DIR/$tdir/$tfile || error "touch failed"
1589         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1590                 error "missing objects"
1591 }
1592 run_test 27i "$LFS getstripe with some objects"
1593
1594 test_27j() {
1595         test_mkdir $DIR/$tdir
1596         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1597                 error "setstripe failed" || true
1598 }
1599 run_test 27j "setstripe with bad stripe offset (should return error)"
1600
1601 test_27k() { # bug 2844
1602         test_mkdir $DIR/$tdir
1603         local file=$DIR/$tdir/$tfile
1604         local ll_max_blksize=$((4 * 1024 * 1024))
1605         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1606         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1607         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1608         dd if=/dev/zero of=$file bs=4k count=1
1609         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1610         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1611 }
1612 run_test 27k "limit i_blksize for broken user apps"
1613
1614 test_27l() {
1615         mcreate $DIR/$tfile || error "creating file"
1616         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1617                 error "setstripe should have failed" || true
1618 }
1619 run_test 27l "check setstripe permissions (should return error)"
1620
1621 test_27m() {
1622         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1623
1624         [ -n "$RCLIENTS" -o -n "$MOUNT_2" ] &&
1625                 skip_env "multiple clients -- skipping"
1626
1627         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1628                    head -n1)
1629         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1630                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1631         fi
1632         trap simple_cleanup_common EXIT
1633         test_mkdir $DIR/$tdir
1634         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1635         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1636                 error "dd should fill OST0"
1637         i=2
1638         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1639                 i=$((i + 1))
1640                 [ $i -gt 256 ] && break
1641         done
1642         i=$((i + 1))
1643         touch $DIR/$tdir/$tfile.$i
1644         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1645             awk '{print $1}'| grep -w "0") ] &&
1646                 error "OST0 was full but new created file still use it"
1647         i=$((i + 1))
1648         touch $DIR/$tdir/$tfile.$i
1649         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1650             awk '{print $1}'| grep -w "0") ] &&
1651                 error "OST0 was full but new created file still use it"
1652         simple_cleanup_common
1653 }
1654 run_test 27m "create file while OST0 was full"
1655
1656 sleep_maxage() {
1657         local delay=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage |
1658                       awk '{ print $1 * 2; exit; }')
1659         sleep $delay
1660 }
1661
1662 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1663 # if the OST isn't full anymore.
1664 reset_enospc() {
1665         local OSTIDX=${1:-""}
1666
1667         local list=$(comma_list $(osts_nodes))
1668         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1669
1670         do_nodes $list lctl set_param fail_loc=0
1671         sync    # initiate all OST_DESTROYs from MDS to OST
1672         sleep_maxage
1673 }
1674
1675 exhaust_precreations() {
1676         local OSTIDX=$1
1677         local FAILLOC=$2
1678         local FAILIDX=${3:-$OSTIDX}
1679         local ofacet=ost$((OSTIDX + 1))
1680
1681         test_mkdir -p -c1 $DIR/$tdir
1682         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1683         local mfacet=mds$((mdtidx + 1))
1684         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1685
1686         local OST=$(ostname_from_index $OSTIDX)
1687
1688         # on the mdt's osc
1689         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1690         local last_id=$(do_facet $mfacet lctl get_param -n \
1691                         osc.$mdtosc_proc1.prealloc_last_id)
1692         local next_id=$(do_facet $mfacet lctl get_param -n \
1693                         osc.$mdtosc_proc1.prealloc_next_id)
1694
1695         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1696         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1697
1698         test_mkdir -p $DIR/$tdir/${OST}
1699         $LFS setstripe -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1700 #define OBD_FAIL_OST_ENOSPC              0x215
1701         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1702         echo "Creating to objid $last_id on ost $OST..."
1703         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1704         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1705         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1706         sleep_maxage
1707 }
1708
1709 exhaust_all_precreations() {
1710         local i
1711         for (( i=0; i < OSTCOUNT; i++ )) ; do
1712                 exhaust_precreations $i $1 -1
1713         done
1714 }
1715
1716 test_27n() {
1717         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1718         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1719         remote_mds_nodsh && skip "remote MDS with nodsh"
1720         remote_ost_nodsh && skip "remote OST with nodsh"
1721
1722         reset_enospc
1723         rm -f $DIR/$tdir/$tfile
1724         exhaust_precreations 0 0x80000215
1725         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1726         touch $DIR/$tdir/$tfile || error "touch failed"
1727         $LFS getstripe $DIR/$tdir/$tfile
1728         reset_enospc
1729 }
1730 run_test 27n "create file with some full OSTs"
1731
1732 test_27o() {
1733         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1734         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1735         remote_mds_nodsh && skip "remote MDS with nodsh"
1736         remote_ost_nodsh && skip "remote OST with nodsh"
1737
1738         reset_enospc
1739         rm -f $DIR/$tdir/$tfile
1740         exhaust_all_precreations 0x215
1741
1742         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1743
1744         reset_enospc
1745         rm -rf $DIR/$tdir/*
1746 }
1747 run_test 27o "create file with all full OSTs (should error)"
1748
1749 test_27p() {
1750         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1751         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1752         remote_mds_nodsh && skip "remote MDS with nodsh"
1753         remote_ost_nodsh && skip "remote OST with nodsh"
1754
1755         reset_enospc
1756         rm -f $DIR/$tdir/$tfile
1757         test_mkdir $DIR/$tdir
1758
1759         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1760         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1761         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1762
1763         exhaust_precreations 0 0x80000215
1764         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1765         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1766         $LFS getstripe $DIR/$tdir/$tfile
1767
1768         reset_enospc
1769 }
1770 run_test 27p "append to a truncated file with some full OSTs"
1771
1772 test_27q() {
1773         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1774         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1775         remote_mds_nodsh && skip "remote MDS with nodsh"
1776         remote_ost_nodsh && skip "remote OST with nodsh"
1777
1778         reset_enospc
1779         rm -f $DIR/$tdir/$tfile
1780
1781         test_mkdir $DIR/$tdir
1782         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1783         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1784                 error "truncate $DIR/$tdir/$tfile failed"
1785         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1786
1787         exhaust_all_precreations 0x215
1788
1789         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1790         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1791
1792         reset_enospc
1793 }
1794 run_test 27q "append to truncated file with all OSTs full (should error)"
1795
1796 test_27r() {
1797         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1798         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1799         remote_mds_nodsh && skip "remote MDS with nodsh"
1800         remote_ost_nodsh && skip "remote OST with nodsh"
1801
1802         reset_enospc
1803         rm -f $DIR/$tdir/$tfile
1804         exhaust_precreations 0 0x80000215
1805
1806         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1807
1808         reset_enospc
1809 }
1810 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1811
1812 test_27s() { # bug 10725
1813         test_mkdir $DIR/$tdir
1814         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1815         local stripe_count=0
1816         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1817         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1818                 error "stripe width >= 2^32 succeeded" || true
1819
1820 }
1821 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1822
1823 test_27t() { # bug 10864
1824         WDIR=$(pwd)
1825         WLFS=$(which lfs)
1826         cd $DIR
1827         touch $tfile
1828         $WLFS getstripe $tfile
1829         cd $WDIR
1830 }
1831 run_test 27t "check that utils parse path correctly"
1832
1833 test_27u() { # bug 4900
1834         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1835         remote_mds_nodsh && skip "remote MDS with nodsh"
1836
1837         local index
1838         local list=$(comma_list $(mdts_nodes))
1839
1840 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1841         do_nodes $list $LCTL set_param fail_loc=0x139
1842         test_mkdir -p $DIR/$tdir
1843         trap simple_cleanup_common EXIT
1844         createmany -o $DIR/$tdir/t- 1000
1845         do_nodes $list $LCTL set_param fail_loc=0
1846
1847         TLOG=$TMP/$tfile.getstripe
1848         $LFS getstripe $DIR/$tdir > $TLOG
1849         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1850         unlinkmany $DIR/$tdir/t- 1000
1851         trap 0
1852         [[ $OBJS -gt 0 ]] &&
1853                 error "$OBJS objects created on OST-0. See $TLOG" ||
1854                 rm -f $TLOG
1855 }
1856 run_test 27u "skip object creation on OSC w/o objects"
1857
1858 test_27v() { # bug 4900
1859         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1860         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1861         remote_mds_nodsh && skip "remote MDS with nodsh"
1862         remote_ost_nodsh && skip "remote OST with nodsh"
1863
1864         exhaust_all_precreations 0x215
1865         reset_enospc
1866
1867         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1868
1869         touch $DIR/$tdir/$tfile
1870         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1871         # all except ost1
1872         for (( i=1; i < OSTCOUNT; i++ )); do
1873                 do_facet ost$i lctl set_param fail_loc=0x705
1874         done
1875         local START=`date +%s`
1876         createmany -o $DIR/$tdir/$tfile 32
1877
1878         local FINISH=`date +%s`
1879         local TIMEOUT=`lctl get_param -n timeout`
1880         local PROCESS=$((FINISH - START))
1881         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1882                error "$FINISH - $START >= $TIMEOUT / 2"
1883         sleep $((TIMEOUT / 2 - PROCESS))
1884         reset_enospc
1885 }
1886 run_test 27v "skip object creation on slow OST"
1887
1888 test_27w() { # bug 10997
1889         test_mkdir $DIR/$tdir
1890         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1891         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1892                 error "stripe size $size != 65536" || true
1893         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1894                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1895 }
1896 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1897
1898 test_27wa() {
1899         [[ $OSTCOUNT -lt 2 ]] &&
1900                 skip_env "skipping multiple stripe count/offset test"
1901
1902         test_mkdir $DIR/$tdir
1903         for i in $(seq 1 $OSTCOUNT); do
1904                 offset=$((i - 1))
1905                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
1906                         error "setstripe -c $i -i $offset failed"
1907                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
1908                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
1909                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1910                 [ $index -ne $offset ] &&
1911                         error "stripe offset $index != $offset" || true
1912         done
1913 }
1914 run_test 27wa "check $LFS setstripe -c -i options"
1915
1916 test_27x() {
1917         remote_ost_nodsh && skip "remote OST with nodsh"
1918         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1919         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1920
1921         OFFSET=$(($OSTCOUNT - 1))
1922         OSTIDX=0
1923         local OST=$(ostname_from_index $OSTIDX)
1924
1925         test_mkdir $DIR/$tdir
1926         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
1927         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1928         sleep_maxage
1929         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1930         for i in $(seq 0 $OFFSET); do
1931                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
1932                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
1933                 error "OST0 was degraded but new created file still use it"
1934         done
1935         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1936 }
1937 run_test 27x "create files while OST0 is degraded"
1938
1939 test_27y() {
1940         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1941         remote_mds_nodsh && skip "remote MDS with nodsh"
1942         remote_ost_nodsh && skip "remote OST with nodsh"
1943         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1944
1945         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1946         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1947                 osc.$mdtosc.prealloc_last_id)
1948         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1949                 osc.$mdtosc.prealloc_next_id)
1950         local fcount=$((last_id - next_id))
1951         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
1952         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1953
1954         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1955                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1956         local OST_DEACTIVE_IDX=-1
1957         local OSC
1958         local OSTIDX
1959         local OST
1960
1961         for OSC in $MDS_OSCS; do
1962                 OST=$(osc_to_ost $OSC)
1963                 OSTIDX=$(index_from_ostuuid $OST)
1964                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1965                         OST_DEACTIVE_IDX=$OSTIDX
1966                 fi
1967                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1968                         echo $OSC "is Deactivated:"
1969                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1970                 fi
1971         done
1972
1973         OSTIDX=$(index_from_ostuuid $OST)
1974         test_mkdir $DIR/$tdir
1975         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
1976
1977         for OSC in $MDS_OSCS; do
1978                 OST=$(osc_to_ost $OSC)
1979                 OSTIDX=$(index_from_ostuuid $OST)
1980                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1981                         echo $OST "is degraded:"
1982                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1983                                                 obdfilter.$OST.degraded=1
1984                 fi
1985         done
1986
1987         sleep_maxage
1988         createmany -o $DIR/$tdir/$tfile $fcount
1989
1990         for OSC in $MDS_OSCS; do
1991                 OST=$(osc_to_ost $OSC)
1992                 OSTIDX=$(index_from_ostuuid $OST)
1993                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1994                         echo $OST "is recovered from degraded:"
1995                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1996                                                 obdfilter.$OST.degraded=0
1997                 else
1998                         do_facet $SINGLEMDS lctl --device %$OSC activate
1999                 fi
2000         done
2001
2002         # all osp devices get activated, hence -1 stripe count restored
2003         local stripe_count=0
2004
2005         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
2006         # devices get activated.
2007         sleep_maxage
2008         $LFS setstripe -c -1 $DIR/$tfile
2009         stripe_count=$($LFS getstripe -c $DIR/$tfile)
2010         rm -f $DIR/$tfile
2011         [ $stripe_count -ne $OSTCOUNT ] &&
2012                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
2013         return 0
2014 }
2015 run_test 27y "create files while OST0 is degraded and the rest inactive"
2016
2017 check_seq_oid()
2018 {
2019         log "check file $1"
2020
2021         lmm_count=$($LFS getstripe -c $1)
2022         lmm_seq=$($LFS getstripe -v $1 | awk '/lmm_seq/ { print $2 }')
2023         lmm_oid=$($LFS getstripe -v $1 | awk '/lmm_object_id/ { print $2 }')
2024
2025         local old_ifs="$IFS"
2026         IFS=$'[:]'
2027         fid=($($LFS path2fid $1))
2028         IFS="$old_ifs"
2029
2030         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
2031         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
2032
2033         # compare lmm_seq and lu_fid->f_seq
2034         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
2035         # compare lmm_object_id and lu_fid->oid
2036         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
2037
2038         # check the trusted.fid attribute of the OST objects of the file
2039         local have_obdidx=false
2040         local stripe_nr=0
2041         $LFS getstripe $1 | while read obdidx oid hex seq; do
2042                 # skip lines up to and including "obdidx"
2043                 [ -z "$obdidx" ] && break
2044                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
2045                 $have_obdidx || continue
2046
2047                 local ost=$((obdidx + 1))
2048                 local dev=$(ostdevname $ost)
2049                 local oid_hex
2050
2051                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
2052
2053                 seq=$(echo $seq | sed -e "s/^0x//g")
2054                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
2055                         oid_hex=$(echo $oid)
2056                 else
2057                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
2058                 fi
2059                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
2060
2061                 local ff=""
2062                 #
2063                 # Don't unmount/remount the OSTs if we don't need to do that.
2064                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2065                 # update too, until that use mount/ll_decode_filter_fid/mount.
2066                 # Re-enable when debugfs will understand new filter_fid.
2067                 #
2068                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2069                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2070                                 $dev 2>/dev/null" | grep "parent=")
2071                 fi
2072                 if [ -z "$ff" ]; then
2073                         stop ost$ost
2074                         mount_fstype ost$ost
2075                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2076                                 $(facet_mntpt ost$ost)/$obj_file)
2077                         unmount_fstype ost$ost
2078                         start ost$ost $dev $OST_MOUNT_OPTS
2079                         clients_up
2080                 fi
2081
2082                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2083
2084                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2085
2086                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2087                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2088                 #
2089                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2090                 #       stripe_size=1048576 component_id=1 component_start=0 \
2091                 #       component_end=33554432
2092                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2093                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2094                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2095                 local ff_pstripe
2096                 if grep -q 'stripe=' <<<$ff; then
2097                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2098                 else
2099                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2100                         # into f_ver in this case.  See comment on ff_parent.
2101                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2102                 fi
2103
2104                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2105                 [ $ff_pseq = $lmm_seq ] ||
2106                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2107                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2108                 [ $ff_poid = $lmm_oid ] ||
2109                         error "FF parent OID $ff_poid != $lmm_oid"
2110                 (($ff_pstripe == $stripe_nr)) ||
2111                         error "FF stripe $ff_pstripe != $stripe_nr"
2112
2113                 stripe_nr=$((stripe_nr + 1))
2114                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2115                         continue
2116                 if grep -q 'stripe_count=' <<<$ff; then
2117                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2118                                             -e 's/ .*//' <<<$ff)
2119                         [ $lmm_count = $ff_scnt ] ||
2120                                 error "FF stripe count $lmm_count != $ff_scnt"
2121                 fi
2122         done
2123 }
2124
2125 test_27z() {
2126         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2127         remote_ost_nodsh && skip "remote OST with nodsh"
2128
2129         test_mkdir $DIR/$tdir
2130         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2131                 { error "setstripe -c -1 failed"; return 1; }
2132         # We need to send a write to every object to get parent FID info set.
2133         # This _should_ also work for setattr, but does not currently.
2134         # touch $DIR/$tdir/$tfile-1 ||
2135         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2136                 { error "dd $tfile-1 failed"; return 2; }
2137         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2138                 { error "setstripe -c -1 failed"; return 3; }
2139         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2140                 { error "dd $tfile-2 failed"; return 4; }
2141
2142         # make sure write RPCs have been sent to OSTs
2143         sync; sleep 5; sync
2144
2145         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2146         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2147 }
2148 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2149
2150 test_27A() { # b=19102
2151         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2152
2153         save_layout_restore_at_exit $MOUNT
2154         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2155         wait_update $HOSTNAME "$LFS getstripe -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2156                 error "stripe count $($LFS getstripe -c $MOUNT) != 1"
2157         local default_size=$($LFS getstripe -S $MOUNT)
2158         local default_offset=$($LFS getstripe -i $MOUNT)
2159         local dsize=$(do_facet $SINGLEMDS \
2160                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2161         [ $default_size -eq $dsize ] ||
2162                 error "stripe size $default_size != $dsize"
2163         [ $default_offset -eq -1 ] ||
2164                 error "stripe offset $default_offset != -1"
2165 }
2166 run_test 27A "check filesystem-wide default LOV EA values"
2167
2168 test_27B() { # LU-2523
2169         test_mkdir $DIR/$tdir
2170         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2171         touch $DIR/$tdir/f0
2172         # open f1 with O_LOV_DELAY_CREATE
2173         # rename f0 onto f1
2174         # call setstripe ioctl on open file descriptor for f1
2175         # close
2176         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2177                 $DIR/$tdir/f0
2178
2179         rm -f $DIR/$tdir/f1
2180         # open f1 with O_LOV_DELAY_CREATE
2181         # unlink f1
2182         # call setstripe ioctl on open file descriptor for f1
2183         # close
2184         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2185
2186         # Allow multiop to fail in imitation of NFS's busted semantics.
2187         true
2188 }
2189 run_test 27B "call setstripe on open unlinked file/rename victim"
2190
2191 test_27C() { #LU-2871
2192         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2193
2194         declare -a ost_idx
2195         local index
2196         local found
2197         local i
2198         local j
2199
2200         test_mkdir $DIR/$tdir
2201         cd $DIR/$tdir
2202         for i in $(seq 0 $((OSTCOUNT - 1))); do
2203                 # set stripe across all OSTs starting from OST$i
2204                 $LFS setstripe -i $i -c -1 $tfile$i
2205                 # get striping information
2206                 ost_idx=($($LFS getstripe $tfile$i |
2207                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2208                 echo ${ost_idx[@]}
2209
2210                 # check the layout
2211                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2212                         error "${#ost_idx[@]} != $OSTCOUNT"
2213
2214                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2215                         found=0
2216                         for j in $(echo ${ost_idx[@]}); do
2217                                 if [ $index -eq $j ]; then
2218                                         found=1
2219                                         break
2220                                 fi
2221                         done
2222                         [ $found = 1 ] ||
2223                                 error "Can not find $index in ${ost_idx[@]}"
2224                 done
2225         done
2226 }
2227 run_test 27C "check full striping across all OSTs"
2228
2229 test_27D() {
2230         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2231         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2232         remote_mds_nodsh && skip "remote MDS with nodsh"
2233
2234         local POOL=${POOL:-testpool}
2235         local first_ost=0
2236         local last_ost=$(($OSTCOUNT - 1))
2237         local ost_step=1
2238         local ost_list=$(seq $first_ost $ost_step $last_ost)
2239         local ost_range="$first_ost $last_ost $ost_step"
2240
2241         if ! combined_mgs_mds ; then
2242                 mount_mgs_client
2243         fi
2244
2245         test_mkdir $DIR/$tdir
2246         pool_add $POOL || error "pool_add failed"
2247         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2248
2249         local skip27D
2250         [ $MDS1_VERSION -lt $(version_code 2.8.55) ] &&
2251                 skip27D+="-s 29"
2252         [ $MDS1_VERSION -lt $(version_code 2.9.55) ] ||
2253                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2254                         skip27D+=" -s 30,31"
2255         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2256                 error "llapi_layout_test failed"
2257
2258         destroy_test_pools || error "destroy test pools failed"
2259
2260         if ! combined_mgs_mds ; then
2261                 umount_mgs_client
2262         fi
2263 }
2264 run_test 27D "validate llapi_layout API"
2265
2266 # Verify that default_easize is increased from its initial value after
2267 # accessing a widely striped file.
2268 test_27E() {
2269         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2270         [ $CLIENT_VERSION -lt $(version_code 2.5.57) ] &&
2271                 skip "client does not have LU-3338 fix"
2272
2273         # 72 bytes is the minimum space required to store striping
2274         # information for a file striped across one OST:
2275         # (sizeof(struct lov_user_md_v3) +
2276         #  sizeof(struct lov_user_ost_data_v1))
2277         local min_easize=72
2278         $LCTL set_param -n llite.*.default_easize $min_easize ||
2279                 error "lctl set_param failed"
2280         local easize=$($LCTL get_param -n llite.*.default_easize)
2281
2282         [ $easize -eq $min_easize ] ||
2283                 error "failed to set default_easize"
2284
2285         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2286                 error "setstripe failed"
2287         cat $DIR/$tfile
2288         rm $DIR/$tfile
2289
2290         easize=$($LCTL get_param -n llite.*.default_easize)
2291
2292         [ $easize -gt $min_easize ] ||
2293                 error "default_easize not updated"
2294 }
2295 run_test 27E "check that default extended attribute size properly increases"
2296
2297 test_27F() { # LU-5346/LU-7975
2298         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2299         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2300         [[ $MDS1_VERSION -lt $(version_code 2.8.51) ]] &&
2301                 skip "Need MDS version at least 2.8.51"
2302         remote_ost_nodsh && skip "remote OST with nodsh"
2303
2304         test_mkdir $DIR/$tdir
2305         rm -f $DIR/$tdir/f0
2306         $LFS setstripe -c 2 $DIR/$tdir
2307
2308         # stop all OSTs to reproduce situation for LU-7975 ticket
2309         for num in $(seq $OSTCOUNT); do
2310                 stop ost$num
2311         done
2312
2313         # open/create f0 with O_LOV_DELAY_CREATE
2314         # truncate f0 to a non-0 size
2315         # close
2316         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2317
2318         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2319         # open/write it again to force delayed layout creation
2320         cat /etc/hosts > $DIR/$tdir/f0 &
2321         catpid=$!
2322
2323         # restart OSTs
2324         for num in $(seq $OSTCOUNT); do
2325                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2326                         error "ost$num failed to start"
2327         done
2328
2329         wait $catpid || error "cat failed"
2330
2331         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2332         [[ $($LFS getstripe -c $DIR/$tdir/f0) == 2 ]] ||
2333                 error "wrong stripecount"
2334
2335 }
2336 run_test 27F "Client resend delayed layout creation with non-zero size"
2337
2338 test_27G() { #LU-10629
2339         [ $MDS1_VERSION -lt $(version_code 2.11.51) ] &&
2340                 skip "Need MDS version at least 2.11.51"
2341         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2342         remote_mds_nodsh && skip "remote MDS with nodsh"
2343         local POOL=${POOL:-testpool}
2344         local ostrange="0 0 1"
2345
2346         test_mkdir $DIR/$tdir
2347         pool_add $POOL || error "pool_add failed"
2348         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2349         $LFS setstripe -p $POOL $DIR/$tdir
2350
2351         local pool=$($LFS getstripe -p $DIR/$tdir)
2352
2353         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2354
2355         $LFS setstripe -d $DIR/$tdir
2356
2357         pool=$($LFS getstripe -p $DIR/$tdir)
2358
2359         rmdir $DIR/$tdir
2360
2361         [ -z "$pool" ] || error "'$pool' is not empty"
2362 }
2363 run_test 27G "Clear OST pool from stripe"
2364
2365 test_27H() {
2366         [[ $MDS1_VERSION -le $(version_code 2.11.54) ]] &&
2367                 skip "Need MDS version newer than 2.11.54"
2368         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2369         test_mkdir $DIR/$tdir
2370         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2371         touch $DIR/$tdir/$tfile
2372         $LFS getstripe -c $DIR/$tdir/$tfile
2373         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2374                 error "two-stripe file doesn't have two stripes"
2375
2376         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2377         $LFS getstripe -y $DIR/$tdir/$tfile
2378         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2379              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2380                 error "expected l_ost_idx: [02]$ not matched"
2381
2382         # make sure ost list has been cleared
2383         local stripesize=$($LFS getstripe -S $DIR/$tdir)
2384         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2385                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2386         touch $DIR/$tdir/f3
2387         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2388 }
2389 run_test 27H "Set specific OSTs stripe"
2390
2391 test_27I() {
2392         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2393         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2394         local pool=$TESTNAME
2395         local ostrange="1 1 1"
2396
2397         save_layout_restore_at_exit $MOUNT
2398         $LFS setstripe -c 2 -i 0 $MOUNT
2399         pool_add $pool || error "pool_add failed"
2400         pool_add_targets $pool $ostrange || "pool_add_targets failed"
2401         test_mkdir $DIR/$tdir
2402         $LFS setstripe -p $pool $DIR/$tdir
2403         $MULTIOP $DIR/$tdir/$tfile Oc || error "multiop failed"
2404         $LFS getstripe $DIR/$tdir/$tfile
2405 }
2406 run_test 27I "check that root dir striping does not break parent dir one"
2407
2408 test_27J() {
2409         [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.12.51) ]] &&
2410                 skip "Need MDS version newer than 2.12.51"
2411
2412         test_mkdir $DIR/$tdir
2413         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2414         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2415
2416         # create foreign file (raw way)
2417         create_foreign_file -f $DIR/$tdir/$tfile -x "${uuid1}@${uuid2}" \
2418                 -t 1 -F 0xda08 || error "create_foreign_file failed"
2419
2420         # verify foreign file (raw way)
2421         parse_foreign_file -f $DIR/$tdir/$tfile |
2422                 grep "lov_foreign_magic: 0x0BD70BD0" ||
2423                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign magic"
2424         parse_foreign_file -f $DIR/$tdir/$tfile | grep "lov_xattr_size: 89" ||
2425                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2426         parse_foreign_file -f $DIR/$tdir/$tfile |
2427                 grep "lov_foreign_size: 73" ||
2428                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2429         parse_foreign_file -f $DIR/$tdir/$tfile |
2430                 grep "lov_foreign_type: 1" ||
2431                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign type"
2432         parse_foreign_file -f $DIR/$tdir/$tfile |
2433                 grep "lov_foreign_flags: 0x0000DA08" ||
2434                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign flags"
2435         local lov=$(parse_foreign_file -f $DIR/$tdir/$tfile |
2436                 grep "lov_foreign_value: 0x" |
2437                 sed -e 's/lov_foreign_value: 0x//')
2438         local lov2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160)
2439         [[ $lov = ${lov2// /} ]] ||
2440                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign value"
2441
2442         # create foreign file (lfs + API)
2443         $LFS setstripe --foreign=daos --flags 0xda08 \
2444                 -x "${uuid1}@${uuid2}" $DIR/$tdir/${tfile}2 ||
2445                 error "$DIR/$tdir/${tfile}2: create failed"
2446
2447         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2448                 grep "lfm_magic:.*0x0BD70BD0" ||
2449                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign magic"
2450         # lfm_length is LOV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2451         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_length:.*73" ||
2452                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign size"
2453         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_type:.*daos" ||
2454                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign type"
2455         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2456                 grep "lfm_flags:.*0x0000DA08" ||
2457                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign flags"
2458         $LFS getstripe $DIR/$tdir/${tfile}2 |
2459                 grep "lfm_value:.*${uuid1}@${uuid2}" ||
2460                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign value"
2461
2462         # modify striping should fail
2463         $LFS setstripe -c 2 $DIR/$tdir/$tfile &&
2464                 error "$DIR/$tdir/$tfile: setstripe should fail"
2465         $LFS setstripe -c 2 $DIR/$tdir/${tfile}2 &&
2466                 error "$DIR/$tdir/${tfile}2: setstripe should fail"
2467
2468         # R/W should fail
2469         cat $DIR/$tdir/$tfile && error "$DIR/$tdir/$tfile: read should fail"
2470         cat $DIR/$tdir/${tfile}2 &&
2471                 error "$DIR/$tdir/${tfile}2: read should fail"
2472         cat /etc/passwd > $DIR/$tdir/$tfile &&
2473                 error "$DIR/$tdir/$tfile: write should fail"
2474         cat /etc/passwd > $DIR/$tdir/${tfile}2 &&
2475                 error "$DIR/$tdir/${tfile}2: write should fail"
2476
2477         # chmod should work
2478         chmod 222 $DIR/$tdir/$tfile ||
2479                 error "$DIR/$tdir/$tfile: chmod failed"
2480         chmod 222 $DIR/$tdir/${tfile}2 ||
2481                 error "$DIR/$tdir/${tfile}2: chmod failed"
2482
2483         # chown should work
2484         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tfile ||
2485                 error "$DIR/$tdir/$tfile: chown failed"
2486         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tfile}2 ||
2487                 error "$DIR/$tdir/${tfile}2: chown failed"
2488
2489         # rename should work
2490         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}.new ||
2491                 error "$DIR/$tdir/$tfile: rename of foreign file has failed"
2492         mv $DIR/$tdir/${tfile}2 $DIR/$tdir/${tfile}2.new ||
2493                 error "$DIR/$tdir/${tfile}2: rename of foreign file has failed"
2494
2495         #remove foreign file
2496         rm $DIR/$tdir/${tfile}.new ||
2497                 error "$DIR/$tdir/${tfile}.new: remove of foreign file has failed"
2498         rm $DIR/$tdir/${tfile}2.new ||
2499                 error "$DIR/$tdir/${tfile}2.new: remove of foreign file has failed"
2500 }
2501 run_test 27J "basic ops on file with foreign LOV"
2502
2503 test_27K() {
2504         [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.12.49) ]] &&
2505                 skip "Need MDS version newer than 2.12.49"
2506
2507         test_mkdir $DIR/$tdir
2508         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2509         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2510
2511         # create foreign dir (raw way)
2512         create_foreign_dir -d $DIR/$tdir/$tdir -x "${uuid1}@${uuid2}" -t 1 ||
2513                 error "create_foreign_dir FAILED"
2514
2515         # verify foreign dir (raw way)
2516         parse_foreign_dir -d $DIR/$tdir/$tdir |
2517                 grep "lmv_foreign_magic:.*0xcd50cd0" ||
2518                 error "$DIR/$tdir/$tfile: invalid LMV EA magic"
2519         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_xattr_size:.*89$" ||
2520                 error "$DIR/$tdir/$tdir: invalid LMV EA size"
2521         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_type: 1$" ||
2522                 error "$DIR/$tdir/$tdir: invalid LMV EA type"
2523         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_flags: 0$" ||
2524                 error "$DIR/$tdir/$tdir: invalid LMV EA flags"
2525         local lmv=$(parse_foreign_dir -d $DIR/$tdir/$tdir |
2526                 grep "lmv_foreign_value: 0x" |
2527                 sed 's/lmv_foreign_value: 0x//')
2528         local lmv2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160 |
2529                 sed 's/ //g')
2530         [[ $lmv == $lmv2 ]] || error "$DIR/$tdir/$tdir: invalid LMV EA value"
2531
2532         # create foreign dir (lfs + API)
2533         $LFS mkdir --foreign=daos --xattr="${uuid1}@${uuid2}" --flags=0xda05 \
2534                 $DIR/$tdir/${tdir}2 ||
2535                 error "$DIR/$tdir/${tdir}2: create failed"
2536
2537         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2538                 grep "lfm_magic:.*0x0CD50CD0" ||
2539                 error "$DIR/$tdir/${tdir}2: invalid LMV EA magic"
2540         # lfm_length is LMV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2541         # - sizeof(lfm_type) - sizeof(lfm_flags)
2542         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_length:.*73" ||
2543                 error "$DIR/$tdir/${tdir}2: invalid LMV EA size"
2544         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_type:.*daos" ||
2545                 error "$DIR/$tdir/${tdir}2: invalid LMV EA type"
2546         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2547                 grep "lfm_flags:.*0x0000DA05" ||
2548                 error "$DIR/$tdir/${tdir}2: invalid LMV EA flags"
2549         $LFS getdirstripe $DIR/$tdir/${tdir}2 |
2550                 grep "lfm_value.*${uuid1}@${uuid2}" ||
2551                 error "$DIR/$tdir/${tdir}2: invalid LMV EA value"
2552
2553         # file create in dir should fail
2554         touch $DIR/$tdir/$tdir/$tfile && "$DIR/$tdir: file create should fail"
2555         touch $DIR/$tdir/${tdir}2/$tfile &&
2556                 "$DIR/${tdir}2: file create should fail"
2557
2558         # chmod should work
2559         chmod 777 $DIR/$tdir/$tdir ||
2560                 error "$DIR/$tdir: chmod failed"
2561         chmod 777 $DIR/$tdir/${tdir}2 ||
2562                 error "$DIR/${tdir}2: chmod failed"
2563
2564         # chown should work
2565         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tdir ||
2566                 error "$DIR/$tdir: chown failed"
2567         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tdir}2 ||
2568                 error "$DIR/${tdir}2: chown failed"
2569
2570         # rename should work
2571         mv $DIR/$tdir/$tdir $DIR/$tdir/${tdir}.new ||
2572                 error "$DIR/$tdir/$tdir: rename of foreign dir has failed"
2573         mv $DIR/$tdir/${tdir}2 $DIR/$tdir/${tdir}2.new ||
2574                 error "$DIR/$tdir/${tdir}2: rename of foreign dir has failed"
2575
2576         #remove foreign dir
2577         rmdir $DIR/$tdir/${tdir}.new ||
2578                 error "$DIR/$tdir/${tdir}.new: remove of foreign dir has failed"
2579         rmdir $DIR/$tdir/${tdir}2.new ||
2580                 error "$DIR/$tdir/${tdir}2.new: remove of foreign dir has failed"
2581 }
2582 run_test 27K "basic ops on dir with foreign LMV"
2583
2584 # createtest also checks that device nodes are created and
2585 # then visible correctly (#2091)
2586 test_28() { # bug 2091
2587         test_mkdir $DIR/d28
2588         $CREATETEST $DIR/d28/ct || error "createtest failed"
2589 }
2590 run_test 28 "create/mknod/mkdir with bad file types ============"
2591
2592 test_29() {
2593         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2594
2595         sync; sleep 1; sync # flush out any dirty pages from previous tests
2596         cancel_lru_locks
2597         test_mkdir $DIR/d29
2598         touch $DIR/d29/foo
2599         log 'first d29'
2600         ls -l $DIR/d29
2601
2602         declare -i LOCKCOUNTORIG=0
2603         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2604                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2605         done
2606         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2607
2608         declare -i LOCKUNUSEDCOUNTORIG=0
2609         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2610                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2611         done
2612
2613         log 'second d29'
2614         ls -l $DIR/d29
2615         log 'done'
2616
2617         declare -i LOCKCOUNTCURRENT=0
2618         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2619                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2620         done
2621
2622         declare -i LOCKUNUSEDCOUNTCURRENT=0
2623         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2624                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2625         done
2626
2627         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2628                 $LCTL set_param -n ldlm.dump_namespaces ""
2629                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2630                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2631                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2632                 return 2
2633         fi
2634         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2635                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2636                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2637                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2638                 return 3
2639         fi
2640 }
2641 run_test 29 "IT_GETATTR regression  ============================"
2642
2643 test_30a() { # was test_30
2644         cp $(which ls) $DIR || cp /bin/ls $DIR
2645         $DIR/ls / || error "Can't execute binary from lustre"
2646         rm $DIR/ls
2647 }
2648 run_test 30a "execute binary from Lustre (execve) =============="
2649
2650 test_30b() {
2651         cp `which ls` $DIR || cp /bin/ls $DIR
2652         chmod go+rx $DIR/ls
2653         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
2654         rm $DIR/ls
2655 }
2656 run_test 30b "execute binary from Lustre as non-root ==========="
2657
2658 test_30c() { # b=22376
2659         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2660
2661         cp `which ls` $DIR || cp /bin/ls $DIR
2662         chmod a-rw $DIR/ls
2663         cancel_lru_locks mdc
2664         cancel_lru_locks osc
2665         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
2666         rm -f $DIR/ls
2667 }
2668 run_test 30c "execute binary from Lustre without read perms ===="
2669
2670 test_31a() {
2671         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
2672         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
2673 }
2674 run_test 31a "open-unlink file =================================="
2675
2676 test_31b() {
2677         touch $DIR/f31 || error "touch $DIR/f31 failed"
2678         ln $DIR/f31 $DIR/f31b || error "ln failed"
2679         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
2680         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
2681 }
2682 run_test 31b "unlink file with multiple links while open ======="
2683
2684 test_31c() {
2685         touch $DIR/f31 || error "touch $DIR/f31 failed"
2686         ln $DIR/f31 $DIR/f31c || error "ln failed"
2687         multiop_bg_pause $DIR/f31 O_uc ||
2688                 error "multiop_bg_pause for $DIR/f31 failed"
2689         MULTIPID=$!
2690         $MULTIOP $DIR/f31c Ouc
2691         kill -USR1 $MULTIPID
2692         wait $MULTIPID
2693 }
2694 run_test 31c "open-unlink file with multiple links ============="
2695
2696 test_31d() {
2697         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
2698         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
2699 }
2700 run_test 31d "remove of open directory ========================="
2701
2702 test_31e() { # bug 2904
2703         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
2704 }
2705 run_test 31e "remove of open non-empty directory ==============="
2706
2707 test_31f() { # bug 4554
2708         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2709
2710         set -vx
2711         test_mkdir $DIR/d31f
2712         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
2713         cp /etc/hosts $DIR/d31f
2714         ls -l $DIR/d31f
2715         $LFS getstripe $DIR/d31f/hosts
2716         multiop_bg_pause $DIR/d31f D_c || return 1
2717         MULTIPID=$!
2718
2719         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2720         test_mkdir $DIR/d31f
2721         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
2722         cp /etc/hosts $DIR/d31f
2723         ls -l $DIR/d31f
2724         $LFS getstripe $DIR/d31f/hosts
2725         multiop_bg_pause $DIR/d31f D_c || return 1
2726         MULTIPID2=$!
2727
2728         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2729         wait $MULTIPID || error "first opendir $MULTIPID failed"
2730
2731         sleep 6
2732
2733         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2734         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2735         set +vx
2736 }
2737 run_test 31f "remove of open directory with open-unlink file ==="
2738
2739 test_31g() {
2740         echo "-- cross directory link --"
2741         test_mkdir -c1 $DIR/${tdir}ga
2742         test_mkdir -c1 $DIR/${tdir}gb
2743         touch $DIR/${tdir}ga/f
2744         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2745         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2746         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2747         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2748         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2749 }
2750 run_test 31g "cross directory link==============="
2751
2752 test_31h() {
2753         echo "-- cross directory link --"
2754         test_mkdir -c1 $DIR/${tdir}
2755         test_mkdir -c1 $DIR/${tdir}/dir
2756         touch $DIR/${tdir}/f
2757         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2758         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2759         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2760         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2761         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2762 }
2763 run_test 31h "cross directory link under child==============="
2764
2765 test_31i() {
2766         echo "-- cross directory link --"
2767         test_mkdir -c1 $DIR/$tdir
2768         test_mkdir -c1 $DIR/$tdir/dir
2769         touch $DIR/$tdir/dir/f
2770         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2771         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2772         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2773         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2774         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2775 }
2776 run_test 31i "cross directory link under parent==============="
2777
2778 test_31j() {
2779         test_mkdir -c1 -p $DIR/$tdir
2780         test_mkdir -c1 -p $DIR/$tdir/dir1
2781         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2782         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2783         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2784         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2785         return 0
2786 }
2787 run_test 31j "link for directory==============="
2788
2789 test_31k() {
2790         test_mkdir -c1 -p $DIR/$tdir
2791         touch $DIR/$tdir/s
2792         touch $DIR/$tdir/exist
2793         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2794         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2795         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2796         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2797         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2798         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2799         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2800         return 0
2801 }
2802 run_test 31k "link to file: the same, non-existing, dir==============="
2803
2804 test_31m() {
2805         mkdir $DIR/d31m
2806         touch $DIR/d31m/s
2807         mkdir $DIR/d31m2
2808         touch $DIR/d31m2/exist
2809         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2810         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2811         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2812         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2813         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2814         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2815         return 0
2816 }
2817 run_test 31m "link to file: the same, non-existing, dir==============="
2818
2819 test_31n() {
2820         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2821         nlink=$(stat --format=%h $DIR/$tfile)
2822         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2823         local fd=$(free_fd)
2824         local cmd="exec $fd<$DIR/$tfile"
2825         eval $cmd
2826         cmd="exec $fd<&-"
2827         trap "eval $cmd" EXIT
2828         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2829         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2830         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2831         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2832         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2833         eval $cmd
2834 }
2835 run_test 31n "check link count of unlinked file"
2836
2837 link_one() {
2838         local TEMPNAME=$(mktemp $1_XXXXXX)
2839         mlink $TEMPNAME $1 2> /dev/null &&
2840                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2841         munlink $TEMPNAME
2842 }
2843
2844 test_31o() { # LU-2901
2845         test_mkdir $DIR/$tdir
2846         for LOOP in $(seq 100); do
2847                 rm -f $DIR/$tdir/$tfile*
2848                 for THREAD in $(seq 8); do
2849                         link_one $DIR/$tdir/$tfile.$LOOP &
2850                 done
2851                 wait
2852                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2853                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2854                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2855                         break || true
2856         done
2857 }
2858 run_test 31o "duplicate hard links with same filename"
2859
2860 test_31p() {
2861         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
2862
2863         test_mkdir $DIR/$tdir
2864         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2865         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
2866
2867         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2868                 error "open unlink test1 failed"
2869         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2870                 error "open unlink test2 failed"
2871
2872         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2873                 error "test1 still exists"
2874         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2875                 error "test2 still exists"
2876 }
2877 run_test 31p "remove of open striped directory"
2878
2879 cleanup_test32_mount() {
2880         local rc=0
2881         trap 0
2882         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2883         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2884         losetup -d $loopdev || true
2885         rm -rf $DIR/$tdir
2886         return $rc
2887 }
2888
2889 test_32a() {
2890         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2891
2892         echo "== more mountpoints and symlinks ================="
2893         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2894         trap cleanup_test32_mount EXIT
2895         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2896         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2897                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2898         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
2899                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
2900         cleanup_test32_mount
2901 }
2902 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2903
2904 test_32b() {
2905         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2906
2907         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2908         trap cleanup_test32_mount EXIT
2909         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2910         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2911                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2912         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
2913                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
2914         cleanup_test32_mount
2915 }
2916 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2917
2918 test_32c() {
2919         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2920
2921         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2922         trap cleanup_test32_mount EXIT
2923         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2924         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2925                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2926         test_mkdir -p $DIR/$tdir/d2/test_dir
2927         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2928                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
2929         cleanup_test32_mount
2930 }
2931 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2932
2933 test_32d() {
2934         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2935
2936         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2937         trap cleanup_test32_mount EXIT
2938         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2939         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2940                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2941         test_mkdir -p $DIR/$tdir/d2/test_dir
2942         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2943                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
2944         cleanup_test32_mount
2945 }
2946 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
2947
2948 test_32e() {
2949         rm -fr $DIR/$tdir
2950         test_mkdir -p $DIR/$tdir/tmp
2951         local tmp_dir=$DIR/$tdir/tmp
2952         ln -s $DIR/$tdir $tmp_dir/symlink11
2953         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2954         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
2955         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
2956 }
2957 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
2958
2959 test_32f() {
2960         rm -fr $DIR/$tdir
2961         test_mkdir -p $DIR/$tdir/tmp
2962         local tmp_dir=$DIR/$tdir/tmp
2963         ln -s $DIR/$tdir $tmp_dir/symlink11
2964         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2965         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
2966         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
2967 }
2968 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
2969
2970 test_32g() {
2971         local tmp_dir=$DIR/$tdir/tmp
2972         test_mkdir -p $tmp_dir
2973         test_mkdir $DIR/${tdir}2
2974         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2975         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2976         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
2977         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
2978         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
2979         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
2980 }
2981 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2982
2983 test_32h() {
2984         rm -fr $DIR/$tdir $DIR/${tdir}2
2985         tmp_dir=$DIR/$tdir/tmp
2986         test_mkdir -p $tmp_dir
2987         test_mkdir $DIR/${tdir}2
2988         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2989         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2990         ls $tmp_dir/symlink12 || error "listing symlink12"
2991         ls $DIR/$tdir/symlink02  || error "listing symlink02"
2992 }
2993 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2994
2995 test_32i() {
2996         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2997
2998         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2999         trap cleanup_test32_mount EXIT
3000         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3001         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3002                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3003         touch $DIR/$tdir/test_file
3004         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
3005                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
3006         cleanup_test32_mount
3007 }
3008 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
3009
3010 test_32j() {
3011         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3012
3013         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3014         trap cleanup_test32_mount EXIT
3015         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3016         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3017                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3018         touch $DIR/$tdir/test_file
3019         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
3020                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
3021         cleanup_test32_mount
3022 }
3023 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
3024
3025 test_32k() {
3026         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3027
3028         rm -fr $DIR/$tdir
3029         trap cleanup_test32_mount EXIT
3030         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3031         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3032                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3033         test_mkdir -p $DIR/$tdir/d2
3034         touch $DIR/$tdir/d2/test_file || error "touch failed"
3035         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3036                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
3037         cleanup_test32_mount
3038 }
3039 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
3040
3041 test_32l() {
3042         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3043
3044         rm -fr $DIR/$tdir
3045         trap cleanup_test32_mount EXIT
3046         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3047         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3048                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3049         test_mkdir -p $DIR/$tdir/d2
3050         touch $DIR/$tdir/d2/test_file || error "touch failed"
3051         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3052                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
3053         cleanup_test32_mount
3054 }
3055 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
3056
3057 test_32m() {
3058         rm -fr $DIR/d32m
3059         test_mkdir -p $DIR/d32m/tmp
3060         TMP_DIR=$DIR/d32m/tmp
3061         ln -s $DIR $TMP_DIR/symlink11
3062         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3063         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
3064                 error "symlink11 not a link"
3065         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
3066                 error "symlink01 not a link"
3067 }
3068 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
3069
3070 test_32n() {
3071         rm -fr $DIR/d32n
3072         test_mkdir -p $DIR/d32n/tmp
3073         TMP_DIR=$DIR/d32n/tmp
3074         ln -s $DIR $TMP_DIR/symlink11
3075         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3076         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
3077         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
3078 }
3079 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
3080
3081 test_32o() {
3082         touch $DIR/$tfile
3083         test_mkdir -p $DIR/d32o/tmp
3084         TMP_DIR=$DIR/d32o/tmp
3085         ln -s $DIR/$tfile $TMP_DIR/symlink12
3086         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3087         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
3088                 error "symlink12 not a link"
3089         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
3090         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
3091                 error "$DIR/d32o/tmp/symlink12 not file type"
3092         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
3093                 error "$DIR/d32o/symlink02 not file type"
3094 }
3095 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
3096
3097 test_32p() {
3098         log 32p_1
3099         rm -fr $DIR/d32p
3100         log 32p_2
3101         rm -f $DIR/$tfile
3102         log 32p_3
3103         touch $DIR/$tfile
3104         log 32p_4
3105         test_mkdir -p $DIR/d32p/tmp
3106         log 32p_5
3107         TMP_DIR=$DIR/d32p/tmp
3108         log 32p_6
3109         ln -s $DIR/$tfile $TMP_DIR/symlink12
3110         log 32p_7
3111         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3112         log 32p_8
3113         cat $DIR/d32p/tmp/symlink12 ||
3114                 error "Can't open $DIR/d32p/tmp/symlink12"
3115         log 32p_9
3116         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
3117         log 32p_10
3118 }
3119 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
3120
3121 test_32q() {
3122         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3123
3124         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3125         trap cleanup_test32_mount EXIT
3126         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3127         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3128         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3129                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3130         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
3131         cleanup_test32_mount
3132 }
3133 run_test 32q "stat follows mountpoints in Lustre (should return error)"
3134
3135 test_32r() {
3136         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3137
3138         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3139         trap cleanup_test32_mount EXIT
3140         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3141         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3142         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3143                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3144         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
3145         cleanup_test32_mount
3146 }
3147 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
3148
3149 test_33aa() {
3150         rm -f $DIR/$tfile
3151         touch $DIR/$tfile
3152         chmod 444 $DIR/$tfile
3153         chown $RUNAS_ID $DIR/$tfile
3154         log 33_1
3155         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3156         log 33_2
3157 }
3158 run_test 33aa "write file with mode 444 (should return error)"
3159
3160 test_33a() {
3161         rm -fr $DIR/$tdir
3162         test_mkdir $DIR/$tdir
3163         chown $RUNAS_ID $DIR/$tdir
3164         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
3165                 error "$RUNAS create $tdir/$tfile failed"
3166         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
3167                 error "open RDWR" || true
3168 }
3169 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
3170
3171 test_33b() {
3172         rm -fr $DIR/$tdir
3173         test_mkdir $DIR/$tdir
3174         chown $RUNAS_ID $DIR/$tdir
3175         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
3176 }
3177 run_test 33b "test open file with malformed flags (No panic)"
3178
3179 test_33c() {
3180         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3181         remote_ost_nodsh && skip "remote OST with nodsh"
3182
3183         local ostnum
3184         local ostname
3185         local write_bytes
3186         local all_zeros
3187
3188         all_zeros=:
3189         rm -fr $DIR/$tdir
3190         test_mkdir $DIR/$tdir
3191         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
3192
3193         sync
3194         for ostnum in $(seq $OSTCOUNT); do
3195                 # test-framework's OST numbering is one-based, while Lustre's
3196                 # is zero-based
3197                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3198                 # Parsing llobdstat's output sucks; we could grep the /proc
3199                 # path, but that's likely to not be as portable as using the
3200                 # llobdstat utility.  So we parse lctl output instead.
3201                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3202                         obdfilter/$ostname/stats |
3203                         awk '/^write_bytes/ {print $7}' )
3204                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
3205                 if (( ${write_bytes:-0} > 0 ))
3206                 then
3207                         all_zeros=false
3208                         break;
3209                 fi
3210         done
3211
3212         $all_zeros || return 0
3213
3214         # Write four bytes
3215         echo foo > $DIR/$tdir/bar
3216         # Really write them
3217         sync
3218
3219         # Total up write_bytes after writing.  We'd better find non-zeros.
3220         for ostnum in $(seq $OSTCOUNT); do
3221                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3222                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3223                         obdfilter/$ostname/stats |
3224                         awk '/^write_bytes/ {print $7}' )
3225                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
3226                 if (( ${write_bytes:-0} > 0 ))
3227                 then
3228                         all_zeros=false
3229                         break;
3230                 fi
3231         done
3232
3233         if $all_zeros
3234         then
3235                 for ostnum in $(seq $OSTCOUNT); do
3236                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3237                         echo "Check that write_bytes is present in obdfilter/*/stats:"
3238                         do_facet ost$ostnum lctl get_param -n \
3239                                 obdfilter/$ostname/stats
3240                 done
3241                 error "OST not keeping write_bytes stats (b22312)"
3242         fi
3243 }
3244 run_test 33c "test llobdstat and write_bytes"
3245
3246 test_33d() {
3247         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3248         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3249
3250         local MDTIDX=1
3251         local remote_dir=$DIR/$tdir/remote_dir
3252
3253         test_mkdir $DIR/$tdir
3254         $LFS mkdir -i $MDTIDX $remote_dir ||
3255                 error "create remote directory failed"
3256
3257         touch $remote_dir/$tfile
3258         chmod 444 $remote_dir/$tfile
3259         chown $RUNAS_ID $remote_dir/$tfile
3260
3261         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3262
3263         chown $RUNAS_ID $remote_dir
3264         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
3265                                         error "create" || true
3266         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
3267                                     error "open RDWR" || true
3268         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
3269 }
3270 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
3271
3272 test_33e() {
3273         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3274
3275         mkdir $DIR/$tdir
3276
3277         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3278         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3279         mkdir $DIR/$tdir/local_dir
3280
3281         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3282         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3283         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3284
3285         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3286                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
3287
3288         rmdir $DIR/$tdir/* || error "rmdir failed"
3289
3290         umask 777
3291         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3292         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3293         mkdir $DIR/$tdir/local_dir
3294
3295         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3296         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3297         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3298
3299         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3300                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3301
3302         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3303
3304         umask 000
3305         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3306         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3307         mkdir $DIR/$tdir/local_dir
3308
3309         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3310         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3311         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3312
3313         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3314                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3315 }
3316 run_test 33e "mkdir and striped directory should have same mode"
3317
3318 cleanup_33f() {
3319         trap 0
3320         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3321 }
3322
3323 test_33f() {
3324         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3325         remote_mds_nodsh && skip "remote MDS with nodsh"
3326
3327         mkdir $DIR/$tdir
3328         chmod go+rwx $DIR/$tdir
3329         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3330         trap cleanup_33f EXIT
3331
3332         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3333                 error "cannot create striped directory"
3334
3335         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3336                 error "cannot create files in striped directory"
3337
3338         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3339                 error "cannot remove files in striped directory"
3340
3341         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3342                 error "cannot remove striped directory"
3343
3344         cleanup_33f
3345 }
3346 run_test 33f "nonroot user can create, access, and remove a striped directory"
3347
3348 test_33g() {
3349         mkdir -p $DIR/$tdir/dir2
3350
3351         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3352         echo $err
3353         [[ $err =~ "exists" ]] || error "Not exists error"
3354 }
3355 run_test 33g "nonroot user create already existing root created file"
3356
3357 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3358 test_34a() {
3359         rm -f $DIR/f34
3360         $MCREATE $DIR/f34 || error "mcreate failed"
3361         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3362                 error "getstripe failed"
3363         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3364         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3365                 error "getstripe failed"
3366         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3367                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3368 }
3369 run_test 34a "truncate file that has not been opened ==========="
3370
3371 test_34b() {
3372         [ ! -f $DIR/f34 ] && test_34a
3373         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3374                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3375         $OPENFILE -f O_RDONLY $DIR/f34
3376         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3377                 error "getstripe failed"
3378         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3379                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3380 }
3381 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3382
3383 test_34c() {
3384         [ ! -f $DIR/f34 ] && test_34a
3385         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3386                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3387         $OPENFILE -f O_RDWR $DIR/f34
3388         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" &&
3389                 error "$LFS getstripe failed"
3390         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3391                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3392 }
3393 run_test 34c "O_RDWR opening file-with-size works =============="
3394
3395 test_34d() {
3396         [ ! -f $DIR/f34 ] && test_34a
3397         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3398                 error "dd failed"
3399         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3400                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3401         rm $DIR/f34
3402 }
3403 run_test 34d "write to sparse file ============================="
3404
3405 test_34e() {
3406         rm -f $DIR/f34e
3407         $MCREATE $DIR/f34e || error "mcreate failed"
3408         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3409         $CHECKSTAT -s 1000 $DIR/f34e ||
3410                 error "Size of $DIR/f34e not equal to 1000 bytes"
3411         $OPENFILE -f O_RDWR $DIR/f34e
3412         $CHECKSTAT -s 1000 $DIR/f34e ||
3413                 error "Size of $DIR/f34e not equal to 1000 bytes"
3414 }
3415 run_test 34e "create objects, some with size and some without =="
3416
3417 test_34f() { # bug 6242, 6243
3418         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3419
3420         SIZE34F=48000
3421         rm -f $DIR/f34f
3422         $MCREATE $DIR/f34f || error "mcreate failed"
3423         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3424         dd if=$DIR/f34f of=$TMP/f34f
3425         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3426         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3427         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3428         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3429         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3430 }
3431 run_test 34f "read from a file with no objects until EOF ======="
3432
3433 test_34g() {
3434         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3435
3436         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3437                 error "dd failed"
3438         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3439         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3440                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3441         cancel_lru_locks osc
3442         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3443                 error "wrong size after lock cancel"
3444
3445         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3446         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3447                 error "expanding truncate failed"
3448         cancel_lru_locks osc
3449         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3450                 error "wrong expanded size after lock cancel"
3451 }
3452 run_test 34g "truncate long file ==============================="
3453
3454 test_34h() {
3455         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3456
3457         local gid=10
3458         local sz=1000
3459
3460         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3461         sync # Flush the cache so that multiop below does not block on cache
3462              # flush when getting the group lock
3463         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3464         MULTIPID=$!
3465
3466         # Since just timed wait is not good enough, let's do a sync write
3467         # that way we are sure enough time for a roundtrip + processing
3468         # passed + 2 seconds of extra margin.
3469         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
3470         rm $DIR/${tfile}-1
3471         sleep 2
3472
3473         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3474                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3475                 kill -9 $MULTIPID
3476         fi
3477         wait $MULTIPID
3478         local nsz=`stat -c %s $DIR/$tfile`
3479         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3480 }
3481 run_test 34h "ftruncate file under grouplock should not block"
3482
3483 test_35a() {
3484         cp /bin/sh $DIR/f35a
3485         chmod 444 $DIR/f35a
3486         chown $RUNAS_ID $DIR/f35a
3487         $RUNAS $DIR/f35a && error || true
3488         rm $DIR/f35a
3489 }
3490 run_test 35a "exec file with mode 444 (should return and not leak)"
3491
3492 test_36a() {
3493         rm -f $DIR/f36
3494         utime $DIR/f36 || error "utime failed for MDS"
3495 }
3496 run_test 36a "MDS utime check (mknod, utime)"
3497
3498 test_36b() {
3499         echo "" > $DIR/f36
3500         utime $DIR/f36 || error "utime failed for OST"
3501 }
3502 run_test 36b "OST utime check (open, utime)"
3503
3504 test_36c() {
3505         rm -f $DIR/d36/f36
3506         test_mkdir $DIR/d36
3507         chown $RUNAS_ID $DIR/d36
3508         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3509 }
3510 run_test 36c "non-root MDS utime check (mknod, utime)"
3511
3512 test_36d() {
3513         [ ! -d $DIR/d36 ] && test_36c
3514         echo "" > $DIR/d36/f36
3515         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3516 }
3517 run_test 36d "non-root OST utime check (open, utime)"
3518
3519 test_36e() {
3520         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3521
3522         test_mkdir $DIR/$tdir
3523         touch $DIR/$tdir/$tfile
3524         $RUNAS utime $DIR/$tdir/$tfile &&
3525                 error "utime worked, expected failure" || true
3526 }
3527 run_test 36e "utime on non-owned file (should return error)"
3528
3529 subr_36fh() {
3530         local fl="$1"
3531         local LANG_SAVE=$LANG
3532         local LC_LANG_SAVE=$LC_LANG
3533         export LANG=C LC_LANG=C # for date language
3534
3535         DATESTR="Dec 20  2000"
3536         test_mkdir $DIR/$tdir
3537         lctl set_param fail_loc=$fl
3538         date; date +%s
3539         cp /etc/hosts $DIR/$tdir/$tfile
3540         sync & # write RPC generated with "current" inode timestamp, but delayed
3541         sleep 1
3542         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3543         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3544         cancel_lru_locks $OSC
3545         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3546         date; date +%s
3547         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3548                 echo "BEFORE: $LS_BEFORE" && \
3549                 echo "AFTER : $LS_AFTER" && \
3550                 echo "WANT  : $DATESTR" && \
3551                 error "$DIR/$tdir/$tfile timestamps changed" || true
3552
3553         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3554 }
3555
3556 test_36f() {
3557         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3558
3559         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3560         subr_36fh "0x80000214"
3561 }
3562 run_test 36f "utime on file racing with OST BRW write =========="
3563
3564 test_36g() {
3565         remote_ost_nodsh && skip "remote OST with nodsh"
3566         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3567         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
3568                 skip "Need MDS version at least 2.12.51"
3569
3570         local fmd_max_age
3571         local fmd
3572         local facet="ost1"
3573         local tgt="obdfilter"
3574
3575         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
3576
3577         test_mkdir $DIR/$tdir
3578         fmd_max_age=$(do_facet $facet \
3579                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
3580                 head -n 1")
3581
3582         echo "FMD max age: ${fmd_max_age}s"
3583         touch $DIR/$tdir/$tfile
3584         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3585                 gawk '{cnt=cnt+$1}  END{print cnt}')
3586         echo "FMD before: $fmd"
3587         [[ $fmd == 0 ]] &&
3588                 error "FMD wasn't create by touch"
3589         sleep $((fmd_max_age + 12))
3590         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3591                 gawk '{cnt=cnt+$1}  END{print cnt}')
3592         echo "FMD after: $fmd"
3593         [[ $fmd == 0 ]] ||
3594                 error "FMD wasn't expired by ping"
3595 }
3596 run_test 36g "FMD cache expiry ====================="
3597
3598 test_36h() {
3599         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3600
3601         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3602         subr_36fh "0x80000227"
3603 }
3604 run_test 36h "utime on file racing with OST BRW write =========="
3605
3606 test_36i() {
3607         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3608
3609         test_mkdir $DIR/$tdir
3610         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3611
3612         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3613         local new_mtime=$((mtime + 200))
3614
3615         #change Modify time of striped dir
3616         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3617                         error "change mtime failed"
3618
3619         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3620
3621         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3622 }
3623 run_test 36i "change mtime on striped directory"
3624
3625 # test_37 - duplicate with tests 32q 32r
3626
3627 test_38() {
3628         local file=$DIR/$tfile
3629         touch $file
3630         openfile -f O_DIRECTORY $file
3631         local RC=$?
3632         local ENOTDIR=20
3633         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3634         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3635 }
3636 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3637
3638 test_39a() { # was test_39
3639         touch $DIR/$tfile
3640         touch $DIR/${tfile}2
3641 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3642 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3643 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3644         sleep 2
3645         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3646         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3647                 echo "mtime"
3648                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3649                 echo "atime"
3650                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3651                 echo "ctime"
3652                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3653                 error "O_TRUNC didn't change timestamps"
3654         fi
3655 }
3656 run_test 39a "mtime changed on create"
3657
3658 test_39b() {
3659         test_mkdir -c1 $DIR/$tdir
3660         cp -p /etc/passwd $DIR/$tdir/fopen
3661         cp -p /etc/passwd $DIR/$tdir/flink
3662         cp -p /etc/passwd $DIR/$tdir/funlink
3663         cp -p /etc/passwd $DIR/$tdir/frename
3664         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3665
3666         sleep 1
3667         echo "aaaaaa" >> $DIR/$tdir/fopen
3668         echo "aaaaaa" >> $DIR/$tdir/flink
3669         echo "aaaaaa" >> $DIR/$tdir/funlink
3670         echo "aaaaaa" >> $DIR/$tdir/frename
3671
3672         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3673         local link_new=`stat -c %Y $DIR/$tdir/flink`
3674         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3675         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3676
3677         cat $DIR/$tdir/fopen > /dev/null
3678         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3679         rm -f $DIR/$tdir/funlink2
3680         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3681
3682         for (( i=0; i < 2; i++ )) ; do
3683                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3684                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3685                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3686                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3687
3688                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3689                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3690                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3691                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3692
3693                 cancel_lru_locks $OSC
3694                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3695         done
3696 }
3697 run_test 39b "mtime change on open, link, unlink, rename  ======"
3698
3699 # this should be set to past
3700 TEST_39_MTIME=`date -d "1 year ago" +%s`
3701
3702 # bug 11063
3703 test_39c() {
3704         touch $DIR1/$tfile
3705         sleep 2
3706         local mtime0=`stat -c %Y $DIR1/$tfile`
3707
3708         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3709         local mtime1=`stat -c %Y $DIR1/$tfile`
3710         [ "$mtime1" = $TEST_39_MTIME ] || \
3711                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3712
3713         local d1=`date +%s`
3714         echo hello >> $DIR1/$tfile
3715         local d2=`date +%s`
3716         local mtime2=`stat -c %Y $DIR1/$tfile`
3717         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3718                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3719
3720         mv $DIR1/$tfile $DIR1/$tfile-1
3721
3722         for (( i=0; i < 2; i++ )) ; do
3723                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3724                 [ "$mtime2" = "$mtime3" ] || \
3725                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3726
3727                 cancel_lru_locks $OSC
3728                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3729         done
3730 }
3731 run_test 39c "mtime change on rename ==========================="
3732
3733 # bug 21114
3734 test_39d() {
3735         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3736
3737         touch $DIR1/$tfile
3738         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3739
3740         for (( i=0; i < 2; i++ )) ; do
3741                 local mtime=`stat -c %Y $DIR1/$tfile`
3742                 [ $mtime = $TEST_39_MTIME ] || \
3743                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3744
3745                 cancel_lru_locks $OSC
3746                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3747         done
3748 }
3749 run_test 39d "create, utime, stat =============================="
3750
3751 # bug 21114
3752 test_39e() {
3753         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3754
3755         touch $DIR1/$tfile
3756         local mtime1=`stat -c %Y $DIR1/$tfile`
3757
3758         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3759
3760         for (( i=0; i < 2; i++ )) ; do
3761                 local mtime2=`stat -c %Y $DIR1/$tfile`
3762                 [ $mtime2 = $TEST_39_MTIME ] || \
3763                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3764
3765                 cancel_lru_locks $OSC
3766                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3767         done
3768 }
3769 run_test 39e "create, stat, utime, stat ========================"
3770
3771 # bug 21114
3772 test_39f() {
3773         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3774
3775         touch $DIR1/$tfile
3776         mtime1=`stat -c %Y $DIR1/$tfile`
3777
3778         sleep 2
3779         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3780
3781         for (( i=0; i < 2; i++ )) ; do
3782                 local mtime2=`stat -c %Y $DIR1/$tfile`
3783                 [ $mtime2 = $TEST_39_MTIME ] || \
3784                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3785
3786                 cancel_lru_locks $OSC
3787                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3788         done
3789 }
3790 run_test 39f "create, stat, sleep, utime, stat ================="
3791
3792 # bug 11063
3793 test_39g() {
3794         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3795
3796         echo hello >> $DIR1/$tfile
3797         local mtime1=`stat -c %Y $DIR1/$tfile`
3798
3799         sleep 2
3800         chmod o+r $DIR1/$tfile
3801
3802         for (( i=0; i < 2; i++ )) ; do
3803                 local mtime2=`stat -c %Y $DIR1/$tfile`
3804                 [ "$mtime1" = "$mtime2" ] || \
3805                         error "lost mtime: $mtime2, should be $mtime1"
3806
3807                 cancel_lru_locks $OSC
3808                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3809         done
3810 }
3811 run_test 39g "write, chmod, stat ==============================="
3812
3813 # bug 11063
3814 test_39h() {
3815         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3816
3817         touch $DIR1/$tfile
3818         sleep 1
3819
3820         local d1=`date`
3821         echo hello >> $DIR1/$tfile
3822         local mtime1=`stat -c %Y $DIR1/$tfile`
3823
3824         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3825         local d2=`date`
3826         if [ "$d1" != "$d2" ]; then
3827                 echo "write and touch not within one second"
3828         else
3829                 for (( i=0; i < 2; i++ )) ; do
3830                         local mtime2=`stat -c %Y $DIR1/$tfile`
3831                         [ "$mtime2" = $TEST_39_MTIME ] || \
3832                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3833
3834                         cancel_lru_locks $OSC
3835                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3836                 done
3837         fi
3838 }
3839 run_test 39h "write, utime within one second, stat ============="
3840
3841 test_39i() {
3842         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3843
3844         touch $DIR1/$tfile
3845         sleep 1
3846
3847         echo hello >> $DIR1/$tfile
3848         local mtime1=`stat -c %Y $DIR1/$tfile`
3849
3850         mv $DIR1/$tfile $DIR1/$tfile-1
3851
3852         for (( i=0; i < 2; i++ )) ; do
3853                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3854
3855                 [ "$mtime1" = "$mtime2" ] || \
3856                         error "lost mtime: $mtime2, should be $mtime1"
3857
3858                 cancel_lru_locks $OSC
3859                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3860         done
3861 }
3862 run_test 39i "write, rename, stat =============================="
3863
3864 test_39j() {
3865         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3866
3867         start_full_debug_logging
3868         touch $DIR1/$tfile
3869         sleep 1
3870
3871         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3872         lctl set_param fail_loc=0x80000412
3873         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3874                 error "multiop failed"
3875         local multipid=$!
3876         local mtime1=`stat -c %Y $DIR1/$tfile`
3877
3878         mv $DIR1/$tfile $DIR1/$tfile-1
3879
3880         kill -USR1 $multipid
3881         wait $multipid || error "multiop close failed"
3882
3883         for (( i=0; i < 2; i++ )) ; do
3884                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3885                 [ "$mtime1" = "$mtime2" ] ||
3886                         error "mtime is lost on close: $mtime2, " \
3887                               "should be $mtime1"
3888
3889                 cancel_lru_locks $OSC
3890                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3891         done
3892         lctl set_param fail_loc=0
3893         stop_full_debug_logging
3894 }
3895 run_test 39j "write, rename, close, stat ======================="
3896
3897 test_39k() {
3898         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3899
3900         touch $DIR1/$tfile
3901         sleep 1
3902
3903         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3904         local multipid=$!
3905         local mtime1=`stat -c %Y $DIR1/$tfile`
3906
3907         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3908
3909         kill -USR1 $multipid
3910         wait $multipid || error "multiop close failed"
3911
3912         for (( i=0; i < 2; i++ )) ; do
3913                 local mtime2=`stat -c %Y $DIR1/$tfile`
3914
3915                 [ "$mtime2" = $TEST_39_MTIME ] || \
3916                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3917
3918                 cancel_lru_locks osc
3919                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3920         done
3921 }
3922 run_test 39k "write, utime, close, stat ========================"
3923
3924 # this should be set to future
3925 TEST_39_ATIME=`date -d "1 year" +%s`
3926
3927 test_39l() {
3928         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3929         remote_mds_nodsh && skip "remote MDS with nodsh"
3930
3931         local atime_diff=$(do_facet $SINGLEMDS \
3932                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3933         rm -rf $DIR/$tdir
3934         mkdir -p $DIR/$tdir
3935
3936         # test setting directory atime to future
3937         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3938         local atime=$(stat -c %X $DIR/$tdir)
3939         [ "$atime" = $TEST_39_ATIME ] ||
3940                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3941
3942         # test setting directory atime from future to now
3943         local now=$(date +%s)
3944         touch -a -d @$now $DIR/$tdir
3945
3946         atime=$(stat -c %X $DIR/$tdir)
3947         [ "$atime" -eq "$now"  ] ||
3948                 error "atime is not updated from future: $atime, $now"
3949
3950         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3951         sleep 3
3952
3953         # test setting directory atime when now > dir atime + atime_diff
3954         local d1=$(date +%s)
3955         ls $DIR/$tdir
3956         local d2=$(date +%s)
3957         cancel_lru_locks mdc
3958         atime=$(stat -c %X $DIR/$tdir)
3959         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3960                 error "atime is not updated  : $atime, should be $d2"
3961
3962         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3963         sleep 3
3964
3965         # test not setting directory atime when now < dir atime + atime_diff
3966         ls $DIR/$tdir
3967         cancel_lru_locks mdc
3968         atime=$(stat -c %X $DIR/$tdir)
3969         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3970                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3971
3972         do_facet $SINGLEMDS \
3973                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3974 }
3975 run_test 39l "directory atime update ==========================="
3976
3977 test_39m() {
3978         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3979
3980         touch $DIR1/$tfile
3981         sleep 2
3982         local far_past_mtime=$(date -d "May 29 1953" +%s)
3983         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3984
3985         touch -m -d @$far_past_mtime $DIR1/$tfile
3986         touch -a -d @$far_past_atime $DIR1/$tfile
3987
3988         for (( i=0; i < 2; i++ )) ; do
3989                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3990                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3991                         error "atime or mtime set incorrectly"
3992
3993                 cancel_lru_locks $OSC
3994                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3995         done
3996 }
3997 run_test 39m "test atime and mtime before 1970"
3998
3999 test_39n() { # LU-3832
4000         remote_mds_nodsh && skip "remote MDS with nodsh"
4001
4002         local atime_diff=$(do_facet $SINGLEMDS \
4003                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4004         local atime0
4005         local atime1
4006         local atime2
4007
4008         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
4009
4010         rm -rf $DIR/$tfile
4011         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
4012         atime0=$(stat -c %X $DIR/$tfile)
4013
4014         sleep 5
4015         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4016         atime1=$(stat -c %X $DIR/$tfile)
4017
4018         sleep 5
4019         cancel_lru_locks mdc
4020         cancel_lru_locks osc
4021         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4022         atime2=$(stat -c %X $DIR/$tfile)
4023
4024         do_facet $SINGLEMDS \
4025                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4026
4027         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
4028         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
4029 }
4030 run_test 39n "check that O_NOATIME is honored"
4031
4032 test_39o() {
4033         TESTDIR=$DIR/$tdir/$tfile
4034         [ -e $TESTDIR ] && rm -rf $TESTDIR
4035         mkdir -p $TESTDIR
4036         cd $TESTDIR
4037         links1=2
4038         ls
4039         mkdir a b
4040         ls
4041         links2=$(stat -c %h .)
4042         [ $(($links1 + 2)) != $links2 ] &&
4043                 error "wrong links count $(($links1 + 2)) != $links2"
4044         rmdir b
4045         links3=$(stat -c %h .)
4046         [ $(($links1 + 1)) != $links3 ] &&
4047                 error "wrong links count $links1 != $links3"
4048         return 0
4049 }
4050 run_test 39o "directory cached attributes updated after create"
4051
4052 test_39p() {
4053         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4054
4055         local MDTIDX=1
4056         TESTDIR=$DIR/$tdir/$tdir
4057         [ -e $TESTDIR ] && rm -rf $TESTDIR
4058         test_mkdir -p $TESTDIR
4059         cd $TESTDIR
4060         links1=2
4061         ls
4062         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
4063         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
4064         ls
4065         links2=$(stat -c %h .)
4066         [ $(($links1 + 2)) != $links2 ] &&
4067                 error "wrong links count $(($links1 + 2)) != $links2"
4068         rmdir remote_dir2
4069         links3=$(stat -c %h .)
4070         [ $(($links1 + 1)) != $links3 ] &&
4071                 error "wrong links count $links1 != $links3"
4072         return 0
4073 }
4074 run_test 39p "remote directory cached attributes updated after create ========"
4075
4076
4077 test_39q() { # LU-8041
4078         local testdir=$DIR/$tdir
4079         mkdir -p $testdir
4080         multiop_bg_pause $testdir D_c || error "multiop failed"
4081         local multipid=$!
4082         cancel_lru_locks mdc
4083         kill -USR1 $multipid
4084         local atime=$(stat -c %X $testdir)
4085         [ "$atime" -ne 0 ] || error "atime is zero"
4086 }
4087 run_test 39q "close won't zero out atime"
4088
4089 test_40() {
4090         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
4091         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
4092                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
4093         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
4094                 error "$tfile is not 4096 bytes in size"
4095 }
4096 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
4097
4098 test_41() {
4099         # bug 1553
4100         small_write $DIR/f41 18
4101 }
4102 run_test 41 "test small file write + fstat ====================="
4103
4104 count_ost_writes() {
4105         lctl get_param -n ${OSC}.*.stats |
4106                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
4107                         END { printf("%0.0f", writes) }'
4108 }
4109
4110 # decent default
4111 WRITEBACK_SAVE=500
4112 DIRTY_RATIO_SAVE=40
4113 MAX_DIRTY_RATIO=50
4114 BG_DIRTY_RATIO_SAVE=10
4115 MAX_BG_DIRTY_RATIO=25
4116
4117 start_writeback() {
4118         trap 0
4119         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
4120         # dirty_ratio, dirty_background_ratio
4121         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4122                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
4123                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
4124                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
4125         else
4126                 # if file not here, we are a 2.4 kernel
4127                 kill -CONT `pidof kupdated`
4128         fi
4129 }
4130
4131 stop_writeback() {
4132         # setup the trap first, so someone cannot exit the test at the
4133         # exact wrong time and mess up a machine
4134         trap start_writeback EXIT
4135         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
4136         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4137                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
4138                 sysctl -w vm.dirty_writeback_centisecs=0
4139                 sysctl -w vm.dirty_writeback_centisecs=0
4140                 # save and increase /proc/sys/vm/dirty_ratio
4141                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
4142                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
4143                 # save and increase /proc/sys/vm/dirty_background_ratio
4144                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
4145                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
4146         else
4147                 # if file not here, we are a 2.4 kernel
4148                 kill -STOP `pidof kupdated`
4149         fi
4150 }
4151
4152 # ensure that all stripes have some grant before we test client-side cache
4153 setup_test42() {
4154         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
4155                 dd if=/dev/zero of=$i bs=4k count=1
4156                 rm $i
4157         done
4158 }
4159
4160 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
4161 # file truncation, and file removal.
4162 test_42a() {
4163         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4164
4165         setup_test42
4166         cancel_lru_locks $OSC
4167         stop_writeback
4168         sync; sleep 1; sync # just to be safe
4169         BEFOREWRITES=`count_ost_writes`
4170         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
4171         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
4172         AFTERWRITES=`count_ost_writes`
4173         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
4174                 error "$BEFOREWRITES < $AFTERWRITES"
4175         start_writeback
4176 }
4177 run_test 42a "ensure that we don't flush on close"
4178
4179 test_42b() {
4180         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4181
4182         setup_test42
4183         cancel_lru_locks $OSC
4184         stop_writeback
4185         sync
4186         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
4187         BEFOREWRITES=$(count_ost_writes)
4188         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
4189         AFTERWRITES=$(count_ost_writes)
4190         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4191                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
4192         fi
4193         BEFOREWRITES=$(count_ost_writes)
4194         sync || error "sync: $?"
4195         AFTERWRITES=$(count_ost_writes)
4196         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4197                 error "$BEFOREWRITES < $AFTERWRITES on sync"
4198         fi
4199         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
4200         start_writeback
4201         return 0
4202 }
4203 run_test 42b "test destroy of file with cached dirty data ======"
4204
4205 # if these tests just want to test the effect of truncation,
4206 # they have to be very careful.  consider:
4207 # - the first open gets a {0,EOF}PR lock
4208 # - the first write conflicts and gets a {0, count-1}PW
4209 # - the rest of the writes are under {count,EOF}PW
4210 # - the open for truncate tries to match a {0,EOF}PR
4211 #   for the filesize and cancels the PWs.
4212 # any number of fixes (don't get {0,EOF} on open, match
4213 # composite locks, do smarter file size management) fix
4214 # this, but for now we want these tests to verify that
4215 # the cancellation with truncate intent works, so we
4216 # start the file with a full-file pw lock to match against
4217 # until the truncate.
4218 trunc_test() {
4219         test=$1
4220         file=$DIR/$test
4221         offset=$2
4222         cancel_lru_locks $OSC
4223         stop_writeback
4224         # prime the file with 0,EOF PW to match
4225         touch $file
4226         $TRUNCATE $file 0
4227         sync; sync
4228         # now the real test..
4229         dd if=/dev/zero of=$file bs=1024 count=100
4230         BEFOREWRITES=`count_ost_writes`
4231         $TRUNCATE $file $offset
4232         cancel_lru_locks $OSC
4233         AFTERWRITES=`count_ost_writes`
4234         start_writeback
4235 }
4236
4237 test_42c() {
4238         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4239
4240         trunc_test 42c 1024
4241         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
4242                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
4243         rm $file
4244 }
4245 run_test 42c "test partial truncate of file with cached dirty data"
4246
4247 test_42d() {
4248         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4249
4250         trunc_test 42d 0
4251         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
4252                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
4253         rm $file
4254 }
4255 run_test 42d "test complete truncate of file with cached dirty data"
4256
4257 test_42e() { # bug22074
4258         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4259
4260         local TDIR=$DIR/${tdir}e
4261         local pages=16 # hardcoded 16 pages, don't change it.
4262         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
4263         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
4264         local max_dirty_mb
4265         local warmup_files
4266
4267         test_mkdir $DIR/${tdir}e
4268         $LFS setstripe -c 1 $TDIR
4269         createmany -o $TDIR/f $files
4270
4271         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
4272
4273         # we assume that with $OSTCOUNT files, at least one of them will
4274         # be allocated on OST0.
4275         warmup_files=$((OSTCOUNT * max_dirty_mb))
4276         createmany -o $TDIR/w $warmup_files
4277
4278         # write a large amount of data into one file and sync, to get good
4279         # avail_grant number from OST.
4280         for ((i=0; i<$warmup_files; i++)); do
4281                 idx=$($LFS getstripe -i $TDIR/w$i)
4282                 [ $idx -ne 0 ] && continue
4283                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
4284                 break
4285         done
4286         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
4287         sync
4288         $LCTL get_param $proc_osc0/cur_dirty_bytes
4289         $LCTL get_param $proc_osc0/cur_grant_bytes
4290
4291         # create as much dirty pages as we can while not to trigger the actual
4292         # RPCs directly. but depends on the env, VFS may trigger flush during this
4293         # period, hopefully we are good.
4294         for ((i=0; i<$warmup_files; i++)); do
4295                 idx=$($LFS getstripe -i $TDIR/w$i)
4296                 [ $idx -ne 0 ] && continue
4297                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4298         done
4299         $LCTL get_param $proc_osc0/cur_dirty_bytes
4300         $LCTL get_param $proc_osc0/cur_grant_bytes
4301
4302         # perform the real test
4303         $LCTL set_param $proc_osc0/rpc_stats 0
4304         for ((;i<$files; i++)); do
4305                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
4306                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
4307         done
4308         sync
4309         $LCTL get_param $proc_osc0/rpc_stats
4310
4311         local percent=0
4312         local have_ppr=false
4313         $LCTL get_param $proc_osc0/rpc_stats |
4314                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4315                         # skip lines until we are at the RPC histogram data
4316                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4317                         $have_ppr || continue
4318
4319                         # we only want the percent stat for < 16 pages
4320                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4321
4322                         percent=$((percent + WPCT))
4323                         if [[ $percent -gt 15 ]]; then
4324                                 error "less than 16-pages write RPCs" \
4325                                       "$percent% > 15%"
4326                                 break
4327                         fi
4328                 done
4329         rm -rf $TDIR
4330 }
4331 run_test 42e "verify sub-RPC writes are not done synchronously"
4332
4333 test_43A() { # was test_43
4334         test_mkdir $DIR/$tdir
4335         cp -p /bin/ls $DIR/$tdir/$tfile
4336         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4337         pid=$!
4338         # give multiop a chance to open
4339         sleep 1
4340
4341         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4342         kill -USR1 $pid
4343 }
4344 run_test 43A "execution of file opened for write should return -ETXTBSY"
4345
4346 test_43a() {
4347         test_mkdir $DIR/$tdir
4348         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4349         $DIR/$tdir/sleep 60 &
4350         SLEEP_PID=$!
4351         # Make sure exec of $tdir/sleep wins race with truncate
4352         sleep 1
4353         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
4354         kill $SLEEP_PID
4355 }
4356 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4357
4358 test_43b() {
4359         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4360
4361         test_mkdir $DIR/$tdir
4362         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4363         $DIR/$tdir/sleep 60 &
4364         SLEEP_PID=$!
4365         # Make sure exec of $tdir/sleep wins race with truncate
4366         sleep 1
4367         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
4368         kill $SLEEP_PID
4369 }
4370 run_test 43b "truncate of file being executed should return -ETXTBSY"
4371
4372 test_43c() {
4373         local testdir="$DIR/$tdir"
4374         test_mkdir $testdir
4375         cp $SHELL $testdir/
4376         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4377                 ( cd $testdir && md5sum -c )
4378 }
4379 run_test 43c "md5sum of copy into lustre"
4380
4381 test_44A() { # was test_44
4382         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4383
4384         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4385         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4386 }
4387 run_test 44A "zero length read from a sparse stripe"
4388
4389 test_44a() {
4390         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4391                 awk '{ print $2 }')
4392         [ -z "$nstripe" ] && skip "can't get stripe info"
4393         [[ $nstripe -gt $OSTCOUNT ]] &&
4394                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4395
4396         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4397                 awk '{ print $2 }')
4398         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4399                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4400                         awk '{ print $2 }')
4401         fi
4402
4403         OFFSETS="0 $((stride/2)) $((stride-1))"
4404         for offset in $OFFSETS; do
4405                 for i in $(seq 0 $((nstripe-1))); do
4406                         local GLOBALOFFSETS=""
4407                         # size in Bytes
4408                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4409                         local myfn=$DIR/d44a-$size
4410                         echo "--------writing $myfn at $size"
4411                         ll_sparseness_write $myfn $size ||
4412                                 error "ll_sparseness_write"
4413                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4414                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4415                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4416
4417                         for j in $(seq 0 $((nstripe-1))); do
4418                                 # size in Bytes
4419                                 size=$((((j + $nstripe )*$stride + $offset)))
4420                                 ll_sparseness_write $myfn $size ||
4421                                         error "ll_sparseness_write"
4422                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4423                         done
4424                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4425                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4426                         rm -f $myfn
4427                 done
4428         done
4429 }
4430 run_test 44a "test sparse pwrite ==============================="
4431
4432 dirty_osc_total() {
4433         tot=0
4434         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4435                 tot=$(($tot + $d))
4436         done
4437         echo $tot
4438 }
4439 do_dirty_record() {
4440         before=`dirty_osc_total`
4441         echo executing "\"$*\""
4442         eval $*
4443         after=`dirty_osc_total`
4444         echo before $before, after $after
4445 }
4446 test_45() {
4447         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4448
4449         f="$DIR/f45"
4450         # Obtain grants from OST if it supports it
4451         echo blah > ${f}_grant
4452         stop_writeback
4453         sync
4454         do_dirty_record "echo blah > $f"
4455         [[ $before -eq $after ]] && error "write wasn't cached"
4456         do_dirty_record "> $f"
4457         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4458         do_dirty_record "echo blah > $f"
4459         [[ $before -eq $after ]] && error "write wasn't cached"
4460         do_dirty_record "sync"
4461         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4462         do_dirty_record "echo blah > $f"
4463         [[ $before -eq $after ]] && error "write wasn't cached"
4464         do_dirty_record "cancel_lru_locks osc"
4465         [[ $before -gt $after ]] ||
4466                 error "lock cancellation didn't lower dirty count"
4467         start_writeback
4468 }
4469 run_test 45 "osc io page accounting ============================"
4470
4471 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4472 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4473 # objects offset and an assert hit when an rpc was built with 1023's mapped
4474 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4475 test_46() {
4476         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4477
4478         f="$DIR/f46"
4479         stop_writeback
4480         sync
4481         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4482         sync
4483         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
4484         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4485         sync
4486         start_writeback
4487 }
4488 run_test 46 "dirtying a previously written page ================"
4489
4490 # test_47 is removed "Device nodes check" is moved to test_28
4491
4492 test_48a() { # bug 2399
4493         [ "$mds1_FSTYPE" = "zfs" ] &&
4494         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
4495                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
4496
4497         test_mkdir $DIR/$tdir
4498         cd $DIR/$tdir
4499         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
4500         test_mkdir $DIR/$tdir
4501         touch foo || error "'touch foo' failed after recreating cwd"
4502         test_mkdir bar
4503         touch .foo || error "'touch .foo' failed after recreating cwd"
4504         test_mkdir .bar
4505         ls . > /dev/null || error "'ls .' failed after recreating cwd"
4506         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4507         cd . || error "'cd .' failed after recreating cwd"
4508         mkdir . && error "'mkdir .' worked after recreating cwd"
4509         rmdir . && error "'rmdir .' worked after recreating cwd"
4510         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4511         cd .. || error "'cd ..' failed after recreating cwd"
4512 }
4513 run_test 48a "Access renamed working dir (should return errors)="
4514
4515 test_48b() { # bug 2399
4516         rm -rf $DIR/$tdir
4517         test_mkdir $DIR/$tdir
4518         cd $DIR/$tdir
4519         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4520         touch foo && error "'touch foo' worked after removing cwd"
4521         mkdir foo && error "'mkdir foo' worked after removing cwd"
4522         touch .foo && error "'touch .foo' worked after removing cwd"
4523         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4524         ls . > /dev/null && error "'ls .' worked after removing cwd"
4525         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4526         mkdir . && error "'mkdir .' worked after removing cwd"
4527         rmdir . && error "'rmdir .' worked after removing cwd"
4528         ln -s . foo && error "'ln -s .' worked after removing cwd"
4529         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4530 }
4531 run_test 48b "Access removed working dir (should return errors)="
4532
4533 test_48c() { # bug 2350
4534         #lctl set_param debug=-1
4535         #set -vx
4536         rm -rf $DIR/$tdir
4537         test_mkdir -p $DIR/$tdir/dir
4538         cd $DIR/$tdir/dir
4539         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4540         $TRACE touch foo && error "touch foo worked after removing cwd"
4541         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4542         touch .foo && error "touch .foo worked after removing cwd"
4543         mkdir .foo && error "mkdir .foo worked after removing cwd"
4544         $TRACE ls . && error "'ls .' worked after removing cwd"
4545         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4546         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4547         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4548         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4549         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4550 }
4551 run_test 48c "Access removed working subdir (should return errors)"
4552
4553 test_48d() { # bug 2350
4554         #lctl set_param debug=-1
4555         #set -vx
4556         rm -rf $DIR/$tdir
4557         test_mkdir -p $DIR/$tdir/dir
4558         cd $DIR/$tdir/dir
4559         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4560         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4561         $TRACE touch foo && error "'touch foo' worked after removing parent"
4562         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4563         touch .foo && error "'touch .foo' worked after removing parent"
4564         mkdir .foo && error "mkdir .foo worked after removing parent"
4565         $TRACE ls . && error "'ls .' worked after removing parent"
4566         $TRACE ls .. && error "'ls ..' worked after removing parent"
4567         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
4568         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4569         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4570         true
4571 }
4572 run_test 48d "Access removed parent subdir (should return errors)"
4573
4574 test_48e() { # bug 4134
4575         #lctl set_param debug=-1
4576         #set -vx
4577         rm -rf $DIR/$tdir
4578         test_mkdir -p $DIR/$tdir/dir
4579         cd $DIR/$tdir/dir
4580         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4581         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4582         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4583         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4584         # On a buggy kernel addition of "touch foo" after cd .. will
4585         # produce kernel oops in lookup_hash_it
4586         touch ../foo && error "'cd ..' worked after recreate parent"
4587         cd $DIR
4588         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4589 }
4590 run_test 48e "Access to recreated parent subdir (should return errors)"
4591
4592 test_49() { # LU-1030
4593         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4594         remote_ost_nodsh && skip "remote OST with nodsh"
4595
4596         # get ost1 size - lustre-OST0000
4597         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4598                 awk '{ print $4 }')
4599         # write 800M at maximum
4600         [[ $ost1_size -lt 2 ]] && ost1_size=2
4601         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4602
4603         $LFS setstripe -c 1 -i 0 $DIR/$tfile
4604         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4605         local dd_pid=$!
4606
4607         # change max_pages_per_rpc while writing the file
4608         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4609         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4610         # loop until dd process exits
4611         while ps ax -opid | grep -wq $dd_pid; do
4612                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4613                 sleep $((RANDOM % 5 + 1))
4614         done
4615         # restore original max_pages_per_rpc
4616         $LCTL set_param $osc1_mppc=$orig_mppc
4617         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4618 }
4619 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4620
4621 test_50() {
4622         # bug 1485
4623         test_mkdir $DIR/$tdir
4624         cd $DIR/$tdir
4625         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4626 }
4627 run_test 50 "special situations: /proc symlinks  ==============="
4628
4629 test_51a() {    # was test_51
4630         # bug 1516 - create an empty entry right after ".." then split dir
4631         test_mkdir -c1 $DIR/$tdir
4632         touch $DIR/$tdir/foo
4633         $MCREATE $DIR/$tdir/bar
4634         rm $DIR/$tdir/foo
4635         createmany -m $DIR/$tdir/longfile 201
4636         FNUM=202
4637         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4638                 $MCREATE $DIR/$tdir/longfile$FNUM
4639                 FNUM=$(($FNUM + 1))
4640                 echo -n "+"
4641         done
4642         echo
4643         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4644 }
4645 run_test 51a "special situations: split htree with empty entry =="
4646
4647 cleanup_print_lfs_df () {
4648         trap 0
4649         $LFS df
4650         $LFS df -i
4651 }
4652
4653 test_51b() {
4654         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4655
4656         local dir=$DIR/$tdir
4657         local nrdirs=$((65536 + 100))
4658
4659         # cleanup the directory
4660         rm -fr $dir
4661
4662         test_mkdir -c1 $dir
4663
4664         $LFS df
4665         $LFS df -i
4666         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
4667         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4668         [[ $numfree -lt $nrdirs ]] &&
4669                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
4670
4671         # need to check free space for the directories as well
4672         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4673         numfree=$(( blkfree / $(fs_inode_ksize) ))
4674         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
4675
4676         trap cleanup_print_lfs_df EXIT
4677
4678         # create files
4679         createmany -d $dir/d $nrdirs || {
4680                 unlinkmany $dir/d $nrdirs
4681                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
4682         }
4683
4684         # really created :
4685         nrdirs=$(ls -U $dir | wc -l)
4686
4687         # unlink all but 100 subdirectories, then check it still works
4688         local left=100
4689         local delete=$((nrdirs - left))
4690
4691         $LFS df
4692         $LFS df -i
4693
4694         # for ldiskfs the nlink count should be 1, but this is OSD specific
4695         # and so this is listed for informational purposes only
4696         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
4697         unlinkmany -d $dir/d $delete ||
4698                 error "unlink of first $delete subdirs failed"
4699
4700         echo "nlink between: $(stat -c %h $dir)"
4701         local found=$(ls -U $dir | wc -l)
4702         [ $found -ne $left ] &&
4703                 error "can't find subdirs: found only $found, expected $left"
4704
4705         unlinkmany -d $dir/d $delete $left ||
4706                 error "unlink of second $left subdirs failed"
4707         # regardless of whether the backing filesystem tracks nlink accurately
4708         # or not, the nlink count shouldn't be more than "." and ".." here
4709         local after=$(stat -c %h $dir)
4710         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
4711                 echo "nlink after: $after"
4712
4713         cleanup_print_lfs_df
4714 }
4715 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
4716
4717 test_51d() {
4718         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4719         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
4720
4721         test_mkdir $DIR/$tdir
4722         createmany -o $DIR/$tdir/t- 1000
4723         $LFS getstripe $DIR/$tdir > $TMP/$tfile
4724         for N in $(seq 0 $((OSTCOUNT - 1))); do
4725                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4726                         END { printf("%0.0f", objs) }' $TMP/$tfile)
4727                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
4728                         '($1 == '$N') { objs += 1 } \
4729                         END { printf("%0.0f", objs) }')
4730                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4731         done
4732         unlinkmany $DIR/$tdir/t- 1000
4733
4734         NLAST=0
4735         for N in $(seq 1 $((OSTCOUNT - 1))); do
4736                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4737                         error "OST $N has less objects vs OST $NLAST" \
4738                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4739                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4740                         error "OST $N has less objects vs OST $NLAST" \
4741                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4742
4743                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4744                         error "OST $N has less #0 objects vs OST $NLAST" \
4745                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4746                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4747                         error "OST $N has less #0 objects vs OST $NLAST" \
4748                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4749                 NLAST=$N
4750         done
4751         rm -f $TMP/$tfile
4752 }
4753 run_test 51d "check object distribution"
4754
4755 test_51e() {
4756         if [ "$mds1_FSTYPE" != ldiskfs ]; then
4757                 skip_env "ldiskfs only test"
4758         fi
4759
4760         test_mkdir -c1 $DIR/$tdir
4761         test_mkdir -c1 $DIR/$tdir/d0
4762
4763         touch $DIR/$tdir/d0/foo
4764         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4765                 error "file exceed 65000 nlink limit!"
4766         unlinkmany $DIR/$tdir/d0/f- 65001
4767         return 0
4768 }
4769 run_test 51e "check file nlink limit"
4770
4771 test_51f() {
4772         test_mkdir $DIR/$tdir
4773
4774         local max=100000
4775         local ulimit_old=$(ulimit -n)
4776         local spare=20 # number of spare fd's for scripts/libraries, etc.
4777         local mdt=$($LFS getstripe -m $DIR/$tdir)
4778         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
4779
4780         echo "MDT$mdt numfree=$numfree, max=$max"
4781         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
4782         if [ $((numfree + spare)) -gt $ulimit_old ]; then
4783                 while ! ulimit -n $((numfree + spare)); do
4784                         numfree=$((numfree * 3 / 4))
4785                 done
4786                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
4787         else
4788                 echo "left ulimit at $ulimit_old"
4789         fi
4790
4791         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
4792                 unlinkmany $DIR/$tdir/f $numfree
4793                 error "create+open $numfree files in $DIR/$tdir failed"
4794         }
4795         ulimit -n $ulimit_old
4796
4797         # if createmany exits at 120s there will be fewer than $numfree files
4798         unlinkmany $DIR/$tdir/f $numfree || true
4799 }
4800 run_test 51f "check many open files limit"
4801
4802 test_52a() {
4803         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4804         test_mkdir $DIR/$tdir
4805         touch $DIR/$tdir/foo
4806         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4807         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4808         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4809         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4810         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4811                                         error "link worked"
4812         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4813         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4814         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4815                                                      error "lsattr"
4816         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4817         cp -r $DIR/$tdir $TMP/
4818         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
4819 }
4820 run_test 52a "append-only flag test (should return errors)"
4821
4822 test_52b() {
4823         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4824         test_mkdir $DIR/$tdir
4825         touch $DIR/$tdir/foo
4826         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4827         cat test > $DIR/$tdir/foo && error "cat test worked"
4828         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4829         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4830         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4831                                         error "link worked"
4832         echo foo >> $DIR/$tdir/foo && error "echo worked"
4833         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4834         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4835         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4836         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4837                                                         error "lsattr"
4838         chattr -i $DIR/$tdir/foo || error "chattr failed"
4839
4840         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4841 }
4842 run_test 52b "immutable flag test (should return errors) ======="
4843
4844 test_53() {
4845         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4846         remote_mds_nodsh && skip "remote MDS with nodsh"
4847         remote_ost_nodsh && skip "remote OST with nodsh"
4848
4849         local param
4850         local param_seq
4851         local ostname
4852         local mds_last
4853         local mds_last_seq
4854         local ost_last
4855         local ost_last_seq
4856         local ost_last_id
4857         local ostnum
4858         local node
4859         local found=false
4860         local support_last_seq=true
4861
4862         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
4863                 support_last_seq=false
4864
4865         # only test MDT0000
4866         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4867         local value
4868         for value in $(do_facet $SINGLEMDS \
4869                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4870                 param=$(echo ${value[0]} | cut -d "=" -f1)
4871                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4872
4873                 if $support_last_seq; then
4874                         param_seq=$(echo $param |
4875                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4876                         mds_last_seq=$(do_facet $SINGLEMDS \
4877                                        $LCTL get_param -n $param_seq)
4878                 fi
4879                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4880
4881                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4882                 node=$(facet_active_host ost$((ostnum+1)))
4883                 param="obdfilter.$ostname.last_id"
4884                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4885                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4886                         ost_last_id=$ost_last
4887
4888                         if $support_last_seq; then
4889                                 ost_last_id=$(echo $ost_last |
4890                                               awk -F':' '{print $2}' |
4891                                               sed -e "s/^0x//g")
4892                                 ost_last_seq=$(echo $ost_last |
4893                                                awk -F':' '{print $1}')
4894                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4895                         fi
4896
4897                         if [[ $ost_last_id != $mds_last ]]; then
4898                                 error "$ost_last_id != $mds_last"
4899                         else
4900                                 found=true
4901                                 break
4902                         fi
4903                 done
4904         done
4905         $found || error "can not match last_seq/last_id for $mdtosc"
4906         return 0
4907 }
4908 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4909
4910 test_54a() {
4911         perl -MSocket -e ';' || skip "no Socket perl module installed"
4912
4913         $SOCKETSERVER $DIR/socket ||
4914                 error "$SOCKETSERVER $DIR/socket failed: $?"
4915         $SOCKETCLIENT $DIR/socket ||
4916                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4917         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4918 }
4919 run_test 54a "unix domain socket test =========================="
4920
4921 test_54b() {
4922         f="$DIR/f54b"
4923         mknod $f c 1 3
4924         chmod 0666 $f
4925         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
4926 }
4927 run_test 54b "char device works in lustre ======================"
4928
4929 find_loop_dev() {
4930         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4931         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4932         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4933
4934         for i in $(seq 3 7); do
4935                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4936                 LOOPDEV=$LOOPBASE$i
4937                 LOOPNUM=$i
4938                 break
4939         done
4940 }
4941
4942 cleanup_54c() {
4943         local rc=0
4944         loopdev="$DIR/loop54c"
4945
4946         trap 0
4947         $UMOUNT $DIR/$tdir || rc=$?
4948         losetup -d $loopdev || true
4949         losetup -d $LOOPDEV || true
4950         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4951         return $rc
4952 }
4953
4954 test_54c() {
4955         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4956
4957         loopdev="$DIR/loop54c"
4958
4959         find_loop_dev
4960         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
4961         trap cleanup_54c EXIT
4962         mknod $loopdev b 7 $LOOPNUM
4963         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4964         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
4965         losetup $loopdev $DIR/$tfile ||
4966                 error "can't set up $loopdev for $DIR/$tfile"
4967         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4968         test_mkdir $DIR/$tdir
4969         mount -t ext2 $loopdev $DIR/$tdir ||
4970                 error "error mounting $loopdev on $DIR/$tdir"
4971         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
4972                 error "dd write"
4973         df $DIR/$tdir
4974         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
4975                 error "dd read"
4976         cleanup_54c
4977 }
4978 run_test 54c "block device works in lustre ====================="
4979
4980 test_54d() {
4981         f="$DIR/f54d"
4982         string="aaaaaa"
4983         mknod $f p
4984         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4985 }
4986 run_test 54d "fifo device works in lustre ======================"
4987
4988 test_54e() {
4989         f="$DIR/f54e"
4990         string="aaaaaa"
4991         cp -aL /dev/console $f
4992         echo $string > $f || error "echo $string to $f failed"
4993 }
4994 run_test 54e "console/tty device works in lustre ======================"
4995
4996 test_56a() {
4997         local numfiles=3
4998         local dir=$DIR/$tdir
4999
5000         rm -rf $dir
5001         test_mkdir -p $dir/dir
5002         for i in $(seq $numfiles); do
5003                 touch $dir/file$i
5004                 touch $dir/dir/file$i
5005         done
5006
5007         local numcomp=$($LFS getstripe --component-count $dir)
5008
5009         [[ $numcomp == 0 ]] && numcomp=1
5010
5011         # test lfs getstripe with --recursive
5012         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
5013
5014         [[ $filenum -eq $((numfiles * 2)) ]] ||
5015                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
5016         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
5017         [[ $filenum -eq $numfiles ]] ||
5018                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
5019         echo "$LFS getstripe showed obdidx or l_ost_idx"
5020
5021         # test lfs getstripe with file instead of dir
5022         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
5023         [[ $filenum -eq 1 ]] ||
5024                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
5025         echo "$LFS getstripe file1 passed"
5026
5027         #test lfs getstripe with --verbose
5028         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
5029         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5030                 error "$LFS getstripe --verbose $dir: "\
5031                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
5032         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
5033                 error "$LFS getstripe $dir: showed lmm_magic"
5034
5035         #test lfs getstripe with -v prints lmm_fid
5036         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
5037         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5038                 error "$LFS getstripe -v $dir: "\
5039                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
5040         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
5041                 error "$LFS getstripe $dir: showed lmm_fid by default"
5042         echo "$LFS getstripe --verbose passed"
5043
5044         #check for FID information
5045         local fid1=$($LFS getstripe --fid $dir/file1)
5046         local fid2=$($LFS getstripe --verbose $dir/file1 |
5047                      awk '/lmm_fid: / { print $2; exit; }')
5048         local fid3=$($LFS path2fid $dir/file1)
5049
5050         [ "$fid1" != "$fid2" ] &&
5051                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
5052         [ "$fid1" != "$fid3" ] &&
5053                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
5054         echo "$LFS getstripe --fid passed"
5055
5056         #test lfs getstripe with --obd
5057         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
5058                 error "$LFS getstripe --obd wrong_uuid: should return error"
5059
5060         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5061
5062         local ostidx=1
5063         local obduuid=$(ostuuid_from_index $ostidx)
5064         local found=$($LFS getstripe -r --obd $obduuid $dir |
5065                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
5066
5067         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
5068         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
5069                 ((filenum--))
5070         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
5071                 ((filenum--))
5072
5073         [[ $found -eq $filenum ]] ||
5074                 error "$LFS getstripe --obd: found $found expect $filenum"
5075         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
5076                 sed '/^[         ]*'${ostidx}'[  ]/d' |
5077                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
5078                 error "$LFS getstripe --obd: should not show file on other obd"
5079         echo "$LFS getstripe --obd passed"
5080 }
5081 run_test 56a "check $LFS getstripe"
5082
5083 test_56b() {
5084         local dir=$DIR/$tdir
5085         local numdirs=3
5086
5087         test_mkdir $dir
5088         for i in $(seq $numdirs); do
5089                 test_mkdir $dir/dir$i
5090         done
5091
5092         # test lfs getdirstripe default mode is non-recursion, which is
5093         # different from lfs getstripe
5094         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
5095
5096         [[ $dircnt -eq 1 ]] ||
5097                 error "$LFS getdirstripe: found $dircnt, not 1"
5098         dircnt=$($LFS getdirstripe --recursive $dir |
5099                 grep -c lmv_stripe_count)
5100         [[ $dircnt -eq $((numdirs + 1)) ]] ||
5101                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
5102 }
5103 run_test 56b "check $LFS getdirstripe"
5104
5105 test_56c() {
5106         remote_ost_nodsh && skip "remote OST with nodsh"
5107
5108         local ost_idx=0
5109         local ost_name=$(ostname_from_index $ost_idx)
5110         local old_status=$(ost_dev_status $ost_idx)
5111
5112         [[ -z "$old_status" ]] ||
5113                 skip_env "OST $ost_name is in $old_status status"
5114
5115         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
5116         sleep_maxage
5117
5118         local new_status=$(ost_dev_status $ost_idx)
5119
5120         [[ "$new_status" = "D" ]] ||
5121                 error "OST $ost_name is in status of '$new_status', not 'D'"
5122
5123         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
5124         sleep_maxage
5125
5126         new_status=$(ost_dev_status $ost_idx)
5127         [[ -z "$new_status" ]] ||
5128                 error "OST $ost_name is in status of '$new_status', not ''"
5129 }
5130 run_test 56c "check 'lfs df' showing device status"
5131
5132 NUMFILES=3
5133 NUMDIRS=3
5134 setup_56() {
5135         local local_tdir="$1"
5136         local local_numfiles="$2"
5137         local local_numdirs="$3"
5138         local dir_params="$4"
5139         local dir_stripe_params="$5"
5140
5141         if [ ! -d "$local_tdir" ] ; then
5142                 test_mkdir -p $dir_stripe_params $local_tdir
5143                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
5144                 for i in $(seq $local_numfiles) ; do
5145                         touch $local_tdir/file$i
5146                 done
5147                 for i in $(seq $local_numdirs) ; do
5148                         test_mkdir $dir_stripe_params $local_tdir/dir$i
5149                         for j in $(seq $local_numfiles) ; do
5150                                 touch $local_tdir/dir$i/file$j
5151                         done
5152                 done
5153         fi
5154 }
5155
5156 setup_56_special() {
5157         local local_tdir=$1
5158         local local_numfiles=$2
5159         local local_numdirs=$3
5160
5161         setup_56 $local_tdir $local_numfiles $local_numdirs
5162
5163         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
5164                 for i in $(seq $local_numfiles) ; do
5165                         mknod $local_tdir/loop${i}b b 7 $i
5166                         mknod $local_tdir/null${i}c c 1 3
5167                         ln -s $local_tdir/file1 $local_tdir/link${i}
5168                 done
5169                 for i in $(seq $local_numdirs) ; do
5170                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
5171                         mknod $local_tdir/dir$i/null${i}c c 1 3
5172                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
5173                 done
5174         fi
5175 }
5176
5177 test_56g() {
5178         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5179         local expected=$(($NUMDIRS + 2))
5180
5181         setup_56 $dir $NUMFILES $NUMDIRS
5182
5183         # test lfs find with -name
5184         for i in $(seq $NUMFILES) ; do
5185                 local nums=$($LFS find -name "*$i" $dir | wc -l)
5186
5187                 [ $nums -eq $expected ] ||
5188                         error "lfs find -name '*$i' $dir wrong: "\
5189                               "found $nums, expected $expected"
5190         done
5191 }
5192 run_test 56g "check lfs find -name"
5193
5194 test_56h() {
5195         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5196         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
5197
5198         setup_56 $dir $NUMFILES $NUMDIRS
5199
5200         # test lfs find with ! -name
5201         for i in $(seq $NUMFILES) ; do
5202                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
5203
5204                 [ $nums -eq $expected ] ||
5205                         error "lfs find ! -name '*$i' $dir wrong: "\
5206                               "found $nums, expected $expected"
5207         done
5208 }
5209 run_test 56h "check lfs find ! -name"
5210
5211 test_56i() {
5212         local dir=$DIR/$tdir
5213
5214         test_mkdir $dir
5215
5216         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
5217         local out=$($cmd)
5218
5219         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
5220 }
5221 run_test 56i "check 'lfs find -ost UUID' skips directories"
5222
5223 test_56j() {
5224         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5225
5226         setup_56_special $dir $NUMFILES $NUMDIRS
5227
5228         local expected=$((NUMDIRS + 1))
5229         local cmd="$LFS find -type d $dir"
5230         local nums=$($cmd | wc -l)
5231
5232         [ $nums -eq $expected ] ||
5233                 error "'$cmd' wrong: found $nums, expected $expected"
5234 }
5235 run_test 56j "check lfs find -type d"
5236
5237 test_56k() {
5238         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5239
5240         setup_56_special $dir $NUMFILES $NUMDIRS
5241
5242         local expected=$(((NUMDIRS + 1) * NUMFILES))
5243         local cmd="$LFS find -type f $dir"
5244         local nums=$($cmd | wc -l)
5245
5246         [ $nums -eq $expected ] ||
5247                 error "'$cmd' wrong: found $nums, expected $expected"
5248 }
5249 run_test 56k "check lfs find -type f"
5250
5251 test_56l() {
5252         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5253
5254         setup_56_special $dir $NUMFILES $NUMDIRS
5255
5256         local expected=$((NUMDIRS + NUMFILES))
5257         local cmd="$LFS find -type b $dir"
5258         local nums=$($cmd | wc -l)
5259
5260         [ $nums -eq $expected ] ||
5261                 error "'$cmd' wrong: found $nums, expected $expected"
5262 }
5263 run_test 56l "check lfs find -type b"
5264
5265 test_56m() {
5266         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5267
5268         setup_56_special $dir $NUMFILES $NUMDIRS
5269
5270         local expected=$((NUMDIRS + NUMFILES))
5271         local cmd="$LFS find -type c $dir"
5272         local nums=$($cmd | wc -l)
5273         [ $nums -eq $expected ] ||
5274                 error "'$cmd' wrong: found $nums, expected $expected"
5275 }
5276 run_test 56m "check lfs find -type c"
5277
5278 test_56n() {
5279         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5280         setup_56_special $dir $NUMFILES $NUMDIRS
5281
5282         local expected=$((NUMDIRS + NUMFILES))
5283         local cmd="$LFS find -type l $dir"
5284         local nums=$($cmd | wc -l)
5285
5286         [ $nums -eq $expected ] ||
5287                 error "'$cmd' wrong: found $nums, expected $expected"
5288 }
5289 run_test 56n "check lfs find -type l"
5290
5291 test_56o() {
5292         local dir=$DIR/$tdir
5293
5294         setup_56 $dir $NUMFILES $NUMDIRS
5295         utime $dir/file1 > /dev/null || error "utime (1)"
5296         utime $dir/file2 > /dev/null || error "utime (2)"
5297         utime $dir/dir1 > /dev/null || error "utime (3)"
5298         utime $dir/dir2 > /dev/null || error "utime (4)"
5299         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5300         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5301
5302         local expected=4
5303         local nums=$($LFS find -mtime +0 $dir | wc -l)
5304
5305         [ $nums -eq $expected ] ||
5306                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5307
5308         expected=12
5309         cmd="$LFS find -mtime 0 $dir"
5310         nums=$($cmd | wc -l)
5311         [ $nums -eq $expected ] ||
5312                 error "'$cmd' wrong: found $nums, expected $expected"
5313 }
5314 run_test 56o "check lfs find -mtime for old files"
5315
5316 test_56ob() {
5317         local dir=$DIR/$tdir
5318         local expected=1
5319         local count=0
5320
5321         # just to make sure there is something that won't be found
5322         test_mkdir $dir
5323         touch $dir/$tfile.now
5324
5325         for age in year week day hour min; do
5326                 count=$((count + 1))
5327
5328                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
5329                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
5330                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
5331
5332                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
5333                 local nums=$($cmd | wc -l)
5334                 [ $nums -eq $expected ] ||
5335                         error "'$cmd' wrong: found $nums, expected $expected"
5336
5337                 cmd="$LFS find $dir -atime $count${age:0:1}"
5338                 nums=$($cmd | wc -l)
5339                 [ $nums -eq $expected ] ||
5340                         error "'$cmd' wrong: found $nums, expected $expected"
5341         done
5342
5343         sleep 2
5344         cmd="$LFS find $dir -ctime +1s -type f"
5345         nums=$($cmd | wc -l)
5346         (( $nums == $count * 2 + 1)) ||
5347                 error "'$cmd' wrong: found $nums, expected $((expected*2+1))"
5348 }
5349 run_test 56ob "check lfs find -atime -mtime -ctime with units"
5350
5351 test_56p() {
5352         [ $RUNAS_ID -eq $UID ] &&
5353                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5354
5355         local dir=$DIR/$tdir
5356
5357         setup_56 $dir $NUMFILES $NUMDIRS
5358         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
5359
5360         local expected=$NUMFILES
5361         local cmd="$LFS find -uid $RUNAS_ID $dir"
5362         local nums=$($cmd | wc -l)
5363
5364         [ $nums -eq $expected ] ||
5365                 error "'$cmd' wrong: found $nums, expected $expected"
5366
5367         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
5368         cmd="$LFS find ! -uid $RUNAS_ID $dir"
5369         nums=$($cmd | wc -l)
5370         [ $nums -eq $expected ] ||
5371                 error "'$cmd' wrong: found $nums, expected $expected"
5372 }
5373 run_test 56p "check lfs find -uid and ! -uid"
5374
5375 test_56q() {
5376         [ $RUNAS_ID -eq $UID ] &&
5377                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5378
5379         local dir=$DIR/$tdir
5380
5381         setup_56 $dir $NUMFILES $NUMDIRS
5382         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
5383
5384         local expected=$NUMFILES
5385         local cmd="$LFS find -gid $RUNAS_GID $dir"
5386         local nums=$($cmd | wc -l)
5387
5388         [ $nums -eq $expected ] ||
5389                 error "'$cmd' wrong: found $nums, expected $expected"
5390
5391         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
5392         cmd="$LFS find ! -gid $RUNAS_GID $dir"
5393         nums=$($cmd | wc -l)
5394         [ $nums -eq $expected ] ||
5395                 error "'$cmd' wrong: found $nums, expected $expected"
5396 }
5397 run_test 56q "check lfs find -gid and ! -gid"
5398
5399 test_56r() {
5400         local dir=$DIR/$tdir
5401
5402         setup_56 $dir $NUMFILES $NUMDIRS
5403
5404         local expected=12
5405         local cmd="$LFS find -size 0 -type f $dir"
5406         local nums=$($cmd | wc -l)
5407
5408         [ $nums -eq $expected ] ||
5409                 error "'$cmd' wrong: found $nums, expected $expected"
5410         expected=0
5411         cmd="$LFS find ! -size 0 -type f $dir"
5412         nums=$($cmd | wc -l)
5413         [ $nums -eq $expected ] ||
5414                 error "'$cmd' wrong: found $nums, expected $expected"
5415         echo "test" > $dir/$tfile
5416         echo "test2" > $dir/$tfile.2 && sync
5417         expected=1
5418         cmd="$LFS find -size 5 -type f $dir"
5419         nums=$($cmd | wc -l)
5420         [ $nums -eq $expected ] ||
5421                 error "'$cmd' wrong: found $nums, expected $expected"
5422         expected=1
5423         cmd="$LFS find -size +5 -type f $dir"
5424         nums=$($cmd | wc -l)
5425         [ $nums -eq $expected ] ||
5426                 error "'$cmd' wrong: found $nums, expected $expected"
5427         expected=2
5428         cmd="$LFS find -size +0 -type f $dir"
5429         nums=$($cmd | wc -l)
5430         [ $nums -eq $expected ] ||
5431                 error "'$cmd' wrong: found $nums, expected $expected"
5432         expected=2
5433         cmd="$LFS find ! -size -5 -type f $dir"
5434         nums=$($cmd | wc -l)
5435         [ $nums -eq $expected ] ||
5436                 error "'$cmd' wrong: found $nums, expected $expected"
5437         expected=12
5438         cmd="$LFS find -size -5 -type f $dir"
5439         nums=$($cmd | wc -l)
5440         [ $nums -eq $expected ] ||
5441                 error "'$cmd' wrong: found $nums, expected $expected"
5442 }
5443 run_test 56r "check lfs find -size works"
5444
5445 test_56s() { # LU-611 #LU-9369
5446         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
5447
5448         local dir=$DIR/$tdir
5449         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
5450
5451         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
5452         for i in $(seq $NUMDIRS); do
5453                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
5454         done
5455
5456         local expected=$NUMDIRS
5457         local cmd="$LFS find -c $OSTCOUNT $dir"
5458         local nums=$($cmd | wc -l)
5459
5460         [ $nums -eq $expected ] || {
5461                 $LFS getstripe -R $dir
5462                 error "'$cmd' wrong: found $nums, expected $expected"
5463         }
5464
5465         expected=$((NUMDIRS + onestripe))
5466         cmd="$LFS find -stripe-count +0 -type f $dir"
5467         nums=$($cmd | wc -l)
5468         [ $nums -eq $expected ] || {
5469                 $LFS getstripe -R $dir
5470                 error "'$cmd' wrong: found $nums, expected $expected"
5471         }
5472
5473         expected=$onestripe
5474         cmd="$LFS find -stripe-count 1 -type f $dir"
5475         nums=$($cmd | wc -l)
5476         [ $nums -eq $expected ] || {
5477                 $LFS getstripe -R $dir
5478                 error "'$cmd' wrong: found $nums, expected $expected"
5479         }
5480
5481         cmd="$LFS find -stripe-count -2 -type f $dir"
5482         nums=$($cmd | wc -l)
5483         [ $nums -eq $expected ] || {
5484                 $LFS getstripe -R $dir
5485                 error "'$cmd' wrong: found $nums, expected $expected"
5486         }
5487
5488         expected=0
5489         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
5490         nums=$($cmd | wc -l)
5491         [ $nums -eq $expected ] || {
5492                 $LFS getstripe -R $dir
5493                 error "'$cmd' wrong: found $nums, expected $expected"
5494         }
5495 }
5496 run_test 56s "check lfs find -stripe-count works"
5497
5498 test_56t() { # LU-611 #LU-9369
5499         local dir=$DIR/$tdir
5500
5501         setup_56 $dir 0 $NUMDIRS
5502         for i in $(seq $NUMDIRS); do
5503                 $LFS setstripe -S 8M $dir/dir$i/$tfile
5504         done
5505
5506         local expected=$NUMDIRS
5507         local cmd="$LFS find -S 8M $dir"
5508         local nums=$($cmd | wc -l)
5509
5510         [ $nums -eq $expected ] || {
5511                 $LFS getstripe -R $dir
5512                 error "'$cmd' wrong: found $nums, expected $expected"
5513         }
5514         rm -rf $dir
5515
5516         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
5517
5518         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
5519
5520         expected=$(((NUMDIRS + 1) * NUMFILES))
5521         cmd="$LFS find -stripe-size 512k -type f $dir"
5522         nums=$($cmd | wc -l)
5523         [ $nums -eq $expected ] ||
5524                 error "'$cmd' wrong: found $nums, expected $expected"
5525
5526         cmd="$LFS find -stripe-size +320k -type f $dir"
5527         nums=$($cmd | wc -l)
5528         [ $nums -eq $expected ] ||
5529                 error "'$cmd' wrong: found $nums, expected $expected"
5530
5531         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
5532         cmd="$LFS find -stripe-size +200k -type f $dir"
5533         nums=$($cmd | wc -l)
5534         [ $nums -eq $expected ] ||
5535                 error "'$cmd' wrong: found $nums, expected $expected"
5536
5537         cmd="$LFS find -stripe-size -640k -type f $dir"
5538         nums=$($cmd | wc -l)
5539         [ $nums -eq $expected ] ||
5540                 error "'$cmd' wrong: found $nums, expected $expected"
5541
5542         expected=4
5543         cmd="$LFS find -stripe-size 256k -type f $dir"
5544         nums=$($cmd | wc -l)
5545         [ $nums -eq $expected ] ||
5546                 error "'$cmd' wrong: found $nums, expected $expected"
5547
5548         cmd="$LFS find -stripe-size -320k -type f $dir"
5549         nums=$($cmd | wc -l)
5550         [ $nums -eq $expected ] ||
5551                 error "'$cmd' wrong: found $nums, expected $expected"
5552
5553         expected=0
5554         cmd="$LFS find -stripe-size 1024k -type f $dir"
5555         nums=$($cmd | wc -l)
5556         [ $nums -eq $expected ] ||
5557                 error "'$cmd' wrong: found $nums, expected $expected"
5558 }
5559 run_test 56t "check lfs find -stripe-size works"
5560
5561 test_56u() { # LU-611
5562         local dir=$DIR/$tdir
5563
5564         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
5565
5566         if [[ $OSTCOUNT -gt 1 ]]; then
5567                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
5568                 onestripe=4
5569         else
5570                 onestripe=0
5571         fi
5572
5573         local expected=$(((NUMDIRS + 1) * NUMFILES))
5574         local cmd="$LFS find -stripe-index 0 -type f $dir"
5575         local nums=$($cmd | wc -l)
5576
5577         [ $nums -eq $expected ] ||
5578                 error "'$cmd' wrong: found $nums, expected $expected"
5579
5580         expected=$onestripe
5581         cmd="$LFS find -stripe-index 1 -type f $dir"
5582         nums=$($cmd | wc -l)
5583         [ $nums -eq $expected ] ||
5584                 error "'$cmd' wrong: found $nums, expected $expected"
5585
5586         cmd="$LFS find ! -stripe-index 0 -type f $dir"
5587         nums=$($cmd | wc -l)
5588         [ $nums -eq $expected ] ||
5589                 error "'$cmd' wrong: found $nums, expected $expected"
5590
5591         expected=0
5592         # This should produce an error and not return any files
5593         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
5594         nums=$($cmd 2>/dev/null | wc -l)
5595         [ $nums -eq $expected ] ||
5596                 error "'$cmd' wrong: found $nums, expected $expected"
5597
5598         if [[ $OSTCOUNT -gt 1 ]]; then
5599                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
5600                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
5601                 nums=$($cmd | wc -l)
5602                 [ $nums -eq $expected ] ||
5603                         error "'$cmd' wrong: found $nums, expected $expected"
5604         fi
5605 }
5606 run_test 56u "check lfs find -stripe-index works"
5607
5608 test_56v() {
5609         local mdt_idx=0
5610         local dir=$DIR/$tdir
5611
5612         setup_56 $dir $NUMFILES $NUMDIRS
5613
5614         UUID=$(mdtuuid_from_index $mdt_idx $dir)
5615         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
5616
5617         for file in $($LFS find -m $UUID $dir); do
5618                 file_midx=$($LFS getstripe -m $file)
5619                 [ $file_midx -eq $mdt_idx ] ||
5620                         error "lfs find -m $UUID != getstripe -m $file_midx"
5621         done
5622 }
5623 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
5624
5625 test_56w() {
5626         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5627         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5628
5629         local dir=$DIR/$tdir
5630
5631         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
5632
5633         local stripe_size=$($LFS getstripe -S -d $dir) ||
5634                 error "$LFS getstripe -S -d $dir failed"
5635         stripe_size=${stripe_size%% *}
5636
5637         local file_size=$((stripe_size * OSTCOUNT))
5638         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
5639         local required_space=$((file_num * file_size))
5640         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
5641                            head -n1)
5642         [[ $free_space -le $((required_space / 1024)) ]] &&
5643                 skip_env "need $required_space, have $free_space kbytes"
5644
5645         local dd_bs=65536
5646         local dd_count=$((file_size / dd_bs))
5647
5648         # write data into the files
5649         local i
5650         local j
5651         local file
5652
5653         for i in $(seq $NUMFILES); do
5654                 file=$dir/file$i
5655                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5656                         error "write data into $file failed"
5657         done
5658         for i in $(seq $NUMDIRS); do
5659                 for j in $(seq $NUMFILES); do
5660                         file=$dir/dir$i/file$j
5661                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5662                                 error "write data into $file failed"
5663                 done
5664         done
5665
5666         # $LFS_MIGRATE will fail if hard link migration is unsupported
5667         if [[ $(lustre_version_code mds1) -gt $(version_code 2.5.55) ]]; then
5668                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
5669                         error "creating links to $dir/dir1/file1 failed"
5670         fi
5671
5672         local expected=-1
5673
5674         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
5675
5676         # lfs_migrate file
5677         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
5678
5679         echo "$cmd"
5680         eval $cmd || error "$cmd failed"
5681
5682         check_stripe_count $dir/file1 $expected
5683
5684         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
5685         then
5686                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
5687                 # OST 1 if it is on OST 0. This file is small enough to
5688                 # be on only one stripe.
5689                 file=$dir/migr_1_ost
5690                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
5691                         error "write data into $file failed"
5692                 local obdidx=$($LFS getstripe -i $file)
5693                 local oldmd5=$(md5sum $file)
5694                 local newobdidx=0
5695
5696                 [[ $obdidx -eq 0 ]] && newobdidx=1
5697                 cmd="$LFS migrate -i $newobdidx $file"
5698                 echo $cmd
5699                 eval $cmd || error "$cmd failed"
5700
5701                 local realobdix=$($LFS getstripe -i $file)
5702                 local newmd5=$(md5sum $file)
5703
5704                 [[ $newobdidx -ne $realobdix ]] &&
5705                         error "new OST is different (was=$obdidx, "\
5706                               "wanted=$newobdidx, got=$realobdix)"
5707                 [[ "$oldmd5" != "$newmd5" ]] &&
5708                         error "md5sum differ: $oldmd5, $newmd5"
5709         fi
5710
5711         # lfs_migrate dir
5712         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
5713         echo "$cmd"
5714         eval $cmd || error "$cmd failed"
5715
5716         for j in $(seq $NUMFILES); do
5717                 check_stripe_count $dir/dir1/file$j $expected
5718         done
5719
5720         # lfs_migrate works with lfs find
5721         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
5722              $LFS_MIGRATE -y -c $expected"
5723         echo "$cmd"
5724         eval $cmd || error "$cmd failed"
5725
5726         for i in $(seq 2 $NUMFILES); do
5727                 check_stripe_count $dir/file$i $expected
5728         done
5729         for i in $(seq 2 $NUMDIRS); do
5730                 for j in $(seq $NUMFILES); do
5731                 check_stripe_count $dir/dir$i/file$j $expected
5732                 done
5733         done
5734 }
5735 run_test 56w "check lfs_migrate -c stripe_count works"
5736
5737 test_56wb() {
5738         local file1=$DIR/$tdir/file1
5739         local create_pool=false
5740         local initial_pool=$($LFS getstripe -p $DIR)
5741         local pool_list=()
5742         local pool=""
5743
5744         echo -n "Creating test dir..."
5745         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5746         echo "done."
5747
5748         echo -n "Creating test file..."
5749         touch $file1 || error "cannot create file"
5750         echo "done."
5751
5752         echo -n "Detecting existing pools..."
5753         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
5754
5755         if [ ${#pool_list[@]} -gt 0 ]; then
5756                 echo "${pool_list[@]}"
5757                 for thispool in "${pool_list[@]}"; do
5758                         if [[ -z "$initial_pool" ||
5759                               "$initial_pool" != "$thispool" ]]; then
5760                                 pool="$thispool"
5761                                 echo "Using existing pool '$pool'"
5762                                 break
5763                         fi
5764                 done
5765         else
5766                 echo "none detected."
5767         fi
5768         if [ -z "$pool" ]; then
5769                 pool=${POOL:-testpool}
5770                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
5771                 echo -n "Creating pool '$pool'..."
5772                 create_pool=true
5773                 pool_add $pool &> /dev/null ||
5774                         error "pool_add failed"
5775                 echo "done."
5776
5777                 echo -n "Adding target to pool..."
5778                 pool_add_targets $pool 0 0 1 &> /dev/null ||
5779                         error "pool_add_targets failed"
5780                 echo "done."
5781         fi
5782
5783         echo -n "Setting pool using -p option..."
5784         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
5785                 error "migrate failed rc = $?"
5786         echo "done."
5787
5788         echo -n "Verifying test file is in pool after migrating..."
5789         [ "$($LFS getstripe -p $file1)" = $pool ] ||
5790                 error "file was not migrated to pool $pool"
5791         echo "done."
5792
5793         echo -n "Removing test file from pool '$pool'..."
5794         $LFS migrate $file1 &> /dev/null ||
5795                 error "cannot remove from pool"
5796         [ "$($LFS getstripe -p $file1)" ] &&
5797                 error "pool still set"
5798         echo "done."
5799
5800         echo -n "Setting pool using --pool option..."
5801         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
5802                 error "migrate failed rc = $?"
5803         echo "done."
5804
5805         # Clean up
5806         rm -f $file1
5807         if $create_pool; then
5808                 destroy_test_pools 2> /dev/null ||
5809                         error "destroy test pools failed"
5810         fi
5811 }
5812 run_test 56wb "check lfs_migrate pool support"
5813
5814 test_56wc() {
5815         local file1="$DIR/$tdir/file1"
5816
5817         echo -n "Creating test dir..."
5818         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5819         local def_stripe_size=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
5820         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
5821                 error "cannot set stripe"
5822         echo "done"
5823
5824         echo -n "Setting initial stripe for test file..."
5825         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
5826                 error "cannot set stripe"
5827         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
5828                 error "stripe size not set"
5829         echo "done."
5830
5831         # File currently set to -S 512K -c 1
5832
5833         # Ensure -c and -S options are rejected when -R is set
5834         echo -n "Verifying incompatible options are detected..."
5835         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
5836                 error "incompatible -c and -R options not detected"
5837         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
5838                 error "incompatible -S and -R options not detected"
5839         echo "done."
5840
5841         # Ensure unrecognized options are passed through to 'lfs migrate'
5842         echo -n "Verifying -S option is passed through to lfs migrate..."
5843         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
5844                 error "migration failed"
5845         [ $($LFS getstripe -S "$file1") -eq 1048576 ] ||
5846                 error "file was not restriped"
5847         echo "done."
5848
5849         # File currently set to -S 1M -c 1
5850
5851         # Ensure long options are supported
5852         echo -n "Verifying long options supported..."
5853         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
5854                 error "long option without argument not supported"
5855         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
5856                 error "long option with argument not supported"
5857         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
5858                 error "file not restriped with --stripe-size option"
5859         echo "done."
5860
5861         # File currently set to -S 512K -c 1
5862
5863         if [ "$OSTCOUNT" -gt 1 ]; then
5864                 echo -n "Verifying explicit stripe count can be set..."
5865                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
5866                         error "migrate failed"
5867                 [ $($LFS getstripe -c "$file1") -eq 2 ] ||
5868                         error "file not restriped to explicit count"
5869                 echo "done."
5870         fi
5871
5872         # File currently set to -S 512K -c 1 or -S 512K -c 2
5873
5874         # Ensure parent striping is used if -R is set, and no stripe
5875         # count or size is specified
5876         echo -n "Setting stripe for parent directory..."
5877         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
5878                 error "cannot set stripe"
5879         echo "done."
5880
5881         echo -n "Verifying restripe option uses parent stripe settings..."
5882         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
5883                 error "migrate failed"
5884         [ $($LFS getstripe -S "$file1") -eq $def_stripe_size ] ||
5885                 error "file not restriped to parent settings"
5886         [ $($LFS getstripe -c "$file1") -eq 1 ] ||
5887                 error "file not restriped to parent settings"
5888         echo "done."
5889
5890         # File currently set to -S 1M -c 1
5891
5892         # Ensure striping is preserved if -R is not set, and no stripe
5893         # count or size is specified
5894         echo -n "Verifying striping size preserved when not specified..."
5895         local orig_stripe_size=$($LFS getstripe -S "$file1" 2>/dev/null)
5896         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
5897                 error "cannot set stripe on parent directory"
5898         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5899                 error "migrate failed"
5900         [ $($LFS getstripe -S "$file1") -eq $orig_stripe_size ] ||
5901                 error "file was restriped"
5902         echo "done."
5903
5904         # Ensure file name properly detected when final option has no argument
5905         echo -n "Verifying file name properly detected..."
5906         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5907                 error "file name interpreted as option argument"
5908         echo "done."
5909
5910         # Clean up
5911         rm -f "$file1"
5912 }
5913 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
5914
5915 test_56wd() {
5916         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5917
5918         local file1=$DIR/$tdir/file1
5919
5920         echo -n "Creating test dir..."
5921         test_mkdir $DIR/$tdir || error "cannot create dir"
5922         echo "done."
5923
5924         echo -n "Creating test file..."
5925         touch $file1
5926         echo "done."
5927
5928         # Ensure 'lfs migrate' will fail by using a non-existent option,
5929         # and make sure rsync is not called to recover
5930         echo -n "Make sure --no-rsync option works..."
5931         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
5932                 grep -q 'refusing to fall back to rsync' ||
5933                 error "rsync was called with --no-rsync set"
5934         echo "done."
5935
5936         # Ensure rsync is called without trying 'lfs migrate' first
5937         echo -n "Make sure --rsync option works..."
5938         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
5939                 grep -q 'falling back to rsync' &&
5940                 error "lfs migrate was called with --rsync set"
5941         echo "done."
5942
5943         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
5944         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
5945                 grep -q 'at the same time' ||
5946                 error "--rsync and --no-rsync accepted concurrently"
5947         echo "done."
5948
5949         # Clean up
5950         rm -f $file1
5951 }
5952 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
5953
5954 test_56x() {
5955         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5956         check_swap_layouts_support
5957
5958         local dir=$DIR/$tdir
5959         local ref1=/etc/passwd
5960         local file1=$dir/file1
5961
5962         test_mkdir $dir || error "creating dir $dir"
5963         $LFS setstripe -c 2 $file1
5964         cp $ref1 $file1
5965         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
5966         stripe=$($LFS getstripe -c $file1)
5967         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5968         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5969
5970         # clean up
5971         rm -f $file1
5972 }
5973 run_test 56x "lfs migration support"
5974
5975 test_56xa() {
5976         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5977         check_swap_layouts_support
5978
5979         local dir=$DIR/$tdir/$testnum
5980
5981         test_mkdir -p $dir
5982
5983         local ref1=/etc/passwd
5984         local file1=$dir/file1
5985
5986         $LFS setstripe -c 2 $file1
5987         cp $ref1 $file1
5988         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
5989
5990         local stripe=$($LFS getstripe -c $file1)
5991
5992         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5993         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5994
5995         # clean up
5996         rm -f $file1
5997 }
5998 run_test 56xa "lfs migration --block support"
5999
6000 check_migrate_links() {
6001         local dir="$1"
6002         local file1="$dir/file1"
6003         local begin="$2"
6004         local count="$3"
6005         local total_count=$(($begin + $count - 1))
6006         local symlink_count=10
6007         local uniq_count=10
6008
6009         if [ ! -f "$file1" ]; then
6010                 echo -n "creating initial file..."
6011                 $LFS setstripe -c 1 -S "512k" "$file1" ||
6012                         error "cannot setstripe initial file"
6013                 echo "done"
6014
6015                 echo -n "creating symlinks..."
6016                 for s in $(seq 1 $symlink_count); do
6017                         ln -s "$file1" "$dir/slink$s" ||
6018                                 error "cannot create symlinks"
6019                 done
6020                 echo "done"
6021
6022                 echo -n "creating nonlinked files..."
6023                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
6024                         error "cannot create nonlinked files"
6025                 echo "done"
6026         fi
6027
6028         # create hard links
6029         if [ ! -f "$dir/file$total_count" ]; then
6030                 echo -n "creating hard links $begin:$total_count..."
6031                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
6032                         /dev/null || error "cannot create hard links"
6033                 echo "done"
6034         fi
6035
6036         echo -n "checking number of hard links listed in xattrs..."
6037         local fid=$($LFS getstripe -F "$file1")
6038         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
6039
6040         echo "${#paths[*]}"
6041         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
6042                         skip "hard link list has unexpected size, skipping test"
6043         fi
6044         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
6045                         error "link names should exceed xattrs size"
6046         fi
6047
6048         echo -n "migrating files..."
6049         local migrate_out=$($LFS_MIGRATE -y -S '1m' $dir)
6050         local rc=$?
6051         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
6052         echo "done"
6053
6054         # make sure all links have been properly migrated
6055         echo -n "verifying files..."
6056         fid=$($LFS getstripe -F "$file1") ||
6057                 error "cannot get fid for file $file1"
6058         for i in $(seq 2 $total_count); do
6059                 local fid2=$($LFS getstripe -F $dir/file$i)
6060
6061                 [ "$fid2" == "$fid" ] ||
6062                         error "migrated hard link has mismatched FID"
6063         done
6064
6065         # make sure hard links were properly detected, and migration was
6066         # performed only once for the entire link set; nonlinked files should
6067         # also be migrated
6068         local actual=$(grep -c 'done migrate' <<< "$migrate_out")
6069         local expected=$(($uniq_count + 1))
6070
6071         [ "$actual" -eq  "$expected" ] ||
6072                 error "hard links individually migrated ($actual != $expected)"
6073
6074         # make sure the correct number of hard links are present
6075         local hardlinks=$(stat -c '%h' "$file1")
6076
6077         [ $hardlinks -eq $total_count ] ||
6078                 error "num hard links $hardlinks != $total_count"
6079         echo "done"
6080
6081         return 0
6082 }
6083
6084 test_56xb() {
6085         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
6086                 skip "Need MDS version at least 2.10.55"
6087
6088         local dir="$DIR/$tdir"
6089
6090         test_mkdir "$dir" || error "cannot create dir $dir"
6091
6092         echo "testing lfs migrate mode when all links fit within xattrs"
6093         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
6094
6095         echo "testing rsync mode when all links fit within xattrs"
6096         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
6097
6098         echo "testing lfs migrate mode when all links do not fit within xattrs"
6099         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
6100
6101         echo "testing rsync mode when all links do not fit within xattrs"
6102         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
6103
6104
6105         # clean up
6106         rm -rf $dir
6107 }
6108 run_test 56xb "lfs migration hard link support"
6109
6110 test_56xc() {
6111         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6112
6113         local dir="$DIR/$tdir"
6114
6115         test_mkdir "$dir" || error "cannot create dir $dir"
6116
6117         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
6118         echo -n "Setting initial stripe for 20MB test file..."
6119         $LFS setstripe -c 2 -i 0 "$dir/20mb" || error "cannot setstripe"
6120         echo "done"
6121         echo -n "Sizing 20MB test file..."
6122         truncate "$dir/20mb" 20971520 || error "cannot create 20MB test file"
6123         echo "done"
6124         echo -n "Verifying small file autostripe count is 1..."
6125         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" &> /dev/null ||
6126                 error "cannot migrate 20MB file"
6127         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
6128                 error "cannot get stripe for $dir/20mb"
6129         [ $stripe_count -eq 1 ] ||
6130                 error "unexpected stripe count $stripe_count for 20MB file"
6131         rm -f "$dir/20mb"
6132         echo "done"
6133
6134         # Test 2: File is small enough to fit within the available space on
6135         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
6136         # have at least an additional 1KB for each desired stripe for test 3
6137         echo -n "Setting stripe for 1GB test file..."
6138         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe"
6139         echo "done"
6140         echo -n "Sizing 1GB test file..."
6141         # File size is 1GB + 3KB
6142         truncate "$dir/1gb" 1073744896 &> /dev/null ||
6143                 error "cannot create 1GB test file"
6144         echo "done"
6145         echo -n "Migrating 1GB file..."
6146         $LFS_MIGRATE -y -A -C 1 "$dir/1gb" &> /dev/null ||
6147                 error "cannot migrate file"
6148         echo "done"
6149         echo -n "Verifying autostripe count is sqrt(n) + 1..."
6150         stripe_count=$($LFS getstripe -c "$dir/1gb") ||
6151                 error "cannot get stripe for $dir/1gb"
6152         [ $stripe_count -eq 2 ] ||
6153                 error "unexpected stripe count $stripe_count (expected 2)"
6154         echo "done"
6155
6156         # Test 3: File is too large to fit within the available space on
6157         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
6158         if [ $OSTCOUNT -ge 3 ]; then
6159                 # The required available space is calculated as
6160                 # file size (1GB + 3KB) / OST count (3).
6161                 local kb_per_ost=349526
6162
6163                 echo -n "Migrating 1GB file..."
6164                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" &>> \
6165                         /dev/null || error "cannot migrate file"
6166                 echo "done"
6167
6168                 stripe_count=$($LFS getstripe -c "$dir/1gb")
6169                 echo -n "Verifying autostripe count with limited space..."
6170                 [ "$stripe_count" -a $stripe_count -eq 3 ] ||
6171                         error "unexpected stripe count $stripe_count (wanted 3)"
6172                 echo "done"
6173         fi
6174
6175         # clean up
6176         rm -rf $dir
6177 }
6178 run_test 56xc "lfs migration autostripe"
6179
6180 test_56y() {
6181         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
6182                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
6183
6184         local res=""
6185         local dir=$DIR/$tdir
6186         local f1=$dir/file1
6187         local f2=$dir/file2
6188
6189         test_mkdir -p $dir || error "creating dir $dir"
6190         touch $f1 || error "creating std file $f1"
6191         $MULTIOP $f2 H2c || error "creating released file $f2"
6192
6193         # a directory can be raid0, so ask only for files
6194         res=$($LFS find $dir -L raid0 -type f | wc -l)
6195         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
6196
6197         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
6198         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
6199
6200         # only files can be released, so no need to force file search
6201         res=$($LFS find $dir -L released)
6202         [[ $res == $f2 ]] || error "search released: found $res != $f2"
6203
6204         res=$($LFS find $dir -type f \! -L released)
6205         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
6206 }
6207 run_test 56y "lfs find -L raid0|released"
6208
6209 test_56z() { # LU-4824
6210         # This checks to make sure 'lfs find' continues after errors
6211         # There are two classes of errors that should be caught:
6212         # - If multiple paths are provided, all should be searched even if one
6213         #   errors out
6214         # - If errors are encountered during the search, it should not terminate
6215         #   early
6216         local dir=$DIR/$tdir
6217         local i
6218
6219         test_mkdir $dir
6220         for i in d{0..9}; do
6221                 test_mkdir $dir/$i
6222         done
6223         touch $dir/d{0..9}/$tfile
6224         $LFS find $DIR/non_existent_dir $dir &&
6225                 error "$LFS find did not return an error"
6226         # Make a directory unsearchable. This should NOT be the last entry in
6227         # directory order.  Arbitrarily pick the 6th entry
6228         chmod 700 $($LFS find $dir -type d | sed '6!d')
6229
6230         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
6231
6232         # The user should be able to see 10 directories and 9 files
6233         [ $count == 19 ] || error "$LFS find did not continue after error"
6234 }
6235 run_test 56z "lfs find should continue after an error"
6236
6237 test_56aa() { # LU-5937
6238         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
6239
6240         local dir=$DIR/$tdir
6241
6242         mkdir $dir
6243         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
6244
6245         createmany -o $dir/striped_dir/${tfile}- 1024
6246         local dirs=$($LFS find --size +8k $dir/)
6247
6248         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
6249 }
6250 run_test 56aa "lfs find --size under striped dir"
6251
6252 test_56ab() { # LU-10705
6253         test_mkdir $DIR/$tdir
6254         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
6255         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
6256         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
6257         # Flush writes to ensure valid blocks.  Need to be more thorough for
6258         # ZFS, since blocks are not allocated/returned to client immediately.
6259         sync_all_data
6260         wait_zfs_commit ost1 2
6261         cancel_lru_locks osc
6262         ls -ls $DIR/$tdir
6263
6264         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
6265
6266         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
6267
6268         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
6269         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
6270
6271         rm -f $DIR/$tdir/$tfile.[123]
6272 }
6273 run_test 56ab "lfs find --blocks"
6274
6275 test_56ba() {
6276         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
6277                 skip "Need MDS version at least 2.10.50"
6278
6279         # Create composite files with one component
6280         local dir=$DIR/$tdir
6281
6282         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
6283         # Create composite files with three components
6284         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
6285         # Create non-composite files
6286         createmany -o $dir/${tfile}- 10
6287
6288         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
6289
6290         [[ $nfiles == 10 ]] ||
6291                 error "lfs find -E 1M found $nfiles != 10 files"
6292
6293         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
6294         [[ $nfiles == 25 ]] ||
6295                 error "lfs find ! -E 1M found $nfiles != 25 files"
6296
6297         # All files have a component that starts at 0
6298         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
6299         [[ $nfiles == 35 ]] ||
6300                 error "lfs find --component-start 0 - $nfiles != 35 files"
6301
6302         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
6303         [[ $nfiles == 15 ]] ||
6304                 error "lfs find --component-start 2M - $nfiles != 15 files"
6305
6306         # All files created here have a componenet that does not starts at 2M
6307         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
6308         [[ $nfiles == 35 ]] ||
6309                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
6310
6311         # Find files with a specified number of components
6312         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
6313         [[ $nfiles == 15 ]] ||
6314                 error "lfs find --component-count 3 - $nfiles != 15 files"
6315
6316         # Remember non-composite files have a component count of zero
6317         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
6318         [[ $nfiles == 10 ]] ||
6319                 error "lfs find --component-count 0 - $nfiles != 10 files"
6320
6321         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
6322         [[ $nfiles == 20 ]] ||
6323                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
6324
6325         # All files have a flag called "init"
6326         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
6327         [[ $nfiles == 35 ]] ||
6328                 error "lfs find --component-flags init - $nfiles != 35 files"
6329
6330         # Multi-component files will have a component not initialized
6331         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
6332         [[ $nfiles == 15 ]] ||
6333                 error "lfs find !--component-flags init - $nfiles != 15 files"
6334
6335         rm -rf $dir
6336
6337 }
6338 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
6339
6340 test_56ca() {
6341         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
6342                 skip "Need MDS version at least 2.10.57"
6343
6344         local td=$DIR/$tdir
6345         local tf=$td/$tfile
6346         local dir
6347         local nfiles
6348         local cmd
6349         local i
6350         local j
6351
6352         # create mirrored directories and mirrored files
6353         mkdir $td || error "mkdir $td failed"
6354         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
6355         createmany -o $tf- 10 || error "create $tf- failed"
6356
6357         for i in $(seq 2); do
6358                 dir=$td/dir$i
6359                 mkdir $dir || error "mkdir $dir failed"
6360                 $LFS mirror create -N$((3 + i)) $dir ||
6361                         error "create mirrored dir $dir failed"
6362                 createmany -o $dir/$tfile- 10 ||
6363                         error "create $dir/$tfile- failed"
6364         done
6365
6366         # change the states of some mirrored files
6367         echo foo > $tf-6
6368         for i in $(seq 2); do
6369                 dir=$td/dir$i
6370                 for j in $(seq 4 9); do
6371                         echo foo > $dir/$tfile-$j
6372                 done
6373         done
6374
6375         # find mirrored files with specific mirror count
6376         cmd="$LFS find --mirror-count 3 --type f $td"
6377         nfiles=$($cmd | wc -l)
6378         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
6379
6380         cmd="$LFS find ! --mirror-count 3 --type f $td"
6381         nfiles=$($cmd | wc -l)
6382         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
6383
6384         cmd="$LFS find --mirror-count +2 --type f $td"
6385         nfiles=$($cmd | wc -l)
6386         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6387
6388         cmd="$LFS find --mirror-count -6 --type f $td"
6389         nfiles=$($cmd | wc -l)
6390         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6391
6392         # find mirrored files with specific file state
6393         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
6394         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
6395
6396         cmd="$LFS find --mirror-state=ro --type f $td"
6397         nfiles=$($cmd | wc -l)
6398         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
6399
6400         cmd="$LFS find ! --mirror-state=ro --type f $td"
6401         nfiles=$($cmd | wc -l)
6402         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6403
6404         cmd="$LFS find --mirror-state=wp --type f $td"
6405         nfiles=$($cmd | wc -l)
6406         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6407
6408         cmd="$LFS find ! --mirror-state=sp --type f $td"
6409         nfiles=$($cmd | wc -l)
6410         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6411 }
6412 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
6413
6414 test_57a() {
6415         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6416         # note test will not do anything if MDS is not local
6417         if [ "$mds1_FSTYPE" != ldiskfs ]; then
6418                 skip_env "ldiskfs only test"
6419         fi
6420         remote_mds_nodsh && skip "remote MDS with nodsh"
6421
6422         local MNTDEV="osd*.*MDT*.mntdev"
6423         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
6424         [ -z "$DEV" ] && error "can't access $MNTDEV"
6425         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
6426                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
6427                         error "can't access $DEV"
6428                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
6429                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
6430                 rm $TMP/t57a.dump
6431         done
6432 }
6433 run_test 57a "verify MDS filesystem created with large inodes =="
6434
6435 test_57b() {
6436         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6437         if [ "$mds1_FSTYPE" != ldiskfs ]; then
6438                 skip_env "ldiskfs only test"
6439         fi
6440         remote_mds_nodsh && skip "remote MDS with nodsh"
6441
6442         local dir=$DIR/$tdir
6443         local filecount=100
6444         local file1=$dir/f1
6445         local fileN=$dir/f$filecount
6446
6447         rm -rf $dir || error "removing $dir"
6448         test_mkdir -c1 $dir
6449         local mdtidx=$($LFS getstripe -m $dir)
6450         local mdtname=MDT$(printf %04x $mdtidx)
6451         local facet=mds$((mdtidx + 1))
6452
6453         echo "mcreating $filecount files"
6454         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
6455
6456         # verify that files do not have EAs yet
6457         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
6458                 error "$file1 has an EA"
6459         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
6460                 error "$fileN has an EA"
6461
6462         sync
6463         sleep 1
6464         df $dir  #make sure we get new statfs data
6465         local mdsfree=$(do_facet $facet \
6466                         lctl get_param -n osd*.*$mdtname.kbytesfree)
6467         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
6468         local file
6469
6470         echo "opening files to create objects/EAs"
6471         for file in $(seq -f $dir/f%g 1 $filecount); do
6472                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
6473                         error "opening $file"
6474         done
6475
6476         # verify that files have EAs now
6477         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
6478         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
6479
6480         sleep 1  #make sure we get new statfs data
6481         df $dir
6482         local mdsfree2=$(do_facet $facet \
6483                          lctl get_param -n osd*.*$mdtname.kbytesfree)
6484         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
6485
6486         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
6487                 if [ "$mdsfree" != "$mdsfree2" ]; then
6488                         error "MDC before $mdcfree != after $mdcfree2"
6489                 else
6490                         echo "MDC before $mdcfree != after $mdcfree2"
6491                         echo "unable to confirm if MDS has large inodes"
6492                 fi
6493         fi
6494         rm -rf $dir
6495 }
6496 run_test 57b "default LOV EAs are stored inside large inodes ==="
6497
6498 test_58() {
6499         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6500         [ -z "$(which wiretest 2>/dev/null)" ] &&
6501                         skip_env "could not find wiretest"
6502
6503         wiretest
6504 }
6505 run_test 58 "verify cross-platform wire constants =============="
6506
6507 test_59() {
6508         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6509
6510         echo "touch 130 files"
6511         createmany -o $DIR/f59- 130
6512         echo "rm 130 files"
6513         unlinkmany $DIR/f59- 130
6514         sync
6515         # wait for commitment of removal
6516         wait_delete_completed
6517 }
6518 run_test 59 "verify cancellation of llog records async ========="
6519
6520 TEST60_HEAD="test_60 run $RANDOM"
6521 test_60a() {
6522         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6523         remote_mgs_nodsh && skip "remote MGS with nodsh"
6524         do_facet mgs "! which run-llog.sh &> /dev/null" &&
6525                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
6526                         skip_env "missing subtest run-llog.sh"
6527
6528         log "$TEST60_HEAD - from kernel mode"
6529         do_facet mgs "$LCTL dk > /dev/null"
6530         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
6531         do_facet mgs $LCTL dk > $TMP/$tfile
6532
6533         # LU-6388: test llog_reader
6534         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
6535         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
6536         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
6537                         skip_env "missing llog_reader"
6538         local fstype=$(facet_fstype mgs)
6539         [ $fstype != ldiskfs -a $fstype != zfs ] &&
6540                 skip_env "Only for ldiskfs or zfs type mgs"
6541
6542         local mntpt=$(facet_mntpt mgs)
6543         local mgsdev=$(mgsdevname 1)
6544         local fid_list
6545         local fid
6546         local rec_list
6547         local rec
6548         local rec_type
6549         local obj_file
6550         local path
6551         local seq
6552         local oid
6553         local pass=true
6554
6555         #get fid and record list
6556         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
6557                 tail -n 4))
6558         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
6559                 tail -n 4))
6560         #remount mgs as ldiskfs or zfs type
6561         stop mgs || error "stop mgs failed"
6562         mount_fstype mgs || error "remount mgs failed"
6563         for ((i = 0; i < ${#fid_list[@]}; i++)); do
6564                 fid=${fid_list[i]}
6565                 rec=${rec_list[i]}
6566                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
6567                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
6568                 oid=$((16#$oid))
6569
6570                 case $fstype in
6571                         ldiskfs )
6572                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
6573                         zfs )
6574                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
6575                 esac
6576                 echo "obj_file is $obj_file"
6577                 do_facet mgs $llog_reader $obj_file
6578
6579                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
6580                         awk '{ print $3 }' | sed -e "s/^type=//g")
6581                 if [ $rec_type != $rec ]; then
6582                         echo "FAILED test_60a wrong record type $rec_type," \
6583                               "should be $rec"
6584                         pass=false
6585                         break
6586                 fi
6587
6588                 #check obj path if record type is LLOG_LOGID_MAGIC
6589                 if [ "$rec" == "1064553b" ]; then
6590                         path=$(do_facet mgs $llog_reader $obj_file |
6591                                 grep "path=" | awk '{ print $NF }' |
6592                                 sed -e "s/^path=//g")
6593                         if [ $obj_file != $mntpt/$path ]; then
6594                                 echo "FAILED test_60a wrong obj path" \
6595                                       "$montpt/$path, should be $obj_file"
6596                                 pass=false
6597                                 break
6598                         fi
6599                 fi
6600         done
6601         rm -f $TMP/$tfile
6602         #restart mgs before "error", otherwise it will block the next test
6603         stop mgs || error "stop mgs failed"
6604         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
6605         $pass || error "test failed, see FAILED test_60a messages for specifics"
6606 }
6607 run_test 60a "llog_test run from kernel module and test llog_reader"
6608
6609 test_60b() { # bug 6411
6610         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6611
6612         dmesg > $DIR/$tfile
6613         LLOG_COUNT=$(do_facet mgs dmesg |
6614                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
6615                           /llog_[a-z]*.c:[0-9]/ {
6616                                 if (marker)
6617                                         from_marker++
6618                                 from_begin++
6619                           }
6620                           END {
6621                                 if (marker)
6622                                         print from_marker
6623                                 else
6624                                         print from_begin
6625                           }")
6626
6627         [[ $LLOG_COUNT -gt 120 ]] &&
6628                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
6629 }
6630 run_test 60b "limit repeated messages from CERROR/CWARN"
6631
6632 test_60c() {
6633         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6634
6635         echo "create 5000 files"
6636         createmany -o $DIR/f60c- 5000
6637 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
6638         lctl set_param fail_loc=0x80000137
6639         unlinkmany $DIR/f60c- 5000
6640         lctl set_param fail_loc=0
6641 }
6642 run_test 60c "unlink file when mds full"
6643
6644 test_60d() {
6645         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6646
6647         SAVEPRINTK=$(lctl get_param -n printk)
6648         # verify "lctl mark" is even working"
6649         MESSAGE="test message ID $RANDOM $$"
6650         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
6651         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
6652
6653         lctl set_param printk=0 || error "set lnet.printk failed"
6654         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
6655         MESSAGE="new test message ID $RANDOM $$"
6656         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
6657         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
6658         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
6659
6660         lctl set_param -n printk="$SAVEPRINTK"
6661 }
6662 run_test 60d "test printk console message masking"
6663
6664 test_60e() {
6665         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6666         remote_mds_nodsh && skip "remote MDS with nodsh"
6667
6668         touch $DIR/$tfile
6669 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
6670         do_facet mds1 lctl set_param fail_loc=0x15b
6671         rm $DIR/$tfile
6672 }
6673 run_test 60e "no space while new llog is being created"
6674
6675 test_60g() {
6676         local pid
6677
6678         test_mkdir -c $MDSCOUNT $DIR/$tdir
6679         $LFS setdirstripe -D -i -1 -c $MDSCOUNT $DIR/$tdir
6680
6681         (
6682                 local index=0
6683                 while true; do
6684                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
6685                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
6686                         index=$((index + 1))
6687                 done
6688         ) &
6689
6690         pid=$!
6691
6692         for i in $(seq 100); do 
6693                 # define OBD_FAIL_OSD_TXN_START    0x19a
6694                 do_facet mds1 lctl set_param fail_loc=0x8000019a
6695                 usleep 100
6696         done
6697
6698         kill -9 $pid
6699
6700         mkdir $DIR/$tdir/new || error "mkdir failed"
6701         rmdir $DIR/$tdir/new || error "rmdir failed"
6702 }
6703 run_test 60g "transaction abort won't cause MDT hung"
6704
6705 test_61a() {
6706         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6707
6708         f="$DIR/f61"
6709         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
6710         cancel_lru_locks osc
6711         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
6712         sync
6713 }
6714 run_test 61a "mmap() writes don't make sync hang ================"
6715
6716 test_61b() {
6717         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
6718 }
6719 run_test 61b "mmap() of unstriped file is successful"
6720
6721 # bug 2330 - insufficient obd_match error checking causes LBUG
6722 test_62() {
6723         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6724
6725         f="$DIR/f62"
6726         echo foo > $f
6727         cancel_lru_locks osc
6728         lctl set_param fail_loc=0x405
6729         cat $f && error "cat succeeded, expect -EIO"
6730         lctl set_param fail_loc=0
6731 }
6732 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
6733 # match every page all of the time.
6734 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
6735
6736 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
6737 # Though this test is irrelevant anymore, it helped to reveal some
6738 # other grant bugs (LU-4482), let's keep it.
6739 test_63a() {   # was test_63
6740         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6741
6742         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
6743
6744         for i in `seq 10` ; do
6745                 dd if=/dev/zero of=$DIR/f63 bs=8k &
6746                 sleep 5
6747                 kill $!
6748                 sleep 1
6749         done
6750
6751         rm -f $DIR/f63 || true
6752 }
6753 run_test 63a "Verify oig_wait interruption does not crash ======="
6754
6755 # bug 2248 - async write errors didn't return to application on sync
6756 # bug 3677 - async write errors left page locked
6757 test_63b() {
6758         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6759
6760         debugsave
6761         lctl set_param debug=-1
6762
6763         # ensure we have a grant to do async writes
6764         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
6765         rm $DIR/$tfile
6766
6767         sync    # sync lest earlier test intercept the fail_loc
6768
6769         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6770         lctl set_param fail_loc=0x80000406
6771         $MULTIOP $DIR/$tfile Owy && \
6772                 error "sync didn't return ENOMEM"
6773         sync; sleep 2; sync     # do a real sync this time to flush page
6774         lctl get_param -n llite.*.dump_page_cache | grep locked && \
6775                 error "locked page left in cache after async error" || true
6776         debugrestore
6777 }
6778 run_test 63b "async write errors should be returned to fsync ==="
6779
6780 test_64a () {
6781         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6782
6783         df $DIR
6784         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
6785 }
6786 run_test 64a "verify filter grant calculations (in kernel) ====="
6787
6788 test_64b () {
6789         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6790
6791         sh oos.sh $MOUNT || error "oos.sh failed: $?"
6792 }
6793 run_test 64b "check out-of-space detection on client"
6794
6795 test_64c() {
6796         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
6797 }
6798 run_test 64c "verify grant shrink"
6799
6800 # this does exactly what osc_request.c:osc_announce_cached() does in
6801 # order to calculate max amount of grants to ask from server
6802 want_grant() {
6803         local tgt=$1
6804
6805         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
6806         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
6807
6808         ((rpc_in_flight ++));
6809         nrpages=$((nrpages * rpc_in_flight))
6810
6811         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
6812
6813         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
6814
6815         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
6816         local undirty=$((nrpages * PAGE_SIZE))
6817
6818         local max_extent_pages
6819         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
6820             grep grant_max_extent_size | awk '{print $2}')
6821         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
6822         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
6823         local grant_extent_tax
6824         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6825             grep grant_extent_tax | awk '{print $2}')
6826
6827         undirty=$((undirty + nrextents * grant_extent_tax))
6828
6829         echo $undirty
6830 }
6831
6832 # this is size of unit for grant allocation. It should be equal to
6833 # what tgt_grant.c:tgt_grant_chunk() calculates
6834 grant_chunk() {
6835         local tgt=$1
6836         local max_brw_size
6837         local grant_extent_tax
6838
6839         max_brw_size=$($LCTL get_param osc.${tgt}.import |
6840             grep max_brw_size | awk '{print $2}')
6841
6842         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6843             grep grant_extent_tax | awk '{print $2}')
6844
6845         echo $(((max_brw_size + grant_extent_tax) * 2))
6846 }
6847
6848 test_64d() {
6849         [ $OST1_VERSION -lt $(version_code 2.10.56) ] &&
6850                 skip "OST < 2.10.55 doesn't limit grants enough"
6851
6852         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
6853         local file=$DIR/$tfile
6854
6855         [[ $($LCTL get_param osc.${tgt}.import |
6856              grep "connect_flags:.*grant_param") ]] ||
6857                 skip "no grant_param connect flag"
6858
6859         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
6860
6861         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
6862
6863         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
6864         stack_trap "rm -f $file" EXIT
6865
6866         $LFS setstripe $file -i 0 -c 1
6867         dd if=/dev/zero of=$file bs=1M count=1000 &
6868         ddpid=$!
6869
6870         while true
6871         do
6872                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
6873                 if [[ $cur_grant -gt $max_cur_granted ]]
6874                 then
6875                         kill $ddpid
6876                         error "cur_grant $cur_grant > $max_cur_granted"
6877                 fi
6878                 kill -0 $ddpid
6879                 [[ $? -ne 0 ]] && break;
6880                 sleep 2
6881         done
6882
6883         rm -f $DIR/$tfile
6884         wait_delete_completed
6885         $LCTL set_param debug="$olddebug" 2> /dev/null || true
6886 }
6887 run_test 64d "check grant limit exceed"
6888
6889 # bug 1414 - set/get directories' stripe info
6890 test_65a() {
6891         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6892
6893         test_mkdir $DIR/$tdir
6894         touch $DIR/$tdir/f1
6895         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
6896 }
6897 run_test 65a "directory with no stripe info"
6898
6899 test_65b() {
6900         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6901
6902         test_mkdir $DIR/$tdir
6903         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
6904
6905         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6906                                                 error "setstripe"
6907         touch $DIR/$tdir/f2
6908         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
6909 }
6910 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
6911
6912 test_65c() {
6913         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6914         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
6915
6916         test_mkdir $DIR/$tdir
6917         local stripesize=$($LFS getstripe -S $DIR/$tdir)
6918
6919         $LFS setstripe -S $((stripesize * 4)) -i 1 \
6920                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
6921         touch $DIR/$tdir/f3
6922         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
6923 }
6924 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
6925
6926 test_65d() {
6927         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6928
6929         test_mkdir $DIR/$tdir
6930         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
6931         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
6932
6933         if [[ $STRIPECOUNT -le 0 ]]; then
6934                 sc=1
6935         elif [[ $STRIPECOUNT -gt 2000 ]]; then
6936 #LOV_MAX_STRIPE_COUNT is 2000
6937                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
6938         else
6939                 sc=$(($STRIPECOUNT - 1))
6940         fi
6941         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
6942         touch $DIR/$tdir/f4 $DIR/$tdir/f5
6943         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
6944                 error "lverify failed"
6945 }
6946 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
6947
6948 test_65e() {
6949         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6950
6951         test_mkdir $DIR/$tdir
6952
6953         $LFS setstripe $DIR/$tdir || error "setstripe"
6954         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
6955                                         error "no stripe info failed"
6956         touch $DIR/$tdir/f6
6957         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
6958 }
6959 run_test 65e "directory setstripe defaults"
6960
6961 test_65f() {
6962         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6963
6964         test_mkdir $DIR/${tdir}f
6965         $RUNAS $LFS setstripe $DIR/${tdir}f &&
6966                 error "setstripe succeeded" || true
6967 }
6968 run_test 65f "dir setstripe permission (should return error) ==="
6969
6970 test_65g() {
6971         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6972
6973         test_mkdir $DIR/$tdir
6974         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
6975
6976         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6977                 error "setstripe -S failed"
6978         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
6979         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
6980                 error "delete default stripe failed"
6981 }
6982 run_test 65g "directory setstripe -d"
6983
6984 test_65h() {
6985         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6986
6987         test_mkdir $DIR/$tdir
6988         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
6989
6990         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6991                 error "setstripe -S failed"
6992         test_mkdir $DIR/$tdir/dd1
6993         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
6994                 error "stripe info inherit failed"
6995 }
6996 run_test 65h "directory stripe info inherit ===================="
6997
6998 test_65i() {
6999         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7000
7001         save_layout_restore_at_exit $MOUNT
7002
7003         # bug6367: set non-default striping on root directory
7004         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
7005
7006         # bug12836: getstripe on -1 default directory striping
7007         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
7008
7009         # bug12836: getstripe -v on -1 default directory striping
7010         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
7011
7012         # bug12836: new find on -1 default directory striping
7013         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
7014 }
7015 run_test 65i "various tests to set root directory striping"
7016
7017 test_65j() { # bug6367
7018         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7019
7020         sync; sleep 1
7021
7022         # if we aren't already remounting for each test, do so for this test
7023         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
7024                 cleanup || error "failed to unmount"
7025                 setup
7026         fi
7027
7028         save_layout_restore_at_exit $MOUNT
7029
7030         $LFS setstripe -d $MOUNT || error "setstripe failed"
7031 }
7032 run_test 65j "set default striping on root directory (bug 6367)="
7033
7034 cleanup_65k() {
7035         rm -rf $DIR/$tdir
7036         wait_delete_completed
7037         do_facet $SINGLEMDS "lctl set_param -n \
7038                 osp.$ost*MDT0000.max_create_count=$max_count"
7039         do_facet $SINGLEMDS "lctl set_param -n \
7040                 osp.$ost*MDT0000.create_count=$count"
7041         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7042         echo $INACTIVE_OSC "is Activate"
7043
7044         wait_osc_import_state mds ost$ostnum FULL
7045 }
7046
7047 test_65k() { # bug11679
7048         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7049         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7050         remote_mds_nodsh && skip "remote MDS with nodsh"
7051
7052         local disable_precreate=true
7053         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
7054                 disable_precreate=false
7055
7056         echo "Check OST status: "
7057         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
7058                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
7059
7060         for OSC in $MDS_OSCS; do
7061                 echo $OSC "is active"
7062                 do_facet $SINGLEMDS lctl --device %$OSC activate
7063         done
7064
7065         for INACTIVE_OSC in $MDS_OSCS; do
7066                 local ost=$(osc_to_ost $INACTIVE_OSC)
7067                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
7068                                lov.*md*.target_obd |
7069                                awk -F: /$ost/'{ print $1 }' | head -n 1)
7070
7071                 mkdir -p $DIR/$tdir
7072                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
7073                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
7074
7075                 echo "Deactivate: " $INACTIVE_OSC
7076                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
7077
7078                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
7079                               osp.$ost*MDT0000.create_count")
7080                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
7081                                   osp.$ost*MDT0000.max_create_count")
7082                 $disable_precreate &&
7083                         do_facet $SINGLEMDS "lctl set_param -n \
7084                                 osp.$ost*MDT0000.max_create_count=0"
7085
7086                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
7087                         [ -f $DIR/$tdir/$idx ] && continue
7088                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
7089                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
7090                                 { cleanup_65k;
7091                                   error "setstripe $idx should succeed"; }
7092                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
7093                 done
7094                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
7095                 rmdir $DIR/$tdir
7096
7097                 do_facet $SINGLEMDS "lctl set_param -n \
7098                         osp.$ost*MDT0000.max_create_count=$max_count"
7099                 do_facet $SINGLEMDS "lctl set_param -n \
7100                         osp.$ost*MDT0000.create_count=$count"
7101                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7102                 echo $INACTIVE_OSC "is Activate"
7103
7104                 wait_osc_import_state mds ost$ostnum FULL
7105         done
7106 }
7107 run_test 65k "validate manual striping works properly with deactivated OSCs"
7108
7109 test_65l() { # bug 12836
7110         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7111
7112         test_mkdir -p $DIR/$tdir/test_dir
7113         $LFS setstripe -c -1 $DIR/$tdir/test_dir
7114         $LFS find -mtime -1 $DIR/$tdir >/dev/null
7115 }
7116 run_test 65l "lfs find on -1 stripe dir ========================"
7117
7118 test_65m() {
7119         local layout=$(save_layout $MOUNT)
7120         $RUNAS $LFS setstripe -c 2 $MOUNT && {
7121                 restore_layout $MOUNT $layout
7122                 error "setstripe should fail by non-root users"
7123         }
7124         true
7125 }
7126 run_test 65m "normal user can't set filesystem default stripe"
7127
7128 test_65n() {
7129         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
7130         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.12.50) ]] ||
7131                 skip "Need MDS version at least 2.12.50"
7132         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
7133
7134         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7135         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
7136         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
7137
7138         local root_layout=$(save_layout $MOUNT)
7139         stack_trap "restore_layout $MOUNT $root_layout" EXIT
7140
7141         # new subdirectory under root directory should not inherit
7142         # the default layout from root
7143         local dir1=$MOUNT/$tdir-1
7144         mkdir $dir1 || error "mkdir $dir1 failed"
7145         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
7146                 error "$dir1 shouldn't have LOV EA"
7147
7148         # delete the default layout on root directory
7149         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
7150
7151         local dir2=$MOUNT/$tdir-2
7152         mkdir $dir2 || error "mkdir $dir2 failed"
7153         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
7154                 error "$dir2 shouldn't have LOV EA"
7155
7156         # set a new striping pattern on root directory
7157         local def_stripe_size=$($LFS getstripe -S $MOUNT)
7158         local new_def_stripe_size=$((def_stripe_size * 2))
7159         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
7160                 error "set stripe size on $MOUNT failed"
7161
7162         # new file created in $dir2 should inherit the new stripe size from
7163         # the filesystem default
7164         local file2=$dir2/$tfile-2
7165         touch $file2 || error "touch $file2 failed"
7166
7167         local file2_stripe_size=$($LFS getstripe -S $file2)
7168         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
7169                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
7170
7171         local dir3=$MOUNT/$tdir-3
7172         mkdir $dir3 || error "mkdir $dir3 failed"
7173         ! getfattr -n trusted.lov $dir3 &> /dev/null ||
7174                 error "$dir3 shouldn't have LOV EA"
7175
7176         # set OST pool on root directory
7177         local pool=$TESTNAME
7178         pool_add $pool || error "add $pool failed"
7179         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
7180                 error "add targets to $pool failed"
7181
7182         $LFS setstripe -p $pool $MOUNT ||
7183                 error "set OST pool on $MOUNT failed"
7184
7185         # new file created in $dir3 should inherit the pool from
7186         # the filesystem default
7187         local file3=$dir3/$tfile-3
7188         touch $file3 || error "touch $file3 failed"
7189
7190         local file3_pool=$($LFS getstripe -p $file3)
7191         [[ "$file3_pool" = "$pool" ]] ||
7192                 error "$file3 didn't inherit OST pool $pool"
7193
7194         local dir4=$MOUNT/$tdir-4
7195         mkdir $dir4 || error "mkdir $dir4 failed"
7196         ! getfattr -n trusted.lov $dir4 &> /dev/null ||
7197                 error "$dir4 shouldn't have LOV EA"
7198
7199         # new file created in $dir4 should inherit the pool from
7200         # the filesystem default
7201         local file4=$dir4/$tfile-4
7202         touch $file4 || error "touch $file4 failed"
7203
7204         local file4_pool=$($LFS getstripe -p $file4)
7205         [[ "$file4_pool" = "$pool" ]] ||
7206                 error "$file4 didn't inherit OST pool $pool"
7207
7208         # new subdirectory under non-root directory should inherit
7209         # the default layout from its parent directory
7210         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
7211                 error "set directory layout on $dir4 failed"
7212
7213         local dir5=$dir4/$tdir-5
7214         mkdir $dir5 || error "mkdir $dir5 failed"
7215
7216         local dir4_layout=$(get_layout_param $dir4)
7217         local dir5_layout=$(get_layout_param $dir5)
7218         [[ "$dir4_layout" = "$dir5_layout" ]] ||
7219                 error "$dir5 should inherit the default layout from $dir4"
7220 }
7221 run_test 65n "don't inherit default layout from root for new subdirectories"
7222
7223 # bug 2543 - update blocks count on client
7224 test_66() {
7225         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7226
7227         COUNT=${COUNT:-8}
7228         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
7229         sync; sync_all_data; sync; sync_all_data
7230         cancel_lru_locks osc
7231         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
7232         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
7233 }
7234 run_test 66 "update inode blocks count on client ==============="
7235
7236 meminfo() {
7237         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
7238 }
7239
7240 swap_used() {
7241         swapon -s | awk '($1 == "'$1'") { print $4 }'
7242 }
7243
7244 # bug5265, obdfilter oa2dentry return -ENOENT
7245 # #define OBD_FAIL_SRV_ENOENT 0x217
7246 test_69() {
7247         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7248         remote_ost_nodsh && skip "remote OST with nodsh"
7249
7250         f="$DIR/$tfile"
7251         $LFS setstripe -c 1 -i 0 $f
7252
7253         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
7254
7255         do_facet ost1 lctl set_param fail_loc=0x217
7256         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
7257         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
7258
7259         do_facet ost1 lctl set_param fail_loc=0
7260         $DIRECTIO write $f 0 2 || error "write error"
7261
7262         cancel_lru_locks osc
7263         $DIRECTIO read $f 0 1 || error "read error"
7264
7265         do_facet ost1 lctl set_param fail_loc=0x217
7266         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
7267
7268         do_facet ost1 lctl set_param fail_loc=0
7269         rm -f $f
7270 }
7271 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
7272
7273 test_71() {
7274         test_mkdir $DIR/$tdir
7275         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
7276         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
7277 }
7278 run_test 71 "Running dbench on lustre (don't segment fault) ===="
7279
7280 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
7281         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7282         [ "$RUNAS_ID" = "$UID" ] &&
7283                 skip_env "RUNAS_ID = UID = $UID -- skipping"
7284         # Check that testing environment is properly set up. Skip if not
7285         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
7286                 skip_env "User $RUNAS_ID does not exist - skipping"
7287
7288         touch $DIR/$tfile
7289         chmod 777 $DIR/$tfile
7290         chmod ug+s $DIR/$tfile
7291         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
7292                 error "$RUNAS dd $DIR/$tfile failed"
7293         # See if we are still setuid/sgid
7294         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
7295                 error "S/gid is not dropped on write"
7296         # Now test that MDS is updated too
7297         cancel_lru_locks mdc
7298         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
7299                 error "S/gid is not dropped on MDS"
7300         rm -f $DIR/$tfile
7301 }
7302 run_test 72a "Test that remove suid works properly (bug5695) ===="
7303
7304 test_72b() { # bug 24226 -- keep mode setting when size is not changing
7305         local perm
7306
7307         [ "$RUNAS_ID" = "$UID" ] &&
7308                 skip_env "RUNAS_ID = UID = $UID -- skipping"
7309         [ "$RUNAS_ID" -eq 0 ] &&
7310                 skip_env "RUNAS_ID = 0 -- skipping"
7311         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7312         # Check that testing environment is properly set up. Skip if not
7313         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
7314                 skip_env "User $RUNAS_ID does not exist - skipping"
7315
7316         touch $DIR/${tfile}-f{g,u}
7317         test_mkdir $DIR/${tfile}-dg
7318         test_mkdir $DIR/${tfile}-du
7319         chmod 770 $DIR/${tfile}-{f,d}{g,u}
7320         chmod g+s $DIR/${tfile}-{f,d}g
7321         chmod u+s $DIR/${tfile}-{f,d}u
7322         for perm in 777 2777 4777; do
7323                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
7324                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
7325                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
7326                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
7327         done
7328         true
7329 }
7330 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
7331
7332 # bug 3462 - multiple simultaneous MDC requests
7333 test_73() {
7334         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7335
7336         test_mkdir $DIR/d73-1
7337         test_mkdir $DIR/d73-2
7338         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
7339         pid1=$!
7340
7341         lctl set_param fail_loc=0x80000129
7342         $MULTIOP $DIR/d73-1/f73-2 Oc &
7343         sleep 1
7344         lctl set_param fail_loc=0
7345
7346         $MULTIOP $DIR/d73-2/f73-3 Oc &
7347         pid3=$!
7348
7349         kill -USR1 $pid1
7350         wait $pid1 || return 1
7351
7352         sleep 25
7353
7354         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
7355         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
7356         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
7357
7358         rm -rf $DIR/d73-*
7359 }
7360 run_test 73 "multiple MDC requests (should not deadlock)"
7361
7362 test_74a() { # bug 6149, 6184
7363         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7364
7365         touch $DIR/f74a
7366         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
7367         #
7368         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
7369         # will spin in a tight reconnection loop
7370         $LCTL set_param fail_loc=0x8000030e
7371         # get any lock that won't be difficult - lookup works.
7372         ls $DIR/f74a
7373         $LCTL set_param fail_loc=0
7374         rm -f $DIR/f74a
7375         true
7376 }
7377 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
7378
7379 test_74b() { # bug 13310
7380         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7381
7382         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
7383         #
7384         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
7385         # will spin in a tight reconnection loop
7386         $LCTL set_param fail_loc=0x8000030e
7387         # get a "difficult" lock
7388         touch $DIR/f74b
7389         $LCTL set_param fail_loc=0
7390         rm -f $DIR/f74b
7391         true
7392 }
7393 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
7394
7395 test_74c() {
7396         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7397
7398         #define OBD_FAIL_LDLM_NEW_LOCK
7399         $LCTL set_param fail_loc=0x319
7400         touch $DIR/$tfile && error "touch successful"
7401         $LCTL set_param fail_loc=0
7402         true
7403 }
7404 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
7405
7406 num_inodes() {
7407         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
7408 }
7409
7410 test_76() { # Now for bug 20433, added originally in bug 1443
7411         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7412
7413         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
7414
7415         cancel_lru_locks osc
7416         BEFORE_INODES=$(num_inodes)
7417         echo "before inodes: $BEFORE_INODES"
7418         local COUNT=1000
7419         [ "$SLOW" = "no" ] && COUNT=100
7420         for i in $(seq $COUNT); do
7421                 touch $DIR/$tfile
7422                 rm -f $DIR/$tfile
7423         done
7424         cancel_lru_locks osc
7425         AFTER_INODES=$(num_inodes)
7426         echo "after inodes: $AFTER_INODES"
7427         local wait=0
7428         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
7429                 sleep 2
7430                 AFTER_INODES=$(num_inodes)
7431                 wait=$((wait+2))
7432                 echo "wait $wait seconds inodes: $AFTER_INODES"
7433                 if [ $wait -gt 30 ]; then
7434                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
7435                 fi
7436         done
7437 }
7438 run_test 76 "confirm clients recycle inodes properly ===="
7439
7440
7441 export ORIG_CSUM=""
7442 set_checksums()
7443 {
7444         # Note: in sptlrpc modes which enable its own bulk checksum, the
7445         # original crc32_le bulk checksum will be automatically disabled,
7446         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
7447         # will be checked by sptlrpc code against sptlrpc bulk checksum.
7448         # In this case set_checksums() will not be no-op, because sptlrpc
7449         # bulk checksum will be enabled all through the test.
7450
7451         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
7452         lctl set_param -n osc.*.checksums $1
7453         return 0
7454 }
7455
7456 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
7457                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
7458 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
7459                              tr -d [] | head -n1)}
7460 set_checksum_type()
7461 {
7462         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
7463         log "set checksum type to $1"
7464         return 0
7465 }
7466 F77_TMP=$TMP/f77-temp
7467 F77SZ=8
7468 setup_f77() {
7469         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
7470                 error "error writing to $F77_TMP"
7471 }
7472
7473 test_77a() { # bug 10889
7474         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7475         $GSS && skip_env "could not run with gss"
7476
7477         [ ! -f $F77_TMP ] && setup_f77
7478         set_checksums 1
7479         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
7480         set_checksums 0
7481         rm -f $DIR/$tfile
7482 }
7483 run_test 77a "normal checksum read/write operation"
7484
7485 test_77b() { # bug 10889
7486         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7487         $GSS && skip_env "could not run with gss"
7488
7489         [ ! -f $F77_TMP ] && setup_f77
7490         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7491         $LCTL set_param fail_loc=0x80000409
7492         set_checksums 1
7493
7494         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7495                 error "dd error: $?"
7496         $LCTL set_param fail_loc=0
7497
7498         for algo in $CKSUM_TYPES; do
7499                 cancel_lru_locks osc
7500                 set_checksum_type $algo
7501                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7502                 $LCTL set_param fail_loc=0x80000408
7503                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
7504                 $LCTL set_param fail_loc=0
7505         done
7506         set_checksums 0
7507         set_checksum_type $ORIG_CSUM_TYPE
7508         rm -f $DIR/$tfile
7509 }
7510 run_test 77b "checksum error on client write, read"
7511
7512 cleanup_77c() {
7513         trap 0
7514         set_checksums 0
7515         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
7516         $check_ost &&
7517                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
7518         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
7519         $check_ost && [ -n "$ost_file_prefix" ] &&
7520                 do_facet ost1 rm -f ${ost_file_prefix}\*
7521 }
7522
7523 test_77c() {
7524         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7525         $GSS && skip_env "could not run with gss"
7526         remote_ost_nodsh && skip "remote OST with nodsh"
7527
7528         local bad1
7529         local osc_file_prefix
7530         local osc_file
7531         local check_ost=false
7532         local ost_file_prefix
7533         local ost_file
7534         local orig_cksum
7535         local dump_cksum
7536         local fid
7537
7538         # ensure corruption will occur on first OSS/OST
7539         $LFS setstripe -i 0 $DIR/$tfile
7540
7541         [ ! -f $F77_TMP ] && setup_f77
7542         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7543                 error "dd write error: $?"
7544         fid=$($LFS path2fid $DIR/$tfile)
7545
7546         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
7547         then
7548                 check_ost=true
7549                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
7550                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
7551         else
7552                 echo "OSS do not support bulk pages dump upon error"
7553         fi
7554
7555         osc_file_prefix=$($LCTL get_param -n debug_path)
7556         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
7557
7558         trap cleanup_77c EXIT
7559
7560         set_checksums 1
7561         # enable bulk pages dump upon error on Client
7562         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
7563         # enable bulk pages dump upon error on OSS
7564         $check_ost &&
7565                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
7566
7567         # flush Client cache to allow next read to reach OSS
7568         cancel_lru_locks osc
7569
7570         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
7571         $LCTL set_param fail_loc=0x80000408
7572         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
7573         $LCTL set_param fail_loc=0
7574
7575         rm -f $DIR/$tfile
7576
7577         # check cksum dump on Client
7578         osc_file=$(ls ${osc_file_prefix}*)
7579         [ -n "$osc_file" ] || error "no checksum dump file on Client"
7580         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
7581         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
7582         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
7583         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
7584                      cksum)
7585         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
7586         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7587                 error "dump content does not match on Client"
7588
7589         $check_ost || skip "No need to check cksum dump on OSS"
7590
7591         # check cksum dump on OSS
7592         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
7593         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
7594         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
7595         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
7596         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7597                 error "dump content does not match on OSS"
7598
7599         cleanup_77c
7600 }
7601 run_test 77c "checksum error on client read with debug"
7602
7603 test_77d() { # bug 10889
7604         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7605         $GSS && skip_env "could not run with gss"
7606
7607         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7608         $LCTL set_param fail_loc=0x80000409
7609         set_checksums 1
7610         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
7611                 error "direct write: rc=$?"
7612         $LCTL set_param fail_loc=0
7613         set_checksums 0
7614
7615         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7616         $LCTL set_param fail_loc=0x80000408
7617         set_checksums 1
7618         cancel_lru_locks osc
7619         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
7620                 error "direct read: rc=$?"
7621         $LCTL set_param fail_loc=0
7622         set_checksums 0
7623 }
7624 run_test 77d "checksum error on OST direct write, read"
7625
7626 test_77f() { # bug 10889
7627         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7628         $GSS && skip_env "could not run with gss"
7629
7630         set_checksums 1
7631         for algo in $CKSUM_TYPES; do
7632                 cancel_lru_locks osc
7633                 set_checksum_type $algo
7634                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7635                 $LCTL set_param fail_loc=0x409
7636                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
7637                         error "direct write succeeded"
7638                 $LCTL set_param fail_loc=0
7639         done
7640         set_checksum_type $ORIG_CSUM_TYPE
7641         set_checksums 0
7642 }
7643 run_test 77f "repeat checksum error on write (expect error)"
7644
7645 test_77g() { # bug 10889
7646         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7647         $GSS && skip_env "could not run with gss"
7648         remote_ost_nodsh && skip "remote OST with nodsh"
7649
7650         [ ! -f $F77_TMP ] && setup_f77
7651
7652         local file=$DIR/$tfile
7653         stack_trap "rm -f $file" EXIT
7654
7655         $LFS setstripe -c 1 -i 0 $file
7656         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
7657         do_facet ost1 lctl set_param fail_loc=0x8000021a
7658         set_checksums 1
7659         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
7660                 error "write error: rc=$?"
7661         do_facet ost1 lctl set_param fail_loc=0
7662         set_checksums 0
7663
7664         cancel_lru_locks osc
7665         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
7666         do_facet ost1 lctl set_param fail_loc=0x8000021b
7667         set_checksums 1
7668         cmp $F77_TMP $file || error "file compare failed"
7669         do_facet ost1 lctl set_param fail_loc=0
7670         set_checksums 0
7671 }
7672 run_test 77g "checksum error on OST write, read"
7673
7674 test_77k() { # LU-10906
7675         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7676         $GSS && skip_env "could not run with gss"
7677
7678         local cksum_param="osc.$FSNAME*.checksums"
7679         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
7680         local checksum
7681         local i
7682
7683         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
7684         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
7685         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
7686                 EXIT
7687
7688         for i in 0 1; do
7689                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
7690                         error "failed to set checksum=$i on MGS"
7691                 wait_update $HOSTNAME "$get_checksum" $i
7692                 #remount
7693                 echo "remount client, checksum should be $i"
7694                 remount_client $MOUNT || "failed to remount client"
7695                 checksum=$(eval $get_checksum)
7696                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
7697         done
7698         # remove persistent param to avoid races with checksum mountopt below
7699         do_facet mgs $LCTL set_param -P -d $cksum_param ||
7700                 error "failed to delete checksum on MGS"
7701
7702         for opt in "checksum" "nochecksum"; do
7703                 #remount with mount option
7704                 echo "remount client with option $opt, checksum should be $i"
7705                 umount_client $MOUNT || "failed to umount client"
7706                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
7707                         "failed to mount client with option '$opt'"
7708                 checksum=$(eval $get_checksum)
7709                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
7710                 i=$((i - 1))
7711         done
7712
7713         remount_client $MOUNT || "failed to remount client"
7714 }
7715 run_test 77k "enable/disable checksum correctly"
7716
7717 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
7718 rm -f $F77_TMP
7719 unset F77_TMP
7720
7721 cleanup_test_78() {
7722         trap 0
7723         rm -f $DIR/$tfile
7724 }
7725
7726 test_78() { # bug 10901
7727         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7728         remote_ost || skip_env "local OST"
7729
7730         NSEQ=5
7731         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
7732         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
7733         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
7734         echo "MemTotal: $MEMTOTAL"
7735
7736         # reserve 256MB of memory for the kernel and other running processes,
7737         # and then take 1/2 of the remaining memory for the read/write buffers.
7738         if [ $MEMTOTAL -gt 512 ] ;then
7739                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
7740         else
7741                 # for those poor memory-starved high-end clusters...
7742                 MEMTOTAL=$((MEMTOTAL / 2))
7743         fi
7744         echo "Mem to use for directio: $MEMTOTAL"
7745
7746         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
7747         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
7748         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
7749         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
7750                 head -n1)
7751         echo "Smallest OST: $SMALLESTOST"
7752         [[ $SMALLESTOST -lt 10240 ]] &&
7753                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
7754
7755         trap cleanup_test_78 EXIT
7756
7757         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
7758                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
7759
7760         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
7761         echo "File size: $F78SIZE"
7762         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
7763         for i in $(seq 1 $NSEQ); do
7764                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
7765                 echo directIO rdwr round $i of $NSEQ
7766                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
7767         done
7768
7769         cleanup_test_78
7770 }
7771 run_test 78 "handle large O_DIRECT writes correctly ============"
7772
7773 test_79() { # bug 12743
7774         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7775
7776         wait_delete_completed
7777
7778         BKTOTAL=$(calc_osc_kbytes kbytestotal)
7779         BKFREE=$(calc_osc_kbytes kbytesfree)
7780         BKAVAIL=$(calc_osc_kbytes kbytesavail)
7781
7782         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
7783         DFTOTAL=`echo $STRING | cut -d, -f1`
7784         DFUSED=`echo $STRING  | cut -d, -f2`
7785         DFAVAIL=`echo $STRING | cut -d, -f3`
7786         DFFREE=$(($DFTOTAL - $DFUSED))
7787
7788         ALLOWANCE=$((64 * $OSTCOUNT))
7789
7790         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
7791            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
7792                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
7793         fi
7794         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
7795            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
7796                 error "df free($DFFREE) mismatch OST free($BKFREE)"
7797         fi
7798         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
7799            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
7800                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
7801         fi
7802 }
7803 run_test 79 "df report consistency check ======================="
7804
7805 test_80() { # bug 10718
7806         remote_ost_nodsh && skip "remote OST with nodsh"
7807         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7808
7809         # relax strong synchronous semantics for slow backends like ZFS
7810         local soc="obdfilter.*.sync_on_lock_cancel"
7811         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
7812         local hosts=
7813         if [ "$soc_old" != "never" ] &&
7814                 [ "$ost1_FSTYPE" != "ldiskfs" ]; then
7815                         hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
7816                                 facet_active_host $host; done | sort -u)
7817                         do_nodes $hosts lctl set_param $soc=never
7818         fi
7819
7820         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
7821         sync; sleep 1; sync
7822         local BEFORE=`date +%s`
7823         cancel_lru_locks osc
7824         local AFTER=`date +%s`
7825         local DIFF=$((AFTER-BEFORE))
7826         if [ $DIFF -gt 1 ] ; then
7827                 error "elapsed for 1M@1T = $DIFF"
7828         fi
7829
7830         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
7831
7832         rm -f $DIR/$tfile
7833 }
7834 run_test 80 "Page eviction is equally fast at high offsets too  ===="
7835
7836 test_81a() { # LU-456
7837         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7838         remote_ost_nodsh && skip "remote OST with nodsh"
7839
7840         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
7841         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
7842         do_facet ost1 lctl set_param fail_loc=0x80000228
7843
7844         # write should trigger a retry and success
7845         $LFS setstripe -i 0 -c 1 $DIR/$tfile
7846         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7847         RC=$?
7848         if [ $RC -ne 0 ] ; then
7849                 error "write should success, but failed for $RC"
7850         fi
7851 }
7852 run_test 81a "OST should retry write when get -ENOSPC ==============="
7853
7854 test_81b() { # LU-456
7855         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7856         remote_ost_nodsh && skip "remote OST with nodsh"
7857
7858         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
7859         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
7860         do_facet ost1 lctl set_param fail_loc=0x228
7861
7862         # write should retry several times and return -ENOSPC finally
7863         $LFS setstripe -i 0 -c 1 $DIR/$tfile
7864         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7865         RC=$?
7866         ENOSPC=28
7867         if [ $RC -ne $ENOSPC ] ; then
7868                 error "dd should fail for -ENOSPC, but succeed."
7869         fi
7870 }
7871 run_test 81b "OST should return -ENOSPC when retry still fails ======="
7872
7873 test_82() { # LU-1031
7874         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
7875         local gid1=14091995
7876         local gid2=16022000
7877
7878         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
7879         local MULTIPID1=$!
7880         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
7881         local MULTIPID2=$!
7882         kill -USR1 $MULTIPID2
7883         sleep 2
7884         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
7885                 error "First grouplock does not block second one"
7886         else
7887                 echo "Second grouplock blocks first one"
7888         fi
7889         kill -USR1 $MULTIPID1
7890         wait $MULTIPID1
7891         wait $MULTIPID2
7892 }
7893 run_test 82 "Basic grouplock test"
7894
7895 test_99() {
7896         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
7897
7898         test_mkdir $DIR/$tdir.cvsroot
7899         chown $RUNAS_ID $DIR/$tdir.cvsroot
7900
7901         cd $TMP
7902         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
7903
7904         cd /etc/init.d
7905         # some versions of cvs import exit(1) when asked to import links or
7906         # files they can't read.  ignore those files.
7907         local toignore=$(find . -type l -printf '-I %f\n' -o \
7908                          ! -perm /4 -printf '-I %f\n')
7909         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
7910                 $tdir.reposname vtag rtag
7911
7912         cd $DIR
7913         test_mkdir $DIR/$tdir.reposname
7914         chown $RUNAS_ID $DIR/$tdir.reposname
7915         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
7916
7917         cd $DIR/$tdir.reposname
7918         $RUNAS touch foo99
7919         $RUNAS cvs add -m 'addmsg' foo99
7920         $RUNAS cvs update
7921         $RUNAS cvs commit -m 'nomsg' foo99
7922         rm -fr $DIR/$tdir.cvsroot
7923 }
7924 run_test 99 "cvs strange file/directory operations"
7925
7926 test_100() {
7927         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7928         [[ "$NETTYPE" =~ tcp ]] ||
7929                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
7930         remote_ost_nodsh && skip "remote OST with nodsh"
7931         remote_mds_nodsh && skip "remote MDS with nodsh"
7932         remote_servers ||
7933                 skip "useless for local single node setup"
7934
7935         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
7936                 [ "$PROT" != "tcp" ] && continue
7937                 RPORT=$(echo $REMOTE | cut -d: -f2)
7938                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
7939
7940                 rc=0
7941                 LPORT=`echo $LOCAL | cut -d: -f2`
7942                 if [ $LPORT -ge 1024 ]; then
7943                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
7944                         netstat -tna
7945                         error_exit "local: $LPORT > 1024, remote: $RPORT"
7946                 fi
7947         done
7948         [ "$rc" = 0 ] || error_exit "privileged port not found" )
7949 }
7950 run_test 100 "check local port using privileged port ==========="
7951
7952 function get_named_value()
7953 {
7954     local tag
7955
7956     tag=$1
7957     while read ;do
7958         line=$REPLY
7959         case $line in
7960         $tag*)
7961             echo $line | sed "s/^$tag[ ]*//"
7962             break
7963             ;;
7964         esac
7965     done
7966 }
7967
7968 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
7969                    awk '/^max_cached_mb/ { print $2 }')
7970
7971 cleanup_101a() {
7972         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
7973         trap 0
7974 }
7975
7976 test_101a() {
7977         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7978         [ $MDSCOUNT -ge 2 ] && skip_env "needs < 2 MDTs" #LU-4322
7979
7980         local s
7981         local discard
7982         local nreads=10000
7983         local cache_limit=32
7984
7985         $LCTL set_param -n osc.*-osc*.rpc_stats 0
7986         trap cleanup_101a EXIT
7987         $LCTL set_param -n llite.*.read_ahead_stats 0
7988         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
7989
7990         #
7991         # randomly read 10000 of 64K chunks from file 3x 32MB in size
7992         #
7993         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
7994         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
7995
7996         discard=0
7997         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
7998                 get_named_value 'read but discarded' | cut -d" " -f1); do
7999                         discard=$(($discard + $s))
8000         done
8001         cleanup_101a
8002
8003         if [[ $(($discard * 10)) -gt $nreads ]]; then
8004                 $LCTL get_param osc.*-osc*.rpc_stats
8005                 $LCTL get_param llite.*.read_ahead_stats
8006                 error "too many ($discard) discarded pages"
8007         fi
8008         rm -f $DIR/$tfile || true
8009 }
8010 run_test 101a "check read-ahead for random reads"
8011
8012 setup_test101bc() {
8013         test_mkdir $DIR/$tdir
8014         local ssize=$1
8015         local FILE_LENGTH=$2
8016         STRIPE_OFFSET=0
8017
8018         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
8019
8020         local list=$(comma_list $(osts_nodes))
8021         set_osd_param $list '' read_cache_enable 0
8022         set_osd_param $list '' writethrough_cache_enable 0
8023
8024         trap cleanup_test101bc EXIT
8025         # prepare the read-ahead file
8026         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
8027
8028         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
8029                                 count=$FILE_SIZE_MB 2> /dev/null
8030
8031 }
8032
8033 cleanup_test101bc() {
8034         trap 0
8035         rm -rf $DIR/$tdir
8036         rm -f $DIR/$tfile
8037
8038         local list=$(comma_list $(osts_nodes))
8039         set_osd_param $list '' read_cache_enable 1
8040         set_osd_param $list '' writethrough_cache_enable 1
8041 }
8042
8043 calc_total() {
8044         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
8045 }
8046
8047 ra_check_101() {
8048         local READ_SIZE=$1
8049         local STRIPE_SIZE=$2
8050         local FILE_LENGTH=$3
8051         local RA_INC=1048576
8052         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
8053         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
8054                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
8055         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
8056                         get_named_value 'read but discarded' |
8057                         cut -d" " -f1 | calc_total)
8058         if [[ $DISCARD -gt $discard_limit ]]; then
8059                 $LCTL get_param llite.*.read_ahead_stats
8060                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
8061         else
8062                 echo "Read-ahead success for size ${READ_SIZE}"
8063         fi
8064 }
8065
8066 test_101b() {
8067         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8068         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8069
8070         local STRIPE_SIZE=1048576
8071         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
8072
8073         if [ $SLOW == "yes" ]; then
8074                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
8075         else
8076                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
8077         fi
8078
8079         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
8080
8081         # prepare the read-ahead file
8082         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8083         cancel_lru_locks osc
8084         for BIDX in 2 4 8 16 32 64 128 256
8085         do
8086                 local BSIZE=$((BIDX*4096))
8087                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
8088                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
8089                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
8090                 $LCTL set_param -n llite.*.read_ahead_stats 0
8091                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
8092                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
8093                 cancel_lru_locks osc
8094                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
8095         done
8096         cleanup_test101bc
8097         true
8098 }
8099 run_test 101b "check stride-io mode read-ahead ================="
8100
8101 test_101c() {
8102         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8103
8104         local STRIPE_SIZE=1048576
8105         local FILE_LENGTH=$((STRIPE_SIZE*100))
8106         local nreads=10000
8107         local rsize=65536
8108         local osc_rpc_stats
8109
8110         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8111
8112         cancel_lru_locks osc
8113         $LCTL set_param osc.*.rpc_stats 0
8114         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
8115         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
8116                 local stats=$($LCTL get_param -n $osc_rpc_stats)
8117                 local lines=$(echo "$stats" | awk 'END {print NR;}')
8118                 local size
8119
8120                 if [ $lines -le 20 ]; then
8121                         continue
8122                 fi
8123                 for size in 1 2 4 8; do
8124                         local rpc=$(echo "$stats" |
8125                                     awk '($1 == "'$size':") {print $2; exit; }')
8126                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
8127                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
8128                 done
8129                 echo "$osc_rpc_stats check passed!"
8130         done
8131         cleanup_test101bc
8132         true
8133 }
8134 run_test 101c "check stripe_size aligned read-ahead ================="
8135
8136 set_read_ahead() {
8137         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
8138         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
8139 }
8140
8141 test_101d() {
8142         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8143
8144         local file=$DIR/$tfile
8145         local sz_MB=${FILESIZE_101d:-500}
8146         local ra_MB=${READAHEAD_MB:-40}
8147
8148         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
8149         [ $free_MB -lt $sz_MB ] &&
8150                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
8151
8152         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
8153         $LFS setstripe -c -1 $file || error "setstripe failed"
8154
8155         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
8156         echo Cancel LRU locks on lustre client to flush the client cache
8157         cancel_lru_locks osc
8158
8159         echo Disable read-ahead
8160         local old_READAHEAD=$(set_read_ahead 0)
8161
8162         echo Reading the test file $file with read-ahead disabled
8163         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8164
8165         echo Cancel LRU locks on lustre client to flush the client cache
8166         cancel_lru_locks osc
8167         echo Enable read-ahead with ${ra_MB}MB
8168         set_read_ahead $ra_MB
8169
8170         echo Reading the test file $file with read-ahead enabled
8171         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8172
8173         echo "read-ahead disabled time read $raOFF"
8174         echo "read-ahead enabled  time read $raON"
8175
8176         set_read_ahead $old_READAHEAD
8177         rm -f $file
8178         wait_delete_completed
8179
8180         [ $raOFF -le 1 ] || [ $raON -lt $raOFF ] ||
8181                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
8182 }
8183 run_test 101d "file read with and without read-ahead enabled"
8184
8185 test_101e() {
8186         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8187
8188         local file=$DIR/$tfile
8189         local size_KB=500  #KB
8190         local count=100
8191         local bsize=1024
8192
8193         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
8194         local need_KB=$((count * size_KB))
8195         [[ $free_KB -le $need_KB ]] &&
8196                 skip_env "Need free space $need_KB, have $free_KB"
8197
8198         echo "Creating $count ${size_KB}K test files"
8199         for ((i = 0; i < $count; i++)); do
8200                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
8201         done
8202
8203         echo "Cancel LRU locks on lustre client to flush the client cache"
8204         cancel_lru_locks $OSC
8205
8206         echo "Reset readahead stats"
8207         $LCTL set_param -n llite.*.read_ahead_stats 0
8208
8209         for ((i = 0; i < $count; i++)); do
8210                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
8211         done
8212
8213         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
8214                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
8215
8216         for ((i = 0; i < $count; i++)); do
8217                 rm -rf $file.$i 2>/dev/null
8218         done
8219
8220         #10000 means 20% reads are missing in readahead
8221         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
8222 }
8223 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
8224
8225 test_101f() {
8226         which iozone || skip_env "no iozone installed"
8227
8228         local old_debug=$($LCTL get_param debug)
8229         old_debug=${old_debug#*=}
8230         $LCTL set_param debug="reada mmap"
8231
8232         # create a test file
8233         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
8234
8235         echo Cancel LRU locks on lustre client to flush the client cache
8236         cancel_lru_locks osc
8237
8238         echo Reset readahead stats
8239         $LCTL set_param -n llite.*.read_ahead_stats 0
8240
8241         echo mmap read the file with small block size
8242         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
8243                 > /dev/null 2>&1
8244
8245         echo checking missing pages
8246         $LCTL get_param llite.*.read_ahead_stats
8247         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
8248                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
8249
8250         $LCTL set_param debug="$old_debug"
8251         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
8252         rm -f $DIR/$tfile
8253 }
8254 run_test 101f "check mmap read performance"
8255
8256 test_101g_brw_size_test() {
8257         local mb=$1
8258         local pages=$((mb * 1048576 / PAGE_SIZE))
8259         local file=$DIR/$tfile
8260
8261         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
8262                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
8263         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
8264                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
8265                         return 2
8266         done
8267
8268         stack_trap "rm -f $file" EXIT
8269         $LCTL set_param -n osc.*.rpc_stats=0
8270
8271         # 10 RPCs should be enough for the test
8272         local count=10
8273         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
8274                 { error "dd write ${mb} MB blocks failed"; return 3; }
8275         cancel_lru_locks osc
8276         dd of=/dev/null if=$file bs=${mb}M count=$count ||
8277                 { error "dd write ${mb} MB blocks failed"; return 4; }
8278
8279         # calculate number of full-sized read and write RPCs
8280         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
8281                 sed -n '/pages per rpc/,/^$/p' |
8282                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
8283                 END { print reads,writes }'))
8284         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
8285                 return 5
8286         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
8287                 return 6
8288
8289         return 0
8290 }
8291
8292 test_101g() {
8293         remote_ost_nodsh && skip "remote OST with nodsh"
8294
8295         local rpcs
8296         local osts=$(get_facets OST)
8297         local list=$(comma_list $(osts_nodes))
8298         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
8299         local brw_size="obdfilter.*.brw_size"
8300
8301         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8302
8303         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
8304
8305         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
8306                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
8307                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
8308            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
8309                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
8310                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
8311
8312                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
8313                         suffix="M"
8314
8315                 if [[ $orig_mb -lt 16 ]]; then
8316                         save_lustre_params $osts "$brw_size" > $p
8317                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
8318                                 error "set 16MB RPC size failed"
8319
8320                         echo "remount client to enable new RPC size"
8321                         remount_client $MOUNT || error "remount_client failed"
8322                 fi
8323
8324                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
8325                 # should be able to set brw_size=12, but no rpc_stats for that
8326                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
8327         fi
8328
8329         test_101g_brw_size_test 4 || error "4MB RPC test failed"
8330
8331         if [[ $orig_mb -lt 16 ]]; then
8332                 restore_lustre_params < $p
8333                 remount_client $MOUNT || error "remount_client restore failed"
8334         fi
8335
8336         rm -f $p $DIR/$tfile
8337 }
8338 run_test 101g "Big bulk(4/16 MiB) readahead"
8339
8340 setup_test102() {
8341         test_mkdir $DIR/$tdir
8342         chown $RUNAS_ID $DIR/$tdir
8343         STRIPE_SIZE=65536
8344         STRIPE_OFFSET=1
8345         STRIPE_COUNT=$OSTCOUNT
8346         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
8347
8348         trap cleanup_test102 EXIT
8349         cd $DIR
8350         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
8351         cd $DIR/$tdir
8352         for num in 1 2 3 4; do
8353                 for count in $(seq 1 $STRIPE_COUNT); do
8354                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
8355                                 local size=`expr $STRIPE_SIZE \* $num`
8356                                 local file=file"$num-$idx-$count"
8357                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
8358                         done
8359                 done
8360         done
8361
8362         cd $DIR
8363         $1 tar cf $TMP/f102.tar $tdir --xattrs
8364 }
8365
8366 cleanup_test102() {
8367         trap 0
8368         rm -f $TMP/f102.tar
8369         rm -rf $DIR/d0.sanity/d102
8370 }
8371
8372 test_102a() {
8373         [ "$UID" != 0 ] && skip "must run as root"
8374         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
8375                 skip_env "must have user_xattr"
8376
8377         [ -z "$(which setfattr 2>/dev/null)" ] &&
8378                 skip_env "could not find setfattr"
8379
8380         local testfile=$DIR/$tfile
8381
8382         touch $testfile
8383         echo "set/get xattr..."
8384         setfattr -n trusted.name1 -v value1 $testfile ||
8385                 error "setfattr -n trusted.name1=value1 $testfile failed"
8386         getfattr -n trusted.name1 $testfile 2> /dev/null |
8387           grep "trusted.name1=.value1" ||
8388                 error "$testfile missing trusted.name1=value1"
8389
8390         setfattr -n user.author1 -v author1 $testfile ||
8391                 error "setfattr -n user.author1=author1 $testfile failed"
8392         getfattr -n user.author1 $testfile 2> /dev/null |
8393           grep "user.author1=.author1" ||
8394                 error "$testfile missing trusted.author1=author1"
8395
8396         echo "listxattr..."
8397         setfattr -n trusted.name2 -v value2 $testfile ||
8398                 error "$testfile unable to set trusted.name2"
8399         setfattr -n trusted.name3 -v value3 $testfile ||
8400                 error "$testfile unable to set trusted.name3"
8401         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
8402             grep "trusted.name" | wc -l) -eq 3 ] ||
8403                 error "$testfile missing 3 trusted.name xattrs"
8404
8405         setfattr -n user.author2 -v author2 $testfile ||
8406                 error "$testfile unable to set user.author2"
8407         setfattr -n user.author3 -v author3 $testfile ||
8408                 error "$testfile unable to set user.author3"
8409         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
8410             grep "user.author" | wc -l) -eq 3 ] ||
8411                 error "$testfile missing 3 user.author xattrs"
8412
8413         echo "remove xattr..."
8414         setfattr -x trusted.name1 $testfile ||
8415                 error "$testfile error deleting trusted.name1"
8416         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
8417                 error "$testfile did not delete trusted.name1 xattr"
8418
8419         setfattr -x user.author1 $testfile ||
8420                 error "$testfile error deleting user.author1"
8421         echo "set lustre special xattr ..."
8422         $LFS setstripe -c1 $testfile
8423         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
8424                 awk -F "=" '/trusted.lov/ { print $2 }' )
8425         setfattr -n "trusted.lov" -v $lovea $testfile ||
8426                 error "$testfile doesn't ignore setting trusted.lov again"
8427         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
8428                 error "$testfile allow setting invalid trusted.lov"
8429         rm -f $testfile
8430 }
8431 run_test 102a "user xattr test =================================="
8432
8433 test_102b() {
8434         [ -z "$(which setfattr 2>/dev/null)" ] &&
8435                 skip_env "could not find setfattr"
8436         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8437
8438         # b10930: get/set/list trusted.lov xattr
8439         echo "get/set/list trusted.lov xattr ..."
8440         local testfile=$DIR/$tfile
8441         $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
8442                 error "setstripe failed"
8443         local STRIPECOUNT=$($LFS getstripe -c $testfile) ||
8444                 error "getstripe failed"
8445         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
8446                 error "can't get trusted.lov from $testfile"
8447
8448         local testfile2=${testfile}2
8449         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
8450                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
8451
8452         $MCREATE $testfile2
8453         setfattr -n trusted.lov -v $value $testfile2
8454         local stripe_size=$($LFS getstripe -S $testfile2)
8455         local stripe_count=$($LFS getstripe -c $testfile2)
8456         [[ $stripe_size -eq 65536 ]] ||
8457                 error "stripe size $stripe_size != 65536"
8458         [[ $stripe_count -eq $STRIPECOUNT ]] ||
8459                 error "stripe count $stripe_count != $STRIPECOUNT"
8460         rm -f $DIR/$tfile
8461 }
8462 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
8463
8464 test_102c() {
8465         [ -z "$(which setfattr 2>/dev/null)" ] &&
8466                 skip_env "could not find setfattr"
8467         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8468
8469         # b10930: get/set/list lustre.lov xattr
8470         echo "get/set/list lustre.lov xattr ..."
8471         test_mkdir $DIR/$tdir
8472         chown $RUNAS_ID $DIR/$tdir
8473         local testfile=$DIR/$tdir/$tfile
8474         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
8475                 error "setstripe failed"
8476         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
8477                 error "getstripe failed"
8478         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
8479         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
8480
8481         local testfile2=${testfile}2
8482         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
8483                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
8484
8485         $RUNAS $MCREATE $testfile2
8486         $RUNAS setfattr -n lustre.lov -v $value $testfile2
8487         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
8488         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
8489         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
8490         [ $stripe_count -eq $STRIPECOUNT ] ||
8491                 error "stripe count $stripe_count != $STRIPECOUNT"
8492 }
8493 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
8494
8495 compare_stripe_info1() {
8496         local stripe_index_all_zero=true
8497
8498         for num in 1 2 3 4; do
8499                 for count in $(seq 1 $STRIPE_COUNT); do
8500                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
8501                                 local size=$((STRIPE_SIZE * num))
8502                                 local file=file"$num-$offset-$count"
8503                                 stripe_size=$($LFS getstripe -S $PWD/$file)
8504                                 [[ $stripe_size -ne $size ]] &&
8505                                     error "$file: size $stripe_size != $size"
8506                                 stripe_count=$($LFS getstripe -c $PWD/$file)
8507                                 # allow fewer stripes to be created, ORI-601
8508                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
8509                                     error "$file: count $stripe_count != $count"
8510                                 stripe_index=$($LFS getstripe -i $PWD/$file)
8511                                 [[ $stripe_index -ne 0 ]] &&
8512                                         stripe_index_all_zero=false
8513                         done
8514                 done
8515         done
8516         $stripe_index_all_zero &&
8517                 error "all files are being extracted starting from OST index 0"
8518         return 0
8519 }
8520
8521 have_xattrs_include() {
8522         tar --help | grep -q xattrs-include &&
8523                 echo --xattrs-include="lustre.*"
8524 }
8525
8526 test_102d() {
8527         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8528         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8529
8530         XINC=$(have_xattrs_include)
8531         setup_test102
8532         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
8533         cd $DIR/$tdir/$tdir
8534         compare_stripe_info1
8535 }
8536 run_test 102d "tar restore stripe info from tarfile,not keep osts"
8537
8538 test_102f() {
8539         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8540         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8541
8542         XINC=$(have_xattrs_include)
8543         setup_test102
8544         test_mkdir $DIR/$tdir.restore
8545         cd $DIR
8546         tar cf - --xattrs $tdir | tar xf - \
8547                 -C $DIR/$tdir.restore --xattrs $XINC
8548         cd $DIR/$tdir.restore/$tdir
8549         compare_stripe_info1
8550 }
8551 run_test 102f "tar copy files, not keep osts"
8552
8553 grow_xattr() {
8554         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
8555                 skip "must have user_xattr"
8556         [ -z "$(which setfattr 2>/dev/null)" ] &&
8557                 skip_env "could not find setfattr"
8558         [ -z "$(which getfattr 2>/dev/null)" ] &&
8559                 skip_env "could not find getfattr"
8560
8561         local xsize=${1:-1024}  # in bytes
8562         local file=$DIR/$tfile
8563         local value="$(generate_string $xsize)"
8564         local xbig=trusted.big
8565         local toobig=$2
8566
8567         touch $file
8568         log "save $xbig on $file"
8569         if [ -z "$toobig" ]
8570         then
8571                 setfattr -n $xbig -v $value $file ||
8572                         error "saving $xbig on $file failed"
8573         else
8574                 setfattr -n $xbig -v $value $file &&
8575                         error "saving $xbig on $file succeeded"
8576                 return 0
8577         fi
8578
8579         local orig=$(get_xattr_value $xbig $file)
8580         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
8581
8582         local xsml=trusted.sml
8583         log "save $xsml on $file"
8584         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
8585
8586         local new=$(get_xattr_value $xbig $file)
8587         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
8588
8589         log "grow $xsml on $file"
8590         setfattr -n $xsml -v "$value" $file ||
8591                 error "growing $xsml on $file failed"
8592
8593         new=$(get_xattr_value $xbig $file)
8594         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
8595         log "$xbig still valid after growing $xsml"
8596
8597         rm -f $file
8598 }
8599
8600 test_102h() { # bug 15777
8601         grow_xattr 1024
8602 }
8603 run_test 102h "grow xattr from inside inode to external block"
8604
8605 test_102ha() {
8606         large_xattr_enabled || skip_env "ea_inode feature disabled"
8607
8608         echo "setting xattr of max xattr size: $(max_xattr_size)"
8609         grow_xattr $(max_xattr_size)
8610
8611         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
8612         echo "This should fail:"
8613         grow_xattr $(($(max_xattr_size) + 10)) 1
8614 }
8615 run_test 102ha "grow xattr from inside inode to external inode"
8616
8617 test_102i() { # bug 17038
8618         [ -z "$(which getfattr 2>/dev/null)" ] &&
8619                 skip "could not find getfattr"
8620
8621         touch $DIR/$tfile
8622         ln -s $DIR/$tfile $DIR/${tfile}link
8623         getfattr -n trusted.lov $DIR/$tfile ||
8624                 error "lgetxattr on $DIR/$tfile failed"
8625         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
8626                 grep -i "no such attr" ||
8627                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
8628         rm -f $DIR/$tfile $DIR/${tfile}link
8629 }
8630 run_test 102i "lgetxattr test on symbolic link ============"
8631
8632 test_102j() {
8633         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8634         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8635
8636         XINC=$(have_xattrs_include)
8637         setup_test102 "$RUNAS"
8638         chown $RUNAS_ID $DIR/$tdir
8639         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
8640         cd $DIR/$tdir/$tdir
8641         compare_stripe_info1 "$RUNAS"
8642 }
8643 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
8644
8645 test_102k() {
8646         [ -z "$(which setfattr 2>/dev/null)" ] &&
8647                 skip "could not find setfattr"
8648
8649         touch $DIR/$tfile
8650         # b22187 just check that does not crash for regular file.
8651         setfattr -n trusted.lov $DIR/$tfile
8652         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
8653         local test_kdir=$DIR/$tdir
8654         test_mkdir $test_kdir
8655         local default_size=$($LFS getstripe -S $test_kdir)
8656         local default_count=$($LFS getstripe -c $test_kdir)
8657         local default_offset=$($LFS getstripe -i $test_kdir)
8658         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
8659                 error 'dir setstripe failed'
8660         setfattr -n trusted.lov $test_kdir
8661         local stripe_size=$($LFS getstripe -S $test_kdir)
8662         local stripe_count=$($LFS getstripe -c $test_kdir)
8663         local stripe_offset=$($LFS getstripe -i $test_kdir)
8664         [ $stripe_size -eq $default_size ] ||
8665                 error "stripe size $stripe_size != $default_size"
8666         [ $stripe_count -eq $default_count ] ||
8667                 error "stripe count $stripe_count != $default_count"
8668         [ $stripe_offset -eq $default_offset ] ||
8669                 error "stripe offset $stripe_offset != $default_offset"
8670         rm -rf $DIR/$tfile $test_kdir
8671 }
8672 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
8673
8674 test_102l() {
8675         [ -z "$(which getfattr 2>/dev/null)" ] &&
8676                 skip "could not find getfattr"
8677
8678         # LU-532 trusted. xattr is invisible to non-root
8679         local testfile=$DIR/$tfile
8680
8681         touch $testfile
8682
8683         echo "listxattr as user..."
8684         chown $RUNAS_ID $testfile
8685         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
8686             grep -q "trusted" &&
8687                 error "$testfile trusted xattrs are user visible"
8688
8689         return 0;
8690 }
8691 run_test 102l "listxattr size test =================================="
8692
8693 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
8694         local path=$DIR/$tfile
8695         touch $path
8696
8697         listxattr_size_check $path || error "listattr_size_check $path failed"
8698 }
8699 run_test 102m "Ensure listxattr fails on small bufffer ========"
8700
8701 cleanup_test102
8702
8703 getxattr() { # getxattr path name
8704         # Return the base64 encoding of the value of xattr name on path.
8705         local path=$1
8706         local name=$2
8707
8708         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
8709         # file: $path
8710         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
8711         #
8712         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
8713
8714         getfattr --absolute-names --encoding=base64 --name=$name $path |
8715                 awk -F= -v name=$name '$1 == name {
8716                         print substr($0, index($0, "=") + 1);
8717         }'
8718 }
8719
8720 test_102n() { # LU-4101 mdt: protect internal xattrs
8721         [ -z "$(which setfattr 2>/dev/null)" ] &&
8722                 skip "could not find setfattr"
8723         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
8724         then
8725                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
8726         fi
8727
8728         local file0=$DIR/$tfile.0
8729         local file1=$DIR/$tfile.1
8730         local xattr0=$TMP/$tfile.0
8731         local xattr1=$TMP/$tfile.1
8732         local namelist="lov lma lmv link fid version som hsm"
8733         local name
8734         local value
8735
8736         rm -rf $file0 $file1 $xattr0 $xattr1
8737         touch $file0 $file1
8738
8739         # Get 'before' xattrs of $file1.
8740         getfattr --absolute-names --dump --match=- $file1 > $xattr0
8741
8742         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
8743                 namelist+=" lfsck_namespace"
8744         for name in $namelist; do
8745                 # Try to copy xattr from $file0 to $file1.
8746                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
8747
8748                 setfattr --name=trusted.$name --value="$value" $file1 ||
8749                         error "setxattr 'trusted.$name' failed"
8750
8751                 # Try to set a garbage xattr.
8752                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
8753
8754                 if [[ x$name == "xlov" ]]; then
8755                         setfattr --name=trusted.lov --value="$value" $file1 &&
8756                         error "setxattr invalid 'trusted.lov' success"
8757                 else
8758                         setfattr --name=trusted.$name --value="$value" $file1 ||
8759                                 error "setxattr invalid 'trusted.$name' failed"
8760                 fi
8761
8762                 # Try to remove the xattr from $file1. We don't care if this
8763                 # appears to succeed or fail, we just don't want there to be
8764                 # any changes or crashes.
8765                 setfattr --remove=$trusted.$name $file1 2> /dev/null
8766         done
8767
8768         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
8769         then
8770                 name="lfsck_ns"
8771                 # Try to copy xattr from $file0 to $file1.
8772                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
8773
8774                 setfattr --name=trusted.$name --value="$value" $file1 ||
8775                         error "setxattr 'trusted.$name' failed"
8776
8777                 # Try to set a garbage xattr.
8778                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
8779
8780                 setfattr --name=trusted.$name --value="$value" $file1 ||
8781                         error "setxattr 'trusted.$name' failed"
8782
8783                 # Try to remove the xattr from $file1. We don't care if this
8784                 # appears to succeed or fail, we just don't want there to be
8785                 # any changes or crashes.
8786                 setfattr --remove=$trusted.$name $file1 2> /dev/null
8787         fi
8788
8789         # Get 'after' xattrs of file1.
8790         getfattr --absolute-names --dump --match=- $file1 > $xattr1
8791
8792         if ! diff $xattr0 $xattr1; then
8793                 error "before and after xattrs of '$file1' differ"
8794         fi
8795
8796         rm -rf $file0 $file1 $xattr0 $xattr1
8797
8798         return 0
8799 }
8800 run_test 102n "silently ignore setxattr on internal trusted xattrs"
8801
8802 test_102p() { # LU-4703 setxattr did not check ownership
8803         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
8804                 skip "MDS needs to be at least 2.5.56"
8805
8806         local testfile=$DIR/$tfile
8807
8808         touch $testfile
8809
8810         echo "setfacl as user..."
8811         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
8812         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
8813
8814         echo "setfattr as user..."
8815         setfacl -m "u:$RUNAS_ID:---" $testfile
8816         $RUNAS setfattr -x system.posix_acl_access $testfile
8817         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
8818 }
8819 run_test 102p "check setxattr(2) correctly fails without permission"
8820
8821 test_102q() {
8822         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
8823                 skip "MDS needs to be at least 2.6.92"
8824
8825         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
8826 }
8827 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
8828
8829 test_102r() {
8830         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
8831                 skip "MDS needs to be at least 2.6.93"
8832
8833         touch $DIR/$tfile || error "touch"
8834         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
8835         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
8836         rm $DIR/$tfile || error "rm"
8837
8838         #normal directory
8839         mkdir -p $DIR/$tdir || error "mkdir"
8840         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
8841         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
8842         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
8843                 error "$testfile error deleting user.author1"
8844         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
8845                 grep "user.$(basename $tdir)" &&
8846                 error "$tdir did not delete user.$(basename $tdir)"
8847         rmdir $DIR/$tdir || error "rmdir"
8848
8849         #striped directory
8850         test_mkdir $DIR/$tdir
8851         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
8852         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
8853         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
8854                 error "$testfile error deleting user.author1"
8855         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
8856                 grep "user.$(basename $tdir)" &&
8857                 error "$tdir did not delete user.$(basename $tdir)"
8858         rmdir $DIR/$tdir || error "rm striped dir"
8859 }
8860 run_test 102r "set EAs with empty values"
8861
8862 test_102s() {
8863         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
8864                 skip "MDS needs to be at least 2.11.52"
8865
8866         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
8867
8868         save_lustre_params client "llite.*.xattr_cache" > $save
8869
8870         for cache in 0 1; do
8871                 lctl set_param llite.*.xattr_cache=$cache
8872
8873                 rm -f $DIR/$tfile
8874                 touch $DIR/$tfile || error "touch"
8875                 for prefix in lustre security system trusted user; do
8876                         # Note getxattr() may fail with 'Operation not
8877                         # supported' or 'No such attribute' depending
8878                         # on prefix and cache.
8879                         getfattr -n $prefix.n102s $DIR/$tfile &&
8880                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
8881                 done
8882         done
8883
8884         restore_lustre_params < $save
8885 }
8886 run_test 102s "getting nonexistent xattrs should fail"
8887
8888 test_102t() {
8889         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
8890                 skip "MDS needs to be at least 2.11.52"
8891
8892         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
8893
8894         save_lustre_params client "llite.*.xattr_cache" > $save
8895
8896         for cache in 0 1; do
8897                 lctl set_param llite.*.xattr_cache=$cache
8898
8899                 for buf_size in 0 256; do
8900                         rm -f $DIR/$tfile
8901                         touch $DIR/$tfile || error "touch"
8902                         setfattr -n user.multiop $DIR/$tfile
8903                         $MULTIOP $DIR/$tfile oa$buf_size ||
8904                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
8905                 done
8906         done
8907
8908         restore_lustre_params < $save
8909 }
8910 run_test 102t "zero length xattr values handled correctly"
8911
8912 run_acl_subtest()
8913 {
8914     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
8915     return $?
8916 }
8917
8918 test_103a() {
8919         [ "$UID" != 0 ] && skip "must run as root"
8920         $GSS && skip_env "could not run under gss"
8921         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
8922                 skip_env "must have acl enabled"
8923         [ -z "$(which setfacl 2>/dev/null)" ] &&
8924                 skip_env "could not find setfacl"
8925         remote_mds_nodsh && skip "remote MDS with nodsh"
8926
8927         gpasswd -a daemon bin                           # LU-5641
8928         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
8929
8930         declare -a identity_old
8931
8932         for num in $(seq $MDSCOUNT); do
8933                 switch_identity $num true || identity_old[$num]=$?
8934         done
8935
8936         SAVE_UMASK=$(umask)
8937         umask 0022
8938         mkdir -p $DIR/$tdir
8939         cd $DIR/$tdir
8940
8941         echo "performing cp ..."
8942         run_acl_subtest cp || error "run_acl_subtest cp failed"
8943         echo "performing getfacl-noacl..."
8944         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
8945         echo "performing misc..."
8946         run_acl_subtest misc || error  "misc test failed"
8947         echo "performing permissions..."
8948         run_acl_subtest permissions || error "permissions failed"
8949         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
8950         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
8951                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
8952                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
8953         then
8954                 echo "performing permissions xattr..."
8955                 run_acl_subtest permissions_xattr ||
8956                         error "permissions_xattr failed"
8957         fi
8958         echo "performing setfacl..."
8959         run_acl_subtest setfacl || error  "setfacl test failed"
8960
8961         # inheritance test got from HP
8962         echo "performing inheritance..."
8963         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
8964         chmod +x make-tree || error "chmod +x failed"
8965         run_acl_subtest inheritance || error "inheritance test failed"
8966         rm -f make-tree
8967
8968         echo "LU-974 ignore umask when acl is enabled..."
8969         run_acl_subtest 974 || error "LU-974 umask test failed"
8970         if [ $MDSCOUNT -ge 2 ]; then
8971                 run_acl_subtest 974_remote ||
8972                         error "LU-974 umask test failed under remote dir"
8973         fi
8974
8975         echo "LU-2561 newly created file is same size as directory..."
8976         if [ "$mds1_FSTYPE" != "zfs" ]; then
8977                 run_acl_subtest 2561 || error "LU-2561 test failed"
8978         else
8979                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
8980         fi
8981
8982         run_acl_subtest 4924 || error "LU-4924 test failed"
8983
8984         cd $SAVE_PWD
8985         umask $SAVE_UMASK
8986
8987         for num in $(seq $MDSCOUNT); do
8988                 if [ "${identity_old[$num]}" = 1 ]; then
8989                         switch_identity $num false || identity_old[$num]=$?
8990                 fi
8991         done
8992 }
8993 run_test 103a "acl test"
8994
8995 test_103b() {
8996         declare -a pids
8997         local U
8998
8999         for U in {0..511}; do
9000                 {
9001                 local O=$(printf "%04o" $U)
9002
9003                 umask $(printf "%04o" $((511 ^ $O)))
9004                 $LFS setstripe -c 1 $DIR/$tfile.s$O
9005                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
9006
9007                 (( $S == ($O & 0666) )) ||
9008                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
9009
9010                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
9011                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
9012                 (( $S == ($O & 0666) )) ||
9013                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
9014
9015                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
9016                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
9017                 (( $S == ($O & 0666) )) ||
9018                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
9019                 rm -f $DIR/$tfile.[smp]$0
9020                 } &
9021                 local pid=$!
9022
9023                 # limit the concurrently running threads to 64. LU-11878
9024                 local idx=$((U % 64))
9025                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
9026                 pids[idx]=$pid
9027         done
9028         wait
9029 }
9030 run_test 103b "umask lfs setstripe"
9031
9032 test_103c() {
9033         mkdir -p $DIR/$tdir
9034         cp -rp $DIR/$tdir $DIR/$tdir.bak
9035
9036         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
9037                 error "$DIR/$tdir shouldn't contain default ACL"
9038         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
9039                 error "$DIR/$tdir.bak shouldn't contain default ACL"
9040         true
9041 }
9042 run_test 103c "'cp -rp' won't set empty acl"
9043
9044 test_104a() {
9045         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9046
9047         touch $DIR/$tfile
9048         lfs df || error "lfs df failed"
9049         lfs df -ih || error "lfs df -ih failed"
9050         lfs df -h $DIR || error "lfs df -h $DIR failed"
9051         lfs df -i $DIR || error "lfs df -i $DIR failed"
9052         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
9053         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
9054
9055         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
9056         lctl --device %$OSC deactivate
9057         lfs df || error "lfs df with deactivated OSC failed"
9058         lctl --device %$OSC activate
9059         # wait the osc back to normal
9060         wait_osc_import_ready client ost
9061
9062         lfs df || error "lfs df with reactivated OSC failed"
9063         rm -f $DIR/$tfile
9064 }
9065 run_test 104a "lfs df [-ih] [path] test ========================="
9066
9067 test_104b() {
9068         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9069         [ $RUNAS_ID -eq $UID ] &&
9070                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9071
9072         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
9073                         grep "Permission denied" | wc -l)))
9074         if [ $denied_cnt -ne 0 ]; then
9075                 error "lfs check servers test failed"
9076         fi
9077 }
9078 run_test 104b "$RUNAS lfs check servers test ===================="
9079
9080 test_105a() {
9081         # doesn't work on 2.4 kernels
9082         touch $DIR/$tfile
9083         if $(flock_is_enabled); then
9084                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
9085         else
9086                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
9087         fi
9088         rm -f $DIR/$tfile
9089 }
9090 run_test 105a "flock when mounted without -o flock test ========"
9091
9092 test_105b() {
9093         touch $DIR/$tfile
9094         if $(flock_is_enabled); then
9095                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
9096         else
9097                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
9098         fi
9099         rm -f $DIR/$tfile
9100 }
9101 run_test 105b "fcntl when mounted without -o flock test ========"
9102
9103 test_105c() {
9104         touch $DIR/$tfile
9105         if $(flock_is_enabled); then
9106                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
9107         else
9108                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
9109         fi
9110         rm -f $DIR/$tfile
9111 }
9112 run_test 105c "lockf when mounted without -o flock test"
9113
9114 test_105d() { # bug 15924
9115         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9116
9117         test_mkdir $DIR/$tdir
9118         flock_is_enabled || skip_env "mount w/o flock enabled"
9119         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
9120         $LCTL set_param fail_loc=0x80000315
9121         flocks_test 2 $DIR/$tdir
9122 }
9123 run_test 105d "flock race (should not freeze) ========"
9124
9125 test_105e() { # bug 22660 && 22040
9126         flock_is_enabled || skip_env "mount w/o flock enabled"
9127
9128         touch $DIR/$tfile
9129         flocks_test 3 $DIR/$tfile
9130 }
9131 run_test 105e "Two conflicting flocks from same process"
9132
9133 test_106() { #bug 10921
9134         test_mkdir $DIR/$tdir
9135         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
9136         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
9137 }
9138 run_test 106 "attempt exec of dir followed by chown of that dir"
9139
9140 test_107() {
9141         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9142
9143         CDIR=`pwd`
9144         local file=core
9145
9146         cd $DIR
9147         rm -f $file
9148
9149         local save_pattern=$(sysctl -n kernel.core_pattern)
9150         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
9151         sysctl -w kernel.core_pattern=$file
9152         sysctl -w kernel.core_uses_pid=0
9153
9154         ulimit -c unlimited
9155         sleep 60 &
9156         SLEEPPID=$!
9157
9158         sleep 1
9159
9160         kill -s 11 $SLEEPPID
9161         wait $SLEEPPID
9162         if [ -e $file ]; then
9163                 size=`stat -c%s $file`
9164                 [ $size -eq 0 ] && error "Fail to create core file $file"
9165         else
9166                 error "Fail to create core file $file"
9167         fi
9168         rm -f $file
9169         sysctl -w kernel.core_pattern=$save_pattern
9170         sysctl -w kernel.core_uses_pid=$save_uses_pid
9171         cd $CDIR
9172 }
9173 run_test 107 "Coredump on SIG"
9174
9175 test_110() {
9176         test_mkdir $DIR/$tdir
9177         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
9178         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
9179                 error "mkdir with 256 char should fail, but did not"
9180         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
9181                 error "create with 255 char failed"
9182         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
9183                 error "create with 256 char should fail, but did not"
9184
9185         ls -l $DIR/$tdir
9186         rm -rf $DIR/$tdir
9187 }
9188 run_test 110 "filename length checking"
9189
9190 #
9191 # Purpose: To verify dynamic thread (OSS) creation.
9192 #
9193 test_115() {
9194         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9195         remote_ost_nodsh && skip "remote OST with nodsh"
9196
9197         # Lustre does not stop service threads once they are started.
9198         # Reset number of running threads to default.
9199         stopall
9200         setupall
9201
9202         local OSTIO_pre
9203         local save_params="$TMP/sanity-$TESTNAME.parameters"
9204
9205         # Get ll_ost_io count before I/O
9206         OSTIO_pre=$(do_facet ost1 \
9207                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
9208         # Exit if lustre is not running (ll_ost_io not running).
9209         [ -z "$OSTIO_pre" ] && error "no OSS threads"
9210
9211         echo "Starting with $OSTIO_pre threads"
9212         local thread_max=$((OSTIO_pre * 2))
9213         local rpc_in_flight=$((thread_max * 2))
9214         # Number of I/O Process proposed to be started.
9215         local nfiles
9216         local facets=$(get_facets OST)
9217
9218         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
9219         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
9220
9221         # Set in_flight to $rpc_in_flight
9222         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
9223                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
9224         nfiles=${rpc_in_flight}
9225         # Set ost thread_max to $thread_max
9226         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
9227
9228         # 5 Minutes should be sufficient for max number of OSS
9229         # threads(thread_max) to be created.
9230         local timeout=300
9231
9232         # Start I/O.
9233         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
9234         test_mkdir $DIR/$tdir
9235         for i in $(seq $nfiles); do
9236                 local file=$DIR/$tdir/${tfile}-$i
9237                 $LFS setstripe -c -1 -i 0 $file
9238                 ($WTL $file $timeout)&
9239         done
9240
9241         # I/O Started - Wait for thread_started to reach thread_max or report
9242         # error if thread_started is more than thread_max.
9243         echo "Waiting for thread_started to reach thread_max"
9244         local thread_started=0
9245         local end_time=$((SECONDS + timeout))
9246
9247         while [ $SECONDS -le $end_time ] ; do
9248                 echo -n "."
9249                 # Get ost i/o thread_started count.
9250                 thread_started=$(do_facet ost1 \
9251                         "$LCTL get_param \
9252                         ost.OSS.ost_io.threads_started | cut -d= -f2")
9253                 # Break out if thread_started is equal/greater than thread_max
9254                 if [[ $thread_started -ge $thread_max ]]; then
9255                         echo ll_ost_io thread_started $thread_started, \
9256                                 equal/greater than thread_max $thread_max
9257                         break
9258                 fi
9259                 sleep 1
9260         done
9261
9262         # Cleanup - We have the numbers, Kill i/o jobs if running.
9263         jobcount=($(jobs -p))
9264         for i in $(seq 0 $((${#jobcount[@]}-1)))
9265         do
9266                 kill -9 ${jobcount[$i]}
9267                 if [ $? -ne 0 ] ; then
9268                         echo Warning: \
9269                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
9270                 fi
9271         done
9272
9273         # Cleanup files left by WTL binary.
9274         for i in $(seq $nfiles); do
9275                 local file=$DIR/$tdir/${tfile}-$i
9276                 rm -rf $file
9277                 if [ $? -ne 0 ] ; then
9278                         echo "Warning: Failed to delete file $file"
9279                 fi
9280         done
9281
9282         restore_lustre_params <$save_params
9283         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
9284
9285         # Error out if no new thread has started or Thread started is greater
9286         # than thread max.
9287         if [[ $thread_started -le $OSTIO_pre ||
9288                         $thread_started -gt $thread_max ]]; then
9289                 error "ll_ost_io: thread_started $thread_started" \
9290                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
9291                       "No new thread started or thread started greater " \
9292                       "than thread_max."
9293         fi
9294 }
9295 run_test 115 "verify dynamic thread creation===================="
9296
9297 free_min_max () {
9298         wait_delete_completed
9299         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
9300         echo "OST kbytes available: ${AVAIL[@]}"
9301         MAXV=${AVAIL[0]}
9302         MAXI=0
9303         MINV=${AVAIL[0]}
9304         MINI=0
9305         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
9306                 #echo OST $i: ${AVAIL[i]}kb
9307                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
9308                         MAXV=${AVAIL[i]}
9309                         MAXI=$i
9310                 fi
9311                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
9312                         MINV=${AVAIL[i]}
9313                         MINI=$i
9314                 fi
9315         done
9316         echo "Min free space: OST $MINI: $MINV"
9317         echo "Max free space: OST $MAXI: $MAXV"
9318 }
9319
9320 test_116a() { # was previously test_116()
9321         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9322         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9323         remote_mds_nodsh && skip "remote MDS with nodsh"
9324
9325         echo -n "Free space priority "
9326         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
9327                 head -n1
9328         declare -a AVAIL
9329         free_min_max
9330
9331         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
9332         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
9333         trap simple_cleanup_common EXIT
9334
9335         # Check if we need to generate uneven OSTs
9336         test_mkdir -p $DIR/$tdir/OST${MINI}
9337         local FILL=$((MINV / 4))
9338         local DIFF=$((MAXV - MINV))
9339         local DIFF2=$((DIFF * 100 / MINV))
9340
9341         local threshold=$(do_facet $SINGLEMDS \
9342                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
9343         threshold=${threshold%%%}
9344         echo -n "Check for uneven OSTs: "
9345         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
9346
9347         if [[ $DIFF2 -gt $threshold ]]; then
9348                 echo "ok"
9349                 echo "Don't need to fill OST$MINI"
9350         else
9351                 # generate uneven OSTs. Write 2% over the QOS threshold value
9352                 echo "no"
9353                 DIFF=$((threshold - DIFF2 + 2))
9354                 DIFF2=$((MINV * DIFF / 100))
9355                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
9356                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
9357                         error "setstripe failed"
9358                 DIFF=$((DIFF2 / 2048))
9359                 i=0
9360                 while [ $i -lt $DIFF ]; do
9361                         i=$((i + 1))
9362                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
9363                                 bs=2M count=1 2>/dev/null
9364                         echo -n .
9365                 done
9366                 echo .
9367                 sync
9368                 sleep_maxage
9369                 free_min_max
9370         fi
9371
9372         DIFF=$((MAXV - MINV))
9373         DIFF2=$((DIFF * 100 / MINV))
9374         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
9375         if [ $DIFF2 -gt $threshold ]; then
9376                 echo "ok"
9377         else
9378                 echo "failed - QOS mode won't be used"
9379                 simple_cleanup_common
9380                 skip "QOS imbalance criteria not met"
9381         fi
9382
9383         MINI1=$MINI
9384         MINV1=$MINV
9385         MAXI1=$MAXI
9386         MAXV1=$MAXV
9387
9388         # now fill using QOS
9389         $LFS setstripe -c 1 $DIR/$tdir
9390         FILL=$((FILL / 200))
9391         if [ $FILL -gt 600 ]; then
9392                 FILL=600
9393         fi
9394         echo "writing $FILL files to QOS-assigned OSTs"
9395         i=0
9396         while [ $i -lt $FILL ]; do
9397                 i=$((i + 1))
9398                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
9399                         count=1 2>/dev/null
9400                 echo -n .
9401         done
9402         echo "wrote $i 200k files"
9403         sync
9404         sleep_maxage
9405
9406         echo "Note: free space may not be updated, so measurements might be off"
9407         free_min_max
9408         DIFF2=$((MAXV - MINV))
9409         echo "free space delta: orig $DIFF final $DIFF2"
9410         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
9411         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
9412         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
9413         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
9414         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
9415         if [[ $DIFF -gt 0 ]]; then
9416                 FILL=$((DIFF2 * 100 / DIFF - 100))
9417                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
9418         fi
9419
9420         # Figure out which files were written where
9421         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
9422                awk '/'$MINI1': / {print $2; exit}')
9423         echo $UUID
9424         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
9425         echo "$MINC files created on smaller OST $MINI1"
9426         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
9427                awk '/'$MAXI1': / {print $2; exit}')
9428         echo $UUID
9429         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
9430         echo "$MAXC files created on larger OST $MAXI1"
9431         if [[ $MINC -gt 0 ]]; then
9432                 FILL=$((MAXC * 100 / MINC - 100))
9433                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
9434         fi
9435         [[ $MAXC -gt $MINC ]] ||
9436                 error_ignore LU-9 "stripe QOS didn't balance free space"
9437         simple_cleanup_common
9438 }
9439 run_test 116a "stripe QOS: free space balance ==================="
9440
9441 test_116b() { # LU-2093
9442         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9443         remote_mds_nodsh && skip "remote MDS with nodsh"
9444
9445 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
9446         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
9447                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
9448         [ -z "$old_rr" ] && skip "no QOS"
9449         do_facet $SINGLEMDS lctl set_param \
9450                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
9451         mkdir -p $DIR/$tdir
9452         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
9453         createmany -o $DIR/$tdir/f- 20 || error "can't create"
9454         do_facet $SINGLEMDS lctl set_param fail_loc=0
9455         rm -rf $DIR/$tdir
9456         do_facet $SINGLEMDS lctl set_param \
9457                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
9458 }
9459 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
9460
9461 test_117() # bug 10891
9462 {
9463         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9464
9465         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
9466         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
9467         lctl set_param fail_loc=0x21e
9468         > $DIR/$tfile || error "truncate failed"
9469         lctl set_param fail_loc=0
9470         echo "Truncate succeeded."
9471         rm -f $DIR/$tfile
9472 }
9473 run_test 117 "verify osd extend =========="
9474
9475 NO_SLOW_RESENDCOUNT=4
9476 export OLD_RESENDCOUNT=""
9477 set_resend_count () {
9478         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
9479         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
9480         lctl set_param -n $PROC_RESENDCOUNT $1
9481         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
9482 }
9483
9484 # for reduce test_118* time (b=14842)
9485 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
9486
9487 # Reset async IO behavior after error case
9488 reset_async() {
9489         FILE=$DIR/reset_async
9490
9491         # Ensure all OSCs are cleared
9492         $LFS setstripe -c -1 $FILE
9493         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
9494         sync
9495         rm $FILE
9496 }
9497
9498 test_118a() #bug 11710
9499 {
9500         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9501
9502         reset_async
9503
9504         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9505         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9506         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9507
9508         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9509                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9510                 return 1;
9511         fi
9512         rm -f $DIR/$tfile
9513 }
9514 run_test 118a "verify O_SYNC works =========="
9515
9516 test_118b()
9517 {
9518         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9519         remote_ost_nodsh && skip "remote OST with nodsh"
9520
9521         reset_async
9522
9523         #define OBD_FAIL_SRV_ENOENT 0x217
9524         set_nodes_failloc "$(osts_nodes)" 0x217
9525         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9526         RC=$?
9527         set_nodes_failloc "$(osts_nodes)" 0
9528         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9529         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9530                     grep -c writeback)
9531
9532         if [[ $RC -eq 0 ]]; then
9533                 error "Must return error due to dropped pages, rc=$RC"
9534                 return 1;
9535         fi
9536
9537         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9538                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9539                 return 1;
9540         fi
9541
9542         echo "Dirty pages not leaked on ENOENT"
9543
9544         # Due to the above error the OSC will issue all RPCs syncronously
9545         # until a subsequent RPC completes successfully without error.
9546         $MULTIOP $DIR/$tfile Ow4096yc
9547         rm -f $DIR/$tfile
9548
9549         return 0
9550 }
9551 run_test 118b "Reclaim dirty pages on fatal error =========="
9552
9553 test_118c()
9554 {
9555         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9556
9557         # for 118c, restore the original resend count, LU-1940
9558         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
9559                                 set_resend_count $OLD_RESENDCOUNT
9560         remote_ost_nodsh && skip "remote OST with nodsh"
9561
9562         reset_async
9563
9564         #define OBD_FAIL_OST_EROFS               0x216
9565         set_nodes_failloc "$(osts_nodes)" 0x216
9566
9567         # multiop should block due to fsync until pages are written
9568         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9569         MULTIPID=$!
9570         sleep 1
9571
9572         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
9573                 error "Multiop failed to block on fsync, pid=$MULTIPID"
9574         fi
9575
9576         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9577                     grep -c writeback)
9578         if [[ $WRITEBACK -eq 0 ]]; then
9579                 error "No page in writeback, writeback=$WRITEBACK"
9580         fi
9581
9582         set_nodes_failloc "$(osts_nodes)" 0
9583         wait $MULTIPID
9584         RC=$?
9585         if [[ $RC -ne 0 ]]; then
9586                 error "Multiop fsync failed, rc=$RC"
9587         fi
9588
9589         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9590         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9591                     grep -c writeback)
9592         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9593                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9594         fi
9595
9596         rm -f $DIR/$tfile
9597         echo "Dirty pages flushed via fsync on EROFS"
9598         return 0
9599 }
9600 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
9601
9602 # continue to use small resend count to reduce test_118* time (b=14842)
9603 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
9604
9605 test_118d()
9606 {
9607         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9608         remote_ost_nodsh && skip "remote OST with nodsh"
9609
9610         reset_async
9611
9612         #define OBD_FAIL_OST_BRW_PAUSE_BULK
9613         set_nodes_failloc "$(osts_nodes)" 0x214
9614         # multiop should block due to fsync until pages are written
9615         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9616         MULTIPID=$!
9617         sleep 1
9618
9619         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
9620                 error "Multiop failed to block on fsync, pid=$MULTIPID"
9621         fi
9622
9623         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9624                     grep -c writeback)
9625         if [[ $WRITEBACK -eq 0 ]]; then
9626                 error "No page in writeback, writeback=$WRITEBACK"
9627         fi
9628
9629         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
9630         set_nodes_failloc "$(osts_nodes)" 0
9631
9632         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9633         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9634                     grep -c writeback)
9635         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9636                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9637         fi
9638
9639         rm -f $DIR/$tfile
9640         echo "Dirty pages gaurenteed flushed via fsync"
9641         return 0
9642 }
9643 run_test 118d "Fsync validation inject a delay of the bulk =========="
9644
9645 test_118f() {
9646         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9647
9648         reset_async
9649
9650         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
9651         lctl set_param fail_loc=0x8000040a
9652
9653         # Should simulate EINVAL error which is fatal
9654         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9655         RC=$?
9656         if [[ $RC -eq 0 ]]; then
9657                 error "Must return error due to dropped pages, rc=$RC"
9658         fi
9659
9660         lctl set_param fail_loc=0x0
9661
9662         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9663         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9664         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9665                     grep -c writeback)
9666         if [[ $LOCKED -ne 0 ]]; then
9667                 error "Locked pages remain in cache, locked=$LOCKED"
9668         fi
9669
9670         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9671                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9672         fi
9673
9674         rm -f $DIR/$tfile
9675         echo "No pages locked after fsync"
9676
9677         reset_async
9678         return 0
9679 }
9680 run_test 118f "Simulate unrecoverable OSC side error =========="
9681
9682 test_118g() {
9683         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9684
9685         reset_async
9686
9687         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
9688         lctl set_param fail_loc=0x406
9689
9690         # simulate local -ENOMEM
9691         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9692         RC=$?
9693
9694         lctl set_param fail_loc=0
9695         if [[ $RC -eq 0 ]]; then
9696                 error "Must return error due to dropped pages, rc=$RC"
9697         fi
9698
9699         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9700         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9701         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9702                         grep -c writeback)
9703         if [[ $LOCKED -ne 0 ]]; then
9704                 error "Locked pages remain in cache, locked=$LOCKED"
9705         fi
9706
9707         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9708                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9709         fi
9710
9711         rm -f $DIR/$tfile
9712         echo "No pages locked after fsync"
9713
9714         reset_async
9715         return 0
9716 }
9717 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
9718
9719 test_118h() {
9720         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9721         remote_ost_nodsh && skip "remote OST with nodsh"
9722
9723         reset_async
9724
9725         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9726         set_nodes_failloc "$(osts_nodes)" 0x20e
9727         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
9728         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9729         RC=$?
9730
9731         set_nodes_failloc "$(osts_nodes)" 0
9732         if [[ $RC -eq 0 ]]; then
9733                 error "Must return error due to dropped pages, rc=$RC"
9734         fi
9735
9736         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9737         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9738         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9739                     grep -c writeback)
9740         if [[ $LOCKED -ne 0 ]]; then
9741                 error "Locked pages remain in cache, locked=$LOCKED"
9742         fi
9743
9744         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9745                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9746         fi
9747
9748         rm -f $DIR/$tfile
9749         echo "No pages locked after fsync"
9750
9751         return 0
9752 }
9753 run_test 118h "Verify timeout in handling recoverables errors  =========="
9754
9755 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
9756
9757 test_118i() {
9758         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9759         remote_ost_nodsh && skip "remote OST with nodsh"
9760
9761         reset_async
9762
9763         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9764         set_nodes_failloc "$(osts_nodes)" 0x20e
9765
9766         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
9767         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9768         PID=$!
9769         sleep 5
9770         set_nodes_failloc "$(osts_nodes)" 0
9771
9772         wait $PID
9773         RC=$?
9774         if [[ $RC -ne 0 ]]; then
9775                 error "got error, but should be not, rc=$RC"
9776         fi
9777
9778         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9779         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9780         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9781         if [[ $LOCKED -ne 0 ]]; then
9782                 error "Locked pages remain in cache, locked=$LOCKED"
9783         fi
9784
9785         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9786                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9787         fi
9788
9789         rm -f $DIR/$tfile
9790         echo "No pages locked after fsync"
9791
9792         return 0
9793 }
9794 run_test 118i "Fix error before timeout in recoverable error  =========="
9795
9796 [ "$SLOW" = "no" ] && set_resend_count 4
9797
9798 test_118j() {
9799         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9800         remote_ost_nodsh && skip "remote OST with nodsh"
9801
9802         reset_async
9803
9804         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
9805         set_nodes_failloc "$(osts_nodes)" 0x220
9806
9807         # return -EIO from OST
9808         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9809         RC=$?
9810         set_nodes_failloc "$(osts_nodes)" 0x0
9811         if [[ $RC -eq 0 ]]; then
9812                 error "Must return error due to dropped pages, rc=$RC"
9813         fi
9814
9815         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9816         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9817         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9818         if [[ $LOCKED -ne 0 ]]; then
9819                 error "Locked pages remain in cache, locked=$LOCKED"
9820         fi
9821
9822         # in recoverable error on OST we want resend and stay until it finished
9823         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9824                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9825         fi
9826
9827         rm -f $DIR/$tfile
9828         echo "No pages locked after fsync"
9829
9830         return 0
9831 }
9832 run_test 118j "Simulate unrecoverable OST side error =========="
9833
9834 test_118k()
9835 {
9836         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9837         remote_ost_nodsh && skip "remote OSTs with nodsh"
9838
9839         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9840         set_nodes_failloc "$(osts_nodes)" 0x20e
9841         test_mkdir $DIR/$tdir
9842
9843         for ((i=0;i<10;i++)); do
9844                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
9845                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
9846                 SLEEPPID=$!
9847                 sleep 0.500s
9848                 kill $SLEEPPID
9849                 wait $SLEEPPID
9850         done
9851
9852         set_nodes_failloc "$(osts_nodes)" 0
9853         rm -rf $DIR/$tdir
9854 }
9855 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
9856
9857 test_118l() # LU-646
9858 {
9859         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9860
9861         test_mkdir $DIR/$tdir
9862         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
9863         rm -rf $DIR/$tdir
9864 }
9865 run_test 118l "fsync dir"
9866
9867 test_118m() # LU-3066
9868 {
9869         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9870
9871         test_mkdir $DIR/$tdir
9872         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
9873         rm -rf $DIR/$tdir
9874 }
9875 run_test 118m "fdatasync dir ========="
9876
9877 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
9878
9879 test_118n()
9880 {
9881         local begin
9882         local end
9883
9884         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9885         remote_ost_nodsh && skip "remote OSTs with nodsh"
9886
9887         # Sleep to avoid a cached response.
9888         #define OBD_STATFS_CACHE_SECONDS 1
9889         sleep 2
9890
9891         # Inject a 10 second delay in the OST_STATFS handler.
9892         #define OBD_FAIL_OST_STATFS_DELAY 0x242
9893         set_nodes_failloc "$(osts_nodes)" 0x242
9894
9895         begin=$SECONDS
9896         stat --file-system $MOUNT > /dev/null
9897         end=$SECONDS
9898
9899         set_nodes_failloc "$(osts_nodes)" 0
9900
9901         if ((end - begin > 20)); then
9902             error "statfs took $((end - begin)) seconds, expected 10"
9903         fi
9904 }
9905 run_test 118n "statfs() sends OST_STATFS requests in parallel"
9906
9907 test_119a() # bug 11737
9908 {
9909         BSIZE=$((512 * 1024))
9910         directio write $DIR/$tfile 0 1 $BSIZE
9911         # We ask to read two blocks, which is more than a file size.
9912         # directio will indicate an error when requested and actual
9913         # sizes aren't equeal (a normal situation in this case) and
9914         # print actual read amount.
9915         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
9916         if [ "$NOB" != "$BSIZE" ]; then
9917                 error "read $NOB bytes instead of $BSIZE"
9918         fi
9919         rm -f $DIR/$tfile
9920 }
9921 run_test 119a "Short directIO read must return actual read amount"
9922
9923 test_119b() # bug 11737
9924 {
9925         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9926
9927         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
9928         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
9929         sync
9930         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
9931                 error "direct read failed"
9932         rm -f $DIR/$tfile
9933 }
9934 run_test 119b "Sparse directIO read must return actual read amount"
9935
9936 test_119c() # bug 13099
9937 {
9938         BSIZE=1048576
9939         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
9940         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
9941         rm -f $DIR/$tfile
9942 }
9943 run_test 119c "Testing for direct read hitting hole"
9944
9945 test_119d() # bug 15950
9946 {
9947         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9948
9949         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
9950         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
9951         BSIZE=1048576
9952         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
9953         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
9954         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
9955         lctl set_param fail_loc=0x40d
9956         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
9957         pid_dio=$!
9958         sleep 1
9959         cat $DIR/$tfile > /dev/null &
9960         lctl set_param fail_loc=0
9961         pid_reads=$!
9962         wait $pid_dio
9963         log "the DIO writes have completed, now wait for the reads (should not block very long)"
9964         sleep 2
9965         [ -n "`ps h -p $pid_reads -o comm`" ] && \
9966         error "the read rpcs have not completed in 2s"
9967         rm -f $DIR/$tfile
9968         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
9969 }
9970 run_test 119d "The DIO path should try to send a new rpc once one is completed"
9971
9972 test_120a() {
9973         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9974         remote_mds_nodsh && skip "remote MDS with nodsh"
9975         test_mkdir -i0 -c1 $DIR/$tdir
9976         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9977                 skip_env "no early lock cancel on server"
9978
9979         lru_resize_disable mdc
9980         lru_resize_disable osc
9981         cancel_lru_locks mdc
9982         # asynchronous object destroy at MDT could cause bl ast to client
9983         cancel_lru_locks osc
9984
9985         stat $DIR/$tdir > /dev/null
9986         can1=$(do_facet mds1 \
9987                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9988                awk '/ldlm_cancel/ {print $2}')
9989         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9990                awk '/ldlm_bl_callback/ {print $2}')
9991         test_mkdir -i0 -c1 $DIR/$tdir/d1
9992         can2=$(do_facet mds1 \
9993                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9994                awk '/ldlm_cancel/ {print $2}')
9995         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9996                awk '/ldlm_bl_callback/ {print $2}')
9997         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9998         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9999         lru_resize_enable mdc
10000         lru_resize_enable osc
10001 }
10002 run_test 120a "Early Lock Cancel: mkdir test"
10003
10004 test_120b() {
10005         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10006         remote_mds_nodsh && skip "remote MDS with nodsh"
10007         test_mkdir $DIR/$tdir
10008         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10009                 skip_env "no early lock cancel on server"
10010
10011         lru_resize_disable mdc
10012         lru_resize_disable osc
10013         cancel_lru_locks mdc
10014         stat $DIR/$tdir > /dev/null
10015         can1=$(do_facet $SINGLEMDS \
10016                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10017                awk '/ldlm_cancel/ {print $2}')
10018         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10019                awk '/ldlm_bl_callback/ {print $2}')
10020         touch $DIR/$tdir/f1
10021         can2=$(do_facet $SINGLEMDS \
10022                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10023                awk '/ldlm_cancel/ {print $2}')
10024         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10025                awk '/ldlm_bl_callback/ {print $2}')
10026         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10027         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10028         lru_resize_enable mdc
10029         lru_resize_enable osc
10030 }
10031 run_test 120b "Early Lock Cancel: create test"
10032
10033 test_120c() {
10034         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10035         remote_mds_nodsh && skip "remote MDS with nodsh"
10036         test_mkdir -i0 -c1 $DIR/$tdir
10037         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10038                 skip "no early lock cancel on server"
10039
10040         lru_resize_disable mdc
10041         lru_resize_disable osc
10042         test_mkdir -i0 -c1 $DIR/$tdir/d1
10043         test_mkdir -i0 -c1 $DIR/$tdir/d2
10044         touch $DIR/$tdir/d1/f1
10045         cancel_lru_locks mdc
10046         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
10047         can1=$(do_facet mds1 \
10048                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10049                awk '/ldlm_cancel/ {print $2}')
10050         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10051                awk '/ldlm_bl_callback/ {print $2}')
10052         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10053         can2=$(do_facet mds1 \
10054                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10055                awk '/ldlm_cancel/ {print $2}')
10056         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10057                awk '/ldlm_bl_callback/ {print $2}')
10058         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10059         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10060         lru_resize_enable mdc
10061         lru_resize_enable osc
10062 }
10063 run_test 120c "Early Lock Cancel: link test"
10064
10065 test_120d() {
10066         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10067         remote_mds_nodsh && skip "remote MDS with nodsh"
10068         test_mkdir -i0 -c1 $DIR/$tdir
10069         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10070                 skip_env "no early lock cancel on server"
10071
10072         lru_resize_disable mdc
10073         lru_resize_disable osc
10074         touch $DIR/$tdir
10075         cancel_lru_locks mdc
10076         stat $DIR/$tdir > /dev/null
10077         can1=$(do_facet mds1 \
10078                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10079                awk '/ldlm_cancel/ {print $2}')
10080         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10081                awk '/ldlm_bl_callback/ {print $2}')
10082         chmod a+x $DIR/$tdir
10083         can2=$(do_facet mds1 \
10084                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10085                awk '/ldlm_cancel/ {print $2}')
10086         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10087                awk '/ldlm_bl_callback/ {print $2}')
10088         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10089         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10090         lru_resize_enable mdc
10091         lru_resize_enable osc
10092 }
10093 run_test 120d "Early Lock Cancel: setattr test"
10094
10095 test_120e() {
10096         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10097         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10098                 skip_env "no early lock cancel on server"
10099         remote_mds_nodsh && skip "remote MDS with nodsh"
10100
10101         local dlmtrace_set=false
10102
10103         test_mkdir -i0 -c1 $DIR/$tdir
10104         lru_resize_disable mdc
10105         lru_resize_disable osc
10106         ! $LCTL get_param debug | grep -q dlmtrace &&
10107                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
10108         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
10109         cancel_lru_locks mdc
10110         cancel_lru_locks osc
10111         dd if=$DIR/$tdir/f1 of=/dev/null
10112         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
10113         # XXX client can not do early lock cancel of OST lock
10114         # during unlink (LU-4206), so cancel osc lock now.
10115         sleep 2
10116         cancel_lru_locks osc
10117         can1=$(do_facet mds1 \
10118                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10119                awk '/ldlm_cancel/ {print $2}')
10120         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10121                awk '/ldlm_bl_callback/ {print $2}')
10122         unlink $DIR/$tdir/f1
10123         sleep 5
10124         can2=$(do_facet mds1 \
10125                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10126                awk '/ldlm_cancel/ {print $2}')
10127         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10128                awk '/ldlm_bl_callback/ {print $2}')
10129         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
10130                 $LCTL dk $TMP/cancel.debug.txt
10131         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
10132                 $LCTL dk $TMP/blocking.debug.txt
10133         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
10134         lru_resize_enable mdc
10135         lru_resize_enable osc
10136 }
10137 run_test 120e "Early Lock Cancel: unlink test"
10138
10139 test_120f() {
10140         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10141         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10142                 skip_env "no early lock cancel on server"
10143         remote_mds_nodsh && skip "remote MDS with nodsh"
10144
10145         test_mkdir -i0 -c1 $DIR/$tdir
10146         lru_resize_disable mdc
10147         lru_resize_disable osc
10148         test_mkdir -i0 -c1 $DIR/$tdir/d1
10149         test_mkdir -i0 -c1 $DIR/$tdir/d2
10150         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
10151         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
10152         cancel_lru_locks mdc
10153         cancel_lru_locks osc
10154         dd if=$DIR/$tdir/d1/f1 of=/dev/null
10155         dd if=$DIR/$tdir/d2/f2 of=/dev/null
10156         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
10157         # XXX client can not do early lock cancel of OST lock
10158         # during rename (LU-4206), so cancel osc lock now.
10159         sleep 2
10160         cancel_lru_locks osc
10161         can1=$(do_facet mds1 \
10162                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10163                awk '/ldlm_cancel/ {print $2}')
10164         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10165                awk '/ldlm_bl_callback/ {print $2}')
10166         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10167         sleep 5
10168         can2=$(do_facet mds1 \
10169                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10170                awk '/ldlm_cancel/ {print $2}')
10171         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10172                awk '/ldlm_bl_callback/ {print $2}')
10173         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10174         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10175         lru_resize_enable mdc
10176         lru_resize_enable osc
10177 }
10178 run_test 120f "Early Lock Cancel: rename test"
10179
10180 test_120g() {
10181         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10182         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10183                 skip_env "no early lock cancel on server"
10184         remote_mds_nodsh && skip "remote MDS with nodsh"
10185
10186         lru_resize_disable mdc
10187         lru_resize_disable osc
10188         count=10000
10189         echo create $count files
10190         test_mkdir $DIR/$tdir
10191         cancel_lru_locks mdc
10192         cancel_lru_locks osc
10193         t0=$(date +%s)
10194
10195         can0=$(do_facet $SINGLEMDS \
10196                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10197                awk '/ldlm_cancel/ {print $2}')
10198         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10199                awk '/ldlm_bl_callback/ {print $2}')
10200         createmany -o $DIR/$tdir/f $count
10201         sync
10202         can1=$(do_facet $SINGLEMDS \
10203                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10204                awk '/ldlm_cancel/ {print $2}')
10205         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10206                awk '/ldlm_bl_callback/ {print $2}')
10207         t1=$(date +%s)
10208         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
10209         echo rm $count files
10210         rm -r $DIR/$tdir
10211         sync
10212         can2=$(do_facet $SINGLEMDS \
10213                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10214                awk '/ldlm_cancel/ {print $2}')
10215         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10216                awk '/ldlm_bl_callback/ {print $2}')
10217         t2=$(date +%s)
10218         echo total: $count removes in $((t2-t1))
10219         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
10220         sleep 2
10221         # wait for commitment of removal
10222         lru_resize_enable mdc
10223         lru_resize_enable osc
10224 }
10225 run_test 120g "Early Lock Cancel: performance test"
10226
10227 test_121() { #bug #10589
10228         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10229
10230         rm -rf $DIR/$tfile
10231         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
10232 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
10233         lctl set_param fail_loc=0x310
10234         cancel_lru_locks osc > /dev/null
10235         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
10236         lctl set_param fail_loc=0
10237         [[ $reads -eq $writes ]] ||
10238                 error "read $reads blocks, must be $writes blocks"
10239 }
10240 run_test 121 "read cancel race ========="
10241
10242 test_123a() { # was test 123, statahead(bug 11401)
10243         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10244
10245         SLOWOK=0
10246         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
10247                 log "testing UP system. Performance may be lower than expected."
10248                 SLOWOK=1
10249         fi
10250
10251         rm -rf $DIR/$tdir
10252         test_mkdir $DIR/$tdir
10253         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
10254         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
10255         MULT=10
10256         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
10257                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
10258
10259                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
10260                 lctl set_param -n llite.*.statahead_max 0
10261                 lctl get_param llite.*.statahead_max
10262                 cancel_lru_locks mdc
10263                 cancel_lru_locks osc
10264                 stime=`date +%s`
10265                 time ls -l $DIR/$tdir | wc -l
10266                 etime=`date +%s`
10267                 delta=$((etime - stime))
10268                 log "ls $i files without statahead: $delta sec"
10269                 lctl set_param llite.*.statahead_max=$max
10270
10271                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
10272                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
10273                 cancel_lru_locks mdc
10274                 cancel_lru_locks osc
10275                 stime=`date +%s`
10276                 time ls -l $DIR/$tdir | wc -l
10277                 etime=`date +%s`
10278                 delta_sa=$((etime - stime))
10279                 log "ls $i files with statahead: $delta_sa sec"
10280                 lctl get_param -n llite.*.statahead_stats
10281                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
10282
10283                 [[ $swrong -lt $ewrong ]] &&
10284                         log "statahead was stopped, maybe too many locks held!"
10285                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
10286
10287                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
10288                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
10289                     lctl set_param -n llite.*.statahead_max 0
10290                     lctl get_param llite.*.statahead_max
10291                     cancel_lru_locks mdc
10292                     cancel_lru_locks osc
10293                     stime=`date +%s`
10294                     time ls -l $DIR/$tdir | wc -l
10295                     etime=`date +%s`
10296                     delta=$((etime - stime))
10297                     log "ls $i files again without statahead: $delta sec"
10298                     lctl set_param llite.*.statahead_max=$max
10299                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
10300                         if [  $SLOWOK -eq 0 ]; then
10301                                 error "ls $i files is slower with statahead!"
10302                         else
10303                                 log "ls $i files is slower with statahead!"
10304                         fi
10305                         break
10306                     fi
10307                 fi
10308
10309                 [ $delta -gt 20 ] && break
10310                 [ $delta -gt 8 ] && MULT=$((50 / delta))
10311                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
10312         done
10313         log "ls done"
10314
10315         stime=`date +%s`
10316         rm -r $DIR/$tdir
10317         sync
10318         etime=`date +%s`
10319         delta=$((etime - stime))
10320         log "rm -r $DIR/$tdir/: $delta seconds"
10321         log "rm done"
10322         lctl get_param -n llite.*.statahead_stats
10323 }
10324 run_test 123a "verify statahead work"
10325
10326 test_123b () { # statahead(bug 15027)
10327         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10328
10329         test_mkdir $DIR/$tdir
10330         createmany -o $DIR/$tdir/$tfile-%d 1000
10331
10332         cancel_lru_locks mdc
10333         cancel_lru_locks osc
10334
10335 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
10336         lctl set_param fail_loc=0x80000803
10337         ls -lR $DIR/$tdir > /dev/null
10338         log "ls done"
10339         lctl set_param fail_loc=0x0
10340         lctl get_param -n llite.*.statahead_stats
10341         rm -r $DIR/$tdir
10342         sync
10343
10344 }
10345 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
10346
10347 test_124a() {
10348         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10349         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10350                 skip_env "no lru resize on server"
10351
10352         local NR=2000
10353
10354         test_mkdir $DIR/$tdir
10355
10356         log "create $NR files at $DIR/$tdir"
10357         createmany -o $DIR/$tdir/f $NR ||
10358                 error "failed to create $NR files in $DIR/$tdir"
10359
10360         cancel_lru_locks mdc
10361         ls -l $DIR/$tdir > /dev/null
10362
10363         local NSDIR=""
10364         local LRU_SIZE=0
10365         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
10366                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
10367                 LRU_SIZE=$($LCTL get_param -n $PARAM)
10368                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
10369                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
10370                         log "NSDIR=$NSDIR"
10371                         log "NS=$(basename $NSDIR)"
10372                         break
10373                 fi
10374         done
10375
10376         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
10377                 skip "Not enough cached locks created!"
10378         fi
10379         log "LRU=$LRU_SIZE"
10380
10381         local SLEEP=30
10382
10383         # We know that lru resize allows one client to hold $LIMIT locks
10384         # for 10h. After that locks begin to be killed by client.
10385         local MAX_HRS=10
10386         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
10387         log "LIMIT=$LIMIT"
10388         if [ $LIMIT -lt $LRU_SIZE ]; then
10389                 skip "Limit is too small $LIMIT"
10390         fi
10391
10392         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
10393         # killing locks. Some time was spent for creating locks. This means
10394         # that up to the moment of sleep finish we must have killed some of
10395         # them (10-100 locks). This depends on how fast ther were created.
10396         # Many of them were touched in almost the same moment and thus will
10397         # be killed in groups.
10398         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
10399
10400         # Use $LRU_SIZE_B here to take into account real number of locks
10401         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
10402         local LRU_SIZE_B=$LRU_SIZE
10403         log "LVF=$LVF"
10404         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
10405         log "OLD_LVF=$OLD_LVF"
10406         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
10407
10408         # Let's make sure that we really have some margin. Client checks
10409         # cached locks every 10 sec.
10410         SLEEP=$((SLEEP+20))
10411         log "Sleep ${SLEEP} sec"
10412         local SEC=0
10413         while ((SEC<$SLEEP)); do
10414                 echo -n "..."
10415                 sleep 5
10416                 SEC=$((SEC+5))
10417                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
10418                 echo -n "$LRU_SIZE"
10419         done
10420         echo ""
10421         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
10422         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
10423
10424         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
10425                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
10426                 unlinkmany $DIR/$tdir/f $NR
10427                 return
10428         }
10429
10430         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
10431         log "unlink $NR files at $DIR/$tdir"
10432         unlinkmany $DIR/$tdir/f $NR
10433 }
10434 run_test 124a "lru resize ======================================="
10435
10436 get_max_pool_limit()
10437 {
10438         local limit=$($LCTL get_param \
10439                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
10440         local max=0
10441         for l in $limit; do
10442                 if [[ $l -gt $max ]]; then
10443                         max=$l
10444                 fi
10445         done
10446         echo $max
10447 }
10448
10449 test_124b() {
10450         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10451         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10452                 skip_env "no lru resize on server"
10453
10454         LIMIT=$(get_max_pool_limit)
10455
10456         NR=$(($(default_lru_size)*20))
10457         if [[ $NR -gt $LIMIT ]]; then
10458                 log "Limit lock number by $LIMIT locks"
10459                 NR=$LIMIT
10460         fi
10461
10462         IFree=$(mdsrate_inodes_available)
10463         if [ $IFree -lt $NR ]; then
10464                 log "Limit lock number by $IFree inodes"
10465                 NR=$IFree
10466         fi
10467
10468         lru_resize_disable mdc
10469         test_mkdir -p $DIR/$tdir/disable_lru_resize
10470
10471         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
10472         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
10473         cancel_lru_locks mdc
10474         stime=`date +%s`
10475         PID=""
10476         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10477         PID="$PID $!"
10478         sleep 2
10479         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10480         PID="$PID $!"
10481         sleep 2
10482         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10483         PID="$PID $!"
10484         wait $PID
10485         etime=`date +%s`
10486         nolruresize_delta=$((etime-stime))
10487         log "ls -la time: $nolruresize_delta seconds"
10488         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
10489         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
10490
10491         lru_resize_enable mdc
10492         test_mkdir -p $DIR/$tdir/enable_lru_resize
10493
10494         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
10495         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
10496         cancel_lru_locks mdc
10497         stime=`date +%s`
10498         PID=""
10499         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
10500         PID="$PID $!"
10501         sleep 2
10502         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
10503         PID="$PID $!"
10504         sleep 2
10505         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
10506         PID="$PID $!"
10507         wait $PID
10508         etime=`date +%s`
10509         lruresize_delta=$((etime-stime))
10510         log "ls -la time: $lruresize_delta seconds"
10511         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
10512
10513         if [ $lruresize_delta -gt $nolruresize_delta ]; then
10514                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
10515         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
10516                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
10517         else
10518                 log "lru resize performs the same with no lru resize"
10519         fi
10520         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
10521 }
10522 run_test 124b "lru resize (performance test) ======================="
10523
10524 test_124c() {
10525         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10526         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10527                 skip_env "no lru resize on server"
10528
10529         # cache ununsed locks on client
10530         local nr=100
10531         cancel_lru_locks mdc
10532         test_mkdir $DIR/$tdir
10533         createmany -o $DIR/$tdir/f $nr ||
10534                 error "failed to create $nr files in $DIR/$tdir"
10535         ls -l $DIR/$tdir > /dev/null
10536
10537         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
10538         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10539         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
10540         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
10541         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
10542
10543         # set lru_max_age to 1 sec
10544         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
10545         echo "sleep $((recalc_p * 2)) seconds..."
10546         sleep $((recalc_p * 2))
10547
10548         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
10549         # restore lru_max_age
10550         $LCTL set_param -n $nsdir.lru_max_age $max_age
10551         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
10552         unlinkmany $DIR/$tdir/f $nr
10553 }
10554 run_test 124c "LRUR cancel very aged locks"
10555
10556 test_125() { # 13358
10557         $LCTL get_param -n llite.*.client_type | grep -q local ||
10558                 skip "must run as local client"
10559         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
10560                 skip_env "must have acl enabled"
10561         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
10562
10563         test_mkdir $DIR/$tdir
10564         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
10565         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
10566         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
10567 }
10568 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
10569
10570 test_126() { # bug 12829/13455
10571         $GSS && skip_env "must run as gss disabled"
10572         $LCTL get_param -n llite.*.client_type | grep -q local ||
10573                 skip "must run as local client"
10574         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
10575
10576         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
10577         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
10578         rm -f $DIR/$tfile
10579         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
10580 }
10581 run_test 126 "check that the fsgid provided by the client is taken into account"
10582
10583 test_127a() { # bug 15521
10584         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10585
10586         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
10587         $LCTL set_param osc.*.stats=0
10588         FSIZE=$((2048 * 1024))
10589         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
10590         cancel_lru_locks osc
10591         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
10592
10593         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
10594         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
10595                 echo "got $COUNT $NAME"
10596                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
10597                 eval $NAME=$COUNT || error "Wrong proc format"
10598
10599                 case $NAME in
10600                         read_bytes|write_bytes)
10601                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
10602                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
10603                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
10604                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
10605                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
10606                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
10607                                 error "sumsquare is too small: $SUMSQ"
10608                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
10609                                 error "sumsquare is too big: $SUMSQ"
10610                         ;;
10611                         *) ;;
10612                 esac
10613         done < $DIR/${tfile}.tmp
10614
10615         #check that we actually got some stats
10616         [ "$read_bytes" ] || error "Missing read_bytes stats"
10617         [ "$write_bytes" ] || error "Missing write_bytes stats"
10618         [ "$read_bytes" != 0 ] || error "no read done"
10619         [ "$write_bytes" != 0 ] || error "no write done"
10620 }
10621 run_test 127a "verify the client stats are sane"
10622
10623 test_127b() { # bug LU-333
10624         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10625         local name count samp unit min max sum sumsq
10626
10627         $LCTL set_param llite.*.stats=0
10628
10629         # perform 2 reads and writes so MAX is different from SUM.
10630         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
10631         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
10632         cancel_lru_locks osc
10633         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
10634         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
10635
10636         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
10637         while read name count samp unit min max sum sumsq; do
10638                 echo "got $count $name"
10639                 eval $name=$count || error "Wrong proc format"
10640
10641                 case $name in
10642                 read_bytes)
10643                         [ $count -ne 2 ] && error "count is not 2: $count"
10644                         [ $min -ne $PAGE_SIZE ] &&
10645                                 error "min is not $PAGE_SIZE: $min"
10646                         [ $max -ne $PAGE_SIZE ] &&
10647                                 error "max is incorrect: $max"
10648                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
10649                                 error "sum is wrong: $sum"
10650                         ;;
10651                 write_bytes)
10652                         [ $count -ne 2 ] && error "count is not 2: $count"
10653                         [ $min -ne $PAGE_SIZE ] &&
10654                                 error "min is not $PAGE_SIZE: $min"
10655                         [ $max -ne $PAGE_SIZE ] &&
10656                                 error "max is incorrect: $max"
10657                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
10658                                 error "sum is wrong: $sum"
10659                         ;;
10660                 *) ;;
10661                 esac
10662         done < $TMP/$tfile.tmp
10663
10664         #check that we actually got some stats
10665         [ "$read_bytes" ] || error "Missing read_bytes stats"
10666         [ "$write_bytes" ] || error "Missing write_bytes stats"
10667         [ "$read_bytes" != 0 ] || error "no read done"
10668         [ "$write_bytes" != 0 ] || error "no write done"
10669
10670         rm -f $TMP/${tfile}.tmp
10671 }
10672 run_test 127b "verify the llite client stats are sane"
10673
10674 test_128() { # bug 15212
10675         touch $DIR/$tfile
10676         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
10677                 find $DIR/$tfile
10678                 find $DIR/$tfile
10679         EOF
10680
10681         result=$(grep error $TMP/$tfile.log)
10682         rm -f $DIR/$tfile $TMP/$tfile.log
10683         [ -z "$result" ] ||
10684                 error "consecutive find's under interactive lfs failed"
10685 }
10686 run_test 128 "interactive lfs for 2 consecutive find's"
10687
10688 set_dir_limits () {
10689         local mntdev
10690         local canondev
10691         local node
10692
10693         local ldproc=/proc/fs/ldiskfs
10694         local facets=$(get_facets MDS)
10695
10696         for facet in ${facets//,/ }; do
10697                 canondev=$(ldiskfs_canon \
10698                            *.$(convert_facet2label $facet).mntdev $facet)
10699                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
10700                         ldproc=/sys/fs/ldiskfs
10701                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
10702                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
10703         done
10704 }
10705
10706 check_mds_dmesg() {
10707         local facets=$(get_facets MDS)
10708         for facet in ${facets//,/ }; do
10709                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
10710         done
10711         return 1
10712 }
10713
10714 test_129() {
10715         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10716         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
10717                 skip "Need MDS version with at least 2.5.56"
10718         if [ "$mds1_FSTYPE" != ldiskfs ]; then
10719                 skip_env "ldiskfs only test"
10720         fi
10721         remote_mds_nodsh && skip "remote MDS with nodsh"
10722
10723         local ENOSPC=28
10724         local EFBIG=27
10725         local has_warning=false
10726
10727         rm -rf $DIR/$tdir
10728         mkdir -p $DIR/$tdir
10729
10730         # block size of mds1
10731         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
10732         set_dir_limits $maxsize $maxsize
10733         local dirsize=$(stat -c%s "$DIR/$tdir")
10734         local nfiles=0
10735         while [[ $dirsize -le $maxsize ]]; do
10736                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
10737                 rc=$?
10738                 if ! $has_warning; then
10739                         check_mds_dmesg '"is approaching"' && has_warning=true
10740                 fi
10741                 # check two errors:
10742                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
10743                 # EFBIG for previous versions included in ldiskfs series
10744                 if [ $rc -eq $EFBIG ] || [ $rc -eq $ENOSPC ]; then
10745                         set_dir_limits 0 0
10746                         echo "return code $rc received as expected"
10747
10748                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
10749                                 error_exit "create failed w/o dir size limit"
10750
10751                         check_mds_dmesg '"has reached"' ||
10752                                 error_exit "reached message should be output"
10753
10754                         [ $has_warning = "false" ] &&
10755                                 error_exit "warning message should be output"
10756
10757                         dirsize=$(stat -c%s "$DIR/$tdir")
10758
10759                         [[ $dirsize -ge $maxsize ]] && return 0
10760                         error_exit "current dir size $dirsize, " \
10761                                    "previous limit $maxsize"
10762                 elif [ $rc -ne 0 ]; then
10763                         set_dir_limits 0 0
10764                         error_exit "return $rc received instead of expected " \
10765                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
10766                 fi
10767                 nfiles=$((nfiles + 1))
10768                 dirsize=$(stat -c%s "$DIR/$tdir")
10769         done
10770
10771         set_dir_limits 0 0
10772         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
10773 }
10774 run_test 129 "test directory size limit ========================"
10775
10776 OLDIFS="$IFS"
10777 cleanup_130() {
10778         trap 0
10779         IFS="$OLDIFS"
10780 }
10781
10782 test_130a() {
10783         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10784         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
10785
10786         trap cleanup_130 EXIT RETURN
10787
10788         local fm_file=$DIR/$tfile
10789         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
10790         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
10791                 error "dd failed for $fm_file"
10792
10793         # LU-1795: test filefrag/FIEMAP once, even if unsupported
10794         filefrag -ves $fm_file
10795         RC=$?
10796         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
10797                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10798         [ $RC != 0 ] && error "filefrag $fm_file failed"
10799
10800         filefrag_op=$(filefrag -ve -k $fm_file |
10801                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10802         lun=$($LFS getstripe -i $fm_file)
10803
10804         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
10805         IFS=$'\n'
10806         tot_len=0
10807         for line in $filefrag_op
10808         do
10809                 frag_lun=`echo $line | cut -d: -f5`
10810                 ext_len=`echo $line | cut -d: -f4`
10811                 if (( $frag_lun != $lun )); then
10812                         cleanup_130
10813                         error "FIEMAP on 1-stripe file($fm_file) failed"
10814                         return
10815                 fi
10816                 (( tot_len += ext_len ))
10817         done
10818
10819         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
10820                 cleanup_130
10821                 error "FIEMAP on 1-stripe file($fm_file) failed;"
10822                 return
10823         fi
10824
10825         cleanup_130
10826
10827         echo "FIEMAP on single striped file succeeded"
10828 }
10829 run_test 130a "FIEMAP (1-stripe file)"
10830
10831 test_130b() {
10832         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
10833
10834         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10835         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
10836
10837         trap cleanup_130 EXIT RETURN
10838
10839         local fm_file=$DIR/$tfile
10840         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
10841                         error "setstripe on $fm_file"
10842         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
10843                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10844
10845         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
10846                 error "dd failed on $fm_file"
10847
10848         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10849         filefrag_op=$(filefrag -ve -k $fm_file |
10850                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10851
10852         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10853                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10854
10855         IFS=$'\n'
10856         tot_len=0
10857         num_luns=1
10858         for line in $filefrag_op
10859         do
10860                 frag_lun=$(echo $line | cut -d: -f5 |
10861                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10862                 ext_len=$(echo $line | cut -d: -f4)
10863                 if (( $frag_lun != $last_lun )); then
10864                         if (( tot_len != 1024 )); then
10865                                 cleanup_130
10866                                 error "FIEMAP on $fm_file failed; returned " \
10867                                 "len $tot_len for OST $last_lun instead of 1024"
10868                                 return
10869                         else
10870                                 (( num_luns += 1 ))
10871                                 tot_len=0
10872                         fi
10873                 fi
10874                 (( tot_len += ext_len ))
10875                 last_lun=$frag_lun
10876         done
10877         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
10878                 cleanup_130
10879                 error "FIEMAP on $fm_file failed; returned wrong number of " \
10880                         "luns or wrong len for OST $last_lun"
10881                 return
10882         fi
10883
10884         cleanup_130
10885
10886         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
10887 }
10888 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
10889
10890 test_130c() {
10891         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10892
10893         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10894         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
10895
10896         trap cleanup_130 EXIT RETURN
10897
10898         local fm_file=$DIR/$tfile
10899         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
10900         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
10901                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10902
10903         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
10904                         error "dd failed on $fm_file"
10905
10906         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10907         filefrag_op=$(filefrag -ve -k $fm_file |
10908                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10909
10910         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10911                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10912
10913         IFS=$'\n'
10914         tot_len=0
10915         num_luns=1
10916         for line in $filefrag_op
10917         do
10918                 frag_lun=$(echo $line | cut -d: -f5 |
10919                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10920                 ext_len=$(echo $line | cut -d: -f4)
10921                 if (( $frag_lun != $last_lun )); then
10922                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
10923                         if (( logical != 512 )); then
10924                                 cleanup_130
10925                                 error "FIEMAP on $fm_file failed; returned " \
10926                                 "logical start for lun $logical instead of 512"
10927                                 return
10928                         fi
10929                         if (( tot_len != 512 )); then
10930                                 cleanup_130
10931                                 error "FIEMAP on $fm_file failed; returned " \
10932                                 "len $tot_len for OST $last_lun instead of 1024"
10933                                 return
10934                         else
10935                                 (( num_luns += 1 ))
10936                                 tot_len=0
10937                         fi
10938                 fi
10939                 (( tot_len += ext_len ))
10940                 last_lun=$frag_lun
10941         done
10942         if (( num_luns != 2 || tot_len != 512 )); then
10943                 cleanup_130
10944                 error "FIEMAP on $fm_file failed; returned wrong number of " \
10945                         "luns or wrong len for OST $last_lun"
10946                 return
10947         fi
10948
10949         cleanup_130
10950
10951         echo "FIEMAP on 2-stripe file with hole succeeded"
10952 }
10953 run_test 130c "FIEMAP (2-stripe file with hole)"
10954
10955 test_130d() {
10956         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
10957
10958         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10959         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
10960
10961         trap cleanup_130 EXIT RETURN
10962
10963         local fm_file=$DIR/$tfile
10964         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
10965                         error "setstripe on $fm_file"
10966         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
10967                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10968
10969         local actual_stripe_count=$($LFS getstripe -c $fm_file)
10970         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
10971                 error "dd failed on $fm_file"
10972
10973         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10974         filefrag_op=$(filefrag -ve -k $fm_file |
10975                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10976
10977         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10978                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10979
10980         IFS=$'\n'
10981         tot_len=0
10982         num_luns=1
10983         for line in $filefrag_op
10984         do
10985                 frag_lun=$(echo $line | cut -d: -f5 |
10986                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10987                 ext_len=$(echo $line | cut -d: -f4)
10988                 if (( $frag_lun != $last_lun )); then
10989                         if (( tot_len != 1024 )); then
10990                                 cleanup_130
10991                                 error "FIEMAP on $fm_file failed; returned " \
10992                                 "len $tot_len for OST $last_lun instead of 1024"
10993                                 return
10994                         else
10995                                 (( num_luns += 1 ))
10996                                 tot_len=0
10997                         fi
10998                 fi
10999                 (( tot_len += ext_len ))
11000                 last_lun=$frag_lun
11001         done
11002         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
11003                 cleanup_130
11004                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11005                         "luns or wrong len for OST $last_lun"
11006                 return
11007         fi
11008
11009         cleanup_130
11010
11011         echo "FIEMAP on N-stripe file succeeded"
11012 }
11013 run_test 130d "FIEMAP (N-stripe file)"
11014
11015 test_130e() {
11016         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11017
11018         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11019         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11020
11021         trap cleanup_130 EXIT RETURN
11022
11023         local fm_file=$DIR/$tfile
11024         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
11025         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11026                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11027
11028         NUM_BLKS=512
11029         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
11030         for ((i = 0; i < $NUM_BLKS; i++))
11031         do
11032                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
11033         done
11034
11035         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11036         filefrag_op=$(filefrag -ve -k $fm_file |
11037                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11038
11039         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11040                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11041
11042         IFS=$'\n'
11043         tot_len=0
11044         num_luns=1
11045         for line in $filefrag_op
11046         do
11047                 frag_lun=$(echo $line | cut -d: -f5 |
11048                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11049                 ext_len=$(echo $line | cut -d: -f4)
11050                 if (( $frag_lun != $last_lun )); then
11051                         if (( tot_len != $EXPECTED_LEN )); then
11052                                 cleanup_130
11053                                 error "FIEMAP on $fm_file failed; returned " \
11054                                 "len $tot_len for OST $last_lun instead " \
11055                                 "of $EXPECTED_LEN"
11056                                 return
11057                         else
11058                                 (( num_luns += 1 ))
11059                                 tot_len=0
11060                         fi
11061                 fi
11062                 (( tot_len += ext_len ))
11063                 last_lun=$frag_lun
11064         done
11065         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
11066                 cleanup_130
11067                 error "FIEMAP on $fm_file failed; returned wrong number " \
11068                         "of luns or wrong len for OST $last_lun"
11069                 return
11070         fi
11071
11072         cleanup_130
11073
11074         echo "FIEMAP with continuation calls succeeded"
11075 }
11076 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
11077
11078 test_130f() {
11079         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11080         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11081
11082         local fm_file=$DIR/$tfile
11083         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
11084                 error "multiop create with lov_delay_create on $fm_file"
11085
11086         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11087         filefrag_extents=$(filefrag -vek $fm_file |
11088                            awk '/extents? found/ { print $2 }')
11089         if [[ "$filefrag_extents" != "0" ]]; then
11090                 error "FIEMAP on $fm_file failed; " \
11091                       "returned $filefrag_extents expected 0"
11092         fi
11093
11094         rm -f $fm_file
11095 }
11096 run_test 130f "FIEMAP (unstriped file)"
11097
11098 # Test for writev/readv
11099 test_131a() {
11100         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
11101                 error "writev test failed"
11102         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
11103                 error "readv failed"
11104         rm -f $DIR/$tfile
11105 }
11106 run_test 131a "test iov's crossing stripe boundary for writev/readv"
11107
11108 test_131b() {
11109         local fsize=$((524288 + 1048576 + 1572864))
11110         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
11111                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
11112                         error "append writev test failed"
11113
11114         ((fsize += 1572864 + 1048576))
11115         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
11116                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
11117                         error "append writev test failed"
11118         rm -f $DIR/$tfile
11119 }
11120 run_test 131b "test append writev"
11121
11122 test_131c() {
11123         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
11124         error "NOT PASS"
11125 }
11126 run_test 131c "test read/write on file w/o objects"
11127
11128 test_131d() {
11129         rwv -f $DIR/$tfile -w -n 1 1572864
11130         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
11131         if [ "$NOB" != 1572864 ]; then
11132                 error "Short read filed: read $NOB bytes instead of 1572864"
11133         fi
11134         rm -f $DIR/$tfile
11135 }
11136 run_test 131d "test short read"
11137
11138 test_131e() {
11139         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
11140         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
11141         error "read hitting hole failed"
11142         rm -f $DIR/$tfile
11143 }
11144 run_test 131e "test read hitting hole"
11145
11146 check_stats() {
11147         local facet=$1
11148         local op=$2
11149         local want=${3:-0}
11150         local res
11151
11152         case $facet in
11153         mds*) res=$(do_facet $facet \
11154                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
11155                  ;;
11156         ost*) res=$(do_facet $facet \
11157                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
11158                  ;;
11159         *) error "Wrong facet '$facet'" ;;
11160         esac
11161         [ "$res" ] || error "The counter for $op on $facet was not incremented"
11162         # if the argument $3 is zero, it means any stat increment is ok.
11163         if [[ $want -gt 0 ]]; then
11164                 local count=$(echo $res | awk '{ print $2 }')
11165                 [[ $count -ne $want ]] &&
11166                         error "The $op counter on $facet is $count, not $want"
11167         fi
11168 }
11169
11170 test_133a() {
11171         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11172         remote_ost_nodsh && skip "remote OST with nodsh"
11173         remote_mds_nodsh && skip "remote MDS with nodsh"
11174         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
11175                 skip_env "MDS doesn't support rename stats"
11176
11177         local testdir=$DIR/${tdir}/stats_testdir
11178
11179         mkdir -p $DIR/${tdir}
11180
11181         # clear stats.
11182         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11183         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11184
11185         # verify mdt stats first.
11186         mkdir ${testdir} || error "mkdir failed"
11187         check_stats $SINGLEMDS "mkdir" 1
11188         touch ${testdir}/${tfile} || error "touch failed"
11189         check_stats $SINGLEMDS "open" 1
11190         check_stats $SINGLEMDS "close" 1
11191         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
11192                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
11193                 check_stats $SINGLEMDS "mknod" 2
11194         }
11195         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
11196         check_stats $SINGLEMDS "unlink" 1
11197         rm -f ${testdir}/${tfile} || error "file remove failed"
11198         check_stats $SINGLEMDS "unlink" 2
11199
11200         # remove working dir and check mdt stats again.
11201         rmdir ${testdir} || error "rmdir failed"
11202         check_stats $SINGLEMDS "rmdir" 1
11203
11204         local testdir1=$DIR/${tdir}/stats_testdir1
11205         mkdir -p ${testdir}
11206         mkdir -p ${testdir1}
11207         touch ${testdir1}/test1
11208         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
11209         check_stats $SINGLEMDS "crossdir_rename" 1
11210
11211         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
11212         check_stats $SINGLEMDS "samedir_rename" 1
11213
11214         rm -rf $DIR/${tdir}
11215 }
11216 run_test 133a "Verifying MDT stats ========================================"
11217
11218 test_133b() {
11219         local res
11220
11221         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11222         remote_ost_nodsh && skip "remote OST with nodsh"
11223         remote_mds_nodsh && skip "remote MDS with nodsh"
11224
11225         local testdir=$DIR/${tdir}/stats_testdir
11226
11227         mkdir -p ${testdir} || error "mkdir failed"
11228         touch ${testdir}/${tfile} || error "touch failed"
11229         cancel_lru_locks mdc
11230
11231         # clear stats.
11232         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11233         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11234
11235         # extra mdt stats verification.
11236         chmod 444 ${testdir}/${tfile} || error "chmod failed"
11237         check_stats $SINGLEMDS "setattr" 1
11238         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11239         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
11240         then            # LU-1740
11241                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
11242                 check_stats $SINGLEMDS "getattr" 1
11243         fi
11244         rm -rf $DIR/${tdir}
11245
11246         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
11247         # so the check below is not reliable
11248         [ $MDSCOUNT -eq 1 ] || return 0
11249
11250         # Sleep to avoid a cached response.
11251         #define OBD_STATFS_CACHE_SECONDS 1
11252         sleep 2
11253         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11254         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
11255         $LFS df || error "lfs failed"
11256         check_stats $SINGLEMDS "statfs" 1
11257
11258         # check aggregated statfs (LU-10018)
11259         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
11260                 return 0
11261         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
11262                 return 0
11263         sleep 2
11264         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11265         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
11266         df $DIR
11267         check_stats $SINGLEMDS "statfs" 1
11268
11269         # We want to check that the client didn't send OST_STATFS to
11270         # ost1 but the MDT also uses OST_STATFS for precreate. So some
11271         # extra care is needed here.
11272         if remote_mds; then
11273                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
11274                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
11275
11276                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
11277                 [ "$res" ] && error "OST got STATFS"
11278         fi
11279
11280         return 0
11281 }
11282 run_test 133b "Verifying extra MDT stats =================================="
11283
11284 test_133c() {
11285         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11286         remote_ost_nodsh && skip "remote OST with nodsh"
11287         remote_mds_nodsh && skip "remote MDS with nodsh"
11288
11289         local testdir=$DIR/$tdir/stats_testdir
11290
11291         test_mkdir -p $testdir
11292
11293         # verify obdfilter stats.
11294         $LFS setstripe -c 1 -i 0 $testdir/$tfile
11295         sync
11296         cancel_lru_locks osc
11297         wait_delete_completed
11298
11299         # clear stats.
11300         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11301         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11302
11303         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
11304                 error "dd failed"
11305         sync
11306         cancel_lru_locks osc
11307         check_stats ost1 "write" 1
11308
11309         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
11310         check_stats ost1 "read" 1
11311
11312         > $testdir/$tfile || error "truncate failed"
11313         check_stats ost1 "punch" 1
11314
11315         rm -f $testdir/$tfile || error "file remove failed"
11316         wait_delete_completed
11317         check_stats ost1 "destroy" 1
11318
11319         rm -rf $DIR/$tdir
11320 }
11321 run_test 133c "Verifying OST stats ========================================"
11322
11323 order_2() {
11324         local value=$1
11325         local orig=$value
11326         local order=1
11327
11328         while [ $value -ge 2 ]; do
11329                 order=$((order*2))
11330                 value=$((value/2))
11331         done
11332
11333         if [ $orig -gt $order ]; then
11334                 order=$((order*2))
11335         fi
11336         echo $order
11337 }
11338
11339 size_in_KMGT() {
11340     local value=$1
11341     local size=('K' 'M' 'G' 'T');
11342     local i=0
11343     local size_string=$value
11344
11345     while [ $value -ge 1024 ]; do
11346         if [ $i -gt 3 ]; then
11347             #T is the biggest unit we get here, if that is bigger,
11348             #just return XXXT
11349             size_string=${value}T
11350             break
11351         fi
11352         value=$((value >> 10))
11353         if [ $value -lt 1024 ]; then
11354             size_string=${value}${size[$i]}
11355             break
11356         fi
11357         i=$((i + 1))
11358     done
11359
11360     echo $size_string
11361 }
11362
11363 get_rename_size() {
11364         local size=$1
11365         local context=${2:-.}
11366         local sample=$(do_facet $SINGLEMDS $LCTL \
11367                 get_param mdt.$FSNAME-MDT0000.rename_stats |
11368                 grep -A1 $context |
11369                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
11370         echo $sample
11371 }
11372
11373 test_133d() {
11374         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11375         remote_ost_nodsh && skip "remote OST with nodsh"
11376         remote_mds_nodsh && skip "remote MDS with nodsh"
11377         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
11378                 skip_env "MDS doesn't support rename stats"
11379
11380         local testdir1=$DIR/${tdir}/stats_testdir1
11381         local testdir2=$DIR/${tdir}/stats_testdir2
11382         mkdir -p $DIR/${tdir}
11383
11384         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
11385
11386         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
11387         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
11388
11389         createmany -o $testdir1/test 512 || error "createmany failed"
11390
11391         # check samedir rename size
11392         mv ${testdir1}/test0 ${testdir1}/test_0
11393
11394         local testdir1_size=$(ls -l $DIR/${tdir} |
11395                 awk '/stats_testdir1/ {print $5}')
11396         local testdir2_size=$(ls -l $DIR/${tdir} |
11397                 awk '/stats_testdir2/ {print $5}')
11398
11399         testdir1_size=$(order_2 $testdir1_size)
11400         testdir2_size=$(order_2 $testdir2_size)
11401
11402         testdir1_size=$(size_in_KMGT $testdir1_size)
11403         testdir2_size=$(size_in_KMGT $testdir2_size)
11404
11405         echo "source rename dir size: ${testdir1_size}"
11406         echo "target rename dir size: ${testdir2_size}"
11407
11408         local cmd="do_facet $SINGLEMDS $LCTL "
11409         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
11410
11411         eval $cmd || error "$cmd failed"
11412         local samedir=$($cmd | grep 'same_dir')
11413         local same_sample=$(get_rename_size $testdir1_size)
11414         [ -z "$samedir" ] && error "samedir_rename_size count error"
11415         [[ $same_sample -eq 1 ]] ||
11416                 error "samedir_rename_size error $same_sample"
11417         echo "Check same dir rename stats success"
11418
11419         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
11420
11421         # check crossdir rename size
11422         mv ${testdir1}/test_0 ${testdir2}/test_0
11423
11424         testdir1_size=$(ls -l $DIR/${tdir} |
11425                 awk '/stats_testdir1/ {print $5}')
11426         testdir2_size=$(ls -l $DIR/${tdir} |
11427                 awk '/stats_testdir2/ {print $5}')
11428
11429         testdir1_size=$(order_2 $testdir1_size)
11430         testdir2_size=$(order_2 $testdir2_size)
11431
11432         testdir1_size=$(size_in_KMGT $testdir1_size)
11433         testdir2_size=$(size_in_KMGT $testdir2_size)
11434
11435         echo "source rename dir size: ${testdir1_size}"
11436         echo "target rename dir size: ${testdir2_size}"
11437
11438         eval $cmd || error "$cmd failed"
11439         local crossdir=$($cmd | grep 'crossdir')
11440         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
11441         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
11442         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
11443         [[ $src_sample -eq 1 ]] ||
11444                 error "crossdir_rename_size error $src_sample"
11445         [[ $tgt_sample -eq 1 ]] ||
11446                 error "crossdir_rename_size error $tgt_sample"
11447         echo "Check cross dir rename stats success"
11448         rm -rf $DIR/${tdir}
11449 }
11450 run_test 133d "Verifying rename_stats ========================================"
11451
11452 test_133e() {
11453         remote_mds_nodsh && skip "remote MDS with nodsh"
11454         remote_ost_nodsh && skip "remote OST with nodsh"
11455         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11456
11457         local testdir=$DIR/${tdir}/stats_testdir
11458         local ctr f0 f1 bs=32768 count=42 sum
11459
11460         mkdir -p ${testdir} || error "mkdir failed"
11461
11462         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
11463
11464         for ctr in {write,read}_bytes; do
11465                 sync
11466                 cancel_lru_locks osc
11467
11468                 do_facet ost1 $LCTL set_param -n \
11469                         "obdfilter.*.exports.clear=clear"
11470
11471                 if [ $ctr = write_bytes ]; then
11472                         f0=/dev/zero
11473                         f1=${testdir}/${tfile}
11474                 else
11475                         f0=${testdir}/${tfile}
11476                         f1=/dev/null
11477                 fi
11478
11479                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
11480                         error "dd failed"
11481                 sync
11482                 cancel_lru_locks osc
11483
11484                 sum=$(do_facet ost1 $LCTL get_param \
11485                         "obdfilter.*.exports.*.stats" |
11486                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
11487                                 $1 == ctr { sum += $7 }
11488                                 END { printf("%0.0f", sum) }')
11489
11490                 if ((sum != bs * count)); then
11491                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
11492                 fi
11493         done
11494
11495         rm -rf $DIR/${tdir}
11496 }
11497 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
11498
11499 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
11500
11501 # Some versions of find (4.5.11, 4.5.14) included in CentOS 7.3-7.5 do
11502 # not honor the -ignore_readdir_race option correctly. So we call
11503 # error_ignore() rather than error() in these cases. See LU-11152.
11504 error_133() {
11505         if (find --version; do_facet mds1 find --version) |
11506                 grep -q '\b4\.5\.1[1-4]\b'; then
11507                 error_ignore LU-11152 "$@"
11508         else
11509                 error "$@"
11510         fi
11511 }
11512
11513 test_133f() {
11514         # First without trusting modes.
11515         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
11516         echo "proc_dirs='$proc_dirs'"
11517         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
11518         find $proc_dirs -exec cat '{}' \; &> /dev/null
11519
11520         # Second verifying readability.
11521         $LCTL get_param -R '*' &> /dev/null
11522
11523         # Verifing writability with badarea_io.
11524         find $proc_dirs \
11525                 -ignore_readdir_race \
11526                 -type f \
11527                 -not -name force_lbug \
11528                 -not -name changelog_mask \
11529                 -exec badarea_io '{}' \; ||
11530                         error_133 "find $proc_dirs failed"
11531 }
11532 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
11533
11534 test_133g() {
11535         remote_mds_nodsh && skip "remote MDS with nodsh"
11536         remote_ost_nodsh && skip "remote OST with nodsh"
11537
11538         # eventually, this can also be replaced with "lctl get_param -R",
11539         # but not until that option is always available on the server
11540         local facet
11541         for facet in mds1 ost1; do
11542                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
11543                         skip_noexit "Too old lustre on $facet"
11544                 local facet_proc_dirs=$(do_facet $facet \
11545                                         \\\ls -d $proc_regexp 2>/dev/null)
11546                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
11547                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
11548                 do_facet $facet find $facet_proc_dirs \
11549                         ! -name req_history \
11550                         -exec cat '{}' \\\; &> /dev/null
11551
11552                 do_facet $facet find $facet_proc_dirs \
11553                         ! -name req_history \
11554                         -type f \
11555                         -exec cat '{}' \\\; &> /dev/null ||
11556                                 error "proc file read failed"
11557
11558                 do_facet $facet find $facet_proc_dirs \
11559                         -ignore_readdir_race \
11560                         -type f \
11561                         -not -name force_lbug \
11562                         -not -name changelog_mask \
11563                         -exec badarea_io '{}' \\\; ||
11564                                 error_133 "$facet find $facet_proc_dirs failed"
11565         done
11566
11567         # remount the FS in case writes/reads /proc break the FS
11568         cleanup || error "failed to unmount"
11569         setup || error "failed to setup"
11570         true
11571 }
11572 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
11573
11574 test_133h() {
11575         remote_mds_nodsh && skip "remote MDS with nodsh"
11576         remote_ost_nodsh && skip "remote OST with nodsh"
11577         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
11578                 skip "Need MDS version at least 2.9.54"
11579
11580         local facet
11581
11582         for facet in client mds1 ost1; do
11583                 local facet_proc_dirs=$(do_facet $facet \
11584                                         \\\ls -d $proc_regexp 2> /dev/null)
11585                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
11586                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
11587                 # Get the list of files that are missing the terminating newline
11588                 local missing=($(do_facet $facet \
11589                         find ${facet_proc_dirs} -type f \|              \
11590                                 while read F\; do                       \
11591                                         awk -v FS='\v' -v RS='\v\v'     \
11592                                         "'END { if(NR>0 &&              \
11593                                         \\\$NF !~ /.*\\\n\$/)           \
11594                                                 print FILENAME}'"       \
11595                                         '\$F'\;                         \
11596                                 done 2>/dev/null))
11597                 [ ${#missing[*]} -eq 0 ] ||
11598                         error "files do not end with newline: ${missing[*]}"
11599         done
11600 }
11601 run_test 133h "Proc files should end with newlines"
11602
11603 test_134a() {
11604         remote_mds_nodsh && skip "remote MDS with nodsh"
11605         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
11606                 skip "Need MDS version at least 2.7.54"
11607
11608         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
11609         cancel_lru_locks mdc
11610
11611         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11612         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11613         [ $unused -eq 0 ] || error "$unused locks are not cleared"
11614
11615         local nr=1000
11616         createmany -o $DIR/$tdir/f $nr ||
11617                 error "failed to create $nr files in $DIR/$tdir"
11618         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11619
11620         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
11621         do_facet mds1 $LCTL set_param fail_loc=0x327
11622         do_facet mds1 $LCTL set_param fail_val=500
11623         touch $DIR/$tdir/m
11624
11625         echo "sleep 10 seconds ..."
11626         sleep 10
11627         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
11628
11629         do_facet mds1 $LCTL set_param fail_loc=0
11630         do_facet mds1 $LCTL set_param fail_val=0
11631         [ $lck_cnt -lt $unused ] ||
11632                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
11633
11634         rm $DIR/$tdir/m
11635         unlinkmany $DIR/$tdir/f $nr
11636 }
11637 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
11638
11639 test_134b() {
11640         remote_mds_nodsh && skip "remote MDS with nodsh"
11641         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
11642                 skip "Need MDS version at least 2.7.54"
11643
11644         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
11645         cancel_lru_locks mdc
11646
11647         local low_wm=$(do_facet mds1 $LCTL get_param -n \
11648                         ldlm.lock_reclaim_threshold_mb)
11649         # disable reclaim temporarily
11650         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
11651
11652         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
11653         do_facet mds1 $LCTL set_param fail_loc=0x328
11654         do_facet mds1 $LCTL set_param fail_val=500
11655
11656         $LCTL set_param debug=+trace
11657
11658         local nr=600
11659         createmany -o $DIR/$tdir/f $nr &
11660         local create_pid=$!
11661
11662         echo "Sleep $TIMEOUT seconds ..."
11663         sleep $TIMEOUT
11664         if ! ps -p $create_pid  > /dev/null 2>&1; then
11665                 do_facet mds1 $LCTL set_param fail_loc=0
11666                 do_facet mds1 $LCTL set_param fail_val=0
11667                 do_facet mds1 $LCTL set_param \
11668                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
11669                 error "createmany finished incorrectly!"
11670         fi
11671         do_facet mds1 $LCTL set_param fail_loc=0
11672         do_facet mds1 $LCTL set_param fail_val=0
11673         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
11674         wait $create_pid || return 1
11675
11676         unlinkmany $DIR/$tdir/f $nr
11677 }
11678 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
11679
11680 test_140() { #bug-17379
11681         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11682
11683         test_mkdir $DIR/$tdir
11684         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
11685         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
11686
11687         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
11688         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
11689         local i=0
11690         while i=$((i + 1)); do
11691                 test_mkdir $i
11692                 cd $i || error "Changing to $i"
11693                 ln -s ../stat stat || error "Creating stat symlink"
11694                 # Read the symlink until ELOOP present,
11695                 # not LBUGing the system is considered success,
11696                 # we didn't overrun the stack.
11697                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
11698                 if [ $ret -ne 0 ]; then
11699                         if [ $ret -eq 40 ]; then
11700                                 break  # -ELOOP
11701                         else
11702                                 error "Open stat symlink"
11703                                         return
11704                         fi
11705                 fi
11706         done
11707         i=$((i - 1))
11708         echo "The symlink depth = $i"
11709         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
11710                 error "Invalid symlink depth"
11711
11712         # Test recursive symlink
11713         ln -s symlink_self symlink_self
11714         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
11715         echo "open symlink_self returns $ret"
11716         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
11717 }
11718 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
11719
11720 test_150() {
11721         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11722
11723         local TF="$TMP/$tfile"
11724
11725         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
11726         cp $TF $DIR/$tfile
11727         cancel_lru_locks $OSC
11728         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
11729         remount_client $MOUNT
11730         df -P $MOUNT
11731         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
11732
11733         $TRUNCATE $TF 6000
11734         $TRUNCATE $DIR/$tfile 6000
11735         cancel_lru_locks $OSC
11736         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
11737
11738         echo "12345" >>$TF
11739         echo "12345" >>$DIR/$tfile
11740         cancel_lru_locks $OSC
11741         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
11742
11743         echo "12345" >>$TF
11744         echo "12345" >>$DIR/$tfile
11745         cancel_lru_locks $OSC
11746         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
11747
11748         rm -f $TF
11749         true
11750 }
11751 run_test 150 "truncate/append tests"
11752
11753 #LU-2902 roc_hit was not able to read all values from lproc
11754 function roc_hit_init() {
11755         local list=$(comma_list $(osts_nodes))
11756         local dir=$DIR/$tdir-check
11757         local file=$dir/$tfile
11758         local BEFORE
11759         local AFTER
11760         local idx
11761
11762         test_mkdir $dir
11763         #use setstripe to do a write to every ost
11764         for i in $(seq 0 $((OSTCOUNT-1))); do
11765                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
11766                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
11767                 idx=$(printf %04x $i)
11768                 BEFORE=$(get_osd_param $list *OST*$idx stats |
11769                         awk '$1 == "cache_access" {sum += $7}
11770                                 END { printf("%0.0f", sum) }')
11771
11772                 cancel_lru_locks osc
11773                 cat $file >/dev/null
11774
11775                 AFTER=$(get_osd_param $list *OST*$idx stats |
11776                         awk '$1 == "cache_access" {sum += $7}
11777                                 END { printf("%0.0f", sum) }')
11778
11779                 echo BEFORE:$BEFORE AFTER:$AFTER
11780                 if ! let "AFTER - BEFORE == 4"; then
11781                         rm -rf $dir
11782                         error "roc_hit is not safe to use"
11783                 fi
11784                 rm $file
11785         done
11786
11787         rm -rf $dir
11788 }
11789
11790 function roc_hit() {
11791         local list=$(comma_list $(osts_nodes))
11792         echo $(get_osd_param $list '' stats |
11793                 awk '$1 == "cache_hit" {sum += $7}
11794                         END { printf("%0.0f", sum) }')
11795 }
11796
11797 function set_cache() {
11798         local on=1
11799
11800         if [ "$2" == "off" ]; then
11801                 on=0;
11802         fi
11803         local list=$(comma_list $(osts_nodes))
11804         set_osd_param $list '' $1_cache_enable $on
11805
11806         cancel_lru_locks osc
11807 }
11808
11809 test_151() {
11810         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11811         remote_ost_nodsh && skip "remote OST with nodsh"
11812
11813         local CPAGES=3
11814         local list=$(comma_list $(osts_nodes))
11815
11816         # check whether obdfilter is cache capable at all
11817         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
11818                 skip "not cache-capable obdfilter"
11819         fi
11820
11821         # check cache is enabled on all obdfilters
11822         if get_osd_param $list '' read_cache_enable | grep 0; then
11823                 skip "oss cache is disabled"
11824         fi
11825
11826         set_osd_param $list '' writethrough_cache_enable 1
11827
11828         # check write cache is enabled on all obdfilters
11829         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
11830                 skip "oss write cache is NOT enabled"
11831         fi
11832
11833         roc_hit_init
11834
11835         #define OBD_FAIL_OBD_NO_LRU  0x609
11836         do_nodes $list $LCTL set_param fail_loc=0x609
11837
11838         # pages should be in the case right after write
11839         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
11840                 error "dd failed"
11841
11842         local BEFORE=$(roc_hit)
11843         cancel_lru_locks osc
11844         cat $DIR/$tfile >/dev/null
11845         local AFTER=$(roc_hit)
11846
11847         do_nodes $list $LCTL set_param fail_loc=0
11848
11849         if ! let "AFTER - BEFORE == CPAGES"; then
11850                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11851         fi
11852
11853         # the following read invalidates the cache
11854         cancel_lru_locks osc
11855         set_osd_param $list '' read_cache_enable 0
11856         cat $DIR/$tfile >/dev/null
11857
11858         # now data shouldn't be found in the cache
11859         BEFORE=$(roc_hit)
11860         cancel_lru_locks osc
11861         cat $DIR/$tfile >/dev/null
11862         AFTER=$(roc_hit)
11863         if let "AFTER - BEFORE != 0"; then
11864                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11865         fi
11866
11867         set_osd_param $list '' read_cache_enable 1
11868         rm -f $DIR/$tfile
11869 }
11870 run_test 151 "test cache on oss and controls ==============================="
11871
11872 test_152() {
11873         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11874
11875         local TF="$TMP/$tfile"
11876
11877         # simulate ENOMEM during write
11878 #define OBD_FAIL_OST_NOMEM      0x226
11879         lctl set_param fail_loc=0x80000226
11880         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
11881         cp $TF $DIR/$tfile
11882         sync || error "sync failed"
11883         lctl set_param fail_loc=0
11884
11885         # discard client's cache
11886         cancel_lru_locks osc
11887
11888         # simulate ENOMEM during read
11889         lctl set_param fail_loc=0x80000226
11890         cmp $TF $DIR/$tfile || error "cmp failed"
11891         lctl set_param fail_loc=0
11892
11893         rm -f $TF
11894 }
11895 run_test 152 "test read/write with enomem ============================"
11896
11897 test_153() {
11898         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
11899 }
11900 run_test 153 "test if fdatasync does not crash ======================="
11901
11902 dot_lustre_fid_permission_check() {
11903         local fid=$1
11904         local ffid=$MOUNT/.lustre/fid/$fid
11905         local test_dir=$2
11906
11907         echo "stat fid $fid"
11908         stat $ffid > /dev/null || error "stat $ffid failed."
11909         echo "touch fid $fid"
11910         touch $ffid || error "touch $ffid failed."
11911         echo "write to fid $fid"
11912         cat /etc/hosts > $ffid || error "write $ffid failed."
11913         echo "read fid $fid"
11914         diff /etc/hosts $ffid || error "read $ffid failed."
11915         echo "append write to fid $fid"
11916         cat /etc/hosts >> $ffid || error "append write $ffid failed."
11917         echo "rename fid $fid"
11918         mv $ffid $test_dir/$tfile.1 &&
11919                 error "rename $ffid to $tfile.1 should fail."
11920         touch $test_dir/$tfile.1
11921         mv $test_dir/$tfile.1 $ffid &&
11922                 error "rename $tfile.1 to $ffid should fail."
11923         rm -f $test_dir/$tfile.1
11924         echo "truncate fid $fid"
11925         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
11926         echo "link fid $fid"
11927         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
11928         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
11929                 echo "setfacl fid $fid"
11930                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
11931                 echo "getfacl fid $fid"
11932                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
11933         fi
11934         echo "unlink fid $fid"
11935         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
11936         echo "mknod fid $fid"
11937         mknod $ffid c 1 3 && error "mknod $ffid should fail."
11938
11939         fid=[0xf00000400:0x1:0x0]
11940         ffid=$MOUNT/.lustre/fid/$fid
11941
11942         echo "stat non-exist fid $fid"
11943         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
11944         echo "write to non-exist fid $fid"
11945         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
11946         echo "link new fid $fid"
11947         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
11948
11949         mkdir -p $test_dir/$tdir
11950         touch $test_dir/$tdir/$tfile
11951         fid=$($LFS path2fid $test_dir/$tdir)
11952         rc=$?
11953         [ $rc -ne 0 ] &&
11954                 error "error: could not get fid for $test_dir/$dir/$tfile."
11955
11956         ffid=$MOUNT/.lustre/fid/$fid
11957
11958         echo "ls $fid"
11959         ls $ffid > /dev/null || error "ls $ffid failed."
11960         echo "touch $fid/$tfile.1"
11961         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
11962
11963         echo "touch $MOUNT/.lustre/fid/$tfile"
11964         touch $MOUNT/.lustre/fid/$tfile && \
11965                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
11966
11967         echo "setxattr to $MOUNT/.lustre/fid"
11968         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
11969
11970         echo "listxattr for $MOUNT/.lustre/fid"
11971         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
11972
11973         echo "delxattr from $MOUNT/.lustre/fid"
11974         setfattr -x trusted.name1 $MOUNT/.lustre/fid
11975
11976         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
11977         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
11978                 error "touch invalid fid should fail."
11979
11980         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
11981         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
11982                 error "touch non-normal fid should fail."
11983
11984         echo "rename $tdir to $MOUNT/.lustre/fid"
11985         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
11986                 error "rename to $MOUNT/.lustre/fid should fail."
11987
11988         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
11989         then            # LU-3547
11990                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
11991                 local new_obf_mode=777
11992
11993                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
11994                 chmod $new_obf_mode $DIR/.lustre/fid ||
11995                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
11996
11997                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
11998                 [ $obf_mode -eq $new_obf_mode ] ||
11999                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
12000
12001                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
12002                 chmod $old_obf_mode $DIR/.lustre/fid ||
12003                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
12004         fi
12005
12006         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
12007         fid=$($LFS path2fid $test_dir/$tfile-2)
12008
12009         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
12010         then # LU-5424
12011                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
12012                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
12013                         error "create lov data thru .lustre failed"
12014         fi
12015         echo "cp /etc/passwd $test_dir/$tfile-2"
12016         cp /etc/passwd $test_dir/$tfile-2 ||
12017                 error "copy to $test_dir/$tfile-2 failed."
12018         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
12019         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
12020                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
12021
12022         rm -rf $test_dir/tfile.lnk
12023         rm -rf $test_dir/$tfile-2
12024 }
12025
12026 test_154A() {
12027         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12028                 skip "Need MDS version at least 2.4.1"
12029
12030         local tf=$DIR/$tfile
12031         touch $tf
12032
12033         local fid=$($LFS path2fid $tf)
12034         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
12035
12036         # check that we get the same pathname back
12037         local found=$($LFS fid2path $MOUNT "$fid")
12038         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
12039         [ "$found" == "$tf" ] ||
12040                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
12041 }
12042 run_test 154A "lfs path2fid and fid2path basic checks"
12043
12044 test_154B() {
12045         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12046                 skip "Need MDS version at least 2.4.1"
12047
12048         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
12049         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
12050         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
12051         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
12052
12053         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
12054         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
12055
12056         # check that we get the same pathname
12057         echo "PFID: $PFID, name: $name"
12058         local FOUND=$($LFS fid2path $MOUNT "$PFID")
12059         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
12060         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
12061                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
12062
12063         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
12064 }
12065 run_test 154B "verify the ll_decode_linkea tool"
12066
12067 test_154a() {
12068         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12069         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12070         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
12071                 skip "Need MDS version at least 2.2.51"
12072         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
12073
12074         cp /etc/hosts $DIR/$tfile
12075
12076         fid=$($LFS path2fid $DIR/$tfile)
12077         rc=$?
12078         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
12079
12080         dot_lustre_fid_permission_check "$fid" $DIR ||
12081                 error "dot lustre permission check $fid failed"
12082
12083         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
12084
12085         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
12086
12087         touch $MOUNT/.lustre/file &&
12088                 error "creation is not allowed under .lustre"
12089
12090         mkdir $MOUNT/.lustre/dir &&
12091                 error "mkdir is not allowed under .lustre"
12092
12093         rm -rf $DIR/$tfile
12094 }
12095 run_test 154a "Open-by-FID"
12096
12097 test_154b() {
12098         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12099         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12100         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
12101         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
12102                 skip "Need MDS version at least 2.2.51"
12103
12104         local remote_dir=$DIR/$tdir/remote_dir
12105         local MDTIDX=1
12106         local rc=0
12107
12108         mkdir -p $DIR/$tdir
12109         $LFS mkdir -i $MDTIDX $remote_dir ||
12110                 error "create remote directory failed"
12111
12112         cp /etc/hosts $remote_dir/$tfile
12113
12114         fid=$($LFS path2fid $remote_dir/$tfile)
12115         rc=$?
12116         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
12117
12118         dot_lustre_fid_permission_check "$fid" $remote_dir ||
12119                 error "dot lustre permission check $fid failed"
12120         rm -rf $DIR/$tdir
12121 }
12122 run_test 154b "Open-by-FID for remote directory"
12123
12124 test_154c() {
12125         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12126                 skip "Need MDS version at least 2.4.1"
12127
12128         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
12129         local FID1=$($LFS path2fid $DIR/$tfile.1)
12130         local FID2=$($LFS path2fid $DIR/$tfile.2)
12131         local FID3=$($LFS path2fid $DIR/$tfile.3)
12132
12133         local N=1
12134         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
12135                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
12136                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
12137                 local want=FID$N
12138                 [ "$FID" = "${!want}" ] ||
12139                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
12140                 N=$((N + 1))
12141         done
12142
12143         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
12144         do
12145                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
12146                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
12147                 N=$((N + 1))
12148         done
12149 }
12150 run_test 154c "lfs path2fid and fid2path multiple arguments"
12151
12152 test_154d() {
12153         remote_mds_nodsh && skip "remote MDS with nodsh"
12154         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
12155                 skip "Need MDS version at least 2.5.53"
12156
12157         if remote_mds; then
12158                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
12159         else
12160                 nid="0@lo"
12161         fi
12162         local proc_ofile="mdt.*.exports.'$nid'.open_files"
12163         local fd
12164         local cmd
12165
12166         rm -f $DIR/$tfile
12167         touch $DIR/$tfile
12168
12169         local fid=$($LFS path2fid $DIR/$tfile)
12170         # Open the file
12171         fd=$(free_fd)
12172         cmd="exec $fd<$DIR/$tfile"
12173         eval $cmd
12174         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
12175         echo "$fid_list" | grep "$fid"
12176         rc=$?
12177
12178         cmd="exec $fd>/dev/null"
12179         eval $cmd
12180         if [ $rc -ne 0 ]; then
12181                 error "FID $fid not found in open files list $fid_list"
12182         fi
12183 }
12184 run_test 154d "Verify open file fid"
12185
12186 test_154e()
12187 {
12188         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
12189                 skip "Need MDS version at least 2.6.50"
12190
12191         if ls -a $MOUNT | grep -q '^\.lustre$'; then
12192                 error ".lustre returned by readdir"
12193         fi
12194 }
12195 run_test 154e ".lustre is not returned by readdir"
12196
12197 test_154f() {
12198         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12199
12200         # create parent directory on a single MDT to avoid cross-MDT hardlinks
12201         test_mkdir -p -c1 $DIR/$tdir/d
12202         # test dirs inherit from its stripe
12203         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
12204         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
12205         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
12206         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
12207         touch $DIR/f
12208
12209         # get fid of parents
12210         local FID0=$($LFS path2fid $DIR/$tdir/d)
12211         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
12212         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
12213         local FID3=$($LFS path2fid $DIR)
12214
12215         # check that path2fid --parents returns expected <parent_fid>/name
12216         # 1) test for a directory (single parent)
12217         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
12218         [ "$parent" == "$FID0/foo1" ] ||
12219                 error "expected parent: $FID0/foo1, got: $parent"
12220
12221         # 2) test for a file with nlink > 1 (multiple parents)
12222         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
12223         echo "$parent" | grep -F "$FID1/$tfile" ||
12224                 error "$FID1/$tfile not returned in parent list"
12225         echo "$parent" | grep -F "$FID2/link" ||
12226                 error "$FID2/link not returned in parent list"
12227
12228         # 3) get parent by fid
12229         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
12230         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12231         echo "$parent" | grep -F "$FID1/$tfile" ||
12232                 error "$FID1/$tfile not returned in parent list (by fid)"
12233         echo "$parent" | grep -F "$FID2/link" ||
12234                 error "$FID2/link not returned in parent list (by fid)"
12235
12236         # 4) test for entry in root directory
12237         parent=$($LFS path2fid --parents $DIR/f)
12238         echo "$parent" | grep -F "$FID3/f" ||
12239                 error "$FID3/f not returned in parent list"
12240
12241         # 5) test it on root directory
12242         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
12243                 error "$MOUNT should not have parents"
12244
12245         # enable xattr caching and check that linkea is correctly updated
12246         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
12247         save_lustre_params client "llite.*.xattr_cache" > $save
12248         lctl set_param llite.*.xattr_cache 1
12249
12250         # 6.1) linkea update on rename
12251         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
12252
12253         # get parents by fid
12254         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12255         # foo1 should no longer be returned in parent list
12256         echo "$parent" | grep -F "$FID1" &&
12257                 error "$FID1 should no longer be in parent list"
12258         # the new path should appear
12259         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
12260                 error "$FID2/$tfile.moved is not in parent list"
12261
12262         # 6.2) linkea update on unlink
12263         rm -f $DIR/$tdir/d/foo2/link
12264         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12265         # foo2/link should no longer be returned in parent list
12266         echo "$parent" | grep -F "$FID2/link" &&
12267                 error "$FID2/link should no longer be in parent list"
12268         true
12269
12270         rm -f $DIR/f
12271         restore_lustre_params < $save
12272         rm -f $save
12273 }
12274 run_test 154f "get parent fids by reading link ea"
12275
12276 test_154g()
12277 {
12278         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12279         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
12280            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
12281                 skip "Need MDS version at least 2.6.92"
12282
12283         mkdir -p $DIR/$tdir
12284         llapi_fid_test -d $DIR/$tdir
12285 }
12286 run_test 154g "various llapi FID tests"
12287
12288 test_155_small_load() {
12289     local temp=$TMP/$tfile
12290     local file=$DIR/$tfile
12291
12292     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
12293         error "dd of=$temp bs=6096 count=1 failed"
12294     cp $temp $file
12295     cancel_lru_locks $OSC
12296     cmp $temp $file || error "$temp $file differ"
12297
12298     $TRUNCATE $temp 6000
12299     $TRUNCATE $file 6000
12300     cmp $temp $file || error "$temp $file differ (truncate1)"
12301
12302     echo "12345" >>$temp
12303     echo "12345" >>$file
12304     cmp $temp $file || error "$temp $file differ (append1)"
12305
12306     echo "12345" >>$temp
12307     echo "12345" >>$file
12308     cmp $temp $file || error "$temp $file differ (append2)"
12309
12310     rm -f $temp $file
12311     true
12312 }
12313
12314 test_155_big_load() {
12315         remote_ost_nodsh && skip "remote OST with nodsh"
12316
12317         local temp=$TMP/$tfile
12318         local file=$DIR/$tfile
12319
12320         free_min_max
12321         local cache_size=$(do_facet ost$((MAXI+1)) \
12322                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
12323         local large_file_size=$((cache_size * 2))
12324
12325         echo "OSS cache size: $cache_size KB"
12326         echo "Large file size: $large_file_size KB"
12327
12328         [ $MAXV -le $large_file_size ] &&
12329                 skip_env "max available OST size needs > $large_file_size KB"
12330
12331         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
12332
12333         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
12334                 error "dd of=$temp bs=$large_file_size count=1k failed"
12335         cp $temp $file
12336         ls -lh $temp $file
12337         cancel_lru_locks osc
12338         cmp $temp $file || error "$temp $file differ"
12339
12340         rm -f $temp $file
12341         true
12342 }
12343
12344 save_writethrough() {
12345         local facets=$(get_facets OST)
12346
12347         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
12348 }
12349
12350 test_155a() {
12351         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12352
12353         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12354
12355         save_writethrough $p
12356
12357         set_cache read on
12358         set_cache writethrough on
12359         test_155_small_load
12360         restore_lustre_params < $p
12361         rm -f $p
12362 }
12363 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
12364
12365 test_155b() {
12366         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12367
12368         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12369
12370         save_writethrough $p
12371
12372         set_cache read on
12373         set_cache writethrough off
12374         test_155_small_load
12375         restore_lustre_params < $p
12376         rm -f $p
12377 }
12378 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
12379
12380 test_155c() {
12381         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12382
12383         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12384
12385         save_writethrough $p
12386
12387         set_cache read off
12388         set_cache writethrough on
12389         test_155_small_load
12390         restore_lustre_params < $p
12391         rm -f $p
12392 }
12393 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
12394
12395 test_155d() {
12396         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12397
12398         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12399
12400         save_writethrough $p
12401
12402         set_cache read off
12403         set_cache writethrough off
12404         test_155_small_load
12405         restore_lustre_params < $p
12406         rm -f $p
12407 }
12408 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
12409
12410 test_155e() {
12411         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12412
12413         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12414
12415         save_writethrough $p
12416
12417         set_cache read on
12418         set_cache writethrough on
12419         test_155_big_load
12420         restore_lustre_params < $p
12421         rm -f $p
12422 }
12423 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
12424
12425 test_155f() {
12426         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12427
12428         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12429
12430         save_writethrough $p
12431
12432         set_cache read on
12433         set_cache writethrough off
12434         test_155_big_load
12435         restore_lustre_params < $p
12436         rm -f $p
12437 }
12438 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
12439
12440 test_155g() {
12441         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12442
12443         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12444
12445         save_writethrough $p
12446
12447         set_cache read off
12448         set_cache writethrough on
12449         test_155_big_load
12450         restore_lustre_params < $p
12451         rm -f $p
12452 }
12453 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
12454
12455 test_155h() {
12456         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12457
12458         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12459
12460         save_writethrough $p
12461
12462         set_cache read off
12463         set_cache writethrough off
12464         test_155_big_load
12465         restore_lustre_params < $p
12466         rm -f $p
12467 }
12468 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
12469
12470 test_156() {
12471         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12472         remote_ost_nodsh && skip "remote OST with nodsh"
12473         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
12474                 skip "stats not implemented on old servers"
12475         [ "$ost1_FSTYPE" = "zfs" ] &&
12476                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
12477
12478         local CPAGES=3
12479         local BEFORE
12480         local AFTER
12481         local file="$DIR/$tfile"
12482         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12483
12484         save_writethrough $p
12485         roc_hit_init
12486
12487         log "Turn on read and write cache"
12488         set_cache read on
12489         set_cache writethrough on
12490
12491         log "Write data and read it back."
12492         log "Read should be satisfied from the cache."
12493         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12494         BEFORE=$(roc_hit)
12495         cancel_lru_locks osc
12496         cat $file >/dev/null
12497         AFTER=$(roc_hit)
12498         if ! let "AFTER - BEFORE == CPAGES"; then
12499                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12500         else
12501                 log "cache hits:: before: $BEFORE, after: $AFTER"
12502         fi
12503
12504         log "Read again; it should be satisfied from the cache."
12505         BEFORE=$AFTER
12506         cancel_lru_locks osc
12507         cat $file >/dev/null
12508         AFTER=$(roc_hit)
12509         if ! let "AFTER - BEFORE == CPAGES"; then
12510                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12511         else
12512                 log "cache hits:: before: $BEFORE, after: $AFTER"
12513         fi
12514
12515         log "Turn off the read cache and turn on the write cache"
12516         set_cache read off
12517         set_cache writethrough on
12518
12519         log "Read again; it should be satisfied from the cache."
12520         BEFORE=$(roc_hit)
12521         cancel_lru_locks osc
12522         cat $file >/dev/null
12523         AFTER=$(roc_hit)
12524         if ! let "AFTER - BEFORE == CPAGES"; then
12525                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12526         else
12527                 log "cache hits:: before: $BEFORE, after: $AFTER"
12528         fi
12529
12530         log "Read again; it should not be satisfied from the cache."
12531         BEFORE=$AFTER
12532         cancel_lru_locks osc
12533         cat $file >/dev/null
12534         AFTER=$(roc_hit)
12535         if ! let "AFTER - BEFORE == 0"; then
12536                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12537         else
12538                 log "cache hits:: before: $BEFORE, after: $AFTER"
12539         fi
12540
12541         log "Write data and read it back."
12542         log "Read should be satisfied from the cache."
12543         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12544         BEFORE=$(roc_hit)
12545         cancel_lru_locks osc
12546         cat $file >/dev/null
12547         AFTER=$(roc_hit)
12548         if ! let "AFTER - BEFORE == CPAGES"; then
12549                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12550         else
12551                 log "cache hits:: before: $BEFORE, after: $AFTER"
12552         fi
12553
12554         log "Read again; it should not be satisfied from the cache."
12555         BEFORE=$AFTER
12556         cancel_lru_locks osc
12557         cat $file >/dev/null
12558         AFTER=$(roc_hit)
12559         if ! let "AFTER - BEFORE == 0"; then
12560                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12561         else
12562                 log "cache hits:: before: $BEFORE, after: $AFTER"
12563         fi
12564
12565         log "Turn off read and write cache"
12566         set_cache read off
12567         set_cache writethrough off
12568
12569         log "Write data and read it back"
12570         log "It should not be satisfied from the cache."
12571         rm -f $file
12572         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12573         cancel_lru_locks osc
12574         BEFORE=$(roc_hit)
12575         cat $file >/dev/null
12576         AFTER=$(roc_hit)
12577         if ! let "AFTER - BEFORE == 0"; then
12578                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
12579         else
12580                 log "cache hits:: before: $BEFORE, after: $AFTER"
12581         fi
12582
12583         log "Turn on the read cache and turn off the write cache"
12584         set_cache read on
12585         set_cache writethrough off
12586
12587         log "Write data and read it back"
12588         log "It should not be satisfied from the cache."
12589         rm -f $file
12590         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12591         BEFORE=$(roc_hit)
12592         cancel_lru_locks osc
12593         cat $file >/dev/null
12594         AFTER=$(roc_hit)
12595         if ! let "AFTER - BEFORE == 0"; then
12596                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
12597         else
12598                 log "cache hits:: before: $BEFORE, after: $AFTER"
12599         fi
12600
12601         log "Read again; it should be satisfied from the cache."
12602         BEFORE=$(roc_hit)
12603         cancel_lru_locks osc
12604         cat $file >/dev/null
12605         AFTER=$(roc_hit)
12606         if ! let "AFTER - BEFORE == CPAGES"; then
12607                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12608         else
12609                 log "cache hits:: before: $BEFORE, after: $AFTER"
12610         fi
12611
12612         restore_lustre_params < $p
12613         rm -f $p $file
12614 }
12615 run_test 156 "Verification of tunables"
12616
12617 test_160a() {
12618         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12619         remote_mds_nodsh && skip "remote MDS with nodsh"
12620         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
12621                 skip "Need MDS version at least 2.2.0"
12622
12623         changelog_register || error "changelog_register failed"
12624         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
12625         changelog_users $SINGLEMDS | grep -q $cl_user ||
12626                 error "User $cl_user not found in changelog_users"
12627
12628         # change something
12629         test_mkdir -p $DIR/$tdir/pics/2008/zachy
12630         changelog_clear 0 || error "changelog_clear failed"
12631         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
12632         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
12633         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
12634         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
12635         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
12636         rm $DIR/$tdir/pics/desktop.jpg
12637
12638         changelog_dump | tail -10
12639
12640         echo "verifying changelog mask"
12641         changelog_chmask "-MKDIR"
12642         changelog_chmask "-CLOSE"
12643
12644         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
12645         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
12646
12647         changelog_chmask "+MKDIR"
12648         changelog_chmask "+CLOSE"
12649
12650         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
12651         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
12652
12653         changelog_dump | tail -10
12654         MKDIRS=$(changelog_dump | grep -c "MKDIR")
12655         CLOSES=$(changelog_dump | grep -c "CLOSE")
12656         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
12657         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
12658
12659         # verify contents
12660         echo "verifying target fid"
12661         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
12662         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
12663         [ "$fidc" == "$fidf" ] ||
12664                 error "changelog '$tfile' fid $fidc != file fid $fidf"
12665         echo "verifying parent fid"
12666         # The FID returned from the Changelog may be the directory shard on
12667         # a different MDT, and not the FID returned by path2fid on the parent.
12668         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
12669         # since this is what will matter when recreating this file in the tree.
12670         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
12671         local pathp=$($LFS fid2path $MOUNT "$fidp")
12672         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
12673                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
12674
12675         echo "getting records for $cl_user"
12676         changelog_users $SINGLEMDS
12677         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
12678         local nclr=3
12679         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
12680                 error "changelog_clear failed"
12681         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
12682         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
12683         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
12684                 error "user index expect $user_rec1 + $nclr != $user_rec2"
12685
12686         local min0_rec=$(changelog_users $SINGLEMDS |
12687                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
12688         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
12689                           awk '{ print $1; exit; }')
12690
12691         changelog_dump | tail -n 5
12692         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
12693         [ $first_rec == $((min0_rec + 1)) ] ||
12694                 error "first index should be $min0_rec + 1 not $first_rec"
12695
12696         # LU-3446 changelog index reset on MDT restart
12697         local cur_rec1=$(changelog_users $SINGLEMDS |
12698                          awk '/^current.index:/ { print $NF }')
12699         changelog_clear 0 ||
12700                 error "clear all changelog records for $cl_user failed"
12701         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
12702         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
12703                 error "Fail to start $SINGLEMDS"
12704         local cur_rec2=$(changelog_users $SINGLEMDS |
12705                          awk '/^current.index:/ { print $NF }')
12706         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
12707         [ $cur_rec1 == $cur_rec2 ] ||
12708                 error "current index should be $cur_rec1 not $cur_rec2"
12709
12710         echo "verifying users from this test are deregistered"
12711         changelog_deregister || error "changelog_deregister failed"
12712         changelog_users $SINGLEMDS | grep -q $cl_user &&
12713                 error "User '$cl_user' still in changelog_users"
12714
12715         # lctl get_param -n mdd.*.changelog_users
12716         # current index: 144
12717         # ID    index (idle seconds)
12718         # cl3   144 (2)
12719         if ! changelog_users $SINGLEMDS | grep "^cl"; then
12720                 # this is the normal case where all users were deregistered
12721                 # make sure no new records are added when no users are present
12722                 local last_rec1=$(changelog_users $SINGLEMDS |
12723                                   awk '/^current.index:/ { print $NF }')
12724                 touch $DIR/$tdir/chloe
12725                 local last_rec2=$(changelog_users $SINGLEMDS |
12726                                   awk '/^current.index:/ { print $NF }')
12727                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
12728                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
12729         else
12730                 # any changelog users must be leftovers from a previous test
12731                 changelog_users $SINGLEMDS
12732                 echo "other changelog users; can't verify off"
12733         fi
12734 }
12735 run_test 160a "changelog sanity"
12736
12737 test_160b() { # LU-3587
12738         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12739         remote_mds_nodsh && skip "remote MDS with nodsh"
12740         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
12741                 skip "Need MDS version at least 2.2.0"
12742
12743         changelog_register || error "changelog_register failed"
12744         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
12745         changelog_users $SINGLEMDS | grep -q $cl_user ||
12746                 error "User '$cl_user' not found in changelog_users"
12747
12748         local longname1=$(str_repeat a 255)
12749         local longname2=$(str_repeat b 255)
12750
12751         cd $DIR
12752         echo "creating very long named file"
12753         touch $longname1 || error "create of '$longname1' failed"
12754         echo "renaming very long named file"
12755         mv $longname1 $longname2
12756
12757         changelog_dump | grep RENME | tail -n 5
12758         rm -f $longname2
12759 }
12760 run_test 160b "Verify that very long rename doesn't crash in changelog"
12761
12762 test_160c() {
12763         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12764         remote_mds_nodsh && skip "remote MDS with nodsh"
12765
12766         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
12767                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
12768                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
12769                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
12770
12771         local rc=0
12772
12773         # Registration step
12774         changelog_register || error "changelog_register failed"
12775
12776         rm -rf $DIR/$tdir
12777         mkdir -p $DIR/$tdir
12778         $MCREATE $DIR/$tdir/foo_160c
12779         changelog_chmask "-TRUNC"
12780         $TRUNCATE $DIR/$tdir/foo_160c 200
12781         changelog_chmask "+TRUNC"
12782         $TRUNCATE $DIR/$tdir/foo_160c 199
12783         changelog_dump | tail -n 5
12784         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
12785         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
12786 }
12787 run_test 160c "verify that changelog log catch the truncate event"
12788
12789 test_160d() {
12790         remote_mds_nodsh && skip "remote MDS with nodsh"
12791         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
12792         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12793         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
12794                 skip "Need MDS version at least 2.7.60"
12795
12796         # Registration step
12797         changelog_register || error "changelog_register failed"
12798
12799         mkdir -p $DIR/$tdir/migrate_dir
12800         changelog_clear 0 || error "changelog_clear failed"
12801
12802         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
12803         changelog_dump | tail -n 5
12804         local migrates=$(changelog_dump | grep -c "MIGRT")
12805         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
12806 }
12807 run_test 160d "verify that changelog log catch the migrate event"
12808
12809 test_160e() {
12810         remote_mds_nodsh && skip "remote MDS with nodsh"
12811
12812         # Create a user
12813         changelog_register || error "changelog_register failed"
12814
12815         # Delete a future user (expect fail)
12816         local MDT0=$(facet_svc $SINGLEMDS)
12817         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
12818         local rc=$?
12819
12820         if [ $rc -eq 0 ]; then
12821                 error "Deleted non-existant user cl77"
12822         elif [ $rc -ne 2 ]; then
12823                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
12824         fi
12825
12826         # Clear to a bad index (1 billion should be safe)
12827         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
12828         rc=$?
12829
12830         if [ $rc -eq 0 ]; then
12831                 error "Successfully cleared to invalid CL index"
12832         elif [ $rc -ne 22 ]; then
12833                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
12834         fi
12835 }
12836 run_test 160e "changelog negative testing (should return errors)"
12837
12838 test_160f() {
12839         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12840         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
12841                 skip "Need MDS version at least 2.10.56"
12842
12843         local mdts=$(comma_list $(mdts_nodes))
12844
12845         # Create a user
12846         changelog_register || error "first changelog_register failed"
12847         changelog_register || error "second changelog_register failed"
12848         local cl_users
12849         declare -A cl_user1
12850         declare -A cl_user2
12851         local user_rec1
12852         local user_rec2
12853         local i
12854
12855         # generate some changelog records to accumulate on each MDT
12856         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
12857         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
12858                 error "create $DIR/$tdir/$tfile failed"
12859
12860         # check changelogs have been generated
12861         local nbcl=$(changelog_dump | wc -l)
12862         [[ $nbcl -eq 0 ]] && error "no changelogs found"
12863
12864         for param in "changelog_max_idle_time=10" \
12865                      "changelog_gc=1" \
12866                      "changelog_min_gc_interval=2" \
12867                      "changelog_min_free_cat_entries=3"; do
12868                 local MDT0=$(facet_svc $SINGLEMDS)
12869                 local var="${param%=*}"
12870                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
12871
12872                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
12873                 do_nodes $mdts $LCTL set_param mdd.*.$param
12874         done
12875
12876         # force cl_user2 to be idle (1st part)
12877         sleep 9
12878
12879         # simulate changelog catalog almost full
12880         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
12881         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
12882
12883         for i in $(seq $MDSCOUNT); do
12884                 cl_users=(${CL_USERS[mds$i]})
12885                 cl_user1[mds$i]="${cl_users[0]}"
12886                 cl_user2[mds$i]="${cl_users[1]}"
12887
12888                 [ -n "${cl_user1[mds$i]}" ] ||
12889                         error "mds$i: no user registered"
12890                 [ -n "${cl_user2[mds$i]}" ] ||
12891                         error "mds$i: only ${cl_user2[mds$i]} is registered"
12892
12893                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12894                 [ -n "$user_rec1" ] ||
12895                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12896                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
12897                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12898                 [ -n "$user_rec2" ] ||
12899                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12900                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
12901                      "$user_rec1 + 2 == $user_rec2"
12902                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
12903                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
12904                               "$user_rec1 + 2, but is $user_rec2"
12905                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
12906                 [ -n "$user_rec2" ] ||
12907                         error "mds$i: User ${cl_user2[mds$i]} not registered"
12908                 [ $user_rec1 == $user_rec2 ] ||
12909                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
12910                               "$user_rec1, but is $user_rec2"
12911         done
12912
12913         # force cl_user2 to be idle (2nd part) and to reach
12914         # changelog_max_idle_time
12915         sleep 2
12916
12917         # generate one more changelog to trigger fail_loc
12918         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
12919                 error "create $DIR/$tdir/${tfile}bis failed"
12920
12921         # ensure gc thread is done
12922         for i in $(mdts_nodes); do
12923                 wait_update $i \
12924                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
12925                         error "$i: GC-thread not done"
12926         done
12927
12928         local first_rec
12929         for i in $(seq $MDSCOUNT); do
12930                 # check cl_user1 still registered
12931                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
12932                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12933                 # check cl_user2 unregistered
12934                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
12935                         error "mds$i: User ${cl_user2[mds$i]} still registered"
12936
12937                 # check changelogs are present and starting at $user_rec1 + 1
12938                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12939                 [ -n "$user_rec1" ] ||
12940                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12941                 first_rec=$($LFS changelog $(facet_svc mds$i) |
12942                             awk '{ print $1; exit; }')
12943
12944                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
12945                 [ $((user_rec1 + 1)) == $first_rec ] ||
12946                         error "mds$i: first index should be $user_rec1 + 1, " \
12947                               "but is $first_rec"
12948         done
12949 }
12950 run_test 160f "changelog garbage collect (timestamped users)"
12951
12952 test_160g() {
12953         remote_mds_nodsh && skip "remote MDS with nodsh"
12954         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
12955                 skip "Need MDS version at least 2.10.56"
12956
12957         local mdts=$(comma_list $(mdts_nodes))
12958
12959         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
12960         do_nodes $mdts $LCTL set_param fail_loc=0x1314
12961
12962         # Create a user
12963         changelog_register || error "first changelog_register failed"
12964         changelog_register || error "second changelog_register failed"
12965         local cl_users
12966         declare -A cl_user1
12967         declare -A cl_user2
12968         local user_rec1
12969         local user_rec2
12970         local i
12971
12972         # generate some changelog records to accumulate on each MDT
12973         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
12974         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
12975                 error "create $DIR/$tdir/$tfile failed"
12976
12977         # check changelogs have been generated
12978         local nbcl=$(changelog_dump | wc -l)
12979         [[ $nbcl -eq 0 ]] && error "no changelogs found"
12980
12981         # reduce the max_idle_indexes value to make sure we exceed it
12982         max_ndx=$((nbcl / 2 - 1))
12983
12984         for param in "changelog_max_idle_indexes=$max_ndx" \
12985                      "changelog_gc=1" \
12986                      "changelog_min_gc_interval=2" \
12987                      "changelog_min_free_cat_entries=3"; do
12988                 local MDT0=$(facet_svc $SINGLEMDS)
12989                 local var="${param%=*}"
12990                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
12991
12992                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
12993                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
12994                         error "unable to set mdd.*.$param"
12995         done
12996
12997         # simulate changelog catalog almost full
12998         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
12999         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13000
13001         for i in $(seq $MDSCOUNT); do
13002                 cl_users=(${CL_USERS[mds$i]})
13003                 cl_user1[mds$i]="${cl_users[0]}"
13004                 cl_user2[mds$i]="${cl_users[1]}"
13005
13006                 [ -n "${cl_user1[mds$i]}" ] ||
13007                         error "mds$i: no user registered"
13008                 [ -n "${cl_user2[mds$i]}" ] ||
13009                         error "mds$i: only ${cl_user1[mds$i]} is registered"
13010
13011                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13012                 [ -n "$user_rec1" ] ||
13013                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13014                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13015                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13016                 [ -n "$user_rec2" ] ||
13017                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13018                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13019                      "$user_rec1 + 2 == $user_rec2"
13020                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13021                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13022                               "$user_rec1 + 2, but is $user_rec2"
13023                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13024                 [ -n "$user_rec2" ] ||
13025                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13026                 [ $user_rec1 == $user_rec2 ] ||
13027                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13028                               "$user_rec1, but is $user_rec2"
13029         done
13030
13031         # ensure we are past the previous changelog_min_gc_interval set above
13032         sleep 2
13033
13034         # generate one more changelog to trigger fail_loc
13035         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13036                 error "create $DIR/$tdir/${tfile}bis failed"
13037
13038         # ensure gc thread is done
13039         for i in $(mdts_nodes); do
13040                 wait_update $i \
13041                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13042                         error "$i: GC-thread not done"
13043         done
13044
13045         local first_rec
13046         for i in $(seq $MDSCOUNT); do
13047                 # check cl_user1 still registered
13048                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13049                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13050                 # check cl_user2 unregistered
13051                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13052                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13053
13054                 # check changelogs are present and starting at $user_rec1 + 1
13055                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13056                 [ -n "$user_rec1" ] ||
13057                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13058                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13059                             awk '{ print $1; exit; }')
13060
13061                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13062                 [ $((user_rec1 + 1)) == $first_rec ] ||
13063                         error "mds$i: first index should be $user_rec1 + 1, " \
13064                               "but is $first_rec"
13065         done
13066 }
13067 run_test 160g "changelog garbage collect (old users)"
13068
13069 test_160h() {
13070         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13071         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13072                 skip "Need MDS version at least 2.10.56"
13073
13074         local mdts=$(comma_list $(mdts_nodes))
13075
13076         # Create a user
13077         changelog_register || error "first changelog_register failed"
13078         changelog_register || error "second changelog_register failed"
13079         local cl_users
13080         declare -A cl_user1
13081         declare -A cl_user2
13082         local user_rec1
13083         local user_rec2
13084         local i
13085
13086         # generate some changelog records to accumulate on each MDT
13087         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
13088         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13089                 error "create $DIR/$tdir/$tfile failed"
13090
13091         # check changelogs have been generated
13092         local nbcl=$(changelog_dump | wc -l)
13093         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13094
13095         for param in "changelog_max_idle_time=10" \
13096                      "changelog_gc=1" \
13097                      "changelog_min_gc_interval=2"; do
13098                 local MDT0=$(facet_svc $SINGLEMDS)
13099                 local var="${param%=*}"
13100                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13101
13102                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13103                 do_nodes $mdts $LCTL set_param mdd.*.$param
13104         done
13105
13106         # force cl_user2 to be idle (1st part)
13107         sleep 9
13108
13109         for i in $(seq $MDSCOUNT); do
13110                 cl_users=(${CL_USERS[mds$i]})
13111                 cl_user1[mds$i]="${cl_users[0]}"
13112                 cl_user2[mds$i]="${cl_users[1]}"
13113
13114                 [ -n "${cl_user1[mds$i]}" ] ||
13115                         error "mds$i: no user registered"
13116                 [ -n "${cl_user2[mds$i]}" ] ||
13117                         error "mds$i: only ${cl_user2[mds$i]} is registered"
13118
13119                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13120                 [ -n "$user_rec1" ] ||
13121                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13122                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13123                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13124                 [ -n "$user_rec2" ] ||
13125                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13126                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13127                      "$user_rec1 + 2 == $user_rec2"
13128                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13129                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13130                               "$user_rec1 + 2, but is $user_rec2"
13131                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13132                 [ -n "$user_rec2" ] ||
13133                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13134                 [ $user_rec1 == $user_rec2 ] ||
13135                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13136                               "$user_rec1, but is $user_rec2"
13137         done
13138
13139         # force cl_user2 to be idle (2nd part) and to reach
13140         # changelog_max_idle_time
13141         sleep 2
13142
13143         # force each GC-thread start and block then
13144         # one per MDT/MDD, set fail_val accordingly
13145         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
13146         do_nodes $mdts $LCTL set_param fail_loc=0x1316
13147
13148         # generate more changelogs to trigger fail_loc
13149         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13150                 error "create $DIR/$tdir/${tfile}bis failed"
13151
13152         # stop MDT to stop GC-thread, should be done in back-ground as it will
13153         # block waiting for the thread to be released and exit
13154         declare -A stop_pids
13155         for i in $(seq $MDSCOUNT); do
13156                 stop mds$i &
13157                 stop_pids[mds$i]=$!
13158         done
13159
13160         for i in $(mdts_nodes); do
13161                 local facet
13162                 local nb=0
13163                 local facets=$(facets_up_on_host $i)
13164
13165                 for facet in ${facets//,/ }; do
13166                         if [[ $facet == mds* ]]; then
13167                                 nb=$((nb + 1))
13168                         fi
13169                 done
13170                 # ensure each MDS's gc threads are still present and all in "R"
13171                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
13172                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
13173                         error "$i: expected $nb GC-thread"
13174                 wait_update $i \
13175                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
13176                         "R" 20 ||
13177                         error "$i: GC-thread not found in R-state"
13178                 # check umounts of each MDT on MDS have reached kthread_stop()
13179                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
13180                         error "$i: expected $nb umount"
13181                 wait_update $i \
13182                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
13183                         error "$i: umount not found in D-state"
13184         done
13185
13186         # release all GC-threads
13187         do_nodes $mdts $LCTL set_param fail_loc=0
13188
13189         # wait for MDT stop to complete
13190         for i in $(seq $MDSCOUNT); do
13191                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
13192         done
13193
13194         # XXX
13195         # may try to check if any orphan changelog records are present
13196         # via ldiskfs/zfs and llog_reader...
13197
13198         # re-start/mount MDTs
13199         for i in $(seq $MDSCOUNT); do
13200                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
13201                         error "Fail to start mds$i"
13202         done
13203
13204         local first_rec
13205         for i in $(seq $MDSCOUNT); do
13206                 # check cl_user1 still registered
13207                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13208                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13209                 # check cl_user2 unregistered
13210                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13211                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13212
13213                 # check changelogs are present and starting at $user_rec1 + 1
13214                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13215                 [ -n "$user_rec1" ] ||
13216                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13217                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13218                             awk '{ print $1; exit; }')
13219
13220                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13221                 [ $((user_rec1 + 1)) == $first_rec ] ||
13222                         error "mds$i: first index should be $user_rec1 + 1, " \
13223                               "but is $first_rec"
13224         done
13225 }
13226 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
13227               "during mount"
13228
13229 test_160i() {
13230
13231         local mdts=$(comma_list $(mdts_nodes))
13232
13233         changelog_register || error "first changelog_register failed"
13234
13235         # generate some changelog records to accumulate on each MDT
13236         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13237         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13238                 error "create $DIR/$tdir/$tfile failed"
13239
13240         # check changelogs have been generated
13241         local nbcl=$(changelog_dump | wc -l)
13242         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13243
13244         # simulate race between register and unregister
13245         # XXX as fail_loc is set per-MDS, with DNE configs the race
13246         # simulation will only occur for one MDT per MDS and for the
13247         # others the normal race scenario will take place
13248         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
13249         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
13250         do_nodes $mdts $LCTL set_param fail_val=1
13251
13252         # unregister 1st user
13253         changelog_deregister &
13254         local pid1=$!
13255         # wait some time for deregister work to reach race rdv
13256         sleep 2
13257         # register 2nd user
13258         changelog_register || error "2nd user register failed"
13259
13260         wait $pid1 || error "1st user deregister failed"
13261
13262         local i
13263         local last_rec
13264         declare -A LAST_REC
13265         for i in $(seq $MDSCOUNT); do
13266                 if changelog_users mds$i | grep "^cl"; then
13267                         # make sure new records are added with one user present
13268                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
13269                                           awk '/^current.index:/ { print $NF }')
13270                 else
13271                         error "mds$i has no user registered"
13272                 fi
13273         done
13274
13275         # generate more changelog records to accumulate on each MDT
13276         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13277                 error "create $DIR/$tdir/${tfile}bis failed"
13278
13279         for i in $(seq $MDSCOUNT); do
13280                 last_rec=$(changelog_users $SINGLEMDS |
13281                            awk '/^current.index:/ { print $NF }')
13282                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
13283                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
13284                         error "changelogs are off on mds$i"
13285         done
13286 }
13287 run_test 160i "changelog user register/unregister race"
13288
13289 test_161a() {
13290         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13291
13292         test_mkdir -c1 $DIR/$tdir
13293         cp /etc/hosts $DIR/$tdir/$tfile
13294         test_mkdir -c1 $DIR/$tdir/foo1
13295         test_mkdir -c1 $DIR/$tdir/foo2
13296         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
13297         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
13298         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
13299         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
13300         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
13301         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
13302                 $LFS fid2path $DIR $FID
13303                 error "bad link ea"
13304         fi
13305         # middle
13306         rm $DIR/$tdir/foo2/zachary
13307         # last
13308         rm $DIR/$tdir/foo2/thor
13309         # first
13310         rm $DIR/$tdir/$tfile
13311         # rename
13312         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
13313         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
13314                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
13315         rm $DIR/$tdir/foo2/maggie
13316
13317         # overflow the EA
13318         local longname=$tfile.avg_len_is_thirty_two_
13319         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
13320                 error_noexit 'failed to unlink many hardlinks'" EXIT
13321         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
13322                 error "failed to hardlink many files"
13323         links=$($LFS fid2path $DIR $FID | wc -l)
13324         echo -n "${links}/1000 links in link EA"
13325         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
13326 }
13327 run_test 161a "link ea sanity"
13328
13329 test_161b() {
13330         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13331         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
13332
13333         local MDTIDX=1
13334         local remote_dir=$DIR/$tdir/remote_dir
13335
13336         mkdir -p $DIR/$tdir
13337         $LFS mkdir -i $MDTIDX $remote_dir ||
13338                 error "create remote directory failed"
13339
13340         cp /etc/hosts $remote_dir/$tfile
13341         mkdir -p $remote_dir/foo1
13342         mkdir -p $remote_dir/foo2
13343         ln $remote_dir/$tfile $remote_dir/foo1/sofia
13344         ln $remote_dir/$tfile $remote_dir/foo2/zachary
13345         ln $remote_dir/$tfile $remote_dir/foo1/luna
13346         ln $remote_dir/$tfile $remote_dir/foo2/thor
13347
13348         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
13349                      tr -d ']')
13350         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
13351                 $LFS fid2path $DIR $FID
13352                 error "bad link ea"
13353         fi
13354         # middle
13355         rm $remote_dir/foo2/zachary
13356         # last
13357         rm $remote_dir/foo2/thor
13358         # first
13359         rm $remote_dir/$tfile
13360         # rename
13361         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
13362         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
13363         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
13364                 $LFS fid2path $DIR $FID
13365                 error "bad link rename"
13366         fi
13367         rm $remote_dir/foo2/maggie
13368
13369         # overflow the EA
13370         local longname=filename_avg_len_is_thirty_two_
13371         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
13372                 error "failed to hardlink many files"
13373         links=$($LFS fid2path $DIR $FID | wc -l)
13374         echo -n "${links}/1000 links in link EA"
13375         [[ ${links} -gt 60 ]] ||
13376                 error "expected at least 60 links in link EA"
13377         unlinkmany $remote_dir/foo2/$longname 1000 ||
13378         error "failed to unlink many hardlinks"
13379 }
13380 run_test 161b "link ea sanity under remote directory"
13381
13382 test_161c() {
13383         remote_mds_nodsh && skip "remote MDS with nodsh"
13384         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13385         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
13386                 skip "Need MDS version at least 2.1.5"
13387
13388         # define CLF_RENAME_LAST 0x0001
13389         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
13390         changelog_register || error "changelog_register failed"
13391
13392         rm -rf $DIR/$tdir
13393         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
13394         touch $DIR/$tdir/foo_161c
13395         touch $DIR/$tdir/bar_161c
13396         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
13397         changelog_dump | grep RENME | tail -n 5
13398         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
13399         changelog_clear 0 || error "changelog_clear failed"
13400         if [ x$flags != "x0x1" ]; then
13401                 error "flag $flags is not 0x1"
13402         fi
13403
13404         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
13405         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
13406         touch $DIR/$tdir/foo_161c
13407         touch $DIR/$tdir/bar_161c
13408         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
13409         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
13410         changelog_dump | grep RENME | tail -n 5
13411         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
13412         changelog_clear 0 || error "changelog_clear failed"
13413         if [ x$flags != "x0x0" ]; then
13414                 error "flag $flags is not 0x0"
13415         fi
13416         echo "rename overwrite a target having nlink > 1," \
13417                 "changelog record has flags of $flags"
13418
13419         # rename doesn't overwrite a target (changelog flag 0x0)
13420         touch $DIR/$tdir/foo_161c
13421         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
13422         changelog_dump | grep RENME | tail -n 5
13423         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
13424         changelog_clear 0 || error "changelog_clear failed"
13425         if [ x$flags != "x0x0" ]; then
13426                 error "flag $flags is not 0x0"
13427         fi
13428         echo "rename doesn't overwrite a target," \
13429                 "changelog record has flags of $flags"
13430
13431         # define CLF_UNLINK_LAST 0x0001
13432         # unlink a file having nlink = 1 (changelog flag 0x1)
13433         rm -f $DIR/$tdir/foo2_161c
13434         changelog_dump | grep UNLNK | tail -n 5
13435         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
13436         changelog_clear 0 || error "changelog_clear failed"
13437         if [ x$flags != "x0x1" ]; then
13438                 error "flag $flags is not 0x1"
13439         fi
13440         echo "unlink a file having nlink = 1," \
13441                 "changelog record has flags of $flags"
13442
13443         # unlink a file having nlink > 1 (changelog flag 0x0)
13444         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
13445         rm -f $DIR/$tdir/foobar_161c
13446         changelog_dump | grep UNLNK | tail -n 5
13447         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
13448         changelog_clear 0 || error "changelog_clear failed"
13449         if [ x$flags != "x0x0" ]; then
13450                 error "flag $flags is not 0x0"
13451         fi
13452         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
13453 }
13454 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
13455
13456 test_161d() {
13457         remote_mds_nodsh && skip "remote MDS with nodsh"
13458         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
13459
13460         local pid
13461         local fid
13462
13463         changelog_register || error "changelog_register failed"
13464
13465         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
13466         # interfer with $MOUNT/.lustre/fid/ access
13467         mkdir $DIR/$tdir
13468         [[ $? -eq 0 ]] || error "mkdir failed"
13469
13470         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
13471         $LCTL set_param fail_loc=0x8000140c
13472         # 5s pause
13473         $LCTL set_param fail_val=5
13474
13475         # create file
13476         echo foofoo > $DIR/$tdir/$tfile &
13477         pid=$!
13478
13479         # wait for create to be delayed
13480         sleep 2
13481
13482         ps -p $pid
13483         [[ $? -eq 0 ]] || error "create should be blocked"
13484
13485         local tempfile=$(mktemp)
13486         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
13487         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
13488         # some delay may occur during ChangeLog publishing and file read just
13489         # above, that could allow file write to happen finally
13490         [[ -s $tempfile ]] && echo "file should be empty"
13491
13492         $LCTL set_param fail_loc=0
13493
13494         wait $pid
13495         [[ $? -eq 0 ]] || error "create failed"
13496 }
13497 run_test 161d "create with concurrent .lustre/fid access"
13498
13499 check_path() {
13500         local expected="$1"
13501         shift
13502         local fid="$2"
13503
13504         local path
13505         path=$($LFS fid2path "$@")
13506         local rc=$?
13507
13508         if [ $rc -ne 0 ]; then
13509                 error "path looked up of '$expected' failed: rc=$rc"
13510         elif [ "$path" != "$expected" ]; then
13511                 error "path looked up '$path' instead of '$expected'"
13512         else
13513                 echo "FID '$fid' resolves to path '$path' as expected"
13514         fi
13515 }
13516
13517 test_162a() { # was test_162
13518         test_mkdir -p -c1 $DIR/$tdir/d2
13519         touch $DIR/$tdir/d2/$tfile
13520         touch $DIR/$tdir/d2/x1
13521         touch $DIR/$tdir/d2/x2
13522         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
13523         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
13524         # regular file
13525         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
13526         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
13527
13528         # softlink
13529         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
13530         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
13531         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
13532
13533         # softlink to wrong file
13534         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
13535         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
13536         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
13537
13538         # hardlink
13539         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
13540         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
13541         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
13542         # fid2path dir/fsname should both work
13543         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
13544         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
13545
13546         # hardlink count: check that there are 2 links
13547         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
13548         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
13549
13550         # hardlink indexing: remove the first link
13551         rm $DIR/$tdir/d2/p/q/r/hlink
13552         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
13553 }
13554 run_test 162a "path lookup sanity"
13555
13556 test_162b() {
13557         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13558         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13559
13560         mkdir $DIR/$tdir
13561         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
13562                                 error "create striped dir failed"
13563
13564         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
13565                                         tail -n 1 | awk '{print $2}')
13566         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
13567
13568         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
13569         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
13570
13571         # regular file
13572         for ((i=0;i<5;i++)); do
13573                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
13574                         error "get fid for f$i failed"
13575                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
13576
13577                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
13578                         error "get fid for d$i failed"
13579                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
13580         done
13581
13582         return 0
13583 }
13584 run_test 162b "striped directory path lookup sanity"
13585
13586 # LU-4239: Verify fid2path works with paths 100 or more directories deep
13587 test_162c() {
13588         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
13589                 skip "Need MDS version at least 2.7.51"
13590
13591         local lpath=$tdir.local
13592         local rpath=$tdir.remote
13593
13594         test_mkdir $DIR/$lpath
13595         test_mkdir $DIR/$rpath
13596
13597         for ((i = 0; i <= 101; i++)); do
13598                 lpath="$lpath/$i"
13599                 mkdir $DIR/$lpath
13600                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
13601                         error "get fid for local directory $DIR/$lpath failed"
13602                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
13603
13604                 rpath="$rpath/$i"
13605                 test_mkdir $DIR/$rpath
13606                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
13607                         error "get fid for remote directory $DIR/$rpath failed"
13608                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
13609         done
13610
13611         return 0
13612 }
13613 run_test 162c "fid2path works with paths 100 or more directories deep"
13614
13615 test_169() {
13616         # do directio so as not to populate the page cache
13617         log "creating a 10 Mb file"
13618         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
13619         log "starting reads"
13620         dd if=$DIR/$tfile of=/dev/null bs=4096 &
13621         log "truncating the file"
13622         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
13623         log "killing dd"
13624         kill %+ || true # reads might have finished
13625         echo "wait until dd is finished"
13626         wait
13627         log "removing the temporary file"
13628         rm -rf $DIR/$tfile || error "tmp file removal failed"
13629 }
13630 run_test 169 "parallel read and truncate should not deadlock"
13631
13632 test_170() {
13633         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13634
13635         $LCTL clear     # bug 18514
13636         $LCTL debug_daemon start $TMP/${tfile}_log_good
13637         touch $DIR/$tfile
13638         $LCTL debug_daemon stop
13639         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
13640                 error "sed failed to read log_good"
13641
13642         $LCTL debug_daemon start $TMP/${tfile}_log_good
13643         rm -rf $DIR/$tfile
13644         $LCTL debug_daemon stop
13645
13646         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
13647                error "lctl df log_bad failed"
13648
13649         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
13650         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
13651
13652         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
13653         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
13654
13655         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
13656                 error "bad_line good_line1 good_line2 are empty"
13657
13658         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
13659         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
13660         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
13661
13662         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
13663         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
13664         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
13665
13666         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
13667                 error "bad_line_new good_line_new are empty"
13668
13669         local expected_good=$((good_line1 + good_line2*2))
13670
13671         rm -f $TMP/${tfile}*
13672         # LU-231, short malformed line may not be counted into bad lines
13673         if [ $bad_line -ne $bad_line_new ] &&
13674                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
13675                 error "expected $bad_line bad lines, but got $bad_line_new"
13676                 return 1
13677         fi
13678
13679         if [ $expected_good -ne $good_line_new ]; then
13680                 error "expected $expected_good good lines, but got $good_line_new"
13681                 return 2
13682         fi
13683         true
13684 }
13685 run_test 170 "test lctl df to handle corrupted log ====================="
13686
13687 test_171() { # bug20592
13688         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13689
13690         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
13691         $LCTL set_param fail_loc=0x50e
13692         $LCTL set_param fail_val=3000
13693         multiop_bg_pause $DIR/$tfile O_s || true
13694         local MULTIPID=$!
13695         kill -USR1 $MULTIPID
13696         # cause log dump
13697         sleep 3
13698         wait $MULTIPID
13699         if dmesg | grep "recursive fault"; then
13700                 error "caught a recursive fault"
13701         fi
13702         $LCTL set_param fail_loc=0
13703         true
13704 }
13705 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
13706
13707 # it would be good to share it with obdfilter-survey/iokit-libecho code
13708 setup_obdecho_osc () {
13709         local rc=0
13710         local ost_nid=$1
13711         local obdfilter_name=$2
13712         echo "Creating new osc for $obdfilter_name on $ost_nid"
13713         # make sure we can find loopback nid
13714         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
13715
13716         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
13717                            ${obdfilter_name}_osc_UUID || rc=2; }
13718         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
13719                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
13720         return $rc
13721 }
13722
13723 cleanup_obdecho_osc () {
13724         local obdfilter_name=$1
13725         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
13726         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
13727         return 0
13728 }
13729
13730 obdecho_test() {
13731         local OBD=$1
13732         local node=$2
13733         local pages=${3:-64}
13734         local rc=0
13735         local id
13736
13737         local count=10
13738         local obd_size=$(get_obd_size $node $OBD)
13739         local page_size=$(get_page_size $node)
13740         if [[ -n "$obd_size" ]]; then
13741                 local new_count=$((obd_size / (pages * page_size / 1024)))
13742                 [[ $new_count -ge $count ]] || count=$new_count
13743         fi
13744
13745         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
13746         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
13747                            rc=2; }
13748         if [ $rc -eq 0 ]; then
13749             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
13750             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
13751         fi
13752         echo "New object id is $id"
13753         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
13754                            rc=4; }
13755         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
13756                            "test_brw $count w v $pages $id" || rc=4; }
13757         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
13758                            rc=4; }
13759         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
13760                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
13761         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
13762                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
13763         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
13764         return $rc
13765 }
13766
13767 test_180a() {
13768         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13769
13770         if ! module_loaded obdecho; then
13771                 load_module obdecho/obdecho &&
13772                         stack_trap "rmmod obdecho" EXIT ||
13773                         error "unable to load obdecho on client"
13774         fi
13775
13776         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
13777         local host=$($LCTL get_param -n osc.$osc.import |
13778                      awk '/current_connection:/ { print $2 }' )
13779         local target=$($LCTL get_param -n osc.$osc.import |
13780                        awk '/target:/ { print $2 }' )
13781         target=${target%_UUID}
13782
13783         if [ -n "$target" ]; then
13784                 setup_obdecho_osc $host $target &&
13785                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
13786                         { error "obdecho setup failed with $?"; return; }
13787
13788                 obdecho_test ${target}_osc client ||
13789                         error "obdecho_test failed on ${target}_osc"
13790         else
13791                 $LCTL get_param osc.$osc.import
13792                 error "there is no osc.$osc.import target"
13793         fi
13794 }
13795 run_test 180a "test obdecho on osc"
13796
13797 test_180b() {
13798         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13799         remote_ost_nodsh && skip "remote OST with nodsh"
13800
13801         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
13802                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
13803                 error "failed to load module obdecho"
13804
13805         local target=$(do_facet ost1 $LCTL dl |
13806                        awk '/obdfilter/ { print $4; exit; }')
13807
13808         if [ -n "$target" ]; then
13809                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
13810         else
13811                 do_facet ost1 $LCTL dl
13812                 error "there is no obdfilter target on ost1"
13813         fi
13814 }
13815 run_test 180b "test obdecho directly on obdfilter"
13816
13817 test_180c() { # LU-2598
13818         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13819         remote_ost_nodsh && skip "remote OST with nodsh"
13820         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
13821                 skip "Need MDS version at least 2.4.0"
13822
13823         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
13824                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
13825                 error "failed to load module obdecho"
13826
13827         local target=$(do_facet ost1 $LCTL dl |
13828                        awk '/obdfilter/ { print $4; exit; }')
13829
13830         if [ -n "$target" ]; then
13831                 local pages=16384 # 64MB bulk I/O RPC size
13832
13833                 obdecho_test "$target" ost1 "$pages" ||
13834                         error "obdecho_test with pages=$pages failed with $?"
13835         else
13836                 do_facet ost1 $LCTL dl
13837                 error "there is no obdfilter target on ost1"
13838         fi
13839 }
13840 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
13841
13842 test_181() { # bug 22177
13843         test_mkdir $DIR/$tdir
13844         # create enough files to index the directory
13845         createmany -o $DIR/$tdir/foobar 4000
13846         # print attributes for debug purpose
13847         lsattr -d .
13848         # open dir
13849         multiop_bg_pause $DIR/$tdir D_Sc || return 1
13850         MULTIPID=$!
13851         # remove the files & current working dir
13852         unlinkmany $DIR/$tdir/foobar 4000
13853         rmdir $DIR/$tdir
13854         kill -USR1 $MULTIPID
13855         wait $MULTIPID
13856         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
13857         return 0
13858 }
13859 run_test 181 "Test open-unlinked dir ========================"
13860
13861 test_182() {
13862         local fcount=1000
13863         local tcount=10
13864
13865         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
13866
13867         $LCTL set_param mdc.*.rpc_stats=clear
13868
13869         for (( i = 0; i < $tcount; i++ )) ; do
13870                 mkdir $DIR/$tdir/$i
13871         done
13872
13873         for (( i = 0; i < $tcount; i++ )) ; do
13874                 createmany -o $DIR/$tdir/$i/f- $fcount &
13875         done
13876         wait
13877
13878         for (( i = 0; i < $tcount; i++ )) ; do
13879                 unlinkmany $DIR/$tdir/$i/f- $fcount &
13880         done
13881         wait
13882
13883         $LCTL get_param mdc.*.rpc_stats
13884
13885         rm -rf $DIR/$tdir
13886 }
13887 run_test 182 "Test parallel modify metadata operations ================"
13888
13889 test_183() { # LU-2275
13890         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13891         remote_mds_nodsh && skip "remote MDS with nodsh"
13892         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
13893                 skip "Need MDS version at least 2.3.56"
13894
13895         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
13896         echo aaa > $DIR/$tdir/$tfile
13897
13898 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
13899         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
13900
13901         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
13902         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
13903
13904         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
13905
13906         # Flush negative dentry cache
13907         touch $DIR/$tdir/$tfile
13908
13909         # We are not checking for any leaked references here, they'll
13910         # become evident next time we do cleanup with module unload.
13911         rm -rf $DIR/$tdir
13912 }
13913 run_test 183 "No crash or request leak in case of strange dispositions ========"
13914
13915 # test suite 184 is for LU-2016, LU-2017
13916 test_184a() {
13917         check_swap_layouts_support
13918
13919         dir0=$DIR/$tdir/$testnum
13920         test_mkdir -p -c1 $dir0
13921         ref1=/etc/passwd
13922         ref2=/etc/group
13923         file1=$dir0/f1
13924         file2=$dir0/f2
13925         $LFS setstripe -c1 $file1
13926         cp $ref1 $file1
13927         $LFS setstripe -c2 $file2
13928         cp $ref2 $file2
13929         gen1=$($LFS getstripe -g $file1)
13930         gen2=$($LFS getstripe -g $file2)
13931
13932         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
13933         gen=$($LFS getstripe -g $file1)
13934         [[ $gen1 != $gen ]] ||
13935                 "Layout generation on $file1 does not change"
13936         gen=$($LFS getstripe -g $file2)
13937         [[ $gen2 != $gen ]] ||
13938                 "Layout generation on $file2 does not change"
13939
13940         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
13941         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
13942
13943         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
13944 }
13945 run_test 184a "Basic layout swap"
13946
13947 test_184b() {
13948         check_swap_layouts_support
13949
13950         dir0=$DIR/$tdir/$testnum
13951         mkdir -p $dir0 || error "creating dir $dir0"
13952         file1=$dir0/f1
13953         file2=$dir0/f2
13954         file3=$dir0/f3
13955         dir1=$dir0/d1
13956         dir2=$dir0/d2
13957         mkdir $dir1 $dir2
13958         $LFS setstripe -c1 $file1
13959         $LFS setstripe -c2 $file2
13960         $LFS setstripe -c1 $file3
13961         chown $RUNAS_ID $file3
13962         gen1=$($LFS getstripe -g $file1)
13963         gen2=$($LFS getstripe -g $file2)
13964
13965         $LFS swap_layouts $dir1 $dir2 &&
13966                 error "swap of directories layouts should fail"
13967         $LFS swap_layouts $dir1 $file1 &&
13968                 error "swap of directory and file layouts should fail"
13969         $RUNAS $LFS swap_layouts $file1 $file2 &&
13970                 error "swap of file we cannot write should fail"
13971         $LFS swap_layouts $file1 $file3 &&
13972                 error "swap of file with different owner should fail"
13973         /bin/true # to clear error code
13974 }
13975 run_test 184b "Forbidden layout swap (will generate errors)"
13976
13977 test_184c() {
13978         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
13979         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
13980         check_swap_layouts_support
13981
13982         local dir0=$DIR/$tdir/$testnum
13983         mkdir -p $dir0 || error "creating dir $dir0"
13984
13985         local ref1=$dir0/ref1
13986         local ref2=$dir0/ref2
13987         local file1=$dir0/file1
13988         local file2=$dir0/file2
13989         # create a file large enough for the concurrent test
13990         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
13991         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
13992         echo "ref file size: ref1($(stat -c %s $ref1))," \
13993              "ref2($(stat -c %s $ref2))"
13994
13995         cp $ref2 $file2
13996         dd if=$ref1 of=$file1 bs=16k &
13997         local DD_PID=$!
13998
13999         # Make sure dd starts to copy file
14000         while [ ! -f $file1 ]; do sleep 0.1; done
14001
14002         $LFS swap_layouts $file1 $file2
14003         local rc=$?
14004         wait $DD_PID
14005         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
14006         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
14007
14008         # how many bytes copied before swapping layout
14009         local copied=$(stat -c %s $file2)
14010         local remaining=$(stat -c %s $ref1)
14011         remaining=$((remaining - copied))
14012         echo "Copied $copied bytes before swapping layout..."
14013
14014         cmp -n $copied $file1 $ref2 | grep differ &&
14015                 error "Content mismatch [0, $copied) of ref2 and file1"
14016         cmp -n $copied $file2 $ref1 ||
14017                 error "Content mismatch [0, $copied) of ref1 and file2"
14018         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
14019                 error "Content mismatch [$copied, EOF) of ref1 and file1"
14020
14021         # clean up
14022         rm -f $ref1 $ref2 $file1 $file2
14023 }
14024 run_test 184c "Concurrent write and layout swap"
14025
14026 test_184d() {
14027         check_swap_layouts_support
14028         [ -z "$(which getfattr 2>/dev/null)" ] &&
14029                 skip_env "no getfattr command"
14030
14031         local file1=$DIR/$tdir/$tfile-1
14032         local file2=$DIR/$tdir/$tfile-2
14033         local file3=$DIR/$tdir/$tfile-3
14034         local lovea1
14035         local lovea2
14036
14037         mkdir -p $DIR/$tdir
14038         touch $file1 || error "create $file1 failed"
14039         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
14040                 error "create $file2 failed"
14041         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
14042                 error "create $file3 failed"
14043         lovea1=$(get_layout_param $file1)
14044
14045         $LFS swap_layouts $file2 $file3 ||
14046                 error "swap $file2 $file3 layouts failed"
14047         $LFS swap_layouts $file1 $file2 ||
14048                 error "swap $file1 $file2 layouts failed"
14049
14050         lovea2=$(get_layout_param $file2)
14051         echo "$lovea1"
14052         echo "$lovea2"
14053         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
14054
14055         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
14056         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
14057 }
14058 run_test 184d "allow stripeless layouts swap"
14059
14060 test_184e() {
14061         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
14062                 skip "Need MDS version at least 2.6.94"
14063         check_swap_layouts_support
14064         [ -z "$(which getfattr 2>/dev/null)" ] &&
14065                 skip_env "no getfattr command"
14066
14067         local file1=$DIR/$tdir/$tfile-1
14068         local file2=$DIR/$tdir/$tfile-2
14069         local file3=$DIR/$tdir/$tfile-3
14070         local lovea
14071
14072         mkdir -p $DIR/$tdir
14073         touch $file1 || error "create $file1 failed"
14074         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
14075                 error "create $file2 failed"
14076         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
14077                 error "create $file3 failed"
14078
14079         $LFS swap_layouts $file1 $file2 ||
14080                 error "swap $file1 $file2 layouts failed"
14081
14082         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
14083         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
14084
14085         echo 123 > $file1 || error "Should be able to write into $file1"
14086
14087         $LFS swap_layouts $file1 $file3 ||
14088                 error "swap $file1 $file3 layouts failed"
14089
14090         echo 123 > $file1 || error "Should be able to write into $file1"
14091
14092         rm -rf $file1 $file2 $file3
14093 }
14094 run_test 184e "Recreate layout after stripeless layout swaps"
14095
14096 test_184f() {
14097         # Create a file with name longer than sizeof(struct stat) ==
14098         # 144 to see if we can get chars from the file name to appear
14099         # in the returned striping. Note that 'f' == 0x66.
14100         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
14101
14102         mkdir -p $DIR/$tdir
14103         mcreate $DIR/$tdir/$file
14104         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
14105                 error "IOC_MDC_GETFILEINFO returned garbage striping"
14106         fi
14107 }
14108 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
14109
14110 test_185() { # LU-2441
14111         # LU-3553 - no volatile file support in old servers
14112         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
14113                 skip "Need MDS version at least 2.3.60"
14114
14115         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14116         touch $DIR/$tdir/spoo
14117         local mtime1=$(stat -c "%Y" $DIR/$tdir)
14118         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
14119                 error "cannot create/write a volatile file"
14120         [ "$FILESET" == "" ] &&
14121         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
14122                 error "FID is still valid after close"
14123
14124         multiop_bg_pause $DIR/$tdir vVw4096_c
14125         local multi_pid=$!
14126
14127         local OLD_IFS=$IFS
14128         IFS=":"
14129         local fidv=($fid)
14130         IFS=$OLD_IFS
14131         # assume that the next FID for this client is sequential, since stdout
14132         # is unfortunately eaten by multiop_bg_pause
14133         local n=$((${fidv[1]} + 1))
14134         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
14135         if [ "$FILESET" == "" ]; then
14136                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
14137                         error "FID is missing before close"
14138         fi
14139         kill -USR1 $multi_pid
14140         # 1 second delay, so if mtime change we will see it
14141         sleep 1
14142         local mtime2=$(stat -c "%Y" $DIR/$tdir)
14143         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
14144 }
14145 run_test 185 "Volatile file support"
14146
14147 test_187a() {
14148         remote_mds_nodsh && skip "remote MDS with nodsh"
14149         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
14150                 skip "Need MDS version at least 2.3.0"
14151
14152         local dir0=$DIR/$tdir/$testnum
14153         mkdir -p $dir0 || error "creating dir $dir0"
14154
14155         local file=$dir0/file1
14156         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
14157         local dv1=$($LFS data_version $file)
14158         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
14159         local dv2=$($LFS data_version $file)
14160         [[ $dv1 != $dv2 ]] ||
14161                 error "data version did not change on write $dv1 == $dv2"
14162
14163         # clean up
14164         rm -f $file1
14165 }
14166 run_test 187a "Test data version change"
14167
14168 test_187b() {
14169         remote_mds_nodsh && skip "remote MDS with nodsh"
14170         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
14171                 skip "Need MDS version at least 2.3.0"
14172
14173         local dir0=$DIR/$tdir/$testnum
14174         mkdir -p $dir0 || error "creating dir $dir0"
14175
14176         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
14177         [[ ${DV[0]} != ${DV[1]} ]] ||
14178                 error "data version did not change on write"\
14179                       " ${DV[0]} == ${DV[1]}"
14180
14181         # clean up
14182         rm -f $file1
14183 }
14184 run_test 187b "Test data version change on volatile file"
14185
14186 test_200() {
14187         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14188         remote_mgs_nodsh && skip "remote MGS with nodsh"
14189         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
14190
14191         local POOL=${POOL:-cea1}
14192         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
14193         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
14194         # Pool OST targets
14195         local first_ost=0
14196         local last_ost=$(($OSTCOUNT - 1))
14197         local ost_step=2
14198         local ost_list=$(seq $first_ost $ost_step $last_ost)
14199         local ost_range="$first_ost $last_ost $ost_step"
14200         local test_path=$POOL_ROOT/$POOL_DIR_NAME
14201         local file_dir=$POOL_ROOT/file_tst
14202         local subdir=$test_path/subdir
14203         local rc=0
14204
14205         if ! combined_mgs_mds ; then
14206                 mount_mgs_client
14207         fi
14208
14209         while : ; do
14210                 # former test_200a test_200b
14211                 pool_add $POOL                          || { rc=$? ; break; }
14212                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
14213                 # former test_200c test_200d
14214                 mkdir -p $test_path
14215                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
14216                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
14217                 mkdir -p $subdir
14218                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
14219                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
14220                                                         || { rc=$? ; break; }
14221                 # former test_200e test_200f
14222                 local files=$((OSTCOUNT*3))
14223                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
14224                                                         || { rc=$? ; break; }
14225                 pool_create_files $POOL $file_dir $files "$ost_list" \
14226                                                         || { rc=$? ; break; }
14227                 # former test_200g test_200h
14228                 pool_lfs_df $POOL                       || { rc=$? ; break; }
14229                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
14230
14231                 # former test_201a test_201b test_201c
14232                 pool_remove_first_target $POOL          || { rc=$? ; break; }
14233
14234                 local f=$test_path/$tfile
14235                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
14236                 pool_remove $POOL $f                    || { rc=$? ; break; }
14237                 break
14238         done
14239
14240         destroy_test_pools
14241
14242         if ! combined_mgs_mds ; then
14243                 umount_mgs_client
14244         fi
14245         return $rc
14246 }
14247 run_test 200 "OST pools"
14248
14249 # usage: default_attr <count | size | offset>
14250 default_attr() {
14251         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
14252 }
14253
14254 # usage: check_default_stripe_attr
14255 check_default_stripe_attr() {
14256         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
14257         case $1 in
14258         --stripe-count|-c)
14259                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
14260         --stripe-size|-S)
14261                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
14262         --stripe-index|-i)
14263                 EXPECTED=-1;;
14264         *)
14265                 error "unknown getstripe attr '$1'"
14266         esac
14267
14268         [ $ACTUAL == $EXPECTED ] ||
14269                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
14270 }
14271
14272 test_204a() {
14273         test_mkdir $DIR/$tdir
14274         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
14275
14276         check_default_stripe_attr --stripe-count
14277         check_default_stripe_attr --stripe-size
14278         check_default_stripe_attr --stripe-index
14279 }
14280 run_test 204a "Print default stripe attributes"
14281
14282 test_204b() {
14283         test_mkdir $DIR/$tdir
14284         $LFS setstripe --stripe-count 1 $DIR/$tdir
14285
14286         check_default_stripe_attr --stripe-size
14287         check_default_stripe_attr --stripe-index
14288 }
14289 run_test 204b "Print default stripe size and offset"
14290
14291 test_204c() {
14292         test_mkdir $DIR/$tdir
14293         $LFS setstripe --stripe-size 65536 $DIR/$tdir
14294
14295         check_default_stripe_attr --stripe-count
14296         check_default_stripe_attr --stripe-index
14297 }
14298 run_test 204c "Print default stripe count and offset"
14299
14300 test_204d() {
14301         test_mkdir $DIR/$tdir
14302         $LFS setstripe --stripe-index 0 $DIR/$tdir
14303
14304         check_default_stripe_attr --stripe-count
14305         check_default_stripe_attr --stripe-size
14306 }
14307 run_test 204d "Print default stripe count and size"
14308
14309 test_204e() {
14310         test_mkdir $DIR/$tdir
14311         $LFS setstripe -d $DIR/$tdir
14312
14313         check_default_stripe_attr --stripe-count --raw
14314         check_default_stripe_attr --stripe-size --raw
14315         check_default_stripe_attr --stripe-index --raw
14316 }
14317 run_test 204e "Print raw stripe attributes"
14318
14319 test_204f() {
14320         test_mkdir $DIR/$tdir
14321         $LFS setstripe --stripe-count 1 $DIR/$tdir
14322
14323         check_default_stripe_attr --stripe-size --raw
14324         check_default_stripe_attr --stripe-index --raw
14325 }
14326 run_test 204f "Print raw stripe size and offset"
14327
14328 test_204g() {
14329         test_mkdir $DIR/$tdir
14330         $LFS setstripe --stripe-size 65536 $DIR/$tdir
14331
14332         check_default_stripe_attr --stripe-count --raw
14333         check_default_stripe_attr --stripe-index --raw
14334 }
14335 run_test 204g "Print raw stripe count and offset"
14336
14337 test_204h() {
14338         test_mkdir $DIR/$tdir
14339         $LFS setstripe --stripe-index 0 $DIR/$tdir
14340
14341         check_default_stripe_attr --stripe-count --raw
14342         check_default_stripe_attr --stripe-size --raw
14343 }
14344 run_test 204h "Print raw stripe count and size"
14345
14346 # Figure out which job scheduler is being used, if any,
14347 # or use a fake one
14348 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
14349         JOBENV=SLURM_JOB_ID
14350 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
14351         JOBENV=LSB_JOBID
14352 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
14353         JOBENV=PBS_JOBID
14354 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
14355         JOBENV=LOADL_STEP_ID
14356 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
14357         JOBENV=JOB_ID
14358 else
14359         $LCTL list_param jobid_name > /dev/null 2>&1
14360         if [ $? -eq 0 ]; then
14361                 JOBENV=nodelocal
14362         else
14363                 JOBENV=FAKE_JOBID
14364         fi
14365 fi
14366 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
14367
14368 verify_jobstats() {
14369         local cmd=($1)
14370         shift
14371         local facets="$@"
14372
14373 # we don't really need to clear the stats for this test to work, since each
14374 # command has a unique jobid, but it makes debugging easier if needed.
14375 #       for facet in $facets; do
14376 #               local dev=$(convert_facet2label $facet)
14377 #               # clear old jobstats
14378 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
14379 #       done
14380
14381         # use a new JobID for each test, or we might see an old one
14382         [ "$JOBENV" = "FAKE_JOBID" ] &&
14383                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
14384
14385         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
14386
14387         [ "$JOBENV" = "nodelocal" ] && {
14388                 FAKE_JOBID=id.$testnum.%e.$RANDOM
14389                 $LCTL set_param jobid_name=$FAKE_JOBID
14390                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
14391         }
14392
14393         log "Test: ${cmd[*]}"
14394         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
14395
14396         if [ $JOBENV = "FAKE_JOBID" ]; then
14397                 FAKE_JOBID=$JOBVAL ${cmd[*]}
14398         else
14399                 ${cmd[*]}
14400         fi
14401
14402         # all files are created on OST0000
14403         for facet in $facets; do
14404                 local stats="*.$(convert_facet2label $facet).job_stats"
14405
14406                 # strip out libtool wrappers for in-tree executables
14407                 if [ $(do_facet $facet lctl get_param $stats |
14408                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
14409                         do_facet $facet lctl get_param $stats
14410                         error "No jobstats for $JOBVAL found on $facet::$stats"
14411                 fi
14412         done
14413 }
14414
14415 jobstats_set() {
14416         local new_jobenv=$1
14417
14418         set_persistent_param_and_check client "jobid_var" \
14419                 "$FSNAME.sys.jobid_var" $new_jobenv
14420 }
14421
14422 test_205() { # Job stats
14423         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14424         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
14425                 skip "Need MDS version with at least 2.7.1"
14426         remote_mgs_nodsh && skip "remote MGS with nodsh"
14427         remote_mds_nodsh && skip "remote MDS with nodsh"
14428         remote_ost_nodsh && skip "remote OST with nodsh"
14429         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
14430                 skip "Server doesn't support jobstats"
14431         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
14432
14433         local old_jobenv=$($LCTL get_param -n jobid_var)
14434         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
14435
14436         if [[ $PERM_CMD == *"set_param -P"* ]]; then
14437                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
14438         else
14439                 stack_trap "do_facet mgs $PERM_CMD \
14440                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
14441         fi
14442         changelog_register
14443
14444         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
14445                                 mdt.*.job_cleanup_interval | head -n 1)
14446         local new_interval=5
14447         do_facet $SINGLEMDS \
14448                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
14449         stack_trap "do_facet $SINGLEMDS \
14450                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
14451         local start=$SECONDS
14452
14453         local cmd
14454         # mkdir
14455         cmd="mkdir $DIR/$tdir"
14456         verify_jobstats "$cmd" "$SINGLEMDS"
14457         # rmdir
14458         cmd="rmdir $DIR/$tdir"
14459         verify_jobstats "$cmd" "$SINGLEMDS"
14460         # mkdir on secondary MDT
14461         if [ $MDSCOUNT -gt 1 ]; then
14462                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
14463                 verify_jobstats "$cmd" "mds2"
14464         fi
14465         # mknod
14466         cmd="mknod $DIR/$tfile c 1 3"
14467         verify_jobstats "$cmd" "$SINGLEMDS"
14468         # unlink
14469         cmd="rm -f $DIR/$tfile"
14470         verify_jobstats "$cmd" "$SINGLEMDS"
14471         # create all files on OST0000 so verify_jobstats can find OST stats
14472         # open & close
14473         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
14474         verify_jobstats "$cmd" "$SINGLEMDS"
14475         # setattr
14476         cmd="touch $DIR/$tfile"
14477         verify_jobstats "$cmd" "$SINGLEMDS ost1"
14478         # write
14479         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
14480         verify_jobstats "$cmd" "ost1"
14481         # read
14482         cancel_lru_locks osc
14483         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
14484         verify_jobstats "$cmd" "ost1"
14485         # truncate
14486         cmd="$TRUNCATE $DIR/$tfile 0"
14487         verify_jobstats "$cmd" "$SINGLEMDS ost1"
14488         # rename
14489         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
14490         verify_jobstats "$cmd" "$SINGLEMDS"
14491         # jobstats expiry - sleep until old stats should be expired
14492         local left=$((new_interval + 5 - (SECONDS - start)))
14493         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
14494                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
14495                         "0" $left
14496         cmd="mkdir $DIR/$tdir.expire"
14497         verify_jobstats "$cmd" "$SINGLEMDS"
14498         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
14499             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
14500
14501         # Ensure that jobid are present in changelog (if supported by MDS)
14502         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
14503                 changelog_dump | tail -10
14504                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
14505                 [ $jobids -eq 9 ] ||
14506                         error "Wrong changelog jobid count $jobids != 9"
14507
14508                 # LU-5862
14509                 JOBENV="disable"
14510                 jobstats_set $JOBENV
14511                 touch $DIR/$tfile
14512                 changelog_dump | grep $tfile
14513                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
14514                 [ $jobids -eq 0 ] ||
14515                         error "Unexpected jobids when jobid_var=$JOBENV"
14516         fi
14517
14518         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
14519         JOBENV="JOBCOMPLEX"
14520         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
14521
14522         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
14523 }
14524 run_test 205 "Verify job stats"
14525
14526 # LU-1480, LU-1773 and LU-1657
14527 test_206() {
14528         mkdir -p $DIR/$tdir
14529         $LFS setstripe -c -1 $DIR/$tdir
14530 #define OBD_FAIL_LOV_INIT 0x1403
14531         $LCTL set_param fail_loc=0xa0001403
14532         $LCTL set_param fail_val=1
14533         touch $DIR/$tdir/$tfile || true
14534 }
14535 run_test 206 "fail lov_init_raid0() doesn't lbug"
14536
14537 test_207a() {
14538         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
14539         local fsz=`stat -c %s $DIR/$tfile`
14540         cancel_lru_locks mdc
14541
14542         # do not return layout in getattr intent
14543 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
14544         $LCTL set_param fail_loc=0x170
14545         local sz=`stat -c %s $DIR/$tfile`
14546
14547         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
14548
14549         rm -rf $DIR/$tfile
14550 }
14551 run_test 207a "can refresh layout at glimpse"
14552
14553 test_207b() {
14554         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
14555         local cksum=`md5sum $DIR/$tfile`
14556         local fsz=`stat -c %s $DIR/$tfile`
14557         cancel_lru_locks mdc
14558         cancel_lru_locks osc
14559
14560         # do not return layout in getattr intent
14561 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
14562         $LCTL set_param fail_loc=0x171
14563
14564         # it will refresh layout after the file is opened but before read issues
14565         echo checksum is "$cksum"
14566         echo "$cksum" |md5sum -c --quiet || error "file differs"
14567
14568         rm -rf $DIR/$tfile
14569 }
14570 run_test 207b "can refresh layout at open"
14571
14572 test_208() {
14573         # FIXME: in this test suite, only RD lease is used. This is okay
14574         # for now as only exclusive open is supported. After generic lease
14575         # is done, this test suite should be revised. - Jinshan
14576
14577         remote_mds_nodsh && skip "remote MDS with nodsh"
14578         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
14579                 skip "Need MDS version at least 2.4.52"
14580
14581         echo "==== test 1: verify get lease work"
14582         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
14583
14584         echo "==== test 2: verify lease can be broken by upcoming open"
14585         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
14586         local PID=$!
14587         sleep 1
14588
14589         $MULTIOP $DIR/$tfile oO_RDONLY:c
14590         kill -USR1 $PID && wait $PID || error "break lease error"
14591
14592         echo "==== test 3: verify lease can't be granted if an open already exists"
14593         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
14594         local PID=$!
14595         sleep 1
14596
14597         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
14598         kill -USR1 $PID && wait $PID || error "open file error"
14599
14600         echo "==== test 4: lease can sustain over recovery"
14601         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
14602         PID=$!
14603         sleep 1
14604
14605         fail mds1
14606
14607         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
14608
14609         echo "==== test 5: lease broken can't be regained by replay"
14610         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
14611         PID=$!
14612         sleep 1
14613
14614         # open file to break lease and then recovery
14615         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
14616         fail mds1
14617
14618         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
14619
14620         rm -f $DIR/$tfile
14621 }
14622 run_test 208 "Exclusive open"
14623
14624 test_209() {
14625         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
14626                 skip_env "must have disp_stripe"
14627
14628         touch $DIR/$tfile
14629         sync; sleep 5; sync;
14630
14631         echo 3 > /proc/sys/vm/drop_caches
14632         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
14633
14634         # open/close 500 times
14635         for i in $(seq 500); do
14636                 cat $DIR/$tfile
14637         done
14638
14639         echo 3 > /proc/sys/vm/drop_caches
14640         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
14641
14642         echo "before: $req_before, after: $req_after"
14643         [ $((req_after - req_before)) -ge 300 ] &&
14644                 error "open/close requests are not freed"
14645         return 0
14646 }
14647 run_test 209 "read-only open/close requests should be freed promptly"
14648
14649 test_212() {
14650         size=`date +%s`
14651         size=$((size % 8192 + 1))
14652         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
14653         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
14654         rm -f $DIR/f212 $DIR/f212.xyz
14655 }
14656 run_test 212 "Sendfile test ============================================"
14657
14658 test_213() {
14659         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
14660         cancel_lru_locks osc
14661         lctl set_param fail_loc=0x8000040f
14662         # generate a read lock
14663         cat $DIR/$tfile > /dev/null
14664         # write to the file, it will try to cancel the above read lock.
14665         cat /etc/hosts >> $DIR/$tfile
14666 }
14667 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
14668
14669 test_214() { # for bug 20133
14670         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
14671         for (( i=0; i < 340; i++ )) ; do
14672                 touch $DIR/$tdir/d214c/a$i
14673         done
14674
14675         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
14676         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
14677         ls $DIR/d214c || error "ls $DIR/d214c failed"
14678         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
14679         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
14680 }
14681 run_test 214 "hash-indexed directory test - bug 20133"
14682
14683 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
14684 create_lnet_proc_files() {
14685         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
14686 }
14687
14688 # counterpart of create_lnet_proc_files
14689 remove_lnet_proc_files() {
14690         rm -f $TMP/lnet_$1.sys
14691 }
14692
14693 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
14694 # 3rd arg as regexp for body
14695 check_lnet_proc_stats() {
14696         local l=$(cat "$TMP/lnet_$1" |wc -l)
14697         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
14698
14699         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
14700 }
14701
14702 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
14703 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
14704 # optional and can be regexp for 2nd line (lnet.routes case)
14705 check_lnet_proc_entry() {
14706         local blp=2          # blp stands for 'position of 1st line of body'
14707         [ -z "$5" ] || blp=3 # lnet.routes case
14708
14709         local l=$(cat "$TMP/lnet_$1" |wc -l)
14710         # subtracting one from $blp because the body can be empty
14711         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
14712
14713         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
14714                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
14715
14716         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
14717                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
14718
14719         # bail out if any unexpected line happened
14720         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
14721         [ "$?" != 0 ] || error "$2 misformatted"
14722 }
14723
14724 test_215() { # for bugs 18102, 21079, 21517
14725         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14726
14727         local N='(0|[1-9][0-9]*)'       # non-negative numeric
14728         local P='[1-9][0-9]*'           # positive numeric
14729         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
14730         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
14731         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
14732         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
14733
14734         local L1 # regexp for 1st line
14735         local L2 # regexp for 2nd line (optional)
14736         local BR # regexp for the rest (body)
14737
14738         # lnet.stats should look as 11 space-separated non-negative numerics
14739         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
14740         create_lnet_proc_files "stats"
14741         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
14742         remove_lnet_proc_files "stats"
14743
14744         # lnet.routes should look like this:
14745         # Routing disabled/enabled
14746         # net hops priority state router
14747         # where net is a string like tcp0, hops > 0, priority >= 0,
14748         # state is up/down,
14749         # router is a string like 192.168.1.1@tcp2
14750         L1="^Routing (disabled|enabled)$"
14751         L2="^net +hops +priority +state +router$"
14752         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
14753         create_lnet_proc_files "routes"
14754         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
14755         remove_lnet_proc_files "routes"
14756
14757         # lnet.routers should look like this:
14758         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
14759         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
14760         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
14761         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
14762         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
14763         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
14764         create_lnet_proc_files "routers"
14765         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
14766         remove_lnet_proc_files "routers"
14767
14768         # lnet.peers should look like this:
14769         # nid refs state last max rtr min tx min queue
14770         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
14771         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
14772         # numeric (0 or >0 or <0), queue >= 0.
14773         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
14774         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
14775         create_lnet_proc_files "peers"
14776         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
14777         remove_lnet_proc_files "peers"
14778
14779         # lnet.buffers  should look like this:
14780         # pages count credits min
14781         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
14782         L1="^pages +count +credits +min$"
14783         BR="^ +$N +$N +$I +$I$"
14784         create_lnet_proc_files "buffers"
14785         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
14786         remove_lnet_proc_files "buffers"
14787
14788         # lnet.nis should look like this:
14789         # nid status alive refs peer rtr max tx min
14790         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
14791         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
14792         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
14793         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
14794         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
14795         create_lnet_proc_files "nis"
14796         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
14797         remove_lnet_proc_files "nis"
14798
14799         # can we successfully write to lnet.stats?
14800         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
14801 }
14802 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
14803
14804 test_216() { # bug 20317
14805         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14806         remote_ost_nodsh && skip "remote OST with nodsh"
14807
14808         local node
14809         local facets=$(get_facets OST)
14810         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
14811
14812         save_lustre_params client "osc.*.contention_seconds" > $p
14813         save_lustre_params $facets \
14814                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
14815         save_lustre_params $facets \
14816                 "ldlm.namespaces.filter-*.contended_locks" >> $p
14817         save_lustre_params $facets \
14818                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
14819         clear_stats osc.*.osc_stats
14820
14821         # agressive lockless i/o settings
14822         do_nodes $(comma_list $(osts_nodes)) \
14823                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
14824                         ldlm.namespaces.filter-*.contended_locks=0 \
14825                         ldlm.namespaces.filter-*.contention_seconds=60"
14826         lctl set_param -n osc.*.contention_seconds=60
14827
14828         $DIRECTIO write $DIR/$tfile 0 10 4096
14829         $CHECKSTAT -s 40960 $DIR/$tfile
14830
14831         # disable lockless i/o
14832         do_nodes $(comma_list $(osts_nodes)) \
14833                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
14834                         ldlm.namespaces.filter-*.contended_locks=32 \
14835                         ldlm.namespaces.filter-*.contention_seconds=0"
14836         lctl set_param -n osc.*.contention_seconds=0
14837         clear_stats osc.*.osc_stats
14838
14839         dd if=/dev/zero of=$DIR/$tfile count=0
14840         $CHECKSTAT -s 0 $DIR/$tfile
14841
14842         restore_lustre_params <$p
14843         rm -f $p
14844         rm $DIR/$tfile
14845 }
14846 run_test 216 "check lockless direct write updates file size and kms correctly"
14847
14848 test_217() { # bug 22430
14849         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14850
14851         local node
14852         local nid
14853
14854         for node in $(nodes_list); do
14855                 nid=$(host_nids_address $node $NETTYPE)
14856                 if [[ $nid = *-* ]] ; then
14857                         echo "lctl ping $(h2nettype $nid)"
14858                         lctl ping $(h2nettype $nid)
14859                 else
14860                         echo "skipping $node (no hyphen detected)"
14861                 fi
14862         done
14863 }
14864 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
14865
14866 test_218() {
14867        # do directio so as not to populate the page cache
14868        log "creating a 10 Mb file"
14869        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
14870        log "starting reads"
14871        dd if=$DIR/$tfile of=/dev/null bs=4096 &
14872        log "truncating the file"
14873        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
14874        log "killing dd"
14875        kill %+ || true # reads might have finished
14876        echo "wait until dd is finished"
14877        wait
14878        log "removing the temporary file"
14879        rm -rf $DIR/$tfile || error "tmp file removal failed"
14880 }
14881 run_test 218 "parallel read and truncate should not deadlock"
14882
14883 test_219() {
14884         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14885
14886         # write one partial page
14887         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
14888         # set no grant so vvp_io_commit_write will do sync write
14889         $LCTL set_param fail_loc=0x411
14890         # write a full page at the end of file
14891         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
14892
14893         $LCTL set_param fail_loc=0
14894         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
14895         $LCTL set_param fail_loc=0x411
14896         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
14897
14898         # LU-4201
14899         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
14900         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
14901 }
14902 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
14903
14904 test_220() { #LU-325
14905         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14906         remote_ost_nodsh && skip "remote OST with nodsh"
14907         remote_mds_nodsh && skip "remote MDS with nodsh"
14908         remote_mgs_nodsh && skip "remote MGS with nodsh"
14909
14910         local OSTIDX=0
14911
14912         # create on MDT0000 so the last_id and next_id are correct
14913         mkdir $DIR/$tdir
14914         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
14915         OST=${OST%_UUID}
14916
14917         # on the mdt's osc
14918         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
14919         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
14920                         osc.$mdtosc_proc1.prealloc_last_id)
14921         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
14922                         osc.$mdtosc_proc1.prealloc_next_id)
14923
14924         $LFS df -i
14925
14926         if ! combined_mgs_mds ; then
14927                 mount_mgs_client
14928         fi
14929
14930         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
14931         #define OBD_FAIL_OST_ENOINO              0x229
14932         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
14933         create_pool $FSNAME.$TESTNAME || return 1
14934         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
14935
14936         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
14937
14938         MDSOBJS=$((last_id - next_id))
14939         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
14940
14941         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
14942         echo "OST still has $count kbytes free"
14943
14944         echo "create $MDSOBJS files @next_id..."
14945         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
14946
14947         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
14948                         osc.$mdtosc_proc1.prealloc_last_id)
14949         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
14950                         osc.$mdtosc_proc1.prealloc_next_id)
14951
14952         echo "after creation, last_id=$last_id2, next_id=$next_id2"
14953         $LFS df -i
14954
14955         echo "cleanup..."
14956
14957         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
14958         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
14959
14960         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
14961                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
14962         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
14963                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
14964         echo "unlink $MDSOBJS files @$next_id..."
14965         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
14966
14967         if ! combined_mgs_mds ; then
14968                 umount_mgs_client
14969         fi
14970 }
14971 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
14972
14973 test_221() {
14974         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14975
14976         dd if=`which date` of=$MOUNT/date oflag=sync
14977         chmod +x $MOUNT/date
14978
14979         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
14980         $LCTL set_param fail_loc=0x80001401
14981
14982         $MOUNT/date > /dev/null
14983         rm -f $MOUNT/date
14984 }
14985 run_test 221 "make sure fault and truncate race to not cause OOM"
14986
14987 test_222a () {
14988         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14989
14990         rm -rf $DIR/$tdir
14991         test_mkdir $DIR/$tdir
14992         $LFS setstripe -c 1 -i 0 $DIR/$tdir
14993         createmany -o $DIR/$tdir/$tfile 10
14994         cancel_lru_locks mdc
14995         cancel_lru_locks osc
14996         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
14997         $LCTL set_param fail_loc=0x31a
14998         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
14999         $LCTL set_param fail_loc=0
15000         rm -r $DIR/$tdir
15001 }
15002 run_test 222a "AGL for ls should not trigger CLIO lock failure"
15003
15004 test_222b () {
15005         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15006
15007         rm -rf $DIR/$tdir
15008         test_mkdir $DIR/$tdir
15009         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15010         createmany -o $DIR/$tdir/$tfile 10
15011         cancel_lru_locks mdc
15012         cancel_lru_locks osc
15013         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
15014         $LCTL set_param fail_loc=0x31a
15015         rm -r $DIR/$tdir || error "AGL for rmdir failed"
15016         $LCTL set_param fail_loc=0
15017 }
15018 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
15019
15020 test_223 () {
15021         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15022
15023         rm -rf $DIR/$tdir
15024         test_mkdir $DIR/$tdir
15025         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15026         createmany -o $DIR/$tdir/$tfile 10
15027         cancel_lru_locks mdc
15028         cancel_lru_locks osc
15029         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
15030         $LCTL set_param fail_loc=0x31b
15031         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
15032         $LCTL set_param fail_loc=0
15033         rm -r $DIR/$tdir
15034 }
15035 run_test 223 "osc reenqueue if without AGL lock granted ======================="
15036
15037 test_224a() { # LU-1039, MRP-303
15038         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15039
15040         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
15041         $LCTL set_param fail_loc=0x508
15042         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
15043         $LCTL set_param fail_loc=0
15044         df $DIR
15045 }
15046 run_test 224a "Don't panic on bulk IO failure"
15047
15048 test_224b() { # LU-1039, MRP-303
15049         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15050
15051         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
15052         cancel_lru_locks osc
15053         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
15054         $LCTL set_param fail_loc=0x515
15055         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
15056         $LCTL set_param fail_loc=0
15057         df $DIR
15058 }
15059 run_test 224b "Don't panic on bulk IO failure"
15060
15061 test_224c() { # LU-6441
15062         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15063         remote_mds_nodsh && skip "remote MDS with nodsh"
15064
15065         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15066         save_writethrough $p
15067         set_cache writethrough on
15068
15069         local pages_per_rpc=$($LCTL get_param \
15070                                 osc.*.max_pages_per_rpc)
15071         local at_max=$($LCTL get_param -n at_max)
15072         local timeout=$($LCTL get_param -n timeout)
15073         local test_at="at_max"
15074         local param_at="$FSNAME.sys.at_max"
15075         local test_timeout="timeout"
15076         local param_timeout="$FSNAME.sys.timeout"
15077
15078         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
15079
15080         set_persistent_param_and_check client "$test_at" "$param_at" 0
15081         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
15082
15083         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
15084         do_facet ost1 "$LCTL set_param fail_loc=0x520"
15085         $LFS setstripe -c 1 -i 0 $DIR/$tfile
15086         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
15087         sync
15088         do_facet ost1 "$LCTL set_param fail_loc=0"
15089
15090         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
15091         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
15092                 $timeout
15093
15094         $LCTL set_param -n $pages_per_rpc
15095         restore_lustre_params < $p
15096         rm -f $p
15097 }
15098 run_test 224c "Don't hang if one of md lost during large bulk RPC"
15099
15100 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
15101 test_225a () {
15102         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15103         if [ -z ${MDSSURVEY} ]; then
15104                 skip_env "mds-survey not found"
15105         fi
15106         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
15107                 skip "Need MDS version at least 2.2.51"
15108
15109         local mds=$(facet_host $SINGLEMDS)
15110         local target=$(do_nodes $mds 'lctl dl' |
15111                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
15112
15113         local cmd1="file_count=1000 thrhi=4"
15114         local cmd2="dir_count=2 layer=mdd stripe_count=0"
15115         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
15116         local cmd="$cmd1 $cmd2 $cmd3"
15117
15118         rm -f ${TMP}/mds_survey*
15119         echo + $cmd
15120         eval $cmd || error "mds-survey with zero-stripe failed"
15121         cat ${TMP}/mds_survey*
15122         rm -f ${TMP}/mds_survey*
15123 }
15124 run_test 225a "Metadata survey sanity with zero-stripe"
15125
15126 test_225b () {
15127         if [ -z ${MDSSURVEY} ]; then
15128                 skip_env "mds-survey not found"
15129         fi
15130         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
15131                 skip "Need MDS version at least 2.2.51"
15132         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15133         remote_mds_nodsh && skip "remote MDS with nodsh"
15134         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
15135                 skip_env "Need to mount OST to test"
15136         fi
15137
15138         local mds=$(facet_host $SINGLEMDS)
15139         local target=$(do_nodes $mds 'lctl dl' |
15140                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
15141
15142         local cmd1="file_count=1000 thrhi=4"
15143         local cmd2="dir_count=2 layer=mdd stripe_count=1"
15144         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
15145         local cmd="$cmd1 $cmd2 $cmd3"
15146
15147         rm -f ${TMP}/mds_survey*
15148         echo + $cmd
15149         eval $cmd || error "mds-survey with stripe_count failed"
15150         cat ${TMP}/mds_survey*
15151         rm -f ${TMP}/mds_survey*
15152 }
15153 run_test 225b "Metadata survey sanity with stripe_count = 1"
15154
15155 mcreate_path2fid () {
15156         local mode=$1
15157         local major=$2
15158         local minor=$3
15159         local name=$4
15160         local desc=$5
15161         local path=$DIR/$tdir/$name
15162         local fid
15163         local rc
15164         local fid_path
15165
15166         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
15167                 error "cannot create $desc"
15168
15169         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
15170         rc=$?
15171         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
15172
15173         fid_path=$($LFS fid2path $MOUNT $fid)
15174         rc=$?
15175         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
15176
15177         [ "$path" == "$fid_path" ] ||
15178                 error "fid2path returned $fid_path, expected $path"
15179
15180         echo "pass with $path and $fid"
15181 }
15182
15183 test_226a () {
15184         rm -rf $DIR/$tdir
15185         mkdir -p $DIR/$tdir
15186
15187         mcreate_path2fid 0010666 0 0 fifo "FIFO"
15188         mcreate_path2fid 0020666 1 3 null "character special file (null)"
15189         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
15190         mcreate_path2fid 0040666 0 0 dir "directory"
15191         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
15192         mcreate_path2fid 0100666 0 0 file "regular file"
15193         mcreate_path2fid 0120666 0 0 link "symbolic link"
15194         mcreate_path2fid 0140666 0 0 sock "socket"
15195 }
15196 run_test 226a "call path2fid and fid2path on files of all type"
15197
15198 test_226b () {
15199         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15200
15201         local MDTIDX=1
15202
15203         rm -rf $DIR/$tdir
15204         mkdir -p $DIR/$tdir
15205         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
15206                 error "create remote directory failed"
15207         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
15208         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
15209                                 "character special file (null)"
15210         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
15211                                 "character special file (no device)"
15212         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
15213         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
15214                                 "block special file (loop)"
15215         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
15216         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
15217         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
15218 }
15219 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
15220
15221 # LU-1299 Executing or running ldd on a truncated executable does not
15222 # cause an out-of-memory condition.
15223 test_227() {
15224         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15225         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
15226
15227         dd if=$(which date) of=$MOUNT/date bs=1k count=1
15228         chmod +x $MOUNT/date
15229
15230         $MOUNT/date > /dev/null
15231         ldd $MOUNT/date > /dev/null
15232         rm -f $MOUNT/date
15233 }
15234 run_test 227 "running truncated executable does not cause OOM"
15235
15236 # LU-1512 try to reuse idle OI blocks
15237 test_228a() {
15238         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15239         remote_mds_nodsh && skip "remote MDS with nodsh"
15240         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
15241
15242         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
15243         local myDIR=$DIR/$tdir
15244
15245         mkdir -p $myDIR
15246         #define OBD_FAIL_SEQ_EXHAUST             0x1002
15247         $LCTL set_param fail_loc=0x80001002
15248         createmany -o $myDIR/t- 10000
15249         $LCTL set_param fail_loc=0
15250         # The guard is current the largest FID holder
15251         touch $myDIR/guard
15252         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
15253                     tr -d '[')
15254         local IDX=$(($SEQ % 64))
15255
15256         do_facet $SINGLEMDS sync
15257         # Make sure journal flushed.
15258         sleep 6
15259         local blk1=$(do_facet $SINGLEMDS \
15260                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15261                      grep Blockcount | awk '{print $4}')
15262
15263         # Remove old files, some OI blocks will become idle.
15264         unlinkmany $myDIR/t- 10000
15265         # Create new files, idle OI blocks should be reused.
15266         createmany -o $myDIR/t- 2000
15267         do_facet $SINGLEMDS sync
15268         # Make sure journal flushed.
15269         sleep 6
15270         local blk2=$(do_facet $SINGLEMDS \
15271                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15272                      grep Blockcount | awk '{print $4}')
15273
15274         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
15275 }
15276 run_test 228a "try to reuse idle OI blocks"
15277
15278 test_228b() {
15279         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15280         remote_mds_nodsh && skip "remote MDS with nodsh"
15281         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
15282
15283         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
15284         local myDIR=$DIR/$tdir
15285
15286         mkdir -p $myDIR
15287         #define OBD_FAIL_SEQ_EXHAUST             0x1002
15288         $LCTL set_param fail_loc=0x80001002
15289         createmany -o $myDIR/t- 10000
15290         $LCTL set_param fail_loc=0
15291         # The guard is current the largest FID holder
15292         touch $myDIR/guard
15293         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
15294                     tr -d '[')
15295         local IDX=$(($SEQ % 64))
15296
15297         do_facet $SINGLEMDS sync
15298         # Make sure journal flushed.
15299         sleep 6
15300         local blk1=$(do_facet $SINGLEMDS \
15301                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15302                      grep Blockcount | awk '{print $4}')
15303
15304         # Remove old files, some OI blocks will become idle.
15305         unlinkmany $myDIR/t- 10000
15306
15307         # stop the MDT
15308         stop $SINGLEMDS || error "Fail to stop MDT."
15309         # remount the MDT
15310         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
15311
15312         df $MOUNT || error "Fail to df."
15313         # Create new files, idle OI blocks should be reused.
15314         createmany -o $myDIR/t- 2000
15315         do_facet $SINGLEMDS sync
15316         # Make sure journal flushed.
15317         sleep 6
15318         local blk2=$(do_facet $SINGLEMDS \
15319                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15320                      grep Blockcount | awk '{print $4}')
15321
15322         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
15323 }
15324 run_test 228b "idle OI blocks can be reused after MDT restart"
15325
15326 #LU-1881
15327 test_228c() {
15328         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15329         remote_mds_nodsh && skip "remote MDS with nodsh"
15330         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
15331
15332         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
15333         local myDIR=$DIR/$tdir
15334
15335         mkdir -p $myDIR
15336         #define OBD_FAIL_SEQ_EXHAUST             0x1002
15337         $LCTL set_param fail_loc=0x80001002
15338         # 20000 files can guarantee there are index nodes in the OI file
15339         createmany -o $myDIR/t- 20000
15340         $LCTL set_param fail_loc=0
15341         # The guard is current the largest FID holder
15342         touch $myDIR/guard
15343         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
15344                     tr -d '[')
15345         local IDX=$(($SEQ % 64))
15346
15347         do_facet $SINGLEMDS sync
15348         # Make sure journal flushed.
15349         sleep 6
15350         local blk1=$(do_facet $SINGLEMDS \
15351                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15352                      grep Blockcount | awk '{print $4}')
15353
15354         # Remove old files, some OI blocks will become idle.
15355         unlinkmany $myDIR/t- 20000
15356         rm -f $myDIR/guard
15357         # The OI file should become empty now
15358
15359         # Create new files, idle OI blocks should be reused.
15360         createmany -o $myDIR/t- 2000
15361         do_facet $SINGLEMDS sync
15362         # Make sure journal flushed.
15363         sleep 6
15364         local blk2=$(do_facet $SINGLEMDS \
15365                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15366                      grep Blockcount | awk '{print $4}')
15367
15368         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
15369 }
15370 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
15371
15372 test_229() { # LU-2482, LU-3448
15373         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15374         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
15375         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
15376                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
15377
15378         rm -f $DIR/$tfile
15379
15380         # Create a file with a released layout and stripe count 2.
15381         $MULTIOP $DIR/$tfile H2c ||
15382                 error "failed to create file with released layout"
15383
15384         $LFS getstripe -v $DIR/$tfile
15385
15386         local pattern=$($LFS getstripe -L $DIR/$tfile)
15387         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
15388
15389         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
15390                 error "getstripe"
15391         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
15392         stat $DIR/$tfile || error "failed to stat released file"
15393
15394         chown $RUNAS_ID $DIR/$tfile ||
15395                 error "chown $RUNAS_ID $DIR/$tfile failed"
15396
15397         chgrp $RUNAS_ID $DIR/$tfile ||
15398                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
15399
15400         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
15401         rm $DIR/$tfile || error "failed to remove released file"
15402 }
15403 run_test 229 "getstripe/stat/rm/attr changes work on released files"
15404
15405 test_230a() {
15406         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15407         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15408         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15409                 skip "Need MDS version at least 2.11.52"
15410
15411         local MDTIDX=1
15412
15413         test_mkdir $DIR/$tdir
15414         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
15415         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
15416         [ $mdt_idx -ne 0 ] &&
15417                 error "create local directory on wrong MDT $mdt_idx"
15418
15419         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
15420                         error "create remote directory failed"
15421         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
15422         [ $mdt_idx -ne $MDTIDX ] &&
15423                 error "create remote directory on wrong MDT $mdt_idx"
15424
15425         createmany -o $DIR/$tdir/test_230/t- 10 ||
15426                 error "create files on remote directory failed"
15427         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
15428         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
15429         rm -r $DIR/$tdir || error "unlink remote directory failed"
15430 }
15431 run_test 230a "Create remote directory and files under the remote directory"
15432
15433 test_230b() {
15434         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15435         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15436         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15437                 skip "Need MDS version at least 2.11.52"
15438
15439         local MDTIDX=1
15440         local mdt_index
15441         local i
15442         local file
15443         local pid
15444         local stripe_count
15445         local migrate_dir=$DIR/$tdir/migrate_dir
15446         local other_dir=$DIR/$tdir/other_dir
15447
15448         test_mkdir $DIR/$tdir
15449         test_mkdir -i0 -c1 $migrate_dir
15450         test_mkdir -i0 -c1 $other_dir
15451         for ((i=0; i<10; i++)); do
15452                 mkdir -p $migrate_dir/dir_${i}
15453                 createmany -o $migrate_dir/dir_${i}/f 10 ||
15454                         error "create files under remote dir failed $i"
15455         done
15456
15457         cp /etc/passwd $migrate_dir/$tfile
15458         cp /etc/passwd $other_dir/$tfile
15459         chattr +SAD $migrate_dir
15460         chattr +SAD $migrate_dir/$tfile
15461
15462         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
15463         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
15464         local old_dir_mode=$(stat -c%f $migrate_dir)
15465         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
15466
15467         mkdir -p $migrate_dir/dir_default_stripe2
15468         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
15469         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
15470
15471         mkdir -p $other_dir
15472         ln $migrate_dir/$tfile $other_dir/luna
15473         ln $migrate_dir/$tfile $migrate_dir/sofia
15474         ln $other_dir/$tfile $migrate_dir/david
15475         ln -s $migrate_dir/$tfile $other_dir/zachary
15476         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
15477         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
15478
15479         $LFS migrate -m $MDTIDX $migrate_dir ||
15480                 error "fails on migrating remote dir to MDT1"
15481
15482         echo "migratate to MDT1, then checking.."
15483         for ((i = 0; i < 10; i++)); do
15484                 for file in $(find $migrate_dir/dir_${i}); do
15485                         mdt_index=$($LFS getstripe -m $file)
15486                         [ $mdt_index == $MDTIDX ] ||
15487                                 error "$file is not on MDT${MDTIDX}"
15488                 done
15489         done
15490
15491         # the multiple link file should still in MDT0
15492         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
15493         [ $mdt_index == 0 ] ||
15494                 error "$file is not on MDT${MDTIDX}"
15495
15496         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
15497         [ "$old_dir_flag" = "$new_dir_flag" ] ||
15498                 error " expect $old_dir_flag get $new_dir_flag"
15499
15500         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
15501         [ "$old_file_flag" = "$new_file_flag" ] ||
15502                 error " expect $old_file_flag get $new_file_flag"
15503
15504         local new_dir_mode=$(stat -c%f $migrate_dir)
15505         [ "$old_dir_mode" = "$new_dir_mode" ] ||
15506                 error "expect mode $old_dir_mode get $new_dir_mode"
15507
15508         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
15509         [ "$old_file_mode" = "$new_file_mode" ] ||
15510                 error "expect mode $old_file_mode get $new_file_mode"
15511
15512         diff /etc/passwd $migrate_dir/$tfile ||
15513                 error "$tfile different after migration"
15514
15515         diff /etc/passwd $other_dir/luna ||
15516                 error "luna different after migration"
15517
15518         diff /etc/passwd $migrate_dir/sofia ||
15519                 error "sofia different after migration"
15520
15521         diff /etc/passwd $migrate_dir/david ||
15522                 error "david different after migration"
15523
15524         diff /etc/passwd $other_dir/zachary ||
15525                 error "zachary different after migration"
15526
15527         diff /etc/passwd $migrate_dir/${tfile}_ln ||
15528                 error "${tfile}_ln different after migration"
15529
15530         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
15531                 error "${tfile}_ln_other different after migration"
15532
15533         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
15534         [ $stripe_count = 2 ] ||
15535                 error "dir strpe_count $d != 2 after migration."
15536
15537         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
15538         [ $stripe_count = 2 ] ||
15539                 error "file strpe_count $d != 2 after migration."
15540
15541         #migrate back to MDT0
15542         MDTIDX=0
15543
15544         $LFS migrate -m $MDTIDX $migrate_dir ||
15545                 error "fails on migrating remote dir to MDT0"
15546
15547         echo "migrate back to MDT0, checking.."
15548         for file in $(find $migrate_dir); do
15549                 mdt_index=$($LFS getstripe -m $file)
15550                 [ $mdt_index == $MDTIDX ] ||
15551                         error "$file is not on MDT${MDTIDX}"
15552         done
15553
15554         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
15555         [ "$old_dir_flag" = "$new_dir_flag" ] ||
15556                 error " expect $old_dir_flag get $new_dir_flag"
15557
15558         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
15559         [ "$old_file_flag" = "$new_file_flag" ] ||
15560                 error " expect $old_file_flag get $new_file_flag"
15561
15562         local new_dir_mode=$(stat -c%f $migrate_dir)
15563         [ "$old_dir_mode" = "$new_dir_mode" ] ||
15564                 error "expect mode $old_dir_mode get $new_dir_mode"
15565
15566         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
15567         [ "$old_file_mode" = "$new_file_mode" ] ||
15568                 error "expect mode $old_file_mode get $new_file_mode"
15569
15570         diff /etc/passwd ${migrate_dir}/$tfile ||
15571                 error "$tfile different after migration"
15572
15573         diff /etc/passwd ${other_dir}/luna ||
15574                 error "luna different after migration"
15575
15576         diff /etc/passwd ${migrate_dir}/sofia ||
15577                 error "sofia different after migration"
15578
15579         diff /etc/passwd ${other_dir}/zachary ||
15580                 error "zachary different after migration"
15581
15582         diff /etc/passwd $migrate_dir/${tfile}_ln ||
15583                 error "${tfile}_ln different after migration"
15584
15585         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
15586                 error "${tfile}_ln_other different after migration"
15587
15588         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
15589         [ $stripe_count = 2 ] ||
15590                 error "dir strpe_count $d != 2 after migration."
15591
15592         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
15593         [ $stripe_count = 2 ] ||
15594                 error "file strpe_count $d != 2 after migration."
15595
15596         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15597 }
15598 run_test 230b "migrate directory"
15599
15600 test_230c() {
15601         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15602         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15603         remote_mds_nodsh && skip "remote MDS with nodsh"
15604         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15605                 skip "Need MDS version at least 2.11.52"
15606
15607         local MDTIDX=1
15608         local total=3
15609         local mdt_index
15610         local file
15611         local migrate_dir=$DIR/$tdir/migrate_dir
15612
15613         #If migrating directory fails in the middle, all entries of
15614         #the directory is still accessiable.
15615         test_mkdir $DIR/$tdir
15616         test_mkdir -i0 -c1 $migrate_dir
15617         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
15618         stat $migrate_dir
15619         createmany -o $migrate_dir/f $total ||
15620                 error "create files under ${migrate_dir} failed"
15621
15622         # fail after migrating top dir, and this will fail only once, so the
15623         # first sub file migration will fail (currently f3), others succeed.
15624         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
15625         do_facet mds1 lctl set_param fail_loc=0x1801
15626         local t=$(ls $migrate_dir | wc -l)
15627         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
15628                 error "migrate should fail"
15629         local u=$(ls $migrate_dir | wc -l)
15630         [ "$u" == "$t" ] || error "$u != $t during migration"
15631
15632         # add new dir/file should succeed
15633         mkdir $migrate_dir/dir ||
15634                 error "mkdir failed under migrating directory"
15635         touch $migrate_dir/file ||
15636                 error "create file failed under migrating directory"
15637
15638         # add file with existing name should fail
15639         for file in $migrate_dir/f*; do
15640                 stat $file > /dev/null || error "stat $file failed"
15641                 $OPENFILE -f O_CREAT:O_EXCL $file &&
15642                         error "open(O_CREAT|O_EXCL) $file should fail"
15643                 $MULTIOP $file m && error "create $file should fail"
15644                 touch $DIR/$tdir/remote_dir/$tfile ||
15645                         error "touch $tfile failed"
15646                 ln $DIR/$tdir/remote_dir/$tfile $file &&
15647                         error "link $file should fail"
15648                 mdt_index=$($LFS getstripe -m $file)
15649                 if [ $mdt_index == 0 ]; then
15650                         # file failed to migrate is not allowed to rename to
15651                         mv $DIR/$tdir/remote_dir/$tfile $file &&
15652                                 error "rename to $file should fail"
15653                 else
15654                         mv $DIR/$tdir/remote_dir/$tfile $file ||
15655                                 error "rename to $file failed"
15656                 fi
15657                 echo hello >> $file || error "write $file failed"
15658         done
15659
15660         # resume migration with different options should fail
15661         $LFS migrate -m 0 $migrate_dir &&
15662                 error "migrate -m 0 $migrate_dir should fail"
15663
15664         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
15665                 error "migrate -c 2 $migrate_dir should fail"
15666
15667         # resume migration should succeed
15668         $LFS migrate -m $MDTIDX $migrate_dir ||
15669                 error "migrate $migrate_dir failed"
15670
15671         echo "Finish migration, then checking.."
15672         for file in $(find $migrate_dir); do
15673                 mdt_index=$($LFS getstripe -m $file)
15674                 [ $mdt_index == $MDTIDX ] ||
15675                         error "$file is not on MDT${MDTIDX}"
15676         done
15677
15678         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15679 }
15680 run_test 230c "check directory accessiblity if migration failed"
15681
15682 test_230d() {
15683         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15684         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15685         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15686                 skip "Need MDS version at least 2.11.52"
15687         # LU-11235
15688         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
15689
15690         local migrate_dir=$DIR/$tdir/migrate_dir
15691         local old_index
15692         local new_index
15693         local old_count
15694         local new_count
15695         local new_hash
15696         local mdt_index
15697         local i
15698         local j
15699
15700         old_index=$((RANDOM % MDSCOUNT))
15701         old_count=$((MDSCOUNT - old_index))
15702         new_index=$((RANDOM % MDSCOUNT))
15703         new_count=$((MDSCOUNT - new_index))
15704         new_hash="all_char"
15705
15706         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
15707         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
15708
15709         test_mkdir $DIR/$tdir
15710         test_mkdir -i $old_index -c $old_count $migrate_dir
15711
15712         for ((i=0; i<100; i++)); do
15713                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
15714                 createmany -o $migrate_dir/dir_${i}/f 100 ||
15715                         error "create files under remote dir failed $i"
15716         done
15717
15718         echo -n "Migrate from MDT$old_index "
15719         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
15720         echo -n "to MDT$new_index"
15721         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
15722         echo
15723
15724         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
15725         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
15726                 error "migrate remote dir error"
15727
15728         echo "Finish migration, then checking.."
15729         for file in $(find $migrate_dir); do
15730                 mdt_index=$($LFS getstripe -m $file)
15731                 if [ $mdt_index -lt $new_index ] ||
15732                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
15733                         error "$file is on MDT$mdt_index"
15734                 fi
15735         done
15736
15737         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15738 }
15739 run_test 230d "check migrate big directory"
15740
15741 test_230e() {
15742         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15743         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15744         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15745                 skip "Need MDS version at least 2.11.52"
15746
15747         local i
15748         local j
15749         local a_fid
15750         local b_fid
15751
15752         mkdir -p $DIR/$tdir
15753         mkdir $DIR/$tdir/migrate_dir
15754         mkdir $DIR/$tdir/other_dir
15755         touch $DIR/$tdir/migrate_dir/a
15756         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
15757         ls $DIR/$tdir/other_dir
15758
15759         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
15760                 error "migrate dir fails"
15761
15762         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
15763         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
15764
15765         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15766         [ $mdt_index == 0 ] || error "a is not on MDT0"
15767
15768         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
15769                 error "migrate dir fails"
15770
15771         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
15772         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
15773
15774         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15775         [ $mdt_index == 1 ] || error "a is not on MDT1"
15776
15777         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
15778         [ $mdt_index == 1 ] || error "b is not on MDT1"
15779
15780         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
15781         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
15782
15783         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
15784
15785         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15786 }
15787 run_test 230e "migrate mulitple local link files"
15788
15789 test_230f() {
15790         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15791         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15792         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15793                 skip "Need MDS version at least 2.11.52"
15794
15795         local a_fid
15796         local ln_fid
15797
15798         mkdir -p $DIR/$tdir
15799         mkdir $DIR/$tdir/migrate_dir
15800         $LFS mkdir -i1 $DIR/$tdir/other_dir
15801         touch $DIR/$tdir/migrate_dir/a
15802         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
15803         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
15804         ls $DIR/$tdir/other_dir
15805
15806         # a should be migrated to MDT1, since no other links on MDT0
15807         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
15808                 error "#1 migrate dir fails"
15809         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
15810         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
15811         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15812         [ $mdt_index == 1 ] || error "a is not on MDT1"
15813
15814         # a should stay on MDT1, because it is a mulitple link file
15815         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
15816                 error "#2 migrate dir fails"
15817         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15818         [ $mdt_index == 1 ] || error "a is not on MDT1"
15819
15820         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
15821                 error "#3 migrate dir fails"
15822
15823         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
15824         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
15825         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
15826
15827         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
15828         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
15829
15830         # a should be migrated to MDT0, since no other links on MDT1
15831         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
15832                 error "#4 migrate dir fails"
15833         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15834         [ $mdt_index == 0 ] || error "a is not on MDT0"
15835
15836         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15837 }
15838 run_test 230f "migrate mulitple remote link files"
15839
15840 test_230g() {
15841         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15842         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15843         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15844                 skip "Need MDS version at least 2.11.52"
15845
15846         mkdir -p $DIR/$tdir/migrate_dir
15847
15848         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
15849                 error "migrating dir to non-exist MDT succeeds"
15850         true
15851 }
15852 run_test 230g "migrate dir to non-exist MDT"
15853
15854 test_230h() {
15855         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15856         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15857         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15858                 skip "Need MDS version at least 2.11.52"
15859
15860         local mdt_index
15861
15862         mkdir -p $DIR/$tdir/migrate_dir
15863
15864         $LFS migrate -m1 $DIR &&
15865                 error "migrating mountpoint1 should fail"
15866
15867         $LFS migrate -m1 $DIR/$tdir/.. &&
15868                 error "migrating mountpoint2 should fail"
15869
15870         # same as mv
15871         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
15872                 error "migrating $tdir/migrate_dir/.. should fail"
15873
15874         true
15875 }
15876 run_test 230h "migrate .. and root"
15877
15878 test_230i() {
15879         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15880         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15881         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15882                 skip "Need MDS version at least 2.11.52"
15883
15884         mkdir -p $DIR/$tdir/migrate_dir
15885
15886         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
15887                 error "migration fails with a tailing slash"
15888
15889         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
15890                 error "migration fails with two tailing slashes"
15891 }
15892 run_test 230i "lfs migrate -m tolerates trailing slashes"
15893
15894 test_230j() {
15895         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
15896         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15897                 skip "Need MDS version at least 2.11.52"
15898
15899         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
15900         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
15901                 error "create $tfile failed"
15902         cat /etc/passwd > $DIR/$tdir/$tfile
15903
15904         $LFS migrate -m 1 $DIR/$tdir
15905
15906         cmp /etc/passwd $DIR/$tdir/$tfile ||
15907                 error "DoM file mismatch after migration"
15908 }
15909 run_test 230j "DoM file data not changed after dir migration"
15910
15911 test_230k() {
15912         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
15913         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
15914                 skip "Need MDS version at least 2.11.56"
15915
15916         local total=20
15917         local files_on_starting_mdt=0
15918
15919         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
15920         $LFS getdirstripe $DIR/$tdir
15921         for i in $(seq $total); do
15922                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
15923                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
15924                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
15925         done
15926
15927         echo "$files_on_starting_mdt files on MDT0"
15928
15929         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
15930         $LFS getdirstripe $DIR/$tdir
15931
15932         files_on_starting_mdt=0
15933         for i in $(seq $total); do
15934                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
15935                         error "file $tfile.$i mismatch after migration"
15936                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
15937                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
15938         done
15939
15940         echo "$files_on_starting_mdt files on MDT1 after migration"
15941         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
15942
15943         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
15944         $LFS getdirstripe $DIR/$tdir
15945
15946         files_on_starting_mdt=0
15947         for i in $(seq $total); do
15948                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
15949                         error "file $tfile.$i mismatch after 2nd migration"
15950                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
15951                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
15952         done
15953
15954         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
15955         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
15956
15957         true
15958 }
15959 run_test 230k "file data not changed after dir migration"
15960
15961 test_230l() {
15962         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
15963         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
15964                 skip "Need MDS version at least 2.11.56"
15965
15966         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
15967         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
15968                 error "create files under remote dir failed $i"
15969         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
15970 }
15971 run_test 230l "readdir between MDTs won't crash"
15972
15973 test_231a()
15974 {
15975         # For simplicity this test assumes that max_pages_per_rpc
15976         # is the same across all OSCs
15977         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
15978         local bulk_size=$((max_pages * PAGE_SIZE))
15979         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
15980                                        head -n 1)
15981
15982         mkdir -p $DIR/$tdir
15983         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
15984                 error "failed to set stripe with -S ${brw_size}M option"
15985
15986         # clear the OSC stats
15987         $LCTL set_param osc.*.stats=0 &>/dev/null
15988         stop_writeback
15989
15990         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
15991         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
15992                 oflag=direct &>/dev/null || error "dd failed"
15993
15994         sync; sleep 1; sync # just to be safe
15995         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
15996         if [ x$nrpcs != "x1" ]; then
15997                 $LCTL get_param osc.*.stats
15998                 error "found $nrpcs ost_write RPCs, not 1 as expected"
15999         fi
16000
16001         start_writeback
16002         # Drop the OSC cache, otherwise we will read from it
16003         cancel_lru_locks osc
16004
16005         # clear the OSC stats
16006         $LCTL set_param osc.*.stats=0 &>/dev/null
16007
16008         # Client reads $bulk_size.
16009         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
16010                 iflag=direct &>/dev/null || error "dd failed"
16011
16012         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
16013         if [ x$nrpcs != "x1" ]; then
16014                 $LCTL get_param osc.*.stats
16015                 error "found $nrpcs ost_read RPCs, not 1 as expected"
16016         fi
16017 }
16018 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
16019
16020 test_231b() {
16021         mkdir -p $DIR/$tdir
16022         local i
16023         for i in {0..1023}; do
16024                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
16025                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
16026                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
16027         done
16028         sync
16029 }
16030 run_test 231b "must not assert on fully utilized OST request buffer"
16031
16032 test_232a() {
16033         mkdir -p $DIR/$tdir
16034         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
16035
16036         #define OBD_FAIL_LDLM_OST_LVB            0x31c
16037         do_facet ost1 $LCTL set_param fail_loc=0x31c
16038
16039         # ignore dd failure
16040         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
16041
16042         do_facet ost1 $LCTL set_param fail_loc=0
16043         umount_client $MOUNT || error "umount failed"
16044         mount_client $MOUNT || error "mount failed"
16045         stop ost1 || error "cannot stop ost1"
16046         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
16047 }
16048 run_test 232a "failed lock should not block umount"
16049
16050 test_232b() {
16051         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
16052                 skip "Need MDS version at least 2.10.58"
16053
16054         mkdir -p $DIR/$tdir
16055         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
16056         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
16057         sync
16058         cancel_lru_locks osc
16059
16060         #define OBD_FAIL_LDLM_OST_LVB            0x31c
16061         do_facet ost1 $LCTL set_param fail_loc=0x31c
16062
16063         # ignore failure
16064         $LFS data_version $DIR/$tdir/$tfile || true
16065
16066         do_facet ost1 $LCTL set_param fail_loc=0
16067         umount_client $MOUNT || error "umount failed"
16068         mount_client $MOUNT || error "mount failed"
16069         stop ost1 || error "cannot stop ost1"
16070         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
16071 }
16072 run_test 232b "failed data version lock should not block umount"
16073
16074 test_233a() {
16075         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
16076                 skip "Need MDS version at least 2.3.64"
16077         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
16078
16079         local fid=$($LFS path2fid $MOUNT)
16080
16081         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16082                 error "cannot access $MOUNT using its FID '$fid'"
16083 }
16084 run_test 233a "checking that OBF of the FS root succeeds"
16085
16086 test_233b() {
16087         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
16088                 skip "Need MDS version at least 2.5.90"
16089         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
16090
16091         local fid=$($LFS path2fid $MOUNT/.lustre)
16092
16093         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16094                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
16095
16096         fid=$($LFS path2fid $MOUNT/.lustre/fid)
16097         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16098                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
16099 }
16100 run_test 233b "checking that OBF of the FS .lustre succeeds"
16101
16102 test_234() {
16103         local p="$TMP/sanityN-$TESTNAME.parameters"
16104         save_lustre_params client "llite.*.xattr_cache" > $p
16105         lctl set_param llite.*.xattr_cache 1 ||
16106                 skip_env "xattr cache is not supported"
16107
16108         mkdir -p $DIR/$tdir || error "mkdir failed"
16109         touch $DIR/$tdir/$tfile || error "touch failed"
16110         # OBD_FAIL_LLITE_XATTR_ENOMEM
16111         $LCTL set_param fail_loc=0x1405
16112         getfattr -n user.attr $DIR/$tdir/$tfile &&
16113                 error "getfattr should have failed with ENOMEM"
16114         $LCTL set_param fail_loc=0x0
16115         rm -rf $DIR/$tdir
16116
16117         restore_lustre_params < $p
16118         rm -f $p
16119 }
16120 run_test 234 "xattr cache should not crash on ENOMEM"
16121
16122 test_235() {
16123         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
16124                 skip "Need MDS version at least 2.4.52"
16125
16126         flock_deadlock $DIR/$tfile
16127         local RC=$?
16128         case $RC in
16129                 0)
16130                 ;;
16131                 124) error "process hangs on a deadlock"
16132                 ;;
16133                 *) error "error executing flock_deadlock $DIR/$tfile"
16134                 ;;
16135         esac
16136 }
16137 run_test 235 "LU-1715: flock deadlock detection does not work properly"
16138
16139 #LU-2935
16140 test_236() {
16141         check_swap_layouts_support
16142
16143         local ref1=/etc/passwd
16144         local ref2=/etc/group
16145         local file1=$DIR/$tdir/f1
16146         local file2=$DIR/$tdir/f2
16147
16148         test_mkdir -c1 $DIR/$tdir
16149         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
16150         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
16151         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
16152         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
16153         local fd=$(free_fd)
16154         local cmd="exec $fd<>$file2"
16155         eval $cmd
16156         rm $file2
16157         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
16158                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
16159         cmd="exec $fd>&-"
16160         eval $cmd
16161         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
16162
16163         #cleanup
16164         rm -rf $DIR/$tdir
16165 }
16166 run_test 236 "Layout swap on open unlinked file"
16167
16168 # LU-4659 linkea consistency
16169 test_238() {
16170         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
16171                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
16172                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
16173                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
16174
16175         touch $DIR/$tfile
16176         ln $DIR/$tfile $DIR/$tfile.lnk
16177         touch $DIR/$tfile.new
16178         mv $DIR/$tfile.new $DIR/$tfile
16179         local fid1=$($LFS path2fid $DIR/$tfile)
16180         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
16181         local path1=$($LFS fid2path $FSNAME "$fid1")
16182         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
16183         local path2=$($LFS fid2path $FSNAME "$fid2")
16184         [ $tfile.lnk == $path2 ] ||
16185                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
16186         rm -f $DIR/$tfile*
16187 }
16188 run_test 238 "Verify linkea consistency"
16189
16190 test_239A() { # was test_239
16191         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
16192                 skip "Need MDS version at least 2.5.60"
16193
16194         local list=$(comma_list $(mdts_nodes))
16195
16196         mkdir -p $DIR/$tdir
16197         createmany -o $DIR/$tdir/f- 5000
16198         unlinkmany $DIR/$tdir/f- 5000
16199         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
16200                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
16201         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
16202                         osp.*MDT*.sync_in_flight" | calc_sum)
16203         [ "$changes" -eq 0 ] || error "$changes not synced"
16204 }
16205 run_test 239A "osp_sync test"
16206
16207 test_239a() { #LU-5297
16208         remote_mds_nodsh && skip "remote MDS with nodsh"
16209
16210         touch $DIR/$tfile
16211         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
16212         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
16213         chgrp $RUNAS_GID $DIR/$tfile
16214         wait_delete_completed
16215 }
16216 run_test 239a "process invalid osp sync record correctly"
16217
16218 test_239b() { #LU-5297
16219         remote_mds_nodsh && skip "remote MDS with nodsh"
16220
16221         touch $DIR/$tfile1
16222         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
16223         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
16224         chgrp $RUNAS_GID $DIR/$tfile1
16225         wait_delete_completed
16226         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
16227         touch $DIR/$tfile2
16228         chgrp $RUNAS_GID $DIR/$tfile2
16229         wait_delete_completed
16230 }
16231 run_test 239b "process osp sync record with ENOMEM error correctly"
16232
16233 test_240() {
16234         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16235         remote_mds_nodsh && skip "remote MDS with nodsh"
16236
16237         mkdir -p $DIR/$tdir
16238
16239         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
16240                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
16241         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
16242                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
16243
16244         umount_client $MOUNT || error "umount failed"
16245         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
16246         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
16247         mount_client $MOUNT || error "failed to mount client"
16248
16249         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
16250         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
16251 }
16252 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
16253
16254 test_241_bio() {
16255         local count=$1
16256         local bsize=$2
16257
16258         for LOOP in $(seq $count); do
16259                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
16260                 cancel_lru_locks $OSC || true
16261         done
16262 }
16263
16264 test_241_dio() {
16265         local count=$1
16266         local bsize=$2
16267
16268         for LOOP in $(seq $1); do
16269                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
16270                         2>/dev/null
16271         done
16272 }
16273
16274 test_241a() { # was test_241
16275         local bsize=$PAGE_SIZE
16276
16277         (( bsize < 40960 )) && bsize=40960
16278         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
16279         ls -la $DIR/$tfile
16280         cancel_lru_locks $OSC
16281         test_241_bio 1000 $bsize &
16282         PID=$!
16283         test_241_dio 1000 $bsize
16284         wait $PID
16285 }
16286 run_test 241a "bio vs dio"
16287
16288 test_241b() {
16289         local bsize=$PAGE_SIZE
16290
16291         (( bsize < 40960 )) && bsize=40960
16292         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
16293         ls -la $DIR/$tfile
16294         test_241_dio 1000 $bsize &
16295         PID=$!
16296         test_241_dio 1000 $bsize
16297         wait $PID
16298 }
16299 run_test 241b "dio vs dio"
16300
16301 test_242() {
16302         remote_mds_nodsh && skip "remote MDS with nodsh"
16303
16304         mkdir -p $DIR/$tdir
16305         touch $DIR/$tdir/$tfile
16306
16307         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
16308         do_facet mds1 lctl set_param fail_loc=0x105
16309         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
16310
16311         do_facet mds1 lctl set_param fail_loc=0
16312         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
16313 }
16314 run_test 242 "mdt_readpage failure should not cause directory unreadable"
16315
16316 test_243()
16317 {
16318         test_mkdir $DIR/$tdir
16319         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
16320 }
16321 run_test 243 "various group lock tests"
16322
16323 test_244()
16324 {
16325         test_mkdir $DIR/$tdir
16326         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
16327         sendfile_grouplock $DIR/$tdir/$tfile || \
16328                 error "sendfile+grouplock failed"
16329         rm -rf $DIR/$tdir
16330 }
16331 run_test 244 "sendfile with group lock tests"
16332
16333 test_245() {
16334         local flagname="multi_mod_rpcs"
16335         local connect_data_name="max_mod_rpcs"
16336         local out
16337
16338         # check if multiple modify RPCs flag is set
16339         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
16340                 grep "connect_flags:")
16341         echo "$out"
16342
16343         echo "$out" | grep -qw $flagname
16344         if [ $? -ne 0 ]; then
16345                 echo "connect flag $flagname is not set"
16346                 return
16347         fi
16348
16349         # check if multiple modify RPCs data is set
16350         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
16351         echo "$out"
16352
16353         echo "$out" | grep -qw $connect_data_name ||
16354                 error "import should have connect data $connect_data_name"
16355 }
16356 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
16357
16358 test_246() { # LU-7371
16359         remote_ost_nodsh && skip "remote OST with nodsh"
16360         [ $OST1_VERSION -lt $(version_code 2.7.62) ] &&
16361                 skip "Need OST version >= 2.7.62"
16362
16363         do_facet ost1 $LCTL set_param fail_val=4095
16364 #define OBD_FAIL_OST_READ_SIZE          0x234
16365         do_facet ost1 $LCTL set_param fail_loc=0x234
16366         $LFS setstripe $DIR/$tfile -i 0 -c 1
16367         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
16368         cancel_lru_locks $FSNAME-OST0000
16369         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
16370 }
16371 run_test 246 "Read file of size 4095 should return right length"
16372
16373 cleanup_247() {
16374         local submount=$1
16375
16376         trap 0
16377         umount_client $submount
16378         rmdir $submount
16379 }
16380
16381 test_247a() {
16382         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
16383                 grep -q subtree ||
16384                 skip_env "Fileset feature is not supported"
16385
16386         local submount=${MOUNT}_$tdir
16387
16388         mkdir $MOUNT/$tdir
16389         mkdir -p $submount || error "mkdir $submount failed"
16390         FILESET="$FILESET/$tdir" mount_client $submount ||
16391                 error "mount $submount failed"
16392         trap "cleanup_247 $submount" EXIT
16393         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
16394         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
16395                 error "read $MOUNT/$tdir/$tfile failed"
16396         cleanup_247 $submount
16397 }
16398 run_test 247a "mount subdir as fileset"
16399
16400 test_247b() {
16401         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
16402                 skip_env "Fileset feature is not supported"
16403
16404         local submount=${MOUNT}_$tdir
16405
16406         rm -rf $MOUNT/$tdir
16407         mkdir -p $submount || error "mkdir $submount failed"
16408         SKIP_FILESET=1
16409         FILESET="$FILESET/$tdir" mount_client $submount &&
16410                 error "mount $submount should fail"
16411         rmdir $submount
16412 }
16413 run_test 247b "mount subdir that dose not exist"
16414
16415 test_247c() {
16416         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
16417                 skip_env "Fileset feature is not supported"
16418
16419         local submount=${MOUNT}_$tdir
16420
16421         mkdir -p $MOUNT/$tdir/dir1
16422         mkdir -p $submount || error "mkdir $submount failed"
16423         trap "cleanup_247 $submount" EXIT
16424         FILESET="$FILESET/$tdir" mount_client $submount ||
16425                 error "mount $submount failed"
16426         local fid=$($LFS path2fid $MOUNT/)
16427         $LFS fid2path $submount $fid && error "fid2path should fail"
16428         cleanup_247 $submount
16429 }
16430 run_test 247c "running fid2path outside root"
16431
16432 test_247d() {
16433         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
16434                 skip "Fileset feature is not supported"
16435
16436         local submount=${MOUNT}_$tdir
16437
16438         mkdir -p $MOUNT/$tdir/dir1
16439         mkdir -p $submount || error "mkdir $submount failed"
16440         FILESET="$FILESET/$tdir" mount_client $submount ||
16441                 error "mount $submount failed"
16442         trap "cleanup_247 $submount" EXIT
16443         local fid=$($LFS path2fid $submount/dir1)
16444         $LFS fid2path $submount $fid || error "fid2path should succeed"
16445         cleanup_247 $submount
16446 }
16447 run_test 247d "running fid2path inside root"
16448
16449 # LU-8037
16450 test_247e() {
16451         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
16452                 grep -q subtree ||
16453                 skip "Fileset feature is not supported"
16454
16455         local submount=${MOUNT}_$tdir
16456
16457         mkdir $MOUNT/$tdir
16458         mkdir -p $submount || error "mkdir $submount failed"
16459         FILESET="$FILESET/.." mount_client $submount &&
16460                 error "mount $submount should fail"
16461         rmdir $submount
16462 }
16463 run_test 247e "mount .. as fileset"
16464
16465 test_248() {
16466         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
16467         [ -z "$fast_read_sav" ] && skip "no fast read support"
16468
16469         # create a large file for fast read verification
16470         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
16471
16472         # make sure the file is created correctly
16473         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
16474                 { rm -f $DIR/$tfile; skip "file creation error"; }
16475
16476         echo "Test 1: verify that fast read is 4 times faster on cache read"
16477
16478         # small read with fast read enabled
16479         $LCTL set_param -n llite.*.fast_read=1
16480         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
16481                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16482                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16483         # small read with fast read disabled
16484         $LCTL set_param -n llite.*.fast_read=0
16485         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
16486                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16487                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16488
16489         # verify that fast read is 4 times faster for cache read
16490         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
16491                 error_not_in_vm "fast read was not 4 times faster: " \
16492                            "$t_fast vs $t_slow"
16493
16494         echo "Test 2: verify the performance between big and small read"
16495         $LCTL set_param -n llite.*.fast_read=1
16496
16497         # 1k non-cache read
16498         cancel_lru_locks osc
16499         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
16500                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16501                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16502
16503         # 1M non-cache read
16504         cancel_lru_locks osc
16505         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
16506                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16507                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16508
16509         # verify that big IO is not 4 times faster than small IO
16510         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
16511                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
16512
16513         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
16514         rm -f $DIR/$tfile
16515 }
16516 run_test 248 "fast read verification"
16517
16518 test_249() { # LU-7890
16519         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
16520                 skip "Need at least version 2.8.54"
16521
16522         rm -f $DIR/$tfile
16523         $LFS setstripe -c 1 $DIR/$tfile
16524         # Offset 2T == 4k * 512M
16525         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
16526                 error "dd to 2T offset failed"
16527 }
16528 run_test 249 "Write above 2T file size"
16529
16530 test_250() {
16531         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
16532          && skip "no 16TB file size limit on ZFS"
16533
16534         $LFS setstripe -c 1 $DIR/$tfile
16535         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
16536         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
16537         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
16538         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
16539                 conv=notrunc,fsync && error "append succeeded"
16540         return 0
16541 }
16542 run_test 250 "Write above 16T limit"
16543
16544 test_251() {
16545         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
16546
16547         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
16548         #Skip once - writing the first stripe will succeed
16549         $LCTL set_param fail_loc=0xa0001407 fail_val=1
16550         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
16551                 error "short write happened"
16552
16553         $LCTL set_param fail_loc=0xa0001407 fail_val=1
16554         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
16555                 error "short read happened"
16556
16557         rm -f $DIR/$tfile
16558 }
16559 run_test 251 "Handling short read and write correctly"
16560
16561 test_252() {
16562         remote_mds_nodsh && skip "remote MDS with nodsh"
16563         remote_ost_nodsh && skip "remote OST with nodsh"
16564         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
16565                 skip_env "ldiskfs only test"
16566         fi
16567
16568         local tgt
16569         local dev
16570         local out
16571         local uuid
16572         local num
16573         local gen
16574
16575         # check lr_reader on OST0000
16576         tgt=ost1
16577         dev=$(facet_device $tgt)
16578         out=$(do_facet $tgt $LR_READER $dev)
16579         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
16580         echo "$out"
16581         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
16582         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
16583                 error "Invalid uuid returned by $LR_READER on target $tgt"
16584         echo -e "uuid returned by $LR_READER is '$uuid'\n"
16585
16586         # check lr_reader -c on MDT0000
16587         tgt=mds1
16588         dev=$(facet_device $tgt)
16589         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
16590                 skip "$LR_READER does not support additional options"
16591         fi
16592         out=$(do_facet $tgt $LR_READER -c $dev)
16593         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
16594         echo "$out"
16595         num=$(echo "$out" | grep -c "mdtlov")
16596         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
16597                 error "Invalid number of mdtlov clients returned by $LR_READER"
16598         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
16599
16600         # check lr_reader -cr on MDT0000
16601         out=$(do_facet $tgt $LR_READER -cr $dev)
16602         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
16603         echo "$out"
16604         echo "$out" | grep -q "^reply_data:$" ||
16605                 error "$LR_READER should have returned 'reply_data' section"
16606         num=$(echo "$out" | grep -c "client_generation")
16607         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
16608 }
16609 run_test 252 "check lr_reader tool"
16610
16611 test_253_fill_ost() {
16612         local size_mb #how many MB should we write to pass watermark
16613         local lwm=$3  #low watermark
16614         local free_10mb #10% of free space
16615
16616         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
16617         size_mb=$((free_kb / 1024 - lwm))
16618         free_10mb=$((free_kb / 10240))
16619         #If 10% of free space cross low watermark use it
16620         if (( free_10mb > size_mb )); then
16621                 size_mb=$free_10mb
16622         else
16623                 #At least we need to store 1.1 of difference between
16624                 #free space and low watermark
16625                 size_mb=$((size_mb + size_mb / 10))
16626         fi
16627         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
16628                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
16629                          oflag=append conv=notrunc
16630         fi
16631
16632         sleep_maxage
16633
16634         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
16635         echo "OST still has $((free_kb / 1024)) mbytes free"
16636 }
16637
16638 test_253() {
16639         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16640         remote_mds_nodsh && skip "remote MDS with nodsh"
16641         remote_mgs_nodsh && skip "remote MGS with nodsh"
16642
16643         local ostidx=0
16644         local rc=0
16645
16646         local ost_name=$($LFS osts |
16647                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
16648         # on the mdt's osc
16649         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
16650         do_facet $SINGLEMDS $LCTL get_param -n \
16651                 osp.$mdtosc_proc1.reserved_mb_high ||
16652                 skip  "remote MDS does not support reserved_mb_high"
16653
16654         rm -rf $DIR/$tdir
16655         wait_mds_ost_sync
16656         wait_delete_completed
16657         mkdir $DIR/$tdir
16658
16659         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
16660                         osp.$mdtosc_proc1.reserved_mb_high)
16661         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
16662                         osp.$mdtosc_proc1.reserved_mb_low)
16663         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
16664
16665         if ! combined_mgs_mds ; then
16666                 mount_mgs_client
16667         fi
16668         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
16669         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
16670                 error "Adding $ost_name to pool failed"
16671
16672         # Wait for client to see a OST at pool
16673         wait_update $HOSTNAME "$LCTL get_param -n
16674                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
16675                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
16676                 error "Client can not see the pool"
16677         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
16678                 error "Setstripe failed"
16679
16680         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
16681         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
16682         echo "OST still has $((blocks/1024)) mbytes free"
16683
16684         local new_lwm=$((blocks/1024-10))
16685         do_facet $SINGLEMDS $LCTL set_param \
16686                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
16687         do_facet $SINGLEMDS $LCTL set_param \
16688                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
16689
16690         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
16691
16692         #First enospc could execute orphan deletion so repeat.
16693         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
16694
16695         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
16696                         osp.$mdtosc_proc1.prealloc_status)
16697         echo "prealloc_status $oa_status"
16698
16699         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
16700                 error "File creation should fail"
16701         #object allocation was stopped, but we still able to append files
16702         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
16703                 error "Append failed"
16704         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
16705
16706         wait_delete_completed
16707
16708         sleep_maxage
16709
16710         for i in $(seq 10 12); do
16711                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
16712                         error "File creation failed after rm";
16713         done
16714
16715         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
16716                         osp.$mdtosc_proc1.prealloc_status)
16717         echo "prealloc_status $oa_status"
16718
16719         if (( oa_status != 0 )); then
16720                 error "Object allocation still disable after rm"
16721         fi
16722         do_facet $SINGLEMDS $LCTL set_param \
16723                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
16724         do_facet $SINGLEMDS $LCTL set_param \
16725                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
16726
16727
16728         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
16729                 error "Remove $ost_name from pool failed"
16730         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
16731                 error "Pool destroy fialed"
16732
16733         if ! combined_mgs_mds ; then
16734                 umount_mgs_client
16735         fi
16736 }
16737 run_test 253 "Check object allocation limit"
16738
16739 test_254() {
16740         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16741         remote_mds_nodsh && skip "remote MDS with nodsh"
16742         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
16743                 skip "MDS does not support changelog_size"
16744
16745         local cl_user
16746         local MDT0=$(facet_svc $SINGLEMDS)
16747
16748         changelog_register || error "changelog_register failed"
16749
16750         changelog_clear 0 || error "changelog_clear failed"
16751
16752         local size1=$(do_facet $SINGLEMDS \
16753                       $LCTL get_param -n mdd.$MDT0.changelog_size)
16754         echo "Changelog size $size1"
16755
16756         rm -rf $DIR/$tdir
16757         $LFS mkdir -i 0 $DIR/$tdir
16758         # change something
16759         mkdir -p $DIR/$tdir/pics/2008/zachy
16760         touch $DIR/$tdir/pics/2008/zachy/timestamp
16761         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
16762         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
16763         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
16764         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
16765         rm $DIR/$tdir/pics/desktop.jpg
16766
16767         local size2=$(do_facet $SINGLEMDS \
16768                       $LCTL get_param -n mdd.$MDT0.changelog_size)
16769         echo "Changelog size after work $size2"
16770
16771         (( $size2 > $size1 )) ||
16772                 error "new Changelog size=$size2 less than old size=$size1"
16773 }
16774 run_test 254 "Check changelog size"
16775
16776 ladvise_no_type()
16777 {
16778         local type=$1
16779         local file=$2
16780
16781         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
16782                 awk -F: '{print $2}' | grep $type > /dev/null
16783         if [ $? -ne 0 ]; then
16784                 return 0
16785         fi
16786         return 1
16787 }
16788
16789 ladvise_no_ioctl()
16790 {
16791         local file=$1
16792
16793         lfs ladvise -a willread $file > /dev/null 2>&1
16794         if [ $? -eq 0 ]; then
16795                 return 1
16796         fi
16797
16798         lfs ladvise -a willread $file 2>&1 |
16799                 grep "Inappropriate ioctl for device" > /dev/null
16800         if [ $? -eq 0 ]; then
16801                 return 0
16802         fi
16803         return 1
16804 }
16805
16806 percent() {
16807         bc <<<"scale=2; ($1 - $2) * 100 / $2"
16808 }
16809
16810 # run a random read IO workload
16811 # usage: random_read_iops <filename> <filesize> <iosize>
16812 random_read_iops() {
16813         local file=$1
16814         local fsize=$2
16815         local iosize=${3:-4096}
16816
16817         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
16818                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
16819 }
16820
16821 drop_file_oss_cache() {
16822         local file="$1"
16823         local nodes="$2"
16824
16825         $LFS ladvise -a dontneed $file 2>/dev/null ||
16826                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
16827 }
16828
16829 ladvise_willread_performance()
16830 {
16831         local repeat=10
16832         local average_origin=0
16833         local average_cache=0
16834         local average_ladvise=0
16835
16836         for ((i = 1; i <= $repeat; i++)); do
16837                 echo "Iter $i/$repeat: reading without willread hint"
16838                 cancel_lru_locks osc
16839                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
16840                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
16841                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
16842                 average_origin=$(bc <<<"$average_origin + $speed_origin")
16843
16844                 cancel_lru_locks osc
16845                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
16846                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
16847                 average_cache=$(bc <<<"$average_cache + $speed_cache")
16848
16849                 cancel_lru_locks osc
16850                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
16851                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
16852                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
16853                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
16854                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
16855         done
16856         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
16857         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
16858         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
16859
16860         speedup_cache=$(percent $average_cache $average_origin)
16861         speedup_ladvise=$(percent $average_ladvise $average_origin)
16862
16863         echo "Average uncached read: $average_origin"
16864         echo "Average speedup with OSS cached read: " \
16865                 "$average_cache = +$speedup_cache%"
16866         echo "Average speedup with ladvise willread: " \
16867                 "$average_ladvise = +$speedup_ladvise%"
16868
16869         local lowest_speedup=20
16870         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
16871                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
16872                         "got $average_cache%. Skipping ladvise willread check."
16873                 return 0
16874         fi
16875
16876         # the test won't work on ZFS until it supports 'ladvise dontneed', but
16877         # it is still good to run until then to exercise 'ladvise willread'
16878         ! $LFS ladvise -a dontneed $DIR/$tfile &&
16879                 [ "$ost1_FSTYPE" = "zfs" ] &&
16880                 echo "osd-zfs does not support dontneed or drop_caches" &&
16881                 return 0
16882
16883         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
16884         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
16885                 error_not_in_vm "Speedup with willread is less than " \
16886                         "$lowest_speedup%, got $average_ladvise%"
16887 }
16888
16889 test_255a() {
16890         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
16891                 skip "lustre < 2.8.54 does not support ladvise "
16892         remote_ost_nodsh && skip "remote OST with nodsh"
16893
16894         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
16895
16896         ladvise_no_type willread $DIR/$tfile &&
16897                 skip "willread ladvise is not supported"
16898
16899         ladvise_no_ioctl $DIR/$tfile &&
16900                 skip "ladvise ioctl is not supported"
16901
16902         local size_mb=100
16903         local size=$((size_mb * 1048576))
16904         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
16905                 error "dd to $DIR/$tfile failed"
16906
16907         lfs ladvise -a willread $DIR/$tfile ||
16908                 error "Ladvise failed with no range argument"
16909
16910         lfs ladvise -a willread -s 0 $DIR/$tfile ||
16911                 error "Ladvise failed with no -l or -e argument"
16912
16913         lfs ladvise -a willread -e 1 $DIR/$tfile ||
16914                 error "Ladvise failed with only -e argument"
16915
16916         lfs ladvise -a willread -l 1 $DIR/$tfile ||
16917                 error "Ladvise failed with only -l argument"
16918
16919         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
16920                 error "End offset should not be smaller than start offset"
16921
16922         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
16923                 error "End offset should not be equal to start offset"
16924
16925         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
16926                 error "Ladvise failed with overflowing -s argument"
16927
16928         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
16929                 error "Ladvise failed with overflowing -e argument"
16930
16931         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
16932                 error "Ladvise failed with overflowing -l argument"
16933
16934         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
16935                 error "Ladvise succeeded with conflicting -l and -e arguments"
16936
16937         echo "Synchronous ladvise should wait"
16938         local delay=4
16939 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
16940         do_nodes $(comma_list $(osts_nodes)) \
16941                 $LCTL set_param fail_val=$delay fail_loc=0x237
16942
16943         local start_ts=$SECONDS
16944         lfs ladvise -a willread $DIR/$tfile ||
16945                 error "Ladvise failed with no range argument"
16946         local end_ts=$SECONDS
16947         local inteval_ts=$((end_ts - start_ts))
16948
16949         if [ $inteval_ts -lt $(($delay - 1)) ]; then
16950                 error "Synchronous advice didn't wait reply"
16951         fi
16952
16953         echo "Asynchronous ladvise shouldn't wait"
16954         local start_ts=$SECONDS
16955         lfs ladvise -a willread -b $DIR/$tfile ||
16956                 error "Ladvise failed with no range argument"
16957         local end_ts=$SECONDS
16958         local inteval_ts=$((end_ts - start_ts))
16959
16960         if [ $inteval_ts -gt $(($delay / 2)) ]; then
16961                 error "Asynchronous advice blocked"
16962         fi
16963
16964         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
16965         ladvise_willread_performance
16966 }
16967 run_test 255a "check 'lfs ladvise -a willread'"
16968
16969 facet_meminfo() {
16970         local facet=$1
16971         local info=$2
16972
16973         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
16974 }
16975
16976 test_255b() {
16977         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
16978                 skip "lustre < 2.8.54 does not support ladvise "
16979         remote_ost_nodsh && skip "remote OST with nodsh"
16980
16981         lfs setstripe -c 1 -i 0 $DIR/$tfile
16982
16983         ladvise_no_type dontneed $DIR/$tfile &&
16984                 skip "dontneed ladvise is not supported"
16985
16986         ladvise_no_ioctl $DIR/$tfile &&
16987                 skip "ladvise ioctl is not supported"
16988
16989         ! $LFS ladvise -a dontneed $DIR/$tfile &&
16990                 [ "$ost1_FSTYPE" = "zfs" ] &&
16991                 skip "zfs-osd does not support 'ladvise dontneed'"
16992
16993         local size_mb=100
16994         local size=$((size_mb * 1048576))
16995         # In order to prevent disturbance of other processes, only check 3/4
16996         # of the memory usage
16997         local kibibytes=$((size_mb * 1024 * 3 / 4))
16998
16999         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
17000                 error "dd to $DIR/$tfile failed"
17001
17002         #force write to complete before dropping OST cache & checking memory
17003         sync
17004
17005         local total=$(facet_meminfo ost1 MemTotal)
17006         echo "Total memory: $total KiB"
17007
17008         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
17009         local before_read=$(facet_meminfo ost1 Cached)
17010         echo "Cache used before read: $before_read KiB"
17011
17012         lfs ladvise -a willread $DIR/$tfile ||
17013                 error "Ladvise willread failed"
17014         local after_read=$(facet_meminfo ost1 Cached)
17015         echo "Cache used after read: $after_read KiB"
17016
17017         lfs ladvise -a dontneed $DIR/$tfile ||
17018                 error "Ladvise dontneed again failed"
17019         local no_read=$(facet_meminfo ost1 Cached)
17020         echo "Cache used after dontneed ladvise: $no_read KiB"
17021
17022         if [ $total -lt $((before_read + kibibytes)) ]; then
17023                 echo "Memory is too small, abort checking"
17024                 return 0
17025         fi
17026
17027         if [ $((before_read + kibibytes)) -gt $after_read ]; then
17028                 error "Ladvise willread should use more memory" \
17029                         "than $kibibytes KiB"
17030         fi
17031
17032         if [ $((no_read + kibibytes)) -gt $after_read ]; then
17033                 error "Ladvise dontneed should release more memory" \
17034                         "than $kibibytes KiB"
17035         fi
17036 }
17037 run_test 255b "check 'lfs ladvise -a dontneed'"
17038
17039 test_255c() {
17040         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
17041                 skip "lustre < 2.10.53 does not support lockahead"
17042
17043         local count
17044         local new_count
17045         local difference
17046         local i
17047         local rc
17048
17049         test_mkdir -p $DIR/$tdir
17050         $LFS setstripe -i 0 -c 1 $DIR/$tdir
17051
17052         #test 10 returns only success/failure
17053         i=10
17054         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17055         rc=$?
17056         if [ $rc -eq 255 ]; then
17057                 error "Ladvise test${i} failed, ${rc}"
17058         fi
17059
17060         #test 11 counts lock enqueue requests, all others count new locks
17061         i=11
17062         count=$(do_facet ost1 \
17063                 $LCTL get_param -n ost.OSS.ost.stats)
17064         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
17065
17066         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17067         rc=$?
17068         if [ $rc -eq 255 ]; then
17069                 error "Ladvise test${i} failed, ${rc}"
17070         fi
17071
17072         new_count=$(do_facet ost1 \
17073                 $LCTL get_param -n ost.OSS.ost.stats)
17074         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
17075                    awk '{ print $2 }')
17076
17077         difference="$((new_count - count))"
17078         if [ $difference -ne $rc ]; then
17079                 error "Ladvise test${i}, bad enqueue count, returned " \
17080                       "${rc}, actual ${difference}"
17081         fi
17082
17083         for i in $(seq 12 21); do
17084                 # If we do not do this, we run the risk of having too many
17085                 # locks and starting lock cancellation while we are checking
17086                 # lock counts.
17087                 cancel_lru_locks osc
17088
17089                 count=$($LCTL get_param -n \
17090                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
17091
17092                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
17093                 rc=$?
17094                 if [ $rc -eq 255 ]; then
17095                         error "Ladvise test ${i} failed, ${rc}"
17096                 fi
17097
17098                 new_count=$($LCTL get_param -n \
17099                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
17100                 difference="$((new_count - count))"
17101
17102                 # Test 15 output is divided by 100 to map down to valid return
17103                 if [ $i -eq 15 ]; then
17104                         rc="$((rc * 100))"
17105                 fi
17106
17107                 if [ $difference -ne $rc ]; then
17108                         error "Ladvise test ${i}, bad lock count, returned " \
17109                               "${rc}, actual ${difference}"
17110                 fi
17111         done
17112
17113         #test 22 returns only success/failure
17114         i=22
17115         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17116         rc=$?
17117         if [ $rc -eq 255 ]; then
17118                 error "Ladvise test${i} failed, ${rc}"
17119         fi
17120 }
17121 run_test 255c "suite of ladvise lockahead tests"
17122
17123 test_256() {
17124         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17125         remote_mds_nodsh && skip "remote MDS with nodsh"
17126         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
17127         changelog_users $SINGLEMDS | grep "^cl" &&
17128                 skip "active changelog user"
17129
17130         local cl_user
17131         local cat_sl
17132         local mdt_dev
17133
17134         mdt_dev=$(mdsdevname 1)
17135         echo $mdt_dev
17136
17137         changelog_register || error "changelog_register failed"
17138
17139         rm -rf $DIR/$tdir
17140         mkdir -p $DIR/$tdir
17141
17142         changelog_clear 0 || error "changelog_clear failed"
17143
17144         # change something
17145         touch $DIR/$tdir/{1..10}
17146
17147         # stop the MDT
17148         stop $SINGLEMDS || error "Fail to stop MDT"
17149
17150         # remount the MDT
17151
17152         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
17153
17154         #after mount new plainllog is used
17155         touch $DIR/$tdir/{11..19}
17156         local tmpfile=$(mktemp -u $tfile.XXXXXX)
17157         cat_sl=$(do_facet $SINGLEMDS "sync; \
17158                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
17159                  llog_reader $tmpfile | grep -c type=1064553b")
17160         do_facet $SINGLEMDS llog_reader $tmpfile
17161
17162         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
17163
17164         changelog_clear 0 || error "changelog_clear failed"
17165
17166         cat_sl=$(do_facet $SINGLEMDS "sync; \
17167                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
17168                  llog_reader $tmpfile | grep -c type=1064553b; rm -f $tmpfile")
17169
17170         if (( cat_sl == 2 )); then
17171                 error "Empty plain llog was not deleted from changelog catalog"
17172         elif (( cat_sl != 1 )); then
17173                 error "Active plain llog shouldn't be deleted from catalog"
17174         fi
17175 }
17176 run_test 256 "Check llog delete for empty and not full state"
17177
17178 test_257() {
17179         remote_mds_nodsh && skip "remote MDS with nodsh"
17180         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
17181                 skip "Need MDS version at least 2.8.55"
17182
17183         test_mkdir $DIR/$tdir
17184
17185         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
17186                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
17187         stat $DIR/$tdir
17188
17189 #define OBD_FAIL_MDS_XATTR_REP                  0x161
17190         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
17191         local facet=mds$((mdtidx + 1))
17192         set_nodes_failloc $(facet_active_host $facet) 0x80000161
17193         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
17194
17195         stop $facet || error "stop MDS failed"
17196         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
17197                 error "start MDS fail"
17198         wait_recovery_complete $facet
17199 }
17200 run_test 257 "xattr locks are not lost"
17201
17202 # Verify we take the i_mutex when security requires it
17203 test_258a() {
17204 #define OBD_FAIL_IMUTEX_SEC 0x141c
17205         $LCTL set_param fail_loc=0x141c
17206         touch $DIR/$tfile
17207         chmod u+s $DIR/$tfile
17208         chmod a+rwx $DIR/$tfile
17209         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
17210         RC=$?
17211         if [ $RC -ne 0 ]; then
17212                 error "error, failed to take i_mutex, rc=$?"
17213         fi
17214         rm -f $DIR/$tfile
17215 }
17216 run_test 258a
17217
17218 # Verify we do NOT take the i_mutex in the normal case
17219 test_258b() {
17220 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
17221         $LCTL set_param fail_loc=0x141d
17222         touch $DIR/$tfile
17223         chmod a+rwx $DIR
17224         chmod a+rw $DIR/$tfile
17225         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
17226         RC=$?
17227         if [ $RC -ne 0 ]; then
17228                 error "error, took i_mutex unnecessarily, rc=$?"
17229         fi
17230         rm -f $DIR/$tfile
17231
17232 }
17233 run_test 258b "verify i_mutex security behavior"
17234
17235 test_259() {
17236         local file=$DIR/$tfile
17237         local before
17238         local after
17239
17240         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
17241
17242         stack_trap "rm -f $file" EXIT
17243
17244         wait_delete_completed
17245         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
17246         echo "before: $before"
17247
17248         $LFS setstripe -i 0 -c 1 $file
17249         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
17250         sync_all_data
17251         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
17252         echo "after write: $after"
17253
17254 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
17255         do_facet ost1 $LCTL set_param fail_loc=0x2301
17256         $TRUNCATE $file 0
17257         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
17258         echo "after truncate: $after"
17259
17260         stop ost1
17261         do_facet ost1 $LCTL set_param fail_loc=0
17262         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17263         sleep 2
17264         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
17265         echo "after restart: $after"
17266         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
17267                 error "missing truncate?"
17268
17269         return 0
17270 }
17271 run_test 259 "crash at delayed truncate"
17272
17273 test_260() {
17274 #define OBD_FAIL_MDC_CLOSE               0x806
17275         $LCTL set_param fail_loc=0x80000806
17276         touch $DIR/$tfile
17277
17278 }
17279 run_test 260 "Check mdc_close fail"
17280
17281 ### Data-on-MDT sanity tests ###
17282 test_270a() {
17283         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17284                 skip "Need MDS version at least 2.10.55 for DoM"
17285
17286         # create DoM file
17287         local dom=$DIR/$tdir/dom_file
17288         local tmp=$DIR/$tdir/tmp_file
17289
17290         mkdir -p $DIR/$tdir
17291
17292         # basic checks for DoM component creation
17293         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
17294                 error "Can set MDT layout to non-first entry"
17295
17296         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
17297                 error "Can define multiple entries as MDT layout"
17298
17299         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
17300
17301         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
17302         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
17303         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
17304
17305         local mdtidx=$($LFS getstripe -m $dom)
17306         local mdtname=MDT$(printf %04x $mdtidx)
17307         local facet=mds$((mdtidx + 1))
17308         local space_check=1
17309
17310         # Skip free space checks with ZFS
17311         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
17312
17313         # write
17314         sync
17315         local size_tmp=$((65536 * 3))
17316         local mdtfree1=$(do_facet $facet \
17317                          lctl get_param -n osd*.*$mdtname.kbytesfree)
17318
17319         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
17320         # check also direct IO along write
17321         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
17322         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
17323         sync
17324         cmp $tmp $dom || error "file data is different"
17325         [ $(stat -c%s $dom) == $size_tmp ] ||
17326                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
17327         if [ $space_check == 1 ]; then
17328                 local mdtfree2=$(do_facet $facet \
17329                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
17330
17331                 # increase in usage from by $size_tmp
17332                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
17333                         error "MDT free space wrong after write: " \
17334                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
17335         fi
17336
17337         # truncate
17338         local size_dom=10000
17339
17340         $TRUNCATE $dom $size_dom
17341         [ $(stat -c%s $dom) == $size_dom ] ||
17342                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
17343         if [ $space_check == 1 ]; then
17344                 mdtfree1=$(do_facet $facet \
17345                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17346                 # decrease in usage from $size_tmp to new $size_dom
17347                 [ $(($mdtfree1 - $mdtfree2)) -ge \
17348                   $(((size_tmp - size_dom) / 1024)) ] ||
17349                         error "MDT free space is wrong after truncate: " \
17350                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
17351         fi
17352
17353         # append
17354         cat $tmp >> $dom
17355         sync
17356         size_dom=$((size_dom + size_tmp))
17357         [ $(stat -c%s $dom) == $size_dom ] ||
17358                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
17359         if [ $space_check == 1 ]; then
17360                 mdtfree2=$(do_facet $facet \
17361                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17362                 # increase in usage by $size_tmp from previous
17363                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
17364                         error "MDT free space is wrong after append: " \
17365                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
17366         fi
17367
17368         # delete
17369         rm $dom
17370         if [ $space_check == 1 ]; then
17371                 mdtfree1=$(do_facet $facet \
17372                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17373                 # decrease in usage by $size_dom from previous
17374                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
17375                         error "MDT free space is wrong after removal: " \
17376                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
17377         fi
17378
17379         # combined striping
17380         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
17381                 error "Can't create DoM + OST striping"
17382
17383         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
17384         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
17385         # check also direct IO along write
17386         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
17387         sync
17388         cmp $tmp $dom || error "file data is different"
17389         [ $(stat -c%s $dom) == $size_tmp ] ||
17390                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
17391         rm $dom $tmp
17392
17393         return 0
17394 }
17395 run_test 270a "DoM: basic functionality tests"
17396
17397 test_270b() {
17398         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17399                 skip "Need MDS version at least 2.10.55"
17400
17401         local dom=$DIR/$tdir/dom_file
17402         local max_size=1048576
17403
17404         mkdir -p $DIR/$tdir
17405         $LFS setstripe -E $max_size -L mdt $dom
17406
17407         # truncate over the limit
17408         $TRUNCATE $dom $(($max_size + 1)) &&
17409                 error "successful truncate over the maximum size"
17410         # write over the limit
17411         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
17412                 error "successful write over the maximum size"
17413         # append over the limit
17414         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
17415         echo "12345" >> $dom && error "successful append over the maximum size"
17416         rm $dom
17417
17418         return 0
17419 }
17420 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
17421
17422 test_270c() {
17423         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17424                 skip "Need MDS version at least 2.10.55"
17425
17426         mkdir -p $DIR/$tdir
17427         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17428
17429         # check files inherit DoM EA
17430         touch $DIR/$tdir/first
17431         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
17432                 error "bad pattern"
17433         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
17434                 error "bad stripe count"
17435         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
17436                 error "bad stripe size"
17437
17438         # check directory inherits DoM EA and uses it as default
17439         mkdir $DIR/$tdir/subdir
17440         touch $DIR/$tdir/subdir/second
17441         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
17442                 error "bad pattern in sub-directory"
17443         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
17444                 error "bad stripe count in sub-directory"
17445         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
17446                 error "bad stripe size in sub-directory"
17447         return 0
17448 }
17449 run_test 270c "DoM: DoM EA inheritance tests"
17450
17451 test_270d() {
17452         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17453                 skip "Need MDS version at least 2.10.55"
17454
17455         mkdir -p $DIR/$tdir
17456         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17457
17458         # inherit default DoM striping
17459         mkdir $DIR/$tdir/subdir
17460         touch $DIR/$tdir/subdir/f1
17461
17462         # change default directory striping
17463         $LFS setstripe -c 1 $DIR/$tdir/subdir
17464         touch $DIR/$tdir/subdir/f2
17465         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
17466                 error "wrong default striping in file 2"
17467         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
17468                 error "bad pattern in file 2"
17469         return 0
17470 }
17471 run_test 270d "DoM: change striping from DoM to RAID0"
17472
17473 test_270e() {
17474         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17475                 skip "Need MDS version at least 2.10.55"
17476
17477         mkdir -p $DIR/$tdir/dom
17478         mkdir -p $DIR/$tdir/norm
17479         DOMFILES=20
17480         NORMFILES=10
17481         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
17482         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
17483
17484         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
17485         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
17486
17487         # find DoM files by layout
17488         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
17489         [ $NUM -eq  $DOMFILES ] ||
17490                 error "lfs find -L: found $NUM, expected $DOMFILES"
17491         echo "Test 1: lfs find 20 DOM files by layout: OK"
17492
17493         # there should be 1 dir with default DOM striping
17494         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
17495         [ $NUM -eq  1 ] ||
17496                 error "lfs find -L: found $NUM, expected 1 dir"
17497         echo "Test 2: lfs find 1 DOM dir by layout: OK"
17498
17499         # find DoM files by stripe size
17500         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
17501         [ $NUM -eq  $DOMFILES ] ||
17502                 error "lfs find -S: found $NUM, expected $DOMFILES"
17503         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
17504
17505         # find files by stripe offset except DoM files
17506         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
17507         [ $NUM -eq  $NORMFILES ] ||
17508                 error "lfs find -i: found $NUM, expected $NORMFILES"
17509         echo "Test 5: lfs find no DOM files by stripe index: OK"
17510         return 0
17511 }
17512 run_test 270e "DoM: lfs find with DoM files test"
17513
17514 test_270f() {
17515         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17516                 skip "Need MDS version at least 2.10.55"
17517
17518         local mdtname=${FSNAME}-MDT0000-mdtlov
17519         local dom=$DIR/$tdir/dom_file
17520         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
17521                                                 lod.$mdtname.dom_stripesize)
17522         local dom_limit=131072
17523
17524         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
17525         local dom_current=$(do_facet mds1 $LCTL get_param -n \
17526                                                 lod.$mdtname.dom_stripesize)
17527         [ ${dom_limit} -eq ${dom_current} ] ||
17528                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
17529
17530         $LFS mkdir -i 0 -c 1 $DIR/$tdir
17531         $LFS setstripe -d $DIR/$tdir
17532         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
17533                 error "Can't set directory default striping"
17534
17535         # exceed maximum stripe size
17536         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
17537                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
17538         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
17539                 error "Able to create DoM component size more than LOD limit"
17540
17541         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
17542         dom_current=$(do_facet mds1 $LCTL get_param -n \
17543                                                 lod.$mdtname.dom_stripesize)
17544         [ 0 -eq ${dom_current} ] ||
17545                 error "Can't set zero DoM stripe limit"
17546         rm $dom
17547
17548         # attempt to create DoM file on server with disabled DoM should
17549         # remove DoM entry from layout and be succeed
17550         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
17551                 error "Can't create DoM file (DoM is disabled)"
17552         [ $($LFS getstripe -L $dom) == "mdt" ] &&
17553                 error "File has DoM component while DoM is disabled"
17554         rm $dom
17555
17556         # attempt to create DoM file with only DoM stripe should return error
17557         $LFS setstripe -E $dom_limit -L mdt $dom &&
17558                 error "Able to create DoM-only file while DoM is disabled"
17559
17560         # too low values to be aligned with smallest stripe size 64K
17561         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
17562         dom_current=$(do_facet mds1 $LCTL get_param -n \
17563                                                 lod.$mdtname.dom_stripesize)
17564         [ 30000 -eq ${dom_current} ] &&
17565                 error "Can set too small DoM stripe limit"
17566
17567         # 64K is a minimal stripe size in Lustre, expect limit of that size
17568         [ 65536 -eq ${dom_current} ] ||
17569                 error "Limit is not set to 64K but ${dom_current}"
17570
17571         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
17572         dom_current=$(do_facet mds1 $LCTL get_param -n \
17573                                                 lod.$mdtname.dom_stripesize)
17574         echo $dom_current
17575         [ 2147483648 -eq ${dom_current} ] &&
17576                 error "Can set too large DoM stripe limit"
17577
17578         do_facet mds1 $LCTL set_param -n \
17579                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
17580         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
17581                 error "Can't create DoM component size after limit change"
17582         do_facet mds1 $LCTL set_param -n \
17583                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
17584         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
17585                 error "Can't create DoM file after limit decrease"
17586         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
17587                 error "Can create big DoM component after limit decrease"
17588         touch ${dom}_def ||
17589                 error "Can't create file with old default layout"
17590
17591         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
17592         return 0
17593 }
17594 run_test 270f "DoM: maximum DoM stripe size checks"
17595
17596 test_271a() {
17597         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17598                 skip "Need MDS version at least 2.10.55"
17599
17600         local dom=$DIR/$tdir/dom
17601
17602         mkdir -p $DIR/$tdir
17603
17604         $LFS setstripe -E 1024K -L mdt $dom
17605
17606         lctl set_param -n mdc.*.stats=clear
17607         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
17608         cat $dom > /dev/null
17609         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
17610         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
17611         ls $dom
17612         rm -f $dom
17613 }
17614 run_test 271a "DoM: data is cached for read after write"
17615
17616 test_271b() {
17617         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17618                 skip "Need MDS version at least 2.10.55"
17619
17620         local dom=$DIR/$tdir/dom
17621
17622         mkdir -p $DIR/$tdir
17623
17624         $LFS setstripe -E 1024K -L mdt -E EOF $dom
17625
17626         lctl set_param -n mdc.*.stats=clear
17627         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
17628         cancel_lru_locks mdc
17629         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
17630         # second stat to check size is cached on client
17631         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
17632         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
17633         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
17634         rm -f $dom
17635 }
17636 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
17637
17638 test_271ba() {
17639         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17640                 skip "Need MDS version at least 2.10.55"
17641
17642         local dom=$DIR/$tdir/dom
17643
17644         mkdir -p $DIR/$tdir
17645
17646         $LFS setstripe -E 1024K -L mdt -E EOF $dom
17647
17648         lctl set_param -n mdc.*.stats=clear
17649         lctl set_param -n osc.*.stats=clear
17650         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
17651         cancel_lru_locks mdc
17652         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
17653         # second stat to check size is cached on client
17654         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
17655         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
17656         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
17657         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
17658         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
17659         rm -f $dom
17660 }
17661 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
17662
17663
17664 get_mdc_stats() {
17665         local mdtidx=$1
17666         local param=$2
17667         local mdt=MDT$(printf %04x $mdtidx)
17668
17669         if [ -z $param ]; then
17670                 lctl get_param -n mdc.*$mdt*.stats
17671         else
17672                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
17673         fi
17674 }
17675
17676 test_271c() {
17677         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17678                 skip "Need MDS version at least 2.10.55"
17679
17680         local dom=$DIR/$tdir/dom
17681
17682         mkdir -p $DIR/$tdir
17683
17684         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17685
17686         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
17687         local facet=mds$((mdtidx + 1))
17688
17689         cancel_lru_locks mdc
17690         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
17691         createmany -o $dom 1000
17692         lctl set_param -n mdc.*.stats=clear
17693         smalliomany -w $dom 1000 200
17694         get_mdc_stats $mdtidx
17695         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
17696         # Each file has 1 open, 1 IO enqueues, total 2000
17697         # but now we have also +1 getxattr for security.capability, total 3000
17698         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
17699         unlinkmany $dom 1000
17700
17701         cancel_lru_locks mdc
17702         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
17703         createmany -o $dom 1000
17704         lctl set_param -n mdc.*.stats=clear
17705         smalliomany -w $dom 1000 200
17706         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
17707         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
17708         # for OPEN and IO lock.
17709         [ $((enq - enq_2)) -ge 1000 ] ||
17710                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
17711         unlinkmany $dom 1000
17712         return 0
17713 }
17714 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
17715
17716 cleanup_271def_tests() {
17717         trap 0
17718         rm -f $1
17719 }
17720
17721 test_271d() {
17722         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
17723                 skip "Need MDS version at least 2.10.57"
17724
17725         local dom=$DIR/$tdir/dom
17726         local tmp=$TMP/$tfile
17727         trap "cleanup_271def_tests $tmp" EXIT
17728
17729         mkdir -p $DIR/$tdir
17730
17731         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17732
17733         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
17734
17735         cancel_lru_locks mdc
17736         dd if=/dev/urandom of=$tmp bs=1000 count=1
17737         dd if=$tmp of=$dom bs=1000 count=1
17738         cancel_lru_locks mdc
17739
17740         cat /etc/hosts >> $tmp
17741         lctl set_param -n mdc.*.stats=clear
17742
17743         # append data to the same file it should update local page
17744         echo "Append to the same page"
17745         cat /etc/hosts >> $dom
17746         local num=$(get_mdc_stats $mdtidx ost_read)
17747         local ra=$(get_mdc_stats $mdtidx req_active)
17748         local rw=$(get_mdc_stats $mdtidx req_waittime)
17749
17750         [ -z $num ] || error "$num READ RPC occured"
17751         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
17752         echo "... DONE"
17753
17754         # compare content
17755         cmp $tmp $dom || error "file miscompare"
17756
17757         cancel_lru_locks mdc
17758         lctl set_param -n mdc.*.stats=clear
17759
17760         echo "Open and read file"
17761         cat $dom > /dev/null
17762         local num=$(get_mdc_stats $mdtidx ost_read)
17763         local ra=$(get_mdc_stats $mdtidx req_active)
17764         local rw=$(get_mdc_stats $mdtidx req_waittime)
17765
17766         [ -z $num ] || error "$num READ RPC occured"
17767         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
17768         echo "... DONE"
17769
17770         # compare content
17771         cmp $tmp $dom || error "file miscompare"
17772
17773         return 0
17774 }
17775 run_test 271d "DoM: read on open (1K file in reply buffer)"
17776
17777 test_271f() {
17778         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
17779                 skip "Need MDS version at least 2.10.57"
17780
17781         local dom=$DIR/$tdir/dom
17782         local tmp=$TMP/$tfile
17783         trap "cleanup_271def_tests $tmp" EXIT
17784
17785         mkdir -p $DIR/$tdir
17786
17787         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17788
17789         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
17790
17791         cancel_lru_locks mdc
17792         dd if=/dev/urandom of=$tmp bs=200000 count=1
17793         dd if=$tmp of=$dom bs=200000 count=1
17794         cancel_lru_locks mdc
17795         cat /etc/hosts >> $tmp
17796         lctl set_param -n mdc.*.stats=clear
17797
17798         echo "Append to the same page"
17799         cat /etc/hosts >> $dom
17800         local num=$(get_mdc_stats $mdtidx ost_read)
17801         local ra=$(get_mdc_stats $mdtidx req_active)
17802         local rw=$(get_mdc_stats $mdtidx req_waittime)
17803
17804         [ -z $num ] || error "$num READ RPC occured"
17805         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
17806         echo "... DONE"
17807
17808         # compare content
17809         cmp $tmp $dom || error "file miscompare"
17810
17811         cancel_lru_locks mdc
17812         lctl set_param -n mdc.*.stats=clear
17813
17814         echo "Open and read file"
17815         cat $dom > /dev/null
17816         local num=$(get_mdc_stats $mdtidx ost_read)
17817         local ra=$(get_mdc_stats $mdtidx req_active)
17818         local rw=$(get_mdc_stats $mdtidx req_waittime)
17819
17820         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
17821         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
17822         echo "... DONE"
17823
17824         # compare content
17825         cmp $tmp $dom || error "file miscompare"
17826
17827         return 0
17828 }
17829 run_test 271f "DoM: read on open (200K file and read tail)"
17830
17831 test_272a() {
17832         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
17833                 skip "Need MDS version at least 2.11.50"
17834
17835         local dom=$DIR/$tdir/dom
17836         mkdir -p $DIR/$tdir
17837
17838         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
17839         dd if=/dev/urandom of=$dom bs=512K count=1 ||
17840                 error "failed to write data into $dom"
17841         local old_md5=$(md5sum $dom)
17842
17843         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
17844                 error "failed to migrate to the same DoM component"
17845
17846         local new_md5=$(md5sum $dom)
17847
17848         [ "$old_md5" == "$new_md5" ] ||
17849                 error "md5sum differ: $old_md5, $new_md5"
17850
17851         [ $($LFS getstripe -c $dom) -eq 2 ] ||
17852                 error "migrate stripe count bad: $(LFS getstripe -c $dom) != 2"
17853 }
17854 run_test 272a "DoM migration: new layout with the same DOM component"
17855
17856 test_272b() {
17857         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
17858                 skip "Need MDS version at least 2.11.50"
17859
17860         local dom=$DIR/$tdir/dom
17861         mkdir -p $DIR/$tdir
17862         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
17863
17864         local mdtidx=$($LFS getstripe -m $dom)
17865         local mdtname=MDT$(printf %04x $mdtidx)
17866         local facet=mds$((mdtidx + 1))
17867
17868         local mdtfree1=$(do_facet $facet \
17869                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17870         dd if=/dev/urandom of=$dom bs=2M count=1 ||
17871                 error "failed to write data into $dom"
17872         local old_md5=$(md5sum $dom)
17873         cancel_lru_locks mdc
17874         local mdtfree1=$(do_facet $facet \
17875                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17876
17877         $LFS migrate -c2 $dom ||
17878                 error "failed to migrate to the new composite layout"
17879         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
17880                 error "MDT stripe was not removed"
17881
17882         cancel_lru_locks mdc
17883         local new_md5=$(md5sum $dom)
17884         [ "$old_md5" != "$new_md5" ] &&
17885                 error "$old_md5 != $new_md5"
17886
17887         # Skip free space checks with ZFS
17888         if [ "$(facet_fstype $facet)" != "zfs" ]; then
17889                 local mdtfree2=$(do_facet $facet \
17890                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17891                 [ $mdtfree2 -gt $mdtfree1 ] ||
17892                         error "MDT space is not freed after migration"
17893         fi
17894         return 0
17895 }
17896 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
17897
17898 test_272c() {
17899         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
17900                 skip "Need MDS version at least 2.11.50"
17901
17902         local dom=$DIR/$tdir/$tfile
17903         mkdir -p $DIR/$tdir
17904         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
17905
17906         local mdtidx=$($LFS getstripe -m $dom)
17907         local mdtname=MDT$(printf %04x $mdtidx)
17908         local facet=mds$((mdtidx + 1))
17909
17910         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
17911                 error "failed to write data into $dom"
17912         local old_md5=$(md5sum $dom)
17913         cancel_lru_locks mdc
17914         local mdtfree1=$(do_facet $facet \
17915                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17916
17917         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
17918                 error "failed to migrate to the new composite layout"
17919         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
17920                 error "MDT stripe was not removed"
17921
17922         cancel_lru_locks mdc
17923         local new_md5=$(md5sum $dom)
17924         [ "$old_md5" != "$new_md5" ] &&
17925                 error "$old_md5 != $new_md5"
17926
17927         # Skip free space checks with ZFS
17928         if [ "$(facet_fstype $facet)" != "zfs" ]; then
17929                 local mdtfree2=$(do_facet $facet \
17930                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17931                 [ $mdtfree2 -gt $mdtfree1 ] ||
17932                         error "MDS space is not freed after migration"
17933         fi
17934         return 0
17935 }
17936 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
17937
17938 test_273a() {
17939         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
17940                 skip "Need MDS version at least 2.11.50"
17941
17942         # Layout swap cannot be done if either file has DOM component,
17943         # this will never be supported, migration should be used instead
17944
17945         local dom=$DIR/$tdir/$tfile
17946         mkdir -p $DIR/$tdir
17947
17948         $LFS setstripe -c2 ${dom}_plain
17949         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
17950         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
17951                 error "can swap layout with DoM component"
17952         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
17953                 error "can swap layout with DoM component"
17954
17955         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
17956         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
17957                 error "can swap layout with DoM component"
17958         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
17959                 error "can swap layout with DoM component"
17960         return 0
17961 }
17962 run_test 273a "DoM: layout swapping should fail with DOM"
17963
17964 test_275() {
17965         remote_ost_nodsh && skip "remote OST with nodsh"
17966         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
17967                 skip "Need OST version >= 2.10.57"
17968
17969         local file=$DIR/$tfile
17970         local oss
17971
17972         oss=$(comma_list $(osts_nodes))
17973
17974         dd if=/dev/urandom of=$file bs=1M count=2 ||
17975                 error "failed to create a file"
17976         cancel_lru_locks osc
17977
17978         #lock 1
17979         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
17980                 error "failed to read a file"
17981
17982 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
17983         $LCTL set_param fail_loc=0x8000031f
17984
17985         cancel_lru_locks osc &
17986         sleep 1
17987
17988 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
17989         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
17990         #IO takes another lock, but matches the PENDING one
17991         #and places it to the IO RPC
17992         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
17993                 error "failed to read a file with PENDING lock"
17994 }
17995 run_test 275 "Read on a canceled duplicate lock"
17996
17997 test_276() {
17998         remote_ost_nodsh && skip "remote OST with nodsh"
17999         local pid
18000
18001         do_facet ost1 "(while true; do \
18002                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
18003                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
18004         pid=$!
18005
18006         for LOOP in $(seq 20); do
18007                 stop ost1
18008                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
18009         done
18010         kill -9 $pid
18011         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
18012                 rm $TMP/sanity_276_pid"
18013 }
18014 run_test 276 "Race between mount and obd_statfs"
18015
18016 cleanup_test_300() {
18017         trap 0
18018         umask $SAVE_UMASK
18019 }
18020 test_striped_dir() {
18021         local mdt_index=$1
18022         local stripe_count
18023         local stripe_index
18024
18025         mkdir -p $DIR/$tdir
18026
18027         SAVE_UMASK=$(umask)
18028         trap cleanup_test_300 RETURN EXIT
18029
18030         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
18031                                                 $DIR/$tdir/striped_dir ||
18032                 error "set striped dir error"
18033
18034         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
18035         [ "$mode" = "755" ] || error "expect 755 got $mode"
18036
18037         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
18038                 error "getdirstripe failed"
18039         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
18040         if [ "$stripe_count" != "2" ]; then
18041                 error "1:stripe_count is $stripe_count, expect 2"
18042         fi
18043         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
18044         if [ "$stripe_count" != "2" ]; then
18045                 error "2:stripe_count is $stripe_count, expect 2"
18046         fi
18047
18048         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
18049         if [ "$stripe_index" != "$mdt_index" ]; then
18050                 error "stripe_index is $stripe_index, expect $mdt_index"
18051         fi
18052
18053         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
18054                 error "nlink error after create striped dir"
18055
18056         mkdir $DIR/$tdir/striped_dir/a
18057         mkdir $DIR/$tdir/striped_dir/b
18058
18059         stat $DIR/$tdir/striped_dir/a ||
18060                 error "create dir under striped dir failed"
18061         stat $DIR/$tdir/striped_dir/b ||
18062                 error "create dir under striped dir failed"
18063
18064         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
18065                 error "nlink error after mkdir"
18066
18067         rmdir $DIR/$tdir/striped_dir/a
18068         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
18069                 error "nlink error after rmdir"
18070
18071         rmdir $DIR/$tdir/striped_dir/b
18072         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
18073                 error "nlink error after rmdir"
18074
18075         chattr +i $DIR/$tdir/striped_dir
18076         createmany -o $DIR/$tdir/striped_dir/f 10 &&
18077                 error "immutable flags not working under striped dir!"
18078         chattr -i $DIR/$tdir/striped_dir
18079
18080         rmdir $DIR/$tdir/striped_dir ||
18081                 error "rmdir striped dir error"
18082
18083         cleanup_test_300
18084
18085         true
18086 }
18087
18088 test_300a() {
18089         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
18090                 skip "skipped for lustre < 2.7.0"
18091         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18092         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18093
18094         test_striped_dir 0 || error "failed on striped dir on MDT0"
18095         test_striped_dir 1 || error "failed on striped dir on MDT0"
18096 }
18097 run_test 300a "basic striped dir sanity test"
18098
18099 test_300b() {
18100         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
18101                 skip "skipped for lustre < 2.7.0"
18102         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18103         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18104
18105         local i
18106         local mtime1
18107         local mtime2
18108         local mtime3
18109
18110         test_mkdir $DIR/$tdir || error "mkdir fail"
18111         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
18112                 error "set striped dir error"
18113         for i in {0..9}; do
18114                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
18115                 sleep 1
18116                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
18117                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
18118                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
18119                 sleep 1
18120                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
18121                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
18122                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
18123         done
18124         true
18125 }
18126 run_test 300b "check ctime/mtime for striped dir"
18127
18128 test_300c() {
18129         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
18130                 skip "skipped for lustre < 2.7.0"
18131         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18132         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18133
18134         local file_count
18135
18136         mkdir -p $DIR/$tdir
18137         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
18138                 error "set striped dir error"
18139
18140         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
18141                 error "chown striped dir failed"
18142
18143         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
18144                 error "create 5k files failed"
18145
18146         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
18147
18148         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
18149
18150         rm -rf $DIR/$tdir
18151 }
18152 run_test 300c "chown && check ls under striped directory"
18153
18154 test_300d() {
18155         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
18156                 skip "skipped for lustre < 2.7.0"
18157         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18158         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18159
18160         local stripe_count
18161         local file
18162
18163         mkdir -p $DIR/$tdir
18164         $LFS setstripe -c 2 $DIR/$tdir
18165
18166         #local striped directory
18167         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
18168                 error "set striped dir error"
18169         createmany -o $DIR/$tdir/striped_dir/f 10 ||
18170                 error "create 10 files failed"
18171
18172         #remote striped directory
18173         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
18174                 error "set striped dir error"
18175         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
18176                 error "create 10 files failed"
18177
18178         for file in $(find $DIR/$tdir); do
18179                 stripe_count=$($LFS getstripe -c $file)
18180                 [ $stripe_count -eq 2 ] ||
18181                         error "wrong stripe $stripe_count for $file"
18182         done
18183
18184         rm -rf $DIR/$tdir
18185 }
18186 run_test 300d "check default stripe under striped directory"
18187
18188 test_300e() {
18189         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18190                 skip "Need MDS version at least 2.7.55"
18191         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18192         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18193
18194         local stripe_count
18195         local file
18196
18197         mkdir -p $DIR/$tdir
18198
18199         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
18200                 error "set striped dir error"
18201
18202         touch $DIR/$tdir/striped_dir/a
18203         touch $DIR/$tdir/striped_dir/b
18204         touch $DIR/$tdir/striped_dir/c
18205
18206         mkdir $DIR/$tdir/striped_dir/dir_a
18207         mkdir $DIR/$tdir/striped_dir/dir_b
18208         mkdir $DIR/$tdir/striped_dir/dir_c
18209
18210         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
18211                 error "set striped adir under striped dir error"
18212
18213         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
18214                 error "set striped bdir under striped dir error"
18215
18216         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
18217                 error "set striped cdir under striped dir error"
18218
18219         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
18220                 error "rename dir under striped dir fails"
18221
18222         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
18223                 error "rename dir under different stripes fails"
18224
18225         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
18226                 error "rename file under striped dir should succeed"
18227
18228         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
18229                 error "rename dir under striped dir should succeed"
18230
18231         rm -rf $DIR/$tdir
18232 }
18233 run_test 300e "check rename under striped directory"
18234
18235 test_300f() {
18236         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18237         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18238         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18239                 skip "Need MDS version at least 2.7.55"
18240
18241         local stripe_count
18242         local file
18243
18244         rm -rf $DIR/$tdir
18245         mkdir -p $DIR/$tdir
18246
18247         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
18248                 error "set striped dir error"
18249
18250         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
18251                 error "set striped dir error"
18252
18253         touch $DIR/$tdir/striped_dir/a
18254         mkdir $DIR/$tdir/striped_dir/dir_a
18255         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
18256                 error "create striped dir under striped dir fails"
18257
18258         touch $DIR/$tdir/striped_dir1/b
18259         mkdir $DIR/$tdir/striped_dir1/dir_b
18260         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
18261                 error "create striped dir under striped dir fails"
18262
18263         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
18264                 error "rename dir under different striped dir should fail"
18265
18266         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
18267                 error "rename striped dir under diff striped dir should fail"
18268
18269         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
18270                 error "rename file under diff striped dirs fails"
18271
18272         rm -rf $DIR/$tdir
18273 }
18274 run_test 300f "check rename cross striped directory"
18275
18276 test_300_check_default_striped_dir()
18277 {
18278         local dirname=$1
18279         local default_count=$2
18280         local default_index=$3
18281         local stripe_count
18282         local stripe_index
18283         local dir_stripe_index
18284         local dir
18285
18286         echo "checking $dirname $default_count $default_index"
18287         $LFS setdirstripe -D -c $default_count -i $default_index \
18288                                 -t all_char $DIR/$tdir/$dirname ||
18289                 error "set default stripe on striped dir error"
18290         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
18291         [ $stripe_count -eq $default_count ] ||
18292                 error "expect $default_count get $stripe_count for $dirname"
18293
18294         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
18295         [ $stripe_index -eq $default_index ] ||
18296                 error "expect $default_index get $stripe_index for $dirname"
18297
18298         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
18299                                                 error "create dirs failed"
18300
18301         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
18302         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
18303         for dir in $(find $DIR/$tdir/$dirname/*); do
18304                 stripe_count=$($LFS getdirstripe -c $dir)
18305                 [ $stripe_count -eq $default_count ] ||
18306                 [ $stripe_count -eq 0 ] || [ $default_count -eq 1 ] ||
18307                 error "stripe count $default_count != $stripe_count for $dir"
18308
18309                 stripe_index=$($LFS getdirstripe -i $dir)
18310                 [ $default_index -eq -1 ] ||
18311                         [ $stripe_index -eq $default_index ] ||
18312                         error "$stripe_index != $default_index for $dir"
18313
18314                 #check default stripe
18315                 stripe_count=$($LFS getdirstripe -D -c $dir)
18316                 [ $stripe_count -eq $default_count ] ||
18317                 error "default count $default_count != $stripe_count for $dir"
18318
18319                 stripe_index=$($LFS getdirstripe -D -i $dir)
18320                 [ $stripe_index -eq $default_index ] ||
18321                 error "default index $default_index != $stripe_index for $dir"
18322         done
18323         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
18324 }
18325
18326 test_300g() {
18327         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18328         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18329                 skip "Need MDS version at least 2.7.55"
18330
18331         local dir
18332         local stripe_count
18333         local stripe_index
18334
18335         mkdir $DIR/$tdir
18336         mkdir $DIR/$tdir/normal_dir
18337
18338         #Checking when client cache stripe index
18339         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
18340         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
18341                 error "create striped_dir failed"
18342
18343         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
18344                 error "create dir0 fails"
18345         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
18346         [ $stripe_index -eq 0 ] ||
18347                 error "dir0 expect index 0 got $stripe_index"
18348
18349         mkdir $DIR/$tdir/striped_dir/dir1 ||
18350                 error "create dir1 fails"
18351         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
18352         [ $stripe_index -eq 1 ] ||
18353                 error "dir1 expect index 1 got $stripe_index"
18354
18355         #check default stripe count/stripe index
18356         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
18357         test_300_check_default_striped_dir normal_dir 1 0
18358         test_300_check_default_striped_dir normal_dir 2 1
18359         test_300_check_default_striped_dir normal_dir 2 -1
18360
18361         #delete default stripe information
18362         echo "delete default stripeEA"
18363         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
18364                 error "set default stripe on striped dir error"
18365
18366         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
18367         for dir in $(find $DIR/$tdir/normal_dir/*); do
18368                 stripe_count=$($LFS getdirstripe -c $dir)
18369                 [ $stripe_count -eq 0 ] ||
18370                         error "expect 1 get $stripe_count for $dir"
18371                 stripe_index=$($LFS getdirstripe -i $dir)
18372                 [ $stripe_index -eq 0 ] ||
18373                         error "expect 0 get $stripe_index for $dir"
18374         done
18375 }
18376 run_test 300g "check default striped directory for normal directory"
18377
18378 test_300h() {
18379         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18380         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18381                 skip "Need MDS version at least 2.7.55"
18382
18383         local dir
18384         local stripe_count
18385
18386         mkdir $DIR/$tdir
18387         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
18388                 error "set striped dir error"
18389
18390         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
18391         test_300_check_default_striped_dir striped_dir 1 0
18392         test_300_check_default_striped_dir striped_dir 2 1
18393         test_300_check_default_striped_dir striped_dir 2 -1
18394
18395         #delete default stripe information
18396         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
18397                 error "set default stripe on striped dir error"
18398
18399         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
18400         for dir in $(find $DIR/$tdir/striped_dir/*); do
18401                 stripe_count=$($LFS getdirstripe -c $dir)
18402                 [ $stripe_count -eq 0 ] ||
18403                         error "expect 1 get $stripe_count for $dir"
18404         done
18405 }
18406 run_test 300h "check default striped directory for striped directory"
18407
18408 test_300i() {
18409         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18410         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18411         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18412                 skip "Need MDS version at least 2.7.55"
18413
18414         local stripe_count
18415         local file
18416
18417         mkdir $DIR/$tdir
18418
18419         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
18420                 error "set striped dir error"
18421
18422         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
18423                 error "create files under striped dir failed"
18424
18425         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
18426                 error "set striped hashdir error"
18427
18428         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
18429                 error "create dir0 under hash dir failed"
18430         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
18431                 error "create dir1 under hash dir failed"
18432
18433         # unfortunately, we need to umount to clear dir layout cache for now
18434         # once we fully implement dir layout, we can drop this
18435         umount_client $MOUNT || error "umount failed"
18436         mount_client $MOUNT || error "mount failed"
18437
18438         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
18439         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
18440         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
18441
18442         #set the stripe to be unknown hash type
18443         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
18444         $LCTL set_param fail_loc=0x1901
18445         for ((i = 0; i < 10; i++)); do
18446                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
18447                         error "stat f-$i failed"
18448                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
18449         done
18450
18451         touch $DIR/$tdir/striped_dir/f0 &&
18452                 error "create under striped dir with unknown hash should fail"
18453
18454         $LCTL set_param fail_loc=0
18455
18456         umount_client $MOUNT || error "umount failed"
18457         mount_client $MOUNT || error "mount failed"
18458
18459         return 0
18460 }
18461 run_test 300i "client handle unknown hash type striped directory"
18462
18463 test_300j() {
18464         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18465         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18466         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18467                 skip "Need MDS version at least 2.7.55"
18468
18469         local stripe_count
18470         local file
18471
18472         mkdir $DIR/$tdir
18473
18474         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
18475         $LCTL set_param fail_loc=0x1702
18476         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
18477                 error "set striped dir error"
18478
18479         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
18480                 error "create files under striped dir failed"
18481
18482         $LCTL set_param fail_loc=0
18483
18484         rm -rf $DIR/$tdir || error "unlink striped dir fails"
18485
18486         return 0
18487 }
18488 run_test 300j "test large update record"
18489
18490 test_300k() {
18491         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18492         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18493         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18494                 skip "Need MDS version at least 2.7.55"
18495
18496         # this test needs a huge transaction
18497         local kb
18498         kb=$(do_facet $SINGLEMDS lctl get_param -n osd*.lustre-MDT0000.kbytestotal)
18499         [ $kb -lt $((1024*1024)) ] && skip "too small mds: $kb"
18500
18501         local stripe_count
18502         local file
18503
18504         mkdir $DIR/$tdir
18505
18506         #define OBD_FAIL_LARGE_STRIPE   0x1703
18507         $LCTL set_param fail_loc=0x1703
18508         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
18509                 error "set striped dir error"
18510         $LCTL set_param fail_loc=0
18511
18512         $LFS getdirstripe $DIR/$tdir/striped_dir ||
18513                 error "getstripeddir fails"
18514         rm -rf $DIR/$tdir/striped_dir ||
18515                 error "unlink striped dir fails"
18516
18517         return 0
18518 }
18519 run_test 300k "test large striped directory"
18520
18521 test_300l() {
18522         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18523         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18524         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18525                 skip "Need MDS version at least 2.7.55"
18526
18527         local stripe_index
18528
18529         test_mkdir -p $DIR/$tdir/striped_dir
18530         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
18531                         error "chown $RUNAS_ID failed"
18532         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
18533                 error "set default striped dir failed"
18534
18535         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
18536         $LCTL set_param fail_loc=0x80000158
18537         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
18538
18539         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
18540         [ $stripe_index -eq 1 ] ||
18541                 error "expect 1 get $stripe_index for $dir"
18542 }
18543 run_test 300l "non-root user to create dir under striped dir with stale layout"
18544
18545 test_300m() {
18546         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18547         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
18548         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18549                 skip "Need MDS version at least 2.7.55"
18550
18551         mkdir -p $DIR/$tdir/striped_dir
18552         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
18553                 error "set default stripes dir error"
18554
18555         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
18556
18557         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
18558         [ $stripe_count -eq 0 ] ||
18559                         error "expect 0 get $stripe_count for a"
18560
18561         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
18562                 error "set default stripes dir error"
18563
18564         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
18565
18566         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
18567         [ $stripe_count -eq 0 ] ||
18568                         error "expect 0 get $stripe_count for b"
18569
18570         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
18571                 error "set default stripes dir error"
18572
18573         mkdir $DIR/$tdir/striped_dir/c &&
18574                 error "default stripe_index is invalid, mkdir c should fails"
18575
18576         rm -rf $DIR/$tdir || error "rmdir fails"
18577 }
18578 run_test 300m "setstriped directory on single MDT FS"
18579
18580 cleanup_300n() {
18581         local list=$(comma_list $(mdts_nodes))
18582
18583         trap 0
18584         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
18585 }
18586
18587 test_300n() {
18588         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18589         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18590         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18591                 skip "Need MDS version at least 2.7.55"
18592         remote_mds_nodsh && skip "remote MDS with nodsh"
18593
18594         local stripe_index
18595         local list=$(comma_list $(mdts_nodes))
18596
18597         trap cleanup_300n RETURN EXIT
18598         mkdir -p $DIR/$tdir
18599         chmod 777 $DIR/$tdir
18600         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
18601                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
18602                 error "create striped dir succeeds with gid=0"
18603
18604         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
18605         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
18606                 error "create striped dir fails with gid=-1"
18607
18608         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
18609         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
18610                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
18611                 error "set default striped dir succeeds with gid=0"
18612
18613
18614         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
18615         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
18616                 error "set default striped dir fails with gid=-1"
18617
18618
18619         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
18620         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
18621                                         error "create test_dir fails"
18622         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
18623                                         error "create test_dir1 fails"
18624         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
18625                                         error "create test_dir2 fails"
18626         cleanup_300n
18627 }
18628 run_test 300n "non-root user to create dir under striped dir with default EA"
18629
18630 test_300o() {
18631         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18632         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18633         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18634                 skip "Need MDS version at least 2.7.55"
18635
18636         local numfree1
18637         local numfree2
18638
18639         mkdir -p $DIR/$tdir
18640
18641         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
18642         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
18643         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
18644                 skip "not enough free inodes $numfree1 $numfree2"
18645         fi
18646
18647         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
18648         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
18649         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
18650                 skip "not enough free space $numfree1 $numfree2"
18651         fi
18652
18653         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
18654                 error "setdirstripe fails"
18655
18656         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
18657                 error "create dirs fails"
18658
18659         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
18660         ls $DIR/$tdir/striped_dir > /dev/null ||
18661                 error "ls striped dir fails"
18662         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
18663                 error "unlink big striped dir fails"
18664 }
18665 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
18666
18667 test_300p() {
18668         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18669         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18670         remote_mds_nodsh && skip "remote MDS with nodsh"
18671
18672         mkdir -p $DIR/$tdir
18673
18674         #define OBD_FAIL_OUT_ENOSPC     0x1704
18675         do_facet mds2 lctl set_param fail_loc=0x80001704
18676         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
18677                  && error "create striped directory should fail"
18678
18679         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
18680
18681         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
18682         true
18683 }
18684 run_test 300p "create striped directory without space"
18685
18686 test_300q() {
18687         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18688         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18689
18690         local fd=$(free_fd)
18691         local cmd="exec $fd<$tdir"
18692         cd $DIR
18693         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
18694         eval $cmd
18695         cmd="exec $fd<&-"
18696         trap "eval $cmd" EXIT
18697         cd $tdir || error "cd $tdir fails"
18698         rmdir  ../$tdir || error "rmdir $tdir fails"
18699         mkdir local_dir && error "create dir succeeds"
18700         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
18701         eval $cmd
18702         return 0
18703 }
18704 run_test 300q "create remote directory under orphan directory"
18705
18706 test_300r() {
18707         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
18708                 skip "Need MDS version at least 2.7.55" && return
18709         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
18710
18711         mkdir $DIR/$tdir
18712
18713         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
18714                 error "set striped dir error"
18715
18716         $LFS getdirstripe $DIR/$tdir/striped_dir ||
18717                 error "getstripeddir fails"
18718
18719         local stripe_count
18720         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
18721                       awk '/lmv_stripe_count:/ { print $2 }')
18722
18723         [ $MDSCOUNT -ne $stripe_count ] &&
18724                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
18725
18726         rm -rf $DIR/$tdir/striped_dir ||
18727                 error "unlink striped dir fails"
18728 }
18729 run_test 300r "test -1 striped directory"
18730
18731 prepare_remote_file() {
18732         mkdir $DIR/$tdir/src_dir ||
18733                 error "create remote source failed"
18734
18735         cp /etc/hosts $DIR/$tdir/src_dir/a ||
18736                  error "cp to remote source failed"
18737         touch $DIR/$tdir/src_dir/a
18738
18739         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
18740                 error "create remote target dir failed"
18741
18742         touch $DIR/$tdir/tgt_dir/b
18743
18744         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
18745                 error "rename dir cross MDT failed!"
18746
18747         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
18748                 error "src_child still exists after rename"
18749
18750         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
18751                 error "missing file(a) after rename"
18752
18753         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
18754                 error "diff after rename"
18755 }
18756
18757 test_310a() {
18758         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
18759         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18760
18761         local remote_file=$DIR/$tdir/tgt_dir/b
18762
18763         mkdir -p $DIR/$tdir
18764
18765         prepare_remote_file || error "prepare remote file failed"
18766
18767         #open-unlink file
18768         $OPENUNLINK $remote_file $remote_file ||
18769                 error "openunlink $remote_file failed"
18770         $CHECKSTAT -a $remote_file || error "$remote_file exists"
18771 }
18772 run_test 310a "open unlink remote file"
18773
18774 test_310b() {
18775         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
18776         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18777
18778         local remote_file=$DIR/$tdir/tgt_dir/b
18779
18780         mkdir -p $DIR/$tdir
18781
18782         prepare_remote_file || error "prepare remote file failed"
18783
18784         ln $remote_file $DIR/$tfile || error "link failed for remote file"
18785         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
18786         $CHECKSTAT -t file $remote_file || error "check file failed"
18787 }
18788 run_test 310b "unlink remote file with multiple links while open"
18789
18790 test_310c() {
18791         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18792         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
18793
18794         local remote_file=$DIR/$tdir/tgt_dir/b
18795
18796         mkdir -p $DIR/$tdir
18797
18798         prepare_remote_file || error "prepare remote file failed"
18799
18800         ln $remote_file $DIR/$tfile || error "link failed for remote file"
18801         multiop_bg_pause $remote_file O_uc ||
18802                         error "mulitop failed for remote file"
18803         MULTIPID=$!
18804         $MULTIOP $DIR/$tfile Ouc
18805         kill -USR1 $MULTIPID
18806         wait $MULTIPID
18807 }
18808 run_test 310c "open-unlink remote file with multiple links"
18809
18810 #LU-4825
18811 test_311() {
18812         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18813         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
18814         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
18815                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
18816         remote_mds_nodsh && skip "remote MDS with nodsh"
18817
18818         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
18819         local mdts=$(comma_list $(mdts_nodes))
18820
18821         mkdir -p $DIR/$tdir
18822         $LFS setstripe -i 0 -c 1 $DIR/$tdir
18823         createmany -o $DIR/$tdir/$tfile. 1000
18824
18825         # statfs data is not real time, let's just calculate it
18826         old_iused=$((old_iused + 1000))
18827
18828         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
18829                         osp.*OST0000*MDT0000.create_count")
18830         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
18831                                 osp.*OST0000*MDT0000.max_create_count")
18832         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
18833
18834         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
18835         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
18836         [ $index -ne 0 ] || error "$tfile stripe index is 0"
18837
18838         unlinkmany $DIR/$tdir/$tfile. 1000
18839
18840         do_nodes $mdts "$LCTL set_param -n \
18841                         osp.*OST0000*.max_create_count=$max_count"
18842         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
18843                 do_nodes $mdts "$LCTL set_param -n \
18844                                 osp.*OST0000*.create_count=$count"
18845         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
18846                         grep "=0" && error "create_count is zero"
18847
18848         local new_iused
18849         for i in $(seq 120); do
18850                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
18851                 # system may be too busy to destroy all objs in time, use
18852                 # a somewhat small value to not fail autotest
18853                 [ $((old_iused - new_iused)) -gt 400 ] && break
18854                 sleep 1
18855         done
18856
18857         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
18858         [ $((old_iused - new_iused)) -gt 400 ] ||
18859                 error "objs not destroyed after unlink"
18860 }
18861 run_test 311 "disable OSP precreate, and unlink should destroy objs"
18862
18863 zfs_oid_to_objid()
18864 {
18865         local ost=$1
18866         local objid=$2
18867
18868         local vdevdir=$(dirname $(facet_vdevice $ost))
18869         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
18870         local zfs_zapid=$(do_facet $ost $cmd |
18871                           grep -w "/O/0/d$((objid%32))" -C 5 |
18872                           awk '/Object/{getline; print $1}')
18873         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
18874                           awk "/$objid = /"'{printf $3}')
18875
18876         echo $zfs_objid
18877 }
18878
18879 zfs_object_blksz() {
18880         local ost=$1
18881         local objid=$2
18882
18883         local vdevdir=$(dirname $(facet_vdevice $ost))
18884         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
18885         local blksz=$(do_facet $ost $cmd $objid |
18886                       awk '/dblk/{getline; printf $4}')
18887
18888         case "${blksz: -1}" in
18889                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
18890                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
18891                 *) ;;
18892         esac
18893
18894         echo $blksz
18895 }
18896
18897 test_312() { # LU-4856
18898         remote_ost_nodsh && skip "remote OST with nodsh"
18899         [ "$ost1_FSTYPE" = "zfs" ] ||
18900                 skip_env "the test only applies to zfs"
18901
18902         local max_blksz=$(do_facet ost1 \
18903                           $ZFS get -p recordsize $(facet_device ost1) |
18904                           awk '!/VALUE/{print $3}')
18905
18906         # to make life a little bit easier
18907         $LFS mkdir -c 1 -i 0 $DIR/$tdir
18908         $LFS setstripe -c 1 -i 0 $DIR/$tdir
18909
18910         local tf=$DIR/$tdir/$tfile
18911         touch $tf
18912         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
18913
18914         # Get ZFS object id
18915         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
18916         # block size change by sequential overwrite
18917         local bs
18918
18919         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
18920                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
18921
18922                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
18923                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
18924         done
18925         rm -f $tf
18926
18927         # block size change by sequential append write
18928         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
18929         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
18930         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
18931         local count
18932
18933         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
18934                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
18935                         oflag=sync conv=notrunc
18936
18937                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
18938                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
18939                         error "blksz error, actual $blksz, " \
18940                                 "expected: 2 * $count * $PAGE_SIZE"
18941         done
18942         rm -f $tf
18943
18944         # random write
18945         touch $tf
18946         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
18947         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
18948
18949         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
18950         blksz=$(zfs_object_blksz ost1 $zfs_objid)
18951         [ $blksz -eq $PAGE_SIZE ] ||
18952                 error "blksz error: $blksz, expected: $PAGE_SIZE"
18953
18954         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
18955         blksz=$(zfs_object_blksz ost1 $zfs_objid)
18956         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
18957
18958         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
18959         blksz=$(zfs_object_blksz ost1 $zfs_objid)
18960         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
18961 }
18962 run_test 312 "make sure ZFS adjusts its block size by write pattern"
18963
18964 test_313() {
18965         remote_ost_nodsh && skip "remote OST with nodsh"
18966
18967         local file=$DIR/$tfile
18968
18969         rm -f $file
18970         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
18971
18972         # define OBD_FAIL_TGT_RCVD_EIO           0x720
18973         do_facet ost1 "$LCTL set_param fail_loc=0x720"
18974         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
18975                 error "write should failed"
18976         do_facet ost1 "$LCTL set_param fail_loc=0"
18977         rm -f $file
18978 }
18979 run_test 313 "io should fail after last_rcvd update fail"
18980
18981 test_314() {
18982         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
18983
18984         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
18985         do_facet ost1 "$LCTL set_param fail_loc=0x720"
18986         rm -f $DIR/$tfile
18987         wait_delete_completed
18988         do_facet ost1 "$LCTL set_param fail_loc=0"
18989 }
18990 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
18991
18992 test_315() { # LU-618
18993         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
18994
18995         local file=$DIR/$tfile
18996         rm -f $file
18997
18998         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
18999                 error "multiop file write failed"
19000         $MULTIOP $file oO_RDONLY:r4063232_c &
19001         PID=$!
19002
19003         sleep 2
19004
19005         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
19006         kill -USR1 $PID
19007
19008         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
19009         rm -f $file
19010 }
19011 run_test 315 "read should be accounted"
19012
19013 test_316() {
19014         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19015         large_xattr_enabled || skip_env "ea_inode feature disabled"
19016
19017         rm -rf $DIR/$tdir/d
19018         mkdir -p $DIR/$tdir/d
19019         chown nobody $DIR/$tdir/d
19020         touch $DIR/$tdir/d/file
19021
19022         $LFS mv -M1 $DIR/$tdir/d || error "lfs mv failed"
19023 }
19024 run_test 316 "lfs mv"
19025
19026 test_317() {
19027         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
19028                 skip "Need MDS version at least 2.11.53"
19029         local trunc_sz
19030         local grant_blk_size
19031
19032         if [ "$(facet_fstype $facet)" == "zfs" ]; then
19033                 skip "LU-10370: no implementation for ZFS" && return
19034         fi
19035
19036         stack_trap "rm -f $DIR/$tfile" EXIT
19037         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
19038                         awk '/grant_block_size:/ { print $2; exit; }')
19039         #
19040         # Create File of size 5M. Truncate it to below size's and verify
19041         # blocks count.
19042         #
19043         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
19044                 error "Create file : $DIR/$tfile"
19045
19046         for trunc_sz in 2097152 4097 4000 509 0; do
19047                 $TRUNCATE $DIR/$tfile $trunc_sz ||
19048                         error "truncate $tfile to $trunc_sz failed"
19049                 local sz=$(stat --format=%s $DIR/$tfile)
19050                 local blk=$(stat --format=%b $DIR/$tfile)
19051                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
19052                                      grant_blk_size) * 8))
19053
19054                 if [[ $blk -ne $trunc_blk ]]; then
19055                         $(which stat) $DIR/$tfile
19056                         error "Expected Block $trunc_blk got $blk for $tfile"
19057                 fi
19058
19059                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
19060                         error "Expected Size $trunc_sz got $sz for $tfile"
19061         done
19062
19063         #
19064         # sparse file test
19065         # Create file with a hole and write actual two blocks. Block count
19066         # must be 16.
19067         #
19068         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
19069                 conv=fsync || error "Create file : $DIR/$tfile"
19070
19071         # Calculate the final truncate size.
19072         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
19073
19074         #
19075         # truncate to size $trunc_sz bytes. Strip the last block
19076         # The block count must drop to 8
19077         #
19078         $TRUNCATE $DIR/$tfile $trunc_sz ||
19079                 error "truncate $tfile to $trunc_sz failed"
19080
19081         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
19082         sz=$(stat --format=%s $DIR/$tfile)
19083         blk=$(stat --format=%b $DIR/$tfile)
19084
19085         if [[ $blk -ne $trunc_bsz ]]; then
19086                 $(which stat) $DIR/$tfile
19087                 error "Expected Block $trunc_bsz got $blk for $tfile"
19088         fi
19089
19090         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
19091                 error "Expected Size $trunc_sz got $sz for $tfile"
19092 }
19093 run_test 317 "Verify blocks get correctly update after truncate"
19094
19095 test_319() {
19096         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
19097
19098         local before=$(date +%s)
19099         local evict
19100         local mdir=$DIR/$tdir
19101         local file=$mdir/xxx
19102
19103         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
19104         touch $file
19105
19106 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
19107         $LCTL set_param fail_val=5 fail_loc=0x8000032c
19108         $LFS mv -m1 $file &
19109
19110         sleep 1
19111         dd if=$file of=/dev/null
19112         wait
19113         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
19114           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
19115
19116         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
19117 }
19118 run_test 319 "lost lease lock on migrate error"
19119
19120 test_fake_rw() {
19121         local read_write=$1
19122         if [ "$read_write" = "write" ]; then
19123                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
19124         elif [ "$read_write" = "read" ]; then
19125                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
19126         else
19127                 error "argument error"
19128         fi
19129
19130         # turn off debug for performance testing
19131         local saved_debug=$($LCTL get_param -n debug)
19132         $LCTL set_param debug=0
19133
19134         $LFS setstripe -c 1 -i 0 $DIR/$tfile
19135
19136         # get ost1 size - lustre-OST0000
19137         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
19138         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
19139         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
19140
19141         if [ "$read_write" = "read" ]; then
19142                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
19143         fi
19144
19145         local start_time=$(date +%s.%N)
19146         $dd_cmd bs=1M count=$blocks oflag=sync ||
19147                 error "real dd $read_write error"
19148         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
19149
19150         if [ "$read_write" = "write" ]; then
19151                 rm -f $DIR/$tfile
19152         fi
19153
19154         # define OBD_FAIL_OST_FAKE_RW           0x238
19155         do_facet ost1 $LCTL set_param fail_loc=0x238
19156
19157         local start_time=$(date +%s.%N)
19158         $dd_cmd bs=1M count=$blocks oflag=sync ||
19159                 error "fake dd $read_write error"
19160         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
19161
19162         if [ "$read_write" = "write" ]; then
19163                 # verify file size
19164                 cancel_lru_locks osc
19165                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
19166                         error "$tfile size not $blocks MB"
19167         fi
19168         do_facet ost1 $LCTL set_param fail_loc=0
19169
19170         echo "fake $read_write $duration_fake vs. normal $read_write" \
19171                 "$duration in seconds"
19172         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
19173                 error_not_in_vm "fake write is slower"
19174
19175         $LCTL set_param -n debug="$saved_debug"
19176         rm -f $DIR/$tfile
19177 }
19178 test_399a() { # LU-7655 for OST fake write
19179         remote_ost_nodsh && skip "remote OST with nodsh"
19180
19181         test_fake_rw write
19182 }
19183 run_test 399a "fake write should not be slower than normal write"
19184
19185 test_399b() { # LU-8726 for OST fake read
19186         remote_ost_nodsh && skip "remote OST with nodsh"
19187         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
19188                 skip_env "ldiskfs only test"
19189         fi
19190
19191         test_fake_rw read
19192 }
19193 run_test 399b "fake read should not be slower than normal read"
19194
19195 test_400a() { # LU-1606, was conf-sanity test_74
19196         if ! which $CC > /dev/null 2>&1; then
19197                 skip_env "$CC is not installed"
19198         fi
19199
19200         local extra_flags=''
19201         local out=$TMP/$tfile
19202         local prefix=/usr/include/lustre
19203         local prog
19204
19205         if ! [[ -d $prefix ]]; then
19206                 # Assume we're running in tree and fixup the include path.
19207                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
19208                 extra_flags+=" -L$LUSTRE/utils/.lib"
19209         fi
19210
19211         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
19212                 $CC -Wall -Werror $extra_flags -o $out $prog -llustreapi ||
19213                         error "client api broken"
19214         done
19215         rm -f $out
19216 }
19217 run_test 400a "Lustre client api program can compile and link"
19218
19219 test_400b() { # LU-1606, LU-5011
19220         local header
19221         local out=$TMP/$tfile
19222         local prefix=/usr/include/linux/lustre
19223
19224         # We use a hard coded prefix so that this test will not fail
19225         # when run in tree. There are headers in lustre/include/lustre/
19226         # that are not packaged (like lustre_idl.h) and have more
19227         # complicated include dependencies (like config.h and lnet/types.h).
19228         # Since this test about correct packaging we just skip them when
19229         # they don't exist (see below) rather than try to fixup cppflags.
19230
19231         if ! which $CC > /dev/null 2>&1; then
19232                 skip_env "$CC is not installed"
19233         fi
19234
19235         for header in $prefix/*.h; do
19236                 if ! [[ -f "$header" ]]; then
19237                         continue
19238                 fi
19239
19240                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
19241                         continue # lustre_ioctl.h is internal header
19242                 fi
19243
19244                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
19245                         error "cannot compile '$header'"
19246         done
19247         rm -f $out
19248 }
19249 run_test 400b "packaged headers can be compiled"
19250
19251 test_401a() { #LU-7437
19252         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
19253         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
19254
19255         #count the number of parameters by "list_param -R"
19256         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
19257         #count the number of parameters by listing proc files
19258         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
19259         echo "proc_dirs='$proc_dirs'"
19260         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
19261         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
19262                       sort -u | wc -l)
19263
19264         [ $params -eq $procs ] ||
19265                 error "found $params parameters vs. $procs proc files"
19266
19267         # test the list_param -D option only returns directories
19268         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
19269         #count the number of parameters by listing proc directories
19270         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
19271                 sort -u | wc -l)
19272
19273         [ $params -eq $procs ] ||
19274                 error "found $params parameters vs. $procs proc files"
19275 }
19276 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
19277
19278 test_401b() {
19279         local save=$($LCTL get_param -n jobid_var)
19280         local tmp=testing
19281
19282         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
19283                 error "no error returned when setting bad parameters"
19284
19285         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
19286         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
19287
19288         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
19289         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
19290         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
19291 }
19292 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
19293
19294 test_401c() {
19295         local jobid_var_old=$($LCTL get_param -n jobid_var)
19296         local jobid_var_new
19297
19298         $LCTL set_param jobid_var= &&
19299                 error "no error returned for 'set_param a='"
19300
19301         jobid_var_new=$($LCTL get_param -n jobid_var)
19302         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
19303                 error "jobid_var was changed by setting without value"
19304
19305         $LCTL set_param jobid_var &&
19306                 error "no error returned for 'set_param a'"
19307
19308         jobid_var_new=$($LCTL get_param -n jobid_var)
19309         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
19310                 error "jobid_var was changed by setting without value"
19311 }
19312 run_test 401c "Verify 'lctl set_param' without value fails in either format."
19313
19314 test_401d() {
19315         local jobid_var_old=$($LCTL get_param -n jobid_var)
19316         local jobid_var_new
19317         local new_value="foo=bar"
19318
19319         $LCTL set_param jobid_var=$new_value ||
19320                 error "'set_param a=b' did not accept a value containing '='"
19321
19322         jobid_var_new=$($LCTL get_param -n jobid_var)
19323         [[ "$jobid_var_new" == "$new_value" ]] ||
19324                 error "'set_param a=b' failed on a value containing '='"
19325
19326         # Reset the jobid_var to test the other format
19327         $LCTL set_param jobid_var=$jobid_var_old
19328         jobid_var_new=$($LCTL get_param -n jobid_var)
19329         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
19330                 error "failed to reset jobid_var"
19331
19332         $LCTL set_param jobid_var $new_value ||
19333                 error "'set_param a b' did not accept a value containing '='"
19334
19335         jobid_var_new=$($LCTL get_param -n jobid_var)
19336         [[ "$jobid_var_new" == "$new_value" ]] ||
19337                 error "'set_param a b' failed on a value containing '='"
19338
19339         $LCTL set_param jobid_var $jobid_var_old
19340         jobid_var_new=$($LCTL get_param -n jobid_var)
19341         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
19342                 error "failed to reset jobid_var"
19343 }
19344 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
19345
19346 test_402() {
19347         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
19348         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
19349                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
19350         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
19351                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
19352                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
19353         remote_mds_nodsh && skip "remote MDS with nodsh"
19354
19355         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
19356 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
19357         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
19358         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
19359                 echo "Touch failed - OK"
19360 }
19361 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
19362
19363 test_403() {
19364         local file1=$DIR/$tfile.1
19365         local file2=$DIR/$tfile.2
19366         local tfile=$TMP/$tfile
19367
19368         rm -f $file1 $file2 $tfile
19369
19370         touch $file1
19371         ln $file1 $file2
19372
19373         # 30 sec OBD_TIMEOUT in ll_getattr()
19374         # right before populating st_nlink
19375         $LCTL set_param fail_loc=0x80001409
19376         stat -c %h $file1 > $tfile &
19377
19378         # create an alias, drop all locks and reclaim the dentry
19379         < $file2
19380         cancel_lru_locks mdc
19381         cancel_lru_locks osc
19382         sysctl -w vm.drop_caches=2
19383
19384         wait
19385
19386         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
19387
19388         rm -f $tfile $file1 $file2
19389 }
19390 run_test 403 "i_nlink should not drop to zero due to aliasing"
19391
19392 test_404() { # LU-6601
19393         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
19394                 skip "Need server version newer than 2.8.52"
19395         remote_mds_nodsh && skip "remote MDS with nodsh"
19396
19397         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
19398                 awk '/osp .*-osc-MDT/ { print $4}')
19399
19400         local osp
19401         for osp in $mosps; do
19402                 echo "Deactivate: " $osp
19403                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
19404                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
19405                         awk -vp=$osp '$4 == p { print $2 }')
19406                 [ $stat = IN ] || {
19407                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
19408                         error "deactivate error"
19409                 }
19410                 echo "Activate: " $osp
19411                 do_facet $SINGLEMDS $LCTL --device %$osp activate
19412                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
19413                         awk -vp=$osp '$4 == p { print $2 }')
19414                 [ $stat = UP ] || {
19415                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
19416                         error "activate error"
19417                 }
19418         done
19419 }
19420 run_test 404 "validate manual {de}activated works properly for OSPs"
19421
19422 test_405() {
19423         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
19424         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
19425                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
19426                         skip "Layout swap lock is not supported"
19427
19428         check_swap_layouts_support
19429
19430         test_mkdir $DIR/$tdir
19431         swap_lock_test -d $DIR/$tdir ||
19432                 error "One layout swap locked test failed"
19433 }
19434 run_test 405 "Various layout swap lock tests"
19435
19436 test_406() {
19437         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19438         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
19439         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
19440         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19441         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
19442                 skip "Need MDS version at least 2.8.50"
19443
19444         local def_stripe_size=$($LFS getstripe -S $MOUNT)
19445         local test_pool=$TESTNAME
19446
19447         if ! combined_mgs_mds ; then
19448                 mount_mgs_client
19449         fi
19450         pool_add $test_pool || error "pool_add failed"
19451         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
19452                 error "pool_add_targets failed"
19453
19454         save_layout_restore_at_exit $MOUNT
19455
19456         # parent set default stripe count only, child will stripe from both
19457         # parent and fs default
19458         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
19459                 error "setstripe $MOUNT failed"
19460         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
19461         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
19462         for i in $(seq 10); do
19463                 local f=$DIR/$tdir/$tfile.$i
19464                 touch $f || error "touch failed"
19465                 local count=$($LFS getstripe -c $f)
19466                 [ $count -eq $OSTCOUNT ] ||
19467                         error "$f stripe count $count != $OSTCOUNT"
19468                 local offset=$($LFS getstripe -i $f)
19469                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
19470                 local size=$($LFS getstripe -S $f)
19471                 [ $size -eq $((def_stripe_size * 2)) ] ||
19472                         error "$f stripe size $size != $((def_stripe_size * 2))"
19473                 local pool=$($LFS getstripe -p $f)
19474                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
19475         done
19476
19477         # change fs default striping, delete parent default striping, now child
19478         # will stripe from new fs default striping only
19479         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
19480                 error "change $MOUNT default stripe failed"
19481         $LFS setstripe -c 0 $DIR/$tdir ||
19482                 error "delete $tdir default stripe failed"
19483         for i in $(seq 11 20); do
19484                 local f=$DIR/$tdir/$tfile.$i
19485                 touch $f || error "touch $f failed"
19486                 local count=$($LFS getstripe -c $f)
19487                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
19488                 local offset=$($LFS getstripe -i $f)
19489                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
19490                 local size=$($LFS getstripe -S $f)
19491                 [ $size -eq $def_stripe_size ] ||
19492                         error "$f stripe size $size != $def_stripe_size"
19493                 local pool=$($LFS getstripe -p $f)
19494                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
19495         done
19496
19497         unlinkmany $DIR/$tdir/$tfile. 1 20
19498
19499         local f=$DIR/$tdir/$tfile
19500         pool_remove_all_targets $test_pool $f
19501         pool_remove $test_pool $f
19502
19503         if ! combined_mgs_mds ; then
19504                 umount_mgs_client
19505         fi
19506 }
19507 run_test 406 "DNE support fs default striping"
19508
19509 test_407() {
19510         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19511         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
19512                 skip "Need MDS version at least 2.8.55"
19513         remote_mds_nodsh && skip "remote MDS with nodsh"
19514
19515         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
19516                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
19517         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
19518                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
19519         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
19520
19521         #define OBD_FAIL_DT_TXN_STOP    0x2019
19522         for idx in $(seq $MDSCOUNT); do
19523                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
19524         done
19525         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
19526         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
19527                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
19528         true
19529 }
19530 run_test 407 "transaction fail should cause operation fail"
19531
19532 test_408() {
19533         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
19534
19535         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
19536         lctl set_param fail_loc=0x8000040a
19537         # let ll_prepare_partial_page() fail
19538         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
19539
19540         rm -f $DIR/$tfile
19541
19542         # create at least 100 unused inodes so that
19543         # shrink_icache_memory(0) should not return 0
19544         touch $DIR/$tfile-{0..100}
19545         rm -f $DIR/$tfile-{0..100}
19546         sync
19547
19548         echo 2 > /proc/sys/vm/drop_caches
19549 }
19550 run_test 408 "drop_caches should not hang due to page leaks"
19551
19552 test_409()
19553 {
19554         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19555
19556         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
19557         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
19558         touch $DIR/$tdir/guard || error "(2) Fail to create"
19559
19560         local PREFIX=$(str_repeat 'A' 128)
19561         echo "Create 1K hard links start at $(date)"
19562         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
19563                 error "(3) Fail to hard link"
19564
19565         echo "Links count should be right although linkEA overflow"
19566         stat $DIR/$tdir/guard || error "(4) Fail to stat"
19567         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
19568         [ $linkcount -eq 1001 ] ||
19569                 error "(5) Unexpected hard links count: $linkcount"
19570
19571         echo "List all links start at $(date)"
19572         ls -l $DIR/$tdir/foo > /dev/null ||
19573                 error "(6) Fail to list $DIR/$tdir/foo"
19574
19575         echo "Unlink hard links start at $(date)"
19576         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
19577                 error "(7) Fail to unlink"
19578         echo "Unlink hard links finished at $(date)"
19579 }
19580 run_test 409 "Large amount of cross-MDTs hard links on the same file"
19581
19582 test_410()
19583 {
19584         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
19585                 skip "Need client version at least 2.9.59"
19586
19587         # Create a file, and stat it from the kernel
19588         local testfile=$DIR/$tfile
19589         touch $testfile
19590
19591         local run_id=$RANDOM
19592         local my_ino=$(stat --format "%i" $testfile)
19593
19594         # Try to insert the module. This will always fail as the
19595         # module is designed to not be inserted.
19596         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
19597             &> /dev/null
19598
19599         # Anything but success is a test failure
19600         dmesg | grep -q \
19601             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
19602             error "no inode match"
19603 }
19604 run_test 410 "Test inode number returned from kernel thread"
19605
19606 cleanup_test411_cgroup() {
19607         trap 0
19608         rmdir "$1"
19609 }
19610
19611 test_411() {
19612         local cg_basedir=/sys/fs/cgroup/memory
19613         # LU-9966
19614         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
19615                 skip "no setup for cgroup"
19616
19617         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
19618                 error "test file creation failed"
19619         cancel_lru_locks osc
19620
19621         # Create a very small memory cgroup to force a slab allocation error
19622         local cgdir=$cg_basedir/osc_slab_alloc
19623         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
19624         trap "cleanup_test411_cgroup $cgdir" EXIT
19625         echo 2M > $cgdir/memory.kmem.limit_in_bytes
19626         echo 1M > $cgdir/memory.limit_in_bytes
19627
19628         # Should not LBUG, just be killed by oom-killer
19629         # dd will return 0 even allocation failure in some environment.
19630         # So don't check return value
19631         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
19632         cleanup_test411_cgroup $cgdir
19633
19634         return 0
19635 }
19636 run_test 411 "Slab allocation error with cgroup does not LBUG"
19637
19638 test_412() {
19639         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19640         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
19641                 skip "Need server version at least 2.10.55"
19642         fi
19643
19644         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
19645                 error "mkdir failed"
19646         $LFS getdirstripe $DIR/$tdir
19647         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
19648         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
19649                 error "expect $((MDSCOUT - 1)) get $stripe_index"
19650         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
19651         [ $stripe_count -eq 2 ] ||
19652                 error "expect 2 get $stripe_count"
19653 }
19654 run_test 412 "mkdir on specific MDTs"
19655
19656 test_413() {
19657         [ $MDSCOUNT -lt 2 ] &&
19658                 skip "We need at least 2 MDTs for this test"
19659
19660         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
19661                 skip "Need server version at least 2.10.55"
19662         fi
19663
19664         mkdir $DIR/$tdir || error "mkdir failed"
19665
19666         # find MDT that is the most full
19667         local max=$($LFS df | grep MDT |
19668                 awk 'BEGIN { a=0 }
19669                         { sub("%", "", $5)
19670                           if (0+$5 >= a)
19671                           {
19672                                 a = $5
19673                                 b = $6
19674                           }
19675                         }
19676                      END { split(b, c, ":")
19677                            sub("]", "", c[2])
19678                            print c[2]
19679                          }')
19680
19681         for i in $(seq $((MDSCOUNT - 1))); do
19682                 $LFS mkdir -c $i $DIR/$tdir/d$i ||
19683                         error "mkdir d$i failed"
19684                 $LFS getdirstripe $DIR/$tdir/d$i
19685                 local stripe_index=$($LFS getdirstripe -i $DIR/$tdir/d$i)
19686                 [ $stripe_index -ne $max ] ||
19687                         error "don't expect $max"
19688         done
19689 }
19690 run_test 413 "mkdir on less full MDTs"
19691
19692 test_414() {
19693 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
19694         $LCTL set_param fail_loc=0x80000521
19695         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
19696         rm -f $DIR/$tfile
19697 }
19698 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
19699
19700 test_415() {
19701         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19702         [ $(lustre_version_code mds1) -lt $(version_code 2.11.52) ] &&
19703                 skip "Need server version at least 2.11.52"
19704
19705         # LU-11102
19706         local total
19707         local setattr_pid
19708         local start_time
19709         local end_time
19710         local duration
19711
19712         total=500
19713         # this test may be slow on ZFS
19714         [ "$mds1_FSTYPE" == "zfs" ] && total=100
19715
19716         # though this test is designed for striped directory, let's test normal
19717         # directory too since lock is always saved as CoS lock.
19718         test_mkdir $DIR/$tdir || error "mkdir $tdir"
19719         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
19720
19721         (
19722                 while true; do
19723                         touch $DIR/$tdir
19724                 done
19725         ) &
19726         setattr_pid=$!
19727
19728         start_time=$(date +%s)
19729         for i in $(seq $total); do
19730                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
19731                         > /dev/null
19732         done
19733         end_time=$(date +%s)
19734         duration=$((end_time - start_time))
19735
19736         kill -9 $setattr_pid
19737
19738         echo "rename $total files took $duration sec"
19739         [ $duration -lt 100 ] || error "rename took $duration sec"
19740 }
19741 run_test 415 "lock revoke is not missing"
19742
19743 test_416() {
19744         [ $(lustre_version_code mds1) -lt $(version_code 2.11.55) ] &&
19745                 skip "Need server version at least 2.11.55"
19746
19747         # define OBD_FAIL_OSD_TXN_START    0x19a
19748         do_facet mds1 lctl set_param fail_loc=0x19a
19749
19750         lfs mkdir -c $MDSCOUNT $DIR/$tdir
19751
19752         true
19753 }
19754 run_test 416 "transaction start failure won't cause system hung"
19755
19756 cleanup_417() {
19757         trap 0
19758         do_nodes $(comma_list $(mdts_nodes)) \
19759                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
19760         do_nodes $(comma_list $(mdts_nodes)) \
19761                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
19762         do_nodes $(comma_list $(mdts_nodes)) \
19763                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
19764 }
19765
19766 test_417() {
19767         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19768         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
19769                 skip "Need MDS version at least 2.11.56"
19770
19771         trap cleanup_417 RETURN EXIT
19772
19773         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
19774         do_nodes $(comma_list $(mdts_nodes)) \
19775                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
19776         $LFS migrate -m 0 $DIR/$tdir.1 &&
19777                 error "migrate dir $tdir.1 should fail"
19778
19779         do_nodes $(comma_list $(mdts_nodes)) \
19780                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
19781         $LFS mkdir -i 1 $DIR/$tdir.2 &&
19782                 error "create remote dir $tdir.2 should fail"
19783
19784         do_nodes $(comma_list $(mdts_nodes)) \
19785                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
19786         $LFS mkdir -c 2 $DIR/$tdir.3 &&
19787                 error "create striped dir $tdir.3 should fail"
19788         true
19789 }
19790 run_test 417 "disable remote dir, striped dir and dir migration"
19791
19792 # Checks that the outputs of df [-i] and lfs df [-i] match
19793 #
19794 # usage: check_lfs_df <blocks | inodes> <mountpoint>
19795 check_lfs_df() {
19796         local dir=$2
19797         local inodes
19798         local df_out
19799         local lfs_df_out
19800         local count
19801         local passed=false
19802
19803         # blocks or inodes
19804         [ "$1" == "blocks" ] && inodes= || inodes="-i"
19805
19806         for count in {1..100}; do
19807                 cancel_lru_locks
19808                 sync; sleep 0.2
19809
19810                 # read the lines of interest
19811                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
19812                         error "df $inodes $dir | tail -n +2 failed"
19813                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
19814                         error "lfs df $inodes $dir | grep summary: failed"
19815
19816                 # skip first substrings of each output as they are different
19817                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
19818                 # compare the two outputs
19819                 passed=true
19820                 for i in {1..5}; do
19821                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
19822                 done
19823                 $passed && break
19824         done
19825
19826         if ! $passed; then
19827                 df -P $inodes $dir
19828                 echo
19829                 lfs df $inodes $dir
19830                 error "df and lfs df $1 output mismatch: "      \
19831                       "df ${inodes}: ${df_out[*]}, "            \
19832                       "lfs df ${inodes}: ${lfs_df_out[*]}"
19833         fi
19834 }
19835
19836 test_418() {
19837         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19838
19839         local dir=$DIR/$tdir
19840         local numfiles=$((RANDOM % 4096 + 2))
19841         local numblocks=$((RANDOM % 256 + 1))
19842
19843         wait_delete_completed
19844         test_mkdir $dir
19845
19846         # check block output
19847         check_lfs_df blocks $dir
19848         # check inode output
19849         check_lfs_df inodes $dir
19850
19851         # create a single file and retest
19852         echo "Creating a single file and testing"
19853         createmany -o $dir/$tfile- 1 &>/dev/null ||
19854                 error "creating 1 file in $dir failed"
19855         check_lfs_df blocks $dir
19856         check_lfs_df inodes $dir
19857
19858         # create a random number of files
19859         echo "Creating $((numfiles - 1)) files and testing"
19860         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
19861                 error "creating $((numfiles - 1)) files in $dir failed"
19862
19863         # write a random number of blocks to the first test file
19864         echo "Writing $numblocks 4K blocks and testing"
19865         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
19866                 count=$numblocks &>/dev/null ||
19867                 error "dd to $dir/${tfile}-0 failed"
19868
19869         # retest
19870         check_lfs_df blocks $dir
19871         check_lfs_df inodes $dir
19872
19873         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
19874                 error "unlinking $numfiles files in $dir failed"
19875 }
19876 run_test 418 "df and lfs df outputs match"
19877
19878 test_419()
19879 {
19880         local dir=$DIR/$tdir
19881
19882         mkdir -p $dir
19883         touch $dir/file
19884
19885         cancel_lru_locks mdc
19886
19887         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
19888         $LCTL set_param fail_loc=0x1410
19889         cat $dir/file
19890         $LCTL set_param fail_loc=0
19891         rm -rf $dir
19892 }
19893 run_test 419 "Verify open file by name doesn't crash kernel"
19894
19895 prep_801() {
19896         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
19897         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
19898                 skip "Need server version at least 2.9.55"
19899
19900         start_full_debug_logging
19901 }
19902
19903 post_801() {
19904         stop_full_debug_logging
19905 }
19906
19907 barrier_stat() {
19908         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
19909                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
19910                            awk '/The barrier for/ { print $7 }')
19911                 echo $st
19912         else
19913                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
19914                 echo \'$st\'
19915         fi
19916 }
19917
19918 barrier_expired() {
19919         local expired
19920
19921         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
19922                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
19923                           awk '/will be expired/ { print $7 }')
19924         else
19925                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
19926         fi
19927
19928         echo $expired
19929 }
19930
19931 test_801a() {
19932         prep_801
19933
19934         echo "Start barrier_freeze at: $(date)"
19935         #define OBD_FAIL_BARRIER_DELAY          0x2202
19936         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
19937         do_facet mgs $LCTL barrier_freeze $FSNAME 10 &
19938
19939         sleep 2
19940         local b_status=$(barrier_stat)
19941         echo "Got barrier status at: $(date)"
19942         [ "$b_status" = "'freezing_p1'" ] ||
19943                 error "(1) unexpected barrier status $b_status"
19944
19945         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
19946         wait
19947         b_status=$(barrier_stat)
19948         [ "$b_status" = "'frozen'" ] ||
19949                 error "(2) unexpected barrier status $b_status"
19950
19951         local expired=$(barrier_expired)
19952         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
19953         sleep $((expired + 3))
19954
19955         b_status=$(barrier_stat)
19956         [ "$b_status" = "'expired'" ] ||
19957                 error "(3) unexpected barrier status $b_status"
19958
19959         do_facet mgs $LCTL barrier_freeze $FSNAME 10 ||
19960                 error "(4) fail to freeze barrier"
19961
19962         b_status=$(barrier_stat)
19963         [ "$b_status" = "'frozen'" ] ||
19964                 error "(5) unexpected barrier status $b_status"
19965
19966         echo "Start barrier_thaw at: $(date)"
19967         #define OBD_FAIL_BARRIER_DELAY          0x2202
19968         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
19969         do_facet mgs $LCTL barrier_thaw $FSNAME &
19970
19971         sleep 2
19972         b_status=$(barrier_stat)
19973         echo "Got barrier status at: $(date)"
19974         [ "$b_status" = "'thawing'" ] ||
19975                 error "(6) unexpected barrier status $b_status"
19976
19977         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
19978         wait
19979         b_status=$(barrier_stat)
19980         [ "$b_status" = "'thawed'" ] ||
19981                 error "(7) unexpected barrier status $b_status"
19982
19983         #define OBD_FAIL_BARRIER_FAILURE        0x2203
19984         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
19985         do_facet mgs $LCTL barrier_freeze $FSNAME
19986
19987         b_status=$(barrier_stat)
19988         [ "$b_status" = "'failed'" ] ||
19989                 error "(8) unexpected barrier status $b_status"
19990
19991         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
19992         do_facet mgs $LCTL barrier_thaw $FSNAME
19993
19994         post_801
19995 }
19996 run_test 801a "write barrier user interfaces and stat machine"
19997
19998 test_801b() {
19999         prep_801
20000
20001         mkdir $DIR/$tdir || error "(1) fail to mkdir"
20002         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
20003         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
20004         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
20005         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
20006
20007         cancel_lru_locks mdc
20008
20009         # 180 seconds should be long enough
20010         do_facet mgs $LCTL barrier_freeze $FSNAME 180
20011
20012         local b_status=$(barrier_stat)
20013         [ "$b_status" = "'frozen'" ] ||
20014                 error "(6) unexpected barrier status $b_status"
20015
20016         mkdir $DIR/$tdir/d0/d10 &
20017         mkdir_pid=$!
20018
20019         touch $DIR/$tdir/d1/f13 &
20020         touch_pid=$!
20021
20022         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
20023         ln_pid=$!
20024
20025         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
20026         mv_pid=$!
20027
20028         rm -f $DIR/$tdir/d4/f12 &
20029         rm_pid=$!
20030
20031         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
20032
20033         # To guarantee taht the 'stat' is not blocked
20034         b_status=$(barrier_stat)
20035         [ "$b_status" = "'frozen'" ] ||
20036                 error "(8) unexpected barrier status $b_status"
20037
20038         # let above commands to run at background
20039         sleep 5
20040
20041         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
20042         ps -p $touch_pid || error "(10) touch should be blocked"
20043         ps -p $ln_pid || error "(11) link should be blocked"
20044         ps -p $mv_pid || error "(12) rename should be blocked"
20045         ps -p $rm_pid || error "(13) unlink should be blocked"
20046
20047         b_status=$(barrier_stat)
20048         [ "$b_status" = "'frozen'" ] ||
20049                 error "(14) unexpected barrier status $b_status"
20050
20051         do_facet mgs $LCTL barrier_thaw $FSNAME
20052         b_status=$(barrier_stat)
20053         [ "$b_status" = "'thawed'" ] ||
20054                 error "(15) unexpected barrier status $b_status"
20055
20056         wait $mkdir_pid || error "(16) mkdir should succeed"
20057         wait $touch_pid || error "(17) touch should succeed"
20058         wait $ln_pid || error "(18) link should succeed"
20059         wait $mv_pid || error "(19) rename should succeed"
20060         wait $rm_pid || error "(20) unlink should succeed"
20061
20062         post_801
20063 }
20064 run_test 801b "modification will be blocked by write barrier"
20065
20066 test_801c() {
20067         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
20068
20069         prep_801
20070
20071         stop mds2 || error "(1) Fail to stop mds2"
20072
20073         do_facet mgs $LCTL barrier_freeze $FSNAME 30
20074
20075         local b_status=$(barrier_stat)
20076         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
20077                 do_facet mgs $LCTL barrier_thaw $FSNAME
20078                 error "(2) unexpected barrier status $b_status"
20079         }
20080
20081         do_facet mgs $LCTL barrier_rescan $FSNAME ||
20082                 error "(3) Fail to rescan barrier bitmap"
20083
20084         do_facet mgs $LCTL barrier_freeze $FSNAME 10
20085
20086         b_status=$(barrier_stat)
20087         [ "$b_status" = "'frozen'" ] ||
20088                 error "(4) unexpected barrier status $b_status"
20089
20090         do_facet mgs $LCTL barrier_thaw $FSNAME
20091         b_status=$(barrier_stat)
20092         [ "$b_status" = "'thawed'" ] ||
20093                 error "(5) unexpected barrier status $b_status"
20094
20095         local devname=$(mdsdevname 2)
20096
20097         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
20098
20099         do_facet mgs $LCTL barrier_rescan $FSNAME ||
20100                 error "(7) Fail to rescan barrier bitmap"
20101
20102         post_801
20103 }
20104 run_test 801c "rescan barrier bitmap"
20105
20106 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
20107 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
20108 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
20109
20110 cleanup_802a() {
20111         trap 0
20112
20113         stopall
20114         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
20115         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
20116         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
20117         setupall
20118 }
20119
20120 test_802a() {
20121
20122         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
20123         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
20124                 skip "Need server version at least 2.9.55"
20125
20126         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
20127
20128         mkdir $DIR/$tdir || error "(1) fail to mkdir"
20129
20130         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
20131                 error "(2) Fail to copy"
20132
20133         trap cleanup_802a EXIT
20134
20135         # sync by force before remount as readonly
20136         sync; sync_all_data; sleep 3; sync_all_data
20137
20138         stopall
20139
20140         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
20141         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
20142         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
20143
20144         echo "Mount the server as read only"
20145         setupall server_only || error "(3) Fail to start servers"
20146
20147         echo "Mount client without ro should fail"
20148         mount_client $MOUNT &&
20149                 error "(4) Mount client without 'ro' should fail"
20150
20151         echo "Mount client with ro should succeed"
20152         mount_client $MOUNT ro ||
20153                 error "(5) Mount client with 'ro' should succeed"
20154
20155         echo "Modify should be refused"
20156         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
20157
20158         echo "Read should be allowed"
20159         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
20160                 error "(7) Read should succeed under ro mode"
20161
20162         cleanup_802a
20163 }
20164 run_test 802a "simulate readonly device"
20165
20166 test_802b() {
20167         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20168         remote_mds_nodsh && skip "remote MDS with nodsh"
20169
20170         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
20171                 skip "readonly option not available"
20172
20173         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
20174
20175         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
20176                 error "(2) Fail to copy"
20177
20178         # write back all cached data before setting MDT to readonly
20179         cancel_lru_locks
20180         sync_all_data
20181
20182         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
20183         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
20184
20185         echo "Modify should be refused"
20186         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
20187
20188         echo "Read should be allowed"
20189         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
20190                 error "(7) Read should succeed under ro mode"
20191
20192         # disable readonly
20193         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
20194 }
20195 run_test 802b "be able to set MDTs to readonly"
20196
20197 test_803() {
20198         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
20199         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
20200                 skip "MDS needs to be newer than 2.10.54"
20201
20202         mkdir -p $DIR/$tdir
20203         # Create some objects on all MDTs to trigger related logs objects
20204         for idx in $(seq $MDSCOUNT); do
20205                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
20206                         $DIR/$tdir/dir${idx} ||
20207                         error "Fail to create $DIR/$tdir/dir${idx}"
20208         done
20209
20210         sync; sleep 3
20211         wait_delete_completed # ensure old test cleanups are finished
20212         echo "before create:"
20213         $LFS df -i $MOUNT
20214         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
20215
20216         for i in {1..10}; do
20217                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
20218                         error "Fail to create $DIR/$tdir/foo$i"
20219         done
20220
20221         sync; sleep 3
20222         echo "after create:"
20223         $LFS df -i $MOUNT
20224         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
20225
20226         # allow for an llog to be cleaned up during the test
20227         [ $after_used -ge $((before_used + 10 - 1)) ] ||
20228                 error "before ($before_used) + 10 > after ($after_used)"
20229
20230         for i in {1..10}; do
20231                 rm -rf $DIR/$tdir/foo$i ||
20232                         error "Fail to remove $DIR/$tdir/foo$i"
20233         done
20234
20235         sleep 3 # avoid MDT return cached statfs
20236         wait_delete_completed
20237         echo "after unlink:"
20238         $LFS df -i $MOUNT
20239         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
20240
20241         # allow for an llog to be created during the test
20242         [ $after_used -le $((before_used + 1)) ] ||
20243                 error "after ($after_used) > before ($before_used) + 1"
20244 }
20245 run_test 803 "verify agent object for remote object"
20246
20247 test_804() {
20248         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
20249         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
20250                 skip "MDS needs to be newer than 2.10.54"
20251         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
20252
20253         mkdir -p $DIR/$tdir
20254         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
20255                 error "Fail to create $DIR/$tdir/dir0"
20256
20257         local fid=$($LFS path2fid $DIR/$tdir/dir0)
20258         local dev=$(mdsdevname 2)
20259
20260         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
20261                 grep ${fid} || error "NOT found agent entry for dir0"
20262
20263         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
20264                 error "Fail to create $DIR/$tdir/dir1"
20265
20266         touch $DIR/$tdir/dir1/foo0 ||
20267                 error "Fail to create $DIR/$tdir/dir1/foo0"
20268         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
20269         local rc=0
20270
20271         for idx in $(seq $MDSCOUNT); do
20272                 dev=$(mdsdevname $idx)
20273                 do_facet mds${idx} \
20274                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
20275                         grep ${fid} && rc=$idx
20276         done
20277
20278         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
20279                 error "Fail to rename foo0 to foo1"
20280         if [ $rc -eq 0 ]; then
20281                 for idx in $(seq $MDSCOUNT); do
20282                         dev=$(mdsdevname $idx)
20283                         do_facet mds${idx} \
20284                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
20285                         grep ${fid} && rc=$idx
20286                 done
20287         fi
20288
20289         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
20290                 error "Fail to rename foo1 to foo2"
20291         if [ $rc -eq 0 ]; then
20292                 for idx in $(seq $MDSCOUNT); do
20293                         dev=$(mdsdevname $idx)
20294                         do_facet mds${idx} \
20295                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
20296                         grep ${fid} && rc=$idx
20297                 done
20298         fi
20299
20300         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
20301
20302         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
20303                 error "Fail to link to $DIR/$tdir/dir1/foo2"
20304         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
20305                 error "Fail to rename foo2 to foo0"
20306         unlink $DIR/$tdir/dir1/foo0 ||
20307                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
20308         rm -rf $DIR/$tdir/dir0 ||
20309                 error "Fail to rm $DIR/$tdir/dir0"
20310
20311         for idx in $(seq $MDSCOUNT); do
20312                 dev=$(mdsdevname $idx)
20313                 rc=0
20314
20315                 stop mds${idx}
20316                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
20317                         rc=$?
20318                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
20319                         error "mount mds$idx failed"
20320                 df $MOUNT > /dev/null 2>&1
20321
20322                 # e2fsck should not return error
20323                 [ $rc -eq 0 ] ||
20324                         error "e2fsck detected error on MDT${idx}: rc=$rc"
20325         done
20326 }
20327 run_test 804 "verify agent entry for remote entry"
20328
20329 cleanup_805() {
20330         do_facet $SINGLEMDS zfs set quota=$old $fsset
20331         unlinkmany $DIR/$tdir/f- 1000000
20332         trap 0
20333 }
20334
20335 test_805() {
20336         local zfs_version=$(do_node $SINGLEMDS cat /sys/module/zfs/version)
20337         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
20338         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
20339                 skip "netfree not implemented before 0.7"
20340         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
20341                 skip "Need MDS version at least 2.10.57"
20342
20343         local fsset
20344         local freekb
20345         local usedkb
20346         local old
20347         local quota
20348         local pref="osd-zfs.lustre-MDT0000."
20349
20350         # limit available space on MDS dataset to meet nospace issue
20351         # quickly. then ZFS 0.7.2 can use reserved space if asked
20352         # properly (using netfree flag in osd_declare_destroy()
20353         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
20354         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
20355                 gawk '{print $3}')
20356         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
20357         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
20358         let "usedkb=usedkb-freekb"
20359         let "freekb=freekb/2"
20360         if let "freekb > 5000"; then
20361                 let "freekb=5000"
20362         fi
20363         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
20364         trap cleanup_805 EXIT
20365         mkdir $DIR/$tdir
20366         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
20367         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
20368         rm -rf $DIR/$tdir || error "not able to remove"
20369         do_facet $SINGLEMDS zfs set quota=$old $fsset
20370         trap 0
20371 }
20372 run_test 805 "ZFS can remove from full fs"
20373
20374 # Size-on-MDS test
20375 check_lsom_data()
20376 {
20377         local file=$1
20378         local size=$($LFS getsom -s $file)
20379         local expect=$(stat -c %s $file)
20380
20381         [[ $size == $expect ]] ||
20382                 error "$file expected size: $expect, got: $size"
20383
20384         local blocks=$($LFS getsom -b $file)
20385         expect=$(stat -c %b $file)
20386         [[ $blocks == $expect ]] ||
20387                 error "$file expected blocks: $expect, got: $blocks"
20388 }
20389
20390 check_lsom_size()
20391 {
20392         local size=$($LFS getsom -s $1)
20393         local expect=$2
20394
20395         [[ $size == $expect ]] ||
20396                 error "$file expected size: $expect, got: $size"
20397 }
20398
20399 test_806() {
20400         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20401                 skip "Need MDS version at least 2.11.52"
20402
20403         local bs=1048576
20404
20405         touch $DIR/$tfile || error "touch $tfile failed"
20406
20407         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
20408         save_lustre_params client "llite.*.xattr_cache" > $save
20409         lctl set_param llite.*.xattr_cache=0
20410         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
20411
20412         # single-threaded write
20413         echo "Test SOM for single-threaded write"
20414         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
20415                 error "write $tfile failed"
20416         check_lsom_size $DIR/$tfile $bs
20417
20418         local num=32
20419         local size=$(($num * $bs))
20420         local offset=0
20421         local i
20422
20423         echo "Test SOM for single client multi-threaded($num) write"
20424         $TRUNCATE $DIR/$tfile 0
20425         for ((i = 0; i < $num; i++)); do
20426                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
20427                 local pids[$i]=$!
20428                 offset=$((offset + $bs))
20429         done
20430         for (( i=0; i < $num; i++ )); do
20431                 wait ${pids[$i]}
20432         done
20433         check_lsom_size $DIR/$tfile $size
20434
20435         $TRUNCATE $DIR/$tfile 0
20436         for ((i = 0; i < $num; i++)); do
20437                 offset=$((offset - $bs))
20438                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
20439                 local pids[$i]=$!
20440         done
20441         for (( i=0; i < $num; i++ )); do
20442                 wait ${pids[$i]}
20443         done
20444         check_lsom_size $DIR/$tfile $size
20445
20446         # multi-client wirtes
20447         num=$(get_node_count ${CLIENTS//,/ })
20448         size=$(($num * $bs))
20449         offset=0
20450         i=0
20451
20452         echo "Test SOM for multi-client ($num) writes"
20453         $TRUNCATE $DIR/$tfile 0
20454         for client in ${CLIENTS//,/ }; do
20455                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
20456                 local pids[$i]=$!
20457                 i=$((i + 1))
20458                 offset=$((offset + $bs))
20459         done
20460         for (( i=0; i < $num; i++ )); do
20461                 wait ${pids[$i]}
20462         done
20463         check_lsom_size $DIR/$tfile $offset
20464
20465         i=0
20466         $TRUNCATE $DIR/$tfile 0
20467         for client in ${CLIENTS//,/ }; do
20468                 offset=$((offset - $bs))
20469                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
20470                 local pids[$i]=$!
20471                 i=$((i + 1))
20472         done
20473         for (( i=0; i < $num; i++ )); do
20474                 wait ${pids[$i]}
20475         done
20476         check_lsom_size $DIR/$tfile $size
20477
20478         # verify truncate
20479         echo "Test SOM for truncate"
20480         $TRUNCATE $DIR/$tfile 1048576
20481         check_lsom_size $DIR/$tfile 1048576
20482         $TRUNCATE $DIR/$tfile 1234
20483         check_lsom_size $DIR/$tfile 1234
20484
20485         # verify SOM blocks count
20486         echo "Verify SOM block count"
20487         $TRUNCATE $DIR/$tfile 0
20488         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
20489                 error "failed to write file $tfile"
20490         check_lsom_data $DIR/$tfile
20491 }
20492 run_test 806 "Verify Lazy Size on MDS"
20493
20494 test_807() {
20495         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
20496         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20497                 skip "Need MDS version at least 2.11.52"
20498
20499         # Registration step
20500         changelog_register || error "changelog_register failed"
20501         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
20502         changelog_users $SINGLEMDS | grep -q $cl_user ||
20503                 error "User $cl_user not found in changelog_users"
20504
20505         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
20506         save_lustre_params client "llite.*.xattr_cache" > $save
20507         lctl set_param llite.*.xattr_cache=0
20508         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
20509
20510         rm -rf $DIR/$tdir || error "rm $tdir failed"
20511         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
20512         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
20513         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
20514         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
20515                 error "truncate $tdir/trunc failed"
20516
20517         local bs=1048576
20518         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 ||
20519                 error "write $tfile failed"
20520
20521         # multi-client wirtes
20522         local num=$(get_node_count ${CLIENTS//,/ })
20523         local offset=0
20524         local i=0
20525
20526         echo "Test SOM for multi-client ($num) writes"
20527         touch $DIR/$tfile || error "touch $tfile failed"
20528         $TRUNCATE $DIR/$tfile 0
20529         for client in ${CLIENTS//,/ }; do
20530                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
20531                 local pids[$i]=$!
20532                 i=$((i + 1))
20533                 offset=$((offset + $bs))
20534         done
20535         for (( i=0; i < $num; i++ )); do
20536                 wait ${pids[$i]}
20537         done
20538
20539         sleep 5
20540         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
20541         check_lsom_data $DIR/$tdir/trunc
20542         check_lsom_data $DIR/$tdir/single_dd
20543         check_lsom_data $DIR/$tfile
20544
20545         rm -rf $DIR/$tdir
20546         # Deregistration step
20547         changelog_deregister || error "changelog_deregister failed"
20548 }
20549 run_test 807 "verify LSOM syncing tool"
20550
20551 check_som_nologged()
20552 {
20553         local lines=$($LFS changelog $FSNAME-MDT0000 |
20554                 grep 'x=trusted.som' | wc -l)
20555         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
20556 }
20557
20558 test_808() {
20559         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
20560                 skip "Need MDS version at least 2.11.55"
20561
20562         # Registration step
20563         changelog_register || error "changelog_register failed"
20564
20565         touch $DIR/$tfile || error "touch $tfile failed"
20566         check_som_nologged
20567
20568         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
20569                 error "write $tfile failed"
20570         check_som_nologged
20571
20572         $TRUNCATE $DIR/$tfile 1234
20573         check_som_nologged
20574
20575         $TRUNCATE $DIR/$tfile 1048576
20576         check_som_nologged
20577
20578         # Deregistration step
20579         changelog_deregister || error "changelog_deregister failed"
20580 }
20581 run_test 808 "Check trusted.som xattr not logged in Changelogs"
20582
20583 check_som_nodata()
20584 {
20585         $LFS getsom $1
20586         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
20587 }
20588
20589 test_809() {
20590         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
20591                 skip "Need MDS version at least 2.11.56"
20592
20593         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
20594                 error "failed to create DoM-only file $DIR/$tfile"
20595         touch $DIR/$tfile || error "touch $tfile failed"
20596         check_som_nodata $DIR/$tfile
20597
20598         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
20599                 error "write $tfile failed"
20600         check_som_nodata $DIR/$tfile
20601
20602         $TRUNCATE $DIR/$tfile 1234
20603         check_som_nodata $DIR/$tfile
20604
20605         $TRUNCATE $DIR/$tfile 4097
20606         check_som_nodata $DIR/$file
20607 }
20608 run_test 809 "Verify no SOM xattr store for DoM-only files"
20609
20610 test_810() {
20611         local ORIG
20612         local CSUM
20613
20614         # t10 seem to dislike partial pages
20615         lctl set_param osc.*.checksum_type=adler
20616         lctl set_param fail_loc=0x411
20617         dd if=/dev/urandom of=$DIR/$tfile bs=10240 count=2
20618         ORIG=$(md5sum $DIR/$tfile)
20619         lctl set_param ldlm.namespaces.*osc*.lru_size=clear
20620         CSUM=$(md5sum $DIR/$tfile)
20621         set_checksum_type adler
20622         if [ "$ORIG" != "$CSUM" ]; then
20623                 error "$ORIG != $CSUM"
20624         fi
20625 }
20626 run_test 810 "partial page writes on ZFS (LU-11663)"
20627
20628 test_811() {
20629         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.56) ] &&
20630                 skip "Need MDS version at least 2.11.56"
20631
20632         #define OBD_FAIL_MDS_ORPHAN_DELETE      0x165
20633         do_facet mds1 $LCTL set_param fail_loc=0x165
20634         $MULTIOP $DIR/$tfile Ouc || error "multiop failed"
20635
20636         stop mds1
20637         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
20638
20639         sleep 5
20640         [[ $(do_facet mds1 pgrep orph_.*-MDD | wc -l) -eq 0 ]] ||
20641                 error "MDD orphan cleanup thread not quit"
20642 }
20643 run_test 811 "orphan name stub can be cleaned up in startup"
20644
20645 test_812() {
20646         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
20647                 skip "OST < 2.12.51 doesn't support this fail_loc"
20648
20649         $LFS setstripe -c 1 -i 0 $DIR/$tfile
20650         # ensure ost1 is connected
20651         stat $DIR/$tfile >/dev/null || error "can't stat"
20652         wait_osc_import_state client ost1 FULL
20653         # no locks, no reqs to let the connection idle
20654         cancel_lru_locks osc
20655
20656         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
20657 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
20658         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
20659         wait_osc_import_state client ost1 CONNECTING
20660         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
20661
20662         stat $DIR/$tfile >/dev/null || error "can't stat file"
20663 }
20664 run_test 812 "do not drop reqs generated when imp is going to idle (LU-11951)"
20665
20666 test_813() {
20667         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
20668         [ -z "$file_heat_sav" ] && skip "no file heat support"
20669
20670         local readsample
20671         local writesample
20672         local readbyte
20673         local writebyte
20674         local readsample1
20675         local writesample1
20676         local readbyte1
20677         local writebyte1
20678
20679         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
20680         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
20681
20682         $LCTL set_param -n llite.*.file_heat=1
20683         echo "Turn on file heat"
20684         echo "Period second: $period_second, Decay percentage: $decay_pct"
20685
20686         echo "QQQQ" > $DIR/$tfile
20687         echo "QQQQ" > $DIR/$tfile
20688         echo "QQQQ" > $DIR/$tfile
20689         cat $DIR/$tfile > /dev/null
20690         cat $DIR/$tfile > /dev/null
20691         cat $DIR/$tfile > /dev/null
20692         cat $DIR/$tfile > /dev/null
20693
20694         local out=$($LFS heat_get $DIR/$tfile)
20695
20696         $LFS heat_get $DIR/$tfile
20697         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
20698         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
20699         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
20700         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
20701
20702         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
20703         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
20704         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
20705         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
20706
20707         sleep $((period_second + 3))
20708         echo "Sleep $((period_second + 3)) seconds..."
20709         # The recursion formula to calculate the heat of the file f is as
20710         # follow:
20711         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
20712         # Where Hi is the heat value in the period between time points i*I and
20713         # (i+1)*I; Ci is the access count in the period; the symbol P refers
20714         # to the weight of Ci.
20715         out=$($LFS heat_get $DIR/$tfile)
20716         $LFS heat_get $DIR/$tfile
20717         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
20718         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
20719         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
20720         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
20721
20722         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
20723                 error "read sample ($readsample) is wrong"
20724         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
20725                 error "write sample ($writesample) is wrong"
20726         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
20727                 error "read bytes ($readbyte) is wrong"
20728         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
20729                 error "write bytes ($writebyte) is wrong"
20730
20731         echo "QQQQ" > $DIR/$tfile
20732         echo "QQQQ" > $DIR/$tfile
20733         echo "QQQQ" > $DIR/$tfile
20734         cat $DIR/$tfile > /dev/null
20735         cat $DIR/$tfile > /dev/null
20736         cat $DIR/$tfile > /dev/null
20737         cat $DIR/$tfile > /dev/null
20738
20739         sleep $((period_second + 3))
20740         echo "Sleep $((period_second + 3)) seconds..."
20741
20742         out=$($LFS heat_get $DIR/$tfile)
20743         $LFS heat_get $DIR/$tfile
20744         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
20745         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
20746         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
20747         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
20748
20749         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
20750                 4 * $decay_pct) / 100") -eq 1 ] ||
20751                 error "read sample ($readsample1) is wrong"
20752         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
20753                 3 * $decay_pct) / 100") -eq 1 ] ||
20754                 error "write sample ($writesample1) is wrong"
20755         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
20756                 20 * $decay_pct) / 100") -eq 1 ] ||
20757                 error "read bytes ($readbyte1) is wrong"
20758         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
20759                 15 * $decay_pct) / 100") -eq 1 ] ||
20760                 error "write bytes ($writebyte1) is wrong"
20761
20762         echo "Turn off file heat for the file $DIR/$tfile"
20763         $LFS heat_set -o $DIR/$tfile
20764
20765         echo "QQQQ" > $DIR/$tfile
20766         echo "QQQQ" > $DIR/$tfile
20767         echo "QQQQ" > $DIR/$tfile
20768         cat $DIR/$tfile > /dev/null
20769         cat $DIR/$tfile > /dev/null
20770         cat $DIR/$tfile > /dev/null
20771         cat $DIR/$tfile > /dev/null
20772
20773         out=$($LFS heat_get $DIR/$tfile)
20774         $LFS heat_get $DIR/$tfile
20775         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
20776         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
20777         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
20778         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
20779
20780         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
20781         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
20782         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
20783         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
20784
20785         echo "Trun on file heat for the file $DIR/$tfile"
20786         $LFS heat_set -O $DIR/$tfile
20787
20788         echo "QQQQ" > $DIR/$tfile
20789         echo "QQQQ" > $DIR/$tfile
20790         echo "QQQQ" > $DIR/$tfile
20791         cat $DIR/$tfile > /dev/null
20792         cat $DIR/$tfile > /dev/null
20793         cat $DIR/$tfile > /dev/null
20794         cat $DIR/$tfile > /dev/null
20795
20796         out=$($LFS heat_get $DIR/$tfile)
20797         $LFS heat_get $DIR/$tfile
20798         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
20799         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
20800         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
20801         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
20802
20803         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
20804         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
20805         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
20806         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
20807
20808         $LFS heat_set -c $DIR/$tfile
20809         $LCTL set_param -n llite.*.file_heat=0
20810         echo "Turn off file heat support for the Lustre filesystem"
20811
20812         echo "QQQQ" > $DIR/$tfile
20813         echo "QQQQ" > $DIR/$tfile
20814         echo "QQQQ" > $DIR/$tfile
20815         cat $DIR/$tfile > /dev/null
20816         cat $DIR/$tfile > /dev/null
20817         cat $DIR/$tfile > /dev/null
20818         cat $DIR/$tfile > /dev/null
20819
20820         out=$($LFS heat_get $DIR/$tfile)
20821         $LFS heat_get $DIR/$tfile
20822         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
20823         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
20824         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
20825         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
20826
20827         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
20828         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
20829         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
20830         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
20831
20832         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
20833         rm -f $DIR/$tfile
20834 }
20835 run_test 813 "File heat verfication"
20836
20837 #
20838 # tests that do cleanup/setup should be run at the end
20839 #
20840
20841 test_900() {
20842         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20843         local ls
20844
20845         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
20846         $LCTL set_param fail_loc=0x903
20847
20848         cancel_lru_locks MGC
20849
20850         FAIL_ON_ERROR=true cleanup
20851         FAIL_ON_ERROR=true setup
20852 }
20853 run_test 900 "umount should not race with any mgc requeue thread"
20854
20855 complete $SECONDS
20856 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
20857 check_and_cleanup_lustre
20858 if [ "$I_MOUNTED" != "yes" ]; then
20859         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
20860 fi
20861 exit_status